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 { |
5f3e811b TF |
34 | my $length = shift; |
35 | my $buffer = ""; | |
36 | while ($length > length $buffer) { | |
37 | my $ret = sysread STDIN, $buffer, $length, length $buffer; | |
38 | fail "Expected $length bytes, got ".length $buffer | |
abb80356 TF |
39 | if defined $ret and $ret == 0; |
40 | fail "read: $!" if not defined $ret and $! != EINTR and $! != EAGAIN; | |
d2707bea | 41 | } |
5f3e811b | 42 | return $buffer; |
d2707bea | 43 | } |
5f3e811b TF |
44 | my $hex_len = xread 4; |
45 | fail "Bad hex in packet length" unless $hex_len =~ m|^[0-9a-fA-F]{4}$|; | |
46 | my $line = xread -4 + hex $hex_len; | |
47 | unless (($service,$path,$host) = $line =~ | |
9ad7f5ed | 48 | m|^(git-[a-z-]+) /*([!-~]+)\0host=([!-~]+)\0$|) { |
5f3e811b TF |
49 | $line =~ s|[^ -~]+| |g; |
50 | fail "Could not parse \"$line\"" | |
fbde0914 | 51 | } |
9ad7f5ed | 52 | our $uri = $_ = "git://$host/$path"; |
3b32ef57 | 53 | for my $cf (@ARGV) { do $cf } |
6fe98f4a | 54 | |
9ad7f5ed TF |
55 | fail "No user for $uri" unless defined $user; |
56 | syslog 'notice', "$client $service $uri"; | |
3b32ef57 | 57 | |
9ad7f5ed TF |
58 | my @opts = map "-D$_=${$::{$_}}", |
59 | grep defined ${$::{$_}} && /^[a-z_]+$/, keys %::; | |
fbde0914 | 60 | |
3b32ef57 | 61 | my @cmd = ('userv', @opts, $user, $service); |
11b88dbb | 62 | no warnings; # suppress errors to stderr |
9ad7f5ed | 63 | exec @cmd or fail "exec userv: $!"; |
fbde0914 TF |
64 | |
65 | # end |