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/>.
21 use Debian::Dgit::Infra; # must precede Debian::Dgit; - can change @INC!
27 open DEBUG, '>/dev/null' or die $!;
28 if (@ARGV && $ARGV[0] eq '-D') {
30 open DEBUG, '>&STDERR' or die $!;
33 die unless @ARGV>=1 && @ARGV<=2 && $ARGV[0] !~ m/^-/;
34 our ($dispatchdir,$authrune) = @ARGV;
36 $authrune //= join ':',
37 '@/keyrings/debian-keyring.gpg,a',
38 '@/keyrings/debian-maintainers.gpg,m@/dm.txt';
40 our $lre = $package_re;
41 our $qre = '["'."']?";
43 # $dispatchdir/distro=DISTRO should contain
44 # dgit-live a clone of dgit (only if not using installed vsns)
46 # repos/ } by virtue of
47 # suites } dgit-repos-server's defaults relating to
48 # policy-hook } dispatch-dir
49 # plus files required by the authrune (by default, keyrings/ and dm.txt)
51 # diverts should be list of
53 # where <pat> is a package name pattern which may contain * or literals.
54 # <divert-to> is for `git config dgit-distro.DISTRO.diverts.<divert-to>'
56 our ($distro,$pkg, $d);
57 our ($dgitlive,$repos,$suites,$diverts,$policyhook,$repo);
61 if (!open DIV, '<', $df) {
70 if (s/\s+(\S+)$//) { $divert=$1; }
71 s/[^-+._0-9a-zA-Z*]/\\$&/g;
73 printf DEBUG 'DISPATCH DIVERT ^%s$ %s'."\n",
74 $_, ($divert // '(undef)');
75 if ($pkg =~ m/^$_$/) { return $divert; }
77 DIV->error and die $!;
84 close STDOUT or die $!;
89 print @_, "\n" or die $!;
92 sub selectpackage ($$;$) {
94 ($distro,$pkg, $divertfn) = @_; # $distro,$pkg must have sane syntax
96 $d = "$dispatchdir/distro=$distro";
99 die $! unless $!==ENOENT;
100 die "unknown distro ($distro)\n";
103 $dgitlive= "$d/dgit-live";
105 $suites= "$d/suites";
106 $policyhook= "$d/policy-hook";
108 $authrune =~ s/\@/$d/g;
110 my $divert = checkdivert("$d/diverts");
111 if (defined $divert) {
113 die "diverted to $divert incompletely or too late!\n";
115 $divertfn->($divert);
119 $repo = "$repos/$pkg.git";
121 print DEBUG "DISPATCH DISTRO $distro PKG $pkg\n";
129 $!==ENOENT or die $!;
136 exec qw(git upload-pack --strict --timeout=1000), $repo;
141 local ($_) = $ENV{'SSH_ORIGINAL_COMMAND'} // '';
143 if (m#^: dgit ($lre) git-check ($lre) ;#) {
144 selectpackage $1,$2, sub { prl "divert @_"; finish; };
148 m#^${qre}git-([-a-z]+) ${qre}/dgit/($lre)/repos/($lre)\.git${qre}$#
152 if ($cmd eq 'receive-pack') {
153 $ENV{'PERLLIB'} //= '';
154 $ENV{'PERLLIB'} =~ s#^(?=.)#:#;
155 $ENV{'PERLLIB'} =~ s#^# $ENV{DGIT_TEST_INTREE} // $dgitlive #e;
156 my $s = "$dgitlive/infra/dgit-repos-server";
157 $s = "dgit-repos-server" if !stat_exists $s;
158 exec $s, $distro, $d, $authrune, qw(--ssh);
160 } elsif ($cmd eq 'upload-pack') {
161 $repo='$repos/_empty' unless hasrepo;
164 die "unsupported git operation $cmd ($_)";
167 m#^${qre}git-upload-pack ${qre}/dgit/($lre)/(?:repos/)?_dgit-repos-server\.git${qre}$#
170 # if running installed packages, source code should come
172 serve_up("$dispatchdir/distro=$1/dgit-live/.git");
173 } elsif (m#^${qre}git-upload-pack\s#) {
174 die "unknown repo to serve ($_). use dgit, or for server source ".
175 "git clone here:/dgit/DISTRO/repos/_dgit-repos-server.git";
177 die "unsupported operation ($_)";