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