"Fossies" - the Fresh Open Source Software Archive

Member "cgiwrap-4.1/fetch.c" (16 Jun 2008, 5409 Bytes) of package /linux/www/old/cgiwrap-4.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* 
    2  *  CGIWrap is free software; you can redistribute it and/or modify
    3  *  it under the terms of the GNU General Public License as published by
    4  *  the Free Software Foundation; either version 2 of the License, or
    5  *  (at your option) any later version.
    6  *
    7  *  CGIWrap is distributed in the hope that it will be useful,
    8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   10  *  GNU General Public License for more details.
   11  *
   12  *  You should have received a copy of the GNU General Public License
   13  *  along with CGIWrap; if not, write to the Free Software
   14  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   15  *
   16  *  Copyright 2003-2005, Nathan Neulinger <nneul@neulinger.org>
   17  *
   18  */
   19 
   20 /**
   21  **  File: fetch.c
   22  **  Purpose: Routines to extract the user and script name from request
   23  **/ 
   24 
   25 #include "cgiwrap.h"    /* Headers for all CGIwrap source files */
   26 RCSID("$Id: fetch.c 306 2008-06-13 14:02:02Z nneul $");
   27 
   28 char *FetchUserString(void)
   29 {
   30     char *pathInfoString;
   31     char *queryString;
   32     char *userStr;
   33 
   34     DEBUG_Msg("\n");
   35 
   36     userStr = (char *) 0;
   37     pathInfoString = getenv("PATH_INFO");
   38     if ( pathInfoString )  /* use PATH_INFO */
   39     {
   40         if ( pathInfoString[0] != 0 )
   41         {
   42             DEBUG_Msg("Trying to extract user from PATH_INFO.");
   43 
   44             userStr = GetPathComponents(1, pathInfoString);
   45         }
   46         else
   47         {
   48             DEBUG_Msg("PATH_INFO is empty, can't check.");
   49         }
   50     }
   51 
   52     queryString = getenv("QUERY_STRING");
   53     if ( !userStr && queryString )  /* or use QUERY_STRING */
   54     {
   55         if ( queryString[0] != 0 )
   56         {
   57             DEBUG_Msg("Trying to extract user from QUERY_STRING");
   58 
   59             DEBUG_Msg("Read in user keyword value");
   60             userStr = GetValue("user", getenv("QUERY_STRING") );
   61         }
   62         else
   63         {
   64             DEBUG_Msg("QUERY_STRING is empty, can't check.");
   65         }
   66     }
   67 
   68 /* Handle ~ notation */
   69     if (userStr)
   70     {
   71         if (userStr[0] == '~')
   72         {
   73             userStr++;
   74         }
   75     }
   76 
   77     if ( !userStr ) /* nothing at all found */
   78     {
   79         MSG_Error_General("Couldn't find user and script name, check your URL.");
   80     }
   81 
   82     return userStr;
   83 }
   84 
   85 char *FetchScriptString( char *basedir )
   86 {
   87     char *tempStr, *tempStr2;
   88     char *pathInfoString;
   89     char *queryString;
   90     char *scrStr;
   91     struct stat fstat;
   92     int i, max, max_user, max_multiuser;
   93 
   94     scrStr = 0;
   95     pathInfoString = getenv("PATH_INFO");
   96     if ( pathInfoString )  /* use PATH_INFO */
   97     {
   98         if ( pathInfoString[0] != 0 )
   99         {
  100             DEBUG_Msg("Trying to extract script from PATH_INFO");
  101 
  102             scrStr = StripPathComponents(1,pathInfoString);
  103             if ( ! strlen(scrStr) ) { scrStr = 0; }
  104 
  105             DEBUG_Str("Extracted PATH_INFO", scrStr);
  106         }
  107         else
  108         {
  109             DEBUG_Msg("PATH_INFO is empty, can't check.");
  110         }
  111     }
  112 
  113     queryString = getenv("QUERY_STRING");
  114     if ( !scrStr && queryString )  /* or use QUERY_STRING */
  115     {
  116         if ( queryString[0] != 0 )
  117         {
  118             DEBUG_Msg("Trying to extract script from QUERY_STRING");
  119 
  120             DEBUG_Msg("Read in script keyword value");
  121             scrStr = GetValue("script", getenv("QUERY_STRING") );
  122             DEBUG_Str("Value", scrStr);
  123         }
  124         else
  125         {
  126             DEBUG_Msg("QUERY_STRING is empty, can't check.");
  127         }
  128     }
  129 
  130 
  131     if ( !scrStr ) /* nothing at all found */
  132     {
  133         MSG_Error_General("Couldn't find script name, check your URL.");
  134     }
  135 
  136     /* Initially neither a user nor a multiuser script was found */
  137     max_user = 0;
  138     max_multiuser = 0;
  139 
  140     /* Split out PATH_INFO from the script name */
  141     /* This pass searches the per-user script dir */
  142     for (i=1; i<=(CountSubDirs(scrStr)+1) && i>0; i++)
  143     {   
  144         tempStr = GetPathComponents(i, scrStr);
  145         tempStr2 = BuildScriptPath(basedir,tempStr);
  146 
  147         if ( !stat( tempStr2, &fstat ) )
  148         {
  149             max_user = i;
  150             if ( S_ISREG(fstat.st_mode) )
  151             {
  152                 i = -1;
  153             }
  154         }       
  155         else
  156         {
  157             i = -1;
  158         }
  159         free(tempStr);
  160         free(tempStr2);
  161     }
  162 
  163 #if defined(CONF_MULTIUSER_CGI_DIR)
  164     /* Split out PATH_INFO from the script name */
  165     /* This pass searches the per-user script dir */
  166     for (i=1; i<=(CountSubDirs(scrStr)+1) && i>0; i++)
  167     {   
  168         tempStr = GetPathComponents(i, scrStr);
  169         tempStr2 = BuildScriptPath(CONF_MULTIUSER_CGI_DIR,tempStr);
  170 
  171         if ( !stat( tempStr2, &fstat ) )
  172         {
  173             max_multiuser = i;
  174             if ( S_ISREG(fstat.st_mode) )
  175             {
  176                 i = -1;
  177             }
  178         }       
  179         else
  180         {
  181             i = -1;
  182         }
  183         free(tempStr);
  184         free(tempStr2);
  185     }
  186 #endif
  187 
  188 
  189 #if !defined(CONF_MULTIUSER_CGI_DIR)
  190     if ( max_user < 1 )
  191 #else
  192     if ( max_user < 1 && max_multiuser < 1 )
  193 #endif  
  194     {
  195         MSG_Error_General("Script File Not Found!");
  196     }
  197 
  198     /* Indicate to rest of cgiwrap which script was chosen */
  199     /* Only use the multiuser one if it is more specific (more dirs in path) */
  200 #if defined(CONF_MULTIUSER_CGI_DIR)
  201     if ( max_multiuser > 0 && max_multiuser > max_user )
  202     {
  203         DEBUG_Msg("Using multiuser cgi script.\n");
  204         Context.multiuser_cgi_script = 1;
  205         max = max_multiuser;
  206     }
  207     else
  208 #endif
  209     {
  210         max = max_user;
  211     }
  212 
  213     /* Figure out the PATH_INFO and the script name */
  214     tempStr = StripPathComponents(max, scrStr);
  215     tempStr2 = (char *) SafeMalloc( strlen(tempStr) + 12, 
  216         "PATH_INFO environment variable" );
  217     if ( !strcmp("", tempStr) )
  218     {
  219 #ifdef HAS_UNSETENV
  220         unsetenv("PATH_INFO");
  221         free(tempStr2);
  222 #else
  223         strcpy(tempStr2, "PATH_INFO=");
  224         Context.newPathInfo = NULL;
  225         SafePutenv(tempStr2, "clear PATH_INFO environment variable");
  226 #endif
  227     }
  228     else
  229     {
  230         sprintf(tempStr2, "PATH_INFO=%s", tempStr);
  231         Context.newPathInfo = strdup(tempStr2 + strlen("PATH_INFO="));
  232         SafePutenv(tempStr2, "set PATH_INFO environment variable");
  233     }
  234     free(tempStr);
  235 
  236     return GetPathComponents(max, scrStr);
  237 }
  238 
  239 
  240