Commit | Line | Data |
---|---|---|
d2707bea TF |
1 | #!/usr/bin/perl |
2 | # | |
fbde0914 | 3 | # A git daemon with an added userv security boundary. |
d2707bea | 4 | # |
d2707bea TF |
5 | # This was written by Tony Finch <dot@dotat.at> |
6 | # You may do anything with it, at your own risk. | |
7 | # http://creativecommons.org/publicdomain/zero/1.0/ | |
8 | ||
9 | use strict; | |
10 | use warnings; | |
11 | ||
12 | use POSIX; | |
fbde0914 TF |
13 | use Socket; |
14 | use Sys::Syslog; | |
d2707bea | 15 | |
6fe98f4a TF |
16 | sub ntoa { |
17 | my $sockaddr = shift; | |
18c4999f TF |
18 | return ('[?.?.?.?]:?') unless defined $sockaddr; |
19 | my ($port,$addr) = sockaddr_in $sockaddr; | |
20 | $addr = inet_ntoa $addr; | |
21 | return ("[$addr]:$port",$addr,$port); | |
22 | } | |
3b32ef57 TF |
23 | our ($client,$client_addr,$client_port) = ::ntoa(getpeername(STDIN)); |
24 | our ($server,$server_addr,$server_port) = ::ntoa(getsockname(STDIN)); | |
25 | our ($service,$path,$host,$uri,$user); | |
6fe98f4a | 26 | |
fbde0914 | 27 | openlog 'userv-git-daemon', 'pid', 'daemon'; |
3b32ef57 | 28 | sub fail { syslog 'err', "$client @_"; exit } |
d2707bea TF |
29 | |
30 | sub xread { | |
31 | my $length = shift; | |
32 | my $buffer = ""; | |
f14a8627 | 33 | local $SIG{ALRM} = sub { fail "timeout" }; |
fbde0914 | 34 | alarm 30; |
d2707bea | 35 | while ($length > length $buffer) { |
abb80356 TF |
36 | my $ret = sysread STDIN, $buffer, $length, length $buffer; |
37 | fail "short read: expected $length bytes, got " . length $buffer | |
38 | if defined $ret and $ret == 0; | |
39 | fail "read: $!" if not defined $ret and $! != EINTR and $! != EAGAIN; | |
d2707bea | 40 | } |
fbde0914 | 41 | alarm 0; |
d2707bea TF |
42 | return $buffer; |
43 | } | |
44 | ||
45 | my $len_hex = xread 4; | |
6fe98f4a | 46 | fail "non-hex packet length" unless $len_hex =~ m{^[0-9a-fA-F]{4}$}; |
f14a8627 | 47 | my $line = xread hex $len_hex; |
3b32ef57 | 48 | if ($line !~ m{^(git-[a-z-]+) /*([!-~]+)\0host=([!-~]+)\0$}) { |
18c4999f | 49 | $line =~ s|[^ -~]+| |g; |
fbde0914 TF |
50 | fail "could not parse \"$line\"" |
51 | } | |
3b32ef57 TF |
52 | ($service,$path,$host) = ($1,$2,$3); |
53 | $_ = $uri = "git://$host/$path"; | |
54 | for my $cf (@ARGV) { do $cf } | |
6fe98f4a | 55 | |
3b32ef57 TF |
56 | fail "no user configured for $uri" unless defined $user; |
57 | syslog 'info', "$client $service $uri"; | |
58 | ||
59 | my @opts = map "-D$_=${$main::{$_}}", | |
60 | grep defined ${$main::{$_}} && /^[a-z]+$/, | |
61 | sort keys %main::; | |
fbde0914 | 62 | |
3b32ef57 | 63 | my @cmd = ('userv', @opts, $user, $service); |
11b88dbb | 64 | no warnings; # suppress errors to stderr |
18c4999f | 65 | exec @cmd or fail "exec @cmd: $!"; |
fbde0914 TF |
66 | |
67 | # end |