Replacing strsav() with string_append()
authorjongfoster <jongfoster@users.sourceforge.net>
Tue, 22 Jan 2002 23:31:43 +0000 (23:31 +0000)
committerjongfoster <jongfoster@users.sourceforge.net>
Tue, 22 Jan 2002 23:31:43 +0000 (23:31 +0000)
loadcfg.c

index 3c3f923..48b72a1 100644 (file)
--- a/loadcfg.c
+++ b/loadcfg.c
@@ -1,4 +1,4 @@
-const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.28 2001/12/30 14:07:32 steudten Exp $";
+const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.29 2002/01/17 21:02:30 jongfoster Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/loadcfg.c,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/loadcfg.c,v $
@@ -35,6 +35,11 @@ const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.28 2001/12/30 14:07:32 steudten E
  *
  * Revisions   :
  *    $Log: loadcfg.c,v $
  *
  * Revisions   :
  *    $Log: loadcfg.c,v $
+ *    Revision 1.29  2002/01/17 21:02:30  jongfoster
+ *    Moving all our URL and URL pattern parsing code to urlmatch.c.
+ *
+ *    Renaming free_url to free_url_spec, since it frees a struct url_spec.
+ *
  *    Revision 1.28  2001/12/30 14:07:32  steudten
  *    - Add signal handling (unix)
  *    - Add SIGHUP handler (unix)
  *    Revision 1.28  2001/12/30 14:07:32  steudten
  *    - Add signal handling (unix)
  *    - Add SIGHUP handler (unix)
@@ -246,6 +251,7 @@ const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.28 2001/12/30 14:07:32 steudten E
 #include <fcntl.h>
 #include <errno.h>
 #include <ctype.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <ctype.h>
+#include <assert.h>
 
 #ifdef _WIN32
 
 
 #ifdef _WIN32
 
@@ -361,7 +367,7 @@ static struct file_list *current_configfile = NULL;
 #define hash_show_on_task_bar           215410365ul /* "show-on-task-bar" */
 
 
 #define hash_show_on_task_bar           215410365ul /* "show-on-task-bar" */
 
 
