| 1 | #!/usr/bin/perl |
|---|
| 2 | # |
|---|
| 3 | # svnproxy: Wrapper around svnserve for Subversion 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 | # Read the initial greeting from a dummy svnserve process. |
|---|
| 28 | my $pid = open(IN, '-|'); |
|---|
| 29 | defined $pid or die "$0: open: $!"; |
|---|
| 30 | if ($pid == 0) { |
|---|
| 31 | close(STDIN) or die "$0: close: $!"; |
|---|
| 32 | exec('svnserve', '-i') or die "$0: exec svnproxy: $!"; |
|---|
| 33 | } |
|---|
| 34 | my $greeting = ''; |
|---|
| 35 | for (;;) { |
|---|
| 36 | my $n = sysread(IN, my $buf, 4096); |
|---|
| 37 | next if $n < 0 and $! == EINTR; |
|---|
| 38 | $n >= 0 or die "$0: read: $!"; |
|---|
| 39 | last if $n == 0; |
|---|
| 40 | $greeting .= $buf; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | # Send the greeting to the client. |
|---|
| 44 | my $buf = $greeting; |
|---|
| 45 | while ($buf ne '') { |
|---|
| 46 | my $n = syswrite(STDOUT, $buf); |
|---|
| 47 | next if $n < 0 and $! == EINTR; |
|---|
| 48 | $n >= 0 or die "$0: write: $!"; |
|---|
| 49 | $buf = substr($buf, $n); |
|---|
| 50 | } |
|---|
| 51 | close(IN) or die "$0: close: $!"; |
|---|
| 52 | waitpid(-1, 0) or die "$0: waitpid: $!"; |
|---|
| 53 | |
|---|
| 54 | # Receive the response from the client, and parse out the URL. |
|---|
| 55 | my $url; |
|---|
| 56 | my $response = ''; |
|---|
| 57 | for (;;) { |
|---|
| 58 | my $n = sysread(STDIN, my $buf, 4096); |
|---|
| 59 | next if $n < 0 and $! == EINTR; |
|---|
| 60 | $n >= 0 or die "$0: read: $!"; |
|---|
| 61 | $n > 0 or die "$0: unexpected response from client"; |
|---|
| 62 | $response .= $buf; |
|---|
| 63 | my $url_len; |
|---|
| 64 | if (($url_len) = $response =~ m/^\(\s\S+\s\(\s[^)]*\)\s(\d+):/ and |
|---|
| 65 | length($') >= $url_len) { |
|---|
| 66 | $url = substr($', 0, $url_len); |
|---|
| 67 | last; |
|---|
| 68 | } elsif ($response !~ m/^(?:\((?:\s(?:\S+(?:\s(?:\((?:\s(?:[^)]*(?:\)(?:\s(?:\d+:?)?)?)?)?)?)?)?)?)?)?$/) { |
|---|
| 69 | die "$0: unexpected response from client"; |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | # Now start the real svnserve based on the URL. |
|---|
| 74 | $pid = open2(\*IN, \*OUT, '/usr/local/sbin/ldapize.pl', $url) or die "$0: open: $!"; |
|---|
| 75 | |
|---|
| 76 | # Read the greeting, expecting it to be identical to the dummy greeting. |
|---|
| 77 | while ($greeting ne '') { |
|---|
| 78 | my $n = sysread(IN, my $buf, length($greeting)); |
|---|
| 79 | next if $n < 0 and $! == EINTR; |
|---|
| 80 | $n >= 0 or die "$0: read: $!"; |
|---|
| 81 | $n > 0 or die "$0: svnserve unexpectedly closed connection"; |
|---|
| 82 | $greeting =~ s/^\Q$buf\E// or die "$0: unexpected greeting from svnserve"; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | # Finally, go into a select loop to transfer the remaining data |
|---|
| 86 | # (STDIN -> OUT, IN -> STDOUT), including the client's response to svnserve. |
|---|
| 87 | my ($cbuf, $sbuf) = ($response, ''); |
|---|
| 88 | my $poll = new IO::Poll; |
|---|
| 89 | $poll->mask(\*STDOUT => POLLHUP); |
|---|
| 90 | $poll->mask(\*OUT => POLLOUT); |
|---|
| 91 | $poll->remove(\*STDIN); |
|---|
| 92 | $poll->mask(\*IN => POLLIN); |
|---|
| 93 | while ($poll->handles()) { |
|---|
| 94 | my $n = $poll->poll(); |
|---|
| 95 | next if $n < 0 and $! == EINTR; |
|---|
| 96 | $n >= 0 or die "select: $!"; |
|---|
| 97 | if ($poll->events(\*STDIN)) { |
|---|
| 98 | my $n = sysread(STDIN, $cbuf, 4096); |
|---|
| 99 | next if $n < 0 and $! == EINTR; |
|---|
| 100 | $n >= 0 or die "read: $!"; |
|---|
| 101 | $poll->remove(\*STDIN); |
|---|
| 102 | $poll->mask(\*OUT => POLLOUT); |
|---|
| 103 | } elsif ($poll->events(\*IN)) { |
|---|
| 104 | my $n = sysread(IN, $sbuf, 4096); |
|---|
| 105 | next if $n < 0 and $! == EINTR; |
|---|
| 106 | $n >= 0 or die "read: $!"; |
|---|
| 107 | $poll->remove(\*IN); |
|---|
| 108 | $poll->mask(\*STDOUT => POLLOUT); |
|---|
| 109 | } elsif ($poll->events(\*STDOUT) & POLLOUT && $sbuf ne '') { |
|---|
| 110 | my $n = syswrite(STDOUT, $sbuf); |
|---|
| 111 | next if $n < 0 and $! == EINTR; |
|---|
| 112 | $n >= 0 or die "write: $!"; |
|---|
| 113 | $sbuf = substr($sbuf, $n); |
|---|
| 114 | if ($sbuf eq '') { |
|---|
| 115 | $poll->mask(\*STDOUT => POLLHUP); |
|---|
| 116 | $poll->mask(\*IN => POLLIN); |
|---|
| 117 | } |
|---|
| 118 | } elsif ($poll->events(\*STDOUT)) { |
|---|
| 119 | $poll->remove(\*STDOUT); |
|---|
| 120 | $poll->remove(\*IN); |
|---|
| 121 | close(STDOUT) or die "close: $!"; |
|---|
| 122 | close(IN) or die "close: $!"; |
|---|
| 123 | } elsif ($poll->events(\*OUT) & POLLOUT && $cbuf ne '') { |
|---|
| 124 | my $n = syswrite(OUT, $cbuf); |
|---|
| 125 | next if $n < 0 and $! == EINTR; |
|---|
| 126 | $n >= 0 or die "write: $!"; |
|---|
| 127 | $cbuf = substr($cbuf, $n); |
|---|
| 128 | if ($cbuf eq '') { |
|---|
| 129 | $poll->mask(\*OUT => POLLHUP); |
|---|
| 130 | $poll->mask(\*STDIN => POLLIN); |
|---|
| 131 | } |
|---|
| 132 | } elsif ($poll->events(\*OUT)) { |
|---|
| 133 | $poll->remove(\*OUT); |
|---|
| 134 | $poll->remove(\*STDIN); |
|---|
| 135 | close(OUT) or die "close: $!"; |
|---|
| 136 | close(STDIN) or die "close: $!"; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | while (waitpid($pid, 0) == -1 && $! == EINTR) { } |
|---|