1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.41 2002/03/31 17:19:00 jongfoster 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 the SourceForge
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.
38 * Revision 1.41 2002/03/31 17:19:00 jongfoster
39 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
41 * Revision 1.40 2002/03/26 22:29:55 swa
42 * we have a new homepage!
44 * Revision 1.39 2002/03/24 13:25:43 swa
45 * name change related issues
47 * Revision 1.38 2002/03/24 13:05:48 jongfoster
48 * Renaming re_filterfile to filterfile
50 * Revision 1.37 2002/03/16 23:54:06 jongfoster
51 * Adding graceful termination feature, to help look for memory leaks.
52 * If you enable this (which, by design, has to be done by hand
53 * editing config.h) and then go to http://i.j.b/die, then the program
54 * will exit cleanly after the *next* request. It should free all the
55 * memory that was used.
57 * Revision 1.36 2002/03/13 00:27:05 jongfoster
60 * Revision 1.35 2002/03/07 03:52:44 oes
61 * Set logging to tty for --no-daemon mode
63 * Revision 1.34 2002/03/06 23:14:35 jongfoster
64 * Trivial cosmetic changes to make function comments easier to find.
66 * Revision 1.33 2002/03/05 04:52:42 oes
67 * Deleted non-errlog debugging code
69 * Revision 1.32 2002/03/04 18:24:53 oes
70 * Re-enabled output of unknown config directive hash
72 * Revision 1.31 2002/03/03 15:07:20 oes
73 * Re-enabled automatic config reloading
75 * Revision 1.30 2002/01/22 23:31:43 jongfoster
76 * Replacing strsav() with string_append()
78 * Revision 1.29 2002/01/17 21:02:30 jongfoster
79 * Moving all our URL and URL pattern parsing code to urlmatch.c.
81 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
83 * Revision 1.28 2001/12/30 14:07:32 steudten
84 * - Add signal handling (unix)
85 * - Add SIGHUP handler (unix)
86 * - Add creation of pidfile (unix)
87 * - Add action 'top' in rc file (RH)
88 * - Add entry 'SIGNALS' to manpage
89 * - Add exit message to logfile (unix)
91 * Revision 1.27 2001/11/07 00:02:13 steudten
92 * Add line number in error output for lineparsing for
93 * actionsfile and configfile.
94 * Special handling for CLF added.
96 * Revision 1.26 2001/11/05 21:41:43 steudten
97 * Add changes to be a real daemon just for unix os.
98 * (change cwd to /, detach from controlling tty, set
99 * process group and session leader to the own process.
101 * Add some fatal-error log message for failed malloc().
102 * Add '-d' if compiled with 'configure --with-debug' to
103 * enable debug output.
105 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
106 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
107 * threads to call select() simultaneously. So, it's time to do a real, live,
108 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
109 * (native). Both versions will work, but using __OS2__ offers multi-threading.
111 * Revision 1.24 2001/10/23 21:40:30 jongfoster
112 * Added support for enable-edit-actions and enable-remote-toggle config
115 * Revision 1.23 2001/10/07 15:36:00 oes
116 * Introduced new config option "buffer-limit"
118 * Revision 1.22 2001/09/22 16:36:59 jongfoster
119 * Removing unused parameter fs from read_config_line()
121 * Revision 1.21 2001/09/16 17:10:43 jongfoster
122 * Moving function savearg() here, since it was the only thing left in
125 * Revision 1.20 2001/07/30 22:08:36 jongfoster
126 * Tidying up #defines:
127 * - All feature #defines are now of the form FEATURE_xxx
128 * - Permanently turned off WIN_GUI_EDIT
129 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
131 * Revision 1.19 2001/07/15 17:45:16 jongfoster
132 * Removing some unused #includes
134 * Revision 1.18 2001/07/13 14:01:14 oes
135 * - Removed all #ifdef PCRS
136 * - Removed vim-settings
138 * Revision 1.17 2001/06/29 13:31:03 oes
139 * - Improved comments
140 * - Fixed (actionsfile) and sorted hashes
141 * - Introduced admin_address and proxy-info-url
142 * as config parameters
143 * - Renamed config->proxy_args_invocation (which didn't have
144 * the invocation but the options!) to config->proxy_args
145 * - Various adaptions
146 * - Removed logentry from cancelled commit
148 * Revision 1.16 2001/06/09 10:55:28 jongfoster
149 * Changing BUFSIZ ==> BUFFER_SIZE
151 * Revision 1.15 2001/06/07 23:13:40 jongfoster
152 * Merging ACL and forward files into config file.
153 * Cosmetic: Sorting config file options alphabetically.
154 * Cosmetic: Adding brief syntax comments to config file options.
156 * Revision 1.14 2001/06/07 14:46:25 joergs
157 * Missing make_path() added for re_filterfile.
159 * Revision 1.13 2001/06/05 22:33:54 jongfoster
161 * Fixed minor memory leak.
162 * Also now uses make_path to prepend the pathnames.
164 * Revision 1.12 2001/06/05 20:04:09 jongfoster
165 * Now uses _snprintf() in place of snprintf() under Win32.
167 * Revision 1.11 2001/06/04 18:31:58 swa
168 * files are now prefixed with either `confdir' or `logdir'.
169 * `make redhat-dist' replaces both entries confdir and logdir
172 * Revision 1.10 2001/06/03 19:11:54 oes
173 * introduced confdir option
175 * Revision 1.9 2001/06/01 20:06:24 jongfoster
176 * Removed support for "tinygif" option - moved to actions file.
178 * Revision 1.8 2001/05/31 21:27:13 jongfoster
179 * Removed many options from the config file and into the
180 * "actions" file: add_forwarded, suppress_vanilla_wafer,
181 * wafer, add_header, user_agent, referer, from
182 * Also globally replaced "permission" with "action".
184 * Revision 1.7 2001/05/29 09:50:24 jongfoster
185 * Unified blocklist/imagelist/permissionslist.
186 * File format is still under discussion, but the internal changes
189 * Also modified interceptor behaviour:
190 * - We now intercept all URLs beginning with one of the following
191 * prefixes (and *only* these prefixes):
193 * * http://ijbswa.sf.net/config/
194 * * http://ijbswa.sourceforge.net/config/
195 * - New interceptors "home page" - go to http://i.j.b/ to see it.
196 * - Internal changes so that intercepted and fast redirect pages
197 * are not replaced with an image.
198 * - Interceptors now have the option to send a binary page direct
199 * to the client. (i.e. ijb-send-banner uses this)
200 * - Implemented show-url-info interceptor. (Which is why I needed
201 * the above interceptors changes - a typical URL is
202 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
203 * The previous mechanism would not have intercepted that, and
204 * if it had been intercepted then it then it would have replaced
207 * Revision 1.6 2001/05/26 00:28:36 jongfoster
208 * Automatic reloading of config file.
209 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
210 * Most of the global variables have been moved to a new
211 * struct configuration_spec, accessed through csp->config->globalname
212 * Most of the globals remaining are used by the Win32 GUI.
214 * Revision 1.5 2001/05/25 22:34:30 jongfoster
217 * Revision 1.4 2001/05/22 18:46:04 oes
219 * - Enabled filtering banners by size rather than URL
220 * by adding patterns that replace all standard banner
221 * sizes with the "Junkbuster" gif to the re_filterfile
223 * - Enabled filtering WebBugs by providing a pattern
224 * which kills all 1x1 images
226 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
227 * which is selected by the (nonstandard and therefore
228 * capital) letter 'U' in the option string.
229 * It causes the quantifiers to be ungreedy by default.
230 * Appending a ? turns back to greedy (!).
232 * - Added a new interceptor ijb-send-banner, which
233 * sends back the "Junkbuster" gif. Without imagelist or
234 * MSIE detection support, or if tinygif = 1, or the
235 * URL isn't recognized as an imageurl, a lame HTML
236 * explanation is sent instead.
238 * - Added new feature, which permits blocking remote
239 * script redirects and firing back a local redirect
241 * The feature is conditionally compiled, i.e. it
242 * can be disabled with --disable-fast-redirects,
243 * plus it must be activated by a "fast-redirects"
244 * line in the config file, has its own log level
245 * and of course wants to be displayed by show-proxy-args
246 * Note: Boy, all the #ifdefs in 1001 locations and
247 * all the fumbling with configure.in and acconfig.h
248 * were *way* more work than the feature itself :-(
250 * - Because a generic redirect template was needed for
251 * this, tinygif = 3 now uses the same.
253 * - Moved GIFs, and other static HTTP response templates
258 * - Removed some >400 CRs again (Jon, you really worked
261 * Revision 1.3 2001/05/20 01:21:20 jongfoster
262 * Version 2.9.4 checkin.
263 * - Merged popupfile and cookiefile, and added control over PCRS
264 * filtering, in new "permissionsfile".
265 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
266 * file error you now get a message box (in the Win32 GUI) rather
267 * than the program exiting with no explanation.
268 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
270 * - Removed tabs from "config"
271 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
272 * - Bumped up version number.
274 * Revision 1.2 2001/05/17 23:01:01 oes
275 * - Cleaned CRLF's from the sources and related files
277 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
278 * Initial import of version 2.9.3 source tree
281 *********************************************************************/
287 #include <sys/types.h>
301 # include <windows.h>
304 # ifndef _WIN_CONSOLE
306 # endif /* ndef _WIN_CONSOLE */
308 /* VC++ has "_snprintf", not "snprintf" */
309 #define snprintf _snprintf
311 #else /* ifndef _WIN32 */
315 # include <sys/wait.h>
317 # include <sys/time.h>
318 # include <sys/stat.h>
328 #include "miscutil.h"
332 #include "urlmatch.h"
334 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
337 * Fix a problem with Solaris. There should be no effect on other
339 * Solaris's isspace() is a macro which uses it's argument directly
340 * as an array index. Therefore we need to make sure that high-bit
341 * characters generate +ve values, and ideally we also want to make
342 * the argument match the declared parameter type of "int".
344 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
345 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
347 #ifdef FEATURE_TOGGLE
348 /* by haroon - indicates if ijb is enabled */
349 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
350 #endif /* def FEATURE_TOGGLE */
352 /* The filename of the configfile */
353 const char *configfile = NULL;
356 * CGI functions will later need access to the invocation args,
357 * so we will make argc and argv global.
360 const char **Argv = NULL;
362 static struct file_list *current_configfile = NULL;
366 * This takes the "cryptic" hash of each keyword and aliases them to
367 * something a little more readable. This also makes changing the
368 * hash values easier if they should change or the hash algorthm changes.
369 * Use the included "hash" program to find out what the hash will be
370 * for any string supplied on the command line. (Or just put it in the
371 * config file and read the number from the error message in the log).
373 * Please keep this list sorted alphabetically (but with the Windows
374 * console and GUI specific options last).
377 #define hash_actions_file 1196306641ul /* "actionsfile" */
378 #define hash_admin_address 4112573064ul /* "admin-address" */
379 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
380 #define hash_confdir 1978389ul /* "confdir" */
381 #define hash_debug 78263ul /* "debug" */
382 #define hash_deny_access 1227333715ul /* "deny-access" */
383 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
384 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
385 #define hash_filterfile 250887266ul /* "filterfile" */
386 #define hash_forward 2029845ul /* "forward" */
387 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
388 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
389 #define hash_jarfile 2046641ul /* "jarfile" */
390 #define hash_listen_address 1255650842ul /* "listen-address" */
391 #define hash_logdir 422889ul /* "logdir" */
392 #define hash_logfile 2114766ul /* "logfile" */
393 #define hash_permit_access 3587953268ul /* "permit-access" */
394 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
395 #define hash_single_threaded 4250084780ul /* "single-threaded" */
396 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
397 #define hash_toggle 447966ul /* "toggle" */
398 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
399 #define hash_trustfile 56494766ul /* "trustfile" */
401 #define hash_activity_animation 1817904738ul /* "activity-animation" */
402 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
403 #define hash_hide_console 2048809870ul /* "hide-console" */
404 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
405 #define hash_log_font_name 2866730124ul /* "log-font-name" */
406 #define hash_log_font_size 2866731014ul /* "log-font-size" */
407 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
408 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
409 #define hash_log_messages 2291744899ul /* "log-messages" */
410 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
413 static void savearg(char *command, char *argument, struct configuration_spec * config);
415 /*********************************************************************
417 * Function : unload_configfile
419 * Description : Free the config structure and all components.
422 * 1 : data: struct configuration_spec to unload
426 *********************************************************************/
427 void unload_configfile (void * data)
429 struct configuration_spec * config = (struct configuration_spec *)data;
430 struct forward_spec *cur_fwd = config->forward;
432 struct access_control_list *cur_acl = config->acl;
434 while (cur_acl != NULL)
436 struct access_control_list * next_acl = cur_acl->next;
441 #endif /* def FEATURE_ACL */
443 while (cur_fwd != NULL)
445 struct forward_spec * next_fwd = cur_fwd->next;
446 free_url_spec(cur_fwd->url);
448 freez(cur_fwd->gateway_host);
449 freez(cur_fwd->forward_host);
453 config->forward = NULL;
455 #ifdef FEATURE_COOKIE_JAR
456 if ( NULL != config->jar )
458 fclose( config->jar );
461 #endif /* def FEATURE_COOKIE_JAR */
463 freez(config->confdir);
464 freez(config->logdir);
466 freez(config->haddr);
467 freez(config->logfile);
469 freez(config->actions_file);
470 freez(config->admin_address);
471 freez(config->proxy_info_url);
472 freez(config->proxy_args);
474 #ifdef FEATURE_COOKIE_JAR
475 freez(config->jarfile);
476 #endif /* def FEATURE_COOKIE_JAR */
478 freez(config->re_filterfile);
483 #ifdef FEATURE_GRACEFUL_TERMINATION
484 /*********************************************************************
486 * Function : unload_current_config_file
488 * Description : Unloads current config file - reset to state at
489 * beginning of program.
495 *********************************************************************/
496 void unload_current_config_file(void)
498 if (current_configfile)
500 current_configfile->unloader = unload_configfile;
501 current_configfile = NULL;
507 /*********************************************************************
509 * Function : load_config
511 * Description : Load the config file and all parameters.
514 * 1 : csp = Client state (the config member will be
515 * filled in by this function).
517 * Returns : 0 => Ok, everything else is an error.
519 *********************************************************************/
520 struct configuration_spec * load_config(void)
522 char buf[BUFFER_SIZE];
524 FILE *configfp = NULL;
525 struct configuration_spec * config = NULL;
526 struct client_state * fake_csp;
527 struct file_list *fs;
528 unsigned long linenum = 0;
530 if ( !check_file_changed(current_configfile, configfile, &fs))
532 /* No need to load */
533 return ((struct configuration_spec *)current_configfile->f);
537 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
541 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
543 #ifdef FEATURE_TOGGLE
545 #endif /* def FEATURE_TOGGLE */
547 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
553 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
554 /* Never get here - LOG_LEVEL_FATAL causes program exit */
558 * This is backwards from how it's usually done.
559 * Following the usual pattern, "fs" would be stored in a member
560 * variable in "csp", and then we'd access "config" from "fs->f",
561 * using a cast. However, "config" is used so often that a
562 * cast each time would be very ugly, and the extra indirection
563 * would waste CPU cycles. Therefore we store "config" in
564 * "csp->config", and "fs" in "csp->config->config_file_list".
566 config->config_file_list = fs;
572 config->multi_threaded = 1;
573 config->hport = HADDR_PORT;
574 config->buffer_limit = 4096 * 1024;
575 config->proxy_args = strdup("");
577 if ((configfp = fopen(configfile, "r")) == NULL)
579 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
581 /* Never get here - LOG_LEVEL_FATAL causes program exit */
584 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
586 char cmd[BUFFER_SIZE];
587 char arg[BUFFER_SIZE];
588 char tmp[BUFFER_SIZE];
590 struct access_control_list *cur_acl;
591 #endif /* def FEATURE_ACL */
592 struct forward_spec *cur_fwd;
598 /* Copy command (i.e. up to space or tab) into cmd */
601 while (*p && (*p != ' ') && (*p != '\t'))
607 /* Skip over the whitespace in buf */
608 while (*p && ((*p == ' ') || (*p == '\t')))
613 /* Copy the argument into arg */
616 /* Should never happen, but check this anyway */
622 /* Make sure the command field is lower case */
627 *p = ijb_tolower(*p);
631 /* Save the argument for show-proxy-args */
632 savearg(cmd, arg, config);
635 switch( hash_string( cmd ) )
637 /* *************************************************************************
638 * actionsfile actions-file-name
639 * In confdir by default
640 * *************************************************************************/
641 case hash_actions_file :
642 freez(config->actions_file);
643 config->actions_file = make_path(config->confdir, arg);
646 /* *************************************************************************
647 * admin-address email-address
648 * *************************************************************************/
649 case hash_admin_address :
650 freez(config->admin_address);
651 config->admin_address = strdup(arg);
654 /* *************************************************************************
656 * *************************************************************************/
657 case hash_buffer_limit :
658 config->buffer_limit = (size_t) 1024 * atoi(arg);
661 /* *************************************************************************
662 * confdir directory-name
663 * *************************************************************************/
665 freez(config->confdir);
666 config->confdir = make_path( NULL, arg);
669 /* *************************************************************************
671 * Specifies debug level, multiple values are ORed together.
672 * *************************************************************************/
674 config->debug |= atoi(arg);
677 /* *************************************************************************
678 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
679 * *************************************************************************/
681 case hash_deny_access:
682 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
684 if ((vec_count != 1) && (vec_count != 2))
686 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
687 "deny-access directive in configuration file.");
688 string_append(&config->proxy_args,
689 "<br>\nWARNING: Wrong number of parameters for "
690 "deny-access directive in configuration file.<br><br>\n");
694 /* allocate a new node */
695 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
699 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
700 /* Never get here - LOG_LEVEL_FATAL causes program exit */
703 cur_acl->action = ACL_DENY;
705 if (acl_addr(vec[0], cur_acl->src) < 0)
707 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
708 "directive in configuration file: \"%s\"", vec[0]);
709 string_append(&config->proxy_args,
710 "<br>\nWARNING: Invalid source IP for deny-access directive"
711 " in configuration file: \"");
712 string_append(&config->proxy_args,
714 string_append(&config->proxy_args,
721 if (acl_addr(vec[1], cur_acl->dst) < 0)
723 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
724 "directive in configuration file: \"%s\"", vec[0]);
725 string_append(&config->proxy_args,
726 "<br>\nWARNING: Invalid destination IP for deny-access directive"
727 " in configuration file: \"");
728 string_append(&config->proxy_args,
730 string_append(&config->proxy_args,
738 * Add it to the list. Note we reverse the list to get the
739 * behaviour the user expects. With both the ACL and
740 * actions file, the last match wins. However, the internal
741 * implementations are different: The actions file is stored
742 * in the same order as the file, and scanned completely.
743 * With the ACL, we reverse the order as we load it, then
744 * when we scan it we stop as soon as we get a match.
746 cur_acl->next = config->acl;
747 config->acl = cur_acl;
750 #endif /* def FEATURE_ACL */
752 /* *************************************************************************
753 * enable-edit-actions 0|1
754 * *************************************************************************/
755 #ifdef FEATURE_CGI_EDIT_ACTIONS
756 case hash_enable_edit_actions:
757 if ((*arg != '\0') && (0 != atoi(arg)))
759 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
763 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
766 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
768 /* *************************************************************************
769 * enable-remote-toggle 0|1
770 * *************************************************************************/
771 #ifdef FEATURE_CGI_EDIT_ACTIONS
772 case hash_enable_remote_toggle:
773 if ((*arg != '\0') && (0 != atoi(arg)))
775 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
779 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
782 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
784 /* *************************************************************************
785 * forward url-pattern (.|http-proxy-host[:port])
786 * *************************************************************************/
788 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
792 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
793 "directive in configuration file.");
794 string_append(&config->proxy_args,
795 "<br>\nWARNING: Wrong number of parameters for "
796 "forward directive in configuration file.");
800 /* allocate a new node */
801 cur_fwd = zalloc(sizeof(*cur_fwd));
804 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
805 /* Never get here - LOG_LEVEL_FATAL causes program exit */
809 cur_fwd->type = SOCKS_NONE;
811 /* Save the URL pattern */
812 if (create_url_spec(cur_fwd->url, vec[0]))
814 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
815 "directive in configuration file.");
816 string_append(&config->proxy_args,
817 "<br>\nWARNING: Bad URL specifier for "
818 "forward directive in configuration file.");
822 /* Parse the parent HTTP proxy host:port */
825 if (strcmp(p, ".") != 0)
827 cur_fwd->forward_host = strdup(p);
829 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
832 cur_fwd->forward_port = atoi(p);
835 if (cur_fwd->forward_port <= 0)
837 cur_fwd->forward_port = 8000;
842 cur_fwd->next = config->forward;
843 config->forward = cur_fwd;
847 /* *************************************************************************
848 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
849 * *************************************************************************/
850 case hash_forward_socks4:
851 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
855 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
856 "forward-socks4 directive in configuration file.");
857 string_append(&config->proxy_args,
858 "<br>\nWARNING: Wrong number of parameters for "
859 "forward-socks4 directive in configuration file.");
863 /* allocate a new node */
864 cur_fwd = zalloc(sizeof(*cur_fwd));
867 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
868 /* Never get here - LOG_LEVEL_FATAL causes program exit */
872 cur_fwd->type = SOCKS_4;
874 /* Save the URL pattern */
875 if (create_url_spec(cur_fwd->url, vec[0]))
877 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
878 "directive in configuration file.");
879 string_append(&config->proxy_args,
880 "<br>\nWARNING: Bad URL specifier for "
881 "forward-socks4 directive in configuration file.");
885 /* Parse the SOCKS proxy host[:port] */
888 if (strcmp(p, ".") != 0)
890 cur_fwd->gateway_host = strdup(p);
892 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
895 cur_fwd->gateway_port = atoi(p);
897 if (cur_fwd->gateway_port <= 0)
899 cur_fwd->gateway_port = 1080;
903 /* Parse the parent HTTP proxy host[:port] */
906 if (strcmp(p, ".") != 0)
908 cur_fwd->forward_host = strdup(p);
910 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
913 cur_fwd->forward_port = atoi(p);
916 if (cur_fwd->forward_port <= 0)
918 cur_fwd->forward_port = 8000;
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 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
936 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
937 "forward-socks4a directive in configuration file.");
938 string_append(&config->proxy_args,
939 "<br>\nWARNING: Wrong number of parameters for "
940 "forward-socks4a directive in configuration file.");
944 /* allocate a new node */
945 cur_fwd = zalloc(sizeof(*cur_fwd));
948 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
949 /* Never get here - LOG_LEVEL_FATAL causes program exit */
953 cur_fwd->type = SOCKS_4A;
955 /* Save the URL pattern */
956 if (create_url_spec(cur_fwd->url, vec[0]))
958 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
959 "directive in configuration file.");
960 string_append(&config->proxy_args,
961 "<br>\nWARNING: Bad URL specifier for "
962 "forward-socks4a directive in configuration file.");
966 /* Parse the SOCKS proxy host[:port] */
969 cur_fwd->gateway_host = strdup(p);
971 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
974 cur_fwd->gateway_port = atoi(p);
976 if (cur_fwd->gateway_port <= 0)
978 cur_fwd->gateway_port = 1080;
981 /* Parse the parent HTTP proxy host[:port] */
984 if (strcmp(p, ".") != 0)
986 cur_fwd->forward_host = strdup(p);
988 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
991 cur_fwd->forward_port = atoi(p);
994 if (cur_fwd->forward_port <= 0)
996 cur_fwd->forward_port = 8000;
1001 cur_fwd->next = config->forward;
1002 config->forward = cur_fwd;
1006 /* *************************************************************************
1007 * jarfile jar-file-name
1008 * In logdir by default
1009 * *************************************************************************/
1010 #ifdef FEATURE_COOKIE_JAR
1012 freez(config->jarfile);
1013 config->jarfile = make_path(config->logdir, arg);
1015 #endif /* def FEATURE_COOKIE_JAR */
1017 /* *************************************************************************
1018 * listen-address [ip][:port]
1019 * *************************************************************************/
1020 case hash_listen_address :
1021 freez(config->haddr);
1022 config->haddr = strdup(arg);
1025 /* *************************************************************************
1026 * logdir directory-name
1027 * *************************************************************************/
1029 freez(config->logdir);
1030 config->logdir = make_path(NULL, arg);
1033 /* *************************************************************************
1034 * logfile log-file-name
1035 * In logdir by default
1036 * *************************************************************************/
1038 freez(config->logfile);
1039 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1042 /* *************************************************************************
1043 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1044 * *************************************************************************/
1046 case hash_permit_access:
1047 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1049 if ((vec_count != 1) && (vec_count != 2))
1051 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1052 "permit-access directive in configuration file.");
1053 string_append(&config->proxy_args,
1054 "<br>\nWARNING: Wrong number of parameters for "
1055 "permit-access directive in configuration file.<br><br>\n");
1060 /* allocate a new node */
1061 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1063 if (cur_acl == NULL)
1065 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1066 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1069 cur_acl->action = ACL_PERMIT;
1071 if (acl_addr(vec[0], cur_acl->src) < 0)
1073 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1074 "directive in configuration file: \"%s\"", vec[0]);
1075 string_append(&config->proxy_args,
1076 "<br>\nWARNING: Invalid source IP for permit-access directive"
1077 " in configuration file: \"");
1078 string_append(&config->proxy_args,
1080 string_append(&config->proxy_args,
1087 if (acl_addr(vec[1], cur_acl->dst) < 0)
1089 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1090 "permit-access directive in configuration file: \"%s\"",
1092 string_append(&config->proxy_args,
1093 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1094 " in configuration file: \"");
1095 string_append(&config->proxy_args,
1097 string_append(&config->proxy_args,
1105 * Add it to the list. Note we reverse the list to get the
1106 * behaviour the user expects. With both the ACL and
1107 * actions file, the last match wins. However, the internal
1108 * implementations are different: The actions file is stored
1109 * in the same order as the file, and scanned completely.
1110 * With the ACL, we reverse the order as we load it, then
1111 * when we scan it we stop as soon as we get a match.
1113 cur_acl->next = config->acl;
1114 config->acl = cur_acl;
1117 #endif /* def FEATURE_ACL */
1119 /* *************************************************************************
1120 * proxy-info-url url
1121 * *************************************************************************/
1122 case hash_proxy_info_url :
1123 freez(config->proxy_info_url);
1124 config->proxy_info_url = strdup(arg);
1127 /* *************************************************************************
1128 * re_filterfile file-name
1129 * In confdir by default.
1130 * *************************************************************************/
1131 case hash_filterfile :
1132 freez(config->re_filterfile);
1133 config->re_filterfile = make_path(config->confdir, arg);
1136 /* *************************************************************************
1138 * *************************************************************************/
1139 case hash_single_threaded :
1140 config->multi_threaded = 0;
1143 /* *************************************************************************
1145 * *************************************************************************/
1146 #ifdef FEATURE_TOGGLE
1148 g_bToggleIJB = atoi(arg);
1150 #endif /* def FEATURE_TOGGLE */
1152 /* *************************************************************************
1153 * trust-info-url url
1154 * *************************************************************************/
1155 #ifdef FEATURE_TRUST
1156 case hash_trust_info_url :
1157 enlist(config->trust_info, arg);
1159 #endif /* def FEATURE_TRUST */
1161 /* *************************************************************************
1162 * trustfile filename
1163 * (In confdir by default.)
1164 * *************************************************************************/
1165 #ifdef FEATURE_TRUST
1166 case hash_trustfile :
1167 freez(config->trustfile);
1168 config->trustfile = make_path(config->confdir, arg);
1170 #endif /* def FEATURE_TRUST */
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 fontnane
1215 * *************************************************************************/
1216 case hash_log_font_name :
1217 strcpy( g_szFontFaceName, arg );
1220 /* *************************************************************************
1222 * *************************************************************************/
1223 case hash_log_font_size :
1224 g_nFontSize = atoi(arg);
1227 /* *************************************************************************
1228 * log-highlight-messages (0|1)
1229 * *************************************************************************/
1230 case hash_log_highlight_messages :
1231 g_bHighlightMessages = atoi(arg);
1234 /* *************************************************************************
1236 * *************************************************************************/
1237 case hash_log_max_lines :
1238 g_nMaxBufferLines = atoi(arg);
1241 /* *************************************************************************
1242 * log-messages (0|1)
1243 * *************************************************************************/
1244 case hash_log_messages :
1245 g_bLogMessages = atoi(arg);
1248 /* *************************************************************************
1249 * show-on-task-bar (0|1)
1250 * *************************************************************************/
1251 case hash_show_on_task_bar :
1252 g_bShowOnTaskBar = atoi(arg);
1255 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1258 /* *************************************************************************
1259 * Warnings about unsupported features
1260 * *************************************************************************/
1262 case hash_deny_access:
1263 #endif /* ndef FEATURE_ACL */
1264 #ifndef FEATURE_CGI_EDIT_ACTIONS
1265 case hash_enable_edit_actions:
1266 case hash_enable_remote_toggle:
1267 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1268 #ifndef FEATURE_COOKIE_JAR
1270 #endif /* ndef FEATURE_COOKIE_JAR */
1272 case hash_permit_access:
1273 #endif /* ndef FEATURE_ACL */
1274 #ifndef FEATURE_TOGGLE
1276 #endif /* ndef FEATURE_TOGGLE */
1277 #ifndef FEATURE_TRUST
1278 case hash_trustfile :
1279 case hash_trust_info_url :
1280 #endif /* ndef FEATURE_TRUST */
1282 #ifndef _WIN_CONSOLE
1283 case hash_hide_console :
1284 #endif /* ndef _WIN_CONSOLE */
1286 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1287 case hash_activity_animation :
1288 case hash_close_button_minimizes :
1289 case hash_log_buffer_size :
1290 case hash_log_font_name :
1291 case hash_log_font_size :
1292 case hash_log_highlight_messages :
1293 case hash_log_max_lines :
1294 case hash_log_messages :
1295 case hash_show_on_task_bar :
1296 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1297 /* These warnings are annoying - so hide them. -- Jon */
1298 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1301 /* *************************************************************************/
1303 /* *************************************************************************/
1305 * I decided that I liked this better as a warning than an
1306 * error. To change back to an error, just change log level
1307 * to LOG_LEVEL_FATAL.
1309 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1310 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1311 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1312 string_append(&config->proxy_args, buf);
1313 string_append(&config->proxy_args, "<br><br>\n");
1316 /* *************************************************************************/
1317 } /* end switch( hash_string(cmd) ) */
1318 } /* end while ( read_config_line(...) ) */
1322 if (NULL == config->proxy_args)
1324 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1327 init_error_log(Argv[0], config->logfile, config->debug);
1329 if (config->actions_file)
1331 add_loader(load_actions_file, config);
1334 if (config->re_filterfile)
1336 add_loader(load_re_filterfile, config);
1339 #ifdef FEATURE_TRUST
1340 if (config->trustfile)
1342 add_loader(load_trustfile, config);
1344 #endif /* def FEATURE_TRUST */
1346 #ifdef FEATURE_COOKIE_JAR
1347 if ( NULL != config->jarfile )
1349 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1351 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1352 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1354 setbuf(config->jar, NULL);
1356 #endif /* def FEATURE_COOKIE_JAR */
1358 if ( NULL == config->haddr )
1360 config->haddr = strdup( HADDR_DEFAULT );
1363 if ( NULL != config->haddr )
1365 if (NULL != (p = strchr(config->haddr, ':')))
1370 config->hport = atoi(p);
1374 if (config->hport <= 0)
1377 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1378 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1380 if (*config->haddr == '\0')
1382 config->haddr = NULL;
1387 * Want to run all the loaders once now.
1389 * Need to set up a fake csp, so they can get to the config.
1391 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1392 fake_csp->config = config;
1394 if (run_loader(fake_csp))
1397 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1398 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1402 /* FIXME: this is a kludge for win32 */
1403 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1405 g_actions_file = config->actions_file;
1406 g_re_filterfile = config->re_filterfile;
1408 #ifdef FEATURE_TRUST
1409 g_trustfile = config->trustfile;
1410 #endif /* def FEATURE_TRUST */
1413 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1414 /* FIXME: end kludge */
1417 config->need_bind = 1;
1419 if (current_configfile)
1421 struct configuration_spec * oldcfg = (struct configuration_spec *)
1422 current_configfile->f;
1424 * Check if config->haddr,hport == oldcfg->haddr,hport
1426 * The following could be written more compactly as a single,
1427 * (unreadably long) if statement.
1429 config->need_bind = 0;
1430 if (config->hport != oldcfg->hport)
1432 config->need_bind = 1;
1434 else if (config->haddr == NULL)
1436 if (oldcfg->haddr != NULL)
1438 config->need_bind = 1;
1441 else if (oldcfg->haddr == NULL)
1443 config->need_bind = 1;
1445 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1447 config->need_bind = 1;
1450 current_configfile->unloader = unload_configfile;
1453 fs->next = files->next;
1456 current_configfile = fs;
1462 /*********************************************************************
1464 * Function : savearg
1466 * Description : Called from `load_config'. It saves each non-empty
1467 * and non-comment line from config into
1468 * config->proxy_args. This is used to create the
1469 * show-proxy-args page. On error, frees
1470 * config->proxy_args and sets it to NULL
1473 * 1 : command = config setting that was found
1474 * 2 : argument = the setting's argument (if any)
1478 *********************************************************************/
1479 static void savearg(char *command, char *argument, struct configuration_spec * config)
1490 s = html_encode(command);
1494 freez(config->proxy_args);
1497 string_append(&buf, "<a href=\"");
1498 string_append(&buf, html_encode(REDIRECT_URL));
1499 string_append(&buf, "option#");
1500 string_append(&buf, s);
1501 string_append(&buf, "\">");
1502 string_join (&buf, s);
1503 string_append(&buf, "</a> ");
1505 if ( (NULL != argument) && ('\0' != *argument) )
1507 s = html_encode(argument);
1511 freez(config->proxy_args);
1515 if (strncmpic(argument, "http://", 7) == 0)
1517 string_append(&buf, "<a href=\"");
1518 string_append(&buf, s);
1519 string_append(&buf, "\">");
1520 string_join (&buf, s);
1521 string_append(&buf, "</a>");
1525 string_join (&buf, s);
1529 string_append(&buf, "<br>\n");
1531 string_join(&config->proxy_args, buf);