-static void savearg(char *c, char *o, struct configuration_spec * config);
+static void savearg(char *command, char *argument, struct configuration_spec * config);
 
 /*********************************************************************
  *
 
 /*********************************************************************
  *
@@ -502,6 +508,7 @@ struct configuration_spec * load_config(void)
    config->multi_threaded    = 1;
    config->hport             = HADDR_PORT;
    config->buffer_limit      = 4096 * 1024;
    config->multi_threaded    = 1;
    config->hport             = HADDR_PORT;
    config->buffer_limit      = 4096 * 1024;
+   config->proxy_args        = strdup("");
 
    if ((configfp = fopen(configfile, "r")) == NULL)
    {
 
    if ((configfp = fopen(configfile, "r")) == NULL)
    {
@@ -614,7 +621,7 @@ struct configuration_spec * load_config(void)
             {
                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
                      "deny-access directive in configuration file.");
             {
                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
                      "deny-access directive in configuration file.");
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   "<br>\nWARNING: Wrong number of parameters for "
                   "deny-access directive in configuration file.<br><br>\n");
                continue;
                   "<br>\nWARNING: Wrong number of parameters for "
                   "deny-access directive in configuration file.<br><br>\n");
                continue;
@@ -635,12 +642,12 @@ struct configuration_spec * load_config(void)
             {
                log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
                      "directive in configuration file: \"%s\"", vec[0]);
             {
                log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
                      "directive in configuration file: \"%s\"", vec[0]);
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   "<br>\nWARNING: Invalid source IP for deny-access directive"
                   " in configuration file: \"");
                   "<br>\nWARNING: Invalid source IP for deny-access directive"
                   " in configuration file: \"");
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   vec[0]);
                   vec[0]);
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   "\"<br><br>\n");
                freez(cur_acl);
                continue;
                   "\"<br><br>\n");
                freez(cur_acl);
                continue;
@@ -651,12 +658,12 @@ struct configuration_spec * load_config(void)
                {
                   log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
                         "directive in configuration file: \"%s\"", vec[0]);
                {
                   log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
                         "directive in configuration file: \"%s\"", vec[0]);
-                  config->proxy_args = strsav( config->proxy_args,
+                  string_append(&config->proxy_args,
                      "<br>\nWARNING: Invalid destination IP for deny-access directive"
                      " in configuration file: \"");
                      "<br>\nWARNING: Invalid destination IP for deny-access directive"
                      " in configuration file: \"");
-                  config->proxy_args = strsav( config->proxy_args,
+                  string_append(&config->proxy_args,
                      vec[0]);
                      vec[0]);
-                  config->proxy_args = strsav( config->proxy_args,
+                  string_append(&config->proxy_args,
                      "\"<br><br>\n");
                   freez(cur_acl);
                   continue;
                      "\"<br><br>\n");
                   freez(cur_acl);
                   continue;
@@ -720,7 +727,7 @@ struct configuration_spec * load_config(void)
             {
                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
                      "directive in configuration file.");
             {
                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
                      "directive in configuration file.");
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   "<br>\nWARNING: Wrong number of parameters for "
                   "forward directive in configuration file.");
                continue;
                   "<br>\nWARNING: Wrong number of parameters for "
                   "forward directive in configuration file.");
                continue;
@@ -742,7 +749,7 @@ struct configuration_spec * load_config(void)
             {
                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
                      "directive in configuration file.");
             {
                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
                      "directive in configuration file.");
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   "<br>\nWARNING: Bad URL specifier for "
                   "forward directive in configuration file.");
                continue;
                   "<br>\nWARNING: Bad URL specifier for "
                   "forward directive in configuration file.");
                continue;
@@ -783,7 +790,7 @@ struct configuration_spec * load_config(void)
             {
                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
                      "forward-socks4 directive in configuration file.");
             {
                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
                      "forward-socks4 directive in configuration file.");
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   "<br>\nWARNING: Wrong number of parameters for "
                   "forward-socks4 directive in configuration file.");
                continue;
                   "<br>\nWARNING: Wrong number of parameters for "
                   "forward-socks4 directive in configuration file.");
                continue;
@@ -805,7 +812,7 @@ struct configuration_spec * load_config(void)
             {
                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
                      "directive in configuration file.");
             {
                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
                      "directive in configuration file.");
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   "<br>\nWARNING: Bad URL specifier for "
                   "forward-socks4 directive in configuration file.");
                continue;
                   "<br>\nWARNING: Bad URL specifier for "
                   "forward-socks4 directive in configuration file.");
                continue;
@@ -864,7 +871,7 @@ struct configuration_spec * load_config(void)
             {
                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
                      "forward-socks4a directive in configuration file.");
             {
                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
                      "forward-socks4a directive in configuration file.");
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   "<br>\nWARNING: Wrong number of parameters for "
                   "forward-socks4a directive in configuration file.");
                continue;
                   "<br>\nWARNING: Wrong number of parameters for "
                   "forward-socks4a directive in configuration file.");
                continue;
@@ -886,7 +893,7 @@ struct configuration_spec * load_config(void)
             {
                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
                      "directive in configuration file.");
             {
                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
                      "directive in configuration file.");
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   "<br>\nWARNING: Bad URL specifier for "
                   "forward-socks4a directive in configuration file.");
                continue;
                   "<br>\nWARNING: Bad URL specifier for "
                   "forward-socks4a directive in configuration file.");
                continue;
@@ -979,7 +986,7 @@ struct configuration_spec * load_config(void)
             {
                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
                      "permit-access directive in configuration file.");
             {
                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
                      "permit-access directive in configuration file.");
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   "<br>\nWARNING: Wrong number of parameters for "
                   "permit-access directive in configuration file.<br><br>\n");
 
                   "<br>\nWARNING: Wrong number of parameters for "
                   "permit-access directive in configuration file.<br><br>\n");
 
@@ -1001,12 +1008,12 @@ struct configuration_spec * load_config(void)
             {
                log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
                      "directive in configuration file: \"%s\"", vec[0]);
             {
                log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
                      "directive in configuration file: \"%s\"", vec[0]);
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   "<br>\nWARNING: Invalid source IP for permit-access directive"
                   " in configuration file: \"");
                   "<br>\nWARNING: Invalid source IP for permit-access directive"
                   " in configuration file: \"");
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   vec[0]);
                   vec[0]);
-               config->proxy_args = strsav( config->proxy_args,
+               string_append(&config->proxy_args,
                   "\"<br><br>\n");
                freez(cur_acl);
                continue;
                   "\"<br><br>\n");
                freez(cur_acl);
                continue;
@@ -1018,12 +1025,12 @@ struct configuration_spec * load_config(void)
                   log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
                         "permit-access directive in configuration file: \"%s\"",
                         vec[0]);
                   log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
                         "permit-access directive in configuration file: \"%s\"",
                         vec[0]);
-                  config->proxy_args = strsav( config->proxy_args,
+                  string_append(&config->proxy_args,
                      "<br>\nWARNING: Invalid destination IP for permit-access directive"
                      " in configuration file: \"");
                      "<br>\nWARNING: Invalid destination IP for permit-access directive"
                      " in configuration file: \"");
-                  config->proxy_args = strsav( config->proxy_args,
+                  string_append(&config->proxy_args,
                      vec[0]);
                      vec[0]);
-                  config->proxy_args = strsav( config->proxy_args,
+                  string_append(&config->proxy_args,
                      "\"<br><br>\n");
                   freez(cur_acl);
                   continue;
                      "\"<br><br>\n");
                   freez(cur_acl);
                   continue;
@@ -1240,9 +1247,9 @@ struct configuration_spec * load_config(void)
             /* log_error(LOG_LEVEL_ERROR, "Unrecognized directive (%luul) in "
                   "configuration file: \"%s\"", hash_string( cmd ), buf);
            */
             /* log_error(LOG_LEVEL_ERROR, "Unrecognized directive (%luul) in "
                   "configuration file: \"%s\"", hash_string( cmd ), buf);
            */
