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