1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 2.2 2002/09/04 15:48:33 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/src/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 2.2 2002/09/04 15:48:33 oes
39 * Synced with the stable branch:
40 * Revision 1.48.2.1 2002/08/21 17:58:05 oes
41 * Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
43 * Revision 2.1 2002/06/04 17:22:36 jongfoster
46 * Revision 2.0 2002/06/04 14:34:21 jongfoster
47 * Moving source files to src/
49 * Revision 1.48 2002/05/14 21:30:38 oes
50 * savearg now uses own linking code instead of (now special-cased) add_help_link
52 * Revision 1.47 2002/05/12 21:36:29 jongfoster
53 * Correcting function comments
55 * Revision 1.46 2002/04/26 12:55:14 oes
56 * - New option "user-manual", defaults to our site
57 * via project.h #define
58 * - savearg now embeds option names in help links
60 * Revision 1.45 2002/04/24 02:11:54 oes
61 * Jon's multiple AF patch: Allow up to MAX_ACTION_FILES actionsfile options
63 * Revision 1.44 2002/04/08 20:37:13 swa
66 * Revision 1.43 2002/04/08 20:36:50 swa
69 * Revision 1.42 2002/04/05 15:50:15 oes
70 * fix for invalid HTML proxy_args
72 * Revision 1.41 2002/03/31 17:19:00 jongfoster
73 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
75 * Revision 1.40 2002/03/26 22:29:55 swa
76 * we have a new homepage!
78 * Revision 1.39 2002/03/24 13:25:43 swa
79 * name change related issues
81 * Revision 1.38 2002/03/24 13:05:48 jongfoster
82 * Renaming re_filterfile to filterfile
84 * Revision 1.37 2002/03/16 23:54:06 jongfoster
85 * Adding graceful termination feature, to help look for memory leaks.
86 * If you enable this (which, by design, has to be done by hand
87 * editing config.h) and then go to http://i.j.b/die, then the program
88 * will exit cleanly after the *next* request. It should free all the
89 * memory that was used.
91 * Revision 1.36 2002/03/13 00:27:05 jongfoster
94 * Revision 1.35 2002/03/07 03:52:44 oes
95 * Set logging to tty for --no-daemon mode
97 * Revision 1.34 2002/03/06 23:14:35 jongfoster
98 * Trivial cosmetic changes to make function comments easier to find.
100 * Revision 1.33 2002/03/05 04:52:42 oes
101 * Deleted non-errlog debugging code
103 * Revision 1.32 2002/03/04 18:24:53 oes
104 * Re-enabled output of unknown config directive hash
106 * Revision 1.31 2002/03/03 15:07:20 oes
107 * Re-enabled automatic config reloading
109 * Revision 1.30 2002/01/22 23:31:43 jongfoster
110 * Replacing strsav() with string_append()
112 * Revision 1.29 2002/01/17 21:02:30 jongfoster
113 * Moving all our URL and URL pattern parsing code to urlmatch.c.
115 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
117 * Revision 1.28 2001/12/30 14:07:32 steudten
118 * - Add signal handling (unix)
119 * - Add SIGHUP handler (unix)
120 * - Add creation of pidfile (unix)
121 * - Add action 'top' in rc file (RH)
122 * - Add entry 'SIGNALS' to manpage
123 * - Add exit message to logfile (unix)
125 * Revision 1.27 2001/11/07 00:02:13 steudten
126 * Add line number in error output for lineparsing for
127 * actionsfile and configfile.
128 * Special handling for CLF added.
130 * Revision 1.26 2001/11/05 21:41:43 steudten
131 * Add changes to be a real daemon just for unix os.
132 * (change cwd to /, detach from controlling tty, set
133 * process group and session leader to the own process.
135 * Add some fatal-error log message for failed malloc().
136 * Add '-d' if compiled with 'configure --with-debug' to
137 * enable debug output.
139 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
140 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
141 * threads to call select() simultaneously. So, it's time to do a real, live,
142 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
143 * (native). Both versions will work, but using __OS2__ offers multi-threading.
145 * Revision 1.24 2001/10/23 21:40:30 jongfoster
146 * Added support for enable-edit-actions and enable-remote-toggle config
149 * Revision 1.23 2001/10/07 15:36:00 oes
150 * Introduced new config option "buffer-limit"
152 * Revision 1.22 2001/09/22 16:36:59 jongfoster
153 * Removing unused parameter fs from read_config_line()
155 * Revision 1.21 2001/09/16 17:10:43 jongfoster
156 * Moving function savearg() here, since it was the only thing left in
159 * Revision 1.20 2001/07/30 22:08:36 jongfoster
160 * Tidying up #defines:
161 * - All feature #defines are now of the form FEATURE_xxx
162 * - Permanently turned off WIN_GUI_EDIT
163 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
165 * Revision 1.19 2001/07/15 17:45:16 jongfoster
166 * Removing some unused #includes
168 * Revision 1.18 2001/07/13 14:01:14 oes
169 * - Removed all #ifdef PCRS
170 * - Removed vim-settings
172 * Revision 1.17 2001/06/29 13:31:03 oes
173 * - Improved comments
174 * - Fixed (actionsfile) and sorted hashes
175 * - Introduced admin_address and proxy-info-url
176 * as config parameters
177 * - Renamed config->proxy_args_invocation (which didn't have
178 * the invocation but the options!) to config->proxy_args
179 * - Various adaptions
180 * - Removed logentry from cancelled commit
182 * Revision 1.16 2001/06/09 10:55:28 jongfoster
183 * Changing BUFSIZ ==> BUFFER_SIZE
185 * Revision 1.15 2001/06/07 23:13:40 jongfoster
186 * Merging ACL and forward files into config file.
187 * Cosmetic: Sorting config file options alphabetically.
188 * Cosmetic: Adding brief syntax comments to config file options.
190 * Revision 1.14 2001/06/07 14:46:25 joergs
191 * Missing make_path() added for re_filterfile.
193 * Revision 1.13 2001/06/05 22:33:54 jongfoster
195 * Fixed minor memory leak.
196 * Also now uses make_path to prepend the pathnames.
198 * Revision 1.12 2001/06/05 20:04:09 jongfoster
199 * Now uses _snprintf() in place of snprintf() under Win32.
201 * Revision 1.11 2001/06/04 18:31:58 swa
202 * files are now prefixed with either `confdir' or `logdir'.
203 * `make redhat-dist' replaces both entries confdir and logdir
206 * Revision 1.10 2001/06/03 19:11:54 oes
207 * introduced confdir option
209 * Revision 1.9 2001/06/01 20:06:24 jongfoster
210 * Removed support for "tinygif" option - moved to actions file.
212 * Revision 1.8 2001/05/31 21:27:13 jongfoster
213 * Removed many options from the config file and into the
214 * "actions" file: add_forwarded, suppress_vanilla_wafer,
215 * wafer, add_header, user_agent, referer, from
216 * Also globally replaced "permission" with "action".
218 * Revision 1.7 2001/05/29 09:50:24 jongfoster
219 * Unified blocklist/imagelist/permissionslist.
220 * File format is still under discussion, but the internal changes
223 * Also modified interceptor behaviour:
224 * - We now intercept all URLs beginning with one of the following
225 * prefixes (and *only* these prefixes):
227 * * http://ijbswa.sf.net/config/
228 * * http://ijbswa.sourceforge.net/config/
229 * - New interceptors "home page" - go to http://i.j.b/ to see it.
230 * - Internal changes so that intercepted and fast redirect pages
231 * are not replaced with an image.
232 * - Interceptors now have the option to send a binary page direct
233 * to the client. (i.e. ijb-send-banner uses this)
234 * - Implemented show-url-info interceptor. (Which is why I needed
235 * the above interceptors changes - a typical URL is
236 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
237 * The previous mechanism would not have intercepted that, and
238 * if it had been intercepted then it then it would have replaced
241 * Revision 1.6 2001/05/26 00:28:36 jongfoster
242 * Automatic reloading of config file.
243 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
244 * Most of the global variables have been moved to a new
245 * struct configuration_spec, accessed through csp->config->globalname
246 * Most of the globals remaining are used by the Win32 GUI.
248 * Revision 1.5 2001/05/25 22:34:30 jongfoster
251 * Revision 1.4 2001/05/22 18:46:04 oes
253 * - Enabled filtering banners by size rather than URL
254 * by adding patterns that replace all standard banner
255 * sizes with the "Junkbuster" gif to the re_filterfile
257 * - Enabled filtering WebBugs by providing a pattern
258 * which kills all 1x1 images
260 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
261 * which is selected by the (nonstandard and therefore
262 * capital) letter 'U' in the option string.
263 * It causes the quantifiers to be ungreedy by default.
264 * Appending a ? turns back to greedy (!).
266 * - Added a new interceptor ijb-send-banner, which
267 * sends back the "Junkbuster" gif. Without imagelist or
268 * MSIE detection support, or if tinygif = 1, or the
269 * URL isn't recognized as an imageurl, a lame HTML
270 * explanation is sent instead.
272 * - Added new feature, which permits blocking remote
273 * script redirects and firing back a local redirect
275 * The feature is conditionally compiled, i.e. it
276 * can be disabled with --disable-fast-redirects,
277 * plus it must be activated by a "fast-redirects"
278 * line in the config file, has its own log level
279 * and of course wants to be displayed by show-proxy-args
280 * Note: Boy, all the #ifdefs in 1001 locations and
281 * all the fumbling with configure.in and acconfig.h
282 * were *way* more work than the feature itself :-(
284 * - Because a generic redirect template was needed for
285 * this, tinygif = 3 now uses the same.
287 * - Moved GIFs, and other static HTTP response templates
292 * - Removed some >400 CRs again (Jon, you really worked
295 * Revision 1.3 2001/05/20 01:21:20 jongfoster
296 * Version 2.9.4 checkin.
297 * - Merged popupfile and cookiefile, and added control over PCRS
298 * filtering, in new "permissionsfile".
299 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
300 * file error you now get a message box (in the Win32 GUI) rather
301 * than the program exiting with no explanation.
302 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
304 * - Removed tabs from "config"
305 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
306 * - Bumped up version number.
308 * Revision 1.2 2001/05/17 23:01:01 oes
309 * - Cleaned CRLF's from the sources and related files
311 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
312 * Initial import of version 2.9.3 source tree
315 *********************************************************************/
321 #include <sys/types.h>
335 # include <windows.h>
338 # ifndef _WIN_CONSOLE
340 # endif /* ndef _WIN_CONSOLE */
342 /* VC++ has "_snprintf", not "snprintf" */
343 #define snprintf _snprintf
345 #else /* ifndef _WIN32 */
349 # include <sys/wait.h>
351 # include <sys/time.h>
352 # include <sys/stat.h>
362 #include "miscutil.h"
366 #include "urlmatch.h"
369 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
372 * Fix a problem with Solaris. There should be no effect on other
374 * Solaris's isspace() is a macro which uses it's argument directly
375 * as an array index. Therefore we need to make sure that high-bit
376 * characters generate +ve values, and ideally we also want to make
377 * the argument match the declared parameter type of "int".
379 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
380 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
382 #ifdef FEATURE_TOGGLE
384 * Indicates if Privoxy is currently enabled.
386 int g_bToggleIJB = 1; /* Privoxy is enabled by default. */
387 #endif /* def FEATURE_TOGGLE */
390 * The filename of the config file
392 const char *configfile = NULL;
395 * Global argument count, so that CGI functions can access the
401 * Global argument list, so that CGI functions can access the
404 const char **Argv = NULL;
407 * The configuration file which is currently loaded.
409 static struct file_list *current_configfile = NULL;
413 * This takes the "cryptic" hash of each keyword and aliases them to
414 * something a little more readable. This also makes changing the
415 * hash values easier if they should change or the hash algorthm changes.
416 * Use the included "hash" program to find out what the hash will be
417 * for any string supplied on the command line. (Or just put it in the
418 * config file and read the number from the error message in the log).
420 * Please keep this list sorted alphabetically (but with the Windows
421 * console and GUI specific options last).
424 #define hash_actions_file 1196306641ul /**< "actionsfile" */
425 #define hash_admin_address 4112573064ul /**< "admin-address" */
426 #define hash_buffer_limit 1881726070ul /**< "buffer-limit */
427 #define hash_confdir 1978389ul /**< "confdir" */
428 #define hash_debug 78263ul /**< "debug" */
429 #define hash_deny_access 1227333715ul /**< "deny-access" */
430 #define hash_enable_edit_actions 2517097536ul /**< "enable-edit-actions" */
431 #define hash_enable_remote_toggle 2979744683ul /**< "enable-remote-toggle" */
432 #define hash_filterfile 250887266ul /**< "filterfile" */
433 #define hash_forward 2029845ul /**< "forward" */
434 #define hash_forward_socks4 3963965521ul /**< "forward-socks4" */
435 #define hash_forward_socks4a 2639958518ul /**< "forward-socks4a" */
436 #define hash_jarfile 2046641ul /**< "jarfile" */
437 #define hash_listen_address 1255650842ul /**< "listen-address" */
438 #define hash_logdir 422889ul /**< "logdir" */
439 #define hash_logfile 2114766ul /**< "logfile" */
440 #define hash_permit_access 3587953268ul /**< "permit-access" */
441 #define hash_proxy_info_url 3903079059ul /**< "proxy-info-url" */
442 #define hash_single_threaded 4250084780ul /**< "single-threaded" */
443 #define hash_suppress_blocklists 1948693308ul /**< "suppress-blocklists" */
444 #define hash_toggle 447966ul /**< "toggle" */
445 #define hash_trust_info_url 430331967ul /**< "trust-info-url" */
446 #define hash_trustfile 56494766ul /**< "trustfile" */
447 #define hash_usermanual 1416668518ul /**< "user-manual" */
449 #define hash_activity_animation 1817904738ul /**< "activity-animation" */
450 #define hash_close_button_minimizes 3651284693ul /**< "close-button-minimizes" */
451 #define hash_hide_console 2048809870ul /**< "hide-console" */
452 #define hash_log_buffer_size 2918070425ul /**< "log-buffer-size" */
453 #define hash_log_font_name 2866730124ul /**< "log-font-name" */
454 #define hash_log_font_size 2866731014ul /**< "log-font-size" */
455 #define hash_log_highlight_messages 4032101240ul /**< "log-highlight-messages" */
456 #define hash_log_max_lines 2868344173ul /**< "log-max-lines" */
457 #define hash_log_messages 2291744899ul /**< "log-messages" */
458 #define hash_show_on_task_bar 215410365ul /**< "show-on-task-bar" */
461 static void savearg(char *command, char *argument, struct configuration_spec * config);
463 /*********************************************************************
465 * Function : unload_configfile
467 * Description : Free the config structure and all components.
470 * 1 : data: struct configuration_spec to unload
474 *********************************************************************/
475 void unload_configfile (void * data)
477 struct configuration_spec * config = (struct configuration_spec *)data;
478 struct forward_spec *cur_fwd = config->forward;
481 struct access_control_list *cur_acl = config->acl;
483 while (cur_acl != NULL)
485 struct access_control_list * next_acl = cur_acl->next;
490 #endif /* def FEATURE_ACL */
492 while (cur_fwd != NULL)
494 struct forward_spec * next_fwd = cur_fwd->next;
495 free_url_spec(cur_fwd->url);
497 freez(cur_fwd->gateway_host);
498 freez(cur_fwd->forward_host);
502 config->forward = NULL;
504 #ifdef FEATURE_COOKIE_JAR
505 if ( NULL != config->jar )
507 fclose( config->jar );
510 #endif /* def FEATURE_COOKIE_JAR */
512 freez(config->confdir);
513 freez(config->logdir);
515 freez(config->haddr);
516 freez(config->logfile);
518 for (i = 0; i < MAX_ACTION_FILES; i++)
520 freez(config->actions_file_short[i]);
521 freez(config->actions_file[i]);
524 freez(config->admin_address);
525 freez(config->proxy_info_url);
526 freez(config->proxy_args);
527 freez(config->usermanual);
529 #ifdef FEATURE_COOKIE_JAR
530 freez(config->jarfile);
531 #endif /* def FEATURE_COOKIE_JAR */
533 freez(config->re_filterfile);
538 #ifdef FEATURE_GRACEFUL_TERMINATION
539 /*********************************************************************
541 * Function : unload_current_config_file
543 * Description : Unloads current config file - reset to state at
544 * beginning of program.
550 *********************************************************************/
551 void unload_current_config_file(void)
553 if (current_configfile)
555 current_configfile->unloader = unload_configfile;
556 current_configfile = NULL;
562 /*********************************************************************
564 * Function : load_config
566 * Description : Load the config file and all parameters.
570 * Returns : The configuration_spec, or NULL on error.
572 *********************************************************************/
573 struct configuration_spec * load_config(void)
575 char buf[BUFFER_SIZE];
577 FILE *configfp = NULL;
578 struct configuration_spec * config = NULL;
579 struct client_state * fake_csp;
580 struct file_list *fs;
581 unsigned long linenum = 0;
584 if ( !check_file_changed(current_configfile, configfile, &fs))
586 /* No need to load */
587 return ((struct configuration_spec *)current_configfile->f);
591 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
595 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
597 #ifdef FEATURE_TOGGLE
599 #endif /* def FEATURE_TOGGLE */
601 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
607 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
608 /* Never get here - LOG_LEVEL_FATAL causes program exit */
612 * This is backwards from how it's usually done.
613 * Following the usual pattern, "fs" would be stored in a member
614 * variable in "csp", and then we'd access "config" from "fs->f",
615 * using a cast. However, "config" is used so often that a
616 * cast each time would be very ugly, and the extra indirection
617 * would waste CPU cycles. Therefore we store "config" in
618 * "csp->config", and "fs" in "csp->config->config_file_list".
620 config->config_file_list = fs;
625 config->multi_threaded = 1;
626 config->hport = HADDR_PORT;
627 config->buffer_limit = 4096 * 1024;
628 config->usermanual = strdup(USER_MANUAL_URL);
629 config->proxy_args = strdup("");
631 if ((configfp = fopen(configfile, "r")) == NULL)
633 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
635 /* Never get here - LOG_LEVEL_FATAL causes program exit */
638 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
640 char cmd[BUFFER_SIZE];
641 char arg[BUFFER_SIZE];
642 char tmp[BUFFER_SIZE];
644 struct access_control_list *cur_acl;
645 #endif /* def FEATURE_ACL */
646 struct forward_spec *cur_fwd;
652 /* Copy command (i.e. up to space or tab) into cmd */
655 while (*p && (*p != ' ') && (*p != '\t'))
661 /* Skip over the whitespace in buf */
662 while (*p && ((*p == ' ') || (*p == '\t')))
667 /* Copy the argument into arg */
670 /* Should never happen, but check this anyway */
676 /* Make sure the command field is lower case */
681 *p = ijb_tolower(*p);
685 /* Save the argument for show-proxy-args */
686 savearg(cmd, arg, config);
689 switch( hash_string( cmd ) )
691 /* *************************************************************************
692 * actionsfile actions-file-name
693 * In confdir by default
694 * *************************************************************************/
695 case hash_actions_file :
697 while ((i < MAX_ACTION_FILES) && (NULL != config->actions_file[i]))
702 if (i >= MAX_ACTION_FILES)
704 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
705 "(You can increase this limit by changing MAX_ACTION_FILES in project.h and recompiling).",
708 config->actions_file_short[i] = strdup(arg);
709 p = malloc(strlen(arg) + sizeof(".action"));
712 log_error(LOG_LEVEL_FATAL, "Out of memory");
715 strcat(p, ".action");
716 config->actions_file[i] = make_path(config->confdir, p);
720 /* *************************************************************************
721 * admin-address email-address
722 * *************************************************************************/
723 case hash_admin_address :
724 freez(config->admin_address);
725 config->admin_address = strdup(arg);
728 /* *************************************************************************
730 * *************************************************************************/
731 case hash_buffer_limit :
732 config->buffer_limit = (size_t) 1024 * atoi(arg);
735 /* *************************************************************************
736 * confdir directory-name
737 * *************************************************************************/
739 freez(config->confdir);
740 config->confdir = make_path( NULL, arg);
743 /* *************************************************************************
745 * Specifies debug level, multiple values are ORed together.
746 * *************************************************************************/
748 config->debug |= atoi(arg);
751 /* *************************************************************************
752 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
753 * *************************************************************************/
755 case hash_deny_access:
756 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
758 if ((vec_count != 1) && (vec_count != 2))
760 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
761 "deny-access directive in configuration file.");
762 string_append(&config->proxy_args,
763 "<br>\nWARNING: Wrong number of parameters for "
764 "deny-access directive in configuration file.<br><br>\n");
768 /* allocate a new node */
769 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
773 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
774 /* Never get here - LOG_LEVEL_FATAL causes program exit */
777 cur_acl->action = ACL_DENY;
779 if (acl_addr(vec[0], cur_acl->src) < 0)
781 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
782 "directive in configuration file: \"%s\"", vec[0]);
783 string_append(&config->proxy_args,
784 "<br>\nWARNING: Invalid source IP for deny-access directive"
785 " in configuration file: \"");
786 string_append(&config->proxy_args,
788 string_append(&config->proxy_args,
795 if (acl_addr(vec[1], cur_acl->dst) < 0)
797 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
798 "directive in configuration file: \"%s\"", vec[0]);
799 string_append(&config->proxy_args,
800 "<br>\nWARNING: Invalid destination IP for deny-access directive"
801 " in configuration file: \"");
802 string_append(&config->proxy_args,
804 string_append(&config->proxy_args,
812 * Add it to the list. Note we reverse the list to get the
813 * behaviour the user expects. With both the ACL and
814 * actions file, the last match wins. However, the internal
815 * implementations are different: The actions file is stored
816 * in the same order as the file, and scanned completely.
817 * With the ACL, we reverse the order as we load it, then
818 * when we scan it we stop as soon as we get a match.
820 cur_acl->next = config->acl;
821 config->acl = cur_acl;
824 #endif /* def FEATURE_ACL */
826 /* *************************************************************************
827 * enable-edit-actions 0|1
828 * *************************************************************************/
829 #ifdef FEATURE_CGI_EDIT_ACTIONS
830 case hash_enable_edit_actions:
831 if ((*arg != '\0') && (0 != atoi(arg)))
833 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
837 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
840 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
842 /* *************************************************************************
843 * enable-remote-toggle 0|1
844 * *************************************************************************/
845 #ifdef FEATURE_CGI_EDIT_ACTIONS
846 case hash_enable_remote_toggle:
847 if ((*arg != '\0') && (0 != atoi(arg)))
849 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
853 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
856 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
858 /* *************************************************************************
859 * forward url-pattern (.|http-proxy-host[:port])
860 * *************************************************************************/
862 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
866 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
867 "directive in configuration file.");
868 string_append(&config->proxy_args,
869 "<br>\nWARNING: Wrong number of parameters for "
870 "forward directive in configuration file.");
874 /* allocate a new node */
875 cur_fwd = zalloc(sizeof(*cur_fwd));
878 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
879 /* Never get here - LOG_LEVEL_FATAL causes program exit */
883 cur_fwd->type = SOCKS_NONE;
885 /* Save the URL pattern */
886 if (create_url_spec(cur_fwd->url, vec[0]))
888 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
889 "directive in configuration file.");
890 string_append(&config->proxy_args,
891 "<br>\nWARNING: Bad URL specifier for "
892 "forward directive in configuration file.");
896 /* Parse the parent HTTP proxy host:port */
899 if (strcmp(p, ".") != 0)
901 cur_fwd->forward_host = strdup(p);
903 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
906 cur_fwd->forward_port = atoi(p);
909 if (cur_fwd->forward_port <= 0)
911 cur_fwd->forward_port = 8000;
916 cur_fwd->next = config->forward;
917 config->forward = cur_fwd;
921 /* *************************************************************************
922 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
923 * *************************************************************************/
924 case hash_forward_socks4:
925 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
929 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
930 "forward-socks4 directive in configuration file.");
931 string_append(&config->proxy_args,
932 "<br>\nWARNING: Wrong number of parameters for "
933 "forward-socks4 directive in configuration file.");
937 /* allocate a new node */
938 cur_fwd = zalloc(sizeof(*cur_fwd));
941 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
942 /* Never get here - LOG_LEVEL_FATAL causes program exit */
946 cur_fwd->type = SOCKS_4;
948 /* Save the URL pattern */
949 if (create_url_spec(cur_fwd->url, vec[0]))
951 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
952 "directive in configuration file.");
953 string_append(&config->proxy_args,
954 "<br>\nWARNING: Bad URL specifier for "
955 "forward-socks4 directive in configuration file.");
959 /* Parse the SOCKS proxy host[:port] */
962 if (strcmp(p, ".") != 0)
964 cur_fwd->gateway_host = strdup(p);
966 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
969 cur_fwd->gateway_port = atoi(p);
971 if (cur_fwd->gateway_port <= 0)
973 cur_fwd->gateway_port = 1080;
977 /* Parse the parent HTTP proxy host[:port] */
980 if (strcmp(p, ".") != 0)
982 cur_fwd->forward_host = strdup(p);
984 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
987 cur_fwd->forward_port = atoi(p);
990 if (cur_fwd->forward_port <= 0)
992 cur_fwd->forward_port = 8000;
997 cur_fwd->next = config->forward;
998 config->forward = cur_fwd;
1002 /* *************************************************************************
1003 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
1004 * *************************************************************************/
1005 case hash_forward_socks4a:
1006 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1010 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1011 "forward-socks4a directive in configuration file.");
1012 string_append(&config->proxy_args,
1013 "<br>\nWARNING: Wrong number of parameters for "
1014 "forward-socks4a directive in configuration file.");
1018 /* allocate a new node */
1019 cur_fwd = zalloc(sizeof(*cur_fwd));
1020 if (cur_fwd == NULL)
1022 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1023 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1027 cur_fwd->type = SOCKS_4A;
1029 /* Save the URL pattern */
1030 if (create_url_spec(cur_fwd->url, vec[0]))
1032 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
1033 "directive in configuration file.");
1034 string_append(&config->proxy_args,
1035 "<br>\nWARNING: Bad URL specifier for "
1036 "forward-socks4a directive in configuration file.");
1040 /* Parse the SOCKS proxy host[:port] */
1043 cur_fwd->gateway_host = strdup(p);
1045 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1048 cur_fwd->gateway_port = atoi(p);
1050 if (cur_fwd->gateway_port <= 0)
1052 cur_fwd->gateway_port = 1080;
1055 /* Parse the parent HTTP proxy host[:port] */
1058 if (strcmp(p, ".") != 0)
1060 cur_fwd->forward_host = strdup(p);
1062 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1065 cur_fwd->forward_port = atoi(p);
1068 if (cur_fwd->forward_port <= 0)
1070 cur_fwd->forward_port = 8000;
1075 cur_fwd->next = config->forward;
1076 config->forward = cur_fwd;
1080 /* *************************************************************************
1081 * jarfile jar-file-name
1082 * In logdir by default
1083 * *************************************************************************/
1084 #ifdef FEATURE_COOKIE_JAR
1086 freez(config->jarfile);
1087 config->jarfile = make_path(config->logdir, arg);
1089 #endif /* def FEATURE_COOKIE_JAR */
1091 /* *************************************************************************
1092 * listen-address [ip][:port]
1093 * *************************************************************************/
1094 case hash_listen_address :
1095 freez(config->haddr);
1096 config->haddr = strdup(arg);
1099 /* *************************************************************************
1100 * logdir directory-name
1101 * *************************************************************************/
1103 freez(config->logdir);
1104 config->logdir = make_path(NULL, arg);
1107 /* *************************************************************************
1108 * logfile log-file-name
1109 * In logdir by default
1110 * *************************************************************************/
1112 freez(config->logfile);
1113 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1116 /* *************************************************************************
1117 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1118 * *************************************************************************/
1120 case hash_permit_access:
1121 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1123 if ((vec_count != 1) && (vec_count != 2))
1125 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1126 "permit-access directive in configuration file.");
1127 string_append(&config->proxy_args,
1128 "<br>\nWARNING: Wrong number of parameters for "
1129 "permit-access directive in configuration file.<br><br>\n");
1134 /* allocate a new node */
1135 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1137 if (cur_acl == NULL)
1139 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1140 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1143 cur_acl->action = ACL_PERMIT;
1145 if (acl_addr(vec[0], cur_acl->src) < 0)
1147 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1148 "directive in configuration file: \"%s\"", vec[0]);
1149 string_append(&config->proxy_args,
1150 "<br>\nWARNING: Invalid source IP for permit-access directive"
1151 " in configuration file: \"");
1152 string_append(&config->proxy_args,
1154 string_append(&config->proxy_args,
1161 if (acl_addr(vec[1], cur_acl->dst) < 0)
1163 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1164 "permit-access directive in configuration file: \"%s\"",
1166 string_append(&config->proxy_args,
1167 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1168 " in configuration file: \"");
1169 string_append(&config->proxy_args,
1171 string_append(&config->proxy_args,
1179 * Add it to the list. Note we reverse the list to get the
1180 * behaviour the user expects. With both the ACL and
1181 * actions file, the last match wins. However, the internal
1182 * implementations are different: The actions file is stored
1183 * in the same order as the file, and scanned completely.
1184 * With the ACL, we reverse the order as we load it, then
1185 * when we scan it we stop as soon as we get a match.
1187 cur_acl->next = config->acl;
1188 config->acl = cur_acl;
1191 #endif /* def FEATURE_ACL */
1193 /* *************************************************************************
1194 * proxy-info-url url
1195 * *************************************************************************/
1196 case hash_proxy_info_url :
1197 freez(config->proxy_info_url);
1198 config->proxy_info_url = strdup(arg);
1201 /* *************************************************************************
1202 * re_filterfile file-name
1203 * In confdir by default.
1204 * *************************************************************************/
1205 case hash_filterfile :
1206 freez(config->re_filterfile);
1207 config->re_filterfile = make_path(config->confdir, arg);
1210 /* *************************************************************************
1212 * *************************************************************************/
1213 case hash_single_threaded :
1214 config->multi_threaded = 0;
1217 /* *************************************************************************
1219 * *************************************************************************/
1220 #ifdef FEATURE_TOGGLE
1222 g_bToggleIJB = atoi(arg);
1224 #endif /* def FEATURE_TOGGLE */
1226 /* *************************************************************************
1227 * trust-info-url url
1228 * *************************************************************************/
1229 #ifdef FEATURE_TRUST
1230 case hash_trust_info_url :
1231 enlist(config->trust_info, arg);
1233 #endif /* def FEATURE_TRUST */
1235 /* *************************************************************************
1236 * trustfile filename
1237 * (In confdir by default.)
1238 * *************************************************************************/
1239 #ifdef FEATURE_TRUST
1240 case hash_trustfile :
1241 freez(config->trustfile);
1242 config->trustfile = make_path(config->confdir, arg);
1244 #endif /* def FEATURE_TRUST */
1246 /* *************************************************************************
1248 * *************************************************************************/
1249 case hash_usermanual :
1250 freez(config->usermanual);
1251 config->usermanual = strdup(arg);
1254 /* *************************************************************************
1255 * Win32 Console options:
1256 * *************************************************************************/
1258 /* *************************************************************************
1260 * *************************************************************************/
1262 case hash_hide_console :
1265 #endif /*def _WIN_CONSOLE*/
1268 /* *************************************************************************
1269 * Win32 GUI options:
1270 * *************************************************************************/
1272 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1273 /* *************************************************************************
1274 * activity-animation (0|1)
1275 * *************************************************************************/
1276 case hash_activity_animation :
1277 g_bShowActivityAnimation = atoi(arg);
1280 /* *************************************************************************
1281 * close-button-minimizes (0|1)
1282 * *************************************************************************/
1283 case hash_close_button_minimizes :
1284 g_bCloseHidesWindow = atoi(arg);
1287 /* *************************************************************************
1288 * log-buffer-size (0|1)
1289 * *************************************************************************/
1290 case hash_log_buffer_size :
1291 g_bLimitBufferSize = atoi(arg);
1294 /* *************************************************************************
1295 * log-font-name fontnane
1296 * *************************************************************************/
1297 case hash_log_font_name :
1298 strcpy( g_szFontFaceName, arg );
1301 /* *************************************************************************
1303 * *************************************************************************/
1304 case hash_log_font_size :
1305 g_nFontSize = atoi(arg);
1308 /* *************************************************************************
1309 * log-highlight-messages (0|1)
1310 * *************************************************************************/
1311 case hash_log_highlight_messages :
1312 g_bHighlightMessages = atoi(arg);
1315 /* *************************************************************************
1317 * *************************************************************************/
1318 case hash_log_max_lines :
1319 g_nMaxBufferLines = atoi(arg);
1322 /* *************************************************************************
1323 * log-messages (0|1)
1324 * *************************************************************************/
1325 case hash_log_messages :
1326 g_bLogMessages = atoi(arg);
1329 /* *************************************************************************
1330 * show-on-task-bar (0|1)
1331 * *************************************************************************/
1332 case hash_show_on_task_bar :
1333 g_bShowOnTaskBar = atoi(arg);
1336 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1339 /* *************************************************************************
1340 * Warnings about unsupported features
1341 * *************************************************************************/
1343 case hash_deny_access:
1344 #endif /* ndef FEATURE_ACL */
1345 #ifndef FEATURE_CGI_EDIT_ACTIONS
1346 case hash_enable_edit_actions:
1347 case hash_enable_remote_toggle:
1348 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1349 #ifndef FEATURE_COOKIE_JAR
1351 #endif /* ndef FEATURE_COOKIE_JAR */
1353 case hash_permit_access:
1354 #endif /* ndef FEATURE_ACL */
1355 #ifndef FEATURE_TOGGLE
1357 #endif /* ndef FEATURE_TOGGLE */
1358 #ifndef FEATURE_TRUST
1359 case hash_trustfile :
1360 case hash_trust_info_url :
1361 #endif /* ndef FEATURE_TRUST */
1363 #ifndef _WIN_CONSOLE
1364 case hash_hide_console :
1365 #endif /* ndef _WIN_CONSOLE */
1367 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1368 case hash_activity_animation :
1369 case hash_close_button_minimizes :
1370 case hash_log_buffer_size :
1371 case hash_log_font_name :
1372 case hash_log_font_size :
1373 case hash_log_highlight_messages :
1374 case hash_log_max_lines :
1375 case hash_log_messages :
1376 case hash_show_on_task_bar :
1377 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1378 /* These warnings are annoying - so hide them. -- Jon */
1379 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1382 /* *************************************************************************/
1384 /* *************************************************************************/
1386 * I decided that I liked this better as a warning than an
1387 * error. To change back to an error, just change log level
1388 * to LOG_LEVEL_FATAL.
1390 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1391 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1392 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1393 string_append(&config->proxy_args, buf);
1394 string_append(&config->proxy_args, "<br><br>\n");
1397 /* *************************************************************************/
1398 } /* end switch( hash_string(cmd) ) */
1399 } /* end while ( read_config_line(...) ) */
1403 if (NULL == config->proxy_args)
1405 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1408 init_error_log(Argv[0], config->logfile, config->debug);
1410 if (config->actions_file[0])
1412 add_loader(load_actions_file, config);
1415 if (config->re_filterfile)
1417 add_loader(load_re_filterfile, config);
1420 #ifdef FEATURE_TRUST
1421 if (config->trustfile)
1423 add_loader(load_trustfile, config);
1425 #endif /* def FEATURE_TRUST */
1427 #ifdef FEATURE_COOKIE_JAR
1428 if ( NULL != config->jarfile )
1430 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1432 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1433 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1435 setbuf(config->jar, NULL);
1437 #endif /* def FEATURE_COOKIE_JAR */
1439 if ( NULL == config->haddr )
1441 config->haddr = strdup( HADDR_DEFAULT );
1444 if ( NULL != config->haddr )
1446 if (NULL != (p = strchr(config->haddr, ':')))
1451 config->hport = atoi(p);
1455 if (config->hport <= 0)
1458 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1459 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1461 if (*config->haddr == '\0')
1463 config->haddr = NULL;
1468 * Want to run all the loaders once now.
1470 * Need to set up a fake csp, so they can get to the config.
1472 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1473 fake_csp->config = config;
1475 if (run_loader(fake_csp))
1478 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1479 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1483 /* FIXME: this is a kludge for win32 */
1484 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1486 g_default_actions_file = config->actions_file[1]; /* FIXME Hope this is default.action */
1487 g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1488 g_re_filterfile = config->re_filterfile;
1490 #ifdef FEATURE_TRUST
1491 g_trustfile = config->trustfile;
1492 #endif /* def FEATURE_TRUST */
1495 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1496 /* FIXME: end kludge */
1499 config->need_bind = 1;
1501 if (current_configfile)
1503 struct configuration_spec * oldcfg = (struct configuration_spec *)
1504 current_configfile->f;
1506 * Check if config->haddr,hport == oldcfg->haddr,hport
1508 * The following could be written more compactly as a single,
1509 * (unreadably long) if statement.
1511 config->need_bind = 0;
1512 if (config->hport != oldcfg->hport)
1514 config->need_bind = 1;
1516 else if (config->haddr == NULL)
1518 if (oldcfg->haddr != NULL)
1520 config->need_bind = 1;
1523 else if (oldcfg->haddr == NULL)
1525 config->need_bind = 1;
1527 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1529 config->need_bind = 1;
1532 current_configfile->unloader = unload_configfile;
1535 fs->next = files->next;
1538 current_configfile = fs;
1544 /*********************************************************************
1546 * Function : savearg
1548 * Description : Called from `load_config'. It saves each non-empty
1549 * and non-comment line from config into
1550 * config->proxy_args. This is used to create the
1551 * show-proxy-args page. On error, frees
1552 * config->proxy_args and sets it to NULL
1555 * 1 : command = config setting that was found
1556 * 2 : argument = the setting's argument (if any)
1557 * 3 : config = Configuration to save into.
1561 *********************************************************************/
1562 static void savearg(char *command, char *argument, struct configuration_spec * config)
1572 * Add config option name embedded in
1573 * link to it's section in the user-manual
1575 buf = strdup("<a href=\"");
1576 string_append(&buf, config->usermanual);
1577 string_append(&buf, CONFIG_HELP_PREFIX);
1578 string_join (&buf, string_toupper(command));
1579 string_append(&buf, "\">");
1580 string_append(&buf, command);
1581 string_append(&buf, "</a> ");
1585 freez(config->proxy_args);
1589 if ( (NULL != argument) && ('\0' != *argument) )
1591 s = html_encode(argument);
1595 freez(config->proxy_args);
1599 if (strncmpic(argument, "http://", 7) == 0)
1601 string_append(&buf, "<a href=\"");
1602 string_append(&buf, s);
1603 string_append(&buf, "\">");
1604 string_join (&buf, s);
1605 string_append(&buf, "</a>");
1609 string_join (&buf, s);
1613 string_append(&buf, "<br>\n");
1615 string_join(&config->proxy_args, buf);