1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#!/usr/local/bin/perl
use strict;
use warnings;
use Term::ANSIColor;
use constant HOME_DIR => '/usr/local/git/';
sub main
{
my $argc = $#ARGV + 1;
if ($argc < 2) {
print colored("Failed!\n", 'bold red')
. "Missing argument, 2 needed "
. colored("[user - ssh public key]", 'bold')
. "\n";
exit 1;
}
my $usr = $ARGV[0];
my $sshkey = "no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ";
$sshkey = $sshkey . $ARGV[1];
my $home_dir = HOME_DIR . $usr . '/';
system(
"adduser << EOF
" . $usr . "
git-shell
" . HOME_DIR . $usr . "
yes
yes
no
EOF"
);
my (undef, undef, $uid, $gid) = getpwnam($usr);
mkdir $home_dir . '.ssh/', 0700;
chown $uid, $gid, $home_dir . '.ssh/';
open(my $fh, '>:encoding(UTF-8)', $home_dir . '.ssh/authorized_keys');
print $fh $sshkey . "\n";
close($fh);
chown $uid, $gid, $home_dir . '.ssh/authorized_keys';
chmod 0600, $home_dir . '.ssh/authorized_keys';
print "Created new git user " . colored($usr, 'bold green') . ".\n";
exit;
}
main();
__END__
|