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