home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / mod_cgi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-07  |  12.9 KB  |  391 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54.  
  55. /*
  56.  * http_script: keeps all script-related ramblings together.
  57.  *
  58.  * Compliant to CGI/1.1 spec
  59.  *
  60.  * Adapted by rst from original NCSA code by Rob McCool
  61.  *
  62.  * Apache adds some new env vars; REDIRECT_URL and REDIRECT_QUERY_STRING for
  63.  * custom error responses, and DOCUMENT_ROOT because we found it useful.
  64.  * It also adds SERVER_ADMIN - useful for scripts to know who to mail when
  65.  * they fail.
  66.  */
  67.  
  68. #include "httpd.h"
  69. #include "http_config.h"
  70. #include "http_request.h"
  71. #include "http_core.h"
  72. #include "http_protocol.h"
  73. #include "http_main.h"
  74. #include "http_log.h"
  75. #include "util_script.h"
  76.  
  77. /* KLUDGE --- for back-combatibility, we don't have to check ExecCGI
  78.  * in ScriptAliased directories, which means we need to know if this
  79.  * request came through ScriptAlias or not... so the Alias module
  80.  * leaves a note for us.
  81.  */
  82.  
  83. int is_scriptaliased (request_rec *r)
  84. {
  85.     char *t = table_get (r->notes, "alias-forced-type");
  86.     return t && (!strcmp (t, "cgi-script"));
  87. }
  88.  
  89. /****************************************************************
  90.  *
  91.  * Actual CGI handling...
  92.  */
  93.  
  94.  
  95. struct cgi_child_stuff {
  96.     request_rec *r;
  97.     int nph;
  98.     char *argv0;
  99. };
  100.  
  101. void cgi_child (void *child_stuff)
  102. {
  103.     struct cgi_child_stuff *cld = (struct cgi_child_stuff *)child_stuff;
  104.     request_rec *r = cld->r;
  105.     char *argv0 = cld->argv0;
  106.     int nph = cld->nph;
  107.  
  108. #ifdef DEBUG_CGI
  109. #ifdef __EMX__
  110.     /* Under OS/2 need to use device con. */
  111.     FILE *dbg = fopen ("con", "w");
  112. #else
  113.     FILE *dbg = fopen ("/dev/tty", "w");
  114. #endif
  115.     int i;
  116. #endif
  117.  
  118.     char **env;
  119.     char err_string[HUGE_STRING_LEN];
  120.  
  121. #ifdef DEBUG_CGI
  122.     fprintf (dbg, "Attempting to exec %s as %sCGI child (argv0 = %s)\n",
  123.         r->filename, nph ? "NPH " : "", argv0);
  124. #endif
  125.  
  126.     add_cgi_vars (r);
  127.     env = create_environment (r->pool, r->subprocess_env);
  128.  
  129. #ifdef DEBUG_CGI
  130.     fprintf (dbg, "Environment: \n");
  131.     for (i = 0; env[i]; ++i) fprintf (dbg, "'%s'\n", env[i]);
  132. #endif
  133.  
  134.     chdir_file (r->filename);
  135.     error_log2stderr (r->server);
  136.  
  137. #ifndef __EMX__
  138.     if (nph) client_to_stdout (r->connection);
  139. #endif
  140.  
  141.     /* Transumute outselves into the script.
  142.      * NB only ISINDEX scripts get decoded arguments.
  143.      */
  144.  
  145.     cleanup_for_exec();
  146.  
  147. #ifdef __EMX__
  148.     if((!r->args) || (!r->args[0]) || (ind(r->args,'=') >= 0)) {
  149.             int emxloop;
  150.             char *emxtemp;
  151.  
  152.             /* For OS/2 place the variables in the current
  153.             enviornment then it will be inherited. This way
  154.             the program will also get all of OS/2's other SETs. */
  155.             for (emxloop=0; ((emxtemp = env[emxloop]) != NULL); emxloop++)
  156.                 putenv(emxtemp);
  157.  
  158.             if (strstr(strupr(r->filename), ".CMD") > 0) {
  159.                 /* Special case to allow use of REXX commands as scripts. */
  160.                 os2pathname(r->filename);
  161.                 execl("CMD.EXE", "CMD.EXE", "/C", r->filename, NULL);
  162.             } else {
  163.                 execl(r->filename, argv0, NULL);
  164.             }
  165.     } else {
  166.             int emxloop;
  167.             char *emxtemp;
  168.  
  169.             /* For OS/2 place the variables in the current
  170.             enviornment then it will be inherited. This way
  171.             the program will also get all of OS/2's other SETs. */
  172.             for (emxloop=0; ((emxtemp = env[emxloop]) != NULL); emxloop++)
  173.                 putenv(emxtemp);
  174.  
  175.             if (strstr(strupr(r->filename), ".CMD") > 0) {
  176.                 /* Special case to allow use of REXX commands as scripts. */
  177.                 os2pathname(r->filename);
  178.                 execv("CMD.EXE", create_argv_cmd(r->pool, argv0, r->args, r->filename));
  179.             } else {
  180.                 execv(r->filename, create_argv(r->pool, argv0, r->args));
  181.             }
  182.     }
  183. #else
  184.     if((!r->args) || (!r->args[0]) || (ind(r->args,'=') >= 0))
  185.         execle(r->filename, argv0, NULL, env);
  186.     else
  187.         execve(r->filename, create_argv(r->pool, argv0, r->args), env);
  188. #endif
  189.  
  190.     /* Uh oh.  Still here.  Where's the kaboom?  There was supposed to be an
  191.      * EARTH-shattering kaboom!
  192.      *
  193.      * Oh, well.  Muddle through as best we can...
  194.      *
  195.      * (NB we can't use log_error, or anything like that, because we
  196.      * just closed the file descriptor which r->server->error_log
  197.      * was tied to in cleanup_for_exec().  It's only available on stderr
  198.      * now, so that's what we use).
  199.      */
  200.  
  201.     sprintf(err_string,
  202.         "exec of %s failed, errno is %d\n", r->filename, errno);
  203.     write(2, err_string, strlen(err_string));
  204.     exit(0);
  205. }
  206.  
  207. int cgi_handler (request_rec *r)
  208. {
  209.     int nph;
  210.     char *argv0;
  211.     FILE *script_out, *script_in;
  212.     char argsbuffer[HUGE_STRING_LEN];
  213.     int is_included = !strcmp (r->protocol, "INCLUDED");
  214.     char *lenp = table_get (r->headers_in, "Content-length");
  215.  
  216.     struct cgi_child_stuff cld;
  217.  
  218.     if((argv0 = strrchr(r->filename,'/')) != NULL)
  219.         argv0++;
  220.     else argv0 = r->filename;
  221.  
  222.     nph = !(strncmp(argv0,"nph-",4));
  223.  
  224.     if (!(allow_options (r) & OPT_EXECCGI) && !is_scriptaliased (r)) {
  225.         log_reason("Options ExecCGI is off in this directory", r->filename, r);
  226.     return FORBIDDEN;
  227.     }
  228.     if (nph && is_included) {
  229.         log_reason("attempt to include NPH CGI script", r->filename, r);
  230.     return FORBIDDEN;
  231.     }
  232.  
  233.     if (S_ISDIR(r->finfo.st_mode)) {
  234.         log_reason("attempt to invoke directory as script", r->filename, r);
  235.     return FORBIDDEN;
  236.     }
  237.     if (r->finfo.st_mode == 0) {
  238.         log_reason("script not found or unable to stat", r->filename, r);
  239.     return NOT_FOUND;
  240.     }
  241.     if(!can_exec(&r->finfo)) {
  242.         log_reason("file permissions deny server execution", r->filename, r);
  243.         return FORBIDDEN;
  244.     }
  245.     if ((r->method_number == M_POST || r->method_number == M_PUT)
  246.     && !lenp) {
  247.         log_reason("POST or PUT without Content-length:", r->filename, r);
  248.     return BAD_REQUEST;
  249.     }
  250.  
  251.     add_common_vars (r);
  252.     cld.argv0 = argv0; cld.r = r; cld.nph = nph;
  253.  
  254.      if (!spawn_child (r->connection->pool, cgi_child, (void *)&cld,
  255.                nph ? just_wait : kill_after_timeout,
  256. #ifdef __EMX__
  257.                &script_out, &script_in)) {
  258. #else
  259.                &script_out, nph ? NULL : &script_in)) {
  260. #endif
  261.         log_reason ("couldn't spawn child process", r->filename, r);
  262.         return SERVER_ERROR;
  263.     }
  264.  
  265.     /* Transfer any put/post args, CERN style...
  266.      * Note that if a buggy script fails to read everything we throw
  267.      * at it, or a buggy client sends too much, we get a SIGPIPE, so
  268.      * we have to ignore SIGPIPE while doing this.  CERN does the same
  269.      * (and in fact, they pretty nearly guarantee themselves a SIGPIPE
  270.      * on every invocation by chasing the real client data with a
  271.      * spurious newline).
  272.      */
  273.  
  274.     if (r->method_number == M_POST || r->method_number == M_PUT) {
  275.         void (*handler)();
  276.         int remaining = atoi (lenp);
  277.  
  278.         hard_timeout ("copy script args", r);
  279.         handler = signal (SIGPIPE, SIG_IGN);
  280.  
  281.         while ((remaining > 0))
  282.         {
  283.             int len_read, len_to_read = remaining;
  284.  
  285.             if (len_to_read > HUGE_STRING_LEN) len_to_read = HUGE_STRING_LEN;
  286.  
  287.             len_read = read_client_block (r, argsbuffer, len_to_read);
  288.             if (len_read == 0)
  289.                 break;
  290.             if (fwrite (argsbuffer, 1, len_read, script_out) == 0)
  291.                 break;
  292.             remaining -= len_read;
  293.         }
  294.  
  295.         /* If script stopped reading early, soak up remaining stuff from
  296.          * client...
  297.          */
  298.  
  299.         while (remaining > 0) {
  300.             int len_read, len_to_read = remaining;
  301.             if (len_to_read > HUGE_STRING_LEN) len_to_read = HUGE_STRING_LEN;
  302.  
  303.             len_read = read_client_block (r, argsbuffer, len_to_read);
  304.             if (len_read == 0) break;
  305.         }
  306.  
  307.         fflush (script_out);
  308.         signal (SIGPIPE, handler);
  309.  
  310.         kill_timeout (r);
  311.     }
  312.  
  313.     pfclose (r->connection->pool, script_out);
  314.  
  315.     /* Handle script return... */
  316.     if (script_in && !nph) {
  317.         char *location;
  318.         int ret;
  319.  
  320.         if ((ret = scan_script_header(r, script_in)))
  321.             return ret;
  322.  
  323.         location = table_get (r->headers_out, "Location");
  324.  
  325.         if (location && location[0] == '/' && r->status == 200) {
  326.  
  327.             /* Soak up all the script output */
  328.             hard_timeout ("read from script", r);
  329.             while (fgets(argsbuffer, HUGE_STRING_LEN-1, script_in) != NULL)
  330.                 continue;
  331.             kill_timeout (r);
  332.  
  333.  
  334.            /* This redirect needs to be a GET no matter what the original
  335.             * method was.
  336.             */
  337.             r->method = pstrdup(r->pool, "GET");
  338.             r->method_number = M_GET;
  339.  
  340.             internal_redirect_handler (location, r);
  341.             return OK;
  342.         } 
  343.         else if (location && r->status == 200) {
  344.             /* XX Note that if a script wants to produce its own Redirect
  345.              * body, it now has to explicitly *say* "Status: 302"
  346.              */
  347.             return REDIRECT;
  348.         }
  349.  
  350.         hard_timeout ("send script output", r);
  351.         send_http_header(r);
  352.         if(!r->header_only) send_fd (script_in, r);
  353.         kill_timeout (r);
  354.         pfclose (r->connection->pool, script_in);
  355.     }
  356.  
  357. #ifdef __EMX__
  358.     if (nph) {
  359.         while (fgets(argsbuffer, HUGE_STRING_LEN-1, script_in) != NULL) {
  360.             bputs(argsbuffer, r->connection->client);
  361.         }
  362.     }
  363. #endif
  364.  
  365.     return OK;          /* NOT r->status, even if it has changed. */
  366. }
  367.  
  368. handler_rec cgi_handlers[] = {
  369. { CGI_MAGIC_TYPE, cgi_handler },
  370. { "cgi-script", cgi_handler },
  371. { NULL }
  372. };
  373.  
  374. module cgi_module = {
  375.    STANDARD_MODULE_STUFF,
  376.    NULL,            /* initializer */
  377.    NULL,            /* dir config creater */
  378.    NULL,            /* dir merger --- default is to override */
  379.    NULL,            /* server config */
  380.    NULL,            /* merge server config */
  381.    NULL,            /* command table */
  382.    cgi_handlers,        /* handlers */
  383.    NULL,            /* filename translation */
  384.    NULL,            /* check_user_id */
  385.    NULL,            /* check auth */
  386.    NULL,            /* check access */
  387.    NULL,            /* type_checker */
  388.    NULL,            /* fixups */
  389.    NULL             /* logger */
  390. };
  391.