2009-08-25 05:14:46 +02:00
|
|
|
#!/usr/bin/perl
|
2009-08-23 14:54:37 +02:00
|
|
|
|
|
|
|
use strict;
|
2009-08-25 05:14:46 +02:00
|
|
|
use warnings;
|
2009-08-23 14:54:37 +02:00
|
|
|
|
2010-09-05 15:13:21 +02:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# you: what's the invocation?
|
|
|
|
# me: Hail, O Lord Ganesha, destroyer of obsta...
|
|
|
|
# you: err hmm not *that* sort of invocation... I meant how does this program
|
|
|
|
# get invoked?
|
|
|
|
# me: oh hehe <hides sheepish grin>, ok here we go...
|
|
|
|
#
|
|
|
|
# ssh mode
|
|
|
|
# - started by sshd
|
|
|
|
# - one argument, the "user" name
|
|
|
|
# - one env var, SSH_ORIGINAL_COMMAND, containing the command
|
|
|
|
# - command typically: git-(receive|upload)-pack 'reponame(.git)?'
|
|
|
|
# - special gitolite commands: info, expand, (get|set)(perms|desc)
|
|
|
|
# - special non-gitolite commands: rsync, svnserve, htpasswd
|
|
|
|
# - other commands: anything in $GL_ADC_PATH if defined (see rc file)
|
|
|
|
#
|
|
|
|
# (smart) http mode
|
|
|
|
# - started by apache (httpd)
|
|
|
|
# - no arguments
|
|
|
|
# - REQUEST_URI contains verb and repo, REMOTE_USER contains username
|
|
|
|
# - REQUEST_URI looks like /path/reponame.git/(info/refs\?service=)?git-(receive|upload)-pack
|
|
|
|
# - no special processing commands currently handled
|
|
|
|
# ----------------------------------------------------------------------------
|
2009-08-23 14:54:37 +02:00
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# common definitions
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
|
2009-12-04 05:21:22 +01:00
|
|
|
# these are set by the "rc" file
|
2011-01-01 15:18:18 +01:00
|
|
|
our ($GL_LOGT, $GL_CONF_COMPILED, $REPO_BASE, $GIT_PATH, $REPO_UMASK, $GL_ADMINDIR, $RSYNC_BASE, $HTPASSWD_FILE, $GL_WILDREPOS, $GL_WILDREPOS_DEFPERMS, $GL_ADC_PATH, $SVNSERVE, $PROJECTS_LIST, $GL_SLAVE_MODE, $GL_PERFLOGT, $GL_ALL_READ_ALL);
|
2009-12-04 05:21:22 +01:00
|
|
|
# and these are set by gitolite.pm
|
2010-10-06 17:00:55 +02:00
|
|
|
our ($R_COMMANDS, $W_COMMANDS, $REPONAME_PATT, $REPOPATT_PATT, $ADC_CMD_ARGS_PATT);
|
2009-08-23 14:54:37 +02:00
|
|
|
our %repos;
|
2010-05-10 08:16:47 +02:00
|
|
|
our %groups;
|
2011-01-01 07:02:58 +01:00
|
|
|
our %git_configs;
|
2011-01-01 10:44:54 +01:00
|
|
|
our %split_conf;;
|
2009-08-23 14:54:37 +02:00
|
|
|
|
2009-10-25 03:59:52 +01:00
|
|
|
# the common setup module is in the same directory as this running program is
|
|
|
|
my $bindir = $0;
|
|
|
|
$bindir =~ s/\/[^\/]+$//;
|
2010-02-09 12:30:01 +01:00
|
|
|
$bindir = "$ENV{PWD}/$bindir" unless $bindir =~ /^\//;
|
2010-10-23 13:13:47 +02:00
|
|
|
unshift @INC, $bindir;
|
|
|
|
require gitolite or die "parse gitolite.pm failed\n";
|
2009-10-25 03:59:52 +01:00
|
|
|
|
|
|
|
# ask where the rc file is, get it, and "do" it
|
|
|
|
&where_is_rc();
|
|
|
|
die "parse $ENV{GL_RC} failed: " . ($! or $@) unless do $ENV{GL_RC};
|
2009-08-23 14:54:37 +02:00
|
|
|
|
2010-01-31 18:40:12 +01:00
|
|
|
# we need to pass GL_ADMINDIR and the bindir to the child hooks
|
2009-12-15 08:05:48 +01:00
|
|
|
$ENV{GL_ADMINDIR} = $GL_ADMINDIR;
|
|
|
|
$ENV{GL_BINDIR} = $bindir;
|
|
|
|
|
2009-10-13 06:32:45 +02:00
|
|
|
# add a custom path for git binaries, if specified
|
|
|
|
$ENV{PATH} .= ":$GIT_PATH" if $GIT_PATH;
|
|
|
|
|
2010-06-22 13:30:48 +02:00
|
|
|
# set default permission of wildcard repositories
|
|
|
|
$ENV{GL_WILDREPOS_DEFPERMS} = $GL_WILDREPOS_DEFPERMS if $GL_WILDREPOS_DEFPERMS;
|
|
|
|
|
2009-12-15 11:41:21 +01:00
|
|
|
# set the umask before creating any files
|
|
|
|
umask($REPO_UMASK);
|
|
|
|
|
2010-06-29 07:25:36 +02:00
|
|
|
$ENV{GL_REPO_BASE_ABS} = ( $REPO_BASE =~ m(^/) ? $REPO_BASE : "$ENV{HOME}/$REPO_BASE" );
|
2010-02-04 10:12:10 +01:00
|
|
|
|
2009-08-23 14:54:37 +02:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# start...
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
|
2009-12-19 16:22:30 +01:00
|
|
|
# if the first argument is a "-s", this user is allowed to get a shell using
|
|
|
|
# this key
|
|
|
|
my $shell_allowed = 0;
|
2010-07-21 02:57:43 +02:00
|
|
|
if (@ARGV and $ARGV[0] eq '-s') {
|
2009-12-19 16:22:30 +01:00
|
|
|
$shell_allowed = 1;
|
|
|
|
shift;
|
|
|
|
}
|
|
|
|
|
2010-09-05 15:13:21 +02:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# set up SSH_ORIGINAL_COMMAND and SSH_CONNECTION in http mode
|
|
|
|
# ----------------------------------------------------------------------------
|
2010-07-21 02:57:43 +02:00
|
|
|
|
2010-09-05 15:13:21 +02:00
|
|
|
# fake out SSH_ORIGINAL_COMMAND and SSH_CONNECTION so the rest of the code
|
|
|
|
# stays the same (except the exec at the end).
|
|
|
|
|
|
|
|
my $user;
|
|
|
|
if ($ENV{REQUEST_URI}) {
|
2010-09-05 16:28:31 +02:00
|
|
|
die "fallback to DAV not supported\n" if $ENV{REQUEST_METHOD} eq 'PROPFIND';
|
|
|
|
|
2010-09-05 15:13:21 +02:00
|
|
|
# these patterns indicate normal git usage; see "services[]" in
|
|
|
|
# http-backend.c for how I got that. Also note that "info" is overloaded;
|
|
|
|
# git uses "info/refs...", while gitolite uses "info" or "info?...". So
|
|
|
|
# there's a "/" after info in the list below
|
2010-09-05 15:36:18 +02:00
|
|
|
if ($ENV{PATH_INFO} =~ m(^/(.*)/(HEAD$|info/refs$|objects/|git-(?:upload|receive)-pack$))) {
|
|
|
|
my $repo = $1;
|
|
|
|
my $verb = ($ENV{REQUEST_URI} =~ /git-receive-pack/) ? 'git-receive-pack' : 'git-upload-pack';
|
2010-09-05 15:13:21 +02:00
|
|
|
$ENV{SSH_ORIGINAL_COMMAND} = "$verb '$repo'";
|
|
|
|
} else {
|
|
|
|
# this is one of our custom commands; could be anything really,
|
|
|
|
# because of the adc feature
|
|
|
|
my ($verb) = ($ENV{PATH_INFO} =~ m(^/(\S+)));
|
|
|
|
my $args = $ENV{QUERY_STRING};
|
|
|
|
$args =~ s/\+/ /g;
|
|
|
|
$ENV{SSH_ORIGINAL_COMMAND} = $verb;
|
|
|
|
$ENV{SSH_ORIGINAL_COMMAND} .= " $args" if $args;
|
|
|
|
&print_http_headers(); # in preparation for the eventual output!
|
|
|
|
}
|
|
|
|
$ENV{SSH_CONNECTION} = "$ENV{REMOTE_ADDR} $ENV{REMOTE_PORT} $ENV{SERVER_ADDR} $ENV{SERVER_PORT}";
|
|
|
|
$user = $ENV{GL_USER} = $ENV{REMOTE_USER};
|
|
|
|
} else {
|
|
|
|
# no (more) arguments given in ssh mode? default user is $USER
|
|
|
|
# (fedorahosted works like this, and it is harmless for others)
|
|
|
|
@ARGV = ($ENV{USER}) unless @ARGV;
|
|
|
|
$user=$ENV{GL_USER}=shift;
|
|
|
|
}
|
2009-08-23 14:54:37 +02:00
|
|
|
|
2010-02-01 12:24:39 +01:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# logging, timestamp env vars
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
|
2010-08-17 18:05:54 +02:00
|
|
|
$ENV{GL_LOG} = &get_logfilename($GL_LOGT);
|
2010-02-01 12:24:39 +01:00
|
|
|
|
2009-08-23 14:54:37 +02:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# sanity checks on SSH_ORIGINAL_COMMAND
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
|
2010-02-01 07:06:24 +01:00
|
|
|
# no SSH_ORIGINAL_COMMAND given...
|
2009-10-28 09:03:24 +01:00
|
|
|
unless ($ENV{SSH_ORIGINAL_COMMAND}) {
|
2010-02-01 07:06:24 +01:00
|
|
|
# if the user is allowed to use a shell, give him one
|
2009-12-19 16:22:30 +01:00
|
|
|
if ($shell_allowed) {
|
|
|
|
my $shell = $ENV{SHELL};
|
|
|
|
$shell =~ s/.*\//-/; # change "/bin/bash" to "-bash"
|
2010-06-16 03:50:12 +02:00
|
|
|
&log_it($shell);
|
2009-12-19 16:22:30 +01:00
|
|
|
exec { $ENV{SHELL} } $shell;
|
|
|
|
}
|
2010-02-01 07:06:24 +01:00
|
|
|
# otherwise, pretend he typed in "info" and carry on...
|
|
|
|
$ENV{SSH_ORIGINAL_COMMAND} = 'info';
|
2009-10-28 09:03:24 +01:00
|
|
|
}
|
|
|
|
|
2010-08-10 10:12:52 +02:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# slave mode should not do much
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
die "server is in slave mode; you can only fetch\n"
|
|
|
|
if ($GL_SLAVE_MODE and $ENV{SSH_ORIGINAL_COMMAND} !~ /^(info|expand|get|git-upload-)/);
|
|
|
|
|
2010-04-24 14:46:13 +02:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# admin defined commands
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# please see doc/admin-defined-commands.mkd for details
|
|
|
|
if ($GL_ADC_PATH and -d $GL_ADC_PATH) {
|
|
|
|
my ($cmd, @args) = split ' ', $ENV{SSH_ORIGINAL_COMMAND};
|
|
|
|
if (-x "$GL_ADC_PATH/$cmd") {
|
|
|
|
# yes this is rather strict, sorry.
|
2010-10-06 17:00:55 +02:00
|
|
|
do { die "I don't like $_\n" unless $_ =~ $ADC_CMD_ARGS_PATT } for ($cmd, @args);
|
2010-06-16 03:50:12 +02:00
|
|
|
&log_it("$GL_ADC_PATH/$ENV{SSH_ORIGINAL_COMMAND}");
|
2010-04-24 14:46:13 +02:00
|
|
|
exec("$GL_ADC_PATH/$cmd", @args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-06 10:09:40 +01:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# get and set perms for actual repo created by wildcard-autoviv
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
|
2010-02-18 14:50:46 +01:00
|
|
|
my $CUSTOM_COMMANDS=qr/^\s*(expand|(get|set)(perms|desc))\b/;
|
2009-12-06 10:09:40 +01:00
|
|
|
|
|
|
|
# note that all the subs called here chdir somewhere else and do not come
|
|
|
|
# back; they all blithely take advantage of the fact that processing custom
|
|
|
|
# commands is sort of a dead end for normal (git) processing
|
|
|
|
|
2010-02-04 10:12:10 +01:00
|
|
|
if ($ENV{SSH_ORIGINAL_COMMAND} =~ $CUSTOM_COMMANDS) {
|
2010-02-05 11:30:47 +01:00
|
|
|
die "wildrepos disabled, sorry\n" unless $GL_WILDREPOS;
|
2010-02-04 10:12:10 +01:00
|
|
|
my $cmd = $ENV{SSH_ORIGINAL_COMMAND};
|
2010-03-01 16:02:54 +01:00
|
|
|
my ($verb, $repo) = ($cmd =~ /^\s*(\S+)(?:\s+'?\/?(.*?)(?:\.git)?'?)?$/);
|
2010-02-18 14:50:46 +01:00
|
|
|
# deal with "no argument" cases
|
2010-02-26 15:55:28 +01:00
|
|
|
$verb eq 'expand' ? $repo = '^' : die "$verb needs an argument\n" unless $repo;
|
2009-12-06 10:09:40 +01:00
|
|
|
if ($repo =~ $REPONAME_PATT and $verb =~ /getperms|setperms/) {
|
|
|
|
# with an actual reponame, you can "getperms" or "setperms"
|
2010-06-29 07:25:36 +02:00
|
|
|
get_set_perms($repo, $verb, $user);
|
2009-12-06 10:09:40 +01:00
|
|
|
}
|
2010-02-04 22:40:13 +01:00
|
|
|
elsif ($repo =~ $REPONAME_PATT and $verb =~ /(get|set)desc/) {
|
|
|
|
# with an actual reponame, you can "getdesc" or "setdesc"
|
2010-06-29 07:25:36 +02:00
|
|
|
get_set_desc($repo, $verb, $user);
|
2009-12-06 10:09:40 +01:00
|
|
|
}
|
2009-12-21 13:19:21 +01:00
|
|
|
elsif ($verb eq 'expand') {
|
2009-12-06 10:09:40 +01:00
|
|
|
# with a wildcard, you can "expand" it to see what repos actually match
|
2010-01-29 10:09:03 +01:00
|
|
|
die "$repo has invalid characters" unless "x$repo" =~ $REPOPATT_PATT;
|
2010-06-29 07:25:36 +02:00
|
|
|
expand_wild($GL_ADMINDIR, $GL_CONF_COMPILED, $repo, $user);
|
2009-12-06 10:09:40 +01:00
|
|
|
} else {
|
|
|
|
die "$cmd doesn't make sense to me\n";
|
|
|
|
}
|
2010-01-08 13:05:11 +01:00
|
|
|
exit 0;
|
2009-12-06 10:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------
|
2010-02-01 11:07:35 +01:00
|
|
|
# non-git commands
|
2009-12-06 10:09:40 +01:00
|
|
|
# ----------------------------------------------------------------------------
|
2009-08-23 14:54:37 +02:00
|
|
|
|
2010-02-01 11:07:35 +01:00
|
|
|
# if the command does NOT fit the pattern of a normal git command, send it off
|
|
|
|
# somewhere else...
|
2009-08-23 14:54:37 +02:00
|
|
|
|
2010-02-01 11:07:35 +01:00
|
|
|
# side notes on detecting a normal git command: the pattern we check allows
|
|
|
|
# old style as well as new style ("git-subcommand arg" or "git subcommand
|
2010-05-21 18:02:33 +02:00
|
|
|
# arg"). Currently, this is how git sends across the command (including the
|
|
|
|
# single quotes):
|
2010-02-01 11:07:35 +01:00
|
|
|
# git-receive-pack 'reponame.git'
|
2009-08-23 14:54:37 +02:00
|
|
|
|
2010-02-01 11:07:35 +01:00
|
|
|
my ($verb, $repo) = ($ENV{SSH_ORIGINAL_COMMAND} =~ /^\s*(git\s+\S+|\S+)\s+'\/?(.*?)(?:\.git)?'/);
|
2010-04-25 01:25:43 +02:00
|
|
|
unless ( $verb and ( $verb eq 'git-init' or $verb =~ $R_COMMANDS or $verb =~ $W_COMMANDS ) and $repo and $repo =~ $REPONAME_PATT ) {
|
2010-02-01 11:07:35 +01:00
|
|
|
# ok, it's not a normal git command; call the special command helper
|
2010-05-09 17:04:55 +02:00
|
|
|
&special_cmd ($GL_ADMINDIR, $GL_CONF_COMPILED, $shell_allowed, $RSYNC_BASE, $HTPASSWD_FILE, $SVNSERVE);
|
2010-02-01 11:07:35 +01:00
|
|
|
exit;
|
2009-12-19 16:22:30 +01:00
|
|
|
}
|
2009-12-11 17:07:33 +01:00
|
|
|
die "$repo ends with a slash; I don't like that\n" if $repo =~ /\/$/;
|
2009-12-13 08:13:44 +01:00
|
|
|
die "$repo has two consecutive periods; I don't like that\n" if $repo =~ /\.\./;
|
2009-08-23 14:54:37 +02:00
|
|
|
|
2010-02-04 10:12:10 +01:00
|
|
|
# reponame
|
|
|
|
$ENV{GL_REPO}=$repo;
|
2009-08-23 14:54:37 +02:00
|
|
|
|
2010-02-01 11:07:35 +01:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# the real git commands (git-receive-pack, etc...)
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
|
2009-08-23 14:54:37 +02:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# first level permissions check
|
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
|
2011-01-01 15:18:18 +01:00
|
|
|
my ($perm, $creator, $wild);
|
|
|
|
if ( $GL_ALL_READ_ALL and $verb =~ $R_COMMANDS and -d "$ENV{GL_REPO_BASE_ABS}/$repo.git") {
|
|
|
|
$perm = 'R';
|
|
|
|
} else {
|
|
|
|
($perm, $creator, $wild) = &repo_rights($repo);
|
|
|
|
}
|
2010-04-24 09:44:16 +02:00
|
|
|
if ($perm =~ /C/) {
|
|
|
|
# it was missing, and you have create perms
|
2010-06-29 07:25:36 +02:00
|
|
|
wrap_chdir("$ENV{GL_REPO_BASE_ABS}");
|
2010-04-24 09:44:16 +02:00
|
|
|
new_repo($repo, "$GL_ADMINDIR/hooks/common", $user);
|
2010-09-03 05:21:16 +02:00
|
|
|
# note pwd is now the bare "repo.git"; new_repo does that...
|
2010-08-20 17:25:23 +02:00
|
|
|
wrap_print("gl-perms", "$GL_WILDREPOS_DEFPERMS\n") if $GL_WILDREPOS_DEFPERMS;
|
2011-01-01 07:02:58 +01:00
|
|
|
&setup_git_configs($repo, \%git_configs);
|
2010-07-19 13:17:38 +02:00
|
|
|
&setup_daemon_access($repo);
|
2010-08-20 19:17:34 +02:00
|
|
|
&add_del_line ("$repo.git", $PROJECTS_LIST, &setup_gitweb_access($repo, '', ''));
|
2010-04-24 09:44:16 +02:00
|
|
|
wrap_chdir($ENV{HOME});
|
2009-12-05 18:09:56 +01:00
|
|
|
}
|
2009-12-04 05:21:22 +01:00
|
|
|
|
2009-08-25 05:51:07 +02:00
|
|
|
# we know the user and repo; we just need to know what perm he's trying
|
2010-04-24 09:44:16 +02:00
|
|
|
# aa == attempted access
|
|
|
|
my $aa = ($verb =~ $R_COMMANDS ? 'R' : 'W');
|
2010-10-08 01:38:13 +02:00
|
|
|
die "$aa access for $repo DENIED to $user
|
|
|
|
(Or there may be no repository at the given path. Did you spell it correctly?)\n" unless $perm =~ /$aa/;
|
2009-08-23 14:54:37 +02:00
|
|
|
|
2010-11-16 15:39:45 +01:00
|
|
|
# check if repo is write-enabled
|
|
|
|
&check_repo_write_enabled($repo) if $aa eq 'W';
|
|
|
|
|
2009-08-23 14:54:37 +02:00
|
|
|
# ----------------------------------------------------------------------------
|
2010-02-01 12:24:39 +01:00
|
|
|
# over to git now
|
2009-08-23 14:54:37 +02:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
|
2010-09-05 15:13:21 +02:00
|
|
|
if ($ENV{REQUEST_URI}) {
|
|
|
|
&log_it($ENV{REQUEST_URI});
|
|
|
|
exec $ENV{GIT_HTTP_BACKEND};
|
|
|
|
# the GIT_HTTP_BACKEND env var should be set either by the rc file, or as
|
|
|
|
# a SetEnv in the apache config somewhere
|
|
|
|
}
|
|
|
|
|
2010-06-16 03:50:12 +02:00
|
|
|
&log_it();
|
2009-08-23 14:54:37 +02:00
|
|
|
|
|
|
|
$repo = "'$REPO_BASE/$repo.git'";
|
2010-04-25 01:25:43 +02:00
|
|
|
exec("git", "shell", "-c", "$verb $repo") unless $verb eq 'git-init';
|