1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.107 2009/11/27 13:46:47 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/loadcfg.c,v $
6 * Purpose : Loads settings from the configuration file into
7 * global variables. This file contains both the
8 * routine to load the configuration and the global
9 * variables it writes to.
11 * Copyright : Written by and Copyright (C) 2001-2009 the
12 * Privoxy team. http://www.privoxy.org/
14 * Based on the Internet Junkbuster originally written
15 * by and Copyright (C) 1997 Anonymous Coders and
16 * Junkbusters Corporation. http://www.junkbusters.com
18 * This program is free software; you can redistribute it
19 * and/or modify it under the terms of the GNU General
20 * Public License as published by the Free Software
21 * Foundation; either version 2 of the License, or (at
22 * your option) any later version.
24 * This program is distributed in the hope that it will
25 * be useful, but WITHOUT ANY WARRANTY; without even the
26 * implied warranty of MERCHANTABILITY or FITNESS FOR A
27 * PARTICULAR PURPOSE. See the GNU General Public
28 * License for more details.
30 * The GNU General Public License should be included with
31 * this file. If not, you can view it at
32 * http://www.gnu.org/copyleft/gpl.html
33 * or write to the Free Software Foundation, Inc., 59
34 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 *********************************************************************/
42 #include <sys/types.h>
61 # endif /* ndef _WIN_CONSOLE */
63 #else /* ifndef _WIN32 */
67 # include <sys/wait.h>
69 # include <sys/time.h>
70 # include <sys/stat.h>
88 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
91 * Fix a problem with Solaris. There should be no effect on other
93 * Solaris's isspace() is a macro which uses it's argument directly
94 * as an array index. Therefore we need to make sure that high-bit
95 * characters generate +ve values, and ideally we also want to make
96 * the argument match the declared parameter type of "int".
98 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
99 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
101 #ifdef FEATURE_TOGGLE
102 /* Privoxy is enabled by default. */
103 int global_toggle_state = 1;
104 #endif /* def FEATURE_TOGGLE */
106 /* The filename of the configfile */
107 const char *configfile = NULL;
110 * CGI functions will later need access to the invocation args,
111 * so we will make argc and argv global.
114 char * const * Argv = NULL;
116 static struct file_list *current_configfile = NULL;
120 * This takes the "cryptic" hash of each keyword and aliases them to
121 * something a little more readable. This also makes changing the
122 * hash values easier if they should change or the hash algorthm changes.
123 * Use the included "hash" program to find out what the hash will be
124 * for any string supplied on the command line. (Or just put it in the
125 * config file and read the number from the error message in the log).
127 * Please keep this list sorted alphabetically (but with the Windows
128 * console and GUI specific options last).
131 #define hash_actions_file 1196306641ul /* "actionsfile" */
132 #define hash_accept_intercepted_requests 1513024973ul /* "accept-intercepted-requests" */
133 #define hash_admin_address 4112573064ul /* "admin-address" */
134 #define hash_allow_cgi_request_crunching 258915987ul /* "allow-cgi-request-crunching" */
135 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
136 #define hash_confdir 1978389ul /* "confdir" */
137 #define hash_connection_sharing 1348841265ul /* "connection-sharing" */
138 #define hash_debug 78263ul /* "debug" */
139 #define hash_default_server_timeout 2530089913ul /* "default-server-timeout" */
140 #define hash_deny_access 1227333715ul /* "deny-access" */
141 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
142 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
143 #define hash_enable_remote_http_toggle 110543988ul /* "enable-remote-http-toggle" */
144 #define hash_enforce_blocks 1862427469ul /* "enforce-blocks" */
145 #define hash_filterfile 250887266ul /* "filterfile" */
146 #define hash_forward 2029845ul /* "forward" */
147 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
148 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
149 #define hash_forward_socks5 3963965522ul /* "forward-socks5" */
150 #define hash_forwarded_connect_retries 101465292ul /* "forwarded-connect-retries" */
151 #define hash_hostname 10308071ul /* "hostname" */
152 #define hash_keep_alive_timeout 3878599515ul /* "keep-alive-timeout" */
153 #define hash_listen_address 1255650842ul /* "listen-address" */
154 #define hash_logdir 422889ul /* "logdir" */
155 #define hash_logfile 2114766ul /* "logfile" */
156 #define hash_max_client_connections 3595884446ul /* "max-client-connections" */
157 #define hash_permit_access 3587953268ul /* "permit-access" */
158 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
159 #define hash_single_threaded 4250084780ul /* "single-threaded" */
160 #define hash_socket_timeout 1809001761ul /* "socket-timeout" */
161 #define hash_split_large_cgi_forms 671658948ul /* "split-large-cgi-forms" */
162 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
163 #define hash_templdir 11067889ul /* "templdir" */
164 #define hash_toggle 447966ul /* "toggle" */
165 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
166 #define hash_trustfile 56494766ul /* "trustfile" */
167 #define hash_usermanual 1416668518ul /* "user-manual" */
168 #define hash_activity_animation 1817904738ul /* "activity-animation" */
169 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
170 #define hash_hide_console 2048809870ul /* "hide-console" */
171 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
172 #define hash_log_font_name 2866730124ul /* "log-font-name" */
173 #define hash_log_font_size 2866731014ul /* "log-font-size" */
174 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
175 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
176 #define hash_log_messages 2291744899ul /* "log-messages" */
177 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
180 static void savearg(char *command, char *argument, struct configuration_spec * config);
182 /*********************************************************************
184 * Function : unload_configfile
186 * Description : Free the config structure and all components.
189 * 1 : data: struct configuration_spec to unload
193 *********************************************************************/
194 static void unload_configfile (void * data)
196 struct configuration_spec * config = (struct configuration_spec *)data;
197 struct forward_spec *cur_fwd = config->forward;
201 struct access_control_list *cur_acl = config->acl;
203 while (cur_acl != NULL)
205 struct access_control_list * next_acl = cur_acl->next;
210 #endif /* def FEATURE_ACL */
212 while (cur_fwd != NULL)
214 struct forward_spec * next_fwd = cur_fwd->next;
215 free_url_spec(cur_fwd->url);
217 freez(cur_fwd->gateway_host);
218 freez(cur_fwd->forward_host);
222 config->forward = NULL;
224 freez(config->confdir);
225 freez(config->logdir);
226 freez(config->templdir);
227 freez(config->hostname);
229 freez(config->haddr);
230 freez(config->logfile);
232 for (i = 0; i < MAX_AF_FILES; i++)
234 freez(config->actions_file_short[i]);
235 freez(config->actions_file[i]);
236 freez(config->re_filterfile_short[i]);
237 freez(config->re_filterfile[i]);
240 freez(config->admin_address);
241 freez(config->proxy_info_url);
242 freez(config->proxy_args);
243 freez(config->usermanual);
246 freez(config->trustfile);
247 list_remove_all(config->trust_info);
248 #endif /* def FEATURE_TRUST */
250 for (i = 0; i < MAX_AF_FILES; i++)
252 freez(config->re_filterfile[i]);
259 #ifdef FEATURE_GRACEFUL_TERMINATION
260 /*********************************************************************
262 * Function : unload_current_config_file
264 * Description : Unloads current config file - reset to state at
265 * beginning of program.
271 *********************************************************************/
272 void unload_current_config_file(void)
274 if (current_configfile)
276 current_configfile->unloader = unload_configfile;
277 current_configfile = NULL;
283 /*********************************************************************
285 * Function : load_config
287 * Description : Load the config file and all parameters.
289 * XXX: more than thousand lines long
290 * and thus in serious need of refactoring.
294 * Returns : The configuration_spec, or NULL on error.
296 *********************************************************************/
297 struct configuration_spec * load_config(void)
299 char buf[BUFFER_SIZE];
301 FILE *configfp = NULL;
302 struct configuration_spec * config = NULL;
303 struct client_state * fake_csp;
304 struct file_list *fs;
305 unsigned long linenum = 0;
307 char *logfile = NULL;
309 if (!check_file_changed(current_configfile, configfile, &fs))
311 /* No need to load */
312 return ((struct configuration_spec *)current_configfile->f);
316 log_error(LOG_LEVEL_FATAL,
317 "can't check configuration file '%s': %E", configfile);
321 if (NULL != current_configfile)
323 log_error(LOG_LEVEL_INFO, "Reloading configuration file '%s'", configfile);
326 #ifdef FEATURE_TOGGLE
327 global_toggle_state = 1;
328 #endif /* def FEATURE_TOGGLE */
330 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
336 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
341 * This is backwards from how it's usually done.
342 * Following the usual pattern, "fs" would be stored in a member
343 * variable in "csp", and then we'd access "config" from "fs->f",
344 * using a cast. However, "config" is used so often that a
345 * cast each time would be very ugly, and the extra indirection
346 * would waste CPU cycles. Therefore we store "config" in
347 * "csp->config", and "fs" in "csp->config->config_file_list".
349 config->config_file_list = fs;
354 config->multi_threaded = 1;
355 config->buffer_limit = 4096 * 1024;
356 config->usermanual = strdup(USER_MANUAL_URL);
357 config->proxy_args = strdup("");
358 config->forwarded_connect_retries = 0;
359 config->max_client_connections = 0;
360 config->socket_timeout = 300; /* XXX: Should be a macro. */
361 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
362 config->default_server_timeout = 0;
363 config->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
364 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
365 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
367 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
368 config->feature_flags &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
369 config->feature_flags &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
371 configfp = fopen(configfile, "r");
372 if (NULL == configfp)
374 log_error(LOG_LEVEL_FATAL,
375 "can't open configuration file '%s': %E", configfile);
376 /* Never get here - LOG_LEVEL_FATAL causes program exit */
379 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
381 char cmd[BUFFER_SIZE];
382 char arg[BUFFER_SIZE];
383 char tmp[BUFFER_SIZE];
385 struct access_control_list *cur_acl;
386 #endif /* def FEATURE_ACL */
387 struct forward_spec *cur_fwd;
390 unsigned long directive_hash;
392 strlcpy(tmp, buf, sizeof(tmp));
394 /* Copy command (i.e. up to space or tab) into cmd */
397 while (*p && (*p != ' ') && (*p != '\t'))
403 /* Skip over the whitespace in buf */
404 while (*p && ((*p == ' ') || (*p == '\t')))
409 /* Copy the argument into arg */
410 strlcpy(arg, p, sizeof(arg));
412 /* Should never happen, but check this anyway */
418 /* Make sure the command field is lower case */
419 for (p = cmd; *p; p++)
423 *p = (char)ijb_tolower(*p);
427 directive_hash = hash_string(cmd);
428 switch (directive_hash)
430 /* *************************************************************************
431 * actionsfile actions-file-name
432 * In confdir by default
433 * *************************************************************************/
434 case hash_actions_file :
436 while ((i < MAX_AF_FILES) && (NULL != config->actions_file[i]))
441 if (i >= MAX_AF_FILES)
443 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
444 "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
447 config->actions_file_short[i] = strdup(arg);
448 config->actions_file[i] = make_path(config->confdir, arg);
451 /* *************************************************************************
452 * accept-intercepted-requests
453 * *************************************************************************/
454 case hash_accept_intercepted_requests:
455 if ((*arg != '\0') && (0 != atoi(arg)))
457 config->feature_flags |= RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
461 config->feature_flags &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
465 /* *************************************************************************
466 * admin-address email-address
467 * *************************************************************************/
468 case hash_admin_address :
469 freez(config->admin_address);
470 config->admin_address = strdup(arg);
473 /* *************************************************************************
474 * allow-cgi-request-crunching
475 * *************************************************************************/
476 case hash_allow_cgi_request_crunching:
477 if ((*arg != '\0') && (0 != atoi(arg)))
479 config->feature_flags |= RUNTIME_FEATURE_CGI_CRUNCHING;
483 config->feature_flags &= ~RUNTIME_FEATURE_CGI_CRUNCHING;
487 /* *************************************************************************
489 * *************************************************************************/
490 case hash_buffer_limit :
491 config->buffer_limit = (size_t)(1024 * atoi(arg));
494 /* *************************************************************************
495 * confdir directory-name
496 * *************************************************************************/
498 freez(config->confdir);
499 config->confdir = make_path( NULL, arg);
502 /* *************************************************************************
503 * connection-sharing (0|1)
504 * *************************************************************************/
505 #ifdef FEATURE_CONNECTION_SHARING
506 case hash_connection_sharing :
507 if ((*arg != '\0') && (0 != atoi(arg)))
509 config->feature_flags |= RUNTIME_FEATURE_CONNECTION_SHARING;
513 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
518 /* *************************************************************************
520 * Specifies debug level, multiple values are ORed together.
521 * *************************************************************************/
523 config->debug |= atoi(arg);
526 /* *************************************************************************
527 * default-server-timeout timeout
528 * *************************************************************************/
529 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
530 case hash_default_server_timeout :
533 int timeout = atoi(arg);
536 config->default_server_timeout = (unsigned int)timeout;
540 log_error(LOG_LEVEL_FATAL,
541 "Invalid default-server-timeout value: %s", arg);
547 /* *************************************************************************
548 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
549 * *************************************************************************/
551 case hash_deny_access:
552 strlcpy(tmp, arg, sizeof(tmp));
553 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
555 if ((vec_count != 1) && (vec_count != 2))
557 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
558 "deny-access directive in configuration file.");
559 string_append(&config->proxy_args,
560 "<br>\nWARNING: Wrong number of parameters for "
561 "deny-access directive in configuration file.<br><br>\n");
565 /* allocate a new node */
566 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
570 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
571 /* Never get here - LOG_LEVEL_FATAL causes program exit */
574 cur_acl->action = ACL_DENY;
576 if (acl_addr(vec[0], cur_acl->src) < 0)
578 log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
579 "for deny-access directive in configuration file: \"%s\"", vec[0]);
580 string_append(&config->proxy_args,
581 "<br>\nWARNING: Invalid source address, port or netmask "
582 "for deny-access directive in configuration file: \"");
583 string_append(&config->proxy_args,
585 string_append(&config->proxy_args,
592 if (acl_addr(vec[1], cur_acl->dst) < 0)
594 log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
595 "for deny-access directive in configuration file: \"%s\"", vec[1]);
596 string_append(&config->proxy_args,
597 "<br>\nWARNING: Invalid destination address, port or netmask "
598 "for deny-access directive in configuration file: \"");
599 string_append(&config->proxy_args,
601 string_append(&config->proxy_args,
610 cur_acl->wildcard_dst = 1;
612 #endif /* def HAVE_RFC2553 */
615 * Add it to the list. Note we reverse the list to get the
616 * behaviour the user expects. With both the ACL and
617 * actions file, the last match wins. However, the internal
618 * implementations are different: The actions file is stored
619 * in the same order as the file, and scanned completely.
620 * With the ACL, we reverse the order as we load it, then
621 * when we scan it we stop as soon as we get a match.
623 cur_acl->next = config->acl;
624 config->acl = cur_acl;
627 #endif /* def FEATURE_ACL */
629 /* *************************************************************************
630 * enable-edit-actions 0|1
631 * *************************************************************************/
632 #ifdef FEATURE_CGI_EDIT_ACTIONS
633 case hash_enable_edit_actions:
634 if ((*arg != '\0') && (0 != atoi(arg)))
636 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
640 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
643 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
645 /* *************************************************************************
646 * enable-remote-toggle 0|1
647 * *************************************************************************/
648 #ifdef FEATURE_TOGGLE
649 case hash_enable_remote_toggle:
650 if ((*arg != '\0') && (0 != atoi(arg)))
652 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
656 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
659 #endif /* def FEATURE_TOGGLE */
661 /* *************************************************************************
662 * enable-remote-http-toggle 0|1
663 * *************************************************************************/
664 case hash_enable_remote_http_toggle:
665 if ((*arg != '\0') && (0 != atoi(arg)))
667 config->feature_flags |= RUNTIME_FEATURE_HTTP_TOGGLE;
671 config->feature_flags &= ~RUNTIME_FEATURE_HTTP_TOGGLE;
675 /* *************************************************************************
677 * *************************************************************************/
678 case hash_enforce_blocks:
679 #ifdef FEATURE_FORCE_LOAD
680 if ((*arg != '\0') && (0 != atoi(arg)))
682 config->feature_flags |= RUNTIME_FEATURE_ENFORCE_BLOCKS;
686 config->feature_flags &= ~RUNTIME_FEATURE_ENFORCE_BLOCKS;
689 log_error(LOG_LEVEL_ERROR, "Ignoring directive 'enforce-blocks'. "
690 "FEATURE_FORCE_LOAD is disabled, blocks will always be enforced.");
691 #endif /* def FEATURE_FORCE_LOAD */
694 /* *************************************************************************
695 * filterfile file-name
696 * In confdir by default.
697 * *************************************************************************/
698 case hash_filterfile :
700 while ((i < MAX_AF_FILES) && (NULL != config->re_filterfile[i]))
705 if (i >= MAX_AF_FILES)
707 log_error(LOG_LEVEL_FATAL, "Too many 'filterfile' directives in config file - limit is %d.\n"
708 "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
711 config->re_filterfile_short[i] = strdup(arg);
712 config->re_filterfile[i] = make_path(config->confdir, arg);
716 /* *************************************************************************
717 * forward url-pattern (.|http-proxy-host[:port])
718 * *************************************************************************/
720 strlcpy(tmp, arg, sizeof(tmp));
721 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
725 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
726 "directive in configuration file.");
727 string_append(&config->proxy_args,
728 "<br>\nWARNING: Wrong number of parameters for "
729 "forward directive in configuration file.");
733 /* allocate a new node */
734 cur_fwd = zalloc(sizeof(*cur_fwd));
737 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
738 /* Never get here - LOG_LEVEL_FATAL causes program exit */
742 cur_fwd->type = SOCKS_NONE;
744 /* Save the URL pattern */
745 if (create_url_spec(cur_fwd->url, vec[0]))
747 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
748 "directive in configuration file.");
749 string_append(&config->proxy_args,
750 "<br>\nWARNING: Bad URL specifier for "
751 "forward directive in configuration file.");
755 /* Parse the parent HTTP proxy host:port */
758 if (strcmp(p, ".") != 0)
760 cur_fwd->forward_port = 8000;
761 parse_forwarder_address(p, &cur_fwd->forward_host,
762 &cur_fwd->forward_port);
766 cur_fwd->next = config->forward;
767 config->forward = cur_fwd;
771 /* *************************************************************************
772 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
773 * *************************************************************************/
774 case hash_forward_socks4:
775 strlcpy(tmp, arg, sizeof(tmp));
776 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
780 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
781 "forward-socks4 directive in configuration file.");
782 string_append(&config->proxy_args,
783 "<br>\nWARNING: Wrong number of parameters for "
784 "forward-socks4 directive in configuration file.");
788 /* allocate a new node */
789 cur_fwd = zalloc(sizeof(*cur_fwd));
792 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
793 /* Never get here - LOG_LEVEL_FATAL causes program exit */
797 cur_fwd->type = SOCKS_4;
799 /* Save the URL pattern */
800 if (create_url_spec(cur_fwd->url, vec[0]))
802 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
803 "directive in configuration file.");
804 string_append(&config->proxy_args,
805 "<br>\nWARNING: Bad URL specifier for "
806 "forward-socks4 directive in configuration file.");
810 /* Parse the SOCKS proxy host[:port] */
813 /* XXX: This check looks like a bug. */
814 if (strcmp(p, ".") != 0)
816 cur_fwd->gateway_port = 1080;
817 parse_forwarder_address(p, &cur_fwd->gateway_host,
818 &cur_fwd->gateway_port);
821 /* Parse the parent HTTP proxy host[:port] */
824 if (strcmp(p, ".") != 0)
826 cur_fwd->forward_port = 8000;
827 parse_forwarder_address(p, &cur_fwd->forward_host,
828 &cur_fwd->forward_port);
832 cur_fwd->next = config->forward;
833 config->forward = cur_fwd;
837 /* *************************************************************************
838 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
839 * *************************************************************************/
840 case hash_forward_socks4a:
841 case hash_forward_socks5:
842 strlcpy(tmp, arg, sizeof(tmp));
843 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
847 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
848 "forward-socks4a directive in configuration file.");
849 string_append(&config->proxy_args,
850 "<br>\nWARNING: Wrong number of parameters for "
851 "forward-socks4a directive in configuration file.");
855 /* allocate a new node */
856 cur_fwd = zalloc(sizeof(*cur_fwd));
859 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
860 /* Never get here - LOG_LEVEL_FATAL causes program exit */
864 if (directive_hash == hash_forward_socks4a)
866 cur_fwd->type = SOCKS_4A;
870 cur_fwd->type = SOCKS_5;
873 /* Save the URL pattern */
874 if (create_url_spec(cur_fwd->url, vec[0]))
876 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
877 "directive in configuration file.");
878 string_append(&config->proxy_args,
879 "<br>\nWARNING: Bad URL specifier for "
880 "forward-socks4a directive in configuration file.");
884 /* Parse the SOCKS proxy host[:port] */
887 cur_fwd->gateway_port = 1080;
888 parse_forwarder_address(p, &cur_fwd->gateway_host,
889 &cur_fwd->gateway_port);
891 /* Parse the parent HTTP proxy host[:port] */
894 if (strcmp(p, ".") != 0)
896 cur_fwd->forward_port = 8000;
897 parse_forwarder_address(p, &cur_fwd->forward_host,
898 &cur_fwd->forward_port);
902 cur_fwd->next = config->forward;
903 config->forward = cur_fwd;
907 /* *************************************************************************
908 * forwarded-connect-retries n
909 * *************************************************************************/
910 case hash_forwarded_connect_retries :
911 config->forwarded_connect_retries = atoi(arg);
914 /* *************************************************************************
915 * hostname hostname-to-show-on-cgi-pages
916 * *************************************************************************/
918 freez(config->hostname);
919 config->hostname = strdup(arg);
920 if (NULL == config->hostname)
922 log_error(LOG_LEVEL_FATAL, "Out of memory saving hostname.");
926 /* *************************************************************************
927 * keep-alive-timeout timeout
928 * *************************************************************************/
929 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
930 case hash_keep_alive_timeout :
933 int timeout = atoi(arg);
936 config->feature_flags |= RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
937 config->keep_alive_timeout = (unsigned int)timeout;
941 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
947 /* *************************************************************************
948 * listen-address [ip][:port]
949 * *************************************************************************/
950 case hash_listen_address :
951 freez(config->haddr);
952 config->haddr = strdup(arg);
955 /* *************************************************************************
956 * logdir directory-name
957 * *************************************************************************/
959 freez(config->logdir);
960 config->logdir = make_path(NULL, arg);
963 /* *************************************************************************
964 * logfile log-file-name
965 * In logdir by default
966 * *************************************************************************/
970 logfile = make_path(config->logdir, arg);
973 log_error(LOG_LEVEL_FATAL, "Out of memory while creating logfile path");
978 /* *************************************************************************
979 * max-client-connections number
980 * *************************************************************************/
981 case hash_max_client_connections :
984 int max_client_connections = atoi(arg);
985 if (0 <= max_client_connections)
987 config->max_client_connections = max_client_connections;
992 /* *************************************************************************
993 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
994 * *************************************************************************/
996 case hash_permit_access:
997 strlcpy(tmp, arg, sizeof(tmp));
998 vec_count = ssplit(tmp, " \t", vec, SZ(vec), 1, 1);
1000 if ((vec_count != 1) && (vec_count != 2))
1002 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1003 "permit-access directive in configuration file.");
1004 string_append(&config->proxy_args,
1005 "<br>\nWARNING: Wrong number of parameters for "
1006 "permit-access directive in configuration file.<br><br>\n");
1011 /* allocate a new node */
1012 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1014 if (cur_acl == NULL)
1016 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1017 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1020 cur_acl->action = ACL_PERMIT;
1022 if (acl_addr(vec[0], cur_acl->src) < 0)
1024 log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
1025 "for permit-access directive in configuration file: \"%s\"", vec[0]);
1026 string_append(&config->proxy_args,
1027 "<br>\nWARNING: Invalid source address, port or netmask for "
1028 "permit-access directive in configuration file: \"");
1029 string_append(&config->proxy_args,
1031 string_append(&config->proxy_args,
1038 if (acl_addr(vec[1], cur_acl->dst) < 0)
1040 log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
1041 "for permit-access directive in configuration file: \"%s\"", vec[1]);
1042 string_append(&config->proxy_args,
1043 "<br>\nWARNING: Invalid destination address, port or netmask for "
1044 "permit-access directive in configuration file: \"");
1045 string_append(&config->proxy_args,
1047 string_append(&config->proxy_args,
1056 cur_acl->wildcard_dst = 1;
1058 #endif /* def HAVE_RFC2553 */
1061 * Add it to the list. Note we reverse the list to get the
1062 * behaviour the user expects. With both the ACL and
1063 * actions file, the last match wins. However, the internal
1064 * implementations are different: The actions file is stored
1065 * in the same order as the file, and scanned completely.
1066 * With the ACL, we reverse the order as we load it, then
1067 * when we scan it we stop as soon as we get a match.
1069 cur_acl->next = config->acl;
1070 config->acl = cur_acl;
1073 #endif /* def FEATURE_ACL */
1075 /* *************************************************************************
1076 * proxy-info-url url
1077 * *************************************************************************/
1078 case hash_proxy_info_url :
1079 freez(config->proxy_info_url);
1080 config->proxy_info_url = strdup(arg);
1083 /* *************************************************************************
1085 * *************************************************************************/
1086 case hash_single_threaded :
1087 config->multi_threaded = 0;
1090 /* *************************************************************************
1091 * socket-timeout numer_of_seconds
1092 * *************************************************************************/
1093 case hash_socket_timeout :
1096 int socket_timeout = atoi(arg);
1097 if (0 < socket_timeout)
1099 config->socket_timeout = socket_timeout;
1103 log_error(LOG_LEVEL_FATAL,
1104 "Invalid socket-timeout: '%s'", arg);
1109 /* *************************************************************************
1110 * split-large-cgi-forms
1111 * *************************************************************************/
1112 case hash_split_large_cgi_forms :
1113 if ((*arg != '\0') && (0 != atoi(arg)))
1115 config->feature_flags |= RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1119 config->feature_flags &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1123 /* *************************************************************************
1124 * templdir directory-name
1125 * *************************************************************************/
1126 case hash_templdir :
1127 freez(config->templdir);
1128 config->templdir = make_path(NULL, arg);
1131 /* *************************************************************************
1133 * *************************************************************************/
1134 #ifdef FEATURE_TOGGLE
1136 global_toggle_state = atoi(arg);
1138 #endif /* def FEATURE_TOGGLE */
1140 /* *************************************************************************
1141 * trust-info-url url
1142 * *************************************************************************/
1143 #ifdef FEATURE_TRUST
1144 case hash_trust_info_url :
1145 enlist(config->trust_info, arg);
1147 #endif /* def FEATURE_TRUST */
1149 /* *************************************************************************
1150 * trustfile filename
1151 * (In confdir by default.)
1152 * *************************************************************************/
1153 #ifdef FEATURE_TRUST
1154 case hash_trustfile :
1155 freez(config->trustfile);
1156 config->trustfile = make_path(config->confdir, arg);
1158 #endif /* def FEATURE_TRUST */
1160 /* *************************************************************************
1162 * *************************************************************************/
1163 case hash_usermanual :
1165 * XXX: If this isn't the first config directive, the
1166 * show-status page links to the website documentation
1167 * for the directives that were already parsed. Lame.
1169 freez(config->usermanual);
1170 config->usermanual = strdup(arg);
1173 /* *************************************************************************
1174 * Win32 Console options:
1175 * *************************************************************************/
1177 /* *************************************************************************
1179 * *************************************************************************/
1181 case hash_hide_console :
1184 #endif /*def _WIN_CONSOLE*/
1187 /* *************************************************************************
1188 * Win32 GUI options:
1189 * *************************************************************************/
1191 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1192 /* *************************************************************************
1193 * activity-animation (0|1)
1194 * *************************************************************************/
1195 case hash_activity_animation :
1196 g_bShowActivityAnimation = atoi(arg);
1199 /* *************************************************************************
1200 * close-button-minimizes (0|1)
1201 * *************************************************************************/
1202 case hash_close_button_minimizes :
1203 g_bCloseHidesWindow = atoi(arg);
1206 /* *************************************************************************
1207 * log-buffer-size (0|1)
1208 * *************************************************************************/
1209 case hash_log_buffer_size :
1210 g_bLimitBufferSize = atoi(arg);
1213 /* *************************************************************************
1214 * log-font-name fontname
1215 * *************************************************************************/
1216 case hash_log_font_name :
1217 if (strlcpy(g_szFontFaceName, arg,
1218 sizeof(g_szFontFaceName)) >= sizeof(g_szFontFaceName))
1220 log_error(LOG_LEVEL_FATAL,
1221 "log-font-name argument '%s' is longer than %u characters.",
1222 arg, sizeof(g_szFontFaceName)-1);
1226 /* *************************************************************************
1228 * *************************************************************************/
1229 case hash_log_font_size :
1230 g_nFontSize = atoi(arg);
1233 /* *************************************************************************
1234 * log-highlight-messages (0|1)
1235 * *************************************************************************/
1236 case hash_log_highlight_messages :
1237 g_bHighlightMessages = atoi(arg);
1240 /* *************************************************************************
1242 * *************************************************************************/
1243 case hash_log_max_lines :
1244 g_nMaxBufferLines = atoi(arg);
1247 /* *************************************************************************
1248 * log-messages (0|1)
1249 * *************************************************************************/
1250 case hash_log_messages :
1251 g_bLogMessages = atoi(arg);
1254 /* *************************************************************************
1255 * show-on-task-bar (0|1)
1256 * *************************************************************************/
1257 case hash_show_on_task_bar :
1258 g_bShowOnTaskBar = atoi(arg);
1261 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1264 /* *************************************************************************
1265 * Warnings about unsupported features
1266 * *************************************************************************/
1268 case hash_deny_access:
1269 #endif /* ndef FEATURE_ACL */
1270 #ifndef FEATURE_CGI_EDIT_ACTIONS
1271 case hash_enable_edit_actions:
1272 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1273 #ifndef FEATURE_TOGGLE
1274 case hash_enable_remote_toggle:
1275 #endif /* ndef FEATURE_TOGGLE */
1277 case hash_permit_access:
1278 #endif /* ndef FEATURE_ACL */
1279 #ifndef FEATURE_TOGGLE
1281 #endif /* ndef FEATURE_TOGGLE */
1282 #ifndef FEATURE_TRUST
1283 case hash_trustfile :
1284 case hash_trust_info_url :
1285 #endif /* ndef FEATURE_TRUST */
1287 #ifndef _WIN_CONSOLE
1288 case hash_hide_console :
1289 #endif /* ndef _WIN_CONSOLE */
1291 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1292 case hash_activity_animation :
1293 case hash_close_button_minimizes :
1294 case hash_log_buffer_size :
1295 case hash_log_font_name :
1296 case hash_log_font_size :
1297 case hash_log_highlight_messages :
1298 case hash_log_max_lines :
1299 case hash_log_messages :
1300 case hash_show_on_task_bar :
1301 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1302 /* These warnings are annoying - so hide them. -- Jon */
1303 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1306 /* *************************************************************************/
1308 /* *************************************************************************/
1310 * I decided that I liked this better as a warning than an
1311 * error. To change back to an error, just change log level
1312 * to LOG_LEVEL_FATAL.
1314 log_error(LOG_LEVEL_ERROR, "Ignoring unrecognized directive '%s' (%luul) in line %lu "
1315 "in configuration file (%s).", buf, directive_hash, linenum, configfile);
1316 string_append(&config->proxy_args,
1317 " <strong class='warning'>Warning: Ignoring unrecognized directive:</strong>");
1320 /* *************************************************************************/
1321 } /* end switch( hash_string(cmd) ) */
1323 /* Save the argument for the show-status page. */
1324 savearg(cmd, arg, config);
1326 } /* end while ( read_config_line(...) ) */
1330 set_debug_level(config->debug);
1332 freez(config->logfile);
1336 if (NULL != logfile)
1338 config->logfile = logfile;
1339 init_error_log(Argv[0], config->logfile);
1347 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1348 if (config->default_server_timeout > config->keep_alive_timeout)
1350 log_error(LOG_LEVEL_ERROR,
1351 "Reducing the default-server-timeout from %d to the keep-alive-timeout %d.",
1352 config->default_server_timeout, config->keep_alive_timeout);
1353 config->default_server_timeout = config->keep_alive_timeout;
1355 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1357 #ifdef FEATURE_CONNECTION_SHARING
1358 if (config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
1360 if (config->multi_threaded)
1362 set_keep_alive_timeout(config->keep_alive_timeout);
1367 * While we could use keep-alive without multiple threads
1368 * if we didn't bother with enforcing the connection timeout,
1369 * that might make Tor users sad, even though they shouldn't
1370 * enable the single-threaded option anyway.
1372 * XXX: We could still use Proxy-Connection: keep-alive.
1374 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
1375 log_error(LOG_LEVEL_ERROR,
1376 "Config option single-threaded disables connection keep-alive.");
1379 else if ((config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING))
1381 log_error(LOG_LEVEL_ERROR, "Config option connection-sharing "
1382 "has no effect if keep-alive-timeout isn't set.");
1383 config->feature_flags &= ~RUNTIME_FEATURE_CONNECTION_SHARING;
1385 #endif /* def FEATURE_CONNECTION_SHARING */
1387 if (NULL == config->proxy_args)
1389 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1392 if (config->actions_file[0])
1394 add_loader(load_action_files, config);
1397 if (config->re_filterfile[0])
1399 add_loader(load_re_filterfiles, config);
1402 #ifdef FEATURE_TRUST
1403 if (config->trustfile)
1405 add_loader(load_trustfile, config);
1407 #endif /* def FEATURE_TRUST */
1409 if ( NULL == config->haddr )
1411 config->haddr = strdup( HADDR_DEFAULT );
1414 if ( NULL != config->haddr )
1416 if ((*config->haddr == '[')
1417 && (NULL != (p = strchr(config->haddr, ']')))
1419 && (0 < (config->hport = atoi(p + 2))))
1422 memmove((void *)config->haddr, config->haddr + 1,
1423 (size_t)(p - config->haddr));
1425 else if (NULL != (p = strchr(config->haddr, ':'))
1426 && (0 < (config->hport = atoi(p + 1))))
1432 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1433 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1435 if (*config->haddr == '\0')
1438 * Only the port specified. We stored it in config->hport
1439 * and don't need its text representation anymore.
1441 freez(config->haddr);
1446 * Want to run all the loaders once now.
1448 * Need to set up a fake csp, so they can get to the config.
1450 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1451 fake_csp->config = config;
1453 if (run_loader(fake_csp))
1456 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1457 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1461 /* FIXME: this is a kludge for win32 */
1462 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1464 g_default_actions_file = config->actions_file[1]; /* FIXME Hope this is default.action */
1465 g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1466 g_re_filterfile = config->re_filterfile[0]; /* FIXME Hope this is default.filter */
1468 #ifdef FEATURE_TRUST
1469 g_trustfile = config->trustfile;
1470 #endif /* def FEATURE_TRUST */
1473 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1474 /* FIXME: end kludge */
1477 config->need_bind = 1;
1479 if (current_configfile)
1481 struct configuration_spec * oldcfg = (struct configuration_spec *)
1482 current_configfile->f;
1484 * Check if config->haddr,hport == oldcfg->haddr,hport
1486 * The following could be written more compactly as a single,
1487 * (unreadably long) if statement.
1489 config->need_bind = 0;
1490 if (config->hport != oldcfg->hport)
1492 config->need_bind = 1;
1494 else if (config->haddr == NULL)
1496 if (oldcfg->haddr != NULL)
1498 config->need_bind = 1;
1501 else if (oldcfg->haddr == NULL)
1503 config->need_bind = 1;
1505 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1507 config->need_bind = 1;
1510 current_configfile->unloader = unload_configfile;
1513 fs->next = files->next;
1516 current_configfile = fs;
1522 /*********************************************************************
1524 * Function : savearg
1526 * Description : Called from `load_config'. It saves each non-empty
1527 * and non-comment line from config into
1528 * config->proxy_args. This is used to create the
1529 * show-proxy-args page. On error, frees
1530 * config->proxy_args and sets it to NULL
1533 * 1 : command = config setting that was found
1534 * 2 : argument = the setting's argument (if any)
1535 * 3 : config = Configuration to save into.
1539 *********************************************************************/
1540 static void savearg(char *command, char *argument, struct configuration_spec * config)
1549 * Add config option name embedded in
1550 * link to its section in the user-manual
1552 buf = strdup("\n<a href=\"");
1553 if (!strncmpic(config->usermanual, "file://", 7) ||
1554 !strncmpic(config->usermanual, "http", 4))
1556 string_append(&buf, config->usermanual);
1560 string_append(&buf, "http://" CGI_SITE_2_HOST "/user-manual/");
1562 string_append(&buf, CONFIG_HELP_PREFIX);
1563 string_join (&buf, string_toupper(command));
1564 string_append(&buf, "\">");
1565 string_append(&buf, command);
1566 string_append(&buf, "</a> ");
1570 freez(config->proxy_args);
1574 if ( (NULL != argument) && ('\0' != *argument) )
1576 s = html_encode(argument);
1580 freez(config->proxy_args);
1584 if (strncmpic(argument, "http://", 7) == 0)
1586 string_append(&buf, "<a href=\"");
1587 string_append(&buf, s);
1588 string_append(&buf, "\">");
1589 string_join (&buf, s);
1590 string_append(&buf, "</a>");
1594 string_join (&buf, s);
1598 string_append(&buf, "<br>");
1599 string_join(&config->proxy_args, buf);