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);
24 our ($version,$suite,$tagname);
27 # We assume that it is not possible for NEW to have a version older
30 # Whenever pushing, we check for
31 # source-package-local tainted history
32 # global tainted history
33 # can be overridden by --deliberately except for an admin prohib taint
35 # ALL of the following apply only if history is secret:
37 # if NEW has no version, or a version which is not in our history[1]
40 # if any suite's version is in our history[1], publish our history
41 # otherwise discard our history,
42 # tainting --deliberately-include-questionable-history
44 # if NEW has a version which is in our history[1]
46 # require explicit specification of one of
47 # --deliberately-include-questionable-history
48 # --deliberately-not-fast-forward
49 # (latter will taint old NEW version --d-i-q-h)
53 # [1] looking for the relevant git tag for the version number and not
54 # caring what that tag refers to.
56 # A wrinkle: if we approved a push recently, we treat NEW as having
57 # a version which is in our history. This is because the package may
58 # still be being uploaded. (We record this using the timestamp of the
59 # package's git repo directory.)
63 $poldbh = DBI->connect($policydb,'','', {
64 RaiseError=>1, PrintError=>1, AutoCommit=>0
66 $poldbh->do("PRAGMA foreign_keys = ON");
69 CREATE TABLE IF NOT EXISTS taints (
70 taint_id INTEGER NOT NULL PRIMARY KEY ASC AUTOINCREMENT,
71 package TEXT NOT NULL,
72 gitobjid TEXT NOT NULL,
73 comment TEXT NOT NULL,
80 CREATE INDEX IF NOT EXISTS taints_by_package
81 ON taints (package, gitobject)
83 # any one of of the listed deliberatelies will override its taint
85 CREATE TABLE IF NOT EXISTS taintoverrides (
86 taint_id INTEGER NOT NULL
87 REFERENCES taints (taint_id)
90 DEFERRABLE INITIALLY DEFERRED,
91 deliberately TEXT NOT NULL,
92 PRIMARY KEY (taint_id, deliberately)
104 $!=0; $?=0; my $json = `dgit -d $distro archive-api-query $subpath`;
105 defined $json or die "$subpath $! $?";
106 return decode_json $json;
109 sub specific_suite_has_vsn_in_our_history ($) {
111 my $in_new = apiquery "/dsc_in_suite/$suite/$pkg";
112 foreach my $entry (@$in_new) {
113 my $vsn = $entry->{version};
114 die "$pkg ?" unless defined $vsn;
115 my $tag = debiantag $vsn;
116 $?=0; my $r = system qw(git show-ref --verify --quiet), $tag;
119 die "$pkg tag $tag $? $!";
124 sub new_has_vsn_in_our_history () {
125 stat $pkgdir or die "$pkgdir $!";
126 my $mtime = ((stat _)[9]);
127 my $age = time - $mtime;
128 return 1 if $age < $new_upload_propagation_slop;
129 return specific_suite_has_vsn_in_our_history('new');
132 sub good_suite_has_vsn_in_our_history () {
133 my $suites = apiquery "/suites";
134 foreach my $suitei (@$suites) {
135 my $suite = $suitei->{name};
136 die unless defined $suite;
137 next if $suite =~ m/\bnew$/;
138 return 1 if specific_suite_has_vsn_in_our_history($suite);
144 die unless @ARGV > 1;
146 die if $pkg =~ m#[^-+.0-9a-z]#;
147 die unless $pkg =~ m#^[^-]#;
149 $pkgdir = "$repos/$pkg";
151 die "$pkgdir $!" unless $!==ENOENT;
155 $pkg_secret = !!(~(stat _)[2] & 05);
159 my ($refobj, $reason);
161 my $tf = new File::Temp or die $!;
162 print $tf "$refobj^0\n" or die $!;
164 my $gcfpid = open GCF, "-|";
165 defined $gcfpid or die $!;
167 open STDIN, "<&", $tf or die $!;
168 exec 'git', 'cat-file';
174 m/^(\w+) (\w+) (\d+)\n/ or die "$objline ?";
180 if ($gitobjtype eq 'commit' or $gitobjtype eq 'tag') {
181 $!=0; read GCF, $gitobjdata, $bytes == $bytes
182 or die "$gitobjid $bytes $!";
186 $poldbh->do("INSERT INTO taints".
187 " (package, gitobjid, gitobjtype, gitobjdata, time, comment)",
188 " VALUES (?,?,?,?,?,?)", {},
189 $pkg, $gitobjid, $gitobjtype, $gitobjdata, time, $reason);
191 my $taint_id = $poldbh->last_insert_id(undef,undef,"taints","taint_id");
192 die unless defined $taint_id;
194 $poldbh->do("INSERT INTO taintoverrides".
195 " (taint_id, deliberately)",
196 " VALUES (?, 'include-questionable-history')", {},
200 sub add_taint_by_tag ($$) {
201 my ($tagname,$refobjid) = @_;
203 "tag $tagname referred to this object in git tree but all".
204 " previously pushed versions were found to have been".
205 " removed from NEW (ie, rejected) (or never arrived)");
208 sub action__check_package () {
210 return 0 unless $pkg_exists;
211 return 0 unless $pkg_secret;
213 chdir $pkgdir or die "$pkgdir $!";
214 return if new_has_vsn_in_our_history();
216 if (good_suite_has_vsn_in_our_history) {
217 chmod $publicmode, "." or die $!;
221 git_for_each_ref('refs/tags', sub {
222 my ($objid,$objtype,$fullrefname,$tagname) = @_;
223 add_taint_by_tag($tagname,$refobjid);
225 $?=0; $!=0; close TAGL or die "git for-each-ref $? $!";
231 die unless @ARGV >= 4;
232 $version = shift @ARGV;
233 $suite = shift @ARGV;
234 $tagname = shift @ARGV;
235 my $delibs = shift @ARGV;
236 foreach my $delib (split /\,/, $delibs) {
237 $deliberately{$delib} = 1;
241 sub deliberately ($) { return $deliberately{$_[0]}; }
245 return 0 unless $pkg_exists;
246 return 0 unless $pkg_secret;
248 # we suppose that NEW has a version which is already in our
249 # history, as otherwise the repo would have been blown away
251 if (deliberately('not-fast-forward')) {
252 add_taint(server_ref($suite),
253 "suite $suite when --deliberately-not-fast-forward".
254 " specified in signed tag $tagname for upload of".
255 " version $version into suite $suite");
256 return NOFFCHECK|FRESHREPO;
258 if (deliberately('include-questionable-history')) {
261 die "Package is in NEW and has not been accepted or rejected yet;".
262 " use a --deliberately option to specify whether you are".
263 " keeping or discarding the previously pushed history. ".
264 " Please RTFM dgit(1).\n";
267 sub action_push_confirm () {
268 my $initq = $dbh->prepare(<<END);
269 SELECT taint_id, gitobjid FROM taints t
270 WHERE (package = ? OR package = '')
272 $initq->execute($pkg);
275 my $chkinput = tempfile();
276 while (my $taint = $initq->fetchrow_hashref()) {
277 push @taintids, $taint->{taint_id};
278 print $chkinput, $taint->{gitobjid}, "\n" or die $!;
280 flush $chkinput or die $!;
281 seek $chkinput,0,0 or die $!;
283 my $checkpid = open2("<&$chkinput", \*CHKOUT, qw(git cat-file --batch));
286 my ($taintinfoq,$overridesanyq,$untaintq,$overridesq);
288 my $overridesstmt = <<END;
289 SELECT deliberately FROM taintoverrides WHERE (
290 my @overridesv = sort keys %deliberately;
291 $overridesstmt .= join <<END, (<<END x @overridesv);
297 $overridesstmt .= <<END;
299 ORDER BY deliberately ASC
305 my $taintid = shift @taintids;
306 die unless defined $taintid;
308 next if m/^\w+ missing$/;
309 die unless m/^(\w+) (\s+) (\d+)\s/;
310 my ($objid,$objtype,$nbytes) = @_;
312 read CHKOUT, $_, $nbytes == $bytes or last;
314 $taintinfoq ||= $dbh->prepare(<<END);
315 SELECT package, time, comment FROM taints WHERE taint_id = ?
317 $taintinfoq->execute($taintid);
319 my $ti = $taintinfoq->fetchrow_hashref();
322 my $timeshow = defined $ti->{time}
323 ? " at time ".strftime("%Y-%m-%d %H:%M:%S Z", gmtime $ti->time)
325 my $pkgshow = length $ti->{package}
326 ? "package $ti->{package}"
331 History contains tainted $objtype $objid
332 Taint recorded$timeshow for $pkginfo
333 Reason: $ti->{comment}
336 $overridesq ||= $dbh->prepare($overridesstmt);
337 $overridesq->execute(@overridesv, $taintid);
338 my ($ovwhy) = $overridesq->fetchrow_array();
339 if (!defined $ovwhy) {
340 $overridesanyq ||= $dbh->prepare(<<END);
341 SELECT 1 FROM taintoverrides WHERE taint_id = ? LIMIT 1
343 $overridesanyq->execute($taintid);
344 my ($ovany) = $overridesanyq->fetchrow_array();
345 print STDERR $ovany ? <<END : <<END;
346 Could be forced using --deliberately. Consult documentation.
348 Uncorrectable error. If confused, consult administrator.
353 Forcing due to --deliberately-$ovwhy
355 $untaintq ||= $dbh->prepare(<<END);
356 DELETE FROM taints WHERE taint_id = ?
358 $untaintq->execute($taint_id);
362 $?=0; my $gotpid = waitpid $checkpid, WNOHANG;
363 die "@taintids $gotpid $? $!";
369 Rejecting push due to questionable history.
382 my $fn = ${*::}{"action__$cmd"};
390 die unless defined $rcode;