1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.48.2.4 2003/04/11 12:06:14 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.4 2003/04/11 12:06:14 oes
39 * Addressed bug #719435
40 * - Extraneous filterfile directives now logged as errors
41 * - This and unrecnonised directives now really obvious on status page
43 * Revision 1.48.2.3 2003/03/11 11:53:59 oes
44 * Cosmetic: Renamed cryptic variable
46 * Revision 1.48.2.2 2002/11/12 16:28:20 oes
47 * Move unrelated variable declaration out of #ifdef FEATURE_ACL; fixes bug #636655
49 * Revision 1.48.2.1 2002/08/21 17:58:05 oes
50 * Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
52 * Revision 1.48 2002/05/14 21:30:38 oes
53 * savearg now uses own linking code instead of (now special-cased) add_help_link
55 * Revision 1.47 2002/05/12 21:36:29 jongfoster
56 * Correcting function comments
58 * Revision 1.46 2002/04/26 12:55:14 oes
59 * - New option "user-manual", defaults to our site
60 * via project.h #define
61 * - savearg now embeds option names in help links
63 * Revision 1.45 2002/04/24 02:11:54 oes
64 * Jon's multiple AF patch: Allow up to MAX_ACTION_FILES actionsfile options
66 * Revision 1.44 2002/04/08 20:37:13 swa
69 * Revision 1.43 2002/04/08 20:36:50 swa
72 * Revision 1.42 2002/04/05 15:50:15 oes
73 * fix for invalid HTML proxy_args
75 * Revision 1.41 2002/03/31 17:19:00 jongfoster
76 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
78 * Revision 1.40 2002/03/26 22:29:55 swa
79 * we have a new homepage!
81 * Revision 1.39 2002/03/24 13:25:43 swa
82 * name change related issues
84 * Revision 1.38 2002/03/24 13:05:48 jongfoster
85 * Renaming re_filterfile to filterfile
87 * Revision 1.37 2002/03/16 23:54:06 jongfoster
88 * Adding graceful termination feature, to help look for memory leaks.
89 * If you enable this (which, by design, has to be done by hand
90 * editing config.h) and then go to http://i.j.b/die, then the program
91 * will exit cleanly after the *next* request. It should free all the
92 * memory that was used.
94 * Revision 1.36 2002/03/13 00:27:05 jongfoster
97 * Revision 1.35 2002/03/07 03:52:44 oes
98 * Set logging to tty for --no-daemon mode
100 * Revision 1.34 2002/03/06 23:14:35 jongfoster
101 * Trivial cosmetic changes to make function comments easier to find.
103 * Revision 1.33 2002/03/05 04:52:42 oes
104 * Deleted non-errlog debugging code
106 * Revision 1.32 2002/03/04 18:24:53 oes
107 * Re-enabled output of unknown config directive hash
109 * Revision 1.31 2002/03/03 15:07:20 oes
110 * Re-enabled automatic config reloading
112 * Revision 1.30 2002/01/22 23:31:43 jongfoster
113 * Replacing strsav() with string_append()
115 * Revision 1.29 2002/01/17 21:02:30 jongfoster
116 * Moving all our URL and URL pattern parsing code to urlmatch.c.
118 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
120 * Revision 1.28 2001/12/30 14:07:32 steudten
121 * - Add signal handling (unix)
122 * - Add SIGHUP handler (unix)
123 * - Add creation of pidfile (unix)
124 * - Add action 'top' in rc file (RH)
125 * - Add entry 'SIGNALS' to manpage
126 * - Add exit message to logfile (unix)
128 * Revision 1.27 2001/11/07 00:02:13 steudten
129 * Add line number in error output for lineparsing for
130 * actionsfile and configfile.
131 * Special handling for CLF added.
133 * Revision 1.26 2001/11/05 21:41:43 steudten
134 * Add changes to be a real daemon just for unix os.
135 * (change cwd to /, detach from controlling tty, set
136 * process group and session leader to the own process.
138 * Add some fatal-error log message for failed malloc().
139 * Add '-d' if compiled with 'configure --with-debug' to
140 * enable debug output.
142 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
143 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
144 * threads to call select() simultaneously. So, it's time to do a real, live,
145 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
146 * (native). Both versions will work, but using __OS2__ offers multi-threading.
148 * Revision 1.24 2001/10/23 21:40:30 jongfoster
149 * Added support for enable-edit-actions and enable-remote-toggle config
152 * Revision 1.23 2001/10/07 15:36:00 oes
153 * Introduced new config option "buffer-limit"
155 * Revision 1.22 2001/09/22 16:36:59 jongfoster
156 * Removing unused parameter fs from read_config_line()
158 * Revision 1.21 2001/09/16 17:10:43 jongfoster
159 * Moving function savearg() here, since it was the only thing left in
162 * Revision 1.20 2001/07/30 22:08:36 jongfoster
163 * Tidying up #defines:
164 * - All feature #defines are now of the form FEATURE_xxx
165 * - Permanently turned off WIN_GUI_EDIT
166 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
168 * Revision 1.19 2001/07/15 17:45:16 jongfoster
169 * Removing some unused #includes
171 * Revision 1.18 2001/07/13 14:01:14 oes
172 * - Removed all #ifdef PCRS
173 * - Removed vim-settings
175 * Revision 1.17 2001/06/29 13:31:03 oes
176 * - Improved comments
177 * - Fixed (actionsfile) and sorted hashes
178 * - Introduced admin_address and proxy-info-url
179 * as config parameters
180 * - Renamed config->proxy_args_invocation (which didn't have
181 * the invocation but the options!) to config->proxy_args
182 * - Various adaptions
183 * - Removed logentry from cancelled commit
185 * Revision 1.16 2001/06/09 10:55:28 jongfoster
186 * Changing BUFSIZ ==> BUFFER_SIZE
188 * Revision 1.15 2001/06/07 23:13:40 jongfoster
189 * Merging ACL and forward files into config file.
190 * Cosmetic: Sorting config file options alphabetically.
191 * Cosmetic: Adding brief syntax comments to config file options.
193 * Revision 1.14 2001/06/07 14:46:25 joergs
194 * Missing make_path() added for re_filterfile.
196 * Revision 1.13 2001/06/05 22:33:54 jongfoster
198 * Fixed minor memory leak.
199 * Also now uses make_path to prepend the pathnames.
201 * Revision 1.12 2001/06/05 20:04:09 jongfoster
202 * Now uses _snprintf() in place of snprintf() under Win32.
204 * Revision 1.11 2001/06/04 18:31:58 swa
205 * files are now prefixed with either `confdir' or `logdir'.
206 * `make redhat-dist' replaces both entries confdir and logdir
209 * Revision 1.10 2001/06/03 19:11:54 oes
210 * introduced confdir option
212 * Revision 1.9 2001/06/01 20:06:24 jongfoster
213 * Removed support for "tinygif" option - moved to actions file.
215 * Revision 1.8 2001/05/31 21:27:13 jongfoster
216 * Removed many options from the config file and into the
217 * "actions" file: add_forwarded, suppress_vanilla_wafer,
218 * wafer, add_header, user_agent, referer, from
219 * Also globally replaced "permission" with "action".
221 * Revision 1.7 2001/05/29 09:50:24 jongfoster
222 * Unified blocklist/imagelist/permissionslist.
223 * File format is still under discussion, but the internal changes
226 * Also modified interceptor behaviour:
227 * - We now intercept all URLs beginning with one of the following
228 * prefixes (and *only* these prefixes):
230 * * http://ijbswa.sf.net/config/
231 * * http://ijbswa.sourceforge.net/config/
232 * - New interceptors "home page" - go to http://i.j.b/ to see it.
233 * - Internal changes so that intercepted and fast redirect pages
234 * are not replaced with an image.
235 * - Interceptors now have the option to send a binary page direct
236 * to the client. (i.e. ijb-send-banner uses this)
237 * - Implemented show-url-info interceptor. (Which is why I needed
238 * the above interceptors changes - a typical URL is
239 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
240 * The previous mechanism would not have intercepted that, and
241 * if it had been intercepted then it then it would have replaced
244 * Revision 1.6 2001/05/26 00:28:36 jongfoster
245 * Automatic reloading of config file.
246 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
247 * Most of the global variables have been moved to a new
248 * struct configuration_spec, accessed through csp->config->globalname
249 * Most of the globals remaining are used by the Win32 GUI.
251 * Revision 1.5 2001/05/25 22:34:30 jongfoster
254 * Revision 1.4 2001/05/22 18:46:04 oes
256 * - Enabled filtering banners by size rather than URL
257 * by adding patterns that replace all standard banner
258 * sizes with the "Junkbuster" gif to the re_filterfile
260 * - Enabled filtering WebBugs by providing a pattern
261 * which kills all 1x1 images
263 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
264 * which is selected by the (nonstandard and therefore
265 * capital) letter 'U' in the option string.
266 * It causes the quantifiers to be ungreedy by default.
267 * Appending a ? turns back to greedy (!).
269 * - Added a new interceptor ijb-send-banner, which
270 * sends back the "Junkbuster" gif. Without imagelist or
271 * MSIE detection support, or if tinygif = 1, or the
272 * URL isn't recognized as an imageurl, a lame HTML
273 * explanation is sent instead.
275 * - Added new feature, which permits blocking remote
276 * script redirects and firing back a local redirect
278 * The feature is conditionally compiled, i.e. it
279 * can be disabled with --disable-fast-redirects,
280 * plus it must be activated by a "fast-redirects"
281 * line in the config file, has its own log level
282 * and of course wants to be displayed by show-proxy-args
283 * Note: Boy, all the #ifdefs in 1001 locations and
284 * all the fumbling with configure.in and acconfig.h
285 * were *way* more work than the feature itself :-(
287 * - Because a generic redirect template was needed for
288 * this, tinygif = 3 now uses the same.
290 * - Moved GIFs, and other static HTTP response templates
295 * - Removed some >400 CRs again (Jon, you really worked
298 * Revision 1.3 2001/05/20 01:21:20 jongfoster
299 * Version 2.9.4 checkin.
300 * - Merged popupfile and cookiefile, and added control over PCRS
301 * filtering, in new "permissionsfile".
302 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
303 * file error you now get a message box (in the Win32 GUI) rather
304 * than the program exiting with no explanation.
305 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
307 * - Removed tabs from "config"
308 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
309 * - Bumped up version number.
311 * Revision 1.2 2001/05/17 23:01:01 oes
312 * - Cleaned CRLF's from the sources and related files
314 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
315 * Initial import of version 2.9.3 source tree
318 *********************************************************************/
324 #include <sys/types.h>
338 # include <windows.h>
341 # ifndef _WIN_CONSOLE
343 # endif /* ndef _WIN_CONSOLE */
345 /* VC++ has "_snprintf", not "snprintf" */
346 #define snprintf _snprintf
348 #else /* ifndef _WIN32 */
352 # include <sys/wait.h>
354 # include <sys/time.h>
355 # include <sys/stat.h>
365 #include "miscutil.h"
369 #include "urlmatch.h"
372 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
375 * Fix a problem with Solaris. There should be no effect on other
377 * Solaris's isspace() is a macro which uses it's argument directly
378 * as an array index. Therefore we need to make sure that high-bit
379 * characters generate +ve values, and ideally we also want to make
380 * the argument match the declared parameter type of "int".
382 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
383 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
385 #ifdef FEATURE_TOGGLE
386 /* Privoxy is enabled by default. */
387 int global_toggle_state = 1;
388 #endif /* def FEATURE_TOGGLE */
390 /* The filename of the configfile */
391 const char *configfile = NULL;
394 * CGI functions will later need access to the invocation args,
395 * so we will make argc and argv global.
398 const char **Argv = NULL;
400 static struct file_list *current_configfile = NULL;
404 * This takes the "cryptic" hash of each keyword and aliases them to
405 * something a little more readable. This also makes changing the
406 * hash values easier if they should change or the hash algorthm changes.
407 * Use the included "hash" program to find out what the hash will be
408 * for any string supplied on the command line. (Or just put it in the
409 * config file and read the number from the error message in the log).
411 * Please keep this list sorted alphabetically (but with the Windows
412 * console and GUI specific options last).
415 #define hash_actions_file 1196306641ul /* "actionsfile" */
416 #define hash_admin_address 4112573064ul /* "admin-address" */
417 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
418 #define hash_confdir 1978389ul /* "confdir" */
419 #define hash_debug 78263ul /* "debug" */
420 #define hash_deny_access 1227333715ul /* "deny-access" */
421 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
422 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
423 #define hash_filterfile 250887266ul /* "filterfile" */
424 #define hash_forward 2029845ul /* "forward" */
425 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
426 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
427 #define hash_jarfile 2046641ul /* "jarfile" */
428 #define hash_listen_address 1255650842ul /* "listen-address" */
429 #define hash_logdir 422889ul /* "logdir" */
430 #define hash_logfile 2114766ul /* "logfile" */
431 #define hash_permit_access 3587953268ul /* "permit-access" */
432 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
433 #define hash_single_threaded 4250084780ul /* "single-threaded" */
434 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
435 #define hash_toggle 447966ul /* "toggle" */
436 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
437 #define hash_trustfile 56494766ul /* "trustfile" */
438 #define hash_usermanual 1416668518ul /* "user-manual" */
439 #define hash_activity_animation 1817904738ul /* "activity-animation" */
440 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
441 #define hash_hide_console 2048809870ul /* "hide-console" */
442 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
443 #define hash_log_font_name 2866730124ul /* "log-font-name" */
444 #define hash_log_font_size 2866731014ul /* "log-font-size" */
445 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
446 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
447 #define hash_log_messages 2291744899ul /* "log-messages" */
448 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
451 static void savearg(char *command, char *argument, struct configuration_spec * config);
453 /*********************************************************************
455 * Function : unload_configfile
457 * Description : Free the config structure and all components.
460 * 1 : data: struct configuration_spec to unload
464 *********************************************************************/
465 void unload_configfile (void * data)
467 struct configuration_spec * config = (struct configuration_spec *)data;
468 struct forward_spec *cur_fwd = config->forward;
472 struct access_control_list *cur_acl = config->acl;
474 while (cur_acl != NULL)
476 struct access_control_list * next_acl = cur_acl->next;
481 #endif /* def FEATURE_ACL */
483 while (cur_fwd != NULL)
485 struct forward_spec * next_fwd = cur_fwd->next;
486 free_url_spec(cur_fwd->url);
488 freez(cur_fwd->gateway_host);
489 freez(cur_fwd->forward_host);
493 config->forward = NULL;
495 #ifdef FEATURE_COOKIE_JAR
496 if ( NULL != config->jar )
498 fclose( config->jar );
501 #endif /* def FEATURE_COOKIE_JAR */
503 freez(config->confdir);
504 freez(config->logdir);
506 freez(config->haddr);
507 freez(config->logfile);
509 for (i = 0; i < MAX_ACTION_FILES; i++)
511 freez(config->actions_file_short[i]);
512 freez(config->actions_file[i]);
515 freez(config->admin_address);
516 freez(config->proxy_info_url);
517 freez(config->proxy_args);
518 freez(config->usermanual);
520 #ifdef FEATURE_COOKIE_JAR
521 freez(config->jarfile);
522 #endif /* def FEATURE_COOKIE_JAR */
525 freez(config->trustfile);
526 list_remove_all(config->trust_info);
527 #endif /* def FEATURE_TRUST */
529 freez(config->re_filterfile);
534 #ifdef FEATURE_GRACEFUL_TERMINATION
535 /*********************************************************************
537 * Function : unload_current_config_file
539 * Description : Unloads current config file - reset to state at
540 * beginning of program.
546 *********************************************************************/
547 void unload_current_config_file(void)
549 if (current_configfile)
551 current_configfile->unloader = unload_configfile;
552 current_configfile = NULL;
558 /*********************************************************************
560 * Function : load_config
562 * Description : Load the config file and all parameters.
566 * Returns : The configuration_spec, or NULL on error.
568 *********************************************************************/
569 struct configuration_spec * load_config(void)
571 char buf[BUFFER_SIZE];
573 FILE *configfp = NULL;
574 struct configuration_spec * config = NULL;
575 struct client_state * fake_csp;
576 struct file_list *fs;
577 unsigned long linenum = 0;
580 if ( !check_file_changed(current_configfile, configfile, &fs))
582 /* No need to load */
583 return ((struct configuration_spec *)current_configfile->f);
587 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
591 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
593 #ifdef FEATURE_TOGGLE
594 global_toggle_state = 1;
595 #endif /* def FEATURE_TOGGLE */
597 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
603 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
604 /* Never get here - LOG_LEVEL_FATAL causes program exit */
608 * This is backwards from how it's usually done.
609 * Following the usual pattern, "fs" would be stored in a member
610 * variable in "csp", and then we'd access "config" from "fs->f",
611 * using a cast. However, "config" is used so often that a
612 * cast each time would be very ugly, and the extra indirection
613 * would waste CPU cycles. Therefore we store "config" in
614 * "csp->config", and "fs" in "csp->config->config_file_list".
616 config->config_file_list = fs;
621 config->multi_threaded = 1;
622 config->hport = HADDR_PORT;
623 config->buffer_limit = 4096 * 1024;
624 config->usermanual = strdup(USER_MANUAL_URL);
625 config->proxy_args = strdup("");
627 if ((configfp = fopen(configfile, "r")) == NULL)
629 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
631 /* Never get here - LOG_LEVEL_FATAL causes program exit */
634 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
636 char cmd[BUFFER_SIZE];
637 char arg[BUFFER_SIZE];
638 char tmp[BUFFER_SIZE];
640 struct access_control_list *cur_acl;
641 #endif /* def FEATURE_ACL */
642 struct forward_spec *cur_fwd;
648 /* Copy command (i.e. up to space or tab) into cmd */
651 while (*p && (*p != ' ') && (*p != '\t'))
657 /* Skip over the whitespace in buf */
658 while (*p && ((*p == ' ') || (*p == '\t')))
663 /* Copy the argument into arg */
666 /* Should never happen, but check this anyway */
672 /* Make sure the command field is lower case */
677 *p = ijb_tolower(*p);
681 /* Save the argument for show-proxy-args */
682 savearg(cmd, arg, config);
685 switch( hash_string( cmd ) )
687 /* *************************************************************************
688 * actionsfile actions-file-name
689 * In confdir by default
690 * *************************************************************************/
691 case hash_actions_file :
693 while ((i < MAX_ACTION_FILES) && (NULL != config->actions_file[i]))
698 if (i >= MAX_ACTION_FILES)
700 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
701 "(You can increase this limit by changing MAX_ACTION_FILES in project.h and recompiling).",
704 config->actions_file_short[i] = strdup(arg);
705 p = malloc(strlen(arg) + sizeof(".action"));
708 log_error(LOG_LEVEL_FATAL, "Out of memory");
711 strcat(p, ".action");
712 config->actions_file[i] = make_path(config->confdir, p);
716 /* *************************************************************************
717 * admin-address email-address
718 * *************************************************************************/
719 case hash_admin_address :
720 freez(config->admin_address);
721 config->admin_address = strdup(arg);
724 /* *************************************************************************
726 * *************************************************************************/
727 case hash_buffer_limit :
728 config->buffer_limit = (size_t) 1024 * atoi(arg);
731 /* *************************************************************************
732 * confdir directory-name
733 * *************************************************************************/
735 freez(config->confdir);
736 config->confdir = make_path( NULL, arg);
739 /* *************************************************************************
741 * Specifies debug level, multiple values are ORed together.
742 * *************************************************************************/
744 config->debug |= atoi(arg);
747 /* *************************************************************************
748 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
749 * *************************************************************************/
751 case hash_deny_access:
752 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
754 if ((vec_count != 1) && (vec_count != 2))
756 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
757 "deny-access directive in configuration file.");
758 string_append(&config->proxy_args,
759 "<br>\nWARNING: Wrong number of parameters for "
760 "deny-access directive in configuration file.<br><br>\n");
764 /* allocate a new node */
765 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
769 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
770 /* Never get here - LOG_LEVEL_FATAL causes program exit */
773 cur_acl->action = ACL_DENY;
775 if (acl_addr(vec[0], cur_acl->src) < 0)
777 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
778 "directive in configuration file: \"%s\"", vec[0]);
779 string_append(&config->proxy_args,
780 "<br>\nWARNING: Invalid source IP for deny-access directive"
781 " in configuration file: \"");
782 string_append(&config->proxy_args,
784 string_append(&config->proxy_args,
791 if (acl_addr(vec[1], cur_acl->dst) < 0)
793 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
794 "directive in configuration file: \"%s\"", vec[0]);
795 string_append(&config->proxy_args,
796 "<br>\nWARNING: Invalid destination IP for deny-access directive"
797 " in configuration file: \"");
798 string_append(&config->proxy_args,
800 string_append(&config->proxy_args,
808 * Add it to the list. Note we reverse the list to get the
809 * behaviour the user expects. With both the ACL and
810 * actions file, the last match wins. However, the internal
811 * implementations are different: The actions file is stored
812 * in the same order as the file, and scanned completely.
813 * With the ACL, we reverse the order as we load it, then
814 * when we scan it we stop as soon as we get a match.
816 cur_acl->next = config->acl;
817 config->acl = cur_acl;
820 #endif /* def FEATURE_ACL */
822 /* *************************************************************************
823 * enable-edit-actions 0|1
824 * *************************************************************************/
825 #ifdef FEATURE_CGI_EDIT_ACTIONS
826 case hash_enable_edit_actions:
827 if ((*arg != '\0') && (0 != atoi(arg)))
829 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
833 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
836 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
838 /* *************************************************************************
839 * enable-remote-toggle 0|1
840 * *************************************************************************/
841 #ifdef FEATURE_CGI_EDIT_ACTIONS
842 case hash_enable_remote_toggle:
843 if ((*arg != '\0') && (0 != atoi(arg)))
845 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
849 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
852 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
854 /* *************************************************************************
855 * filterfile file-name
856 * In confdir by default.
857 * *************************************************************************/
858 case hash_filterfile :
859 if(config->re_filterfile)
861 log_error(LOG_LEVEL_ERROR, "Ignoring extraneous directive 'filterfile %s' "
862 "in line %lu in configuration file (%s).", arg, linenum, configfile);
863 string_append(&config->proxy_args,
864 " <b><font color=\"red\">WARNING: extraneous directive, ignored</font></b>");
867 config->re_filterfile = make_path(config->confdir, arg);
870 /* *************************************************************************
871 * forward url-pattern (.|http-proxy-host[:port])
872 * *************************************************************************/
874 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
878 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
879 "directive in configuration file.");
880 string_append(&config->proxy_args,
881 "<br>\nWARNING: Wrong number of parameters for "
882 "forward directive in configuration file.");
886 /* allocate a new node */
887 cur_fwd = zalloc(sizeof(*cur_fwd));
890 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
891 /* Never get here - LOG_LEVEL_FATAL causes program exit */
895 cur_fwd->type = SOCKS_NONE;
897 /* Save the URL pattern */
898 if (create_url_spec(cur_fwd->url, vec[0]))
900 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
901 "directive in configuration file.");
902 string_append(&config->proxy_args,
903 "<br>\nWARNING: Bad URL specifier for "
904 "forward directive in configuration file.");
908 /* Parse the parent HTTP proxy host:port */
911 if (strcmp(p, ".") != 0)
913 cur_fwd->forward_host = strdup(p);
915 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
918 cur_fwd->forward_port = atoi(p);
921 if (cur_fwd->forward_port <= 0)
923 cur_fwd->forward_port = 8000;
928 cur_fwd->next = config->forward;
929 config->forward = cur_fwd;
933 /* *************************************************************************
934 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
935 * *************************************************************************/
936 case hash_forward_socks4:
937 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
941 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
942 "forward-socks4 directive in configuration file.");
943 string_append(&config->proxy_args,
944 "<br>\nWARNING: Wrong number of parameters for "
945 "forward-socks4 directive in configuration file.");
949 /* allocate a new node */
950 cur_fwd = zalloc(sizeof(*cur_fwd));
953 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
954 /* Never get here - LOG_LEVEL_FATAL causes program exit */
958 cur_fwd->type = SOCKS_4;
960 /* Save the URL pattern */
961 if (create_url_spec(cur_fwd->url, vec[0]))
963 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
964 "directive in configuration file.");
965 string_append(&config->proxy_args,
966 "<br>\nWARNING: Bad URL specifier for "
967 "forward-socks4 directive in configuration file.");
971 /* Parse the SOCKS proxy host[:port] */
974 if (strcmp(p, ".") != 0)
976 cur_fwd->gateway_host = strdup(p);
978 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
981 cur_fwd->gateway_port = atoi(p);
983 if (cur_fwd->gateway_port <= 0)
985 cur_fwd->gateway_port = 1080;
989 /* Parse the parent HTTP proxy host[:port] */
992 if (strcmp(p, ".") != 0)
994 cur_fwd->forward_host = strdup(p);
996 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
999 cur_fwd->forward_port = atoi(p);
1002 if (cur_fwd->forward_port <= 0)
1004 cur_fwd->forward_port = 8000;
1009 cur_fwd->next = config->forward;
1010 config->forward = cur_fwd;
1014 /* *************************************************************************
1015 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
1016 * *************************************************************************/
1017 case hash_forward_socks4a:
1018 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1022 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1023 "forward-socks4a directive in configuration file.");
1024 string_append(&config->proxy_args,
1025 "<br>\nWARNING: Wrong number of parameters for "
1026 "forward-socks4a directive in configuration file.");
1030 /* allocate a new node */
1031 cur_fwd = zalloc(sizeof(*cur_fwd));
1032 if (cur_fwd == NULL)
1034 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1035 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1039 cur_fwd->type = SOCKS_4A;
1041 /* Save the URL pattern */
1042 if (create_url_spec(cur_fwd->url, vec[0]))
1044 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
1045 "directive in configuration file.");
1046 string_append(&config->proxy_args,
1047 "<br>\nWARNING: Bad URL specifier for "
1048 "forward-socks4a directive in configuration file.");
1052 /* Parse the SOCKS proxy host[:port] */
1055 cur_fwd->gateway_host = strdup(p);
1057 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1060 cur_fwd->gateway_port = atoi(p);
1062 if (cur_fwd->gateway_port <= 0)
1064 cur_fwd->gateway_port = 1080;
1067 /* Parse the parent HTTP proxy host[:port] */
1070 if (strcmp(p, ".") != 0)
1072 cur_fwd->forward_host = strdup(p);
1074 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1077 cur_fwd->forward_port = atoi(p);
1080 if (cur_fwd->forward_port <= 0)
1082 cur_fwd->forward_port = 8000;
1087 cur_fwd->next = config->forward;
1088 config->forward = cur_fwd;
1092 /* *************************************************************************
1093 * jarfile jar-file-name
1094 * In logdir by default
1095 * *************************************************************************/
1096 #ifdef FEATURE_COOKIE_JAR
1098 freez(config->jarfile);
1099 config->jarfile = make_path(config->logdir, arg);
1101 #endif /* def FEATURE_COOKIE_JAR */
1103 /* *************************************************************************
1104 * listen-address [ip][:port]
1105 * *************************************************************************/
1106 case hash_listen_address :
1107 freez(config->haddr);
1108 config->haddr = strdup(arg);
1111 /* *************************************************************************
1112 * logdir directory-name
1113 * *************************************************************************/
1115 freez(config->logdir);
1116 config->logdir = make_path(NULL, arg);
1119 /* *************************************************************************
1120 * logfile log-file-name
1121 * In logdir by default
1122 * *************************************************************************/
1124 freez(config->logfile);
1125 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1128 /* *************************************************************************
1129 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1130 * *************************************************************************/
1132 case hash_permit_access:
1133 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1135 if ((vec_count != 1) && (vec_count != 2))
1137 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1138 "permit-access directive in configuration file.");
1139 string_append(&config->proxy_args,
1140 "<br>\nWARNING: Wrong number of parameters for "
1141 "permit-access directive in configuration file.<br><br>\n");
1146 /* allocate a new node */
1147 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1149 if (cur_acl == NULL)
1151 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1152 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1155 cur_acl->action = ACL_PERMIT;
1157 if (acl_addr(vec[0], cur_acl->src) < 0)
1159 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1160 "directive in configuration file: \"%s\"", vec[0]);
1161 string_append(&config->proxy_args,
1162 "<br>\nWARNING: Invalid source IP for permit-access directive"
1163 " in configuration file: \"");
1164 string_append(&config->proxy_args,
1166 string_append(&config->proxy_args,
1173 if (acl_addr(vec[1], cur_acl->dst) < 0)
1175 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1176 "permit-access directive in configuration file: \"%s\"",
1178 string_append(&config->proxy_args,
1179 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1180 " in configuration file: \"");
1181 string_append(&config->proxy_args,
1183 string_append(&config->proxy_args,
1191 * Add it to the list. Note we reverse the list to get the
1192 * behaviour the user expects. With both the ACL and
1193 * actions file, the last match wins. However, the internal
1194 * implementations are different: The actions file is stored
1195 * in the same order as the file, and scanned completely.
1196 * With the ACL, we reverse the order as we load it, then
1197 * when we scan it we stop as soon as we get a match.
1199 cur_acl->next = config->acl;
1200 config->acl = cur_acl;
1203 #endif /* def FEATURE_ACL */
1205 /* *************************************************************************
1206 * proxy-info-url url
1207 * *************************************************************************/
1208 case hash_proxy_info_url :
1209 freez(config->proxy_info_url);
1210 config->proxy_info_url = strdup(arg);
1213 /* *************************************************************************
1215 * *************************************************************************/
1216 case hash_single_threaded :
1217 config->multi_threaded = 0;
1220 /* *************************************************************************
1222 * *************************************************************************/
1223 #ifdef FEATURE_TOGGLE
1225 global_toggle_state = atoi(arg);
1227 #endif /* def FEATURE_TOGGLE */
1229 /* *************************************************************************
1230 * trust-info-url url
1231 * *************************************************************************/
1232 #ifdef FEATURE_TRUST
1233 case hash_trust_info_url :
1234 enlist(config->trust_info, arg);
1236 #endif /* def FEATURE_TRUST */
1238 /* *************************************************************************
1239 * trustfile filename
1240 * (In confdir by default.)
1241 * *************************************************************************/
1242 #ifdef FEATURE_TRUST
1243 case hash_trustfile :
1244 freez(config->trustfile);
1245 config->trustfile = make_path(config->confdir, arg);
1247 #endif /* def FEATURE_TRUST */
1249 /* *************************************************************************
1251 * *************************************************************************/
1252 case hash_usermanual :
1253 freez(config->usermanual);
1254 config->usermanual = strdup(arg);
1257 /* *************************************************************************
1258 * Win32 Console options:
1259 * *************************************************************************/
1261 /* *************************************************************************
1263 * *************************************************************************/
1265 case hash_hide_console :
1268 #endif /*def _WIN_CONSOLE*/
1271 /* *************************************************************************
1272 * Win32 GUI options:
1273 * *************************************************************************/
1275 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1276 /* *************************************************************************
1277 * activity-animation (0|1)
1278 * *************************************************************************/
1279 case hash_activity_animation :
1280 g_bShowActivityAnimation = atoi(arg);
1283 /* *************************************************************************
1284 * close-button-minimizes (0|1)
1285 * *************************************************************************/
1286 case hash_close_button_minimizes :
1287 g_bCloseHidesWindow = atoi(arg);
1290 /* *************************************************************************
1291 * log-buffer-size (0|1)
1292 * *************************************************************************/
1293 case hash_log_buffer_size :
1294 g_bLimitBufferSize = atoi(arg);
1297 /* *************************************************************************
1298 * log-font-name fontnane
1299 * *************************************************************************/
1300 case hash_log_font_name :
1301 strcpy( g_szFontFaceName, arg );
1304 /* *************************************************************************
1306 * *************************************************************************/
1307 case hash_log_font_size :
1308 g_nFontSize = atoi(arg);
1311 /* *************************************************************************
1312 * log-highlight-messages (0|1)
1313 * *************************************************************************/
1314 case hash_log_highlight_messages :
1315 g_bHighlightMessages = atoi(arg);
1318 /* *************************************************************************
1320 * *************************************************************************/
1321 case hash_log_max_lines :
1322 g_nMaxBufferLines = atoi(arg);
1325 /* *************************************************************************
1326 * log-messages (0|1)
1327 * *************************************************************************/
1328 case hash_log_messages :
1329 g_bLogMessages = atoi(arg);
1332 /* *************************************************************************
1333 * show-on-task-bar (0|1)
1334 * *************************************************************************/
1335 case hash_show_on_task_bar :
1336 g_bShowOnTaskBar = atoi(arg);
1339 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1342 /* *************************************************************************
1343 * Warnings about unsupported features
1344 * *************************************************************************/
1346 case hash_deny_access:
1347 #endif /* ndef FEATURE_ACL */
1348 #ifndef FEATURE_CGI_EDIT_ACTIONS
1349 case hash_enable_edit_actions:
1350 case hash_enable_remote_toggle:
1351 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1352 #ifndef FEATURE_COOKIE_JAR
1354 #endif /* ndef FEATURE_COOKIE_JAR */
1356 case hash_permit_access:
1357 #endif /* ndef FEATURE_ACL */
1358 #ifndef FEATURE_TOGGLE
1360 #endif /* ndef FEATURE_TOGGLE */
1361 #ifndef FEATURE_TRUST
1362 case hash_trustfile :
1363 case hash_trust_info_url :
1364 #endif /* ndef FEATURE_TRUST */
1366 #ifndef _WIN_CONSOLE
1367 case hash_hide_console :
1368 #endif /* ndef _WIN_CONSOLE */
1370 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1371 case hash_activity_animation :
1372 case hash_close_button_minimizes :
1373 case hash_log_buffer_size :
1374 case hash_log_font_name :
1375 case hash_log_font_size :
1376 case hash_log_highlight_messages :
1377 case hash_log_max_lines :
1378 case hash_log_messages :
1379 case hash_show_on_task_bar :
1380 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1381 /* These warnings are annoying - so hide them. -- Jon */
1382 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1385 /* *************************************************************************/
1387 /* *************************************************************************/
1389 * I decided that I liked this better as a warning than an
1390 * error. To change back to an error, just change log level
1391 * to LOG_LEVEL_FATAL.
1393 log_error(LOG_LEVEL_ERROR, "Ignoring unrecognized directive '%s' (%luul) in line %lu "
1394 "in configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1395 string_append(&config->proxy_args,
1396 " <b><font color=\"red\">WARNING: unrecognized directive, ignored</font></b>");
1399 /* *************************************************************************/
1400 } /* end switch( hash_string(cmd) ) */
1401 } /* end while ( read_config_line(...) ) */
1405 if (NULL == config->proxy_args)
1407 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1410 init_error_log(Argv[0], config->logfile, config->debug);
1412 if (config->actions_file[0])
1414 add_loader(load_actions_file, config);
1417 if (config->re_filterfile)
1419 add_loader(load_re_filterfile, config);
1422 #ifdef FEATURE_TRUST
1423 if (config->trustfile)
1425 add_loader(load_trustfile, config);
1427 #endif /* def FEATURE_TRUST */
1429 #ifdef FEATURE_COOKIE_JAR
1430 if ( NULL != config->jarfile )
1432 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1434 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1435 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1437 setbuf(config->jar, NULL);
1439 #endif /* def FEATURE_COOKIE_JAR */
1441 if ( NULL == config->haddr )
1443 config->haddr = strdup( HADDR_DEFAULT );
1446 if ( NULL != config->haddr )
1448 if (NULL != (p = strchr(config->haddr, ':')))
1453 config->hport = atoi(p);
1457 if (config->hport <= 0)
1460 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1461 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1463 if (*config->haddr == '\0')
1465 config->haddr = NULL;
1470 * Want to run all the loaders once now.
1472 * Need to set up a fake csp, so they can get to the config.
1474 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1475 fake_csp->config = config;
1477 if (run_loader(fake_csp))
1480 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1481 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1485 /* FIXME: this is a kludge for win32 */
1486 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1488 g_default_actions_file = config->actions_file[1]; /* FIXME Hope this is default.action */
1489 g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1490 g_re_filterfile = config->re_filterfile;
1492 #ifdef FEATURE_TRUST
1493 g_trustfile = config->trustfile;
1494 #endif /* def FEATURE_TRUST */
1497 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1498 /* FIXME: end kludge */
1501 config->need_bind = 1;
1503 if (current_configfile)
1505 struct configuration_spec * oldcfg = (struct configuration_spec *)
1506 current_configfile->f;
1508 * Check if config->haddr,hport == oldcfg->haddr,hport
1510 * The following could be written more compactly as a single,
1511 * (unreadably long) if statement.
1513 config->need_bind = 0;
1514 if (config->hport != oldcfg->hport)
1516 config->need_bind = 1;
1518 else if (config->haddr == NULL)
1520 if (oldcfg->haddr != NULL)
1522 config->need_bind = 1;
1525 else if (oldcfg->haddr == NULL)
1527 config->need_bind = 1;
1529 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1531 config->need_bind = 1;
1534 current_configfile->unloader = unload_configfile;
1537 fs->next = files->next;
1540 current_configfile = fs;
1546 /*********************************************************************
1548 * Function : savearg
1550 * Description : Called from `load_config'. It saves each non-empty
1551 * and non-comment line from config into
1552 * config->proxy_args. This is used to create the
1553 * show-proxy-args page. On error, frees
1554 * config->proxy_args and sets it to NULL
1557 * 1 : command = config setting that was found
1558 * 2 : argument = the setting's argument (if any)
1559 * 3 : config = Configuration to save into.
1563 *********************************************************************/
1564 static void savearg(char *command, char *argument, struct configuration_spec * config)
1574 * Add config option name embedded in
1575 * link to it's section in the user-manual
1577 buf = strdup("\n<br><a href=\"");
1578 string_append(&buf, config->usermanual);
1579 string_append(&buf, CONFIG_HELP_PREFIX);
1580 string_join (&buf, string_toupper(command));
1581 string_append(&buf, "\">");
1582 string_append(&buf, command);
1583 string_append(&buf, "</a> ");
1587 freez(config->proxy_args);
1591 if ( (NULL != argument) && ('\0' != *argument) )
1593 s = html_encode(argument);
1597 freez(config->proxy_args);
1601 if (strncmpic(argument, "http://", 7) == 0)
1603 string_append(&buf, "<a href=\"");
1604 string_append(&buf, s);
1605 string_append(&buf, "\">");
1606 string_join (&buf, s);
1607 string_append(&buf, "</a>");
1611 string_join (&buf, s);
1615 string_join(&config->proxy_args, buf);