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