1 | #!/usr/bin/perl |
---|
2 | # |
---|
3 | # gitproxy: Wrapper around git daemon for Git virtual hosting. |
---|
4 | # version 1.1, released 2008-12-28 |
---|
5 | # Copyright © 2008 Anders Kaseorg <andersk@mit.edu> |
---|
6 | # |
---|
7 | # This program is free software; you can redistribute it and/or |
---|
8 | # modify it under the terms of the GNU General Public License |
---|
9 | # as published by the Free Software Foundation; either version 2 |
---|
10 | # of the License, or (at your option) any later version. |
---|
11 | # |
---|
12 | # This program is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with this program; if not, write to the Free Software |
---|
19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | |
---|
21 | use strict; |
---|
22 | use warnings; |
---|
23 | use IPC::Open2; |
---|
24 | use Errno qw(EINTR); |
---|
25 | use IO::Poll qw(POLLIN POLLOUT POLLHUP); |
---|
26 | |
---|
27 | # Receive the first message from the client, and parse out the URL. |
---|
28 | my $host; |
---|
29 | my $msg = ''; |
---|
30 | for (;;) { |
---|
31 | my $n = sysread(STDIN, my $buf, 4096); |
---|
32 | next if $n < 0 and $! == EINTR; |
---|
33 | $n >= 0 or die "$0: read: $!"; |
---|
34 | $n > 0 or die "$0: unexpected message from client"; |
---|
35 | $msg .= $buf; |
---|
36 | my $len; |
---|
37 | if (($len) = $msg =~ m/^([[:xdigit:]]{4})/ and length($msg) >= hex($len)) { |
---|
38 | foreach (split("\0", $')) { |
---|
39 | last if ($host) = m/^host=(.*)$/; |
---|
40 | } |
---|
41 | last if defined($host); |
---|
42 | die "$0: no host found in client message"; |
---|
43 | } elsif ($msg !~ m/^[[:xdigit:]]{0,3}$/) { |
---|
44 | die "$0: unexpected message from client"; |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | # Now start the real git daemon based on the URL. |
---|
49 | my $pid = open2(\*IN, \*OUT, '/usr/local/sbin/ldapize.pl', "git://$host/") or die "$0: open: $!"; |
---|
50 | |
---|
51 | # Finally, go into a poll loop to transfer the remaining data |
---|
52 | # (STDIN -> OUT, IN -> STDOUT), including the client's message to git daemon. |
---|
53 | my ($cbuf, $sbuf) = ($msg, ''); |
---|
54 | my $poll = new IO::Poll; |
---|
55 | $poll->mask(\*STDOUT => POLLHUP); |
---|
56 | $poll->mask(\*OUT => POLLOUT); |
---|
57 | $poll->remove(\*STDIN); |
---|
58 | $poll->mask(\*IN => POLLIN); |
---|
59 | while ($poll->handles()) { |
---|
60 | my $n = $poll->poll(); |
---|
61 | next if $n < 0 and $! == EINTR; |
---|
62 | $n >= 0 or die "select: $!"; |
---|
63 | if ($poll->events(\*STDIN)) { |
---|
64 | my $n = sysread(STDIN, $cbuf, 4096); |
---|
65 | next if $n < 0 and $! == EINTR; |
---|
66 | $n >= 0 or die "read: $!"; |
---|
67 | $poll->remove(\*STDIN); |
---|
68 | $poll->mask(\*OUT => POLLOUT); |
---|
69 | } elsif ($poll->events(\*IN)) { |
---|
70 | my $n = sysread(IN, $sbuf, 4096); |
---|
71 | next if $n < 0 and $! == EINTR; |
---|
72 | $n >= 0 or die "read: $!"; |
---|
73 | $poll->remove(\*IN); |
---|
74 | $poll->mask(\*STDOUT => POLLOUT); |
---|
75 | } elsif ($poll->events(\*STDOUT) & POLLOUT && $sbuf ne '') { |
---|
76 | my $n = syswrite(STDOUT, $sbuf); |
---|
77 | next if $n < 0 and $! == EINTR; |
---|
78 | $n >= 0 or die "write: $!"; |
---|
79 | $sbuf = substr($sbuf, $n); |
---|
80 | if ($sbuf eq '') { |
---|
81 | $poll->mask(\*STDOUT => POLLHUP); |
---|
82 | $poll->mask(\*IN => POLLIN); |
---|
83 | } |
---|
84 | } elsif ($poll->events(\*STDOUT)) { |
---|
85 | $poll->remove(\*STDOUT); |
---|
86 | $poll->remove(\*IN); |
---|
87 | close(STDOUT) or die "close: $!"; |
---|
88 | close(IN) or die "close: $!"; |
---|
89 | } elsif ($poll->events(\*OUT) & POLLOUT && $cbuf ne '') { |
---|
90 | my $n = syswrite(OUT, $cbuf); |
---|
91 | next if $n < 0 and $! == EINTR; |
---|
92 | $n >= 0 or die "write: $!"; |
---|
93 | $cbuf = substr($cbuf, $n); |
---|
94 | if ($cbuf eq '') { |
---|
95 | $poll->mask(\*OUT => POLLHUP); |
---|
96 | $poll->mask(\*STDIN => POLLIN); |
---|
97 | } |
---|
98 | } elsif ($poll->events(\*OUT)) { |
---|
99 | $poll->remove(\*OUT); |
---|
100 | $poll->remove(\*STDIN); |
---|
101 | close(OUT) or die "close: $!"; |
---|
102 | close(STDIN) or die "close: $!"; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | while (waitpid($pid, 0) == -1 && $! == EINTR) { } |
---|