5 # dgit-repos-server DISTRO DISTRO-DIR AUTH-SPEC [<settings>] --ssh
6 # dgit-repos-server DISTRO DISTRO-DIR AUTH-SPEC [<settings>] --cron
8 # --repos=GIT-REPOS-DIR default DISTRO-DIR/repos/
9 # --suites=SUITES-FILE default DISTRO-DIR/suites
10 # --policy-hook=POLICY-HOOK default DISTRO-DIR/policy-hook
11 # --dgit-live=DGIT-LIVE-DIR default DISTRO-DIR/dgit-live
12 # (DISTRO-DIR is not used other than as default and to pass to policy hook)
14 # .../dgit-repos-server --pre-receive-hook PACKAGE
16 # Invoked as the ssh restricted command
18 # Works like git-receive-pack
20 # SUITES is the name of a file which lists the permissible suites
21 # one per line (#-comments and blank lines ignored)
23 # AUTH-SPEC is a :-separated list of
24 # KEYRING.GPG,AUTH-SPEC
25 # where AUTH-SPEC is one of
28 # (With --cron AUTH-SPEC is not used and may be the empty string.)
31 $SIG{__WARN__} = sub { die $_[0]; };
33 # DGIT-REPOS-DIR contains:
34 # git tree (or other object) lock (in acquisition order, outer first)
36 # _tmp/PACKAGE_prospective ! } SAME.lock, held during receive-pack
38 # _tmp/PACKAGE_incoming$$ ! } SAME.lock, held during receive-pack
39 # _tmp/PACKAGE_incoming$$_fresh ! }
41 # PACKAGE.git } PACKAGE.git.lock
42 # PACKAGE_garbage } (also covers executions of
43 # PACKAGE_garbage-old } policy hook script for PACKAGE)
44 # PACKAGE_garbage-tmp }
45 # policy* } (for policy hook script, covered by
46 # } lock only when invoked for a package)
48 # leaf locks, held during brief operaton only:
53 # _template } SAME.lock
55 # locks marked ! may be held during client data transfer
57 # What we do on push is this:
58 # - extract the destination repo name
59 # - make a hardlink clone of the destination repo
60 # - provide the destination with a stunt pre-receive hook
61 # - run actual git-receive-pack with that new destination
62 # as a result of this the stunt pre-receive hook runs; it does this:
63 # + understand what refs we are allegedly updating and
64 # check some correspondences:
65 # * we are updating only refs/tags/debian/* and refs/dgit/*
66 # * and only one of each
67 # * and the tag does not already exist
69 # * recover the suite name from the destination refs/dgit/ ref
70 # + disassemble the signed tag into its various fields and signature
72 # * parsing the first line of the tag message to recover
73 # the package name, version and suite
74 # * checking that the package name corresponds to the dest repo name
75 # * checking that the suite name is as recovered above
76 # + verify the signature on the signed tag
77 # and if necessary check that the keyid and package are listed in dm.txt
78 # + check various correspondences:
79 # * the signed tag must refer to a commit
80 # * the signed tag commit must be the refs/dgit value
81 # * the name in the signed tag must correspond to its ref name
82 # * the tag name must be debian/<version> (massaged as needed)
83 # * the suite is one of those permitted
84 # * the signed tag has a suitable name
85 # * run the "push" policy hook
86 # * replay prevention for --deliberately-not-fast-forward
87 # * check the commit is a fast forward
88 # * handle a request from the policy hook for a fresh repo
89 # + push the signed tag and new dgit branch to the actual repo
91 # If the destination repo does not already exist, we need to make
92 # sure that we create it reasonably atomically, and also that
93 # we don't every have a destination repo containing no refs at all
94 # (because such a thing causes git-fetch-pack to barf). So then we
95 # do as above, except:
96 # - before starting, we take out our own lock for the destination repo
97 # - we create a prospective new destination repo by making a copy
99 # - we use the prospective new destination repo instead of the
100 # actual new destination repo (since the latter doesn't exist)
101 # - after git-receive-pack exits, we
102 # + check that the prospective repo contains a tag and head
103 # + rename the prospective destination repo into place
106 # - We are crash-only
107 # - Temporary working trees and their locks are cleaned up
108 # opportunistically by a program which tries to take each lock and
109 # if successful deletes both the tree and the lockfile
110 # - Prospective working trees and their locks are cleaned up by
111 # a program which tries to take each lock and if successful
112 # deletes any prospective working tree and the lock (but not
113 # of course any actual tree)
114 # - It is forbidden to _remove_ the lockfile without removing
115 # the corresponding temporary tree, as the lockfile is also
116 # a stampfile whose presence indicates that there may be
119 # Policy hook script is invoked like this:
120 # POLICY-HOOK-SCRIPT DISTRO DGIT-REPOS-DIR DGIT-LIVE-DIR DISTRO-DIR ACTION...
122 # POLICY-HOOK-SCRIPT ... check-list [...]
123 # POLICY-HOOK-SCRIPT ... check-package PACKAGE [...]
124 # POLICY-HOOK-SCRIPT ... push PACKAGE \
125 # VERSION SUITE TAGNAME DELIBERATELIES [...]
126 # POLICY-HOOK-SCRIPT ... push-confirm PACKAGE \
127 # VERSION SUITE TAGNAME DELIBERATELIES FRESH-REPO|'' [...]
129 # Exit status is a bitmask. Bit weight constants are defined in Dgit.pm.
131 # suppress dgit-repos-server's fast-forward check ("push" only)
133 # blow away repo right away (ie, as if before push or fetch)
134 # ("check-package" and "push" only)
135 # any unexpected bits mean failure, and then known set bits are ignored
136 # if no unexpected bits set, operation continues (subject to meaning
137 # of any expected bits set). So, eg, exit 0 means "continue normally"
138 # and would be appropriate for an unknown action.
140 # cwd for push and push-confirm is a temporary repo where the
141 # to-be-pushed objects have been received; TAGNAME is the
144 # FRESH-REPO is '' iff the repo for this package already existed, or
145 # the pathname of the newly-created repo which will be renamed into
146 # place if everything goes well. (NB that this is generally not the
147 # same repo as the cwd, because the objects are first received into a
148 # temporary repo so they can be examined.)
150 # if push requested FRESHREPO, push-confirm happens in said fresh repo
151 # and FRESH-REPO is guaranteed not to be ''.
153 # policy hook for a particular package will be invoked only once at
154 # a time - (see comments about DGIT-REPOS-DIR, above)
156 # check-list and check-package are invoked via the --cron option.
157 # First, without any locking, check-list is called. It should produce
158 # a list of package names (one per line). Then check-package will be
159 # invoked for each named package, in each case after taking an
162 # If policy hook wants to run dgit (or something else in the dgit
163 # package), it should use DGIT-LIVE-DIR/dgit (etc.)
167 use Fcntl qw(:flock);
168 use File::Path qw(rmtree);
169 use File::Temp qw(tempfile);
171 use Debian::Dgit qw(:DEFAULT :policyflags);
192 #----- utilities -----
194 sub realdestrepo () { "$dgitrepos/$package.git"; }
196 sub acquirelock ($$) {
197 my ($lock, $must) = @_;
199 printdebug sprintf "locking %s %d\n", $lock, $must;
202 $fh = new IO::File $lock, ">" or die "open $lock: $!";
203 my $ok = flock $fh, $must ? LOCK_EX : (LOCK_EX|LOCK_NB);
205 die "flock $lock: $!" if $must;
206 printdebug " locking $lock failed\n";
209 next unless stat_exists $lock;
210 my $want = (stat _)[1];
212 my $got = (stat _)[1];
213 last if $got == $want;
218 sub acquirermtree ($$) {
219 my ($tree, $must) = @_;
220 my $fh = acquirelock("$tree.lock", $must);
228 sub locksometree ($) {
230 acquirelock("$tree.lock", 1);
233 sub lockrealtree () {
234 locksometree(realdestrepo);
237 sub mkrepotmp () { ensuredir "$dgitrepos/_tmp" };
239 sub removedtagsfile () { "$dgitrepos/_removed-tags/$package"; }
241 sub recorderror ($) {
243 my $w = $ENV{'DGIT_DRS_WORK'}; # we are in stunthook
246 open ERR, ">", "$w/drs-error" or die $!;
247 print ERR $why, "\n" or die $!;
256 recorderror "reject: $why";
257 die "dgit-repos-server: reject: $why\n";
264 die (shellquote @_)." $? $!" if $r;
268 my ($policyallowbits, @polargs) = @_;
269 # => ($exitstatuspolicybitmap);
270 die if $policyallowbits & ~0x3e;
271 my @cmd = ($policyhook,$distro,$dgitrepos,$dgitlive,$distrodir,@polargs);
274 die "system: $!" if $r < 0;
275 die "dgit-repos-server: policy hook failed (or rejected) ($?)\n"
276 if $r & ~($policyallowbits << 8);
277 printdebug sprintf "hook => %#x\n", $r;
281 sub mkemptyrepo ($$) {
282 my ($dir,$sharedperm) = @_;
283 runcmd qw(git init --bare --quiet), "--shared=$sharedperm", $dir;
286 sub mkrepo_fromtemplate ($) {
288 my $template = "$dgitrepos/_template";
289 locksometree($template);
290 printdebug "copy template $template -> $dir\n";
291 my $r = system qw(cp -a --), $template, $dir;
292 !$r or die "create new repo $dir failed: $r $!";
295 sub movetogarbage () {
296 # realdestrepo must have been locked
298 ensuredir "$dgitrepos/_removed-tags";
299 open PREVIOUS, ">>", removedtagsfile or die removedtagsfile." $!";
300 git_for_each_ref(debiantag('*'), sub {
301 my ($objid,$objtype,$fullrefname,$reftail) = @_;
302 print PREVIOUS "\n$objid $reftail .\n" or die $!;
304 close PREVIOUS or die $!;
306 my $garbagerepo = "$dgitrepos/${package}_garbage";
307 # We arrange to always keep at least one old tree, for recovery
308 # from mistakes. This is either $garbage or $garbage-old.
309 if (stat_exists "$garbagerepo") {
310 printdebug "movetogarbage: rmtree $garbagerepo-tmp\n";
311 rmtree "$garbagerepo-tmp";
312 if (rename "$garbagerepo-old", "$garbagerepo-tmp") {
313 printdebug "movetogarbage: $garbagerepo-old -> -tmp, rmtree\n";
314 rmtree "$garbagerepo-tmp";
316 die "$garbagerepo $!" unless $!==ENOENT;
317 printdebug "movetogarbage: $garbagerepo-old -> -tmp\n";
319 printdebug "movetogarbage: $garbagerepo -> -old\n";
320 rename "$garbagerepo", "$garbagerepo-old" or die "$garbagerepo $!";
322 my $real = realdestrepo;
323 printdebug "movetogarbage: $real -> $garbagerepo\n";
324 rename $real, $garbagerepo
326 or die "$garbagerepo $!";
329 sub policy_checkpackage () {
330 my $lfh = lockrealtree();
332 $policy = policyhook(FRESHREPO,'check-package',$package);
333 if ($policy & FRESHREPO) {
340 #----- git-receive-pack -----
342 sub fixmissing__git_receive_pack () {
344 $destrepo = "$dgitrepos/_tmp/${package}_prospective";
345 acquirermtree($destrepo, 1);
346 mkrepo_fromtemplate($destrepo);
349 sub makeworkingclone () {
351 $workrepo = "$dgitrepos/_tmp/${package}_incoming$$";
352 acquirermtree($workrepo, 1);
353 my $lfh = lockrealtree();
354 runcmd qw(git clone -l -q --mirror), $destrepo, $workrepo;
356 rmtree "${workrepo}_fresh";
359 sub setupstunthook () {
360 my $prerecv = "$workrepo/hooks/pre-receive";
361 my $fh = new IO::File $prerecv, O_WRONLY|O_CREAT|O_TRUNC, 0777
362 or die "$prerecv: $!";
363 print $fh <<END or die "$prerecv: $!";
366 exec $0 --pre-receive-hook $package
368 close $fh or die "$prerecv: $!";
369 $ENV{'DGIT_DRS_WORK'}= $workrepo;
370 $ENV{'DGIT_DRS_DEST'}= $destrepo;
371 printdebug " stunt hook set up $prerecv\n";
374 sub dealwithfreshrepo () {
375 my $freshrepo = "${workrepo}_fresh";
376 return unless stat_exists $freshrepo;
377 $destrepo = $freshrepo;
380 sub maybeinstallprospective () {
381 return if $destrepo eq realdestrepo;
383 if (open REJ, "<", "$workrepo/drs-error") {
386 REJ->error and die $!;
390 $!==&ENOENT or die $!;
393 printdebug " show-ref ($destrepo) ...\n";
395 my $child = open SR, "-|";
396 defined $child or die $!;
398 chdir $destrepo or die $!;
399 exec qw(git show-ref);
402 my %got = qw(tag 0 head 0);
405 printdebug " show-refs| $_\n";
406 s/^\S*[1-9a-f]\S* (\S+)$/$1/ or die;
408 m{^refs/tags/} ? 'tag' :
409 m{^refs/dgit/} ? 'head' :
413 $!=0; $?=0; close SR or $?==256 or die "$? $!";
415 printdebug "installprospective ?\n";
416 die Dumper(\%got)." -- missing refs in new repo"
417 if grep { !$_ } values %got;
421 if ($destrepo eq "${workrepo}_fresh") {
425 printdebug "install $destrepo => ".realdestrepo."\n";
426 rename $destrepo, realdestrepo or die $!;
427 remove "$destrepo.lock" or die $!;
430 sub main__git_receive_pack () {
433 runcmd qw(git receive-pack), $workrepo;
435 maybeinstallprospective();
438 #----- stunt post-receive hook -----
440 our ($tagname, $tagval, $suite, $oldcommit, $commit);
441 our ($version, %tagh);
444 printdebug " updates ...\n";
447 printdebug " upd.| $_\n";
448 m/^(\S+) (\S+) (\S+)$/ or die "$_ ?";
449 my ($old, $sha1, $refname) = ($1, $2, $3);
450 if ($refname =~ m{^refs/tags/(?=debian/)}) {
451 reject "pushing multiple tags!" if defined $tagname;
454 reject "tag $tagname already exists -".
455 " not replacing previously-pushed version"
457 } elsif ($refname =~ m{^refs/dgit/}) {
458 reject "pushing multiple heads!" if defined $suite;
463 reject "pushing unexpected ref!";
466 STDIN->error and die $!;
468 reject "push is missing tag ref update" unless defined $tagname;
469 reject "push is missing head ref update" unless defined $suite;
470 printdebug " updates ok.\n";
474 printdebug " parsetag...\n";
475 open PT, ">dgit-tmp/plaintext" or die $!;
476 open DS, ">dgit-tmp/plaintext.asc" or die $!;
477 open T, "-|", qw(git cat-file tag), $tagval or die $!;
479 $!=0; $_=<T>; defined or die $!;
481 if (m/^(\S+) (.*)/) {
482 push @{ $tagh{$1} }, $2;
489 $!=0; $_=<T>; defined or die $!;
490 m/^($package_re) release (\S+) for \S+ \((\S+)\) \[dgit\]$/ or
491 reject "tag message not in expected format";
493 die unless $1 eq $package;
495 die "$3 != $suite " unless $3 eq $suite;
499 print PT $copyl or die $!;
500 $!=0; $_=<T>; defined or die "missing signature? $!";
502 if (m/^\[dgit ([^"].*)\]$/) { # [dgit "something"] is for future
505 if (s/^distro\=(\S+) //) {
506 die "$1 != $distro" unless $1 eq $distro;
507 } elsif (s/^(--deliberately-$deliberately_re) //) {
508 push @deliberatelies, $1;
509 } elsif (s/^supersede:(\S+)=(\w+) //) {
510 die "supersede $1 twice" if defined $supersedes{$1};
511 $supersedes{$1} = $2;
512 } elsif (s/^[-+.=0-9a-z]\S* //) {
514 die "unknown dgit info in tag ($_)";
519 last if m/^-----BEGIN PGP/;
530 printdebug " parsetag ok.\n";
533 sub checksig_keyring ($) {
534 my ($keyringfile) = @_;
535 # returns primary-keyid if signed by a key in this keyring
537 # or dies on other errors
541 printdebug " checksig keyring $keyringfile...\n";
543 our @cmd = (qw(gpgv --status-fd=1 --keyring),
545 qw(dgit-tmp/plaintext.asc dgit-tmp/plaintext));
552 next unless s/^\[GNUPG:\] //;
554 printdebug " checksig| $_\n";
555 my @l = split / /, $_;
556 if ($l[0] eq 'NO_PUBKEY') {
558 } elsif ($l[0] eq 'VALIDSIG') {
560 $sigtype eq '00' or reject "signature is not of type 00!";
562 die unless defined $ok;
568 printdebug sprintf " checksig ok=%d\n", !!$ok;
573 sub dm_txt_check ($$) {
574 my ($keyid, $dmtxtfn) = @_;
575 printdebug " dm_txt_check $keyid $dmtxtfn\n";
576 open DT, '<', $dmtxtfn or die "$dmtxtfn $!";
578 m/^fingerprint:\s+$keyid$/oi
580 if (s/^allow:/ /i..0) {
583 or reject "key $keyid missing Allow section in permissions!";
588 or reject "package $package not allowed for key $keyid";
593 printdebug " dm_txt_check allow| $_\n";
594 foreach my $p (split /\s+/) {
595 if ($p eq $package) {
597 printdebug " dm_txt_check ok\n";
602 DT->error and die $!;
604 reject "key $keyid not in permissions list although in keyring!";
608 foreach my $kas (split /:/, $keyrings) {
609 printdebug "verifytag $kas...\n";
610 $kas =~ s/^([^,]+),// or die;
611 my $keyid = checksig_keyring $1;
612 if (defined $keyid) {
613 if ($kas =~ m/^a$/) {
614 printdebug "verifytag a ok\n";
616 } elsif ($kas =~ m/^m([^,]+)$/) {
617 dm_txt_check($keyid, $1);
618 printdebug "verifytag m ok\n";
625 reject "key not found in keyrings";
629 printdebug "checksuite ($suitesfile)\n";
630 open SUITES, "<", $suitesfile or die $!;
636 return if $_ eq $suite;
638 die $! if SUITES->error;
639 reject "unknown suite";
642 sub checktagnoreplay () {
643 # We need to prevent a replay attack using an earlier signed tag.
644 # We also want to archive in the history the object ids of
645 # anything we remove, even if we get rid of the actual objects.
647 # So, we check that the signed tag mentions the name and tag
650 # (a) In the case of FRESHREPO: all tags and refs/heads/* in
651 # the repo. That is, effectively, all the things we are
654 # This prevents any tag implying a FRESHREPO push
655 # being replayed into a different state of the repo.
657 # There is still the folowing risk: If a non-ff push is of a
658 # head which is an ancestor of a previous ff-only push, the
659 # previous push can be replayed.
661 # So we keep a separate list, as a file in the repo, of all
662 # the tag object ids we have ever seen and removed. Any such
663 # tag object id will be rejected even for ff-only pushes.
665 # (b) In the case of just NOFFCHECK: all tags referring to the
666 # current head for the suite (there must be at least one).
668 # This prevents any tag implying a NOFFCHECK push being
669 # replayed to rewind from a different head.
671 # The possibility of an earlier ff-only push being replayed is
672 # eliminated as follows: the tag from such a push would still
673 # be in our repo, and therefore the replayed push would be
674 # rejected because the set of refs being updated would be
677 if (!open PREVIOUS, "<", removedtagsfile) {
678 die removedtagsfile." $!" unless $!==ENOENT;
680 # Protocol for updating this file is to append to it, not
681 # write-new-and-rename. So all updates are prefixed with \n
682 # and suffixed with " .\n" so that partial writes can be
685 next unless m/^(\w+) (.*) \.\n/;
686 next unless $1 eq $tagval;
687 reject "Replay of previously-rewound upload ($tagval $2)";
689 die removedtagsfile." $!" if PREVIOUS->error;
693 return unless $policy & (FRESHREPO|NOFFCHECK);
695 my $garbagerepo = "$dgitrepos/${package}_garbage";
701 my $check_ref_superseded= sub {
702 my ($objid,$objtype,$fullrefname,$reftail) = @_;
703 my $supkey = $fullrefname;
704 $supkey =~ s{^refs/}{} or die "$supkey $objid ?";
705 my $supobjid = $supersedes{$supkey};
706 if (!defined $supobjid) {
707 printdebug "checktagnoreply - missing\n";
708 push @problems, "does not supersede $supkey";
709 } elsif ($supobjid ne $objid) {
710 push @problems, "supersedes $supkey=$supobjid".
711 " but previously $supkey=$objid";
717 if ($policy & FRESHREPO) {
718 foreach my $kind (qw(tags heads)) {
719 git_for_each_ref("refs/$kind", $check_ref_superseded);
722 my $branch= server_branch($suite);
723 my $branchhead= git_get_ref($branch);
724 if (!length $branchhead) {
725 # No such branch - NOFFCHECK was unnecessary. Oh well.
726 printdebug "checktagnoreplay - not FRESHREPO, new branch, ok\n";
728 printdebug "checktagnoreplay - not FRESHREPO,".
729 " checking for overwriting refs/$branch=$branchhead\n";
730 git_for_each_tag_referring($branchhead, sub {
731 my ($tagobjid,$refobjid,$fullrefname,$tagname) = @_;
732 $check_ref_superseded->($tagobjid,undef,$fullrefname,undef);
734 printdebug "checktagnoreply - not FRESHREPO, nchecked=$nchecked";
735 push @problems, "does not supersede any tag".
736 " referring to branch head $branch=$branchhead"
742 reject "replay attack prevention check failed:".
743 " signed tag for $version: ".
744 join("; ", @problems).
747 printdebug "checktagnoreply - all ok\n"
752 my $vals = $tagh{$tag};
753 reject "missing header $tag in signed tag object" unless $vals;
754 reject "multiple headers $tag in signed tag object" unless @$vals == 1;
759 printdebug "checks\n";
761 tagh1('type') eq 'commit' or reject "tag refers to wrong kind of object";
762 tagh1('object') eq $commit or reject "tag refers to wrong commit";
763 tagh1('tag') eq $tagname or reject "tag name in tag is wrong";
768 printdebug "translated version $v\n";
769 $tagname eq "debian/$v" or die;
773 my @policy_args = ($package,$version,$suite,$tagname,
774 join(",",@deliberatelies));
775 $policy = policyhook(NOFFCHECK|FRESHREPO, 'push', @policy_args);
780 # check that our ref is being fast-forwarded
781 printdebug "oldcommit $oldcommit\n";
782 if (!($policy & NOFFCHECK) && $oldcommit =~ m/[^0]/) {
783 $?=0; $!=0; my $mb = `git merge-base $commit $oldcommit`;
785 $mb eq $oldcommit or reject "not fast forward on dgit branch";
788 if ($policy & FRESHREPO) {
789 # This is troublesome. We have been asked by the policy hook
790 # to receive the push into a fresh repo. But of course we
791 # have actually already mostly received the push into the working
792 # repo. (This is unavoidable because the instruction to use a new
793 # repo comes ultimately from the signed tag for the dgit push,
794 # which has to have been received into some repo.)
796 # So what we do is generate a fresh working repo right now and
797 # push the head and tag into it. The presence of this fresh
798 # working repo is detected by the parent, which responds by
799 # making a fresh master repo from the template.
801 $destrepo = "${workrepo}_fresh"; # workrepo lock covers
802 mkrepo_fromtemplate $destrepo;
805 my $willinstall = ($destrepo eq realdestrepo ? '' : $destrepo);
806 policyhook(0, 'push-confirm', @policy_args, $willinstall);
810 my @cmd = (qw(git send-pack), $destrepo);
811 push @cmd, qw(--force) if $policy & NOFFCHECK;
812 push @cmd, "$commit:refs/dgit/$suite",
813 "$tagval:refs/tags/$tagname";
817 !$r or die "onward push to $destrepo failed: $r $!";
821 printdebug "stunthook in $workrepo\n";
822 chdir $workrepo or die "chdir $workrepo: $!";
823 mkdir "dgit-tmp" or $!==EEXIST or die $!;
829 printdebug "stunthook done.\n";
832 #----- git-upload-pack -----
834 sub fixmissing__git_upload_pack () {
835 $destrepo = "$dgitrepos/_empty";
836 my $lfh = locksometree($destrepo);
837 return if stat_exists $destrepo;
838 rmtree "$destrepo.new";
839 mkemptyrepo "$destrepo.new", "0644";
840 rename "$destrepo.new", $destrepo or die $!;
841 unlink "$destrepo.lock" or die $!;
845 sub main__git_upload_pack () {
846 my $lfh = locksometree($destrepo);
847 printdebug "git-upload-pack in $destrepo\n";
848 chdir $destrepo or die "$destrepo: $!";
850 runcmd qw(git upload-pack), ".";
853 #----- arg parsing and main program -----
863 # keys are used for DGIT_DRS_XXX too
864 'repos' => \$dgitrepos,
865 'suites' => \$suitesfile,
866 'policy-hook' => \$policyhook,
867 'dgit-live' => \$dgitlive,
870 our @hookenvs = qw(distro suitesfile policyhook
871 dgitlive keyrings dgitrepos distrodir);
873 # workrepo and destrepo handled ad-hoc
878 my $cmd = $ENV{'SSH_ORIGINAL_COMMAND'};
888 or reject "command string not understood";
894 my $mainfunc = $main::{"main__$funcn"};
896 reject "unknown method" unless $mainfunc;
898 policy_checkpackage();
900 if (stat_exists realdestrepo) {
901 $destrepo = realdestrepo;
903 printdebug " fixmissing $funcn\n";
904 my $fixfunc = $main::{"fixmissing__$funcn"};
908 printdebug " running main $funcn\n";
915 my $listfh = tempfile();
916 open STDOUT, ">&", $listfh or die $!;
917 policyhook(0,'check-list');
918 open STDOUT, ">&STDERR" or die $!;
920 seek $listfh, 0, 0 or die $!;
925 die unless m/^($package_re)$/;
928 policy_checkpackage();
930 die $! if $listfh->error;
933 sub parseargsdispatch () {
936 delete $ENV{'GIT_DIR'}; # if not run via ssh, our parent git process
937 delete $ENV{'GIT_PREFIX'}; # sets these and they mess things up
939 if ($ENV{'DGIT_DRS_DEBUG'}) {
943 if ($ARGV[0] eq '--pre-receive-hook') {
946 printdebug "in stunthook ".(shellquote @ARGV)."\n";
947 foreach my $k (sort keys %ENV) {
948 printdebug "$k=$ENV{$k}\n" if $k =~ m/^DGIT/;
953 $package = shift @ARGV;
954 ${ $main::{$_} } = $ENV{"DGIT_DRS_\U$_"} foreach @hookenvs;
955 defined($workrepo = $ENV{'DGIT_DRS_WORK'}) or die;
956 defined($destrepo = $ENV{'DGIT_DRS_DEST'}) or die;
957 open STDOUT, ">&STDERR" or die $!;
962 recorderror "$@" or die;
969 $distrodir = argval();
970 $keyrings = argval();
972 foreach my $dk (keys %indistrodir) {
973 ${ $indistrodir{$dk} } = "$distrodir/$dk";
976 while (@ARGV && $ARGV[0] =~ m/^--([-0-9a-z]+)=/ && $indistrodir{$1}) {
977 ${ $indistrodir{$1} } = $'; #';
981 $ENV{"DGIT_DRS_\U$_"} = ${ $main::{$_} } foreach @hookenvs;
985 my $mode = shift @ARGV;
986 die unless $mode =~ m/^--(\w+)$/;
987 my $fn = ${*::}{"mode_$1"};
993 while (my $fh = pop @lockfhs) { close $fh; }
998 if (!chdir "$dgitrepos/_tmp") {
999 $!==ENOENT or die $!;
1002 foreach my $lf (<*.lock>) {
1004 $tree =~ s/\.lock$//;
1005 next unless acquirermtree($tree, 0);
1006 remove $lf or warn $!;
1011 parseargsdispatch();