1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.39 2002/03/24 13:25:43 swa 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.39 2002/03/24 13:25:43 swa
39 * name change related issues
41 * Revision 1.38 2002/03/24 13:05:48 jongfoster
42 * Renaming re_filterfile to filterfile
44 * Revision 1.37 2002/03/16 23:54:06 jongfoster
45 * Adding graceful termination feature, to help look for memory leaks.
46 * If you enable this (which, by design, has to be done by hand
47 * editing config.h) and then go to http://i.j.b/die, then the program
48 * will exit cleanly after the *next* request. It should free all the
49 * memory that was used.
51 * Revision 1.36 2002/03/13 00:27:05 jongfoster
54 * Revision 1.35 2002/03/07 03:52:44 oes
55 * Set logging to tty for --no-daemon mode
57 * Revision 1.34 2002/03/06 23:14:35 jongfoster
58 * Trivial cosmetic changes to make function comments easier to find.
60 * Revision 1.33 2002/03/05 04:52:42 oes
61 * Deleted non-errlog debugging code
63 * Revision 1.32 2002/03/04 18:24:53 oes
64 * Re-enabled output of unknown config directive hash
66 * Revision 1.31 2002/03/03 15:07:20 oes
67 * Re-enabled automatic config reloading
69 * Revision 1.30 2002/01/22 23:31:43 jongfoster
70 * Replacing strsav() with string_append()
72 * Revision 1.29 2002/01/17 21:02:30 jongfoster
73 * Moving all our URL and URL pattern parsing code to urlmatch.c.
75 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
77 * Revision 1.28 2001/12/30 14:07:32 steudten
78 * - Add signal handling (unix)
79 * - Add SIGHUP handler (unix)
80 * - Add creation of pidfile (unix)
81 * - Add action 'top' in rc file (RH)
82 * - Add entry 'SIGNALS' to manpage
83 * - Add exit message to logfile (unix)
85 * Revision 1.27 2001/11/07 00:02:13 steudten
86 * Add line number in error output for lineparsing for
87 * actionsfile and configfile.
88 * Special handling for CLF added.
90 * Revision 1.26 2001/11/05 21:41:43 steudten
91 * Add changes to be a real daemon just for unix os.
92 * (change cwd to /, detach from controlling tty, set
93 * process group and session leader to the own process.
95 * Add some fatal-error log message for failed malloc().
96 * Add '-d' if compiled with 'configure --with-debug' to
97 * enable debug output.
99 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
100 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
101 * threads to call select() simultaneously. So, it's time to do a real, live,
102 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
103 * (native). Both versions will work, but using __OS2__ offers multi-threading.
105 * Revision 1.24 2001/10/23 21:40:30 jongfoster
106 * Added support for enable-edit-actions and enable-remote-toggle config
109 * Revision 1.23 2001/10/07 15:36:00 oes
110 * Introduced new config option "buffer-limit"
112 * Revision 1.22 2001/09/22 16:36:59 jongfoster
113 * Removing unused parameter fs from read_config_line()
115 * Revision 1.21 2001/09/16 17:10:43 jongfoster
116 * Moving function savearg() here, since it was the only thing left in
119 * Revision 1.20 2001/07/30 22:08:36 jongfoster
120 * Tidying up #defines:
121 * - All feature #defines are now of the form FEATURE_xxx
122 * - Permanently turned off WIN_GUI_EDIT
123 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
125 * Revision 1.19 2001/07/15 17:45:16 jongfoster
126 * Removing some unused #includes
128 * Revision 1.18 2001/07/13 14:01:14 oes
129 * - Removed all #ifdef PCRS
130 * - Removed vim-settings
132 * Revision 1.17 2001/06/29 13:31:03 oes
133 * - Improved comments
134 * - Fixed (actionsfile) and sorted hashes
135 * - Introduced admin_address and proxy-info-url
136 * as config parameters
137 * - Renamed config->proxy_args_invocation (which didn't have
138 * the invocation but the options!) to config->proxy_args
139 * - Various adaptions
140 * - Removed logentry from cancelled commit
142 * Revision 1.16 2001/06/09 10:55:28 jongfoster
143 * Changing BUFSIZ ==> BUFFER_SIZE
145 * Revision 1.15 2001/06/07 23:13:40 jongfoster
146 * Merging ACL and forward files into config file.
147 * Cosmetic: Sorting config file options alphabetically.
148 * Cosmetic: Adding brief syntax comments to config file options.
150 * Revision 1.14 2001/06/07 14:46:25 joergs
151 * Missing make_path() added for re_filterfile.
153 * Revision 1.13 2001/06/05 22:33:54 jongfoster
155 * Fixed minor memory leak.
156 * Also now uses make_path to prepend the pathnames.
158 * Revision 1.12 2001/06/05 20:04:09 jongfoster
159 * Now uses _snprintf() in place of snprintf() under Win32.
161 * Revision 1.11 2001/06/04 18:31:58 swa
162 * files are now prefixed with either `confdir' or `logdir'.
163 * `make redhat-dist' replaces both entries confdir and logdir
166 * Revision 1.10 2001/06/03 19:11:54 oes
167 * introduced confdir option
169 * Revision 1.9 2001/06/01 20:06:24 jongfoster
170 * Removed support for "tinygif" option - moved to actions file.
172 * Revision 1.8 2001/05/31 21:27:13 jongfoster
173 * Removed many options from the config file and into the
174 * "actions" file: add_forwarded, suppress_vanilla_wafer,
175 * wafer, add_header, user_agent, referer, from
176 * Also globally replaced "permission" with "action".
178 * Revision 1.7 2001/05/29 09:50:24 jongfoster
179 * Unified blocklist/imagelist/permissionslist.
180 * File format is still under discussion, but the internal changes
183 * Also modified interceptor behaviour:
184 * - We now intercept all URLs beginning with one of the following
185 * prefixes (and *only* these prefixes):
187 * * http://ijbswa.sf.net/config/
188 * * http://ijbswa.sourceforge.net/config/
189 * - New interceptors "home page" - go to http://i.j.b/ to see it.
190 * - Internal changes so that intercepted and fast redirect pages
191 * are not replaced with an image.
192 * - Interceptors now have the option to send a binary page direct
193 * to the client. (i.e. ijb-send-banner uses this)
194 * - Implemented show-url-info interceptor. (Which is why I needed
195 * the above interceptors changes - a typical URL is
196 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
197 * The previous mechanism would not have intercepted that, and
198 * if it had been intercepted then it then it would have replaced
201 * Revision 1.6 2001/05/26 00:28:36 jongfoster
202 * Automatic reloading of config file.
203 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
204 * Most of the global variables have been moved to a new
205 * struct configuration_spec, accessed through csp->config->globalname
206 * Most of the globals remaining are used by the Win32 GUI.
208 * Revision 1.5 2001/05/25 22:34:30 jongfoster
211 * Revision 1.4 2001/05/22 18:46:04 oes
213 * - Enabled filtering banners by size rather than URL
214 * by adding patterns that replace all standard banner
215 * sizes with the "Junkbuster" gif to the re_filterfile
217 * - Enabled filtering WebBugs by providing a pattern
218 * which kills all 1x1 images
220 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
221 * which is selected by the (nonstandard and therefore
222 * capital) letter 'U' in the option string.
223 * It causes the quantifiers to be ungreedy by default.
224 * Appending a ? turns back to greedy (!).
226 * - Added a new interceptor ijb-send-banner, which
227 * sends back the "Junkbuster" gif. Without imagelist or
228 * MSIE detection support, or if tinygif = 1, or the
229 * URL isn't recognized as an imageurl, a lame HTML
230 * explanation is sent instead.
232 * - Added new feature, which permits blocking remote
233 * script redirects and firing back a local redirect
235 * The feature is conditionally compiled, i.e. it
236 * can be disabled with --disable-fast-redirects,
237 * plus it must be activated by a "fast-redirects"
238 * line in the config file, has its own log level
239 * and of course wants to be displayed by show-proxy-args
240 * Note: Boy, all the #ifdefs in 1001 locations and
241 * all the fumbling with configure.in and acconfig.h
242 * were *way* more work than the feature itself :-(
244 * - Because a generic redirect template was needed for
245 * this, tinygif = 3 now uses the same.
247 * - Moved GIFs, and other static HTTP response templates
252 * - Removed some >400 CRs again (Jon, you really worked
255 * Revision 1.3 2001/05/20 01:21:20 jongfoster
256 * Version 2.9.4 checkin.
257 * - Merged popupfile and cookiefile, and added control over PCRS
258 * filtering, in new "permissionsfile".
259 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
260 * file error you now get a message box (in the Win32 GUI) rather
261 * than the program exiting with no explanation.
262 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
264 * - Removed tabs from "config"
265 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
266 * - Bumped up version number.
268 * Revision 1.2 2001/05/17 23:01:01 oes
269 * - Cleaned CRLF's from the sources and related files
271 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
272 * Initial import of version 2.9.3 source tree
275 *********************************************************************/
281 #include <sys/types.h>
292 # include <windows.h>
295 # ifndef _WIN_CONSOLE
297 # endif /* ndef _WIN_CONSOLE */
299 /* VC++ has "_snprintf", not "snprintf" */
300 #define snprintf _snprintf
302 #else /* ifndef _WIN32 */
306 # include <sys/wait.h>
308 # include <sys/time.h>
309 # include <sys/stat.h>
319 #include "miscutil.h"
323 #include "urlmatch.h"
325 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
328 * Fix a problem with Solaris. There should be no effect on other
330 * Solaris's isspace() is a macro which uses it's argument directly
331 * as an array index. Therefore we need to make sure that high-bit
332 * characters generate +ve values, and ideally we also want to make
333 * the argument match the declared parameter type of "int".
335 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
336 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
338 #ifdef FEATURE_TOGGLE
339 /* by haroon - indicates if ijb is enabled */
340 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
341 #endif /* def FEATURE_TOGGLE */
343 /* The filename of the configfile */
344 const char *configfile = NULL;
347 * CGI functions will later need access to the invocation args,
348 * so we will make argc and argv global.
351 const char **Argv = NULL;
353 static struct file_list *current_configfile = NULL;
357 * This takes the "cryptic" hash of each keyword and aliases them to
358 * something a little more readable. This also makes changing the
359 * hash values easier if they should change or the hash algorthm changes.
360 * Use the included "hash" program to find out what the hash will be
361 * for any string supplied on the command line. (Or just put it in the
362 * config file and read the number from the error message in the log).
364 * Please keep this list sorted alphabetically (but with the Windows
365 * console and GUI specific options last).
368 #define hash_actions_file 1196306641ul /* "actionsfile" */
369 #define hash_admin_address 4112573064ul /* "admin-address" */
370 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
371 #define hash_confdir 1978389ul /* "confdir" */
372 #define hash_debug 78263ul /* "debug" */
373 #define hash_deny_access 1227333715ul /* "deny-access" */
374 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
375 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
376 #define hash_filterfile 250887266ul /* "filterfile" */
377 #define hash_forward 2029845ul /* "forward" */
378 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
379 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
380 #define hash_jarfile 2046641ul /* "jarfile" */
381 #define hash_listen_address 1255650842ul /* "listen-address" */
382 #define hash_logdir 422889ul /* "logdir" */
383 #define hash_logfile 2114766ul /* "logfile" */
384 #define hash_permit_access 3587953268ul /* "permit-access" */
385 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
386 #define hash_single_threaded 4250084780ul /* "single-threaded" */
387 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
388 #define hash_toggle 447966ul /* "toggle" */
389 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
390 #define hash_trustfile 56494766ul /* "trustfile" */
392 #define hash_activity_animation 1817904738ul /* "activity-animation" */
393 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
394 #define hash_hide_console 2048809870ul /* "hide-console" */
395 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
396 #define hash_log_font_name 2866730124ul /* "log-font-name" */
397 #define hash_log_font_size 2866731014ul /* "log-font-size" */
398 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
399 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
400 #define hash_log_messages 2291744899ul /* "log-messages" */
401 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
404 static void savearg(char *command, char *argument, struct configuration_spec * config);
406 /*********************************************************************
408 * Function : unload_configfile
410 * Description : Free the config structure and all components.
413 * 1 : data: struct configuration_spec to unload
417 *********************************************************************/
418 void unload_configfile (void * data)
420 struct configuration_spec * config = (struct configuration_spec *)data;
421 struct forward_spec *cur_fwd = config->forward;
423 struct access_control_list *cur_acl = config->acl;
425 while (cur_acl != NULL)
427 struct access_control_list * next_acl = cur_acl->next;
432 #endif /* def FEATURE_ACL */
434 while (cur_fwd != NULL)
436 struct forward_spec * next_fwd = cur_fwd->next;
437 free_url_spec(cur_fwd->url);
439 freez(cur_fwd->gateway_host);
440 freez(cur_fwd->forward_host);
444 config->forward = NULL;
446 #ifdef FEATURE_COOKIE_JAR
447 if ( NULL != config->jar )
449 fclose( config->jar );
452 #endif /* def FEATURE_COOKIE_JAR */
454 freez(config->confdir);
455 freez(config->logdir);
457 freez(config->haddr);
458 freez(config->logfile);
460 freez(config->actions_file);
461 freez(config->admin_address);
462 freez(config->proxy_info_url);
463 freez(config->proxy_args);
465 #ifdef FEATURE_COOKIE_JAR
466 freez(config->jarfile);
467 #endif /* def FEATURE_COOKIE_JAR */
469 freez(config->re_filterfile);
474 #ifdef FEATURE_GRACEFUL_TERMINATION
475 /*********************************************************************
477 * Function : unload_current_config_file
479 * Description : Unloads current config file - reset to state at
480 * beginning of program.
486 *********************************************************************/
487 void unload_current_config_file(void)
489 if (current_configfile)
491 current_configfile->unloader = unload_configfile;
492 current_configfile = NULL;
498 /*********************************************************************
500 * Function : load_config
502 * Description : Load the config file and all parameters.
505 * 1 : csp = Client state (the config member will be
506 * filled in by this function).
508 * Returns : 0 => Ok, everything else is an error.
510 *********************************************************************/
511 struct configuration_spec * load_config(void)
513 char buf[BUFFER_SIZE];
515 FILE *configfp = NULL;
516 struct configuration_spec * config = NULL;
517 struct client_state * fake_csp;
518 struct file_list *fs;
519 unsigned long linenum = 0;
521 if ( !check_file_changed(current_configfile, configfile, &fs))
523 /* No need to load */
524 return ((struct configuration_spec *)current_configfile->f);
528 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
532 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
534 #ifdef FEATURE_TOGGLE
536 #endif /* def FEATURE_TOGGLE */
538 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
544 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
545 /* Never get here - LOG_LEVEL_FATAL causes program exit */
549 * This is backwards from how it's usually done.
550 * Following the usual pattern, "fs" would be stored in a member
551 * variable in "csp", and then we'd access "config" from "fs->f",
552 * using a cast. However, "config" is used so often that a
553 * cast each time would be very ugly, and the extra indirection
554 * would waste CPU cycles. Therefore we store "config" in
555 * "csp->config", and "fs" in "csp->config->config_file_list".
557 config->config_file_list = fs;
563 config->multi_threaded = 1;
564 config->hport = HADDR_PORT;
565 config->buffer_limit = 4096 * 1024;
566 config->proxy_args = strdup("");
568 if ((configfp = fopen(configfile, "r")) == NULL)
570 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
572 /* Never get here - LOG_LEVEL_FATAL causes program exit */
575 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
577 char cmd[BUFFER_SIZE];
578 char arg[BUFFER_SIZE];
579 char tmp[BUFFER_SIZE];
581 struct access_control_list *cur_acl;
582 #endif /* def FEATURE_ACL */
583 struct forward_spec *cur_fwd;
589 /* Copy command (i.e. up to space or tab) into cmd */
592 while (*p && (*p != ' ') && (*p != '\t'))
598 /* Skip over the whitespace in buf */
599 while (*p && ((*p == ' ') || (*p == '\t')))
604 /* Copy the argument into arg */
607 /* Should never happen, but check this anyway */
613 /* Make sure the command field is lower case */
618 *p = ijb_tolower(*p);
622 /* Save the argument for show-proxy-args */
623 savearg(cmd, arg, config);
626 switch( hash_string( cmd ) )
628 /* *************************************************************************
629 * actionsfile actions-file-name
630 * In confdir by default
631 * *************************************************************************/
632 case hash_actions_file :
633 freez(config->actions_file);
634 config->actions_file = make_path(config->confdir, arg);
637 /* *************************************************************************
638 * admin-address email-address
639 * *************************************************************************/
640 case hash_admin_address :
641 freez(config->admin_address);
642 config->admin_address = strdup(arg);
645 /* *************************************************************************
647 * *************************************************************************/
648 case hash_buffer_limit :
649 config->buffer_limit = (size_t) 1024 * atoi(arg);
652 /* *************************************************************************
653 * confdir directory-name
654 * *************************************************************************/
656 freez(config->confdir);
657 config->confdir = make_path( NULL, arg);
660 /* *************************************************************************
662 * Specifies debug level, multiple values are ORed together.
663 * *************************************************************************/
665 config->debug |= atoi(arg);
668 /* *************************************************************************
669 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
670 * *************************************************************************/
672 case hash_deny_access:
673 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
675 if ((vec_count != 1) && (vec_count != 2))
677 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
678 "deny-access directive in configuration file.");
679 string_append(&config->proxy_args,
680 "<br>\nWARNING: Wrong number of parameters for "
681 "deny-access directive in configuration file.<br><br>\n");
685 /* allocate a new node */
686 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
690 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
691 /* Never get here - LOG_LEVEL_FATAL causes program exit */
694 cur_acl->action = ACL_DENY;
696 if (acl_addr(vec[0], cur_acl->src) < 0)
698 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
699 "directive in configuration file: \"%s\"", vec[0]);
700 string_append(&config->proxy_args,
701 "<br>\nWARNING: Invalid source IP for deny-access directive"
702 " in configuration file: \"");
703 string_append(&config->proxy_args,
705 string_append(&config->proxy_args,
712 if (acl_addr(vec[1], cur_acl->dst) < 0)
714 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
715 "directive in configuration file: \"%s\"", vec[0]);
716 string_append(&config->proxy_args,
717 "<br>\nWARNING: Invalid destination IP for deny-access directive"
718 " in configuration file: \"");
719 string_append(&config->proxy_args,
721 string_append(&config->proxy_args,
729 * Add it to the list. Note we reverse the list to get the
730 * behaviour the user expects. With both the ACL and
731 * actions file, the last match wins. However, the internal
732 * implementations are different: The actions file is stored
733 * in the same order as the file, and scanned completely.
734 * With the ACL, we reverse the order as we load it, then
735 * when we scan it we stop as soon as we get a match.
737 cur_acl->next = config->acl;
738 config->acl = cur_acl;
741 #endif /* def FEATURE_ACL */
743 /* *************************************************************************
744 * enable-edit-actions 0|1
745 * *************************************************************************/
746 #ifdef FEATURE_CGI_EDIT_ACTIONS
747 case hash_enable_edit_actions:
748 if ((*arg != '\0') && (0 != atoi(arg)))
750 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
754 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
757 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
759 /* *************************************************************************
760 * enable-remote-toggle 0|1
761 * *************************************************************************/
762 #ifdef FEATURE_CGI_EDIT_ACTIONS
763 case hash_enable_remote_toggle:
764 if ((*arg != '\0') && (0 != atoi(arg)))
766 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
770 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
773 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
775 /* *************************************************************************
776 * forward url-pattern (.|http-proxy-host[:port])
777 * *************************************************************************/
779 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
783 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
784 "directive in configuration file.");
785 string_append(&config->proxy_args,
786 "<br>\nWARNING: Wrong number of parameters for "
787 "forward directive in configuration file.");
791 /* allocate a new node */
792 cur_fwd = zalloc(sizeof(*cur_fwd));
795 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
796 /* Never get here - LOG_LEVEL_FATAL causes program exit */
800 cur_fwd->type = SOCKS_NONE;
802 /* Save the URL pattern */
803 if (create_url_spec(cur_fwd->url, vec[0]))
805 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
806 "directive in configuration file.");
807 string_append(&config->proxy_args,
808 "<br>\nWARNING: Bad URL specifier for "
809 "forward directive in configuration file.");
813 /* Parse the parent HTTP proxy host:port */
816 if (strcmp(p, ".") != 0)
818 cur_fwd->forward_host = strdup(p);
820 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
823 cur_fwd->forward_port = atoi(p);
826 if (cur_fwd->forward_port <= 0)
828 cur_fwd->forward_port = 8000;
833 cur_fwd->next = config->forward;
834 config->forward = cur_fwd;
838 /* *************************************************************************
839 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
840 * *************************************************************************/
841 case hash_forward_socks4:
842 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
846 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
847 "forward-socks4 directive in configuration file.");
848 string_append(&config->proxy_args,
849 "<br>\nWARNING: Wrong number of parameters for "
850 "forward-socks4 directive in configuration file.");
854 /* allocate a new node */
855 cur_fwd = zalloc(sizeof(*cur_fwd));
858 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
859 /* Never get here - LOG_LEVEL_FATAL causes program exit */
863 cur_fwd->type = SOCKS_4;
865 /* Save the URL pattern */
866 if (create_url_spec(cur_fwd->url, vec[0]))
868 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
869 "directive in configuration file.");
870 string_append(&config->proxy_args,
871 "<br>\nWARNING: Bad URL specifier for "
872 "forward-socks4 directive in configuration file.");
876 /* Parse the SOCKS proxy host[:port] */
879 if (strcmp(p, ".") != 0)
881 cur_fwd->gateway_host = strdup(p);
883 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
886 cur_fwd->gateway_port = atoi(p);
888 if (cur_fwd->gateway_port <= 0)
890 cur_fwd->gateway_port = 1080;
894 /* Parse the parent HTTP proxy host[:port] */
897 if (strcmp(p, ".") != 0)
899 cur_fwd->forward_host = strdup(p);
901 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
904 cur_fwd->forward_port = atoi(p);
907 if (cur_fwd->forward_port <= 0)
909 cur_fwd->forward_port = 8000;
914 cur_fwd->next = config->forward;
915 config->forward = cur_fwd;
919 /* *************************************************************************
920 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
921 * *************************************************************************/
922 case hash_forward_socks4a:
923 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
927 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
928 "forward-socks4a directive in configuration file.");
929 string_append(&config->proxy_args,
930 "<br>\nWARNING: Wrong number of parameters for "
931 "forward-socks4a directive in configuration file.");
935 /* allocate a new node */
936 cur_fwd = zalloc(sizeof(*cur_fwd));
939 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
940 /* Never get here - LOG_LEVEL_FATAL causes program exit */
944 cur_fwd->type = SOCKS_4A;
946 /* Save the URL pattern */
947 if (create_url_spec(cur_fwd->url, vec[0]))
949 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
950 "directive in configuration file.");
951 string_append(&config->proxy_args,
952 "<br>\nWARNING: Bad URL specifier for "
953 "forward-socks4a directive in configuration file.");
957 /* Parse the SOCKS proxy host[:port] */
960 cur_fwd->gateway_host = strdup(p);
962 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
965 cur_fwd->gateway_port = atoi(p);
967 if (cur_fwd->gateway_port <= 0)
969 cur_fwd->gateway_port = 1080;
972 /* Parse the parent HTTP proxy host[:port] */
975 if (strcmp(p, ".") != 0)
977 cur_fwd->forward_host = strdup(p);
979 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
982 cur_fwd->forward_port = atoi(p);
985 if (cur_fwd->forward_port <= 0)
987 cur_fwd->forward_port = 8000;
992 cur_fwd->next = config->forward;
993 config->forward = cur_fwd;
997 /* *************************************************************************
998 * jarfile jar-file-name
999 * In logdir by default
1000 * *************************************************************************/
1001 #ifdef FEATURE_COOKIE_JAR
1003 freez(config->jarfile);
1004 config->jarfile = make_path(config->logdir, arg);
1006 #endif /* def FEATURE_COOKIE_JAR */
1008 /* *************************************************************************
1009 * listen-address [ip][:port]
1010 * *************************************************************************/
1011 case hash_listen_address :
1012 freez(config->haddr);
1013 config->haddr = strdup(arg);
1016 /* *************************************************************************
1017 * logdir directory-name
1018 * *************************************************************************/
1020 freez(config->logdir);
1021 config->logdir = make_path(NULL, arg);
1024 /* *************************************************************************
1025 * logfile log-file-name
1026 * In logdir by default
1027 * *************************************************************************/
1029 freez(config->logfile);
1030 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1033 /* *************************************************************************
1034 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1035 * *************************************************************************/
1037 case hash_permit_access:
1038 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1040 if ((vec_count != 1) && (vec_count != 2))
1042 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1043 "permit-access directive in configuration file.");
1044 string_append(&config->proxy_args,
1045 "<br>\nWARNING: Wrong number of parameters for "
1046 "permit-access directive in configuration file.<br><br>\n");
1051 /* allocate a new node */
1052 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1054 if (cur_acl == NULL)
1056 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1057 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1060 cur_acl->action = ACL_PERMIT;
1062 if (acl_addr(vec[0], cur_acl->src) < 0)
1064 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1065 "directive in configuration file: \"%s\"", vec[0]);
1066 string_append(&config->proxy_args,
1067 "<br>\nWARNING: Invalid source IP for permit-access directive"
1068 " in configuration file: \"");
1069 string_append(&config->proxy_args,
1071 string_append(&config->proxy_args,
1078 if (acl_addr(vec[1], cur_acl->dst) < 0)
1080 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1081 "permit-access directive in configuration file: \"%s\"",
1083 string_append(&config->proxy_args,
1084 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1085 " in configuration file: \"");
1086 string_append(&config->proxy_args,
1088 string_append(&config->proxy_args,
1096 * Add it to the list. Note we reverse the list to get the
1097 * behaviour the user expects. With both the ACL and
1098 * actions file, the last match wins. However, the internal
1099 * implementations are different: The actions file is stored
1100 * in the same order as the file, and scanned completely.
1101 * With the ACL, we reverse the order as we load it, then
1102 * when we scan it we stop as soon as we get a match.
1104 cur_acl->next = config->acl;
1105 config->acl = cur_acl;
1108 #endif /* def FEATURE_ACL */
1110 /* *************************************************************************
1111 * proxy-info-url url
1112 * *************************************************************************/
1113 case hash_proxy_info_url :
1114 freez(config->proxy_info_url);
1115 config->proxy_info_url = strdup(arg);
1118 /* *************************************************************************
1119 * re_filterfile file-name
1120 * In confdir by default.
1121 * *************************************************************************/
1122 case hash_filterfile :
1123 freez(config->re_filterfile);
1124 config->re_filterfile = make_path(config->confdir, arg);
1127 /* *************************************************************************
1129 * *************************************************************************/
1130 case hash_single_threaded :
1131 config->multi_threaded = 0;
1134 /* *************************************************************************
1136 * *************************************************************************/
1137 #ifdef FEATURE_TOGGLE
1139 g_bToggleIJB = atoi(arg);
1141 #endif /* def FEATURE_TOGGLE */
1143 /* *************************************************************************
1144 * trust-info-url url
1145 * *************************************************************************/
1146 #ifdef FEATURE_TRUST
1147 case hash_trust_info_url :
1148 enlist(config->trust_info, arg);
1150 #endif /* def FEATURE_TRUST */
1152 /* *************************************************************************
1153 * trustfile filename
1154 * (In confdir by default.)
1155 * *************************************************************************/
1156 #ifdef FEATURE_TRUST
1157 case hash_trustfile :
1158 freez(config->trustfile);
1159 config->trustfile = make_path(config->confdir, arg);
1161 #endif /* def FEATURE_TRUST */
1164 /* *************************************************************************
1165 * Win32 Console options:
1166 * *************************************************************************/
1168 /* *************************************************************************
1170 * *************************************************************************/
1172 case hash_hide_console :
1175 #endif /*def _WIN_CONSOLE*/
1178 /* *************************************************************************
1179 * Win32 GUI options:
1180 * *************************************************************************/
1182 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1183 /* *************************************************************************
1184 * activity-animation (0|1)
1185 * *************************************************************************/
1186 case hash_activity_animation :
1187 g_bShowActivityAnimation = atoi(arg);
1190 /* *************************************************************************
1191 * close-button-minimizes (0|1)
1192 * *************************************************************************/
1193 case hash_close_button_minimizes :
1194 g_bCloseHidesWindow = atoi(arg);
1197 /* *************************************************************************
1198 * log-buffer-size (0|1)
1199 * *************************************************************************/
1200 case hash_log_buffer_size :
1201 g_bLimitBufferSize = atoi(arg);
1204 /* *************************************************************************
1205 * log-font-name fontnane
1206 * *************************************************************************/
1207 case hash_log_font_name :
1208 strcpy( g_szFontFaceName, arg );
1211 /* *************************************************************************
1213 * *************************************************************************/
1214 case hash_log_font_size :
1215 g_nFontSize = atoi(arg);
1218 /* *************************************************************************
1219 * log-highlight-messages (0|1)
1220 * *************************************************************************/
1221 case hash_log_highlight_messages :
1222 g_bHighlightMessages = atoi(arg);
1225 /* *************************************************************************
1227 * *************************************************************************/
1228 case hash_log_max_lines :
1229 g_nMaxBufferLines = atoi(arg);
1232 /* *************************************************************************
1233 * log-messages (0|1)
1234 * *************************************************************************/
1235 case hash_log_messages :
1236 g_bLogMessages = atoi(arg);
1239 /* *************************************************************************
1240 * show-on-task-bar (0|1)
1241 * *************************************************************************/
1242 case hash_show_on_task_bar :
1243 g_bShowOnTaskBar = atoi(arg);
1246 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1249 /* *************************************************************************
1250 * Warnings about unsupported features
1251 * *************************************************************************/
1253 case hash_deny_access:
1254 #endif /* ndef FEATURE_ACL */
1255 #ifndef FEATURE_CGI_EDIT_ACTIONS
1256 case hash_enable_edit_actions:
1257 case hash_enable_remote_toggle:
1258 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1259 #ifndef FEATURE_COOKIE_JAR
1261 #endif /* ndef FEATURE_COOKIE_JAR */
1263 case hash_permit_access:
1264 #endif /* ndef FEATURE_ACL */
1265 #ifndef FEATURE_TOGGLE
1267 #endif /* ndef FEATURE_TOGGLE */
1268 #ifndef FEATURE_TRUST
1269 case hash_trustfile :
1270 case hash_trust_info_url :
1271 #endif /* ndef FEATURE_TRUST */
1273 #ifndef _WIN_CONSOLE
1274 case hash_hide_console :
1275 #endif /* ndef _WIN_CONSOLE */
1277 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1278 case hash_activity_animation :
1279 case hash_close_button_minimizes :
1280 case hash_log_buffer_size :
1281 case hash_log_font_name :
1282 case hash_log_font_size :
1283 case hash_log_highlight_messages :
1284 case hash_log_max_lines :
1285 case hash_log_messages :
1286 case hash_show_on_task_bar :
1287 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1288 /* These warnings are annoying - so hide them. -- Jon */
1289 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1292 /* *************************************************************************/
1294 /* *************************************************************************/
1296 * I decided that I liked this better as a warning than an
1297 * error. To change back to an error, just change log level
1298 * to LOG_LEVEL_FATAL.
1300 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1301 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1302 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1303 string_append(&config->proxy_args, buf);
1304 string_append(&config->proxy_args, "<br><br>\n");
1307 /* *************************************************************************/
1308 } /* end switch( hash_string(cmd) ) */
1309 } /* end while ( read_config_line(...) ) */
1313 if (NULL == config->proxy_args)
1315 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1318 init_error_log(Argv[0], config->logfile, config->debug);
1320 if (config->actions_file)
1322 add_loader(load_actions_file, config);
1325 if (config->re_filterfile)
1327 add_loader(load_re_filterfile, config);
1330 #ifdef FEATURE_TRUST
1331 if (config->trustfile)
1333 add_loader(load_trustfile, config);
1335 #endif /* def FEATURE_TRUST */
1337 #ifdef FEATURE_COOKIE_JAR
1338 if ( NULL != config->jarfile )
1340 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1342 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1343 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1345 setbuf(config->jar, NULL);
1347 #endif /* def FEATURE_COOKIE_JAR */
1349 if ( NULL == config->haddr )
1351 config->haddr = strdup( HADDR_DEFAULT );
1354 if ( NULL != config->haddr )
1356 if (NULL != (p = strchr(config->haddr, ':')))
1361 config->hport = atoi(p);
1365 if (config->hport <= 0)
1368 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1369 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1371 if (*config->haddr == '\0')
1373 config->haddr = NULL;
1378 * Want to run all the loaders once now.
1380 * Need to set up a fake csp, so they can get to the config.
1382 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1383 fake_csp->config = config;
1385 if (run_loader(fake_csp))
1388 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1389 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1393 /* FIXME: this is a kludge for win32 */
1394 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1396 g_actions_file = config->actions_file;
1397 g_re_filterfile = config->re_filterfile;
1399 #ifdef FEATURE_TRUST
1400 g_trustfile = config->trustfile;
1401 #endif /* def FEATURE_TRUST */
1404 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1405 /* FIXME: end kludge */
1408 config->need_bind = 1;
1410 if (current_configfile)
1412 struct configuration_spec * oldcfg = (struct configuration_spec *)
1413 current_configfile->f;
1415 * Check if config->haddr,hport == oldcfg->haddr,hport
1417 * The following could be written more compactly as a single,
1418 * (unreadably long) if statement.
1420 config->need_bind = 0;
1421 if (config->hport != oldcfg->hport)
1423 config->need_bind = 1;
1425 else if (config->haddr == NULL)
1427 if (oldcfg->haddr != NULL)
1429 config->need_bind = 1;
1432 else if (oldcfg->haddr == NULL)
1434 config->need_bind = 1;
1436 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1438 config->need_bind = 1;
1441 current_configfile->unloader = unload_configfile;
1444 fs->next = files->next;
1447 current_configfile = fs;
1453 /*********************************************************************
1455 * Function : savearg
1457 * Description : Called from `load_config'. It saves each non-empty
1458 * and non-comment line from config into
1459 * config->proxy_args. This is used to create the
1460 * show-proxy-args page. On error, frees
1461 * config->proxy_args and sets it to NULL
1464 * 1 : command = config setting that was found
1465 * 2 : argument = the setting's argument (if any)
1469 *********************************************************************/
1470 static void savearg(char *command, char *argument, struct configuration_spec * config)
1481 s = html_encode(command);
1485 freez(config->proxy_args);
1488 string_append(&buf, "<a href=\"" REDIRECT_URL "option#");
1489 string_append(&buf, s);
1490 string_append(&buf, "\">");
1491 string_join (&buf, s);
1492 string_append(&buf, "</a> ");
1494 if ( (NULL != argument) && ('\0' != *argument) )
1496 s = html_encode(argument);
1500 freez(config->proxy_args);
1504 if (strncmpic(argument, "http://", 7) == 0)
1506 string_append(&buf, "<a href=\"");
1507 string_append(&buf, s);
1508 string_append(&buf, "\">");
1509 string_join (&buf, s);
1510 string_append(&buf, "</a>");
1514 string_join (&buf, s);
1518 string_append(&buf, "<br>\n");
1520 string_join(&config->proxy_args, buf);