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 $action = shift @ARGV // die "need ACTION";
20 our $publicmode = 02775;
21 our $new_upload_propagation_slop = 3600*4 + 100;
26 our ($pkg_exists,$pkg_secret);
30 our ($version,$suite,$tagname);
33 # We assume that it is not possible for NEW to have a version older
36 # Whenever pushing, we check for
37 # source-package-local tainted history
38 # global tainted history
39 # can be overridden by --deliberately except for an admin prohib taint
41 # ALL of the following apply only if history is secret:
43 # if NEW has no version, or a version which is not in our history[1]
46 # if any suite's version is in our history[1], publish our history
47 # otherwise discard our history,
48 # tainting --deliberately-include-questionable-history
50 # if NEW has a version which is in our history[1]
52 # require explicit specification of one of
53 # --deliberately-include-questionable-history
54 # --deliberately-not-fast-forward
55 # (latter will taint old NEW version --d-i-q-h)
59 # [1] looking for the relevant git tag for the version number and not
60 # caring what that tag refers to.
62 # A wrinkle: if we approved a push recently, we treat NEW as having
63 # a version which is in our history. This is because the package may
64 # still be being uploaded. (We record this using the timestamp of the
65 # package's git repo directory.)
67 # We aim for the following invariants and properties:
69 # - .dsc of published dgit package will have corresponding publicly
70 # visible dgit-repo (soon)
72 # - when a new package is rejected we help maintainer avoid
73 # accidentally including bad objects in published dgit history
75 # - .dsc of NEW dgit package has corresponding dgit-repo but not
81 $!=0; $?=0; my $json = `dgit -d $distro archive-api-query $subpath`;
82 defined $json or die "$subpath $! $?";
83 return decode_json $json;
86 sub specific_suite_has_vsn_in_our_history ($) {
88 my $in_suite = apiquery "/dsc_in_suite/$suite/$pkg";
89 foreach my $entry (@$in_suite) {
90 my $vsn = $entry->{version};
91 die "$pkg ?" unless defined $vsn;
92 my $tag = debiantag $vsn;
93 $?=0; my $r = system qw(git show-ref --verify --quiet), $tag;
96 die "$pkg tag $tag $? $!";
101 sub new_has_vsn_in_our_history () {
102 stat $pkgdir or die "$pkgdir $!";
103 my $mtime = ((stat _)[9]);
104 my $age = time - $mtime;
105 return 1 if $age < $new_upload_propagation_slop;
106 return specific_suite_has_vsn_in_our_history('new');
109 sub good_suite_has_vsn_in_our_history () {
110 my $suites = apiquery "/suites";
111 foreach my $suitei (@$suites) {
112 my $suite = $suitei->{name};
113 die unless defined $suite;
114 next if $suite =~ m/\bnew$/;
115 return 1 if specific_suite_has_vsn_in_our_history($suite);
121 die unless @ARGV >= 1;
123 die unless $pkg =~ m/^$package_re$/;
125 $pkgdir = "$repos/$pkg";
126 if (!stat_exists $pkgdir) {
130 $pkg_secret = !!(~(stat _)[2] & 05);
135 my ($refobj, $reason);
137 my $tf = new File::Temp or die $!;
138 print $tf "$refobj^0\n" or die $!;
140 my $gcfpid = open GCF, "-|";
141 defined $gcfpid or die $!;
143 open STDIN, "<&", $tf or die $!;
144 exec 'git', 'cat-file';
150 m/^(\w+) (\w+) (\d+)\n/ or die "$_ ?";
156 if ($gitobjtype eq 'commit' or $gitobjtype eq 'tag') {
157 $!=0; read GCF, $gitobjdata, $bytes == $bytes
158 or die "$gitobjid $bytes $!";
162 $poldbh->do("INSERT INTO taints".
163 " (package, gitobjid, gitobjtype, gitobjdata, time, comment)",
164 " VALUES (?,?,?,?,?,?)", {},
165 $pkg, $gitobjid, $gitobjtype, $gitobjdata, time, $reason);
167 my $taint_id = $poldbh->last_insert_id(undef,undef,"taints","taint_id");
168 die unless defined $taint_id;
170 $poldbh->do("INSERT INTO taintoverrides".
171 " (taint_id, deliberately)",
172 " VALUES (?, 'include-questionable-history')", {},
176 sub add_taint_by_tag ($$) {
177 my ($tagname,$refobjid) = @_;
179 "tag $tagname referred to this object in git tree but all".
180 " previously pushed versions were found to have been".
181 " removed from NEW (ie, rejected) (or never arrived)");
184 sub action__check_package () {
186 return 0 unless $pkg_exists;
187 return 0 unless $pkg_secret;
189 chdir $pkgdir or die "$pkgdir $!";
190 return if new_has_vsn_in_our_history();
192 if (good_suite_has_vsn_in_our_history) {
193 chmod $publicmode, "." or die $!;
197 git_for_each_ref('refs/tags', sub {
198 my ($objid,$objtype,$fullrefname,$tagname) = @_;
199 add_taint_by_tag($tagname,$objid);
206 die unless @ARGV >= 4;
207 $version = shift @ARGV;
208 $suite = shift @ARGV;
209 $tagname = shift @ARGV;
210 my $delibs = shift @ARGV;
211 foreach my $delib (split /\,/, $delibs) {
212 $deliberately{$delib} = 1;
216 sub deliberately ($) { return $deliberately{$_[0]}; }
220 return 0 unless $pkg_exists;
221 return 0 unless $pkg_secret;
223 # we suppose that NEW has a version which is already in our
224 # history, as otherwise the repo would have been blown away
226 if (deliberately('not-fast-forward')) {
227 add_taint(server_ref($suite),
228 "suite $suite when --deliberately-not-fast-forward".
229 " specified in signed tag $tagname for upload of".
230 " version $version into suite $suite");
231 return NOFFCHECK|FRESHREPO;
233 if (deliberately('include-questionable-history')) {
236 die "Package is in NEW and has not been accepted or rejected yet;".
237 " use a --deliberately option to specify whether you are".
238 " keeping or discarding the previously pushed history. ".
239 " Please RTFM dgit(1).\n";
242 sub action_push_confirm () {
243 my $initq = $poldbh->prepare(<<END);
244 SELECT taint_id, gitobjid FROM taints t
245 WHERE (package = ? OR package = '')
247 $initq->execute($pkg);
250 my $chkinput = tempfile();
251 while (my $taint = $initq->fetchrow_hashref()) {
252 push @taintids, $taint->{taint_id};
253 print $chkinput $taint->{gitobjid}, "\n" or die $!;
255 flush $chkinput or die $!;
256 seek $chkinput,0,0 or die $!;
258 my $checkpid = open CHKOUT, "-|" // die $!;
260 open STDIN, "<&", $chkinput or die $!;
261 exec qw(git cat-file --batch) or die $!;
264 my ($taintinfoq,$overridesanyq,$untaintq,$overridesq);
266 my $overridesstmt = <<END;
267 SELECT deliberately FROM taintoverrides WHERE ( 1
269 my @overridesv = sort keys %deliberately;
270 $overridesstmt .= join '', (<<END x @overridesv);
273 $overridesstmt .= <<END;
275 ORDER BY deliberately ASC
280 while (my $taintid = shift @taintids) {
281 # git cat-file prints a spurious newline after it gets EOF
282 # This is not documented. I guess it might go away. So we
283 # just read what we expect and then let it get SIGPIPE.
285 die "$? $!" unless defined $_;
287 next if m/^\w+ missing$/;
288 die unless m/^(\w+) (\w+) (\d+)\s/;
289 my ($objid,$objtype,$nbytes) = ($1,$2,$3);
292 (read CHKOUT, $drop, $nbytes) == $nbytes or die;
294 $taintinfoq ||= $poldbh->prepare(<<END);
295 SELECT package, time, comment FROM taints WHERE taint_id = ?
297 $taintinfoq->execute($taintid);
299 my $ti = $taintinfoq->fetchrow_hashref();
302 my $timeshow = defined $ti->{time}
303 ? " at time ".strftime("%Y-%m-%d %H:%M:%S Z", gmtime $ti->{time})
305 my $pkgshow = length $ti->{package}
306 ? "package $ti->{package}"
311 History contains tainted $objtype $objid
312 Taint recorded$timeshow for $pkgshow
313 Reason: $ti->{comment}
316 $overridesq ||= $poldbh->prepare($overridesstmt);
317 $overridesq->execute(@overridesv, $taintid);
318 my ($ovwhy) = $overridesq->fetchrow_array();
319 if (!defined $ovwhy) {
320 $overridesanyq ||= $poldbh->prepare(<<END);
321 SELECT 1 FROM taintoverrides WHERE taint_id = ? LIMIT 1
323 $overridesanyq->execute($taintid);
324 my ($ovany) = $overridesanyq->fetchrow_array();
325 $stderr .= $ovany ? <<END : <<END;
326 Could be forced using --deliberately. Consult documentation.
328 Uncorrectable error. If confused, consult administrator.
333 Forcing due to --deliberately-$ovwhy
335 $untaintq ||= $poldbh->prepare(<<END);
336 DELETE FROM taints WHERE taint_id = ?
338 $untaintq->execute($taintid);
346 Rejecting push due to questionable history.
355 my $fn = ${*::}{"action_$action"};
364 poldb_setup(poldb_path($repos));
368 die unless defined $rcode;
370 eval { $poldbh->commit; };
371 last unless length $@;
373 die if $sleepy >= 20;
374 print STDERR "[policy database busy, retrying]\n";
380 print STDERR $stderr;