CHips L MINI SHELL

CHips L pro

Current Path : /proc/3/task/3/cwd/proc/3/root/scripts/
Upload File :
Current File : //proc/3/task/3/cwd/proc/3/root/scripts/clean_cgiemail

#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - scripts/clean_cgiemail                    Copyright 2017 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package scripts::clean_cgiemail;

use strict;
use warnings;

use Getopt::Long ();
use File::Spec   ();

use Cpanel::AccessIds                              ();
use Cpanel::Config::Users                          ();
use Cpanel::DomainLookup::DocRoot                  ();
use Cpanel::FindBin                                ();
use Cpanel::Logger                                 ();
use Cpanel::SafeRun::Object                        ();
use Cpanel::iContact::Class::CleanCgiemail::Notify ();

our $LOGFILE = '/var/cpanel/logs/clean_cgiemail_log';
my $logger;

sub main {
    my @argv = @_;

    my ( $rpm, $docroot, $user, $dryrun, $help, $notify );

    my %opts = (
        'rpm'          => \$rpm,
        'docroot'      => \$docroot,
        'user=s'       => \$user,
        'dryrun'       => \$dryrun,
        'help|usage|?' => \$help,
        'notify'       => \$notify,
    );

    Getopt::Long::GetOptionsFromArray( \@argv, %opts );

    if ($help) {
        usage();
        return 0;
    }

    if ( !( $rpm || $docroot ) ) {
        print "Must pass '--rpm' or '--docroot' options.\n\n";
        usage();
        return 1;
    }

    if ($rpm) {
        clean_rpms($dryrun);
    }

    if ($docroot) {
        clean_docroots( $user, $dryrun );
    }

    if ($notify) {
        Cpanel::iContact::Class::CleanCgiemail::Notify->new( logfile => $LOGFILE );
    }

    return 0;
}

sub clean_rpms {
    my ($dryrun) = @_;
    my $rpms = _get_installed_rpms();

    if ( @{$rpms} ) {
        _remove_rpms( $rpms, $dryrun );
    }
    else {
        _info("No RPMs found.");
    }

    return;
}

sub _get_installed_rpms {
    my ( $error_code, $out ) = _run_rpm( '-q', 'cpanel-cgiemail' );

    if ( $error_code == 0 ) {
        my @rpms = split( "\n", $out );
        return \@rpms;
    }

    return [];
}

sub _remove_rpms {
    my ( $rpms, $dryrun ) = @_;

    for my $rpm ( @{$rpms} ) {
        if ($dryrun) {
            _info("Will remove RPM: $rpm");
        }
        else {
            _info("Removing RPM: $rpm ... ");
            _remove_rpm($rpm);
        }
    }

    return;
}

sub _remove_rpm {
    my ($rpm) = @_;
    my ( $error_code, $out ) = _run_rpm( '-e', $rpm );

    if ( $error_code == 0 ) {
        _info("Success.");
    }
    else {
        chomp($out);
        _info("Failure: $out");
    }

    return;
}

sub _run_rpm {
    my @args    = @_;
    my $rpm_bin = Cpanel::FindBin::findbin('rpm');
    my $run     = Cpanel::SafeRun::Object->new(
        program => $rpm_bin,
        args    => [@args],
    );
    my $error_code = $run->error_code() || 0;
    my $out = $error_code == 0 ? $run->stdout : $run->stderr;
    return ( $error_code, $out );
}

sub clean_docroots {
    my ( $user, $dryrun ) = @_;
    my $found = 0;

    my @users = $user ? ($user) : ( Cpanel::Config::Users::getcpusers() );
    for my $user (@users) {
        $found += _remove_from_user( $user, $dryrun );
    }

    if ($found) {
        _info("Found $found scripts in user docroots.");
    }
    else {
        _info("No scripts found in user docroots.");
    }

    return;
}

sub _remove_from_user {
    my ( $user, $dryrun ) = @_;
    my @docroots = Cpanel::DomainLookup::DocRoot::getdocroots($user);

    return Cpanel::AccessIds::do_as_user(
        $user,
        sub {
            my $found = 0;
            for my $docroot (@docroots) {
                for my $file ( map { File::Spec->catfile( $docroot, 'cgi-bin', $_ ) } qw { cgiemail cgiecho } ) {
                    if ( -f $file ) {
                        $found++;
                        if ($dryrun) {
                            _info("Will remove file: $file");
                        }
                        else {
                            _info("Removing file: $file ... ");
                            if ( unlink $file ) {
                                _info("Success.");
                            }
                            else {
                                _info("Failure: $!");
                            }
                        }
                    }
                }
            }
            return $found;
        },
    );
}

sub _info {
    my ($message) = @_;
    $logger ||= Cpanel::Logger->new( { 'alternate_logfile' => $LOGFILE } );
    $logger->info($message);
    return;
}

sub usage {
    print <<USAGE;
clean_cgiemail

Clean up old cgiemail and cgiecho installations from the system and user accounts.

Options:

    --rpm            Clean installed cpanel cgiemail rpms.

    --docroot        Clean cgiemail scripts from user docroots. Cleans all users by default.

    --user=<user>    Clean only user specified when passed with the '--docroot' option.

    --dryrun         Do not actually commit any changes. Display what will be removed only.

    --notify         Send iContact notification when script runs.
USAGE
    return;
}

unless ( caller() ) {
    exit main(@ARGV);
}


Copyright 2K16 - 2K18 Indonesian Hacker Rulez