#!/usr/bin/perl
#
-# A very simple userv git-daemon wrapper.
-#
-# This reads the first packet-line of the protocol, checks the syntax
-# of the user, pathname, and hostname, then uses userv to invoke the
-# real git daemon as the target user with safe arguments.
+# A git daemon with an added userv security boundary.
#
# This was written by Tony Finch <dot@dotat.at>
# You may do anything with it, at your own risk.
use warnings;
use POSIX;
+use Socket;
+use Sys::Syslog;
+
+sub ntoa {
+ my $sockaddr = shift;
+ return ('[?.?.?.?]:?') unless defined $sockaddr;
+ my ($port,$addr) = sockaddr_in $sockaddr;
+ $addr = inet_ntoa $addr;
+ return ("[$addr]:$port",$addr,$port);
+}
+our ($client,$client_addr,$client_port) = ::ntoa(getpeername(STDIN));
+our ($server,$server_addr,$server_port) = ::ntoa(getsockname(STDIN));
+our ($service,$path,$host,$uri,$user);
-my $USER = qr{[0-9a-z]+};
-my $PATH = qr{[-+,._/0-9A-Za-z]+};
-my $HOST = qr{[-.0-9A-Za-z]+};
+openlog 'userv-git-daemon', 'pid', 'daemon';
+sub fail { syslog 'err', "$client @_"; exit }
sub xread {
my $length = shift;
my $buffer = "";
- my $count = 0;
+ local $SIG{ALRM} = sub { fail "timeout" };
+ alarm 30;
while ($length > length $buffer) {
- my $data;
- my $ret = sysread STDIN, $data, $len
- while not defined $ret and ($! == EINTR or $! == EAGAIN);
- die "read" unless defined $ret;
- die "short read: expected $length bytes, got $count\n" if $ret == 0;
- $buffer .= $data;
- $count += $ret;
+ my $ret = sysread STDIN, $buffer, $length, length $buffer;
+ fail "short read: expected $length bytes, got " . length $buffer
+ if defined $ret and $ret == 0;
+ fail "read: $!" if not defined $ret and $! != EINTR and $! != EAGAIN;
}
+ alarm 0;
return $buffer;
}
my $len_hex = xread 4;
-die "bad packet length" unless $len_hex =~ m{^[0-9a-zA-Z]{4}$};
-my $len = hex $len;
-
-my $line = xread $len;
-$line =~ m{^git-upload-pack ~($USER)/($PATH[.]git)\0host=($HOST)\0$};
-my ($user,$path,$host) = ($1,$2,$3);
-
-# child's output will go directly to inetd
-open CHILD, '-|', 'userv', $user,
- qw(git daemon --inetd --strict-paths
- --user-path=public-git --forbid-override=receive-pack)
- or die "open pipe to userv: $!\n";
-
-# proxy command line to child
-syswrite CHILD, $len_hex.$line
- or die "write to userv: $!\n";
-
-# relay stdin to child
-open STDOUT, ">&CHILD"
- or die "dup: $!\n";
-exec 'cat'
- or die "exec: $!\n";
-
-die
+fail "non-hex packet length" unless $len_hex =~ m{^[0-9a-fA-F]{4}$};
+my $line = xread hex $len_hex;
+if ($line !~ m{^(git-[a-z-]+) /*([!-~]+)\0host=([!-~]+)\0$}) {
+ $line =~ s|[^ -~]+| |g;
+ fail "could not parse \"$line\""
+}
+($service,$path,$host) = ($1,$2,$3);
+$_ = $uri = "git://$host/$path";
+for my $cf (@ARGV) { do $cf }
+
+fail "no user configured for $uri" unless defined $user;
+syslog 'info', "$client $service $uri";
+
+my @opts = map "-D$_=${$main::{$_}}",
+ grep defined ${$main::{$_}} && /^[a-z]+$/,
+ sort keys %main::;
+
+my @cmd = ('userv', @opts, $user, $service);
+no warnings; # suppress errors to stderr
+exec @cmd or fail "exec @cmd: $!";
+
+# end