1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.33 2002/03/05 04:52:42 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 * IJBSWA team. http://ijbswa.sourceforge.net
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.33 2002/03/05 04:52:42 oes
39 * Deleted non-errlog debugging code
41 * Revision 1.32 2002/03/04 18:24:53 oes
42 * Re-enabled output of unknown config directive hash
44 * Revision 1.31 2002/03/03 15:07:20 oes
45 * Re-enabled automatic config reloading
47 * Revision 1.30 2002/01/22 23:31:43 jongfoster
48 * Replacing strsav() with string_append()
50 * Revision 1.29 2002/01/17 21:02:30 jongfoster
51 * Moving all our URL and URL pattern parsing code to urlmatch.c.
53 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
55 * Revision 1.28 2001/12/30 14:07:32 steudten
56 * - Add signal handling (unix)
57 * - Add SIGHUP handler (unix)
58 * - Add creation of pidfile (unix)
59 * - Add action 'top' in rc file (RH)
60 * - Add entry 'SIGNALS' to manpage
61 * - Add exit message to logfile (unix)
63 * Revision 1.27 2001/11/07 00:02:13 steudten
64 * Add line number in error output for lineparsing for
65 * actionsfile and configfile.
66 * Special handling for CLF added.
68 * Revision 1.26 2001/11/05 21:41:43 steudten
69 * Add changes to be a real daemon just for unix os.
70 * (change cwd to /, detach from controlling tty, set
71 * process group and session leader to the own process.
73 * Add some fatal-error log message for failed malloc().
74 * Add '-d' if compiled with 'configure --with-debug' to
75 * enable debug output.
77 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
78 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
79 * threads to call select() simultaneously. So, it's time to do a real, live,
80 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
81 * (native). Both versions will work, but using __OS2__ offers multi-threading.
83 * Revision 1.24 2001/10/23 21:40:30 jongfoster
84 * Added support for enable-edit-actions and enable-remote-toggle config
87 * Revision 1.23 2001/10/07 15:36:00 oes
88 * Introduced new config option "buffer-limit"
90 * Revision 1.22 2001/09/22 16:36:59 jongfoster
91 * Removing unused parameter fs from read_config_line()
93 * Revision 1.21 2001/09/16 17:10:43 jongfoster
94 * Moving function savearg() here, since it was the only thing left in
97 * Revision 1.20 2001/07/30 22:08:36 jongfoster
98 * Tidying up #defines:
99 * - All feature #defines are now of the form FEATURE_xxx
100 * - Permanently turned off WIN_GUI_EDIT
101 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
103 * Revision 1.19 2001/07/15 17:45:16 jongfoster
104 * Removing some unused #includes
106 * Revision 1.18 2001/07/13 14:01:14 oes
107 * - Removed all #ifdef PCRS
108 * - Removed vim-settings
110 * Revision 1.17 2001/06/29 13:31:03 oes
111 * - Improved comments
112 * - Fixed (actionsfile) and sorted hashes
113 * - Introduced admin_address and proxy-info-url
114 * as config parameters
115 * - Renamed config->proxy_args_invocation (which didn't have
116 * the invocation but the options!) to config->proxy_args
117 * - Various adaptions
118 * - Removed logentry from cancelled commit
120 * Revision 1.16 2001/06/09 10:55:28 jongfoster
121 * Changing BUFSIZ ==> BUFFER_SIZE
123 * Revision 1.15 2001/06/07 23:13:40 jongfoster
124 * Merging ACL and forward files into config file.
125 * Cosmetic: Sorting config file options alphabetically.
126 * Cosmetic: Adding brief syntax comments to config file options.
128 * Revision 1.14 2001/06/07 14:46:25 joergs
129 * Missing make_path() added for re_filterfile.
131 * Revision 1.13 2001/06/05 22:33:54 jongfoster
133 * Fixed minor memory leak.
134 * Also now uses make_path to prepend the pathnames.
136 * Revision 1.12 2001/06/05 20:04:09 jongfoster
137 * Now uses _snprintf() in place of snprintf() under Win32.
139 * Revision 1.11 2001/06/04 18:31:58 swa
140 * files are now prefixed with either `confdir' or `logdir'.
141 * `make redhat-dist' replaces both entries confdir and logdir
144 * Revision 1.10 2001/06/03 19:11:54 oes
145 * introduced confdir option
147 * Revision 1.9 2001/06/01 20:06:24 jongfoster
148 * Removed support for "tinygif" option - moved to actions file.
150 * Revision 1.8 2001/05/31 21:27:13 jongfoster
151 * Removed many options from the config file and into the
152 * "actions" file: add_forwarded, suppress_vanilla_wafer,
153 * wafer, add_header, user_agent, referer, from
154 * Also globally replaced "permission" with "action".
156 * Revision 1.7 2001/05/29 09:50:24 jongfoster
157 * Unified blocklist/imagelist/permissionslist.
158 * File format is still under discussion, but the internal changes
161 * Also modified interceptor behaviour:
162 * - We now intercept all URLs beginning with one of the following
163 * prefixes (and *only* these prefixes):
165 * * http://ijbswa.sf.net/config/
166 * * http://ijbswa.sourceforge.net/config/
167 * - New interceptors "home page" - go to http://i.j.b/ to see it.
168 * - Internal changes so that intercepted and fast redirect pages
169 * are not replaced with an image.
170 * - Interceptors now have the option to send a binary page direct
171 * to the client. (i.e. ijb-send-banner uses this)
172 * - Implemented show-url-info interceptor. (Which is why I needed
173 * the above interceptors changes - a typical URL is
174 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
175 * The previous mechanism would not have intercepted that, and
176 * if it had been intercepted then it then it would have replaced
179 * Revision 1.6 2001/05/26 00:28:36 jongfoster
180 * Automatic reloading of config file.
181 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
182 * Most of the global variables have been moved to a new
183 * struct configuration_spec, accessed through csp->config->globalname
184 * Most of the globals remaining are used by the Win32 GUI.
186 * Revision 1.5 2001/05/25 22:34:30 jongfoster
189 * Revision 1.4 2001/05/22 18:46:04 oes
191 * - Enabled filtering banners by size rather than URL
192 * by adding patterns that replace all standard banner
193 * sizes with the "Junkbuster" gif to the re_filterfile
195 * - Enabled filtering WebBugs by providing a pattern
196 * which kills all 1x1 images
198 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
199 * which is selected by the (nonstandard and therefore
200 * capital) letter 'U' in the option string.
201 * It causes the quantifiers to be ungreedy by default.
202 * Appending a ? turns back to greedy (!).
204 * - Added a new interceptor ijb-send-banner, which
205 * sends back the "Junkbuster" gif. Without imagelist or
206 * MSIE detection support, or if tinygif = 1, or the
207 * URL isn't recognized as an imageurl, a lame HTML
208 * explanation is sent instead.
210 * - Added new feature, which permits blocking remote
211 * script redirects and firing back a local redirect
213 * The feature is conditionally compiled, i.e. it
214 * can be disabled with --disable-fast-redirects,
215 * plus it must be activated by a "fast-redirects"
216 * line in the config file, has its own log level
217 * and of course wants to be displayed by show-proxy-args
218 * Note: Boy, all the #ifdefs in 1001 locations and
219 * all the fumbling with configure.in and acconfig.h
220 * were *way* more work than the feature itself :-(
222 * - Because a generic redirect template was needed for
223 * this, tinygif = 3 now uses the same.
225 * - Moved GIFs, and other static HTTP response templates
230 * - Removed some >400 CRs again (Jon, you really worked
233 * Revision 1.3 2001/05/20 01:21:20 jongfoster
234 * Version 2.9.4 checkin.
235 * - Merged popupfile and cookiefile, and added control over PCRS
236 * filtering, in new "permissionsfile".
237 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
238 * file error you now get a message box (in the Win32 GUI) rather
239 * than the program exiting with no explanation.
240 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
242 * - Removed tabs from "config"
243 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
244 * - Bumped up version number.
246 * Revision 1.2 2001/05/17 23:01:01 oes
247 * - Cleaned CRLF's from the sources and related files
249 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
250 * Initial import of version 2.9.3 source tree
253 *********************************************************************/
259 #include <sys/types.h>
270 # include <windows.h>
273 # ifndef _WIN_CONSOLE
275 # endif /* ndef _WIN_CONSOLE */
277 /* VC++ has "_snprintf", not "snprintf" */
278 #define snprintf _snprintf
280 #else /* ifndef _WIN32 */
284 # include <sys/wait.h>
286 # include <sys/time.h>
287 # include <sys/stat.h>
297 #include "miscutil.h"
301 #include "urlmatch.h"
303 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
306 * Fix a problem with Solaris. There should be no effect on other
308 * Solaris's isspace() is a macro which uses it's argument directly
309 * as an array index. Therefore we need to make sure that high-bit
310 * characters generate +ve values, and ideally we also want to make
311 * the argument match the declared parameter type of "int".
313 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
314 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
316 #ifdef FEATURE_TOGGLE
317 /* by haroon - indicates if ijb is enabled */
318 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
319 #endif /* def FEATURE_TOGGLE */
321 /* The filename of the configfile */
322 const char *configfile = NULL;
325 * CGI functions will later need access to the invocation args,
326 * so we will make argc and argv global.
329 const char **Argv = NULL;
331 static struct file_list *current_configfile = NULL;
335 * This takes the "cryptic" hash of each keyword and aliases them to
336 * something a little more readable. This also makes changing the
337 * hash values easier if they should change or the hash algorthm changes.
338 * Use the included "hash" program to find out what the hash will be
339 * for any string supplied on the command line. (Or just put it in the
340 * config file and read the number from the error message in the log).
342 * Please keep this list sorted alphabetically (but with the Windows
343 * console and GUI specific options last).
346 #define hash_actions_file 1196306641ul /* "actionsfile" */
347 #define hash_admin_address 4112573064ul /* "admin-address" */
348 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
349 #define hash_confdir 1978389ul /* "confdir" */
350 #define hash_debug 78263ul /* "debug" */
351 #define hash_deny_access 1227333715ul /* "deny-access" */
352 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
353 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
354 #define hash_forward 2029845ul /* "forward" */
355 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
356 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
357 #define hash_jarfile 2046641ul /* "jarfile" */
358 #define hash_listen_address 1255650842ul /* "listen-address" */
359 #define hash_logdir 422889ul /* "logdir" */
360 #define hash_logfile 2114766ul /* "logfile" */
361 #define hash_permit_access 3587953268ul /* "permit-access" */
362 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
363 #define hash_re_filterfile 3877522444ul /* "re_filterfile" */
364 #define hash_single_threaded 4250084780ul /* "single-threaded" */
365 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
366 #define hash_toggle 447966ul /* "toggle" */
367 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
368 #define hash_trustfile 56494766ul /* "trustfile" */
370 #define hash_activity_animation 1817904738ul /* "activity-animation" */
371 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
372 #define hash_hide_console 2048809870ul /* "hide-console" */
373 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
374 #define hash_log_font_name 2866730124ul /* "log-font-name" */
375 #define hash_log_font_size 2866731014ul /* "log-font-size" */
376 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
377 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
378 #define hash_log_messages 2291744899ul /* "log-messages" */
379 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
382 static void savearg(char *command, char *argument, struct configuration_spec * config);
384 /*********************************************************************
386 * Function : unload_configfile
388 * Description : Free the config structure and all components.
391 * 1 : data: struct configuration_spec to unload
395 *********************************************************************/
396 void unload_configfile (void * data)
398 struct configuration_spec * config = (struct configuration_spec *)data;
399 struct forward_spec *cur_fwd = config->forward;
401 struct access_control_list *cur_acl = config->acl;
403 while (cur_acl != NULL)
405 struct access_control_list * next_acl = cur_acl->next;
410 #endif /* def FEATURE_ACL */
412 while (cur_fwd != NULL)
414 struct forward_spec * next_fwd = cur_fwd->next;
415 free_url_spec(cur_fwd->url);
417 freez(cur_fwd->gateway_host);
418 freez(cur_fwd->forward_host);
422 config->forward = NULL;
424 #ifdef FEATURE_COOKIE_JAR
425 if ( NULL != config->jar )
427 fclose( config->jar );
430 #endif /* def FEATURE_COOKIE_JAR */
432 freez(config->confdir);
433 freez(config->logdir);
435 freez(config->haddr);
436 freez(config->logfile);
438 freez(config->actions_file);
439 freez(config->admin_address);
440 freez(config->proxy_info_url);
441 freez(config->proxy_args);
443 #ifdef FEATURE_COOKIE_JAR
444 freez(config->jarfile);
445 #endif /* def FEATURE_COOKIE_JAR */
447 freez(config->re_filterfile);
452 /*********************************************************************
454 * Function : load_config
456 * Description : Load the config file and all parameters.
459 * 1 : csp = Client state (the config member will be
460 * filled in by this function).
462 * Returns : 0 => Ok, everything else is an error.
464 *********************************************************************/
465 struct configuration_spec * load_config(void)
467 char buf[BUFFER_SIZE];
469 FILE *configfp = NULL;
470 struct configuration_spec * config = NULL;
471 struct client_state * fake_csp;
472 struct file_list *fs;
473 unsigned long linenum = 0;
475 if ( !check_file_changed(current_configfile, configfile, &fs))
477 /* No need to load */
478 return ((struct configuration_spec *)current_configfile->f);
482 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
486 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
488 #ifdef FEATURE_TOGGLE
490 #endif /* def FEATURE_TOGGLE */
492 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
498 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
499 /* Never get here - LOG_LEVEL_FATAL causes program exit */
503 * This is backwards from how it's usually done.
504 * Following the usual pattern, "fs" would be stored in a member
505 * variable in "csp", and then we'd access "config" from "fs->f",
506 * using a cast. However, "config" is used so often that a
507 * cast each time would be very ugly, and the extra indirection
508 * would waste CPU cycles. Therefore we store "config" in
509 * "csp->config", and "fs" in "csp->config->config_file_list".
511 config->config_file_list = fs;
517 config->multi_threaded = 1;
518 config->hport = HADDR_PORT;
519 config->buffer_limit = 4096 * 1024;
520 config->proxy_args = strdup("");
522 if ((configfp = fopen(configfile, "r")) == NULL)
524 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
526 /* Never get here - LOG_LEVEL_FATAL causes program exit */
529 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
531 char cmd[BUFFER_SIZE];
532 char arg[BUFFER_SIZE];
533 char tmp[BUFFER_SIZE];
535 struct access_control_list *cur_acl;
536 #endif /* def FEATURE_ACL */
537 struct forward_spec *cur_fwd;
543 /* Copy command (i.e. up to space or tab) into cmd */
546 while (*p && (*p != ' ') && (*p != '\t'))
552 /* Skip over the whitespace in buf */
553 while (*p && ((*p == ' ') || (*p == '\t')))
558 /* Copy the argument into arg */
561 /* Should never happen, but check this anyway */
567 /* Make sure the command field is lower case */
572 *p = ijb_tolower(*p);
576 /* Save the argument for show-proxy-args */
577 savearg(cmd, arg, config);
580 switch( hash_string( cmd ) )
582 /* *************************************************************************
583 * actionsfile actions-file-name
584 * In confdir by default
585 * *************************************************************************/
586 case hash_actions_file :
587 freez(config->actions_file);
588 config->actions_file = make_path(config->confdir, arg);
591 /* *************************************************************************
592 * admin-address email-address
593 * *************************************************************************/
594 case hash_admin_address :
595 freez(config->admin_address);
596 config->admin_address = strdup(arg);
599 /* *************************************************************************
601 * *************************************************************************/
602 case hash_buffer_limit :
603 config->buffer_limit = (size_t) 1024 * atoi(arg);
606 /* *************************************************************************
607 * confdir directory-name
608 * *************************************************************************/
610 freez(config->confdir);
611 config->confdir = make_path( NULL, arg);
614 /* *************************************************************************
616 * Specifies debug level, multiple values are ORed together.
617 * *************************************************************************/
619 config->debug |= atoi(arg);
622 /* *************************************************************************
623 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
624 * *************************************************************************/
626 case hash_deny_access:
627 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
629 if ((vec_count != 1) && (vec_count != 2))
631 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
632 "deny-access directive in configuration file.");
633 string_append(&config->proxy_args,
634 "<br>\nWARNING: Wrong number of parameters for "
635 "deny-access directive in configuration file.<br><br>\n");
639 /* allocate a new node */
640 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
644 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
645 /* Never get here - LOG_LEVEL_FATAL causes program exit */
648 cur_acl->action = ACL_DENY;
650 if (acl_addr(vec[0], cur_acl->src) < 0)
652 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
653 "directive in configuration file: \"%s\"", vec[0]);
654 string_append(&config->proxy_args,
655 "<br>\nWARNING: Invalid source IP for deny-access directive"
656 " in configuration file: \"");
657 string_append(&config->proxy_args,
659 string_append(&config->proxy_args,
666 if (acl_addr(vec[1], cur_acl->dst) < 0)
668 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
669 "directive in configuration file: \"%s\"", vec[0]);
670 string_append(&config->proxy_args,
671 "<br>\nWARNING: Invalid destination IP for deny-access directive"
672 " in configuration file: \"");
673 string_append(&config->proxy_args,
675 string_append(&config->proxy_args,
683 * Add it to the list. Note we reverse the list to get the
684 * behaviour the user expects. With both the ACL and
685 * actions file, the last match wins. However, the internal
686 * implementations are different: The actions file is stored
687 * in the same order as the file, and scanned completely.
688 * With the ACL, we reverse the order as we load it, then
689 * when we scan it we stop as soon as we get a match.
691 cur_acl->next = config->acl;
692 config->acl = cur_acl;
695 #endif /* def FEATURE_ACL */
697 /* *************************************************************************
698 * enable-edit-actions 0|1
699 * *************************************************************************/
700 #ifdef FEATURE_CGI_EDIT_ACTIONS
701 case hash_enable_edit_actions:
702 if ((*arg != '\0') && (0 != atoi(arg)))
704 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
708 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
711 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
713 /* *************************************************************************
714 * enable-remote-toggle 0|1
715 * *************************************************************************/
716 #ifdef FEATURE_CGI_EDIT_ACTIONS
717 case hash_enable_remote_toggle:
718 if ((*arg != '\0') && (0 != atoi(arg)))
720 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
724 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
727 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
729 /* *************************************************************************
730 * forward url-pattern (.|http-proxy-host[:port])
731 * *************************************************************************/
733 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
737 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
738 "directive in configuration file.");
739 string_append(&config->proxy_args,
740 "<br>\nWARNING: Wrong number of parameters for "
741 "forward directive in configuration file.");
745 /* allocate a new node */
746 cur_fwd = zalloc(sizeof(*cur_fwd));
749 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
750 /* Never get here - LOG_LEVEL_FATAL causes program exit */
754 cur_fwd->type = SOCKS_NONE;
756 /* Save the URL pattern */
757 if (create_url_spec(cur_fwd->url, vec[0]))
759 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
760 "directive in configuration file.");
761 string_append(&config->proxy_args,
762 "<br>\nWARNING: Bad URL specifier for "
763 "forward directive in configuration file.");
767 /* Parse the parent HTTP proxy host:port */
770 if (strcmp(p, ".") != 0)
772 cur_fwd->forward_host = strdup(p);
774 if ((p = strchr(cur_fwd->forward_host, ':')))
777 cur_fwd->forward_port = atoi(p);
780 if (cur_fwd->forward_port <= 0)
782 cur_fwd->forward_port = 8000;
787 cur_fwd->next = config->forward;
788 config->forward = cur_fwd;
792 /* *************************************************************************
793 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
794 * *************************************************************************/
795 case hash_forward_socks4:
796 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
800 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
801 "forward-socks4 directive in configuration file.");
802 string_append(&config->proxy_args,
803 "<br>\nWARNING: Wrong number of parameters for "
804 "forward-socks4 directive in configuration file.");
808 /* allocate a new node */
809 cur_fwd = zalloc(sizeof(*cur_fwd));
812 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
813 /* Never get here - LOG_LEVEL_FATAL causes program exit */
817 cur_fwd->type = SOCKS_4;
819 /* Save the URL pattern */
820 if (create_url_spec(cur_fwd->url, vec[0]))
822 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
823 "directive in configuration file.");
824 string_append(&config->proxy_args,
825 "<br>\nWARNING: Bad URL specifier for "
826 "forward-socks4 directive in configuration file.");
830 /* Parse the SOCKS proxy host[:port] */
833 if (strcmp(p, ".") != 0)
835 cur_fwd->gateway_host = strdup(p);
837 if ((p = strchr(cur_fwd->gateway_host, ':')))
840 cur_fwd->gateway_port = atoi(p);
842 if (cur_fwd->gateway_port <= 0)
844 cur_fwd->gateway_port = 1080;
848 /* Parse the parent HTTP proxy host[:port] */
851 if (strcmp(p, ".") != 0)
853 cur_fwd->forward_host = strdup(p);
855 if ((p = strchr(cur_fwd->forward_host, ':')))
858 cur_fwd->forward_port = atoi(p);
861 if (cur_fwd->forward_port <= 0)
863 cur_fwd->forward_port = 8000;
868 cur_fwd->next = config->forward;
869 config->forward = cur_fwd;
873 /* *************************************************************************
874 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
875 * *************************************************************************/
876 case hash_forward_socks4a:
877 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
881 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
882 "forward-socks4a directive in configuration file.");
883 string_append(&config->proxy_args,
884 "<br>\nWARNING: Wrong number of parameters for "
885 "forward-socks4a directive in configuration file.");
889 /* allocate a new node */
890 cur_fwd = zalloc(sizeof(*cur_fwd));
893 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
894 /* Never get here - LOG_LEVEL_FATAL causes program exit */
898 cur_fwd->type = SOCKS_4A;
900 /* Save the URL pattern */
901 if (create_url_spec(cur_fwd->url, vec[0]))
903 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
904 "directive in configuration file.");
905 string_append(&config->proxy_args,
906 "<br>\nWARNING: Bad URL specifier for "
907 "forward-socks4a directive in configuration file.");
911 /* Parse the SOCKS proxy host[:port] */
914 cur_fwd->gateway_host = strdup(p);
916 if ((p = strchr(cur_fwd->gateway_host, ':')))
919 cur_fwd->gateway_port = atoi(p);
921 if (cur_fwd->gateway_port <= 0)
923 cur_fwd->gateway_port = 1080;
926 /* Parse the parent HTTP proxy host[:port] */
929 if (strcmp(p, ".") != 0)
931 cur_fwd->forward_host = strdup(p);
933 if ((p = strchr(cur_fwd->forward_host, ':')))
936 cur_fwd->forward_port = atoi(p);
939 if (cur_fwd->forward_port <= 0)
941 cur_fwd->forward_port = 8000;
946 cur_fwd->next = config->forward;
947 config->forward = cur_fwd;
951 /* *************************************************************************
952 * jarfile jar-file-name
953 * In logdir by default
954 * *************************************************************************/
955 #ifdef FEATURE_COOKIE_JAR
957 freez(config->jarfile);
958 config->jarfile = make_path(config->logdir, arg);
960 #endif /* def FEATURE_COOKIE_JAR */
962 /* *************************************************************************
963 * listen-address [ip][:port]
964 * *************************************************************************/
965 case hash_listen_address :
966 freez(config->haddr);
967 config->haddr = strdup(arg);
970 /* *************************************************************************
971 * logdir directory-name
972 * *************************************************************************/
974 freez(config->logdir);
975 config->logdir = make_path(NULL, arg);
978 /* *************************************************************************
979 * logfile log-file-name
980 * In logdir by default
981 * *************************************************************************/
983 freez(config->logfile);
984 config->logfile = make_path(config->logdir, arg);
987 /* *************************************************************************
988 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
989 * *************************************************************************/
991 case hash_permit_access:
992 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
994 if ((vec_count != 1) && (vec_count != 2))
996 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
997 "permit-access directive in configuration file.");
998 string_append(&config->proxy_args,
999 "<br>\nWARNING: Wrong number of parameters for "
1000 "permit-access directive in configuration file.<br><br>\n");
1005 /* allocate a new node */
1006 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1008 if (cur_acl == NULL)
1010 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1011 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1014 cur_acl->action = ACL_PERMIT;
1016 if (acl_addr(vec[0], cur_acl->src) < 0)
1018 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1019 "directive in configuration file: \"%s\"", vec[0]);
1020 string_append(&config->proxy_args,
1021 "<br>\nWARNING: Invalid source IP for permit-access directive"
1022 " in configuration file: \"");
1023 string_append(&config->proxy_args,
1025 string_append(&config->proxy_args,
1032 if (acl_addr(vec[1], cur_acl->dst) < 0)
1034 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1035 "permit-access directive in configuration file: \"%s\"",
1037 string_append(&config->proxy_args,
1038 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1039 " in configuration file: \"");
1040 string_append(&config->proxy_args,
1042 string_append(&config->proxy_args,
1050 * Add it to the list. Note we reverse the list to get the
1051 * behaviour the user expects. With both the ACL and
1052 * actions file, the last match wins. However, the internal
1053 * implementations are different: The actions file is stored
1054 * in the same order as the file, and scanned completely.
1055 * With the ACL, we reverse the order as we load it, then
1056 * when we scan it we stop as soon as we get a match.
1058 cur_acl->next = config->acl;
1059 config->acl = cur_acl;
1062 #endif /* def FEATURE_ACL */
1064 /* *************************************************************************
1065 * proxy-info-url url
1066 * *************************************************************************/
1067 case hash_proxy_info_url :
1068 freez(config->proxy_info_url);
1069 config->proxy_info_url = strdup(arg);
1072 /* *************************************************************************
1073 * re_filterfile file-name
1074 * In confdir by default.
1075 * *************************************************************************/
1076 case hash_re_filterfile :
1077 freez(config->re_filterfile);
1078 config->re_filterfile = make_path(config->confdir, arg);
1081 /* *************************************************************************
1083 * *************************************************************************/
1084 case hash_single_threaded :
1085 config->multi_threaded = 0;
1088 /* *************************************************************************
1090 * *************************************************************************/
1091 #ifdef FEATURE_TOGGLE
1093 g_bToggleIJB = atoi(arg);
1095 #endif /* def FEATURE_TOGGLE */
1097 /* *************************************************************************
1098 * trust-info-url url
1099 * *************************************************************************/
1100 #ifdef FEATURE_TRUST
1101 case hash_trust_info_url :
1102 enlist(config->trust_info, arg);
1104 #endif /* def FEATURE_TRUST */
1106 /* *************************************************************************
1107 * trustfile filename
1108 * (In confdir by default.)
1109 * *************************************************************************/
1110 #ifdef FEATURE_TRUST
1111 case hash_trustfile :
1112 freez(config->trustfile);
1113 config->trustfile = make_path(config->confdir, arg);
1115 #endif /* def FEATURE_TRUST */
1118 /* *************************************************************************
1119 * Win32 Console options:
1120 * *************************************************************************/
1122 /* *************************************************************************
1124 * *************************************************************************/
1126 case hash_hide_console :
1129 #endif /*def _WIN_CONSOLE*/
1132 /* *************************************************************************
1133 * Win32 GUI options:
1134 * *************************************************************************/
1136 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1137 /* *************************************************************************
1138 * activity-animation (0|1)
1139 * *************************************************************************/
1140 case hash_activity_animation :
1141 g_bShowActivityAnimation = atoi(arg);
1144 /* *************************************************************************
1145 * close-button-minimizes (0|1)
1146 * *************************************************************************/
1147 case hash_close_button_minimizes :
1148 g_bCloseHidesWindow = atoi(arg);
1151 /* *************************************************************************
1152 * log-buffer-size (0|1)
1153 * *************************************************************************/
1154 case hash_log_buffer_size :
1155 g_bLimitBufferSize = atoi(arg);
1158 /* *************************************************************************
1159 * log-font-name fontnane
1160 * *************************************************************************/
1161 case hash_log_font_name :
1162 strcpy( g_szFontFaceName, arg );
1165 /* *************************************************************************
1167 * *************************************************************************/
1168 case hash_log_font_size :
1169 g_nFontSize = atoi(arg);
1172 /* *************************************************************************
1173 * log-highlight-messages (0|1)
1174 * *************************************************************************/
1175 case hash_log_highlight_messages :
1176 g_bHighlightMessages = atoi(arg);
1179 /* *************************************************************************
1181 * *************************************************************************/
1182 case hash_log_max_lines :
1183 g_nMaxBufferLines = atoi(arg);
1186 /* *************************************************************************
1187 * log-messages (0|1)
1188 * *************************************************************************/
1189 case hash_log_messages :
1190 g_bLogMessages = atoi(arg);
1193 /* *************************************************************************
1194 * show-on-task-bar (0|1)
1195 * *************************************************************************/
1196 case hash_show_on_task_bar :
1197 g_bShowOnTaskBar = atoi(arg);
1200 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1203 /* *************************************************************************
1204 * Warnings about unsupported features
1205 * *************************************************************************/
1207 case hash_deny_access:
1208 #endif /* ndef FEATURE_ACL */
1209 #ifndef FEATURE_CGI_EDIT_ACTIONS
1210 case hash_enable_edit_actions:
1211 case hash_enable_remote_toggle:
1212 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1213 #ifndef FEATURE_COOKIE_JAR
1215 #endif /* ndef FEATURE_COOKIE_JAR */
1217 case hash_permit_access:
1218 #endif /* ndef FEATURE_ACL */
1219 #ifndef FEATURE_TOGGLE
1221 #endif /* ndef FEATURE_TOGGLE */
1222 #ifndef FEATURE_TRUST
1223 case hash_trustfile :
1224 case hash_trust_info_url :
1225 #endif /* ndef FEATURE_TRUST */
1227 #ifndef _WIN_CONSOLE
1228 case hash_hide_console :
1229 #endif /* ndef _WIN_CONSOLE */
1231 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1232 case hash_activity_animation :
1233 case hash_close_button_minimizes :
1234 case hash_log_buffer_size :
1235 case hash_log_font_name :
1236 case hash_log_font_size :
1237 case hash_log_highlight_messages :
1238 case hash_log_max_lines :
1239 case hash_log_messages :
1240 case hash_show_on_task_bar :
1241 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1242 /* These warnings are annoying - so hide them. -- Jon */
1243 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1246 /* *************************************************************************/
1248 /* *************************************************************************/
1250 * I decided that I liked this better as a warning than an
1251 * error. To change back to an error, just change log level
1252 * to LOG_LEVEL_FATAL.
1254 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1255 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1256 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1257 string_append(&config->proxy_args, buf);
1258 string_append(&config->proxy_args, "<br><br>\n");
1261 /* *************************************************************************/
1262 } /* end switch( hash_string(cmd) ) */
1263 } /* end while ( read_config_line(...) ) */
1267 if (NULL == config->proxy_args)
1269 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1272 init_error_log(Argv[0], config->logfile, config->debug);
1274 if (config->actions_file)
1276 add_loader(load_actions_file, config);
1279 if (config->re_filterfile)
1281 add_loader(load_re_filterfile, config);
1284 #ifdef FEATURE_TRUST
1285 if (config->trustfile)
1287 add_loader(load_trustfile, config);
1289 #endif /* def FEATURE_TRUST */
1291 #ifdef FEATURE_COOKIE_JAR
1292 if ( NULL != config->jarfile )
1294 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1296 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1297 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1299 setbuf(config->jar, NULL);
1301 #endif /* def FEATURE_COOKIE_JAR */
1303 if ( NULL == config->haddr )
1305 config->haddr = strdup( HADDR_DEFAULT );
1308 if ( NULL != config->haddr )
1310 if ((p = strchr(config->haddr, ':')))
1315 config->hport = atoi(p);
1319 if (config->hport <= 0)
1322 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1323 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1325 if (*config->haddr == '\0')
1327 config->haddr = NULL;
1332 * Want to run all the loaders once now.
1334 * Need to set up a fake csp, so they can get to the config.
1336 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1337 fake_csp->config = config;
1339 if (run_loader(fake_csp))
1342 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1343 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1347 /* FIXME: this is a kludge for win32 */
1348 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1350 g_actions_file = config->actions_file;
1351 g_re_filterfile = config->re_filterfile;
1353 #ifdef FEATURE_TRUST
1354 g_trustfile = config->trustfile;
1355 #endif /* def FEATURE_TRUST */
1358 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1359 /* FIXME: end kludge */
1362 config->need_bind = 1;
1364 if (current_configfile)
1366 struct configuration_spec * oldcfg = (struct configuration_spec *)
1367 current_configfile->f;
1369 * Check if config->haddr,hport == oldcfg->haddr,hport
1371 * The following could be written more compactly as a single,
1372 * (unreadably long) if statement.
1374 config->need_bind = 0;
1375 if (config->hport != oldcfg->hport)
1377 config->need_bind = 1;
1379 else if (config->haddr == NULL)
1381 if (oldcfg->haddr != NULL)
1383 config->need_bind = 1;
1386 else if (oldcfg->haddr == NULL)
1388 config->need_bind = 1;
1390 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1392 config->need_bind = 1;
1395 current_configfile->unloader = unload_configfile;
1398 fs->next = files->next;
1401 current_configfile = fs;
1407 /*********************************************************************
1409 * Function : savearg
1411 * Description : Called from `load_config'. It saves each non-empty
1412 * and non-comment line from config into
1413 * config->proxy_args. This is used to create the
1414 * show-proxy-args page. On error, frees
1415 * config->proxy_args and sets it to NULL
1418 * 1 : command = config setting that was found
1419 * 2 : argument = the setting's argument (if any)
1423 *********************************************************************/
1424 static void savearg(char *command, char *argument, struct configuration_spec * config)
1435 s = html_encode(command);
1439 freez(config->proxy_args);
1442 string_append(&buf, "<a href=\"" REDIRECT_URL "option#");
1443 string_append(&buf, s);
1444 string_append(&buf, "\">");
1445 string_join (&buf, s);
1446 string_append(&buf, "</a> ");
1448 if ( (NULL != argument) && ('\0' != *argument) )
1450 s = html_encode(argument);
1454 freez(config->proxy_args);
1458 if (strncmpic(argument, "http://", 7) == 0)
1460 string_append(&buf, "<a href=\"");
1461 string_append(&buf, s);
1462 string_append(&buf, "\">");
1463 string_join (&buf, s);
1464 string_append(&buf, "</a>");
1468 string_join (&buf, s);
1472 string_append(&buf, "<br>\n");
1474 string_join(&config->proxy_args, buf);