Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
[privoxy.git] / loadcfg.c
similarity index 82%
rename from src/loadcfg.c
rename to loadcfg.c
index 3471d56..831af1c 100644 (file)
+++ b/loadcfg.c
@@ -1,7 +1,7 @@
-const char loadcfg_rcs[] = "$Id: loadcfg.c,v 2.5 2003/09/22 00:33:01 david__schmidt Exp $";
+const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.48.2.7 2006/02/02 17:29:16 david__schmidt Exp $";
 /*********************************************************************
  *
- * File        :  $Source: /cvsroot/ijbswa/current/src/loadcfg.c,v $
+ * File        :  $Source: /cvsroot/ijbswa/current/Attic/loadcfg.c,v $
  *
  * Purpose     :  Loads settings from the configuration file into
  *                global variables.  This file contains both the
@@ -35,28 +35,29 @@ const char loadcfg_rcs[] = "$Id: loadcfg.c,v 2.5 2003/09/22 00:33:01 david__schm
  *
  * Revisions   :
  *    $Log: loadcfg.c,v $
- *    Revision 2.5  2003/09/22 00:33:01  david__schmidt
- *    Enable sending a custom 'blocked' image.  Shows up as
- *    "image-blocker-custom-file" parameter in config, and
- *    "+set-image-blocker{custom}" in action files.
+ *    Revision 1.48.2.7  2006/02/02 17:29:16  david__schmidt
+ *    Don't forget to malloc space for the null terminator...
  *
- *    Revision 2.4  2002/12/28 03:58:19  david__schmidt
- *    Initial drop of dashboard instrumentation - enabled with
- *    --enable-activity-console
+ *    Revision 1.48.2.6  2006/01/29 23:10:56  david__schmidt
+ *    Multiple filter file support
  *
- *    Revision 2.3  2002/09/19 03:48:29  iwanttokeepanon
- *    Just moved "int i" up 3 lines in function unload_configfile, out of the "ifdef FEATURE_ACL" clause.  I disable ACL and it was not compiling because "int i" was ifdef(d) out.  I noticed this in the past, but am just now in a spot where I can change/commit stuff ... long live broadband!
+ *    Revision 1.48.2.5  2003/05/08 15:17:25  oes
+ *    Closed two memory leaks; hopefully the last remaining ones
+ *    (in the main execution paths, anyway).
  *
- *    Revision 2.2  2002/09/04 15:48:33  oes
- *    Synced with the stable branch:
- *        Revision 1.48.2.1  2002/08/21 17:58:05  oes
- *        Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
+ *    Revision 1.48.2.4  2003/04/11 12:06:14  oes
+ *    Addressed bug #719435
+ *     - Extraneous filterfile directives now logged as errors
+ *     - This and unrecnonised directives now really obvious on status page
  *
- *    Revision 2.1  2002/06/04 17:22:36  jongfoster
- *    Adding comments
+ *    Revision 1.48.2.3  2003/03/11 11:53:59  oes
+ *    Cosmetic: Renamed cryptic variable
  *
- *    Revision 2.0  2002/06/04 14:34:21  jongfoster
- *    Moving source files to src/
+ *    Revision 1.48.2.2  2002/11/12 16:28:20  oes
+ *    Move unrelated variable declaration out of #ifdef FEATURE_ACL; fixes bug #636655
+ *
+ *    Revision 1.48.2.1  2002/08/21 17:58:05  oes
+ *    Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
  *
  *    Revision 1.48  2002/05/14 21:30:38  oes
  *    savearg now uses own linking code instead of (now special-cased) add_help_link
@@ -70,7 +71,7 @@ const char loadcfg_rcs[] = "$Id: loadcfg.c,v 2.5 2003/09/22 00:33:01 david__schm
  *     - savearg now embeds option names in help links
  *
  *    Revision 1.45  2002/04/24 02:11:54  oes
- *    Jon's multiple AF patch: Allow up to MAX_ACTION_FILES actionsfile options
+ *    Jon's multiple AF patch: Allow up to MAX_AF_FILES actionsfile options
  *
  *    Revision 1.44  2002/04/08 20:37:13  swa
  *    fixed JB spelling
@@ -331,7 +332,6 @@ const char loadcfg_rcs[] = "$Id: loadcfg.c,v 2.5 2003/09/22 00:33:01 david__schm
 
 #include <stdio.h>
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <stdlib.h>
 #include <string.h>
 #include <signal.h>
@@ -355,16 +355,17 @@ const char loadcfg_rcs[] = "$Id: loadcfg.c,v 2.5 2003/09/22 00:33:01 david__schm
 /* VC++ has "_snprintf", not "snprintf" */
 #define snprintf _snprintf
 
