1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.40 2002/03/26 22:29:55 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.40 2002/03/26 22:29:55 swa
39 * we have a new homepage!
41 * Revision 1.39 2002/03/24 13:25:43 swa
42 * name change related issues
44 * Revision 1.38 2002/03/24 13:05:48 jongfoster
45 * Renaming re_filterfile to filterfile
47 * Revision 1.37 2002/03/16 23:54:06 jongfoster
48 * Adding graceful termination feature, to help look for memory leaks.
49 * If you enable this (which, by design, has to be done by hand
50 * editing config.h) and then go to http://i.j.b/die, then the program
51 * will exit cleanly after the *next* request. It should free all the
52 * memory that was used.
54 * Revision 1.36 2002/03/13 00:27:05 jongfoster
57 * Revision 1.35 2002/03/07 03:52:44 oes
58 * Set logging to tty for --no-daemon mode
60 * Revision 1.34 2002/03/06 23:14:35 jongfoster
61 * Trivial cosmetic changes to make function comments easier to find.
63 * Revision 1.33 2002/03/05 04:52:42 oes
64 * Deleted non-errlog debugging code
66 * Revision 1.32 2002/03/04 18:24:53 oes
67 * Re-enabled output of unknown config directive hash
69 * Revision 1.31 2002/03/03 15:07:20 oes
70 * Re-enabled automatic config reloading
72 * Revision 1.30 2002/01/22 23:31:43 jongfoster
73 * Replacing strsav() with string_append()
75 * Revision 1.29 2002/01/17 21:02:30 jongfoster
76 * Moving all our URL and URL pattern parsing code to urlmatch.c.
78 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
80 * Revision 1.28 2001/12/30 14:07:32 steudten
81 * - Add signal handling (unix)
82 * - Add SIGHUP handler (unix)
83 * - Add creation of pidfile (unix)
84 * - Add action 'top' in rc file (RH)
85 * - Add entry 'SIGNALS' to manpage
86 * - Add exit message to logfile (unix)
88 * Revision 1.27 2001/11/07 00:02:13 steudten
89 * Add line number in error output for lineparsing for
90 * actionsfile and configfile.
91 * Special handling for CLF added.
93 * Revision 1.26 2001/11/05 21:41:43 steudten
94 * Add changes to be a real daemon just for unix os.
95 * (change cwd to /, detach from controlling tty, set
96 * process group and session leader to the own process.
98 * Add some fatal-error log message for failed malloc().
99 * Add '-d' if compiled with 'configure --with-debug' to
100 * enable debug output.
102 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
103 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
104 * threads to call select() simultaneously. So, it's time to do a real, live,
105 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
106 * (native). Both versions will work, but using __OS2__ offers multi-threading.
108 * Revision 1.24 2001/10/23 21:40:30 jongfoster
109 * Added support for enable-edit-actions and enable-remote-toggle config
112 * Revision 1.23 2001/10/07 15:36:00 oes
113 * Introduced new config option "buffer-limit"
115 * Revision 1.22 2001/09/22 16:36:59 jongfoster
116 * Removing unused parameter fs from read_config_line()
118 * Revision 1.21 2001/09/16 17:10:43 jongfoster
119 * Moving function savearg() here, since it was the only thing left in
122 * Revision 1.20 2001/07/30 22:08:36 jongfoster
123 * Tidying up #defines:
124 * - All feature #defines are now of the form FEATURE_xxx
125 * - Permanently turned off WIN_GUI_EDIT
126 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
128 * Revision 1.19 2001/07/15 17:45:16 jongfoster
129 * Removing some unused #includes
131 * Revision 1.18 2001/07/13 14:01:14 oes
132 * - Removed all #ifdef PCRS
133 * - Removed vim-settings
135 * Revision 1.17 2001/06/29 13:31:03 oes
136 * - Improved comments
137 * - Fixed (actionsfile) and sorted hashes
138 * - Introduced admin_address and proxy-info-url
139 * as config parameters
140 * - Renamed config->proxy_args_invocation (which didn't have
141 * the invocation but the options!) to config->proxy_args
142 * - Various adaptions
143 * - Removed logentry from cancelled commit
145 * Revision 1.16 2001/06/09 10:55:28 jongfoster
146 * Changing BUFSIZ ==> BUFFER_SIZE
148 * Revision 1.15 2001/06/07 23:13:40 jongfoster
149 * Merging ACL and forward files into config file.
150 * Cosmetic: Sorting config file options alphabetically.
151 * Cosmetic: Adding brief syntax comments to config file options.
153 * Revision 1.14 2001/06/07 14:46:25 joergs
154 * Missing make_path() added for re_filterfile.
156 * Revision 1.13 2001/06/05 22:33:54 jongfoster
158 * Fixed minor memory leak.
159 * Also now uses make_path to prepend the pathnames.
161 * Revision 1.12 2001/06/05 20:04:09 jongfoster
162 * Now uses _snprintf() in place of snprintf() under Win32.
164 * Revision 1.11 2001/06/04 18:31:58 swa
165 * files are now prefixed with either `confdir' or `logdir'.
166 * `make redhat-dist' replaces both entries confdir and logdir
169 * Revision 1.10 2001/06/03 19:11:54 oes
170 * introduced confdir option
172 * Revision 1.9 2001/06/01 20:06:24 jongfoster
173 * Removed support for "tinygif" option - moved to actions file.
175 * Revision 1.8 2001/05/31 21:27:13 jongfoster
176 * Removed many options from the config file and into the
177 * "actions" file: add_forwarded, suppress_vanilla_wafer,
178 * wafer, add_header, user_agent, referer, from
179 * Also globally replaced "permission" with "action".
181 * Revision 1.7 2001/05/29 09:50:24 jongfoster
182 * Unified blocklist/imagelist/permissionslist.
183 * File format is still under discussion, but the internal changes
186 * Also modified interceptor behaviour:
187 * - We now intercept all URLs beginning with one of the following
188 * prefixes (and *only* these prefixes):
190 * * http://ijbswa.sf.net/config/
191 * * http://ijbswa.sourceforge.net/config/
192 * - New interceptors "home page" - go to http://i.j.b/ to see it.
193 * - Internal changes so that intercepted and fast redirect pages
194 * are not replaced with an image.
195 * - Interceptors now have the option to send a binary page direct
196 * to the client. (i.e. ijb-send-banner uses this)
197 * - Implemented show-url-info interceptor. (Which is why I needed
198 * the above interceptors changes - a typical URL is
199 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
200 * The previous mechanism would not have intercepted that, and
201 * if it had been intercepted then it then it would have replaced
204 * Revision 1.6 2001/05/26 00:28:36 jongfoster
205 * Automatic reloading of config file.
206 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
207 * Most of the global variables have been moved to a new
208 * struct configuration_spec, accessed through csp->config->globalname
209 * Most of the globals remaining are used by the Win32 GUI.
211 * Revision 1.5 2001/05/25 22:34:30 jongfoster
214 * Revision 1.4 2001/05/22 18:46:04 oes
216 * - Enabled filtering banners by size rather than URL
217 * by adding patterns that replace all standard banner
218 * sizes with the "Junkbuster" gif to the re_filterfile
220 * - Enabled filtering WebBugs by providing a pattern
221 * which kills all 1x1 images
223 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
224 * which is selected by the (nonstandard and therefore
225 * capital) letter 'U' in the option string.
226 * It causes the quantifiers to be ungreedy by default.
227 * Appending a ? turns back to greedy (!).
229 * - Added a new interceptor ijb-send-banner, which
230 * sends back the "Junkbuster" gif. Without imagelist or
231 * MSIE detection support, or if tinygif = 1, or the
232 * URL isn't recognized as an imageurl, a lame HTML
233 * explanation is sent instead.
235 * - Added new feature, which permits blocking remote
236 * script redirects and firing back a local redirect
238 * The feature is conditionally compiled, i.e. it
239 * can be disabled with --disable-fast-redirects,
240 * plus it must be activated by a "fast-redirects"
241 * line in the config file, has its own log level
242 * and of course wants to be displayed by show-proxy-args
243 * Note: Boy, all the #ifdefs in 1001 locations and
244 * all the fumbling with configure.in and acconfig.h
245 * were *way* more work than the feature itself :-(
247 * - Because a generic redirect template was needed for
248 * this, tinygif = 3 now uses the same.
250 * - Moved GIFs, and other static HTTP response templates
255 * - Removed some >400 CRs again (Jon, you really worked
258 * Revision 1.3 2001/05/20 01:21:20 jongfoster
259 * Version 2.9.4 checkin.
260 * - Merged popupfile and cookiefile, and added control over PCRS
261 * filtering, in new "permissionsfile".
262 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
263 * file error you now get a message box (in the Win32 GUI) rather
264 * than the program exiting with no explanation.
265 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
267 * - Removed tabs from "config"
268 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
269 * - Bumped up version number.
271 * Revision 1.2 2001/05/17 23:01:01 oes
272 * - Cleaned CRLF's from the sources and related files
274 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
275 * Initial import of version 2.9.3 source tree
278 *********************************************************************/
284 #include <sys/types.h>
298 # include <windows.h>
301 # ifndef _WIN_CONSOLE
303 # endif /* ndef _WIN_CONSOLE */
305 /* VC++ has "_snprintf", not "snprintf" */
306 #define snprintf _snprintf
308 #else /* ifndef _WIN32 */
312 # include <sys/wait.h>
314 # include <sys/time.h>
315 # include <sys/stat.h>
325 #include "miscutil.h"
329 #include "urlmatch.h"
331 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
334 * Fix a problem with Solaris. There should be no effect on other
336 * Solaris's isspace() is a macro which uses it's argument directly
337 * as an array index. Therefore we need to make sure that high-bit
338 * characters generate +ve values, and ideally we also want to make
339 * the argument match the declared parameter type of "int".
341 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
342 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
344 #ifdef FEATURE_TOGGLE
345 /* by haroon - indicates if ijb is enabled */
346 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
347 #endif /* def FEATURE_TOGGLE */
349 /* The filename of the configfile */
350 const char *configfile = NULL;
353 * CGI functions will later need access to the invocation args,
354 * so we will make argc and argv global.
357 const char **Argv = NULL;
359 static struct file_list *current_configfile = NULL;
363 * This takes the "cryptic" hash of each keyword and aliases them to
364 * something a little more readable. This also makes changing the
365 * hash values easier if they should change or the hash algorthm changes.
366 * Use the included "hash" program to find out what the hash will be
367 * for any string supplied on the command line. (Or just put it in the
368 * config file and read the number from the error message in the log).
370 * Please keep this list sorted alphabetically (but with the Windows
371 * console and GUI specific options last).
374 #define hash_actions_file 1196306641ul /* "actionsfile" */
375 #define hash_admin_address 4112573064ul /* "admin-address" */
376 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
377 #define hash_confdir 1978389ul /* "confdir" */
378 #define hash_debug 78263ul /* "debug" */
379 #define hash_deny_access 1227333715ul /* "deny-access" */
380 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
381 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
382 #define hash_filterfile 250887266ul /* "filterfile" */
383 #define hash_forward 2029845ul /* "forward" */
384 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
385 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
386 #define hash_jarfile 2046641ul /* "jarfile" */
387 #define hash_listen_address 1255650842ul /* "listen-address" */
388 #define hash_logdir 422889ul /* "logdir" */
389 #define hash_logfile 2114766ul /* "logfile" */
390 #define hash_permit_access 3587953268ul /* "permit-access" */
391 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
392 #define hash_single_threaded 4250084780ul /* "single-threaded" */
393 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
394 #define hash_toggle 447966ul /* "toggle" */
395 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
396 #define hash_trustfile 56494766ul /* "trustfile" */
398 #define hash_activity_animation 1817904738ul /* "activity-animation" */
399 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
400 #define hash_hide_console 2048809870ul /* "hide-console" */
401 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
402 #define hash_log_font_name 2866730124ul /* "log-font-name" */
403 #define hash_log_font_size 2866731014ul /* "log-font-size" */
404 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
405 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
406 #define hash_log_messages 2291744899ul /* "log-messages" */
407 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
410 static void savearg(char *command, char *argument, struct configuration_spec * config);
412 /*********************************************************************
414 * Function : unload_configfile
416 * Description : Free the config structure and all components.
419 * 1 : data: struct configuration_spec to unload
423 *********************************************************************/
424 void unload_configfile (void * data)
426 struct configuration_spec * config = (struct configuration_spec *)data;
427 struct forward_spec *cur_fwd = config->forward;
429 struct access_control_list *cur_acl = config->acl;
431 while (cur_acl != NULL)
433 struct access_control_list * next_acl = cur_acl->next;
438 #endif /* def FEATURE_ACL */
440 while (cur_fwd != NULL)
442 struct forward_spec * next_fwd = cur_fwd->next;
443 free_url_spec(cur_fwd->url);
445 freez(cur_fwd->gateway_host);
446 freez(cur_fwd->forward_host);
450 config->forward = NULL;
452 #ifdef FEATURE_COOKIE_JAR
453 if ( NULL != config->jar )
455 fclose( config->jar );
458 #endif /* def FEATURE_COOKIE_JAR */
460 freez(config->confdir);
461 freez(config->logdir);
463 freez(config->haddr);
464 freez(config->logfile);
466 freez(config->actions_file);
467 freez(config->admin_address);
468 freez(config->proxy_info_url);
469 freez(config->proxy_args);
471 #ifdef FEATURE_COOKIE_JAR
472 freez(config->jarfile);
473 #endif /* def FEATURE_COOKIE_JAR */
475 freez(config->re_filterfile);
480 #ifdef FEATURE_GRACEFUL_TERMINATION
481 /*********************************************************************
483 * Function : unload_current_config_file
485 * Description : Unloads current config file - reset to state at
486 * beginning of program.
492 *********************************************************************/
493 void unload_current_config_file(void)
495 if (current_configfile)
497 current_configfile->unloader = unload_configfile;
498 current_configfile = NULL;
504 /*********************************************************************
506 * Function : load_config
508 * Description : Load the config file and all parameters.
511 * 1 : csp = Client state (the config member will be
512 * filled in by this function).
514 * Returns : 0 => Ok, everything else is an error.
516 *********************************************************************/
517 struct configuration_spec * load_config(void)
519 char buf[BUFFER_SIZE];
521 FILE *configfp = NULL;
522 struct configuration_spec * config = NULL;
523 struct client_state * fake_csp;
524 struct file_list *fs;
525 unsigned long linenum = 0;
527 if ( !check_file_changed(current_configfile, configfile, &fs))
529 /* No need to load */
530 return ((struct configuration_spec *)current_configfile->f);
534 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
538 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
540 #ifdef FEATURE_TOGGLE
542 #endif /* def FEATURE_TOGGLE */
544 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
550 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
551 /* Never get here - LOG_LEVEL_FATAL causes program exit */
555 * This is backwards from how it's usually done.
556 * Following the usual pattern, "fs" would be stored in a member
557 * variable in "csp", and then we'd access "config" from "fs->f",
558 * using a cast. However, "config" is used so often that a
559 * cast each time would be very ugly, and the extra indirection
560 * would waste CPU cycles. Therefore we store "config" in
561 * "csp->config", and "fs" in "csp->config->config_file_list".
563 config->config_file_list = fs;
569 config->multi_threaded = 1;
570 config->hport = HADDR_PORT;
571 config->buffer_limit = 4096 * 1024;
572 config->proxy_args = strdup("");
574 if ((configfp = fopen(configfile, "r")) == NULL)
576 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
578 /* Never get here - LOG_LEVEL_FATAL causes program exit */
581 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
583 char cmd[BUFFER_SIZE];
584 char arg[BUFFER_SIZE];
585 char tmp[BUFFER_SIZE];
587 struct access_control_list *cur_acl;
588 #endif /* def FEATURE_ACL */
589 struct forward_spec *cur_fwd;
595 /* Copy command (i.e. up to space or tab) into cmd */
598 while (*p && (*p != ' ') && (*p != '\t'))
604 /* Skip over the whitespace in buf */
605 while (*p && ((*p == ' ') || (*p == '\t')))
610 /* Copy the argument into arg */
613 /* Should never happen, but check this anyway */
619 /* Make sure the command field is lower case */
624 *p = ijb_tolower(*p);
628 /* Save the argument for show-proxy-args */
629 savearg(cmd, arg, config);
632 switch( hash_string( cmd ) )
634 /* *************************************************************************
635 * actionsfile actions-file-name
636 * In confdir by default
637 * *************************************************************************/
638 case hash_actions_file :
639 freez(config->actions_file);
640 config->actions_file = make_path(config->confdir, arg);
643 /* *************************************************************************
644 * admin-address email-address
645 * *************************************************************************/
646 case hash_admin_address :
647 freez(config->admin_address);
648 config->admin_address = strdup(arg);
651 /* *************************************************************************
653 * *************************************************************************/
654 case hash_buffer_limit :
655 config->buffer_limit = (size_t) 1024 * atoi(arg);
658 /* *************************************************************************
659 * confdir directory-name
660 * *************************************************************************/
662 freez(config->confdir);
663 config->confdir = make_path( NULL, arg);
666 /* *************************************************************************
668 * Specifies debug level, multiple values are ORed together.
669 * *************************************************************************/
671 config->debug |= atoi(arg);
674 /* *************************************************************************
675 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
676 * *************************************************************************/
678 case hash_deny_access:
679 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
681 if ((vec_count != 1) && (vec_count != 2))
683 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
684 "deny-access directive in configuration file.");
685 string_append(&config->proxy_args,
686 "<br>\nWARNING: Wrong number of parameters for "
687 "deny-access directive in configuration file.<br><br>\n");
691 /* allocate a new node */
692 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
696 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
697 /* Never get here - LOG_LEVEL_FATAL causes program exit */
700 cur_acl->action = ACL_DENY;
702 if (acl_addr(vec[0], cur_acl->src) < 0)
704 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
705 "directive in configuration file: \"%s\"", vec[0]);
706 string_append(&config->proxy_args,
707 "<br>\nWARNING: Invalid source IP for deny-access directive"
708 " in configuration file: \"");
709 string_append(&config->proxy_args,
711 string_append(&config->proxy_args,
718 if (acl_addr(vec[1], cur_acl->dst) < 0)
720 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
721 "directive in configuration file: \"%s\"", vec[0]);
722 string_append(&config->proxy_args,
723 "<br>\nWARNING: Invalid destination IP for deny-access directive"
724 " in configuration file: \"");
725 string_append(&config->proxy_args,
727 string_append(&config->proxy_args,
735 * Add it to the list. Note we reverse the list to get the
736 * behaviour the user expects. With both the ACL and
737 * actions file, the last match wins. However, the internal
738 * implementations are different: The actions file is stored
739 * in the same order as the file, and scanned completely.
740 * With the ACL, we reverse the order as we load it, then
741 * when we scan it we stop as soon as we get a match.
743 cur_acl->next = config->acl;
744 config->acl = cur_acl;
747 #endif /* def FEATURE_ACL */
749 /* *************************************************************************
750 * enable-edit-actions 0|1
751 * *************************************************************************/
752 #ifdef FEATURE_CGI_EDIT_ACTIONS
753 case hash_enable_edit_actions:
754 if ((*arg != '\0') && (0 != atoi(arg)))
756 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
760 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
763 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
765 /* *************************************************************************
766 * enable-remote-toggle 0|1
767 * *************************************************************************/
768 #ifdef FEATURE_CGI_EDIT_ACTIONS
769 case hash_enable_remote_toggle:
770 if ((*arg != '\0') && (0 != atoi(arg)))
772 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
776 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
779 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
781 /* *************************************************************************
782 * forward url-pattern (.|http-proxy-host[:port])
783 * *************************************************************************/
785 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
789 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
790 "directive in configuration file.");
791 string_append(&config->proxy_args,
792 "<br>\nWARNING: Wrong number of parameters for "
793 "forward directive in configuration file.");
797 /* allocate a new node */
798 cur_fwd = zalloc(sizeof(*cur_fwd));
801 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
802 /* Never get here - LOG_LEVEL_FATAL causes program exit */
806 cur_fwd->type = SOCKS_NONE;
808 /* Save the URL pattern */
809 if (create_url_spec(cur_fwd->url, vec[0]))
811 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
812 "directive in configuration file.");
813 string_append(&config->proxy_args,
814 "<br>\nWARNING: Bad URL specifier for "
815 "forward directive in configuration file.");
819 /* Parse the parent HTTP proxy host:port */
822 if (strcmp(p, ".") != 0)
824 cur_fwd->forward_host = strdup(p);
826 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
829 cur_fwd->forward_port = atoi(p);
832 if (cur_fwd->forward_port <= 0)
834 cur_fwd->forward_port = 8000;
839 cur_fwd->next = config->forward;
840 config->forward = cur_fwd;
844 /* *************************************************************************
845 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
846 * *************************************************************************/
847 case hash_forward_socks4:
848 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
852 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
853 "forward-socks4 directive in configuration file.");
854 string_append(&config->proxy_args,
855 "<br>\nWARNING: Wrong number of parameters for "
856 "forward-socks4 directive in configuration file.");
860 /* allocate a new node */
861 cur_fwd = zalloc(sizeof(*cur_fwd));
864 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
865 /* Never get here - LOG_LEVEL_FATAL causes program exit */
869 cur_fwd->type = SOCKS_4;
871 /* Save the URL pattern */
872 if (create_url_spec(cur_fwd->url, vec[0]))
874 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
875 "directive in configuration file.");
876 string_append(&config->proxy_args,
877 "<br>\nWARNING: Bad URL specifier for "
878 "forward-socks4 directive in configuration file.");
882 /* Parse the SOCKS proxy host[:port] */
885 if (strcmp(p, ".") != 0)
887 cur_fwd->gateway_host = strdup(p);
889 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
892 cur_fwd->gateway_port = atoi(p);
894 if (cur_fwd->gateway_port <= 0)
896 cur_fwd->gateway_port = 1080;
900 /* Parse the parent HTTP proxy host[:port] */
903 if (strcmp(p, ".") != 0)
905 cur_fwd->forward_host = strdup(p);
907 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
910 cur_fwd->forward_port = atoi(p);
913 if (cur_fwd->forward_port <= 0)
915 cur_fwd->forward_port = 8000;
920 cur_fwd->next = config->forward;
921 config->forward = cur_fwd;
925 /* *************************************************************************
926 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
927 * *************************************************************************/
928 case hash_forward_socks4a:
929 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
933 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
934 "forward-socks4a directive in configuration file.");
935 string_append(&config->proxy_args,
936 "<br>\nWARNING: Wrong number of parameters for "
937 "forward-socks4a directive in configuration file.");
941 /* allocate a new node */
942 cur_fwd = zalloc(sizeof(*cur_fwd));
945 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
946 /* Never get here - LOG_LEVEL_FATAL causes program exit */
950 cur_fwd->type = SOCKS_4A;
952 /* Save the URL pattern */
953 if (create_url_spec(cur_fwd->url, vec[0]))
955 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
956 "directive in configuration file.");
957 string_append(&config->proxy_args,
958 "<br>\nWARNING: Bad URL specifier for "
959 "forward-socks4a directive in configuration file.");
963 /* Parse the SOCKS proxy host[:port] */
966 cur_fwd->gateway_host = strdup(p);
968 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
971 cur_fwd->gateway_port = atoi(p);
973 if (cur_fwd->gateway_port <= 0)
975 cur_fwd->gateway_port = 1080;
978 /* Parse the parent HTTP proxy host[:port] */
981 if (strcmp(p, ".") != 0)
983 cur_fwd->forward_host = strdup(p);
985 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
988 cur_fwd->forward_port = atoi(p);
991 if (cur_fwd->forward_port <= 0)
993 cur_fwd->forward_port = 8000;
998 cur_fwd->next = config->forward;
999 config->forward = cur_fwd;
1003 /* *************************************************************************
1004 * jarfile jar-file-name
1005 * In logdir by default
1006 * *************************************************************************/
1007 #ifdef FEATURE_COOKIE_JAR
1009 freez(config->jarfile);
1010 config->jarfile = make_path(config->logdir, arg);
1012 #endif /* def FEATURE_COOKIE_JAR */
1014 /* *************************************************************************
1015 * listen-address [ip][:port]
1016 * *************************************************************************/
1017 case hash_listen_address :
1018 freez(config->haddr);
1019 config->haddr = strdup(arg);
1022 /* *************************************************************************
1023 * logdir directory-name
1024 * *************************************************************************/
1026 freez(config->logdir);
1027 config->logdir = make_path(NULL, arg);
1030 /* *************************************************************************
1031 * logfile log-file-name
1032 * In logdir by default
1033 * *************************************************************************/
1035 freez(config->logfile);
1036 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1039 /* *************************************************************************
1040 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1041 * *************************************************************************/
1043 case hash_permit_access:
1044 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1046 if ((vec_count != 1) && (vec_count != 2))
1048 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1049 "permit-access directive in configuration file.");
1050 string_append(&config->proxy_args,
1051 "<br>\nWARNING: Wrong number of parameters for "
1052 "permit-access directive in configuration file.<br><br>\n");
1057 /* allocate a new node */
1058 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1060 if (cur_acl == NULL)
1062 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1063 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1066 cur_acl->action = ACL_PERMIT;
1068 if (acl_addr(vec[0], cur_acl->src) < 0)
1070 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1071 "directive in configuration file: \"%s\"", vec[0]);
1072 string_append(&config->proxy_args,
1073 "<br>\nWARNING: Invalid source IP for permit-access directive"
1074 " in configuration file: \"");
1075 string_append(&config->proxy_args,
1077 string_append(&config->proxy_args,
1084 if (acl_addr(vec[1], cur_acl->dst) < 0)
1086 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1087 "permit-access directive in configuration file: \"%s\"",
1089 string_append(&config->proxy_args,
1090 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1091 " in configuration file: \"");
1092 string_append(&config->proxy_args,
1094 string_append(&config->proxy_args,
1102 * Add it to the list. Note we reverse the list to get the
1103 * behaviour the user expects. With both the ACL and
1104 * actions file, the last match wins. However, the internal
1105 * implementations are different: The actions file is stored
1106 * in the same order as the file, and scanned completely.
1107 * With the ACL, we reverse the order as we load it, then
1108 * when we scan it we stop as soon as we get a match.
1110 cur_acl->next = config->acl;
1111 config->acl = cur_acl;
1114 #endif /* def FEATURE_ACL */
1116 /* *************************************************************************
1117 * proxy-info-url url
1118 * *************************************************************************/
1119 case hash_proxy_info_url :
1120 freez(config->proxy_info_url);
1121 config->proxy_info_url = strdup(arg);
1124 /* *************************************************************************
1125 * re_filterfile file-name
1126 * In confdir by default.
1127 * *************************************************************************/
1128 case hash_filterfile :
1129 freez(config->re_filterfile);
1130 config->re_filterfile = make_path(config->confdir, arg);
1133 /* *************************************************************************
1135 * *************************************************************************/
1136 case hash_single_threaded :
1137 config->multi_threaded = 0;
1140 /* *************************************************************************
1142 * *************************************************************************/
1143 #ifdef FEATURE_TOGGLE
1145 g_bToggleIJB = atoi(arg);
1147 #endif /* def FEATURE_TOGGLE */
1149 /* *************************************************************************
1150 * trust-info-url url
1151 * *************************************************************************/
1152 #ifdef FEATURE_TRUST
1153 case hash_trust_info_url :
1154 enlist(config->trust_info, arg);
1156 #endif /* def FEATURE_TRUST */
1158 /* *************************************************************************
1159 * trustfile filename
1160 * (In confdir by default.)
1161 * *************************************************************************/
1162 #ifdef FEATURE_TRUST
1163 case hash_trustfile :
1164 freez(config->trustfile);
1165 config->trustfile = make_path(config->confdir, arg);
1167 #endif /* def FEATURE_TRUST */
1170 /* *************************************************************************
1171 * Win32 Console options:
1172 * *************************************************************************/
1174 /* *************************************************************************
1176 * *************************************************************************/
1178 case hash_hide_console :
1181 #endif /*def _WIN_CONSOLE*/
1184 /* *************************************************************************
1185 * Win32 GUI options:
1186 * *************************************************************************/
1188 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1189 /* *************************************************************************
1190 * activity-animation (0|1)
1191 * *************************************************************************/
1192 case hash_activity_animation :
1193 g_bShowActivityAnimation = atoi(arg);
1196 /* *************************************************************************
1197 * close-button-minimizes (0|1)
1198 * *************************************************************************/
1199 case hash_close_button_minimizes :
1200 g_bCloseHidesWindow = atoi(arg);
1203 /* *************************************************************************
1204 * log-buffer-size (0|1)
1205 * *************************************************************************/
1206 case hash_log_buffer_size :
1207 g_bLimitBufferSize = atoi(arg);
1210 /* *************************************************************************
1211 * log-font-name fontnane
1212 * *************************************************************************/
1213 case hash_log_font_name :
1214 strcpy( g_szFontFaceName, arg );
1217 /* *************************************************************************
1219 * *************************************************************************/
1220 case hash_log_font_size :
1221 g_nFontSize = atoi(arg);
1224 /* *************************************************************************
1225 * log-highlight-messages (0|1)
1226 * *************************************************************************/
1227 case hash_log_highlight_messages :
1228 g_bHighlightMessages = atoi(arg);
1231 /* *************************************************************************
1233 * *************************************************************************/
1234 case hash_log_max_lines :
1235 g_nMaxBufferLines = atoi(arg);
1238 /* *************************************************************************
1239 * log-messages (0|1)
1240 * *************************************************************************/
1241 case hash_log_messages :
1242 g_bLogMessages = atoi(arg);
1245 /* *************************************************************************
1246 * show-on-task-bar (0|1)
1247 * *************************************************************************/
1248 case hash_show_on_task_bar :
1249 g_bShowOnTaskBar = atoi(arg);
1252 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1255 /* *************************************************************************
1256 * Warnings about unsupported features
1257 * *************************************************************************/
1259 case hash_deny_access:
1260 #endif /* ndef FEATURE_ACL */
1261 #ifndef FEATURE_CGI_EDIT_ACTIONS
1262 case hash_enable_edit_actions:
1263 case hash_enable_remote_toggle:
1264 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1265 #ifndef FEATURE_COOKIE_JAR
1267 #endif /* ndef FEATURE_COOKIE_JAR */
1269 case hash_permit_access:
1270 #endif /* ndef FEATURE_ACL */
1271 #ifndef FEATURE_TOGGLE
1273 #endif /* ndef FEATURE_TOGGLE */
1274 #ifndef FEATURE_TRUST
1275 case hash_trustfile :
1276 case hash_trust_info_url :
1277 #endif /* ndef FEATURE_TRUST */
1279 #ifndef _WIN_CONSOLE
1280 case hash_hide_console :
1281 #endif /* ndef _WIN_CONSOLE */
1283 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1284 case hash_activity_animation :
1285 case hash_close_button_minimizes :
1286 case hash_log_buffer_size :
1287 case hash_log_font_name :
1288 case hash_log_font_size :
1289 case hash_log_highlight_messages :
1290 case hash_log_max_lines :
1291 case hash_log_messages :
1292 case hash_show_on_task_bar :
1293 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1294 /* These warnings are annoying - so hide them. -- Jon */
1295 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1298 /* *************************************************************************/
1300 /* *************************************************************************/
1302 * I decided that I liked this better as a warning than an
1303 * error. To change back to an error, just change log level
1304 * to LOG_LEVEL_FATAL.
1306 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1307 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1308 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1309 string_append(&config->proxy_args, buf);
1310 string_append(&config->proxy_args, "<br><br>\n");
1313 /* *************************************************************************/
1314 } /* end switch( hash_string(cmd) ) */
1315 } /* end while ( read_config_line(...) ) */
1319 if (NULL == config->proxy_args)
1321 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1324 init_error_log(Argv[0], config->logfile, config->debug);
1326 if (config->actions_file)
1328 add_loader(load_actions_file, config);
1331 if (config->re_filterfile)
1333 add_loader(load_re_filterfile, config);
1336 #ifdef FEATURE_TRUST
1337 if (config->trustfile)
1339 add_loader(load_trustfile, config);
1341 #endif /* def FEATURE_TRUST */
1343 #ifdef FEATURE_COOKIE_JAR
1344 if ( NULL != config->jarfile )
1346 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1348 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1349 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1351 setbuf(config->jar, NULL);
1353 #endif /* def FEATURE_COOKIE_JAR */
1355 if ( NULL == config->haddr )
1357 config->haddr = strdup( HADDR_DEFAULT );
1360 if ( NULL != config->haddr )
1362 if (NULL != (p = strchr(config->haddr, ':')))
1367 config->hport = atoi(p);
1371 if (config->hport <= 0)
1374 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1375 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1377 if (*config->haddr == '\0')
1379 config->haddr = NULL;
1384 * Want to run all the loaders once now.
1386 * Need to set up a fake csp, so they can get to the config.
1388 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1389 fake_csp->config = config;
1391 if (run_loader(fake_csp))
1394 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1395 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1399 /* FIXME: this is a kludge for win32 */
1400 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1402 g_actions_file = config->actions_file;
1403 g_re_filterfile = config->re_filterfile;
1405 #ifdef FEATURE_TRUST
1406 g_trustfile = config->trustfile;
1407 #endif /* def FEATURE_TRUST */
1410 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1411 /* FIXME: end kludge */
1414 config->need_bind = 1;
1416 if (current_configfile)
1418 struct configuration_spec * oldcfg = (struct configuration_spec *)
1419 current_configfile->f;
1421 * Check if config->haddr,hport == oldcfg->haddr,hport
1423 * The following could be written more compactly as a single,
1424 * (unreadably long) if statement.
1426 config->need_bind = 0;
1427 if (config->hport != oldcfg->hport)
1429 config->need_bind = 1;
1431 else if (config->haddr == NULL)
1433 if (oldcfg->haddr != NULL)
1435 config->need_bind = 1;
1438 else if (oldcfg->haddr == NULL)
1440 config->need_bind = 1;
1442 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1444 config->need_bind = 1;
1447 current_configfile->unloader = unload_configfile;
1450 fs->next = files->next;
1453 current_configfile = fs;
1459 /*********************************************************************
1461 * Function : savearg
1463 * Description : Called from `load_config'. It saves each non-empty
1464 * and non-comment line from config into
1465 * config->proxy_args. This is used to create the
1466 * show-proxy-args page. On error, frees
1467 * config->proxy_args and sets it to NULL
1470 * 1 : command = config setting that was found
1471 * 2 : argument = the setting's argument (if any)
1475 *********************************************************************/
1476 static void savearg(char *command, char *argument, struct configuration_spec * config)
1487 s = html_encode(command);
1491 freez(config->proxy_args);
1494 string_append(&buf, "<a href=\"" REDIRECT_URL "option#");
1495 string_append(&buf, s);
1496 string_append(&buf, "\">");
1497 string_join (&buf, s);
1498 string_append(&buf, "</a> ");
1500 if ( (NULL != argument) && ('\0' != *argument) )
1502 s = html_encode(argument);
1506 freez(config->proxy_args);
1510 if (strncmpic(argument, "http://", 7) == 0)
1512 string_append(&buf, "<a href=\"");
1513 string_append(&buf, s);
1514 string_append(&buf, "\">");
1515 string_join (&buf, s);
1516 string_append(&buf, "</a>");
1520 string_join (&buf, s);
1524 string_append(&buf, "<br>\n");
1526 string_join(&config->proxy_args, buf);