2 # wrapper to dispatch git ssh service requests
4 # Copyright (C) 2015-2016 Ian Jackson
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 open DEBUG, '>/dev/null' or die $!;
27 if (@ARGV && $ARGV[0] eq '-D') {
29 open DEBUG, '>&STDERR' or die $!;
32 die unless @ARGV>=1 && @ARGV<=2 && $ARGV[0] !~ m/^-/;
33 our ($dispatchdir,$authrune) = @ARGV;
35 $authrune //= join ':',
36 '@/keyrings/debian-keyring.gpg,a',
37 '@/keyrings/debian-maintainers.gpg,m@/dm.txt';
39 our $lre = $package_re;
40 our $qre = '["'."']?";
42 # $dispatchdir/distro=DISTRO should contain
43 # dgit-live a clone of dgit (only if not using installed vsns)
45 # repos/ } by virtue of
46 # suites } dgit-repos-server's defaults relating to
47 # policy-hook } dispatch-dir
48 # plus files required by the authrune (by default, keyrings/ and dm.txt)
50 # diverts should be list of
52 # where <pat> is a package name pattern which may contain * or literals.
53 # <divert-to> is for `git config dgit-distro.DISTRO.diverts.<divert-to>'
55 our ($distro,$pkg, $d);
56 our ($dgitlive,$repos,$suites,$diverts,$policyhook,$repo);
60 if (!open DIV, '<', $df) {
69 if (s/\s+(\S+)$//) { $divert=$1; }
70 s/[^-+._0-9a-zA-Z*]/\\$&/g;
72 printf DEBUG 'DISPATCH DIVERT ^%s$ %s'."\n",
73 $_, ($divert // '(undef)');
74 if ($pkg =~ m/^$_$/) { return $divert; }
76 DIV->error and die $!;
83 close STDOUT or die $!;
88 print @_, "\n" or die $!;
91 sub selectpackage ($$;$) {
93 ($distro,$pkg, $divertfn) = @_; # $distro,$pkg must have sane syntax
95 $d = "$dispatchdir/distro=$distro";
98 die $! unless $!==ENOENT;
99 die "unknown distro ($distro)\n";
102 $dgitlive= "$d/dgit-live";
104 $suites= "$d/suites";
105 $policyhook= "$d/policy-hook";
107 $authrune =~ s/\@/$d/g;
109 my $divert = checkdivert("$d/diverts");
110 if (defined $divert) {
112 die "diverted to $divert incompletely or too late!\n";
114 $divertfn->($divert);
118 $repo = "$repos/$pkg.git";
120 print DEBUG "DISPATCH DISTRO $distro PKG $pkg\n";
128 $!==ENOENT or die $!;
135 exec qw(git upload-pack --strict --timeout=1000), $repo;
140 local ($_) = $ENV{'SSH_ORIGINAL_COMMAND'} // '';
142 if (m#^: dgit ($lre) git-check ($lre) ;#) {
143 selectpackage $1,$2, sub { prl "divert @_"; finish; };
147 m#^${qre}git-([-a-z]+) ${qre}/dgit/($lre)/repos/($lre)\.git${qre}$#
151 if ($cmd eq 'receive-pack') {
152 $ENV{'PERLLIB'} //= '';
153 $ENV{'PERLLIB'} =~ s#^(?=.)#:#;
154 $ENV{'PERLLIB'} =~ s#^# $ENV{DGIT_TEST_INTREE} // $dgitlive #e;
155 my $s = "$dgitlive/infra/dgit-repos-server";
156 $s = "dgit-repos-server" if !stat_exists $s;
157 exec $s, $distro, $d, $authrune, qw(--ssh);
159 } elsif ($cmd eq 'upload-pack') {
160 $repo='$repos/_empty' unless hasrepo;
163 die "unsupported git operation $cmd ($_)";
166 m#^${qre}git-upload-pack ${qre}/dgit/($lre)/(?:repos/)?_dgit-repos-server\.git${qre}$#
169 # if running installed packages, source code should come
171 serve_up("$dispatchdir/distro=$1/dgit-live/.git");
172 } elsif (m#^${qre}git-upload-pack\s#) {
173 die "unknown repo to serve ($_). use dgit, or for server source ".
174 "git clone here:/dgit/DISTRO/repos/_dgit-repos-server.git";
176 die "unsupported operation ($_)";