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 $action = shift @ARGV // die "need ACTION";
25 our $publicmode = 02775;
26 our $new_upload_propagation_slop = 3600*4 + 100;# fixme config;
31 our ($pkg_exists,$pkg_secret);
35 our ($version,$suite,$tagname);
38 # We assume that it is not possible for NEW to have a version older
41 # Whenever pushing, we check for
42 # source-package-local tainted history
43 # global tainted history
44 # can be overridden by --deliberately except for an admin prohib taint
46 # ALL of the following apply only if history is secret:
48 # if NEW has no version, or a version which is not in our history[1]
51 # if any suite's version is in our history[1], publish our history
52 # otherwise discard our history,
53 # tainting --deliberately-include-questionable-history
55 # if NEW has a version which is in our history[1]
57 # require explicit specification of one of
58 # --deliberately-include-questionable-history
59 # --deliberately-not-fast-forward
60 # (latter will taint old NEW version --d-i-q-h)
64 # [1] looking for the relevant git tag for the version number and not
65 # caring what that tag refers to.
67 # A wrinkle: if we approved a push recently, we treat NEW as having
68 # a version which is in our history. This is because the package may
69 # still be being uploaded. (We record this using the timestamp of the
70 # package's git repo directory.)
72 # We aim for the following invariants and properties:
74 # - .dsc of published dgit package will have corresponding publicly
75 # visible dgit-repo (soon)
77 # - when a new package is rejected we help maintainer avoid
78 # accidentally including bad objects in published dgit history
80 # - .dsc of NEW dgit package has corresponding dgit-repo but not
86 my $cmd = "$dgitlive/dgit -d$distro \$DGIT_TEST_OPTS";
87 $cmd .= " -".("D" x $debuglevel) if $debuglevel;
88 $cmd .= " archive-api-query $subpath";
89 printdebug "apiquery $cmd\n";
90 $!=0; $?=0; my $json = `$cmd`;
91 defined $json or die "$subpath $! $?";
92 my $r = decode_json $json;
93 my $d = new Data::Dumper([$r], [qw(r)]);
94 printdebug "apiquery $subpath | ", $d->Dump(), "\n" if $debuglevel>=2;
98 sub specific_suite_has_vsn_in_our_history ($) {
100 my $in_suite = apiquery "/dsc_in_suite/$suite/$pkg";
101 foreach my $entry (@$in_suite) {
102 my $vsn = $entry->{version};
103 die "$pkg ?" unless defined $vsn;
104 my $tag = debiantag $vsn;
105 $?=0; my $r = system qw(git show-ref --verify --quiet), $tag;
108 die "$pkg tag $tag $? $!";
113 sub new_has_vsn_in_our_history () {
114 stat $pkgdir or die "$pkgdir $!";
115 my $mtime = ((stat _)[9]);
116 my $age = time - $mtime;
117 return 1 if $age < $new_upload_propagation_slop;
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 my $tf = new File::Temp or die $!;
156 print $tf "$refobj^0\n" or die $!;
158 my $gcfpid = open GCF, "-|";
159 defined $gcfpid or die $!;
161 open STDIN, "<&", $tf or die $!;
162 exec 'git', 'cat-file';
168 m/^(\w+) (\w+) (\d+)\n/ or die "$_ ?";
174 if ($gitobjtype eq 'commit' or $gitobjtype eq 'tag') {
175 $!=0; read GCF, $gitobjdata, $bytes == $bytes
176 or die "$gitobjid $bytes $!";
180 $poldbh->do("INSERT INTO taints".
181 " (package, gitobjid, gitobjtype, gitobjdata, time, comment)",
182 " VALUES (?,?,?,?,?,?)", {},
183 $pkg, $gitobjid, $gitobjtype, $gitobjdata, time, $reason);
185 my $taint_id = $poldbh->last_insert_id(undef,undef,"taints","taint_id");
186 die unless defined $taint_id;
188 $poldbh->do("INSERT INTO taintoverrides".
189 " (taint_id, deliberately)",
190 " VALUES (?, 'include-questionable-history')", {},
194 sub add_taint_by_tag ($$) {
195 my ($tagname,$refobjid) = @_;
197 "tag $tagname referred to this object in git tree but all".
198 " previously pushed versions were found to have been".
199 " removed from NEW (ie, rejected) (or never arrived)");
202 sub action_check_package () {
204 return 0 unless $pkg_exists;
205 return 0 unless $pkg_secret;
207 chdir $pkgdir or die "$pkgdir $!";
208 return if new_has_vsn_in_our_history();
210 if (good_suite_has_vsn_in_our_history) {
211 chmod $publicmode, "." or die $!;
215 git_for_each_ref('refs/tags', sub {
216 my ($objid,$objtype,$fullrefname,$tagname) = @_;
217 add_taint_by_tag($tagname,$objid);
224 die unless @ARGV >= 4;
225 $version = shift @ARGV;
226 $suite = shift @ARGV;
227 $tagname = shift @ARGV;
228 my $delibs = shift @ARGV;
229 foreach my $delib (split /\,/, $delibs) {
230 $deliberately{$delib} = 1;
234 sub deliberately ($) { return $deliberately{$_[0]}; }
238 return 0 unless $pkg_exists;
239 return 0 unless $pkg_secret;
241 # we suppose that NEW has a version which is already in our
242 # history, as otherwise the repo would have been blown away
244 if (deliberately('not-fast-forward')) {
245 add_taint(server_ref($suite),
246 "suite $suite when --deliberately-not-fast-forward".
247 " specified in signed tag $tagname for upload of".
248 " version $version into suite $suite");
249 return NOFFCHECK|FRESHREPO;
251 if (deliberately('include-questionable-history')) {
254 die "Package is in NEW and has not been accepted or rejected yet;".
255 " use a --deliberately option to specify whether you are".
256 " keeping or discarding the previously pushed history. ".
257 " Please RTFM dgit(1).\n";
260 sub action_push_confirm () {
262 die unless @ARGV >= 5;
263 my $freshrepo = $ARGV[4];
265 my $initq = $poldbh->prepare(<<END);
266 SELECT taint_id, gitobjid FROM taints t
267 WHERE (package = ? OR package = '')
269 $initq->execute($pkg);
272 my $chkinput = tempfile();
273 while (my $taint = $initq->fetchrow_hashref()) {
274 push @taintids, $taint->{taint_id};
275 print $chkinput $taint->{gitobjid}, "\n" or die $!;
277 flush $chkinput or die $!;
278 seek $chkinput,0,0 or die $!;
280 my $checkpid = open CHKOUT, "-|" // die $!;
282 open STDIN, "<&", $chkinput or die $!;
283 exec qw(git cat-file --batch) or die $!;
286 my ($taintinfoq,$overridesanyq,$untaintq,$overridesq);
288 my $overridesstmt = <<END;
289 SELECT deliberately FROM taintoverrides WHERE ( 1
291 my @overridesv = sort keys %deliberately;
292 $overridesstmt .= join '', (<<END x @overridesv);
295 $overridesstmt .= <<END;
297 ORDER BY deliberately ASC
302 while (my $taintid = shift @taintids) {
303 # git cat-file prints a spurious newline after it gets EOF
304 # This is not documented. I guess it might go away. So we
305 # just read what we expect and then let it get SIGPIPE.
307 die "$? $!" unless defined $_;
309 next if m/^\w+ missing$/;
310 die unless m/^(\w+) (\w+) (\d+)\s/;
311 my ($objid,$objtype,$nbytes) = ($1,$2,$3);
314 (read CHKOUT, $drop, $nbytes) == $nbytes or die;
316 $taintinfoq ||= $poldbh->prepare(<<END);
317 SELECT package, time, comment FROM taints WHERE taint_id = ?
319 $taintinfoq->execute($taintid);
321 my $ti = $taintinfoq->fetchrow_hashref();
324 my $timeshow = defined $ti->{time}
325 ? " at time ".strftime("%Y-%m-%d %H:%M:%S Z", gmtime $ti->{time})
327 my $pkgshow = length $ti->{package}
328 ? "package $ti->{package}"
333 History contains tainted $objtype $objid
334 Taint recorded$timeshow for $pkgshow
335 Reason: $ti->{comment}
338 $overridesq ||= $poldbh->prepare($overridesstmt);
339 $overridesq->execute(@overridesv, $taintid);
340 my ($ovwhy) = $overridesq->fetchrow_array();
341 if (!defined $ovwhy) {
342 $overridesanyq ||= $poldbh->prepare(<<END);
343 SELECT 1 FROM taintoverrides WHERE taint_id = ? LIMIT 1
345 $overridesanyq->execute($taintid);
346 my ($ovany) = $overridesanyq->fetchrow_array();
347 $stderr .= $ovany ? <<END : <<END;
348 Could be forced using --deliberately. Consult documentation.
350 Uncorrectable error. If confused, consult administrator.
355 Forcing due to --deliberately-$ovwhy
357 $untaintq ||= $poldbh->prepare(<<END);
358 DELETE FROM taints WHERE taint_id = ?
360 $untaintq->execute($taintid);
368 Rejecting push due to questionable history.
373 if (length $freshrepo) {
374 if (!good_suite_has_vsn_in_our_history()) {
375 stat $freshrepo or die "$freshrepo $!";
376 my $oldmode = ((stat _)[2]);
377 my $oldwrites = $oldmode & 0222;
378 # remove r and x bits which have corresponding w bits clear
379 my $newmode = $oldmode &
380 (~0555 | ($oldwrites << 1) | ($oldwrites >> 1));
381 printdebug sprintf "chmod %#o (was %#o) %s\n",
382 $newmode, $oldmode, $freshrepo;
383 chmod $newmode, $freshrepo or die $!;
390 sub action_check_list () {
391 opendir L, "$repos" or die "$repos $!";
392 while (defined (my $dent = readdir L)) {
393 next unless $dent =~ m/^($package_re)\.git$/;
396 next unless $pkg_exists;
397 next unless $pkg_secret;
398 printdebug "$pkg\n" or die $!;
400 closedir L or die $!;
401 close STDOUT or die $!;
406 my $fn = ${*::}{"action_$action"};
408 printdebug "dgit-repos-policy-debian: unknown action $action\n";
416 poldb_setup(poldb_path($repos));
420 die unless defined $rcode;
422 eval { $poldbh->commit; };
423 last unless length $@;
425 die if $sleepy >= 20;
426 print STDERR "[policy database busy, retrying]\n";
432 print STDERR $stderr;