Guest User

0x000BLAh

a guest
Oct 17th, 2009
131
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. use FCGI;
  3. use Socket;
  4. use FCGI::ProcManager;
  5. sub shutdown { FCGI::CloseSocket($socket); exit; }
  6. sub restart  { FCGI::CloseSocket($socket); &main; }
  7. use sigtrap 'handler', \&shutdown, 'normal-signals';
  8. use sigtrap 'handler', \&restart,  'HUP';
  9. require 'syscall.ph';
  10. use POSIX qw(setsid);
  11.  
  12. END()   { }
  13. BEGIN() { }
  14. {
  15.  no warnings;
  16. *CORE::GLOBAL::exit = sub { die "fakeexit\nrc=" . shift() . "\n"; };
  17. };
  18. eval q{exit};
  19. if ($@) {
  20.  exit unless $@ =~ /^fakeexit/;
  21. }
  22. &main;
  23.  
  24. sub daemonize() {
  25.  chdir '/' or die "Can't chdir to /: $!";
  26.  defined( my $pid = fork ) or die "Can't fork: $!";
  27.  exit if $pid;
  28.  setsid() or die "Can't start a new session: $!";
  29.  umask 0;
  30. }
  31.  
  32. sub main {
  33.  
  34.  $proc_manager = FCGI::ProcManager->new( {n_processes => 5} );
  35. @@ $socket = FCGI::OpenSocket( "/nginx/fcgi/cgi.sock", 10 )
  36.  ; #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
  37.  $request =
  38.  FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket,
  39.  &FCGI::FAIL_ACCEPT_ON_INTR );
  40.  $proc_manager->pm_manage();
  41.  if ($request) { request_loop() }
  42.  FCGI::CloseSocket($socket);
  43. }
  44.  
  45. sub request_loop {
  46.  while ( $request->Accept() >= 0 ) {
  47.  $proc_manager->pm_pre_dispatch();
  48.  
  49.  #processing any STDIN input from WebServer (for CGI-POST actions)
  50.  $stdin_passthrough = '';
  51.  { no warnings; $req_len = 0 + $req_params{'CONTENT_LENGTH'}; };
  52.  if ( ( $req_params{'REQUEST_METHOD'} eq 'POST' ) && ( $req_len != 0 ) )
  53.  {
  54.  my $bytes_read = 0;
  55.  while ( $bytes_read < $req_len ) {
  56.  my $data = '';
  57.  my $bytes = read( STDIN, $data, ( $req_len - $bytes_read ) );
  58.  last if ( $bytes == 0 || !defined($bytes) );
  59.  $stdin_passthrough .= $data;
  60.  $bytes_read += $bytes;
  61.  }
  62.  }
  63.  
  64.  #running the cgi app
  65.  if (
  66.  ( -x $req_params{SCRIPT_FILENAME} ) &&    #can I execute this?
  67.  ( -s $req_params{SCRIPT_FILENAME} ) &&    #Is this file empty?
  68.  ( -r $req_params{SCRIPT_FILENAME} )       #can I read this file?
  69.  )
  70.  {
  71.  pipe( CHILD_RD,   PARENT_WR );
  72.  pipe( PARENT_ERR, CHILD_ERR );
  73.  my $pid = open( CHILD_O, "-|" );
  74.  unless ( defined($pid) ) {
  75.  print("Content-type: text/plain\r\n\r\n");
  76.  print
  77. "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n";
  78.  next;
  79.  }
  80.  $oldfh = select(PARENT_ERR);
  81.  $|     = 1;
  82.  select(CHILD_O);
  83.  $| = 1;
  84.  select($oldfh);
  85.  if ( $pid > 0 ) {
  86.  close(CHILD_RD);
  87.  close(CHILD_ERR);
  88.  print PARENT_WR $stdin_passthrough;
  89.  close(PARENT_WR);
  90.  $rin = $rout = $ein = $eout = '';
  91.  vec( $rin, fileno(CHILD_O),    1 ) = 1;
  92.  vec( $rin, fileno(PARENT_ERR), 1 ) = 1;
  93.  $ein    = $rin;
  94.  $nfound = 0;
  95.  
  96.  while ( $nfound =
  97.  select( $rout = $rin, undef, $ein = $eout, 10 ) )
  98.  {
  99.  die "$!" unless $nfound != -1;
  100.  $r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1;
  101.  $r2 = vec( $rout, fileno(CHILD_O),    1 ) == 1;
  102.  $e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1;
  103.  $e2 = vec( $eout, fileno(CHILD_O),    1 ) == 1;
  104.  
  105.  if ($r1) {
  106.  while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) {
  107.  print STDERR $errbytes;
  108.  }
  109.  if ($!) {
  110.  $err = $!;
  111.  die $!;
  112.  vec( $rin, fileno(PARENT_ERR), 1 ) = 0
  113.  unless ( $err == EINTR or $err == EAGAIN );
  114.  }
  115.  }
  116.  if ($r2) {
  117.  while ( $bytes = read( CHILD_O, $s, 4096 ) ) {
  118.  print $s;
  119.  }
  120.  if ( !defined($bytes) ) {
  121.  $err = $!;
  122.  die $!;
  123.  vec( $rin, fileno(CHILD_O), 1 ) = 0
  124.  unless ( $err == EINTR or $err == EAGAIN );
  125.  }
  126.  }
  127.  last if ( $e1 || $e2 );
  128.  }
  129.  close CHILD_RD;
  130.  close PARENT_ERR;
  131.  waitpid( $pid, 0 );
  132.  } else {
  133.  foreach $key ( keys %req_params ) {
  134.  $ENV{$key} = $req_params{$key};
  135.  }
  136.  
  137.  # cd to the script's local directory
  138.  if ( $req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/] +$/ ) {
  139.  chdir $1;
  140.  }
  141.  close(PARENT_WR);
  142.  
  143.  #close(PARENT_ERR);
  144.  close(STDIN);
  145.  close(STDERR);
  146.  
  147.  #fcntl(CHILD_RD, F_DUPFD, 0);
  148.  syscall( &SYS_dup2, fileno(CHILD_RD),  0 );
  149.  syscall( &SYS_dup2, fileno(CHILD_ERR), 2 );
  150.  
  151.  #open(STDIN, "<&CHILD_RD");
  152.  exec( $req_params{SCRIPT_FILENAME} );
  153.  die("exec failed");
  154.  }
  155.  } else {
  156.  print("Content-type: text/plain\r\n\r\n");
  157.  print
  158. "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
  159.  }
  160.  }
  161. }
RAW Paste Data