mirror of
https://github.com/ovh/the-bastion.git
synced 2025-01-08 00:12:10 +08:00
135 lines
4.7 KiB
Perl
Executable file
135 lines
4.7 KiB
Perl
Executable file
#! /usr/bin/env perl
|
|
# vim: set filetype=perl ts=4 sw=4 sts=4 et:
|
|
use common::sense;
|
|
|
|
use File::Basename;
|
|
use lib dirname(__FILE__) . '/../../../lib/perl';
|
|
use OVH::Result;
|
|
use OVH::Bastion;
|
|
use OVH::Bastion::Plugin qw( :DEFAULT help );
|
|
use OVH::Bastion::Plugin::ACL;
|
|
|
|
my $remainingOptions = OVH::Bastion::Plugin::begin(
|
|
loadConfig => 1,
|
|
argv => \@ARGV,
|
|
header => "adding personal access to a server on your account",
|
|
options => {
|
|
"user-any" => \my $userAny,
|
|
"port-any" => \my $portAny,
|
|
"scpup" => \my $scpUp,
|
|
"scpdown" => \my $scpDown,
|
|
"sftp" => \my $sftp,
|
|
"force-key=s" => \my $forceKey,
|
|
"force-password=s" => \my $forcePassword,
|
|
"force" => \my $force,
|
|
"ttl=s" => \my $ttl,
|
|
"comment=s" => \my $comment,
|
|
},
|
|
helptext => <<'EOF',
|
|
Add a personal server access on your account
|
|
|
|
Usage: --osh SCRIPT_NAME --host HOST [OPTIONS]
|
|
|
|
--host IP|HOST|IP/MASK Server to add access to
|
|
--user USER Remote login to use, if you want to allow any login, use --user-any
|
|
--user-any Allow access with any remote login
|
|
--port PORT Remote SSH port to use, if you want to allow any port, use --port-any
|
|
--port-any Allow access to all remote ports
|
|
--scpup Allow SCP upload, you--bastion-->server (omit --user in this case)
|
|
--scpdown Allow SCP download, you<--bastion--server (omit --user in this case)
|
|
--sftp Allow usage of the SFTP subsystem, you<--bastion-->server (omit --user in this case)
|
|
--force Add the access without checking that the public SSH key is properly installed remotely
|
|
--force-key FINGERPRINT Only use the key with the specified fingerprint to connect to the server (cf selfListEgressKeys)
|
|
--force-password HASH Only use the password with the specified hash to connect to the server (cf selfListPasswords)
|
|
--ttl SECONDS|DURATION Specify a number of seconds (or a duration string, such as "1d7h8m") after which the access will automatically expire
|
|
--comment "'ANY TEXT'" Add a comment alongside this server. Quote it twice as shown if you're under a shell.
|
|
EOF
|
|
);
|
|
|
|
my $fnret;
|
|
|
|
if (!$ip) {
|
|
help();
|
|
osh_exit 'ERR_MISSING_PARAMETER', "Missing parameter 'host' or didn't resolve correctly";
|
|
}
|
|
|
|
$fnret = OVH::Bastion::Plugin::ACL::check(
|
|
user => $user,
|
|
userAny => $userAny,
|
|
port => $port,
|
|
portAny => $portAny,
|
|
scpUp => $scpUp,
|
|
scpDown => $scpDown,
|
|
sftp => $sftp
|
|
);
|
|
if (!$fnret) {
|
|
help();
|
|
osh_exit($fnret);
|
|
}
|
|
$user = $fnret->value->{'user'};
|
|
|
|
if (defined $ttl) {
|
|
$fnret = OVH::Bastion::is_valid_ttl(ttl => $ttl);
|
|
$fnret or osh_exit $fnret;
|
|
$ttl = $fnret->value->{'seconds'};
|
|
}
|
|
|
|
if ($forceKey) {
|
|
$fnret = OVH::Bastion::is_valid_fingerprint(fingerprint => $forceKey);
|
|
$fnret or osh_exit $fnret;
|
|
$forceKey = $fnret->value->{'fingerprint'};
|
|
}
|
|
|
|
if ($forcePassword) {
|
|
$fnret = OVH::Bastion::is_valid_hash(hash => $forcePassword);
|
|
$fnret or osh_exit $fnret;
|
|
$forcePassword = $fnret->value->{'hash'};
|
|
}
|
|
|
|
if ($forceKey && $forcePassword) {
|
|
osh_exit 'ERR_INCOMPATIBLE_PARAMETERS', "Can't use --force-key and --force-password at the same time";
|
|
}
|
|
|
|
if (not $force) {
|
|
$fnret = OVH::Bastion::ssh_test_access_way(
|
|
account => $self,
|
|
user => $user,
|
|
port => $port,
|
|
ip => $ip,
|
|
forceKey => $forceKey,
|
|
forcePassword => $forcePassword
|
|
);
|
|
if ($fnret->is_ok and $fnret->err ne 'OK') {
|
|
|
|
# we have something to say, say it
|
|
osh_info $fnret->msg;
|
|
}
|
|
elsif (not $fnret) {
|
|
osh_info "Note: if you still want to add this access even if it doesn't work, use --force";
|
|
osh_exit $fnret;
|
|
}
|
|
}
|
|
else {
|
|
osh_info "Forcing add as asked, we didn't test the SSH connection, maybe it won't work!";
|
|
}
|
|
|
|
# if no comment is specified, but we're adding the server by hostname,
|
|
# use it to craft a comment
|
|
if (!$comment && $host ne $ip) {
|
|
$comment = "hostname=$host";
|
|
}
|
|
|
|
my @command = qw{ sudo -n -u allowkeeper -- /usr/bin/env perl -T };
|
|
push @command, $OVH::Bastion::BASEPATH . '/bin/helper/osh-accountModifyPersonalAccess';
|
|
push @command, '--target', 'self';
|
|
push @command, '--action', 'add';
|
|
push @command, '--account', $self;
|
|
push @command, '--ip', $ip;
|
|
push @command, '--user', $user if $user;
|
|
push @command, '--port', $port if $port;
|
|
push @command, '--force-key', $forceKey if $forceKey;
|
|
push @command, '--force-password', $forcePassword if $forcePassword;
|
|
push @command, '--ttl', $ttl if $ttl;
|
|
push @command, '--comment', $comment if $comment;
|
|
|
|
osh_exit OVH::Bastion::helper(cmd => \@command);
|