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; | |
9ad7f5ed | 18 | return ('(local)') unless defined $sockaddr; |
18c4999f TF |
19 | my ($port,$addr) = sockaddr_in $sockaddr; |
20 | $addr = inet_ntoa $addr; | |
21 | return ("[$addr]:$port",$addr,$port); | |
22 | } | |
9ad7f5ed 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,$user); | |
6fe98f4a | 26 | |
fbde0914 | 27 | openlog 'userv-git-daemon', 'pid', 'daemon'; |
3b32ef57 | 28 | sub fail { syslog 'err', "$client @_"; exit } |
d2707bea | 29 | |
9ad7f5ed TF |
30 | $SIG{ALRM} = sub { fail "timeout" }; |
31 | alarm 30; | |
32 | ||
d2707bea | 33 | sub xread { |
9ad7f5ed TF |
34 | my $length = shift; $_ = ""; |
35 | while ($length > length) { | |
36 | my $ret = sysread STDIN, $_, $length, length; | |
37 | fail "Expected $length bytes, got ".length | |
abb80356 TF |
38 | if defined $ret and $ret == 0; |
39 | fail "read: $!" if not defined $ret and $! != EINTR and $! != EAGAIN; | |
d2707bea | 40 | } |
d2707bea | 41 | } |
9ad7f5ed TF |
42 | xread 4; |
43 | fail "Bad hex in packet length" unless m|^[0-9a-fA-F]{4}$|; | |
44 | xread hex; | |
45 | unless (($service,$path,$host) = | |
46 | m|^(git-[a-z-]+) /*([!-~]+)\0host=([!-~]+)\0$|) { | |
47 | s|[^ -~]+| |g; | |
48 | fail "Could not parse \"$_\"" | |
fbde0914 | 49 | } |
9ad7f5ed | 50 | our $uri = $_ = "git://$host/$path"; |
3b32ef57 | 51 | for my $cf (@ARGV) { do $cf } |
6fe98f4a | 52 | |
9ad7f5ed TF |
53 | fail "No user for $uri" unless defined $user; |
54 | syslog 'notice', "$client $service $uri"; | |
3b32ef57 | 55 | |
9ad7f5ed TF |
56 | my @opts = map "-D$_=${$::{$_}}", |
57 | grep defined ${$::{$_}} && /^[a-z_]+$/, keys %::; | |
fbde0914 | 58 | |
3b32ef57 | 59 | my @cmd = ('userv', @opts, $user, $service); |
11b88dbb | 60 | no warnings; # suppress errors to stderr |
9ad7f5ed | 61 | exec @cmd or fail "exec userv: $!"; |
fbde0914 TF |
62 | |
63 | # end |