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