the-bastion/bin/helper/osh-accountListIngressKeys

93 lines
2.4 KiB
Text
Raw Normal View History

2020-10-16 00:32:37 +08:00
#! /usr/bin/perl -T
# vim: set filetype=perl ts=4 sw=4 sts=4 et:
# NEEDGROUP osh-accountListIngressKeys
# SUDOERS %osh-accountListIngressKeys ALL=(keyreader) NOPASSWD:/usr/bin/env perl -T /opt/bastion/bin/helper/osh-accountListIngressKeys *
# FILEMODE 0750
# FILEOWN 0 keyreader
2020-10-16 00:32:37 +08:00
#>HEADER
use common::sense;
use Getopt::Long qw(:config no_auto_abbrev no_ignore_case);
2020-10-16 00:32:37 +08:00
use File::Basename;
use lib dirname(__FILE__) . '/../../lib/perl';
use OVH::Bastion;
2021-11-30 19:20:28 +08:00
use OVH::Bastion::Helper;
2020-10-16 00:32:37 +08:00
# Fetch command options
my $fnret;
my ($result, @optwarns);
my ($account, $allfiles);
2020-10-16 00:32:37 +08:00
eval {
local $SIG{__WARN__} = sub { push @optwarns, shift };
$result = GetOptions(
"account=s" => sub { $account //= $_[1] },
"all-files" => sub { $allfiles //= $_[1] },
);
2020-10-16 00:32:37 +08:00
};
if ($@) { die $@ }
if (!$result) {
local $" = ", ";
HEXIT('ERR_BAD_OPTIONS', msg => "Error parsing options: @optwarns");
}
OVH::Bastion::Helper::check_spurious_args();
2020-10-16 00:32:37 +08:00
if (!$account) {
HEXIT('ERR_MISSING_PARAMETER', msg => "Missing argument 'account'");
}
#<HEADER
#>RIGHTSCHECK
if ($self eq 'root') {
osh_debug "Real root, skipping checks of permissions";
}
else {
# need to perform another security check
2022-06-30 21:00:29 +08:00
$fnret = OVH::Bastion::is_user_in_group(
user => $self,
group => ($account eq 'root' ? "osh-rootListIngressKeys" : "osh-accountListIngressKeys")
);
2020-10-16 00:32:37 +08:00
if (!$fnret) {
HEXIT('ERR_SECURITY_VIOLATION', msg => "You're not allowed to run this, dear $self");
}
}
#<RIGHTSCHECK
#>PARAMS:ACCOUNT
osh_debug("Checking account");
my $accounthome;
if ($account ne 'root') {
$fnret = OVH::Bastion::is_bastion_account_valid_and_existing(account => $account);
$fnret or HEXIT($fnret);
$account = $fnret->value->{'account'}; # untainted
$accounthome = $fnret->value->{'dir'};
}
else {
$account = 'root';
$accounthome = '/root';
}
#<PARAMS:ACCOUNT
my @keys;
# by default, we only look in the authorized_keys file used by the bastion
my %files = ($accounthome . '/' . OVH::Bastion::AK_FILE => 1);
if ($allfiles) {
# if allfiles is requested, add all well-known potentially existing authorized_keys used by sshd
$files{"$accounthome/.ssh/authorized_keys"} = 1;
$files{"$accounthome/.ssh/authorized_keys2"} = 1;
}
foreach my $file (keys %files) {
2020-10-16 00:32:37 +08:00
$fnret = OVH::Bastion::get_authorized_keys_from_file(file => $file);
push @keys, @{$fnret->value} if ($fnret && $fnret->value);
}
HEXIT('OK', value => \@keys);