Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. sub start_file_download
  2. {
  3. return "Bad Encryption Key" unless(decrypt_param(pop(@_)) eq "Encryption checking OK");
  4. my ($url, $destination, $filename, $action, $post_script) = &decrypt_params(@_);
  5. logger
  6. "Starting to download URL $url. Destination: $destination - Filename: $filename";
  7.  
  8. if (!-e $destination)
  9. {
  10. logger "Creating destination directory.";
  11. if (!mkpath $destination )
  12. {
  13. logger "Could not create destination '$destination' directory : $!";
  14. return -2;
  15. }
  16. }
  17.  
  18. my $download_file_path = Path::Class::File->new($destination, "$filename");
  19.  
  20. my $pid = fork();
  21. if (not defined $pid)
  22. {
  23. logger "Could not allocate resources for download.";
  24. return -3;
  25. }
  26.  
  27. # Only the forked child goes here.
  28. elsif ($pid == 0)
  29. {
  30. my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0,
  31. SSL_verify_mode => 0x00 } );
  32. $ua->agent('Mozilla/5.0');
  33. my $response = $ua->get($url, ':content_file' => "$download_file_path");
  34.  
  35. if ($response->is_success)
  36. {
  37. logger "Successfully fetched $url and stored it to $download_file_path. Retval: ".$response->status_line;
  38.  
  39. if (!-e $download_file_path)
  40. {
  41. logger "File $download_file_path does not exist.";
  42. exit(0);
  43. }
  44.  
  45. if ($action eq "uncompress")
  46. {
  47. logger "Starting file uncompress as ordered.";
  48. uncompress_file_without_decrypt($download_file_path,
  49. $destination);
  50. }
  51.  
  52. # Run post scripts if any
  53. if ($post_script ne "")
  54. {
  55. logger "1 Running postscript commands.";
  56. my @postcmdlines = split /[\r\n]+/, $post_script;
  57. my $postcmdfile = $destination."/".'postinstall.sh';
  58. open FILE, '>', $postcmdfile;
  59. print FILE "cd $destination\n";
  60. print FILE "while kill -0 $pid >/dev/null 2>&1\n";
  61. print FILE "do\n";
  62. print FILE " sleep 1\n";
  63. print FILE "done\n";
  64. foreach my $line (@postcmdlines) {
  65. print FILE "$line\n";
  66. logger "1 Postscript command received \"" . $line ."\".";
  67. }
  68. print FILE "rm -f $destination/postinstall.sh\n";
  69. close FILE;
  70. chmod 0755, $postcmdfile;
  71. my $screen_id = create_screen_id("post_script", $pid);
  72. my $cli_bin = create_screen_cmd($screen_id, "bash $postcmdfile");
  73. system($cli_bin);
  74. }
  75. }
  76. else
  77. {
  78. logger
  79. "Unable to fetch $url, or save to $download_file_path. Retval: ".$response->status_line;
  80. exit(0);
  81. }
  82.  
  83. # Child process must exit.
  84. exit(0);
  85. }
  86. else
  87. {
  88. sleep 10;
  89. if ($post_script ne "")
  90. {
  91. logger "2 Running postscript commands.";
  92. my @postcmdlines = split /[\r\n]+/, $post_script;
  93. my $postcmdfile = $destination."/".'postinstall.sh';
  94. open FILE, '>', $postcmdfile;
  95. print FILE "cd $destination\n";
  96. print FILE "while kill -0 $pid >/dev/null 2>&1\n";
  97. print FILE "do\n";
  98. print FILE " sleep 1\n";
  99. print FILE "done\n";
  100. foreach my $line (@postcmdlines) {
  101. print FILE "$line\n";
  102. logger "2 Postscript command received \"" . $line ."\".";
  103. }
  104. print FILE "rm -f $destination/postinstall.sh\n";
  105. close FILE;
  106. chmod 0755, $postcmdfile;
  107. my $screen_id = create_screen_id("post_script", $pid);
  108. sudo_exec_without_decrypt("bash $postcmdfile");
  109. }
  110. logger "Download process for $download_file_path has pid number $pid.";
  111. return "$pid";
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement