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