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-03-08 09:00:13 +01:00
|
|
|
use lib $ENV{GL_BINDIR};
|
|
|
|
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...
|
|
|
|
if ( exists $ENV{G3T_USER} ) {
|
|
|
|
in_local(); # file:// masquerading as ssh:// for easy testing
|
|
|
|
} elsif ( exists $ENV{SSH_CONNECTION} ) {
|
|
|
|
in_ssh();
|
|
|
|
} elsif ( exists $ENV{REQUEST_URI} ) {
|
|
|
|
in_http();
|
|
|
|
} else {
|
|
|
|
_die "who the *heck* are you?";
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|
|
|
|
|
|
|
|
exit 0;
|
2012-03-08 09:00:13 +01:00
|
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
# XXX lots of stuff from gl-auth-command is missing for now...
|
|
|
|
|
2012-03-12 16:24:30 +01:00
|
|
|
sub in_local {
|
2012-03-18 05:31:07 +01:00
|
|
|
if ($ENV{SSH_ORIGINAL_COMMAND} =~ /git-\w+-pack/) {
|
|
|
|
print STDERR "TRACE: gsh(", join( ")(", @ARGV ), ")\n";
|
|
|
|
print STDERR "TRACE: gsh(SOC=$ENV{SSH_ORIGINAL_COMMAND})\n";
|
|
|
|
}
|
2012-03-12 16:24:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub in_http {
|
|
|
|
_die 'http not yet implemented...';
|
|
|
|
}
|
|
|
|
|
|
|
|
sub in_ssh {
|
|
|
|
}
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
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-17 16:43:28 +01:00
|
|
|
gl_log( 'gitolite-shell', @ARGV, $ENV{SSH_ORIGINAL_COMMAND} );
|
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-17 16:43:28 +01:00
|
|
|
gl_log( 'gitolite-shell:new_wild_repo', $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.
|
|
|
|
my $ret = access( $repo, $user, $aa, 'any' );
|
2012-03-15 16:30:39 +01:00
|
|
|
trace( 1, "access($repo, $user, $aa, 'any')", "-> $ret" );
|
2012-03-17 16:43:28 +01:00
|
|
|
gl_log( 'gitolite-shell:check', $repo, $user, $aa, 'any', '->', $ret );
|
2012-03-12 16:24:30 +01:00
|
|
|
_die $ret if $ret =~ /DENIED/;
|
|
|
|
|
|
|
|
$repo = "'$rc{GL_REPO_BASE}/$repo.git'";
|
|
|
|
exec( "git", "shell", "-c", "$verb $repo" );
|
|
|
|
}
|
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-15 17:19:47 +01:00
|
|
|
if ( $soc =~ m(^(git-(?:upload|receive)-pack) '/?(.*?)(?:\.git(\d)?)?'$) ) {
|
2012-03-12 16:24:30 +01:00
|
|
|
# TODO git archive
|
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(\.\.$);
|
|
|
|
}
|