home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 10 / amigaformatcd10.iso / -in_the_mag- / html_tutorial / mas_run.cpp < prev    next >
C/C++ Source or Header  |  1996-09-03  |  4KB  |  146 lines

  1. // Execute a CGI script in a users public_html/cgi-bin directory
  2. //
  3. // (C) M.A.Smith University of Brighton
  4. //
  5. // Permission is granted to use this code
  6. //   provided this declaration and copyright notice remains intact.
  7. //
  8. // 1 April 1996
  9.  
  10. #include <iostream.h>
  11. #include <fstream.h>
  12. #include <iomanip.h>
  13.  
  14. #include <stdlib.h>
  15. #include <time.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18.  
  19. #include <unistd.h>             // fork exec
  20. #include <sys/wait.h>           // wait
  21. #include <signal.h>             // kill
  22.  
  23. #include "parse.h"
  24. #include "parse.cpp"
  25.  
  26. void cgi_var_output();
  27.  
  28. void execute( char file[], char user[], char *argv[], char *env[] );
  29. char* map_uname( char *path );
  30.  
  31. // Parameters to the run program
  32. //  passed in the QUERY_STRING environment variable
  33. //
  34. //  file=mame  - Names the file to be run
  35. //  user=name  - The user
  36.  
  37. // Remember all files name must be absolute or relative to the
  38. //  directory in which the CGI script is run
  39. //  ~user/public_html/cgi-bin
  40.  
  41.  
  42. int main(int argc, char* argv[], char *env[] )
  43. {
  44.   char *query_str = getenv("QUERY_STRING");
  45.   Parse list( query_str == 0 ?
  46.           "user=mas&file=cgi&other=123" : query_str );
  47.  
  48.   execute( list.get_item_n( "file"), list.get_item_n( "user" ),
  49.            argv, env );
  50.   return 0;
  51. }
  52.  
  53. inline void html( char str[] ) { cout << str << "\n"; }
  54.  
  55. inline void html_( char str[] ) { cout << str; }
  56.  
  57. inline void html_( char c ) { cout << c; }
  58.  
  59.  
  60. void cgi_error( char mes[], char mes1[] ="", char mes2[]="" )
  61. {
  62.     html("Content-type: text/html"); html("");
  63.     html("<HTML>");
  64.  
  65.     html("<HEAD>");
  66.     html("<TITLE>Error</TITLE>");
  67.     html("</HEAD>");
  68.  
  69.     html("<BODY>");
  70.  
  71.     html("<H2>Error calling CGI script</H2>");
  72.     html("<P>");
  73.     html_( mes ); html_( mes1 ); html( mes2 );
  74.     html("<P>");
  75.     cgi_var_output();
  76.     html("</BODY>");
  77.     html("</HTML>");
  78. }
  79.  
  80.  
  81. void execute( char file[], char user[], char *argv[], char *env[] )
  82. {
  83.   const int RUN_FOR = 20;
  84.   char prog[255]; prog[0] = '\0';
  85.   strcat( prog, "~" );
  86.   strcat( prog, user );
  87.   strcat( prog, "/public_html/" );
  88.   char *path = map_uname( prog );
  89.   chdir( path );                         // ~user/public_html/
  90.  
  91.   pid_t pid = fork();
  92.   if ( pid == 0 )
  93.   {
  94.     strcat( path, "cgi-bin/" );
  95.     strcat( path, file );
  96.     alarm( RUN_FOR );
  97.     execve( path, argv, env );                  // prog
  98.     cgi_error( "File not readable [", path, "]" );
  99.   } else {
  100.     close(0); close(1); close(2);
  101.     pid_t pid2 = fork();
  102.     if ( pid2 == 0 )
  103.     {
  104.       sleep( RUN_FOR + 2 );
  105.       kill( pid, 9 );                    // Hand of GOD
  106.     } else {
  107.       // Just die
  108.     }
  109.   }
  110. }
  111.  
  112.  
  113. char* map_uname( char *path )
  114. {
  115. #ifndef NO_MAP
  116.   if ( path[0] == '~' )
  117.   {
  118.      char uname[255];                       // Holds user name
  119.      char *rest = &path[1];                 // omit ~
  120.      char *p = &uname[0];                   // user
  121.  
  122.      while ( *rest != '/' && *rest != '\0' )
  123.      {
  124.        *p++ = *rest++;
  125.      }
  126.      *p = '\0';                             // Terminator
  127.      char *root = uname;                    // If fails
  128.      passwd *pw = getpwnam( uname );        // Structure about user
  129.      if ( pw != NULL )
  130.      {
  131.        root = pw->pw_dir;                   // Users home directory
  132.      }
  133.      int len_root = strlen(root);
  134.      int len_path  = len_root + strlen(rest);
  135.      char *new_path = new char[len_path+1]; // Dynamic string
  136.  
  137.      strcpy( &new_path[0], root );          // abs user path
  138.      strcpy( &new_path[len_root], rest );   // remainder
  139.      return new_path;
  140.   } else {
  141.     return path;
  142.   }
  143. #endif
  144.  return path;
  145. }
  146.