#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - modcpuser Copyright 2010 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
use Cpanel::Sys ();
use Cpanel::PwCache ();
use Cpanel::Config::CpUserGuard ();
my %OPTS;
my %ARRAY_KEYS = ( 'value' => 1 );
my %ACTIONS = (
'adddomain' => ['value'],
'deldomain' => ['value'],
'unset' => [ 'key', 'value' ],
'set' => [ 'key', 'value' ],
'dump' => [],
);
for ( my $i = 0; $i < $#ARGV; $i++ ) {
my $arg = $ARGV[$i];
if ( $arg =~ /^\-+/ ) {
$arg =~ s/^\-+//g;
if ( exists $ARRAY_KEYS{$arg} ) {
push @{ $OPTS{$arg} }, $ARGV[ ++$i ];
}
else {
$OPTS{$arg} = $ARGV[ ++$i ];
}
}
}
_clean_opts( \%OPTS );
_validate_opts( \%OPTS );
my $cpuser_guard = Cpanel::Config::CpUserGuard->new( $OPTS{'user'} );
my $cpuser_data = $cpuser_guard->{'data'};
_validate_action( $OPTS{'action'}, $cpuser_data );
my @validdomains;
my @invaliddomains;
if ( $OPTS{'help'} ) {
usage();
exit 1;
}
elsif ( $OPTS{'action'} eq 'dump' ) {
no warnings 'once';
require Data::Dumper;
print "cPanel user files for: $OPTS{'user'}:\n\n";
local $Data::Dumper::Quotekeys = 0;
local $Data::Dumper::Pair = ' = ';
local $Data::Dumper::Indent = 1;
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Sortkeys = 1;
print Data::Dumper::Dumper($cpuser_data);
exit;
}
elsif ( $OPTS{'action'} eq 'set' ) {
$cpuser_data->{ $OPTS{'key'} } = $OPTS{'value'}->[0];
$cpuser_guard->save();
print "$OPTS{'key'} has been set to $OPTS{'value'}->[0] for $OPTS{'user'}\n";
exit;
}
elsif ( $OPTS{'action'} eq 'unset' ) {
delete $cpuser_data->{ $OPTS{'key'}->[0] };
$cpuser_guard->save();
print "$OPTS{'key'} has been unset for $OPTS{'user'}\n";
exit;
}
elsif ( $OPTS{'action'} eq 'adddomain' ) {
my @validdomains;
foreach my $value ( @{ $OPTS{'value'} } ) {
if ( !grep( /^\Q$value\E$/, @{ $cpuser_data->{'DOMAINS'} } ) ) {
if ( Cpanel::Sys::validdomainname( $value, 1 ) ) {
push @{ $cpuser_data->{'DOMAINS'} }, $value;
push @validdomains, $value;
}
else {
push @invaliddomains, $value;
}
}
@{ $cpuser_data->{'DEADDOMAINS'} } = grep( !/^\Q$value\E/, @{ $cpuser_data->{'DEADDOMAINS'} } );
}
$cpuser_guard->save();
if (@validdomains) {
print "The domain(s): [" . join( ',', @validdomains ) . "] have been added to $OPTS{'user'}\n";
}
if (@invaliddomains) {
print "The domain(s): [" . join( ',', @invaliddomains ) . "] where are invalid and could not be added to $OPTS{'user'}\n";
exit(2);
}
exit;
}
elsif ( $OPTS{'action'} eq 'deldomain' ) {
foreach my $value ( @{ $OPTS{'value'} } ) {
if ( !grep( /^\Q$value\E$/, @{ $cpuser_data->{'DEADDOMAINS'} } ) ) {
if ( Cpanel::Sys::validdomainname( $value, 1 ) ) {
push @{ $cpuser_data->{'DEADDOMAINS'} }, $value;
push @validdomains, $value;
}
else {
push @invaliddomains, $value;
}
}
@{ $cpuser_data->{'DOMAINS'} } = grep( !/^\Q$value\E/, @{ $cpuser_data->{'DOMAINS'} } );
}
$cpuser_guard->save();
if (@validdomains) {
print "The domain(s): [" . join( ',', @validdomains ) . "] have been removed from $OPTS{'user'}\n";
}
if (@invaliddomains) {
print "The domain(s): [" . join( ',', @invaliddomains ) . "] where are invalid, all refrences to them have been from $OPTS{'user'}\n";
exit(2);
}
exit;
}
usage();
exit 1;
sub _clean_opts {
my $opref = shift;
if ( exists $opref->{'user'} ) { $opref->{'user'} =~ s/\///g; }
foreach my $opt ( keys %$opref ) {
if ( ref $opref->{$opt} ) {
$opref->{$opt} = [ map { $_ =~ s/[\r\n]//g; $_; } @{ $opref->{$opt} } ];
}
else {
$opref->{$opt} =~ s/[\r\n]//g;
}
}
}
sub _validate_opts {
my $opref = shift;
if ( !$opref->{'user'} ) {
print "$0: a user (--user) is required.\n\n";
usage();
exit 1;
}
elsif ( !( Cpanel::PwCache::getpwnam( $opref->{'user'} ) )[0] || !-e '/var/cpanel/users/' . $opref->{'user'} ) {
print "$0: $opref->{'user'} is not a valid cPanel user.\n\n";
usage();
exit 1;
}
elsif ( !$opref->{'action'} ) {
print "$0: an action (--action) is required.\n\n";
usage();
exit 1;
}
elsif ( !exists $ACTIONS{ $opref->{'action'} } ) {
print "$0: action (--action) $opref->{'action'} is not a valid action.\n\n";
usage();
exit 1;
}
else {
foreach my $key ( @{ $ACTIONS{ $opref->{'action'} } } ) {
if ( !exists $opref->{$key} ) {
print "$0: $key (--$key) is required for the $opref->{'action'} action.\n\n";
usage();
exit 1;
}
}
}
if ( $OPTS{'action'} eq 'set' || $OPTS{'action'} eq 'unset' ) {
if ( $#{ $OPTS{'value'} } > 0 ) {
print "$0: only one value one value must be specified when using set or unset.\n\n";
usage();
exit 1;
}
}
}
sub _validate_action {
my $action = shift;
my $cpuserref = shift;
if ( ( $action eq 'set' || $action eq 'unset' ) && ref $cpuserref->{ $OPTS{'key'} } ) {
print "$0: $OPTS{'key'} cannot be set or unset.\n\n";
usage();
exit 1;
}
}
sub usage {
print <<EO_USAGE;
Usage:
modcpuser --user (user) --action (set|unset) --key ? --value ?
modcpuser --user (user) --action (adddomain|deldomain) --value ? [--value ? --value ? ...]
modcpuser --user (user) --action dump
Options:
--user Brief help message
--action Action to carry out
adddomain = Add a domain to the users cPanel configuration
deldomain = Remove a domain from the users cPanel configuration
set = set a key value in the users cPanel configuration
unset = unset a key value in the users cPanel configuration
dump = display the user's current cPanel configuration
--key Used only with 'set' or 'unset' , examples are MAXPOP, MAXFTP
--value When setting,unsetting,adding, or deleting this is the value to be modified.
Examples:
Setting a users main domain
modcpuser --user nick --action set --key DOMAIN --value mydomain.org
Changing the max allowed email accounts
modcpuser --user nick --action set --key MAXPOP --value 12
modcpuser --user nick --action set --key MAXPOP --value unlimited
Add a parked, sub, or addon domain to the datastore
(this should only be done when it is missing as it will not setup
the http userdata, or config)
modcpuser --user nick --action adddomain --value parked1.org \\
--value parked2.org --value sub1.maindomain.org
EO_USAGE
exit 1;
}
Copyright 2K16 - 2K18 Indonesian Hacker Rulez