chiark / gitweb /
f2f30884854a83e2403b4142235f8fe4617ecd4c
[dgit.git] / infra / dgit-repos-server
1 #!/usr/bin/perl -w
2 # dgit-repos-server
3 #
4 # usages:
5 #   dgit-repos-server DISTRO DISTRO-DIR AUTH-SPEC [<settings>] --ssh
6 #   dgit-repos-server DISTRO DISTRO-DIR AUTH-SPEC [<settings>] --cron
7 # settings
8 #   --repos=GIT-REPOS-DIR      default DISTRO-DIR/repos/
9 #   --suites=SUITES-FILE       default DISTRO-DIR/suites
10 #   --policy-hook=POLICY-HOOK  default DISTRO-DIR/policy-hook
11 #   --dgit-live=DGIT-LIVE-DIR  default DISTRO-DIR/dgit-live
12 # (DISTRO-DIR is not used other than as default and to pass to policy hook)
13 # internal usage:
14 #  .../dgit-repos-server --pre-receive-hook PACKAGE
15 #
16 # Invoked as the ssh restricted command
17 #
18 # Works like git-receive-pack
19 #
20 # SUITES is the name of a file which lists the permissible suites
21 # one per line (#-comments and blank lines ignored)
22 #
23 # AUTH-SPEC is a :-separated list of
24 #   KEYRING.GPG,AUTH-SPEC
25 # where AUTH-SPEC is one of
26 #   a
27 #   mDM.TXT
28 # (With --cron AUTH-SPEC is not used and may be the empty string.)
29
30 use strict;
31 $SIG{__WARN__} = sub { die $_[0]; };
32
33 # DGIT-REPOS-DIR contains:
34 # git tree (or other object)      lock (in acquisition order, outer first)
35 #
36 #  _tmp/PACKAGE_prospective       ! } SAME.lock, held during receive-pack
37 #
38 #  _tmp/PACKAGE_incoming$$        ! } SAME.lock, held during receive-pack
39 #  _tmp/PACKAGE_incoming$$_fresh  ! }
40 #
41 #  PACKAGE.git                      } PACKAGE.git.lock
42 #  PACKAGE_garbage                  }   (also covers executions of
43 #  PACKAGE_garbage-old              }    policy hook script for PACKAGE)
44 #  PACKAGE_garbage-tmp              }
45 #  policy*                          } (for policy hook script, covered by
46 #                                   }  lock only when invoked for a package)
47 #
48 # leaf locks, held during brief operaton only:
49 #
50 #  _empty                           } SAME.lock
51 #  _empty.new                       }
52 #
53 #  _template                        } SAME.lock
54 #
55 # locks marked ! may be held during client data transfer
56
57 # What we do on push is this:
58 #  - extract the destination repo name
59 #  - make a hardlink clone of the destination repo
60 #  - provide the destination with a stunt pre-receive hook
61 #  - run actual git-receive-pack with that new destination
62 #   as a result of this the stunt pre-receive hook runs; it does this:
63 #    + understand what refs we are allegedly updating and
64 #      check some correspondences:
65 #        * we are updating only refs/tags/debian/* and refs/dgit/*
66 #        * and only one of each
67 #        * and the tag does not already exist
68 #      and
69 #        * recover the suite name from the destination refs/dgit/ ref
70 #    + disassemble the signed tag into its various fields and signature
71 #      including:
72 #        * parsing the first line of the tag message to recover
73 #          the package name, version and suite
74 #        * checking that the package name corresponds to the dest repo name
75 #        * checking that the suite name is as recovered above
76 #    + verify the signature on the signed tag
77 #      and if necessary check that the keyid and package are listed in dm.txt
78 #    + check various correspondences:
79 #        * the signed tag must refer to a commit
80 #        * the signed tag commit must be the refs/dgit value
81 #        * the name in the signed tag must correspond to its ref name
82 #        * the tag name must be debian/<version> (massaged as needed)
83 #        * the suite is one of those permitted
84 #        * the signed tag has a suitable name
85 #        * run the "push" policy hook
86 #        * replay prevention for --deliberately-not-fast-forward
87 #        * check the commit is a fast forward
88 #        * handle a request from the policy hook for a fresh repo
89 #    + push the signed tag and new dgit branch to the actual repo
90 #
91 # If the destination repo does not already exist, we need to make
92 # sure that we create it reasonably atomically, and also that
93 # we don't every have a destination repo containing no refs at all
94 # (because such a thing causes git-fetch-pack to barf).  So then we
95 # do as above, except:
96 #  - before starting, we take out our own lock for the destination repo
97 #  - we create a prospective new destination repo by making a copy
98 #    of _template
99 #  - we use the prospective new destination repo instead of the
100 #    actual new destination repo (since the latter doesn't exist)
101 #  - after git-receive-pack exits, we
102 #    + check that the prospective repo contains a tag and head
103 #    + rename the prospective destination repo into place
104 #
105 # Cleanup strategy:
106 #  - We are crash-only
107 #  - Temporary working trees and their locks are cleaned up
108 #    opportunistically by a program which tries to take each lock and
109 #    if successful deletes both the tree and the lockfile
110 #  - Prospective working trees and their locks are cleaned up by
111 #    a program which tries to take each lock and if successful
112 #    deletes any prospective working tree and the lock (but not
113 #    of course any actual tree)
114 #  - It is forbidden to _remove_ the lockfile without removing
115 #    the corresponding temporary tree, as the lockfile is also
116 #    a stampfile whose presence indicates that there may be
117 #    cleanup to do
118 #
119 # Policy hook script is invoked like this:
120 #   POLICY-HOOK-SCRIPT DISTRO DGIT-REPOS-DIR DGIT-LIVE-DIR DISTRO-DIR ACTION...
121 # ie.
122 #   POLICY-HOOK-SCRIPT ... check-list [...]
123 #   POLICY-HOOK-SCRIPT ... check-package PACKAGE [...]
124 #   POLICY-HOOK-SCRIPT ... push PACKAGE \
125 #         VERSION SUITE TAGNAME DELIBERATELIES [...]
126 #   POLICY-HOOK-SCRIPT ... push-confirm PACKAGE \
127 #         VERSION SUITE TAGNAME DELIBERATELIES FRESH-REPO|'' [...]
128 #
129 # Exit status is a bitmask.  Bit weight constants are defined in Dgit.pm.
130 #    NOFFCHECK   (2)
131 #         suppress dgit-repos-server's fast-forward check ("push" only)
132 #    FRESHREPO   (4)
133 #         blow away repo right away (ie, as if before push or fetch)
134 #         ("check-package" and "push" only)
135 # any unexpected bits mean failure, and then known set bits are ignored
136 # if no unexpected bits set, operation continues (subject to meaning
137 # of any expected bits set).  So, eg, exit 0 means "continue normally"
138 # and would be appropriate for an unknown action.
139 #
140 # cwd for push and push-confirm is a temporary repo where the
141 # to-be-pushed objects have been received; TAGNAME is the
142 # version-based tag
143 #
144 # FRESH-REPO is '' iff the repo for this package already existed, or
145 # the pathname of the newly-created repo which will be renamed into
146 # place if everything goes well.  (NB that this is generally not the
147 # same repo as the cwd, because the objects are first received into a
148 # temporary repo so they can be examined.)
149
150 # if push requested FRESHREPO, push-confirm happens in said fresh repo
151 # and FRESH-REPO is guaranteed not to be ''.
152 #
153 # policy hook for a particular package will be invoked only once at
154 # a time - (see comments about DGIT-REPOS-DIR, above)
155 #
156 # check-list and check-package are invoked via the --cron option.
157 # First, without any locking, check-list is called.  It should produce
158 # a list of package names (one per line).  Then check-package will be
159 # invoked for each named package, in each case after taking an
160 # appropriate lock.
161 #
162 # If policy hook wants to run dgit (or something else in the dgit
163 # package), it should use DGIT-LIVE-DIR/dgit (etc.)
164
165
166 use POSIX;
167 use Fcntl qw(:flock);
168 use File::Path qw(rmtree);
169 use File::Temp qw(tempfile);
170
171 use Debian::Dgit qw(:DEFAULT :policyflags);
172
173 initdebug('');
174
175 our $func;
176 our $dgitrepos;
177 our $package;
178 our $distro;
179 our $suitesfile;
180 our $policyhook;
181 our $dgitlive;
182 our $distrodir;
183 our $destrepo;
184 our $workrepo;
185 our $keyrings;
186 our @lockfhs;
187
188 our @deliberatelies;
189 our %supersedes;
190 our $policy;
191
192 #----- utilities -----
193
194 sub realdestrepo () { "$dgitrepos/$package.git"; }
195
196 sub acquirelock ($$) {
197     my ($lock, $must) = @_;
198     my $fh;
199     printdebug sprintf "locking %s %d\n", $lock, $must;
200     for (;;) {
201         close $fh if $fh;
202         $fh = new IO::File $lock, ">" or die "open $lock: $!";
203         my $ok = flock $fh, $must ? LOCK_EX : (LOCK_EX|LOCK_NB);
204         if (!$ok) {
205             die "flock $lock: $!" if $must;
206             printdebug " locking $lock failed\n";
207             return undef;
208         }
209         next unless stat_exists $lock;
210         my $want = (stat _)[1];
211         stat $fh or die $!;
212         my $got = (stat _)[1];
213         last if $got == $want;
214     }
215     return $fh;
216 }
217
218 sub acquirermtree ($$) {
219     my ($tree, $must) = @_;
220     my $fh = acquirelock("$tree.lock", $must);
221     if ($fh) {
222         push @lockfhs, $fh;
223         rmtree $tree;
224     }
225     return $fh;
226 }
227
228 sub locksometree ($) {
229     my ($tree) = @_;
230     acquirelock("$tree.lock", 1);
231 }
232
233 sub lockrealtree () {
234     locksometree(realdestrepo);
235 }
236
237 sub mkrepotmp () { ensuredir "$dgitrepos/_tmp" };
238
239 sub removedtagsfile () { "$dgitrepos/_removed-tags/$package"; }
240
241 sub recorderror ($) {
242     my ($why) = @_;
243     my $w = $ENV{'DGIT_DRS_WORK'}; # we are in stunthook
244     if (defined $w) {
245         chomp $why;
246         open ERR, ">", "$w/drs-error" or die $!;
247         print ERR $why, "\n" or die $!;
248         close ERR or die $!;
249         return 1;
250     }
251     return 0;
252 }
253
254 sub reject ($) {
255     my ($why) = @_;
256     recorderror "reject: $why";
257     die "dgit-repos-server: reject: $why\n";
258 }
259
260 sub runcmd {
261     debugcmd '+',@_;
262     $!=0; $?=0;
263     my $r = system @_;
264     die (shellquote @_)." $? $!" if $r;
265 }
266
267 sub policyhook {
268     my ($policyallowbits, @polargs) = @_;
269     # => ($exitstatuspolicybitmap);
270     die if $policyallowbits & ~0x3e;
271     my @cmd = ($policyhook,$distro,$dgitrepos,$dgitlive,$distrodir,@polargs);
272     debugcmd '+',@cmd;
273     my $r = system @cmd;
274     die "system: $!" if $r < 0;
275     die "dgit-repos-server: policy hook failed (or rejected) ($?)\n"
276         if $r & ~($policyallowbits << 8);
277     printdebug sprintf "hook => %#x\n", $r;
278     return $r >> 8;
279 }
280
281 sub mkemptyrepo ($$) {
282     my ($dir,$sharedperm) = @_;
283     runcmd qw(git init --bare --quiet), "--shared=$sharedperm", $dir;
284 }
285
286 sub mkrepo_fromtemplate ($) {
287     my ($dir) = @_;
288     my $template = "$dgitrepos/_template";
289     locksometree($template);
290     printdebug "copy template $template -> $dir\n";
291     my $r = system qw(cp -a --), $template, $dir;
292     !$r or die "create new repo $dir failed: $r $!";
293 }
294
295 sub movetogarbage () {
296     # realdestrepo must have been locked
297     my $garbagerepo = "$dgitrepos/${package}_garbage";
298     # We arrange to always keep at least one old tree, for anti-rewind
299     # purposes (and, I guess, recovery from mistakes).  This is either
300     # $garbage or $garbage-old.
301     if (stat_exists "$garbagerepo") {
302         printdebug "movetogarbage: rmtree $garbagerepo-tmp\n";
303         rmtree "$garbagerepo-tmp";
304         if (rename "$garbagerepo-old", "$garbagerepo-tmp") {
305             printdebug "movetogarbage: $garbagerepo-old -> -tmp, rmtree\n";
306             rmtree "$garbagerepo-tmp";
307         } else {
308             die "$garbagerepo $!" unless $!==ENOENT;
309             printdebug "movetogarbage: $garbagerepo-old -> -tmp\n";
310         }
311         printdebug "movetogarbage: $garbagerepo -> -old\n";
312         rename "$garbagerepo", "$garbagerepo-old" or die "$garbagerepo $!";
313     }
314     my $real = realdestrepo;
315     printdebug "movetogarbage: $real -> $garbagerepo\n";
316     rename $real, $garbagerepo
317         or $! == ENOENT
318         or die "$garbagerepo $!";
319 }
320
321 sub policy_checkpackage () {
322     my $lfh = lockrealtree();
323
324     $policy = policyhook(FRESHREPO,'check-package',$package);
325     if ($policy & FRESHREPO) {
326         movetogarbage();
327     }
328
329     close $lfh;
330 }
331
332 #----- git-receive-pack -----
333
334 sub fixmissing__git_receive_pack () {
335     mkrepotmp();
336     $destrepo = "$dgitrepos/_tmp/${package}_prospective";
337     acquirermtree($destrepo, 1);
338     mkrepo_fromtemplate($destrepo);
339 }
340
341 sub makeworkingclone () {
342     mkrepotmp();
343     $workrepo = "$dgitrepos/_tmp/${package}_incoming$$";
344     acquirermtree($workrepo, 1);
345     my $lfh = lockrealtree();
346     runcmd qw(git clone -l -q --mirror), $destrepo, $workrepo;
347     close $lfh;
348     rmtree "${workrepo}_fresh";
349 }
350
351 sub setupstunthook () {
352     my $prerecv = "$workrepo/hooks/pre-receive";
353     my $fh = new IO::File $prerecv, O_WRONLY|O_CREAT|O_TRUNC, 0777
354         or die "$prerecv: $!";
355     print $fh <<END or die "$prerecv: $!";
356 #!/bin/sh
357 set -e
358 exec $0 --pre-receive-hook $package
359 END
360     close $fh or die "$prerecv: $!";
361     $ENV{'DGIT_DRS_WORK'}= $workrepo;
362     $ENV{'DGIT_DRS_DEST'}= $destrepo;
363     printdebug " stunt hook set up $prerecv\n";
364 }
365
366 sub dealwithfreshrepo () {
367     my $freshrepo = "${workrepo}_fresh";
368     return unless stat_exists $freshrepo;
369     $destrepo = $freshrepo;
370 }
371
372 sub maybeinstallprospective () {
373     return if $destrepo eq realdestrepo;
374
375     if (open REJ, "<", "$workrepo/drs-error") {
376         local $/ = undef;
377         my $msg = <REJ>;
378         REJ->error and die $!;
379         print STDERR $msg;
380         exit 1;
381     } else {
382         $!==&ENOENT or die $!;
383     }
384
385     printdebug " show-ref ($destrepo) ...\n";
386
387     my $child = open SR, "-|";
388     defined $child or die $!;
389     if (!$child) {
390         chdir $destrepo or die $!;
391         exec qw(git show-ref);
392         die $!;
393     }
394     my %got = qw(tag 0 head 0);
395     while (<SR>) {
396         chomp or die;
397         printdebug " show-refs| $_\n";
398         s/^\S*[1-9a-f]\S* (\S+)$/$1/ or die;
399         my $wh =
400             m{^refs/tags/} ? 'tag' :
401             m{^refs/dgit/} ? 'head' :
402             die;
403         die if $got{$wh}++;
404     }
405     $!=0; $?=0; close SR or $?==256 or die "$? $!";
406
407     printdebug "installprospective ?\n";
408     die Dumper(\%got)." -- missing refs in new repo"
409         if grep { !$_ } values %got;
410
411     lockrealtree();
412
413     if ($destrepo eq "${workrepo}_fresh") {
414         movetogarbage;
415     }
416
417     printdebug "install $destrepo => ".realdestrepo."\n";
418     rename $destrepo, realdestrepo or die $!;
419     remove "$destrepo.lock" or die $!;
420 }
421
422 sub main__git_receive_pack () {
423     makeworkingclone();
424     setupstunthook();
425     runcmd qw(git receive-pack), $workrepo;
426     dealwithfreshrepo();
427     maybeinstallprospective();
428 }
429
430 #----- stunt post-receive hook -----
431
432 our ($tagname, $tagval, $suite, $oldcommit, $commit);
433 our ($version, %tagh);
434
435 sub readupdates () {
436     printdebug " updates ...\n";
437     while (<STDIN>) {
438         chomp or die;
439         printdebug " upd.| $_\n";
440         m/^(\S+) (\S+) (\S+)$/ or die "$_ ?";
441         my ($old, $sha1, $refname) = ($1, $2, $3);
442         if ($refname =~ m{^refs/tags/(?=debian/)}) {
443             reject "pushing multiple tags!" if defined $tagname;
444             $tagname = $'; #';
445             $tagval = $sha1;
446             reject "tag $tagname already exists -".
447                 " not replacing previously-pushed version"
448                 if $old =~ m/[^0]/;
449         } elsif ($refname =~ m{^refs/dgit/}) {
450             reject "pushing multiple heads!" if defined $suite;
451             $suite = $'; #';
452             $oldcommit = $old;
453             $commit = $sha1;
454         } else {
455             reject "pushing unexpected ref!";
456         }
457     }
458     STDIN->error and die $!;
459
460     reject "push is missing tag ref update" unless defined $tagname;
461     reject "push is missing head ref update" unless defined $suite;
462     printdebug " updates ok.\n";
463 }
464
465 sub parsetag () {
466     printdebug " parsetag...\n";
467     open PT, ">dgit-tmp/plaintext" or die $!;
468     open DS, ">dgit-tmp/plaintext.asc" or die $!;
469     open T, "-|", qw(git cat-file tag), $tagval or die $!;
470     for (;;) {
471         $!=0; $_=<T>; defined or die $!;
472         print PT or die $!;
473         if (m/^(\S+) (.*)/) {
474             push @{ $tagh{$1} }, $2;
475         } elsif (!m/\S/) {
476             last;
477         } else {
478             die;
479         }
480     }
481     $!=0; $_=<T>; defined or die $!;
482     m/^($package_re) release (\S+) for \S+ \((\S+)\) \[dgit\]$/ or
483         reject "tag message not in expected format";
484
485     die unless $1 eq $package;
486     $version = $2;
487     die "$3 != $suite " unless $3 eq $suite;
488
489     my $copyl = $_;
490     for (;;) {
491         print PT $copyl or die $!;
492         $!=0; $_=<T>; defined or die "missing signature? $!";
493         $copyl = $_;
494         if (m/^\[dgit ([^"].*)\]$/) { # [dgit "something"] is for future
495             $_ = $1." ";
496             while (length) {
497                 if (s/^distro\=(\S+) //) {
498                     die "$1 != $distro" unless $1 eq $distro;
499                 } elsif (s/^(--deliberately-$deliberately_re) //) {
500                     push @deliberatelies, $1;
501                 } elsif (s/^supersede:(\S+)=(\w+) //) {
502                     die "supersede $1 twice" if defined $supersedes{$1};
503                     $supersedes{$1} = $2;
504                 } elsif (s/^[-+.=0-9a-z]\S* //) {
505                 } else {
506                     die "unknown dgit info in tag ($_)";
507                 }
508             }
509             next;
510         }
511         last if m/^-----BEGIN PGP/;
512     }
513     $_ = $copyl;
514     for (;;) {
515         print DS or die $!;
516         $!=0; $_=<T>;
517         last if !defined;
518     }
519     T->error and die $!;
520     close PT or die $!;
521     close DS or die $!;
522     printdebug " parsetag ok.\n";
523 }
524
525 sub checksig_keyring ($) {
526     my ($keyringfile) = @_;
527     # returns primary-keyid if signed by a key in this keyring
528     # or undef if not
529     # or dies on other errors
530
531     my $ok = undef;
532
533     printdebug " checksig keyring $keyringfile...\n";
534
535     our @cmd = (qw(gpgv --status-fd=1 --keyring),
536                    $keyringfile,
537                    qw(dgit-tmp/plaintext.asc dgit-tmp/plaintext));
538     debugcmd '|',@cmd;
539
540     open P, "-|", @cmd
541         or die $!;
542
543     while (<P>) {
544         next unless s/^\[GNUPG:\] //;
545         chomp or die;
546         printdebug " checksig| $_\n";
547         my @l = split / /, $_;
548         if ($l[0] eq 'NO_PUBKEY') {
549             last;
550         } elsif ($l[0] eq 'VALIDSIG') {
551             my $sigtype = $l[9];
552             $sigtype eq '00' or reject "signature is not of type 00!";
553             $ok = $l[10];
554             die unless defined $ok;
555             last;
556         }
557     }
558     close P;
559
560     printdebug sprintf " checksig ok=%d\n", !!$ok;
561
562     return $ok;
563 }
564
565 sub dm_txt_check ($$) {
566     my ($keyid, $dmtxtfn) = @_;
567     printdebug " dm_txt_check $keyid $dmtxtfn\n";
568     open DT, '<', $dmtxtfn or die "$dmtxtfn $!";
569     while (<DT>) {
570         m/^fingerprint:\s+$keyid$/oi
571             ..0 or next;
572         if (s/^allow:/ /i..0) {
573         } else {
574             m/^./
575                 or reject "key $keyid missing Allow section in permissions!";
576             next;
577         }
578         # in right stanza...
579         s/^[ \t]+//
580             or reject "package $package not allowed for key $keyid";
581         # in allow field...
582         s/\([^()]+\)//;
583         s/\,//;
584         chomp or die;
585         printdebug " dm_txt_check allow| $_\n";
586         foreach my $p (split /\s+/) {
587             if ($p eq $package) {
588                 # yay!
589                 printdebug " dm_txt_check ok\n";
590                 return;
591             }
592         }
593     }
594     DT->error and die $!;
595     close DT or die $!;
596     reject "key $keyid not in permissions list although in keyring!";
597 }
598
599 sub verifytag () {
600     foreach my $kas (split /:/, $keyrings) {
601         printdebug "verifytag $kas...\n";
602         $kas =~ s/^([^,]+),// or die;
603         my $keyid = checksig_keyring $1;
604         if (defined $keyid) {
605             if ($kas =~ m/^a$/) {
606                 printdebug "verifytag a ok\n";
607                 return; # yay
608             } elsif ($kas =~ m/^m([^,]+)$/) {
609                 dm_txt_check($keyid, $1);
610                 printdebug "verifytag m ok\n";
611                 return;
612             } else {
613                 die;
614             }
615         }   
616     }
617     reject "key not found in keyrings";
618 }
619
620 sub checksuite () {
621     printdebug "checksuite ($suitesfile)\n";
622     open SUITES, "<", $suitesfile or die $!;
623     while (<SUITES>) {
624         chomp;
625         next unless m/\S/;
626         next if m/^\#/;
627         s/\s+$//;
628         return if $_ eq $suite;
629     }
630     die $! if SUITES->error;
631     reject "unknown suite";
632 }
633
634 sub checktagnoreplay () {
635     # We need to prevent a replay attack using an earlier signed tag.
636     # We also want to archive in the history anything 
637     #
638     # We check that the signed tag mentions the name and tag object id of
639     #
640     # (a) In the case of FRESHREPO all tags and refs/heads/heads in the
641     #     repo.  That is, effectively, all the things we are deleting.
642     #     This prevents any tag implying a FRESHREPO push being replayed
643     #     into a different state of the repo.
644     #
645     # (b) In the case of just NOFFCHECK all tags referring to
646     #     the current head for the suite (there must be at least one).
647     #     This guarantees that the 
648     #
649     # 
650     return unless $policy & (FRESHREPO|NOFFCHECK);
651
652     my $garbagerepo = "$dgitrepos/${package}_garbage";
653     lockrealtree();
654
655     local $ENV{GIT_DIR};
656     foreach my $garb ("$garbagerepo", "$garbagerepo-old") {
657         if (stat_exists $garb) {
658             $ENV{GIT_DIR} = $garb;
659             last;
660         }
661     }
662     if (!defined $ENV{GIT_DIR}) {
663         # Nothing to overwrite so the FRESHREPO and NOFFCHECK were
664         # pointless.  Oh well.
665         printdebug "checktagnoreplay - no garbage, ok\n";
666         return;
667     }
668
669     my $onlyreferring;
670     if (!($policy & FRESHREPO)) {
671         my $branch = server_branch($suite);
672         $!=0; $?=0; $_ =
673             `git for-each-ref --format='%(objectname)' '[r]efs/$branch'`;
674         defined or die "$branch $? $!";
675         $? and die "$branch $?";
676         if (!length) {
677             # No such branch - NOFFCHECK was unnecessary.  Oh well.
678             printdebug "checktagnoreplay - not FRESHREPO, new branch, ok\n";
679             return;
680         }
681         m/^(\w+)\n$/ or die "$branch $_ ?";
682         $onlyreferring = $1;
683         printdebug "checktagnoreplay - not FRESHREPO,".
684             " checking for overwriting refs/$branch=$onlyreferring\n";
685     }
686
687     my @problems;
688
689     git_for_each_tag_referring($onlyreferring, sub {
690         my ($tagobjid,$refobjid,$fullrefname,$tagname) = @_;
691         printdebug "checktagnoreplay - overwriting".
692             " $fullrefname=$tagobjid->$refobjid\n";
693         my $supers = $supersedes{$fullrefname};
694         if (!defined $supers) {
695             printdebug "checktagnoreply - fallbacks\n";
696             my $super_fallback = 0;
697             foreach my $didsuper (sort keys %supersedes) {
698                 my $didsuper_tagobjid = $supersedes{$didsuper};
699                 my $didsuper_refobjid = git_rev_parse $didsuper_tagobjid;
700                 printdebug "checktagnoreply - fallback".
701                     " $didsuper=$didsuper_refobjid->$didsuper_tagobjid\n";
702                 last if 
703                     $refobjid ne $didsuper_refobjid
704                     and is_fast_fwd($refobjid, $didsuper_refobjid);
705                 printdebug "checktagnoreply - fallback $didsuper OK\n";
706                 $super_fallback = 1;
707             }
708             push @problems, "does not supersede $fullrefname"
709                 unless $super_fallback;
710         } elsif ($supers ne $tagobjid) {
711             push @problems,
712  "supersedes $fullrefname=$supers but previously $fullrefname=$tagobjid";
713         } else {
714             # ok;
715         }
716     });
717
718     if (@problems) {
719         reject "replay attack prevention check failed:".
720             " signed tag for $version: ".
721             join("; ", @problems).
722             "\n";
723     }
724     printdebug "checktagnoreply - all ok\n"
725 }
726
727 sub tagh1 ($) {
728     my ($tag) = @_;
729     my $vals = $tagh{$tag};
730     reject "missing header $tag in signed tag object" unless $vals;
731     reject "multiple headers $tag in signed tag object" unless @$vals == 1;
732     return $vals->[0];
733 }
734
735 sub checks () {
736     printdebug "checks\n";
737
738     tagh1('type') eq 'commit' or reject "tag refers to wrong kind of object";
739     tagh1('object') eq $commit or reject "tag refers to wrong commit";
740     tagh1('tag') eq $tagname or reject "tag name in tag is wrong";
741
742     my $v = $version;
743     $v =~ y/~:/_%/;
744
745     printdebug "translated version $v\n";
746     $tagname eq "debian/$v" or die;
747
748     lockrealtree();
749
750     my @policy_args = ($package,$version,$suite,$tagname,
751                        join(",",@deliberatelies));
752     $policy = policyhook(NOFFCHECK|FRESHREPO, 'push', @policy_args);
753
754     checktagnoreplay();
755     checksuite();
756
757     # check that our ref is being fast-forwarded
758     printdebug "oldcommit $oldcommit\n";
759     if (!($policy & NOFFCHECK) && $oldcommit =~ m/[^0]/) {
760         $?=0; $!=0; my $mb = `git merge-base $commit $oldcommit`;
761         chomp $mb;
762         $mb eq $oldcommit or reject "not fast forward on dgit branch";
763     }
764
765     if ($policy & FRESHREPO) {
766         # This is troublesome.  We have been asked by the policy hook
767         # to receive the push into a fresh repo.  But of course we
768         # have actually already mostly received the push into the working
769         # repo.  (This is unavoidable because the instruction to use a new
770         # repo comes ultimately from the signed tag for the dgit push,
771         # which has to have been received into some repo.)
772         #
773         # So what we do is generate a fresh working repo right now and
774         # push the head and tag into it.  The presence of this fresh
775         # working repo is detected by the parent, which responds by
776         # making a fresh master repo from the template.
777
778         $destrepo = "${workrepo}_fresh"; # workrepo lock covers
779         mkrepo_fromtemplate $destrepo;
780     }
781
782     my $willinstall = ($destrepo eq realdestrepo ? '' : $destrepo);
783     policyhook(0, 'push-confirm', @policy_args, $willinstall);
784 }
785
786 sub onwardpush () {
787     my @cmd = (qw(git send-pack), $destrepo);
788     push @cmd, qw(--force) if $policy & NOFFCHECK;
789     push @cmd, "$commit:refs/dgit/$suite",
790                "$tagval:refs/tags/$tagname";
791     debugcmd '+',@cmd;
792     $!=0;
793     my $r = system @cmd;
794     !$r or die "onward push to $destrepo failed: $r $!";
795 }
796
797 sub stunthook () {
798     printdebug "stunthook in $workrepo\n";
799     chdir $workrepo or die "chdir $workrepo: $!";
800     mkdir "dgit-tmp" or $!==EEXIST or die $!;
801     readupdates();
802     parsetag();
803     verifytag();
804     checks();
805     onwardpush();
806     printdebug "stunthook done.\n";
807 }
808
809 #----- git-upload-pack -----
810
811 sub fixmissing__git_upload_pack () {
812     $destrepo = "$dgitrepos/_empty";
813     my $lfh = locksometree($destrepo);
814     return if stat_exists $destrepo;
815     rmtree "$destrepo.new";
816     mkemptyrepo "$destrepo.new", "0644";
817     rename "$destrepo.new", $destrepo or die $!;
818     unlink "$destrepo.lock" or die $!;
819     close $lfh;
820 }
821
822 sub main__git_upload_pack () {
823     my $lfh = locksometree($destrepo);
824     printdebug "git-upload-pack in $destrepo\n";
825     chdir $destrepo or die "$destrepo: $!";
826     close $lfh;
827     runcmd qw(git upload-pack), ".";
828 }
829
830 #----- arg parsing and main program -----
831
832 sub argval () {
833     die unless @ARGV;
834     my $v = shift @ARGV;
835     die if $v =~ m/^-/;
836     return $v;
837 }
838
839 our %indistrodir = (
840     # keys are used for DGIT_DRS_XXX too
841     'repos' => \$dgitrepos,
842     'suites' => \$suitesfile,
843     'policy-hook' => \$policyhook,
844     'dgit-live' => \$dgitlive,
845     );
846
847 our @hookenvs = qw(distro suitesfile policyhook
848                    dgitlive keyrings dgitrepos distrodir);
849
850 # workrepo and destrepo handled ad-hoc
851
852 sub mode_ssh () {
853     die if @ARGV;
854
855     my $cmd = $ENV{'SSH_ORIGINAL_COMMAND'};
856     $cmd =~ m{
857         ^
858         (?: \S* / )?
859         ( [-0-9a-z]+ )
860         \s+
861         '? (?: \S* / )?
862         ($package_re) \.git
863         '?$
864     }ox 
865     or reject "command string not understood";
866     my $method = $1;
867     $package = $2;
868
869     my $funcn = $method;
870     $funcn =~ y/-/_/;
871     my $mainfunc = $main::{"main__$funcn"};
872
873     reject "unknown method" unless $mainfunc;
874
875     policy_checkpackage();
876
877     if (stat_exists realdestrepo) {
878         $destrepo = realdestrepo;
879     } else {
880         printdebug " fixmissing $funcn\n";
881         my $fixfunc = $main::{"fixmissing__$funcn"};
882         &$fixfunc;
883     }
884
885     printdebug " running main $funcn\n";
886     &$mainfunc;
887 }
888
889 sub mode_cron () {
890     die if @ARGV;
891
892     my $listfh = tempfile();
893     open STDOUT, ">&", $listfh or die $!;
894     policyhook(0,'check-list');
895     open STDOUT, ">&STDERR" or die $!;
896
897     seek $listfh, 0, 0 or die $!;
898     while (<$listfh>) {
899         chomp or die;
900         next if m/^\s*\#/;
901         next unless m/\S/;
902         die unless m/^($package_re)$/;
903         
904         $package = $1;
905         policy_checkpackage();
906     }
907     die $! if $listfh->error;
908 }    
909
910 sub parseargsdispatch () {
911     die unless @ARGV;
912
913     delete $ENV{'GIT_DIR'}; # if not run via ssh, our parent git process
914     delete $ENV{'GIT_PREFIX'}; # sets these and they mess things up
915
916     if ($ENV{'DGIT_DRS_DEBUG'}) {
917         enabledebug();
918     }
919
920     if ($ARGV[0] eq '--pre-receive-hook') {
921         if ($debuglevel) {
922             $debugprefix.="=";
923             printdebug "in stunthook ".(shellquote @ARGV)."\n";
924             foreach my $k (sort keys %ENV) {
925                 printdebug "$k=$ENV{$k}\n" if $k =~  m/^DGIT/;
926             }
927         }
928         shift @ARGV;
929         @ARGV == 1 or die;
930         $package = shift @ARGV;
931         ${ $main::{$_} } = $ENV{"DGIT_DRS_\U$_"} foreach @hookenvs;
932         defined($workrepo = $ENV{'DGIT_DRS_WORK'}) or die;
933         defined($destrepo = $ENV{'DGIT_DRS_DEST'}) or die;
934         open STDOUT, ">&STDERR" or die $!;
935         eval {
936             stunthook();
937         };
938         if ($@) {
939             recorderror "$@" or die;
940             die $@;
941         }
942         exit 0;
943     }
944
945     $distro    = argval();
946     $distrodir = argval();
947     $keyrings  = argval();
948
949     foreach my $dk (keys %indistrodir) {
950         ${ $indistrodir{$dk} } = "$distrodir/$dk";
951     }
952
953     while (@ARGV && $ARGV[0] =~ m/^--([-0-9a-z]+)=/ && $indistrodir{$1}) {
954         ${ $indistrodir{$1} } = $'; #';
955         shift @ARGV;
956     }
957
958     $ENV{"DGIT_DRS_\U$_"} = ${ $main::{$_} } foreach @hookenvs;
959
960     die unless @ARGV==1;
961
962     my $mode = shift @ARGV;
963     die unless $mode =~ m/^--(\w+)$/;
964     my $fn = ${*::}{"mode_$1"};
965     die unless $fn;
966     $fn->();
967 }
968
969 sub unlockall () {
970     while (my $fh = pop @lockfhs) { close $fh; }
971 }
972
973 sub cleanup () {
974     unlockall();
975     if (!chdir "$dgitrepos/_tmp") {
976         $!==ENOENT or die $!;
977         return;
978     }
979     foreach my $lf (<*.lock>) {
980         my $tree = $lf;
981         $tree =~ s/\.lock$//;
982         next unless acquirermtree($tree, 0);
983         remove $lf or warn $!;
984         unlockall();
985     }
986 }
987
988 parseargsdispatch();
989 cleanup();