-#else /* every platform but _WIN32 */
+#else /* ifndef _WIN32 */
 
 #ifndef __OS2__
 # include <unistd.h>
 # include <sys/wait.h>
-#endif /* ndef __OS2__ */
+#endif
 # include <sys/time.h>
+# include <sys/stat.h>
 # include <signal.h>
 
-#endif /* ndef _WIN32 */
+#endif
 
 #include "loadcfg.h"
 #include "list.h"
@@ -392,32 +393,20 @@ const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
 
 #ifdef FEATURE_TOGGLE
-/**
- * Indicates if Privoxy is currently enabled.
- */
-int g_bToggleIJB        = 1;   /* Privoxy is enabled by default. */
+/* Privoxy is enabled by default. */
+int global_toggle_state = 1;
 #endif /* def FEATURE_TOGGLE */
 
-/**
- * The filename of the config file
- */
+/* The filename of the configfile */
 const char *configfile  = NULL;
 
-/**
- * Global argument count, so that CGI functions can access the
- * command line.
+/*
+ * CGI functions will later need access to the invocation args,
+ * so we will make argc and argv global.
  */
 int Argc = 0;
-
-/**
- * Global argument list, so that CGI functions can access the
- * command line.
- */
 const char **Argv = NULL;
 
-/**
- * The configuration file which is currently loaded.
- */
 static struct file_list *current_configfile = NULL;
 
 
@@ -433,44 +422,41 @@ static struct file_list *current_configfile = NULL;
  * console and GUI specific options last).
  */
 
