1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.42 2002/04/05 15:50:15 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.42 2002/04/05 15:50:15 oes
39 * fix for invalid HTML proxy_args
41 * Revision 1.41 2002/03/31 17:19:00 jongfoster
42 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
44 * Revision 1.40 2002/03/26 22:29:55 swa
45 * we have a new homepage!
47 * Revision 1.39 2002/03/24 13:25:43 swa
48 * name change related issues
50 * Revision 1.38 2002/03/24 13:05:48 jongfoster
51 * Renaming re_filterfile to filterfile
53 * Revision 1.37 2002/03/16 23:54:06 jongfoster
54 * Adding graceful termination feature, to help look for memory leaks.
55 * If you enable this (which, by design, has to be done by hand
56 * editing config.h) and then go to http://i.j.b/die, then the program
57 * will exit cleanly after the *next* request. It should free all the
58 * memory that was used.
60 * Revision 1.36 2002/03/13 00:27:05 jongfoster
63 * Revision 1.35 2002/03/07 03:52:44 oes
64 * Set logging to tty for --no-daemon mode
66 * Revision 1.34 2002/03/06 23:14:35 jongfoster
67 * Trivial cosmetic changes to make function comments easier to find.
69 * Revision 1.33 2002/03/05 04:52:42 oes
70 * Deleted non-errlog debugging code
72 * Revision 1.32 2002/03/04 18:24:53 oes
73 * Re-enabled output of unknown config directive hash
75 * Revision 1.31 2002/03/03 15:07:20 oes
76 * Re-enabled automatic config reloading
78 * Revision 1.30 2002/01/22 23:31:43 jongfoster
79 * Replacing strsav() with string_append()
81 * Revision 1.29 2002/01/17 21:02:30 jongfoster
82 * Moving all our URL and URL pattern parsing code to urlmatch.c.
84 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
86 * Revision 1.28 2001/12/30 14:07:32 steudten
87 * - Add signal handling (unix)
88 * - Add SIGHUP handler (unix)
89 * - Add creation of pidfile (unix)
90 * - Add action 'top' in rc file (RH)
91 * - Add entry 'SIGNALS' to manpage
92 * - Add exit message to logfile (unix)
94 * Revision 1.27 2001/11/07 00:02:13 steudten
95 * Add line number in error output for lineparsing for
96 * actionsfile and configfile.
97 * Special handling for CLF added.
99 * Revision 1.26 2001/11/05 21:41:43 steudten
100 * Add changes to be a real daemon just for unix os.
101 * (change cwd to /, detach from controlling tty, set
102 * process group and session leader to the own process.
104 * Add some fatal-error log message for failed malloc().
105 * Add '-d' if compiled with 'configure --with-debug' to
106 * enable debug output.
108 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
109 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
110 * threads to call select() simultaneously. So, it's time to do a real, live,
111 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
112 * (native). Both versions will work, but using __OS2__ offers multi-threading.
114 * Revision 1.24 2001/10/23 21:40:30 jongfoster
115 * Added support for enable-edit-actions and enable-remote-toggle config
118 * Revision 1.23 2001/10/07 15:36:00 oes
119 * Introduced new config option "buffer-limit"
121 * Revision 1.22 2001/09/22 16:36:59 jongfoster
122 * Removing unused parameter fs from read_config_line()
124 * Revision 1.21 2001/09/16 17:10:43 jongfoster
125 * Moving function savearg() here, since it was the only thing left in
128 * Revision 1.20 2001/07/30 22:08:36 jongfoster
129 * Tidying up #defines:
130 * - All feature #defines are now of the form FEATURE_xxx
131 * - Permanently turned off WIN_GUI_EDIT
132 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
134 * Revision 1.19 2001/07/15 17:45:16 jongfoster
135 * Removing some unused #includes
137 * Revision 1.18 2001/07/13 14:01:14 oes
138 * - Removed all #ifdef PCRS
139 * - Removed vim-settings
141 * Revision 1.17 2001/06/29 13:31:03 oes
142 * - Improved comments
143 * - Fixed (actionsfile) and sorted hashes
144 * - Introduced admin_address and proxy-info-url
145 * as config parameters
146 * - Renamed config->proxy_args_invocation (which didn't have
147 * the invocation but the options!) to config->proxy_args
148 * - Various adaptions
149 * - Removed logentry from cancelled commit
151 * Revision 1.16 2001/06/09 10:55:28 jongfoster
152 * Changing BUFSIZ ==> BUFFER_SIZE
154 * Revision 1.15 2001/06/07 23:13:40 jongfoster
155 * Merging ACL and forward files into config file.
156 * Cosmetic: Sorting config file options alphabetically.
157 * Cosmetic: Adding brief syntax comments to config file options.
159 * Revision 1.14 2001/06/07 14:46:25 joergs
160 * Missing make_path() added for re_filterfile.
162 * Revision 1.13 2001/06/05 22:33:54 jongfoster
164 * Fixed minor memory leak.
165 * Also now uses make_path to prepend the pathnames.
167 * Revision 1.12 2001/06/05 20:04:09 jongfoster
168 * Now uses _snprintf() in place of snprintf() under Win32.
170 * Revision 1.11 2001/06/04 18:31:58 swa
171 * files are now prefixed with either `confdir' or `logdir'.
172 * `make redhat-dist' replaces both entries confdir and logdir
175 * Revision 1.10 2001/06/03 19:11:54 oes
176 * introduced confdir option
178 * Revision 1.9 2001/06/01 20:06:24 jongfoster
179 * Removed support for "tinygif" option - moved to actions file.
181 * Revision 1.8 2001/05/31 21:27:13 jongfoster
182 * Removed many options from the config file and into the
183 * "actions" file: add_forwarded, suppress_vanilla_wafer,
184 * wafer, add_header, user_agent, referer, from
185 * Also globally replaced "permission" with "action".
187 * Revision 1.7 2001/05/29 09:50:24 jongfoster
188 * Unified blocklist/imagelist/permissionslist.
189 * File format is still under discussion, but the internal changes
192 * Also modified interceptor behaviour:
193 * - We now intercept all URLs beginning with one of the following
194 * prefixes (and *only* these prefixes):
196 * * http://ijbswa.sf.net/config/
197 * * http://ijbswa.sourceforge.net/config/
198 * - New interceptors "home page" - go to http://i.j.b/ to see it.
199 * - Internal changes so that intercepted and fast redirect pages
200 * are not replaced with an image.
201 * - Interceptors now have the option to send a binary page direct
202 * to the client. (i.e. ijb-send-banner uses this)
203 * - Implemented show-url-info interceptor. (Which is why I needed
204 * the above interceptors changes - a typical URL is
205 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
206 * The previous mechanism would not have intercepted that, and
207 * if it had been intercepted then it then it would have replaced
210 * Revision 1.6 2001/05/26 00:28:36 jongfoster
211 * Automatic reloading of config file.
212 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
213 * Most of the global variables have been moved to a new
214 * struct configuration_spec, accessed through csp->config->globalname
215 * Most of the globals remaining are used by the Win32 GUI.
217 * Revision 1.5 2001/05/25 22:34:30 jongfoster
220 * Revision 1.4 2001/05/22 18:46:04 oes
222 * - Enabled filtering banners by size rather than URL
223 * by adding patterns that replace all standard banner
224 * sizes with the "Junkbuster" gif to the re_filterfile
226 * - Enabled filtering WebBugs by providing a pattern
227 * which kills all 1x1 images
229 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
230 * which is selected by the (nonstandard and therefore
231 * capital) letter 'U' in the option string.
232 * It causes the quantifiers to be ungreedy by default.
233 * Appending a ? turns back to greedy (!).
235 * - Added a new interceptor ijb-send-banner, which
236 * sends back the "Junkbuster" gif. Without imagelist or
237 * MSIE detection support, or if tinygif = 1, or the
238 * URL isn't recognized as an imageurl, a lame HTML
239 * explanation is sent instead.
241 * - Added new feature, which permits blocking remote
242 * script redirects and firing back a local redirect
244 * The feature is conditionally compiled, i.e. it
245 * can be disabled with --disable-fast-redirects,
246 * plus it must be activated by a "fast-redirects"
247 * line in the config file, has its own log level
248 * and of course wants to be displayed by show-proxy-args
249 * Note: Boy, all the #ifdefs in 1001 locations and
250 * all the fumbling with configure.in and acconfig.h
251 * were *way* more work than the feature itself :-(
253 * - Because a generic redirect template was needed for
254 * this, tinygif = 3 now uses the same.
256 * - Moved GIFs, and other static HTTP response templates
261 * - Removed some >400 CRs again (Jon, you really worked
264 * Revision 1.3 2001/05/20 01:21:20 jongfoster
265 * Version 2.9.4 checkin.
266 * - Merged popupfile and cookiefile, and added control over PCRS
267 * filtering, in new "permissionsfile".
268 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
269 * file error you now get a message box (in the Win32 GUI) rather
270 * than the program exiting with no explanation.
271 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
273 * - Removed tabs from "config"
274 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
275 * - Bumped up version number.
277 * Revision 1.2 2001/05/17 23:01:01 oes
278 * - Cleaned CRLF's from the sources and related files
280 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
281 * Initial import of version 2.9.3 source tree
284 *********************************************************************/
290 #include <sys/types.h>
304 # include <windows.h>
307 # ifndef _WIN_CONSOLE
309 # endif /* ndef _WIN_CONSOLE */
311 /* VC++ has "_snprintf", not "snprintf" */
312 #define snprintf _snprintf
314 #else /* ifndef _WIN32 */
318 # include <sys/wait.h>
320 # include <sys/time.h>
321 # include <sys/stat.h>
331 #include "miscutil.h"
335 #include "urlmatch.h"
337 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
340 * Fix a problem with Solaris. There should be no effect on other
342 * Solaris's isspace() is a macro which uses it's argument directly
343 * as an array index. Therefore we need to make sure that high-bit
344 * characters generate +ve values, and ideally we also want to make
345 * the argument match the declared parameter type of "int".
347 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
348 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
350 #ifdef FEATURE_TOGGLE
351 /* by haroon - indicates if ijb is enabled */
352 int g_bToggleIJB = 1; /* Junkbusters is enabled by default. */
353 #endif /* def FEATURE_TOGGLE */
355 /* The filename of the configfile */
356 const char *configfile = NULL;
359 * CGI functions will later need access to the invocation args,
360 * so we will make argc and argv global.
363 const char **Argv = NULL;
365 static struct file_list *current_configfile = NULL;
369 * This takes the "cryptic" hash of each keyword and aliases them to
370 * something a little more readable. This also makes changing the
371 * hash values easier if they should change or the hash algorthm changes.
372 * Use the included "hash" program to find out what the hash will be
373 * for any string supplied on the command line. (Or just put it in the
374 * config file and read the number from the error message in the log).
376 * Please keep this list sorted alphabetically (but with the Windows
377 * console and GUI specific options last).
380 #define hash_actions_file 1196306641ul /* "actionsfile" */
381 #define hash_admin_address 4112573064ul /* "admin-address" */
382 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
383 #define hash_confdir 1978389ul /* "confdir" */
384 #define hash_debug 78263ul /* "debug" */
385 #define hash_deny_access 1227333715ul /* "deny-access" */
386 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
387 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
388 #define hash_filterfile 250887266ul /* "filterfile" */
389 #define hash_forward 2029845ul /* "forward" */
390 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
391 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
392 #define hash_jarfile 2046641ul /* "jarfile" */
393 #define hash_listen_address 1255650842ul /* "listen-address" */
394 #define hash_logdir 422889ul /* "logdir" */
395 #define hash_logfile 2114766ul /* "logfile" */
396 #define hash_permit_access 3587953268ul /* "permit-access" */
397 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
398 #define hash_single_threaded 4250084780ul /* "single-threaded" */
399 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
400 #define hash_toggle 447966ul /* "toggle" */
401 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
402 #define hash_trustfile 56494766ul /* "trustfile" */
404 #define hash_activity_animation 1817904738ul /* "activity-animation" */
405 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
406 #define hash_hide_console 2048809870ul /* "hide-console" */
407 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
408 #define hash_log_font_name 2866730124ul /* "log-font-name" */
409 #define hash_log_font_size 2866731014ul /* "log-font-size" */
410 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
411 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
412 #define hash_log_messages 2291744899ul /* "log-messages" */
413 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
416 static void savearg(char *command, char *argument, struct configuration_spec * config);
418 /*********************************************************************
420 * Function : unload_configfile
422 * Description : Free the config structure and all components.
425 * 1 : data: struct configuration_spec to unload
429 *********************************************************************/
430 void unload_configfile (void * data)
432 struct configuration_spec * config = (struct configuration_spec *)data;
433 struct forward_spec *cur_fwd = config->forward;
435 struct access_control_list *cur_acl = config->acl;
437 while (cur_acl != NULL)
439 struct access_control_list * next_acl = cur_acl->next;
444 #endif /* def FEATURE_ACL */
446 while (cur_fwd != NULL)
448 struct forward_spec * next_fwd = cur_fwd->next;
449 free_url_spec(cur_fwd->url);
451 freez(cur_fwd->gateway_host);
452 freez(cur_fwd->forward_host);
456 config->forward = NULL;
458 #ifdef FEATURE_COOKIE_JAR
459 if ( NULL != config->jar )
461 fclose( config->jar );
464 #endif /* def FEATURE_COOKIE_JAR */
466 freez(config->confdir);
467 freez(config->logdir);
469 freez(config->haddr);
470 freez(config->logfile);
472 freez(config->actions_file);
473 freez(config->admin_address);
474 freez(config->proxy_info_url);
475 freez(config->proxy_args);
477 #ifdef FEATURE_COOKIE_JAR
478 freez(config->jarfile);
479 #endif /* def FEATURE_COOKIE_JAR */
481 freez(config->re_filterfile);
486 #ifdef FEATURE_GRACEFUL_TERMINATION
487 /*********************************************************************
489 * Function : unload_current_config_file
491 * Description : Unloads current config file - reset to state at
492 * beginning of program.
498 *********************************************************************/
499 void unload_current_config_file(void)
501 if (current_configfile)
503 current_configfile->unloader = unload_configfile;
504 current_configfile = NULL;
510 /*********************************************************************
512 * Function : load_config
514 * Description : Load the config file and all parameters.
517 * 1 : csp = Client state (the config member will be
518 * filled in by this function).
520 * Returns : 0 => Ok, everything else is an error.
522 *********************************************************************/
523 struct configuration_spec * load_config(void)
525 char buf[BUFFER_SIZE];
527 FILE *configfp = NULL;
528 struct configuration_spec * config = NULL;
529 struct client_state * fake_csp;
530 struct file_list *fs;
531 unsigned long linenum = 0;
533 if ( !check_file_changed(current_configfile, configfile, &fs))
535 /* No need to load */
536 return ((struct configuration_spec *)current_configfile->f);
540 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
544 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
546 #ifdef FEATURE_TOGGLE
548 #endif /* def FEATURE_TOGGLE */
550 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
556 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
557 /* Never get here - LOG_LEVEL_FATAL causes program exit */
561 * This is backwards from how it's usually done.
562 * Following the usual pattern, "fs" would be stored in a member
563 * variable in "csp", and then we'd access "config" from "fs->f",
564 * using a cast. However, "config" is used so often that a
565 * cast each time would be very ugly, and the extra indirection
566 * would waste CPU cycles. Therefore we store "config" in
567 * "csp->config", and "fs" in "csp->config->config_file_list".
569 config->config_file_list = fs;
575 config->multi_threaded = 1;
576 config->hport = HADDR_PORT;
577 config->buffer_limit = 4096 * 1024;
578 config->proxy_args = strdup("");
580 if ((configfp = fopen(configfile, "r")) == NULL)
582 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
584 /* Never get here - LOG_LEVEL_FATAL causes program exit */
587 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
589 char cmd[BUFFER_SIZE];
590 char arg[BUFFER_SIZE];
591 char tmp[BUFFER_SIZE];
593 struct access_control_list *cur_acl;
594 #endif /* def FEATURE_ACL */
595 struct forward_spec *cur_fwd;
601 /* Copy command (i.e. up to space or tab) into cmd */
604 while (*p && (*p != ' ') && (*p != '\t'))
610 /* Skip over the whitespace in buf */
611 while (*p && ((*p == ' ') || (*p == '\t')))
616 /* Copy the argument into arg */
619 /* Should never happen, but check this anyway */
625 /* Make sure the command field is lower case */
630 *p = ijb_tolower(*p);
634 /* Save the argument for show-proxy-args */
635 savearg(cmd, arg, config);
638 switch( hash_string( cmd ) )
640 /* *************************************************************************
641 * actionsfile actions-file-name
642 * In confdir by default
643 * *************************************************************************/
644 case hash_actions_file :
645 freez(config->actions_file);
646 config->actions_file = make_path(config->confdir, arg);
649 /* *************************************************************************
650 * admin-address email-address
651 * *************************************************************************/
652 case hash_admin_address :
653 freez(config->admin_address);
654 config->admin_address = strdup(arg);
657 /* *************************************************************************
659 * *************************************************************************/
660 case hash_buffer_limit :
661 config->buffer_limit = (size_t) 1024 * atoi(arg);
664 /* *************************************************************************
665 * confdir directory-name
666 * *************************************************************************/
668 freez(config->confdir);
669 config->confdir = make_path( NULL, arg);
672 /* *************************************************************************
674 * Specifies debug level, multiple values are ORed together.
675 * *************************************************************************/
677 config->debug |= atoi(arg);
680 /* *************************************************************************
681 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
682 * *************************************************************************/
684 case hash_deny_access:
685 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
687 if ((vec_count != 1) && (vec_count != 2))
689 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
690 "deny-access directive in configuration file.");
691 string_append(&config->proxy_args,
692 "<br>\nWARNING: Wrong number of parameters for "
693 "deny-access directive in configuration file.<br><br>\n");
697 /* allocate a new node */
698 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
702 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
703 /* Never get here - LOG_LEVEL_FATAL causes program exit */
706 cur_acl->action = ACL_DENY;
708 if (acl_addr(vec[0], cur_acl->src) < 0)
710 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
711 "directive in configuration file: \"%s\"", vec[0]);
712 string_append(&config->proxy_args,
713 "<br>\nWARNING: Invalid source IP for deny-access directive"
714 " in configuration file: \"");
715 string_append(&config->proxy_args,
717 string_append(&config->proxy_args,
724 if (acl_addr(vec[1], cur_acl->dst) < 0)
726 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
727 "directive in configuration file: \"%s\"", vec[0]);
728 string_append(&config->proxy_args,
729 "<br>\nWARNING: Invalid destination IP for deny-access directive"
730 " in configuration file: \"");
731 string_append(&config->proxy_args,
733 string_append(&config->proxy_args,
741 * Add it to the list. Note we reverse the list to get the
742 * behaviour the user expects. With both the ACL and
743 * actions file, the last match wins. However, the internal
744 * implementations are different: The actions file is stored
745 * in the same order as the file, and scanned completely.
746 * With the ACL, we reverse the order as we load it, then
747 * when we scan it we stop as soon as we get a match.
749 cur_acl->next = config->acl;
750 config->acl = cur_acl;
753 #endif /* def FEATURE_ACL */
755 /* *************************************************************************
756 * enable-edit-actions 0|1
757 * *************************************************************************/
758 #ifdef FEATURE_CGI_EDIT_ACTIONS
759 case hash_enable_edit_actions:
760 if ((*arg != '\0') && (0 != atoi(arg)))
762 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
766 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
769 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
771 /* *************************************************************************
772 * enable-remote-toggle 0|1
773 * *************************************************************************/
774 #ifdef FEATURE_CGI_EDIT_ACTIONS
775 case hash_enable_remote_toggle:
776 if ((*arg != '\0') && (0 != atoi(arg)))
778 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
782 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
785 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
787 /* *************************************************************************
788 * forward url-pattern (.|http-proxy-host[:port])
789 * *************************************************************************/
791 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
795 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
796 "directive in configuration file.");
797 string_append(&config->proxy_args,
798 "<br>\nWARNING: Wrong number of parameters for "
799 "forward directive in configuration file.");
803 /* allocate a new node */
804 cur_fwd = zalloc(sizeof(*cur_fwd));
807 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
808 /* Never get here - LOG_LEVEL_FATAL causes program exit */
812 cur_fwd->type = SOCKS_NONE;
814 /* Save the URL pattern */
815 if (create_url_spec(cur_fwd->url, vec[0]))
817 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
818 "directive in configuration file.");
819 string_append(&config->proxy_args,
820 "<br>\nWARNING: Bad URL specifier for "
821 "forward directive in configuration file.");
825 /* Parse the parent HTTP proxy host:port */
828 if (strcmp(p, ".") != 0)
830 cur_fwd->forward_host = strdup(p);
832 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
835 cur_fwd->forward_port = atoi(p);
838 if (cur_fwd->forward_port <= 0)
840 cur_fwd->forward_port = 8000;
845 cur_fwd->next = config->forward;
846 config->forward = cur_fwd;
850 /* *************************************************************************
851 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
852 * *************************************************************************/
853 case hash_forward_socks4:
854 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
858 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
859 "forward-socks4 directive in configuration file.");
860 string_append(&config->proxy_args,
861 "<br>\nWARNING: Wrong number of parameters for "
862 "forward-socks4 directive in configuration file.");
866 /* allocate a new node */
867 cur_fwd = zalloc(sizeof(*cur_fwd));
870 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
871 /* Never get here - LOG_LEVEL_FATAL causes program exit */
875 cur_fwd->type = SOCKS_4;
877 /* Save the URL pattern */
878 if (create_url_spec(cur_fwd->url, vec[0]))
880 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
881 "directive in configuration file.");
882 string_append(&config->proxy_args,
883 "<br>\nWARNING: Bad URL specifier for "
884 "forward-socks4 directive in configuration file.");
888 /* Parse the SOCKS proxy host[:port] */
891 if (strcmp(p, ".") != 0)
893 cur_fwd->gateway_host = strdup(p);
895 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
898 cur_fwd->gateway_port = atoi(p);
900 if (cur_fwd->gateway_port <= 0)
902 cur_fwd->gateway_port = 1080;
906 /* Parse the parent HTTP proxy host[:port] */
909 if (strcmp(p, ".") != 0)
911 cur_fwd->forward_host = strdup(p);
913 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
916 cur_fwd->forward_port = atoi(p);
919 if (cur_fwd->forward_port <= 0)
921 cur_fwd->forward_port = 8000;
926 cur_fwd->next = config->forward;
927 config->forward = cur_fwd;
931 /* *************************************************************************
932 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
933 * *************************************************************************/
934 case hash_forward_socks4a:
935 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
939 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
940 "forward-socks4a directive in configuration file.");
941 string_append(&config->proxy_args,
942 "<br>\nWARNING: Wrong number of parameters for "
943 "forward-socks4a directive in configuration file.");
947 /* allocate a new node */
948 cur_fwd = zalloc(sizeof(*cur_fwd));
951 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
952 /* Never get here - LOG_LEVEL_FATAL causes program exit */
956 cur_fwd->type = SOCKS_4A;
958 /* Save the URL pattern */
959 if (create_url_spec(cur_fwd->url, vec[0]))
961 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
962 "directive in configuration file.");
963 string_append(&config->proxy_args,
964 "<br>\nWARNING: Bad URL specifier for "
965 "forward-socks4a directive in configuration file.");
969 /* Parse the SOCKS proxy host[:port] */
972 cur_fwd->gateway_host = strdup(p);
974 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
977 cur_fwd->gateway_port = atoi(p);
979 if (cur_fwd->gateway_port <= 0)
981 cur_fwd->gateway_port = 1080;
984 /* Parse the parent HTTP proxy host[:port] */
987 if (strcmp(p, ".") != 0)
989 cur_fwd->forward_host = strdup(p);
991 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
994 cur_fwd->forward_port = atoi(p);
997 if (cur_fwd->forward_port <= 0)
999 cur_fwd->forward_port = 8000;
1004 cur_fwd->next = config->forward;
1005 config->forward = cur_fwd;
1009 /* *************************************************************************
1010 * jarfile jar-file-name
1011 * In logdir by default
1012 * *************************************************************************/
1013 #ifdef FEATURE_COOKIE_JAR
1015 freez(config->jarfile);
1016 config->jarfile = make_path(config->logdir, arg);
1018 #endif /* def FEATURE_COOKIE_JAR */
1020 /* *************************************************************************
1021 * listen-address [ip][:port]
1022 * *************************************************************************/
1023 case hash_listen_address :
1024 freez(config->haddr);
1025 config->haddr = strdup(arg);
1028 /* *************************************************************************
1029 * logdir directory-name
1030 * *************************************************************************/
1032 freez(config->logdir);
1033 config->logdir = make_path(NULL, arg);
1036 /* *************************************************************************
1037 * logfile log-file-name
1038 * In logdir by default
1039 * *************************************************************************/
1041 freez(config->logfile);
1042 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1045 /* *************************************************************************
1046 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1047 * *************************************************************************/
1049 case hash_permit_access:
1050 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1052 if ((vec_count != 1) && (vec_count != 2))
1054 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1055 "permit-access directive in configuration file.");
1056 string_append(&config->proxy_args,
1057 "<br>\nWARNING: Wrong number of parameters for "
1058 "permit-access directive in configuration file.<br><br>\n");
1063 /* allocate a new node */
1064 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1066 if (cur_acl == NULL)
1068 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1069 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1072 cur_acl->action = ACL_PERMIT;
1074 if (acl_addr(vec[0], cur_acl->src) < 0)
1076 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1077 "directive in configuration file: \"%s\"", vec[0]);
1078 string_append(&config->proxy_args,
1079 "<br>\nWARNING: Invalid source IP for permit-access directive"
1080 " in configuration file: \"");
1081 string_append(&config->proxy_args,
1083 string_append(&config->proxy_args,
1090 if (acl_addr(vec[1], cur_acl->dst) < 0)
1092 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1093 "permit-access directive in configuration file: \"%s\"",
1095 string_append(&config->proxy_args,
1096 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1097 " in configuration file: \"");
1098 string_append(&config->proxy_args,
1100 string_append(&config->proxy_args,
1108 * Add it to the list. Note we reverse the list to get the
1109 * behaviour the user expects. With both the ACL and
1110 * actions file, the last match wins. However, the internal
1111 * implementations are different: The actions file is stored
1112 * in the same order as the file, and scanned completely.
1113 * With the ACL, we reverse the order as we load it, then
1114 * when we scan it we stop as soon as we get a match.
1116 cur_acl->next = config->acl;
1117 config->acl = cur_acl;
1120 #endif /* def FEATURE_ACL */
1122 /* *************************************************************************
1123 * proxy-info-url url
1124 * *************************************************************************/
1125 case hash_proxy_info_url :
1126 freez(config->proxy_info_url);
1127 config->proxy_info_url = strdup(arg);
1130 /* *************************************************************************
1131 * re_filterfile file-name
1132 * In confdir by default.
1133 * *************************************************************************/
1134 case hash_filterfile :
1135 freez(config->re_filterfile);
1136 config->re_filterfile = make_path(config->confdir, arg);
1139 /* *************************************************************************
1141 * *************************************************************************/
1142 case hash_single_threaded :
1143 config->multi_threaded = 0;
1146 /* *************************************************************************
1148 * *************************************************************************/
1149 #ifdef FEATURE_TOGGLE
1151 g_bToggleIJB = atoi(arg);
1153 #endif /* def FEATURE_TOGGLE */
1155 /* *************************************************************************
1156 * trust-info-url url
1157 * *************************************************************************/
1158 #ifdef FEATURE_TRUST
1159 case hash_trust_info_url :
1160 enlist(config->trust_info, arg);
1162 #endif /* def FEATURE_TRUST */
1164 /* *************************************************************************
1165 * trustfile filename
1166 * (In confdir by default.)
1167 * *************************************************************************/
1168 #ifdef FEATURE_TRUST
1169 case hash_trustfile :
1170 freez(config->trustfile);
1171 config->trustfile = make_path(config->confdir, arg);
1173 #endif /* def FEATURE_TRUST */
1176 /* *************************************************************************
1177 * Win32 Console options:
1178 * *************************************************************************/
1180 /* *************************************************************************
1182 * *************************************************************************/
1184 case hash_hide_console :
1187 #endif /*def _WIN_CONSOLE*/
1190 /* *************************************************************************
1191 * Win32 GUI options:
1192 * *************************************************************************/
1194 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1195 /* *************************************************************************
1196 * activity-animation (0|1)
1197 * *************************************************************************/
1198 case hash_activity_animation :
1199 g_bShowActivityAnimation = atoi(arg);
1202 /* *************************************************************************
1203 * close-button-minimizes (0|1)
1204 * *************************************************************************/
1205 case hash_close_button_minimizes :
1206 g_bCloseHidesWindow = atoi(arg);
1209 /* *************************************************************************
1210 * log-buffer-size (0|1)
1211 * *************************************************************************/
1212 case hash_log_buffer_size :
1213 g_bLimitBufferSize = atoi(arg);
1216 /* *************************************************************************
1217 * log-font-name fontnane
1218 * *************************************************************************/
1219 case hash_log_font_name :
1220 strcpy( g_szFontFaceName, arg );
1223 /* *************************************************************************
1225 * *************************************************************************/
1226 case hash_log_font_size :
1227 g_nFontSize = atoi(arg);
1230 /* *************************************************************************
1231 * log-highlight-messages (0|1)
1232 * *************************************************************************/
1233 case hash_log_highlight_messages :
1234 g_bHighlightMessages = atoi(arg);
1237 /* *************************************************************************
1239 * *************************************************************************/
1240 case hash_log_max_lines :
1241 g_nMaxBufferLines = atoi(arg);
1244 /* *************************************************************************
1245 * log-messages (0|1)
1246 * *************************************************************************/
1247 case hash_log_messages :
1248 g_bLogMessages = atoi(arg);
1251 /* *************************************************************************
1252 * show-on-task-bar (0|1)
1253 * *************************************************************************/
1254 case hash_show_on_task_bar :
1255 g_bShowOnTaskBar = atoi(arg);
1258 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1261 /* *************************************************************************
1262 * Warnings about unsupported features
1263 * *************************************************************************/
1265 case hash_deny_access:
1266 #endif /* ndef FEATURE_ACL */
1267 #ifndef FEATURE_CGI_EDIT_ACTIONS
1268 case hash_enable_edit_actions:
1269 case hash_enable_remote_toggle:
1270 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1271 #ifndef FEATURE_COOKIE_JAR
1273 #endif /* ndef FEATURE_COOKIE_JAR */
1275 case hash_permit_access:
1276 #endif /* ndef FEATURE_ACL */
1277 #ifndef FEATURE_TOGGLE
1279 #endif /* ndef FEATURE_TOGGLE */
1280 #ifndef FEATURE_TRUST
1281 case hash_trustfile :
1282 case hash_trust_info_url :
1283 #endif /* ndef FEATURE_TRUST */
1285 #ifndef _WIN_CONSOLE
1286 case hash_hide_console :
1287 #endif /* ndef _WIN_CONSOLE */
1289 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1290 case hash_activity_animation :
1291 case hash_close_button_minimizes :
1292 case hash_log_buffer_size :
1293 case hash_log_font_name :
1294 case hash_log_font_size :
1295 case hash_log_highlight_messages :
1296 case hash_log_max_lines :
1297 case hash_log_messages :
1298 case hash_show_on_task_bar :
1299 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1300 /* These warnings are annoying - so hide them. -- Jon */
1301 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1304 /* *************************************************************************/
1306 /* *************************************************************************/
1308 * I decided that I liked this better as a warning than an
1309 * error. To change back to an error, just change log level
1310 * to LOG_LEVEL_FATAL.
1312 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1313 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1314 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1315 string_append(&config->proxy_args, buf);
1316 string_append(&config->proxy_args, "<br><br>\n");
1319 /* *************************************************************************/
1320 } /* end switch( hash_string(cmd) ) */
1321 } /* end while ( read_config_line(...) ) */
1325 if (NULL == config->proxy_args)
1327 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1330 init_error_log(Argv[0], config->logfile, config->debug);
1332 if (config->actions_file)
1334 add_loader(load_actions_file, config);
1337 if (config->re_filterfile)
1339 add_loader(load_re_filterfile, config);
1342 #ifdef FEATURE_TRUST
1343 if (config->trustfile)
1345 add_loader(load_trustfile, config);
1347 #endif /* def FEATURE_TRUST */
1349 #ifdef FEATURE_COOKIE_JAR
1350 if ( NULL != config->jarfile )
1352 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1354 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1355 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1357 setbuf(config->jar, NULL);
1359 #endif /* def FEATURE_COOKIE_JAR */
1361 if ( NULL == config->haddr )
1363 config->haddr = strdup( HADDR_DEFAULT );
1366 if ( NULL != config->haddr )
1368 if (NULL != (p = strchr(config->haddr, ':')))
1373 config->hport = atoi(p);
1377 if (config->hport <= 0)
1380 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1381 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1383 if (*config->haddr == '\0')
1385 config->haddr = NULL;
1390 * Want to run all the loaders once now.
1392 * Need to set up a fake csp, so they can get to the config.
1394 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1395 fake_csp->config = config;
1397 if (run_loader(fake_csp))
1400 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1401 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1405 /* FIXME: this is a kludge for win32 */
1406 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1408 g_actions_file = config->actions_file;
1409 g_re_filterfile = config->re_filterfile;
1411 #ifdef FEATURE_TRUST
1412 g_trustfile = config->trustfile;
1413 #endif /* def FEATURE_TRUST */
1416 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1417 /* FIXME: end kludge */
1420 config->need_bind = 1;
1422 if (current_configfile)
1424 struct configuration_spec * oldcfg = (struct configuration_spec *)
1425 current_configfile->f;
1427 * Check if config->haddr,hport == oldcfg->haddr,hport
1429 * The following could be written more compactly as a single,
1430 * (unreadably long) if statement.
1432 config->need_bind = 0;
1433 if (config->hport != oldcfg->hport)
1435 config->need_bind = 1;
1437 else if (config->haddr == NULL)
1439 if (oldcfg->haddr != NULL)
1441 config->need_bind = 1;
1444 else if (oldcfg->haddr == NULL)
1446 config->need_bind = 1;
1448 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1450 config->need_bind = 1;
1453 current_configfile->unloader = unload_configfile;
1456 fs->next = files->next;
1459 current_configfile = fs;
1465 /*********************************************************************
1467 * Function : savearg
1469 * Description : Called from `load_config'. It saves each non-empty
1470 * and non-comment line from config into
1471 * config->proxy_args. This is used to create the
1472 * show-proxy-args page. On error, frees
1473 * config->proxy_args and sets it to NULL
1476 * 1 : command = config setting that was found
1477 * 2 : argument = the setting's argument (if any)
1481 *********************************************************************/
1482 static void savearg(char *command, char *argument, struct configuration_spec * config)
1493 s = html_encode(command);
1497 freez(config->proxy_args);
1500 string_append(&buf, "<a href=\"");
1501 string_append(&buf, html_encode(REDIRECT_URL));
1502 string_append(&buf, "option#");
1503 string_append(&buf, s);
1504 string_append(&buf, "\">");
1505 string_join (&buf, s);
1506 string_append(&buf, "</a> ");
1508 if ( (NULL != argument) && ('\0' != *argument) )
1510 s = html_encode(argument);
1514 freez(config->proxy_args);
1518 if (strncmpic(argument, "http://", 7) == 0)
1520 string_append(&buf, "<a href=\"");
1521 string_append(&buf, s);
1522 string_append(&buf, "\">");
1523 string_join (&buf, s);
1524 string_append(&buf, "</a>");
1528 string_join (&buf, s);
1532 string_append(&buf, "<br>\n");
1534 string_join(&config->proxy_args, buf);