1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.101 2009/05/13 18:20:54 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 const char **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_confdir 1978389ul /* "confdir" */
137 #define hash_connection_sharing 1348841265ul /* "connection-sharing" */
138 #define hash_debug 78263ul /* "debug" */
139 #define hash_deny_access 1227333715ul /* "deny-access" */
140 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
141 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
142 #define hash_enable_remote_http_toggle 110543988ul /* "enable-remote-http-toggle" */
143 #define hash_enforce_blocks 1862427469ul /* "enforce-blocks" */
144 #define hash_filterfile 250887266ul /* "filterfile" */
145 #define hash_forward 2029845ul /* "forward" */
146 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
147 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
148 #define hash_forward_socks5 3963965522ul /* "forward-socks5" */
149 #define hash_forwarded_connect_retries 101465292ul /* "forwarded-connect-retries" */
150 #define hash_hostname 10308071ul /* "hostname" */
151 #define hash_keep_alive_timeout 3878599515ul /* "keep-alive-timeout" */
152 #define hash_listen_address 1255650842ul /* "listen-address" */
153 #define hash_logdir 422889ul /* "logdir" */
154 #define hash_logfile 2114766ul /* "logfile" */
155 #define hash_max_client_connections 3595884446ul /* "max-client-connections" */
156 #define hash_permit_access 3587953268ul /* "permit-access" */
157 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
158 #define hash_single_threaded 4250084780ul /* "single-threaded" */
159 #define hash_socket_timeout 1809001761ul /* "socket-timeout" */
160 #define hash_split_large_cgi_forms 671658948ul /* "split-large-cgi-forms" */
161 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
162 #define hash_templdir 11067889ul /* "templdir" */
163 #define hash_toggle 447966ul /* "toggle" */
164 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
165 #define hash_trustfile 56494766ul /* "trustfile" */
166 #define hash_usermanual 1416668518ul /* "user-manual" */
167 #define hash_activity_animation 1817904738ul /* "activity-animation" */
168 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
169 #define hash_hide_console 2048809870ul /* "hide-console" */
170 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
171 #define hash_log_font_name 2866730124ul /* "log-font-name" */
172 #define hash_log_font_size 2866731014ul /* "log-font-size" */
173 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
174 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
175 #define hash_log_messages 2291744899ul /* "log-messages" */
176 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
179 static void savearg(char *command, char *argument, struct configuration_spec * config);
181 /*********************************************************************
183 * Function : unload_configfile
185 * Description : Free the config structure and all components.
188 * 1 : data: struct configuration_spec to unload
192 *********************************************************************/
193 static void unload_configfile (void * data)
195 struct configuration_spec * config = (struct configuration_spec *)data;
196 struct forward_spec *cur_fwd = config->forward;
200 struct access_control_list *cur_acl = config->acl;
202 while (cur_acl != NULL)
204 struct access_control_list * next_acl = cur_acl->next;
209 #endif /* def FEATURE_ACL */
211 while (cur_fwd != NULL)
213 struct forward_spec * next_fwd = cur_fwd->next;
214 free_url_spec(cur_fwd->url);
216 freez(cur_fwd->gateway_host);
217 freez(cur_fwd->forward_host);
221 config->forward = NULL;
223 freez(config->confdir);
224 freez(config->logdir);
225 freez(config->templdir);
226 freez(config->hostname);
228 freez(config->haddr);
229 freez(config->logfile);
231 for (i = 0; i < MAX_AF_FILES; i++)
233 freez(config->actions_file_short[i]);
234 freez(config->actions_file[i]);
235 freez(config->re_filterfile_short[i]);
236 freez(config->re_filterfile[i]);
239 freez(config->admin_address);
240 freez(config->proxy_info_url);
241 freez(config->proxy_args);
242 freez(config->usermanual);
245 freez(config->trustfile);
246 list_remove_all(config->trust_info);
247 #endif /* def FEATURE_TRUST */
249 for (i = 0; i < MAX_AF_FILES; i++)
251 freez(config->re_filterfile[i]);
258 #ifdef FEATURE_GRACEFUL_TERMINATION
259 /*********************************************************************
261 * Function : unload_current_config_file
263 * Description : Unloads current config file - reset to state at
264 * beginning of program.
270 *********************************************************************/
271 void unload_current_config_file(void)
273 if (current_configfile)
275 current_configfile->unloader = unload_configfile;
276 current_configfile = NULL;
282 /*********************************************************************
284 * Function : load_config
286 * Description : Load the config file and all parameters.
288 * XXX: more than thousand lines long
289 * and thus in serious need of refactoring.
293 * Returns : The configuration_spec, or NULL on error.
295 *********************************************************************/
296 struct configuration_spec * load_config(void)
298 char buf[BUFFER_SIZE];
300 FILE *configfp = NULL;
301 struct configuration_spec * config = NULL;
302 struct client_state * fake_csp;
303 struct file_list *fs;
304 unsigned long linenum = 0;
306 char *logfile = NULL;
308 if (!check_file_changed(current_configfile, configfile, &fs))
310 /* No need to load */
311 return ((struct configuration_spec *)current_configfile->f);
315 log_error(LOG_LEVEL_FATAL,
316 "can't check configuration file '%s': %E", configfile);
320 if (NULL != current_configfile)
322 log_error(LOG_LEVEL_INFO, "Reloading configuration file '%s'", configfile);
325 #ifdef FEATURE_TOGGLE
326 global_toggle_state = 1;
327 #endif /* def FEATURE_TOGGLE */
329 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
335 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
340 * This is backwards from how it's usually done.
341 * Following the usual pattern, "fs" would be stored in a member
342 * variable in "csp", and then we'd access "config" from "fs->f",
343 * using a cast. However, "config" is used so often that a
344 * cast each time would be very ugly, and the extra indirection
345 * would waste CPU cycles. Therefore we store "config" in
346 * "csp->config", and "fs" in "csp->config->config_file_list".
348 config->config_file_list = fs;
353 config->multi_threaded = 1;
354 config->buffer_limit = 4096 * 1024;
355 config->usermanual = strdup(USER_MANUAL_URL);
356 config->proxy_args = strdup("");
357 config->forwarded_connect_retries = 0;
358 config->max_client_connections = 0;
359 config->socket_timeout = 300; /* XXX: Should be a macro. */
360 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
361 config->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
362 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
363 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
365 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
366 config->feature_flags &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
367 config->feature_flags &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
369 configfp = fopen(configfile, "r");
370 if (NULL == configfp)
372 log_error(LOG_LEVEL_FATAL,
373 "can't open configuration file '%s': %E", configfile);
374 /* Never get here - LOG_LEVEL_FATAL causes program exit */
377 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
379 char cmd[BUFFER_SIZE];
380 char arg[BUFFER_SIZE];
381 char tmp[BUFFER_SIZE];
383 struct access_control_list *cur_acl;
384 #endif /* def FEATURE_ACL */
385 struct forward_spec *cur_fwd;
388 unsigned long directive_hash;
390 strlcpy(tmp, buf, sizeof(tmp));
392 /* Copy command (i.e. up to space or tab) into cmd */
395 while (*p && (*p != ' ') && (*p != '\t'))
401 /* Skip over the whitespace in buf */
402 while (*p && ((*p == ' ') || (*p == '\t')))
407 /* Copy the argument into arg */
408 strlcpy(arg, p, sizeof(arg));
410 /* Should never happen, but check this anyway */
416 /* Make sure the command field is lower case */
417 for (p = cmd; *p; p++)
421 *p = (char)ijb_tolower(*p);
425 directive_hash = hash_string(cmd);
426 switch (directive_hash)
428 /* *************************************************************************
429 * actionsfile actions-file-name
430 * In confdir by default
431 * *************************************************************************/
432 case hash_actions_file :
434 while ((i < MAX_AF_FILES) && (NULL != config->actions_file[i]))
439 if (i >= MAX_AF_FILES)
441 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
442 "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
445 config->actions_file_short[i] = strdup(arg);
446 config->actions_file[i] = make_path(config->confdir, arg);
449 /* *************************************************************************
450 * accept-intercepted-requests
451 * *************************************************************************/
452 case hash_accept_intercepted_requests:
453 if ((*arg != '\0') && (0 != atoi(arg)))
455 config->feature_flags |= RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
459 config->feature_flags &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
463 /* *************************************************************************
464 * admin-address email-address
465 * *************************************************************************/
466 case hash_admin_address :
467 freez(config->admin_address);
468 config->admin_address = strdup(arg);
471 /* *************************************************************************
472 * allow-cgi-request-crunching
473 * *************************************************************************/
474 case hash_allow_cgi_request_crunching:
475 if ((*arg != '\0') && (0 != atoi(arg)))
477 config->feature_flags |= RUNTIME_FEATURE_CGI_CRUNCHING;
481 config->feature_flags &= ~RUNTIME_FEATURE_CGI_CRUNCHING;
485 /* *************************************************************************
487 * *************************************************************************/
488 case hash_buffer_limit :
489 config->buffer_limit = (size_t)(1024 * atoi(arg));
492 /* *************************************************************************
493 * confdir directory-name
494 * *************************************************************************/
496 freez(config->confdir);
497 config->confdir = make_path( NULL, arg);
500 /* *************************************************************************
501 * connection-sharing (0|1)
502 * *************************************************************************/
503 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
504 case hash_connection_sharing :
505 if ((*arg != '\0') && (0 != atoi(arg)))
507 config->feature_flags |= RUNTIME_FEATURE_CONNECTION_SHARING;
511 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
516 /* *************************************************************************
518 * Specifies debug level, multiple values are ORed together.
519 * *************************************************************************/
521 config->debug |= atoi(arg);
524 /* *************************************************************************
525 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
526 * *************************************************************************/
528 case hash_deny_access:
529 strlcpy(tmp, arg, sizeof(tmp));
530 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
532 if ((vec_count != 1) && (vec_count != 2))
534 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
535 "deny-access directive in configuration file.");
536 string_append(&config->proxy_args,
537 "<br>\nWARNING: Wrong number of parameters for "
538 "deny-access directive in configuration file.<br><br>\n");
542 /* allocate a new node */
543 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
547 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
548 /* Never get here - LOG_LEVEL_FATAL causes program exit */
551 cur_acl->action = ACL_DENY;
553 if (acl_addr(vec[0], cur_acl->src) < 0)
555 log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
556 "for deny-access directive in configuration file: \"%s\"", vec[0]);
557 string_append(&config->proxy_args,
558 "<br>\nWARNING: Invalid source address, port or netmask "
559 "for deny-access directive in configuration file: \"");
560 string_append(&config->proxy_args,
562 string_append(&config->proxy_args,
569 if (acl_addr(vec[1], cur_acl->dst) < 0)
571 log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
572 "for deny-access directive in configuration file: \"%s\"", vec[1]);
573 string_append(&config->proxy_args,
574 "<br>\nWARNING: Invalid destination address, port or netmask "
575 "for deny-access directive in configuration file: \"");
576 string_append(&config->proxy_args,
578 string_append(&config->proxy_args,
587 cur_acl->wildcard_dst = 1;
589 #endif /* def HAVE_RFC2553 */
592 * Add it to the list. Note we reverse the list to get the
593 * behaviour the user expects. With both the ACL and
594 * actions file, the last match wins. However, the internal
595 * implementations are different: The actions file is stored
596 * in the same order as the file, and scanned completely.
597 * With the ACL, we reverse the order as we load it, then
598 * when we scan it we stop as soon as we get a match.
600 cur_acl->next = config->acl;
601 config->acl = cur_acl;
604 #endif /* def FEATURE_ACL */
606 /* *************************************************************************
607 * enable-edit-actions 0|1
608 * *************************************************************************/
609 #ifdef FEATURE_CGI_EDIT_ACTIONS
610 case hash_enable_edit_actions:
611 if ((*arg != '\0') && (0 != atoi(arg)))
613 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
617 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
620 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
622 /* *************************************************************************
623 * enable-remote-toggle 0|1
624 * *************************************************************************/
625 #ifdef FEATURE_TOGGLE
626 case hash_enable_remote_toggle:
627 if ((*arg != '\0') && (0 != atoi(arg)))
629 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
633 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
636 #endif /* def FEATURE_TOGGLE */
638 /* *************************************************************************
639 * enable-remote-http-toggle 0|1
640 * *************************************************************************/
641 case hash_enable_remote_http_toggle:
642 if ((*arg != '\0') && (0 != atoi(arg)))
644 config->feature_flags |= RUNTIME_FEATURE_HTTP_TOGGLE;
648 config->feature_flags &= ~RUNTIME_FEATURE_HTTP_TOGGLE;
652 /* *************************************************************************
654 * *************************************************************************/
655 case hash_enforce_blocks:
656 #ifdef FEATURE_FORCE_LOAD
657 if ((*arg != '\0') && (0 != atoi(arg)))
659 config->feature_flags |= RUNTIME_FEATURE_ENFORCE_BLOCKS;
663 config->feature_flags &= ~RUNTIME_FEATURE_ENFORCE_BLOCKS;
666 log_error(LOG_LEVEL_ERROR, "Ignoring directive 'enforce-blocks'. "
667 "FEATURE_FORCE_LOAD is disabled, blocks will always be enforced.");
668 #endif /* def FEATURE_FORCE_LOAD */
671 /* *************************************************************************
672 * filterfile file-name
673 * In confdir by default.
674 * *************************************************************************/
675 case hash_filterfile :
677 while ((i < MAX_AF_FILES) && (NULL != config->re_filterfile[i]))
682 if (i >= MAX_AF_FILES)
684 log_error(LOG_LEVEL_FATAL, "Too many 'filterfile' directives in config file - limit is %d.\n"
685 "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
688 config->re_filterfile_short[i] = strdup(arg);
689 config->re_filterfile[i] = make_path(config->confdir, arg);
693 /* *************************************************************************
694 * forward url-pattern (.|http-proxy-host[:port])
695 * *************************************************************************/
697 strlcpy(tmp, arg, sizeof(tmp));
698 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
702 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
703 "directive in configuration file.");
704 string_append(&config->proxy_args,
705 "<br>\nWARNING: Wrong number of parameters for "
706 "forward directive in configuration file.");
710 /* allocate a new node */
711 cur_fwd = zalloc(sizeof(*cur_fwd));
714 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
715 /* Never get here - LOG_LEVEL_FATAL causes program exit */
719 cur_fwd->type = SOCKS_NONE;
721 /* Save the URL pattern */
722 if (create_url_spec(cur_fwd->url, vec[0]))
724 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
725 "directive in configuration file.");
726 string_append(&config->proxy_args,
727 "<br>\nWARNING: Bad URL specifier for "
728 "forward directive in configuration file.");
732 /* Parse the parent HTTP proxy host:port */
735 if (strcmp(p, ".") != 0)
737 cur_fwd->forward_port = 8000;
738 parse_forwarder_address(p, &cur_fwd->forward_host,
739 &cur_fwd->forward_port);
743 cur_fwd->next = config->forward;
744 config->forward = cur_fwd;
748 /* *************************************************************************
749 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
750 * *************************************************************************/
751 case hash_forward_socks4:
752 strlcpy(tmp, arg, sizeof(tmp));
753 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
757 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
758 "forward-socks4 directive in configuration file.");
759 string_append(&config->proxy_args,
760 "<br>\nWARNING: Wrong number of parameters for "
761 "forward-socks4 directive in configuration file.");
765 /* allocate a new node */
766 cur_fwd = zalloc(sizeof(*cur_fwd));
769 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
770 /* Never get here - LOG_LEVEL_FATAL causes program exit */
774 cur_fwd->type = SOCKS_4;
776 /* Save the URL pattern */
777 if (create_url_spec(cur_fwd->url, vec[0]))
779 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
780 "directive in configuration file.");
781 string_append(&config->proxy_args,
782 "<br>\nWARNING: Bad URL specifier for "
783 "forward-socks4 directive in configuration file.");
787 /* Parse the SOCKS proxy host[:port] */
790 /* XXX: This check looks like a bug. */
791 if (strcmp(p, ".") != 0)
793 cur_fwd->gateway_port = 1080;
794 parse_forwarder_address(p, &cur_fwd->gateway_host,
795 &cur_fwd->gateway_port);
798 /* Parse the parent HTTP proxy host[:port] */
801 if (strcmp(p, ".") != 0)
803 cur_fwd->forward_port = 8000;
804 parse_forwarder_address(p, &cur_fwd->forward_host,
805 &cur_fwd->forward_port);
809 cur_fwd->next = config->forward;
810 config->forward = cur_fwd;
814 /* *************************************************************************
815 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
816 * *************************************************************************/
817 case hash_forward_socks4a:
818 case hash_forward_socks5:
819 strlcpy(tmp, arg, sizeof(tmp));
820 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
824 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
825 "forward-socks4a directive in configuration file.");
826 string_append(&config->proxy_args,
827 "<br>\nWARNING: Wrong number of parameters for "
828 "forward-socks4a directive in configuration file.");
832 /* allocate a new node */
833 cur_fwd = zalloc(sizeof(*cur_fwd));
836 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
837 /* Never get here - LOG_LEVEL_FATAL causes program exit */
841 if (directive_hash == hash_forward_socks4a)
843 cur_fwd->type = SOCKS_4A;
847 cur_fwd->type = SOCKS_5;
850 /* Save the URL pattern */
851 if (create_url_spec(cur_fwd->url, vec[0]))
853 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
854 "directive in configuration file.");
855 string_append(&config->proxy_args,
856 "<br>\nWARNING: Bad URL specifier for "
857 "forward-socks4a directive in configuration file.");
861 /* Parse the SOCKS proxy host[:port] */
864 cur_fwd->gateway_port = 1080;
865 parse_forwarder_address(p, &cur_fwd->gateway_host,
866 &cur_fwd->gateway_port);
868 /* Parse the parent HTTP proxy host[:port] */
871 if (strcmp(p, ".") != 0)
873 cur_fwd->forward_port = 8000;
874 parse_forwarder_address(p, &cur_fwd->forward_host,
875 &cur_fwd->forward_port);
879 cur_fwd->next = config->forward;
880 config->forward = cur_fwd;
884 /* *************************************************************************
885 * forwarded-connect-retries n
886 * *************************************************************************/
887 case hash_forwarded_connect_retries :
888 config->forwarded_connect_retries = atoi(arg);
891 /* *************************************************************************
892 * hostname hostname-to-show-on-cgi-pages
893 * *************************************************************************/
895 freez(config->hostname);
896 config->hostname = strdup(arg);
897 if (NULL == config->hostname)
899 log_error(LOG_LEVEL_FATAL, "Out of memory saving hostname.");
903 /* *************************************************************************
904 * keep-alive-timeout timeout
905 * *************************************************************************/
906 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
907 case hash_keep_alive_timeout :
910 int timeout = atoi(arg);
913 config->feature_flags |= RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
914 config->keep_alive_timeout = (unsigned int)timeout;
918 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
924 /* *************************************************************************
925 * listen-address [ip][:port]
926 * *************************************************************************/
927 case hash_listen_address :
928 freez(config->haddr);
929 config->haddr = strdup(arg);
932 /* *************************************************************************
933 * logdir directory-name
934 * *************************************************************************/
936 freez(config->logdir);
937 config->logdir = make_path(NULL, arg);
940 /* *************************************************************************
941 * logfile log-file-name
942 * In logdir by default
943 * *************************************************************************/
947 logfile = make_path(config->logdir, arg);
950 log_error(LOG_LEVEL_FATAL, "Out of memory while creating logfile path");
955 /* *************************************************************************
956 * max-client-connections number
957 * *************************************************************************/
958 case hash_max_client_connections :
961 int max_client_connections = atoi(arg);
962 if (0 <= max_client_connections)
964 config->max_client_connections = max_client_connections;
969 /* *************************************************************************
970 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
971 * *************************************************************************/
973 case hash_permit_access:
974 strlcpy(tmp, arg, sizeof(tmp));
975 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
977 if ((vec_count != 1) && (vec_count != 2))
979 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
980 "permit-access directive in configuration file.");
981 string_append(&config->proxy_args,
982 "<br>\nWARNING: Wrong number of parameters for "
983 "permit-access directive in configuration file.<br><br>\n");
988 /* allocate a new node */
989 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
993 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
994 /* Never get here - LOG_LEVEL_FATAL causes program exit */
997 cur_acl->action = ACL_PERMIT;
999 if (acl_addr(vec[0], cur_acl->src) < 0)
1001 log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
1002 "for permit-access directive in configuration file: \"%s\"", vec[0]);
1003 string_append(&config->proxy_args,
1004 "<br>\nWARNING: Invalid source address, port or netmask for "
1005 "permit-access directive in configuration file: \"");
1006 string_append(&config->proxy_args,
1008 string_append(&config->proxy_args,
1015 if (acl_addr(vec[1], cur_acl->dst) < 0)
1017 log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
1018 "for permit-access directive in configuration file: \"%s\"", vec[1]);
1019 string_append(&config->proxy_args,
1020 "<br>\nWARNING: Invalid destination address, port or netmask for "
1021 "permit-access directive in configuration file: \"");
1022 string_append(&config->proxy_args,
1024 string_append(&config->proxy_args,
1033 cur_acl->wildcard_dst = 1;
1035 #endif /* def HAVE_RFC2553 */
1038 * Add it to the list. Note we reverse the list to get the
1039 * behaviour the user expects. With both the ACL and
1040 * actions file, the last match wins. However, the internal
1041 * implementations are different: The actions file is stored
1042 * in the same order as the file, and scanned completely.
1043 * With the ACL, we reverse the order as we load it, then
1044 * when we scan it we stop as soon as we get a match.
1046 cur_acl->next = config->acl;
1047 config->acl = cur_acl;
1050 #endif /* def FEATURE_ACL */
1052 /* *************************************************************************
1053 * proxy-info-url url
1054 * *************************************************************************/
1055 case hash_proxy_info_url :
1056 freez(config->proxy_info_url);
1057 config->proxy_info_url = strdup(arg);
1060 /* *************************************************************************
1062 * *************************************************************************/
1063 case hash_single_threaded :
1064 config->multi_threaded = 0;
1067 /* *************************************************************************
1068 * socket-timeout numer_of_seconds
1069 * *************************************************************************/
1070 case hash_socket_timeout :
1073 int socket_timeout = atoi(arg);
1074 if (0 < socket_timeout)
1076 config->socket_timeout = socket_timeout;
1080 log_error(LOG_LEVEL_FATAL,
1081 "Invalid socket-timeout: '%s'", arg);
1086 /* *************************************************************************
1087 * split-large-cgi-forms
1088 * *************************************************************************/
1089 case hash_split_large_cgi_forms :
1090 if ((*arg != '\0') && (0 != atoi(arg)))
1092 config->feature_flags |= RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1096 config->feature_flags &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1100 /* *************************************************************************
1101 * templdir directory-name
1102 * *************************************************************************/
1103 case hash_templdir :
1104 freez(config->templdir);
1105 config->templdir = make_path(NULL, arg);
1108 /* *************************************************************************
1110 * *************************************************************************/
1111 #ifdef FEATURE_TOGGLE
1113 global_toggle_state = atoi(arg);
1115 #endif /* def FEATURE_TOGGLE */
1117 /* *************************************************************************
1118 * trust-info-url url
1119 * *************************************************************************/
1120 #ifdef FEATURE_TRUST
1121 case hash_trust_info_url :
1122 enlist(config->trust_info, arg);
1124 #endif /* def FEATURE_TRUST */
1126 /* *************************************************************************
1127 * trustfile filename
1128 * (In confdir by default.)
1129 * *************************************************************************/
1130 #ifdef FEATURE_TRUST
1131 case hash_trustfile :
1132 freez(config->trustfile);
1133 config->trustfile = make_path(config->confdir, arg);
1135 #endif /* def FEATURE_TRUST */
1137 /* *************************************************************************
1139 * *************************************************************************/
1140 case hash_usermanual :
1142 * XXX: If this isn't the first config directive, the
1143 * show-status page links to the website documentation
1144 * for the directives that were already parsed. Lame.
1146 freez(config->usermanual);
1147 config->usermanual = strdup(arg);
1150 /* *************************************************************************
1151 * Win32 Console options:
1152 * *************************************************************************/
1154 /* *************************************************************************
1156 * *************************************************************************/
1158 case hash_hide_console :
1161 #endif /*def _WIN_CONSOLE*/
1164 /* *************************************************************************
1165 * Win32 GUI options:
1166 * *************************************************************************/
1168 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1169 /* *************************************************************************
1170 * activity-animation (0|1)
1171 * *************************************************************************/
1172 case hash_activity_animation :
1173 g_bShowActivityAnimation = atoi(arg);
1176 /* *************************************************************************
1177 * close-button-minimizes (0|1)
1178 * *************************************************************************/
1179 case hash_close_button_minimizes :
1180 g_bCloseHidesWindow = atoi(arg);
1183 /* *************************************************************************
1184 * log-buffer-size (0|1)
1185 * *************************************************************************/
1186 case hash_log_buffer_size :
1187 g_bLimitBufferSize = atoi(arg);
1190 /* *************************************************************************
1191 * log-font-name fontname
1192 * *************************************************************************/
1193 case hash_log_font_name :
1194 if (strlcpy(g_szFontFaceName, arg,
1195 sizeof(g_szFontFaceName)) >= sizeof(g_szFontFaceName))
1197 log_error(LOG_LEVEL_FATAL,
1198 "log-font-name argument '%s' is longer than %u characters.",
1199 arg, sizeof(g_szFontFaceName)-1);
1203 /* *************************************************************************
1205 * *************************************************************************/
1206 case hash_log_font_size :
1207 g_nFontSize = atoi(arg);
1210 /* *************************************************************************
1211 * log-highlight-messages (0|1)
1212 * *************************************************************************/
1213 case hash_log_highlight_messages :
1214 g_bHighlightMessages = atoi(arg);
1217 /* *************************************************************************
1219 * *************************************************************************/
1220 case hash_log_max_lines :
1221 g_nMaxBufferLines = atoi(arg);
1224 /* *************************************************************************
1225 * log-messages (0|1)
1226 * *************************************************************************/
1227 case hash_log_messages :
1228 g_bLogMessages = atoi(arg);
1231 /* *************************************************************************
1232 * show-on-task-bar (0|1)
1233 * *************************************************************************/
1234 case hash_show_on_task_bar :
1235 g_bShowOnTaskBar = atoi(arg);
1238 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1241 /* *************************************************************************
1242 * Warnings about unsupported features
1243 * *************************************************************************/
1245 case hash_deny_access:
1246 #endif /* ndef FEATURE_ACL */
1247 #ifndef FEATURE_CGI_EDIT_ACTIONS
1248 case hash_enable_edit_actions:
1249 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1250 #ifndef FEATURE_TOGGLE
1251 case hash_enable_remote_toggle:
1252 #endif /* ndef FEATURE_TOGGLE */
1254 case hash_permit_access:
1255 #endif /* ndef FEATURE_ACL */
1256 #ifndef FEATURE_TOGGLE
1258 #endif /* ndef FEATURE_TOGGLE */
1259 #ifndef FEATURE_TRUST
1260 case hash_trustfile :
1261 case hash_trust_info_url :
1262 #endif /* ndef FEATURE_TRUST */
1264 #ifndef _WIN_CONSOLE
1265 case hash_hide_console :
1266 #endif /* ndef _WIN_CONSOLE */
1268 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1269 case hash_activity_animation :
1270 case hash_close_button_minimizes :
1271 case hash_log_buffer_size :
1272 case hash_log_font_name :
1273 case hash_log_font_size :
1274 case hash_log_highlight_messages :
1275 case hash_log_max_lines :
1276 case hash_log_messages :
1277 case hash_show_on_task_bar :
1278 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1279 /* These warnings are annoying - so hide them. -- Jon */
1280 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1283 /* *************************************************************************/
1285 /* *************************************************************************/
1287 * I decided that I liked this better as a warning than an
1288 * error. To change back to an error, just change log level
1289 * to LOG_LEVEL_FATAL.
1291 log_error(LOG_LEVEL_ERROR, "Ignoring unrecognized directive '%s' (%luul) in line %lu "
1292 "in configuration file (%s).", buf, directive_hash, linenum, configfile);
1293 string_append(&config->proxy_args,
1294 " <strong class='warning'>Warning: Ignoring unrecognized directive:</strong>");
1297 /* *************************************************************************/
1298 } /* end switch( hash_string(cmd) ) */
1300 /* Save the argument for the show-status page. */
1301 savearg(cmd, arg, config);
1303 } /* end while ( read_config_line(...) ) */
1307 set_debug_level(config->debug);
1309 freez(config->logfile);
1313 if (NULL != logfile)
1315 config->logfile = logfile;
1316 init_error_log(Argv[0], config->logfile);
1324 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1325 if (config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
1327 if (config->multi_threaded)
1329 set_keep_alive_timeout(config->keep_alive_timeout);
1334 * While we could use keep-alive without multiple threads
1335 * if we didn't bother with enforcing the connection timeout,
1336 * that might make Tor users sad, even though they shouldn't
1337 * enable the single-threaded option anyway.
1339 * XXX: We could still use Proxy-Connection: keep-alive.
1341 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
1342 log_error(LOG_LEVEL_ERROR,
1343 "Config option single-threaded disables connection keep-alive.");
1348 if (NULL == config->proxy_args)
1350 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1353 if (config->actions_file[0])
1355 add_loader(load_action_files, config);
1358 if (config->re_filterfile[0])
1360 add_loader(load_re_filterfiles, config);
1363 #ifdef FEATURE_TRUST
1364 if (config->trustfile)
1366 add_loader(load_trustfile, config);
1368 #endif /* def FEATURE_TRUST */
1370 if ( NULL == config->haddr )
1372 config->haddr = strdup( HADDR_DEFAULT );
1375 if ( NULL != config->haddr )
1377 if ((*config->haddr == '[')
1378 && (NULL != (p = strchr(config->haddr, ']')))
1380 && (0 < (config->hport = atoi(p + 2))))
1383 memmove((void *)config->haddr, config->haddr + 1,
1384 (size_t)(p - config->haddr));
1386 else if (NULL != (p = strchr(config->haddr, ':'))
1387 && (0 < (config->hport = atoi(p + 1))))
1393 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1394 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1396 if (*config->haddr == '\0')
1399 * Only the port specified. We stored it in config->hport
1400 * and don't need its text representation anymore.
1402 freez(config->haddr);
1407 * Want to run all the loaders once now.
1409 * Need to set up a fake csp, so they can get to the config.
1411 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1412 fake_csp->config = config;
1414 if (run_loader(fake_csp))
1417 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1418 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1422 /* FIXME: this is a kludge for win32 */
1423 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1425 g_default_actions_file = config->actions_file[1]; /* FIXME Hope this is default.action */
1426 g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1427 g_re_filterfile = config->re_filterfile[0]; /* FIXME Hope this is default.filter */
1429 #ifdef FEATURE_TRUST
1430 g_trustfile = config->trustfile;
1431 #endif /* def FEATURE_TRUST */
1434 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1435 /* FIXME: end kludge */
1438 config->need_bind = 1;
1440 if (current_configfile)
1442 struct configuration_spec * oldcfg = (struct configuration_spec *)
1443 current_configfile->f;
1445 * Check if config->haddr,hport == oldcfg->haddr,hport
1447 * The following could be written more compactly as a single,
1448 * (unreadably long) if statement.
1450 config->need_bind = 0;
1451 if (config->hport != oldcfg->hport)
1453 config->need_bind = 1;
1455 else if (config->haddr == NULL)
1457 if (oldcfg->haddr != NULL)
1459 config->need_bind = 1;
1462 else if (oldcfg->haddr == NULL)
1464 config->need_bind = 1;
1466 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1468 config->need_bind = 1;
1471 current_configfile->unloader = unload_configfile;
1474 fs->next = files->next;
1477 current_configfile = fs;
1483 /*********************************************************************
1485 * Function : savearg
1487 * Description : Called from `load_config'. It saves each non-empty
1488 * and non-comment line from config into
1489 * config->proxy_args. This is used to create the
1490 * show-proxy-args page. On error, frees
1491 * config->proxy_args and sets it to NULL
1494 * 1 : command = config setting that was found
1495 * 2 : argument = the setting's argument (if any)
1496 * 3 : config = Configuration to save into.
1500 *********************************************************************/
1501 static void savearg(char *command, char *argument, struct configuration_spec * config)
1510 * Add config option name embedded in
1511 * link to its section in the user-manual
1513 buf = strdup("\n<a href=\"");
1514 if (!strncmpic(config->usermanual, "file://", 7) ||
1515 !strncmpic(config->usermanual, "http", 4))
1517 string_append(&buf, config->usermanual);
1521 string_append(&buf, "http://" CGI_SITE_2_HOST "/user-manual/");
1523 string_append(&buf, CONFIG_HELP_PREFIX);
1524 string_join (&buf, string_toupper(command));
1525 string_append(&buf, "\">");
1526 string_append(&buf, command);
1527 string_append(&buf, "</a> ");
1531 freez(config->proxy_args);
1535 if ( (NULL != argument) && ('\0' != *argument) )
1537 s = html_encode(argument);
1541 freez(config->proxy_args);
1545 if (strncmpic(argument, "http://", 7) == 0)
1547 string_append(&buf, "<a href=\"");
1548 string_append(&buf, s);
1549 string_append(&buf, "\">");
1550 string_join (&buf, s);
1551 string_append(&buf, "</a>");
1555 string_join (&buf, s);
1559 string_append(&buf, "<br>");
1560 string_join(&config->proxy_args, buf);