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