2 # dgit-repos-push-receiver
5 # .../dgit-repos-push-receiver SUITES KEYRING-AUTH-SPEC DGIT-REPOS-DIR --ssh
7 # .../dgit-repos-push-receiver --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 our $package_re = '[0-9a-z][-+.0-9a-z]+';
101 #----- utilities -----
103 sub acquirelock ($$) {
104 my ($lock, $must) = @_;
108 $fh = new IO::File, ">", $lock or die "open $lock: $!";
109 my $ok = flock $fh, $must ? LOCK_EX : (LOCK_EX|LOCK_NB);
111 return undef unless $must;
112 die "flock $lock: $!";
115 next if $! == ENOENT;
116 die "stat $lock: $!";
118 my $want = (stat _)[1];
120 my $got = (stat _)[1];
121 last if $got == $want;
126 sub acquiretree ($$) {
127 my ($tree, $must) = @_;
128 my $fh = acquirelock("$tree.lock", $must);
137 die "dgit-repos-server: reject: $_[0]\n";
140 #----- git-receive-pack -----
142 sub fixmissing__git_receive_pack () {
143 $destrepo = "$dgitrepos/_tmp/${pkg}_prospective";
144 acquiretree($destrepo, 1);
145 my $r = system qw(cp -a --), "$dgitrepos/_template", "$destrepo";
146 !$r or die "create new repo failed failed: $r $!";
149 sub makeworkingclone () {
150 $workrepo = "$dgitrepos/_tmp/${pkg}_incoming$$";
151 acquiretree($workrepo, 1);
152 runcmd qw(git clone -l -q --mirror), $destrepo, $workrepo;
155 sub setupstunthook () {
156 my $prerecv = "$workrepo/hooks/pre-receive";
157 my $fh = new IO::File, $prerecv, O_WRONLY|O_CREAT|O_TRUNC, 0777
158 or die "$prerecv: $!";
159 print $fh <<END or die "$prerecv: $!";
162 exec $0 --pre-receive-hook $pkg
164 close $fh or die "$prerecv: $!";
165 $ENV{'DGIT_RPR_WORK'}= $workrepo;
166 $ENV{'DGIT_RPR_DEST'}= $destrepo;
169 sub maybeinstallprospective () {
170 return if $destrepo eq $realdestrepo;
172 my $child = open SR, "-|";
173 defined $child or die $!;
175 chdir $destrepo or die $!;
176 exec qw(git show-ref);
179 my %got = qw(tag 0 head 0);
182 s/^\S*[1-9a-f]\S* (\S+)$/$1/ or die;
184 m{^refs/tags/} ? 'tag' :
185 m{^refs/dgit/} ? 'head' :
189 die if grep { !$_ } values %got;
190 $!=0; $?=0; close SR or die "$? $!";
192 rename $destrepo, $realdestrepo or die $!;
193 remove "$destrepo.lock" or die $!;
196 sub main__git_receive_pack () {
199 runcmd qw(git receive-pack), $destdir;
200 maybeinstallprospective();
203 #----- stunt post-receive hook -----
205 our ($tagname, $tagval, $suite, $oldcommit, $commit);
206 our ($version, %tagh);
210 m/^(\S+) (\S+) (\S+)$/ or die "$_ ?";
211 my ($old, $sha1, $refname) = ($1, $2, $3);
212 if ($refname =~ m{^refs/tags/(?=debian/)}) {
213 die if defined $tagname;
216 reject "tag $tagname already exists -".
217 " not replacing previously-pushed version"
219 } elsif ($refname =~ m{^refs/dgit/}) {
220 die if defined $suite;
228 STDIN->error and die $!;
230 die unless defined $refname;
231 die unless defined $branchname;
235 open PT, ">dgit-tmp/plaintext" or die $!;
236 open DS, ">dgit-tmp/plaintext.asc" or die $!;
237 open T, "-|", qw(git cat-file tag), $tagval or die $!;
239 $!=0; $_=<T>; defined or die $!;
241 if (m/^(\S+) (.*)/) {
242 push @{ $tagh{$1} }, $2;
249 $!=0; $_=<T>; defined or die $!;
250 m/^($package_re) release (\S+) for (\S+) \[dgit\]$/ or die;
252 die unless $1 eq $pkg;
254 die unless $3 eq $suite;
258 $!=0; $_=<T>; defined or die $!;
259 last if m/^-----BEGIN PGP/;
271 sub checksig_keyring ($) {
272 my ($keyringfile) = @_;
273 # returns primary-keyid if signed by a key in this keyring
275 # or dies on other errors
279 open P, "-|", (qw(gpgv --status-fd=1),
280 map { '--keyring', $_ }, @keyrings,
281 qw(dgit-tmp/plaintext.asc dgit-tmp/plaintext))
285 next unless s/^\[GNUPG:\]: //;
287 my @l = split / /, $_;
288 if ($l[0] eq 'NO_PUBKEY') {
290 } elsif ($l[0] eq 'VALIDSIG') {
292 $sigtype eq '00' or reject "signature is not of type 00!";
294 die unless defined $ok;
303 sub dm_txt_check ($$) {
304 my ($keyid, $dmtxtfn) = @_;
305 open DT, '<', $dmtxtfn or die "$dmtxtfn $!";
307 m/^fingerprint:\s+$keyid$/oi
310 or reject "key $keyid missing Allow section in permissions!";
315 or reject "package $package not allowed for key $keyid";
319 foreach my $p (split /\s+/) {
320 return if $p eq $package; # yay!
323 DT->error and die $!;
325 reject "key $keyid not in permissions list although in keyring!";
329 foreach my $kas (split /:/, $keyrings) {
330 $kas =~ s/^([^,]+),// or die;
331 my $keyid = checksig_keyring $1;
332 if (defined $keyid) {
333 if ($kas =~ m/^a$/) {
335 } elsif ($kas =~ m/^m([^,]+)$/) {
336 dm_txt_check($keyid, $1);
343 reject "key not found in keyrings";
347 open SUITES, "<", $suitesfile or die $!;
353 return if $_ eq $suite;
355 die $! if SUITES->error;
356 reject "unknown suite";
361 tagh1('type') eq 'commit' or die;
362 tagh1('object') eq $commit or die;
363 tagh1('tag') eq $tagname or die;
367 $tagname eq "debian/$v" or die;
369 # check that our ref is being fast-forwarded
370 if ($oldcommit =~ m/[^0]/) {
371 $?=0; $!=0; my $mb = `git merge-base $commit $oldcommit`;
373 $mb eq $oldcommit or reject "not fast forward on dgit branch";
379 my $r = system (qw(git send-pack),
381 "$commit:refs/dgit/$suite",
382 "$tagval:refs/tags/$tagname");
383 !$r or die "onward push failed: $r $!";
387 chdir $workrepo or die "chdir $workrepo: $!";
388 mkdir "dgit-tmp" or $!==EEXIST or die $!;
396 #----- arg parsing and main program -----
405 sub parseargsdispatch () {
408 if ($ARGV[0] eq '--pre-receive-hook') {
412 defined($suitesfile = $ENV{'DGIT_RPR_SUITES'}) or die;
413 defined($workrepo = $ENV{'DGIT_RPR_WORK'}) or die;
414 defined($destrepo = $ENV{'DGIT_RPR_DEST'}) or die;
415 defined($keyrings = $ENV{'DGIT_RPR_KEYRINGS'}) or die $!;
416 open STDOUT, ">&STDERR" or die $!;
421 $ENV{'DGIT_RPR_SUITES'} = argval();
422 $ENV{'DGIT_RPR_KEYRINGS'} = argval();
423 $dgitrepos = argval();
425 die unless @ARGV==1 && $ARGV[0] eq '--ssh';
427 my $cmd = $ENV{'SSH_ORIGINAL_COMMAND'};
437 or reject "command string not understood";
440 $realdestrepo = "$dgitrepos/$pkg.git";
444 my $mainfunc = $main::{"main__$funcn"};
446 reject "unknown method" unless $mainfunc;
448 if (stat $realdestrepo) {
449 $destrepo = $realdestrepo;
451 $! == ENOENT or die "stat dest repo $destrepo: $!";
452 my $fixfunc = $main::{"fixmissing__$funcn"};
460 while (my $fh = pop $lockfhs) { close $fh; }
465 chdir "$dgitrepos/_tmp" or die $!;
466 foreach my $lock (<*.lock>) {
468 $tree =~ s/\.lock$//;
469 next unless acquiretree($tree, 0);
470 remove $lock or warn $!;