Rebuild after ditching GNUisms in GNUMakefile.in
[privoxy.git] / loadcfg.c
1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.120 2011/07/19 11:00:20 fabiankeil Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/loadcfg.c,v $
5  *
6  * Purpose     :  Loads settings from the configuration file into
7  *                global variables.  This file contains both the
8  *                routine to load the configuration and the global
9  *                variables it writes to.
10  *
11  * Copyright   :  Written by and Copyright (C) 2001-2009 the
12  *                Privoxy team. http://www.privoxy.org/
13  *
14  *                Based on the Internet Junkbuster originally written
15  *                by and Copyright (C) 1997 Anonymous Coders and
16  *                Junkbusters Corporation.  http://www.junkbusters.com
17  *
18  *                This program is free software; you can redistribute it
19  *                and/or modify it under the terms of the GNU General
20  *                Public License as published by the Free Software
21  *                Foundation; either version 2 of the License, or (at
22  *                your option) any later version.
23  *
24  *                This program is distributed in the hope that it will
25  *                be useful, but WITHOUT ANY WARRANTY; without even the
26  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
27  *                PARTICULAR PURPOSE.  See the GNU General Public
28  *                License for more details.
29  *
30  *                The GNU General Public License should be included with
31  *                this file.  If not, you can view it at
32  *                http://www.gnu.org/copyleft/gpl.html
33  *                or write to the Free Software Foundation, Inc., 59
34  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
35  *
36  *********************************************************************/
37
38
39 #include "config.h"
40
41 #include <stdio.h>
42 #include <sys/types.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <signal.h>
46 #include <fcntl.h>
47 #include <errno.h>
48 #include <ctype.h>
49 #include <assert.h>
50
51 #ifdef _WIN32
52
53 # ifndef STRICT
54 #  define STRICT
55 # endif
56 # include <windows.h>
57
58 # include "win32.h"
59 # ifndef _WIN_CONSOLE
60 #  include "w32log.h"
61 # endif /* ndef _WIN_CONSOLE */
62
63 #else /* ifndef _WIN32 */
64
65 #ifndef __OS2__
66 # include <unistd.h>
67 # include <sys/wait.h>
68 #endif
69 # include <sys/time.h>
70 # include <sys/stat.h>
71 # include <signal.h>
72
73 #endif
74
75 #include "loadcfg.h"
76 #include "list.h"
77 #include "jcc.h"
78 #include "filters.h"
79 #include "loaders.h"
80 #include "miscutil.h"
81 #include "errlog.h"
82 #include "ssplit.h"
83 #include "encode.h"
84 #include "urlmatch.h"
85 #include "cgi.h"
86 #include "gateway.h"
87
88 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
89
90 /*
91  * Fix a problem with Solaris.  There should be no effect on other
92  * platforms.
93  * Solaris's isspace() is a macro which uses it's argument directly
94  * as an array index.  Therefore we need to make sure that high-bit
95  * characters generate +ve values, and ideally we also want to make
96  * the argument match the declared parameter type of "int".
97  */
98 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
99 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
100
101 #ifdef FEATURE_TOGGLE
102 /* Privoxy is enabled by default. */
103 int global_toggle_state = 1;
104 #endif /* def FEATURE_TOGGLE */
105
106 /* The filename of the configfile */
107 const char *configfile  = NULL;
108
109 /*
110  * CGI functions will later need access to the invocation args,
111  * so we will make argc and argv global.
112  */
113 int Argc = 0;
114 char * const * Argv = NULL;
115
116 static struct file_list *current_configfile = NULL;
117
118
119 /*
120  * This takes the "cryptic" hash of each keyword and aliases them to
121  * something a little more readable.  This also makes changing the
122  * hash values easier if they should change or the hash algorthm changes.
123  * Use the included "hash" program to find out what the hash will be
124  * for any string supplied on the command line.  (Or just put it in the
125  * config file and read the number from the error message in the log).
126  *
127  * Please keep this list sorted alphabetically (but with the Windows
128  * console and GUI specific options last).
129  */
130
131 #define hash_actions_file                1196306641ul /* "actionsfile" */
132 #define hash_accept_intercepted_requests 1513024973ul /* "accept-intercepted-requests" */
133 #define hash_admin_address               4112573064ul /* "admin-address" */
134 #define hash_allow_cgi_request_crunching  258915987ul /* "allow-cgi-request-crunching" */
135 #define hash_buffer_limit                1881726070ul /* "buffer-limit */
136 #define hash_compression_level           2464423563ul /* "compression-level" */
137 #define hash_confdir                        1978389ul /* "confdir" */
138 #define hash_connection_sharing          1348841265ul /* "connection-sharing" */
139 #define hash_debug                            78263ul /* "debug" */
140 #define hash_default_server_timeout      2530089913ul /* "default-server-timeout" */
141 #define hash_deny_access                 1227333715ul /* "deny-access" */
142 #define hash_enable_edit_actions         2517097536ul /* "enable-edit-actions" */
143 #define hash_enable_compression          3943696946ul /* "enable-compression" */
144 #define hash_enable_remote_toggle        2979744683ul /* "enable-remote-toggle" */
145 #define hash_enable_remote_http_toggle    110543988ul /* "enable-remote-http-toggle" */
146 #define hash_enforce_blocks              1862427469ul /* "enforce-blocks" */
147 #define hash_filterfile                   250887266ul /* "filterfile" */
148 #define hash_forward                        2029845ul /* "forward" */
149 #define hash_forward_socks4              3963965521ul /* "forward-socks4" */
150 #define hash_forward_socks4a             2639958518ul /* "forward-socks4a" */
151 #define hash_forward_socks5              3963965522ul /* "forward-socks5" */
152 #define hash_forwarded_connect_retries    101465292ul /* "forwarded-connect-retries" */
153 #define hash_handle_as_empty_returns_ok  1444873247ul /* "handle-as-empty-doc-returns-ok" */
154 #define hash_hostname                      10308071ul /* "hostname" */
155 #define hash_keep_alive_timeout          3878599515ul /* "keep-alive-timeout" */
156 #define hash_listen_address              1255650842ul /* "listen-address" */
157 #define hash_logdir                          422889ul /* "logdir" */
158 #define hash_logfile                        2114766ul /* "logfile" */
159 #define hash_max_client_connections      3595884446ul /* "max-client-connections" */
160 #define hash_permit_access               3587953268ul /* "permit-access" */
161 #define hash_proxy_info_url              3903079059ul /* "proxy-info-url" */
162 #define hash_single_threaded             4250084780ul /* "single-threaded" */
163 #define hash_socket_timeout              1809001761ul /* "socket-timeout" */
164 #define hash_split_large_cgi_forms        671658948ul /* "split-large-cgi-forms" */
165 #define hash_suppress_blocklists         1948693308ul /* "suppress-blocklists" */
166 #define hash_templdir                      11067889ul /* "templdir" */
167 #define hash_toggle                          447966ul /* "toggle" */
168 #define hash_trust_info_url               430331967ul /* "trust-info-url" */
169 #define hash_trustfile                     56494766ul /* "trustfile" */
170 #define hash_usermanual                  1416668518ul /* "user-manual" */
171 #define hash_activity_animation          1817904738ul /* "activity-animation" */
172 #define hash_close_button_minimizes      3651284693ul /* "close-button-minimizes" */
173 #define hash_hide_console                2048809870ul /* "hide-console" */
174 #define hash_log_buffer_size             2918070425ul /* "log-buffer-size" */
175 #define hash_log_font_name               2866730124ul /* "log-font-name" */
176 #define hash_log_font_size               2866731014ul /* "log-font-size" */
177 #define hash_log_highlight_messages      4032101240ul /* "log-highlight-messages" */
178 #define hash_log_max_lines               2868344173ul /* "log-max-lines" */
179 #define hash_log_messages                2291744899ul /* "log-messages" */
180 #define hash_show_on_task_bar             215410365ul /* "show-on-task-bar" */
181
182
183 static void savearg(char *command, char *argument, struct configuration_spec * config);
184
185 /*********************************************************************
186  *
187  * Function    :  unload_configfile
188  *
189  * Description :  Free the config structure and all components.
190  *
191  * Parameters  :
192  *          1  :  data: struct configuration_spec to unload
193  *
194  * Returns     :  N/A
195  *
196  *********************************************************************/
197 static void unload_configfile (void * data)
198 {
199    struct configuration_spec * config = (struct configuration_spec *)data;
200    struct forward_spec *cur_fwd = config->forward;
201    int i;
202
203 #ifdef FEATURE_ACL
204    struct access_control_list *cur_acl = config->acl;
205
206    while (cur_acl != NULL)
207    {
208       struct access_control_list * next_acl = cur_acl->next;
209       free(cur_acl);
210       cur_acl = next_acl;
211    }
212    config->acl = NULL;
213 #endif /* def FEATURE_ACL */
214
215    while (cur_fwd != NULL)
216    {
217       struct forward_spec * next_fwd = cur_fwd->next;
218       free_url_spec(cur_fwd->url);
219
220       freez(cur_fwd->gateway_host);
221       freez(cur_fwd->forward_host);
222       free(cur_fwd);
223       cur_fwd = next_fwd;
224    }
225    config->forward = NULL;
226
227    freez(config->confdir);
228    freez(config->logdir);
229    freez(config->templdir);
230    freez(config->hostname);
231
232    for (i = 0; i < MAX_LISTENING_SOCKETS; i++)
233    {
234       freez(config->haddr[i]);
235    }
236    freez(config->logfile);
237
238    for (i = 0; i < MAX_AF_FILES; i++)
239    {
240       freez(config->actions_file_short[i]);
241       freez(config->actions_file[i]);
242       freez(config->re_filterfile_short[i]);
243       freez(config->re_filterfile[i]);
244    }
245
246    freez(config->admin_address);
247    freez(config->proxy_info_url);
248    freez(config->proxy_args);
249    freez(config->usermanual);
250
251 #ifdef FEATURE_TRUST
252    freez(config->trustfile);
253    list_remove_all(config->trust_info);
254 #endif /* def FEATURE_TRUST */
255
256    freez(config);
257 }
258
259
260 #ifdef FEATURE_GRACEFUL_TERMINATION
261 /*********************************************************************
262  *
263  * Function    :  unload_current_config_file
264  *
265  * Description :  Unloads current config file - reset to state at
266  *                beginning of program.
267  *
268  * Parameters  :  None
269  *
270  * Returns     :  N/A
271  *
272  *********************************************************************/
273 void unload_current_config_file(void)
274 {
275    if (current_configfile)
276    {
277       current_configfile->unloader = unload_configfile;
278       current_configfile = NULL;
279    }
280 }
281 #endif
282
283
284 /*********************************************************************
285  *
286  * Function    :  parse_toggle_value
287  *
288  * Description :  Parse the value of a directive that can only be
289  *                enabled or disabled. Terminates with a fatal error
290  *                if the value is NULL or something other than 0 or 1.
291  *
292  * Parameters  :
293  *          1  :  name:  The name of the directive. Used for log messages.
294  *          2  :  value: The value to parse
295  *
296  *
297  * Returns     :  The numerical toggle state
298  *
299  *********************************************************************/
300 static int parse_toggle_state(const char *name, const char *value)
301 {
302    int toggle_state;
303    assert(name != NULL);
304    assert(value != NULL);
305
306    if ((value == NULL) || (*value == '\0'))
307    {
308       log_error(LOG_LEVEL_FATAL, "Directive %s used without argument", name);
309    }
310
311    toggle_state = atoi(value);
312
313    /*
314     * Also check the length as atoi() doesn't mind
315     * garbage after a valid integer, but we do.
316     */
317    if (((toggle_state != 0) && (toggle_state != 1)) || (strlen(value) != 1))
318    {
319       log_error(LOG_LEVEL_FATAL,
320          "Directive %s used with invalid argument '%s'. Use either '0' or '1'.",
321          name, value);
322    }
323
324    return toggle_state;
325
326 }
327
328
329 /*********************************************************************
330  *
331  * Function    :  load_config
332  *
333  * Description :  Load the config file and all parameters.
334  *
335  *                XXX: more than thousand lines long
336  *                and thus in serious need of refactoring.
337  *
338  * Parameters  :  None
339  *
340  * Returns     :  The configuration_spec, or NULL on error.
341  *
342  *********************************************************************/
343 struct configuration_spec * load_config(void)
344 {
345    char *buf = NULL;
346    char *p, *q;
347    FILE *configfp = NULL;
348    struct configuration_spec * config = NULL;
349    struct client_state * fake_csp;
350    struct file_list *fs;
351    unsigned long linenum = 0;
352    int i;
353    char *logfile = NULL;
354
355    if (!check_file_changed(current_configfile, configfile, &fs))
356    {
357       /* No need to load */
358       return ((struct configuration_spec *)current_configfile->f);
359    }
360    if (NULL == fs)
361    {
362       log_error(LOG_LEVEL_FATAL,
363          "can't check configuration file '%s':  %E", configfile);
364       return NULL;
365    }
366
367    if (NULL != current_configfile)
368    {
369       log_error(LOG_LEVEL_INFO, "Reloading configuration file '%s'", configfile);
370    }
371
372 #ifdef FEATURE_TOGGLE
373    global_toggle_state = 1;
374 #endif /* def FEATURE_TOGGLE */
375
376    fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
377
378    if (NULL == config)
379    {
380       freez(fs->filename);
381       freez(fs);
382       log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
383       return NULL;
384    }
385
386    /*
387     * This is backwards from how it's usually done.
388     * Following the usual pattern, "fs" would be stored in a member
389     * variable in "csp", and then we'd access "config" from "fs->f",
390     * using a cast.  However, "config" is used so often that a
391     * cast each time would be very ugly, and the extra indirection
392     * would waste CPU cycles.  Therefore we store "config" in
393     * "csp->config", and "fs" in "csp->config->config_file_list".
394     */
395    config->config_file_list = fs;
396
397    /*
398     * Set to defaults
399     */
400    config->multi_threaded            = 1;
401    config->buffer_limit              = 4096 * 1024;
402    config->usermanual                = strdup(USER_MANUAL_URL);
403    config->proxy_args                = strdup("");
404    config->forwarded_connect_retries = 0;
405    config->max_client_connections    = 0;
406    config->socket_timeout            = 300; /* XXX: Should be a macro. */
407 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
408    config->default_server_timeout    = 0;
409    config->keep_alive_timeout        = DEFAULT_KEEP_ALIVE_TIMEOUT;
410    config->feature_flags            &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
411    config->feature_flags            &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
412 #endif
413    config->feature_flags            &= ~RUNTIME_FEATURE_CGI_TOGGLE;
414    config->feature_flags            &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
415    config->feature_flags            &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
416    config->feature_flags            &= ~RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
417 #ifdef FEATURE_COMPRESSION
418    config->feature_flags            &= ~RUNTIME_FEATURE_COMPRESSION;
419    /*
420     * XXX: Run some benchmarks to see if there are better default values.
421     */
422    config->compression_level         = 1;
423 #endif
424
425    configfp = fopen(configfile, "r");
426    if (NULL == configfp)
427    {
428       log_error(LOG_LEVEL_FATAL,
429          "can't open configuration file '%s':  %E", configfile);
430       /* Never get here - LOG_LEVEL_FATAL causes program exit */
431    }
432
433    while (read_config_line(configfp, &linenum, &buf) != NULL)
434    {
435       char cmd[BUFFER_SIZE];
436       char arg[BUFFER_SIZE];
437       char tmp[BUFFER_SIZE];
438 #ifdef FEATURE_ACL
439       struct access_control_list *cur_acl;
440 #endif /* def FEATURE_ACL */
441       struct forward_spec *cur_fwd;
442       int vec_count;
443       char *vec[3];
444       unsigned long directive_hash;
445
446       strlcpy(tmp, buf, sizeof(tmp));
447
448       /* Copy command (i.e. up to space or tab) into cmd */
449       p = buf;
450       q = cmd;
451       while (*p && (*p != ' ') && (*p != '\t'))
452       {
453          *q++ = *p++;
454       }
455       *q = '\0';
456
457       /* Skip over the whitespace in buf */
458       while (*p && ((*p == ' ') || (*p == '\t')))
459       {
460          p++;
461       }
462
463       /* Copy the argument into arg */
464       if (strlcpy(arg, p, sizeof(arg)) >= sizeof(arg))
465       {
466          log_error(LOG_LEVEL_FATAL, "Config line too long: %s", buf);
467       }
468
469       /* Should never happen, but check this anyway */
470       if (*cmd == '\0')
471       {
472          freez(buf);
473          continue;
474       }
475
476       /* Make sure the command field is lower case */
477       for (p = cmd; *p; p++)
478       {
479          if (ijb_isupper(*p))
480          {
481             *p = (char)ijb_tolower(*p);
482          }
483       }
484
485       directive_hash = hash_string(cmd);
486       switch (directive_hash)
487       {
488 /* *************************************************************************
489  * actionsfile actions-file-name
490  * In confdir by default
491  * *************************************************************************/
492          case hash_actions_file :
493             i = 0;
494             while ((i < MAX_AF_FILES) && (NULL != config->actions_file[i]))
495             {
496                i++;
497             }
498
499             if (i >= MAX_AF_FILES)
500             {
501                log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
502                   "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
503                   MAX_AF_FILES);
504             }
505             config->actions_file_short[i] = strdup(arg);
506             config->actions_file[i] = make_path(config->confdir, arg);
507
508             break;
509 /* *************************************************************************
510  * accept-intercepted-requests
511  * *************************************************************************/
512          case hash_accept_intercepted_requests:
513             if (parse_toggle_state(cmd, arg) == 1)
514             {
515                config->feature_flags |= RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
516             }
517             else
518             {
519                config->feature_flags &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
520             }
521             break;
522
523 /* *************************************************************************
524  * admin-address email-address
525  * *************************************************************************/
526          case hash_admin_address :
527             freez(config->admin_address);
528             config->admin_address = strdup(arg);
529             break;
530
531 /* *************************************************************************
532  * allow-cgi-request-crunching
533  * *************************************************************************/
534          case hash_allow_cgi_request_crunching:
535             if (parse_toggle_state(cmd, arg) == 1)
536             {
537                config->feature_flags |= RUNTIME_FEATURE_CGI_CRUNCHING;
538             }
539             else
540             {
541                config->feature_flags &= ~RUNTIME_FEATURE_CGI_CRUNCHING;
542             }
543             break;
544
545 /* *************************************************************************
546  * buffer-limit n
547  * *************************************************************************/
548          case hash_buffer_limit :
549             config->buffer_limit = (size_t)(1024 * atoi(arg));
550             break;
551
552 /* *************************************************************************
553  * confdir directory-name
554  * *************************************************************************/
555          case hash_confdir :
556             freez(config->confdir);
557             config->confdir = make_path( NULL, arg);
558             break;
559
560 /* *************************************************************************
561  * compression-level 0-9
562  * *************************************************************************/
563 #ifdef FEATURE_COMPRESSION
564          case hash_compression_level :
565             if (*arg != '\0')
566             {
567                int compression_level = atoi(arg);
568                if (-1 <= compression_level && compression_level <= 9)
569                {
570                   config->compression_level = compression_level;;
571                }
572                else
573                {
574                   log_error(LOG_LEVEL_FATAL,
575                      "Invalid compression-level value: %s", arg);
576                }
577             }
578             else
579             {
580                log_error(LOG_LEVEL_FATAL,
581                   "Invalid compression-level directive. Compression value missing");
582             }
583             break;
584 #endif
585
586 /* *************************************************************************
587  * connection-sharing (0|1)
588  * *************************************************************************/
589 #ifdef FEATURE_CONNECTION_SHARING
590          case hash_connection_sharing :
591             if (parse_toggle_state(cmd, arg) == 1)
592             {
593                config->feature_flags |= RUNTIME_FEATURE_CONNECTION_SHARING;
594             }
595             else
596             {
597                config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
598             }
599             break;
600 #endif
601
602 /* *************************************************************************
603  * debug n
604  * Specifies debug level, multiple values are ORed together.
605  * *************************************************************************/
606          case hash_debug :
607             config->debug |= atoi(arg);
608             break;
609
610 /* *************************************************************************
611  * default-server-timeout timeout
612  * *************************************************************************/
613 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
614          case hash_default_server_timeout :
615             if (*arg != '\0')
616             {
617                int timeout = atoi(arg);
618                if (0 < timeout)
619                {
620                   config->default_server_timeout = (unsigned int)timeout;
621                }
622                else
623                {
624                   log_error(LOG_LEVEL_FATAL,
625                      "Invalid default-server-timeout value: %s", arg);
626                }
627             }
628             break;
629 #endif
630
631 /* *************************************************************************
632  * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
633  * *************************************************************************/
634 #ifdef FEATURE_ACL
635          case hash_deny_access:
636             strlcpy(tmp, arg, sizeof(tmp));
637             vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
638
639             if ((vec_count != 1) && (vec_count != 2))
640             {
641                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
642                      "deny-access directive in configuration file.");
643                string_append(&config->proxy_args,
644                   "<br>\nWARNING: Wrong number of parameters for "
645                   "deny-access directive in configuration file.<br><br>\n");
646                break;
647             }
648
649             /* allocate a new node */
650             cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
651
652             if (cur_acl == NULL)
653             {
654                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
655                /* Never get here - LOG_LEVEL_FATAL causes program exit */
656                break;
657             }
658             cur_acl->action = ACL_DENY;
659
660             if (acl_addr(vec[0], cur_acl->src) < 0)
661             {
662                log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
663                   "for deny-access directive in configuration file: \"%s\"", vec[0]);
664                string_append(&config->proxy_args,
665                   "<br>\nWARNING: Invalid source address, port or netmask "
666                   "for deny-access directive in configuration file: \"");
667                string_append(&config->proxy_args,
668                   vec[0]);
669                string_append(&config->proxy_args,
670                   "\"<br><br>\n");
671                freez(cur_acl);
672                break;
673             }
674             if (vec_count == 2)
675             {
676                if (acl_addr(vec[1], cur_acl->dst) < 0)
677                {
678                   log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
679                      "for deny-access directive in configuration file: \"%s\"", vec[1]);
680                   string_append(&config->proxy_args,
681                      "<br>\nWARNING: Invalid destination address, port or netmask "
682                      "for deny-access directive in configuration file: \"");
683                   string_append(&config->proxy_args,
684                      vec[1]);
685                   string_append(&config->proxy_args,
686                      "\"<br><br>\n");
687                   freez(cur_acl);
688                   break;
689                }
690             }
691 #ifdef HAVE_RFC2553
692             else
693             {
694                cur_acl->wildcard_dst = 1;
695             }
696 #endif /* def HAVE_RFC2553 */
697
698             /*
699              * Add it to the list.  Note we reverse the list to get the
700              * behaviour the user expects.  With both the ACL and
701              * actions file, the last match wins.  However, the internal
702              * implementations are different:  The actions file is stored
703              * in the same order as the file, and scanned completely.
704              * With the ACL, we reverse the order as we load it, then
705              * when we scan it we stop as soon as we get a match.
706              */
707             cur_acl->next  = config->acl;
708             config->acl = cur_acl;
709
710             break;
711 #endif /* def FEATURE_ACL */
712
713 /* *************************************************************************
714  * enable-edit-actions 0|1
715  * *************************************************************************/
716 #ifdef FEATURE_CGI_EDIT_ACTIONS
717          case hash_enable_edit_actions:
718             if (parse_toggle_state(cmd, arg) == 1)
719             {
720                config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
721             }
722             else
723             {
724                config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
725             }
726             break;
727 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
728
729 /* *************************************************************************
730  * enable-compression 0|1
731  * *************************************************************************/
732 #ifdef FEATURE_COMPRESSION
733          case hash_enable_compression:
734             if (parse_toggle_state(cmd, arg) == 1)
735             {
736                config->feature_flags |= RUNTIME_FEATURE_COMPRESSION;
737             }
738             else
739             {
740                config->feature_flags &= ~RUNTIME_FEATURE_COMPRESSION;
741             }
742             break;
743 #endif /* def FEATURE_COMPRESSION */
744
745
746 /* *************************************************************************
747  * enable-remote-toggle 0|1
748  * *************************************************************************/
749 #ifdef FEATURE_TOGGLE
750          case hash_enable_remote_toggle:
751             if (parse_toggle_state(cmd, arg) == 1)
752             {
753                config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
754             }
755             else
756             {
757                config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
758             }
759             break;
760 #endif /* def FEATURE_TOGGLE */
761
762 /* *************************************************************************
763  * enable-remote-http-toggle 0|1
764  * *************************************************************************/
765          case hash_enable_remote_http_toggle:
766             if (parse_toggle_state(cmd, arg) == 1)
767             {
768                config->feature_flags |= RUNTIME_FEATURE_HTTP_TOGGLE;
769             }
770             else
771             {
772                config->feature_flags &= ~RUNTIME_FEATURE_HTTP_TOGGLE;
773             }
774             break;
775
776 /* *************************************************************************
777  * enforce-blocks 0|1
778  * *************************************************************************/
779          case hash_enforce_blocks:
780 #ifdef FEATURE_FORCE_LOAD
781             if (parse_toggle_state(cmd, arg) == 1)
782             {
783                config->feature_flags |= RUNTIME_FEATURE_ENFORCE_BLOCKS;
784             }
785             else
786             {
787                config->feature_flags &= ~RUNTIME_FEATURE_ENFORCE_BLOCKS;
788             }
789 #else
790             log_error(LOG_LEVEL_ERROR, "Ignoring directive 'enforce-blocks'. "
791                "FEATURE_FORCE_LOAD is disabled, blocks will always be enforced.");
792 #endif /* def FEATURE_FORCE_LOAD */
793             break;
794
795 /* *************************************************************************
796  * filterfile file-name
797  * In confdir by default.
798  * *************************************************************************/
799          case hash_filterfile :
800             i = 0;
801             while ((i < MAX_AF_FILES) && (NULL != config->re_filterfile[i]))
802             {
803                i++;
804             }
805
806             if (i >= MAX_AF_FILES)
807             {
808                log_error(LOG_LEVEL_FATAL, "Too many 'filterfile' directives in config file - limit is %d.\n"
809                   "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
810                   MAX_AF_FILES);
811             }
812             config->re_filterfile_short[i] = strdup(arg);
813             config->re_filterfile[i] = make_path(config->confdir, arg);
814
815             break;
816
817 /* *************************************************************************
818  * forward url-pattern (.|http-proxy-host[:port])
819  * *************************************************************************/
820          case hash_forward:
821             strlcpy(tmp, arg, sizeof(tmp));
822             vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
823
824             if (vec_count != 2)
825             {
826                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
827                      "directive in configuration file.");
828                string_append(&config->proxy_args,
829                   "<br>\nWARNING: Wrong number of parameters for "
830                   "forward directive in configuration file.");
831                break;
832             }
833
834             /* allocate a new node */
835             cur_fwd = zalloc(sizeof(*cur_fwd));
836             if (cur_fwd == NULL)
837             {
838                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
839                /* Never get here - LOG_LEVEL_FATAL causes program exit */
840                break;
841             }
842
843             cur_fwd->type = SOCKS_NONE;
844
845             /* Save the URL pattern */
846             if (create_url_spec(cur_fwd->url, vec[0]))
847             {
848                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
849                      "directive in configuration file.");
850                string_append(&config->proxy_args,
851                   "<br>\nWARNING: Bad URL specifier for "
852                   "forward directive in configuration file.");
853                break;
854             }
855
856             /* Parse the parent HTTP proxy host:port */
857             p = vec[1];
858
859             if (strcmp(p, ".") != 0)
860             {
861                cur_fwd->forward_port = 8000;
862                parse_forwarder_address(p, &cur_fwd->forward_host,
863                   &cur_fwd->forward_port);
864             }
865
866             /* Add to list. */
867             cur_fwd->next = config->forward;
868             config->forward = cur_fwd;
869
870             break;
871
872 /* *************************************************************************
873  * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
874  * *************************************************************************/
875          case hash_forward_socks4:
876             strlcpy(tmp, arg, sizeof(tmp));
877             vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
878
879             if (vec_count != 3)
880             {
881                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
882                      "forward-socks4 directive in configuration file.");
883                string_append(&config->proxy_args,
884                   "<br>\nWARNING: Wrong number of parameters for "
885                   "forward-socks4 directive in configuration file.");
886                break;
887             }
888
889             /* allocate a new node */
890             cur_fwd = zalloc(sizeof(*cur_fwd));
891             if (cur_fwd == NULL)
892             {
893                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
894                /* Never get here - LOG_LEVEL_FATAL causes program exit */
895                break;
896             }
897
898             cur_fwd->type = SOCKS_4;
899
900             /* Save the URL pattern */
901             if (create_url_spec(cur_fwd->url, vec[0]))
902             {
903                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
904                      "directive in configuration file.");
905                string_append(&config->proxy_args,
906                   "<br>\nWARNING: Bad URL specifier for "
907                   "forward-socks4 directive in configuration file.");
908                break;
909             }
910
911             /* Parse the SOCKS proxy host[:port] */
912             p = vec[1];
913
914             /* XXX: This check looks like a bug. */
915             if (strcmp(p, ".") != 0)
916             {
917                cur_fwd->gateway_port = 1080;
918                parse_forwarder_address(p, &cur_fwd->gateway_host,
919                   &cur_fwd->gateway_port);
920             }
921
922             /* Parse the parent HTTP proxy host[:port] */
923             p = vec[2];
924
925             if (strcmp(p, ".") != 0)
926             {
927                cur_fwd->forward_port = 8000;
928                parse_forwarder_address(p, &cur_fwd->forward_host,
929                   &cur_fwd->forward_port);
930             }
931
932             /* Add to list. */
933             cur_fwd->next = config->forward;
934             config->forward = cur_fwd;
935
936             break;
937
938 /* *************************************************************************
939  * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
940  * *************************************************************************/
941          case hash_forward_socks4a:
942          case hash_forward_socks5:
943             strlcpy(tmp, arg, sizeof(tmp));
944             vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
945
946             if (vec_count != 3)
947             {
948                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
949                      "forward-socks4a directive in configuration file.");
950                string_append(&config->proxy_args,
951                   "<br>\nWARNING: Wrong number of parameters for "
952                   "forward-socks4a directive in configuration file.");
953                break;
954             }
955
956             /* allocate a new node */
957             cur_fwd = zalloc(sizeof(*cur_fwd));
958             if (cur_fwd == NULL)
959             {
960                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
961                /* Never get here - LOG_LEVEL_FATAL causes program exit */
962                break;
963             }
964
965             if (directive_hash == hash_forward_socks4a)
966             {
967                cur_fwd->type = SOCKS_4A;
968             }
969             else
970             {
971                cur_fwd->type = SOCKS_5;
972             }
973
974             /* Save the URL pattern */
975             if (create_url_spec(cur_fwd->url, vec[0]))
976             {
977                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
978                      "directive in configuration file.");
979                string_append(&config->proxy_args,
980                   "<br>\nWARNING: Bad URL specifier for "
981                   "forward-socks4a directive in configuration file.");
982                break;
983             }
984
985             /* Parse the SOCKS proxy host[:port] */
986             p = vec[1];
987
988             cur_fwd->gateway_port = 1080;
989             parse_forwarder_address(p, &cur_fwd->gateway_host,
990                &cur_fwd->gateway_port);
991
992             /* Parse the parent HTTP proxy host[:port] */
993             p = vec[2];
994
995             if (strcmp(p, ".") != 0)
996             {
997                cur_fwd->forward_port = 8000;
998                parse_forwarder_address(p, &cur_fwd->forward_host,
999                   &cur_fwd->forward_port);
1000             }
1001
1002             /* Add to list. */
1003             cur_fwd->next = config->forward;
1004             config->forward = cur_fwd;
1005
1006             break;
1007
1008 /* *************************************************************************
1009  * forwarded-connect-retries n
1010  * *************************************************************************/
1011          case hash_forwarded_connect_retries :
1012             config->forwarded_connect_retries = atoi(arg);
1013             break;
1014
1015 /* *************************************************************************
1016  * handle-as-empty-doc-returns-ok 0|1
1017  *
1018  * Workaround for firefox hanging on blocked javascript pages.
1019  *   Block with the "+handle-as-empty-document" flag and set the
1020  *   "handle-as-empty-doc-returns-ok" run-time config flag so that
1021  *   Privoxy returns a 200/OK status instead of a 403/Forbidden status
1022  *   to the browser for blocked pages.
1023  ***************************************************************************/
1024          case hash_handle_as_empty_returns_ok:
1025             if (parse_toggle_state(cmd, arg) == 1)
1026             {
1027                config->feature_flags |= RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
1028             }
1029             else
1030             {
1031                config->feature_flags &= ~RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
1032             }
1033             break;
1034
1035 /* *************************************************************************
1036  * hostname hostname-to-show-on-cgi-pages
1037  * *************************************************************************/
1038          case hash_hostname :
1039             freez(config->hostname);
1040             config->hostname = strdup(arg);
1041             if (NULL == config->hostname)
1042             {
1043                log_error(LOG_LEVEL_FATAL, "Out of memory saving hostname.");
1044             }
1045             break;
1046
1047 /* *************************************************************************
1048  * keep-alive-timeout timeout
1049  * *************************************************************************/
1050 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1051          case hash_keep_alive_timeout :
1052             if (*arg != '\0')
1053             {
1054                int timeout = atoi(arg);
1055                if (0 < timeout)
1056                {
1057                   config->feature_flags |= RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
1058                   config->keep_alive_timeout = (unsigned int)timeout;
1059                }
1060                else
1061                {
1062                   config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
1063                }
1064             }
1065             break;
1066 #endif
1067
1068 /* *************************************************************************
1069  * listen-address [ip][:port]
1070  * *************************************************************************/
1071          case hash_listen_address :
1072             i = 0;
1073             while ((i < MAX_LISTENING_SOCKETS) && (NULL != config->haddr[i]))
1074             {
1075                i++;
1076             }
1077
1078             if (i >= MAX_LISTENING_SOCKETS)
1079             {
1080                log_error(LOG_LEVEL_FATAL, "Too many 'listen-address' directives in config file - limit is %d.\n"
1081                   "(You can increase this limit by changing MAX_LISTENING_SOCKETS in project.h and recompiling).",
1082                   MAX_LISTENING_SOCKETS);
1083             }
1084             config->haddr[i] = strdup(arg);
1085             if (NULL == config->haddr[i])
1086             {
1087                log_error(LOG_LEVEL_FATAL, "Out of memory while copying listening address");
1088             }
1089             break;
1090
1091 /* *************************************************************************
1092  * logdir directory-name
1093  * *************************************************************************/
1094          case hash_logdir :
1095             freez(config->logdir);
1096             config->logdir = make_path(NULL, arg);
1097             break;
1098
1099 /* *************************************************************************
1100  * logfile log-file-name
1101  * In logdir by default
1102  * *************************************************************************/
1103          case hash_logfile :
1104             if (daemon_mode)
1105             {
1106                logfile = make_path(config->logdir, arg);
1107                if (NULL == logfile)
1108                {
1109                   log_error(LOG_LEVEL_FATAL, "Out of memory while creating logfile path");
1110                }
1111             }
1112             break;
1113
1114 /* *************************************************************************
1115  * max-client-connections number
1116  * *************************************************************************/
1117          case hash_max_client_connections :
1118             if (*arg != '\0')
1119             {
1120                int max_client_connections = atoi(arg);
1121                if (0 <= max_client_connections)
1122                {
1123                   config->max_client_connections = max_client_connections;
1124                }
1125             }
1126             break;
1127
1128 /* *************************************************************************
1129  * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1130  * *************************************************************************/
1131 #ifdef FEATURE_ACL
1132          case hash_permit_access:
1133             strlcpy(tmp, arg, sizeof(tmp));
1134             vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
1135
1136             if ((vec_count != 1) && (vec_count != 2))
1137             {
1138                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1139                      "permit-access directive in configuration file.");
1140                string_append(&config->proxy_args,
1141                   "<br>\nWARNING: Wrong number of parameters for "
1142                   "permit-access directive in configuration file.<br><br>\n");
1143
1144                break;
1145             }
1146
1147             /* allocate a new node */
1148             cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1149
1150             if (cur_acl == NULL)
1151             {
1152                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1153                /* Never get here - LOG_LEVEL_FATAL causes program exit */
1154                break;
1155             }
1156             cur_acl->action = ACL_PERMIT;
1157
1158             if (acl_addr(vec[0], cur_acl->src) < 0)
1159             {
1160                log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
1161                   "for permit-access directive in configuration file: \"%s\"", vec[0]);
1162                string_append(&config->proxy_args,
1163                   "<br>\nWARNING: Invalid source address, port or netmask for "
1164                   "permit-access directive in configuration file: \"");
1165                string_append(&config->proxy_args,
1166                   vec[0]);
1167                string_append(&config->proxy_args,
1168                   "\"<br><br>\n");
1169                freez(cur_acl);
1170                break;
1171             }
1172             if (vec_count == 2)
1173             {
1174                if (acl_addr(vec[1], cur_acl->dst) < 0)
1175                {
1176                   log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
1177                      "for permit-access directive in configuration file: \"%s\"", vec[1]);
1178                   string_append(&config->proxy_args,
1179                      "<br>\nWARNING: Invalid destination address, port or netmask for "
1180                      "permit-access directive in configuration file: \"");
1181                   string_append(&config->proxy_args,
1182                      vec[1]);
1183                   string_append(&config->proxy_args,
1184                      "\"<br><br>\n");
1185                   freez(cur_acl);
1186                   break;
1187                }
1188             }
1189 #ifdef HAVE_RFC2553
1190             else
1191             {
1192                cur_acl->wildcard_dst = 1;
1193             }
1194 #endif /* def HAVE_RFC2553 */
1195
1196             /*
1197              * Add it to the list.  Note we reverse the list to get the
1198              * behaviour the user expects.  With both the ACL and
1199              * actions file, the last match wins.  However, the internal
1200              * implementations are different:  The actions file is stored
1201              * in the same order as the file, and scanned completely.
1202              * With the ACL, we reverse the order as we load it, then
1203              * when we scan it we stop as soon as we get a match.
1204              */
1205             cur_acl->next  = config->acl;
1206             config->acl = cur_acl;
1207
1208             break;
1209 #endif /* def FEATURE_ACL */
1210
1211 /* *************************************************************************
1212  * proxy-info-url url
1213  * *************************************************************************/
1214          case hash_proxy_info_url :
1215             freez(config->proxy_info_url);
1216             config->proxy_info_url = strdup(arg);
1217             break;
1218
1219 /* *************************************************************************
1220  * single-threaded
1221  * *************************************************************************/
1222          case hash_single_threaded :
1223             config->multi_threaded = 0;
1224             break;
1225
1226 /* *************************************************************************
1227  * socket-timeout numer_of_seconds
1228  * *************************************************************************/
1229          case hash_socket_timeout :
1230             if (*arg != '\0')
1231             {
1232                int socket_timeout = atoi(arg);
1233                if (0 < socket_timeout)
1234                {
1235                   config->socket_timeout = socket_timeout;
1236                }
1237                else
1238                {
1239                   log_error(LOG_LEVEL_FATAL,
1240                      "Invalid socket-timeout: '%s'", arg);
1241                }
1242             }
1243             break;
1244
1245 /* *************************************************************************
1246  * split-large-cgi-forms
1247  * *************************************************************************/
1248          case hash_split_large_cgi_forms :
1249             if (parse_toggle_state(cmd, arg) == 1)
1250             {
1251                config->feature_flags |= RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1252             }
1253             else
1254             {
1255                config->feature_flags &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1256             }
1257             break;
1258
1259 /* *************************************************************************
1260  * templdir directory-name
1261  * *************************************************************************/
1262          case hash_templdir :
1263             freez(config->templdir);
1264             config->templdir = make_path(NULL, arg);
1265             break;
1266
1267 /* *************************************************************************
1268  * toggle (0|1)
1269  * *************************************************************************/
1270 #ifdef FEATURE_TOGGLE
1271          case hash_toggle :
1272             global_toggle_state = atoi(arg);
1273             break;
1274 #endif /* def FEATURE_TOGGLE */
1275
1276 /* *************************************************************************
1277  * trust-info-url url
1278  * *************************************************************************/
1279 #ifdef FEATURE_TRUST
1280          case hash_trust_info_url :
1281             enlist(config->trust_info, arg);
1282             break;
1283 #endif /* def FEATURE_TRUST */
1284
1285 /* *************************************************************************
1286  * trustfile filename
1287  * (In confdir by default.)
1288  * *************************************************************************/
1289 #ifdef FEATURE_TRUST
1290          case hash_trustfile :
1291             freez(config->trustfile);
1292             config->trustfile = make_path(config->confdir, arg);
1293             break;
1294 #endif /* def FEATURE_TRUST */
1295
1296 /* *************************************************************************
1297  * usermanual url
1298  * *************************************************************************/
1299          case hash_usermanual :
1300             /*
1301              * XXX: If this isn't the first config directive, the
1302              * show-status page links to the website documentation
1303              * for the directives that were already parsed. Lame.
1304              */
1305             freez(config->usermanual);
1306             config->usermanual = strdup(arg);
1307             break;
1308
1309 /* *************************************************************************
1310  * Win32 Console options:
1311  * *************************************************************************/
1312
1313 /* *************************************************************************
1314  * hide-console
1315  * *************************************************************************/
1316 #ifdef _WIN_CONSOLE
1317          case hash_hide_console :
1318             hideConsole = 1;
1319             break;
1320 #endif /*def _WIN_CONSOLE*/
1321
1322
1323 /* *************************************************************************
1324  * Win32 GUI options:
1325  * *************************************************************************/
1326
1327 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1328 /* *************************************************************************
1329  * activity-animation (0|1)
1330  * *************************************************************************/
1331          case hash_activity_animation :
1332             g_bShowActivityAnimation = atoi(arg);
1333             break;
1334
1335 /* *************************************************************************
1336  *  close-button-minimizes (0|1)
1337  * *************************************************************************/
1338          case hash_close_button_minimizes :
1339             g_bCloseHidesWindow = atoi(arg);
1340             break;
1341
1342 /* *************************************************************************
1343  * log-buffer-size (0|1)
1344  * *************************************************************************/
1345          case hash_log_buffer_size :
1346             g_bLimitBufferSize = atoi(arg);
1347             break;
1348
1349 /* *************************************************************************
1350  * log-font-name fontname
1351  * *************************************************************************/
1352          case hash_log_font_name :
1353             if (strlcpy(g_szFontFaceName, arg,
1354                    sizeof(g_szFontFaceName)) >= sizeof(g_szFontFaceName))
1355             {
1356                log_error(LOG_LEVEL_FATAL,
1357                   "log-font-name argument '%s' is longer than %u characters.",
1358                   arg, sizeof(g_szFontFaceName)-1);
1359             }
1360             break;
1361
1362 /* *************************************************************************
1363  * log-font-size n
1364  * *************************************************************************/
1365          case hash_log_font_size :
1366             g_nFontSize = atoi(arg);
1367             break;
1368
1369 /* *************************************************************************
1370  * log-highlight-messages (0|1)
1371  * *************************************************************************/
1372          case hash_log_highlight_messages :
1373             g_bHighlightMessages = atoi(arg);
1374             break;
1375
1376 /* *************************************************************************
1377  * log-max-lines n
1378  * *************************************************************************/
1379          case hash_log_max_lines :
1380             g_nMaxBufferLines = atoi(arg);
1381             break;
1382
1383 /* *************************************************************************
1384  * log-messages (0|1)
1385  * *************************************************************************/
1386          case hash_log_messages :
1387             g_bLogMessages = atoi(arg);
1388             break;
1389
1390 /* *************************************************************************
1391  * show-on-task-bar (0|1)
1392  * *************************************************************************/
1393          case hash_show_on_task_bar :
1394             g_bShowOnTaskBar = atoi(arg);
1395             break;
1396
1397 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1398
1399
1400 /* *************************************************************************
1401  * Warnings about unsupported features
1402  * *************************************************************************/
1403 #ifndef FEATURE_ACL
1404          case hash_deny_access:
1405 #endif /* ndef FEATURE_ACL */
1406 #ifndef FEATURE_CGI_EDIT_ACTIONS
1407          case hash_enable_edit_actions:
1408 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1409 #ifndef FEATURE_TOGGLE
1410          case hash_enable_remote_toggle:
1411 #endif /* ndef FEATURE_TOGGLE */
1412 #ifndef FEATURE_ACL
1413          case hash_permit_access:
1414 #endif /* ndef FEATURE_ACL */
1415 #ifndef FEATURE_TOGGLE
1416          case hash_toggle :
1417 #endif /* ndef FEATURE_TOGGLE */
1418 #ifndef FEATURE_TRUST
1419          case hash_trustfile :
1420          case hash_trust_info_url :
1421 #endif /* ndef FEATURE_TRUST */
1422
1423 #ifndef _WIN_CONSOLE
1424          case hash_hide_console :
1425 #endif /* ndef _WIN_CONSOLE */
1426
1427 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1428          case hash_activity_animation :
1429          case hash_close_button_minimizes :
1430          case hash_log_buffer_size :
1431          case hash_log_font_name :
1432          case hash_log_font_size :
1433          case hash_log_highlight_messages :
1434          case hash_log_max_lines :
1435          case hash_log_messages :
1436          case hash_show_on_task_bar :
1437 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1438             /* These warnings are annoying - so hide them. -- Jon */
1439             /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1440             break;
1441
1442 /* *************************************************************************/
1443          default :
1444 /* *************************************************************************/
1445             /*
1446              * I decided that I liked this better as a warning than an
1447              * error.  To change back to an error, just change log level
1448              * to LOG_LEVEL_FATAL.
1449              */
1450             log_error(LOG_LEVEL_ERROR, "Ignoring unrecognized directive '%s' (%luul) in line %lu "
1451                   "in configuration file (%s).",  buf, directive_hash, linenum, configfile);
1452             string_append(&config->proxy_args,
1453                " <strong class='warning'>Warning: Ignoring unrecognized directive:</strong>");
1454             break;
1455
1456 /* *************************************************************************/
1457       } /* end switch( hash_string(cmd) ) */
1458
1459       /* Save the argument for the show-status page. */
1460       savearg(cmd, arg, config);
1461       freez(buf);
1462    } /* end while ( read_config_line(...) ) */
1463
1464    fclose(configfp);
1465
1466    set_debug_level(config->debug);
1467
1468    freez(config->logfile);
1469
1470    if (daemon_mode)
1471    {
1472       if (NULL != logfile)
1473       {
1474          config->logfile = logfile;
1475          init_error_log(Argv[0], config->logfile);
1476       }
1477       else
1478       {
1479          disable_logging();
1480       }
1481    }
1482
1483 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1484    if (config->default_server_timeout > config->keep_alive_timeout)
1485    {
1486       log_error(LOG_LEVEL_ERROR,
1487          "Reducing the default-server-timeout from %d to the keep-alive-timeout %d.",
1488          config->default_server_timeout, config->keep_alive_timeout);
1489       config->default_server_timeout = config->keep_alive_timeout;
1490    }
1491 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1492
1493 #ifdef FEATURE_CONNECTION_SHARING
1494    if (config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
1495    {
1496       if (config->multi_threaded)
1497       {
1498          set_keep_alive_timeout(config->keep_alive_timeout);
1499       }
1500       else
1501       {
1502          /*
1503           * While we could use keep-alive without multiple threads
1504           * if we didn't bother with enforcing the connection timeout,
1505           * that might make Tor users sad, even though they shouldn't
1506           * enable the single-threaded option anyway.
1507           *
1508           * XXX: We could still use Proxy-Connection: keep-alive.
1509           */
1510          config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
1511          log_error(LOG_LEVEL_ERROR,
1512             "Config option single-threaded disables connection keep-alive.");
1513       }
1514    }
1515    else if ((config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING))
1516    {
1517       log_error(LOG_LEVEL_ERROR, "Config option connection-sharing "
1518          "has no effect if keep-alive-timeout isn't set.");
1519       config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
1520    }
1521 #endif /* def FEATURE_CONNECTION_SHARING */
1522
1523    if (NULL == config->proxy_args)
1524    {
1525       log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1526    }
1527
1528    if (config->re_filterfile[0])
1529    {
1530       add_loader(load_re_filterfiles, config);
1531    }
1532
1533    if (config->actions_file[0])
1534    {
1535       add_loader(load_action_files, config);
1536    }
1537
1538 #ifdef FEATURE_TRUST
1539    if (config->trustfile)
1540    {
1541       add_loader(load_trustfile, config);
1542    }
1543 #endif /* def FEATURE_TRUST */
1544
1545    if ( NULL == config->haddr[0] )
1546    {
1547       config->haddr[0] = strdup( HADDR_DEFAULT );
1548       if (NULL == config->haddr[0])
1549       {
1550          log_error(LOG_LEVEL_FATAL, "Out of memory while copying default listening address");
1551       }
1552    }
1553
1554    for (i = 0; i < MAX_LISTENING_SOCKETS && NULL != config->haddr[i]; i++)
1555    {
1556       if ((*config->haddr[i] == '[')
1557          && (NULL != (p = strchr(config->haddr[i], ']')))
1558          && (p[1] == ':')
1559          && (0 < (config->hport[i] = atoi(p + 2))))
1560       {
1561          *p = '\0';
1562          memmove((void *)config->haddr[i], config->haddr[i] + 1,
1563             (size_t)(p - config->haddr[i]));
1564       }
1565       else if (NULL != (p = strchr(config->haddr[i], ':'))
1566          && (0 < (config->hport[i] = atoi(p + 1))))
1567       {
1568          *p = '\0';
1569       }
1570       else
1571       {
1572          log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr[i]);
1573          /* Never get here - LOG_LEVEL_FATAL causes program exit */
1574       }
1575       if (*config->haddr[i] == '\0')
1576       {
1577          /*
1578           * Only the port specified. We stored it in config->hport[i]
1579           * and don't need its text representation anymore.
1580           * Use config->hport[i] == 0 to iterate listening addresses since
1581           * now.
1582           */
1583          freez(config->haddr[i]);
1584       }
1585    }
1586
1587    /*
1588     * Want to run all the loaders once now.
1589     *
1590     * Need to set up a fake csp, so they can get to the config.
1591     */
1592    fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1593    fake_csp->config = config;
1594
1595    if (run_loader(fake_csp))
1596    {
1597       freez(fake_csp);
1598       log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1599       /* Never get here - LOG_LEVEL_FATAL causes program exit */
1600    }
1601    freez(fake_csp);
1602
1603 /* FIXME: this is a kludge for win32 */
1604 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1605
1606    g_default_actions_file = config->actions_file[1]; /* FIXME Hope this is default.action */
1607    g_user_actions_file  = config->actions_file[2];  /* FIXME Hope this is user.action */
1608    g_default_filterfile = config->re_filterfile[0]; /* FIXME Hope this is default.filter */
1609    g_user_filterfile    = config->re_filterfile[1]; /* FIXME Hope this is user.filter */
1610
1611 #ifdef FEATURE_TRUST
1612    g_trustfile        = config->trustfile;
1613 #endif /* def FEATURE_TRUST */
1614
1615
1616 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1617 /* FIXME: end kludge */
1618
1619
1620    config->need_bind = 1;
1621
1622    if (current_configfile)
1623    {
1624       struct configuration_spec * oldcfg = (struct configuration_spec *)
1625                                            current_configfile->f;
1626       /*
1627        * Check if config->haddr[i],hport[i] == oldcfg->haddr[i],hport[i]
1628        *
1629        * The following could be written more compactly as a single,
1630        * (unreadably long) if statement.
1631        */
1632       config->need_bind = 0;
1633
1634       for (i = 0; i < MAX_LISTENING_SOCKETS; i++)
1635       {
1636          if (config->hport[i] != oldcfg->hport[i])
1637          {
1638             config->need_bind = 1;
1639          }
1640          else if (config->haddr[i] == NULL)
1641          {
1642             if (oldcfg->haddr[i] != NULL)
1643             {
1644                config->need_bind = 1;
1645             }
1646          }
1647          else if (oldcfg->haddr[i] == NULL)
1648          {
1649             config->need_bind = 1;
1650          }
1651          else if (0 != strcmp(config->haddr[i], oldcfg->haddr[i]))
1652          {
1653             config->need_bind = 1;
1654          }
1655       }
1656
1657       current_configfile->unloader = unload_configfile;
1658    }
1659
1660    fs->next = files->next;
1661    files->next = fs;
1662
1663    current_configfile = fs;
1664
1665    return (config);
1666 }
1667
1668
1669 /*********************************************************************
1670  *
1671  * Function    :  savearg
1672  *
1673  * Description :  Called from `load_config'.  It saves each non-empty
1674  *                and non-comment line from config into
1675  *                config->proxy_args.  This is used to create the
1676  *                show-proxy-args page.  On error, frees
1677  *                config->proxy_args and sets it to NULL
1678  *
1679  * Parameters  :
1680  *          1  :  command = config setting that was found
1681  *          2  :  argument = the setting's argument (if any)
1682  *          3  :  config = Configuration to save into.
1683  *
1684  * Returns     :  N/A
1685  *
1686  *********************************************************************/
1687 static void savearg(char *command, char *argument, struct configuration_spec * config)
1688 {
1689    char * buf;
1690    char * s;
1691
1692    assert(command);
1693    assert(argument);
1694
1695    /*
1696     * Add config option name embedded in
1697     * link to its section in the user-manual
1698     */
1699    buf = strdup("\n<a href=\"");
1700    if (!strncmpic(config->usermanual, "file://", 7) ||
1701        !strncmpic(config->usermanual, "http", 4))
1702    {
1703       string_append(&buf, config->usermanual);
1704    }
1705    else
1706    {
1707       string_append(&buf, "http://" CGI_SITE_2_HOST "/user-manual/");
1708    }
1709    string_append(&buf, CONFIG_HELP_PREFIX);
1710    string_join  (&buf, string_toupper(command));
1711    string_append(&buf, "\">");
1712    string_append(&buf, command);
1713    string_append(&buf, "</a> ");
1714
1715    if (NULL == buf)
1716    {
1717       freez(config->proxy_args);
1718       return;
1719    }
1720
1721    if ( (NULL != argument) && ('\0' != *argument) )
1722    {
1723       s = html_encode(argument);
1724       if (NULL == s)
1725       {
1726          freez(buf);
1727          freez(config->proxy_args);
1728          return;
1729       }
1730
1731       if (strncmpic(argument, "http://", 7) == 0)
1732       {
1733          string_append(&buf, "<a href=\"");
1734          string_append(&buf, s);
1735          string_append(&buf, "\">");
1736          string_join  (&buf, s);
1737          string_append(&buf, "</a>");
1738       }
1739       else
1740       {
1741          string_join  (&buf, s);
1742       }
1743    }
1744
1745    string_append(&buf, "<br>");
1746    string_join(&config->proxy_args, buf);
1747 }
1748
1749
1750 /*
1751   Local Variables:
1752   tab-width: 3
1753   end:
1754 */