"Fossies" - the Fresh Open Source Software Archive

Member "cgiwrap-4.1/msgs.c" (16 Jun 2008, 14922 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: messages.c
   22  **  Purpose: Routines for printing out error and other messages
   23  **/ 
   24 
   25 #include "cgiwrap.h"    /* Headers for all CGIwrap source files */
   26 RCSID("$Id: msgs.c 306 2008-06-13 14:02:02Z nneul $");
   27 
   28 /* 
   29  * Mode to output the message - plaintext or HTML
   30  */
   31 int MSG_HTMLMessages = 1;   
   32 int MSG_Need_NPH_Header = 0;
   33 
   34 #ifdef CONF_QUIET_ERRORS
   35 int MSG_QuietErrors = 1;
   36 #else
   37 int MSG_QuietErrors = 0;
   38 #endif
   39 
   40 
   41 /*
   42  * Print out a content-type message, but only if one hasn't been
   43  * printed out already.
   44  */
   45 void MSG_ContentType(char *typestring)
   46 {
   47     static int printed = 0;
   48     if ( !printed )
   49     {
   50         printed = 1;
   51         if ( MSG_Need_NPH_Header )
   52         {
   53             printf("HTTP/1.0 200 Ok\n");
   54         }
   55 
   56         printf("Content-type: %s; charset=%s\n\n", typestring, HTTP_CHARSET);
   57     }
   58 }
   59 
   60 /*
   61  * Utility Routines
   62  */
   63 void MSG_Header(char *title, char *msg)
   64 {
   65     if ( MSG_HTMLMessages )
   66     {
   67         MSG_ContentType("text/html");
   68         MSG_HTML_Header(title,msg);
   69     }
   70     else
   71     {
   72         MSG_ContentType("text/plain");
   73         MSG_Plain_Header(title,msg);
   74     }
   75 }
   76 
   77 void MSG_HTML_Header(char *title, char *msg)
   78 {
   79     printf("<HTML>\n<HEAD>\n");
   80     printf("<TITLE>%s: %s</TITLE>\n",title,msg);
   81     printf("<CENTER><H2>%s: %s</H2></CENTER>\n",title,msg);
   82     printf("<HR><p></HEAD><BODY>\n");
   83 }
   84 
   85 void MSG_Footer(void)
   86 {
   87     MSG_Info();
   88     if ( MSG_HTMLMessages )
   89     {
   90         MSG_HTML_Footer();
   91     }
   92     else
   93     {
   94         MSG_Plain_Footer();
   95     }
   96 }
   97 
   98 void MSG_HTML_Footer(void)
   99 {
  100     printf("\n</BODY></HTML>\n");
  101 }
  102     
  103 void MSG_Plain_Header(char *title, char *msg)
  104 {
  105     printf("\n");
  106     MSG_BoxedText(title);
  107     printf("\n");
  108 }
  109 
  110 void MSG_Plain_Footer(void)
  111 {
  112 }
  113 
  114 void MSG_Error_General(char *message)
  115 {
  116     if ( MSG_QuietErrors )
  117     {
  118         MSG_Header("CGIWrap Error", "Error executing script");
  119         printf("There was an error executing the script.");
  120     }
  121     else
  122     {
  123         MSG_Header("CGIWrap Error", message);
  124         printf("%s", message);
  125     }
  126     MSG_Footer();
  127     exit(1);
  128 }
  129 
  130 void MSG_Info(void)
  131 {
  132     char *prefix_html = "<DD><B>";
  133     char *prefix_plain = "\t";
  134     char *suffix_html = "</B>";
  135     char *suffix_plain = "";
  136     char *prefix, *suffix;
  137     
  138     /* Handle the prefix and suffix for list items */
  139     if ( MSG_HTMLMessages )
  140     {
  141         prefix = prefix_html;
  142         suffix = suffix_html;
  143     }
  144     else
  145     {
  146         prefix = prefix_plain;
  147         suffix = suffix_plain;
  148     }
  149 
  150 
  151 #if defined(CONF_LOCAL_INFO_ENABLED)
  152     if ( MSG_HTMLMessages )
  153     {
  154         printf("<P><HR><P>\n");
  155         printf("<DL>\n");
  156         printf("<DT><B>Local Information and Documentation:</B>\n");
  157         printf("<P>\n");
  158         printf("</DL>\n");
  159     }
  160     else
  161     {
  162         printf("\n\n");
  163         MSG_BoxedText("Local Information and Documentation:");
  164         printf("\n");
  165     }
  166 #endif
  167 
  168 #if defined(CONF_LOCAL_SITE_URL)
  169     if ( MSG_HTMLMessages )
  170     {
  171         printf("%sWeb Site%s: <A HREF=\"%s\">%s</A>\n", 
  172             prefix, suffix, CONF_LOCAL_SITE_URL, CONF_LOCAL_SITE_URL);
  173     }
  174     else
  175     {
  176         printf("%sWeb Site%s: %s\n", 
  177             prefix, suffix, CONF_LOCAL_SITE_URL);
  178     }
  179 #endif
  180 #if defined(CONF_LOCAL_DOC_URL)
  181     if ( MSG_HTMLMessages )
  182     {
  183         printf("%sCGIWrap Docs%s: <A HREF=\"%s\">%s</A>\n", 
  184             prefix, suffix, CONF_LOCAL_DOC_URL, CONF_LOCAL_DOC_URL);
  185     }
  186     else
  187     {
  188         printf("%sCGIWrap Docs%s: %s\n", 
  189             prefix, suffix, CONF_LOCAL_DOC_URL);
  190     }
  191 #endif
  192 
  193 #if defined(CONF_LOCAL_CONTACT_NAME)
  194     printf("%sContact Name%s: %s\n", 
  195         prefix, suffix, CONF_LOCAL_CONTACT_NAME);
  196 #endif
  197 #if defined(CONF_LOCAL_CONTACT_EMAIL)
  198     if ( MSG_HTMLMessages )
  199     {
  200         printf("%sContact EMail%s: <A HREF=\"mailto:%s\">%s</A>\n", 
  201             prefix, suffix, CONF_LOCAL_CONTACT_EMAIL, 
  202             CONF_LOCAL_CONTACT_EMAIL);
  203     }
  204     else
  205     {
  206         printf("%sContact EMail%s: %s\n", 
  207             prefix, suffix, CONF_LOCAL_CONTACT_EMAIL);
  208     }
  209 #endif
  210 #if defined(CONF_LOCAL_CONTACT_PHONE)
  211     printf("%sContact Phone%s: %s\n", 
  212         prefix, suffix, CONF_LOCAL_CONTACT_PHONE);
  213 #endif
  214 #if defined(CONF_LOCAL_CONTACT_URL)
  215     if ( MSG_HTMLMessages )
  216     {
  217         printf("%sContact Web Site%s: <A HREF=\"%s\">%s</A>\n", 
  218             prefix, suffix, CONF_LOCAL_CONTACT_URL, 
  219             CONF_LOCAL_CONTACT_URL);
  220     }
  221     else
  222     {
  223         printf("%sContact Web Site%s: %s\n", 
  224             prefix, suffix, CONF_LOCAL_CONTACT_URL);
  225     }
  226 #endif
  227 
  228 
  229     if ( MSG_HTMLMessages )
  230     {
  231         printf("<P>\n");
  232         printf("<DL>\n");
  233         printf("<DT><B>Server Data:</B>\n");
  234         printf("<P>\n");
  235     }
  236     else
  237     {
  238         printf("\n");
  239         MSG_BoxedText("Server Data:");
  240         printf("\n");
  241     }
  242     
  243             
  244     if ( getenv("SERVER_ADMIN") )
  245     {
  246           printf("%sServer Administrator/Contact%s: %s\n", 
  247               prefix, suffix, HTMLEncode(getenv("SERVER_ADMIN")));
  248     }
  249     if ( ! MSG_QuietErrors && getenv("SERVER_NAME") )
  250     {
  251           printf("%sServer Name%s: %s\n", 
  252               prefix, suffix, HTMLEncode(getenv("SERVER_NAME")));
  253     }
  254     if ( ! MSG_QuietErrors && getenv("SERVER_HOST") )
  255     {
  256           printf("%sServer Host%s: %s\n", 
  257               prefix, suffix, HTMLEncode(getenv("SERVER_HOST")));
  258     }
  259     if ( ! MSG_QuietErrors && getenv("SERVER_PORT") )
  260     {
  261           printf("%sServer Port%s: %s\n", 
  262               prefix, suffix, HTMLEncode(getenv("SERVER_PORT")));
  263     }
  264     if ( ! MSG_QuietErrors && getenv("SERVER_PROTOCOL") )
  265     {
  266           printf("%sServer Protocol%s: %s\n", 
  267               prefix, suffix, HTMLEncode(getenv("SERVER_PROTOCOL")));
  268     }
  269     if ( ! MSG_QuietErrors && getenv("HTTP_HOST") )
  270     {
  271           printf("%sVirtual Host%s: %s\n", 
  272               prefix, suffix, HTMLEncode(getenv("HTTP_HOST")));
  273     }
  274     
  275     if ( MSG_HTMLMessages )
  276     {
  277         printf("</DL>\n");
  278         printf("<P>\n");
  279         printf("<DL>\n");
  280         printf("<DT><B>Request Data:</B>\n");
  281         printf("<P>\n");
  282     }
  283     else
  284     {
  285         printf("\n");
  286         MSG_BoxedText("Request Data:");
  287         printf("\n");
  288     }
  289     
  290     if ( getenv("HTTP_USER_AGENT") )
  291     {
  292         printf("%sUser Agent/Browser%s: %s\n", 
  293             prefix, suffix, HTMLEncode(getenv("HTTP_USER_AGENT")));
  294     }
  295     if ( getenv("REQUEST_METHOD") )
  296     {
  297         printf("%sRequest Method%s: %s\n", 
  298             prefix, suffix, HTMLEncode(getenv("REQUEST_METHOD")));
  299     }
  300     if ( getenv("REMOTE_HOST") )
  301     {
  302         printf("%sRemote Host%s: %s\n", 
  303             prefix, suffix, HTMLEncode(getenv("REMOTE_HOST")));
  304     }
  305     if ( getenv("REMOTE_ADDR") )
  306     {
  307         printf("%sRemote Address%s: %s\n", 
  308             prefix, suffix, HTMLEncode(getenv("REMOTE_ADDR")));
  309     }
  310     if ( getenv("REMOTE_PORT") )
  311     {
  312         printf("%sRemote Port%s: %s\n", 
  313             prefix, suffix, HTMLEncode(getenv("REMOTE_PORT")));
  314     }
  315     if ( getenv("REMOTE_USER") )
  316     {
  317         printf("%sRemote User%s: %s\n", 
  318             prefix, suffix, HTMLEncode(getenv("REMOTE_USER")));
  319     }
  320     if ( getenv("QUERY_STRING") )
  321     {
  322         if ( strlen(getenv("QUERY_STRING")) > 0)
  323         {   
  324             printf("%sQuery String%s: %s\n", 
  325                 prefix, suffix, HTMLEncode(getenv("QUERY_STRING")));
  326         }
  327     }
  328     if ( ! MSG_QuietErrors && getenv("PATH_INFO") )
  329     {
  330         if ( strlen(getenv("PATH_INFO")) > 0)
  331         {   
  332             printf("%sExtra Path Info%s: %s\n", 
  333                 prefix, suffix, HTMLEncode(getenv("PATH_INFO")));
  334         }
  335     }
  336     if ( getenv("HTTP_REFERRER") )
  337     {
  338         printf("%sReferring Page%s: %s\n", 
  339             prefix, suffix, HTMLEncode(getenv("HTTP_REFERRER")));
  340     }
  341     if ( getenv("HTTP_REFERER") )
  342     {
  343         printf("%sReferring Page%s: %s\n", 
  344             prefix, suffix, HTMLEncode(getenv("HTTP_REFERER")));
  345     }
  346 
  347 
  348     if ( MSG_HTMLMessages )
  349     {
  350         printf("</DL>\n");
  351     }
  352 }
  353 
  354 /*
  355  * Error Messages 
  356  */
  357 
  358 void MSG_Error_CGIWrapNotSetUID(void)
  359 {
  360     if ( MSG_QuietErrors )
  361     {
  362         MSG_Error_ServerConfigError();
  363     }
  364     else
  365     {
  366         MSG_Header("CGIWrap Error", "CGIWrap is not setuid");
  367 
  368         printf(
  369 "The cgiwrap executable(s) were not made setuid-root. This is required\n"
  370 "for it to function properly. (SetUID root is needed in order to change\n"
  371 "the uid to that of the script owner. This is an installation error\n"
  372 "please make the executable setuid root, or use the 'make install'\n"
  373 "method of installing the executables.\n"
  374         );
  375 
  376         MSG_Footer();
  377         exit(1);
  378     }
  379 }
  380 
  381 
  382 void MSG_Error_ServerUserMismatch(void)
  383 {
  384     if ( MSG_QuietErrors )
  385     {
  386         MSG_Error_ServerConfigError();
  387     }
  388     else
  389     {
  390         MSG_Header("CGIWrap Error", "Server UserID Mismatch");
  391 
  392         printf(
  393 "The userid that the web server ran cgiwrap as does not match the\n"
  394 "userid that was configured into the cgiwrap executable.\n\n"
  395 "This is a configuration/setup problem with cgiwrap on this server.\n"
  396 "Please contact the server administrator.\n"
  397         );
  398 
  399         MSG_Footer();
  400         exit(1);
  401     }
  402 }
  403 
  404 
  405 void MSG_Error_ServerUserNotFound(void)
  406 {
  407     if ( MSG_QuietErrors )
  408     {
  409         MSG_Error_ServerConfigError();
  410     }
  411     else
  412     {
  413     struct passwd *pw;
  414     char *user = "";
  415 
  416     pw = getpwuid(getuid());
  417     if ( pw )
  418     {
  419         user = pw->pw_name;
  420     }
  421 
  422     MSG_Header("CGIWrap Error", "Server UserID Not Found");
  423 
  424     printf(
  425 "CGIWrap was configured with a server userid that does not exist\n"
  426 "on this server. The '--with-httpd-user' option should be specified\n"
  427 "when configuring cgiwrap. The user that should be specified is the one\n"
  428 "in the 'User' line of your httpd.conf file.\n"
  429     );
  430 
  431     if ( MSG_HTMLMessages )
  432     {
  433         printf("<P>");
  434     }
  435 
  436     printf(
  437 "This is a configuration/setup problem with CGIWrap on this server.\n"
  438 "Please contact the server administrator.\n"
  439     );
  440 
  441     if ( MSG_HTMLMessages )
  442     {
  443         printf("<P>");
  444     }
  445 
  446     printf(
  447 "As near as can be determined from the current execution environment,\n"
  448 "the userid this server is running as is \"%s\". Try reconfiguring\n"
  449 "cgiwrap using '--with-httpd-user=%s'.\n", user, user
  450     );
  451 
  452     MSG_Footer();
  453     exit(1);
  454     }
  455 }
  456 
  457 
  458 void MSG_Error_ExecutionNotPermitted(char *path, char *reason)
  459 {
  460     MSG_Header("CGIWrap Error", "Execution of this script not permitted");
  461 
  462     if ( MSG_QuietErrors )
  463     {
  464         MSG_Error_RequestError();
  465     }
  466     else
  467     {
  468         if ( path )
  469         {
  470             printf("Execution of (%s) is not permitted\n",HTMLEncode(path));
  471         }
  472         else
  473         {
  474             printf("Execution of that script is not permitted\n");
  475         }
  476         printf("for the following reason:\n\n");
  477 
  478         if ( MSG_HTMLMessages )
  479         {
  480             printf("<P><DL><DT>%s</DL>\n", reason);
  481         }
  482         else
  483         {
  484             printf("\t%s\n", reason);
  485         }
  486     }
  487 
  488     MSG_Footer();
  489     exit(1);
  490 }
  491 
  492 void MSG_Error_AccessControl(char *why, char *allowfile, char *denyfile)
  493 {
  494 
  495     if ( MSG_QuietErrors )
  496     {
  497         MSG_Error_RequestError();
  498     }
  499     else
  500     {
  501         MSG_Header("CGIWrap Error", "Access Control");
  502         printf("CGIWrap access control mechanism denied execution of this\n");
  503         printf("script for the following reason:\n\n");
  504 
  505         if ( MSG_HTMLMessages )
  506         {
  507             printf("<P>\n");
  508         }
  509         printf("\t%s\n", why);  
  510 
  511         if ( allowfile || denyfile )
  512         {
  513             if ( MSG_HTMLMessages )
  514             {
  515                 printf("<P>\n");
  516             }
  517             if ( allowfile )
  518             {
  519                 printf("\tAccess Control Allow File: %s\n", HTMLEncode(allowfile));
  520             }
  521             if ( denyfile )
  522             {
  523                 printf("\tAccess Control Deny File: %s\n", HTMLEncode(denyfile));
  524             }
  525         }
  526         MSG_Footer();
  527     }
  528     exit(1);
  529 }
  530 
  531 void MSG_Error_SystemError(char *when)
  532 {
  533     MSG_Header("CGIWrap Error", "System Error");
  534     printf("CGIWrap encountered a system error.\n");
  535 
  536     if ( ! MSG_QuietErrors )
  537     {
  538     if ( MSG_HTMLMessages )
  539     {
  540         printf("<DL>\n");
  541         printf("<DT>When: %s\n", HTMLEncode(when));
  542 #if defined(HAS_STRERROR)
  543         printf("<DT>Error Message: %s\n", strerror(errno));
  544 #elif defined(HAS_PERROR)
  545         perror("<DT>Error Message");
  546 #endif
  547         printf("<DT>Error Number: %d\n", errno);
  548         printf("</DL>\n");
  549     }
  550     else
  551     {
  552         printf("\tWhen: %s\n", when);
  553 #if defined(HAS_STRERROR)
  554         printf("\tError Message: %s\n", strerror(errno));
  555 #elif defined(HAS_PERROR)
  556         perror("\tError Message");
  557 #endif
  558         printf("\tError Number: %d\n\n", errno);
  559     }
  560     }
  561     
  562     MSG_Footer();
  563     exit(1);
  564 }
  565 
  566 
  567 void MSG_Error_ExecFailed(void)
  568 {
  569     MSG_Header("CGIWrap Error", "Script Execution Failed");
  570 
  571     printf("CGIWrap encountered an error while attempting to execute\n");
  572     printf("this script:\n\n");
  573 
  574     if ( ! MSG_QuietErrors )
  575     {
  576     if ( MSG_HTMLMessages )
  577     {
  578         printf("<DL>\n");
  579 #if defined(HAS_STRERROR)
  580         printf("<DT>Error Message: %s\n", strerror(errno));
  581 #elif defined(HAS_PERROR)
  582         perror("<DT>Error Message");
  583 #endif
  584         printf("<DT>Error Number: %d\n", errno);
  585         printf("</DL>\n");
  586     }
  587     else
  588     {
  589 #if defined(HAS_STRERROR)
  590         printf("\tError Message: %s\n", strerror(errno));
  591 #elif defined(HAS_PERROR)
  592         perror("\tError Message");
  593 #endif
  594         printf("\tError Number: %d\n\n", errno);
  595     }
  596 
  597     printf(
  598     "This message usually indicates there is a problem with the script\n"
  599     "itself. Often this indicates either that the #! line of the script\n"
  600     "is incorrect, or the script was uploaded in binary mode instead of\n"
  601     "ascii mode. Check to make sure that the script does not have\n"
  602     "control-M's at the end of every line. That will prevent it from\n"
  603     "executing. An easy fix that takes care of this most of the time\n"
  604     "is to put '#!/.../perl --' instead of '#!/.../perl' on the first\n"
  605     "line of the script.\n"
  606     "This is typically a problem if the script was edited or uploaded\n"
  607     "from a DOS/Windows/Macintosh station to a unix based server.\n"
  608     );
  609 
  610     if ( MSG_HTMLMessages )
  611     {
  612         printf("<P>\n");
  613     }
  614     
  615     printf(
  616     "If you are not the owner of this script, please forward this error\n"
  617     "and the URL that caused it to the script owner. That is often the\n"
  618     "component in the URL right after /cgiwrap/.\n"
  619     );
  620     }
  621     
  622     MSG_Footer();
  623     exit(1);
  624 }
  625 
  626 void MSG_Error_NoSuchUser(char *user)
  627 {
  628     if ( MSG_QuietErrors )
  629     {
  630         MSG_Error_RequestError();
  631     }
  632     else
  633     {
  634     MSG_Header("CGIWrap Error", "User not found");
  635 
  636     printf("CGIWrap was unable to find the user '%s' in the\n", 
  637         HTMLEncode(user));
  638     printf("password file on this server.\n\n");
  639     if ( MSG_HTMLMessages )
  640     {
  641         printf("<P>\n");
  642     }
  643     printf("Check the URL and try again.\n");
  644 
  645     MSG_Footer();
  646     exit(1);
  647     }
  648 }
  649 
  650 void MSG_Error_NoScriptDir(void)
  651 {
  652     if ( MSG_QuietErrors )
  653     {
  654         MSG_Error_RequestError();
  655     }
  656     else
  657     {
  658         MSG_Header("CGIWrap Error", "Script dir not found");
  659 
  660         printf("The specified user does not have a script directory set up\n");
  661         printf("for execution of cgi scripts, or the directory permissions\n");
  662         printf("prevent cgiwrap from using that directory.");
  663     }
  664     MSG_Footer();
  665     exit(1);
  666 }
  667 
  668 void MSG_Error_ServerConfigError(void)
  669 {
  670 #ifdef CONF_QUIET_ERRORS
  671     MSG_Header("CGIWrap Error", "Server Configuration Error");
  672 
  673     printf(
  674 "There is a problem with the server configuration for execution\n"
  675 "of CGI scripts. Please contact the server administrator.\n"
  676     );
  677 
  678     MSG_Footer();
  679 #endif
  680     exit(1);
  681 }
  682 
  683 void MSG_Error_RequestError(void)
  684 {
  685 #ifdef CONF_QUIET_ERRORS
  686     MSG_Header("CGIWrap Error", "Request Error");
  687 
  688     printf(
  689 "There is a problem with the request. Please contact the owner of the site you are\n"
  690 "trying to access.\n"
  691     );
  692 
  693     MSG_Footer();
  694 #endif
  695     exit(1);
  696 }
  697 
  698 
  699 void MSG_BoxedText(char *string)
  700 {
  701     int i, l; 
  702     
  703     l = strlen(string); 
  704     for (i=0; i<=l+3; i++)
  705     {
  706         printf("*");
  707     }
  708     printf("\n* %s *\n", string);
  709     for (i=0; i<=l+3; i++)
  710     {
  711         printf("*");
  712     }
  713     printf("\n");
  714 }