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;
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";
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