Server IP : 192.168.23.10  /  Your IP : 216.73.216.48
Web Server : Apache
System : Linux echo.premieradvertising.com 5.14.0-362.8.1.el9_3.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Nov 7 14:54:22 EST 2023 x86_64
User : rrrallyteam ( 1049)
PHP Version : 8.1.34
Disable Function : exec,passthru,shell_exec,system
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF
Directory (0755) :  /sys/../../proc/37/../93/../../media/../boot/../dev/shm/../snd/../pts/../../scripts/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : //sys/../../proc/37/../93/../../media/../boot/../dev/shm/../snd/../pts/../../scripts/process_cpmove
#!/usr/local/cpanel/3rdparty/bin/perl

#                                      Copyright 2025 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited.

=encoding utf-8

=head1 NAME

process_cpmove - Wrapper script for cPanel & WHM's restorepkg binary

=head1 SYNOPSIS

    process_cpmove [--help] [--username <username>] [restorepkg options]

=head1 DESCRIPTION

This script provides an easy to call wrapper for cPanel & WHM's restorepkg binary with the specific settings
needed for Comet Backup's agent. It reads a tar archive from stdin, saves it to a temporary
location, and then calls restorepkg to restore the cPanel & WHM specific account settings.

Make sure to call this script AFTER restoring the user's home directory and other files from Comet Backup.

All other arguments are passed through to restorepkg. See C<restorepkg --help> for available options.

=head1 OPTIONS

=over 4

=item B<--help>

Display this help message and exit.

=item B<--username> <username>

B<Required>. Specify the username to restore the account as. This will be passed to restorepkg as C<--newuser>.

=back

=head1 EXAMPLES

    # Restore a backup from stdin
    cat /path/to/backup.tar | process_cpmove --username user

    # Restore with additional restorepkg options
    cat /path/to/backup.tar | process_cpmove --username user --keep_local_cpuser_values PLAN,DNS

=cut

package scripts::process_cpmove;

use cPstrict;

use Getopt::Long ();
use Pod::Usage   ();
use File::Copy   ();
use File::Temp   ();

use FindBin;
use lib "$FindBin::Bin/../plugins/whm/comet/perl/usr/local/cpanel";

run(@ARGV) if !caller();

sub run {
    my (@args) = @_;

    my $help;
    my $username;
    Getopt::Long::Configure('pass_through');
    Getopt::Long::GetOptionsFromArray(
        \@args,
        'help|h|?'   => \$help,
        'username=s' => \$username,
    ) or Pod::Usage::pod2usage(2);

    if ($help) {
        Pod::Usage::pod2usage(
            -exitval => 0,
            -verbose => 2,
        );
    }

    if ( !defined $username || !length $username ) {
        die "Error: --username is required\n";
    }

    my $restorepkg_bin = '/usr/local/cpanel/bin/restorepkg';

    if ( !-x $restorepkg_bin ) {
        die "Error: restorepkg binary not found or not executable at $restorepkg_bin\n";
    }

    # Create a temporary file to store the incoming cpmove archive
    my $temp_fh = File::Temp->new(
        TEMPLATE => 'cpmove_XXXXXX',
        SUFFIX   => '.tar',
        TMPDIR   => 1,
        UNLINK   => 1,
    );

    my $temp_file = $temp_fh->filename();

    if ( !defined $temp_file ) {
        die "Error: Failed to create temporary file\n";
    }

    # Read from STDIN and write to the temporary file
    binmode STDIN;
    binmode $temp_fh;

    my $buffer;
    my $bytes_written = 0;
    while ( my $bytes_read = read( STDIN, $buffer, 65536 ) ) {
        print {$temp_fh} $buffer or die "Error: Failed to write to temporary file: $!\n";
        $bytes_written += $bytes_read;
    }

    if ( !defined $buffer ) {
        die "Error: Failed to read from STDIN: $!\n";
    }

    # Flush and close the filehandle to ensure all data is written
    close $temp_fh or die "Error: Failed to close temporary file: $!\n";

    if ( $bytes_written == 0 ) {
        die "Error: No data received from STDIN\n";
    }

    # Call restorepkg with the temporary file
    # We use system() instead of exec() to ensure the temp file isn't cleaned up
    # until after restorepkg completes
    my @cmd = ( $restorepkg_bin, $temp_file, '--newuser', $username, @args );

    my $exit_code = system(@cmd);

    if ( $exit_code == -1 ) {
        die "Error: Failed to execute restorepkg: $!\n";
    }
    elsif ( $exit_code & 127 ) {
        my $signal = $exit_code & 127;
        die sprintf( "Error: restorepkg died with signal %d\n", $signal );
    }
    else {
        my $status = $exit_code >> 8;
        if ( $status != 0 ) {
            die sprintf( "Error: restorepkg exited with status %d\n", $status );
        }
    }

    # Temp file will be automatically cleaned up when $temp_fh goes out of scope
    return 0;
}

1;