summaryrefslogtreecommitdiff
path: root/infra/dgit-ssh-dispatch
blob: 377f8b81d6786ba706ea73e75e0ac2aa6c3043d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/perl -w

use strict;
use POSIX;

use Debian::Dgit;

open DEBUG, '>/dev/null' or die $!;
if (@ARGV && $ARGV[0] eq '-D') {
    shift @ARGV;
    open DEBUG, '>&STDERR' or die $!;
}

die unless @ARGV>=1 && @ARGV<=2 && $ARGV[0] !~ m/^-/;
our ($dispatchdir,$authrune) = @ARGV;

$authrune //= join ':',
    '@/keyrings/debian-keyring.gpg,a',
    '@/keyrings/debian-maintainers.gpg,m@/dm.txt';

our $lre = $package_re;
our $qre = '["'."']?";

# $dispatchdir/distro=DISTRO should contain
#    dgit-live   a clone of dgit
#    diverts
#    repos/             }  by virtue of
#    suites             }    dgit-repos-server's defaults relating to
#    policy-hook        }    dispatch-dir
# plus files required by the authrune (by default, keyrings/ and dm.txt)
#
# diverts should be list of
#  <pat> [<divert-to>]
# where <pat> is a package name pattern which may contain * or literals.

our ($distro,$pkg, $d);
our ($dgitlive,$repos,$suites,$diverts,$policyhook,$repo);

sub checkdivert ($) {
    my ($df) = @_;
    if (!open DIV, '<', $df) {
	$!==ENOENT or die $!;
	return undef;
    } else {
	while (<DIV>) {
	    s/^\s+//; s/\s+$//;
	    next unless m/\S/;
	    next if m/^\#/;
	    my $divert;
	    if (s/\s+(\S+)$//) { $divert=$1; }
	    s/[^-+._0-9a-zA-Z*]/\\$&/g;
	    s/\*/.*/g;
	    printf DEBUG 'DISPATCH DIVERT ^%s$ %s'."\n",
	        $_, ($divert // '(undef)');
	    if ($pkg =~ m/^$_$/) { return $divert; }
	}
	DIV->error and die $!;
	close DIV;
	return undef;
    }
}
	
sub finish () {
    close STDOUT or die $!;
    exit 0;
}

sub prl ($) {
    print @_, "\n" or die $!;
}
	
sub selectpackage ($$;$) {
    my $divertfn;
    ($distro,$pkg, $divertfn) = @_; # $distro,$pkg must have sane syntax

    $d = "$dispatchdir/distro=$distro";

    if (!stat $d) {
	die $! unless $!==ENOENT;
	die "unknown distro ($distro)\n";
    }

    $dgitlive=    "$d/dgit-live";
    $repos=       "$d/repos";
    $suites=      "$d/suites";
    $policyhook=  "$d/policy-hook";

    $authrune =~ s/\@/$d/g;

    my $divert = checkdivert("$d/diverts");
    if (defined $divert) {
	$divertfn //= sub {
	    die "diverted to $divert incompletely or too late!\n";
	};
	$divertfn->($divert);
	die;
    }

    $repo = "$repos/$pkg.git";

    print DEBUG "DISPATCH DISTRO $distro PKG $pkg\n";
}

sub hasrepo () {
    if (stat $repo) {
	-d _ or die;
	return 1;
    } else {
	$!==ENOENT or die $!;
	return 0;
    }
}

sub serve_up ($) {
    my ($repo) = @_;
    exec qw(git upload-pack --strict --timeout=1000), $repo;
    die "exec git: $!";
}

sub dispatch () {
    local ($_) = $ENV{'SSH_ORIGINAL_COMMAND'};

    if (m#^: dgit ($lre) git-check ($lre) ;#) {
	selectpackage $1,$2, sub { prl "divert @_"; finish; };
	prl hasrepo;
	finish;
    } elsif (
	m#^${qre}git-([-a-z]+) ${qre}/dgit/($lre)/repos/($lre)\.git${qre}$#
	) {
	my $cmd=$1;
	selectpackage $2,$3;
	if ($cmd eq 'receive-pack') {
	    my $s = "$dgitlive/infra/dgit-repos-server";
	    exec $s, $distro, $d, $authrune, qw(--ssh);
	    die "exec $s: $!";
	} elsif ($cmd eq 'upload-pack') {
	    $repo='$repos/_empty' unless hasrepo;
	    serve_up $repo;
	} else {
	    die "unsupported git operation $cmd ($_)";
	}
    } elsif (
 m#^${qre}git-upload-pack ${qre}/dgit/($lre)/(?:repos/)?_dgit-repos-server\.git${qre}$#
	) {
	my $distro= $1;
	serve_up("$dispatchdir/distro=$1/dgit-live/.git");
    } elsif (m#^${qre}git-upload-pack\s#) {
	die "unknown repo to serve ($_).  use dgit, or for server source ".
	    "git clone here:/dgit/DISTRO/repos/_dgit-repos-server.git";
    } else {
	die "unsupported operation ($_)";
    }
}

dispatch;