-#define hash_actions_file                  1196306641ul /**< "actionsfile" */
-#define hash_activity_console_address        18904208ul /**< "activity-console-address" */
-#define hash_activity_console_update_freq  3442780376ul /**< "activity-console-update-freq" */
-#define hash_admin_address                 4112573064ul /**< "admin-address" */
-#define hash_buffer_limit                  1881726070ul /**< "buffer-limit */
-#define hash_confdir                          1978389ul /**< "confdir" */
-#define hash_debug                              78263ul /**< "debug" */
-#define hash_deny_access                   1227333715ul /**< "deny-access" */
-#define hash_enable_edit_actions           2517097536ul /**< "enable-edit-actions" */
-#define hash_enable_remote_toggle          2979744683ul /**< "enable-remote-toggle" */
-#define hash_filterfile                     250887266ul /**< "filterfile" */
-#define hash_forward                          2029845ul /**< "forward" */
-#define hash_forward_socks4                3963965521ul /**< "forward-socks4" */
-#define hash_forward_socks4a               2639958518ul /**< "forward-socks4a" */
-#define hash_image_blocker_custom_file     2863352327ul /**< "image-blocker-custom-file" */
-#define hash_jarfile                          2046641ul /**< "jarfile" */
-#define hash_listen_address                1255650842ul /**< "listen-address" */
-#define hash_logdir                            422889ul /**< "logdir" */
-#define hash_logfile                          2114766ul /**< "logfile" */
-#define hash_permit_access                 3587953268ul /**< "permit-access" */
-#define hash_proxy_info_url                3903079059ul /**< "proxy-info-url" */
-#define hash_single_threaded               4250084780ul /**< "single-threaded" */
-#define hash_suppress_blocklists           1948693308ul /**< "suppress-blocklists" */
-#define hash_toggle                            447966ul /**< "toggle" */
-#define hash_trust_info_url                 430331967ul /**< "trust-info-url" */
-#define hash_trustfile                       56494766ul /**< "trustfile" */
-#define hash_usermanual                    1416668518ul /**< "user-manual" */
-
-#define hash_activity_animation            1817904738ul /**< "activity-animation" */
-#define hash_close_button_minimizes        3651284693ul /**< "close-button-minimizes" */
-#define hash_hide_console                  2048809870ul /**< "hide-console" */
-#define hash_log_buffer_size               2918070425ul /**< "log-buffer-size" */
-#define hash_log_font_name                 2866730124ul /**< "log-font-name" */
-#define hash_log_font_size                 2866731014ul /**< "log-font-size" */
-#define hash_log_highlight_messages        4032101240ul /**< "log-highlight-messages" */
-#define hash_log_max_lines                 2868344173ul /**< "log-max-lines" */
-#define hash_log_messages                  2291744899ul /**< "log-messages" */
-#define hash_show_on_task_bar               215410365ul /**< "show-on-task-bar" */
+#define hash_actions_file              1196306641ul /* "actionsfile" */
+#define hash_admin_address             4112573064ul /* "admin-address" */
+#define hash_buffer_limit              1881726070ul /* "buffer-limit */
+#define hash_confdir                      1978389ul /* "confdir" */
+#define hash_debug                          78263ul /* "debug" */
+#define hash_deny_access               1227333715ul /* "deny-access" */
+#define hash_enable_edit_actions       2517097536ul /* "enable-edit-actions" */
+#define hash_enable_remote_toggle      2979744683ul /* "enable-remote-toggle" */
+#define hash_filterfile                 250887266ul /* "filterfile" */
+#define hash_forward                      2029845ul /* "forward" */
+#define hash_forward_socks4            3963965521ul /* "forward-socks4" */
+#define hash_forward_socks4a           2639958518ul /* "forward-socks4a" */
+#define hash_jarfile                      2046641ul /* "jarfile" */
+#define hash_listen_address            1255650842ul /* "listen-address" */
+#define hash_logdir                        422889ul /* "logdir" */
+#define hash_logfile                      2114766ul /* "logfile" */
+#define hash_permit_access             3587953268ul /* "permit-access" */
+#define hash_proxy_info_url            3903079059ul /* "proxy-info-url" */
+#define hash_single_threaded           4250084780ul /* "single-threaded" */
+#define hash_suppress_blocklists       1948693308ul /* "suppress-blocklists" */
+#define hash_toggle                        447966ul /* "toggle" */
+#define hash_trust_info_url             430331967ul /* "trust-info-url" */
+#define hash_trustfile                   56494766ul /* "trustfile" */
+#define hash_usermanual                1416668518ul /* "user-manual" */
+#define hash_activity_animation        1817904738ul /* "activity-animation" */
+#define hash_close_button_minimizes    3651284693ul /* "close-button-minimizes" */
+#define hash_hide_console              2048809870ul /* "hide-console" */
+#define hash_log_buffer_size           2918070425ul /* "log-buffer-size" */
+#define hash_log_font_name             2866730124ul /* "log-font-name" */
+#define hash_log_font_size             2866731014ul /* "log-font-size" */
+#define hash_log_highlight_messages    4032101240ul /* "log-highlight-messages" */
+#define hash_log_max_lines             2868344173ul /* "log-max-lines" */
+#define hash_log_messages              2291744899ul /* "log-messages" */
+#define hash_show_on_task_bar           215410365ul /* "show-on-task-bar" */
+
 
 static void savearg(char *command, char *argument, struct configuration_spec * config);
 
@@ -491,6 +477,7 @@ void unload_configfile (void * data)
    struct configuration_spec * config = (struct configuration_spec *)data;
    struct forward_spec *cur_fwd = config->forward;
    int i;
+
 #ifdef FEATURE_ACL
    struct access_control_list *cur_acl = config->acl;
 
@@ -529,7 +516,7 @@ void unload_configfile (void * data)
    freez(config->haddr);
    freez(config->logfile);
 
