Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sub start_file_download
- {
- return "Bad Encryption Key" unless(decrypt_param(pop(@_)) eq "Encryption checking OK");
- my ($url, $destination, $filename, $action, $post_script) = &decrypt_params(@_);
- logger
- "Starting to download URL $url. Destination: $destination - Filename: $filename";
- if (!-e $destination)
- {
- logger "Creating destination directory.";
- if (!mkpath $destination )
- {
- logger "Could not create destination '$destination' directory : $!";
- return -2;
- }
- }
- my $download_file_path = Path::Class::File->new($destination, "$filename");
- my $pid = fork();
- if (not defined $pid)
- {
- logger "Could not allocate resources for download.";
- return -3;
- }
- # Only the forked child goes here.
- elsif ($pid == 0)
- {
- my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0,
- SSL_verify_mode => 0x00 } );
- $ua->agent('Mozilla/5.0');
- my $response = $ua->get($url, ':content_file' => "$download_file_path");
- if ($response->is_success)
- {
- logger "Successfully fetched $url and stored it to $download_file_path. Retval: ".$response->status_line;
- if (!-e $download_file_path)
- {
- logger "File $download_file_path does not exist.";
- exit(0);
- }
- if ($action eq "uncompress")
- {
- logger "Starting file uncompress as ordered.";
- uncompress_file_without_decrypt($download_file_path,
- $destination);
- }
- # Run post scripts if any
- if ($post_script ne "")
- {
- logger "1 Running postscript commands.";
- my @postcmdlines = split /[\r\n]+/, $post_script;
- my $postcmdfile = $destination."/".'postinstall.sh';
- open FILE, '>', $postcmdfile;
- print FILE "cd $destination\n";
- print FILE "while kill -0 $pid >/dev/null 2>&1\n";
- print FILE "do\n";
- print FILE " sleep 1\n";
- print FILE "done\n";
- foreach my $line (@postcmdlines) {
- print FILE "$line\n";
- logger "1 Postscript command received \"" . $line ."\".";
- }
- print FILE "rm -f $destination/postinstall.sh\n";
- close FILE;
- chmod 0755, $postcmdfile;
- my $screen_id = create_screen_id("post_script", $pid);
- my $cli_bin = create_screen_cmd($screen_id, "bash $postcmdfile");
- system($cli_bin);
- }
- }
- else
- {
- logger
- "Unable to fetch $url, or save to $download_file_path. Retval: ".$response->status_line;
- exit(0);
- }
- # Child process must exit.
- exit(0);
- }
- else
- {
- sleep 10;
- if ($post_script ne "")
- {
- logger "2 Running postscript commands.";
- my @postcmdlines = split /[\r\n]+/, $post_script;
- my $postcmdfile = $destination."/".'postinstall.sh';
- open FILE, '>', $postcmdfile;
- print FILE "cd $destination\n";
- print FILE "while kill -0 $pid >/dev/null 2>&1\n";
- print FILE "do\n";
- print FILE " sleep 1\n";
- print FILE "done\n";
- foreach my $line (@postcmdlines) {
- print FILE "$line\n";
- logger "2 Postscript command received \"" . $line ."\".";
- }
- print FILE "rm -f $destination/postinstall.sh\n";
- close FILE;
- chmod 0755, $postcmdfile;
- my $screen_id = create_screen_id("post_script", $pid);
- sudo_exec_without_decrypt("bash $postcmdfile");
- }
- logger "Download process for $download_file_path has pid number $pid.";
- return "$pid";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement