1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.46 2002/04/26 12:55:14 oes 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.46 2002/04/26 12:55:14 oes
39 * - New option "user-manual", defaults to our site
40 * via project.h #define
41 * - savearg now embeds option names in help links
43 * Revision 1.45 2002/04/24 02:11:54 oes
44 * Jon's multiple AF patch: Allow up to MAX_ACTION_FILES actionsfile options
46 * Revision 1.44 2002/04/08 20:37:13 swa
49 * Revision 1.43 2002/04/08 20:36:50 swa
52 * Revision 1.42 2002/04/05 15:50:15 oes
53 * fix for invalid HTML proxy_args
55 * Revision 1.41 2002/03/31 17:19:00 jongfoster
56 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
58 * Revision 1.40 2002/03/26 22:29:55 swa
59 * we have a new homepage!
61 * Revision 1.39 2002/03/24 13:25:43 swa
62 * name change related issues
64 * Revision 1.38 2002/03/24 13:05:48 jongfoster
65 * Renaming re_filterfile to filterfile
67 * Revision 1.37 2002/03/16 23:54:06 jongfoster
68 * Adding graceful termination feature, to help look for memory leaks.
69 * If you enable this (which, by design, has to be done by hand
70 * editing config.h) and then go to http://i.j.b/die, then the program
71 * will exit cleanly after the *next* request. It should free all the
72 * memory that was used.
74 * Revision 1.36 2002/03/13 00:27:05 jongfoster
77 * Revision 1.35 2002/03/07 03:52:44 oes
78 * Set logging to tty for --no-daemon mode
80 * Revision 1.34 2002/03/06 23:14:35 jongfoster
81 * Trivial cosmetic changes to make function comments easier to find.
83 * Revision 1.33 2002/03/05 04:52:42 oes
84 * Deleted non-errlog debugging code
86 * Revision 1.32 2002/03/04 18:24:53 oes
87 * Re-enabled output of unknown config directive hash
89 * Revision 1.31 2002/03/03 15:07:20 oes
90 * Re-enabled automatic config reloading
92 * Revision 1.30 2002/01/22 23:31:43 jongfoster
93 * Replacing strsav() with string_append()
95 * Revision 1.29 2002/01/17 21:02:30 jongfoster
96 * Moving all our URL and URL pattern parsing code to urlmatch.c.
98 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
100 * Revision 1.28 2001/12/30 14:07:32 steudten
101 * - Add signal handling (unix)
102 * - Add SIGHUP handler (unix)
103 * - Add creation of pidfile (unix)
104 * - Add action 'top' in rc file (RH)
105 * - Add entry 'SIGNALS' to manpage
106 * - Add exit message to logfile (unix)
108 * Revision 1.27 2001/11/07 00:02:13 steudten
109 * Add line number in error output for lineparsing for
110 * actionsfile and configfile.
111 * Special handling for CLF added.
113 * Revision 1.26 2001/11/05 21:41:43 steudten
114 * Add changes to be a real daemon just for unix os.
115 * (change cwd to /, detach from controlling tty, set
116 * process group and session leader to the own process.
118 * Add some fatal-error log message for failed malloc().
119 * Add '-d' if compiled with 'configure --with-debug' to
120 * enable debug output.
122 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
123 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
124 * threads to call select() simultaneously. So, it's time to do a real, live,
125 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
126 * (native). Both versions will work, but using __OS2__ offers multi-threading.
128 * Revision 1.24 2001/10/23 21:40:30 jongfoster
129 * Added support for enable-edit-actions and enable-remote-toggle config
132 * Revision 1.23 2001/10/07 15:36:00 oes
133 * Introduced new config option "buffer-limit"
135 * Revision 1.22 2001/09/22 16:36:59 jongfoster
136 * Removing unused parameter fs from read_config_line()
138 * Revision 1.21 2001/09/16 17:10:43 jongfoster
139 * Moving function savearg() here, since it was the only thing left in
142 * Revision 1.20 2001/07/30 22:08:36 jongfoster
143 * Tidying up #defines:
144 * - All feature #defines are now of the form FEATURE_xxx
145 * - Permanently turned off WIN_GUI_EDIT
146 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
148 * Revision 1.19 2001/07/15 17:45:16 jongfoster
149 * Removing some unused #includes
151 * Revision 1.18 2001/07/13 14:01:14 oes
152 * - Removed all #ifdef PCRS
153 * - Removed vim-settings
155 * Revision 1.17 2001/06/29 13:31:03 oes
156 * - Improved comments
157 * - Fixed (actionsfile) and sorted hashes
158 * - Introduced admin_address and proxy-info-url
159 * as config parameters
160 * - Renamed config->proxy_args_invocation (which didn't have
161 * the invocation but the options!) to config->proxy_args
162 * - Various adaptions
163 * - Removed logentry from cancelled commit
165 * Revision 1.16 2001/06/09 10:55:28 jongfoster
166 * Changing BUFSIZ ==> BUFFER_SIZE
168 * Revision 1.15 2001/06/07 23:13:40 jongfoster
169 * Merging ACL and forward files into config file.
170 * Cosmetic: Sorting config file options alphabetically.
171 * Cosmetic: Adding brief syntax comments to config file options.
173 * Revision 1.14 2001/06/07 14:46:25 joergs
174 * Missing make_path() added for re_filterfile.
176 * Revision 1.13 2001/06/05 22:33:54 jongfoster
178 * Fixed minor memory leak.
179 * Also now uses make_path to prepend the pathnames.
181 * Revision 1.12 2001/06/05 20:04:09 jongfoster
182 * Now uses _snprintf() in place of snprintf() under Win32.
184 * Revision 1.11 2001/06/04 18:31:58 swa
185 * files are now prefixed with either `confdir' or `logdir'.
186 * `make redhat-dist' replaces both entries confdir and logdir
189 * Revision 1.10 2001/06/03 19:11:54 oes
190 * introduced confdir option
192 * Revision 1.9 2001/06/01 20:06:24 jongfoster
193 * Removed support for "tinygif" option - moved to actions file.
195 * Revision 1.8 2001/05/31 21:27:13 jongfoster
196 * Removed many options from the config file and into the
197 * "actions" file: add_forwarded, suppress_vanilla_wafer,
198 * wafer, add_header, user_agent, referer, from
199 * Also globally replaced "permission" with "action".
201 * Revision 1.7 2001/05/29 09:50:24 jongfoster
202 * Unified blocklist/imagelist/permissionslist.
203 * File format is still under discussion, but the internal changes
206 * Also modified interceptor behaviour:
207 * - We now intercept all URLs beginning with one of the following
208 * prefixes (and *only* these prefixes):
210 * * http://ijbswa.sf.net/config/
211 * * http://ijbswa.sourceforge.net/config/
212 * - New interceptors "home page" - go to http://i.j.b/ to see it.
213 * - Internal changes so that intercepted and fast redirect pages
214 * are not replaced with an image.
215 * - Interceptors now have the option to send a binary page direct
216 * to the client. (i.e. ijb-send-banner uses this)
217 * - Implemented show-url-info interceptor. (Which is why I needed
218 * the above interceptors changes - a typical URL is
219 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
220 * The previous mechanism would not have intercepted that, and
221 * if it had been intercepted then it then it would have replaced
224 * Revision 1.6 2001/05/26 00:28:36 jongfoster
225 * Automatic reloading of config file.
226 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
227 * Most of the global variables have been moved to a new
228 * struct configuration_spec, accessed through csp->config->globalname
229 * Most of the globals remaining are used by the Win32 GUI.
231 * Revision 1.5 2001/05/25 22:34:30 jongfoster
234 * Revision 1.4 2001/05/22 18:46:04 oes
236 * - Enabled filtering banners by size rather than URL
237 * by adding patterns that replace all standard banner
238 * sizes with the "Junkbuster" gif to the re_filterfile
240 * - Enabled filtering WebBugs by providing a pattern
241 * which kills all 1x1 images
243 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
244 * which is selected by the (nonstandard and therefore
245 * capital) letter 'U' in the option string.
246 * It causes the quantifiers to be ungreedy by default.
247 * Appending a ? turns back to greedy (!).
249 * - Added a new interceptor ijb-send-banner, which
250 * sends back the "Junkbuster" gif. Without imagelist or
251 * MSIE detection support, or if tinygif = 1, or the
252 * URL isn't recognized as an imageurl, a lame HTML
253 * explanation is sent instead.
255 * - Added new feature, which permits blocking remote
256 * script redirects and firing back a local redirect
258 * The feature is conditionally compiled, i.e. it
259 * can be disabled with --disable-fast-redirects,
260 * plus it must be activated by a "fast-redirects"
261 * line in the config file, has its own log level
262 * and of course wants to be displayed by show-proxy-args
263 * Note: Boy, all the #ifdefs in 1001 locations and
264 * all the fumbling with configure.in and acconfig.h
265 * were *way* more work than the feature itself :-(
267 * - Because a generic redirect template was needed for
268 * this, tinygif = 3 now uses the same.
270 * - Moved GIFs, and other static HTTP response templates
275 * - Removed some >400 CRs again (Jon, you really worked
278 * Revision 1.3 2001/05/20 01:21:20 jongfoster
279 * Version 2.9.4 checkin.
280 * - Merged popupfile and cookiefile, and added control over PCRS
281 * filtering, in new "permissionsfile".
282 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
283 * file error you now get a message box (in the Win32 GUI) rather
284 * than the program exiting with no explanation.
285 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
287 * - Removed tabs from "config"
288 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
289 * - Bumped up version number.
291 * Revision 1.2 2001/05/17 23:01:01 oes
292 * - Cleaned CRLF's from the sources and related files
294 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
295 * Initial import of version 2.9.3 source tree
298 *********************************************************************/
304 #include <sys/types.h>
318 # include <windows.h>
321 # ifndef _WIN_CONSOLE
323 # endif /* ndef _WIN_CONSOLE */
325 /* VC++ has "_snprintf", not "snprintf" */
326 #define snprintf _snprintf
328 #else /* ifndef _WIN32 */
332 # include <sys/wait.h>
334 # include <sys/time.h>
335 # include <sys/stat.h>
345 #include "miscutil.h"
349 #include "urlmatch.h"
352 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
355 * Fix a problem with Solaris. There should be no effect on other
357 * Solaris's isspace() is a macro which uses it's argument directly
358 * as an array index. Therefore we need to make sure that high-bit
359 * characters generate +ve values, and ideally we also want to make
360 * the argument match the declared parameter type of "int".
362 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
363 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
365 #ifdef FEATURE_TOGGLE
366 /* by haroon - indicates if ijb is enabled */
367 int g_bToggleIJB = 1; /* Privoxy is enabled by default. */
368 #endif /* def FEATURE_TOGGLE */
370 /* The filename of the configfile */
371 const char *configfile = NULL;
374 * CGI functions will later need access to the invocation args,
375 * so we will make argc and argv global.
378 const char **Argv = NULL;
380 static struct file_list *current_configfile = NULL;
384 * This takes the "cryptic" hash of each keyword and aliases them to
385 * something a little more readable. This also makes changing the
386 * hash values easier if they should change or the hash algorthm changes.
387 * Use the included "hash" program to find out what the hash will be
388 * for any string supplied on the command line. (Or just put it in the
389 * config file and read the number from the error message in the log).
391 * Please keep this list sorted alphabetically (but with the Windows
392 * console and GUI specific options last).
395 #define hash_actions_file 1196306641ul /* "actionsfile" */
396 #define hash_admin_address 4112573064ul /* "admin-address" */
397 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
398 #define hash_confdir 1978389ul /* "confdir" */
399 #define hash_debug 78263ul /* "debug" */
400 #define hash_deny_access 1227333715ul /* "deny-access" */
401 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
402 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
403 #define hash_filterfile 250887266ul /* "filterfile" */
404 #define hash_forward 2029845ul /* "forward" */
405 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
406 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
407 #define hash_jarfile 2046641ul /* "jarfile" */
408 #define hash_listen_address 1255650842ul /* "listen-address" */
409 #define hash_logdir 422889ul /* "logdir" */
410 #define hash_logfile 2114766ul /* "logfile" */
411 #define hash_permit_access 3587953268ul /* "permit-access" */
412 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
413 #define hash_single_threaded 4250084780ul /* "single-threaded" */
414 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
415 #define hash_toggle 447966ul /* "toggle" */
416 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
417 #define hash_trustfile 56494766ul /* "trustfile" */
418 #define hash_usermanual 1416668518ul /* "user-manual" */
419 #define hash_activity_animation 1817904738ul /* "activity-animation" */
420 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
421 #define hash_hide_console 2048809870ul /* "hide-console" */
422 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
423 #define hash_log_font_name 2866730124ul /* "log-font-name" */
424 #define hash_log_font_size 2866731014ul /* "log-font-size" */
425 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
426 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
427 #define hash_log_messages 2291744899ul /* "log-messages" */
428 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
431 static void savearg(char *command, char *argument, struct configuration_spec * config);
433 /*********************************************************************
435 * Function : unload_configfile
437 * Description : Free the config structure and all components.
440 * 1 : data: struct configuration_spec to unload
444 *********************************************************************/
445 void unload_configfile (void * data)
447 struct configuration_spec * config = (struct configuration_spec *)data;
448 struct forward_spec *cur_fwd = config->forward;
450 struct access_control_list *cur_acl = config->acl;
453 while (cur_acl != NULL)
455 struct access_control_list * next_acl = cur_acl->next;
460 #endif /* def FEATURE_ACL */
462 while (cur_fwd != NULL)
464 struct forward_spec * next_fwd = cur_fwd->next;
465 free_url_spec(cur_fwd->url);
467 freez(cur_fwd->gateway_host);
468 freez(cur_fwd->forward_host);
472 config->forward = NULL;
474 #ifdef FEATURE_COOKIE_JAR
475 if ( NULL != config->jar )
477 fclose( config->jar );
480 #endif /* def FEATURE_COOKIE_JAR */
482 freez(config->confdir);
483 freez(config->logdir);
485 freez(config->haddr);
486 freez(config->logfile);
488 for (i = 0; i < MAX_ACTION_FILES; i++)
490 freez(config->actions_file_short[i]);
491 freez(config->actions_file[i]);
494 freez(config->admin_address);
495 freez(config->proxy_info_url);
496 freez(config->proxy_args);
497 freez(config->usermanual);
499 #ifdef FEATURE_COOKIE_JAR
500 freez(config->jarfile);
501 #endif /* def FEATURE_COOKIE_JAR */
503 freez(config->re_filterfile);
508 #ifdef FEATURE_GRACEFUL_TERMINATION
509 /*********************************************************************
511 * Function : unload_current_config_file
513 * Description : Unloads current config file - reset to state at
514 * beginning of program.
520 *********************************************************************/
521 void unload_current_config_file(void)
523 if (current_configfile)
525 current_configfile->unloader = unload_configfile;
526 current_configfile = NULL;
532 /*********************************************************************
534 * Function : load_config
536 * Description : Load the config file and all parameters.
540 * Returns : The configuration_spec, or NULL on error.
542 *********************************************************************/
543 struct configuration_spec * load_config(void)
545 char buf[BUFFER_SIZE];
547 FILE *configfp = NULL;
548 struct configuration_spec * config = NULL;
549 struct client_state * fake_csp;
550 struct file_list *fs;
551 unsigned long linenum = 0;
554 if ( !check_file_changed(current_configfile, configfile, &fs))
556 /* No need to load */
557 return ((struct configuration_spec *)current_configfile->f);
561 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
565 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
567 #ifdef FEATURE_TOGGLE
569 #endif /* def FEATURE_TOGGLE */
571 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
577 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
578 /* Never get here - LOG_LEVEL_FATAL causes program exit */
582 * This is backwards from how it's usually done.
583 * Following the usual pattern, "fs" would be stored in a member
584 * variable in "csp", and then we'd access "config" from "fs->f",
585 * using a cast. However, "config" is used so often that a
586 * cast each time would be very ugly, and the extra indirection
587 * would waste CPU cycles. Therefore we store "config" in
588 * "csp->config", and "fs" in "csp->config->config_file_list".
590 config->config_file_list = fs;
595 config->multi_threaded = 1;
596 config->hport = HADDR_PORT;
597 config->buffer_limit = 4096 * 1024;
598 config->usermanual = strdup(USER_MANUAL_URL);
599 config->proxy_args = strdup("");
601 if ((configfp = fopen(configfile, "r")) == NULL)
603 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
605 /* Never get here - LOG_LEVEL_FATAL causes program exit */
608 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
610 char cmd[BUFFER_SIZE];
611 char arg[BUFFER_SIZE];
612 char tmp[BUFFER_SIZE];
614 struct access_control_list *cur_acl;
615 #endif /* def FEATURE_ACL */
616 struct forward_spec *cur_fwd;
622 /* Copy command (i.e. up to space or tab) into cmd */
625 while (*p && (*p != ' ') && (*p != '\t'))
631 /* Skip over the whitespace in buf */
632 while (*p && ((*p == ' ') || (*p == '\t')))
637 /* Copy the argument into arg */
640 /* Should never happen, but check this anyway */
646 /* Make sure the command field is lower case */
651 *p = ijb_tolower(*p);
655 /* Save the argument for show-proxy-args */
656 savearg(cmd, arg, config);
659 switch( hash_string( cmd ) )
661 /* *************************************************************************
662 * actionsfile actions-file-name
663 * In confdir by default
664 * *************************************************************************/
665 case hash_actions_file :
667 while ((i < MAX_ACTION_FILES) && (NULL != config->actions_file[i]))
672 if (i >= MAX_ACTION_FILES)
674 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
675 "(You can increase this limit by changing MAX_ACTION_FILES in project.h and recompiling).",
678 config->actions_file_short[i] = strdup(arg);
679 p = malloc(strlen(arg) + sizeof(".action"));
682 log_error(LOG_LEVEL_FATAL, "Out of memory");
685 strcat(p, ".action");
686 config->actions_file[i] = make_path(config->confdir, p);
690 /* *************************************************************************
691 * admin-address email-address
692 * *************************************************************************/
693 case hash_admin_address :
694 freez(config->admin_address);
695 config->admin_address = strdup(arg);
698 /* *************************************************************************
700 * *************************************************************************/
701 case hash_buffer_limit :
702 config->buffer_limit = (size_t) 1024 * atoi(arg);
705 /* *************************************************************************
706 * confdir directory-name
707 * *************************************************************************/
709 freez(config->confdir);
710 config->confdir = make_path( NULL, arg);
713 /* *************************************************************************
715 * Specifies debug level, multiple values are ORed together.
716 * *************************************************************************/
718 config->debug |= atoi(arg);
721 /* *************************************************************************
722 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
723 * *************************************************************************/
725 case hash_deny_access:
726 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
728 if ((vec_count != 1) && (vec_count != 2))
730 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
731 "deny-access directive in configuration file.");
732 string_append(&config->proxy_args,
733 "<br>\nWARNING: Wrong number of parameters for "
734 "deny-access directive in configuration file.<br><br>\n");
738 /* allocate a new node */
739 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
743 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
744 /* Never get here - LOG_LEVEL_FATAL causes program exit */
747 cur_acl->action = ACL_DENY;
749 if (acl_addr(vec[0], cur_acl->src) < 0)
751 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
752 "directive in configuration file: \"%s\"", vec[0]);
753 string_append(&config->proxy_args,
754 "<br>\nWARNING: Invalid source IP for deny-access directive"
755 " in configuration file: \"");
756 string_append(&config->proxy_args,
758 string_append(&config->proxy_args,
765 if (acl_addr(vec[1], cur_acl->dst) < 0)
767 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
768 "directive in configuration file: \"%s\"", vec[0]);
769 string_append(&config->proxy_args,
770 "<br>\nWARNING: Invalid destination IP for deny-access directive"
771 " in configuration file: \"");
772 string_append(&config->proxy_args,
774 string_append(&config->proxy_args,
782 * Add it to the list. Note we reverse the list to get the
783 * behaviour the user expects. With both the ACL and
784 * actions file, the last match wins. However, the internal
785 * implementations are different: The actions file is stored
786 * in the same order as the file, and scanned completely.
787 * With the ACL, we reverse the order as we load it, then
788 * when we scan it we stop as soon as we get a match.
790 cur_acl->next = config->acl;
791 config->acl = cur_acl;
794 #endif /* def FEATURE_ACL */
796 /* *************************************************************************
797 * enable-edit-actions 0|1
798 * *************************************************************************/
799 #ifdef FEATURE_CGI_EDIT_ACTIONS
800 case hash_enable_edit_actions:
801 if ((*arg != '\0') && (0 != atoi(arg)))
803 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
807 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
810 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
812 /* *************************************************************************
813 * enable-remote-toggle 0|1
814 * *************************************************************************/
815 #ifdef FEATURE_CGI_EDIT_ACTIONS
816 case hash_enable_remote_toggle:
817 if ((*arg != '\0') && (0 != atoi(arg)))
819 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
823 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
826 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
828 /* *************************************************************************
829 * forward url-pattern (.|http-proxy-host[:port])
830 * *************************************************************************/
832 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
836 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
837 "directive in configuration file.");
838 string_append(&config->proxy_args,
839 "<br>\nWARNING: Wrong number of parameters for "
840 "forward directive in configuration file.");
844 /* allocate a new node */
845 cur_fwd = zalloc(sizeof(*cur_fwd));
848 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
849 /* Never get here - LOG_LEVEL_FATAL causes program exit */
853 cur_fwd->type = SOCKS_NONE;
855 /* Save the URL pattern */
856 if (create_url_spec(cur_fwd->url, vec[0]))
858 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
859 "directive in configuration file.");
860 string_append(&config->proxy_args,
861 "<br>\nWARNING: Bad URL specifier for "
862 "forward directive in configuration file.");
866 /* Parse the parent HTTP proxy host:port */
869 if (strcmp(p, ".") != 0)
871 cur_fwd->forward_host = strdup(p);
873 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
876 cur_fwd->forward_port = atoi(p);
879 if (cur_fwd->forward_port <= 0)
881 cur_fwd->forward_port = 8000;
886 cur_fwd->next = config->forward;
887 config->forward = cur_fwd;
891 /* *************************************************************************
892 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
893 * *************************************************************************/
894 case hash_forward_socks4:
895 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
899 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
900 "forward-socks4 directive in configuration file.");
901 string_append(&config->proxy_args,
902 "<br>\nWARNING: Wrong number of parameters for "
903 "forward-socks4 directive in configuration file.");
907 /* allocate a new node */
908 cur_fwd = zalloc(sizeof(*cur_fwd));
911 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
912 /* Never get here - LOG_LEVEL_FATAL causes program exit */
916 cur_fwd->type = SOCKS_4;
918 /* Save the URL pattern */
919 if (create_url_spec(cur_fwd->url, vec[0]))
921 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
922 "directive in configuration file.");
923 string_append(&config->proxy_args,
924 "<br>\nWARNING: Bad URL specifier for "
925 "forward-socks4 directive in configuration file.");
929 /* Parse the SOCKS proxy host[:port] */
932 if (strcmp(p, ".") != 0)
934 cur_fwd->gateway_host = strdup(p);
936 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
939 cur_fwd->gateway_port = atoi(p);
941 if (cur_fwd->gateway_port <= 0)
943 cur_fwd->gateway_port = 1080;
947 /* Parse the parent HTTP proxy host[:port] */
950 if (strcmp(p, ".") != 0)
952 cur_fwd->forward_host = strdup(p);
954 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
957 cur_fwd->forward_port = atoi(p);
960 if (cur_fwd->forward_port <= 0)
962 cur_fwd->forward_port = 8000;
967 cur_fwd->next = config->forward;
968 config->forward = cur_fwd;
972 /* *************************************************************************
973 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
974 * *************************************************************************/
975 case hash_forward_socks4a:
976 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
980 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
981 "forward-socks4a directive in configuration file.");
982 string_append(&config->proxy_args,
983 "<br>\nWARNING: Wrong number of parameters for "
984 "forward-socks4a directive in configuration file.");
988 /* allocate a new node */
989 cur_fwd = zalloc(sizeof(*cur_fwd));
992 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
993 /* Never get here - LOG_LEVEL_FATAL causes program exit */
997 cur_fwd->type = SOCKS_4A;
999 /* Save the URL pattern */
1000 if (create_url_spec(cur_fwd->url, vec[0]))
1002 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
1003 "directive in configuration file.");
1004 string_append(&config->proxy_args,
1005 "<br>\nWARNING: Bad URL specifier for "
1006 "forward-socks4a directive in configuration file.");
1010 /* Parse the SOCKS proxy host[:port] */
1013 cur_fwd->gateway_host = strdup(p);
1015 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1018 cur_fwd->gateway_port = atoi(p);
1020 if (cur_fwd->gateway_port <= 0)
1022 cur_fwd->gateway_port = 1080;
1025 /* Parse the parent HTTP proxy host[:port] */
1028 if (strcmp(p, ".") != 0)
1030 cur_fwd->forward_host = strdup(p);
1032 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1035 cur_fwd->forward_port = atoi(p);
1038 if (cur_fwd->forward_port <= 0)
1040 cur_fwd->forward_port = 8000;
1045 cur_fwd->next = config->forward;
1046 config->forward = cur_fwd;
1050 /* *************************************************************************
1051 * jarfile jar-file-name
1052 * In logdir by default
1053 * *************************************************************************/
1054 #ifdef FEATURE_COOKIE_JAR
1056 freez(config->jarfile);
1057 config->jarfile = make_path(config->logdir, arg);
1059 #endif /* def FEATURE_COOKIE_JAR */
1061 /* *************************************************************************
1062 * listen-address [ip][:port]
1063 * *************************************************************************/
1064 case hash_listen_address :
1065 freez(config->haddr);
1066 config->haddr = strdup(arg);
1069 /* *************************************************************************
1070 * logdir directory-name
1071 * *************************************************************************/
1073 freez(config->logdir);
1074 config->logdir = make_path(NULL, arg);
1077 /* *************************************************************************
1078 * logfile log-file-name
1079 * In logdir by default
1080 * *************************************************************************/
1082 freez(config->logfile);
1083 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1086 /* *************************************************************************
1087 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1088 * *************************************************************************/
1090 case hash_permit_access:
1091 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1093 if ((vec_count != 1) && (vec_count != 2))
1095 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1096 "permit-access directive in configuration file.");
1097 string_append(&config->proxy_args,
1098 "<br>\nWARNING: Wrong number of parameters for "
1099 "permit-access directive in configuration file.<br><br>\n");
1104 /* allocate a new node */
1105 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1107 if (cur_acl == NULL)
1109 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1110 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1113 cur_acl->action = ACL_PERMIT;
1115 if (acl_addr(vec[0], cur_acl->src) < 0)
1117 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1118 "directive in configuration file: \"%s\"", vec[0]);
1119 string_append(&config->proxy_args,
1120 "<br>\nWARNING: Invalid source IP for permit-access directive"
1121 " in configuration file: \"");
1122 string_append(&config->proxy_args,
1124 string_append(&config->proxy_args,
1131 if (acl_addr(vec[1], cur_acl->dst) < 0)
1133 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1134 "permit-access directive in configuration file: \"%s\"",
1136 string_append(&config->proxy_args,
1137 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1138 " in configuration file: \"");
1139 string_append(&config->proxy_args,
1141 string_append(&config->proxy_args,
1149 * Add it to the list. Note we reverse the list to get the
1150 * behaviour the user expects. With both the ACL and
1151 * actions file, the last match wins. However, the internal
1152 * implementations are different: The actions file is stored
1153 * in the same order as the file, and scanned completely.
1154 * With the ACL, we reverse the order as we load it, then
1155 * when we scan it we stop as soon as we get a match.
1157 cur_acl->next = config->acl;
1158 config->acl = cur_acl;
1161 #endif /* def FEATURE_ACL */
1163 /* *************************************************************************
1164 * proxy-info-url url
1165 * *************************************************************************/
1166 case hash_proxy_info_url :
1167 freez(config->proxy_info_url);
1168 config->proxy_info_url = strdup(arg);
1171 /* *************************************************************************
1172 * re_filterfile file-name
1173 * In confdir by default.
1174 * *************************************************************************/
1175 case hash_filterfile :
1176 freez(config->re_filterfile);
1177 config->re_filterfile = make_path(config->confdir, arg);
1180 /* *************************************************************************
1182 * *************************************************************************/
1183 case hash_single_threaded :
1184 config->multi_threaded = 0;
1187 /* *************************************************************************
1189 * *************************************************************************/
1190 #ifdef FEATURE_TOGGLE
1192 g_bToggleIJB = atoi(arg);
1194 #endif /* def FEATURE_TOGGLE */
1196 /* *************************************************************************
1197 * trust-info-url url
1198 * *************************************************************************/
1199 #ifdef FEATURE_TRUST
1200 case hash_trust_info_url :
1201 enlist(config->trust_info, arg);
1203 #endif /* def FEATURE_TRUST */
1205 /* *************************************************************************
1206 * trustfile filename
1207 * (In confdir by default.)
1208 * *************************************************************************/
1209 #ifdef FEATURE_TRUST
1210 case hash_trustfile :
1211 freez(config->trustfile);
1212 config->trustfile = make_path(config->confdir, arg);
1214 #endif /* def FEATURE_TRUST */
1216 /* *************************************************************************
1218 * *************************************************************************/
1219 case hash_usermanual :
1220 freez(config->usermanual);
1221 config->usermanual = strdup(arg);
1224 /* *************************************************************************
1225 * Win32 Console options:
1226 * *************************************************************************/
1228 /* *************************************************************************
1230 * *************************************************************************/
1232 case hash_hide_console :
1235 #endif /*def _WIN_CONSOLE*/
1238 /* *************************************************************************
1239 * Win32 GUI options:
1240 * *************************************************************************/
1242 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1243 /* *************************************************************************
1244 * activity-animation (0|1)
1245 * *************************************************************************/
1246 case hash_activity_animation :
1247 g_bShowActivityAnimation = atoi(arg);
1250 /* *************************************************************************
1251 * close-button-minimizes (0|1)
1252 * *************************************************************************/
1253 case hash_close_button_minimizes :
1254 g_bCloseHidesWindow = atoi(arg);
1257 /* *************************************************************************
1258 * log-buffer-size (0|1)
1259 * *************************************************************************/
1260 case hash_log_buffer_size :
1261 g_bLimitBufferSize = atoi(arg);
1264 /* *************************************************************************
1265 * log-font-name fontnane
1266 * *************************************************************************/
1267 case hash_log_font_name :
1268 strcpy( g_szFontFaceName, arg );
1271 /* *************************************************************************
1273 * *************************************************************************/
1274 case hash_log_font_size :
1275 g_nFontSize = atoi(arg);
1278 /* *************************************************************************
1279 * log-highlight-messages (0|1)
1280 * *************************************************************************/
1281 case hash_log_highlight_messages :
1282 g_bHighlightMessages = atoi(arg);
1285 /* *************************************************************************
1287 * *************************************************************************/
1288 case hash_log_max_lines :
1289 g_nMaxBufferLines = atoi(arg);
1292 /* *************************************************************************
1293 * log-messages (0|1)
1294 * *************************************************************************/
1295 case hash_log_messages :
1296 g_bLogMessages = atoi(arg);
1299 /* *************************************************************************
1300 * show-on-task-bar (0|1)
1301 * *************************************************************************/
1302 case hash_show_on_task_bar :
1303 g_bShowOnTaskBar = atoi(arg);
1306 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1309 /* *************************************************************************
1310 * Warnings about unsupported features
1311 * *************************************************************************/
1313 case hash_deny_access:
1314 #endif /* ndef FEATURE_ACL */
1315 #ifndef FEATURE_CGI_EDIT_ACTIONS
1316 case hash_enable_edit_actions:
1317 case hash_enable_remote_toggle:
1318 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1319 #ifndef FEATURE_COOKIE_JAR
1321 #endif /* ndef FEATURE_COOKIE_JAR */
1323 case hash_permit_access:
1324 #endif /* ndef FEATURE_ACL */
1325 #ifndef FEATURE_TOGGLE
1327 #endif /* ndef FEATURE_TOGGLE */
1328 #ifndef FEATURE_TRUST
1329 case hash_trustfile :
1330 case hash_trust_info_url :
1331 #endif /* ndef FEATURE_TRUST */
1333 #ifndef _WIN_CONSOLE
1334 case hash_hide_console :
1335 #endif /* ndef _WIN_CONSOLE */
1337 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1338 case hash_activity_animation :
1339 case hash_close_button_minimizes :
1340 case hash_log_buffer_size :
1341 case hash_log_font_name :
1342 case hash_log_font_size :
1343 case hash_log_highlight_messages :
1344 case hash_log_max_lines :
1345 case hash_log_messages :
1346 case hash_show_on_task_bar :
1347 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1348 /* These warnings are annoying - so hide them. -- Jon */
1349 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1352 /* *************************************************************************/
1354 /* *************************************************************************/
1356 * I decided that I liked this better as a warning than an
1357 * error. To change back to an error, just change log level
1358 * to LOG_LEVEL_FATAL.
1360 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1361 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1362 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1363 string_append(&config->proxy_args, buf);
1364 string_append(&config->proxy_args, "<br><br>\n");
1367 /* *************************************************************************/
1368 } /* end switch( hash_string(cmd) ) */
1369 } /* end while ( read_config_line(...) ) */
1373 if (NULL == config->proxy_args)
1375 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1378 init_error_log(Argv[0], config->logfile, config->debug);
1380 if (config->actions_file[0])
1382 add_loader(load_actions_file, config);
1385 if (config->re_filterfile)
1387 add_loader(load_re_filterfile, config);
1390 #ifdef FEATURE_TRUST
1391 if (config->trustfile)
1393 add_loader(load_trustfile, config);
1395 #endif /* def FEATURE_TRUST */
1397 #ifdef FEATURE_COOKIE_JAR
1398 if ( NULL != config->jarfile )
1400 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1402 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1403 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1405 setbuf(config->jar, NULL);
1407 #endif /* def FEATURE_COOKIE_JAR */
1409 if ( NULL == config->haddr )
1411 config->haddr = strdup( HADDR_DEFAULT );
1414 if ( NULL != config->haddr )
1416 if (NULL != (p = strchr(config->haddr, ':')))
1421 config->hport = atoi(p);
1425 if (config->hport <= 0)
1428 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1429 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1431 if (*config->haddr == '\0')
1433 config->haddr = NULL;
1438 * Want to run all the loaders once now.
1440 * Need to set up a fake csp, so they can get to the config.
1442 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1443 fake_csp->config = config;
1445 if (run_loader(fake_csp))
1448 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1449 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1453 /* FIXME: this is a kludge for win32 */
1454 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1456 g_actions_file = config->actions_file[0]; /* FIXME only works for first action file */
1457 g_re_filterfile = config->re_filterfile;
1459 #ifdef FEATURE_TRUST
1460 g_trustfile = config->trustfile;
1461 #endif /* def FEATURE_TRUST */
1464 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1465 /* FIXME: end kludge */
1468 config->need_bind = 1;
1470 if (current_configfile)
1472 struct configuration_spec * oldcfg = (struct configuration_spec *)
1473 current_configfile->f;
1475 * Check if config->haddr,hport == oldcfg->haddr,hport
1477 * The following could be written more compactly as a single,
1478 * (unreadably long) if statement.
1480 config->need_bind = 0;
1481 if (config->hport != oldcfg->hport)
1483 config->need_bind = 1;
1485 else if (config->haddr == NULL)
1487 if (oldcfg->haddr != NULL)
1489 config->need_bind = 1;
1492 else if (oldcfg->haddr == NULL)
1494 config->need_bind = 1;
1496 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1498 config->need_bind = 1;
1501 current_configfile->unloader = unload_configfile;
1504 fs->next = files->next;
1507 current_configfile = fs;
1513 /*********************************************************************
1515 * Function : savearg
1517 * Description : Called from `load_config'. It saves each non-empty
1518 * and non-comment line from config into
1519 * config->proxy_args. This is used to create the
1520 * show-proxy-args page. On error, frees
1521 * config->proxy_args and sets it to NULL
1524 * 1 : command = config setting that was found
1525 * 2 : argument = the setting's argument (if any)
1526 * 3 : config = Configuration to save into.
1530 *********************************************************************/
1531 static void savearg(char *command, char *argument, struct configuration_spec * config)
1541 string_join(&buf, add_help_link(command, config));
1545 freez(config->proxy_args);
1549 if ( (NULL != argument) && ('\0' != *argument) )
1551 s = html_encode(argument);
1555 freez(config->proxy_args);
1559 if (strncmpic(argument, "http://", 7) == 0)
1561 string_append(&buf, "<a href=\"");
1562 string_append(&buf, s);
1563 string_append(&buf, "\">");
1564 string_join (&buf, s);
1565 string_append(&buf, "</a>");
1569 string_join (&buf, s);
1573 string_append(&buf, "<br>\n");
1575 string_join(&config->proxy_args, buf);