-            config->proxy_args = strsav( config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
-            config->proxy_args = strsav( config->proxy_args, buf);
-            config->proxy_args = strsav( config->proxy_args, "<br><br>\n");
+            string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
+            string_append(&config->proxy_args, buf);
+            string_append(&config->proxy_args, "<br><br>\n");
             continue;
 
 /****************************************************************************/
             continue;
 
 /****************************************************************************/
@@ -1251,6 +1258,11 @@ struct configuration_spec * load_config(void)
 
    fclose(configfp);
 
 
    fclose(configfp);
 
+   if (NULL == config->proxy_args)
+   {
+      log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
+   }
+
    init_error_log(Argv[0], config->logfile, config->debug);
 
    if (config->actions_file)
    init_error_log(Argv[0], config->logfile, config->debug);
 
    if (config->actions_file)
@@ -1392,54 +1404,69 @@ struct configuration_spec * load_config(void)
  * Function    :  savearg
  *
  * Description :  Called from `load_config'.  It saves each non-empty
  * Function    :  savearg
  *
  * Description :  Called from `load_config'.  It saves each non-empty
- *                and non-comment line from config into a list.  This
- *                list is used to create the show-proxy-args page.
+ *                and non-comment line from config into
+ *                config->proxy_args.  This is used to create the
+ *                show-proxy-args page.  On error, frees
+ *                config->proxy_args and sets it to NULL
  *
  * Parameters  :
  *
  * Parameters  :
- *          1  :  c = config setting that was found
- *          2  :  o = the setting's argument (if any)
+ *          1  :  command = config setting that was found
+ *          2  :  argument = the setting's argument (if any)
  *
  * Returns     :  N/A
  *
  *********************************************************************/
  *
  * Returns     :  N/A
  *
  *********************************************************************/
-static void savearg(char *c, char *o, struct configuration_spec * config)
+static void savearg(char *command, char *argument, struct configuration_spec * config)
 {
 {
-   char buf[BUFFER_SIZE];
+   char * buf;
+   char * s;
+
+   assert(command);
+   assert(*command);
+   assert(argument);
 
 
-   *buf = '\0';
+   buf = strdup("");
 
 
-   if ( ( NULL != c ) && ( '\0' != *c ) )
+   s = html_encode(command);
+   if (NULL == s)
    {
    {
-      if ((c = html_encode(c)))
-      {
-         sprintf(buf, "<a href=\"" REDIRECT_URL "option#%s\">%s</a> ", c, c);
-      }
-      freez(c);
+      freez(buf);
+      freez(config->proxy_args);
+      return;
    }
    }
-   if ( ( NULL != o ) && ( '\0' != *o ) )
+   string_append(&buf, "<a href=\"" REDIRECT_URL "option#");
+   string_append(&buf, s);
+   string_append(&buf, "\">");
+   string_join  (&buf, s);
+   string_append(&buf, "</a> ");
+
+   if ( (NULL != argument) && ('\0' != *argument) )
    {
    {
-      if ((o = html_encode(o)))
+      s = html_encode(argument);
+      if (NULL == s)
       {
       {
-         if (strncmpic(o, "http://", 7) == 0)
-         {
-            strcat(buf, "<a href=\"");
-            strcat(buf, o);
-            strcat(buf, "\">");
-            strcat(buf, o);
-            strcat(buf, "</a>");
-         }
-         else
-         {
-            strcat(buf, o);
-         }
+         freez(buf);
+         freez(config->proxy_args);
+         return;
       }
       }
-      freez(o);
-   }
 
 
-   strcat(buf, "<br>\n");
+      if (strncmpic(argument, "http://", 7) == 0)
+      {
+         string_append(&buf, "<a href=\"");
+         string_append(&buf, s);
+         string_append(&buf, "\">");
+         string_join  (&buf, s);
+         string_append(&buf, "</a>");
+      }
+      else
+      {
+         string_join  (&buf, s);
+      }
+   }
 
 
-   config->proxy_args = strsav(config->proxy_args, buf);
+   string_append(&buf, "<br>\n");
 
 
+   string_join(&config->proxy_args, buf);
 }
 
 
 }