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