chiark / gitweb /
Turn all perl warnings into errors
[dgit.git] / infra / dgit-repos-policy-debian
1 #!/usr/bin/perl -w
2 # dgit repos policy hook script for Debian
3
4 use strict;
5 $SIG{__WARN__} = sub { die $_[0]; };
6
7 use POSIX;
8 use JSON;
9 use File::Temp qw(tempfile);
10 use DBI;
11 use IPC::Open2;
12
13 use Debian::Dgit qw(:DEFAULT :policyflags);
14 use Debian::Dgit::Policy::Debian;
15
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";
19
20 our $publicmode = 02775;
21 our $new_upload_propagation_slop = 3600*4 + 100;
22
23 our $poldbh;
24 our $pkg;
25 our $pkgdir;
26 our ($pkg_exists,$pkg_secret);
27
28 our $stderr;
29
30 our ($version,$suite,$tagname);
31 our %deliberately;
32
33 # We assume that it is not possible for NEW to have a version older
34 # than sid.
35
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
40
41 # ALL of the following apply only if history is secret:
42
43 # if NEW has no version, or a version which is not in our history[1]
44 #   (always)
45 #   check all suites
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
49
50 # if NEW has a version which is in our history[1]
51 #   (on push only)
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)
56 #   (otherwise)
57 #   leave it be
58
59 # [1] looking for the relevant git tag for the version number and not
60 #    caring what that tag refers to.
61 #
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.)
66
67 # We aim for the following invariants and properties:
68 #
69 # - .dsc of published dgit package will have corresponding publicly
70 #   visible dgit-repo (soon)
71 #
72 # - when a new package is rejected we help maintainer avoid
73 #   accidentally including bad objects in published dgit history
74 #
75 # - .dsc of NEW dgit package has corresponding dgit-repo but not
76 #   publicly readable
77
78 sub apiquery ($) {
79     my ($subpath) = @_;
80     local $/=undef;
81     $!=0; $?=0; my $json = `dgit -d $distro archive-api-query $subpath`;
82     defined $json or die "$subpath $! $?";
83     return decode_json $json;
84 }
85
86 sub specific_suite_has_vsn_in_our_history ($) {
87     my ($suite) = @_;
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;
94         return 1 if !$r;
95         next if $r==256;
96         die "$pkg tag $tag $? $!";
97     }
98     return 0;
99 }
100
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');
107 }
108
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);
116     }
117     return 0;
118 }
119
120 sub getpackage () {
121     die unless @ARGV >= 1;
122     $pkg = shift @ARGV;
123     die unless $pkg =~ m/^$package_re$/;
124
125     $pkgdir = "$repos/$pkg";
126     if (!stat_exists $pkgdir) {
127         $pkg_exists = 0;
128     } else {
129         $pkg_exists = 1;
130         $pkg_secret = !!(~(stat _)[2] & 05);
131     }
132 }
133
134 sub add_taint ($$) {
135     my ($refobj, $reason);
136
137     my $tf = new File::Temp or die $!;
138     print $tf "$refobj^0\n" or die $!;
139
140     my $gcfpid = open GCF, "-|";
141     defined $gcfpid or die $!;
142     if (!$gcfpid) {
143         open STDIN, "<&", $tf or die $!;
144         exec 'git', 'cat-file';
145         die $!;
146     }
147
148     close $tf or die $!;
149     $_ = <GCF>;
150     m/^(\w+) (\w+) (\d+)\n/ or die "$_ ?";
151     my $gitobjid = $1;
152     my $gitobjtype = $2;
153     my $bytes = $3;
154
155     my $gitobjdata;
156     if ($gitobjtype eq 'commit' or $gitobjtype eq 'tag') {
157         $!=0; read GCF, $gitobjdata, $bytes == $bytes
158             or die "$gitobjid $bytes $!";
159     }
160     close GCF;
161
162     $poldbh->do("INSERT INTO taints".
163                 " (package, gitobjid, gitobjtype, gitobjdata, time, comment)",
164                 " VALUES (?,?,?,?,?,?)", {},
165                 $pkg, $gitobjid, $gitobjtype, $gitobjdata, time, $reason);
166
167     my $taint_id = $poldbh->last_insert_id(undef,undef,"taints","taint_id");
168     die unless defined $taint_id;
169
170     $poldbh->do("INSERT INTO taintoverrides".
171                 " (taint_id, deliberately)",
172                 " VALUES (?, 'include-questionable-history')", {},
173                 $taint_id);
174 }
175
176 sub add_taint_by_tag ($$) {
177     my ($tagname,$refobjid) = @_;
178     add_taint($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)");
182 }
183
184 sub action__check_package () {
185     getpackage();
186     return 0 unless $pkg_exists;
187     return 0 unless $pkg_secret;
188
189     chdir $pkgdir or die "$pkgdir $!";
190     return if new_has_vsn_in_our_history();
191
192     if (good_suite_has_vsn_in_our_history) {
193         chmod $publicmode, "." or die $!;
194         return 0;
195     }
196
197     git_for_each_ref('refs/tags', sub {
198         my ($objid,$objtype,$fullrefname,$tagname) = @_;
199         add_taint_by_tag($tagname,$objid);
200     });
201
202     return FRESHREPO;
203 }
204
205 sub getpushinfo () {
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;
213     }
214 }
215
216 sub deliberately ($) { return $deliberately{$_[0]}; }
217
218 sub action_push () {
219     getpackage();
220     return 0 unless $pkg_exists;
221     return 0 unless $pkg_secret;
222
223     # we suppose that NEW has a version which is already in our
224     # history, as otherwise the repo would have been blown away
225
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;
232     }
233     if (deliberately('include-questionable-history')) {
234         return 0;
235     }
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";
240 }
241
242 sub action_push_confirm () {
243     my $initq = $poldbh->prepare(<<END);
244         SELECT taint_id, gitobjid FROM taints t
245             WHERE (package = ? OR package = '')
246 END
247     $initq->execute($pkg);
248
249     my @taintids;
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 $!;
254     }
255     flush $chkinput or die $!;
256     seek $chkinput,0,0 or die $!;
257
258     my $checkpid = open CHKOUT, "-|" // die $!;
259     if (!$checkpid) {
260         open STDIN, "<&", $chkinput or die $!;
261         exec qw(git cat-file --batch) or die $!;
262     }
263
264     my ($taintinfoq,$overridesanyq,$untaintq,$overridesq);
265
266     my $overridesstmt = <<END;
267         SELECT deliberately FROM taintoverrides WHERE ( 1
268 END
269     my @overridesv = sort keys %deliberately;
270     $overridesstmt .= join '', (<<END x @overridesv);
271             OR deliberately = ?
272 END
273     $overridesstmt .= <<END;
274         ) AND taint_id = ?
275         ORDER BY deliberately ASC
276 END
277
278     my $mustreject=0;
279
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.
284         $!=0; $_ = <CHKOUT>;
285         die "$? $!" unless defined $_;
286
287         next if m/^\w+ missing$/;
288         die unless m/^(\w+) (\w+) (\d+)\s/;
289         my ($objid,$objtype,$nbytes) = ($1,$2,$3);
290
291         my $drop;
292         (read CHKOUT, $drop, $nbytes) == $nbytes or die;
293
294         $taintinfoq ||= $poldbh->prepare(<<END);
295             SELECT package, time, comment FROM taints WHERE taint_id =  ?
296 END
297         $taintinfoq->execute($taintid);
298
299         my $ti = $taintinfoq->fetchrow_hashref();
300         die unless $ti;
301
302         my $timeshow = defined $ti->{time}
303             ? " at time ".strftime("%Y-%m-%d %H:%M:%S Z", gmtime $ti->{time})
304             : "";
305         my $pkgshow = length $ti->{package}
306             ? "package $ti->{package}"
307             : "any package";
308
309         $stderr .= <<END;
310
311 History contains tainted $objtype $objid
312 Taint recorded$timeshow for $pkgshow
313 Reason: $ti->{comment}
314 END
315
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
322 END
323             $overridesanyq->execute($taintid);
324             my ($ovany) = $overridesanyq->fetchrow_array();
325             $stderr .= $ovany ? <<END : <<END;
326 Could be forced using --deliberately.  Consult documentation.
327 END
328 Uncorrectable error.  If confused, consult administrator.
329 END
330             $mustreject = 1;
331         } else {
332             $stderr .= <<END;
333 Forcing due to --deliberately-$ovwhy
334 END
335             $untaintq ||= $poldbh->prepare(<<END);
336                 DELETE FROM taints WHERE taint_id = ?
337 END
338             $untaintq->execute($taintid);
339         }
340     }
341     close CHKOUT;
342
343     if ($mustreject) {
344         $stderr .= <<END;
345
346 Rejecting push due to questionable history.
347 END
348         return 1;
349     }
350
351     return 0;
352 }
353
354 $action =~ y/-/_/;
355 my $fn = ${*::}{"action_$action"};
356 if (!$fn) {
357     exit 0;
358 }
359
360 my $sleepy=0;
361 our $rcode = 127;
362
363 for (;;) {
364     poldb_setup(poldb_path($repos));
365     $stderr = '';
366
367     $rcode = $fn->();
368     die unless defined $rcode;
369
370     eval { $poldbh->commit; };
371     last unless length $@;
372
373     die if $sleepy >= 20;
374     print STDERR "[policy database busy, retrying]\n";
375     sleep ++$sleepy;
376
377     $poldbh->rollback;
378 }
379
380 print STDERR $stderr;
381 exit $rcode;