2 # dgit repos policy hook script for Debian
6 use Debian::Dgit qw(:DEFAULT :policyflags);
11 use File::Temp qw(tempfile);
16 use Debian::Dgit::Policy::Debian;
19 enabledebuglevel $ENV{'DGIT_DRS_DEBUG'};
21 END { $? = 127; } # deliberate exit uses _exit
23 our $distro = shift @ARGV // die "need DISTRO";
24 our $repos = shift @ARGV // die "need DGIT-REPOS-DIR";
25 our $dgitlive = shift @ARGV // die "need DGIT-LIVE-DIR";
26 our $distrodir = shift @ARGV // die "need DISTRO-DIR";
27 our $action = shift @ARGV // die "need ACTION";
29 our $publicmode = 02775;
30 our $new_upload_propagation_slop = 3600*4 + 100;# fixme config;
35 our ($pkg_exists,$pkg_secret);
39 our ($version,$suite,$tagname);
42 # We assume that it is not possible for NEW to have a version older
45 # Whenever pushing, we check for
46 # source-package-local tainted history
47 # global tainted history
48 # can be overridden by --deliberately except for an admin prohib taint
50 # ALL of the following apply only if history is secret:
52 # if NEW has no version, or a version which is not in our history[1]
55 # if any suite's version is in our history[1], publish our history
56 # otherwise discard our history,
57 # tainting --deliberately-include-questionable-history
59 # if NEW has a version which is in our history[1]
61 # require explicit specification of one of
62 # --deliberately-include-questionable-history
63 # --deliberately-not-fast-forward
64 # (latter will taint old NEW version --d-i-q-h)
68 # [1] looking for the relevant git tag for the version number and not
69 # caring what that tag refers to.
71 # When we are doing a push to a fresh repo, any version will do: in
72 # this case, this is the first dgit upload of an existing package,
73 # and we trust that the uploader hasn't included in their git
74 # history any previous non-dgit uploads.
76 # A wrinkle: if we approved a push recently, we treat NEW as having
77 # a version which is in our history. This is because the package may
78 # still be being uploaded. (We record this using the timestamp of the
79 # package's git repo directory.)
81 # We aim for the following invariants and properties:
83 # - .dsc of published dgit package will have corresponding publicly
84 # visible dgit-repo (soon)
86 # - when a new package is rejected we help maintainer avoid
87 # accidentally including bad objects in published dgit history
89 # - .dsc of NEW dgit package has corresponding dgit-repo but not
95 my $dgit = "$dgitlive/dgit";
96 $dgit = "dgit" if !stat_exists $dgit;
97 my $cmd = "$dgit -d$distro \$DGIT_TEST_OPTS";
98 $cmd .= " -".("D" x $debuglevel) if $debuglevel;
99 $cmd .= " archive-api-query $subpath";
100 printdebug "apiquery $cmd\n";
101 $!=0; $?=0; my $json = `$cmd`;
102 defined $json && !$? or die "$subpath $! $?";
103 my $r = decode_json $json;
104 my $d = new Data::Dumper([$r], [qw(r)]);
105 printdebug "apiquery $subpath | ", $d->Dump() if $debuglevel>=2;
109 sub vsn_in_our_history ($) {
112 # Eventually, when we withdraw support for old-format (DEP-14
113 # namespace) tags, we will need to change this to only look
114 # for debiantag_new. See the commit
115 # "Tag change: Update dgit-repos-policy-debian"
116 # (reverting which is a good start for that change).
118 my @tagrefs = map { "refs/tags/".$_ } debiantags $vsn, $distro;
119 printdebug " checking history vsn=$vsn tagrefs=@tagrefs\n";
120 open F, "-|", qw(git-for-each-ref), @tagrefs;
123 return 1 if defined && m/\S/;
124 die "$pkg tagrefs @tagrefs $? $!" if $?;
128 sub specific_suite_has_suitable_vsn ($$) {
129 my ($suite, $vsn_check) = @_; # tests $vsn_check->($version)
130 my $in_suite = apiquery "dsc_in_suite/$suite/$pkg";
131 foreach my $entry (@$in_suite) {
132 my $vsn = $entry->{version};
133 die "$pkg ?" unless defined $vsn;
134 printdebug " checking history found suite=$suite vsn=$vsn\n";
135 return 1 if $vsn_check->($vsn);
140 sub new_has_vsn_in_our_history () {
141 return specific_suite_has_suitable_vsn('new', \&vsn_in_our_history);
144 sub good_suite_has_suitable_vsn ($) {
145 my ($vsn_check) = @_; # as for specific_suite_has_specific_vsn
146 my $suites = apiquery "suites";
147 foreach my $suitei (@$suites) {
148 my $suite = $suitei->{name};
149 die unless defined $suite;
150 next if $suite =~ m/\bnew$/;
151 return 1 if specific_suite_has_suitable_vsn($suite, $vsn_check);
157 $pkgdir = "$repos/$pkg.git";
158 if (!stat_exists $pkgdir) {
159 printdebug "statpackage $pkg => ENOENT\n";
163 $pkg_secret = !!(~(stat _)[2] & 05);
164 printdebug "statpackage $pkg => exists, secret=$pkg_secret.\n";
169 die unless @ARGV >= 1;
171 die unless $pkg =~ m/^$package_re$/;
177 my ($refobj, $reason) = @_;
179 printdebug "TAINTING $refobj\n",
180 (map { "\%| $_" } split "\n", $reason),
183 my $tf = new File::Temp or die $!;
184 print $tf "$refobj^0\n" or die $!;
186 seek $tf,0,0 or die $!;
188 my $gcfpid = open GCF, "-|";
189 defined $gcfpid or die $!;
191 open STDIN, "<&", $tf or die $!;
192 exec 'git', 'cat-file', '--batch';
199 m/^(\w+) (\w+) (\d+)\n/ or die "$_ ?";
205 if ($gitobjtype eq 'commit' or $gitobjtype eq 'tag') {
206 $!=0; read GCF, $gitobjdata, $bytes == $bytes
207 or die "$gitobjid $bytes $!";
211 $poldbh->do("INSERT INTO taints".
212 " (package, gitobjid, gitobjtype, gitobjdata, time, comment)".
213 " VALUES (?,?,?,?,?,?)", {},
214 $pkg, $gitobjid, $gitobjtype, $gitobjdata, time, $reason);
216 my $taint_id = $poldbh->last_insert_id(undef,undef,"taints","taint_id");
217 die unless defined $taint_id;
219 $poldbh->do("INSERT INTO taintoverrides".
220 " (taint_id, deliberately)".
221 " VALUES (?, '--deliberately-include-questionable-history')",
225 sub add_taint_by_tag ($$) {
226 my ($tagname,$refobjid) = @_;
228 "tag $tagname referred to this object in git tree but all".
229 " previously pushed versions were found to have been".
230 " removed from NEW (ie, rejected) (or never arrived)");
233 sub check_package () {
234 return 0 unless $pkg_exists;
235 return 0 unless $pkg_secret;
237 printdebug "check_package\n";
239 chdir $pkgdir or die "$pkgdir $!";
241 stat '.' or die "$pkgdir $!";
242 my $mtime = ((stat _)[9]);
243 my $age = time - $mtime;
244 printdebug "check_package age=$age\n";
246 if (good_suite_has_suitable_vsn(\&vsn_in_our_history)) {
247 chmod $publicmode, "." or die $!;
252 return 0 if $age < $new_upload_propagation_slop;
254 return 0 if new_has_vsn_in_our_history();
256 printdebug "check_package secret, deleted, tainting\n";
258 git_for_each_ref('refs/tags', sub {
259 my ($objid,$objtype,$fullrefname,$tagname) = @_;
260 add_taint_by_tag($tagname,$objid);
266 sub action_check_package () {
268 return check_package();
272 die unless @ARGV >= 4;
273 $version = shift @ARGV;
274 $suite = shift @ARGV;
275 $tagname = shift @ARGV;
276 my $delibs = shift @ARGV;
277 foreach my $delib (split /\,/, $delibs) {
278 $deliberately{$delib} = 1;
282 sub deliberately ($) { return $deliberately{"--deliberately-$_[0]"}; }
288 check_package(); # might make package public, or might add taints
290 return 0 unless $pkg_exists;
291 return 0 unless $pkg_secret;
293 # we suppose that NEW has a version which is already in our
294 # history, as otherwise the repo would have been blown away
296 if (deliberately('not-fast-forward')) {
297 add_taint(server_ref($suite),
298 "rewound suite $suite; --deliberately-not-fast-forward".
299 " specified in signed tag $tagname for upload of".
300 " version $version");
301 return NOFFCHECK|FRESHREPO;
303 if (deliberately('include-questionable-history')) {
306 die "\nPackage is in NEW and has not been accepted or rejected yet;".
307 " use a --deliberately option to specify whether you are".
308 " keeping or discarding the previously pushed history. ".
309 " Please RTFM dgit(1).\n\n";
312 sub action_push_confirm () {
315 die unless @ARGV >= 1;
316 my $freshrepo = shift @ARGV;
318 my $initq = $poldbh->prepare(<<END);
319 SELECT taint_id, gitobjid FROM taints t
320 WHERE (package = ? OR package = '')
322 $initq->execute($pkg);
324 my @objscatcmd = qw(git);
325 push @objscatcmd, qw(--git-dir), $freshrepo if length $freshrepo;
326 push @objscatcmd, qw(cat-file --batch);
327 debugcmd '|',@objscatcmd if $debuglevel>=2;
330 my $chkinput = tempfile();
331 while (my $taint = $initq->fetchrow_hashref()) {
332 push @taintids, $taint->{taint_id};
333 print $chkinput $taint->{gitobjid}, "\n" or die $!;
334 printdebug '|> ', $taint->{gitobjid}, "\n" if $debuglevel>=2;
336 flush $chkinput or die $!;
337 seek $chkinput,0,0 or die $!;
339 my $checkpid = open CHKOUT, "-|" // die $!;
341 open STDIN, "<&", $chkinput or die $!;
342 exec @objscatcmd or die $!;
345 my ($taintinfoq,$overridesanyq,$untaintq,$overridesq);
347 my $overridesstmt = <<END;
348 SELECT deliberately FROM taintoverrides WHERE (
351 my @overridesv = sort keys %deliberately;
352 $overridesstmt .= <<END foreach @overridesv;
355 $overridesstmt .= <<END;
357 ORDER BY deliberately ASC
362 while (my $taintid = shift @taintids) {
364 die "($taintid @objscatcmd) $!" unless defined $_;
365 printdebug "|< ", $_ if $debuglevel>=2;
367 next if m/^\w+ missing$/;
368 die "($taintid @objscatcmd) $_ ?" unless m/^(\w+) (\w+) (\d+)\s/;
369 my ($objid,$objtype,$nbytes) = ($1,$2,$3);
372 (read CHKOUT, $drop, $nbytes) == $nbytes
373 or die "($taintid @objscatcmd) $!";
376 die "($taintid @objscatcmd) $!" unless defined $_;
377 die "($taintid @objscatcmd) $_ ?" if m/\S/;
379 $taintinfoq ||= $poldbh->prepare(<<END);
380 SELECT package, time, comment FROM taints WHERE taint_id = ?
382 $taintinfoq->execute($taintid);
384 my $ti = $taintinfoq->fetchrow_hashref();
385 die "($taintid)" unless $ti;
387 my $timeshow = defined $ti->{time}
388 ? " at time ".strftime("%Y-%m-%d %H:%M:%S Z", gmtime $ti->{time})
390 my $pkgshow = length $ti->{package}
391 ? "package $ti->{package}"
396 History contains tainted $objtype $objid
397 Taint recorded$timeshow for $pkgshow
398 Reason: $ti->{comment}
401 printdebug "SQL overrides: @overridesv $taintid /\n$overridesstmt\n";
403 $overridesq ||= $poldbh->prepare($overridesstmt);
404 $overridesq->execute(@overridesv, $taintid);
405 my ($ovwhy) = $overridesq->fetchrow_array();
406 if (!defined $ovwhy) {
407 $overridesanyq ||= $poldbh->prepare(<<END);
408 SELECT 1 FROM taintoverrides WHERE taint_id = ? LIMIT 1
410 $overridesanyq->execute($taintid);
411 my ($ovany) = $overridesanyq->fetchrow_array();
412 $stderr .= $ovany ? <<END : <<END;
413 Could be forced using --deliberately. Consult documentation.
415 Uncorrectable error. If confused, consult administrator.
420 Forcing due to --deliberately-$ovwhy
422 $untaintq ||= $poldbh->prepare(<<END);
423 DELETE FROM taints WHERE taint_id = ?
425 $untaintq->execute($taintid);
433 Rejecting push due to questionable history.
438 if (length $freshrepo) {
439 if (!good_suite_has_suitable_vsn(sub { 1; })) {
440 stat $freshrepo or die "$freshrepo $!";
441 my $oldmode = ((stat _)[2]);
442 my $oldwrites = $oldmode & 0222;
443 # remove r and x bits which have corresponding w bits clear
444 my $newmode = $oldmode &
445 (~0555 | ($oldwrites << 1) | ($oldwrites >> 1));
446 printdebug sprintf "chmod %#o (was %#o) %s\n",
447 $newmode, $oldmode, $freshrepo;
448 chmod $newmode, $freshrepo or die $!;
449 utime undef, undef, $freshrepo or die $!;
456 sub action_check_list () {
457 opendir L, "$repos" or die "$repos $!";
458 while (defined (my $dent = readdir L)) {
459 next unless $dent =~ m/^($package_re)\.git$/;
462 next unless $pkg_exists;
463 next unless $pkg_secret;
464 print "$pkg\n" or die $!;
466 closedir L or die $!;
467 close STDOUT or die $!;
472 my $fn = ${*::}{"action_$action"};
474 printdebug "dgit-repos-policy-debian: unknown action $action\n";
481 my $db_busy_exception= 'Debian::Dgit::Policy::Debian::DB_BUSY';
488 poldb_setup(poldb_path($repos), sub {
489 $poldbh->{HandleError} = sub {
490 return 0 unless $poldbh->err == 5; # SQLITE_BUSY, not in .pm :-(
491 die bless { }, $db_busy_exception;
494 eval ($ENV{'DGIT_RPD_TEST_DBLOOP_HOOK'}//'');
496 # used by tests/tests/debpolicy-dbretry
502 die unless defined $rcode;
506 last unless length $@;
507 die $@ unless ref $@ eq $db_busy_exception;
509 die if $sleepy >= 20;
511 print STDERR "[policy database busy, retrying (${sleepy}s)]\n";
513 eval { $poldbh->rollback; };
516 print STDERR $stderr or die $!;
517 flush STDERR or die $!;