unified the classification of the passed command for all shell apis

As discussed on the pve-devel list [0] I extracted the identical chunks from each
shell api into one function as basis for future changes.

[0] https://pve.proxmox.com/pipermail/pve-devel/2019-January/035387.html

Signed-off-by: Tim Marx <t.marx@proxmox.com>
This commit is contained in:
Tim Marx 2019-02-27 15:01:16 +01:00 committed by Thomas Lamprecht
parent f9d26e098f
commit d03d7e1eee

View File

@ -701,6 +701,25 @@ __PACKAGE__->register_method({
my $sslcert; my $sslcert;
my $cmdMapper = {
'login' => [ '/bin/login', '-f', 'root' ],
'upgrade' => [ '/usr/bin/pveupgrade', '--shell' ],
};
sub get_shell_command {
my ($user, $shellcmd) = @_;
if ($user eq 'root@pam') {
if (exists($cmdMapper->{$shellcmd})) {
return $cmdMapper->{$shellcmd};
} else {
return [ '/bin/login', '-f', 'root' ];
}
} else {
return [ '/bin/login' ];
}
}
__PACKAGE__->register_method ({ __PACKAGE__->register_method ({
name => 'vncshell', name => 'vncshell',
path => 'vncshell', path => 'vncshell',
@ -721,6 +740,13 @@ __PACKAGE__->register_method ({
optional => 1, optional => 1,
default => 0, default => 0,
}, },
cmd => {
type => 'string',
description => "Run specific command or default to login.",
enum => [keys %$cmdMapper],
optional => 1,
default => 'login',
},
websocket => { websocket => {
optional => 1, optional => 1,
type => 'boolean', type => 'boolean',
@ -787,19 +813,10 @@ __PACKAGE__->register_method ({
my $remcmd = $remip ? my $remcmd = $remip ?
['/usr/bin/ssh', '-e', 'none', '-t', $remip] : []; ['/usr/bin/ssh', '-e', 'none', '-t', $remip] : [];
my $shcmd;
if ($user eq 'root@pam') {
if ($param->{upgrade}) { if ($param->{upgrade}) {
my $upgradecmd = "pveupgrade --shell"; $param->{cmd} = 'upgrade';
$upgradecmd = PVE::Tools::shellquote($upgradecmd) if $remip;
$shcmd = [ '/bin/bash', '-c', $upgradecmd ];
} else {
$shcmd = [ '/bin/login', '-f', 'root' ];
}
} else {
$shcmd = [ '/bin/login' ];
} }
my $shcmd = get_shell_command($user, $param->{cmd});
my $timeout = 10; my $timeout = 10;
@ -880,6 +897,13 @@ __PACKAGE__->register_method ({
optional => 1, optional => 1,
default => 0, default => 0,
}, },
cmd => {
type => 'string',
description => "Run specific command or default to login.",
enum => [keys %$cmdMapper],
optional => 1,
default => 'login',
},
}, },
}, },
returns => { returns => {
@ -919,17 +943,10 @@ __PACKAGE__->register_method ({
my $remcmd = $remip ? my $remcmd = $remip ?
['/usr/bin/ssh', '-e', 'none', '-t', $remip , '--'] : []; ['/usr/bin/ssh', '-e', 'none', '-t', $remip , '--'] : [];
my $concmd;
if ($user eq 'root@pam') {
if ($param->{upgrade}) { if ($param->{upgrade}) {
$concmd = [ '/usr/bin/pveupgrade', '--shell' ]; $param->{cmd} = 'upgrade';
} else {
$concmd = [ '/bin/login', '-f', 'root' ];
}
} else {
$concmd = [ '/bin/login' ];
} }
my $shcmd = get_shell_command($user, $param->{cmd});
my $realcmd = sub { my $realcmd = sub {
my $upid = shift; my $upid = shift;
@ -938,7 +955,7 @@ __PACKAGE__->register_method ({
my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath, my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath,
'--perm', 'Sys.Console', '--']; '--perm', 'Sys.Console', '--'];
push @$cmd, @$remcmd, @$concmd; push @$cmd, @$remcmd, @$shcmd;
PVE::Tools::run_command($cmd); PVE::Tools::run_command($cmd);
}; };
@ -1027,6 +1044,13 @@ __PACKAGE__->register_method ({
optional => 1, optional => 1,
default => 0, default => 0,
}, },
cmd => {
type => 'string',
description => "Run specific command or default to login.",
enum => [keys %$cmdMapper],
optional => 1,
default => 'login',
},
}, },
}, },
returns => get_standard_option('remote-viewer-config'), returns => get_standard_option('remote-viewer-config'),
@ -1048,18 +1072,10 @@ __PACKAGE__->register_method ({
my $authpath = "/nodes/$node"; my $authpath = "/nodes/$node";
my $permissions = 'Sys.Console'; my $permissions = 'Sys.Console';
my $shcmd;
if ($user eq 'root@pam') {
if ($param->{upgrade}) { if ($param->{upgrade}) {
my $upgradecmd = "pveupgrade --shell"; $param->{cmd} = 'upgrade';
$shcmd = [ '/bin/bash', '-c', $upgradecmd ];
} else {
$shcmd = [ '/bin/login', '-f', 'root' ];
}
} else {
$shcmd = [ '/bin/login' ];
} }
my $shcmd = get_shell_command($user, $param->{cmd});
my $title = "Shell on '$node'"; my $title = "Shell on '$node'";