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