2 # dgit repos policy hook script for Debian
7 use File::Temp qw(tempfile);
11 use Debian::Dgit qw(:DEFAULT :policyflags);
12 use Debian::Dgit::Policy::Debian;
14 our $distro = shift @ARGV // die "need DISTRO";
15 our $repos = shift @ARGV // die "need DGIT-REPOS-DIR";
16 our $action = shift @ARGV // die "need ACTION";
18 our $publicmode = 02775;
19 our $new_upload_propagation_slop = 3600*4 + 100;
24 our ($pkg_exists,$pkg_secret);
28 our ($version,$suite,$tagname);
31 # We assume that it is not possible for NEW to have a version older
34 # Whenever pushing, we check for
35 # source-package-local tainted history
36 # global tainted history
37 # can be overridden by --deliberately except for an admin prohib taint
39 # ALL of the following apply only if history is secret:
41 # if NEW has no version, or a version which is not in our history[1]
44 # if any suite's version is in our history[1], publish our history
45 # otherwise discard our history,
46 # tainting --deliberately-include-questionable-history
48 # if NEW has a version which is in our history[1]
50 # require explicit specification of one of
51 # --deliberately-include-questionable-history
52 # --deliberately-not-fast-forward
53 # (latter will taint old NEW version --d-i-q-h)
57 # [1] looking for the relevant git tag for the version number and not
58 # caring what that tag refers to.
60 # A wrinkle: if we approved a push recently, we treat NEW as having
61 # a version which is in our history. This is because the package may
62 # still be being uploaded. (We record this using the timestamp of the
63 # package's git repo directory.)
65 # We aim for the following invariants and properties:
67 # - .dsc of published dgit package will have corresponding publicly
68 # visible dgit-repo (soon)
70 # - when a new package is rejected we help maintainer avoid
71 # accidentally including bad objects in published dgit history
73 # - .dsc of NEW dgit package has corresponding dgit-repo but not
79 $!=0; $?=0; my $json = `dgit -d $distro archive-api-query $subpath`;
80 defined $json or die "$subpath $! $?";
81 return decode_json $json;
84 sub specific_suite_has_vsn_in_our_history ($) {
86 my $in_suite = apiquery "/dsc_in_suite/$suite/$pkg";
87 foreach my $entry (@$in_suite) {
88 my $vsn = $entry->{version};
89 die "$pkg ?" unless defined $vsn;
90 my $tag = debiantag $vsn;
91 $?=0; my $r = system qw(git show-ref --verify --quiet), $tag;
94 die "$pkg tag $tag $? $!";
99 sub new_has_vsn_in_our_history () {
100 stat $pkgdir or die "$pkgdir $!";
101 my $mtime = ((stat _)[9]);
102 my $age = time - $mtime;
103 return 1 if $age < $new_upload_propagation_slop;
104 return specific_suite_has_vsn_in_our_history('new');
107 sub good_suite_has_vsn_in_our_history () {
108 my $suites = apiquery "/suites";
109 foreach my $suitei (@$suites) {
110 my $suite = $suitei->{name};
111 die unless defined $suite;
112 next if $suite =~ m/\bnew$/;
113 return 1 if specific_suite_has_vsn_in_our_history($suite);
119 die unless @ARGV >= 1;
121 die unless $pkg =~ m/^$package_re$/;
123 $pkgdir = "$repos/$pkg";
124 if (!stat_exists $pkgdir) {
128 $pkg_secret = !!(~(stat _)[2] & 05);
133 my ($refobj, $reason);
135 my $tf = new File::Temp or die $!;
136 print $tf "$refobj^0\n" or die $!;
138 my $gcfpid = open GCF, "-|";
139 defined $gcfpid or die $!;
141 open STDIN, "<&", $tf or die $!;
142 exec 'git', 'cat-file';
148 m/^(\w+) (\w+) (\d+)\n/ or die "$_ ?";
154 if ($gitobjtype eq 'commit' or $gitobjtype eq 'tag') {
155 $!=0; read GCF, $gitobjdata, $bytes == $bytes
156 or die "$gitobjid $bytes $!";
160 $poldbh->do("INSERT INTO taints".
161 " (package, gitobjid, gitobjtype, gitobjdata, time, comment)",
162 " VALUES (?,?,?,?,?,?)", {},
163 $pkg, $gitobjid, $gitobjtype, $gitobjdata, time, $reason);
165 my $taint_id = $poldbh->last_insert_id(undef,undef,"taints","taint_id");
166 die unless defined $taint_id;
168 $poldbh->do("INSERT INTO taintoverrides".
169 " (taint_id, deliberately)",
170 " VALUES (?, 'include-questionable-history')", {},
174 sub add_taint_by_tag ($$) {
175 my ($tagname,$refobjid) = @_;
177 "tag $tagname referred to this object in git tree but all".
178 " previously pushed versions were found to have been".
179 " removed from NEW (ie, rejected) (or never arrived)");
182 sub action__check_package () {
184 return 0 unless $pkg_exists;
185 return 0 unless $pkg_secret;
187 chdir $pkgdir or die "$pkgdir $!";
188 return if new_has_vsn_in_our_history();
190 if (good_suite_has_vsn_in_our_history) {
191 chmod $publicmode, "." or die $!;
195 git_for_each_ref('refs/tags', sub {
196 my ($objid,$objtype,$fullrefname,$tagname) = @_;
197 add_taint_by_tag($tagname,$objid);
204 die unless @ARGV >= 4;
205 $version = shift @ARGV;
206 $suite = shift @ARGV;
207 $tagname = shift @ARGV;
208 my $delibs = shift @ARGV;
209 foreach my $delib (split /\,/, $delibs) {
210 $deliberately{$delib} = 1;
214 sub deliberately ($) { return $deliberately{$_[0]}; }
218 return 0 unless $pkg_exists;
219 return 0 unless $pkg_secret;
221 # we suppose that NEW has a version which is already in our
222 # history, as otherwise the repo would have been blown away
224 if (deliberately('not-fast-forward')) {
225 add_taint(server_ref($suite),
226 "suite $suite when --deliberately-not-fast-forward".
227 " specified in signed tag $tagname for upload of".
228 " version $version into suite $suite");
229 return NOFFCHECK|FRESHREPO;
231 if (deliberately('include-questionable-history')) {
234 die "Package is in NEW and has not been accepted or rejected yet;".
235 " use a --deliberately option to specify whether you are".
236 " keeping or discarding the previously pushed history. ".
237 " Please RTFM dgit(1).\n";
240 sub action_push_confirm () {
241 my $initq = $poldbh->prepare(<<END);
242 SELECT taint_id, gitobjid FROM taints t
243 WHERE (package = ? OR package = '')
245 $initq->execute($pkg);
248 my $chkinput = tempfile();
249 while (my $taint = $initq->fetchrow_hashref()) {
250 push @taintids, $taint->{taint_id};
251 print $chkinput $taint->{gitobjid}, "\n" or die $!;
253 flush $chkinput or die $!;
254 seek $chkinput,0,0 or die $!;
256 my $checkpid = open CHKOUT, "-|" // die $!;
258 open STDIN, "<&", $chkinput or die $!;
259 exec qw(git cat-file --batch) or die $!;
262 my ($taintinfoq,$overridesanyq,$untaintq,$overridesq);
264 my $overridesstmt = <<END;
265 SELECT deliberately FROM taintoverrides WHERE ( 1
267 my @overridesv = sort keys %deliberately;
268 $overridesstmt .= join '', (<<END x @overridesv);
271 $overridesstmt .= <<END;
273 ORDER BY deliberately ASC
278 while (my $taintid = shift @taintids) {
279 # git cat-file prints a spurious newline after it gets EOF
280 # This is not documented. I guess it might go away. So we
281 # just read what we expect and then let it get SIGPIPE.
283 die "$? $!" unless defined $_;
285 next if m/^\w+ missing$/;
286 die unless m/^(\w+) (\w+) (\d+)\s/;
287 my ($objid,$objtype,$nbytes) = ($1,$2,$3);
290 (read CHKOUT, $drop, $nbytes) == $nbytes or die;
292 $taintinfoq ||= $poldbh->prepare(<<END);
293 SELECT package, time, comment FROM taints WHERE taint_id = ?
295 $taintinfoq->execute($taintid);
297 my $ti = $taintinfoq->fetchrow_hashref();
300 my $timeshow = defined $ti->{time}
301 ? " at time ".strftime("%Y-%m-%d %H:%M:%S Z", gmtime $ti->{time})
303 my $pkgshow = length $ti->{package}
304 ? "package $ti->{package}"
309 History contains tainted $objtype $objid
310 Taint recorded$timeshow for $pkgshow
311 Reason: $ti->{comment}
314 $overridesq ||= $poldbh->prepare($overridesstmt);
315 $overridesq->execute(@overridesv, $taintid);
316 my ($ovwhy) = $overridesq->fetchrow_array();
317 if (!defined $ovwhy) {
318 $overridesanyq ||= $poldbh->prepare(<<END);
319 SELECT 1 FROM taintoverrides WHERE taint_id = ? LIMIT 1
321 $overridesanyq->execute($taintid);
322 my ($ovany) = $overridesanyq->fetchrow_array();
323 $stderr .= $ovany ? <<END : <<END;
324 Could be forced using --deliberately. Consult documentation.
326 Uncorrectable error. If confused, consult administrator.
331 Forcing due to --deliberately-$ovwhy
333 $untaintq ||= $poldbh->prepare(<<END);
334 DELETE FROM taints WHERE taint_id = ?
336 $untaintq->execute($taintid);
344 Rejecting push due to questionable history.
353 my $fn = ${*::}{"action_$action"};
362 poldb_setup(poldb_path($repos));
366 die unless defined $rcode;
368 eval { $poldbh->commit; };
369 last unless length $@;
371 die if $sleepy >= 20;
372 print STDERR "[policy database busy, retrying]\n";
378 print STDERR $stderr;