5 # .../dgit-repos-server DISTRO SUITES KEYRING-AUTH-SPEC \
6 # DGIT-REPOS-DIR POLICY-HOOK-SCRIPT --ssh
8 # .../dgit-repos-server --pre-receive-hook PACKAGE
10 # Invoked as the ssh restricted command
12 # Works like git-receive-pack
14 # SUITES is the name of a file which lists the permissible suites
15 # one per line (#-comments and blank lines ignored)
17 # KEYRING-AUTH-SPEC is a :-separated list of
18 # KEYRING.GPG,AUTH-SPEC
19 # where AUTH-SPEC is one of
26 # - extract the destination repo name
27 # - make a hardlink clone of the destination repo
28 # - provide the destination with a stunt pre-receive hook
29 # - run actual git-receive-pack with that new destination
30 # as a result of this the stunt pre-receive hook runs; it does this:
31 # + understand what refs we are allegedly updating and
32 # check some correspondences:
33 # * we are updating only refs/tags/debian/* and refs/dgit/*
34 # * and only one of each
35 # * and the tag does not already exist
37 # * recover the suite name from the destination refs/dgit/ ref
38 # + disassemble the signed tag into its various fields and signature
40 # * parsing the first line of the tag message to recover
41 # the package name, version and suite
42 # * checking that the package name corresponds to the dest repo name
43 # * checking that the suite name is as recovered above
44 # + verify the signature on the signed tag
45 # and if necessary check that the keyid and package are listed in dm.txt
46 # + check various correspondences:
47 # * the suite is one of those permitted
48 # * the signed tag must refer to a commit
49 # * the signed tag commit must be the refs/dgit value
50 # * the name in the signed tag must correspond to its ref name
51 # * the tag name must be debian/<version> (massaged as needed)
52 # * the signed tag has a suitable name
53 # * the commit is a fast forward
54 # + push the signed tag and new dgit branch to the actual repo
56 # If the destination repo does not already exist, we need to make
57 # sure that we create it reasonably atomically, and also that
58 # we don't every have a destination repo containing no refs at all
59 # (because such a thing causes git-fetch-pack to barf). So then we
60 # do as above, except:
61 # - before starting, we take out our own lock for the destination repo
62 # - we create a prospective new destination repo by making a copy
64 # - we use the prospective new destination repo instead of the
65 # actual new destination repo (since the latter doesn't exist)
66 # - we set up a post-receive hook as well, which
67 # + touches a stamp file
68 # - after git-receive-pack exits, we
69 # + check that the prospective repo contains a tag and head
70 # + rename the prospective destination repo into place
74 # - Temporary working trees and their locks are cleaned up
75 # opportunistically by a program which tries to take each lock and
76 # if successful deletes both the tree and the lockfile
77 # - Prospective working trees and their locks are cleaned up by
78 # a program which tries to take each lock and if successful
79 # deletes any prospective working tree and the lock (but not
80 # of course any actual tree)
81 # - It is forbidden to _remove_ the lockfile without removing
82 # the corresponding temporary tree, as the lockfile is also
83 # a stampfile whose presence indicates that there may be
88 use File::Path qw(rmtree);
92 open DEBUG, ">/dev/null" or die $!;
108 #----- utilities -----
111 print DEBUG "$debug @_\n";
114 sub acquirelock ($$) {
115 my ($lock, $must) = @_;
117 printf DEBUG "$debug locking %s %d\n", $lock, $must;
120 $fh = new IO::File $lock, ">" or die "open $lock: $!";
121 my $ok = flock $fh, $must ? LOCK_EX : (LOCK_EX|LOCK_NB);
123 die "flock $lock: $!" if $must;
124 debug " locking $lock failed";
128 next if $! == ENOENT;
129 die "stat $lock: $!";
131 my $want = (stat _)[1];
133 my $got = (stat _)[1];
134 last if $got == $want;
139 sub acquiretree ($$) {
140 my ($tree, $must) = @_;
141 my $fh = acquirelock("$tree.lock", $must);
150 my $tmpdir = "$dgitrepos/_tmp";
151 return if mkdir $tmpdir;
152 return if $! == EEXIST;
156 sub recorderror ($) {
158 my $w = $ENV{'DGIT_DRS_WORK'}; # we are in stunthook
161 open ERR, ">", "$w/drs-error" or die $!;
162 print ERR $why, "\n" or die $!;
171 recorderror "reject: $why";
172 die "dgit-repos-server: reject: $why\n";
178 local $Data::Dumper::Indent = 0;
179 local $Data::Dumper::Terse = 1;
180 debug "|".Dumper(\@_);
188 die "@_ $? $!" if $r;
192 my ($policyallowbits, @polargs) = @_;
193 # => ($exitstatuspolicybitmap, $policylockfh);
194 die if $policyallowbits & ~0x3e;
195 my @cmd = ($policyhook,$distro,$repos,@polargs);
198 die "system: $!" if $r < 0;
199 die "hook (@cmd) failed ($?)" if $r & ~($policyallowbits << 8);
203 #----- git-receive-pack -----
205 sub fixmissing__git_receive_pack () {
207 $destrepo = "$dgitrepos/_tmp/${package}_prospective";
208 acquiretree($destrepo, 1);
209 my $template = "$dgitrepos/_template";
210 debug "fixmissing copy tempalate $template -> $destrepo";
211 my $r = system qw(cp -a --), $template, $destrepo;
212 !$r or die "create new repo failed failed: $r $!";
215 sub makeworkingclone () {
217 $workrepo = "$dgitrepos/_tmp/${package}_incoming$$";
218 acquiretree($workrepo, 1);
219 runcmd qw(git clone -l -q --mirror), $destrepo, $workrepo;
222 sub setupstunthook () {
223 my $prerecv = "$workrepo/hooks/pre-receive";
224 my $fh = new IO::File $prerecv, O_WRONLY|O_CREAT|O_TRUNC, 0777
225 or die "$prerecv: $!";
226 print $fh <<END or die "$prerecv: $!";
229 exec $0 --pre-receive-hook $package
231 close $fh or die "$prerecv: $!";
232 $ENV{'DGIT_DRS_WORK'}= $workrepo;
233 $ENV{'DGIT_DRS_DEST'}= $destrepo;
234 debug " stunt hook set up $prerecv";
237 sub maybeinstallprospective () {
238 return if $destrepo eq $realdestrepo;
240 if (open REJ, "<", "$workrepo/drs-error") {
243 REJ->error and die $!;
247 $!==&ENOENT or die $!;
250 debug " show-ref ($destrepo) ...";
252 my $child = open SR, "-|";
253 defined $child or die $!;
255 chdir $destrepo or die $!;
256 exec qw(git show-ref);
259 my %got = qw(tag 0 head 0);
262 debug " show-refs| $_";
263 s/^\S*[1-9a-f]\S* (\S+)$/$1/ or die;
265 m{^refs/tags/} ? 'tag' :
266 m{^refs/dgit/} ? 'head' :
270 $!=0; $?=0; close SR or $?==256 or die "$? $!";
272 debug "installprospective ?";
273 die Dumper(\%got)." -- missing refs in new repo"
274 if grep { !$_ } values %got;
276 debug "install $destrepo => $realdestrepo";
277 rename $destrepo, $realdestrepo or die $!;
278 remove "$destrepo.lock" or die $!;
281 sub main__git_receive_pack () {
284 runcmd qw(git receive-pack), $workrepo;
285 maybeinstallprospective();
288 #----- stunt post-receive hook -----
290 our ($tagname, $tagval, $suite, $oldcommit, $commit);
291 our ($version, %tagh);
294 debug " updates ...";
298 m/^(\S+) (\S+) (\S+)$/ or die "$_ ?";
299 my ($old, $sha1, $refname) = ($1, $2, $3);
300 if ($refname =~ m{^refs/tags/(?=debian/)}) {
301 reject "pushing multiple tags!" if defined $tagname;
304 reject "tag $tagname already exists -".
305 " not replacing previously-pushed version"
307 } elsif ($refname =~ m{^refs/dgit/}) {
308 reject "pushing multiple heads!" if defined $suite;
313 reject "pushing unexpected ref!";
316 STDIN->error and die $!;
318 reject "push is missing tag ref update" unless defined $tagname;
319 reject "push is missing head ref update" unless defined $suite;
320 debug " updates ok.";
324 debug " parsetag...";
325 open PT, ">dgit-tmp/plaintext" or die $!;
326 open DS, ">dgit-tmp/plaintext.asc" or die $!;
327 open T, "-|", qw(git cat-file tag), $tagval or die $!;
329 $!=0; $_=<T>; defined or die $!;
331 if (m/^(\S+) (.*)/) {
332 push @{ $tagh{$1} }, $2;
339 $!=0; $_=<T>; defined or die $!;
340 m/^($package_re) release (\S+) for \S+ \((\S+)\) \[dgit\]$/ or
341 reject "tag message not in expected format";
343 die unless $1 eq $package;
345 die "$3 != $suite " unless $3 eq $suite;
349 $!=0; $_=<T>; defined or die "missing signature? $!";
350 if (m/^\[dgit ([^"].*)\]$/) { # [dgit "something"] is for future
353 if (s/^distro\=(\S+) //) {
354 die "$1 != $distro" unless $1 eq $distro;
355 } elsif (s/^(--deliberately-$package_re) //) {
356 push @deliberatelies, $1;
357 } elsif (s/^[-+.=0-9a-z]\S* //) {
359 die "unknown dgit info in tag";
364 last if m/^-----BEGIN PGP/;
374 debug " parsetag ok.";
377 sub checksig_keyring ($) {
378 my ($keyringfile) = @_;
379 # returns primary-keyid if signed by a key in this keyring
381 # or dies on other errors
385 debug " checksig keyring $keyringfile...";
387 our @cmd = (qw(gpgv --status-fd=1 --keyring),
389 qw(dgit-tmp/plaintext.asc dgit-tmp/plaintext));
396 next unless s/^\[GNUPG:\] //;
398 debug " checksig| $_";
399 my @l = split / /, $_;
400 if ($l[0] eq 'NO_PUBKEY') {
402 } elsif ($l[0] eq 'VALIDSIG') {
404 $sigtype eq '00' or reject "signature is not of type 00!";
406 die unless defined $ok;
412 debug sprintf " checksig ok=%d", !!$ok;
417 sub dm_txt_check ($$) {
418 my ($keyid, $dmtxtfn) = @_;
419 debug " dm_txt_check $keyid $dmtxtfn";
420 open DT, '<', $dmtxtfn or die "$dmtxtfn $!";
422 m/^fingerprint:\s+$keyid$/oi
424 if (s/^allow:/ /i..0) {
427 or reject "key $keyid missing Allow section in permissions!";
432 or reject "package $package not allowed for key $keyid";
437 debug " dm_txt_check allow| $_";
438 foreach my $p (split /\s+/) {
439 if ($p eq $package) {
441 debug " dm_txt_check ok";
446 DT->error and die $!;
448 reject "key $keyid not in permissions list although in keyring!";
452 foreach my $kas (split /:/, $keyrings) {
453 debug "verifytag $kas...";
454 $kas =~ s/^([^,]+),// or die;
455 my $keyid = checksig_keyring $1;
456 if (defined $keyid) {
457 if ($kas =~ m/^a$/) {
458 debug "verifytag a ok";
460 } elsif ($kas =~ m/^m([^,]+)$/) {
461 dm_txt_check($keyid, $1);
462 debug "verifytag m ok";
469 reject "key not found in keyrings";
473 debug "checksuite ($suitesfile)";
474 open SUITES, "<", $suitesfile or die $!;
480 return if $_ eq $suite;
482 die $! if SUITES->error;
483 reject "unknown suite";
488 my $vals = $tagh{$tag};
489 reject "missing header $tag in signed tag object" unless $vals;
490 reject "multiple headers $tag in signed tag object" unless @$vals == 1;
497 tagh1('type') eq 'commit' or reject "tag refers to wrong kind of object";
498 tagh1('object') eq $commit or reject "tag refers to wrong commit";
499 tagh1('tag') eq $tagname or reject "tag name in tag is wrong";
504 debug "translated version $v";
505 $tagname eq "debian/$v" or die;
507 my ($policy) = policyhook(2,'push',$package,
508 $version,$suite,$tagname,
509 join(",",@delberatelies));
513 # check that our ref is being fast-forwarded
514 debug "oldcommit $oldcommit";
515 if (!($policy & 2) && $oldcommit =~ m/[^0]/) {
516 $?=0; $!=0; my $mb = `git merge-base $commit $oldcommit`;
518 $mb eq $oldcommit or reject "not fast forward on dgit branch";
523 my @cmd = (qw(git send-pack), $destrepo,
524 "$commit:refs/dgit/$suite",
525 "$tagval:refs/tags/$tagname");
529 !$r or die "onward push failed: $r $!";
534 chdir $workrepo or die "chdir $workrepo: $!";
535 mkdir "dgit-tmp" or $!==EEXIST or die $!;
541 debug "stunthook done.";
544 #----- git-upload-pack -----
546 sub fixmissing__git_upload_pack () {
547 $destrepo = "$dgitrepos/_empty";
548 my $lfh = acquiretree($destrepo,1);
549 return if stat $destrepo;
550 die $! unless $!==ENOENT;
551 rmtree "$destrepo.new";
553 runcmd qw(git init --bare --quiet), "$destrepo.new";
554 rename "$destrepo.new", $destrepo or die $!;
555 unlink "$destrepo.lock" or die $!;
559 sub main__git_upload_pack () {
560 runcmd qw(git upload-pack), $destrepo;
563 #----- arg parsing and main program -----
572 sub parseargsdispatch () {
575 delete $ENV{'GIT_DIR'}; # if not run via ssh, our parent git process
576 delete $ENV{'GIT_PREFIX'}; # sets these and they mess things up
578 if ($ENV{'DGIT_DRS_DEBUG'}) {
580 open DEBUG, ">&STDERR" or die $!;
583 if ($ARGV[0] eq '--pre-receive-hook') {
584 if ($debug) { $debug.="="; }
587 $package = shift @ARGV;
588 defined($distro = $ENV{'DGIT_DRS_DISTRO'}) or die;
589 defined($suitesfile = $ENV{'DGIT_DRS_SUITES'}) or die;
590 defined($workrepo = $ENV{'DGIT_DRS_WORK'}) or die;
591 defined($destrepo = $ENV{'DGIT_DRS_DEST'}) or die;
592 defined($keyrings = $ENV{'DGIT_DRS_KEYRINGS'}) or die $!;
593 defined($policyhook = $ENV{'DGIT_DRS_POLICYHOOK'}) or die $!;
594 open STDOUT, ">&STDERR" or die $!;
599 recorderror "$@" or die;
605 $ENV{'DGIT_DRS_DISTRO'} = argval();
606 $ENV{'DGIT_DRS_SUITES'} = argval();
607 $ENV{'DGIT_DRS_KEYRINGS'} = argval();
608 $dgitrepos = argval();
609 $ENV{'DGIT_DRS_POLICYHOOK'} = $policyhook = argval();
611 die unless @ARGV==1 && $ARGV[0] eq '--ssh';
613 my $cmd = $ENV{'SSH_ORIGINAL_COMMAND'};
623 or reject "command string not understood";
626 $realdestrepo = "$dgitrepos/$package.git";
630 my $mainfunc = $main::{"main__$funcn"};
632 reject "unknown method" unless $mainfunc;
634 my ($policy, $pollock) = policyhook(4, 'check-package',$package);
636 my $garbagerepo = "$dgitrepos/_tmp/${package}_garbage";
637 acquiretree($garbagerepo,1);
639 rename $realdestrepo, $garbagerepo
641 or die "rename repo $destrepo to $garbagerepo: $!";
643 close $pollock or die $!;
645 if (stat $realdestrepo) {
646 $destrepo = $realdestrepo;
648 $! == ENOENT or die "stat dest repo $destrepo: $!";
649 debug " fixmissing $funcn";
650 my $fixfunc = $main::{"fixmissing__$funcn"};
654 debug " running main $funcn";
659 while (my $fh = pop @lockfhs) { close $fh; }
664 if (!chdir "$dgitrepos/_tmp") {
665 $!==ENOENT or die $!;
668 foreach my $lf (<*.lock>) {
670 $tree =~ s/\.lock$//;
671 next unless acquiretree($tree, 0);
672 remove $lf or warn $!;