the-bastion/bin/plugin/group-aclkeeper/groupDelServer

89 lines
2.9 KiB
Plaintext
Raw Normal View History

2020-10-16 00:32:37 +08:00
#! /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 );
2023-03-03 19:26:51 +08:00
use OVH::Bastion::Plugin::ACL;
2020-10-16 00:32:37 +08:00
my $remainingOptions = OVH::Bastion::Plugin::begin(
argv => \@ARGV,
header => "removing a server from a group",
options => {
"group=s" => \my $group,
"user-any" => \my $userAny,
"port-any" => \my $portAny,
"scpup" => \my $scpUp,
"scpdown" => \my $scpDown,
2023-03-03 19:26:51 +08:00
"sftp" => \my $sftp,
2020-10-16 00:32:37 +08:00
"force" => \my $force,
},
helptext => <<'EOF',
2023-03-03 19:26:51 +08:00
Remove an IP or IP block from a group's server list
2020-10-16 00:32:37 +08:00
Usage: --osh SCRIPT_NAME --group GROUP [OPTIONS]
--group GROUP Specify which group this machine should be removed from
--host HOST|IP|NET/CIDR Host(s) we want to remove access to
--user USER Remote user that was allowed, if any user was allowed, use --user-any
--user-any Use if any remote login was allowed
--port PORT Remote SSH port that was allowed, if any port was allowed, use --port-any
--port-any Use if any remote port was allowed
--scpup Remove SCP upload right, you--bastion-->server (omit --user in this case)
--scpdown Remove SCP download right, you<--bastion--server (omit --user in this case)
2023-03-03 19:26:51 +08:00
--sftp Remove usage of the SFTP subsystem, you<--bastion-->server (omit --user in this case)
2020-10-16 00:32:37 +08:00
EOF
);
my $fnret;
if (not $group or not $ip) {
help();
2022-06-30 21:00:29 +08:00
osh_exit 'ERR_MISSING_PARAMETER',
"Missing mandatory parameter 'host' or 'group' (or host didn't resolve correctly)";
2020-10-16 00:32:37 +08:00
}
2023-03-03 19:26:51 +08:00
$fnret = OVH::Bastion::Plugin::ACL::check(
user => $user,
userAny => $userAny,
port => $port,
portAny => $portAny,
scpUp => $scpUp,
scpDown => $scpDown,
sftp => $sftp
);
if (!$fnret) {
2020-10-16 00:32:37 +08:00
help();
2023-03-03 19:26:51 +08:00
osh_exit($fnret);
2020-10-16 00:32:37 +08:00
}
2023-03-03 19:26:51 +08:00
$user = $fnret->value->{'user'};
2020-10-16 00:32:37 +08:00
$fnret = OVH::Bastion::is_valid_group_and_existing(group => $group, groupType => "key");
$fnret or osh_exit($fnret);
# get returned untainted value
$group = $fnret->value->{'group'};
my $shortGroup = $fnret->value->{'shortGroup'};
#
# Now do it
#
$fnret = OVH::Bastion::is_group_aclkeeper(account => $self, group => $shortGroup, superowner => 1);
2022-06-30 21:00:29 +08:00
$fnret
or osh_exit 'ERR_NOT_GROUP_ACLKEEPER',
"Sorry, you must be an aclkeeper of group $shortGroup to be able to delete servers from it";
2020-10-16 00:32:37 +08:00
my @command = qw{ sudo -n -u };
push @command, ($group, '--', '/usr/bin/env', 'perl', '-T', $OVH::Bastion::BASEPATH . '/bin/helper/osh-groupAddServer');
push @command, '--group', $group;
push @command, '--action', 'del';
push @command, '--ip', $ip;
push @command, '--user', $user if $user;
push @command, '--port', $port if $port;
osh_exit OVH::Bastion::helper(cmd => \@command);