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);
90 use Debian::Dgit qw(:DEFAULT :policyflags);
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 sub mkemptyrepo ($$) {
204 my ($dir,$sharedperm) = @_;
205 runcmd qw(git init --bare --quiet), "--shared=$sharedperm", $dir;
208 sub mkrepo_fromtemplate ($) {
210 my $template = "$dgitrepos/_template";
211 debug "copy tempalate $template -> $dir";
212 my $r = system qw(cp -a --), $template, $dir;
213 !$r or die "create new repo $dir failed: $r $!";
216 sub movetogarbage () {
217 my $garbagerepo = "$dgitrepos/_tmp/${package}_garbage";
218 acquiretree($garbagerepo,1);
220 rename $realdestrepo, $garbagerepo
222 or die "rename repo $realdestrepo to $garbagerepo: $!";
225 #----- git-receive-pack -----
227 sub fixmissing__git_receive_pack () {
229 $destrepo = "$dgitrepos/_tmp/${package}_prospective";
230 acquiretree($destrepo, 1);
231 mkrepo_fromtemplate($destrepo);
234 sub makeworkingclone () {
236 $workrepo = "$dgitrepos/_tmp/${package}_incoming$$";
237 acquiretree($workrepo, 1);
238 runcmd qw(git clone -l -q --mirror), $destrepo, $workrepo;
241 sub setupstunthook () {
242 my $prerecv = "$workrepo/hooks/pre-receive";
243 my $fh = new IO::File $prerecv, O_WRONLY|O_CREAT|O_TRUNC, 0777
244 or die "$prerecv: $!";
245 print $fh <<END or die "$prerecv: $!";
248 exec $0 --pre-receive-hook $package
250 close $fh or die "$prerecv: $!";
251 $ENV{'DGIT_DRS_WORK'}= $workrepo;
252 $ENV{'DGIT_DRS_DEST'}= $destrepo;
253 debug " stunt hook set up $prerecv";
256 sub maybeinstallprospective () {
257 return if $destrepo eq $realdestrepo;
259 if (open REJ, "<", "$workrepo/drs-error") {
262 REJ->error and die $!;
266 $!==&ENOENT or die $!;
269 debug " show-ref ($destrepo) ...";
271 my $child = open SR, "-|";
272 defined $child or die $!;
274 chdir $destrepo or die $!;
275 exec qw(git show-ref);
278 my %got = qw(tag 0 head 0);
281 debug " show-refs| $_";
282 s/^\S*[1-9a-f]\S* (\S+)$/$1/ or die;
284 m{^refs/tags/} ? 'tag' :
285 m{^refs/dgit/} ? 'head' :
289 $!=0; $?=0; close SR or $?==256 or die "$? $!";
291 debug "installprospective ?";
292 die Dumper(\%got)." -- missing refs in new repo"
293 if grep { !$_ } values %got;
295 debug "install $destrepo => $realdestrepo";
296 rename $destrepo, $realdestrepo or die $!;
297 remove "$destrepo.lock" or die $!;
300 sub main__git_receive_pack () {
303 runcmd qw(git receive-pack), $workrepo;
304 maybeinstallprospective();
307 #----- stunt post-receive hook -----
309 our ($tagname, $tagval, $suite, $oldcommit, $commit);
310 our ($version, %tagh);
313 debug " updates ...";
317 m/^(\S+) (\S+) (\S+)$/ or die "$_ ?";
318 my ($old, $sha1, $refname) = ($1, $2, $3);
319 if ($refname =~ m{^refs/tags/(?=debian/)}) {
320 reject "pushing multiple tags!" if defined $tagname;
323 reject "tag $tagname already exists -".
324 " not replacing previously-pushed version"
326 } elsif ($refname =~ m{^refs/dgit/}) {
327 reject "pushing multiple heads!" if defined $suite;
332 reject "pushing unexpected ref!";
335 STDIN->error and die $!;
337 reject "push is missing tag ref update" unless defined $tagname;
338 reject "push is missing head ref update" unless defined $suite;
339 debug " updates ok.";
343 debug " parsetag...";
344 open PT, ">dgit-tmp/plaintext" or die $!;
345 open DS, ">dgit-tmp/plaintext.asc" or die $!;
346 open T, "-|", qw(git cat-file tag), $tagval or die $!;
348 $!=0; $_=<T>; defined or die $!;
350 if (m/^(\S+) (.*)/) {
351 push @{ $tagh{$1} }, $2;
358 $!=0; $_=<T>; defined or die $!;
359 m/^($package_re) release (\S+) for \S+ \((\S+)\) \[dgit\]$/ or
360 reject "tag message not in expected format";
362 die unless $1 eq $package;
364 die "$3 != $suite " unless $3 eq $suite;
368 $!=0; $_=<T>; defined or die "missing signature? $!";
369 if (m/^\[dgit ([^"].*)\]$/) { # [dgit "something"] is for future
372 if (s/^distro\=(\S+) //) {
373 die "$1 != $distro" unless $1 eq $distro;
374 } elsif (s/^(--deliberately-$package_re) //) {
375 push @deliberatelies, $1;
376 } elsif (s/^[-+.=0-9a-z]\S* //) {
378 die "unknown dgit info in tag";
383 last if m/^-----BEGIN PGP/;
393 debug " parsetag ok.";
396 sub checksig_keyring ($) {
397 my ($keyringfile) = @_;
398 # returns primary-keyid if signed by a key in this keyring
400 # or dies on other errors
404 debug " checksig keyring $keyringfile...";
406 our @cmd = (qw(gpgv --status-fd=1 --keyring),
408 qw(dgit-tmp/plaintext.asc dgit-tmp/plaintext));
415 next unless s/^\[GNUPG:\] //;
417 debug " checksig| $_";
418 my @l = split / /, $_;
419 if ($l[0] eq 'NO_PUBKEY') {
421 } elsif ($l[0] eq 'VALIDSIG') {
423 $sigtype eq '00' or reject "signature is not of type 00!";
425 die unless defined $ok;
431 debug sprintf " checksig ok=%d", !!$ok;
436 sub dm_txt_check ($$) {
437 my ($keyid, $dmtxtfn) = @_;
438 debug " dm_txt_check $keyid $dmtxtfn";
439 open DT, '<', $dmtxtfn or die "$dmtxtfn $!";
441 m/^fingerprint:\s+$keyid$/oi
443 if (s/^allow:/ /i..0) {
446 or reject "key $keyid missing Allow section in permissions!";
451 or reject "package $package not allowed for key $keyid";
456 debug " dm_txt_check allow| $_";
457 foreach my $p (split /\s+/) {
458 if ($p eq $package) {
460 debug " dm_txt_check ok";
465 DT->error and die $!;
467 reject "key $keyid not in permissions list although in keyring!";
471 foreach my $kas (split /:/, $keyrings) {
472 debug "verifytag $kas...";
473 $kas =~ s/^([^,]+),// or die;
474 my $keyid = checksig_keyring $1;
475 if (defined $keyid) {
476 if ($kas =~ m/^a$/) {
477 debug "verifytag a ok";
479 } elsif ($kas =~ m/^m([^,]+)$/) {
480 dm_txt_check($keyid, $1);
481 debug "verifytag m ok";
488 reject "key not found in keyrings";
492 debug "checksuite ($suitesfile)";
493 open SUITES, "<", $suitesfile or die $!;
499 return if $_ eq $suite;
501 die $! if SUITES->error;
502 reject "unknown suite";
507 my $vals = $tagh{$tag};
508 reject "missing header $tag in signed tag object" unless $vals;
509 reject "multiple headers $tag in signed tag object" unless @$vals == 1;
516 tagh1('type') eq 'commit' or reject "tag refers to wrong kind of object";
517 tagh1('object') eq $commit or reject "tag refers to wrong commit";
518 tagh1('tag') eq $tagname or reject "tag name in tag is wrong";
523 debug "translated version $v";
524 $tagname eq "debian/$v" or die;
526 my ($policy) = policyhook(NOFFCHECK, 'push',$package,
527 $version,$suite,$tagname,
528 join(",",@delberatelies));
532 # check that our ref is being fast-forwarded
533 debug "oldcommit $oldcommit";
534 if (!($policy & NOFFCHECK) && $oldcommit =~ m/[^0]/) {
535 $?=0; $!=0; my $mb = `git merge-base $commit $oldcommit`;
537 $mb eq $oldcommit or reject "not fast forward on dgit branch";
542 my @cmd = (qw(git send-pack), $destrepo,
543 "$commit:refs/dgit/$suite",
544 "$tagval:refs/tags/$tagname");
548 !$r or die "onward push failed: $r $!";
553 chdir $workrepo or die "chdir $workrepo: $!";
554 mkdir "dgit-tmp" or $!==EEXIST or die $!;
560 debug "stunthook done.";
563 #----- git-upload-pack -----
565 sub fixmissing__git_upload_pack () {
566 $destrepo = "$dgitrepos/_empty";
567 my $lfh = acquiretree($destrepo,1);
568 return if stat $destrepo;
569 die $! unless $!==ENOENT;
570 rmtree "$destrepo.new";
571 mkemptyrepo "$destrepo.new", "0644";
572 rename "$destrepo.new", $destrepo or die $!;
573 unlink "$destrepo.lock" or die $!;
577 sub main__git_upload_pack () {
578 runcmd qw(git upload-pack), $destrepo;
581 #----- arg parsing and main program -----
590 sub parseargsdispatch () {
593 delete $ENV{'GIT_DIR'}; # if not run via ssh, our parent git process
594 delete $ENV{'GIT_PREFIX'}; # sets these and they mess things up
596 if ($ENV{'DGIT_DRS_DEBUG'}) {
598 open DEBUG, ">&STDERR" or die $!;
601 if ($ARGV[0] eq '--pre-receive-hook') {
602 if ($debug) { $debug.="="; }
605 $package = shift @ARGV;
606 defined($distro = $ENV{'DGIT_DRS_DISTRO'}) or die;
607 defined($suitesfile = $ENV{'DGIT_DRS_SUITES'}) or die;
608 defined($workrepo = $ENV{'DGIT_DRS_WORK'}) or die;
609 defined($destrepo = $ENV{'DGIT_DRS_DEST'}) or die;
610 defined($keyrings = $ENV{'DGIT_DRS_KEYRINGS'}) or die $!;
611 defined($policyhook = $ENV{'DGIT_DRS_POLICYHOOK'}) or die $!;
612 open STDOUT, ">&STDERR" or die $!;
617 recorderror "$@" or die;
623 $ENV{'DGIT_DRS_DISTRO'} = argval();
624 $ENV{'DGIT_DRS_SUITES'} = argval();
625 $ENV{'DGIT_DRS_KEYRINGS'} = argval();
626 $dgitrepos = argval();
627 $ENV{'DGIT_DRS_POLICYHOOK'} = $policyhook = argval();
629 die unless @ARGV==1 && $ARGV[0] eq '--ssh';
631 my $cmd = $ENV{'SSH_ORIGINAL_COMMAND'};
641 or reject "command string not understood";
644 $realdestrepo = "$dgitrepos/$package.git";
648 my $mainfunc = $main::{"main__$funcn"};
650 reject "unknown method" unless $mainfunc;
652 my ($policy, $pollock) = policyhook(FRESHREPO,'check-package',$package);
653 if ($policy & FRESHREPO) {
656 close $pollock or die $!;
658 if (stat $realdestrepo) {
659 $destrepo = $realdestrepo;
661 $! == ENOENT or die "stat dest repo $destrepo: $!";
662 debug " fixmissing $funcn";
663 my $fixfunc = $main::{"fixmissing__$funcn"};
667 debug " running main $funcn";
672 while (my $fh = pop @lockfhs) { close $fh; }
677 if (!chdir "$dgitrepos/_tmp") {
678 $!==ENOENT or die $!;
681 foreach my $lf (<*.lock>) {
683 $tree =~ s/\.lock$//;
684 next unless acquiretree($tree, 0);
685 remove $lf or warn $!;