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