@matheusbsbs / matheus-bagliano / commits / 693b63273e
Fork 0

cvsserver: Add some useful commandline options

Make git-cvsserver understand some options inspired by git-daemon, namely --base-path, --export-all, --strict-paths. Also allow the caller to specify a whitelist of allowed directories, again similar to git-daemon. While already adding option parsing also support the common --help and --version options. Rationale: While the gitcvs.enabled configuration option already offers means to limit git-cvsserver access to a repository, there are some use cases where other methods of access control prove to be more useful. E.g. if setting up a pserver for a collection of public repositories one might want limit the exported repositories to exactly the directory this collection is located whithout having to worry about other repositories that might lie around with the configuration variable set (never trust your users ;) Signed-off-by: Frank Lichtenheld <frank@lichtenheld.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Frank Lichtenheld committed Jun 7, 2007 at 16:57 UTC 693b63273e05b5db4612178989271de14439b08f
3 files changed +143 -6
Documentation/git-cvsserver.txt
+42
@@ -7,10 +7,52 @@ git-cvsserver - A CVS server emulator for git
7
8 SYNOPSIS
9 --------
10 +
11 +SSH:
12 +
13 [verse]
14 export CVS_SERVER=git-cvsserver
15 'cvs' -d :ext:user@server/path/repo.git co <HEAD_name>
16
17 +pserver (/etc/inetd.conf):
18 +
19 +[verse]
20 +cvspserver stream tcp nowait nobody /usr/bin/git-cvsserver git-cvsserver pserver
21 +
22 +Usage:
23 +
24 +[verse]
25 +'git-cvsserver' [options] [pserver|server] [<directory> ...]
26 +
27 +OPTIONS
28 +-------
29 +
30 +All these options obviously only make sense if enforced by the server side.
31 +They have been implemented to resemble the gitlink:git-daemon[1] options as
32 +closely as possible.
33 +
34 +--base-path <path>::
35 +Prepend 'path' to requested CVSROOT
36 +
37 +--strict-paths::
38 +Don't allow recursing into subdirectories
39 +
40 +--export-all::
41 +Don't check for `gitcvs.enabled` in config
42 +
43 +--version, -V::
44 +Print version information and exit
45 +
46 +--help, -h, -H::
47 +Print usage information and exit
48 +
49 +<directory>::
50 +You can specify a list of allowed directories. If no directories
51 +are given, all are allowed. This is an additional restriction, gitcvs
52 +access still needs to be enabled by the `gitcvs.enabled` config option
53 +unless '--export-all' was given, too.
54 +
55 +
56 DESCRIPTION
57 -----------
58
git-cvsserver.perl
+73 -6
@@ -22,6 +22,9 @@ use bytes;
22 use Fcntl;
23 use File::Temp qw/tempdir tempfile/;
24 use File::Basename;
25 +use Getopt::Long qw(:config require_order no_ignore_case);
26 +
27 +my $VERSION = '@@GIT_VERSION@@';
28
29 my $log = GITCVS::log->new();
30 my $cfg;
@@ -85,15 +88,52 @@ my $methods = {
88 my $state = { prependdir => '' };
89 $log->info("--------------- STARTING -----------------");
90
91 +my $usage =
92 + "Usage: git-cvsserver [options] [pserver|server] [<directory> ...]\n".
93 + " --base-path <path> : Prepend to requested CVSROOT\n".
94 + " --strict-paths : Don't allow recursing into subdirectories\n".
95 + " --export-all : Don't check for gitcvs.enabled in config\n".
96 + " --version, -V : Print version information and exit\n".
97 + " --help, -h, -H : Print usage information and exit\n".
98 + "\n".
99 + "<directory> ... is a list of allowed directories. If no directories\n".
100 + "are given, all are allowed. This is an additional restriction, gitcvs\n".
101 + "access still needs to be enabled by the gitcvs.enabled config option.\n";
102 +
103 +my @opts = ( 'help|h|H', 'version|V',
104 + 'base-path=s', 'strict-paths', 'export-all' );
105 +GetOptions( $state, @opts )
106 + or die $usage;
107 +
108 +if ($state->{version}) {
109 + print "git-cvsserver version $VERSION\n";
110 + exit;
111 +}
112 +if ($state->{help}) {
113 + print $usage;
114 + exit;
115 +}
116 +
117 my $TEMP_DIR = tempdir( CLEANUP => 1 );
118 $log->debug("Temporary directory is '$TEMP_DIR'");
119
120 +$state->{method} = 'ext';
121 +if (@ARGV) {
122 + if ($ARGV[0] eq 'pserver') {
123 + $state->{method} = 'pserver';
124 + shift @ARGV;
125 + } elsif ($ARGV[0] eq 'server') {
126 + shift @ARGV;
127 + }
128 +}
129 +
130 +# everything else is a directory
131 +$state->{allowed_roots} = [ @ARGV ];
132 +
133 # if we are called with a pserver argument,
134 # deal with the authentication cat before entering the
135 # main loop
94 -$state->{method} = 'ext';
95 -if (@ARGV && $ARGV[0] eq 'pserver') {
96 - $state->{method} = 'pserver';
136 +if ($state->{method} eq 'pserver') {
137 my $line = <STDIN>; chomp $line;
138 unless( $line =~ /^BEGIN (AUTH|VERIFICATION) REQUEST$/) {
139 die "E Do not understand $line - expecting BEGIN AUTH REQUEST\n";
@@ -178,13 +218,40 @@ sub req_Root
218 return 0;
219 }
220
181 - $state->{CVSROOT} = $data;
221 + $state->{CVSROOT} = $state->{'base-path'} || '';
222 + $state->{CVSROOT} =~ s#/+$##;
223 + $state->{CVSROOT} .= $data;
224
225 $ENV{GIT_DIR} = $state->{CVSROOT} . "/";
226 +
227 + if (@{$state->{allowed_roots}}) {
228 + my $allowed = 0;
229 + foreach my $dir (@{$state->{allowed_roots}}) {
230 + next unless $dir =~ m#^/#;
231 + $dir =~ s#/+$##;
232 + if ($state->{'strict-paths'}) {
233 + if ($ENV{GIT_DIR} =~ m#^\Q$dir\E/?$#) {
234 + $allowed = 1;
235 + last;
236 + }
237 + } elsif ($ENV{GIT_DIR} =~ m#^\Q$dir\E(/?$|/)#) {
238 + $allowed = 1;
239 + last;
240 + }
241 + }
242 +
243 + unless ($allowed) {
244 + print "E $ENV{GIT_DIR} does not seem to be a valid GIT repository\n";
245 + print "E \n";
246 + print "error 1 $ENV{GIT_DIR} is not a valid repository\n";
247 + return 0;
248 + }
249 + }
250 +
251 unless (-d $ENV{GIT_DIR} && -e $ENV{GIT_DIR}.'HEAD') {
252 print "E $ENV{GIT_DIR} does not seem to be a valid GIT repository\n";
186 - print "E \n";
187 - print "error 1 $ENV{GIT_DIR} is not a valid repository\n";
253 + print "E \n";
254 + print "error 1 $ENV{GIT_DIR} is not a valid repository\n";
255 return 0;
256 }
257
t/t9400-git-cvsserver-server.sh
+28
@@ -143,6 +143,34 @@ test_expect_success 'req_Root failure (conflicting roots)' \
143 'cat request-conflict | git-cvsserver pserver >log 2>&1 &&
144 tail log | grep -q "^error 1 Conflicting roots specified$"'
145
146 +test_expect_success 'req_Root (strict paths)' \
147 + 'cat request-anonymous | git-cvsserver --strict-paths pserver $SERVERDIR >log 2>&1 &&
148 + tail -n1 log | grep -q "^I LOVE YOU$"'
149 +
150 +test_expect_failure 'req_Root failure (strict-paths)' \
151 + 'cat request-anonymous | git-cvsserver --strict-paths pserver $WORKDIR >log 2>&1'
152 +
153 +test_expect_success 'req_Root (w/o strict-paths)' \
154 + 'cat request-anonymous | git-cvsserver pserver $WORKDIR/ >log 2>&1 &&
155 + tail -n1 log | grep -q "^I LOVE YOU$"'
156 +
157 +test_expect_failure 'req_Root failure (w/o strict-paths)' \
158 + 'cat request-anonymous | git-cvsserver pserver $WORKDIR/gitcvs >log 2>&1'
159 +
160 +cat >request-base <<EOF
161 +BEGIN AUTH REQUEST
162 +/gitcvs.git
163 +anonymous
164 +
165 +END AUTH REQUEST
166 +EOF
167 +
168 +test_expect_success 'req_Root (base-path)' \
169 + 'cat request-base | git-cvsserver --strict-paths --base-path $WORKDIR/ pserver $SERVERDIR >log 2>&1 &&
170 + tail -n1 log | grep -q "^I LOVE YOU$"'
171 +
172 +test_expect_failure 'req_Root failure (base-path)' \
173 + 'cat request-anonymous | git-cvsserver --strict-paths --base-path $WORKDIR pserver $SERVERDIR >log 2>&1'
174
175 #--------------
176 # CONFIG TESTS