2 # dgit repos policy hook script for Debian
5 $SIG{__WARN__} = sub { die $_[0]; };
9 use File::Temp qw(tempfile);
14 use Debian::Dgit qw(:DEFAULT :policyflags);
15 use Debian::Dgit::Policy::Debian;
18 enabledebuglevel $ENV{'DGIT_DRS_DEBUG'};
20 our $distro = shift @ARGV // die "need DISTRO";
21 our $repos = shift @ARGV // die "need DGIT-REPOS-DIR";
22 our $dgitlive = shift @ARGV // die "need DGIT-LIVE-DIR";
23 our $distrodir = shift @ARGV // die "need DISTRO-DIR";
24 our $action = shift @ARGV // die "need ACTION";
26 our $publicmode = 02775;
27 our $new_upload_propagation_slop = 3600*4 + 100;# fixme config;
32 our ($pkg_exists,$pkg_secret);
36 our ($version,$suite,$tagname);
39 # We assume that it is not possible for NEW to have a version older
42 # Whenever pushing, we check for
43 # source-package-local tainted history
44 # global tainted history
45 # can be overridden by --deliberately except for an admin prohib taint
47 # ALL of the following apply only if history is secret:
49 # if NEW has no version, or a version which is not in our history[1]
52 # if any suite's version is in our history[1], publish our history
53 # otherwise discard our history,
54 # tainting --deliberately-include-questionable-history
56 # if NEW has a version which is in our history[1]
58 # require explicit specification of one of
59 # --deliberately-include-questionable-history
60 # --deliberately-not-fast-forward
61 # (latter will taint old NEW version --d-i-q-h)
65 # [1] looking for the relevant git tag for the version number and not
66 # caring what that tag refers to.
68 # A wrinkle: if we approved a push recently, we treat NEW as having
69 # a version which is in our history. This is because the package may
70 # still be being uploaded. (We record this using the timestamp of the
71 # package's git repo directory.)
73 # We aim for the following invariants and properties:
75 # - .dsc of published dgit package will have corresponding publicly
76 # visible dgit-repo (soon)
78 # - when a new package is rejected we help maintainer avoid
79 # accidentally including bad objects in published dgit history
81 # - .dsc of NEW dgit package has corresponding dgit-repo but not
87 my $cmd = "$dgitlive/dgit -d$distro \$DGIT_TEST_OPTS";
88 $cmd .= " -".("D" x $debuglevel) if $debuglevel;
89 $cmd .= " archive-api-query $subpath";
90 printdebug "apiquery $cmd\n";
91 $!=0; $?=0; my $json = `$cmd`;
92 defined $json && !$? or die "$subpath $! $?";
93 my $r = decode_json $json;
94 my $d = new Data::Dumper([$r], [qw(r)]);
95 printdebug "apiquery $subpath | ", $d->Dump() if $debuglevel>=2;
99 sub specific_suite_has_vsn_in_our_history ($) {
101 my $in_suite = apiquery "/dsc_in_suite/$suite/$pkg";
102 foreach my $entry (@$in_suite) {
103 my $vsn = $entry->{version};
104 die "$pkg ?" unless defined $vsn;
105 my $tag = debiantag $vsn;
106 $?=0; my $r = system qw(git show-ref --verify --quiet), $tag;
109 die "$pkg tag $tag $? $!";
114 sub new_has_vsn_in_our_history () {
115 return specific_suite_has_vsn_in_our_history('new');
118 sub good_suite_has_vsn_in_our_history () {
119 my $suites = apiquery "/suites";
120 foreach my $suitei (@$suites) {
121 my $suite = $suitei->{name};
122 die unless defined $suite;
123 next if $suite =~ m/\bnew$/;
124 return 1 if specific_suite_has_vsn_in_our_history($suite);
130 $pkgdir = "$repos/$pkg.git";
131 if (!stat_exists $pkgdir) {
132 printdebug "statpackage $pkg => ENOENT\n";
136 $pkg_secret = !!(~(stat _)[2] & 05);
137 printdebug "statpackage $pkg => exists, secret=$pkg_secret.\n";
142 die unless @ARGV >= 1;
144 die unless $pkg =~ m/^$package_re$/;
150 my ($refobj, $reason) = @_;
152 printdebug "TAINTING $refobj\n",
153 (map { "\%| $_" } split "\n", $reason),
156 my $tf = new File::Temp or die $!;
157 print $tf "$refobj^0\n" or die $!;
159 seek $tf,0,0 or die $!;
161 my $gcfpid = open GCF, "-|";
162 defined $gcfpid or die $!;
164 open STDIN, "<&", $tf or die $!;
165 exec 'git', 'cat-file', '--batch';
172 m/^(\w+) (\w+) (\d+)\n/ or die "$_ ?";
178 if ($gitobjtype eq 'commit' or $gitobjtype eq 'tag') {
179 $!=0; read GCF, $gitobjdata, $bytes == $bytes
180 or die "$gitobjid $bytes $!";
184 $poldbh->do("INSERT INTO taints".
185 " (package, gitobjid, gitobjtype, gitobjdata, time, comment)".
186 " VALUES (?,?,?,?,?,?)", {},
187 $pkg, $gitobjid, $gitobjtype, $gitobjdata, time, $reason);
189 my $taint_id = $poldbh->last_insert_id(undef,undef,"taints","taint_id");
190 die unless defined $taint_id;
192 $poldbh->do("INSERT INTO taintoverrides".
193 " (taint_id, deliberately)".
194 " VALUES (?, '--deliberately-include-questionable-history')",
198 sub add_taint_by_tag ($$) {
199 my ($tagname,$refobjid) = @_;
201 "tag $tagname referred to this object in git tree but all".
202 " previously pushed versions were found to have been".
203 " removed from NEW (ie, rejected) (or never arrived)");
206 sub action_check_package () {
208 return 0 unless $pkg_exists;
209 return 0 unless $pkg_secret;
211 printdebug "check_package\n";
213 chdir $pkgdir or die "$pkgdir $!";
215 stat '.' or die "$pkgdir $!";
216 my $mtime = ((stat _)[9]);
217 my $age = time - $mtime;
218 printdebug "check_package age=$age\n";
220 return 0 if $age < $new_upload_propagation_slop;
222 return 0 if new_has_vsn_in_our_history();
224 if (good_suite_has_vsn_in_our_history) {
225 chmod $publicmode, "." or die $!;
229 printdebug "check_package secret, deleted, tainting\n";
231 git_for_each_ref('refs/tags', sub {
232 my ($objid,$objtype,$fullrefname,$tagname) = @_;
233 add_taint_by_tag($tagname,$objid);
240 die unless @ARGV >= 4;
241 $version = shift @ARGV;
242 $suite = shift @ARGV;
243 $tagname = shift @ARGV;
244 my $delibs = shift @ARGV;
245 foreach my $delib (split /\,/, $delibs) {
246 $deliberately{$delib} = 1;
250 sub deliberately ($) { return $deliberately{$_[0]}; }
254 return 0 unless $pkg_exists;
255 return 0 unless $pkg_secret;
257 # we suppose that NEW has a version which is already in our
258 # history, as otherwise the repo would have been blown away
260 if (deliberately('not-fast-forward')) {
261 add_taint(server_ref($suite),
262 "suite $suite when --deliberately-not-fast-forward".
263 " specified in signed tag $tagname for upload of".
264 " version $version into suite $suite");
265 return NOFFCHECK|FRESHREPO;
267 if (deliberately('include-questionable-history')) {
270 die "Package is in NEW and has not been accepted or rejected yet;".
271 " use a --deliberately option to specify whether you are".
272 " keeping or discarding the previously pushed history. ".
273 " Please RTFM dgit(1).\n";
276 sub action_push_confirm () {
278 die unless @ARGV >= 5;
279 my $freshrepo = $ARGV[4];
281 my $initq = $poldbh->prepare(<<END);
282 SELECT taint_id, gitobjid FROM taints t
283 WHERE (package = ? OR package = '')
285 $initq->execute($pkg);
288 my $chkinput = tempfile();
289 while (my $taint = $initq->fetchrow_hashref()) {
290 push @taintids, $taint->{taint_id};
291 print $chkinput $taint->{gitobjid}, "\n" or die $!;
293 flush $chkinput or die $!;
294 seek $chkinput,0,0 or die $!;
296 my $checkpid = open CHKOUT, "-|" // die $!;
298 open STDIN, "<&", $chkinput or die $!;
299 exec qw(git cat-file --batch) or die $!;
302 my ($taintinfoq,$overridesanyq,$untaintq,$overridesq);
304 my $overridesstmt = <<END;
305 SELECT deliberately FROM taintoverrides WHERE (
308 my @overridesv = sort keys %deliberately;
309 $overridesstmt .= <<END foreach @overridesv;
312 $overridesstmt .= <<END;
314 ORDER BY deliberately ASC
319 while (my $taintid = shift @taintids) {
320 # git cat-file prints a spurious newline after it gets EOF
321 # This is not documented. I guess it might go away. So we
322 # just read what we expect and then let it get SIGPIPE.
324 die "$? $!" unless defined $_;
326 next if m/^\w+ missing$/;
327 die unless m/^(\w+) (\w+) (\d+)\s/;
328 my ($objid,$objtype,$nbytes) = ($1,$2,$3);
331 (read CHKOUT, $drop, $nbytes) == $nbytes or die;
333 $taintinfoq ||= $poldbh->prepare(<<END);
334 SELECT package, time, comment FROM taints WHERE taint_id = ?
336 $taintinfoq->execute($taintid);
338 my $ti = $taintinfoq->fetchrow_hashref();
341 my $timeshow = defined $ti->{time}
342 ? " at time ".strftime("%Y-%m-%d %H:%M:%S Z", gmtime $ti->{time})
344 my $pkgshow = length $ti->{package}
345 ? "package $ti->{package}"
350 History contains tainted $objtype $objid
351 Taint recorded$timeshow for $pkgshow
352 Reason: $ti->{comment}
355 printdebug "SQL overrides: @overridesv $taintid /\n$overridesstmt\n";
357 $overridesq ||= $poldbh->prepare($overridesstmt);
358 $overridesq->execute(@overridesv, $taintid);
359 my ($ovwhy) = $overridesq->fetchrow_array();
360 if (!defined $ovwhy) {
361 $overridesanyq ||= $poldbh->prepare(<<END);
362 SELECT 1 FROM taintoverrides WHERE taint_id = ? LIMIT 1
364 $overridesanyq->execute($taintid);
365 my ($ovany) = $overridesanyq->fetchrow_array();
366 $stderr .= $ovany ? <<END : <<END;
367 Could be forced using --deliberately. Consult documentation.
369 Uncorrectable error. If confused, consult administrator.
374 Forcing due to --deliberately-$ovwhy
376 $untaintq ||= $poldbh->prepare(<<END);
377 DELETE FROM taints WHERE taint_id = ?
379 $untaintq->execute($taintid);
387 Rejecting push due to questionable history.
392 if (length $freshrepo) {
393 if (!good_suite_has_vsn_in_our_history()) {
394 stat $freshrepo or die "$freshrepo $!";
395 my $oldmode = ((stat _)[2]);
396 my $oldwrites = $oldmode & 0222;
397 # remove r and x bits which have corresponding w bits clear
398 my $newmode = $oldmode &
399 (~0555 | ($oldwrites << 1) | ($oldwrites >> 1));
400 printdebug sprintf "chmod %#o (was %#o) %s\n",
401 $newmode, $oldmode, $freshrepo;
402 chmod $newmode, $freshrepo or die $!;
403 utime undef, undef, $freshrepo or die $!;
410 sub action_check_list () {
411 opendir L, "$repos" or die "$repos $!";
412 while (defined (my $dent = readdir L)) {
413 next unless $dent =~ m/^($package_re)\.git$/;
416 next unless $pkg_exists;
417 next unless $pkg_secret;
418 print "$pkg\n" or die $!;
420 closedir L or die $!;
421 close STDOUT or die $!;
426 my $fn = ${*::}{"action_$action"};
428 printdebug "dgit-repos-policy-debian: unknown action $action\n";
436 poldb_setup(poldb_path($repos));
440 die unless defined $rcode;
442 eval { $poldbh->commit; };
443 last unless length $@;
445 die if $sleepy >= 20;
446 print STDERR "[policy database busy, retrying]\n";
452 print STDERR $stderr;