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]}; }
256 return 0 unless $pkg_exists;
257 return 0 unless $pkg_secret;
259 # we suppose that NEW has a version which is already in our
260 # history, as otherwise the repo would have been blown away
262 if (deliberately('not-fast-forward')) {
263 add_taint(server_ref($suite),
264 "suite $suite when --deliberately-not-fast-forward".
265 " specified in signed tag $tagname for upload of".
266 " version $version into suite $suite");
267 return NOFFCHECK|FRESHREPO;
269 if (deliberately('include-questionable-history')) {
272 die "Package is in NEW and has not been accepted or rejected yet;".
273 " use a --deliberately option to specify whether you are".
274 " keeping or discarding the previously pushed history. ".
275 " Please RTFM dgit(1).\n";
278 sub action_push_confirm () {
281 die unless @ARGV >= 1;
282 my $freshrepo = shift @ARGV;
284 my $initq = $poldbh->prepare(<<END);
285 SELECT taint_id, gitobjid FROM taints t
286 WHERE (package = ? OR package = '')
288 $initq->execute($pkg);
291 my $chkinput = tempfile();
292 while (my $taint = $initq->fetchrow_hashref()) {
293 push @taintids, $taint->{taint_id};
294 print $chkinput $taint->{gitobjid}, "\n" or die $!;
296 flush $chkinput or die $!;
297 seek $chkinput,0,0 or die $!;
299 my $checkpid = open CHKOUT, "-|" // die $!;
301 open STDIN, "<&", $chkinput or die $!;
302 exec qw(git cat-file --batch) or die $!;
305 my ($taintinfoq,$overridesanyq,$untaintq,$overridesq);
307 my $overridesstmt = <<END;
308 SELECT deliberately FROM taintoverrides WHERE (
311 my @overridesv = sort keys %deliberately;
312 $overridesstmt .= <<END foreach @overridesv;
315 $overridesstmt .= <<END;
317 ORDER BY deliberately ASC
322 while (my $taintid = shift @taintids) {
323 # git cat-file prints a spurious newline after it gets EOF
324 # This is not documented. I guess it might go away. So we
325 # just read what we expect and then let it get SIGPIPE.
327 die "$? $!" unless defined $_;
329 next if m/^\w+ missing$/;
330 die unless m/^(\w+) (\w+) (\d+)\s/;
331 my ($objid,$objtype,$nbytes) = ($1,$2,$3);
334 (read CHKOUT, $drop, $nbytes) == $nbytes or die;
336 $taintinfoq ||= $poldbh->prepare(<<END);
337 SELECT package, time, comment FROM taints WHERE taint_id = ?
339 $taintinfoq->execute($taintid);
341 my $ti = $taintinfoq->fetchrow_hashref();
344 my $timeshow = defined $ti->{time}
345 ? " at time ".strftime("%Y-%m-%d %H:%M:%S Z", gmtime $ti->{time})
347 my $pkgshow = length $ti->{package}
348 ? "package $ti->{package}"
353 History contains tainted $objtype $objid
354 Taint recorded$timeshow for $pkgshow
355 Reason: $ti->{comment}
358 printdebug "SQL overrides: @overridesv $taintid /\n$overridesstmt\n";
360 $overridesq ||= $poldbh->prepare($overridesstmt);
361 $overridesq->execute(@overridesv, $taintid);
362 my ($ovwhy) = $overridesq->fetchrow_array();
363 if (!defined $ovwhy) {
364 $overridesanyq ||= $poldbh->prepare(<<END);
365 SELECT 1 FROM taintoverrides WHERE taint_id = ? LIMIT 1
367 $overridesanyq->execute($taintid);
368 my ($ovany) = $overridesanyq->fetchrow_array();
369 $stderr .= $ovany ? <<END : <<END;
370 Could be forced using --deliberately. Consult documentation.
372 Uncorrectable error. If confused, consult administrator.
377 Forcing due to --deliberately-$ovwhy
379 $untaintq ||= $poldbh->prepare(<<END);
380 DELETE FROM taints WHERE taint_id = ?
382 $untaintq->execute($taintid);
390 Rejecting push due to questionable history.
395 if (length $freshrepo) {
396 if (!good_suite_has_vsn_in_our_history()) {
397 stat $freshrepo or die "$freshrepo $!";
398 my $oldmode = ((stat _)[2]);
399 my $oldwrites = $oldmode & 0222;
400 # remove r and x bits which have corresponding w bits clear
401 my $newmode = $oldmode &
402 (~0555 | ($oldwrites << 1) | ($oldwrites >> 1));
403 printdebug sprintf "chmod %#o (was %#o) %s\n",
404 $newmode, $oldmode, $freshrepo;
405 chmod $newmode, $freshrepo or die $!;
406 utime undef, undef, $freshrepo or die $!;
413 sub action_check_list () {
414 opendir L, "$repos" or die "$repos $!";
415 while (defined (my $dent = readdir L)) {
416 next unless $dent =~ m/^($package_re)\.git$/;
419 next unless $pkg_exists;
420 next unless $pkg_secret;
421 print "$pkg\n" or die $!;
423 closedir L or die $!;
424 close STDOUT or die $!;
429 my $fn = ${*::}{"action_$action"};
431 printdebug "dgit-repos-policy-debian: unknown action $action\n";
439 poldb_setup(poldb_path($repos));
443 die unless defined $rcode;
445 eval { $poldbh->commit; };
446 last unless length $@;
448 die if $sleepy >= 20;
449 print STDERR "[policy database busy, retrying]\n";
455 print STDERR $stderr;