2 # dgit repos policy hook script for Debian
9 use Debian::Dgit qw(:DEFAULT :policyflags);
11 our $distro = shift @ARGV // die "need DISTRO";
12 our $repos = shift @ARGV // die "need DGIT-REPOS-DIR";
13 our $action = shift @ARGV // die "need ACTION";
15 our $publicmode = 02775;
16 our $policydb = "dbi:SQLite:$repos/policy.sqlite3";
17 our $new_upload_propagation_slop = 3600*4 + 100;
22 our ($pkg_exists,$pkg_secret);
26 our ($version,$suite,$tagname);
29 # We assume that it is not possible for NEW to have a version older
32 # Whenever pushing, we check for
33 # source-package-local tainted history
34 # global tainted history
35 # can be overridden by --deliberately except for an admin prohib taint
37 # ALL of the following apply only if history is secret:
39 # if NEW has no version, or a version which is not in our history[1]
42 # if any suite's version is in our history[1], publish our history
43 # otherwise discard our history,
44 # tainting --deliberately-include-questionable-history
46 # if NEW has a version which is in our history[1]
48 # require explicit specification of one of
49 # --deliberately-include-questionable-history
50 # --deliberately-not-fast-forward
51 # (latter will taint old NEW version --d-i-q-h)
55 # [1] looking for the relevant git tag for the version number and not
56 # caring what that tag refers to.
58 # A wrinkle: if we approved a push recently, we treat NEW as having
59 # a version which is in our history. This is because the package may
60 # still be being uploaded. (We record this using the timestamp of the
61 # package's git repo directory.)
65 $poldbh ||= DBI->connect($policydb,'','', {
66 RaiseError=>1, PrintError=>1, AutoCommit=>0
68 $poldbh->do("PRAGMA foreign_keys = ON");
71 CREATE TABLE IF NOT EXISTS taints (
72 taint_id INTEGER NOT NULL PRIMARY KEY ASC AUTOINCREMENT,
73 package TEXT NOT NULL,
74 gitobjid TEXT NOT NULL,
75 comment TEXT NOT NULL,
82 CREATE INDEX IF NOT EXISTS taints_by_package
83 ON taints (package, gitobject)
85 # any one of of the listed deliberatelies will override its taint
87 CREATE TABLE IF NOT EXISTS taintoverrides (
88 taint_id INTEGER NOT NULL
89 REFERENCES taints (taint_id)
92 DEFERRABLE INITIALLY DEFERRED,
93 deliberately TEXT NOT NULL,
94 PRIMARY KEY (taint_id, deliberately)
102 $!=0; $?=0; my $json = `dgit -d $distro archive-api-query $subpath`;
103 defined $json or die "$subpath $! $?";
104 return decode_json $json;
107 sub specific_suite_has_vsn_in_our_history ($) {
109 my $in_new = apiquery "/dsc_in_suite/$suite/$pkg";
110 foreach my $entry (@$in_new) {
111 my $vsn = $entry->{version};
112 die "$pkg ?" unless defined $vsn;
113 my $tag = debiantag $vsn;
114 $?=0; my $r = system qw(git show-ref --verify --quiet), $tag;
117 die "$pkg tag $tag $? $!";
122 sub new_has_vsn_in_our_history () {
123 stat $pkgdir or die "$pkgdir $!";
124 my $mtime = ((stat _)[9]);
125 my $age = time - $mtime;
126 return 1 if $age < $new_upload_propagation_slop;
127 return specific_suite_has_vsn_in_our_history('new');
130 sub good_suite_has_vsn_in_our_history () {
131 my $suites = apiquery "/suites";
132 foreach my $suitei (@$suites) {
133 my $suite = $suitei->{name};
134 die unless defined $suite;
135 next if $suite =~ m/\bnew$/;
136 return 1 if specific_suite_has_vsn_in_our_history($suite);
142 die unless @ARGV > 1;
144 die if $pkg =~ m#[^-+.0-9a-z]#;
145 die unless $pkg =~ m#^[^-]#;
147 $pkgdir = "$repos/$pkg";
149 die "$pkgdir $!" unless $!==ENOENT;
153 $pkg_secret = !!(~(stat _)[2] & 05);
157 my ($refobj, $reason);
159 my $tf = new File::Temp or die $!;
160 print $tf "$refobj^0\n" or die $!;
162 my $gcfpid = open GCF, "-|";
163 defined $gcfpid or die $!;
165 open STDIN, "<&", $tf or die $!;
166 exec 'git', 'cat-file';
172 m/^(\w+) (\w+) (\d+)\n/ or die "$objline ?";
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 (?, '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 chdir $pkgdir or die "$pkgdir $!";
212 return if new_has_vsn_in_our_history();
214 if (good_suite_has_vsn_in_our_history) {
215 chmod $publicmode, "." or die $!;
219 git_for_each_ref('refs/tags', sub {
220 my ($objid,$objtype,$fullrefname,$tagname) = @_;
221 add_taint_by_tag($tagname,$refobjid);
223 $?=0; $!=0; close TAGL or die "git for-each-ref $? $!";
229 die unless @ARGV >= 4;
230 $version = shift @ARGV;
231 $suite = shift @ARGV;
232 $tagname = shift @ARGV;
233 my $delibs = shift @ARGV;
234 foreach my $delib (split /\,/, $delibs) {
235 $deliberately{$delib} = 1;
239 sub deliberately ($) { return $deliberately{$_[0]}; }
243 return 0 unless $pkg_exists;
244 return 0 unless $pkg_secret;
246 # we suppose that NEW has a version which is already in our
247 # history, as otherwise the repo would have been blown away
249 if (deliberately('not-fast-forward')) {
250 add_taint(server_ref($suite),
251 "suite $suite when --deliberately-not-fast-forward".
252 " specified in signed tag $tagname for upload of".
253 " version $version into suite $suite");
254 return NOFFCHECK|FRESHREPO;
256 if (deliberately('include-questionable-history')) {
259 die "Package is in NEW and has not been accepted or rejected yet;".
260 " use a --deliberately option to specify whether you are".
261 " keeping or discarding the previously pushed history. ".
262 " Please RTFM dgit(1).\n";
265 sub action_push_confirm () {
266 my $initq = $dbh->prepare(<<END);
267 SELECT taint_id, gitobjid FROM taints t
268 WHERE (package = ? OR package = '')
270 $initq->execute($pkg);
273 my $chkinput = tempfile();
274 while (my $taint = $initq->fetchrow_hashref()) {
275 push @taintids, $taint->{taint_id};
276 print $chkinput, $taint->{gitobjid}, "\n" or die $!;
278 flush $chkinput or die $!;
279 seek $chkinput,0,0 or die $!;
281 my $checkpid = open2("<&$chkinput", \*CHKOUT, qw(git cat-file --batch));
284 my ($taintinfoq,$overridesanyq,$untaintq,$overridesq);
286 my $overridesstmt = <<END;
287 SELECT deliberately FROM taintoverrides WHERE (
288 my @overridesv = sort keys %deliberately;
289 $overridesstmt .= join <<END, (<<END x @overridesv);
295 $overridesstmt .= <<END;
297 ORDER BY deliberately ASC
303 my $taintid = shift @taintids;
304 die unless defined $taintid;
306 next if m/^\w+ missing$/;
307 die unless m/^(\w+) (\s+) (\d+)\s/;
308 my ($objid,$objtype,$nbytes) = @_;
310 read CHKOUT, $_, $nbytes == $bytes or last;
312 $taintinfoq ||= $dbh->prepare(<<END);
313 SELECT package, time, comment FROM taints WHERE taint_id = ?
315 $taintinfoq->execute($taintid);
317 my $ti = $taintinfoq->fetchrow_hashref();
320 my $timeshow = defined $ti->{time}
321 ? " at time ".strftime("%Y-%m-%d %H:%M:%S Z", gmtime $ti->time)
323 my $pkgshow = length $ti->{package}
324 ? "package $ti->{package}"
329 History contains tainted $objtype $objid
330 Taint recorded$timeshow for $pkginfo
331 Reason: $ti->{comment}
334 $overridesq ||= $dbh->prepare($overridesstmt);
335 $overridesq->execute(@overridesv, $taintid);
336 my ($ovwhy) = $overridesq->fetchrow_array();
337 if (!defined $ovwhy) {
338 $overridesanyq ||= $dbh->prepare(<<END);
339 SELECT 1 FROM taintoverrides WHERE taint_id = ? LIMIT 1
341 $overridesanyq->execute($taintid);
342 my ($ovany) = $overridesanyq->fetchrow_array();
343 $stderr .= $ovany ? <<END : <<END;
344 Could be forced using --deliberately. Consult documentation.
346 Uncorrectable error. If confused, consult administrator.
351 Forcing due to --deliberately-$ovwhy
353 $untaintq ||= $dbh->prepare(<<END);
354 DELETE FROM taints WHERE taint_id = ?
356 $untaintq->execute($taint_id);
360 $?=0; my $gotpid = waitpid $checkpid, WNOHANG;
361 die "@taintids $gotpid $? $!";
367 Rejecting push due to questionable history.
376 my $fn = ${*::}{"action__$cmd"};
388 die unless defined $rcode;
390 eval { $poldbh->commit; };
391 last unless length $@;
393 die if $sleepy >= 20;
394 print STDERR "[policy database busy, retrying]\n";
400 print STDERR $stderr;