1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.48.2.2 2002/11/12 16:28:20 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.2 2002/11/12 16:28:20 oes
39 * Move unrelated variable declaration out of #ifdef FEATURE_ACL; fixes bug #636655
41 * Revision 1.48.2.1 2002/08/21 17:58:05 oes
42 * Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
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
378 /* Privoxy is enabled by default. */
379 int global_toggle_state = 1;
380 #endif /* def FEATURE_TOGGLE */
382 /* The filename of the configfile */
383 const char *configfile = NULL;
386 * CGI functions will later need access to the invocation args,
387 * so we will make argc and argv global.
390 const char **Argv = NULL;
392 static struct file_list *current_configfile = NULL;
396 * This takes the "cryptic" hash of each keyword and aliases them to
397 * something a little more readable. This also makes changing the
398 * hash values easier if they should change or the hash algorthm changes.
399 * Use the included "hash" program to find out what the hash will be
400 * for any string supplied on the command line. (Or just put it in the
401 * config file and read the number from the error message in the log).
403 * Please keep this list sorted alphabetically (but with the Windows
404 * console and GUI specific options last).
407 #define hash_actions_file 1196306641ul /* "actionsfile" */
408 #define hash_admin_address 4112573064ul /* "admin-address" */
409 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
410 #define hash_confdir 1978389ul /* "confdir" */
411 #define hash_debug 78263ul /* "debug" */
412 #define hash_deny_access 1227333715ul /* "deny-access" */
413 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
414 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
415 #define hash_filterfile 250887266ul /* "filterfile" */
416 #define hash_forward 2029845ul /* "forward" */
417 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
418 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
419 #define hash_jarfile 2046641ul /* "jarfile" */
420 #define hash_listen_address 1255650842ul /* "listen-address" */
421 #define hash_logdir 422889ul /* "logdir" */
422 #define hash_logfile 2114766ul /* "logfile" */
423 #define hash_permit_access 3587953268ul /* "permit-access" */
424 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
425 #define hash_single_threaded 4250084780ul /* "single-threaded" */
426 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
427 #define hash_toggle 447966ul /* "toggle" */
428 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
429 #define hash_trustfile 56494766ul /* "trustfile" */
430 #define hash_usermanual 1416668518ul /* "user-manual" */
431 #define hash_activity_animation 1817904738ul /* "activity-animation" */
432 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
433 #define hash_hide_console 2048809870ul /* "hide-console" */
434 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
435 #define hash_log_font_name 2866730124ul /* "log-font-name" */
436 #define hash_log_font_size 2866731014ul /* "log-font-size" */
437 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
438 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
439 #define hash_log_messages 2291744899ul /* "log-messages" */
440 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
443 static void savearg(char *command, char *argument, struct configuration_spec * config);
445 /*********************************************************************
447 * Function : unload_configfile
449 * Description : Free the config structure and all components.
452 * 1 : data: struct configuration_spec to unload
456 *********************************************************************/
457 void unload_configfile (void * data)
459 struct configuration_spec * config = (struct configuration_spec *)data;
460 struct forward_spec *cur_fwd = config->forward;
464 struct access_control_list *cur_acl = config->acl;
466 while (cur_acl != NULL)
468 struct access_control_list * next_acl = cur_acl->next;
473 #endif /* def FEATURE_ACL */
475 while (cur_fwd != NULL)
477 struct forward_spec * next_fwd = cur_fwd->next;
478 free_url_spec(cur_fwd->url);
480 freez(cur_fwd->gateway_host);
481 freez(cur_fwd->forward_host);
485 config->forward = NULL;
487 #ifdef FEATURE_COOKIE_JAR
488 if ( NULL != config->jar )
490 fclose( config->jar );
493 #endif /* def FEATURE_COOKIE_JAR */
495 freez(config->confdir);
496 freez(config->logdir);
498 freez(config->haddr);
499 freez(config->logfile);
501 for (i = 0; i < MAX_ACTION_FILES; i++)
503 freez(config->actions_file_short[i]);
504 freez(config->actions_file[i]);
507 freez(config->admin_address);
508 freez(config->proxy_info_url);
509 freez(config->proxy_args);
510 freez(config->usermanual);
512 #ifdef FEATURE_COOKIE_JAR
513 freez(config->jarfile);
514 #endif /* def FEATURE_COOKIE_JAR */
516 freez(config->re_filterfile);
521 #ifdef FEATURE_GRACEFUL_TERMINATION
522 /*********************************************************************
524 * Function : unload_current_config_file
526 * Description : Unloads current config file - reset to state at
527 * beginning of program.
533 *********************************************************************/
534 void unload_current_config_file(void)
536 if (current_configfile)
538 current_configfile->unloader = unload_configfile;
539 current_configfile = NULL;
545 /*********************************************************************
547 * Function : load_config
549 * Description : Load the config file and all parameters.
553 * Returns : The configuration_spec, or NULL on error.
555 *********************************************************************/
556 struct configuration_spec * load_config(void)
558 char buf[BUFFER_SIZE];
560 FILE *configfp = NULL;
561 struct configuration_spec * config = NULL;
562 struct client_state * fake_csp;
563 struct file_list *fs;
564 unsigned long linenum = 0;
567 if ( !check_file_changed(current_configfile, configfile, &fs))
569 /* No need to load */
570 return ((struct configuration_spec *)current_configfile->f);
574 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
578 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
580 #ifdef FEATURE_TOGGLE
581 global_toggle_state = 1;
582 #endif /* def FEATURE_TOGGLE */
584 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
590 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
591 /* Never get here - LOG_LEVEL_FATAL causes program exit */
595 * This is backwards from how it's usually done.
596 * Following the usual pattern, "fs" would be stored in a member
597 * variable in "csp", and then we'd access "config" from "fs->f",
598 * using a cast. However, "config" is used so often that a
599 * cast each time would be very ugly, and the extra indirection
600 * would waste CPU cycles. Therefore we store "config" in
601 * "csp->config", and "fs" in "csp->config->config_file_list".
603 config->config_file_list = fs;
608 config->multi_threaded = 1;
609 config->hport = HADDR_PORT;
610 config->buffer_limit = 4096 * 1024;
611 config->usermanual = strdup(USER_MANUAL_URL);
612 config->proxy_args = strdup("");
614 if ((configfp = fopen(configfile, "r")) == NULL)
616 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
618 /* Never get here - LOG_LEVEL_FATAL causes program exit */
621 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
623 char cmd[BUFFER_SIZE];
624 char arg[BUFFER_SIZE];
625 char tmp[BUFFER_SIZE];
627 struct access_control_list *cur_acl;
628 #endif /* def FEATURE_ACL */
629 struct forward_spec *cur_fwd;
635 /* Copy command (i.e. up to space or tab) into cmd */
638 while (*p && (*p != ' ') && (*p != '\t'))
644 /* Skip over the whitespace in buf */
645 while (*p && ((*p == ' ') || (*p == '\t')))
650 /* Copy the argument into arg */
653 /* Should never happen, but check this anyway */
659 /* Make sure the command field is lower case */
664 *p = ijb_tolower(*p);
668 /* Save the argument for show-proxy-args */
669 savearg(cmd, arg, config);
672 switch( hash_string( cmd ) )
674 /* *************************************************************************
675 * actionsfile actions-file-name
676 * In confdir by default
677 * *************************************************************************/
678 case hash_actions_file :
680 while ((i < MAX_ACTION_FILES) && (NULL != config->actions_file[i]))
685 if (i >= MAX_ACTION_FILES)
687 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
688 "(You can increase this limit by changing MAX_ACTION_FILES in project.h and recompiling).",
691 config->actions_file_short[i] = strdup(arg);
692 p = malloc(strlen(arg) + sizeof(".action"));
695 log_error(LOG_LEVEL_FATAL, "Out of memory");
698 strcat(p, ".action");
699 config->actions_file[i] = make_path(config->confdir, p);
703 /* *************************************************************************
704 * admin-address email-address
705 * *************************************************************************/
706 case hash_admin_address :
707 freez(config->admin_address);
708 config->admin_address = strdup(arg);
711 /* *************************************************************************
713 * *************************************************************************/
714 case hash_buffer_limit :
715 config->buffer_limit = (size_t) 1024 * atoi(arg);
718 /* *************************************************************************
719 * confdir directory-name
720 * *************************************************************************/
722 freez(config->confdir);
723 config->confdir = make_path( NULL, arg);
726 /* *************************************************************************
728 * Specifies debug level, multiple values are ORed together.
729 * *************************************************************************/
731 config->debug |= atoi(arg);
734 /* *************************************************************************
735 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
736 * *************************************************************************/
738 case hash_deny_access:
739 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
741 if ((vec_count != 1) && (vec_count != 2))
743 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
744 "deny-access directive in configuration file.");
745 string_append(&config->proxy_args,
746 "<br>\nWARNING: Wrong number of parameters for "
747 "deny-access directive in configuration file.<br><br>\n");
751 /* allocate a new node */
752 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
756 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
757 /* Never get here - LOG_LEVEL_FATAL causes program exit */
760 cur_acl->action = ACL_DENY;
762 if (acl_addr(vec[0], cur_acl->src) < 0)
764 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
765 "directive in configuration file: \"%s\"", vec[0]);
766 string_append(&config->proxy_args,
767 "<br>\nWARNING: Invalid source IP for deny-access directive"
768 " in configuration file: \"");
769 string_append(&config->proxy_args,
771 string_append(&config->proxy_args,
778 if (acl_addr(vec[1], cur_acl->dst) < 0)
780 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
781 "directive in configuration file: \"%s\"", vec[0]);
782 string_append(&config->proxy_args,
783 "<br>\nWARNING: Invalid destination IP for deny-access directive"
784 " in configuration file: \"");
785 string_append(&config->proxy_args,
787 string_append(&config->proxy_args,
795 * Add it to the list. Note we reverse the list to get the
796 * behaviour the user expects. With both the ACL and
797 * actions file, the last match wins. However, the internal
798 * implementations are different: The actions file is stored
799 * in the same order as the file, and scanned completely.
800 * With the ACL, we reverse the order as we load it, then
801 * when we scan it we stop as soon as we get a match.
803 cur_acl->next = config->acl;
804 config->acl = cur_acl;
807 #endif /* def FEATURE_ACL */
809 /* *************************************************************************
810 * enable-edit-actions 0|1
811 * *************************************************************************/
812 #ifdef FEATURE_CGI_EDIT_ACTIONS
813 case hash_enable_edit_actions:
814 if ((*arg != '\0') && (0 != atoi(arg)))
816 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
820 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
823 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
825 /* *************************************************************************
826 * enable-remote-toggle 0|1
827 * *************************************************************************/
828 #ifdef FEATURE_CGI_EDIT_ACTIONS
829 case hash_enable_remote_toggle:
830 if ((*arg != '\0') && (0 != atoi(arg)))
832 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
836 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
839 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
841 /* *************************************************************************
842 * forward url-pattern (.|http-proxy-host[:port])
843 * *************************************************************************/
845 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
849 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
850 "directive in configuration file.");
851 string_append(&config->proxy_args,
852 "<br>\nWARNING: Wrong number of parameters for "
853 "forward directive in configuration file.");
857 /* allocate a new node */
858 cur_fwd = zalloc(sizeof(*cur_fwd));
861 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
862 /* Never get here - LOG_LEVEL_FATAL causes program exit */
866 cur_fwd->type = SOCKS_NONE;
868 /* Save the URL pattern */
869 if (create_url_spec(cur_fwd->url, vec[0]))
871 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
872 "directive in configuration file.");
873 string_append(&config->proxy_args,
874 "<br>\nWARNING: Bad URL specifier for "
875 "forward directive in configuration file.");
879 /* Parse the parent HTTP proxy host:port */
882 if (strcmp(p, ".") != 0)
884 cur_fwd->forward_host = strdup(p);
886 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
889 cur_fwd->forward_port = atoi(p);
892 if (cur_fwd->forward_port <= 0)
894 cur_fwd->forward_port = 8000;
899 cur_fwd->next = config->forward;
900 config->forward = cur_fwd;
904 /* *************************************************************************
905 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
906 * *************************************************************************/
907 case hash_forward_socks4:
908 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
912 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
913 "forward-socks4 directive in configuration file.");
914 string_append(&config->proxy_args,
915 "<br>\nWARNING: Wrong number of parameters for "
916 "forward-socks4 directive in configuration file.");
920 /* allocate a new node */
921 cur_fwd = zalloc(sizeof(*cur_fwd));
924 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
925 /* Never get here - LOG_LEVEL_FATAL causes program exit */
929 cur_fwd->type = SOCKS_4;
931 /* Save the URL pattern */
932 if (create_url_spec(cur_fwd->url, vec[0]))
934 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
935 "directive in configuration file.");
936 string_append(&config->proxy_args,
937 "<br>\nWARNING: Bad URL specifier for "
938 "forward-socks4 directive in configuration file.");
942 /* Parse the SOCKS proxy host[:port] */
945 if (strcmp(p, ".") != 0)
947 cur_fwd->gateway_host = strdup(p);
949 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
952 cur_fwd->gateway_port = atoi(p);
954 if (cur_fwd->gateway_port <= 0)
956 cur_fwd->gateway_port = 1080;
960 /* Parse the parent HTTP proxy host[:port] */
963 if (strcmp(p, ".") != 0)
965 cur_fwd->forward_host = strdup(p);
967 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
970 cur_fwd->forward_port = atoi(p);
973 if (cur_fwd->forward_port <= 0)
975 cur_fwd->forward_port = 8000;
980 cur_fwd->next = config->forward;
981 config->forward = cur_fwd;
985 /* *************************************************************************
986 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
987 * *************************************************************************/
988 case hash_forward_socks4a:
989 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
993 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
994 "forward-socks4a directive in configuration file.");
995 string_append(&config->proxy_args,
996 "<br>\nWARNING: Wrong number of parameters for "
997 "forward-socks4a directive in configuration file.");
1001 /* allocate a new node */
1002 cur_fwd = zalloc(sizeof(*cur_fwd));
1003 if (cur_fwd == NULL)
1005 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1006 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1010 cur_fwd->type = SOCKS_4A;
1012 /* Save the URL pattern */
1013 if (create_url_spec(cur_fwd->url, vec[0]))
1015 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
1016 "directive in configuration file.");
1017 string_append(&config->proxy_args,
1018 "<br>\nWARNING: Bad URL specifier for "
1019 "forward-socks4a directive in configuration file.");
1023 /* Parse the SOCKS proxy host[:port] */
1026 cur_fwd->gateway_host = strdup(p);
1028 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1031 cur_fwd->gateway_port = atoi(p);
1033 if (cur_fwd->gateway_port <= 0)
1035 cur_fwd->gateway_port = 1080;
1038 /* Parse the parent HTTP proxy host[:port] */
1041 if (strcmp(p, ".") != 0)
1043 cur_fwd->forward_host = strdup(p);
1045 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1048 cur_fwd->forward_port = atoi(p);
1051 if (cur_fwd->forward_port <= 0)
1053 cur_fwd->forward_port = 8000;
1058 cur_fwd->next = config->forward;
1059 config->forward = cur_fwd;
1063 /* *************************************************************************
1064 * jarfile jar-file-name
1065 * In logdir by default
1066 * *************************************************************************/
1067 #ifdef FEATURE_COOKIE_JAR
1069 freez(config->jarfile);
1070 config->jarfile = make_path(config->logdir, arg);
1072 #endif /* def FEATURE_COOKIE_JAR */
1074 /* *************************************************************************
1075 * listen-address [ip][:port]
1076 * *************************************************************************/
1077 case hash_listen_address :
1078 freez(config->haddr);
1079 config->haddr = strdup(arg);
1082 /* *************************************************************************
1083 * logdir directory-name
1084 * *************************************************************************/
1086 freez(config->logdir);
1087 config->logdir = make_path(NULL, arg);
1090 /* *************************************************************************
1091 * logfile log-file-name
1092 * In logdir by default
1093 * *************************************************************************/
1095 freez(config->logfile);
1096 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1099 /* *************************************************************************
1100 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1101 * *************************************************************************/
1103 case hash_permit_access:
1104 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1106 if ((vec_count != 1) && (vec_count != 2))
1108 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1109 "permit-access directive in configuration file.");
1110 string_append(&config->proxy_args,
1111 "<br>\nWARNING: Wrong number of parameters for "
1112 "permit-access directive in configuration file.<br><br>\n");
1117 /* allocate a new node */
1118 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1120 if (cur_acl == NULL)
1122 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1123 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1126 cur_acl->action = ACL_PERMIT;
1128 if (acl_addr(vec[0], cur_acl->src) < 0)
1130 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1131 "directive in configuration file: \"%s\"", vec[0]);
1132 string_append(&config->proxy_args,
1133 "<br>\nWARNING: Invalid source IP for permit-access directive"
1134 " in configuration file: \"");
1135 string_append(&config->proxy_args,
1137 string_append(&config->proxy_args,
1144 if (acl_addr(vec[1], cur_acl->dst) < 0)
1146 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1147 "permit-access directive in configuration file: \"%s\"",
1149 string_append(&config->proxy_args,
1150 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1151 " in configuration file: \"");
1152 string_append(&config->proxy_args,
1154 string_append(&config->proxy_args,
1162 * Add it to the list. Note we reverse the list to get the
1163 * behaviour the user expects. With both the ACL and
1164 * actions file, the last match wins. However, the internal
1165 * implementations are different: The actions file is stored
1166 * in the same order as the file, and scanned completely.
1167 * With the ACL, we reverse the order as we load it, then
1168 * when we scan it we stop as soon as we get a match.
1170 cur_acl->next = config->acl;
1171 config->acl = cur_acl;
1174 #endif /* def FEATURE_ACL */
1176 /* *************************************************************************
1177 * proxy-info-url url
1178 * *************************************************************************/
1179 case hash_proxy_info_url :
1180 freez(config->proxy_info_url);
1181 config->proxy_info_url = strdup(arg);
1184 /* *************************************************************************
1185 * re_filterfile file-name
1186 * In confdir by default.
1187 * *************************************************************************/
1188 case hash_filterfile :
1189 freez(config->re_filterfile);
1190 config->re_filterfile = make_path(config->confdir, arg);
1193 /* *************************************************************************
1195 * *************************************************************************/
1196 case hash_single_threaded :
1197 config->multi_threaded = 0;
1200 /* *************************************************************************
1202 * *************************************************************************/
1203 #ifdef FEATURE_TOGGLE
1205 global_toggle_state = atoi(arg);
1207 #endif /* def FEATURE_TOGGLE */
1209 /* *************************************************************************
1210 * trust-info-url url
1211 * *************************************************************************/
1212 #ifdef FEATURE_TRUST
1213 case hash_trust_info_url :
1214 enlist(config->trust_info, arg);
1216 #endif /* def FEATURE_TRUST */
1218 /* *************************************************************************
1219 * trustfile filename
1220 * (In confdir by default.)
1221 * *************************************************************************/
1222 #ifdef FEATURE_TRUST
1223 case hash_trustfile :
1224 freez(config->trustfile);
1225 config->trustfile = make_path(config->confdir, arg);
1227 #endif /* def FEATURE_TRUST */
1229 /* *************************************************************************
1231 * *************************************************************************/
1232 case hash_usermanual :
1233 freez(config->usermanual);
1234 config->usermanual = strdup(arg);
1237 /* *************************************************************************
1238 * Win32 Console options:
1239 * *************************************************************************/
1241 /* *************************************************************************
1243 * *************************************************************************/
1245 case hash_hide_console :
1248 #endif /*def _WIN_CONSOLE*/
1251 /* *************************************************************************
1252 * Win32 GUI options:
1253 * *************************************************************************/
1255 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1256 /* *************************************************************************
1257 * activity-animation (0|1)
1258 * *************************************************************************/
1259 case hash_activity_animation :
1260 g_bShowActivityAnimation = atoi(arg);
1263 /* *************************************************************************
1264 * close-button-minimizes (0|1)
1265 * *************************************************************************/
1266 case hash_close_button_minimizes :
1267 g_bCloseHidesWindow = atoi(arg);
1270 /* *************************************************************************
1271 * log-buffer-size (0|1)
1272 * *************************************************************************/
1273 case hash_log_buffer_size :
1274 g_bLimitBufferSize = atoi(arg);
1277 /* *************************************************************************
1278 * log-font-name fontnane
1279 * *************************************************************************/
1280 case hash_log_font_name :
1281 strcpy( g_szFontFaceName, arg );
1284 /* *************************************************************************
1286 * *************************************************************************/
1287 case hash_log_font_size :
1288 g_nFontSize = atoi(arg);
1291 /* *************************************************************************
1292 * log-highlight-messages (0|1)
1293 * *************************************************************************/
1294 case hash_log_highlight_messages :
1295 g_bHighlightMessages = atoi(arg);
1298 /* *************************************************************************
1300 * *************************************************************************/
1301 case hash_log_max_lines :
1302 g_nMaxBufferLines = atoi(arg);
1305 /* *************************************************************************
1306 * log-messages (0|1)
1307 * *************************************************************************/
1308 case hash_log_messages :
1309 g_bLogMessages = atoi(arg);
1312 /* *************************************************************************
1313 * show-on-task-bar (0|1)
1314 * *************************************************************************/
1315 case hash_show_on_task_bar :
1316 g_bShowOnTaskBar = atoi(arg);
1319 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1322 /* *************************************************************************
1323 * Warnings about unsupported features
1324 * *************************************************************************/
1326 case hash_deny_access:
1327 #endif /* ndef FEATURE_ACL */
1328 #ifndef FEATURE_CGI_EDIT_ACTIONS
1329 case hash_enable_edit_actions:
1330 case hash_enable_remote_toggle:
1331 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1332 #ifndef FEATURE_COOKIE_JAR
1334 #endif /* ndef FEATURE_COOKIE_JAR */
1336 case hash_permit_access:
1337 #endif /* ndef FEATURE_ACL */
1338 #ifndef FEATURE_TOGGLE
1340 #endif /* ndef FEATURE_TOGGLE */
1341 #ifndef FEATURE_TRUST
1342 case hash_trustfile :
1343 case hash_trust_info_url :
1344 #endif /* ndef FEATURE_TRUST */
1346 #ifndef _WIN_CONSOLE
1347 case hash_hide_console :
1348 #endif /* ndef _WIN_CONSOLE */
1350 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1351 case hash_activity_animation :
1352 case hash_close_button_minimizes :
1353 case hash_log_buffer_size :
1354 case hash_log_font_name :
1355 case hash_log_font_size :
1356 case hash_log_highlight_messages :
1357 case hash_log_max_lines :
1358 case hash_log_messages :
1359 case hash_show_on_task_bar :
1360 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1361 /* These warnings are annoying - so hide them. -- Jon */
1362 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1365 /* *************************************************************************/
1367 /* *************************************************************************/
1369 * I decided that I liked this better as a warning than an
1370 * error. To change back to an error, just change log level
1371 * to LOG_LEVEL_FATAL.
1373 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1374 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1375 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1376 string_append(&config->proxy_args, buf);
1377 string_append(&config->proxy_args, "<br><br>\n");
1380 /* *************************************************************************/
1381 } /* end switch( hash_string(cmd) ) */
1382 } /* end while ( read_config_line(...) ) */
1386 if (NULL == config->proxy_args)
1388 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1391 init_error_log(Argv[0], config->logfile, config->debug);
1393 if (config->actions_file[0])
1395 add_loader(load_actions_file, config);
1398 if (config->re_filterfile)
1400 add_loader(load_re_filterfile, config);
1403 #ifdef FEATURE_TRUST
1404 if (config->trustfile)
1406 add_loader(load_trustfile, config);
1408 #endif /* def FEATURE_TRUST */
1410 #ifdef FEATURE_COOKIE_JAR
1411 if ( NULL != config->jarfile )
1413 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1415 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1416 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1418 setbuf(config->jar, NULL);
1420 #endif /* def FEATURE_COOKIE_JAR */
1422 if ( NULL == config->haddr )
1424 config->haddr = strdup( HADDR_DEFAULT );
1427 if ( NULL != config->haddr )
1429 if (NULL != (p = strchr(config->haddr, ':')))
1434 config->hport = atoi(p);
1438 if (config->hport <= 0)
1441 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1442 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1444 if (*config->haddr == '\0')
1446 config->haddr = NULL;
1451 * Want to run all the loaders once now.
1453 * Need to set up a fake csp, so they can get to the config.
1455 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1456 fake_csp->config = config;
1458 if (run_loader(fake_csp))
1461 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1462 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1466 /* FIXME: this is a kludge for win32 */
1467 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1469 g_default_actions_file = config->actions_file[1]; /* FIXME Hope this is default.action */
1470 g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1471 g_re_filterfile = config->re_filterfile;
1473 #ifdef FEATURE_TRUST
1474 g_trustfile = config->trustfile;
1475 #endif /* def FEATURE_TRUST */
1478 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1479 /* FIXME: end kludge */
1482 config->need_bind = 1;
1484 if (current_configfile)
1486 struct configuration_spec * oldcfg = (struct configuration_spec *)
1487 current_configfile->f;
1489 * Check if config->haddr,hport == oldcfg->haddr,hport
1491 * The following could be written more compactly as a single,
1492 * (unreadably long) if statement.
1494 config->need_bind = 0;
1495 if (config->hport != oldcfg->hport)
1497 config->need_bind = 1;
1499 else if (config->haddr == NULL)
1501 if (oldcfg->haddr != NULL)
1503 config->need_bind = 1;
1506 else if (oldcfg->haddr == NULL)
1508 config->need_bind = 1;
1510 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1512 config->need_bind = 1;
1515 current_configfile->unloader = unload_configfile;
1518 fs->next = files->next;
1521 current_configfile = fs;
1527 /*********************************************************************
1529 * Function : savearg
1531 * Description : Called from `load_config'. It saves each non-empty
1532 * and non-comment line from config into
1533 * config->proxy_args. This is used to create the
1534 * show-proxy-args page. On error, frees
1535 * config->proxy_args and sets it to NULL
1538 * 1 : command = config setting that was found
1539 * 2 : argument = the setting's argument (if any)
1540 * 3 : config = Configuration to save into.
1544 *********************************************************************/
1545 static void savearg(char *command, char *argument, struct configuration_spec * config)
1555 * Add config option name embedded in
1556 * link to it's section in the user-manual
1558 buf = strdup("<a href=\"");
1559 string_append(&buf, config->usermanual);
1560 string_append(&buf, CONFIG_HELP_PREFIX);
1561 string_join (&buf, string_toupper(command));
1562 string_append(&buf, "\">");
1563 string_append(&buf, command);
1564 string_append(&buf, "</a> ");
1568 freez(config->proxy_args);
1572 if ( (NULL != argument) && ('\0' != *argument) )
1574 s = html_encode(argument);
1578 freez(config->proxy_args);
1582 if (strncmpic(argument, "http://", 7) == 0)
1584 string_append(&buf, "<a href=\"");
1585 string_append(&buf, s);
1586 string_append(&buf, "\">");
1587 string_join (&buf, s);
1588 string_append(&buf, "</a>");
1592 string_join (&buf, s);
1596 string_append(&buf, "<br>\n");
1598 string_join(&config->proxy_args, buf);