-   for (i = 0; i < MAX_ACTION_FILES; i++)
+   for (i = 0; i < MAX_AF_FILES; i++)
    {
       freez(config->actions_file_short[i]);
       freez(config->actions_file[i]);
@@ -544,10 +531,17 @@ void unload_configfile (void * data)
    freez(config->jarfile);
 #endif /* def FEATURE_COOKIE_JAR */
 
-   freez(config->re_filterfile);
+#ifdef FEATURE_TRUST
+   freez(config->trustfile);
+   list_remove_all(config->trust_info);
+#endif /* def FEATURE_TRUST */
+
+   for (i = 0; i < MAX_AF_FILES; i++)
+   {
+      freez(config->re_filterfile[i]);
+   }
 
-   freez(config->image_blocker_data);
-   freez(config->image_blocker_format);
+   freez(config);
 }
 
 
@@ -589,15 +583,13 @@ void unload_current_config_file(void)
 struct configuration_spec * load_config(void)
 {
    char buf[BUFFER_SIZE];
-   char *p, *q, *image_buf, *image_path;
-   FILE *configfp = NULL,
-        *imagefp = NULL;
+   char *p, *q;
+   FILE *configfp = NULL;
    struct configuration_spec * config = NULL;
    struct client_state * fake_csp;
    struct file_list *fs;
-   struct stat statbuf[1];
    unsigned long linenum = 0;
-   int i, file_size, bytes_read;
+   int i;
 
    if ( !check_file_changed(current_configfile, configfile, &fs))
    {
@@ -613,7 +605,7 @@ struct configuration_spec * load_config(void)
    log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
 
 #ifdef FEATURE_TOGGLE
-   g_bToggleIJB      = 1;
+   global_toggle_state      = 1;
 #endif /* def FEATURE_TOGGLE */
 
    fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
@@ -645,10 +637,6 @@ struct configuration_spec * load_config(void)
    config->buffer_limit      = 4096 * 1024;
    config->usermanual        = strdup(USER_MANUAL_URL);
    config->proxy_args        = strdup("");
-#ifdef FEATURE_ACTIVITY_CONSOLE
-   config->activity_port     = ACTIVTY_ADDR_PORT;
-   config->activity_freq     = 5;
-#endif /* def FEATURE_ACTIVITY_CONSOLE */
 
    if ((configfp = fopen(configfile, "r")) == NULL)
    {
@@ -716,16 +704,16 @@ struct configuration_spec * load_config(void)
  * *************************************************************************/
          case hash_actions_file :
             i = 0;
-            while ((i < MAX_ACTION_FILES) && (NULL != config->actions_file[i]))
+            while ((i < MAX_AF_FILES) && (NULL != config->actions_file[i]))
             {
                i++;
             }
 
-            if (i >= MAX_ACTION_FILES)
+            if (i >= MAX_AF_FILES)
             {
                log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
-                  "(You can increase this limit by changing MAX_ACTION_FILES in project.h and recompiling).",
-                  MAX_ACTION_FILES);
+                  "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
+                  MAX_AF_FILES);
             }
             config->actions_file_short[i] = strdup(arg);
             p = malloc(strlen(arg) + sizeof(".action"));
@@ -739,27 +727,6 @@ struct configuration_spec * load_config(void)
             free(p);
             continue;
 
-/* *************************************************************************
- * activity-console-address [ip][:port]
- * *************************************************************************/
-#ifdef FEATURE_ACTIVITY_CONSOLE
-         case hash_activity_console_address :
-            freez(config->activity_address);
-            config->activity_address = strdup(arg);
-            continue;
-#endif /* def FEATURE_ACTIVITY_CONSOLE */
-
-/* *************************************************************************
- * activity-console-update-freq
- * *************************************************************************/
-#ifdef FEATURE_ACTIVITY_CONSOLE
-         case hash_activity_console_update_freq :
-            config->activity_freq = atoi(arg);
-            if (config->activity_freq < 1)
-              config->activity_freq = 5;
-            continue;
-#endif /* def FEATURE_ACTIVITY_CONSOLE */
-
 /* *************************************************************************
  * admin-address email-address
  * *************************************************************************/
@@ -898,6 +865,34 @@ struct configuration_spec * load_config(void)
             continue;
 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
 
+/* *************************************************************************
+ * filterfile file-name
+ * In confdir by default.
+ * *************************************************************************/
+         case hash_filterfile :
+            i = 0;
+            while ((i < MAX_AF_FILES) && (NULL != config->re_filterfile[i]))
+            {
+               i++;
+            }
+
+            if (i >= MAX_AF_FILES)
+            {
+               log_error(LOG_LEVEL_FATAL, "Too many 'filterfile' directives in config file - limit is %d.\n"
+                  "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
+                  MAX_AF_FILES);
+            }
+            config->re_filterfile_short[i] = strdup(arg);
+            p = malloc(strlen(arg) + 1);
+            if (p == NULL)
+            {
+               log_error(LOG_LEVEL_FATAL, "Out of memory");
+            }
+            strcpy(p, arg);
+            config->re_filterfile[i] = make_path(config->confdir, p);
+            free(p);
+            continue;
+
 /* *************************************************************************
  * forward url-pattern (.|http-proxy-host[:port])
  * *************************************************************************/
@@ -1241,15 +1236,6 @@ struct configuration_spec * load_config(void)
             config->proxy_info_url = strdup(arg);
             continue;
 
-/* *************************************************************************
- * re_filterfile file-name
- * In confdir by default.
- * *************************************************************************/
-         case hash_filterfile :
-            freez(config->re_filterfile);
-            config->re_filterfile = make_path(config->confdir, arg);
-            continue;
-
 /* *************************************************************************
  * single-threaded
  * *************************************************************************/
@@ -1262,7 +1248,7 @@ struct configuration_spec * load_config(void)
  * *************************************************************************/
 #ifdef FEATURE_TOGGLE
          case hash_toggle :
-            g_bToggleIJB = atoi(arg);
+            global_toggle_state = atoi(arg);
             continue;
 #endif /* def FEATURE_TOGGLE */
 
@@ -1378,87 +1364,6 @@ struct configuration_spec * load_config(void)
 
 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
 
-/* *************************************************************************
- * image-blocker-custom-file
- * *************************************************************************/
-         case hash_image_blocker_custom_file :
-            freez(config->image_blocker_data);
-            freez(config->image_blocker_format);
-            config->image_blocker_length = 0; 
-            image_path = make_path(config->confdir, arg);
-
-            /*
-             * Load up the custom image bitmap file
-             */
-            if (NULL == (imagefp = fopen(image_path, "rb")))
-            {
-              /*
-               * If we can't open the user's requested image, complain
-               */
-              log_error(LOG_LEVEL_ERROR, "Unable to load custom blocker image: %s.", image_path);
-            }
-            else
-            {
-              if (stat(image_path, statbuf) == 0)
-              {
-                file_size = statbuf->st_size;
-                image_buf = zalloc(file_size);
-                if (image_buf != NULL)
-                {
-                  bytes_read = fread(image_buf,1,file_size,imagefp);
-                  if (bytes_read > 0)
-                  {
-                    config->image_blocker_data = image_buf;
-                    config->image_blocker_length = file_size;
-                    /*
-                     * Ensure we can look into files for file signatures
-                     */
-                    if (file_size > 10)
-                    {
-                      /*
-                       * Snoop into the binary data for a filetype signature
-                       */
-                      if (memcmp(image_buf,"GIF",3) == 0)
-                        config->image_blocker_format = IMAGE_MIMETYPE_GIF;
-                      else if (memcmp(&image_buf[6],"JFIF",4) == 0)
-                        config->image_blocker_format = IMAGE_MIMETYPE_JPG;
-                      else if (memcmp(&image_buf[1],"PNG",3) == 0)
-                        config->image_blocker_format = IMAGE_MIMETYPE_PNG;
-                      else
-                      {
-                        log_error(LOG_LEVEL_ERROR, "Unsupported custom image file type.");
-                        freez(config->image_blocker_data);
-                      }
-                    }
-                    else
-                      freez(config->image_blocker_data);
-                  }
-                  else
-                  {
-                    log_error(LOG_LEVEL_ERROR, "Unable to read custom blocker image: %s", image_path, bytes_read, file_size);
-                    freez(image_buf);
-                    config->image_blocker_length = 0;
-                  }
-                }
-                else
-                  log_error(LOG_LEVEL_ERROR, "Unable to allocate memory for custom blocker image: %s.", image_path);
-              }
-              else
-                log_error(LOG_LEVEL_ERROR, "Unable to get statistics on custom blocker image file: %s", image_path);
-            }
-            freez(image_path);
-            /*
-             * If our load failed for some reason, just give the default
-             * checkerboard pattern 
-             */
-            if (config->image_blocker_data == NULL)
-            {
-              log_error(LOG_LEVEL_ERROR, "Custom blocker image processing failed; defaulting to \"pattern\".");
-              config->image_blocker_data = (char*)image_pattern_data;
-              config->image_blocker_length = image_pattern_length;
-              config->image_blocker_format = BUILTIN_IMAGE_MIMETYPE;
-            }
-            continue;
 
 /* *************************************************************************
  * Warnings about unsupported features
@@ -1511,11 +1416,10 @@ struct configuration_spec * load_config(void)
              * error.  To change back to an error, just change log level
              * to LOG_LEVEL_FATAL.
              */
-            log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
-                  "configuration file (%s).",  buf, hash_string(cmd), linenum, configfile);
-            string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
-            string_append(&config->proxy_args, buf);
-            string_append(&config->proxy_args, "<br><br>\n");
+            log_error(LOG_LEVEL_ERROR, "Ignoring unrecognized directive '%s' (%luul) in line %lu "
+                  "in configuration file (%s).",  buf, hash_string(cmd), linenum, configfile);
+            string_append(&config->proxy_args,
+               " <b><font color=\"red\">WARNING: unrecognized directive, ignored</font></b>");
             continue;
 
 /* *************************************************************************/
