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 | } | |
23 | { | |
24 | package cf; | |
25 | our ($client,$client_addr,$client_port) = ::ntoa(getpeername(STDIN)); | |
26 | our ($server,$server_addr,$server_port) = ::ntoa(getsockname(STDIN)); | |
fbde0914 | 27 | } |
6fe98f4a | 28 | |
fbde0914 TF |
29 | openlog 'userv-git-daemon', 'pid', 'daemon'; |
30 | ||
31 | sub fail { | |
18c4999f | 32 | syslog 'err', "$cf::client @_"; |
fbde0914 TF |
33 | exit; |
34 | } | |
d2707bea TF |
35 | |
36 | sub xread { | |
37 | my $length = shift; | |
38 | my $buffer = ""; | |
f14a8627 | 39 | local $SIG{ALRM} = sub { fail "timeout" }; |
fbde0914 | 40 | alarm 30; |
d2707bea | 41 | while ($length > length $buffer) { |
abb80356 TF |
42 | my $ret = sysread STDIN, $buffer, $length, length $buffer; |
43 | fail "short read: expected $length bytes, got " . length $buffer | |
44 | if defined $ret and $ret == 0; | |
45 | fail "read: $!" if not defined $ret and $! != EINTR and $! != EAGAIN; | |
d2707bea | 46 | } |
fbde0914 | 47 | alarm 0; |
d2707bea TF |
48 | return $buffer; |
49 | } | |
50 | ||
51 | my $len_hex = xread 4; | |
6fe98f4a | 52 | fail "non-hex packet length" unless $len_hex =~ m{^[0-9a-fA-F]{4}$}; |
f14a8627 | 53 | my $line = xread hex $len_hex; |
18c4999f TF |
54 | if ($line =~ m{^(git-[a-z-]+) ([!-~]+)\0host=([!-~]+)\0$}) { |
55 | package cf; | |
56 | our ($service,$path,$host) = ($1,$2,$3); | |
57 | $path =~ s|^/*||; | |
58 | our $uri = $_ = "git://$host/$path"; | |
59 | } else { | |
60 | $line =~ s|[^ -~]+| |g; | |
fbde0914 TF |
61 | fail "could not parse \"$line\"" |
62 | } | |
6fe98f4a | 63 | |
08e30b5e | 64 | for my $cf (@ARGV) { |
18c4999f TF |
65 | package cf; |
66 | our ($user,$dir,$repo); | |
67 | do $cf; | |
08e30b5e | 68 | } |
18c4999f TF |
69 | fail "no user configured for $cf::uri" unless defined $cf::user; |
70 | syslog 'info', "$cf::client $cf::service $cf::uri"; | |
fbde0914 | 71 | |
18c4999f TF |
72 | my @opts = map "-D$_=${$cf::{$_}}", grep defined ${$cf::{$_}}, sort keys %cf::; |
73 | my @cmd = ('userv', @opts, $cf::user, $cf::service); | |
11b88dbb | 74 | no warnings; # suppress errors to stderr |
18c4999f | 75 | exec @cmd or fail "exec @cmd: $!"; |
fbde0914 TF |
76 | |
77 | # end |