1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.114 2011/07/08 13:29:06 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 : parse_toggle_value
284 * Description : Parse the value of a directive that can only be
285 * enabled or disabled. Terminates with a fatal error
286 * if the value is NULL or something other than 0 or 1.
289 * 1 : name: The name of the directive. Used for log messages.
290 * 2 : value: The value to parse
293 * Returns : The numerical toggle state
295 *********************************************************************/
296 static int parse_toggle_state(const char *name, const char *value)
299 assert(name != NULL);
300 assert(value != NULL);
302 if ((value == NULL) || (*value == '\0'))
304 log_error(LOG_LEVEL_FATAL, "Directive %s used without argument", name);
307 toggle_state = atoi(value);
310 * Also check the length as atoi() doesn't mind
311 * garbage after a valid integer, but we do.
313 if (((toggle_state != 0) && (toggle_state != 1)) || (strlen(value) != 1))
315 log_error(LOG_LEVEL_FATAL,
316 "Directive %s used with invalid argument '%s'. Use either '0' or '1'.",
325 /*********************************************************************
327 * Function : load_config
329 * Description : Load the config file and all parameters.
331 * XXX: more than thousand lines long
332 * and thus in serious need of refactoring.
336 * Returns : The configuration_spec, or NULL on error.
338 *********************************************************************/
339 struct configuration_spec * load_config(void)
343 FILE *configfp = NULL;
344 struct configuration_spec * config = NULL;
345 struct client_state * fake_csp;
346 struct file_list *fs;
347 unsigned long linenum = 0;
349 char *logfile = NULL;
351 if (!check_file_changed(current_configfile, configfile, &fs))
353 /* No need to load */
354 return ((struct configuration_spec *)current_configfile->f);
358 log_error(LOG_LEVEL_FATAL,
359 "can't check configuration file '%s': %E", configfile);
363 if (NULL != current_configfile)
365 log_error(LOG_LEVEL_INFO, "Reloading configuration file '%s'", configfile);
368 #ifdef FEATURE_TOGGLE
369 global_toggle_state = 1;
370 #endif /* def FEATURE_TOGGLE */
372 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
378 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
383 * This is backwards from how it's usually done.
384 * Following the usual pattern, "fs" would be stored in a member
385 * variable in "csp", and then we'd access "config" from "fs->f",
386 * using a cast. However, "config" is used so often that a
387 * cast each time would be very ugly, and the extra indirection
388 * would waste CPU cycles. Therefore we store "config" in
389 * "csp->config", and "fs" in "csp->config->config_file_list".
391 config->config_file_list = fs;
396 config->multi_threaded = 1;
397 config->buffer_limit = 4096 * 1024;
398 config->usermanual = strdup(USER_MANUAL_URL);
399 config->proxy_args = strdup("");
400 config->forwarded_connect_retries = 0;
401 config->max_client_connections = 0;
402 config->socket_timeout = 300; /* XXX: Should be a macro. */
403 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
404 config->default_server_timeout = 0;
405 config->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
406 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
407 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
409 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
410 config->feature_flags &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
411 config->feature_flags &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
412 config->feature_flags &= ~RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
413 #ifdef FEATURE_COMPRESSION
415 * XXX: Run some benchmarks to see if there are better default values.
417 config->compression_level = 1;
420 configfp = fopen(configfile, "r");
421 if (NULL == configfp)
423 log_error(LOG_LEVEL_FATAL,
424 "can't open configuration file '%s': %E", configfile);
425 /* Never get here - LOG_LEVEL_FATAL causes program exit */
428 while (read_config_line(configfp, &linenum, &buf) != NULL)
430 char cmd[BUFFER_SIZE];
431 char arg[BUFFER_SIZE];
432 char tmp[BUFFER_SIZE];
434 struct access_control_list *cur_acl;
435 #endif /* def FEATURE_ACL */
436 struct forward_spec *cur_fwd;
439 unsigned long directive_hash;
441 strlcpy(tmp, buf, sizeof(tmp));
443 /* Copy command (i.e. up to space or tab) into cmd */
446 while (*p && (*p != ' ') && (*p != '\t'))
452 /* Skip over the whitespace in buf */
453 while (*p && ((*p == ' ') || (*p == '\t')))
458 /* Copy the argument into arg */
459 if (strlcpy(arg, p, sizeof(arg)) >= sizeof(arg))
461 log_error(LOG_LEVEL_FATAL, "Config line too long: %s", buf);
464 /* Should never happen, but check this anyway */
471 /* Make sure the command field is lower case */
472 for (p = cmd; *p; p++)
476 *p = (char)ijb_tolower(*p);
480 directive_hash = hash_string(cmd);
481 switch (directive_hash)
483 /* *************************************************************************
484 * actionsfile actions-file-name
485 * In confdir by default
486 * *************************************************************************/
487 case hash_actions_file :
489 while ((i < MAX_AF_FILES) && (NULL != config->actions_file[i]))
494 if (i >= MAX_AF_FILES)
496 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
497 "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
500 config->actions_file_short[i] = strdup(arg);
501 config->actions_file[i] = make_path(config->confdir, arg);
504 /* *************************************************************************
505 * accept-intercepted-requests
506 * *************************************************************************/
507 case hash_accept_intercepted_requests:
508 if (0 != parse_toggle_state(cmd, arg))
510 config->feature_flags |= RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
514 config->feature_flags &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
518 /* *************************************************************************
519 * admin-address email-address
520 * *************************************************************************/
521 case hash_admin_address :
522 freez(config->admin_address);
523 config->admin_address = strdup(arg);
526 /* *************************************************************************
527 * allow-cgi-request-crunching
528 * *************************************************************************/
529 case hash_allow_cgi_request_crunching:
530 if (0 != parse_toggle_state(cmd, arg))
532 config->feature_flags |= RUNTIME_FEATURE_CGI_CRUNCHING;
536 config->feature_flags &= ~RUNTIME_FEATURE_CGI_CRUNCHING;
540 /* *************************************************************************
542 * *************************************************************************/
543 case hash_buffer_limit :
544 config->buffer_limit = (size_t)(1024 * atoi(arg));
547 /* *************************************************************************
548 * confdir directory-name
549 * *************************************************************************/
551 freez(config->confdir);
552 config->confdir = make_path( NULL, arg);
555 /* *************************************************************************
556 * compression-level 0-9
557 * *************************************************************************/
558 #ifdef FEATURE_COMPRESSION
559 case hash_compression_level :
562 int compression_level = atoi(arg);
563 if (-1 <= compression_level && compression_level <= 9)
565 config->compression_level = compression_level;;
569 log_error(LOG_LEVEL_FATAL,
570 "Invalid compression-level value: %s", arg);
575 log_error(LOG_LEVEL_FATAL,
576 "Invalid compression-level directive. Compression value missing");
581 /* *************************************************************************
582 * connection-sharing (0|1)
583 * *************************************************************************/
584 #ifdef FEATURE_CONNECTION_SHARING
585 case hash_connection_sharing :
586 if (0 != parse_toggle_state(cmd, arg))
588 config->feature_flags |= RUNTIME_FEATURE_CONNECTION_SHARING;
592 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
597 /* *************************************************************************
599 * Specifies debug level, multiple values are ORed together.
600 * *************************************************************************/
602 config->debug |= atoi(arg);
605 /* *************************************************************************
606 * default-server-timeout timeout
607 * *************************************************************************/
608 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
609 case hash_default_server_timeout :
612 int timeout = atoi(arg);
615 config->default_server_timeout = (unsigned int)timeout;
619 log_error(LOG_LEVEL_FATAL,
620 "Invalid default-server-timeout value: %s", arg);
626 /* *************************************************************************
627 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
628 * *************************************************************************/
630 case hash_deny_access:
631 strlcpy(tmp, arg, sizeof(tmp));
632 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
634 if ((vec_count != 1) && (vec_count != 2))
636 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
637 "deny-access directive in configuration file.");
638 string_append(&config->proxy_args,
639 "<br>\nWARNING: Wrong number of parameters for "
640 "deny-access directive in configuration file.<br><br>\n");
644 /* allocate a new node */
645 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
649 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
650 /* Never get here - LOG_LEVEL_FATAL causes program exit */
653 cur_acl->action = ACL_DENY;
655 if (acl_addr(vec[0], cur_acl->src) < 0)
657 log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
658 "for deny-access directive in configuration file: \"%s\"", vec[0]);
659 string_append(&config->proxy_args,
660 "<br>\nWARNING: Invalid source address, port or netmask "
661 "for deny-access directive in configuration file: \"");
662 string_append(&config->proxy_args,
664 string_append(&config->proxy_args,
671 if (acl_addr(vec[1], cur_acl->dst) < 0)
673 log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
674 "for deny-access directive in configuration file: \"%s\"", vec[1]);
675 string_append(&config->proxy_args,
676 "<br>\nWARNING: Invalid destination address, port or netmask "
677 "for deny-access directive in configuration file: \"");
678 string_append(&config->proxy_args,
680 string_append(&config->proxy_args,
689 cur_acl->wildcard_dst = 1;
691 #endif /* def HAVE_RFC2553 */
694 * Add it to the list. Note we reverse the list to get the
695 * behaviour the user expects. With both the ACL and
696 * actions file, the last match wins. However, the internal
697 * implementations are different: The actions file is stored
698 * in the same order as the file, and scanned completely.
699 * With the ACL, we reverse the order as we load it, then
700 * when we scan it we stop as soon as we get a match.
702 cur_acl->next = config->acl;
703 config->acl = cur_acl;
706 #endif /* def FEATURE_ACL */
708 /* *************************************************************************
709 * enable-edit-actions 0|1
710 * *************************************************************************/
711 #ifdef FEATURE_CGI_EDIT_ACTIONS
712 case hash_enable_edit_actions:
713 if (0 != parse_toggle_state(cmd, arg))
715 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
719 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
722 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
724 /* *************************************************************************
725 * enable-remote-toggle 0|1
726 * *************************************************************************/
727 #ifdef FEATURE_TOGGLE
728 case hash_enable_remote_toggle:
729 if (0 != parse_toggle_state(cmd, arg))
731 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
735 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
738 #endif /* def FEATURE_TOGGLE */
740 /* *************************************************************************
741 * enable-remote-http-toggle 0|1
742 * *************************************************************************/
743 case hash_enable_remote_http_toggle:
744 if (0 != parse_toggle_state(cmd, arg))
746 config->feature_flags |= RUNTIME_FEATURE_HTTP_TOGGLE;
750 config->feature_flags &= ~RUNTIME_FEATURE_HTTP_TOGGLE;
754 /* *************************************************************************
756 * *************************************************************************/
757 case hash_enforce_blocks:
758 #ifdef FEATURE_FORCE_LOAD
759 if (0 != parse_toggle_state(cmd, arg))
761 config->feature_flags |= RUNTIME_FEATURE_ENFORCE_BLOCKS;
765 config->feature_flags &= ~RUNTIME_FEATURE_ENFORCE_BLOCKS;
768 log_error(LOG_LEVEL_ERROR, "Ignoring directive 'enforce-blocks'. "
769 "FEATURE_FORCE_LOAD is disabled, blocks will always be enforced.");
770 #endif /* def FEATURE_FORCE_LOAD */
773 /* *************************************************************************
774 * filterfile file-name
775 * In confdir by default.
776 * *************************************************************************/
777 case hash_filterfile :
779 while ((i < MAX_AF_FILES) && (NULL != config->re_filterfile[i]))
784 if (i >= MAX_AF_FILES)
786 log_error(LOG_LEVEL_FATAL, "Too many 'filterfile' directives in config file - limit is %d.\n"
787 "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
790 config->re_filterfile_short[i] = strdup(arg);
791 config->re_filterfile[i] = make_path(config->confdir, arg);
795 /* *************************************************************************
796 * forward url-pattern (.|http-proxy-host[:port])
797 * *************************************************************************/
799 strlcpy(tmp, arg, sizeof(tmp));
800 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
804 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
805 "directive in configuration file.");
806 string_append(&config->proxy_args,
807 "<br>\nWARNING: Wrong number of parameters for "
808 "forward directive in configuration file.");
812 /* allocate a new node */
813 cur_fwd = zalloc(sizeof(*cur_fwd));
816 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
817 /* Never get here - LOG_LEVEL_FATAL causes program exit */
821 cur_fwd->type = SOCKS_NONE;
823 /* Save the URL pattern */
824 if (create_url_spec(cur_fwd->url, vec[0]))
826 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
827 "directive in configuration file.");
828 string_append(&config->proxy_args,
829 "<br>\nWARNING: Bad URL specifier for "
830 "forward directive in configuration file.");
834 /* Parse the parent HTTP proxy host:port */
837 if (strcmp(p, ".") != 0)
839 cur_fwd->forward_port = 8000;
840 parse_forwarder_address(p, &cur_fwd->forward_host,
841 &cur_fwd->forward_port);
845 cur_fwd->next = config->forward;
846 config->forward = cur_fwd;
850 /* *************************************************************************
851 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
852 * *************************************************************************/
853 case hash_forward_socks4:
854 strlcpy(tmp, arg, sizeof(tmp));
855 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
859 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
860 "forward-socks4 directive in configuration file.");
861 string_append(&config->proxy_args,
862 "<br>\nWARNING: Wrong number of parameters for "
863 "forward-socks4 directive in configuration file.");
867 /* allocate a new node */
868 cur_fwd = zalloc(sizeof(*cur_fwd));
871 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
872 /* Never get here - LOG_LEVEL_FATAL causes program exit */
876 cur_fwd->type = SOCKS_4;
878 /* Save the URL pattern */
879 if (create_url_spec(cur_fwd->url, vec[0]))
881 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
882 "directive in configuration file.");
883 string_append(&config->proxy_args,
884 "<br>\nWARNING: Bad URL specifier for "
885 "forward-socks4 directive in configuration file.");
889 /* Parse the SOCKS proxy host[:port] */
892 /* XXX: This check looks like a bug. */
893 if (strcmp(p, ".") != 0)
895 cur_fwd->gateway_port = 1080;
896 parse_forwarder_address(p, &cur_fwd->gateway_host,
897 &cur_fwd->gateway_port);
900 /* Parse the parent HTTP proxy host[:port] */
903 if (strcmp(p, ".") != 0)
905 cur_fwd->forward_port = 8000;
906 parse_forwarder_address(p, &cur_fwd->forward_host,
907 &cur_fwd->forward_port);
911 cur_fwd->next = config->forward;
912 config->forward = cur_fwd;
916 /* *************************************************************************
917 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
918 * *************************************************************************/
919 case hash_forward_socks4a:
920 case hash_forward_socks5:
921 strlcpy(tmp, arg, sizeof(tmp));
922 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
926 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
927 "forward-socks4a directive in configuration file.");
928 string_append(&config->proxy_args,
929 "<br>\nWARNING: Wrong number of parameters for "
930 "forward-socks4a directive in configuration file.");
934 /* allocate a new node */
935 cur_fwd = zalloc(sizeof(*cur_fwd));
938 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
939 /* Never get here - LOG_LEVEL_FATAL causes program exit */
943 if (directive_hash == hash_forward_socks4a)
945 cur_fwd->type = SOCKS_4A;
949 cur_fwd->type = SOCKS_5;
952 /* Save the URL pattern */
953 if (create_url_spec(cur_fwd->url, vec[0]))
955 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
956 "directive in configuration file.");
957 string_append(&config->proxy_args,
958 "<br>\nWARNING: Bad URL specifier for "
959 "forward-socks4a directive in configuration file.");
963 /* Parse the SOCKS proxy host[:port] */
966 cur_fwd->gateway_port = 1080;
967 parse_forwarder_address(p, &cur_fwd->gateway_host,
968 &cur_fwd->gateway_port);
970 /* Parse the parent HTTP proxy host[:port] */
973 if (strcmp(p, ".") != 0)
975 cur_fwd->forward_port = 8000;
976 parse_forwarder_address(p, &cur_fwd->forward_host,
977 &cur_fwd->forward_port);
981 cur_fwd->next = config->forward;
982 config->forward = cur_fwd;
986 /* *************************************************************************
987 * forwarded-connect-retries n
988 * *************************************************************************/
989 case hash_forwarded_connect_retries :
990 config->forwarded_connect_retries = atoi(arg);
993 /* *************************************************************************
994 * handle-as-empty-doc-returns-ok 0|1
996 * Workaround for firefox hanging on blocked javascript pages.
997 * Block with the "+handle-as-empty-document" flag and set the
998 * "handle-as-empty-doc-returns-ok" run-time config flag so that
999 * Privoxy returns a 200/OK status instead of a 403/Forbidden status
1000 * to the browser for blocked pages.
1001 ***************************************************************************/
1002 case hash_handle_as_empty_returns_ok:
1003 if (0 != parse_toggle_state(cmd, arg))
1005 config->feature_flags |= RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
1009 config->feature_flags &= ~RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
1013 /* *************************************************************************
1014 * hostname hostname-to-show-on-cgi-pages
1015 * *************************************************************************/
1016 case hash_hostname :
1017 freez(config->hostname);
1018 config->hostname = strdup(arg);
1019 if (NULL == config->hostname)
1021 log_error(LOG_LEVEL_FATAL, "Out of memory saving hostname.");
1025 /* *************************************************************************
1026 * keep-alive-timeout timeout
1027 * *************************************************************************/
1028 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1029 case hash_keep_alive_timeout :
1032 int timeout = atoi(arg);
1035 config->feature_flags |= RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
1036 config->keep_alive_timeout = (unsigned int)timeout;
1040 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
1046 /* *************************************************************************
1047 * listen-address [ip][:port]
1048 * *************************************************************************/
1049 case hash_listen_address :
1050 freez(config->haddr);
1051 config->haddr = strdup(arg);
1054 /* *************************************************************************
1055 * logdir directory-name
1056 * *************************************************************************/
1058 freez(config->logdir);
1059 config->logdir = make_path(NULL, arg);
1062 /* *************************************************************************
1063 * logfile log-file-name
1064 * In logdir by default
1065 * *************************************************************************/
1069 logfile = make_path(config->logdir, arg);
1070 if (NULL == logfile)
1072 log_error(LOG_LEVEL_FATAL, "Out of memory while creating logfile path");
1077 /* *************************************************************************
1078 * max-client-connections number
1079 * *************************************************************************/
1080 case hash_max_client_connections :
1083 int max_client_connections = atoi(arg);
1084 if (0 <= max_client_connections)
1086 config->max_client_connections = max_client_connections;
1091 /* *************************************************************************
1092 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1093 * *************************************************************************/
1095 case hash_permit_access:
1096 strlcpy(tmp, arg, sizeof(tmp));
1097 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
1099 if ((vec_count != 1) && (vec_count != 2))
1101 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1102 "permit-access directive in configuration file.");
1103 string_append(&config->proxy_args,
1104 "<br>\nWARNING: Wrong number of parameters for "
1105 "permit-access directive in configuration file.<br><br>\n");
1110 /* allocate a new node */
1111 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1113 if (cur_acl == NULL)
1115 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1116 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1119 cur_acl->action = ACL_PERMIT;
1121 if (acl_addr(vec[0], cur_acl->src) < 0)
1123 log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
1124 "for permit-access directive in configuration file: \"%s\"", vec[0]);
1125 string_append(&config->proxy_args,
1126 "<br>\nWARNING: Invalid source address, port or netmask for "
1127 "permit-access directive in configuration file: \"");
1128 string_append(&config->proxy_args,
1130 string_append(&config->proxy_args,
1137 if (acl_addr(vec[1], cur_acl->dst) < 0)
1139 log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
1140 "for permit-access directive in configuration file: \"%s\"", vec[1]);
1141 string_append(&config->proxy_args,
1142 "<br>\nWARNING: Invalid destination address, port or netmask for "
1143 "permit-access directive in configuration file: \"");
1144 string_append(&config->proxy_args,
1146 string_append(&config->proxy_args,
1155 cur_acl->wildcard_dst = 1;
1157 #endif /* def HAVE_RFC2553 */
1160 * Add it to the list. Note we reverse the list to get the
1161 * behaviour the user expects. With both the ACL and
1162 * actions file, the last match wins. However, the internal
1163 * implementations are different: The actions file is stored
1164 * in the same order as the file, and scanned completely.
1165 * With the ACL, we reverse the order as we load it, then
1166 * when we scan it we stop as soon as we get a match.
1168 cur_acl->next = config->acl;
1169 config->acl = cur_acl;
1172 #endif /* def FEATURE_ACL */
1174 /* *************************************************************************
1175 * proxy-info-url url
1176 * *************************************************************************/
1177 case hash_proxy_info_url :
1178 freez(config->proxy_info_url);
1179 config->proxy_info_url = strdup(arg);
1182 /* *************************************************************************
1184 * *************************************************************************/
1185 case hash_single_threaded :
1186 config->multi_threaded = 0;
1189 /* *************************************************************************
1190 * socket-timeout numer_of_seconds
1191 * *************************************************************************/
1192 case hash_socket_timeout :
1195 int socket_timeout = atoi(arg);
1196 if (0 < socket_timeout)
1198 config->socket_timeout = socket_timeout;
1202 log_error(LOG_LEVEL_FATAL,
1203 "Invalid socket-timeout: '%s'", arg);
1208 /* *************************************************************************
1209 * split-large-cgi-forms
1210 * *************************************************************************/
1211 case hash_split_large_cgi_forms :
1212 if (0 != parse_toggle_state(cmd, arg))
1214 config->feature_flags |= RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1218 config->feature_flags &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1222 /* *************************************************************************
1223 * templdir directory-name
1224 * *************************************************************************/
1225 case hash_templdir :
1226 freez(config->templdir);
1227 config->templdir = make_path(NULL, arg);
1230 /* *************************************************************************
1232 * *************************************************************************/
1233 #ifdef FEATURE_TOGGLE
1235 global_toggle_state = atoi(arg);
1237 #endif /* def FEATURE_TOGGLE */
1239 /* *************************************************************************
1240 * trust-info-url url
1241 * *************************************************************************/
1242 #ifdef FEATURE_TRUST
1243 case hash_trust_info_url :
1244 enlist(config->trust_info, arg);
1246 #endif /* def FEATURE_TRUST */
1248 /* *************************************************************************
1249 * trustfile filename
1250 * (In confdir by default.)
1251 * *************************************************************************/
1252 #ifdef FEATURE_TRUST
1253 case hash_trustfile :
1254 freez(config->trustfile);
1255 config->trustfile = make_path(config->confdir, arg);
1257 #endif /* def FEATURE_TRUST */
1259 /* *************************************************************************
1261 * *************************************************************************/
1262 case hash_usermanual :
1264 * XXX: If this isn't the first config directive, the
1265 * show-status page links to the website documentation
1266 * for the directives that were already parsed. Lame.
1268 freez(config->usermanual);
1269 config->usermanual = strdup(arg);
1272 /* *************************************************************************
1273 * Win32 Console options:
1274 * *************************************************************************/
1276 /* *************************************************************************
1278 * *************************************************************************/
1280 case hash_hide_console :
1283 #endif /*def _WIN_CONSOLE*/
1286 /* *************************************************************************
1287 * Win32 GUI options:
1288 * *************************************************************************/
1290 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1291 /* *************************************************************************
1292 * activity-animation (0|1)
1293 * *************************************************************************/
1294 case hash_activity_animation :
1295 g_bShowActivityAnimation = atoi(arg);
1298 /* *************************************************************************
1299 * close-button-minimizes (0|1)
1300 * *************************************************************************/
1301 case hash_close_button_minimizes :
1302 g_bCloseHidesWindow = atoi(arg);
1305 /* *************************************************************************
1306 * log-buffer-size (0|1)
1307 * *************************************************************************/
1308 case hash_log_buffer_size :
1309 g_bLimitBufferSize = atoi(arg);
1312 /* *************************************************************************
1313 * log-font-name fontname
1314 * *************************************************************************/
1315 case hash_log_font_name :
1316 if (strlcpy(g_szFontFaceName, arg,
1317 sizeof(g_szFontFaceName)) >= sizeof(g_szFontFaceName))
1319 log_error(LOG_LEVEL_FATAL,
1320 "log-font-name argument '%s' is longer than %u characters.",
1321 arg, sizeof(g_szFontFaceName)-1);
1325 /* *************************************************************************
1327 * *************************************************************************/
1328 case hash_log_font_size :
1329 g_nFontSize = atoi(arg);
1332 /* *************************************************************************
1333 * log-highlight-messages (0|1)
1334 * *************************************************************************/
1335 case hash_log_highlight_messages :
1336 g_bHighlightMessages = atoi(arg);
1339 /* *************************************************************************
1341 * *************************************************************************/
1342 case hash_log_max_lines :
1343 g_nMaxBufferLines = atoi(arg);
1346 /* *************************************************************************
1347 * log-messages (0|1)
1348 * *************************************************************************/
1349 case hash_log_messages :
1350 g_bLogMessages = atoi(arg);
1353 /* *************************************************************************
1354 * show-on-task-bar (0|1)
1355 * *************************************************************************/
1356 case hash_show_on_task_bar :
1357 g_bShowOnTaskBar = atoi(arg);
1360 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1363 /* *************************************************************************
1364 * Warnings about unsupported features
1365 * *************************************************************************/
1367 case hash_deny_access:
1368 #endif /* ndef FEATURE_ACL */
1369 #ifndef FEATURE_CGI_EDIT_ACTIONS
1370 case hash_enable_edit_actions:
1371 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1372 #ifndef FEATURE_TOGGLE
1373 case hash_enable_remote_toggle:
1374 #endif /* ndef FEATURE_TOGGLE */
1376 case hash_permit_access:
1377 #endif /* ndef FEATURE_ACL */
1378 #ifndef FEATURE_TOGGLE
1380 #endif /* ndef FEATURE_TOGGLE */
1381 #ifndef FEATURE_TRUST
1382 case hash_trustfile :
1383 case hash_trust_info_url :
1384 #endif /* ndef FEATURE_TRUST */
1386 #ifndef _WIN_CONSOLE
1387 case hash_hide_console :
1388 #endif /* ndef _WIN_CONSOLE */
1390 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1391 case hash_activity_animation :
1392 case hash_close_button_minimizes :
1393 case hash_log_buffer_size :
1394 case hash_log_font_name :
1395 case hash_log_font_size :
1396 case hash_log_highlight_messages :
1397 case hash_log_max_lines :
1398 case hash_log_messages :
1399 case hash_show_on_task_bar :
1400 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1401 /* These warnings are annoying - so hide them. -- Jon */
1402 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1405 /* *************************************************************************/
1407 /* *************************************************************************/
1409 * I decided that I liked this better as a warning than an
1410 * error. To change back to an error, just change log level
1411 * to LOG_LEVEL_FATAL.
1413 log_error(LOG_LEVEL_ERROR, "Ignoring unrecognized directive '%s' (%luul) in line %lu "
1414 "in configuration file (%s).", buf, directive_hash, linenum, configfile);
1415 string_append(&config->proxy_args,
1416 " <strong class='warning'>Warning: Ignoring unrecognized directive:</strong>");
1419 /* *************************************************************************/
1420 } /* end switch( hash_string(cmd) ) */
1422 /* Save the argument for the show-status page. */
1423 savearg(cmd, arg, config);
1425 } /* end while ( read_config_line(...) ) */
1429 set_debug_level(config->debug);
1431 freez(config->logfile);
1435 if (NULL != logfile)
1437 config->logfile = logfile;
1438 init_error_log(Argv[0], config->logfile);
1446 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1447 if (config->default_server_timeout > config->keep_alive_timeout)
1449 log_error(LOG_LEVEL_ERROR,
1450 "Reducing the default-server-timeout from %d to the keep-alive-timeout %d.",
1451 config->default_server_timeout, config->keep_alive_timeout);
1452 config->default_server_timeout = config->keep_alive_timeout;
1454 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1456 #ifdef FEATURE_CONNECTION_SHARING
1457 if (config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
1459 if (config->multi_threaded)
1461 set_keep_alive_timeout(config->keep_alive_timeout);
1466 * While we could use keep-alive without multiple threads
1467 * if we didn't bother with enforcing the connection timeout,
1468 * that might make Tor users sad, even though they shouldn't
1469 * enable the single-threaded option anyway.
1471 * XXX: We could still use Proxy-Connection: keep-alive.
1473 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
1474 log_error(LOG_LEVEL_ERROR,
1475 "Config option single-threaded disables connection keep-alive.");
1478 else if ((config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING))
1480 log_error(LOG_LEVEL_ERROR, "Config option connection-sharing "
1481 "has no effect if keep-alive-timeout isn't set.");
1482 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
1484 #endif /* def FEATURE_CONNECTION_SHARING */
1486 if (NULL == config->proxy_args)
1488 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1491 if (config->actions_file[0])
1493 add_loader(load_action_files, config);
1496 if (config->re_filterfile[0])
1498 add_loader(load_re_filterfiles, config);
1501 #ifdef FEATURE_TRUST
1502 if (config->trustfile)
1504 add_loader(load_trustfile, config);
1506 #endif /* def FEATURE_TRUST */
1508 if ( NULL == config->haddr )
1510 config->haddr = strdup( HADDR_DEFAULT );
1513 if ( NULL != config->haddr )
1515 if ((*config->haddr == '[')
1516 && (NULL != (p = strchr(config->haddr, ']')))
1518 && (0 < (config->hport = atoi(p + 2))))
1521 memmove((void *)config->haddr, config->haddr + 1,
1522 (size_t)(p - config->haddr));
1524 else if (NULL != (p = strchr(config->haddr, ':'))
1525 && (0 < (config->hport = atoi(p + 1))))
1531 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1532 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1534 if (*config->haddr == '\0')
1537 * Only the port specified. We stored it in config->hport
1538 * and don't need its text representation anymore.
1540 freez(config->haddr);
1545 * Want to run all the loaders once now.
1547 * Need to set up a fake csp, so they can get to the config.
1549 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1550 fake_csp->config = config;
1552 if (run_loader(fake_csp))
1555 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1556 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1560 /* FIXME: this is a kludge for win32 */
1561 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1563 g_default_actions_file = config->actions_file[1]; /* FIXME Hope this is default.action */
1564 g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1565 g_default_filterfile = config->re_filterfile[0]; /* FIXME Hope this is default.filter */
1566 g_user_filterfile = config->re_filterfile[1]; /* FIXME Hope this is user.filter */
1568 #ifdef FEATURE_TRUST
1569 g_trustfile = config->trustfile;
1570 #endif /* def FEATURE_TRUST */
1573 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1574 /* FIXME: end kludge */
1577 config->need_bind = 1;
1579 if (current_configfile)
1581 struct configuration_spec * oldcfg = (struct configuration_spec *)
1582 current_configfile->f;
1584 * Check if config->haddr,hport == oldcfg->haddr,hport
1586 * The following could be written more compactly as a single,
1587 * (unreadably long) if statement.
1589 config->need_bind = 0;
1590 if (config->hport != oldcfg->hport)
1592 config->need_bind = 1;
1594 else if (config->haddr == NULL)
1596 if (oldcfg->haddr != NULL)
1598 config->need_bind = 1;
1601 else if (oldcfg->haddr == NULL)
1603 config->need_bind = 1;
1605 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1607 config->need_bind = 1;
1610 current_configfile->unloader = unload_configfile;
1613 fs->next = files->next;
1616 current_configfile = fs;
1622 /*********************************************************************
1624 * Function : savearg
1626 * Description : Called from `load_config'. It saves each non-empty
1627 * and non-comment line from config into
1628 * config->proxy_args. This is used to create the
1629 * show-proxy-args page. On error, frees
1630 * config->proxy_args and sets it to NULL
1633 * 1 : command = config setting that was found
1634 * 2 : argument = the setting's argument (if any)
1635 * 3 : config = Configuration to save into.
1639 *********************************************************************/
1640 static void savearg(char *command, char *argument, struct configuration_spec * config)
1649 * Add config option name embedded in
1650 * link to its section in the user-manual
1652 buf = strdup("\n<a href=\"");
1653 if (!strncmpic(config->usermanual, "file://", 7) ||
1654 !strncmpic(config->usermanual, "http", 4))
1656 string_append(&buf, config->usermanual);
1660 string_append(&buf, "http://" CGI_SITE_2_HOST "/user-manual/");
1662 string_append(&buf, CONFIG_HELP_PREFIX);
1663 string_join (&buf, string_toupper(command));
1664 string_append(&buf, "\">");
1665 string_append(&buf, command);
1666 string_append(&buf, "</a> ");
1670 freez(config->proxy_args);
1674 if ( (NULL != argument) && ('\0' != *argument) )
1676 s = html_encode(argument);
1680 freez(config->proxy_args);
1684 if (strncmpic(argument, "http://", 7) == 0)
1686 string_append(&buf, "<a href=\"");
1687 string_append(&buf, s);
1688 string_append(&buf, "\">");
1689 string_join (&buf, s);
1690 string_append(&buf, "</a>");
1694 string_join (&buf, s);
1698 string_append(&buf, "<br>");
1699 string_join(&config->proxy_args, buf);