#! /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(
    argv               => \@ARGV,
    header             => "adding a server to a group",
    userAllowWildcards => 1,
    options            => {
        "group=s"          => \my $group,
        "protocol=s"       => \my $protocol,
        "force"            => \my $force,           # for subnets, and/or for servers that are down (no connection test)
        "force-key=s"      => \my $forceKey,
        "force-password=s" => \my $forcePassword,
        "ttl=s"            => \my $ttl,
        "comment=s"        => \my $comment,
        # undocumented/compatibility:
        "user-any" => \my $userAny,
        "port-any" => \my $portAny,
        "scpup"    => \my $scpUp,
        "scpdown"  => \my $scpDown,
        "sftp"     => \my $sftp,
    },
    helptext => <<'EOF',
Add an IP or IP block to a group's servers list

Usage: --osh SCRIPT_NAME --group GROUP --host HOST --user USER|* --port PORT|* [OPTIONS]

  --group GROUP            Specify which group this machine should be added to
  --host HOST|IP|SUBNET    Host(s) to add access to, either a HOST which will be resolved to an IP immediately,
                             or an IP, or a whole subnet using the PREFIX/SIZE notation
  --user USER|PATTERN|*    Specify which remote user should be allowed to connect as.
                             Globbing characters '*' and '?' are supported, so you can specify a pattern
                             that will be matched against the actual remote user name.
                             To allow any user, use '--user *' (you might need to escape '*' from your shell)
  --port PORT|*            Remote port allowed to connect to
                             To allow any port, use '--port *' (you might need to escape '*' from your shell)
  --protocol PROTO         Specify that a special protocol should be allowed for this HOST:PORT tuple, note that you
                              must not specify --user in that case. However, for this protocol to be usable under a given
                              remote user, access to the USER@HOST:PORT tuple must also be allowed.
                              PROTO must be one of:
                              scpup    allow SCP upload, you--bastion-->server
                              scpdown  allow SCP download, you<--bastion--server
                              sftp     allow usage of the SFTP subsystem, through the bastion
                              rsync    allow usage of rsync, through the bastion
  --force                  Don't try the ssh connection, just add the host to the group blindly
  --force-key FINGERPRINT  Only use the key with the specified fingerprint to connect to the server (cf groupInfo)
  --force-password HASH    Only use the password with the specified hash to connect to the server (cf groupListPasswords)
  --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.

Examples::

  --osh SCRIPT_NAME --group grp1 --host 203.0.113.0/24 --user '*' --port '*' --force --ttl 1d12h --comment '"a whole network"'
  --osh SCRIPT_NAME --group grp2 --host srv1.example.org --user data --port 22
  --osh SCRIPT_NAME --group grp2 --host srv1.example.org --user file --port 22

Example to allow using sftp to srv1.example.org using remote user 'data' or 'file', in addition to the above commands::

  --osh SCRIPT_NAME --group grp2 --host srv1.example.org --port 22 --protocol sftp
EOF
);

my $fnret;

if (not $group or not $ip) {
    help();
    osh_exit 'ERR_MISSING_PARAMETER',
      "Missing mandatory parameter 'host' or 'group' (or host didn't resolve correctly)";
}

$fnret = OVH::Bastion::Plugin::ACL::check(
    user     => $user,
    userAny  => $userAny,
    port     => $port,
    portAny  => $portAny,
    scpUp    => $scpUp,
    scpDown  => $scpDown,
    sftp     => $sftp,
    protocol => $protocol,
);
if (!$fnret) {
    help();
    osh_exit($fnret);
}
$user = $fnret->value->{'user'};
$port = $fnret->value->{'port'};

$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'};

if (defined $ttl) {
    $fnret = OVH::Bastion::is_valid_ttl(ttl => $ttl);
    $fnret or osh_exit $fnret;
    $ttl = $fnret->value->{'seconds'};
}

if ($forceKey && $forcePassword) {
    osh_exit 'ERR_INCOMPATIBLE_PARAMETERS', "Can't use --force-key and --force-password at the same time";
}

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'};
}

#
# Now do it
#

$fnret = OVH::Bastion::is_group_aclkeeper(account => $self, group => $shortGroup, superowner => 1);
$fnret
  or osh_exit 'ERR_NOT_GROUP_ACLKEEPER',
  "Sorry, you must be an aclkeeper of group $shortGroup to be able to add servers to it";

if (not $force) {
    $fnret = OVH::Bastion::ssh_test_access_way(
        group         => $group,
        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 };
push @command, ($group, '--', '/usr/bin/env', 'perl', '-T', $OVH::Bastion::BASEPATH . '/bin/helper/osh-groupAddServer');
push @command, '--group',          $group;
push @command, '--action',         'add';
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);
