home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / mod_cgi.c~0 < prev    next >
Encoding:
Text File  |  1996-08-05  |  12.8 KB  |  397 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. #ifdef __EMX__
  255.     /* Due to an inherited socket handle limitation in the EMX libraries. */
  256.  
  257.      if (!spawn_child (r->connection->pool, cgi_child, (void *)&cld,
  258.                nph ? just_wait : kill_after_timeout,
  259.                &script_out, &script_in)) {
  260.         log_reason ("couldn't spawn child process", r->filename, r);
  261.         return SERVER_ERROR;
  262.     }
  263. #else
  264.      if (!spawn_child (r->connection->pool, cgi_child, (void *)&cld,
  265.                nph ? just_wait : kill_after_timeout,
  266.                &script_out, nph ? NULL : &script_in)) {
  267.         log_reason ("couldn't spawn child process", r->filename, r);
  268.         return SERVER_ERROR;
  269.     }
  270. #endif
  271.  
  272.     /* Transfer any put/post args, CERN style...
  273.      * Note that if a buggy script fails to read everything we throw
  274.      * at it, or a buggy client sends too much, we get a SIGPIPE, so
  275.      * we have to ignore SIGPIPE while doing this.  CERN does the same
  276.      * (and in fact, they pretty nearly guarantee themselves a SIGPIPE
  277.      * on every invocation by chasing the real client data with a
  278.      * spurious newline).
  279.      */
  280.  
  281.     if (r->method_number == M_POST || r->method_number == M_PUT) {
  282.         void (*handler)();
  283.         int remaining = atoi (lenp);
  284.  
  285.         hard_timeout ("copy script args", r);
  286.         handler = signal (SIGPIPE, SIG_IGN);
  287.  
  288.         while ((remaining > 0))
  289.         {
  290.             int len_read, len_to_read = remaining;
  291.  
  292.             if (len_to_read > HUGE_STRING_LEN) len_to_read = HUGE_STRING_LEN;
  293.  
  294.             len_read = read_client_block (r, argsbuffer, len_to_read);
  295.             if (len_read == 0)
  296.                 break;
  297.             if (fwrite (argsbuffer, 1, len_read, script_out) == 0)
  298.                 break;
  299.             remaining -= len_read;
  300.         }
  301.  
  302.         /* If script stopped reading early, soak up remaining stuff from
  303.          * client...
  304.          */
  305.  
  306.         while (remaining > 0) {
  307.             int len_read, len_to_read = remaining;
  308.             if (len_to_read > HUGE_STRING_LEN) len_to_read = HUGE_STRING_LEN;
  309.  
  310.             len_read = read_client_block (r, argsbuffer, len_to_read);
  311.             if (len_read == 0) break;
  312.         }
  313.  
  314.         fflush (script_out);
  315.         signal (SIGPIPE, handler);
  316.  
  317.         kill_timeout (r);
  318.     }
  319.  
  320.     pfclose (r->connection->pool, script_out);
  321.  
  322.     /* Handle script return... */
  323.     if (script_in && !nph) {
  324.         char *location;
  325.         int ret;
  326.  
  327.         if ((ret = scan_script_header(r, script_in)))
  328.             return ret;
  329.  
  330.         location = table_get (r->headers_out, "Location");
  331.  
  332.         if (location && location[0] == '/' && r->status == 200) {
  333.  
  334.             /* Soak up all the script output */
  335.             hard_timeout ("read from script", r);
  336.             while (fgets(argsbuffer, HUGE_STRING_LEN-1, script_in) != NULL)
  337.                 continue;
  338.             kill_timeout (r);
  339.  
  340.  
  341.            /* This redirect needs to be a GET no matter what the original
  342.             * method was.
  343.             */
  344.             r->method = pstrdup(r->pool, "GET");
  345.             r->method_number = M_GET;
  346.  
  347.             internal_redirect_handler (location, r);
  348.             return OK;
  349.         } else if (location && r->status == 200) {
  350.             /* XX Note that if a script wants to produce its own Redirect
  351.              * body, it now has to explicitly *say* "Status: 302"
  352.              */
  353.             return REDIRECT;
  354.         }
  355.  
  356.         hard_timeout ("send script output", r);
  357.         send_http_header(r);
  358.         if(!r->header_only) send_fd (script_in, r);
  359.         kill_timeout (r);
  360.         pfclose (r->connection->pool, script_in);
  361.     }
  362.  
  363. #ifdef __EMX__
  364.     if (nph) {
  365.         while (fgets(argsbuffer, HUGE_STRING_LEN-1, script_in) != NULL) {
  366.             bputs(argsbuffer, r->connection->client);
  367.         }
  368.     }
  369. #endif
  370.  
  371.     return OK;          /* NOT r->status, even if it has changed. */
  372. }
  373.  
  374. handler_rec cgi_handlers[] = {
  375. { CGI_MAGIC_TYPE, cgi_handler },
  376. { "cgi-script", cgi_handler },
  377. { NULL }
  378. };
  379.  
  380. module cgi_module = {
  381.    STANDARD_MODULE_STUFF,
  382.    NULL,            /* initializer */
  383.    NULL,            /* dir config creater */
  384.    NULL,            /* dir merger --- default is to override */
  385.    NULL,            /* server config */
  386.    NULL,            /* merge server config */
  387.    NULL,            /* command table */
  388.    cgi_handlers,        /* handlers */
  389.    NULL,            /* filename translation */
  390.    NULL,            /* check_user_id */
  391.    NULL,            /* check auth */
  392.    NULL,            /* check access */
  393.    NULL,            /* type_checker */
  394.    NULL,            /* fixups */
  395.    NULL             /* logger */
  396. };
  397.