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