1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.48 2002/05/14 21:30:38 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/loadcfg.c,v $
6 * Purpose : Loads settings from the configuration file into
7 * global variables. This file contains both the
8 * routine to load the configuration and the global
9 * variables it writes to.
11 * Copyright : Written by and Copyright (C) 2001 the SourceForge
12 * Privoxy team. http://www.privoxy.org/
14 * Based on the Internet Junkbuster originally written
15 * by and Copyright (C) 1997 Anonymous Coders and
16 * Junkbusters Corporation. http://www.junkbusters.com
18 * This program is free software; you can redistribute it
19 * and/or modify it under the terms of the GNU General
20 * Public License as published by the Free Software
21 * Foundation; either version 2 of the License, or (at
22 * your option) any later version.
24 * This program is distributed in the hope that it will
25 * be useful, but WITHOUT ANY WARRANTY; without even the
26 * implied warranty of MERCHANTABILITY or FITNESS FOR A
27 * PARTICULAR PURPOSE. See the GNU General Public
28 * License for more details.
30 * The GNU General Public License should be included with
31 * this file. If not, you can view it at
32 * http://www.gnu.org/copyleft/gpl.html
33 * or write to the Free Software Foundation, Inc., 59
34 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 * Revision 1.48 2002/05/14 21:30:38 oes
39 * savearg now uses own linking code instead of (now special-cased) add_help_link
41 * Revision 1.47 2002/05/12 21:36:29 jongfoster
42 * Correcting function comments
44 * Revision 1.46 2002/04/26 12:55:14 oes
45 * - New option "user-manual", defaults to our site
46 * via project.h #define
47 * - savearg now embeds option names in help links
49 * Revision 1.45 2002/04/24 02:11:54 oes
50 * Jon's multiple AF patch: Allow up to MAX_ACTION_FILES actionsfile options
52 * Revision 1.44 2002/04/08 20:37:13 swa
55 * Revision 1.43 2002/04/08 20:36:50 swa
58 * Revision 1.42 2002/04/05 15:50:15 oes
59 * fix for invalid HTML proxy_args
61 * Revision 1.41 2002/03/31 17:19:00 jongfoster
62 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
64 * Revision 1.40 2002/03/26 22:29:55 swa
65 * we have a new homepage!
67 * Revision 1.39 2002/03/24 13:25:43 swa
68 * name change related issues
70 * Revision 1.38 2002/03/24 13:05:48 jongfoster
71 * Renaming re_filterfile to filterfile
73 * Revision 1.37 2002/03/16 23:54:06 jongfoster
74 * Adding graceful termination feature, to help look for memory leaks.
75 * If you enable this (which, by design, has to be done by hand
76 * editing config.h) and then go to http://i.j.b/die, then the program
77 * will exit cleanly after the *next* request. It should free all the
78 * memory that was used.
80 * Revision 1.36 2002/03/13 00:27:05 jongfoster
83 * Revision 1.35 2002/03/07 03:52:44 oes
84 * Set logging to tty for --no-daemon mode
86 * Revision 1.34 2002/03/06 23:14:35 jongfoster
87 * Trivial cosmetic changes to make function comments easier to find.
89 * Revision 1.33 2002/03/05 04:52:42 oes
90 * Deleted non-errlog debugging code
92 * Revision 1.32 2002/03/04 18:24:53 oes
93 * Re-enabled output of unknown config directive hash
95 * Revision 1.31 2002/03/03 15:07:20 oes
96 * Re-enabled automatic config reloading
98 * Revision 1.30 2002/01/22 23:31:43 jongfoster
99 * Replacing strsav() with string_append()
101 * Revision 1.29 2002/01/17 21:02:30 jongfoster
102 * Moving all our URL and URL pattern parsing code to urlmatch.c.
104 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
106 * Revision 1.28 2001/12/30 14:07:32 steudten
107 * - Add signal handling (unix)
108 * - Add SIGHUP handler (unix)
109 * - Add creation of pidfile (unix)
110 * - Add action 'top' in rc file (RH)
111 * - Add entry 'SIGNALS' to manpage
112 * - Add exit message to logfile (unix)
114 * Revision 1.27 2001/11/07 00:02:13 steudten
115 * Add line number in error output for lineparsing for
116 * actionsfile and configfile.
117 * Special handling for CLF added.
119 * Revision 1.26 2001/11/05 21:41:43 steudten
120 * Add changes to be a real daemon just for unix os.
121 * (change cwd to /, detach from controlling tty, set
122 * process group and session leader to the own process.
124 * Add some fatal-error log message for failed malloc().
125 * Add '-d' if compiled with 'configure --with-debug' to
126 * enable debug output.
128 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
129 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
130 * threads to call select() simultaneously. So, it's time to do a real, live,
131 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
132 * (native). Both versions will work, but using __OS2__ offers multi-threading.
134 * Revision 1.24 2001/10/23 21:40:30 jongfoster
135 * Added support for enable-edit-actions and enable-remote-toggle config
138 * Revision 1.23 2001/10/07 15:36:00 oes
139 * Introduced new config option "buffer-limit"
141 * Revision 1.22 2001/09/22 16:36:59 jongfoster
142 * Removing unused parameter fs from read_config_line()
144 * Revision 1.21 2001/09/16 17:10:43 jongfoster
145 * Moving function savearg() here, since it was the only thing left in
148 * Revision 1.20 2001/07/30 22:08:36 jongfoster
149 * Tidying up #defines:
150 * - All feature #defines are now of the form FEATURE_xxx
151 * - Permanently turned off WIN_GUI_EDIT
152 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
154 * Revision 1.19 2001/07/15 17:45:16 jongfoster
155 * Removing some unused #includes
157 * Revision 1.18 2001/07/13 14:01:14 oes
158 * - Removed all #ifdef PCRS
159 * - Removed vim-settings
161 * Revision 1.17 2001/06/29 13:31:03 oes
162 * - Improved comments
163 * - Fixed (actionsfile) and sorted hashes
164 * - Introduced admin_address and proxy-info-url
165 * as config parameters
166 * - Renamed config->proxy_args_invocation (which didn't have
167 * the invocation but the options!) to config->proxy_args
168 * - Various adaptions
169 * - Removed logentry from cancelled commit
171 * Revision 1.16 2001/06/09 10:55:28 jongfoster
172 * Changing BUFSIZ ==> BUFFER_SIZE
174 * Revision 1.15 2001/06/07 23:13:40 jongfoster
175 * Merging ACL and forward files into config file.
176 * Cosmetic: Sorting config file options alphabetically.
177 * Cosmetic: Adding brief syntax comments to config file options.
179 * Revision 1.14 2001/06/07 14:46:25 joergs
180 * Missing make_path() added for re_filterfile.
182 * Revision 1.13 2001/06/05 22:33:54 jongfoster
184 * Fixed minor memory leak.
185 * Also now uses make_path to prepend the pathnames.
187 * Revision 1.12 2001/06/05 20:04:09 jongfoster
188 * Now uses _snprintf() in place of snprintf() under Win32.
190 * Revision 1.11 2001/06/04 18:31:58 swa
191 * files are now prefixed with either `confdir' or `logdir'.
192 * `make redhat-dist' replaces both entries confdir and logdir
195 * Revision 1.10 2001/06/03 19:11:54 oes
196 * introduced confdir option
198 * Revision 1.9 2001/06/01 20:06:24 jongfoster
199 * Removed support for "tinygif" option - moved to actions file.
201 * Revision 1.8 2001/05/31 21:27:13 jongfoster
202 * Removed many options from the config file and into the
203 * "actions" file: add_forwarded, suppress_vanilla_wafer,
204 * wafer, add_header, user_agent, referer, from
205 * Also globally replaced "permission" with "action".
207 * Revision 1.7 2001/05/29 09:50:24 jongfoster
208 * Unified blocklist/imagelist/permissionslist.
209 * File format is still under discussion, but the internal changes
212 * Also modified interceptor behaviour:
213 * - We now intercept all URLs beginning with one of the following
214 * prefixes (and *only* these prefixes):
216 * * http://ijbswa.sf.net/config/
217 * * http://ijbswa.sourceforge.net/config/
218 * - New interceptors "home page" - go to http://i.j.b/ to see it.
219 * - Internal changes so that intercepted and fast redirect pages
220 * are not replaced with an image.
221 * - Interceptors now have the option to send a binary page direct
222 * to the client. (i.e. ijb-send-banner uses this)
223 * - Implemented show-url-info interceptor. (Which is why I needed
224 * the above interceptors changes - a typical URL is
225 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
226 * The previous mechanism would not have intercepted that, and
227 * if it had been intercepted then it then it would have replaced
230 * Revision 1.6 2001/05/26 00:28:36 jongfoster
231 * Automatic reloading of config file.
232 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
233 * Most of the global variables have been moved to a new
234 * struct configuration_spec, accessed through csp->config->globalname
235 * Most of the globals remaining are used by the Win32 GUI.
237 * Revision 1.5 2001/05/25 22:34:30 jongfoster
240 * Revision 1.4 2001/05/22 18:46:04 oes
242 * - Enabled filtering banners by size rather than URL
243 * by adding patterns that replace all standard banner
244 * sizes with the "Junkbuster" gif to the re_filterfile
246 * - Enabled filtering WebBugs by providing a pattern
247 * which kills all 1x1 images
249 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
250 * which is selected by the (nonstandard and therefore
251 * capital) letter 'U' in the option string.
252 * It causes the quantifiers to be ungreedy by default.
253 * Appending a ? turns back to greedy (!).
255 * - Added a new interceptor ijb-send-banner, which
256 * sends back the "Junkbuster" gif. Without imagelist or
257 * MSIE detection support, or if tinygif = 1, or the
258 * URL isn't recognized as an imageurl, a lame HTML
259 * explanation is sent instead.
261 * - Added new feature, which permits blocking remote
262 * script redirects and firing back a local redirect
264 * The feature is conditionally compiled, i.e. it
265 * can be disabled with --disable-fast-redirects,
266 * plus it must be activated by a "fast-redirects"
267 * line in the config file, has its own log level
268 * and of course wants to be displayed by show-proxy-args
269 * Note: Boy, all the #ifdefs in 1001 locations and
270 * all the fumbling with configure.in and acconfig.h
271 * were *way* more work than the feature itself :-(
273 * - Because a generic redirect template was needed for
274 * this, tinygif = 3 now uses the same.
276 * - Moved GIFs, and other static HTTP response templates
281 * - Removed some >400 CRs again (Jon, you really worked
284 * Revision 1.3 2001/05/20 01:21:20 jongfoster
285 * Version 2.9.4 checkin.
286 * - Merged popupfile and cookiefile, and added control over PCRS
287 * filtering, in new "permissionsfile".
288 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
289 * file error you now get a message box (in the Win32 GUI) rather
290 * than the program exiting with no explanation.
291 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
293 * - Removed tabs from "config"
294 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
295 * - Bumped up version number.
297 * Revision 1.2 2001/05/17 23:01:01 oes
298 * - Cleaned CRLF's from the sources and related files
300 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
301 * Initial import of version 2.9.3 source tree
304 *********************************************************************/
310 #include <sys/types.h>
324 # include <windows.h>
327 # ifndef _WIN_CONSOLE
329 # endif /* ndef _WIN_CONSOLE */
331 /* VC++ has "_snprintf", not "snprintf" */
332 #define snprintf _snprintf
334 #else /* ifndef _WIN32 */
338 # include <sys/wait.h>
340 # include <sys/time.h>
341 # include <sys/stat.h>
351 #include "miscutil.h"
355 #include "urlmatch.h"
358 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
361 * Fix a problem with Solaris. There should be no effect on other
363 * Solaris's isspace() is a macro which uses it's argument directly
364 * as an array index. Therefore we need to make sure that high-bit
365 * characters generate +ve values, and ideally we also want to make
366 * the argument match the declared parameter type of "int".
368 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
369 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
371 #ifdef FEATURE_TOGGLE
372 /* by haroon - indicates if ijb is enabled */
373 int g_bToggleIJB = 1; /* Privoxy is enabled by default. */
374 #endif /* def FEATURE_TOGGLE */
376 /* The filename of the configfile */
377 const char *configfile = NULL;
380 * CGI functions will later need access to the invocation args,
381 * so we will make argc and argv global.
384 const char **Argv = NULL;
386 static struct file_list *current_configfile = NULL;
390 * This takes the "cryptic" hash of each keyword and aliases them to
391 * something a little more readable. This also makes changing the
392 * hash values easier if they should change or the hash algorthm changes.
393 * Use the included "hash" program to find out what the hash will be
394 * for any string supplied on the command line. (Or just put it in the
395 * config file and read the number from the error message in the log).
397 * Please keep this list sorted alphabetically (but with the Windows
398 * console and GUI specific options last).
401 #define hash_actions_file 1196306641ul /* "actionsfile" */
402 #define hash_admin_address 4112573064ul /* "admin-address" */
403 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
404 #define hash_confdir 1978389ul /* "confdir" */
405 #define hash_debug 78263ul /* "debug" */
406 #define hash_deny_access 1227333715ul /* "deny-access" */
407 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
408 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
409 #define hash_filterfile 250887266ul /* "filterfile" */
410 #define hash_forward 2029845ul /* "forward" */
411 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
412 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
413 #define hash_jarfile 2046641ul /* "jarfile" */
414 #define hash_listen_address 1255650842ul /* "listen-address" */
415 #define hash_logdir 422889ul /* "logdir" */
416 #define hash_logfile 2114766ul /* "logfile" */
417 #define hash_permit_access 3587953268ul /* "permit-access" */
418 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
419 #define hash_single_threaded 4250084780ul /* "single-threaded" */
420 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
421 #define hash_toggle 447966ul /* "toggle" */
422 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
423 #define hash_trustfile 56494766ul /* "trustfile" */
424 #define hash_usermanual 1416668518ul /* "user-manual" */
425 #define hash_activity_animation 1817904738ul /* "activity-animation" */
426 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
427 #define hash_hide_console 2048809870ul /* "hide-console" */
428 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
429 #define hash_log_font_name 2866730124ul /* "log-font-name" */
430 #define hash_log_font_size 2866731014ul /* "log-font-size" */
431 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
432 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
433 #define hash_log_messages 2291744899ul /* "log-messages" */
434 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
437 static void savearg(char *command, char *argument, struct configuration_spec * config);
439 /*********************************************************************
441 * Function : unload_configfile
443 * Description : Free the config structure and all components.
446 * 1 : data: struct configuration_spec to unload
450 *********************************************************************/
451 void unload_configfile (void * data)
453 struct configuration_spec * config = (struct configuration_spec *)data;
454 struct forward_spec *cur_fwd = config->forward;
456 struct access_control_list *cur_acl = config->acl;
459 while (cur_acl != NULL)
461 struct access_control_list * next_acl = cur_acl->next;
466 #endif /* def FEATURE_ACL */
468 while (cur_fwd != NULL)
470 struct forward_spec * next_fwd = cur_fwd->next;
471 free_url_spec(cur_fwd->url);
473 freez(cur_fwd->gateway_host);
474 freez(cur_fwd->forward_host);
478 config->forward = NULL;
480 #ifdef FEATURE_COOKIE_JAR
481 if ( NULL != config->jar )
483 fclose( config->jar );
486 #endif /* def FEATURE_COOKIE_JAR */
488 freez(config->confdir);
489 freez(config->logdir);
491 freez(config->haddr);
492 freez(config->logfile);
494 for (i = 0; i < MAX_ACTION_FILES; i++)
496 freez(config->actions_file_short[i]);
497 freez(config->actions_file[i]);
500 freez(config->admin_address);
501 freez(config->proxy_info_url);
502 freez(config->proxy_args);
503 freez(config->usermanual);
505 #ifdef FEATURE_COOKIE_JAR
506 freez(config->jarfile);
507 #endif /* def FEATURE_COOKIE_JAR */
509 freez(config->re_filterfile);
514 #ifdef FEATURE_GRACEFUL_TERMINATION
515 /*********************************************************************
517 * Function : unload_current_config_file
519 * Description : Unloads current config file - reset to state at
520 * beginning of program.
526 *********************************************************************/
527 void unload_current_config_file(void)
529 if (current_configfile)
531 current_configfile->unloader = unload_configfile;
532 current_configfile = NULL;
538 /*********************************************************************
540 * Function : load_config
542 * Description : Load the config file and all parameters.
546 * Returns : The configuration_spec, or NULL on error.
548 *********************************************************************/
549 struct configuration_spec * load_config(void)
551 char buf[BUFFER_SIZE];
553 FILE *configfp = NULL;
554 struct configuration_spec * config = NULL;
555 struct client_state * fake_csp;
556 struct file_list *fs;
557 unsigned long linenum = 0;
560 if ( !check_file_changed(current_configfile, configfile, &fs))
562 /* No need to load */
563 return ((struct configuration_spec *)current_configfile->f);
567 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
571 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
573 #ifdef FEATURE_TOGGLE
575 #endif /* def FEATURE_TOGGLE */
577 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
583 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
584 /* Never get here - LOG_LEVEL_FATAL causes program exit */
588 * This is backwards from how it's usually done.
589 * Following the usual pattern, "fs" would be stored in a member
590 * variable in "csp", and then we'd access "config" from "fs->f",
591 * using a cast. However, "config" is used so often that a
592 * cast each time would be very ugly, and the extra indirection
593 * would waste CPU cycles. Therefore we store "config" in
594 * "csp->config", and "fs" in "csp->config->config_file_list".
596 config->config_file_list = fs;
601 config->multi_threaded = 1;
602 config->hport = HADDR_PORT;
603 config->buffer_limit = 4096 * 1024;
604 config->usermanual = strdup(USER_MANUAL_URL);
605 config->proxy_args = strdup("");
607 if ((configfp = fopen(configfile, "r")) == NULL)
609 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
611 /* Never get here - LOG_LEVEL_FATAL causes program exit */
614 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
616 char cmd[BUFFER_SIZE];
617 char arg[BUFFER_SIZE];
618 char tmp[BUFFER_SIZE];
620 struct access_control_list *cur_acl;
621 #endif /* def FEATURE_ACL */
622 struct forward_spec *cur_fwd;
628 /* Copy command (i.e. up to space or tab) into cmd */
631 while (*p && (*p != ' ') && (*p != '\t'))
637 /* Skip over the whitespace in buf */
638 while (*p && ((*p == ' ') || (*p == '\t')))
643 /* Copy the argument into arg */
646 /* Should never happen, but check this anyway */
652 /* Make sure the command field is lower case */
657 *p = ijb_tolower(*p);
661 /* Save the argument for show-proxy-args */
662 savearg(cmd, arg, config);
665 switch( hash_string( cmd ) )
667 /* *************************************************************************
668 * actionsfile actions-file-name
669 * In confdir by default
670 * *************************************************************************/
671 case hash_actions_file :
673 while ((i < MAX_ACTION_FILES) && (NULL != config->actions_file[i]))
678 if (i >= MAX_ACTION_FILES)
680 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
681 "(You can increase this limit by changing MAX_ACTION_FILES in project.h and recompiling).",
684 config->actions_file_short[i] = strdup(arg);
685 p = malloc(strlen(arg) + sizeof(".action"));
688 log_error(LOG_LEVEL_FATAL, "Out of memory");
691 strcat(p, ".action");
692 config->actions_file[i] = make_path(config->confdir, p);
696 /* *************************************************************************
697 * admin-address email-address
698 * *************************************************************************/
699 case hash_admin_address :
700 freez(config->admin_address);
701 config->admin_address = strdup(arg);
704 /* *************************************************************************
706 * *************************************************************************/
707 case hash_buffer_limit :
708 config->buffer_limit = (size_t) 1024 * atoi(arg);
711 /* *************************************************************************
712 * confdir directory-name
713 * *************************************************************************/
715 freez(config->confdir);
716 config->confdir = make_path( NULL, arg);
719 /* *************************************************************************
721 * Specifies debug level, multiple values are ORed together.
722 * *************************************************************************/
724 config->debug |= atoi(arg);
727 /* *************************************************************************
728 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
729 * *************************************************************************/
731 case hash_deny_access:
732 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
734 if ((vec_count != 1) && (vec_count != 2))
736 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
737 "deny-access directive in configuration file.");
738 string_append(&config->proxy_args,
739 "<br>\nWARNING: Wrong number of parameters for "
740 "deny-access directive in configuration file.<br><br>\n");
744 /* allocate a new node */
745 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
749 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
750 /* Never get here - LOG_LEVEL_FATAL causes program exit */
753 cur_acl->action = ACL_DENY;
755 if (acl_addr(vec[0], cur_acl->src) < 0)
757 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
758 "directive in configuration file: \"%s\"", vec[0]);
759 string_append(&config->proxy_args,
760 "<br>\nWARNING: Invalid source IP for deny-access directive"
761 " in configuration file: \"");
762 string_append(&config->proxy_args,
764 string_append(&config->proxy_args,
771 if (acl_addr(vec[1], cur_acl->dst) < 0)
773 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
774 "directive in configuration file: \"%s\"", vec[0]);
775 string_append(&config->proxy_args,
776 "<br>\nWARNING: Invalid destination IP for deny-access directive"
777 " in configuration file: \"");
778 string_append(&config->proxy_args,
780 string_append(&config->proxy_args,
788 * Add it to the list. Note we reverse the list to get the
789 * behaviour the user expects. With both the ACL and
790 * actions file, the last match wins. However, the internal
791 * implementations are different: The actions file is stored
792 * in the same order as the file, and scanned completely.
793 * With the ACL, we reverse the order as we load it, then
794 * when we scan it we stop as soon as we get a match.
796 cur_acl->next = config->acl;
797 config->acl = cur_acl;
800 #endif /* def FEATURE_ACL */
802 /* *************************************************************************
803 * enable-edit-actions 0|1
804 * *************************************************************************/
805 #ifdef FEATURE_CGI_EDIT_ACTIONS
806 case hash_enable_edit_actions:
807 if ((*arg != '\0') && (0 != atoi(arg)))
809 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
813 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
816 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
818 /* *************************************************************************
819 * enable-remote-toggle 0|1
820 * *************************************************************************/
821 #ifdef FEATURE_CGI_EDIT_ACTIONS
822 case hash_enable_remote_toggle:
823 if ((*arg != '\0') && (0 != atoi(arg)))
825 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
829 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
832 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
834 /* *************************************************************************
835 * forward url-pattern (.|http-proxy-host[:port])
836 * *************************************************************************/
838 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
842 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
843 "directive in configuration file.");
844 string_append(&config->proxy_args,
845 "<br>\nWARNING: Wrong number of parameters for "
846 "forward directive in configuration file.");
850 /* allocate a new node */
851 cur_fwd = zalloc(sizeof(*cur_fwd));
854 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
855 /* Never get here - LOG_LEVEL_FATAL causes program exit */
859 cur_fwd->type = SOCKS_NONE;
861 /* Save the URL pattern */
862 if (create_url_spec(cur_fwd->url, vec[0]))
864 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
865 "directive in configuration file.");
866 string_append(&config->proxy_args,
867 "<br>\nWARNING: Bad URL specifier for "
868 "forward directive in configuration file.");
872 /* Parse the parent HTTP proxy host:port */
875 if (strcmp(p, ".") != 0)
877 cur_fwd->forward_host = strdup(p);
879 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
882 cur_fwd->forward_port = atoi(p);
885 if (cur_fwd->forward_port <= 0)
887 cur_fwd->forward_port = 8000;
892 cur_fwd->next = config->forward;
893 config->forward = cur_fwd;
897 /* *************************************************************************
898 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
899 * *************************************************************************/
900 case hash_forward_socks4:
901 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
905 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
906 "forward-socks4 directive in configuration file.");
907 string_append(&config->proxy_args,
908 "<br>\nWARNING: Wrong number of parameters for "
909 "forward-socks4 directive in configuration file.");
913 /* allocate a new node */
914 cur_fwd = zalloc(sizeof(*cur_fwd));
917 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
918 /* Never get here - LOG_LEVEL_FATAL causes program exit */
922 cur_fwd->type = SOCKS_4;
924 /* Save the URL pattern */
925 if (create_url_spec(cur_fwd->url, vec[0]))
927 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
928 "directive in configuration file.");
929 string_append(&config->proxy_args,
930 "<br>\nWARNING: Bad URL specifier for "
931 "forward-socks4 directive in configuration file.");
935 /* Parse the SOCKS proxy host[:port] */
938 if (strcmp(p, ".") != 0)
940 cur_fwd->gateway_host = strdup(p);
942 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
945 cur_fwd->gateway_port = atoi(p);
947 if (cur_fwd->gateway_port <= 0)
949 cur_fwd->gateway_port = 1080;
953 /* Parse the parent HTTP proxy host[:port] */
956 if (strcmp(p, ".") != 0)
958 cur_fwd->forward_host = strdup(p);
960 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
963 cur_fwd->forward_port = atoi(p);
966 if (cur_fwd->forward_port <= 0)
968 cur_fwd->forward_port = 8000;
973 cur_fwd->next = config->forward;
974 config->forward = cur_fwd;
978 /* *************************************************************************
979 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
980 * *************************************************************************/
981 case hash_forward_socks4a:
982 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
986 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
987 "forward-socks4a directive in configuration file.");
988 string_append(&config->proxy_args,
989 "<br>\nWARNING: Wrong number of parameters for "
990 "forward-socks4a directive in configuration file.");
994 /* allocate a new node */
995 cur_fwd = zalloc(sizeof(*cur_fwd));
998 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
999 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1003 cur_fwd->type = SOCKS_4A;
1005 /* Save the URL pattern */
1006 if (create_url_spec(cur_fwd->url, vec[0]))
1008 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
1009 "directive in configuration file.");
1010 string_append(&config->proxy_args,
1011 "<br>\nWARNING: Bad URL specifier for "
1012 "forward-socks4a directive in configuration file.");
1016 /* Parse the SOCKS proxy host[:port] */
1019 cur_fwd->gateway_host = strdup(p);
1021 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1024 cur_fwd->gateway_port = atoi(p);
1026 if (cur_fwd->gateway_port <= 0)
1028 cur_fwd->gateway_port = 1080;
1031 /* Parse the parent HTTP proxy host[:port] */
1034 if (strcmp(p, ".") != 0)
1036 cur_fwd->forward_host = strdup(p);
1038 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1041 cur_fwd->forward_port = atoi(p);
1044 if (cur_fwd->forward_port <= 0)
1046 cur_fwd->forward_port = 8000;
1051 cur_fwd->next = config->forward;
1052 config->forward = cur_fwd;
1056 /* *************************************************************************
1057 * jarfile jar-file-name
1058 * In logdir by default
1059 * *************************************************************************/
1060 #ifdef FEATURE_COOKIE_JAR
1062 freez(config->jarfile);
1063 config->jarfile = make_path(config->logdir, arg);
1065 #endif /* def FEATURE_COOKIE_JAR */
1067 /* *************************************************************************
1068 * listen-address [ip][:port]
1069 * *************************************************************************/
1070 case hash_listen_address :
1071 freez(config->haddr);
1072 config->haddr = strdup(arg);
1075 /* *************************************************************************
1076 * logdir directory-name
1077 * *************************************************************************/
1079 freez(config->logdir);
1080 config->logdir = make_path(NULL, arg);
1083 /* *************************************************************************
1084 * logfile log-file-name
1085 * In logdir by default
1086 * *************************************************************************/
1088 freez(config->logfile);
1089 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1092 /* *************************************************************************
1093 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1094 * *************************************************************************/
1096 case hash_permit_access:
1097 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1099 if ((vec_count != 1) && (vec_count != 2))
1101 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1102 "permit-access directive in configuration file.");
1103 string_append(&config->proxy_args,
1104 "<br>\nWARNING: Wrong number of parameters for "
1105 "permit-access directive in configuration file.<br><br>\n");
1110 /* allocate a new node */
1111 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1113 if (cur_acl == NULL)
1115 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1116 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1119 cur_acl->action = ACL_PERMIT;
1121 if (acl_addr(vec[0], cur_acl->src) < 0)
1123 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1124 "directive in configuration file: \"%s\"", vec[0]);
1125 string_append(&config->proxy_args,
1126 "<br>\nWARNING: Invalid source IP for permit-access directive"
1127 " in configuration file: \"");
1128 string_append(&config->proxy_args,
1130 string_append(&config->proxy_args,
1137 if (acl_addr(vec[1], cur_acl->dst) < 0)
1139 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1140 "permit-access directive in configuration file: \"%s\"",
1142 string_append(&config->proxy_args,
1143 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1144 " in configuration file: \"");
1145 string_append(&config->proxy_args,
1147 string_append(&config->proxy_args,
1155 * Add it to the list. Note we reverse the list to get the
1156 * behaviour the user expects. With both the ACL and
1157 * actions file, the last match wins. However, the internal
1158 * implementations are different: The actions file is stored
1159 * in the same order as the file, and scanned completely.
1160 * With the ACL, we reverse the order as we load it, then
1161 * when we scan it we stop as soon as we get a match.
1163 cur_acl->next = config->acl;
1164 config->acl = cur_acl;
1167 #endif /* def FEATURE_ACL */
1169 /* *************************************************************************
1170 * proxy-info-url url
1171 * *************************************************************************/
1172 case hash_proxy_info_url :
1173 freez(config->proxy_info_url);
1174 config->proxy_info_url = strdup(arg);
1177 /* *************************************************************************
1178 * re_filterfile file-name
1179 * In confdir by default.
1180 * *************************************************************************/
1181 case hash_filterfile :
1182 freez(config->re_filterfile);
1183 config->re_filterfile = make_path(config->confdir, arg);
1186 /* *************************************************************************
1188 * *************************************************************************/
1189 case hash_single_threaded :
1190 config->multi_threaded = 0;
1193 /* *************************************************************************
1195 * *************************************************************************/
1196 #ifdef FEATURE_TOGGLE
1198 g_bToggleIJB = atoi(arg);
1200 #endif /* def FEATURE_TOGGLE */
1202 /* *************************************************************************
1203 * trust-info-url url
1204 * *************************************************************************/
1205 #ifdef FEATURE_TRUST
1206 case hash_trust_info_url :
1207 enlist(config->trust_info, arg);
1209 #endif /* def FEATURE_TRUST */
1211 /* *************************************************************************
1212 * trustfile filename
1213 * (In confdir by default.)
1214 * *************************************************************************/
1215 #ifdef FEATURE_TRUST
1216 case hash_trustfile :
1217 freez(config->trustfile);
1218 config->trustfile = make_path(config->confdir, arg);
1220 #endif /* def FEATURE_TRUST */
1222 /* *************************************************************************
1224 * *************************************************************************/
1225 case hash_usermanual :
1226 freez(config->usermanual);
1227 config->usermanual = strdup(arg);
1230 /* *************************************************************************
1231 * Win32 Console options:
1232 * *************************************************************************/
1234 /* *************************************************************************
1236 * *************************************************************************/
1238 case hash_hide_console :
1241 #endif /*def _WIN_CONSOLE*/
1244 /* *************************************************************************
1245 * Win32 GUI options:
1246 * *************************************************************************/
1248 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1249 /* *************************************************************************
1250 * activity-animation (0|1)
1251 * *************************************************************************/
1252 case hash_activity_animation :
1253 g_bShowActivityAnimation = atoi(arg);
1256 /* *************************************************************************
1257 * close-button-minimizes (0|1)
1258 * *************************************************************************/
1259 case hash_close_button_minimizes :
1260 g_bCloseHidesWindow = atoi(arg);
1263 /* *************************************************************************
1264 * log-buffer-size (0|1)
1265 * *************************************************************************/
1266 case hash_log_buffer_size :
1267 g_bLimitBufferSize = atoi(arg);
1270 /* *************************************************************************
1271 * log-font-name fontnane
1272 * *************************************************************************/
1273 case hash_log_font_name :
1274 strcpy( g_szFontFaceName, arg );
1277 /* *************************************************************************
1279 * *************************************************************************/
1280 case hash_log_font_size :
1281 g_nFontSize = atoi(arg);
1284 /* *************************************************************************
1285 * log-highlight-messages (0|1)
1286 * *************************************************************************/
1287 case hash_log_highlight_messages :
1288 g_bHighlightMessages = atoi(arg);
1291 /* *************************************************************************
1293 * *************************************************************************/
1294 case hash_log_max_lines :
1295 g_nMaxBufferLines = atoi(arg);
1298 /* *************************************************************************
1299 * log-messages (0|1)
1300 * *************************************************************************/
1301 case hash_log_messages :
1302 g_bLogMessages = atoi(arg);
1305 /* *************************************************************************
1306 * show-on-task-bar (0|1)
1307 * *************************************************************************/
1308 case hash_show_on_task_bar :
1309 g_bShowOnTaskBar = atoi(arg);
1312 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1315 /* *************************************************************************
1316 * Warnings about unsupported features
1317 * *************************************************************************/
1319 case hash_deny_access:
1320 #endif /* ndef FEATURE_ACL */
1321 #ifndef FEATURE_CGI_EDIT_ACTIONS
1322 case hash_enable_edit_actions:
1323 case hash_enable_remote_toggle:
1324 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1325 #ifndef FEATURE_COOKIE_JAR
1327 #endif /* ndef FEATURE_COOKIE_JAR */
1329 case hash_permit_access:
1330 #endif /* ndef FEATURE_ACL */
1331 #ifndef FEATURE_TOGGLE
1333 #endif /* ndef FEATURE_TOGGLE */
1334 #ifndef FEATURE_TRUST
1335 case hash_trustfile :
1336 case hash_trust_info_url :
1337 #endif /* ndef FEATURE_TRUST */
1339 #ifndef _WIN_CONSOLE
1340 case hash_hide_console :
1341 #endif /* ndef _WIN_CONSOLE */
1343 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1344 case hash_activity_animation :
1345 case hash_close_button_minimizes :
1346 case hash_log_buffer_size :
1347 case hash_log_font_name :
1348 case hash_log_font_size :
1349 case hash_log_highlight_messages :
1350 case hash_log_max_lines :
1351 case hash_log_messages :
1352 case hash_show_on_task_bar :
1353 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1354 /* These warnings are annoying - so hide them. -- Jon */
1355 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1358 /* *************************************************************************/
1360 /* *************************************************************************/
1362 * I decided that I liked this better as a warning than an
1363 * error. To change back to an error, just change log level
1364 * to LOG_LEVEL_FATAL.
1366 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1367 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1368 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1369 string_append(&config->proxy_args, buf);
1370 string_append(&config->proxy_args, "<br><br>\n");
1373 /* *************************************************************************/
1374 } /* end switch( hash_string(cmd) ) */
1375 } /* end while ( read_config_line(...) ) */
1379 if (NULL == config->proxy_args)
1381 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1384 init_error_log(Argv[0], config->logfile, config->debug);
1386 if (config->actions_file[0])
1388 add_loader(load_actions_file, config);
1391 if (config->re_filterfile)
1393 add_loader(load_re_filterfile, config);
1396 #ifdef FEATURE_TRUST
1397 if (config->trustfile)
1399 add_loader(load_trustfile, config);
1401 #endif /* def FEATURE_TRUST */
1403 #ifdef FEATURE_COOKIE_JAR
1404 if ( NULL != config->jarfile )
1406 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1408 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1409 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1411 setbuf(config->jar, NULL);
1413 #endif /* def FEATURE_COOKIE_JAR */
1415 if ( NULL == config->haddr )
1417 config->haddr = strdup( HADDR_DEFAULT );
1420 if ( NULL != config->haddr )
1422 if (NULL != (p = strchr(config->haddr, ':')))
1427 config->hport = atoi(p);
1431 if (config->hport <= 0)
1434 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1435 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1437 if (*config->haddr == '\0')
1439 config->haddr = NULL;
1444 * Want to run all the loaders once now.
1446 * Need to set up a fake csp, so they can get to the config.
1448 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1449 fake_csp->config = config;
1451 if (run_loader(fake_csp))
1454 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1455 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1459 /* FIXME: this is a kludge for win32 */
1460 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1462 g_actions_file = config->actions_file[0]; /* FIXME only works for first action file */
1463 g_re_filterfile = config->re_filterfile;
1465 #ifdef FEATURE_TRUST
1466 g_trustfile = config->trustfile;
1467 #endif /* def FEATURE_TRUST */
1470 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1471 /* FIXME: end kludge */
1474 config->need_bind = 1;
1476 if (current_configfile)
1478 struct configuration_spec * oldcfg = (struct configuration_spec *)
1479 current_configfile->f;
1481 * Check if config->haddr,hport == oldcfg->haddr,hport
1483 * The following could be written more compactly as a single,
1484 * (unreadably long) if statement.
1486 config->need_bind = 0;
1487 if (config->hport != oldcfg->hport)
1489 config->need_bind = 1;
1491 else if (config->haddr == NULL)
1493 if (oldcfg->haddr != NULL)
1495 config->need_bind = 1;
1498 else if (oldcfg->haddr == NULL)
1500 config->need_bind = 1;
1502 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1504 config->need_bind = 1;
1507 current_configfile->unloader = unload_configfile;
1510 fs->next = files->next;
1513 current_configfile = fs;
1519 /*********************************************************************
1521 * Function : savearg
1523 * Description : Called from `load_config'. It saves each non-empty
1524 * and non-comment line from config into
1525 * config->proxy_args. This is used to create the
1526 * show-proxy-args page. On error, frees
1527 * config->proxy_args and sets it to NULL
1530 * 1 : command = config setting that was found
1531 * 2 : argument = the setting's argument (if any)
1532 * 3 : config = Configuration to save into.
1536 *********************************************************************/
1537 static void savearg(char *command, char *argument, struct configuration_spec * config)
1547 * Add config option name embedded in
1548 * link to it's section in the user-manual
1550 buf = strdup("<a href=\"");
1551 string_append(&buf, config->usermanual);
1552 string_append(&buf, CONFIG_HELP_PREFIX);
1553 string_join (&buf, string_toupper(command));
1554 string_append(&buf, "\">");
1555 string_append(&buf, command);
1556 string_append(&buf, "</a> ");
1560 freez(config->proxy_args);
1564 if ( (NULL != argument) && ('\0' != *argument) )
1566 s = html_encode(argument);
1570 freez(config->proxy_args);
1574 if (strncmpic(argument, "http://", 7) == 0)
1576 string_append(&buf, "<a href=\"");
1577 string_append(&buf, s);
1578 string_append(&buf, "\">");
1579 string_join (&buf, s);
1580 string_append(&buf, "</a>");
1584 string_join (&buf, s);
1588 string_append(&buf, "<br>\n");
1590 string_join(&config->proxy_args, buf);