3 # A git daemon with an added userv security boundary.
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/
18 return ('(local)') unless defined $sockaddr;
19 my ($port,$addr) = sockaddr_in $sockaddr;
20 $addr = inet_ntoa $addr;
21 return ("[$addr]:$port",$addr,$port);
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);
27 openlog 'userv-git-daemon', 'pid', 'daemon';
28 sub fail { syslog 'err', "$client @_"; exit }
30 $SIG{ALRM} = sub { fail "timeout" };
34 my $length = shift; $_ = "";
35 while ($length > length) {
36 my $ret = sysread STDIN, $_, $length, length;
37 fail "Expected $length bytes, got ".length
38 if defined $ret and $ret == 0;
39 fail "read: $!" if not defined $ret and $! != EINTR and $! != EAGAIN;
43 fail "Bad hex in packet length" unless m|^[0-9a-fA-F]{4}$|;
45 unless (($service,$path,$host) =
46 m|^(git-[a-z-]+) /*([!-~]+)\0host=([!-~]+)\0$|) {
48 fail "Could not parse \"$_\""
50 our $uri = $_ = "git://$host/$path";
51 for my $cf (@ARGV) { do $cf }
53 fail "No user for $uri" unless defined $user;
54 syslog 'notice', "$client $service $uri";
56 my @opts = map "-D$_=${$::{$_}}",
57 grep defined ${$::{$_}} && /^[a-z_]+$/, keys %::;
59 my @cmd = ('userv', @opts, $user, $service);
60 no warnings; # suppress errors to stderr
61 exec @cmd or fail "exec userv: $!";