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 # A wrinkle: if we approved a push recently, we treat NEW as having
71 # a version which is in our history. This is because the package may
72 # still be being uploaded. (We record this using the timestamp of the
73 # package's git repo directory.)
75 # We aim for the following invariants and properties:
77 # - .dsc of published dgit package will have corresponding publicly
78 # visible dgit-repo (soon)
80 # - when a new package is rejected we help maintainer avoid
81 # accidentally including bad objects in published dgit history
83 # - .dsc of NEW dgit package has corresponding dgit-repo but not
89 my $cmd = "$dgitlive/dgit -d$distro \$DGIT_TEST_OPTS";
90 $cmd .= " -".("D" x $debuglevel) if $debuglevel;
91 $cmd .= " archive-api-query $subpath";
92 printdebug "apiquery $cmd\n";
93 $!=0; $?=0; my $json = `$cmd`;
94 defined $json && !$? or die "$subpath $! $?";
95 my $r = decode_json $json;
96 my $d = new Data::Dumper([$r], [qw(r)]);
97 printdebug "apiquery $subpath | ", $d->Dump() if $debuglevel>=2;
101 sub specific_suite_has_vsn_in_our_history ($) {
103 my $in_suite = apiquery "dsc_in_suite/$suite/$pkg";
104 foreach my $entry (@$in_suite) {
105 my $vsn = $entry->{version};
106 die "$pkg ?" unless defined $vsn;
107 my $tagref = "refs/tags/".debiantag $vsn;
108 printdebug " checking history suite=$suite vsn=$vsn tagref=$tagref\n";
109 $?=0; my $r = system qw(git show-ref --verify --quiet), $tagref;
112 die "$pkg tagref $tagref $? $!";
117 sub new_has_vsn_in_our_history () {
118 return specific_suite_has_vsn_in_our_history('new');
121 sub good_suite_has_vsn_in_our_history () {
122 my $suites = apiquery "suites";
123 foreach my $suitei (@$suites) {
124 my $suite = $suitei->{name};
125 die unless defined $suite;
126 next if $suite =~ m/\bnew$/;
127 return 1 if specific_suite_has_vsn_in_our_history($suite);
133 $pkgdir = "$repos/$pkg.git";
134 if (!stat_exists $pkgdir) {
135 printdebug "statpackage $pkg => ENOENT\n";
139 $pkg_secret = !!(~(stat _)[2] & 05);
140 printdebug "statpackage $pkg => exists, secret=$pkg_secret.\n";
145 die unless @ARGV >= 1;
147 die unless $pkg =~ m/^$package_re$/;
153 my ($refobj, $reason) = @_;
155 printdebug "TAINTING $refobj\n",
156 (map { "\%| $_" } split "\n", $reason),
159 my $tf = new File::Temp or die $!;
160 print $tf "$refobj^0\n" or die $!;
162 seek $tf,0,0 or die $!;
164 my $gcfpid = open GCF, "-|";
165 defined $gcfpid or die $!;
167 open STDIN, "<&", $tf or die $!;
168 exec 'git', 'cat-file', '--batch';
175 m/^(\w+) (\w+) (\d+)\n/ or die "$_ ?";
181 if ($gitobjtype eq 'commit' or $gitobjtype eq 'tag') {
182 $!=0; read GCF, $gitobjdata, $bytes == $bytes
183 or die "$gitobjid $bytes $!";
187 $poldbh->do("INSERT INTO taints".
188 " (package, gitobjid, gitobjtype, gitobjdata, time, comment)".
189 " VALUES (?,?,?,?,?,?)", {},
190 $pkg, $gitobjid, $gitobjtype, $gitobjdata, time, $reason);
192 my $taint_id = $poldbh->last_insert_id(undef,undef,"taints","taint_id");
193 die unless defined $taint_id;
195 $poldbh->do("INSERT INTO taintoverrides".
196 " (taint_id, deliberately)".
197 " VALUES (?, '--deliberately-include-questionable-history')",
201 sub add_taint_by_tag ($$) {
202 my ($tagname,$refobjid) = @_;
204 "tag $tagname referred to this object in git tree but all".
205 " previously pushed versions were found to have been".
206 " removed from NEW (ie, rejected) (or never arrived)");
209 sub action_check_package () {
211 return 0 unless $pkg_exists;
212 return 0 unless $pkg_secret;
214 printdebug "check_package\n";
216 chdir $pkgdir or die "$pkgdir $!";
218 stat '.' or die "$pkgdir $!";
219 my $mtime = ((stat _)[9]);
220 my $age = time - $mtime;
221 printdebug "check_package age=$age\n";
223 return 0 if $age < $new_upload_propagation_slop;
225 return 0 if new_has_vsn_in_our_history();
227 if (good_suite_has_vsn_in_our_history) {
228 chmod $publicmode, "." or die $!;
232 printdebug "check_package secret, deleted, tainting\n";
234 git_for_each_ref('refs/tags', sub {
235 my ($objid,$objtype,$fullrefname,$tagname) = @_;
236 add_taint_by_tag($tagname,$objid);
243 die unless @ARGV >= 4;
244 $version = shift @ARGV;
245 $suite = shift @ARGV;
246 $tagname = shift @ARGV;
247 my $delibs = shift @ARGV;
248 foreach my $delib (split /\,/, $delibs) {
249 $deliberately{$delib} = 1;
253 sub deliberately ($) { return $deliberately{"--deliberately-$_[0]"}; }
259 return 0 unless $pkg_exists;
260 return 0 unless $pkg_secret;
262 # we suppose that NEW has a version which is already in our
263 # history, as otherwise the repo would have been blown away
265 if (deliberately('not-fast-forward')) {
266 add_taint(server_ref($suite),
267 "rewound suite $suite; --deliberately-not-fast-forward".
268 " specified in signed tag $tagname for upload of".
269 " version $version");
270 return NOFFCHECK|FRESHREPO;
272 if (deliberately('include-questionable-history')) {
275 die "Package is in NEW and has not been accepted or rejected yet;".
276 " use a --deliberately option to specify whether you are".
277 " keeping or discarding the previously pushed history. ".
278 " Please RTFM dgit(1).\n";
281 sub action_push_confirm () {
284 die unless @ARGV >= 1;
285 my $freshrepo = shift @ARGV;
287 my $initq = $poldbh->prepare(<<END);
288 SELECT taint_id, gitobjid FROM taints t
289 WHERE (package = ? OR package = '')
291 $initq->execute($pkg);
293 my @objscatcmd = qw(git);
294 push @objscatcmd, qw(cat-file --batch);
295 debugcmd '|',@objscatcmd if $debuglevel>=2;
298 my $chkinput = tempfile();
299 while (my $taint = $initq->fetchrow_hashref()) {
300 push @taintids, $taint->{taint_id};
301 print $chkinput $taint->{gitobjid}, "\n" or die $!;
302 printdebug '|> ', $taint->{gitobjid}, "\n" if $debuglevel>=2;
304 flush $chkinput or die $!;
305 seek $chkinput,0,0 or die $!;
307 my $checkpid = open CHKOUT, "-|" // die $!;
309 open STDIN, "<&", $chkinput or die $!;
310 exec @objscatcmd or die $!;
313 my ($taintinfoq,$overridesanyq,$untaintq,$overridesq);
315 my $overridesstmt = <<END;
316 SELECT deliberately FROM taintoverrides WHERE (
319 my @overridesv = sort keys %deliberately;
320 $overridesstmt .= <<END foreach @overridesv;
323 $overridesstmt .= <<END;
325 ORDER BY deliberately ASC
330 while (my $taintid = shift @taintids) {
331 # git cat-file prints a spurious newline after it gets EOF
332 # This is not documented. I guess it might go away. So we
333 # just read what we expect and then let it get SIGPIPE.
335 die "$? $!" unless defined $_;
336 printdebug "|< ", $_ if $debuglevel>=2;
338 next if m/^\w+ missing$/;
339 die unless m/^(\w+) (\w+) (\d+)\s/;
340 my ($objid,$objtype,$nbytes) = ($1,$2,$3);
343 (read CHKOUT, $drop, $nbytes) == $nbytes or die;
345 $taintinfoq ||= $poldbh->prepare(<<END);
346 SELECT package, time, comment FROM taints WHERE taint_id = ?
348 $taintinfoq->execute($taintid);
350 my $ti = $taintinfoq->fetchrow_hashref();
353 my $timeshow = defined $ti->{time}
354 ? " at time ".strftime("%Y-%m-%d %H:%M:%S Z", gmtime $ti->{time})
356 my $pkgshow = length $ti->{package}
357 ? "package $ti->{package}"
362 History contains tainted $objtype $objid
363 Taint recorded$timeshow for $pkgshow
364 Reason: $ti->{comment}
367 printdebug "SQL overrides: @overridesv $taintid /\n$overridesstmt\n";
369 $overridesq ||= $poldbh->prepare($overridesstmt);
370 $overridesq->execute(@overridesv, $taintid);
371 my ($ovwhy) = $overridesq->fetchrow_array();
372 if (!defined $ovwhy) {
373 $overridesanyq ||= $poldbh->prepare(<<END);
374 SELECT 1 FROM taintoverrides WHERE taint_id = ? LIMIT 1
376 $overridesanyq->execute($taintid);
377 my ($ovany) = $overridesanyq->fetchrow_array();
378 $stderr .= $ovany ? <<END : <<END;
379 Could be forced using --deliberately. Consult documentation.
381 Uncorrectable error. If confused, consult administrator.
386 Forcing due to --deliberately-$ovwhy
388 $untaintq ||= $poldbh->prepare(<<END);
389 DELETE FROM taints WHERE taint_id = ?
391 $untaintq->execute($taintid);
399 Rejecting push due to questionable history.
404 if (length $freshrepo) {
405 if (!good_suite_has_vsn_in_our_history()) {
406 stat $freshrepo or die "$freshrepo $!";
407 my $oldmode = ((stat _)[2]);
408 my $oldwrites = $oldmode & 0222;
409 # remove r and x bits which have corresponding w bits clear
410 my $newmode = $oldmode &
411 (~0555 | ($oldwrites << 1) | ($oldwrites >> 1));
412 printdebug sprintf "chmod %#o (was %#o) %s\n",
413 $newmode, $oldmode, $freshrepo;
414 chmod $newmode, $freshrepo or die $!;
415 utime undef, undef, $freshrepo or die $!;
422 sub action_check_list () {
423 opendir L, "$repos" or die "$repos $!";
424 while (defined (my $dent = readdir L)) {
425 next unless $dent =~ m/^($package_re)\.git$/;
428 next unless $pkg_exists;
429 next unless $pkg_secret;
430 print "$pkg\n" or die $!;
432 closedir L or die $!;
433 close STDOUT or die $!;
438 my $fn = ${*::}{"action_$action"};
440 printdebug "dgit-repos-policy-debian: unknown action $action\n";
448 poldb_setup(poldb_path($repos));
452 die unless defined $rcode;
454 eval { $poldbh->commit; };
455 last unless length $@;
457 die if $sleepy >= 20;
458 print STDERR "[policy database busy, retrying]\n";
464 print STDERR $stderr or die $!;
465 flush STDERR or die $!;