#!/usr/bin/perl -w
die
'Need $HOME to be set!'
unless
exists
(
$ENV
{HOME});
my
$config
= SReview::Config::Common::setup;
my
$eventname
=
$config
->get(
'event'
);
my
$action
=
"add"
;
my
$oknodo
= 0;
my
$help
= 0;
my
$keyfile
=
undef
;
my
$bindir
=
$ENV
{HOME} .
"/bin"
;
GetOptions(
"event|e=s"
=> \
$eventname
,
"action|a=s"
=> \
$action
,
"help"
=> \
$help
,
"oknodo|o"
=> \
$oknodo
,
"file|f=s"
=> \
$keyfile
,
) or pod2usage(
"command line invalid"
);
if
(
$help
) {
pod2usage(0);
}
if
(!
defined
(
$keyfile
)) {
pod2usage(
"key file not specified"
);
}
if
(
$action
ne
"add"
&&
$action
ne
"remove"
) {
print
STDERR
"Unknown action: $action\n"
;
exit
1;
}
my
$event
= SReview::Model::Event->new(
config
=>
$config
,
name
=>
$eventname
);
my
$akf
= Net::SSH::AuthorizedKeysFile->new();
my
$file
=
$config
->get(
'authkeyfile'
);
$akf
->
read
(
$file
);
open
KEY,
"<"
,
$keyfile
;
my
$mkey
=
""
;
while
(<KEY>) {
chomp
;
$mkey
.=
$_
;
}
close
KEY;
$mkey
= Net::SSH::AuthorizedKey->parse(
$mkey
);
my
@newkeys
= ();
foreach
my
$key
(
$akf
->
keys
()) {
if
(
$key
->fingerprint() eq
$mkey
->fingerprint()) {
if
(
$action
eq
"add"
) {
if
(!
$oknodo
) {
print
STDERR
"The provided key already exists in the file! Please remove it first\n"
;
exit
1;
}
else
{
print
"Key already added, ignoring\n"
;
exit
0;
}
}
else
{
next
;
}
}
push
@newkeys
,
$key
;
}
if
(
$action
eq
"add"
) {
if
(! -x
"$bindir/rrsync"
) {
print
STDERR
"E: please install rrsync as $bindir/rrsync, and make sure it's executable (hint: /usr/share/doc/rsync/scripts/rrsync.gz)"
;
exit
1;
}
my
$iglob
=
$config
->get(
'inputglob'
);
my
@input
=
split
(
'/'
,
$iglob
);
my
@dirs
= ();
foreach
my
$in
(
@input
) {
if
(
$in
=~ /\*/) {
last
;
}
push
@dirs
,
$in
;
}
$mkey
->option(
"command"
,
"$bindir/rrsync '"
.
join
('/
', @dirs, $event->inputdir) . "'
", 1);
$mkey
->option(
"no-agent-forwarding"
, 1, 1);
$mkey
->option(
"no-port-forwarding"
, 1, 1);
$mkey
->option(
"no-pty"
, 1, 1);
$mkey
->option(
"no-user-rc"
, 1, 1);
$mkey
->option(
"no-X11-forwarding"
, 1, 1);
push
@newkeys
,
$mkey
;
}
if
(!
defined
(
$file
)) {
$file
=
$akf
->path_locate;
}
$akf
= Net::SSH::AuthorizedKeysFile->new(
keys
=> \
@newkeys
,
file
=>
$file
);
$akf
->save();