1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.125 2012/03/09 16:23:50 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>
89 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
92 /* Privoxy is enabled by default. */
93 int global_toggle_state = 1;
94 #endif /* def FEATURE_TOGGLE */
96 /* The filename of the configfile */
97 const char *configfile = NULL;
100 * CGI functions will later need access to the invocation args,
101 * so we will make argc and argv global.
104 char * const * Argv = NULL;
106 static struct file_list *current_configfile = NULL;
110 * This takes the "cryptic" hash of each keyword and aliases them to
111 * something a little more readable. This also makes changing the
112 * hash values easier if they should change or the hash algorthm changes.
113 * Use the included "hash" program to find out what the hash will be
114 * for any string supplied on the command line. (Or just put it in the
115 * config file and read the number from the error message in the log).
117 * Please keep this list sorted alphabetically (but with the Windows
118 * console and GUI specific options last).
121 #define hash_actions_file 1196306641U /* "actionsfile" */
122 #define hash_accept_intercepted_requests 1513024973U /* "accept-intercepted-requests" */
123 #define hash_admin_address 4112573064U /* "admin-address" */
124 #define hash_allow_cgi_request_crunching 258915987U /* "allow-cgi-request-crunching" */
125 #define hash_buffer_limit 1881726070U /* "buffer-limit */
126 #define hash_compression_level 2464423563U /* "compression-level" */
127 #define hash_confdir 1978389U /* "confdir" */
128 #define hash_connection_sharing 1348841265U /* "connection-sharing" */
129 #define hash_debug 78263U /* "debug" */
130 #define hash_default_server_timeout 2530089913U /* "default-server-timeout" */
131 #define hash_deny_access 1227333715U /* "deny-access" */
132 #define hash_enable_edit_actions 2517097536U /* "enable-edit-actions" */
133 #define hash_enable_compression 3943696946U /* "enable-compression" */
134 #define hash_enable_remote_toggle 2979744683U /* "enable-remote-toggle" */
135 #define hash_enable_remote_http_toggle 110543988U /* "enable-remote-http-toggle" */
136 #define hash_enforce_blocks 1862427469U /* "enforce-blocks" */
137 #define hash_filterfile 250887266U /* "filterfile" */
138 #define hash_forward 2029845U /* "forward" */
139 #define hash_forward_socks4 3963965521U /* "forward-socks4" */
140 #define hash_forward_socks4a 2639958518U /* "forward-socks4a" */
141 #define hash_forward_socks5 3963965522U /* "forward-socks5" */
142 #define hash_forwarded_connect_retries 101465292U /* "forwarded-connect-retries" */
143 #define hash_handle_as_empty_returns_ok 1444873247U /* "handle-as-empty-doc-returns-ok" */
144 #define hash_hostname 10308071U /* "hostname" */
145 #define hash_keep_alive_timeout 3878599515U /* "keep-alive-timeout" */
146 #define hash_listen_address 1255650842U /* "listen-address" */
147 #define hash_logdir 422889U /* "logdir" */
148 #define hash_logfile 2114766U /* "logfile" */
149 #define hash_max_client_connections 3595884446U /* "max-client-connections" */
150 #define hash_permit_access 3587953268U /* "permit-access" */
151 #define hash_proxy_info_url 3903079059U /* "proxy-info-url" */
152 #define hash_single_threaded 4250084780U /* "single-threaded" */
153 #define hash_socket_timeout 1809001761U /* "socket-timeout" */
154 #define hash_split_large_cgi_forms 671658948U /* "split-large-cgi-forms" */
155 #define hash_suppress_blocklists 1948693308U /* "suppress-blocklists" */
156 #define hash_templdir 11067889U /* "templdir" */
157 #define hash_toggle 447966U /* "toggle" */
158 #define hash_trust_info_url 430331967U /* "trust-info-url" */
159 #define hash_trustfile 56494766U /* "trustfile" */
160 #define hash_usermanual 1416668518U /* "user-manual" */
161 #define hash_activity_animation 1817904738U /* "activity-animation" */
162 #define hash_close_button_minimizes 3651284693U /* "close-button-minimizes" */
163 #define hash_hide_console 2048809870U /* "hide-console" */
164 #define hash_log_buffer_size 2918070425U /* "log-buffer-size" */
165 #define hash_log_font_name 2866730124U /* "log-font-name" */
166 #define hash_log_font_size 2866731014U /* "log-font-size" */
167 #define hash_log_highlight_messages 4032101240U /* "log-highlight-messages" */
168 #define hash_log_max_lines 2868344173U /* "log-max-lines" */
169 #define hash_log_messages 2291744899U /* "log-messages" */
170 #define hash_show_on_task_bar 215410365U /* "show-on-task-bar" */
173 static void savearg(char *command, char *argument, struct configuration_spec * config);
175 /*********************************************************************
177 * Function : unload_configfile
179 * Description : Free the config structure and all components.
182 * 1 : data: struct configuration_spec to unload
186 *********************************************************************/
187 static void unload_configfile (void * data)
189 struct configuration_spec * config = (struct configuration_spec *)data;
190 struct forward_spec *cur_fwd = config->forward;
194 struct access_control_list *cur_acl = config->acl;
196 while (cur_acl != NULL)
198 struct access_control_list * next_acl = cur_acl->next;
203 #endif /* def FEATURE_ACL */
205 while (cur_fwd != NULL)
207 struct forward_spec * next_fwd = cur_fwd->next;
208 free_url_spec(cur_fwd->url);
210 freez(cur_fwd->gateway_host);
211 freez(cur_fwd->forward_host);
215 config->forward = NULL;
217 freez(config->confdir);
218 freez(config->logdir);
219 freez(config->templdir);
220 freez(config->hostname);
222 for (i = 0; i < MAX_LISTENING_SOCKETS; i++)
224 freez(config->haddr[i]);
226 freez(config->logfile);
228 for (i = 0; i < MAX_AF_FILES; i++)
230 freez(config->actions_file_short[i]);
231 freez(config->actions_file[i]);
232 freez(config->re_filterfile_short[i]);
233 freez(config->re_filterfile[i]);
236 freez(config->admin_address);
237 freez(config->proxy_info_url);
238 freez(config->proxy_args);
239 freez(config->usermanual);
242 freez(config->trustfile);
243 list_remove_all(config->trust_info);
244 #endif /* def FEATURE_TRUST */
250 #ifdef FEATURE_GRACEFUL_TERMINATION
251 /*********************************************************************
253 * Function : unload_current_config_file
255 * Description : Unloads current config file - reset to state at
256 * beginning of program.
262 *********************************************************************/
263 void unload_current_config_file(void)
265 if (current_configfile)
267 current_configfile->unloader = unload_configfile;
268 current_configfile = NULL;
274 /*********************************************************************
276 * Function : parse_toggle_value
278 * Description : Parse the value of a directive that can only be
279 * enabled or disabled. Terminates with a fatal error
280 * if the value is NULL or something other than 0 or 1.
283 * 1 : name: The name of the directive. Used for log messages.
284 * 2 : value: The value to parse
287 * Returns : The numerical toggle state
289 *********************************************************************/
290 static int parse_toggle_state(const char *name, const char *value)
293 assert(name != NULL);
294 assert(value != NULL);
296 if ((value == NULL) || (*value == '\0'))
298 log_error(LOG_LEVEL_FATAL, "Directive %s used without argument", name);
301 toggle_state = atoi(value);
304 * Also check the length as atoi() doesn't mind
305 * garbage after a valid integer, but we do.
307 if (((toggle_state != 0) && (toggle_state != 1)) || (strlen(value) != 1))
309 log_error(LOG_LEVEL_FATAL,
310 "Directive %s used with invalid argument '%s'. Use either '0' or '1'.",
319 /*********************************************************************
321 * Function : load_config
323 * Description : Load the config file and all parameters.
325 * XXX: more than thousand lines long
326 * and thus in serious need of refactoring.
330 * Returns : The configuration_spec, or NULL on error.
332 *********************************************************************/
333 struct configuration_spec * load_config(void)
337 FILE *configfp = NULL;
338 struct configuration_spec * config = NULL;
339 struct client_state * fake_csp;
340 struct file_list *fs;
341 unsigned long linenum = 0;
343 char *logfile = NULL;
345 if (!check_file_changed(current_configfile, configfile, &fs))
347 /* No need to load */
348 return ((struct configuration_spec *)current_configfile->f);
352 log_error(LOG_LEVEL_FATAL,
353 "can't check configuration file '%s': %E", configfile);
357 if (NULL != current_configfile)
359 log_error(LOG_LEVEL_INFO, "Reloading configuration file '%s'", configfile);
362 #ifdef FEATURE_TOGGLE
363 global_toggle_state = 1;
364 #endif /* def FEATURE_TOGGLE */
366 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
372 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
377 * This is backwards from how it's usually done.
378 * Following the usual pattern, "fs" would be stored in a member
379 * variable in "csp", and then we'd access "config" from "fs->f",
380 * using a cast. However, "config" is used so often that a
381 * cast each time would be very ugly, and the extra indirection
382 * would waste CPU cycles. Therefore we store "config" in
383 * "csp->config", and "fs" in "csp->config->config_file_list".
385 config->config_file_list = fs;
390 config->multi_threaded = 1;
391 config->buffer_limit = 4096 * 1024;
392 config->usermanual = strdup(USER_MANUAL_URL);
393 config->proxy_args = strdup("");
394 config->forwarded_connect_retries = 0;
395 config->max_client_connections = 0;
396 config->socket_timeout = 300; /* XXX: Should be a macro. */
397 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
398 config->default_server_timeout = 0;
399 config->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
400 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
401 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
403 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
404 config->feature_flags &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
405 config->feature_flags &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
406 config->feature_flags &= ~RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
407 #ifdef FEATURE_COMPRESSION
408 config->feature_flags &= ~RUNTIME_FEATURE_COMPRESSION;
410 * XXX: Run some benchmarks to see if there are better default values.
412 config->compression_level = 1;
415 configfp = fopen(configfile, "r");
416 if (NULL == configfp)
418 log_error(LOG_LEVEL_FATAL,
419 "can't open configuration file '%s': %E", configfile);
420 /* Never get here - LOG_LEVEL_FATAL causes program exit */
423 while (read_config_line(configfp, &linenum, &buf) != NULL)
425 char cmd[BUFFER_SIZE];
426 char arg[BUFFER_SIZE];
427 char tmp[BUFFER_SIZE];
429 struct access_control_list *cur_acl;
430 #endif /* def FEATURE_ACL */
431 struct forward_spec *cur_fwd;
434 unsigned int directive_hash;
436 strlcpy(tmp, buf, sizeof(tmp));
438 /* Copy command (i.e. up to space or tab) into cmd */
441 while (*p && (*p != ' ') && (*p != '\t'))
447 /* Skip over the whitespace in buf */
448 while (*p && ((*p == ' ') || (*p == '\t')))
453 /* Copy the argument into arg */
454 if (strlcpy(arg, p, sizeof(arg)) >= sizeof(arg))
456 log_error(LOG_LEVEL_FATAL, "Config line too long: %s", buf);
459 /* Should never happen, but check this anyway */
466 /* Make sure the command field is lower case */
467 for (p = cmd; *p; p++)
469 if (privoxy_isupper(*p))
471 *p = (char)privoxy_tolower(*p);
475 directive_hash = hash_string(cmd);
476 switch (directive_hash)
478 /* *************************************************************************
479 * actionsfile actions-file-name
480 * In confdir by default
481 * *************************************************************************/
482 case hash_actions_file :
484 while ((i < MAX_AF_FILES) && (NULL != config->actions_file[i]))
489 if (i >= MAX_AF_FILES)
491 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
492 "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
495 config->actions_file_short[i] = strdup(arg);
496 config->actions_file[i] = make_path(config->confdir, arg);
499 /* *************************************************************************
500 * accept-intercepted-requests
501 * *************************************************************************/
502 case hash_accept_intercepted_requests:
503 if (parse_toggle_state(cmd, arg) == 1)
505 config->feature_flags |= RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
509 config->feature_flags &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
513 /* *************************************************************************
514 * admin-address email-address
515 * *************************************************************************/
516 case hash_admin_address :
517 freez(config->admin_address);
518 config->admin_address = strdup(arg);
521 /* *************************************************************************
522 * allow-cgi-request-crunching
523 * *************************************************************************/
524 case hash_allow_cgi_request_crunching:
525 if (parse_toggle_state(cmd, arg) == 1)
527 config->feature_flags |= RUNTIME_FEATURE_CGI_CRUNCHING;
531 config->feature_flags &= ~RUNTIME_FEATURE_CGI_CRUNCHING;
535 /* *************************************************************************
537 * *************************************************************************/
538 case hash_buffer_limit :
539 config->buffer_limit = (size_t)(1024 * atoi(arg));
542 /* *************************************************************************
543 * confdir directory-name
544 * *************************************************************************/
546 freez(config->confdir);
547 config->confdir = make_path(NULL, arg);
550 /* *************************************************************************
551 * compression-level 0-9
552 * *************************************************************************/
553 #ifdef FEATURE_COMPRESSION
554 case hash_compression_level :
557 int compression_level = atoi(arg);
558 if (-1 <= compression_level && compression_level <= 9)
560 config->compression_level = compression_level;;
564 log_error(LOG_LEVEL_FATAL,
565 "Invalid compression-level value: %s", arg);
570 log_error(LOG_LEVEL_FATAL,
571 "Invalid compression-level directive. Compression value missing");
576 /* *************************************************************************
577 * connection-sharing (0|1)
578 * *************************************************************************/
579 #ifdef FEATURE_CONNECTION_SHARING
580 case hash_connection_sharing :
581 if (parse_toggle_state(cmd, arg) == 1)
583 config->feature_flags |= RUNTIME_FEATURE_CONNECTION_SHARING;
587 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
592 /* *************************************************************************
594 * Specifies debug level, multiple values are ORed together.
595 * *************************************************************************/
597 config->debug |= atoi(arg);
600 /* *************************************************************************
601 * default-server-timeout timeout
602 * *************************************************************************/
603 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
604 case hash_default_server_timeout :
607 int timeout = atoi(arg);
610 config->default_server_timeout = (unsigned int)timeout;
614 log_error(LOG_LEVEL_FATAL,
615 "Invalid default-server-timeout value: %s", arg);
621 /* *************************************************************************
622 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
623 * *************************************************************************/
625 case hash_deny_access:
626 strlcpy(tmp, arg, sizeof(tmp));
627 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
629 if ((vec_count != 1) && (vec_count != 2))
631 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
632 "deny-access directive in configuration file.");
633 string_append(&config->proxy_args,
634 "<br>\nWARNING: Wrong number of parameters for "
635 "deny-access directive in configuration file.<br><br>\n");
639 /* allocate a new node */
640 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
644 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
645 /* Never get here - LOG_LEVEL_FATAL causes program exit */
648 cur_acl->action = ACL_DENY;
650 if (acl_addr(vec[0], cur_acl->src) < 0)
652 log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
653 "for deny-access directive in configuration file: \"%s\"", vec[0]);
654 string_append(&config->proxy_args,
655 "<br>\nWARNING: Invalid source address, port or netmask "
656 "for deny-access directive in configuration file: \"");
657 string_append(&config->proxy_args,
659 string_append(&config->proxy_args,
666 if (acl_addr(vec[1], cur_acl->dst) < 0)
668 log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
669 "for deny-access directive in configuration file: \"%s\"", vec[1]);
670 string_append(&config->proxy_args,
671 "<br>\nWARNING: Invalid destination address, port or netmask "
672 "for deny-access directive in configuration file: \"");
673 string_append(&config->proxy_args,
675 string_append(&config->proxy_args,
684 cur_acl->wildcard_dst = 1;
686 #endif /* def HAVE_RFC2553 */
689 * Add it to the list. Note we reverse the list to get the
690 * behaviour the user expects. With both the ACL and
691 * actions file, the last match wins. However, the internal
692 * implementations are different: The actions file is stored
693 * in the same order as the file, and scanned completely.
694 * With the ACL, we reverse the order as we load it, then
695 * when we scan it we stop as soon as we get a match.
697 cur_acl->next = config->acl;
698 config->acl = cur_acl;
701 #endif /* def FEATURE_ACL */
703 /* *************************************************************************
704 * enable-edit-actions 0|1
705 * *************************************************************************/
706 #ifdef FEATURE_CGI_EDIT_ACTIONS
707 case hash_enable_edit_actions:
708 if (parse_toggle_state(cmd, arg) == 1)
710 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
714 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
717 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
719 /* *************************************************************************
720 * enable-compression 0|1
721 * *************************************************************************/
722 #ifdef FEATURE_COMPRESSION
723 case hash_enable_compression:
724 if (parse_toggle_state(cmd, arg) == 1)
726 config->feature_flags |= RUNTIME_FEATURE_COMPRESSION;
730 config->feature_flags &= ~RUNTIME_FEATURE_COMPRESSION;
733 #endif /* def FEATURE_COMPRESSION */
736 /* *************************************************************************
737 * enable-remote-toggle 0|1
738 * *************************************************************************/
739 #ifdef FEATURE_TOGGLE
740 case hash_enable_remote_toggle:
741 if (parse_toggle_state(cmd, arg) == 1)
743 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
747 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
750 #endif /* def FEATURE_TOGGLE */
752 /* *************************************************************************
753 * enable-remote-http-toggle 0|1
754 * *************************************************************************/
755 case hash_enable_remote_http_toggle:
756 if (parse_toggle_state(cmd, arg) == 1)
758 config->feature_flags |= RUNTIME_FEATURE_HTTP_TOGGLE;
762 config->feature_flags &= ~RUNTIME_FEATURE_HTTP_TOGGLE;
766 /* *************************************************************************
768 * *************************************************************************/
769 case hash_enforce_blocks:
770 #ifdef FEATURE_FORCE_LOAD
771 if (parse_toggle_state(cmd, arg) == 1)
773 config->feature_flags |= RUNTIME_FEATURE_ENFORCE_BLOCKS;
777 config->feature_flags &= ~RUNTIME_FEATURE_ENFORCE_BLOCKS;
780 log_error(LOG_LEVEL_ERROR, "Ignoring directive 'enforce-blocks'. "
781 "FEATURE_FORCE_LOAD is disabled, blocks will always be enforced.");
782 #endif /* def FEATURE_FORCE_LOAD */
785 /* *************************************************************************
786 * filterfile file-name
787 * In confdir by default.
788 * *************************************************************************/
789 case hash_filterfile :
791 while ((i < MAX_AF_FILES) && (NULL != config->re_filterfile[i]))
796 if (i >= MAX_AF_FILES)
798 log_error(LOG_LEVEL_FATAL, "Too many 'filterfile' directives in config file - limit is %d.\n"
799 "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
802 config->re_filterfile_short[i] = strdup(arg);
803 config->re_filterfile[i] = make_path(config->confdir, arg);
807 /* *************************************************************************
808 * forward url-pattern (.|http-proxy-host[:port])
809 * *************************************************************************/
811 strlcpy(tmp, arg, sizeof(tmp));
812 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
816 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
817 "directive in configuration file.");
818 string_append(&config->proxy_args,
819 "<br>\nWARNING: Wrong number of parameters for "
820 "forward directive in configuration file.");
824 /* allocate a new node */
825 cur_fwd = zalloc(sizeof(*cur_fwd));
828 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
829 /* Never get here - LOG_LEVEL_FATAL causes program exit */
833 cur_fwd->type = SOCKS_NONE;
835 /* Save the URL pattern */
836 if (create_url_spec(cur_fwd->url, vec[0]))
838 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
839 "directive in configuration file.");
840 string_append(&config->proxy_args,
841 "<br>\nWARNING: Bad URL specifier for "
842 "forward directive in configuration file.");
846 /* Parse the parent HTTP proxy host:port */
849 if (strcmp(p, ".") != 0)
851 cur_fwd->forward_port = 8000;
852 parse_forwarder_address(p, &cur_fwd->forward_host,
853 &cur_fwd->forward_port);
857 cur_fwd->next = config->forward;
858 config->forward = cur_fwd;
862 /* *************************************************************************
863 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
864 * *************************************************************************/
865 case hash_forward_socks4:
866 strlcpy(tmp, arg, sizeof(tmp));
867 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
871 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
872 "forward-socks4 directive in configuration file.");
873 string_append(&config->proxy_args,
874 "<br>\nWARNING: Wrong number of parameters for "
875 "forward-socks4 directive in configuration file.");
879 /* allocate a new node */
880 cur_fwd = zalloc(sizeof(*cur_fwd));
883 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
884 /* Never get here - LOG_LEVEL_FATAL causes program exit */
888 cur_fwd->type = SOCKS_4;
890 /* Save the URL pattern */
891 if (create_url_spec(cur_fwd->url, vec[0]))
893 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
894 "directive in configuration file.");
895 string_append(&config->proxy_args,
896 "<br>\nWARNING: Bad URL specifier for "
897 "forward-socks4 directive in configuration file.");
901 /* Parse the SOCKS proxy host[:port] */
904 /* XXX: This check looks like a bug. */
905 if (strcmp(p, ".") != 0)
907 cur_fwd->gateway_port = 1080;
908 parse_forwarder_address(p, &cur_fwd->gateway_host,
909 &cur_fwd->gateway_port);
912 /* Parse the parent HTTP proxy host[:port] */
915 if (strcmp(p, ".") != 0)
917 cur_fwd->forward_port = 8000;
918 parse_forwarder_address(p, &cur_fwd->forward_host,
919 &cur_fwd->forward_port);
923 cur_fwd->next = config->forward;
924 config->forward = cur_fwd;
928 /* *************************************************************************
929 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
930 * *************************************************************************/
931 case hash_forward_socks4a:
932 case hash_forward_socks5:
933 strlcpy(tmp, arg, sizeof(tmp));
934 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
938 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
939 "forward-socks4a directive in configuration file.");
940 string_append(&config->proxy_args,
941 "<br>\nWARNING: Wrong number of parameters for "
942 "forward-socks4a directive in configuration file.");
946 /* allocate a new node */
947 cur_fwd = zalloc(sizeof(*cur_fwd));
950 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
951 /* Never get here - LOG_LEVEL_FATAL causes program exit */
955 if (directive_hash == hash_forward_socks4a)
957 cur_fwd->type = SOCKS_4A;
961 cur_fwd->type = SOCKS_5;
964 /* Save the URL pattern */
965 if (create_url_spec(cur_fwd->url, vec[0]))
967 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
968 "directive in configuration file.");
969 string_append(&config->proxy_args,
970 "<br>\nWARNING: Bad URL specifier for "
971 "forward-socks4a directive in configuration file.");
975 /* Parse the SOCKS proxy host[:port] */
978 cur_fwd->gateway_port = 1080;
979 parse_forwarder_address(p, &cur_fwd->gateway_host,
980 &cur_fwd->gateway_port);
982 /* Parse the parent HTTP proxy host[:port] */
985 if (strcmp(p, ".") != 0)
987 cur_fwd->forward_port = 8000;
988 parse_forwarder_address(p, &cur_fwd->forward_host,
989 &cur_fwd->forward_port);
993 cur_fwd->next = config->forward;
994 config->forward = cur_fwd;
998 /* *************************************************************************
999 * forwarded-connect-retries n
1000 * *************************************************************************/
1001 case hash_forwarded_connect_retries :
1002 config->forwarded_connect_retries = atoi(arg);
1005 /* *************************************************************************
1006 * handle-as-empty-doc-returns-ok 0|1
1008 * Workaround for firefox hanging on blocked javascript pages.
1009 * Block with the "+handle-as-empty-document" flag and set the
1010 * "handle-as-empty-doc-returns-ok" run-time config flag so that
1011 * Privoxy returns a 200/OK status instead of a 403/Forbidden status
1012 * to the browser for blocked pages.
1013 ***************************************************************************/
1014 case hash_handle_as_empty_returns_ok:
1015 if (parse_toggle_state(cmd, arg) == 1)
1017 config->feature_flags |= RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
1021 config->feature_flags &= ~RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
1025 /* *************************************************************************
1026 * hostname hostname-to-show-on-cgi-pages
1027 * *************************************************************************/
1028 case hash_hostname :
1029 freez(config->hostname);
1030 config->hostname = strdup(arg);
1031 if (NULL == config->hostname)
1033 log_error(LOG_LEVEL_FATAL, "Out of memory saving hostname.");
1037 /* *************************************************************************
1038 * keep-alive-timeout timeout
1039 * *************************************************************************/
1040 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1041 case hash_keep_alive_timeout :
1044 int timeout = atoi(arg);
1047 config->feature_flags |= RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
1048 config->keep_alive_timeout = (unsigned int)timeout;
1052 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
1058 /* *************************************************************************
1059 * listen-address [ip][:port]
1060 * *************************************************************************/
1061 case hash_listen_address :
1063 while ((i < MAX_LISTENING_SOCKETS) && (NULL != config->haddr[i]))
1068 if (i >= MAX_LISTENING_SOCKETS)
1070 log_error(LOG_LEVEL_FATAL, "Too many 'listen-address' directives in config file - limit is %d.\n"
1071 "(You can increase this limit by changing MAX_LISTENING_SOCKETS in project.h and recompiling).",
1072 MAX_LISTENING_SOCKETS);
1074 config->haddr[i] = strdup(arg);
1075 if (NULL == config->haddr[i])
1077 log_error(LOG_LEVEL_FATAL, "Out of memory while copying listening address");
1081 /* *************************************************************************
1082 * logdir directory-name
1083 * *************************************************************************/
1085 freez(config->logdir);
1086 config->logdir = make_path(NULL, arg);
1089 /* *************************************************************************
1090 * logfile log-file-name
1091 * In logdir by default
1092 * *************************************************************************/
1096 logfile = make_path(config->logdir, arg);
1097 if (NULL == logfile)
1099 log_error(LOG_LEVEL_FATAL, "Out of memory while creating logfile path");
1104 /* *************************************************************************
1105 * max-client-connections number
1106 * *************************************************************************/
1107 case hash_max_client_connections :
1110 int max_client_connections = atoi(arg);
1111 if (0 <= max_client_connections)
1113 config->max_client_connections = max_client_connections;
1118 /* *************************************************************************
1119 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1120 * *************************************************************************/
1122 case hash_permit_access:
1123 strlcpy(tmp, arg, sizeof(tmp));
1124 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
1126 if ((vec_count != 1) && (vec_count != 2))
1128 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1129 "permit-access directive in configuration file.");
1130 string_append(&config->proxy_args,
1131 "<br>\nWARNING: Wrong number of parameters for "
1132 "permit-access directive in configuration file.<br><br>\n");
1137 /* allocate a new node */
1138 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1140 if (cur_acl == NULL)
1142 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1143 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1146 cur_acl->action = ACL_PERMIT;
1148 if (acl_addr(vec[0], cur_acl->src) < 0)
1150 log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
1151 "for permit-access directive in configuration file: \"%s\"", vec[0]);
1152 string_append(&config->proxy_args,
1153 "<br>\nWARNING: Invalid source address, port or netmask for "
1154 "permit-access directive in configuration file: \"");
1155 string_append(&config->proxy_args,
1157 string_append(&config->proxy_args,
1164 if (acl_addr(vec[1], cur_acl->dst) < 0)
1166 log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
1167 "for permit-access directive in configuration file: \"%s\"", vec[1]);
1168 string_append(&config->proxy_args,
1169 "<br>\nWARNING: Invalid destination address, port or netmask for "
1170 "permit-access directive in configuration file: \"");
1171 string_append(&config->proxy_args,
1173 string_append(&config->proxy_args,
1182 cur_acl->wildcard_dst = 1;
1184 #endif /* def HAVE_RFC2553 */
1187 * Add it to the list. Note we reverse the list to get the
1188 * behaviour the user expects. With both the ACL and
1189 * actions file, the last match wins. However, the internal
1190 * implementations are different: The actions file is stored
1191 * in the same order as the file, and scanned completely.
1192 * With the ACL, we reverse the order as we load it, then
1193 * when we scan it we stop as soon as we get a match.
1195 cur_acl->next = config->acl;
1196 config->acl = cur_acl;
1199 #endif /* def FEATURE_ACL */
1201 /* *************************************************************************
1202 * proxy-info-url url
1203 * *************************************************************************/
1204 case hash_proxy_info_url :
1205 freez(config->proxy_info_url);
1206 config->proxy_info_url = strdup(arg);
1209 /* *************************************************************************
1211 * *************************************************************************/
1212 case hash_single_threaded :
1213 config->multi_threaded = 0;
1216 /* *************************************************************************
1217 * socket-timeout numer_of_seconds
1218 * *************************************************************************/
1219 case hash_socket_timeout :
1222 int socket_timeout = atoi(arg);
1223 if (0 < socket_timeout)
1225 config->socket_timeout = socket_timeout;
1229 log_error(LOG_LEVEL_FATAL,
1230 "Invalid socket-timeout: '%s'", arg);
1235 /* *************************************************************************
1236 * split-large-cgi-forms
1237 * *************************************************************************/
1238 case hash_split_large_cgi_forms :
1239 if (parse_toggle_state(cmd, arg) == 1)
1241 config->feature_flags |= RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1245 config->feature_flags &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1249 /* *************************************************************************
1250 * templdir directory-name
1251 * *************************************************************************/
1252 case hash_templdir :
1253 freez(config->templdir);
1254 config->templdir = make_path(NULL, arg);
1257 /* *************************************************************************
1259 * *************************************************************************/
1260 #ifdef FEATURE_TOGGLE
1262 global_toggle_state = atoi(arg);
1264 #endif /* def FEATURE_TOGGLE */
1266 /* *************************************************************************
1267 * trust-info-url url
1268 * *************************************************************************/
1269 #ifdef FEATURE_TRUST
1270 case hash_trust_info_url :
1271 enlist(config->trust_info, arg);
1273 #endif /* def FEATURE_TRUST */
1275 /* *************************************************************************
1276 * trustfile filename
1277 * (In confdir by default.)
1278 * *************************************************************************/
1279 #ifdef FEATURE_TRUST
1280 case hash_trustfile :
1281 freez(config->trustfile);
1282 config->trustfile = make_path(config->confdir, arg);
1284 #endif /* def FEATURE_TRUST */
1286 /* *************************************************************************
1288 * *************************************************************************/
1289 case hash_usermanual :
1291 * XXX: If this isn't the first config directive, the
1292 * show-status page links to the website documentation
1293 * for the directives that were already parsed. Lame.
1295 freez(config->usermanual);
1296 config->usermanual = strdup(arg);
1299 /* *************************************************************************
1300 * Win32 Console options:
1301 * *************************************************************************/
1303 /* *************************************************************************
1305 * *************************************************************************/
1307 case hash_hide_console :
1310 #endif /*def _WIN_CONSOLE*/
1313 /* *************************************************************************
1314 * Win32 GUI options:
1315 * *************************************************************************/
1317 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1318 /* *************************************************************************
1319 * activity-animation (0|1)
1320 * *************************************************************************/
1321 case hash_activity_animation :
1322 g_bShowActivityAnimation = atoi(arg);
1325 /* *************************************************************************
1326 * close-button-minimizes (0|1)
1327 * *************************************************************************/
1328 case hash_close_button_minimizes :
1329 g_bCloseHidesWindow = atoi(arg);
1332 /* *************************************************************************
1333 * log-buffer-size (0|1)
1334 * *************************************************************************/
1335 case hash_log_buffer_size :
1336 g_bLimitBufferSize = atoi(arg);
1339 /* *************************************************************************
1340 * log-font-name fontname
1341 * *************************************************************************/
1342 case hash_log_font_name :
1343 if (strlcpy(g_szFontFaceName, arg,
1344 sizeof(g_szFontFaceName)) >= sizeof(g_szFontFaceName))
1346 log_error(LOG_LEVEL_FATAL,
1347 "log-font-name argument '%s' is longer than %u characters.",
1348 arg, sizeof(g_szFontFaceName)-1);
1352 /* *************************************************************************
1354 * *************************************************************************/
1355 case hash_log_font_size :
1356 g_nFontSize = atoi(arg);
1359 /* *************************************************************************
1360 * log-highlight-messages (0|1)
1361 * *************************************************************************/
1362 case hash_log_highlight_messages :
1363 g_bHighlightMessages = atoi(arg);
1366 /* *************************************************************************
1368 * *************************************************************************/
1369 case hash_log_max_lines :
1370 g_nMaxBufferLines = atoi(arg);
1373 /* *************************************************************************
1374 * log-messages (0|1)
1375 * *************************************************************************/
1376 case hash_log_messages :
1377 g_bLogMessages = atoi(arg);
1380 /* *************************************************************************
1381 * show-on-task-bar (0|1)
1382 * *************************************************************************/
1383 case hash_show_on_task_bar :
1384 g_bShowOnTaskBar = atoi(arg);
1387 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1390 /* *************************************************************************
1391 * Warnings about unsupported features
1392 * *************************************************************************/
1394 case hash_deny_access:
1395 #endif /* ndef FEATURE_ACL */
1396 #ifndef FEATURE_CGI_EDIT_ACTIONS
1397 case hash_enable_edit_actions:
1398 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1399 #ifndef FEATURE_TOGGLE
1400 case hash_enable_remote_toggle:
1401 #endif /* ndef FEATURE_TOGGLE */
1403 case hash_permit_access:
1404 #endif /* ndef FEATURE_ACL */
1405 #ifndef FEATURE_TOGGLE
1407 #endif /* ndef FEATURE_TOGGLE */
1408 #ifndef FEATURE_TRUST
1409 case hash_trustfile :
1410 case hash_trust_info_url :
1411 #endif /* ndef FEATURE_TRUST */
1413 #ifndef _WIN_CONSOLE
1414 case hash_hide_console :
1415 #endif /* ndef _WIN_CONSOLE */
1417 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1418 case hash_activity_animation :
1419 case hash_close_button_minimizes :
1420 case hash_log_buffer_size :
1421 case hash_log_font_name :
1422 case hash_log_font_size :
1423 case hash_log_highlight_messages :
1424 case hash_log_max_lines :
1425 case hash_log_messages :
1426 case hash_show_on_task_bar :
1427 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1428 /* These warnings are annoying - so hide them. -- Jon */
1429 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1432 /* *************************************************************************/
1434 /* *************************************************************************/
1436 * I decided that I liked this better as a warning than an
1437 * error. To change back to an error, just change log level
1438 * to LOG_LEVEL_FATAL.
1440 log_error(LOG_LEVEL_ERROR, "Ignoring unrecognized directive "
1441 "'%s' (%uU) in line %lu in configuration file (%s).",
1442 buf, directive_hash, linenum, configfile);
1443 string_append(&config->proxy_args,
1444 " <strong class='warning'>Warning: Ignoring unrecognized directive:</strong>");
1447 /* *************************************************************************/
1448 } /* end switch(hash_string(cmd)) */
1450 /* Save the argument for the show-status page. */
1451 savearg(cmd, arg, config);
1453 } /* end while (read_config_line(...)) */
1457 set_debug_level(config->debug);
1459 freez(config->logfile);
1463 if (NULL != logfile)
1465 config->logfile = logfile;
1466 init_error_log(Argv[0], config->logfile);
1474 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1475 if (config->default_server_timeout > config->keep_alive_timeout)
1477 log_error(LOG_LEVEL_ERROR,
1478 "Reducing the default-server-timeout from %d to the keep-alive-timeout %d.",
1479 config->default_server_timeout, config->keep_alive_timeout);
1480 config->default_server_timeout = config->keep_alive_timeout;
1482 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1484 #ifdef FEATURE_CONNECTION_SHARING
1485 if (config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
1487 if (config->multi_threaded)
1489 set_keep_alive_timeout(config->keep_alive_timeout);
1494 * While we could use keep-alive without multiple threads
1495 * if we didn't bother with enforcing the connection timeout,
1496 * that might make Tor users sad, even though they shouldn't
1497 * enable the single-threaded option anyway.
1499 * XXX: We could still use Proxy-Connection: keep-alive.
1501 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
1502 log_error(LOG_LEVEL_ERROR,
1503 "Config option single-threaded disables connection keep-alive.");
1506 else if ((config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING))
1508 log_error(LOG_LEVEL_ERROR, "Config option connection-sharing "
1509 "has no effect if keep-alive-timeout isn't set.");
1510 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
1512 #endif /* def FEATURE_CONNECTION_SHARING */
1514 if (NULL == config->proxy_args)
1516 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1519 if (config->re_filterfile[0])
1521 add_loader(load_re_filterfiles, config);
1524 if (config->actions_file[0])
1526 add_loader(load_action_files, config);
1529 #ifdef FEATURE_TRUST
1530 if (config->trustfile)
1532 add_loader(load_trustfile, config);
1534 #endif /* def FEATURE_TRUST */
1536 if (NULL == config->haddr[0])
1538 config->haddr[0] = strdup(HADDR_DEFAULT);
1539 if (NULL == config->haddr[0])
1541 log_error(LOG_LEVEL_FATAL, "Out of memory while copying default listening address");
1545 for (i = 0; i < MAX_LISTENING_SOCKETS && NULL != config->haddr[i]; i++)
1547 if ((*config->haddr[i] == '[')
1548 && (NULL != (p = strchr(config->haddr[i], ']')))
1550 && (0 < (config->hport[i] = atoi(p + 2))))
1553 memmove((void *)config->haddr[i], config->haddr[i] + 1,
1554 (size_t)(p - config->haddr[i]));
1556 else if (NULL != (p = strchr(config->haddr[i], ':'))
1557 && (0 < (config->hport[i] = atoi(p + 1))))
1563 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr[i]);
1564 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1566 if (*config->haddr[i] == '\0')
1569 * Only the port specified. We stored it in config->hport[i]
1570 * and don't need its text representation anymore.
1571 * Use config->hport[i] == 0 to iterate listening addresses since
1574 freez(config->haddr[i]);
1579 * Want to run all the loaders once now.
1581 * Need to set up a fake csp, so they can get to the config.
1583 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1584 fake_csp->config = config;
1586 if (run_loader(fake_csp))
1589 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1590 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1594 /* FIXME: this is a kludge for win32 */
1595 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1597 g_default_actions_file = config->actions_file[1]; /* FIXME Hope this is default.action */
1598 g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1599 g_default_filterfile = config->re_filterfile[0]; /* FIXME Hope this is default.filter */
1600 g_user_filterfile = config->re_filterfile[1]; /* FIXME Hope this is user.filter */
1602 #ifdef FEATURE_TRUST
1603 g_trustfile = config->trustfile;
1604 #endif /* def FEATURE_TRUST */
1607 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1608 /* FIXME: end kludge */
1611 config->need_bind = 1;
1613 if (current_configfile)
1615 struct configuration_spec * oldcfg = (struct configuration_spec *)
1616 current_configfile->f;
1618 * Check if config->haddr[i],hport[i] == oldcfg->haddr[i],hport[i]
1620 * The following could be written more compactly as a single,
1621 * (unreadably long) if statement.
1623 config->need_bind = 0;
1625 for (i = 0; i < MAX_LISTENING_SOCKETS; i++)
1627 if (config->hport[i] != oldcfg->hport[i])
1629 config->need_bind = 1;
1631 else if (config->haddr[i] == NULL)
1633 if (oldcfg->haddr[i] != NULL)
1635 config->need_bind = 1;
1638 else if (oldcfg->haddr[i] == NULL)
1640 config->need_bind = 1;
1642 else if (0 != strcmp(config->haddr[i], oldcfg->haddr[i]))
1644 config->need_bind = 1;
1648 current_configfile->unloader = unload_configfile;
1651 fs->next = files->next;
1654 current_configfile = fs;
1660 /*********************************************************************
1662 * Function : savearg
1664 * Description : Called from `load_config'. It saves each non-empty
1665 * and non-comment line from config into
1666 * config->proxy_args. This is used to create the
1667 * show-proxy-args page. On error, frees
1668 * config->proxy_args and sets it to NULL
1671 * 1 : command = config setting that was found
1672 * 2 : argument = the setting's argument (if any)
1673 * 3 : config = Configuration to save into.
1677 *********************************************************************/
1678 static void savearg(char *command, char *argument, struct configuration_spec * config)
1687 * Add config option name embedded in
1688 * link to its section in the user-manual
1690 buf = strdup("\n<a href=\"");
1691 if (!strncmpic(config->usermanual, "file://", 7) ||
1692 !strncmpic(config->usermanual, "http", 4))
1694 string_append(&buf, config->usermanual);
1698 string_append(&buf, "http://" CGI_SITE_2_HOST "/user-manual/");
1700 string_append(&buf, CONFIG_HELP_PREFIX);
1701 string_join (&buf, string_toupper(command));
1702 string_append(&buf, "\">");
1703 string_append(&buf, command);
1704 string_append(&buf, "</a> ");
1708 freez(config->proxy_args);
1712 if ((NULL != argument) && ('\0' != *argument))
1714 s = html_encode(argument);
1718 freez(config->proxy_args);
1722 if (strncmpic(argument, "http://", 7) == 0)
1724 string_append(&buf, "<a href=\"");
1725 string_append(&buf, s);
1726 string_append(&buf, "\">");
1727 string_join (&buf, s);
1728 string_append(&buf, "</a>");
1732 string_join (&buf, s);
1736 string_append(&buf, "<br>");
1737 string_join(&config->proxy_args, buf);