1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.48.2.1 2002/08/21 17:58:05 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/Attic/loadcfg.c,v $
6 * Purpose : Loads settings from the configuration file into
7 * global variables. This file contains both the
8 * routine to load the configuration and the global
9 * variables it writes to.
11 * Copyright : Written by and Copyright (C) 2001 the SourceForge
12 * Privoxy team. http://www.privoxy.org/
14 * Based on the Internet Junkbuster originally written
15 * by and Copyright (C) 1997 Anonymous Coders and
16 * Junkbusters Corporation. http://www.junkbusters.com
18 * This program is free software; you can redistribute it
19 * and/or modify it under the terms of the GNU General
20 * Public License as published by the Free Software
21 * Foundation; either version 2 of the License, or (at
22 * your option) any later version.
24 * This program is distributed in the hope that it will
25 * be useful, but WITHOUT ANY WARRANTY; without even the
26 * implied warranty of MERCHANTABILITY or FITNESS FOR A
27 * PARTICULAR PURPOSE. See the GNU General Public
28 * License for more details.
30 * The GNU General Public License should be included with
31 * this file. If not, you can view it at
32 * http://www.gnu.org/copyleft/gpl.html
33 * or write to the Free Software Foundation, Inc., 59
34 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 * Revision 1.48.2.1 2002/08/21 17:58:05 oes
39 * Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
41 * Revision 1.48 2002/05/14 21:30:38 oes
42 * savearg now uses own linking code instead of (now special-cased) add_help_link
44 * Revision 1.47 2002/05/12 21:36:29 jongfoster
45 * Correcting function comments
47 * Revision 1.46 2002/04/26 12:55:14 oes
48 * - New option "user-manual", defaults to our site
49 * via project.h #define
50 * - savearg now embeds option names in help links
52 * Revision 1.45 2002/04/24 02:11:54 oes
53 * Jon's multiple AF patch: Allow up to MAX_ACTION_FILES actionsfile options
55 * Revision 1.44 2002/04/08 20:37:13 swa
58 * Revision 1.43 2002/04/08 20:36:50 swa
61 * Revision 1.42 2002/04/05 15:50:15 oes
62 * fix for invalid HTML proxy_args
64 * Revision 1.41 2002/03/31 17:19:00 jongfoster
65 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
67 * Revision 1.40 2002/03/26 22:29:55 swa
68 * we have a new homepage!
70 * Revision 1.39 2002/03/24 13:25:43 swa
71 * name change related issues
73 * Revision 1.38 2002/03/24 13:05:48 jongfoster
74 * Renaming re_filterfile to filterfile
76 * Revision 1.37 2002/03/16 23:54:06 jongfoster
77 * Adding graceful termination feature, to help look for memory leaks.
78 * If you enable this (which, by design, has to be done by hand
79 * editing config.h) and then go to http://i.j.b/die, then the program
80 * will exit cleanly after the *next* request. It should free all the
81 * memory that was used.
83 * Revision 1.36 2002/03/13 00:27:05 jongfoster
86 * Revision 1.35 2002/03/07 03:52:44 oes
87 * Set logging to tty for --no-daemon mode
89 * Revision 1.34 2002/03/06 23:14:35 jongfoster
90 * Trivial cosmetic changes to make function comments easier to find.
92 * Revision 1.33 2002/03/05 04:52:42 oes
93 * Deleted non-errlog debugging code
95 * Revision 1.32 2002/03/04 18:24:53 oes
96 * Re-enabled output of unknown config directive hash
98 * Revision 1.31 2002/03/03 15:07:20 oes
99 * Re-enabled automatic config reloading
101 * Revision 1.30 2002/01/22 23:31:43 jongfoster
102 * Replacing strsav() with string_append()
104 * Revision 1.29 2002/01/17 21:02:30 jongfoster
105 * Moving all our URL and URL pattern parsing code to urlmatch.c.
107 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
109 * Revision 1.28 2001/12/30 14:07:32 steudten
110 * - Add signal handling (unix)
111 * - Add SIGHUP handler (unix)
112 * - Add creation of pidfile (unix)
113 * - Add action 'top' in rc file (RH)
114 * - Add entry 'SIGNALS' to manpage
115 * - Add exit message to logfile (unix)
117 * Revision 1.27 2001/11/07 00:02:13 steudten
118 * Add line number in error output for lineparsing for
119 * actionsfile and configfile.
120 * Special handling for CLF added.
122 * Revision 1.26 2001/11/05 21:41:43 steudten
123 * Add changes to be a real daemon just for unix os.
124 * (change cwd to /, detach from controlling tty, set
125 * process group and session leader to the own process.
127 * Add some fatal-error log message for failed malloc().
128 * Add '-d' if compiled with 'configure --with-debug' to
129 * enable debug output.
131 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
132 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
133 * threads to call select() simultaneously. So, it's time to do a real, live,
134 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
135 * (native). Both versions will work, but using __OS2__ offers multi-threading.
137 * Revision 1.24 2001/10/23 21:40:30 jongfoster
138 * Added support for enable-edit-actions and enable-remote-toggle config
141 * Revision 1.23 2001/10/07 15:36:00 oes
142 * Introduced new config option "buffer-limit"
144 * Revision 1.22 2001/09/22 16:36:59 jongfoster
145 * Removing unused parameter fs from read_config_line()
147 * Revision 1.21 2001/09/16 17:10:43 jongfoster
148 * Moving function savearg() here, since it was the only thing left in
151 * Revision 1.20 2001/07/30 22:08:36 jongfoster
152 * Tidying up #defines:
153 * - All feature #defines are now of the form FEATURE_xxx
154 * - Permanently turned off WIN_GUI_EDIT
155 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
157 * Revision 1.19 2001/07/15 17:45:16 jongfoster
158 * Removing some unused #includes
160 * Revision 1.18 2001/07/13 14:01:14 oes
161 * - Removed all #ifdef PCRS
162 * - Removed vim-settings
164 * Revision 1.17 2001/06/29 13:31:03 oes
165 * - Improved comments
166 * - Fixed (actionsfile) and sorted hashes
167 * - Introduced admin_address and proxy-info-url
168 * as config parameters
169 * - Renamed config->proxy_args_invocation (which didn't have
170 * the invocation but the options!) to config->proxy_args
171 * - Various adaptions
172 * - Removed logentry from cancelled commit
174 * Revision 1.16 2001/06/09 10:55:28 jongfoster
175 * Changing BUFSIZ ==> BUFFER_SIZE
177 * Revision 1.15 2001/06/07 23:13:40 jongfoster
178 * Merging ACL and forward files into config file.
179 * Cosmetic: Sorting config file options alphabetically.
180 * Cosmetic: Adding brief syntax comments to config file options.
182 * Revision 1.14 2001/06/07 14:46:25 joergs
183 * Missing make_path() added for re_filterfile.
185 * Revision 1.13 2001/06/05 22:33:54 jongfoster
187 * Fixed minor memory leak.
188 * Also now uses make_path to prepend the pathnames.
190 * Revision 1.12 2001/06/05 20:04:09 jongfoster
191 * Now uses _snprintf() in place of snprintf() under Win32.
193 * Revision 1.11 2001/06/04 18:31:58 swa
194 * files are now prefixed with either `confdir' or `logdir'.
195 * `make redhat-dist' replaces both entries confdir and logdir
198 * Revision 1.10 2001/06/03 19:11:54 oes
199 * introduced confdir option
201 * Revision 1.9 2001/06/01 20:06:24 jongfoster
202 * Removed support for "tinygif" option - moved to actions file.
204 * Revision 1.8 2001/05/31 21:27:13 jongfoster
205 * Removed many options from the config file and into the
206 * "actions" file: add_forwarded, suppress_vanilla_wafer,
207 * wafer, add_header, user_agent, referer, from
208 * Also globally replaced "permission" with "action".
210 * Revision 1.7 2001/05/29 09:50:24 jongfoster
211 * Unified blocklist/imagelist/permissionslist.
212 * File format is still under discussion, but the internal changes
215 * Also modified interceptor behaviour:
216 * - We now intercept all URLs beginning with one of the following
217 * prefixes (and *only* these prefixes):
219 * * http://ijbswa.sf.net/config/
220 * * http://ijbswa.sourceforge.net/config/
221 * - New interceptors "home page" - go to http://i.j.b/ to see it.
222 * - Internal changes so that intercepted and fast redirect pages
223 * are not replaced with an image.
224 * - Interceptors now have the option to send a binary page direct
225 * to the client. (i.e. ijb-send-banner uses this)
226 * - Implemented show-url-info interceptor. (Which is why I needed
227 * the above interceptors changes - a typical URL is
228 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
229 * The previous mechanism would not have intercepted that, and
230 * if it had been intercepted then it then it would have replaced
233 * Revision 1.6 2001/05/26 00:28:36 jongfoster
234 * Automatic reloading of config file.
235 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
236 * Most of the global variables have been moved to a new
237 * struct configuration_spec, accessed through csp->config->globalname
238 * Most of the globals remaining are used by the Win32 GUI.
240 * Revision 1.5 2001/05/25 22:34:30 jongfoster
243 * Revision 1.4 2001/05/22 18:46:04 oes
245 * - Enabled filtering banners by size rather than URL
246 * by adding patterns that replace all standard banner
247 * sizes with the "Junkbuster" gif to the re_filterfile
249 * - Enabled filtering WebBugs by providing a pattern
250 * which kills all 1x1 images
252 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
253 * which is selected by the (nonstandard and therefore
254 * capital) letter 'U' in the option string.
255 * It causes the quantifiers to be ungreedy by default.
256 * Appending a ? turns back to greedy (!).
258 * - Added a new interceptor ijb-send-banner, which
259 * sends back the "Junkbuster" gif. Without imagelist or
260 * MSIE detection support, or if tinygif = 1, or the
261 * URL isn't recognized as an imageurl, a lame HTML
262 * explanation is sent instead.
264 * - Added new feature, which permits blocking remote
265 * script redirects and firing back a local redirect
267 * The feature is conditionally compiled, i.e. it
268 * can be disabled with --disable-fast-redirects,
269 * plus it must be activated by a "fast-redirects"
270 * line in the config file, has its own log level
271 * and of course wants to be displayed by show-proxy-args
272 * Note: Boy, all the #ifdefs in 1001 locations and
273 * all the fumbling with configure.in and acconfig.h
274 * were *way* more work than the feature itself :-(
276 * - Because a generic redirect template was needed for
277 * this, tinygif = 3 now uses the same.
279 * - Moved GIFs, and other static HTTP response templates
284 * - Removed some >400 CRs again (Jon, you really worked
287 * Revision 1.3 2001/05/20 01:21:20 jongfoster
288 * Version 2.9.4 checkin.
289 * - Merged popupfile and cookiefile, and added control over PCRS
290 * filtering, in new "permissionsfile".
291 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
292 * file error you now get a message box (in the Win32 GUI) rather
293 * than the program exiting with no explanation.
294 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
296 * - Removed tabs from "config"
297 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
298 * - Bumped up version number.
300 * Revision 1.2 2001/05/17 23:01:01 oes
301 * - Cleaned CRLF's from the sources and related files
303 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
304 * Initial import of version 2.9.3 source tree
307 *********************************************************************/
313 #include <sys/types.h>
327 # include <windows.h>
330 # ifndef _WIN_CONSOLE
332 # endif /* ndef _WIN_CONSOLE */
334 /* VC++ has "_snprintf", not "snprintf" */
335 #define snprintf _snprintf
337 #else /* ifndef _WIN32 */
341 # include <sys/wait.h>
343 # include <sys/time.h>
344 # include <sys/stat.h>
354 #include "miscutil.h"
358 #include "urlmatch.h"
361 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
364 * Fix a problem with Solaris. There should be no effect on other
366 * Solaris's isspace() is a macro which uses it's argument directly
367 * as an array index. Therefore we need to make sure that high-bit
368 * characters generate +ve values, and ideally we also want to make
369 * the argument match the declared parameter type of "int".
371 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
372 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
374 #ifdef FEATURE_TOGGLE
375 /* by haroon - indicates if ijb is enabled */
376 int g_bToggleIJB = 1; /* Privoxy is enabled by default. */
377 #endif /* def FEATURE_TOGGLE */
379 /* The filename of the configfile */
380 const char *configfile = NULL;
383 * CGI functions will later need access to the invocation args,
384 * so we will make argc and argv global.
387 const char **Argv = NULL;
389 static struct file_list *current_configfile = NULL;
393 * This takes the "cryptic" hash of each keyword and aliases them to
394 * something a little more readable. This also makes changing the
395 * hash values easier if they should change or the hash algorthm changes.
396 * Use the included "hash" program to find out what the hash will be
397 * for any string supplied on the command line. (Or just put it in the
398 * config file and read the number from the error message in the log).
400 * Please keep this list sorted alphabetically (but with the Windows
401 * console and GUI specific options last).
404 #define hash_actions_file 1196306641ul /* "actionsfile" */
405 #define hash_admin_address 4112573064ul /* "admin-address" */
406 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
407 #define hash_confdir 1978389ul /* "confdir" */
408 #define hash_debug 78263ul /* "debug" */
409 #define hash_deny_access 1227333715ul /* "deny-access" */
410 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
411 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
412 #define hash_filterfile 250887266ul /* "filterfile" */
413 #define hash_forward 2029845ul /* "forward" */
414 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
415 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
416 #define hash_jarfile 2046641ul /* "jarfile" */
417 #define hash_listen_address 1255650842ul /* "listen-address" */
418 #define hash_logdir 422889ul /* "logdir" */
419 #define hash_logfile 2114766ul /* "logfile" */
420 #define hash_permit_access 3587953268ul /* "permit-access" */
421 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
422 #define hash_single_threaded 4250084780ul /* "single-threaded" */
423 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
424 #define hash_toggle 447966ul /* "toggle" */
425 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
426 #define hash_trustfile 56494766ul /* "trustfile" */
427 #define hash_usermanual 1416668518ul /* "user-manual" */
428 #define hash_activity_animation 1817904738ul /* "activity-animation" */
429 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
430 #define hash_hide_console 2048809870ul /* "hide-console" */
431 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
432 #define hash_log_font_name 2866730124ul /* "log-font-name" */
433 #define hash_log_font_size 2866731014ul /* "log-font-size" */
434 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
435 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
436 #define hash_log_messages 2291744899ul /* "log-messages" */
437 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
440 static void savearg(char *command, char *argument, struct configuration_spec * config);
442 /*********************************************************************
444 * Function : unload_configfile
446 * Description : Free the config structure and all components.
449 * 1 : data: struct configuration_spec to unload
453 *********************************************************************/
454 void unload_configfile (void * data)
456 struct configuration_spec * config = (struct configuration_spec *)data;
457 struct forward_spec *cur_fwd = config->forward;
461 struct access_control_list *cur_acl = config->acl;
463 while (cur_acl != NULL)
465 struct access_control_list * next_acl = cur_acl->next;
470 #endif /* def FEATURE_ACL */
472 while (cur_fwd != NULL)
474 struct forward_spec * next_fwd = cur_fwd->next;
475 free_url_spec(cur_fwd->url);
477 freez(cur_fwd->gateway_host);
478 freez(cur_fwd->forward_host);
482 config->forward = NULL;
484 #ifdef FEATURE_COOKIE_JAR
485 if ( NULL != config->jar )
487 fclose( config->jar );
490 #endif /* def FEATURE_COOKIE_JAR */
492 freez(config->confdir);
493 freez(config->logdir);
495 freez(config->haddr);
496 freez(config->logfile);
498 for (i = 0; i < MAX_ACTION_FILES; i++)
500 freez(config->actions_file_short[i]);
501 freez(config->actions_file[i]);
504 freez(config->admin_address);
505 freez(config->proxy_info_url);
506 freez(config->proxy_args);
507 freez(config->usermanual);
509 #ifdef FEATURE_COOKIE_JAR
510 freez(config->jarfile);
511 #endif /* def FEATURE_COOKIE_JAR */
513 freez(config->re_filterfile);
518 #ifdef FEATURE_GRACEFUL_TERMINATION
519 /*********************************************************************
521 * Function : unload_current_config_file
523 * Description : Unloads current config file - reset to state at
524 * beginning of program.
530 *********************************************************************/
531 void unload_current_config_file(void)
533 if (current_configfile)
535 current_configfile->unloader = unload_configfile;
536 current_configfile = NULL;
542 /*********************************************************************
544 * Function : load_config
546 * Description : Load the config file and all parameters.
550 * Returns : The configuration_spec, or NULL on error.
552 *********************************************************************/
553 struct configuration_spec * load_config(void)
555 char buf[BUFFER_SIZE];
557 FILE *configfp = NULL;
558 struct configuration_spec * config = NULL;
559 struct client_state * fake_csp;
560 struct file_list *fs;
561 unsigned long linenum = 0;
564 if ( !check_file_changed(current_configfile, configfile, &fs))
566 /* No need to load */
567 return ((struct configuration_spec *)current_configfile->f);
571 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
575 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
577 #ifdef FEATURE_TOGGLE
579 #endif /* def FEATURE_TOGGLE */
581 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
587 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
588 /* Never get here - LOG_LEVEL_FATAL causes program exit */
592 * This is backwards from how it's usually done.
593 * Following the usual pattern, "fs" would be stored in a member
594 * variable in "csp", and then we'd access "config" from "fs->f",
595 * using a cast. However, "config" is used so often that a
596 * cast each time would be very ugly, and the extra indirection
597 * would waste CPU cycles. Therefore we store "config" in
598 * "csp->config", and "fs" in "csp->config->config_file_list".
600 config->config_file_list = fs;
605 config->multi_threaded = 1;
606 config->hport = HADDR_PORT;
607 config->buffer_limit = 4096 * 1024;
608 config->usermanual = strdup(USER_MANUAL_URL);
609 config->proxy_args = strdup("");
611 if ((configfp = fopen(configfile, "r")) == NULL)
613 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
615 /* Never get here - LOG_LEVEL_FATAL causes program exit */
618 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
620 char cmd[BUFFER_SIZE];
621 char arg[BUFFER_SIZE];
622 char tmp[BUFFER_SIZE];
624 struct access_control_list *cur_acl;
625 #endif /* def FEATURE_ACL */
626 struct forward_spec *cur_fwd;
632 /* Copy command (i.e. up to space or tab) into cmd */
635 while (*p && (*p != ' ') && (*p != '\t'))
641 /* Skip over the whitespace in buf */
642 while (*p && ((*p == ' ') || (*p == '\t')))
647 /* Copy the argument into arg */
650 /* Should never happen, but check this anyway */
656 /* Make sure the command field is lower case */
661 *p = ijb_tolower(*p);
665 /* Save the argument for show-proxy-args */
666 savearg(cmd, arg, config);
669 switch( hash_string( cmd ) )
671 /* *************************************************************************
672 * actionsfile actions-file-name
673 * In confdir by default
674 * *************************************************************************/
675 case hash_actions_file :
677 while ((i < MAX_ACTION_FILES) && (NULL != config->actions_file[i]))
682 if (i >= MAX_ACTION_FILES)
684 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
685 "(You can increase this limit by changing MAX_ACTION_FILES in project.h and recompiling).",
688 config->actions_file_short[i] = strdup(arg);
689 p = malloc(strlen(arg) + sizeof(".action"));
692 log_error(LOG_LEVEL_FATAL, "Out of memory");
695 strcat(p, ".action");
696 config->actions_file[i] = make_path(config->confdir, p);
700 /* *************************************************************************
701 * admin-address email-address
702 * *************************************************************************/
703 case hash_admin_address :
704 freez(config->admin_address);
705 config->admin_address = strdup(arg);
708 /* *************************************************************************
710 * *************************************************************************/
711 case hash_buffer_limit :
712 config->buffer_limit = (size_t) 1024 * atoi(arg);
715 /* *************************************************************************
716 * confdir directory-name
717 * *************************************************************************/
719 freez(config->confdir);
720 config->confdir = make_path( NULL, arg);
723 /* *************************************************************************
725 * Specifies debug level, multiple values are ORed together.
726 * *************************************************************************/
728 config->debug |= atoi(arg);
731 /* *************************************************************************
732 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
733 * *************************************************************************/
735 case hash_deny_access:
736 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
738 if ((vec_count != 1) && (vec_count != 2))
740 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
741 "deny-access directive in configuration file.");
742 string_append(&config->proxy_args,
743 "<br>\nWARNING: Wrong number of parameters for "
744 "deny-access directive in configuration file.<br><br>\n");
748 /* allocate a new node */
749 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
753 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
754 /* Never get here - LOG_LEVEL_FATAL causes program exit */
757 cur_acl->action = ACL_DENY;
759 if (acl_addr(vec[0], cur_acl->src) < 0)
761 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
762 "directive in configuration file: \"%s\"", vec[0]);
763 string_append(&config->proxy_args,
764 "<br>\nWARNING: Invalid source IP for deny-access directive"
765 " in configuration file: \"");
766 string_append(&config->proxy_args,
768 string_append(&config->proxy_args,
775 if (acl_addr(vec[1], cur_acl->dst) < 0)
777 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
778 "directive in configuration file: \"%s\"", vec[0]);
779 string_append(&config->proxy_args,
780 "<br>\nWARNING: Invalid destination IP for deny-access directive"
781 " in configuration file: \"");
782 string_append(&config->proxy_args,
784 string_append(&config->proxy_args,
792 * Add it to the list. Note we reverse the list to get the
793 * behaviour the user expects. With both the ACL and
794 * actions file, the last match wins. However, the internal
795 * implementations are different: The actions file is stored
796 * in the same order as the file, and scanned completely.
797 * With the ACL, we reverse the order as we load it, then
798 * when we scan it we stop as soon as we get a match.
800 cur_acl->next = config->acl;
801 config->acl = cur_acl;
804 #endif /* def FEATURE_ACL */
806 /* *************************************************************************
807 * enable-edit-actions 0|1
808 * *************************************************************************/
809 #ifdef FEATURE_CGI_EDIT_ACTIONS
810 case hash_enable_edit_actions:
811 if ((*arg != '\0') && (0 != atoi(arg)))
813 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
817 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
820 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
822 /* *************************************************************************
823 * enable-remote-toggle 0|1
824 * *************************************************************************/
825 #ifdef FEATURE_CGI_EDIT_ACTIONS
826 case hash_enable_remote_toggle:
827 if ((*arg != '\0') && (0 != atoi(arg)))
829 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
833 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
836 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
838 /* *************************************************************************
839 * forward url-pattern (.|http-proxy-host[:port])
840 * *************************************************************************/
842 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
846 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
847 "directive in configuration file.");
848 string_append(&config->proxy_args,
849 "<br>\nWARNING: Wrong number of parameters for "
850 "forward directive in configuration file.");
854 /* allocate a new node */
855 cur_fwd = zalloc(sizeof(*cur_fwd));
858 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
859 /* Never get here - LOG_LEVEL_FATAL causes program exit */
863 cur_fwd->type = SOCKS_NONE;
865 /* Save the URL pattern */
866 if (create_url_spec(cur_fwd->url, vec[0]))
868 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
869 "directive in configuration file.");
870 string_append(&config->proxy_args,
871 "<br>\nWARNING: Bad URL specifier for "
872 "forward directive in configuration file.");
876 /* Parse the parent HTTP proxy host:port */
879 if (strcmp(p, ".") != 0)
881 cur_fwd->forward_host = strdup(p);
883 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
886 cur_fwd->forward_port = atoi(p);
889 if (cur_fwd->forward_port <= 0)
891 cur_fwd->forward_port = 8000;
896 cur_fwd->next = config->forward;
897 config->forward = cur_fwd;
901 /* *************************************************************************
902 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
903 * *************************************************************************/
904 case hash_forward_socks4:
905 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
909 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
910 "forward-socks4 directive in configuration file.");
911 string_append(&config->proxy_args,
912 "<br>\nWARNING: Wrong number of parameters for "
913 "forward-socks4 directive in configuration file.");
917 /* allocate a new node */
918 cur_fwd = zalloc(sizeof(*cur_fwd));
921 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
922 /* Never get here - LOG_LEVEL_FATAL causes program exit */
926 cur_fwd->type = SOCKS_4;
928 /* Save the URL pattern */
929 if (create_url_spec(cur_fwd->url, vec[0]))
931 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
932 "directive in configuration file.");
933 string_append(&config->proxy_args,
934 "<br>\nWARNING: Bad URL specifier for "
935 "forward-socks4 directive in configuration file.");
939 /* Parse the SOCKS proxy host[:port] */
942 if (strcmp(p, ".") != 0)
944 cur_fwd->gateway_host = strdup(p);
946 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
949 cur_fwd->gateway_port = atoi(p);
951 if (cur_fwd->gateway_port <= 0)
953 cur_fwd->gateway_port = 1080;
957 /* Parse the parent HTTP proxy host[:port] */
960 if (strcmp(p, ".") != 0)
962 cur_fwd->forward_host = strdup(p);
964 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
967 cur_fwd->forward_port = atoi(p);
970 if (cur_fwd->forward_port <= 0)
972 cur_fwd->forward_port = 8000;
977 cur_fwd->next = config->forward;
978 config->forward = cur_fwd;
982 /* *************************************************************************
983 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
984 * *************************************************************************/
985 case hash_forward_socks4a:
986 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
990 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
991 "forward-socks4a directive in configuration file.");
992 string_append(&config->proxy_args,
993 "<br>\nWARNING: Wrong number of parameters for "
994 "forward-socks4a directive in configuration file.");
998 /* allocate a new node */
999 cur_fwd = zalloc(sizeof(*cur_fwd));
1000 if (cur_fwd == NULL)
1002 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1003 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1007 cur_fwd->type = SOCKS_4A;
1009 /* Save the URL pattern */
1010 if (create_url_spec(cur_fwd->url, vec[0]))
1012 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
1013 "directive in configuration file.");
1014 string_append(&config->proxy_args,
1015 "<br>\nWARNING: Bad URL specifier for "
1016 "forward-socks4a directive in configuration file.");
1020 /* Parse the SOCKS proxy host[:port] */
1023 cur_fwd->gateway_host = strdup(p);
1025 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1028 cur_fwd->gateway_port = atoi(p);
1030 if (cur_fwd->gateway_port <= 0)
1032 cur_fwd->gateway_port = 1080;
1035 /* Parse the parent HTTP proxy host[:port] */
1038 if (strcmp(p, ".") != 0)
1040 cur_fwd->forward_host = strdup(p);
1042 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1045 cur_fwd->forward_port = atoi(p);
1048 if (cur_fwd->forward_port <= 0)
1050 cur_fwd->forward_port = 8000;
1055 cur_fwd->next = config->forward;
1056 config->forward = cur_fwd;
1060 /* *************************************************************************
1061 * jarfile jar-file-name
1062 * In logdir by default
1063 * *************************************************************************/
1064 #ifdef FEATURE_COOKIE_JAR
1066 freez(config->jarfile);
1067 config->jarfile = make_path(config->logdir, arg);
1069 #endif /* def FEATURE_COOKIE_JAR */
1071 /* *************************************************************************
1072 * listen-address [ip][:port]
1073 * *************************************************************************/
1074 case hash_listen_address :
1075 freez(config->haddr);
1076 config->haddr = strdup(arg);
1079 /* *************************************************************************
1080 * logdir directory-name
1081 * *************************************************************************/
1083 freez(config->logdir);
1084 config->logdir = make_path(NULL, arg);
1087 /* *************************************************************************
1088 * logfile log-file-name
1089 * In logdir by default
1090 * *************************************************************************/
1092 freez(config->logfile);
1093 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1096 /* *************************************************************************
1097 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1098 * *************************************************************************/
1100 case hash_permit_access:
1101 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1103 if ((vec_count != 1) && (vec_count != 2))
1105 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1106 "permit-access directive in configuration file.");
1107 string_append(&config->proxy_args,
1108 "<br>\nWARNING: Wrong number of parameters for "
1109 "permit-access directive in configuration file.<br><br>\n");
1114 /* allocate a new node */
1115 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1117 if (cur_acl == NULL)
1119 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1120 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1123 cur_acl->action = ACL_PERMIT;
1125 if (acl_addr(vec[0], cur_acl->src) < 0)
1127 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1128 "directive in configuration file: \"%s\"", vec[0]);
1129 string_append(&config->proxy_args,
1130 "<br>\nWARNING: Invalid source IP for permit-access directive"
1131 " in configuration file: \"");
1132 string_append(&config->proxy_args,
1134 string_append(&config->proxy_args,
1141 if (acl_addr(vec[1], cur_acl->dst) < 0)
1143 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1144 "permit-access directive in configuration file: \"%s\"",
1146 string_append(&config->proxy_args,
1147 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1148 " in configuration file: \"");
1149 string_append(&config->proxy_args,
1151 string_append(&config->proxy_args,
1159 * Add it to the list. Note we reverse the list to get the
1160 * behaviour the user expects. With both the ACL and
1161 * actions file, the last match wins. However, the internal
1162 * implementations are different: The actions file is stored
1163 * in the same order as the file, and scanned completely.
1164 * With the ACL, we reverse the order as we load it, then
1165 * when we scan it we stop as soon as we get a match.
1167 cur_acl->next = config->acl;
1168 config->acl = cur_acl;
1171 #endif /* def FEATURE_ACL */
1173 /* *************************************************************************
1174 * proxy-info-url url
1175 * *************************************************************************/
1176 case hash_proxy_info_url :
1177 freez(config->proxy_info_url);
1178 config->proxy_info_url = strdup(arg);
1181 /* *************************************************************************
1182 * re_filterfile file-name
1183 * In confdir by default.
1184 * *************************************************************************/
1185 case hash_filterfile :
1186 freez(config->re_filterfile);
1187 config->re_filterfile = make_path(config->confdir, arg);
1190 /* *************************************************************************
1192 * *************************************************************************/
1193 case hash_single_threaded :
1194 config->multi_threaded = 0;
1197 /* *************************************************************************
1199 * *************************************************************************/
1200 #ifdef FEATURE_TOGGLE
1202 g_bToggleIJB = atoi(arg);
1204 #endif /* def FEATURE_TOGGLE */
1206 /* *************************************************************************
1207 * trust-info-url url
1208 * *************************************************************************/
1209 #ifdef FEATURE_TRUST
1210 case hash_trust_info_url :
1211 enlist(config->trust_info, arg);
1213 #endif /* def FEATURE_TRUST */
1215 /* *************************************************************************
1216 * trustfile filename
1217 * (In confdir by default.)
1218 * *************************************************************************/
1219 #ifdef FEATURE_TRUST
1220 case hash_trustfile :
1221 freez(config->trustfile);
1222 config->trustfile = make_path(config->confdir, arg);
1224 #endif /* def FEATURE_TRUST */
1226 /* *************************************************************************
1228 * *************************************************************************/
1229 case hash_usermanual :
1230 freez(config->usermanual);
1231 config->usermanual = strdup(arg);
1234 /* *************************************************************************
1235 * Win32 Console options:
1236 * *************************************************************************/
1238 /* *************************************************************************
1240 * *************************************************************************/
1242 case hash_hide_console :
1245 #endif /*def _WIN_CONSOLE*/
1248 /* *************************************************************************
1249 * Win32 GUI options:
1250 * *************************************************************************/
1252 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1253 /* *************************************************************************
1254 * activity-animation (0|1)
1255 * *************************************************************************/
1256 case hash_activity_animation :
1257 g_bShowActivityAnimation = atoi(arg);
1260 /* *************************************************************************
1261 * close-button-minimizes (0|1)
1262 * *************************************************************************/
1263 case hash_close_button_minimizes :
1264 g_bCloseHidesWindow = atoi(arg);
1267 /* *************************************************************************
1268 * log-buffer-size (0|1)
1269 * *************************************************************************/
1270 case hash_log_buffer_size :
1271 g_bLimitBufferSize = atoi(arg);
1274 /* *************************************************************************
1275 * log-font-name fontnane
1276 * *************************************************************************/
1277 case hash_log_font_name :
1278 strcpy( g_szFontFaceName, arg );
1281 /* *************************************************************************
1283 * *************************************************************************/
1284 case hash_log_font_size :
1285 g_nFontSize = atoi(arg);
1288 /* *************************************************************************
1289 * log-highlight-messages (0|1)
1290 * *************************************************************************/
1291 case hash_log_highlight_messages :
1292 g_bHighlightMessages = atoi(arg);
1295 /* *************************************************************************
1297 * *************************************************************************/
1298 case hash_log_max_lines :
1299 g_nMaxBufferLines = atoi(arg);
1302 /* *************************************************************************
1303 * log-messages (0|1)
1304 * *************************************************************************/
1305 case hash_log_messages :
1306 g_bLogMessages = atoi(arg);
1309 /* *************************************************************************
1310 * show-on-task-bar (0|1)
1311 * *************************************************************************/
1312 case hash_show_on_task_bar :
1313 g_bShowOnTaskBar = atoi(arg);
1316 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1319 /* *************************************************************************
1320 * Warnings about unsupported features
1321 * *************************************************************************/
1323 case hash_deny_access:
1324 #endif /* ndef FEATURE_ACL */
1325 #ifndef FEATURE_CGI_EDIT_ACTIONS
1326 case hash_enable_edit_actions:
1327 case hash_enable_remote_toggle:
1328 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1329 #ifndef FEATURE_COOKIE_JAR
1331 #endif /* ndef FEATURE_COOKIE_JAR */
1333 case hash_permit_access:
1334 #endif /* ndef FEATURE_ACL */
1335 #ifndef FEATURE_TOGGLE
1337 #endif /* ndef FEATURE_TOGGLE */
1338 #ifndef FEATURE_TRUST
1339 case hash_trustfile :
1340 case hash_trust_info_url :
1341 #endif /* ndef FEATURE_TRUST */
1343 #ifndef _WIN_CONSOLE
1344 case hash_hide_console :
1345 #endif /* ndef _WIN_CONSOLE */
1347 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1348 case hash_activity_animation :
1349 case hash_close_button_minimizes :
1350 case hash_log_buffer_size :
1351 case hash_log_font_name :
1352 case hash_log_font_size :
1353 case hash_log_highlight_messages :
1354 case hash_log_max_lines :
1355 case hash_log_messages :
1356 case hash_show_on_task_bar :
1357 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1358 /* These warnings are annoying - so hide them. -- Jon */
1359 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1362 /* *************************************************************************/
1364 /* *************************************************************************/
1366 * I decided that I liked this better as a warning than an
1367 * error. To change back to an error, just change log level
1368 * to LOG_LEVEL_FATAL.
1370 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1371 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1372 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1373 string_append(&config->proxy_args, buf);
1374 string_append(&config->proxy_args, "<br><br>\n");
1377 /* *************************************************************************/
1378 } /* end switch( hash_string(cmd) ) */
1379 } /* end while ( read_config_line(...) ) */
1383 if (NULL == config->proxy_args)
1385 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1388 init_error_log(Argv[0], config->logfile, config->debug);
1390 if (config->actions_file[0])
1392 add_loader(load_actions_file, config);
1395 if (config->re_filterfile)
1397 add_loader(load_re_filterfile, config);
1400 #ifdef FEATURE_TRUST
1401 if (config->trustfile)
1403 add_loader(load_trustfile, config);
1405 #endif /* def FEATURE_TRUST */
1407 #ifdef FEATURE_COOKIE_JAR
1408 if ( NULL != config->jarfile )
1410 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1412 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1413 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1415 setbuf(config->jar, NULL);
1417 #endif /* def FEATURE_COOKIE_JAR */
1419 if ( NULL == config->haddr )
1421 config->haddr = strdup( HADDR_DEFAULT );
1424 if ( NULL != config->haddr )
1426 if (NULL != (p = strchr(config->haddr, ':')))
1431 config->hport = atoi(p);
1435 if (config->hport <= 0)
1438 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1439 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1441 if (*config->haddr == '\0')
1443 config->haddr = NULL;
1448 * Want to run all the loaders once now.
1450 * Need to set up a fake csp, so they can get to the config.
1452 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1453 fake_csp->config = config;
1455 if (run_loader(fake_csp))
1458 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1459 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1463 /* FIXME: this is a kludge for win32 */
1464 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1466 g_default_actions_file = config->actions_file[1]; /* FIXME Hope this is default.action */
1467 g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1468 g_re_filterfile = config->re_filterfile;
1470 #ifdef FEATURE_TRUST
1471 g_trustfile = config->trustfile;
1472 #endif /* def FEATURE_TRUST */
1475 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1476 /* FIXME: end kludge */
1479 config->need_bind = 1;
1481 if (current_configfile)
1483 struct configuration_spec * oldcfg = (struct configuration_spec *)
1484 current_configfile->f;
1486 * Check if config->haddr,hport == oldcfg->haddr,hport
1488 * The following could be written more compactly as a single,
1489 * (unreadably long) if statement.
1491 config->need_bind = 0;
1492 if (config->hport != oldcfg->hport)
1494 config->need_bind = 1;
1496 else if (config->haddr == NULL)
1498 if (oldcfg->haddr != NULL)
1500 config->need_bind = 1;
1503 else if (oldcfg->haddr == NULL)
1505 config->need_bind = 1;
1507 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1509 config->need_bind = 1;
1512 current_configfile->unloader = unload_configfile;
1515 fs->next = files->next;
1518 current_configfile = fs;
1524 /*********************************************************************
1526 * Function : savearg
1528 * Description : Called from `load_config'. It saves each non-empty
1529 * and non-comment line from config into
1530 * config->proxy_args. This is used to create the
1531 * show-proxy-args page. On error, frees
1532 * config->proxy_args and sets it to NULL
1535 * 1 : command = config setting that was found
1536 * 2 : argument = the setting's argument (if any)
1537 * 3 : config = Configuration to save into.
1541 *********************************************************************/
1542 static void savearg(char *command, char *argument, struct configuration_spec * config)
1552 * Add config option name embedded in
1553 * link to it's section in the user-manual
1555 buf = strdup("<a href=\"");
1556 string_append(&buf, config->usermanual);
1557 string_append(&buf, CONFIG_HELP_PREFIX);
1558 string_join (&buf, string_toupper(command));
1559 string_append(&buf, "\">");
1560 string_append(&buf, command);
1561 string_append(&buf, "</a> ");
1565 freez(config->proxy_args);
1569 if ( (NULL != argument) && ('\0' != *argument) )
1571 s = html_encode(argument);
1575 freez(config->proxy_args);
1579 if (strncmpic(argument, "http://", 7) == 0)
1581 string_append(&buf, "<a href=\"");
1582 string_append(&buf, s);
1583 string_append(&buf, "\">");
1584 string_join (&buf, s);
1585 string_append(&buf, "</a>");
1589 string_join (&buf, s);
1593 string_append(&buf, "<br>\n");
1595 string_join(&config->proxy_args, buf);