2012-03-08 09:00:13 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
# gitolite shell, invoked from ~/.ssh/authorized_keys
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
2012-03-10 14:26:29 +01:00
|
|
|
use FindBin;
|
2012-03-08 09:00:13 +01:00
|
|
|
|
2012-03-10 14:26:29 +01:00
|
|
|
BEGIN { $ENV{GL_BINDIR} = $FindBin::RealBin; }
|
2012-04-06 16:59:05 +02:00
|
|
|
BEGIN { $ENV{GL_LIBDIR} = "$ENV{GL_BINDIR}/lib"; }
|
|
|
|
use lib $ENV{GL_LIBDIR};
|
2012-04-15 07:47:44 +02:00
|
|
|
|
|
|
|
# set HOME
|
|
|
|
BEGIN { $ENV{HOME} = $ENV{GITOLITE_HTTP_HOME} if $ENV{GITOLITE_HTTP_HOME}; }
|
|
|
|
|
2012-03-08 09:00:13 +01:00
|
|
|
use Gitolite::Rc;
|
|
|
|
use Gitolite::Common;
|
|
|
|
use Gitolite::Conf::Load;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
2012-03-12 16:24:30 +01:00
|
|
|
|
|
|
|
# the main() sub expects ssh-ish things; set them up...
|
2012-03-21 11:53:50 +01:00
|
|
|
my $id = '';
|
2012-03-12 16:24:30 +01:00
|
|
|
if ( exists $ENV{G3T_USER} ) {
|
2012-03-30 02:41:06 +02:00
|
|
|
$id = in_file(); # file:// masquerading as ssh:// for easy testing
|
2012-03-12 16:24:30 +01:00
|
|
|
} elsif ( exists $ENV{SSH_CONNECTION} ) {
|
2012-03-21 11:53:50 +01:00
|
|
|
$id = in_ssh();
|
2012-03-12 16:24:30 +01:00
|
|
|
} elsif ( exists $ENV{REQUEST_URI} ) {
|
2012-03-21 11:53:50 +01:00
|
|
|
$id = in_http();
|
2012-03-12 16:24:30 +01:00
|
|
|
} else {
|
|
|
|
_die "who the *heck* are you?";
|
|
|
|
}
|
|
|
|
|
2012-03-30 06:00:13 +02:00
|
|
|
trigger('INPUT');
|
|
|
|
|
2012-03-21 11:53:50 +01:00
|
|
|
main($id);
|
|
|
|
|
2012-03-30 02:41:06 +02:00
|
|
|
gl_log('END') if $$ == $ENV{GL_TID};
|
2012-03-12 16:24:30 +01:00
|
|
|
|
|
|
|
exit 0;
|
2012-03-08 09:00:13 +01:00
|
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
2012-03-30 02:41:06 +02:00
|
|
|
sub in_file {
|
|
|
|
gl_log( 'file', "ARGV=" . join( ",", @ARGV ), "SOC=$ENV{SSH_ORIGINAL_COMMAND}" );
|
|
|
|
|
2012-03-19 03:19:01 +01:00
|
|
|
if ( $ENV{SSH_ORIGINAL_COMMAND} =~ /git-\w+-pack/ ) {
|
2012-03-18 05:31:07 +01:00
|
|
|
print STDERR "TRACE: gsh(", join( ")(", @ARGV ), ")\n";
|
|
|
|
print STDERR "TRACE: gsh(SOC=$ENV{SSH_ORIGINAL_COMMAND})\n";
|
|
|
|
}
|
2012-03-30 02:41:06 +02:00
|
|
|
return 'file';
|
2012-03-12 16:24:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub in_http {
|
2012-04-15 07:47:44 +02:00
|
|
|
http_setup_die_handler();
|
|
|
|
|
|
|
|
_die "GITOLITE_HTTP_HOME not set" unless $ENV{GITOLITE_HTTP_HOME};
|
|
|
|
|
|
|
|
_die "fallback to DAV not supported" if $ENV{REQUEST_METHOD} eq 'PROPFIND';
|
|
|
|
|
|
|
|
# fake out SSH_ORIGINAL_COMMAND and SSH_CONNECTION when called via http,
|
|
|
|
# so the rest of the code stays the same (except the exec at the end).
|
|
|
|
http_simulate_ssh_connection();
|
|
|
|
|
|
|
|
$ENV{REMOTE_USER} ||= $rc{HTTP_ANON_USER};
|
|
|
|
@ARGV = ( $ENV{REMOTE_USER} );
|
|
|
|
|
|
|
|
return 'http';
|
2012-03-12 16:24:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub in_ssh {
|
2012-03-30 02:41:06 +02:00
|
|
|
my $ip;
|
|
|
|
( $ip = $ENV{SSH_CONNECTION} || '(no-IP)' ) =~ s/ .*//;
|
|
|
|
|
2012-04-18 02:56:18 +02:00
|
|
|
gl_log( 'ssh', "ARGV=" . join( ",", @ARGV ), "SOC=" . ( $ENV{SSH_ORIGINAL_COMMAND} || '' ), "FROM=$ip" );
|
2012-03-30 02:41:06 +02:00
|
|
|
|
2012-03-20 19:21:18 +01:00
|
|
|
$ENV{SSH_ORIGINAL_COMMAND} ||= '';
|
|
|
|
my $soc = $ENV{SSH_ORIGINAL_COMMAND};
|
|
|
|
$soc =~ s/[\n\r]+/<<newline>>/g;
|
|
|
|
_die "I don't like newlines in the command: $soc\n" if $ENV{SSH_ORIGINAL_COMMAND} ne $soc;
|
2012-03-21 11:53:50 +01:00
|
|
|
|
|
|
|
return $ip;
|
2012-03-12 16:24:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
2012-03-08 09:00:13 +01:00
|
|
|
|
2012-03-12 16:24:30 +01:00
|
|
|
# call this once you are sure arg-1 is the username and SSH_ORIGINAL_COMMAND
|
|
|
|
# has been setup (even if it's not actually coming via ssh).
|
|
|
|
sub main {
|
2012-03-21 11:53:50 +01:00
|
|
|
my $id = shift;
|
|
|
|
|
2012-03-15 17:29:36 +01:00
|
|
|
umask $rc{UMASK};
|
|
|
|
|
2012-03-12 16:24:30 +01:00
|
|
|
# set up the user
|
|
|
|
my $user = $ENV{GL_USER} = shift @ARGV;
|
2012-03-08 09:00:13 +01:00
|
|
|
|
2012-03-12 16:24:30 +01:00
|
|
|
# set up the repo and the attempted access
|
2012-03-15 15:34:30 +01:00
|
|
|
my ( $verb, $repo ) = parse_soc(); # returns only for git commands
|
2012-03-12 16:24:30 +01:00
|
|
|
sanity($repo);
|
|
|
|
$ENV{GL_REPO} = $repo;
|
|
|
|
my $aa = ( $verb =~ 'upload' ? 'R' : 'W' );
|
2012-03-08 09:00:13 +01:00
|
|
|
|
2012-03-17 02:30:17 +01:00
|
|
|
# auto-create?
|
|
|
|
if ( repo_missing($repo) and access( $repo, $user, '^C', 'any' ) !~ /DENIED/ ) {
|
|
|
|
require Gitolite::Conf::Store;
|
|
|
|
Gitolite::Conf::Store->import;
|
|
|
|
new_wild_repo( $repo, $user );
|
2012-03-21 11:53:50 +01:00
|
|
|
gl_log( 'create', $repo, $user );
|
2012-03-17 02:30:17 +01:00
|
|
|
}
|
|
|
|
|
2012-03-12 16:24:30 +01:00
|
|
|
# a ref of 'any' signifies that this is a pre-git check, where we don't
|
|
|
|
# yet know the ref that will be eventually pushed (and even that won't
|
|
|
|
# apply if it's a read operation). See the matching code in access() for
|
|
|
|
# more information.
|
2012-03-30 02:41:06 +02:00
|
|
|
unless ( $ENV{GL_BYPASS_ACCESS_CHECKS} ) {
|
2012-03-28 11:55:32 +02:00
|
|
|
my $ret = access( $repo, $user, $aa, 'any' );
|
|
|
|
trace( 1, "access($repo, $user, $aa, 'any')", "-> $ret" );
|
|
|
|
trigger( 'ACCESS_1', $repo, $user, $aa, 'any', $ret );
|
|
|
|
_die $ret . "\n(or you mis-spelled the reponame)" if $ret =~ /DENIED/;
|
2012-03-30 02:41:06 +02:00
|
|
|
|
|
|
|
gl_log( "pre_git", $repo, $user, $aa, 'any', "-> $ret" );
|
2012-03-28 11:55:32 +02:00
|
|
|
}
|
2012-03-12 16:24:30 +01:00
|
|
|
|
2012-04-15 07:47:44 +02:00
|
|
|
exec $ENV{GIT_HTTP_BACKEND} if $ENV{REQUEST_URI};
|
|
|
|
|
2012-03-19 03:19:01 +01:00
|
|
|
trigger( 'PRE_GIT', $repo, $user, $aa, 'any', $verb );
|
2012-03-20 08:21:00 +01:00
|
|
|
my $repodir = "'$rc{GL_REPO_BASE}/$repo.git'";
|
|
|
|
_system( "git", "shell", "-c", "$verb $repodir" );
|
2012-04-06 02:57:34 +02:00
|
|
|
trigger( 'POST_GIT', $repo, $user, $aa, 'any', $verb );
|
2012-03-12 16:24:30 +01:00
|
|
|
}
|
2012-03-08 09:00:13 +01:00
|
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
2012-03-12 16:24:30 +01:00
|
|
|
sub parse_soc {
|
2012-03-08 09:00:13 +01:00
|
|
|
my $soc = $ENV{SSH_ORIGINAL_COMMAND};
|
2012-03-12 16:24:30 +01:00
|
|
|
$soc ||= 'info';
|
|
|
|
|
2012-03-22 01:01:17 +01:00
|
|
|
my $git_commands = "git-upload-pack|git-receive-pack|git-upload-archive";
|
|
|
|
if ( $soc =~ m(^($git_commands) '/?(.*?)(?:\.git(\d)?)?'$) ) {
|
2012-03-15 17:19:47 +01:00
|
|
|
my ( $verb, $repo, $trace_level ) = ( $1, $2, $3 );
|
|
|
|
$ENV{D} = $trace_level if $trace_level;
|
2012-03-12 16:24:30 +01:00
|
|
|
_die "invalid repo name: '$repo'" if $repo !~ $REPONAME_PATT;
|
2012-03-15 16:30:39 +01:00
|
|
|
trace( 2, "git command", $soc );
|
2012-03-15 15:34:30 +01:00
|
|
|
return ( $verb, $repo );
|
2012-03-12 16:24:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# after this we should not return; caller expects us to handle it all here
|
|
|
|
# and exit out
|
|
|
|
|
|
|
|
_die "suspicious characters loitering about '$soc'" if $soc !~ $REMOTE_COMMAND_PATT;
|
|
|
|
|
|
|
|
my @words = split ' ', $soc;
|
2012-03-15 15:34:30 +01:00
|
|
|
if ( $rc{COMMANDS}{ $words[0] } ) {
|
2012-03-15 16:30:39 +01:00
|
|
|
trace( 2, "gitolite command", $soc );
|
2012-03-15 15:34:30 +01:00
|
|
|
_system( "gitolite", @words );
|
2012-03-12 16:24:30 +01:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_die "unknown git/gitolite command: $soc";
|
2012-03-08 09:00:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub sanity {
|
|
|
|
my $repo = shift;
|
|
|
|
_die "'$repo' contains bad characters" if $repo !~ $REPONAME_PATT;
|
|
|
|
_die "'$repo' ends with a '/'" if $repo =~ m(/$);
|
|
|
|
_die "'$repo' contains '..'" if $repo =~ m(\.\.$);
|
|
|
|
}
|
2012-04-15 07:47:44 +02:00
|
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
# helper functions for "in_http"
|
|
|
|
|
|
|
|
sub http_setup_die_handler {
|
|
|
|
|
|
|
|
$SIG{__DIE__} = sub {
|
2012-04-18 02:56:18 +02:00
|
|
|
my $service = ( $ENV{SSH_ORIGINAL_COMMAND} =~ /git-receive-pack/ ? 'git-receive-pack' : 'git-upload-pack' );
|
2012-04-15 07:47:44 +02:00
|
|
|
my $message = shift; chomp($message);
|
|
|
|
print STDERR "$message\n";
|
|
|
|
|
|
|
|
# format the service response, then the message. With initial
|
|
|
|
# help from Ilari and then a more detailed email from Shawn...
|
|
|
|
$service = "# service=$service\n"; $message = "ERR $message\n";
|
2012-04-18 02:56:18 +02:00
|
|
|
$service = sprintf( "%04X", length($service) + 4 ) . "$service"; # no CRLF on this one
|
|
|
|
$message = sprintf( "%04X", length($message) + 4 ) . "$message";
|
2012-04-15 07:47:44 +02:00
|
|
|
|
|
|
|
http_print_headers();
|
|
|
|
print $service;
|
2012-04-18 02:56:18 +02:00
|
|
|
print "0000"; # flush-pkt, apparently
|
2012-04-15 07:47:44 +02:00
|
|
|
print $message;
|
|
|
|
print STDERR $service;
|
|
|
|
print STDERR $message;
|
2012-04-18 02:56:18 +02:00
|
|
|
exit 0; # if it's ok for die_webcgi in git.git/http-backend.c, it's ok for me ;-)
|
|
|
|
}
|
2012-04-15 07:47:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub http_simulate_ssh_connection {
|
|
|
|
# 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
|
2012-04-18 02:56:18 +02:00
|
|
|
if ( $ENV{PATH_INFO} =~ m(^/(.*)/(HEAD$|info/refs$|objects/|git-(?:upload|receive)-pack$)) ) {
|
2012-04-15 07:47:44 +02:00
|
|
|
my $repo = $1;
|
2012-04-18 02:56:18 +02:00
|
|
|
my $verb = ( $ENV{REQUEST_URI} =~ /git-receive-pack/ ) ? 'git-receive-pack' : 'git-upload-pack';
|
2012-04-15 07:47:44 +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
|
2012-04-18 02:56:18 +02:00
|
|
|
my ($verb) = ( $ENV{PATH_INFO} =~ m(^/(\S+)) );
|
2012-04-15 07:47:44 +02:00
|
|
|
my $args = $ENV{QUERY_STRING};
|
|
|
|
$args =~ s/\+/ /g;
|
|
|
|
$ENV{SSH_ORIGINAL_COMMAND} = $verb;
|
|
|
|
$ENV{SSH_ORIGINAL_COMMAND} .= " $args" if $args;
|
2012-04-18 02:56:18 +02:00
|
|
|
http_print_headers(); # in preparation for the eventual output!
|
2012-04-15 07:47:44 +02:00
|
|
|
}
|
|
|
|
$ENV{SSH_CONNECTION} = "$ENV{REMOTE_ADDR} $ENV{REMOTE_PORT} $ENV{SERVER_ADDR} $ENV{SERVER_PORT}";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $http_headers_printed = 0;
|
2012-04-18 02:56:18 +02:00
|
|
|
|
2012-04-15 07:47:44 +02:00
|
|
|
sub http_print_headers {
|
2012-04-18 02:56:18 +02:00
|
|
|
my ( $code, $text ) = @_;
|
2012-04-15 07:47:44 +02:00
|
|
|
|
|
|
|
return if $http_headers_printed++;
|
|
|
|
$code ||= 200;
|
|
|
|
$text ||= "OK - gitolite";
|
|
|
|
|
|
|
|
$|++;
|
|
|
|
print "Status: $code $text\r\n";
|
|
|
|
print "Expires: Fri, 01 Jan 1980 00:00:00 GMT\r\n";
|
|
|
|
print "Pragma: no-cache\r\n";
|
|
|
|
print "Cache-Control: no-cache, max-age=0, must-revalidate\r\n";
|
|
|
|
print "\r\n";
|
|
|
|
}
|