1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 2.1 2002/06/04 17:22:36 jongfoster 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.1 2002/06/04 17:22:36 jongfoster
41 * Revision 2.0 2002/06/04 14:34:21 jongfoster
42 * Moving source files to src/
44 * Revision 1.48 2002/05/14 21:30:38 oes
45 * savearg now uses own linking code instead of (now special-cased) add_help_link
47 * Revision 1.47 2002/05/12 21:36:29 jongfoster
48 * Correcting function comments
50 * Revision 1.46 2002/04/26 12:55:14 oes
51 * - New option "user-manual", defaults to our site
52 * via project.h #define
53 * - savearg now embeds option names in help links
55 * Revision 1.45 2002/04/24 02:11:54 oes
56 * Jon's multiple AF patch: Allow up to MAX_ACTION_FILES actionsfile options
58 * Revision 1.44 2002/04/08 20:37:13 swa
61 * Revision 1.43 2002/04/08 20:36:50 swa
64 * Revision 1.42 2002/04/05 15:50:15 oes
65 * fix for invalid HTML proxy_args
67 * Revision 1.41 2002/03/31 17:19:00 jongfoster
68 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
70 * Revision 1.40 2002/03/26 22:29:55 swa
71 * we have a new homepage!
73 * Revision 1.39 2002/03/24 13:25:43 swa
74 * name change related issues
76 * Revision 1.38 2002/03/24 13:05:48 jongfoster
77 * Renaming re_filterfile to filterfile
79 * Revision 1.37 2002/03/16 23:54:06 jongfoster
80 * Adding graceful termination feature, to help look for memory leaks.
81 * If you enable this (which, by design, has to be done by hand
82 * editing config.h) and then go to http://i.j.b/die, then the program
83 * will exit cleanly after the *next* request. It should free all the
84 * memory that was used.
86 * Revision 1.36 2002/03/13 00:27:05 jongfoster
89 * Revision 1.35 2002/03/07 03:52:44 oes
90 * Set logging to tty for --no-daemon mode
92 * Revision 1.34 2002/03/06 23:14:35 jongfoster
93 * Trivial cosmetic changes to make function comments easier to find.
95 * Revision 1.33 2002/03/05 04:52:42 oes
96 * Deleted non-errlog debugging code
98 * Revision 1.32 2002/03/04 18:24:53 oes
99 * Re-enabled output of unknown config directive hash
101 * Revision 1.31 2002/03/03 15:07:20 oes
102 * Re-enabled automatic config reloading
104 * Revision 1.30 2002/01/22 23:31:43 jongfoster
105 * Replacing strsav() with string_append()
107 * Revision 1.29 2002/01/17 21:02:30 jongfoster
108 * Moving all our URL and URL pattern parsing code to urlmatch.c.
110 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
112 * Revision 1.28 2001/12/30 14:07:32 steudten
113 * - Add signal handling (unix)
114 * - Add SIGHUP handler (unix)
115 * - Add creation of pidfile (unix)
116 * - Add action 'top' in rc file (RH)
117 * - Add entry 'SIGNALS' to manpage
118 * - Add exit message to logfile (unix)
120 * Revision 1.27 2001/11/07 00:02:13 steudten
121 * Add line number in error output for lineparsing for
122 * actionsfile and configfile.
123 * Special handling for CLF added.
125 * Revision 1.26 2001/11/05 21:41:43 steudten
126 * Add changes to be a real daemon just for unix os.
127 * (change cwd to /, detach from controlling tty, set
128 * process group and session leader to the own process.
130 * Add some fatal-error log message for failed malloc().
131 * Add '-d' if compiled with 'configure --with-debug' to
132 * enable debug output.
134 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
135 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
136 * threads to call select() simultaneously. So, it's time to do a real, live,
137 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
138 * (native). Both versions will work, but using __OS2__ offers multi-threading.
140 * Revision 1.24 2001/10/23 21:40:30 jongfoster
141 * Added support for enable-edit-actions and enable-remote-toggle config
144 * Revision 1.23 2001/10/07 15:36:00 oes
145 * Introduced new config option "buffer-limit"
147 * Revision 1.22 2001/09/22 16:36:59 jongfoster
148 * Removing unused parameter fs from read_config_line()
150 * Revision 1.21 2001/09/16 17:10:43 jongfoster
151 * Moving function savearg() here, since it was the only thing left in
154 * Revision 1.20 2001/07/30 22:08:36 jongfoster
155 * Tidying up #defines:
156 * - All feature #defines are now of the form FEATURE_xxx
157 * - Permanently turned off WIN_GUI_EDIT
158 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
160 * Revision 1.19 2001/07/15 17:45:16 jongfoster
161 * Removing some unused #includes
163 * Revision 1.18 2001/07/13 14:01:14 oes
164 * - Removed all #ifdef PCRS
165 * - Removed vim-settings
167 * Revision 1.17 2001/06/29 13:31:03 oes
168 * - Improved comments
169 * - Fixed (actionsfile) and sorted hashes
170 * - Introduced admin_address and proxy-info-url
171 * as config parameters
172 * - Renamed config->proxy_args_invocation (which didn't have
173 * the invocation but the options!) to config->proxy_args
174 * - Various adaptions
175 * - Removed logentry from cancelled commit
177 * Revision 1.16 2001/06/09 10:55:28 jongfoster
178 * Changing BUFSIZ ==> BUFFER_SIZE
180 * Revision 1.15 2001/06/07 23:13:40 jongfoster
181 * Merging ACL and forward files into config file.
182 * Cosmetic: Sorting config file options alphabetically.
183 * Cosmetic: Adding brief syntax comments to config file options.
185 * Revision 1.14 2001/06/07 14:46:25 joergs
186 * Missing make_path() added for re_filterfile.
188 * Revision 1.13 2001/06/05 22:33:54 jongfoster
190 * Fixed minor memory leak.
191 * Also now uses make_path to prepend the pathnames.
193 * Revision 1.12 2001/06/05 20:04:09 jongfoster
194 * Now uses _snprintf() in place of snprintf() under Win32.
196 * Revision 1.11 2001/06/04 18:31:58 swa
197 * files are now prefixed with either `confdir' or `logdir'.
198 * `make redhat-dist' replaces both entries confdir and logdir
201 * Revision 1.10 2001/06/03 19:11:54 oes
202 * introduced confdir option
204 * Revision 1.9 2001/06/01 20:06:24 jongfoster
205 * Removed support for "tinygif" option - moved to actions file.
207 * Revision 1.8 2001/05/31 21:27:13 jongfoster
208 * Removed many options from the config file and into the
209 * "actions" file: add_forwarded, suppress_vanilla_wafer,
210 * wafer, add_header, user_agent, referer, from
211 * Also globally replaced "permission" with "action".
213 * Revision 1.7 2001/05/29 09:50:24 jongfoster
214 * Unified blocklist/imagelist/permissionslist.
215 * File format is still under discussion, but the internal changes
218 * Also modified interceptor behaviour:
219 * - We now intercept all URLs beginning with one of the following
220 * prefixes (and *only* these prefixes):
222 * * http://ijbswa.sf.net/config/
223 * * http://ijbswa.sourceforge.net/config/
224 * - New interceptors "home page" - go to http://i.j.b/ to see it.
225 * - Internal changes so that intercepted and fast redirect pages
226 * are not replaced with an image.
227 * - Interceptors now have the option to send a binary page direct
228 * to the client. (i.e. ijb-send-banner uses this)
229 * - Implemented show-url-info interceptor. (Which is why I needed
230 * the above interceptors changes - a typical URL is
231 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
232 * The previous mechanism would not have intercepted that, and
233 * if it had been intercepted then it then it would have replaced
236 * Revision 1.6 2001/05/26 00:28:36 jongfoster
237 * Automatic reloading of config file.
238 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
239 * Most of the global variables have been moved to a new
240 * struct configuration_spec, accessed through csp->config->globalname
241 * Most of the globals remaining are used by the Win32 GUI.
243 * Revision 1.5 2001/05/25 22:34:30 jongfoster
246 * Revision 1.4 2001/05/22 18:46:04 oes
248 * - Enabled filtering banners by size rather than URL
249 * by adding patterns that replace all standard banner
250 * sizes with the "Junkbuster" gif to the re_filterfile
252 * - Enabled filtering WebBugs by providing a pattern
253 * which kills all 1x1 images
255 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
256 * which is selected by the (nonstandard and therefore
257 * capital) letter 'U' in the option string.
258 * It causes the quantifiers to be ungreedy by default.
259 * Appending a ? turns back to greedy (!).
261 * - Added a new interceptor ijb-send-banner, which
262 * sends back the "Junkbuster" gif. Without imagelist or
263 * MSIE detection support, or if tinygif = 1, or the
264 * URL isn't recognized as an imageurl, a lame HTML
265 * explanation is sent instead.
267 * - Added new feature, which permits blocking remote
268 * script redirects and firing back a local redirect
270 * The feature is conditionally compiled, i.e. it
271 * can be disabled with --disable-fast-redirects,
272 * plus it must be activated by a "fast-redirects"
273 * line in the config file, has its own log level
274 * and of course wants to be displayed by show-proxy-args
275 * Note: Boy, all the #ifdefs in 1001 locations and
276 * all the fumbling with configure.in and acconfig.h
277 * were *way* more work than the feature itself :-(
279 * - Because a generic redirect template was needed for
280 * this, tinygif = 3 now uses the same.
282 * - Moved GIFs, and other static HTTP response templates
287 * - Removed some >400 CRs again (Jon, you really worked
290 * Revision 1.3 2001/05/20 01:21:20 jongfoster
291 * Version 2.9.4 checkin.
292 * - Merged popupfile and cookiefile, and added control over PCRS
293 * filtering, in new "permissionsfile".
294 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
295 * file error you now get a message box (in the Win32 GUI) rather
296 * than the program exiting with no explanation.
297 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
299 * - Removed tabs from "config"
300 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
301 * - Bumped up version number.
303 * Revision 1.2 2001/05/17 23:01:01 oes
304 * - Cleaned CRLF's from the sources and related files
306 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
307 * Initial import of version 2.9.3 source tree
310 *********************************************************************/
316 #include <sys/types.h>
330 # include <windows.h>
333 # ifndef _WIN_CONSOLE
335 # endif /* ndef _WIN_CONSOLE */
337 /* VC++ has "_snprintf", not "snprintf" */
338 #define snprintf _snprintf
340 #else /* ifndef _WIN32 */
344 # include <sys/wait.h>
346 # include <sys/time.h>
347 # include <sys/stat.h>
357 #include "miscutil.h"
361 #include "urlmatch.h"
364 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
367 * Fix a problem with Solaris. There should be no effect on other
369 * Solaris's isspace() is a macro which uses it's argument directly
370 * as an array index. Therefore we need to make sure that high-bit
371 * characters generate +ve values, and ideally we also want to make
372 * the argument match the declared parameter type of "int".
374 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
375 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
377 #ifdef FEATURE_TOGGLE
379 * Indicates if Privoxy is currently enabled.
381 int g_bToggleIJB = 1; /* Privoxy is enabled by default. */
382 #endif /* def FEATURE_TOGGLE */
385 * The filename of the config file
387 const char *configfile = NULL;
390 * Global argument count, so that CGI functions can access the
396 * Global argument list, so that CGI functions can access the
399 const char **Argv = NULL;
402 * The configuration file which is currently loaded.
404 static struct file_list *current_configfile = NULL;
408 * This takes the "cryptic" hash of each keyword and aliases them to
409 * something a little more readable. This also makes changing the
410 * hash values easier if they should change or the hash algorthm changes.
411 * Use the included "hash" program to find out what the hash will be
412 * for any string supplied on the command line. (Or just put it in the
413 * config file and read the number from the error message in the log).
415 * Please keep this list sorted alphabetically (but with the Windows
416 * console and GUI specific options last).
419 #define hash_actions_file 1196306641ul /**< "actionsfile" */
420 #define hash_admin_address 4112573064ul /**< "admin-address" */
421 #define hash_buffer_limit 1881726070ul /**< "buffer-limit */
422 #define hash_confdir 1978389ul /**< "confdir" */
423 #define hash_debug 78263ul /**< "debug" */
424 #define hash_deny_access 1227333715ul /**< "deny-access" */
425 #define hash_enable_edit_actions 2517097536ul /**< "enable-edit-actions" */
426 #define hash_enable_remote_toggle 2979744683ul /**< "enable-remote-toggle" */
427 #define hash_filterfile 250887266ul /**< "filterfile" */
428 #define hash_forward 2029845ul /**< "forward" */
429 #define hash_forward_socks4 3963965521ul /**< "forward-socks4" */
430 #define hash_forward_socks4a 2639958518ul /**< "forward-socks4a" */
431 #define hash_jarfile 2046641ul /**< "jarfile" */
432 #define hash_listen_address 1255650842ul /**< "listen-address" */
433 #define hash_logdir 422889ul /**< "logdir" */
434 #define hash_logfile 2114766ul /**< "logfile" */
435 #define hash_permit_access 3587953268ul /**< "permit-access" */
436 #define hash_proxy_info_url 3903079059ul /**< "proxy-info-url" */
437 #define hash_single_threaded 4250084780ul /**< "single-threaded" */
438 #define hash_suppress_blocklists 1948693308ul /**< "suppress-blocklists" */
439 #define hash_toggle 447966ul /**< "toggle" */
440 #define hash_trust_info_url 430331967ul /**< "trust-info-url" */
441 #define hash_trustfile 56494766ul /**< "trustfile" */
442 #define hash_usermanual 1416668518ul /**< "user-manual" */
444 #define hash_activity_animation 1817904738ul /**< "activity-animation" */
445 #define hash_close_button_minimizes 3651284693ul /**< "close-button-minimizes" */
446 #define hash_hide_console 2048809870ul /**< "hide-console" */
447 #define hash_log_buffer_size 2918070425ul /**< "log-buffer-size" */
448 #define hash_log_font_name 2866730124ul /**< "log-font-name" */
449 #define hash_log_font_size 2866731014ul /**< "log-font-size" */
450 #define hash_log_highlight_messages 4032101240ul /**< "log-highlight-messages" */
451 #define hash_log_max_lines 2868344173ul /**< "log-max-lines" */
452 #define hash_log_messages 2291744899ul /**< "log-messages" */
453 #define hash_show_on_task_bar 215410365ul /**< "show-on-task-bar" */
456 static void savearg(char *command, char *argument, struct configuration_spec * config);
458 /*********************************************************************
460 * Function : unload_configfile
462 * Description : Free the config structure and all components.
465 * 1 : data: struct configuration_spec to unload
469 *********************************************************************/
470 void unload_configfile (void * data)
472 struct configuration_spec * config = (struct configuration_spec *)data;
473 struct forward_spec *cur_fwd = config->forward;
475 struct access_control_list *cur_acl = config->acl;
478 while (cur_acl != NULL)
480 struct access_control_list * next_acl = cur_acl->next;
485 #endif /* def FEATURE_ACL */
487 while (cur_fwd != NULL)
489 struct forward_spec * next_fwd = cur_fwd->next;
490 free_url_spec(cur_fwd->url);
492 freez(cur_fwd->gateway_host);
493 freez(cur_fwd->forward_host);
497 config->forward = NULL;
499 #ifdef FEATURE_COOKIE_JAR
500 if ( NULL != config->jar )
502 fclose( config->jar );
505 #endif /* def FEATURE_COOKIE_JAR */
507 freez(config->confdir);
508 freez(config->logdir);
510 freez(config->haddr);
511 freez(config->logfile);
513 for (i = 0; i < MAX_ACTION_FILES; i++)
515 freez(config->actions_file_short[i]);
516 freez(config->actions_file[i]);
519 freez(config->admin_address);
520 freez(config->proxy_info_url);
521 freez(config->proxy_args);
522 freez(config->usermanual);
524 #ifdef FEATURE_COOKIE_JAR
525 freez(config->jarfile);
526 #endif /* def FEATURE_COOKIE_JAR */
528 freez(config->re_filterfile);
533 #ifdef FEATURE_GRACEFUL_TERMINATION
534 /*********************************************************************
536 * Function : unload_current_config_file
538 * Description : Unloads current config file - reset to state at
539 * beginning of program.
545 *********************************************************************/
546 void unload_current_config_file(void)
548 if (current_configfile)
550 current_configfile->unloader = unload_configfile;
551 current_configfile = NULL;
557 /*********************************************************************
559 * Function : load_config
561 * Description : Load the config file and all parameters.
565 * Returns : The configuration_spec, or NULL on error.
567 *********************************************************************/
568 struct configuration_spec * load_config(void)
570 char buf[BUFFER_SIZE];
572 FILE *configfp = NULL;
573 struct configuration_spec * config = NULL;
574 struct client_state * fake_csp;
575 struct file_list *fs;
576 unsigned long linenum = 0;
579 if ( !check_file_changed(current_configfile, configfile, &fs))
581 /* No need to load */
582 return ((struct configuration_spec *)current_configfile->f);
586 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
590 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
592 #ifdef FEATURE_TOGGLE
594 #endif /* def FEATURE_TOGGLE */
596 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
602 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
603 /* Never get here - LOG_LEVEL_FATAL causes program exit */
607 * This is backwards from how it's usually done.
608 * Following the usual pattern, "fs" would be stored in a member
609 * variable in "csp", and then we'd access "config" from "fs->f",
610 * using a cast. However, "config" is used so often that a
611 * cast each time would be very ugly, and the extra indirection
612 * would waste CPU cycles. Therefore we store "config" in
613 * "csp->config", and "fs" in "csp->config->config_file_list".
615 config->config_file_list = fs;
620 config->multi_threaded = 1;
621 config->hport = HADDR_PORT;
622 config->buffer_limit = 4096 * 1024;
623 config->usermanual = strdup(USER_MANUAL_URL);
624 config->proxy_args = strdup("");
626 if ((configfp = fopen(configfile, "r")) == NULL)
628 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
630 /* Never get here - LOG_LEVEL_FATAL causes program exit */
633 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
635 char cmd[BUFFER_SIZE];
636 char arg[BUFFER_SIZE];
637 char tmp[BUFFER_SIZE];
639 struct access_control_list *cur_acl;
640 #endif /* def FEATURE_ACL */
641 struct forward_spec *cur_fwd;
647 /* Copy command (i.e. up to space or tab) into cmd */
650 while (*p && (*p != ' ') && (*p != '\t'))
656 /* Skip over the whitespace in buf */
657 while (*p && ((*p == ' ') || (*p == '\t')))
662 /* Copy the argument into arg */
665 /* Should never happen, but check this anyway */
671 /* Make sure the command field is lower case */
676 *p = ijb_tolower(*p);
680 /* Save the argument for show-proxy-args */
681 savearg(cmd, arg, config);
684 switch( hash_string( cmd ) )
686 /* *************************************************************************
687 * actionsfile actions-file-name
688 * In confdir by default
689 * *************************************************************************/
690 case hash_actions_file :
692 while ((i < MAX_ACTION_FILES) && (NULL != config->actions_file[i]))
697 if (i >= MAX_ACTION_FILES)
699 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
700 "(You can increase this limit by changing MAX_ACTION_FILES in project.h and recompiling).",
703 config->actions_file_short[i] = strdup(arg);
704 p = malloc(strlen(arg) + sizeof(".action"));
707 log_error(LOG_LEVEL_FATAL, "Out of memory");
710 strcat(p, ".action");
711 config->actions_file[i] = make_path(config->confdir, p);
715 /* *************************************************************************
716 * admin-address email-address
717 * *************************************************************************/
718 case hash_admin_address :
719 freez(config->admin_address);
720 config->admin_address = strdup(arg);
723 /* *************************************************************************
725 * *************************************************************************/
726 case hash_buffer_limit :
727 config->buffer_limit = (size_t) 1024 * atoi(arg);
730 /* *************************************************************************
731 * confdir directory-name
732 * *************************************************************************/
734 freez(config->confdir);
735 config->confdir = make_path( NULL, arg);
738 /* *************************************************************************
740 * Specifies debug level, multiple values are ORed together.
741 * *************************************************************************/
743 config->debug |= atoi(arg);
746 /* *************************************************************************
747 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
748 * *************************************************************************/
750 case hash_deny_access:
751 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
753 if ((vec_count != 1) && (vec_count != 2))
755 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
756 "deny-access directive in configuration file.");
757 string_append(&config->proxy_args,
758 "<br>\nWARNING: Wrong number of parameters for "
759 "deny-access directive in configuration file.<br><br>\n");
763 /* allocate a new node */
764 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
768 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
769 /* Never get here - LOG_LEVEL_FATAL causes program exit */
772 cur_acl->action = ACL_DENY;
774 if (acl_addr(vec[0], cur_acl->src) < 0)
776 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
777 "directive in configuration file: \"%s\"", vec[0]);
778 string_append(&config->proxy_args,
779 "<br>\nWARNING: Invalid source IP for deny-access directive"
780 " in configuration file: \"");
781 string_append(&config->proxy_args,
783 string_append(&config->proxy_args,
790 if (acl_addr(vec[1], cur_acl->dst) < 0)
792 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
793 "directive in configuration file: \"%s\"", vec[0]);
794 string_append(&config->proxy_args,
795 "<br>\nWARNING: Invalid destination IP for deny-access directive"
796 " in configuration file: \"");
797 string_append(&config->proxy_args,
799 string_append(&config->proxy_args,
807 * Add it to the list. Note we reverse the list to get the
808 * behaviour the user expects. With both the ACL and
809 * actions file, the last match wins. However, the internal
810 * implementations are different: The actions file is stored
811 * in the same order as the file, and scanned completely.
812 * With the ACL, we reverse the order as we load it, then
813 * when we scan it we stop as soon as we get a match.
815 cur_acl->next = config->acl;
816 config->acl = cur_acl;
819 #endif /* def FEATURE_ACL */
821 /* *************************************************************************
822 * enable-edit-actions 0|1
823 * *************************************************************************/
824 #ifdef FEATURE_CGI_EDIT_ACTIONS
825 case hash_enable_edit_actions:
826 if ((*arg != '\0') && (0 != atoi(arg)))
828 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
832 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
835 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
837 /* *************************************************************************
838 * enable-remote-toggle 0|1
839 * *************************************************************************/
840 #ifdef FEATURE_CGI_EDIT_ACTIONS
841 case hash_enable_remote_toggle:
842 if ((*arg != '\0') && (0 != atoi(arg)))
844 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
848 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
851 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
853 /* *************************************************************************
854 * forward url-pattern (.|http-proxy-host[:port])
855 * *************************************************************************/
857 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
861 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
862 "directive in configuration file.");
863 string_append(&config->proxy_args,
864 "<br>\nWARNING: Wrong number of parameters for "
865 "forward directive in configuration file.");
869 /* allocate a new node */
870 cur_fwd = zalloc(sizeof(*cur_fwd));
873 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
874 /* Never get here - LOG_LEVEL_FATAL causes program exit */
878 cur_fwd->type = SOCKS_NONE;
880 /* Save the URL pattern */
881 if (create_url_spec(cur_fwd->url, vec[0]))
883 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
884 "directive in configuration file.");
885 string_append(&config->proxy_args,
886 "<br>\nWARNING: Bad URL specifier for "
887 "forward directive in configuration file.");
891 /* Parse the parent HTTP proxy host:port */
894 if (strcmp(p, ".") != 0)
896 cur_fwd->forward_host = strdup(p);
898 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
901 cur_fwd->forward_port = atoi(p);
904 if (cur_fwd->forward_port <= 0)
906 cur_fwd->forward_port = 8000;
911 cur_fwd->next = config->forward;
912 config->forward = cur_fwd;
916 /* *************************************************************************
917 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
918 * *************************************************************************/
919 case hash_forward_socks4:
920 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
924 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
925 "forward-socks4 directive in configuration file.");
926 string_append(&config->proxy_args,
927 "<br>\nWARNING: Wrong number of parameters for "
928 "forward-socks4 directive in configuration file.");
932 /* allocate a new node */
933 cur_fwd = zalloc(sizeof(*cur_fwd));
936 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
937 /* Never get here - LOG_LEVEL_FATAL causes program exit */
941 cur_fwd->type = SOCKS_4;
943 /* Save the URL pattern */
944 if (create_url_spec(cur_fwd->url, vec[0]))
946 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
947 "directive in configuration file.");
948 string_append(&config->proxy_args,
949 "<br>\nWARNING: Bad URL specifier for "
950 "forward-socks4 directive in configuration file.");
954 /* Parse the SOCKS proxy host[:port] */
957 if (strcmp(p, ".") != 0)
959 cur_fwd->gateway_host = strdup(p);
961 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
964 cur_fwd->gateway_port = atoi(p);
966 if (cur_fwd->gateway_port <= 0)
968 cur_fwd->gateway_port = 1080;
972 /* Parse the parent HTTP proxy host[:port] */
975 if (strcmp(p, ".") != 0)
977 cur_fwd->forward_host = strdup(p);
979 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
982 cur_fwd->forward_port = atoi(p);
985 if (cur_fwd->forward_port <= 0)
987 cur_fwd->forward_port = 8000;
992 cur_fwd->next = config->forward;
993 config->forward = cur_fwd;
997 /* *************************************************************************
998 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
999 * *************************************************************************/
1000 case hash_forward_socks4a:
1001 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1005 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1006 "forward-socks4a directive in configuration file.");
1007 string_append(&config->proxy_args,
1008 "<br>\nWARNING: Wrong number of parameters for "
1009 "forward-socks4a directive in configuration file.");
1013 /* allocate a new node */
1014 cur_fwd = zalloc(sizeof(*cur_fwd));
1015 if (cur_fwd == NULL)
1017 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1018 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1022 cur_fwd->type = SOCKS_4A;
1024 /* Save the URL pattern */
1025 if (create_url_spec(cur_fwd->url, vec[0]))
1027 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
1028 "directive in configuration file.");
1029 string_append(&config->proxy_args,
1030 "<br>\nWARNING: Bad URL specifier for "
1031 "forward-socks4a directive in configuration file.");
1035 /* Parse the SOCKS proxy host[:port] */
1038 cur_fwd->gateway_host = strdup(p);
1040 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1043 cur_fwd->gateway_port = atoi(p);
1045 if (cur_fwd->gateway_port <= 0)
1047 cur_fwd->gateway_port = 1080;
1050 /* Parse the parent HTTP proxy host[:port] */
1053 if (strcmp(p, ".") != 0)
1055 cur_fwd->forward_host = strdup(p);
1057 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1060 cur_fwd->forward_port = atoi(p);
1063 if (cur_fwd->forward_port <= 0)
1065 cur_fwd->forward_port = 8000;
1070 cur_fwd->next = config->forward;
1071 config->forward = cur_fwd;
1075 /* *************************************************************************
1076 * jarfile jar-file-name
1077 * In logdir by default
1078 * *************************************************************************/
1079 #ifdef FEATURE_COOKIE_JAR
1081 freez(config->jarfile);
1082 config->jarfile = make_path(config->logdir, arg);
1084 #endif /* def FEATURE_COOKIE_JAR */
1086 /* *************************************************************************
1087 * listen-address [ip][:port]
1088 * *************************************************************************/
1089 case hash_listen_address :
1090 freez(config->haddr);
1091 config->haddr = strdup(arg);
1094 /* *************************************************************************
1095 * logdir directory-name
1096 * *************************************************************************/
1098 freez(config->logdir);
1099 config->logdir = make_path(NULL, arg);
1102 /* *************************************************************************
1103 * logfile log-file-name
1104 * In logdir by default
1105 * *************************************************************************/
1107 freez(config->logfile);
1108 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1111 /* *************************************************************************
1112 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1113 * *************************************************************************/
1115 case hash_permit_access:
1116 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1118 if ((vec_count != 1) && (vec_count != 2))
1120 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1121 "permit-access directive in configuration file.");
1122 string_append(&config->proxy_args,
1123 "<br>\nWARNING: Wrong number of parameters for "
1124 "permit-access directive in configuration file.<br><br>\n");
1129 /* allocate a new node */
1130 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1132 if (cur_acl == NULL)
1134 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1135 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1138 cur_acl->action = ACL_PERMIT;
1140 if (acl_addr(vec[0], cur_acl->src) < 0)
1142 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1143 "directive in configuration file: \"%s\"", vec[0]);
1144 string_append(&config->proxy_args,
1145 "<br>\nWARNING: Invalid source IP for permit-access directive"
1146 " in configuration file: \"");
1147 string_append(&config->proxy_args,
1149 string_append(&config->proxy_args,
1156 if (acl_addr(vec[1], cur_acl->dst) < 0)
1158 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1159 "permit-access directive in configuration file: \"%s\"",
1161 string_append(&config->proxy_args,
1162 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1163 " in configuration file: \"");
1164 string_append(&config->proxy_args,
1166 string_append(&config->proxy_args,
1174 * Add it to the list. Note we reverse the list to get the
1175 * behaviour the user expects. With both the ACL and
1176 * actions file, the last match wins. However, the internal
1177 * implementations are different: The actions file is stored
1178 * in the same order as the file, and scanned completely.
1179 * With the ACL, we reverse the order as we load it, then
1180 * when we scan it we stop as soon as we get a match.
1182 cur_acl->next = config->acl;
1183 config->acl = cur_acl;
1186 #endif /* def FEATURE_ACL */
1188 /* *************************************************************************
1189 * proxy-info-url url
1190 * *************************************************************************/
1191 case hash_proxy_info_url :
1192 freez(config->proxy_info_url);
1193 config->proxy_info_url = strdup(arg);
1196 /* *************************************************************************
1197 * re_filterfile file-name
1198 * In confdir by default.
1199 * *************************************************************************/
1200 case hash_filterfile :
1201 freez(config->re_filterfile);
1202 config->re_filterfile = make_path(config->confdir, arg);
1205 /* *************************************************************************
1207 * *************************************************************************/
1208 case hash_single_threaded :
1209 config->multi_threaded = 0;
1212 /* *************************************************************************
1214 * *************************************************************************/
1215 #ifdef FEATURE_TOGGLE
1217 g_bToggleIJB = atoi(arg);
1219 #endif /* def FEATURE_TOGGLE */
1221 /* *************************************************************************
1222 * trust-info-url url
1223 * *************************************************************************/
1224 #ifdef FEATURE_TRUST
1225 case hash_trust_info_url :
1226 enlist(config->trust_info, arg);
1228 #endif /* def FEATURE_TRUST */
1230 /* *************************************************************************
1231 * trustfile filename
1232 * (In confdir by default.)
1233 * *************************************************************************/
1234 #ifdef FEATURE_TRUST
1235 case hash_trustfile :
1236 freez(config->trustfile);
1237 config->trustfile = make_path(config->confdir, arg);
1239 #endif /* def FEATURE_TRUST */
1241 /* *************************************************************************
1243 * *************************************************************************/
1244 case hash_usermanual :
1245 freez(config->usermanual);
1246 config->usermanual = strdup(arg);
1249 /* *************************************************************************
1250 * Win32 Console options:
1251 * *************************************************************************/
1253 /* *************************************************************************
1255 * *************************************************************************/
1257 case hash_hide_console :
1260 #endif /*def _WIN_CONSOLE*/
1263 /* *************************************************************************
1264 * Win32 GUI options:
1265 * *************************************************************************/
1267 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1268 /* *************************************************************************
1269 * activity-animation (0|1)
1270 * *************************************************************************/
1271 case hash_activity_animation :
1272 g_bShowActivityAnimation = atoi(arg);
1275 /* *************************************************************************
1276 * close-button-minimizes (0|1)
1277 * *************************************************************************/
1278 case hash_close_button_minimizes :
1279 g_bCloseHidesWindow = atoi(arg);
1282 /* *************************************************************************
1283 * log-buffer-size (0|1)
1284 * *************************************************************************/
1285 case hash_log_buffer_size :
1286 g_bLimitBufferSize = atoi(arg);
1289 /* *************************************************************************
1290 * log-font-name fontnane
1291 * *************************************************************************/
1292 case hash_log_font_name :
1293 strcpy( g_szFontFaceName, arg );
1296 /* *************************************************************************
1298 * *************************************************************************/
1299 case hash_log_font_size :
1300 g_nFontSize = atoi(arg);
1303 /* *************************************************************************
1304 * log-highlight-messages (0|1)
1305 * *************************************************************************/
1306 case hash_log_highlight_messages :
1307 g_bHighlightMessages = atoi(arg);
1310 /* *************************************************************************
1312 * *************************************************************************/
1313 case hash_log_max_lines :
1314 g_nMaxBufferLines = atoi(arg);
1317 /* *************************************************************************
1318 * log-messages (0|1)
1319 * *************************************************************************/
1320 case hash_log_messages :
1321 g_bLogMessages = atoi(arg);
1324 /* *************************************************************************
1325 * show-on-task-bar (0|1)
1326 * *************************************************************************/
1327 case hash_show_on_task_bar :
1328 g_bShowOnTaskBar = atoi(arg);
1331 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1334 /* *************************************************************************
1335 * Warnings about unsupported features
1336 * *************************************************************************/
1338 case hash_deny_access:
1339 #endif /* ndef FEATURE_ACL */
1340 #ifndef FEATURE_CGI_EDIT_ACTIONS
1341 case hash_enable_edit_actions:
1342 case hash_enable_remote_toggle:
1343 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1344 #ifndef FEATURE_COOKIE_JAR
1346 #endif /* ndef FEATURE_COOKIE_JAR */
1348 case hash_permit_access:
1349 #endif /* ndef FEATURE_ACL */
1350 #ifndef FEATURE_TOGGLE
1352 #endif /* ndef FEATURE_TOGGLE */
1353 #ifndef FEATURE_TRUST
1354 case hash_trustfile :
1355 case hash_trust_info_url :
1356 #endif /* ndef FEATURE_TRUST */
1358 #ifndef _WIN_CONSOLE
1359 case hash_hide_console :
1360 #endif /* ndef _WIN_CONSOLE */
1362 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1363 case hash_activity_animation :
1364 case hash_close_button_minimizes :
1365 case hash_log_buffer_size :
1366 case hash_log_font_name :
1367 case hash_log_font_size :
1368 case hash_log_highlight_messages :
1369 case hash_log_max_lines :
1370 case hash_log_messages :
1371 case hash_show_on_task_bar :
1372 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1373 /* These warnings are annoying - so hide them. -- Jon */
1374 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1377 /* *************************************************************************/
1379 /* *************************************************************************/
1381 * I decided that I liked this better as a warning than an
1382 * error. To change back to an error, just change log level
1383 * to LOG_LEVEL_FATAL.
1385 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1386 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1387 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1388 string_append(&config->proxy_args, buf);
1389 string_append(&config->proxy_args, "<br><br>\n");
1392 /* *************************************************************************/
1393 } /* end switch( hash_string(cmd) ) */
1394 } /* end while ( read_config_line(...) ) */
1398 if (NULL == config->proxy_args)
1400 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1403 init_error_log(Argv[0], config->logfile, config->debug);
1405 if (config->actions_file[0])
1407 add_loader(load_actions_file, config);
1410 if (config->re_filterfile)
1412 add_loader(load_re_filterfile, config);
1415 #ifdef FEATURE_TRUST
1416 if (config->trustfile)
1418 add_loader(load_trustfile, config);
1420 #endif /* def FEATURE_TRUST */
1422 #ifdef FEATURE_COOKIE_JAR
1423 if ( NULL != config->jarfile )
1425 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1427 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1428 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1430 setbuf(config->jar, NULL);
1432 #endif /* def FEATURE_COOKIE_JAR */
1434 if ( NULL == config->haddr )
1436 config->haddr = strdup( HADDR_DEFAULT );
1439 if ( NULL != config->haddr )
1441 if (NULL != (p = strchr(config->haddr, ':')))
1446 config->hport = atoi(p);
1450 if (config->hport <= 0)
1453 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1454 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1456 if (*config->haddr == '\0')
1458 config->haddr = NULL;
1463 * Want to run all the loaders once now.
1465 * Need to set up a fake csp, so they can get to the config.
1467 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1468 fake_csp->config = config;
1470 if (run_loader(fake_csp))
1473 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1474 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1478 /* FIXME: this is a kludge for win32 */
1479 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1481 g_default_actions_file = config->actions_file[1]; /* FIXME Hope this is default.action */
1482 g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1483 g_re_filterfile = config->re_filterfile;
1485 #ifdef FEATURE_TRUST
1486 g_trustfile = config->trustfile;
1487 #endif /* def FEATURE_TRUST */
1490 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1491 /* FIXME: end kludge */
1494 config->need_bind = 1;
1496 if (current_configfile)
1498 struct configuration_spec * oldcfg = (struct configuration_spec *)
1499 current_configfile->f;
1501 * Check if config->haddr,hport == oldcfg->haddr,hport
1503 * The following could be written more compactly as a single,
1504 * (unreadably long) if statement.
1506 config->need_bind = 0;
1507 if (config->hport != oldcfg->hport)
1509 config->need_bind = 1;
1511 else if (config->haddr == NULL)
1513 if (oldcfg->haddr != NULL)
1515 config->need_bind = 1;
1518 else if (oldcfg->haddr == NULL)
1520 config->need_bind = 1;
1522 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1524 config->need_bind = 1;
1527 current_configfile->unloader = unload_configfile;
1530 fs->next = files->next;
1533 current_configfile = fs;
1539 /*********************************************************************
1541 * Function : savearg
1543 * Description : Called from `load_config'. It saves each non-empty
1544 * and non-comment line from config into
1545 * config->proxy_args. This is used to create the
1546 * show-proxy-args page. On error, frees
1547 * config->proxy_args and sets it to NULL
1550 * 1 : command = config setting that was found
1551 * 2 : argument = the setting's argument (if any)
1552 * 3 : config = Configuration to save into.
1556 *********************************************************************/
1557 static void savearg(char *command, char *argument, struct configuration_spec * config)
1567 * Add config option name embedded in
1568 * link to it's section in the user-manual
1570 buf = strdup("<a href=\"");
1571 string_append(&buf, config->usermanual);
1572 string_append(&buf, CONFIG_HELP_PREFIX);
1573 string_join (&buf, string_toupper(command));
1574 string_append(&buf, "\">");
1575 string_append(&buf, command);
1576 string_append(&buf, "</a> ");
1580 freez(config->proxy_args);
1584 if ( (NULL != argument) && ('\0' != *argument) )
1586 s = html_encode(argument);
1590 freez(config->proxy_args);
1594 if (strncmpic(argument, "http://", 7) == 0)
1596 string_append(&buf, "<a href=\"");
1597 string_append(&buf, s);
1598 string_append(&buf, "\">");
1599 string_join (&buf, s);
1600 string_append(&buf, "</a>");
1604 string_join (&buf, s);
1608 string_append(&buf, "<br>\n");
1610 string_join(&config->proxy_args, buf);