@@ -1560,36 +1464,6 @@ struct configuration_spec * load_config(void)
    }
 #endif /* def FEATURE_COOKIE_JAR */
 
-#ifdef FEATURE_ACTIVITY_CONSOLE
-   if ( NULL == config->activity_address )
-   {
-      config->activity_address = strdup( ACTIVTY_ADDR_DEFAULT );
-   }
-
-   if ( NULL != config->activity_address )
-   {
-      if (NULL != (p = strchr(config->activity_address, ':')))
-      {
-         *p++ = '\0';
-         if (*p)
-         {
-            config->activity_port = atoi(p);
-         }
-      }
-
-      if (config->activity_port <= 0)
-      {
-         *--p = ':';
-         log_error(LOG_LEVEL_ERROR, "invalid activity port spec %s", config->activity_address);
-      }
-      if (*config->activity_address == '\0')
-      {
-         config->activity_address = NULL;
-      }
-   }
-   log_error(LOG_LEVEL_INFO, "Sending statistics updates to %s, port %d.", config->activity_address, config->activity_port);
-#endif /* def FEATURE_ACTIVITY_CONSOLE */
-
    if ( NULL == config->haddr )
    {
       config->haddr = strdup( HADDR_DEFAULT );
@@ -1726,7 +1600,7 @@ static void savearg(char *command, char *argument, struct configuration_spec * c
     * Add config option name embedded in
     * link to it's section in the user-manual
     */
-   buf = strdup("<a href=\"");
+   buf = strdup("\n<br><a href=\"");
    string_append(&buf, config->usermanual);
    string_append(&buf, CONFIG_HELP_PREFIX);
    string_join  (&buf, string_toupper(command));
@@ -1764,8 +1638,6 @@ static void savearg(char *command, char *argument, struct configuration_spec * c
       }
    }
 
-   string_append(&buf, "<br>\n");
-
    string_join(&config->proxy_args, buf);
 }