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 END { $? = 127; } # deliberate exit uses _exit
22 our $distro = shift @ARGV // die "need DISTRO";
23 our $repos = shift @ARGV // die "need DGIT-REPOS-DIR";
24 our $dgitlive = shift @ARGV // die "need DGIT-LIVE-DIR";
25 our $distrodir = shift @ARGV // die "need DISTRO-DIR";
26 our $action = shift @ARGV // die "need ACTION";
28 our $publicmode = 02775;
29 our $new_upload_propagation_slop = 3600*4 + 100;# fixme config;
34 our ($pkg_exists,$pkg_secret);
38 our ($version,$suite,$tagname);
41 # We assume that it is not possible for NEW to have a version older
44 # Whenever pushing, we check for
45 # source-package-local tainted history
46 # global tainted history
47 # can be overridden by --deliberately except for an admin prohib taint
49 # ALL of the following apply only if history is secret:
51 # if NEW has no version, or a version which is not in our history[1]
54 # if any suite's version is in our history[1], publish our history
55 # otherwise discard our history,
56 # tainting --deliberately-include-questionable-history
58 # if NEW has a version which is in our history[1]
60 # require explicit specification of one of
61 # --deliberately-include-questionable-history
62 # --deliberately-not-fast-forward
63 # (latter will taint old NEW version --d-i-q-h)
67 # [1] looking for the relevant git tag for the version number and not
68 # caring what that tag refers to.
70 # When we are doing a push to a fresh repo, any version will do: in
71 # this case, this is the first dgit upload of an existing package,
72 # and we trust that the uploader hasn't included in their git
73 # history any previous non-dgit uploads.
75 # A wrinkle: if we approved a push recently, we treat NEW as having
76 # a version which is in our history. This is because the package may
77 # still be being uploaded. (We record this using the timestamp of the
78 # package's git repo directory.)
80 # We aim for the following invariants and properties:
82 # - .dsc of published dgit package will have corresponding publicly
83 # visible dgit-repo (soon)
85 # - when a new package is rejected we help maintainer avoid
86 # accidentally including bad objects in published dgit history
88 # - .dsc of NEW dgit package has corresponding dgit-repo but not
94 my $dgit = "$dgitlive/dgit";
95 $dgit = "dgit" if !stat_exists $dgit;
96 my $cmd = "$dgit -d$distro \$DGIT_TEST_OPTS";
97 $cmd .= " -".("D" x $debuglevel) if $debuglevel;
98 $cmd .= " archive-api-query $subpath";
99 printdebug "apiquery $cmd\n";
100 $!=0; $?=0; my $json = `$cmd`;
101 defined $json && !$? or die "$subpath $! $?";
102 my $r = decode_json $json;
103 my $d = new Data::Dumper([$r], [qw(r)]);
104 printdebug "apiquery $subpath | ", $d->Dump() if $debuglevel>=2;
108 sub vsn_in_our_history ($) {
111 my $tagref = "refs/tags/".debiantag $vsn, $distro;
112 printdebug " checking history vsn=$vsn tagref=$tagref\n";
113 $?=0; my $r = system qw(git show-ref --verify --quiet), $tagref;
116 die "$pkg tagref $tagref $? $!";
119 sub specific_suite_has_suitable_vsn ($$) {
120 my ($suite, $vsn_check) = @_; # tests $vsn_check->($version)
121 my $in_suite = apiquery "dsc_in_suite/$suite/$pkg";
122 foreach my $entry (@$in_suite) {
123 my $vsn = $entry->{version};
124 die "$pkg ?" unless defined $vsn;
125 printdebug " checking history found suite=$suite vsn=$vsn\n";
126 return 1 if $vsn_check->($vsn);
131 sub new_has_vsn_in_our_history () {
132 return specific_suite_has_suitable_vsn('new', \&vsn_in_our_history);
135 sub good_suite_has_suitable_vsn ($) {
136 my ($vsn_check) = @_; # as for specific_suite_has_specific_vsn
137 my $suites = apiquery "suites";
138 foreach my $suitei (@$suites) {
139 my $suite = $suitei->{name};
140 die unless defined $suite;
141 next if $suite =~ m/\bnew$/;
142 return 1 if specific_suite_has_suitable_vsn($suite, $vsn_check);
148 $pkgdir = "$repos/$pkg.git";
149 if (!stat_exists $pkgdir) {
150 printdebug "statpackage $pkg => ENOENT\n";
154 $pkg_secret = !!(~(stat _)[2] & 05);
155 printdebug "statpackage $pkg => exists, secret=$pkg_secret.\n";
160 die unless @ARGV >= 1;
162 die unless $pkg =~ m/^$package_re$/;
168 my ($refobj, $reason) = @_;
170 printdebug "TAINTING $refobj\n",
171 (map { "\%| $_" } split "\n", $reason),
174 my $tf = new File::Temp or die $!;
175 print $tf "$refobj^0\n" or die $!;
177 seek $tf,0,0 or die $!;
179 my $gcfpid = open GCF, "-|";
180 defined $gcfpid or die $!;
182 open STDIN, "<&", $tf or die $!;
183 exec 'git', 'cat-file', '--batch';
190 m/^(\w+) (\w+) (\d+)\n/ or die "$_ ?";
196 if ($gitobjtype eq 'commit' or $gitobjtype eq 'tag') {
197 $!=0; read GCF, $gitobjdata, $bytes == $bytes
198 or die "$gitobjid $bytes $!";
202 $poldbh->do("INSERT INTO taints".
203 " (package, gitobjid, gitobjtype, gitobjdata, time, comment)".
204 " VALUES (?,?,?,?,?,?)", {},
205 $pkg, $gitobjid, $gitobjtype, $gitobjdata, time, $reason);
207 my $taint_id = $poldbh->last_insert_id(undef,undef,"taints","taint_id");
208 die unless defined $taint_id;
210 $poldbh->do("INSERT INTO taintoverrides".
211 " (taint_id, deliberately)".
212 " VALUES (?, '--deliberately-include-questionable-history')",
216 sub add_taint_by_tag ($$) {
217 my ($tagname,$refobjid) = @_;
219 "tag $tagname referred to this object in git tree but all".
220 " previously pushed versions were found to have been".
221 " removed from NEW (ie, rejected) (or never arrived)");
224 sub check_package () {
225 return 0 unless $pkg_exists;
226 return 0 unless $pkg_secret;
228 printdebug "check_package\n";
230 chdir $pkgdir or die "$pkgdir $!";
232 stat '.' or die "$pkgdir $!";
233 my $mtime = ((stat _)[9]);
234 my $age = time - $mtime;
235 printdebug "check_package age=$age\n";
237 if (good_suite_has_suitable_vsn(\&vsn_in_our_history)) {
238 chmod $publicmode, "." or die $!;
243 return 0 if $age < $new_upload_propagation_slop;
245 return 0 if new_has_vsn_in_our_history();
247 printdebug "check_package secret, deleted, tainting\n";
249 git_for_each_ref('refs/tags', sub {
250 my ($objid,$objtype,$fullrefname,$tagname) = @_;
251 add_taint_by_tag($tagname,$objid);
257 sub action_check_package () {
259 return check_package();
263 die unless @ARGV >= 4;
264 $version = shift @ARGV;
265 $suite = shift @ARGV;
266 $tagname = shift @ARGV;
267 my $delibs = shift @ARGV;
268 foreach my $delib (split /\,/, $delibs) {
269 $deliberately{$delib} = 1;
273 sub deliberately ($) { return $deliberately{"--deliberately-$_[0]"}; }
279 check_package(); # might make package public, or might add taints
281 return 0 unless $pkg_exists;
282 return 0 unless $pkg_secret;
284 # we suppose that NEW has a version which is already in our
285 # history, as otherwise the repo would have been blown away
287 if (deliberately('not-fast-forward')) {
288 add_taint(server_ref($suite),
289 "rewound suite $suite; --deliberately-not-fast-forward".
290 " specified in signed tag $tagname for upload of".
291 " version $version");
292 return NOFFCHECK|FRESHREPO;
294 if (deliberately('include-questionable-history')) {
297 die "\nPackage is in NEW and has not been accepted or rejected yet;".
298 " use a --deliberately option to specify whether you are".
299 " keeping or discarding the previously pushed history. ".
300 " Please RTFM dgit(1).\n\n";
303 sub action_push_confirm () {
306 die unless @ARGV >= 1;
307 my $freshrepo = shift @ARGV;
309 my $initq = $poldbh->prepare(<<END);
310 SELECT taint_id, gitobjid FROM taints t
311 WHERE (package = ? OR package = '')
313 $initq->execute($pkg);
315 my @objscatcmd = qw(git);
316 push @objscatcmd, qw(--git-dir), $freshrepo if length $freshrepo;
317 push @objscatcmd, qw(cat-file --batch);
318 debugcmd '|',@objscatcmd if $debuglevel>=2;
321 my $chkinput = tempfile();
322 while (my $taint = $initq->fetchrow_hashref()) {
323 push @taintids, $taint->{taint_id};
324 print $chkinput $taint->{gitobjid}, "\n" or die $!;
325 printdebug '|> ', $taint->{gitobjid}, "\n" if $debuglevel>=2;
327 flush $chkinput or die $!;
328 seek $chkinput,0,0 or die $!;
330 my $checkpid = open CHKOUT, "-|" // die $!;
332 open STDIN, "<&", $chkinput or die $!;
333 exec @objscatcmd or die $!;
336 my ($taintinfoq,$overridesanyq,$untaintq,$overridesq);
338 my $overridesstmt = <<END;
339 SELECT deliberately FROM taintoverrides WHERE (
342 my @overridesv = sort keys %deliberately;
343 $overridesstmt .= <<END foreach @overridesv;
346 $overridesstmt .= <<END;
348 ORDER BY deliberately ASC
353 while (my $taintid = shift @taintids) {
354 # git cat-file prints a spurious newline after it gets EOF
355 # This is not documented. I guess it might go away. So we
356 # just read what we expect and then let it get SIGPIPE.
358 die "$? $!" unless defined $_;
359 printdebug "|< ", $_ if $debuglevel>=2;
361 next if m/^\w+ missing$/;
362 die "$_ ?" unless m/^(\w+) (\w+) (\d+)\s/;
363 my ($objid,$objtype,$nbytes) = ($1,$2,$3);
366 (read CHKOUT, $drop, $nbytes) == $nbytes or die;
368 $taintinfoq ||= $poldbh->prepare(<<END);
369 SELECT package, time, comment FROM taints WHERE taint_id = ?
371 $taintinfoq->execute($taintid);
373 my $ti = $taintinfoq->fetchrow_hashref();
376 my $timeshow = defined $ti->{time}
377 ? " at time ".strftime("%Y-%m-%d %H:%M:%S Z", gmtime $ti->{time})
379 my $pkgshow = length $ti->{package}
380 ? "package $ti->{package}"
385 History contains tainted $objtype $objid
386 Taint recorded$timeshow for $pkgshow
387 Reason: $ti->{comment}
390 printdebug "SQL overrides: @overridesv $taintid /\n$overridesstmt\n";
392 $overridesq ||= $poldbh->prepare($overridesstmt);
393 $overridesq->execute(@overridesv, $taintid);
394 my ($ovwhy) = $overridesq->fetchrow_array();
395 if (!defined $ovwhy) {
396 $overridesanyq ||= $poldbh->prepare(<<END);
397 SELECT 1 FROM taintoverrides WHERE taint_id = ? LIMIT 1
399 $overridesanyq->execute($taintid);
400 my ($ovany) = $overridesanyq->fetchrow_array();
401 $stderr .= $ovany ? <<END : <<END;
402 Could be forced using --deliberately. Consult documentation.
404 Uncorrectable error. If confused, consult administrator.
409 Forcing due to --deliberately-$ovwhy
411 $untaintq ||= $poldbh->prepare(<<END);
412 DELETE FROM taints WHERE taint_id = ?
414 $untaintq->execute($taintid);
422 Rejecting push due to questionable history.
427 if (length $freshrepo) {
428 if (!good_suite_has_suitable_vsn(sub { 1; })) {
429 stat $freshrepo or die "$freshrepo $!";
430 my $oldmode = ((stat _)[2]);
431 my $oldwrites = $oldmode & 0222;
432 # remove r and x bits which have corresponding w bits clear
433 my $newmode = $oldmode &
434 (~0555 | ($oldwrites << 1) | ($oldwrites >> 1));
435 printdebug sprintf "chmod %#o (was %#o) %s\n",
436 $newmode, $oldmode, $freshrepo;
437 chmod $newmode, $freshrepo or die $!;
438 utime undef, undef, $freshrepo or die $!;
445 sub action_check_list () {
446 opendir L, "$repos" or die "$repos $!";
447 while (defined (my $dent = readdir L)) {
448 next unless $dent =~ m/^($package_re)\.git$/;
451 next unless $pkg_exists;
452 next unless $pkg_secret;
453 print "$pkg\n" or die $!;
455 closedir L or die $!;
456 close STDOUT or die $!;
461 my $fn = ${*::}{"action_$action"};
463 printdebug "dgit-repos-policy-debian: unknown action $action\n";
470 my $db_busy_exception= 'Debian::Dgit::Policy::Debian::DB_BUSY';
477 poldb_setup(poldb_path($repos), sub {
478 $poldbh->{HandleError} = sub {
479 return 0 unless $poldbh->err == 5; # SQLITE_BUSY, not in .pm :-(
480 die bless { }, $db_busy_exception;
483 eval ($ENV{'DGIT_RPD_TEST_DBLOOP_HOOK'}//'');
485 # used by tests/tests/debpolicy-dbretry
491 die unless defined $rcode;
495 last unless length $@;
496 die $@ unless ref $@ eq $db_busy_exception;
498 die if $sleepy >= 20;
500 print STDERR "[policy database busy, retrying (${sleepy}s)]\n";
502 eval { $poldbh->rollback; };
505 print STDERR $stderr or die $!;
506 flush STDERR or die $!;