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 open DEBUG, ">/dev/null" or die $!;
92 our $package_re = '[0-9a-z][-+.0-9a-z]+';
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;
191 #----- git-receive-pack -----
193 sub fixmissing__git_receive_pack () {
195 $destrepo = "$dgitrepos/_tmp/${package}_prospective";
196 acquiretree($destrepo, 1);
197 my $template = "$dgitrepos/_template";
198 debug "fixmissing copy tempalate $template -> $destrepo";
199 my $r = system qw(cp -a --), $template, $destrepo;
200 !$r or die "create new repo failed failed: $r $!";
203 sub makeworkingclone () {
205 $workrepo = "$dgitrepos/_tmp/${package}_incoming$$";
206 acquiretree($workrepo, 1);
207 runcmd qw(git clone -l -q --mirror), $destrepo, $workrepo;
210 sub setupstunthook () {
211 my $prerecv = "$workrepo/hooks/pre-receive";
212 my $fh = new IO::File $prerecv, O_WRONLY|O_CREAT|O_TRUNC, 0777
213 or die "$prerecv: $!";
214 print $fh <<END or die "$prerecv: $!";
217 exec $0 --pre-receive-hook $package
219 close $fh or die "$prerecv: $!";
220 $ENV{'DGIT_DRS_WORK'}= $workrepo;
221 $ENV{'DGIT_DRS_DEST'}= $destrepo;
222 debug " stunt hook set up $prerecv";
225 sub maybeinstallprospective () {
226 return if $destrepo eq $realdestrepo;
228 if (open REJ, "<", "$workrepo/drs-error") {
231 REJ->error and die $!;
235 $!==&ENOENT or die $!;
238 debug " show-ref ($destrepo) ...";
240 my $child = open SR, "-|";
241 defined $child or die $!;
243 chdir $destrepo or die $!;
244 exec qw(git show-ref);
247 my %got = qw(tag 0 head 0);
250 debug " show-refs| $_";
251 s/^\S*[1-9a-f]\S* (\S+)$/$1/ or die;
253 m{^refs/tags/} ? 'tag' :
254 m{^refs/dgit/} ? 'head' :
258 $!=0; $?=0; close SR or $?==256 or die "$? $!";
260 debug "installprospective ?";
261 die Dumper(\%got)." -- missing refs in new repo"
262 if grep { !$_ } values %got;
264 debug "install $destrepo => $realdestrepo";
265 rename $destrepo, $realdestrepo or die $!;
266 remove "$destrepo.lock" or die $!;
269 sub main__git_receive_pack () {
272 runcmd qw(git receive-pack), $workrepo;
273 maybeinstallprospective();
276 #----- stunt post-receive hook -----
278 our ($tagname, $tagval, $suite, $oldcommit, $commit);
279 our ($version, %tagh);
282 debug " updates ...";
286 m/^(\S+) (\S+) (\S+)$/ or die "$_ ?";
287 my ($old, $sha1, $refname) = ($1, $2, $3);
288 if ($refname =~ m{^refs/tags/(?=debian/)}) {
289 reject "pushing multiple tags!" if defined $tagname;
292 reject "tag $tagname already exists -".
293 " not replacing previously-pushed version"
295 } elsif ($refname =~ m{^refs/dgit/}) {
296 reject "pushing multiple heads!" if defined $suite;
301 reject "pushing unexpected ref!";
304 STDIN->error and die $!;
306 reject "push is missing tag ref update" unless defined $tagname;
307 reject "push is missing head ref update" unless defined $suite;
308 debug " updates ok.";
312 debug " parsetag...";
313 open PT, ">dgit-tmp/plaintext" or die $!;
314 open DS, ">dgit-tmp/plaintext.asc" or die $!;
315 open T, "-|", qw(git cat-file tag), $tagval or die $!;
317 $!=0; $_=<T>; defined or die $!;
319 if (m/^(\S+) (.*)/) {
320 push @{ $tagh{$1} }, $2;
327 $!=0; $_=<T>; defined or die $!;
328 m/^($package_re) release (\S+) for \S+ \((\S+)\) \[dgit\]$/ or
329 reject "tag message not in expected format";
331 die unless $1 eq $package;
333 die "$3 != $suite " unless $3 eq $suite;
337 $!=0; $_=<T>; defined or die "missing signature? $!";
338 if (m/^\[dgit ([^"].*)\]$/) { # [dgit "something"] is for future
341 if (s/^distro\=(\S+) //) {
342 die "$1 != $distro" unless $1 eq $distro;
343 } elsif (s/^(--deliberately-$package_re) //) {
344 push @deliberatelies, $1;
345 } elsif (s/^[-+.=0-9a-z]\S* //) {
347 die "unknown dgit info in tag";
352 last if m/^-----BEGIN PGP/;
362 debug " parsetag ok.";
365 sub checksig_keyring ($) {
366 my ($keyringfile) = @_;
367 # returns primary-keyid if signed by a key in this keyring
369 # or dies on other errors
373 debug " checksig keyring $keyringfile...";
375 our @cmd = (qw(gpgv --status-fd=1 --keyring),
377 qw(dgit-tmp/plaintext.asc dgit-tmp/plaintext));
384 next unless s/^\[GNUPG:\] //;
386 debug " checksig| $_";
387 my @l = split / /, $_;
388 if ($l[0] eq 'NO_PUBKEY') {
390 } elsif ($l[0] eq 'VALIDSIG') {
392 $sigtype eq '00' or reject "signature is not of type 00!";
394 die unless defined $ok;
400 debug sprintf " checksig ok=%d", !!$ok;
405 sub dm_txt_check ($$) {
406 my ($keyid, $dmtxtfn) = @_;
407 debug " dm_txt_check $keyid $dmtxtfn";
408 open DT, '<', $dmtxtfn or die "$dmtxtfn $!";
410 m/^fingerprint:\s+$keyid$/oi
412 if (s/^allow:/ /i..0) {
415 or reject "key $keyid missing Allow section in permissions!";
420 or reject "package $package not allowed for key $keyid";
425 debug " dm_txt_check allow| $_";
426 foreach my $p (split /\s+/) {
427 if ($p eq $package) {
429 debug " dm_txt_check ok";
434 DT->error and die $!;
436 reject "key $keyid not in permissions list although in keyring!";
440 foreach my $kas (split /:/, $keyrings) {
441 debug "verifytag $kas...";
442 $kas =~ s/^([^,]+),// or die;
443 my $keyid = checksig_keyring $1;
444 if (defined $keyid) {
445 if ($kas =~ m/^a$/) {
446 debug "verifytag a ok";
448 } elsif ($kas =~ m/^m([^,]+)$/) {
449 dm_txt_check($keyid, $1);
450 debug "verifytag m ok";
457 reject "key not found in keyrings";
461 debug "checksuite ($suitesfile)";
462 open SUITES, "<", $suitesfile or die $!;
468 return if $_ eq $suite;
470 die $! if SUITES->error;
471 reject "unknown suite";
476 my $vals = $tagh{$tag};
477 reject "missing header $tag in signed tag object" unless $vals;
478 reject "multiple headers $tag in signed tag object" unless @$vals == 1;
485 tagh1('type') eq 'commit' or reject "tag refers to wrong kind of object";
486 tagh1('object') eq $commit or reject "tag refers to wrong commit";
487 tagh1('tag') eq $tagname or reject "tag name in tag is wrong";
492 debug "translated version $v";
493 $tagname eq "debian/$v" or die;
497 # check that our ref is being fast-forwarded
498 debug "oldcommit $oldcommit";
499 if ($oldcommit =~ m/[^0]/) {
500 $?=0; $!=0; my $mb = `git merge-base $commit $oldcommit`;
502 $mb eq $oldcommit or reject "not fast forward on dgit branch";
507 my @cmd = (qw(git send-pack), $destrepo,
508 "$commit:refs/dgit/$suite",
509 "$tagval:refs/tags/$tagname");
513 !$r or die "onward push failed: $r $!";
518 chdir $workrepo or die "chdir $workrepo: $!";
519 mkdir "dgit-tmp" or $!==EEXIST or die $!;
525 debug "stunthook done.";
528 #----- git-upload-pack -----
530 sub fixmissing__git_upload_pack () {
531 $destrepo = "$dgitrepos/_empty";
532 my $lfh = acquiretree($destrepo,1);
533 return if stat $destrepo;
534 die $! unless $!==ENOENT;
535 rmtree "$destrepo.new";
537 runcmd qw(git init --bare --quiet), "$destrepo.new";
538 rename "$destrepo.new", $destrepo or die $!;
539 unlink "$destrepo.lock" or die $!;
543 sub main__git_upload_pack () {
544 runcmd qw(git upload-pack), $destrepo;
547 #----- arg parsing and main program -----
556 sub parseargsdispatch () {
559 delete $ENV{'GIT_DIR'}; # if not run via ssh, our parent git process
560 delete $ENV{'GIT_PREFIX'}; # sets these and they mess things up
562 if ($ENV{'DGIT_DRS_DEBUG'}) {
564 open DEBUG, ">&STDERR" or die $!;
567 if ($ARGV[0] eq '--pre-receive-hook') {
568 if ($debug) { $debug.="="; }
571 $package = shift @ARGV;
572 defined($distro = $ENV{'DGIT_DRS_DISTRO'}) or die;
573 defined($suitesfile = $ENV{'DGIT_DRS_SUITES'}) or die;
574 defined($workrepo = $ENV{'DGIT_DRS_WORK'}) or die;
575 defined($destrepo = $ENV{'DGIT_DRS_DEST'}) or die;
576 defined($keyrings = $ENV{'DGIT_DRS_KEYRINGS'}) or die $!;
577 defined($policyhook = $ENV{'DGIT_DRS_POLICYHOOK'}) or die $!;
578 open STDOUT, ">&STDERR" or die $!;
583 recorderror "$@" or die;
589 $ENV{'DGIT_DRS_DISTRO'} = argval();
590 $ENV{'DGIT_DRS_SUITES'} = argval();
591 $ENV{'DGIT_DRS_KEYRINGS'} = argval();
592 $dgitrepos = argval();
593 $ENV{'DGIT_DRS_POLICYHOOK'} = $policyhook = argval();
595 die unless @ARGV==1 && $ARGV[0] eq '--ssh';
597 my $cmd = $ENV{'SSH_ORIGINAL_COMMAND'};
607 or reject "command string not understood";
610 $realdestrepo = "$dgitrepos/$package.git";
614 my $mainfunc = $main::{"main__$funcn"};
616 reject "unknown method" unless $mainfunc;
618 if (stat $realdestrepo) {
619 $destrepo = $realdestrepo;
621 $! == ENOENT or die "stat dest repo $destrepo: $!";
622 debug " fixmissing $funcn";
623 my $fixfunc = $main::{"fixmissing__$funcn"};
627 debug " running main $funcn";
632 while (my $fh = pop @lockfhs) { close $fh; }
637 if (!chdir "$dgitrepos/_tmp") {
638 $!==ENOENT or die $!;
641 foreach my $lf (<*.lock>) {
643 $tree =~ s/\.lock$//;
644 next unless acquiretree($tree, 0);
645 remove $lf or warn $!;