2011-08-12 18:40:12 +02:00
|
|
|
#!/usr/bin/perl -w
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
# help/instructions are at the bottom, in the __DATA__ section
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
use FindBin;
|
|
|
|
BEGIN { $ENV{GL_BINDIR} = $FindBin::Bin; }
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
use lib $ENV{GL_BINDIR};
|
|
|
|
use gitolite_rc;
|
|
|
|
use gitolite;
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
sub usage { print <DATA>; exit 1; }
|
|
|
|
usage() unless (@ARGV);
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
my $cmd = shift;
|
|
|
|
my $pub = shift;
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
if ($cmd eq 'add-shell-user' or $cmd eq 'add-mirroring-peer') {
|
|
|
|
# sanity checks
|
|
|
|
$pub or usage();
|
|
|
|
my $user = validate_pubkeyfile($pub);
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
# write the file out, with the new authkeys line added just *before* the
|
|
|
|
# gitolite section. But first, set the command that gets invoked
|
|
|
|
$cmd = ( $cmd eq 'add-shell-user' ? 'gl-auth-command -s' : 'gl-mirror-shell' );
|
|
|
|
ak_insert($cmd, $user, $pub);
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
exit 0;
|
|
|
|
}
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
die "could not understand command $cmd\n";
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
sub validate_pubkeyfile {
|
|
|
|
my $pub = shift;
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
-f $pub or die "$pub does not exist\n";
|
|
|
|
die "$pub contains more than one line\n" if wc_l($pub) > 1;
|
|
|
|
|
|
|
|
my $user = $pub;
|
|
|
|
$user =~ s(^.*/)(); # remove optional directory
|
|
|
|
die "file name must end in .pub\n" unless $user =~ /(.*)\.pub$/;
|
|
|
|
$user = $1;
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ak_insert {
|
|
|
|
my ($cmd, $user, $pub) = @_;
|
2010-04-09 17:22:32 +02:00
|
|
|
|
|
|
|
# must be kept consistent with what's in src/gl-compile-conf; on the plus
|
|
|
|
# side, it's not likely to change anytime soon!
|
2011-08-12 18:40:12 +02:00
|
|
|
my $AUTH_OPTIONS = "no-port-forwarding,no-X11-forwarding,no-agent-forwarding";
|
|
|
|
|
|
|
|
my $authline = "command=\"$ENV{GL_BINDIR}/$cmd $user\",$AUTH_OPTIONS " . slurp($pub);
|
|
|
|
|
|
|
|
my $authkeys = "$ENV{HOME}/.ssh/authorized_keys";
|
|
|
|
my $ak_lines = slurp($authkeys);
|
|
|
|
$ak_lines =~ s/^.*$cmd $user.*\n//m; # remove existing keyline, if present
|
|
|
|
$ak_lines =~ s/^# gitolite start/$authline# gitolite start/m;
|
|
|
|
my $akfh = wrap_open(">", $authkeys);
|
|
|
|
print $akfh $ak_lines;
|
|
|
|
close $akfh;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub wc_l {
|
|
|
|
my $fh = wrap_open("<", shift);
|
|
|
|
my @l = <$fh>;
|
|
|
|
my $l = @l;
|
|
|
|
return $l;
|
|
|
|
}
|
|
|
|
|
|
|
|
__DATA__
|
|
|
|
|
|
|
|
gl-tool -- make some server side tasks easier
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
gl-tool [sub-command [args]]
|
|
|
|
|
|
|
|
Security notes: this program does not do any sanitisation of input. You're
|
|
|
|
running it at the CLI on the server, so you already have the power to do
|
|
|
|
whatever you want anyway.
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
current sub-commands:
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
(1) REPLACE THE OLD $SHELL_USERS MECHANISM
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
gl-tool add-shell-user foo.pub
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
Adds the pubkey in foo.pub into the authkeys file with "-s" argument (shell
|
|
|
|
access) and user "foo". The line will be added *before* the "# gitolite
|
|
|
|
start" section, so that a gitolite-admin push will not affect it.
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
Although there is no "remove-shell-user" sub-command, you can do that quite
|
|
|
|
easily by editing ~/.ssh/authorized_keys and deleting the appropriate line.
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
(2) ADD A MIRRORING PEER KEY
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
gl-tool add-mirroring-peer git@server.company.com.pub
|
2010-04-09 17:22:32 +02:00
|
|
|
|
2011-08-12 18:40:12 +02:00
|
|
|
As above, but the given key will invoke 'gl-mirror-shell' instead of the
|
|
|
|
usual 'gl-auth-command'. This is meant to be a server-to-server key, allowing
|
|
|
|
(in this example), the gitolite server called 'git@server.company.com' to
|
|
|
|
access this server for mirroring operations.
|