source: server/common/oursrc/execsys/svnproxy.pl @ 825

Last change on this file since 825 was 825, checked in by andersk, 16 years ago
Package the svn vhosting infrastructure into execsys.
  • Property svn:executable set to *
File size: 4.6 KB
Line 
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
21use strict;
22use warnings;
23use IPC::Open2;
24use Errno qw(EINTR);
25
26# Read the initial greeting from a dummy svnserve process.
27my $pid = open(IN, '-|');
28defined $pid or die "$0: open: $!";
29if ($pid == 0) {
30    close(STDIN) or die "$0: close: $!";
31    exec('svnserve', '-i') or die "$0: exec svnproxy: $!";
32}
33my $greeting = '';
34for (;;) {
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.
43my $buf = $greeting;
44while ($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}
50close(IN) or die "$0: close: $!";
51waitpid(-1, 0) or die "$0: waitpid: $!";
52
53# Receive the response from the client, and parse out the URL.
54my $url;
55my $response = '';
56for (;;) {
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.
73open2(\*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.
76while ($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# Write the client's response to svnserve.
85$buf = $response;
86while ($buf ne '') {
87    my $n = syswrite(OUT, $buf);
88    next if $n < 0 and $! == EINTR;
89    $n >= 0 or die "$0: write: $!";
90    $buf = substr($buf, $n);
91}
92
93# Finally, go into a select loop to transfer the remaining data
94# (STDIN -> OUT, IN -> STDOUT).
95my ($cbuf, $sbuf) = ('', '');
96my ($rin, $win, $ein) = ('', '', '');
97my ($stdout, $out, $stdin, $in) = (fileno(STDOUT), fileno(OUT), fileno(STDIN), fileno(IN));
98vec($win, $stdout, 1) = 0;
99vec($win, $out, 1) = 0;
100vec($rin, $stdin, 1) = 1;
101vec($rin, $in, 1) = 1;
102while (vec($win, $stdout, 1) or vec($win, $out, 1) or
103       vec($rin, $stdin, 1) or vec($rin, $in, 1)) {
104    my $n = select(my $rout = $rin, my $wout = $win, my $eout = $ein, undef);
105    next if $n < 0 and $! == EINTR;
106    $n >= 0 or die "select: $!";
107    if (vec($rout, $stdin, 1)) {
108        my $n = sysread(STDIN, $cbuf, 4096);
109        next if $n < 0 and $! == EINTR;
110        $n >= 0 or die "read: $!";
111        vec($rin, $stdin, 1) = 0;
112        vec($win, $out, 1) = 1;
113    } elsif (vec($rout, $in, 1)) {
114        my $n = sysread(IN, $sbuf, 4096);
115        next if $n < 0 and $! == EINTR;
116        $n >= 0 or die "read: $!";
117        vec($rin, $in, 1) = 0;
118        vec($win, $stdout, 1) = 1;
119    } elsif (vec($wout, $stdout, 1) && $sbuf ne '') {
120        my $n = syswrite(STDOUT, $sbuf);
121        next if $n < 0 and $! == EINTR;
122        $n >= 0 or die "write: $!";
123        $sbuf = substr($sbuf, $n);
124        if ($sbuf eq '') {
125            vec($win, $stdout, 1) = 0;
126            vec($rin, $in, 1) = 1;
127        }
128    } elsif (vec($wout, $stdout, 1)) {
129        vec($win, $stdout, 1) = 0;
130        close(STDOUT) or die "close: $!";
131        close(IN) or die "close: $!";
132    } elsif (vec($wout, $out, 1) && $cbuf ne '') {
133        my $n = syswrite(OUT, $cbuf);
134        next if $n < 0 and $! == EINTR;
135        $n >= 0 or die "write: $!";
136        $cbuf = substr($cbuf, $n);
137        if ($cbuf eq '') {
138            vec($win, $out, 1) = 0;
139            vec($rin, $stdin, 1) = 1;
140        }
141    } elsif (vec($wout, $out, 1)) {
142        vec($win, $out, 1) = 0;
143        close(OUT) or die "close: $!";
144        close(STDIN) or die "close: $!";
145    }
146}
Note: See TracBrowser for help on using the repository browser.