%PDF- %PDF-
| Direktori : /usr/share/l.v.e-manager/cpanel/lveversion/ |
| Current File : //usr/share/l.v.e-manager/cpanel/lveversion/cl-selector.cgi |
#!/bin/bash
eval 'if [ -x /usr/local/cpanel/3rdparty/bin/perl ]; then exec /usr/local/cpanel/3rdparty/bin/perl -x -- $0 ${1+"$@"}; else exec /usr/bin/perl -x -- $0 ${1+"$@"};fi'
if 0;
#!/usr/bin/perl
# CloudLinux LVE Manager VERSION:0.83
BEGIN { unshift @INC, "/usr/local/cpanel"; }
use strict;
use Cpanel::Form ();
use Cpanel::SafeRun ();
use Data::Dumper ();
use Cpanel::JSON ();
my %FORM = Cpanel::Form::parseform();
my $user = ($ENV{'TEAM_OWNER'} ? $ENV{'TEAM_OWNER'} : $ENV{'REMOTE_USER'})||$ARGV[0];
my $action = exists $FORM{action} ? $FORM{action} : 'extlist';
my %dispatchTable = (
extlist => \&getExtensionList,
optlist => \&getOptionList,
extsave => \&saveExtensionSet,
optsave => \&saveOptionSet,
extdefaults => \&extensionsToDefaults,
altappcreate => \&altappcreate,
altappdelete => \&altappdelete,
altappupdate => \&altappupdate,
altapprestart => \&altapprestart,
altappexecute => \&altappexecute,
pymodlist => \&pymodlist,
rbmodlist => \&rbmodlist,
pythonmodver => \&pymodver,
rubymodver => \&rbmodver,
);
$action = 'extlist' unless exists $dispatchTable{$action};
$dispatchTable{$action}->();
sub getExtensionList {
my %data;
my ( $version, $status, $message ) = ( $FORM{version}, 'OK', [] );
my @params = (
'/usr/bin/selectorctl',
'--list-user-extensions',
"--user=$user",
"--version=$version",
'--all',
);
my $result = Cpanel::SafeRun::Errors::saferunallerrors(@params);
for my $line ( split( /\n/, $result ) ) {
if ( index( $line, 'ERROR:' ) != -1) {
$status = 'ERROR';
push @{ $message }, substr( $line, 6 );
last;
}
if ( index( $line, 'WARN:' ) != -1) {
$status = 'WARN';
push @{ $message }, substr( $line, 5 );
next;
}
my ( $status, $extension ) = split /\s+/, $line;
$status = $status eq '+' ? 1 : 0;
$data{$extension} = $status;
}
my $output = {
status => $status,
data => [ map +{ title=>$_, status=>$data{$_} }, sort keys %data ],
};
$output->{message} = $message if @{ $message };
my $d = Data::Dumper->new([ $output ]);
print "Content-Type: application/json\n\n";
print $d->Terse(1)->Pair(':')->Useqq(1)->Deepcopy(1)->Dump;
}
sub getOptionList {
my ( $version, $status, $message ) = ( $FORM{version}, 'OK', [] );
my @params = (
'/usr/bin/selectorctl',
"--user=$user",
"--version=$version",
'--print-options',
'--json',
);
print "Content-Type: application/json\n\n";
print Cpanel::SafeRun::Errors::saferunallerrors(@params);
}
sub saveExtensionSet {
my ( $version, $extset, $status, $message ) = ( $FORM{version}, $FORM{extset}, 'OK', [] );
my %exthash = map { $_ => 1 } split( /,/, $extset );
if ( exists $FORM{version} ) {
$version = $FORM{version};
}
my @params = (
'/usr/bin/selectorctl',
'--interpreter=php',
"--replace-user-extensions=$extset",
"--user=$user",
"--version=$version"
);
my $result = Cpanel::SafeRun::Errors::saferunallerrors(@params);
if ( $result ) {
for my $line ( split( /\n/, $result ) ) {
if ( index( $line, 'ERROR:' ) != -1) {
$status = 'ERROR';
push @{ $message }, substr( $line, 6 );
last;
}
if ( index( $line, 'WARN:' ) != -1) {
$status = 'WARN';
push @{ $message }, substr( $line, 5 );
if ( $line =~ /WARN:(\S+)\s(\S+).*?(?:\((.*)\))?\.?$/ ) {
my ( $ext, $action, $arg ) = ( $1, $2, $3 );
if ( $action eq 'skipped' ) {
$exthash{$ext} = 0;
}
elsif ( $action eq 'enabled' ) {
$exthash{$ext} = 1
}
elsif ( $action eq 'left' ) {
$exthash{$ext} = 1
}
}
}
}
}
my $output = {
status => $status,
data => [ map +{ title=>$_, status=>$exthash{$_} }, sort keys %exthash ],
};
$output->{message} = $message if @{ $message };
my $d = Data::Dumper->new([ $output ]);
print "Content-Type: application/json\n\n";
print $d->Terse(1)->Pair(':')->Useqq(1)->Deepcopy(1)->Dump;
}
sub saveOptionSet {
my ( $version, $optset, $status, $message ) = ( $FORM{version}, $FORM{optset}, 'OK', [] );
my @params = (
'/usr/bin/selectorctl',
"--user=$user",
"--version=$version",
"--replace-options=$optset",
'--base64',
'--json'
);
print "Content-Type: application/json\n\n";
print Cpanel::SafeRun::Errors::saferunallerrors(@params);
}
sub extensionsToDefaults {
my %exthash = ();
my %buffer = ();
my ( $version, $status, $message ) = ( $FORM{version}, 'OK', [] );
my @params = (
'/usr/bin/selectorctl',
'--interpreter=php',
'--reset-user-extensions',
"--user=$user",
"--version=$version",
);
my $result = Cpanel::SafeRun::Errors::saferunallerrors(@params);
if ( $result ) {
for my $line ( split( /\n/, $result ) ) {
if ( index( $line, 'ERROR:' ) != -1) {
$status = 'ERROR';
push @{ $message }, substr( $line, 6 );
last;
}
if ( index( $line, 'WARN:' ) != -1) {
$status = 'WARN';
push @{ $message }, substr( $line, 5 );
if ( $line =~ /WARN:(\S+)\s(\S+).*?(?:\((.*)\))?\.?$/ ) {
my ( $ext, $action, $arg ) = ( $1, $2, $3 );
if ( $action eq 'skipped' ) {
$buffer{$ext} = 0;
}
elsif ( $action eq 'enabled' ) {
$buffer{$ext} = 1
}
elsif ( $action eq 'left' ) {
$buffer{$ext} = 1
}
}
next;
}
else {
%exthash = map { $_ => 1 } split( /,/, $line );
}
}
}
for my $ext ( keys %buffer ) {
if ( $buffer{$ext} == 1 ) {
$exthash{$ext} = 1;
}
elsif ( $buffer{$ext} == 0 ) {
delete $exthash{$ext} if exists $exthash{$ext};
}
}
my $output = {
status => $status,
data => [ map +{ title=>$_, status=>$exthash{$_} }, sort keys %exthash ],
};
$output->{message} = $message if @{ $message };
my $d = Data::Dumper->new([ $output ]);
print "Content-Type: application/json\n\n";
print $d->Terse(1)->Pair(':')->Useqq(1)->Deepcopy(1)->Dump;
}
sub altappcreate {
my ($app_path, $app_version, $app_uri, $alt, $domain) = ($FORM{path}, $FORM{version}, $FORM{uri}, $FORM{alt}, $FORM{domain});
my $data = {};
my @params = (
'/usr/bin/selectorctl',
'--interpreter', $alt,
'--user', $user);
if ( $domain ) {
push @params, ('--domain', $domain); # using --domain only if present in request data
};
push @params, (
'--version', $app_version,
'--print-summary',
'--json',
'--create-webapp',
$app_path, $app_uri);
return if(isAllowedText($app_path) == 0);
return if(isAllowedText($app_uri) == 0);
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK') {
for my $key ( keys %{ $rv->{data} } ) {
next unless $key eq $app_path;
$data->{data}->{path} = $key;
$data->{data}->{version} = $rv->{data}->{$key}->{interpreter}->{version};
$data->{data}->{uri} = $rv->{data}->{$key}->{alias};
$data->{data}->{domain} = $rv->{data}->{$key}->{domain};
if ($rv->{data}->{$key}->{domains}) {
$data->{data}->{domains} = $rv->{data}->{$key}->{domains};
}
$data->{data}->{activate} = "source $rv->{data}->{$key}->{interpreter}->{prefix}/bin/activate";
$data->{data}->{modules} = {
map {
$_=>$rv->{data}->{$key}->{extensions}->{$_}->{version}
} keys %{ $rv->{data}->{$key}->{extensions} }
};
}
@params = (
'/usr/bin/selectorctl',
'--interpreter', $alt,
'--list', '--json',
);
$rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
$rv = Cpanel::JSON::Load($rv);
my $versions = [sort keys %{$rv->{data}}];
$data->{data}->{interpreters} = $versions;
$data->{status} = 'OK';
}
else {
$data->{status} = lc $rv->{status};
$data->{message} = $rv->{message};
}
my $d = Data::Dumper->new([$data]);
print "Content-Type: application/json\n\n";
print $d->Terse(1)->Pair(':')->Useqq(1)->Deepcopy(1)->Dump;
}
sub altappdelete {
my ($app_path, $alt) = ($FORM{path}, $FORM{alt});
my $data = {};
my @params = (
'/usr/bin/selectorctl',
'--interpreter', $alt,
'--user', $user,
'--json',
'--destroy-webapp', $app_path,
);
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
chomp $rv;
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK') {
$data->{data}->{path} = $app_path;
$data->{status} = 'OK';
}
else {
$data = $rv;
}
my $d = Data::Dumper->new([$data]);
print "Content-Type: application/json\n\n";
print $d->Terse(1)->Pair(':')->Useqq(1)->Deepcopy(1)->Dump;
}
sub altapprestart {
my ($app_path, $alt) = ($FORM{path}, $FORM{alt});
my $data = {};
my @params = (
'/usr/bin/selectorctl',
'--interpreter', $alt,
'--user', $user,
'--json',
'--restart-webapp', $app_path,
);
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
chomp $rv;
$rv = Cpanel::JSON::Load($rv);
$rv->{status} = $rv->{status} eq 'OK' ? $rv->{status} : lc($rv->{status});
my $d = Data::Dumper->new([$rv]);
print "Content-Type: application/json\n\n";
print $d->Terse(1)->Pair(':')->Useqq(1)->Deepcopy(1)->Dump;
};
sub altappexecute {
chdir;
my ($app_path, $alt, $app_version, $cmd) = ($FORM{path}, $FORM{alt}, $FORM{version}, $FORM{cmd});
my @allowed_symb = (0..9, 'a'..'z', 'A'..'Z', ' ', '-', '_', '/', '.', ',', '"', '~', '>', '<');
my %json_output = ('status' => 'OK');
my @bad_symb = grep ( (index("@allowed_symb", $_) == -1), split('',$cmd));
if ( @bad_symb ) {
push_json( { "status" => "warn", 'message' => "Can not run command '$cmd'; disallowed symbols present: '@bad_symb' " } );
};
my $user_summary_raw = Cpanel::SafeRun::Errors::saferunallerrors("/usr/bin/selectorctl", "--interpreter=$alt", "--user-summary", "--json");
chomp $user_summary_raw;
my $user_summary = Cpanel::JSON::Load($user_summary_raw);
my $prefix;
if ( $user_summary->{status} eq 'OK' ) {
$prefix = $user_summary->{data}->{$app_path}->{interpreter}->{prefix};
} else {
push_json( { 'status' => 'ERROR', 'message' => $user_summary->{'message'} } );
}
my $activate_path = "$prefix/bin/activate";
my @params = ("/bin/cagefs_enter", "/bin/bash", "-c", "source $activate_path && $cmd");
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
if ( $? ) {
push_json( { 'status' => 'ERROR', 'message' => $rv } );
} else {
push_json( { 'status' => 'info', 'message' => 'Command execution succeeded' } );
}
}
sub altappupdate {
my $dump_dispatch = sub {
my ($info) = @_;
my $d = Data::Dumper->new([$info]);
print "Content-Type: application/json\n\n";
print $d->Terse(1)->Pair(':')->Useqq(1)->Deepcopy(1)->Dump;
};
my $data = {};
if (exists $FORM{'uri-curr'} and exists $FORM{'path-orig'}) {
my($domain, $uri) = split('/', $FORM{'uri-curr'}, 2);
if (!$uri) {$uri='/'};
return if(isAllowedText($uri) == 0);
my @params = (
'/usr/bin/selectorctl', '--interpreter', $FORM{alt},
'--user', $user);
if ( $domain ) {
push @params, ('--domain', $domain); # using --domain only if present in request data
};
push @params, (
'--print-summary',
'--json',
'--transit-webapp', $FORM{'path-orig'}, $uri);
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK' and exists $rv->{data}->{$FORM{'path-orig'}}) {
$data->{uri} = "$domain/" . $rv->{data}->{$FORM{'path-orig'}}->{alias};
if ($rv->{data}->{$FORM{'path-orig'}}->{domains}) {
$data->{domains} = $rv->{data}->{$FORM{'path-orig'}}->{domains};
}
}
else {
my $data_to_return = {status=>lc($rv->{status})};
$data_to_return->{message} = $rv->{message} if exists $rv->{message};
$data_to_return->{data} = $rv->{data} if exists $rv->{data};
$dump_dispatch->($data_to_return);
return;
}
}
if (exists $FORM{'version-curr'} and exists $FORM{'path-orig'}) {
my @params = ('/usr/bin/selectorctl',
'--interpreter', $FORM{alt},
'--user', $user,
'--print-summary', '--json',
'--version', $FORM{'version-curr'},
'--set-user-current',
$FORM{'path-orig'});
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK' and exists $rv->{data}->{$FORM{'path-orig'}}) {
my $activate_path = $rv->{data}->{$FORM{'path-orig'}}->{interpreter}->{prefix}.'/bin/activate';
$data->{version} = $rv->{data}->{$FORM{'path-orig'}}->{interpreter}->{version};
$data->{activate} = "source $activate_path";
}
else {
my $data_to_return = {status=>lc($rv->{status})};
$data_to_return->{message} = $rv->{message} if exists $rv->{message};
$data_to_return->{data} = $rv->{data} if exists $rv->{data};
for my $key (keys %{$data}){
$data_to_return->{data}->{$key} = $data->{$key};
}
$dump_dispatch->($data_to_return);
return;
}
}
if (exists $FORM{'wsgi-curr'} and exists $FORM{'path-orig'}) {
my @params = ('/usr/bin/selectorctl',
'--interpreter', $FORM{alt},
'--user', $user,
'--print-summary', '--json',
'--setup-wsgi', $FORM{'wsgi-curr'},
$FORM{'path-orig'});
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK' and exists $rv->{data}->{$FORM{'path-orig'}}) {
$data->{wsgi} = $rv->{data}->{$FORM{'path-orig'}}->{wsgi};
}
else {
my $data_to_return = {status=>lc($rv->{status})};
$data_to_return->{message} = $rv->{message} if exists $rv->{message};
$data_to_return->{data} = $rv->{data} if exists $rv->{data};
for my $key (keys %{$data}){
$data_to_return->{data}->{$key} = $data->{$key};
}
$dump_dispatch->($data_to_return);
return;
}
}
if (exists $FORM{remmod} and exists $FORM{'path-orig'}) {
my @params = ('/usr/bin/selectorctl',
'--interpreter', $FORM{alt},
'--user', $user,
'--print-summary', '--json',
'--disable-user-extensions', $FORM{remmod},
$FORM{'path-orig'});
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK') {
my @params = ('/usr/bin/selectorctl',
'--interpreter', $FORM{alt},
'--user', $user,
'--json', '--list-user-extensions', $FORM{'path-orig'});
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK') {
$data->{modules} = {map {$_=>$rv->{data}->{$_}->{version}} keys %{$rv->{data}}};
}
}
else {
my $data_to_return = {status=>lc($rv->{status})};
$data_to_return->{message} = $rv->{message} if exists $rv->{message};
$data_to_return->{data} = $rv->{data} if exists $rv->{data};
for my $key (keys %{$data}){
$data_to_return->{data}->{$key} = $data->{$key};
}
$dump_dispatch->($data_to_return);
return;
}
}
if (exists $FORM{addmod} and exists $FORM{'path-orig'}) {
my @params = ('/usr/bin/selectorctl',
'--interpreter', $FORM{alt},
'--user', $user,
'--print-summary', '--json',
'--enable-user-extensions', $FORM{addmod},
$FORM{'path-orig'});
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK') {
my @params = ('/usr/bin/selectorctl',
'--interpreter', $FORM{alt},
'--user', $user,
'--json', '--list-user-extensions', $FORM{'path-orig'});
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK') {
$data->{modules} = {map {$_=>$rv->{data}->{$_}->{version}} keys %{$rv->{data}}};
}
else {
$data->{modules} = {};
}
}
else {
my $data_to_return = {status=>lc($rv->{status})};
$data_to_return->{message} = $rv->{message} if exists $rv->{message};
$data_to_return->{data} = $rv->{data} if exists $rv->{data};
for my $key (keys %{$data}){
$data_to_return->{data}->{$key} = $data->{$key};
}
$dump_dispatch->($data_to_return);
return;
}
}
if (exists $FORM{'path-curr'} and exists $FORM{'path-orig'}) {
return if(isAllowedText($FORM{'path-curr'}) == 0);
my @params = ('/usr/bin/selectorctl',
'--user', $user,
'--interpreter', $FORM{alt},
'--print-summary', '--json',
'--relocate-webapp',
$FORM{'path-orig'}, $FORM{'path-curr'});
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK') {
my $activate_path = $rv->{data}->{$FORM{'path-curr'}}->{interpreter}->{prefix}.'/bin/activate';
my $homedir = (getpwnam($user))[7];
my @params = ('/bin/cp', '-r',
$homedir.'/'.$FORM{'path-orig'}.'/*',
$homedir.'/'.$FORM{'path-curr'});
system(@params);
$data->{path} = $FORM{'path-curr'};
$data->{activate} = "source $activate_path";
}
else {
my $data_to_return = {status=>lc($rv->{status})};
$data_to_return->{message} = $rv->{message} if exists $rv->{message};
$data_to_return->{data} = $rv->{data} if exists $rv->{data};
for my $key (keys %{$data}){
$data_to_return->{data}->{$key} = $data->{$key};
}
$dump_dispatch->($data_to_return);
return;
}
}
$dump_dispatch->({status=>'OK', data=>$data});
}
sub pymodlist {
my @params = (
'/usr/bin/selectorctl',
'--interpreter=python',
'--list-extensions',
'--json',
);
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
chomp $rv;
my $data;
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK') {
$data = {status=>'OK', data=>[sort { "\L$a" cmp "\L$b" } keys %{ $rv->{data} }]};
}
else {
$data = $rv;
}
my $d = Data::Dumper->new([$data]);
print "Content-Type: application/json\n\n";
print $d->Terse(1)->Pair(':')->Useqq(1)->Deepcopy(1)->Dump;
}
sub rbmodlist {
my @params = (
'/usr/bin/selectorctl',
'--interpreter=ruby',
'--list-extensions',
'--json',
);
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
chomp $rv;
my $data;
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK') {
$data = {status=>'OK', data=>[sort { "\L$a" cmp "\L$b" } keys %{ $rv->{data} }]};
}
else {
$data = $rv;
}
my $d = Data::Dumper->new([$data]);
print "Content-Type: application/json\n\n";
print $d->Terse(1)->Pair(':')->Useqq(1)->Deepcopy(1)->Dump;
}
sub pymodver {
my @params = (
'/usr/bin/selectorctl',
'--interpreter=python',
'--list-extensions-version', $FORM{'extension'},
'--json',
);
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
chomp $rv;
my $data;
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK') {
$data = {status=>'OK', data=>$rv->{data}};
}
else {
$data = $rv;
}
my $d = Data::Dumper->new([$data]);
print "Content-Type: application/json\n\n";
print $d->Terse(1)->Pair(':')->Useqq(1)->Deepcopy(1)->Dump;
}
sub rbmodver {
my @params = (
'/usr/bin/selectorctl',
'--interpreter=ruby',
'--list-extensions-version', $FORM{'extension'},
'--json',
);
my $rv = Cpanel::SafeRun::Errors::saferunallerrors(@params);
chomp $rv;
my $data;
$rv = Cpanel::JSON::Load($rv);
if ($rv->{status} eq 'OK') {
$data = {status=>'OK', data=>$rv->{data}};
}
else {
$data = $rv;
}
my $d = Data::Dumper->new([$data]);
print "Content-Type: application/json\n\n";
print $d->Terse(1)->Pair(':')->Useqq(1)->Deepcopy(1)->Dump;
}
sub get_user_domain {
my $path = shift;
my $user = getpwuid($<);
my $full_path = qq#$path/users/$user#;
my $dns;
return unless -e $full_path;
open my $f, '<', $full_path or return;
while (my $line = <$f>) {
if ($line =~ /^DNS\s?=\s?(\S*)/) {
$dns = $1;
last;
}
}
close $f;
return $dns;
}
sub isAllowedText() {
my $text = shift;
my $status = 1;
# symbols / - \w
if($text !~ /^[\w.\/-]+$/) {
print "Content-Type: application/json\n\n";
print Cpanel::JSON::Dump( {status => 'error', message => 'String ' . $text . ' contains not allowed symbols'} );
$status = 0;
}
return $status;
}
# return json data end finalization http session
sub push_json {
my $json_output_rel = shift;
print "Content-Type: application/json\n\n";
print Cpanel::JSON::Dump($json_output_rel);
exit;
};