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