5 # .../dgit-repos-server DISTRO SUITES KEYRING-AUTH-SPEC DGIT-REPOS-DIR --ssh
7 # .../dgit-repos-server --pre-receive-hook PACKAGE
9 # Invoked as the ssh restricted command
11 # Works like git-receive-pack
13 # SUITES is the name of a file which lists the permissible suites
14 # one per line (#-comments and blank lines ignored)
16 # KEYRING-AUTH-SPEC is a :-separated list of
17 # KEYRING.GPG,AUTH-SPEC
18 # where AUTH-SPEC is one of
25 # - extract the destination repo name
26 # - make a hardlink clone of the destination repo
27 # - provide the destination with a stunt pre-receive hook
28 # - run actual git-receive-pack with that new destination
29 # as a result of this the stunt pre-receive hook runs; it does this:
30 # + understand what refs we are allegedly updating and
31 # check some correspondences:
32 # * we are updating only refs/tags/debian/* and refs/dgit/*
33 # * and only one of each
34 # * and the tag does not already exist
36 # * recover the suite name from the destination refs/dgit/ ref
37 # + disassemble the signed tag into its various fields and signature
39 # * parsing the first line of the tag message to recover
40 # the package name, version and suite
41 # * checking that the package name corresponds to the dest repo name
42 # * checking that the suite name is as recovered above
43 # + verify the signature on the signed tag
44 # and if necessary check that the keyid and package are listed in dm.txt
45 # + check various correspondences:
46 # * the suite is one of those permitted
47 # * the signed tag must refer to a commit
48 # * the signed tag commit must be the refs/dgit value
49 # * the name in the signed tag must correspond to its ref name
50 # * the tag name must be debian/<version> (massaged as needed)
51 # * the signed tag has a suitable name
52 # * the commit is a fast forward
53 # + push the signed tag and new dgit branch to the actual repo
55 # If the destination repo does not already exist, we need to make
56 # sure that we create it reasonably atomically, and also that
57 # we don't every have a destination repo containing no refs at all
58 # (because such a thing causes git-fetch-pack to barf). So then we
59 # do as above, except:
60 # - before starting, we take out our own lock for the destination repo
61 # - we create a prospective new destination repo by making a copy
63 # - we use the prospective new destination repo instead of the
64 # actual new destination repo (since the latter doesn't exist)
65 # - we set up a post-receive hook as well, which
66 # + touches a stamp file
67 # - after git-receive-pack exits, we
68 # + check that the prospective repo contains a tag and head
69 # + rename the prospective destination repo into place
73 # - Temporary working trees and their locks are cleaned up
74 # opportunistically by a program which tries to take each lock and
75 # if successful deletes both the tree and the lockfile
76 # - Prospective working trees and their locks are cleaned up by
77 # a program which tries to take each lock and if successful
78 # deletes any prospective working tree and the lock (but not
79 # of course any actual tree)
80 # - It is forbidden to _remove_ the lockfile without removing
81 # the corresponding temporary tree, as the lockfile is also
82 # a stampfile whose presence indicates that there may be
87 use File::Path qw(rmtree);
89 open DEBUG, ">/dev/null" or die $!;
91 our $package_re = '[0-9a-z][-+.0-9a-z]+';
104 #----- utilities -----
107 print DEBUG "$debug @_\n";
110 sub acquirelock ($$) {
111 my ($lock, $must) = @_;
113 printf DEBUG "$debug locking %s %d\n", $lock, $must;
116 $fh = new IO::File $lock, ">" or die "open $lock: $!";
117 my $ok = flock $fh, $must ? LOCK_EX : (LOCK_EX|LOCK_NB);
119 die "flock $lock: $!" if $must;
120 debug " locking $lock failed";
124 next if $! == ENOENT;
125 die "stat $lock: $!";
127 my $want = (stat _)[1];
129 my $got = (stat _)[1];
130 last if $got == $want;
135 sub acquiretree ($$) {
136 my ($tree, $must) = @_;
137 my $fh = acquirelock("$tree.lock", $must);
146 my $tmpdir = "$dgitrepos/_tmp";
147 return if mkdir $tmpdir;
148 return if $! == EEXIST;
152 sub recorderror ($) {
154 my $w = $ENV{'DGIT_DRS_WORK'}; # we are in stunthook
157 open ERR, ">", "$w/drs-error" or die $!;
158 print ERR $why, "\n" or die $!;
167 recorderror "reject: $why";
168 die "dgit-repos-server: reject: $why\n";
174 local $Data::Dumper::Indent = 0;
175 local $Data::Dumper::Terse = 1;
176 debug "|".Dumper(\@_);
184 die "@_ $? $!" if $r;
187 #----- git-receive-pack -----
189 sub fixmissing__git_receive_pack () {
191 $destrepo = "$dgitrepos/_tmp/${package}_prospective";
192 acquiretree($destrepo, 1);
193 my $template = "$dgitrepos/_template";
194 debug "fixmissing copy tempalate $template -> $destrepo";
195 my $r = system qw(cp -a --), $template, $destrepo;
196 !$r or die "create new repo failed failed: $r $!";
199 sub makeworkingclone () {
201 $workrepo = "$dgitrepos/_tmp/${package}_incoming$$";
202 acquiretree($workrepo, 1);
203 runcmd qw(git clone -l -q --mirror), $destrepo, $workrepo;
206 sub setupstunthook () {
207 my $prerecv = "$workrepo/hooks/pre-receive";
208 my $fh = new IO::File $prerecv, O_WRONLY|O_CREAT|O_TRUNC, 0777
209 or die "$prerecv: $!";
210 print $fh <<END or die "$prerecv: $!";
213 exec $0 --pre-receive-hook $package
215 close $fh or die "$prerecv: $!";
216 $ENV{'DGIT_DRS_WORK'}= $workrepo;
217 $ENV{'DGIT_DRS_DEST'}= $destrepo;
218 debug " stunt hook set up $prerecv";
221 sub maybeinstallprospective () {
222 return if $destrepo eq $realdestrepo;
224 if (open REJ, "<", "$workrepo/drs-error") {
227 REJ->error and die $!;
231 $!==&ENOENT or die $!;
234 debug " show-ref ($destrepo) ...";
236 my $child = open SR, "-|";
237 defined $child or die $!;
239 chdir $destrepo or die $!;
240 exec qw(git show-ref);
243 my %got = qw(tag 0 head 0);
246 debug " show-refs| $_";
247 s/^\S*[1-9a-f]\S* (\S+)$/$1/ or die;
249 m{^refs/tags/} ? 'tag' :
250 m{^refs/dgit/} ? 'head' :
254 $!=0; $?=0; close SR or $?==256 or die "$? $!";
256 debug "installprospective ?";
257 die Dumper(\%got)." -- missing refs in new repo"
258 if grep { !$_ } values %got;
260 debug "install $destrepo => $realdestrepo";
261 rename $destrepo, $realdestrepo or die $!;
262 remove "$destrepo.lock" or die $!;
265 sub main__git_receive_pack () {
268 runcmd qw(git receive-pack), $workrepo;
269 maybeinstallprospective();
272 #----- stunt post-receive hook -----
274 our ($tagname, $tagval, $suite, $oldcommit, $commit);
275 our ($version, %tagh);
278 debug " updates ...";
282 m/^(\S+) (\S+) (\S+)$/ or die "$_ ?";
283 my ($old, $sha1, $refname) = ($1, $2, $3);
284 if ($refname =~ m{^refs/tags/(?=debian/)}) {
285 reject "pushing multiple tags!" if defined $tagname;
288 reject "tag $tagname already exists -".
289 " not replacing previously-pushed version"
291 } elsif ($refname =~ m{^refs/dgit/}) {
292 reject "pushing multiple heads!" if defined $suite;
297 reject "pushing unexpected ref!";
300 STDIN->error and die $!;
302 reject "push is missing tag ref update" unless defined $tagname;
303 reject "push is missing head ref update" unless defined $suite;
304 debug " updates ok.";
308 debug " parsetag...";
309 open PT, ">dgit-tmp/plaintext" or die $!;
310 open DS, ">dgit-tmp/plaintext.asc" or die $!;
311 open T, "-|", qw(git cat-file tag), $tagval or die $!;
313 $!=0; $_=<T>; defined or die $!;
315 if (m/^(\S+) (.*)/) {
316 push @{ $tagh{$1} }, $2;
323 $!=0; $_=<T>; defined or die $!;
324 m/^($package_re) release (\S+) for \S+ \((\S+)\) \[dgit\]$/ or
325 reject "tag message not in expected format";
327 die unless $1 eq $package;
329 die "$3 != $suite " unless $3 eq $suite;
333 $!=0; $_=<T>; defined or die "missing signature? $!";
334 if (m/^\[dgit ([^"].*)\]$/) { # [dgit "something"] is for future
337 if (s/^distro\=(\S+) //) {
338 die "$1 != $distro" unless $1 eq $distro;
339 } elsif (s/^[-+.=0-9a-z]+ //) {
341 die "unknown dgit info in tag";
346 last if m/^-----BEGIN PGP/;
356 debug " parsetag ok.";
359 sub checksig_keyring ($) {
360 my ($keyringfile) = @_;
361 # returns primary-keyid if signed by a key in this keyring
363 # or dies on other errors
367 debug " checksig keyring $keyringfile...";
369 our @cmd = (qw(gpgv --status-fd=1 --keyring),
371 qw(dgit-tmp/plaintext.asc dgit-tmp/plaintext));
378 next unless s/^\[GNUPG:\] //;
380 debug " checksig| $_";
381 my @l = split / /, $_;
382 if ($l[0] eq 'NO_PUBKEY') {
384 } elsif ($l[0] eq 'VALIDSIG') {
386 $sigtype eq '00' or reject "signature is not of type 00!";
388 die unless defined $ok;
394 debug sprintf " checksig ok=%d", !!$ok;
399 sub dm_txt_check ($$) {
400 my ($keyid, $dmtxtfn) = @_;
401 debug " dm_txt_check $keyid $dmtxtfn";
402 open DT, '<', $dmtxtfn or die "$dmtxtfn $!";
404 m/^fingerprint:\s+$keyid$/oi
406 if (s/^allow:/ /i..0) {
409 or reject "key $keyid missing Allow section in permissions!";
414 or reject "package $package not allowed for key $keyid";
419 debug " dm_txt_check allow| $_";
420 foreach my $p (split /\s+/) {
421 if ($p eq $package) {
423 debug " dm_txt_check ok";
428 DT->error and die $!;
430 reject "key $keyid not in permissions list although in keyring!";
434 foreach my $kas (split /:/, $keyrings) {
435 debug "verifytag $kas...";
436 $kas =~ s/^([^,]+),// or die;
437 my $keyid = checksig_keyring $1;
438 if (defined $keyid) {
439 if ($kas =~ m/^a$/) {
440 debug "verifytag a ok";
442 } elsif ($kas =~ m/^m([^,]+)$/) {
443 dm_txt_check($keyid, $1);
444 debug "verifytag m ok";
451 reject "key not found in keyrings";
455 debug "checksuite ($suitesfile)";
456 open SUITES, "<", $suitesfile or die $!;
462 return if $_ eq $suite;
464 die $! if SUITES->error;
465 reject "unknown suite";
470 my $vals = $tagh{$tag};
471 reject "missing header $tag in signed tag object" unless $vals;
472 reject "multiple headers $tag in signed tag object" unless @$vals == 1;
479 tagh1('type') eq 'commit' or reject "tag refers to wrong kind of object";
480 tagh1('object') eq $commit or reject "tag refers to wrong commit";
481 tagh1('tag') eq $tagname or reject "tag name in tag is wrong";
486 debug "translated version $v";
487 $tagname eq "debian/$v" or die;
491 # check that our ref is being fast-forwarded
492 debug "oldcommit $oldcommit";
493 if ($oldcommit =~ m/[^0]/) {
494 $?=0; $!=0; my $mb = `git merge-base $commit $oldcommit`;
496 $mb eq $oldcommit or reject "not fast forward on dgit branch";
501 my @cmd = (qw(git send-pack), $destrepo,
502 "$commit:refs/dgit/$suite",
503 "$tagval:refs/tags/$tagname");
507 !$r or die "onward push failed: $r $!";
512 chdir $workrepo or die "chdir $workrepo: $!";
513 mkdir "dgit-tmp" or $!==EEXIST or die $!;
519 debug "stunthook done.";
522 #----- git-upload-pack -----
524 sub fixmissing__git_upload_pack () {
525 $destrepo = "$dgitrepos/_empty";
526 my $lfh = acquiretree($destrepo,1);
527 return if stat $destrepo;
528 die $! unless $!==ENOENT;
529 rmtree "$destrepo.new";
531 runcmd qw(git init --bare --quiet), "$destrepo.new";
532 rename "$destrepo.new", $destrepo or die $!;
533 unlink "$destrepo.lock" or die $!;
537 sub main__git_upload_pack () {
538 runcmd qw(git upload-pack), $destrepo;
541 #----- arg parsing and main program -----
550 sub parseargsdispatch () {
553 delete $ENV{'GIT_DIR'}; # if not run via ssh, our parent git process
554 delete $ENV{'GIT_PREFIX'}; # sets these and they mess things up
556 if ($ENV{'DGIT_DRS_DEBUG'}) {
558 open DEBUG, ">&STDERR" or die $!;
561 if ($ARGV[0] eq '--pre-receive-hook') {
562 if ($debug) { $debug.="="; }
565 $package = shift @ARGV;
566 defined($distro = $ENV{'DGIT_DRS_DISTRO'}) or die;
567 defined($suitesfile = $ENV{'DGIT_DRS_SUITES'}) or die;
568 defined($workrepo = $ENV{'DGIT_DRS_WORK'}) or die;
569 defined($destrepo = $ENV{'DGIT_DRS_DEST'}) or die;
570 defined($keyrings = $ENV{'DGIT_DRS_KEYRINGS'}) or die $!;
571 open STDOUT, ">&STDERR" or die $!;
576 recorderror "$@" or die;
582 $ENV{'DGIT_DRS_DISTRO'} = argval();
583 $ENV{'DGIT_DRS_SUITES'} = argval();
584 $ENV{'DGIT_DRS_KEYRINGS'} = argval();
585 $dgitrepos = argval();
587 die unless @ARGV==1 && $ARGV[0] eq '--ssh';
589 my $cmd = $ENV{'SSH_ORIGINAL_COMMAND'};
599 or reject "command string not understood";
602 $realdestrepo = "$dgitrepos/$package.git";
606 my $mainfunc = $main::{"main__$funcn"};
608 reject "unknown method" unless $mainfunc;
610 if (stat $realdestrepo) {
611 $destrepo = $realdestrepo;
613 $! == ENOENT or die "stat dest repo $destrepo: $!";
614 debug " fixmissing $funcn";
615 my $fixfunc = $main::{"fixmissing__$funcn"};
619 debug " running main $funcn";
624 while (my $fh = pop @lockfhs) { close $fh; }
629 if (!chdir "$dgitrepos/_tmp") {
630 $!==ENOENT or die $!;
633 foreach my $lf (<*.lock>) {
635 $tree =~ s/\.lock$//;
636 next unless acquiretree($tree, 0);
637 remove $lf or warn $!;