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