2 # dgit repos policy hook script for Debian
5 $SIG{__WARN__} = sub { die $_[0]; };
9 use File::Temp qw(tempfile);
13 use Debian::Dgit qw(:DEFAULT :policyflags);
14 use Debian::Dgit::Policy::Debian;
16 our $distro = shift @ARGV // die "need DISTRO";
17 our $repos = shift @ARGV // die "need DGIT-REPOS-DIR";
18 our $dgitlive = shift @ARGV // die "need DGIT-LIVE-DIR";
19 our $action = shift @ARGV // die "need ACTION";
21 our $publicmode = 02775;
22 our $new_upload_propagation_slop = 3600*4 + 100;
27 our ($pkg_exists,$pkg_secret);
31 our ($version,$suite,$tagname);
34 # We assume that it is not possible for NEW to have a version older
37 # Whenever pushing, we check for
38 # source-package-local tainted history
39 # global tainted history
40 # can be overridden by --deliberately except for an admin prohib taint
42 # ALL of the following apply only if history is secret:
44 # if NEW has no version, or a version which is not in our history[1]
47 # if any suite's version is in our history[1], publish our history
48 # otherwise discard our history,
49 # tainting --deliberately-include-questionable-history
51 # if NEW has a version which is in our history[1]
53 # require explicit specification of one of
54 # --deliberately-include-questionable-history
55 # --deliberately-not-fast-forward
56 # (latter will taint old NEW version --d-i-q-h)
60 # [1] looking for the relevant git tag for the version number and not
61 # caring what that tag refers to.
63 # A wrinkle: if we approved a push recently, we treat NEW as having
64 # a version which is in our history. This is because the package may
65 # still be being uploaded. (We record this using the timestamp of the
66 # package's git repo directory.)
68 # We aim for the following invariants and properties:
70 # - .dsc of published dgit package will have corresponding publicly
71 # visible dgit-repo (soon)
73 # - when a new package is rejected we help maintainer avoid
74 # accidentally including bad objects in published dgit history
76 # - .dsc of NEW dgit package has corresponding dgit-repo but not
82 my $cmd = "$dgitlive/dgit -d $distro ".
83 "\$DGIT_TEST_OPTS \$DGIT_TEST_DEBUG archive-api-query $subpath";
84 $!=0; $?=0; my $json = `$cmd`;
85 defined $json or die "$subpath $! $?";
86 return decode_json $json;
89 sub specific_suite_has_vsn_in_our_history ($) {
91 my $in_suite = apiquery "/dsc_in_suite/$suite/$pkg";
92 foreach my $entry (@$in_suite) {
93 my $vsn = $entry->{version};
94 die "$pkg ?" unless defined $vsn;
95 my $tag = debiantag $vsn;
96 $?=0; my $r = system qw(git show-ref --verify --quiet), $tag;
99 die "$pkg tag $tag $? $!";
104 sub new_has_vsn_in_our_history () {
105 stat $pkgdir or die "$pkgdir $!";
106 my $mtime = ((stat _)[9]);
107 my $age = time - $mtime;
108 return 1 if $age < $new_upload_propagation_slop;
109 return specific_suite_has_vsn_in_our_history('new');
112 sub good_suite_has_vsn_in_our_history () {
113 my $suites = apiquery "/suites";
114 foreach my $suitei (@$suites) {
115 my $suite = $suitei->{name};
116 die unless defined $suite;
117 next if $suite =~ m/\bnew$/;
118 return 1 if specific_suite_has_vsn_in_our_history($suite);
124 $pkgdir = "$repos/$pkg.git";
125 if (!stat_exists $pkgdir) {
129 $pkg_secret = !!(~(stat _)[2] & 05);
134 die unless @ARGV >= 1;
136 die unless $pkg =~ m/^$package_re$/;
142 my ($refobj, $reason);
144 my $tf = new File::Temp or die $!;
145 print $tf "$refobj^0\n" or die $!;
147 my $gcfpid = open GCF, "-|";
148 defined $gcfpid or die $!;
150 open STDIN, "<&", $tf or die $!;
151 exec 'git', 'cat-file';
157 m/^(\w+) (\w+) (\d+)\n/ or die "$_ ?";
163 if ($gitobjtype eq 'commit' or $gitobjtype eq 'tag') {
164 $!=0; read GCF, $gitobjdata, $bytes == $bytes
165 or die "$gitobjid $bytes $!";
169 $poldbh->do("INSERT INTO taints".
170 " (package, gitobjid, gitobjtype, gitobjdata, time, comment)",
171 " VALUES (?,?,?,?,?,?)", {},
172 $pkg, $gitobjid, $gitobjtype, $gitobjdata, time, $reason);
174 my $taint_id = $poldbh->last_insert_id(undef,undef,"taints","taint_id");
175 die unless defined $taint_id;
177 $poldbh->do("INSERT INTO taintoverrides".
178 " (taint_id, deliberately)",
179 " VALUES (?, 'include-questionable-history')", {},
183 sub add_taint_by_tag ($$) {
184 my ($tagname,$refobjid) = @_;
186 "tag $tagname referred to this object in git tree but all".
187 " previously pushed versions were found to have been".
188 " removed from NEW (ie, rejected) (or never arrived)");
191 sub action__check_package () {
193 return 0 unless $pkg_exists;
194 return 0 unless $pkg_secret;
196 chdir $pkgdir or die "$pkgdir $!";
197 return if new_has_vsn_in_our_history();
199 if (good_suite_has_vsn_in_our_history) {
200 chmod $publicmode, "." or die $!;
204 git_for_each_ref('refs/tags', sub {
205 my ($objid,$objtype,$fullrefname,$tagname) = @_;
206 add_taint_by_tag($tagname,$objid);
213 die unless @ARGV >= 4;
214 $version = shift @ARGV;
215 $suite = shift @ARGV;
216 $tagname = shift @ARGV;
217 my $delibs = shift @ARGV;
218 foreach my $delib (split /\,/, $delibs) {
219 $deliberately{$delib} = 1;
223 sub deliberately ($) { return $deliberately{$_[0]}; }
227 return 0 unless $pkg_exists;
228 return 0 unless $pkg_secret;
230 # we suppose that NEW has a version which is already in our
231 # history, as otherwise the repo would have been blown away
233 if (deliberately('not-fast-forward')) {
234 add_taint(server_ref($suite),
235 "suite $suite when --deliberately-not-fast-forward".
236 " specified in signed tag $tagname for upload of".
237 " version $version into suite $suite");
238 return NOFFCHECK|FRESHREPO;
240 if (deliberately('include-questionable-history')) {
243 die "Package is in NEW and has not been accepted or rejected yet;".
244 " use a --deliberately option to specify whether you are".
245 " keeping or discarding the previously pushed history. ".
246 " Please RTFM dgit(1).\n";
249 sub action_push_confirm () {
252 my $freshrepo = shift @ARGV;
254 my $initq = $poldbh->prepare(<<END);
255 SELECT taint_id, gitobjid FROM taints t
256 WHERE (package = ? OR package = '')
258 $initq->execute($pkg);
261 my $chkinput = tempfile();
262 while (my $taint = $initq->fetchrow_hashref()) {
263 push @taintids, $taint->{taint_id};
264 print $chkinput $taint->{gitobjid}, "\n" or die $!;
266 flush $chkinput or die $!;
267 seek $chkinput,0,0 or die $!;
269 my $checkpid = open CHKOUT, "-|" // die $!;
271 open STDIN, "<&", $chkinput or die $!;
272 exec qw(git cat-file --batch) or die $!;
275 my ($taintinfoq,$overridesanyq,$untaintq,$overridesq);
277 my $overridesstmt = <<END;
278 SELECT deliberately FROM taintoverrides WHERE ( 1
280 my @overridesv = sort keys %deliberately;
281 $overridesstmt .= join '', (<<END x @overridesv);
284 $overridesstmt .= <<END;
286 ORDER BY deliberately ASC
291 while (my $taintid = shift @taintids) {
292 # git cat-file prints a spurious newline after it gets EOF
293 # This is not documented. I guess it might go away. So we
294 # just read what we expect and then let it get SIGPIPE.
296 die "$? $!" unless defined $_;
298 next if m/^\w+ missing$/;
299 die unless m/^(\w+) (\w+) (\d+)\s/;
300 my ($objid,$objtype,$nbytes) = ($1,$2,$3);
303 (read CHKOUT, $drop, $nbytes) == $nbytes or die;
305 $taintinfoq ||= $poldbh->prepare(<<END);
306 SELECT package, time, comment FROM taints WHERE taint_id = ?
308 $taintinfoq->execute($taintid);
310 my $ti = $taintinfoq->fetchrow_hashref();
313 my $timeshow = defined $ti->{time}
314 ? " at time ".strftime("%Y-%m-%d %H:%M:%S Z", gmtime $ti->{time})
316 my $pkgshow = length $ti->{package}
317 ? "package $ti->{package}"
322 History contains tainted $objtype $objid
323 Taint recorded$timeshow for $pkgshow
324 Reason: $ti->{comment}
327 $overridesq ||= $poldbh->prepare($overridesstmt);
328 $overridesq->execute(@overridesv, $taintid);
329 my ($ovwhy) = $overridesq->fetchrow_array();
330 if (!defined $ovwhy) {
331 $overridesanyq ||= $poldbh->prepare(<<END);
332 SELECT 1 FROM taintoverrides WHERE taint_id = ? LIMIT 1
334 $overridesanyq->execute($taintid);
335 my ($ovany) = $overridesanyq->fetchrow_array();
336 $stderr .= $ovany ? <<END : <<END;
337 Could be forced using --deliberately. Consult documentation.
339 Uncorrectable error. If confused, consult administrator.
344 Forcing due to --deliberately-$ovwhy
346 $untaintq ||= $poldbh->prepare(<<END);
347 DELETE FROM taints WHERE taint_id = ?
349 $untaintq->execute($taintid);
357 Rejecting push due to questionable history.
362 if (length $freshrepo) {
363 if (!good_suite_has_vsn_in_our_history()) {
364 stat $freshrepo or die "$freshrepo $!";
365 my $oldmode = ((stat _)[2]);
366 my $oldwrites = $oldmode & 0222;
367 # remove r and x bits which have corresponding w bits clear
368 my $newmode = $oldmode & ($oldwrites << 1) & ($oldwrites > 1);
369 chmod $newmode, $freshrepo or die $!;
376 sub action_check_list () {
377 opendir L, "$repos" or die "$repos $!";
378 while (defined (my $dent = readdir L)) {
379 next unless $dent =~ m/^($package_re)\.git$/;
382 next unless $pkg_exists;
383 next unless $pkg_secret;
384 print "$pkg\n" or die $!;
386 closedir L or die $!;
387 close STDOUT or die $!;
392 my $fn = ${*::}{"action_$action"};
401 poldb_setup(poldb_path($repos));
405 die unless defined $rcode;
407 eval { $poldbh->commit; };
408 last unless length $@;
410 die if $sleepy >= 20;
411 print STDERR "[policy database busy, retrying]\n";
417 print STDERR $stderr;