1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.112 2011/03/03 14:38:36 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/loadcfg.c,v $
6 * Purpose : Loads settings from the configuration file into
7 * global variables. This file contains both the
8 * routine to load the configuration and the global
9 * variables it writes to.
11 * Copyright : Written by and Copyright (C) 2001-2009 the
12 * Privoxy team. http://www.privoxy.org/
14 * Based on the Internet Junkbuster originally written
15 * by and Copyright (C) 1997 Anonymous Coders and
16 * Junkbusters Corporation. http://www.junkbusters.com
18 * This program is free software; you can redistribute it
19 * and/or modify it under the terms of the GNU General
20 * Public License as published by the Free Software
21 * Foundation; either version 2 of the License, or (at
22 * your option) any later version.
24 * This program is distributed in the hope that it will
25 * be useful, but WITHOUT ANY WARRANTY; without even the
26 * implied warranty of MERCHANTABILITY or FITNESS FOR A
27 * PARTICULAR PURPOSE. See the GNU General Public
28 * License for more details.
30 * The GNU General Public License should be included with
31 * this file. If not, you can view it at
32 * http://www.gnu.org/copyleft/gpl.html
33 * or write to the Free Software Foundation, Inc., 59
34 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 *********************************************************************/
42 #include <sys/types.h>
61 # endif /* ndef _WIN_CONSOLE */
63 #else /* ifndef _WIN32 */
67 # include <sys/wait.h>
69 # include <sys/time.h>
70 # include <sys/stat.h>
88 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
91 * Fix a problem with Solaris. There should be no effect on other
93 * Solaris's isspace() is a macro which uses it's argument directly
94 * as an array index. Therefore we need to make sure that high-bit
95 * characters generate +ve values, and ideally we also want to make
96 * the argument match the declared parameter type of "int".
98 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
99 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
101 #ifdef FEATURE_TOGGLE
102 /* Privoxy is enabled by default. */
103 int global_toggle_state = 1;
104 #endif /* def FEATURE_TOGGLE */
106 /* The filename of the configfile */
107 const char *configfile = NULL;
110 * CGI functions will later need access to the invocation args,
111 * so we will make argc and argv global.
114 char * const * Argv = NULL;
116 static struct file_list *current_configfile = NULL;
120 * This takes the "cryptic" hash of each keyword and aliases them to
121 * something a little more readable. This also makes changing the
122 * hash values easier if they should change or the hash algorthm changes.
123 * Use the included "hash" program to find out what the hash will be
124 * for any string supplied on the command line. (Or just put it in the
125 * config file and read the number from the error message in the log).
127 * Please keep this list sorted alphabetically (but with the Windows
128 * console and GUI specific options last).
131 #define hash_actions_file 1196306641ul /* "actionsfile" */
132 #define hash_accept_intercepted_requests 1513024973ul /* "accept-intercepted-requests" */
133 #define hash_admin_address 4112573064ul /* "admin-address" */
134 #define hash_allow_cgi_request_crunching 258915987ul /* "allow-cgi-request-crunching" */
135 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
136 #define hash_compression_level 2464423563ul /* "compression-level" */
137 #define hash_confdir 1978389ul /* "confdir" */
138 #define hash_connection_sharing 1348841265ul /* "connection-sharing" */
139 #define hash_debug 78263ul /* "debug" */
140 #define hash_default_server_timeout 2530089913ul /* "default-server-timeout" */
141 #define hash_deny_access 1227333715ul /* "deny-access" */
142 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
143 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
144 #define hash_enable_remote_http_toggle 110543988ul /* "enable-remote-http-toggle" */
145 #define hash_enforce_blocks 1862427469ul /* "enforce-blocks" */
146 #define hash_filterfile 250887266ul /* "filterfile" */
147 #define hash_forward 2029845ul /* "forward" */
148 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
149 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
150 #define hash_forward_socks5 3963965522ul /* "forward-socks5" */
151 #define hash_forwarded_connect_retries 101465292ul /* "forwarded-connect-retries" */
152 #define hash_handle_as_empty_returns_ok 1444873247ul /* "handle-as-empty-doc-returns-ok" */
153 #define hash_hostname 10308071ul /* "hostname" */
154 #define hash_keep_alive_timeout 3878599515ul /* "keep-alive-timeout" */
155 #define hash_listen_address 1255650842ul /* "listen-address" */
156 #define hash_logdir 422889ul /* "logdir" */
157 #define hash_logfile 2114766ul /* "logfile" */
158 #define hash_max_client_connections 3595884446ul /* "max-client-connections" */
159 #define hash_permit_access 3587953268ul /* "permit-access" */
160 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
161 #define hash_single_threaded 4250084780ul /* "single-threaded" */
162 #define hash_socket_timeout 1809001761ul /* "socket-timeout" */
163 #define hash_split_large_cgi_forms 671658948ul /* "split-large-cgi-forms" */
164 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
165 #define hash_templdir 11067889ul /* "templdir" */
166 #define hash_toggle 447966ul /* "toggle" */
167 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
168 #define hash_trustfile 56494766ul /* "trustfile" */
169 #define hash_usermanual 1416668518ul /* "user-manual" */
170 #define hash_activity_animation 1817904738ul /* "activity-animation" */
171 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
172 #define hash_hide_console 2048809870ul /* "hide-console" */
173 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
174 #define hash_log_font_name 2866730124ul /* "log-font-name" */
175 #define hash_log_font_size 2866731014ul /* "log-font-size" */
176 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
177 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
178 #define hash_log_messages 2291744899ul /* "log-messages" */
179 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
182 static void savearg(char *command, char *argument, struct configuration_spec * config);
184 /*********************************************************************
186 * Function : unload_configfile
188 * Description : Free the config structure and all components.
191 * 1 : data: struct configuration_spec to unload
195 *********************************************************************/
196 static void unload_configfile (void * data)
198 struct configuration_spec * config = (struct configuration_spec *)data;
199 struct forward_spec *cur_fwd = config->forward;
203 struct access_control_list *cur_acl = config->acl;
205 while (cur_acl != NULL)
207 struct access_control_list * next_acl = cur_acl->next;
212 #endif /* def FEATURE_ACL */
214 while (cur_fwd != NULL)
216 struct forward_spec * next_fwd = cur_fwd->next;
217 free_url_spec(cur_fwd->url);
219 freez(cur_fwd->gateway_host);
220 freez(cur_fwd->forward_host);
224 config->forward = NULL;
226 freez(config->confdir);
227 freez(config->logdir);
228 freez(config->templdir);
229 freez(config->hostname);
231 freez(config->haddr);
232 freez(config->logfile);
234 for (i = 0; i < MAX_AF_FILES; i++)
236 freez(config->actions_file_short[i]);
237 freez(config->actions_file[i]);
238 freez(config->re_filterfile_short[i]);
239 freez(config->re_filterfile[i]);
242 freez(config->admin_address);
243 freez(config->proxy_info_url);
244 freez(config->proxy_args);
245 freez(config->usermanual);
248 freez(config->trustfile);
249 list_remove_all(config->trust_info);
250 #endif /* def FEATURE_TRUST */
256 #ifdef FEATURE_GRACEFUL_TERMINATION
257 /*********************************************************************
259 * Function : unload_current_config_file
261 * Description : Unloads current config file - reset to state at
262 * beginning of program.
268 *********************************************************************/
269 void unload_current_config_file(void)
271 if (current_configfile)
273 current_configfile->unloader = unload_configfile;
274 current_configfile = NULL;
280 /*********************************************************************
282 * Function : load_config
284 * Description : Load the config file and all parameters.
286 * XXX: more than thousand lines long
287 * and thus in serious need of refactoring.
291 * Returns : The configuration_spec, or NULL on error.
293 *********************************************************************/
294 struct configuration_spec * load_config(void)
298 FILE *configfp = NULL;
299 struct configuration_spec * config = NULL;
300 struct client_state * fake_csp;
301 struct file_list *fs;
302 unsigned long linenum = 0;
304 char *logfile = NULL;
306 if (!check_file_changed(current_configfile, configfile, &fs))
308 /* No need to load */
309 return ((struct configuration_spec *)current_configfile->f);
313 log_error(LOG_LEVEL_FATAL,
314 "can't check configuration file '%s': %E", configfile);
318 if (NULL != current_configfile)
320 log_error(LOG_LEVEL_INFO, "Reloading configuration file '%s'", configfile);
323 #ifdef FEATURE_TOGGLE
324 global_toggle_state = 1;
325 #endif /* def FEATURE_TOGGLE */
327 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
333 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
338 * This is backwards from how it's usually done.
339 * Following the usual pattern, "fs" would be stored in a member
340 * variable in "csp", and then we'd access "config" from "fs->f",
341 * using a cast. However, "config" is used so often that a
342 * cast each time would be very ugly, and the extra indirection
343 * would waste CPU cycles. Therefore we store "config" in
344 * "csp->config", and "fs" in "csp->config->config_file_list".
346 config->config_file_list = fs;
351 config->multi_threaded = 1;
352 config->buffer_limit = 4096 * 1024;
353 config->usermanual = strdup(USER_MANUAL_URL);
354 config->proxy_args = strdup("");
355 config->forwarded_connect_retries = 0;
356 config->max_client_connections = 0;
357 config->socket_timeout = 300; /* XXX: Should be a macro. */
358 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
359 config->default_server_timeout = 0;
360 config->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
361 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
362 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
364 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
365 config->feature_flags &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
366 config->feature_flags &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
367 config->feature_flags &= ~RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
368 #ifdef FEATURE_COMPRESSION
370 * XXX: Run some benchmarks to see if there are better default values.
372 config->compression_level = 1;
375 configfp = fopen(configfile, "r");
376 if (NULL == configfp)
378 log_error(LOG_LEVEL_FATAL,
379 "can't open configuration file '%s': %E", configfile);
380 /* Never get here - LOG_LEVEL_FATAL causes program exit */
383 while (read_config_line(configfp, &linenum, &buf) != NULL)
385 char cmd[BUFFER_SIZE];
386 char arg[BUFFER_SIZE];
387 char tmp[BUFFER_SIZE];
389 struct access_control_list *cur_acl;
390 #endif /* def FEATURE_ACL */
391 struct forward_spec *cur_fwd;
394 unsigned long directive_hash;
396 strlcpy(tmp, buf, sizeof(tmp));
398 /* Copy command (i.e. up to space or tab) into cmd */
401 while (*p && (*p != ' ') && (*p != '\t'))
407 /* Skip over the whitespace in buf */
408 while (*p && ((*p == ' ') || (*p == '\t')))
413 /* Copy the argument into arg */
414 if (strlcpy(arg, p, sizeof(arg)) >= sizeof(arg))
416 log_error(LOG_LEVEL_FATAL, "Config line too long: %s", buf);
419 /* Should never happen, but check this anyway */
426 /* Make sure the command field is lower case */
427 for (p = cmd; *p; p++)
431 *p = (char)ijb_tolower(*p);
435 directive_hash = hash_string(cmd);
436 switch (directive_hash)
438 /* *************************************************************************
439 * actionsfile actions-file-name
440 * In confdir by default
441 * *************************************************************************/
442 case hash_actions_file :
444 while ((i < MAX_AF_FILES) && (NULL != config->actions_file[i]))
449 if (i >= MAX_AF_FILES)
451 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
452 "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
455 config->actions_file_short[i] = strdup(arg);
456 config->actions_file[i] = make_path(config->confdir, arg);
459 /* *************************************************************************
460 * accept-intercepted-requests
461 * *************************************************************************/
462 case hash_accept_intercepted_requests:
463 if ((*arg != '\0') && (0 != atoi(arg)))
465 config->feature_flags |= RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
469 config->feature_flags &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
473 /* *************************************************************************
474 * admin-address email-address
475 * *************************************************************************/
476 case hash_admin_address :
477 freez(config->admin_address);
478 config->admin_address = strdup(arg);
481 /* *************************************************************************
482 * allow-cgi-request-crunching
483 * *************************************************************************/
484 case hash_allow_cgi_request_crunching:
485 if ((*arg != '\0') && (0 != atoi(arg)))
487 config->feature_flags |= RUNTIME_FEATURE_CGI_CRUNCHING;
491 config->feature_flags &= ~RUNTIME_FEATURE_CGI_CRUNCHING;
495 /* *************************************************************************
497 * *************************************************************************/
498 case hash_buffer_limit :
499 config->buffer_limit = (size_t)(1024 * atoi(arg));
502 /* *************************************************************************
503 * confdir directory-name
504 * *************************************************************************/
506 freez(config->confdir);
507 config->confdir = make_path( NULL, arg);
510 /* *************************************************************************
511 * compression-level 0-9
512 * *************************************************************************/
513 #ifdef FEATURE_COMPRESSION
514 case hash_compression_level :
517 int compression_level = atoi(arg);
518 if (-1 <= compression_level && compression_level <= 9)
520 config->compression_level = compression_level;;
524 log_error(LOG_LEVEL_FATAL,
525 "Invalid compression-level value: %s", arg);
530 log_error(LOG_LEVEL_FATAL,
531 "Invalid compression-level directive. Compression value missing");
536 /* *************************************************************************
537 * connection-sharing (0|1)
538 * *************************************************************************/
539 #ifdef FEATURE_CONNECTION_SHARING
540 case hash_connection_sharing :
541 if ((*arg != '\0') && (0 != atoi(arg)))
543 config->feature_flags |= RUNTIME_FEATURE_CONNECTION_SHARING;
547 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
552 /* *************************************************************************
554 * Specifies debug level, multiple values are ORed together.
555 * *************************************************************************/
557 config->debug |= atoi(arg);
560 /* *************************************************************************
561 * default-server-timeout timeout
562 * *************************************************************************/
563 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
564 case hash_default_server_timeout :
567 int timeout = atoi(arg);
570 config->default_server_timeout = (unsigned int)timeout;
574 log_error(LOG_LEVEL_FATAL,
575 "Invalid default-server-timeout value: %s", arg);
581 /* *************************************************************************
582 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
583 * *************************************************************************/
585 case hash_deny_access:
586 strlcpy(tmp, arg, sizeof(tmp));
587 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
589 if ((vec_count != 1) && (vec_count != 2))
591 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
592 "deny-access directive in configuration file.");
593 string_append(&config->proxy_args,
594 "<br>\nWARNING: Wrong number of parameters for "
595 "deny-access directive in configuration file.<br><br>\n");
599 /* allocate a new node */
600 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
604 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
605 /* Never get here - LOG_LEVEL_FATAL causes program exit */
608 cur_acl->action = ACL_DENY;
610 if (acl_addr(vec[0], cur_acl->src) < 0)
612 log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
613 "for deny-access directive in configuration file: \"%s\"", vec[0]);
614 string_append(&config->proxy_args,
615 "<br>\nWARNING: Invalid source address, port or netmask "
616 "for deny-access directive in configuration file: \"");
617 string_append(&config->proxy_args,
619 string_append(&config->proxy_args,
626 if (acl_addr(vec[1], cur_acl->dst) < 0)
628 log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
629 "for deny-access directive in configuration file: \"%s\"", vec[1]);
630 string_append(&config->proxy_args,
631 "<br>\nWARNING: Invalid destination address, port or netmask "
632 "for deny-access directive in configuration file: \"");
633 string_append(&config->proxy_args,
635 string_append(&config->proxy_args,
644 cur_acl->wildcard_dst = 1;
646 #endif /* def HAVE_RFC2553 */
649 * Add it to the list. Note we reverse the list to get the
650 * behaviour the user expects. With both the ACL and
651 * actions file, the last match wins. However, the internal
652 * implementations are different: The actions file is stored
653 * in the same order as the file, and scanned completely.
654 * With the ACL, we reverse the order as we load it, then
655 * when we scan it we stop as soon as we get a match.
657 cur_acl->next = config->acl;
658 config->acl = cur_acl;
661 #endif /* def FEATURE_ACL */
663 /* *************************************************************************
664 * enable-edit-actions 0|1
665 * *************************************************************************/
666 #ifdef FEATURE_CGI_EDIT_ACTIONS
667 case hash_enable_edit_actions:
668 if ((*arg != '\0') && (0 != atoi(arg)))
670 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
674 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
677 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
679 /* *************************************************************************
680 * enable-remote-toggle 0|1
681 * *************************************************************************/
682 #ifdef FEATURE_TOGGLE
683 case hash_enable_remote_toggle:
684 if ((*arg != '\0') && (0 != atoi(arg)))
686 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
690 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
693 #endif /* def FEATURE_TOGGLE */
695 /* *************************************************************************
696 * enable-remote-http-toggle 0|1
697 * *************************************************************************/
698 case hash_enable_remote_http_toggle:
699 if ((*arg != '\0') && (0 != atoi(arg)))
701 config->feature_flags |= RUNTIME_FEATURE_HTTP_TOGGLE;
705 config->feature_flags &= ~RUNTIME_FEATURE_HTTP_TOGGLE;
709 /* *************************************************************************
711 * *************************************************************************/
712 case hash_enforce_blocks:
713 #ifdef FEATURE_FORCE_LOAD
714 if ((*arg != '\0') && (0 != atoi(arg)))
716 config->feature_flags |= RUNTIME_FEATURE_ENFORCE_BLOCKS;
720 config->feature_flags &= ~RUNTIME_FEATURE_ENFORCE_BLOCKS;
723 log_error(LOG_LEVEL_ERROR, "Ignoring directive 'enforce-blocks'. "
724 "FEATURE_FORCE_LOAD is disabled, blocks will always be enforced.");
725 #endif /* def FEATURE_FORCE_LOAD */
728 /* *************************************************************************
729 * filterfile file-name
730 * In confdir by default.
731 * *************************************************************************/
732 case hash_filterfile :
734 while ((i < MAX_AF_FILES) && (NULL != config->re_filterfile[i]))
739 if (i >= MAX_AF_FILES)
741 log_error(LOG_LEVEL_FATAL, "Too many 'filterfile' directives in config file - limit is %d.\n"
742 "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
745 config->re_filterfile_short[i] = strdup(arg);
746 config->re_filterfile[i] = make_path(config->confdir, arg);
750 /* *************************************************************************
751 * forward url-pattern (.|http-proxy-host[:port])
752 * *************************************************************************/
754 strlcpy(tmp, arg, sizeof(tmp));
755 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
759 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
760 "directive in configuration file.");
761 string_append(&config->proxy_args,
762 "<br>\nWARNING: Wrong number of parameters for "
763 "forward directive in configuration file.");
767 /* allocate a new node */
768 cur_fwd = zalloc(sizeof(*cur_fwd));
771 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
772 /* Never get here - LOG_LEVEL_FATAL causes program exit */
776 cur_fwd->type = SOCKS_NONE;
778 /* Save the URL pattern */
779 if (create_url_spec(cur_fwd->url, vec[0]))
781 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
782 "directive in configuration file.");
783 string_append(&config->proxy_args,
784 "<br>\nWARNING: Bad URL specifier for "
785 "forward directive in configuration file.");
789 /* Parse the parent HTTP proxy host:port */
792 if (strcmp(p, ".") != 0)
794 cur_fwd->forward_port = 8000;
795 parse_forwarder_address(p, &cur_fwd->forward_host,
796 &cur_fwd->forward_port);
800 cur_fwd->next = config->forward;
801 config->forward = cur_fwd;
805 /* *************************************************************************
806 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
807 * *************************************************************************/
808 case hash_forward_socks4:
809 strlcpy(tmp, arg, sizeof(tmp));
810 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
814 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
815 "forward-socks4 directive in configuration file.");
816 string_append(&config->proxy_args,
817 "<br>\nWARNING: Wrong number of parameters for "
818 "forward-socks4 directive in configuration file.");
822 /* allocate a new node */
823 cur_fwd = zalloc(sizeof(*cur_fwd));
826 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
827 /* Never get here - LOG_LEVEL_FATAL causes program exit */
831 cur_fwd->type = SOCKS_4;
833 /* Save the URL pattern */
834 if (create_url_spec(cur_fwd->url, vec[0]))
836 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
837 "directive in configuration file.");
838 string_append(&config->proxy_args,
839 "<br>\nWARNING: Bad URL specifier for "
840 "forward-socks4 directive in configuration file.");
844 /* Parse the SOCKS proxy host[:port] */
847 /* XXX: This check looks like a bug. */
848 if (strcmp(p, ".") != 0)
850 cur_fwd->gateway_port = 1080;
851 parse_forwarder_address(p, &cur_fwd->gateway_host,
852 &cur_fwd->gateway_port);
855 /* Parse the parent HTTP proxy host[:port] */
858 if (strcmp(p, ".") != 0)
860 cur_fwd->forward_port = 8000;
861 parse_forwarder_address(p, &cur_fwd->forward_host,
862 &cur_fwd->forward_port);
866 cur_fwd->next = config->forward;
867 config->forward = cur_fwd;
871 /* *************************************************************************
872 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
873 * *************************************************************************/
874 case hash_forward_socks4a:
875 case hash_forward_socks5:
876 strlcpy(tmp, arg, sizeof(tmp));
877 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
881 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
882 "forward-socks4a directive in configuration file.");
883 string_append(&config->proxy_args,
884 "<br>\nWARNING: Wrong number of parameters for "
885 "forward-socks4a directive in configuration file.");
889 /* allocate a new node */
890 cur_fwd = zalloc(sizeof(*cur_fwd));
893 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
894 /* Never get here - LOG_LEVEL_FATAL causes program exit */
898 if (directive_hash == hash_forward_socks4a)
900 cur_fwd->type = SOCKS_4A;
904 cur_fwd->type = SOCKS_5;
907 /* Save the URL pattern */
908 if (create_url_spec(cur_fwd->url, vec[0]))
910 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
911 "directive in configuration file.");
912 string_append(&config->proxy_args,
913 "<br>\nWARNING: Bad URL specifier for "
914 "forward-socks4a directive in configuration file.");
918 /* Parse the SOCKS proxy host[:port] */
921 cur_fwd->gateway_port = 1080;
922 parse_forwarder_address(p, &cur_fwd->gateway_host,
923 &cur_fwd->gateway_port);
925 /* Parse the parent HTTP proxy host[:port] */
928 if (strcmp(p, ".") != 0)
930 cur_fwd->forward_port = 8000;
931 parse_forwarder_address(p, &cur_fwd->forward_host,
932 &cur_fwd->forward_port);
936 cur_fwd->next = config->forward;
937 config->forward = cur_fwd;
941 /* *************************************************************************
942 * forwarded-connect-retries n
943 * *************************************************************************/
944 case hash_forwarded_connect_retries :
945 config->forwarded_connect_retries = atoi(arg);
948 /* *************************************************************************
949 * handle-as-empty-doc-returns-ok 0|1
951 * Workaround for firefox hanging on blocked javascript pages.
952 * Block with the "+handle-as-empty-document" flag and set the
953 * "handle-as-empty-doc-returns-ok" run-time config flag so that
954 * Privoxy returns a 200/OK status instead of a 403/Forbidden status
955 * to the browser for blocked pages.
956 ***************************************************************************/
957 case hash_handle_as_empty_returns_ok:
958 if ((*arg != '\0') && (0 != atoi(arg)))
960 config->feature_flags |= RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
964 config->feature_flags &= ~RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
968 /* *************************************************************************
969 * hostname hostname-to-show-on-cgi-pages
970 * *************************************************************************/
972 freez(config->hostname);
973 config->hostname = strdup(arg);
974 if (NULL == config->hostname)
976 log_error(LOG_LEVEL_FATAL, "Out of memory saving hostname.");
980 /* *************************************************************************
981 * keep-alive-timeout timeout
982 * *************************************************************************/
983 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
984 case hash_keep_alive_timeout :
987 int timeout = atoi(arg);
990 config->feature_flags |= RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
991 config->keep_alive_timeout = (unsigned int)timeout;
995 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
1001 /* *************************************************************************
1002 * listen-address [ip][:port]
1003 * *************************************************************************/
1004 case hash_listen_address :
1005 freez(config->haddr);
1006 config->haddr = strdup(arg);
1009 /* *************************************************************************
1010 * logdir directory-name
1011 * *************************************************************************/
1013 freez(config->logdir);
1014 config->logdir = make_path(NULL, arg);
1017 /* *************************************************************************
1018 * logfile log-file-name
1019 * In logdir by default
1020 * *************************************************************************/
1024 logfile = make_path(config->logdir, arg);
1025 if (NULL == logfile)
1027 log_error(LOG_LEVEL_FATAL, "Out of memory while creating logfile path");
1032 /* *************************************************************************
1033 * max-client-connections number
1034 * *************************************************************************/
1035 case hash_max_client_connections :
1038 int max_client_connections = atoi(arg);
1039 if (0 <= max_client_connections)
1041 config->max_client_connections = max_client_connections;
1046 /* *************************************************************************
1047 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1048 * *************************************************************************/
1050 case hash_permit_access:
1051 strlcpy(tmp, arg, sizeof(tmp));
1052 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
1054 if ((vec_count != 1) && (vec_count != 2))
1056 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1057 "permit-access directive in configuration file.");
1058 string_append(&config->proxy_args,
1059 "<br>\nWARNING: Wrong number of parameters for "
1060 "permit-access directive in configuration file.<br><br>\n");
1065 /* allocate a new node */
1066 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1068 if (cur_acl == NULL)
1070 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1071 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1074 cur_acl->action = ACL_PERMIT;
1076 if (acl_addr(vec[0], cur_acl->src) < 0)
1078 log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
1079 "for permit-access directive in configuration file: \"%s\"", vec[0]);
1080 string_append(&config->proxy_args,
1081 "<br>\nWARNING: Invalid source address, port or netmask for "
1082 "permit-access directive in configuration file: \"");
1083 string_append(&config->proxy_args,
1085 string_append(&config->proxy_args,
1092 if (acl_addr(vec[1], cur_acl->dst) < 0)
1094 log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
1095 "for permit-access directive in configuration file: \"%s\"", vec[1]);
1096 string_append(&config->proxy_args,
1097 "<br>\nWARNING: Invalid destination address, port or netmask for "
1098 "permit-access directive in configuration file: \"");
1099 string_append(&config->proxy_args,
1101 string_append(&config->proxy_args,
1110 cur_acl->wildcard_dst = 1;
1112 #endif /* def HAVE_RFC2553 */
1115 * Add it to the list. Note we reverse the list to get the
1116 * behaviour the user expects. With both the ACL and
1117 * actions file, the last match wins. However, the internal
1118 * implementations are different: The actions file is stored
1119 * in the same order as the file, and scanned completely.
1120 * With the ACL, we reverse the order as we load it, then
1121 * when we scan it we stop as soon as we get a match.
1123 cur_acl->next = config->acl;
1124 config->acl = cur_acl;
1127 #endif /* def FEATURE_ACL */
1129 /* *************************************************************************
1130 * proxy-info-url url
1131 * *************************************************************************/
1132 case hash_proxy_info_url :
1133 freez(config->proxy_info_url);
1134 config->proxy_info_url = strdup(arg);
1137 /* *************************************************************************
1139 * *************************************************************************/
1140 case hash_single_threaded :
1141 config->multi_threaded = 0;
1144 /* *************************************************************************
1145 * socket-timeout numer_of_seconds
1146 * *************************************************************************/
1147 case hash_socket_timeout :
1150 int socket_timeout = atoi(arg);
1151 if (0 < socket_timeout)
1153 config->socket_timeout = socket_timeout;
1157 log_error(LOG_LEVEL_FATAL,
1158 "Invalid socket-timeout: '%s'", arg);
1163 /* *************************************************************************
1164 * split-large-cgi-forms
1165 * *************************************************************************/
1166 case hash_split_large_cgi_forms :
1167 if ((*arg != '\0') && (0 != atoi(arg)))
1169 config->feature_flags |= RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1173 config->feature_flags &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1177 /* *************************************************************************
1178 * templdir directory-name
1179 * *************************************************************************/
1180 case hash_templdir :
1181 freez(config->templdir);
1182 config->templdir = make_path(NULL, arg);
1185 /* *************************************************************************
1187 * *************************************************************************/
1188 #ifdef FEATURE_TOGGLE
1190 global_toggle_state = atoi(arg);
1192 #endif /* def FEATURE_TOGGLE */
1194 /* *************************************************************************
1195 * trust-info-url url
1196 * *************************************************************************/
1197 #ifdef FEATURE_TRUST
1198 case hash_trust_info_url :
1199 enlist(config->trust_info, arg);
1201 #endif /* def FEATURE_TRUST */
1203 /* *************************************************************************
1204 * trustfile filename
1205 * (In confdir by default.)
1206 * *************************************************************************/
1207 #ifdef FEATURE_TRUST
1208 case hash_trustfile :
1209 freez(config->trustfile);
1210 config->trustfile = make_path(config->confdir, arg);
1212 #endif /* def FEATURE_TRUST */
1214 /* *************************************************************************
1216 * *************************************************************************/
1217 case hash_usermanual :
1219 * XXX: If this isn't the first config directive, the
1220 * show-status page links to the website documentation
1221 * for the directives that were already parsed. Lame.
1223 freez(config->usermanual);
1224 config->usermanual = strdup(arg);
1227 /* *************************************************************************
1228 * Win32 Console options:
1229 * *************************************************************************/
1231 /* *************************************************************************
1233 * *************************************************************************/
1235 case hash_hide_console :
1238 #endif /*def _WIN_CONSOLE*/
1241 /* *************************************************************************
1242 * Win32 GUI options:
1243 * *************************************************************************/
1245 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1246 /* *************************************************************************
1247 * activity-animation (0|1)
1248 * *************************************************************************/
1249 case hash_activity_animation :
1250 g_bShowActivityAnimation = atoi(arg);
1253 /* *************************************************************************
1254 * close-button-minimizes (0|1)
1255 * *************************************************************************/
1256 case hash_close_button_minimizes :
1257 g_bCloseHidesWindow = atoi(arg);
1260 /* *************************************************************************
1261 * log-buffer-size (0|1)
1262 * *************************************************************************/
1263 case hash_log_buffer_size :
1264 g_bLimitBufferSize = atoi(arg);
1267 /* *************************************************************************
1268 * log-font-name fontname
1269 * *************************************************************************/
1270 case hash_log_font_name :
1271 if (strlcpy(g_szFontFaceName, arg,
1272 sizeof(g_szFontFaceName)) >= sizeof(g_szFontFaceName))
1274 log_error(LOG_LEVEL_FATAL,
1275 "log-font-name argument '%s' is longer than %u characters.",
1276 arg, sizeof(g_szFontFaceName)-1);
1280 /* *************************************************************************
1282 * *************************************************************************/
1283 case hash_log_font_size :
1284 g_nFontSize = atoi(arg);
1287 /* *************************************************************************
1288 * log-highlight-messages (0|1)
1289 * *************************************************************************/
1290 case hash_log_highlight_messages :
1291 g_bHighlightMessages = atoi(arg);
1294 /* *************************************************************************
1296 * *************************************************************************/
1297 case hash_log_max_lines :
1298 g_nMaxBufferLines = atoi(arg);
1301 /* *************************************************************************
1302 * log-messages (0|1)
1303 * *************************************************************************/
1304 case hash_log_messages :
1305 g_bLogMessages = atoi(arg);
1308 /* *************************************************************************
1309 * show-on-task-bar (0|1)
1310 * *************************************************************************/
1311 case hash_show_on_task_bar :
1312 g_bShowOnTaskBar = atoi(arg);
1315 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1318 /* *************************************************************************
1319 * Warnings about unsupported features
1320 * *************************************************************************/
1322 case hash_deny_access:
1323 #endif /* ndef FEATURE_ACL */
1324 #ifndef FEATURE_CGI_EDIT_ACTIONS
1325 case hash_enable_edit_actions:
1326 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1327 #ifndef FEATURE_TOGGLE
1328 case hash_enable_remote_toggle:
1329 #endif /* ndef FEATURE_TOGGLE */
1331 case hash_permit_access:
1332 #endif /* ndef FEATURE_ACL */
1333 #ifndef FEATURE_TOGGLE
1335 #endif /* ndef FEATURE_TOGGLE */
1336 #ifndef FEATURE_TRUST
1337 case hash_trustfile :
1338 case hash_trust_info_url :
1339 #endif /* ndef FEATURE_TRUST */
1341 #ifndef _WIN_CONSOLE
1342 case hash_hide_console :
1343 #endif /* ndef _WIN_CONSOLE */
1345 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1346 case hash_activity_animation :
1347 case hash_close_button_minimizes :
1348 case hash_log_buffer_size :
1349 case hash_log_font_name :
1350 case hash_log_font_size :
1351 case hash_log_highlight_messages :
1352 case hash_log_max_lines :
1353 case hash_log_messages :
1354 case hash_show_on_task_bar :
1355 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1356 /* These warnings are annoying - so hide them. -- Jon */
1357 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1360 /* *************************************************************************/
1362 /* *************************************************************************/
1364 * I decided that I liked this better as a warning than an
1365 * error. To change back to an error, just change log level
1366 * to LOG_LEVEL_FATAL.
1368 log_error(LOG_LEVEL_ERROR, "Ignoring unrecognized directive '%s' (%luul) in line %lu "
1369 "in configuration file (%s).", buf, directive_hash, linenum, configfile);
1370 string_append(&config->proxy_args,
1371 " <strong class='warning'>Warning: Ignoring unrecognized directive:</strong>");
1374 /* *************************************************************************/
1375 } /* end switch( hash_string(cmd) ) */
1377 /* Save the argument for the show-status page. */
1378 savearg(cmd, arg, config);
1380 } /* end while ( read_config_line(...) ) */
1384 set_debug_level(config->debug);
1386 freez(config->logfile);
1390 if (NULL != logfile)
1392 config->logfile = logfile;
1393 init_error_log(Argv[0], config->logfile);
1401 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1402 if (config->default_server_timeout > config->keep_alive_timeout)
1404 log_error(LOG_LEVEL_ERROR,
1405 "Reducing the default-server-timeout from %d to the keep-alive-timeout %d.",
1406 config->default_server_timeout, config->keep_alive_timeout);
1407 config->default_server_timeout = config->keep_alive_timeout;
1409 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1411 #ifdef FEATURE_CONNECTION_SHARING
1412 if (config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
1414 if (config->multi_threaded)
1416 set_keep_alive_timeout(config->keep_alive_timeout);
1421 * While we could use keep-alive without multiple threads
1422 * if we didn't bother with enforcing the connection timeout,
1423 * that might make Tor users sad, even though they shouldn't
1424 * enable the single-threaded option anyway.
1426 * XXX: We could still use Proxy-Connection: keep-alive.
1428 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
1429 log_error(LOG_LEVEL_ERROR,
1430 "Config option single-threaded disables connection keep-alive.");
1433 else if ((config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING))
1435 log_error(LOG_LEVEL_ERROR, "Config option connection-sharing "
1436 "has no effect if keep-alive-timeout isn't set.");
1437 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
1439 #endif /* def FEATURE_CONNECTION_SHARING */
1441 if (NULL == config->proxy_args)
1443 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1446 if (config->actions_file[0])
1448 add_loader(load_action_files, config);
1451 if (config->re_filterfile[0])
1453 add_loader(load_re_filterfiles, config);
1456 #ifdef FEATURE_TRUST
1457 if (config->trustfile)
1459 add_loader(load_trustfile, config);
1461 #endif /* def FEATURE_TRUST */
1463 if ( NULL == config->haddr )
1465 config->haddr = strdup( HADDR_DEFAULT );
1468 if ( NULL != config->haddr )
1470 if ((*config->haddr == '[')
1471 && (NULL != (p = strchr(config->haddr, ']')))
1473 && (0 < (config->hport = atoi(p + 2))))
1476 memmove((void *)config->haddr, config->haddr + 1,
1477 (size_t)(p - config->haddr));
1479 else if (NULL != (p = strchr(config->haddr, ':'))
1480 && (0 < (config->hport = atoi(p + 1))))
1486 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1487 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1489 if (*config->haddr == '\0')
1492 * Only the port specified. We stored it in config->hport
1493 * and don't need its text representation anymore.
1495 freez(config->haddr);
1500 * Want to run all the loaders once now.
1502 * Need to set up a fake csp, so they can get to the config.
1504 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1505 fake_csp->config = config;
1507 if (run_loader(fake_csp))
1510 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1511 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1515 /* FIXME: this is a kludge for win32 */
1516 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1518 g_default_actions_file = config->actions_file[1]; /* FIXME Hope this is default.action */
1519 g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1520 g_default_filterfile = config->re_filterfile[0]; /* FIXME Hope this is default.filter */
1521 g_user_filterfile = config->re_filterfile[1]; /* FIXME Hope this is user.filter */
1523 #ifdef FEATURE_TRUST
1524 g_trustfile = config->trustfile;
1525 #endif /* def FEATURE_TRUST */
1528 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1529 /* FIXME: end kludge */
1532 config->need_bind = 1;
1534 if (current_configfile)
1536 struct configuration_spec * oldcfg = (struct configuration_spec *)
1537 current_configfile->f;
1539 * Check if config->haddr,hport == oldcfg->haddr,hport
1541 * The following could be written more compactly as a single,
1542 * (unreadably long) if statement.
1544 config->need_bind = 0;
1545 if (config->hport != oldcfg->hport)
1547 config->need_bind = 1;
1549 else if (config->haddr == NULL)
1551 if (oldcfg->haddr != NULL)
1553 config->need_bind = 1;
1556 else if (oldcfg->haddr == NULL)
1558 config->need_bind = 1;
1560 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1562 config->need_bind = 1;
1565 current_configfile->unloader = unload_configfile;
1568 fs->next = files->next;
1571 current_configfile = fs;
1577 /*********************************************************************
1579 * Function : savearg
1581 * Description : Called from `load_config'. It saves each non-empty
1582 * and non-comment line from config into
1583 * config->proxy_args. This is used to create the
1584 * show-proxy-args page. On error, frees
1585 * config->proxy_args and sets it to NULL
1588 * 1 : command = config setting that was found
1589 * 2 : argument = the setting's argument (if any)
1590 * 3 : config = Configuration to save into.
1594 *********************************************************************/
1595 static void savearg(char *command, char *argument, struct configuration_spec * config)
1604 * Add config option name embedded in
1605 * link to its section in the user-manual
1607 buf = strdup("\n<a href=\"");
1608 if (!strncmpic(config->usermanual, "file://", 7) ||
1609 !strncmpic(config->usermanual, "http", 4))
1611 string_append(&buf, config->usermanual);
1615 string_append(&buf, "http://" CGI_SITE_2_HOST "/user-manual/");
1617 string_append(&buf, CONFIG_HELP_PREFIX);
1618 string_join (&buf, string_toupper(command));
1619 string_append(&buf, "\">");
1620 string_append(&buf, command);
1621 string_append(&buf, "</a> ");
1625 freez(config->proxy_args);
1629 if ( (NULL != argument) && ('\0' != *argument) )
1631 s = html_encode(argument);
1635 freez(config->proxy_args);
1639 if (strncmpic(argument, "http://", 7) == 0)
1641 string_append(&buf, "<a href=\"");
1642 string_append(&buf, s);
1643 string_append(&buf, "\">");
1644 string_join (&buf, s);
1645 string_append(&buf, "</a>");
1649 string_join (&buf, s);
1653 string_append(&buf, "<br>");
1654 string_join(&config->proxy_args, buf);