1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.35 2002/03/07 03:52:44 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.35 2002/03/07 03:52:44 oes
39 * Set logging to tty for --no-daemon mode
41 * Revision 1.34 2002/03/06 23:14:35 jongfoster
42 * Trivial cosmetic changes to make function comments easier to find.
44 * Revision 1.33 2002/03/05 04:52:42 oes
45 * Deleted non-errlog debugging code
47 * Revision 1.32 2002/03/04 18:24:53 oes
48 * Re-enabled output of unknown config directive hash
50 * Revision 1.31 2002/03/03 15:07:20 oes
51 * Re-enabled automatic config reloading
53 * Revision 1.30 2002/01/22 23:31:43 jongfoster
54 * Replacing strsav() with string_append()
56 * Revision 1.29 2002/01/17 21:02:30 jongfoster
57 * Moving all our URL and URL pattern parsing code to urlmatch.c.
59 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
61 * Revision 1.28 2001/12/30 14:07:32 steudten
62 * - Add signal handling (unix)
63 * - Add SIGHUP handler (unix)
64 * - Add creation of pidfile (unix)
65 * - Add action 'top' in rc file (RH)
66 * - Add entry 'SIGNALS' to manpage
67 * - Add exit message to logfile (unix)
69 * Revision 1.27 2001/11/07 00:02:13 steudten
70 * Add line number in error output for lineparsing for
71 * actionsfile and configfile.
72 * Special handling for CLF added.
74 * Revision 1.26 2001/11/05 21:41:43 steudten
75 * Add changes to be a real daemon just for unix os.
76 * (change cwd to /, detach from controlling tty, set
77 * process group and session leader to the own process.
79 * Add some fatal-error log message for failed malloc().
80 * Add '-d' if compiled with 'configure --with-debug' to
81 * enable debug output.
83 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
84 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
85 * threads to call select() simultaneously. So, it's time to do a real, live,
86 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
87 * (native). Both versions will work, but using __OS2__ offers multi-threading.
89 * Revision 1.24 2001/10/23 21:40:30 jongfoster
90 * Added support for enable-edit-actions and enable-remote-toggle config
93 * Revision 1.23 2001/10/07 15:36:00 oes
94 * Introduced new config option "buffer-limit"
96 * Revision 1.22 2001/09/22 16:36:59 jongfoster
97 * Removing unused parameter fs from read_config_line()
99 * Revision 1.21 2001/09/16 17:10:43 jongfoster
100 * Moving function savearg() here, since it was the only thing left in
103 * Revision 1.20 2001/07/30 22:08:36 jongfoster
104 * Tidying up #defines:
105 * - All feature #defines are now of the form FEATURE_xxx
106 * - Permanently turned off WIN_GUI_EDIT
107 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
109 * Revision 1.19 2001/07/15 17:45:16 jongfoster
110 * Removing some unused #includes
112 * Revision 1.18 2001/07/13 14:01:14 oes
113 * - Removed all #ifdef PCRS
114 * - Removed vim-settings
116 * Revision 1.17 2001/06/29 13:31:03 oes
117 * - Improved comments
118 * - Fixed (actionsfile) and sorted hashes
119 * - Introduced admin_address and proxy-info-url
120 * as config parameters
121 * - Renamed config->proxy_args_invocation (which didn't have
122 * the invocation but the options!) to config->proxy_args
123 * - Various adaptions
124 * - Removed logentry from cancelled commit
126 * Revision 1.16 2001/06/09 10:55:28 jongfoster
127 * Changing BUFSIZ ==> BUFFER_SIZE
129 * Revision 1.15 2001/06/07 23:13:40 jongfoster
130 * Merging ACL and forward files into config file.
131 * Cosmetic: Sorting config file options alphabetically.
132 * Cosmetic: Adding brief syntax comments to config file options.
134 * Revision 1.14 2001/06/07 14:46:25 joergs
135 * Missing make_path() added for re_filterfile.
137 * Revision 1.13 2001/06/05 22:33:54 jongfoster
139 * Fixed minor memory leak.
140 * Also now uses make_path to prepend the pathnames.
142 * Revision 1.12 2001/06/05 20:04:09 jongfoster
143 * Now uses _snprintf() in place of snprintf() under Win32.
145 * Revision 1.11 2001/06/04 18:31:58 swa
146 * files are now prefixed with either `confdir' or `logdir'.
147 * `make redhat-dist' replaces both entries confdir and logdir
150 * Revision 1.10 2001/06/03 19:11:54 oes
151 * introduced confdir option
153 * Revision 1.9 2001/06/01 20:06:24 jongfoster
154 * Removed support for "tinygif" option - moved to actions file.
156 * Revision 1.8 2001/05/31 21:27:13 jongfoster
157 * Removed many options from the config file and into the
158 * "actions" file: add_forwarded, suppress_vanilla_wafer,
159 * wafer, add_header, user_agent, referer, from
160 * Also globally replaced "permission" with "action".
162 * Revision 1.7 2001/05/29 09:50:24 jongfoster
163 * Unified blocklist/imagelist/permissionslist.
164 * File format is still under discussion, but the internal changes
167 * Also modified interceptor behaviour:
168 * - We now intercept all URLs beginning with one of the following
169 * prefixes (and *only* these prefixes):
171 * * http://ijbswa.sf.net/config/
172 * * http://ijbswa.sourceforge.net/config/
173 * - New interceptors "home page" - go to http://i.j.b/ to see it.
174 * - Internal changes so that intercepted and fast redirect pages
175 * are not replaced with an image.
176 * - Interceptors now have the option to send a binary page direct
177 * to the client. (i.e. ijb-send-banner uses this)
178 * - Implemented show-url-info interceptor. (Which is why I needed
179 * the above interceptors changes - a typical URL is
180 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
181 * The previous mechanism would not have intercepted that, and
182 * if it had been intercepted then it then it would have replaced
185 * Revision 1.6 2001/05/26 00:28:36 jongfoster
186 * Automatic reloading of config file.
187 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
188 * Most of the global variables have been moved to a new
189 * struct configuration_spec, accessed through csp->config->globalname
190 * Most of the globals remaining are used by the Win32 GUI.
192 * Revision 1.5 2001/05/25 22:34:30 jongfoster
195 * Revision 1.4 2001/05/22 18:46:04 oes
197 * - Enabled filtering banners by size rather than URL
198 * by adding patterns that replace all standard banner
199 * sizes with the "Junkbuster" gif to the re_filterfile
201 * - Enabled filtering WebBugs by providing a pattern
202 * which kills all 1x1 images
204 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
205 * which is selected by the (nonstandard and therefore
206 * capital) letter 'U' in the option string.
207 * It causes the quantifiers to be ungreedy by default.
208 * Appending a ? turns back to greedy (!).
210 * - Added a new interceptor ijb-send-banner, which
211 * sends back the "Junkbuster" gif. Without imagelist or
212 * MSIE detection support, or if tinygif = 1, or the
213 * URL isn't recognized as an imageurl, a lame HTML
214 * explanation is sent instead.
216 * - Added new feature, which permits blocking remote
217 * script redirects and firing back a local redirect
219 * The feature is conditionally compiled, i.e. it
220 * can be disabled with --disable-fast-redirects,
221 * plus it must be activated by a "fast-redirects"
222 * line in the config file, has its own log level
223 * and of course wants to be displayed by show-proxy-args
224 * Note: Boy, all the #ifdefs in 1001 locations and
225 * all the fumbling with configure.in and acconfig.h
226 * were *way* more work than the feature itself :-(
228 * - Because a generic redirect template was needed for
229 * this, tinygif = 3 now uses the same.
231 * - Moved GIFs, and other static HTTP response templates
236 * - Removed some >400 CRs again (Jon, you really worked
239 * Revision 1.3 2001/05/20 01:21:20 jongfoster
240 * Version 2.9.4 checkin.
241 * - Merged popupfile and cookiefile, and added control over PCRS
242 * filtering, in new "permissionsfile".
243 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
244 * file error you now get a message box (in the Win32 GUI) rather
245 * than the program exiting with no explanation.
246 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
248 * - Removed tabs from "config"
249 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
250 * - Bumped up version number.
252 * Revision 1.2 2001/05/17 23:01:01 oes
253 * - Cleaned CRLF's from the sources and related files
255 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
256 * Initial import of version 2.9.3 source tree
259 *********************************************************************/
265 #include <sys/types.h>
276 # include <windows.h>
279 # ifndef _WIN_CONSOLE
281 # endif /* ndef _WIN_CONSOLE */
283 /* VC++ has "_snprintf", not "snprintf" */
284 #define snprintf _snprintf
286 #else /* ifndef _WIN32 */
290 # include <sys/wait.h>
292 # include <sys/time.h>
293 # include <sys/stat.h>
303 #include "miscutil.h"
307 #include "urlmatch.h"
309 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
312 * Fix a problem with Solaris. There should be no effect on other
314 * Solaris's isspace() is a macro which uses it's argument directly
315 * as an array index. Therefore we need to make sure that high-bit
316 * characters generate +ve values, and ideally we also want to make
317 * the argument match the declared parameter type of "int".
319 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
320 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
322 #ifdef FEATURE_TOGGLE
323 /* by haroon - indicates if ijb is enabled */
324 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
325 #endif /* def FEATURE_TOGGLE */
327 /* The filename of the configfile */
328 const char *configfile = NULL;
331 * CGI functions will later need access to the invocation args,
332 * so we will make argc and argv global.
335 const char **Argv = NULL;
337 static struct file_list *current_configfile = NULL;
341 * This takes the "cryptic" hash of each keyword and aliases them to
342 * something a little more readable. This also makes changing the
343 * hash values easier if they should change or the hash algorthm changes.
344 * Use the included "hash" program to find out what the hash will be
345 * for any string supplied on the command line. (Or just put it in the
346 * config file and read the number from the error message in the log).
348 * Please keep this list sorted alphabetically (but with the Windows
349 * console and GUI specific options last).
352 #define hash_actions_file 1196306641ul /* "actionsfile" */
353 #define hash_admin_address 4112573064ul /* "admin-address" */
354 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
355 #define hash_confdir 1978389ul /* "confdir" */
356 #define hash_debug 78263ul /* "debug" */
357 #define hash_deny_access 1227333715ul /* "deny-access" */
358 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
359 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
360 #define hash_forward 2029845ul /* "forward" */
361 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
362 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
363 #define hash_jarfile 2046641ul /* "jarfile" */
364 #define hash_listen_address 1255650842ul /* "listen-address" */
365 #define hash_logdir 422889ul /* "logdir" */
366 #define hash_logfile 2114766ul /* "logfile" */
367 #define hash_permit_access 3587953268ul /* "permit-access" */
368 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
369 #define hash_re_filterfile 3877522444ul /* "re_filterfile" */
370 #define hash_single_threaded 4250084780ul /* "single-threaded" */
371 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
372 #define hash_toggle 447966ul /* "toggle" */
373 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
374 #define hash_trustfile 56494766ul /* "trustfile" */
376 #define hash_activity_animation 1817904738ul /* "activity-animation" */
377 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
378 #define hash_hide_console 2048809870ul /* "hide-console" */
379 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
380 #define hash_log_font_name 2866730124ul /* "log-font-name" */
381 #define hash_log_font_size 2866731014ul /* "log-font-size" */
382 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
383 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
384 #define hash_log_messages 2291744899ul /* "log-messages" */
385 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
388 static void savearg(char *command, char *argument, struct configuration_spec * config);
390 /*********************************************************************
392 * Function : unload_configfile
394 * Description : Free the config structure and all components.
397 * 1 : data: struct configuration_spec to unload
401 *********************************************************************/
402 void unload_configfile (void * data)
404 struct configuration_spec * config = (struct configuration_spec *)data;
405 struct forward_spec *cur_fwd = config->forward;
407 struct access_control_list *cur_acl = config->acl;
409 while (cur_acl != NULL)
411 struct access_control_list * next_acl = cur_acl->next;
416 #endif /* def FEATURE_ACL */
418 while (cur_fwd != NULL)
420 struct forward_spec * next_fwd = cur_fwd->next;
421 free_url_spec(cur_fwd->url);
423 freez(cur_fwd->gateway_host);
424 freez(cur_fwd->forward_host);
428 config->forward = NULL;
430 #ifdef FEATURE_COOKIE_JAR
431 if ( NULL != config->jar )
433 fclose( config->jar );
436 #endif /* def FEATURE_COOKIE_JAR */
438 freez(config->confdir);
439 freez(config->logdir);
441 freez(config->haddr);
442 freez(config->logfile);
444 freez(config->actions_file);
445 freez(config->admin_address);
446 freez(config->proxy_info_url);
447 freez(config->proxy_args);
449 #ifdef FEATURE_COOKIE_JAR
450 freez(config->jarfile);
451 #endif /* def FEATURE_COOKIE_JAR */
453 freez(config->re_filterfile);
458 /*********************************************************************
460 * Function : load_config
462 * Description : Load the config file and all parameters.
465 * 1 : csp = Client state (the config member will be
466 * filled in by this function).
468 * Returns : 0 => Ok, everything else is an error.
470 *********************************************************************/
471 struct configuration_spec * load_config(void)
473 char buf[BUFFER_SIZE];
475 FILE *configfp = NULL;
476 struct configuration_spec * config = NULL;
477 struct client_state * fake_csp;
478 struct file_list *fs;
479 unsigned long linenum = 0;
481 if ( !check_file_changed(current_configfile, configfile, &fs))
483 /* No need to load */
484 return ((struct configuration_spec *)current_configfile->f);
488 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
492 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
494 #ifdef FEATURE_TOGGLE
496 #endif /* def FEATURE_TOGGLE */
498 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
504 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
505 /* Never get here - LOG_LEVEL_FATAL causes program exit */
509 * This is backwards from how it's usually done.
510 * Following the usual pattern, "fs" would be stored in a member
511 * variable in "csp", and then we'd access "config" from "fs->f",
512 * using a cast. However, "config" is used so often that a
513 * cast each time would be very ugly, and the extra indirection
514 * would waste CPU cycles. Therefore we store "config" in
515 * "csp->config", and "fs" in "csp->config->config_file_list".
517 config->config_file_list = fs;
523 config->multi_threaded = 1;
524 config->hport = HADDR_PORT;
525 config->buffer_limit = 4096 * 1024;
526 config->proxy_args = strdup("");
528 if ((configfp = fopen(configfile, "r")) == NULL)
530 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
532 /* Never get here - LOG_LEVEL_FATAL causes program exit */
535 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
537 char cmd[BUFFER_SIZE];
538 char arg[BUFFER_SIZE];
539 char tmp[BUFFER_SIZE];
541 struct access_control_list *cur_acl;
542 #endif /* def FEATURE_ACL */
543 struct forward_spec *cur_fwd;
549 /* Copy command (i.e. up to space or tab) into cmd */
552 while (*p && (*p != ' ') && (*p != '\t'))
558 /* Skip over the whitespace in buf */
559 while (*p && ((*p == ' ') || (*p == '\t')))
564 /* Copy the argument into arg */
567 /* Should never happen, but check this anyway */
573 /* Make sure the command field is lower case */
578 *p = ijb_tolower(*p);
582 /* Save the argument for show-proxy-args */
583 savearg(cmd, arg, config);
586 switch( hash_string( cmd ) )
588 /* *************************************************************************
589 * actionsfile actions-file-name
590 * In confdir by default
591 * *************************************************************************/
592 case hash_actions_file :
593 freez(config->actions_file);
594 config->actions_file = make_path(config->confdir, arg);
597 /* *************************************************************************
598 * admin-address email-address
599 * *************************************************************************/
600 case hash_admin_address :
601 freez(config->admin_address);
602 config->admin_address = strdup(arg);
605 /* *************************************************************************
607 * *************************************************************************/
608 case hash_buffer_limit :
609 config->buffer_limit = (size_t) 1024 * atoi(arg);
612 /* *************************************************************************
613 * confdir directory-name
614 * *************************************************************************/
616 freez(config->confdir);
617 config->confdir = make_path( NULL, arg);
620 /* *************************************************************************
622 * Specifies debug level, multiple values are ORed together.
623 * *************************************************************************/
625 config->debug |= atoi(arg);
628 /* *************************************************************************
629 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
630 * *************************************************************************/
632 case hash_deny_access:
633 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
635 if ((vec_count != 1) && (vec_count != 2))
637 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
638 "deny-access directive in configuration file.");
639 string_append(&config->proxy_args,
640 "<br>\nWARNING: Wrong number of parameters for "
641 "deny-access directive in configuration file.<br><br>\n");
645 /* allocate a new node */
646 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
650 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
651 /* Never get here - LOG_LEVEL_FATAL causes program exit */
654 cur_acl->action = ACL_DENY;
656 if (acl_addr(vec[0], cur_acl->src) < 0)
658 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
659 "directive in configuration file: \"%s\"", vec[0]);
660 string_append(&config->proxy_args,
661 "<br>\nWARNING: Invalid source IP for deny-access directive"
662 " in configuration file: \"");
663 string_append(&config->proxy_args,
665 string_append(&config->proxy_args,
672 if (acl_addr(vec[1], cur_acl->dst) < 0)
674 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
675 "directive in configuration file: \"%s\"", vec[0]);
676 string_append(&config->proxy_args,
677 "<br>\nWARNING: Invalid destination IP for deny-access directive"
678 " in configuration file: \"");
679 string_append(&config->proxy_args,
681 string_append(&config->proxy_args,
689 * Add it to the list. Note we reverse the list to get the
690 * behaviour the user expects. With both the ACL and
691 * actions file, the last match wins. However, the internal
692 * implementations are different: The actions file is stored
693 * in the same order as the file, and scanned completely.
694 * With the ACL, we reverse the order as we load it, then
695 * when we scan it we stop as soon as we get a match.
697 cur_acl->next = config->acl;
698 config->acl = cur_acl;
701 #endif /* def FEATURE_ACL */
703 /* *************************************************************************
704 * enable-edit-actions 0|1
705 * *************************************************************************/
706 #ifdef FEATURE_CGI_EDIT_ACTIONS
707 case hash_enable_edit_actions:
708 if ((*arg != '\0') && (0 != atoi(arg)))
710 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
714 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
717 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
719 /* *************************************************************************
720 * enable-remote-toggle 0|1
721 * *************************************************************************/
722 #ifdef FEATURE_CGI_EDIT_ACTIONS
723 case hash_enable_remote_toggle:
724 if ((*arg != '\0') && (0 != atoi(arg)))
726 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
730 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
733 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
735 /* *************************************************************************
736 * forward url-pattern (.|http-proxy-host[:port])
737 * *************************************************************************/
739 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
743 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
744 "directive in configuration file.");
745 string_append(&config->proxy_args,
746 "<br>\nWARNING: Wrong number of parameters for "
747 "forward directive in configuration file.");
751 /* allocate a new node */
752 cur_fwd = zalloc(sizeof(*cur_fwd));
755 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
756 /* Never get here - LOG_LEVEL_FATAL causes program exit */
760 cur_fwd->type = SOCKS_NONE;
762 /* Save the URL pattern */
763 if (create_url_spec(cur_fwd->url, vec[0]))
765 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
766 "directive in configuration file.");
767 string_append(&config->proxy_args,
768 "<br>\nWARNING: Bad URL specifier for "
769 "forward directive in configuration file.");
773 /* Parse the parent HTTP proxy host:port */
776 if (strcmp(p, ".") != 0)
778 cur_fwd->forward_host = strdup(p);
780 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
783 cur_fwd->forward_port = atoi(p);
786 if (cur_fwd->forward_port <= 0)
788 cur_fwd->forward_port = 8000;
793 cur_fwd->next = config->forward;
794 config->forward = cur_fwd;
798 /* *************************************************************************
799 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
800 * *************************************************************************/
801 case hash_forward_socks4:
802 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
806 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
807 "forward-socks4 directive in configuration file.");
808 string_append(&config->proxy_args,
809 "<br>\nWARNING: Wrong number of parameters for "
810 "forward-socks4 directive in configuration file.");
814 /* allocate a new node */
815 cur_fwd = zalloc(sizeof(*cur_fwd));
818 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
819 /* Never get here - LOG_LEVEL_FATAL causes program exit */
823 cur_fwd->type = SOCKS_4;
825 /* Save the URL pattern */
826 if (create_url_spec(cur_fwd->url, vec[0]))
828 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
829 "directive in configuration file.");
830 string_append(&config->proxy_args,
831 "<br>\nWARNING: Bad URL specifier for "
832 "forward-socks4 directive in configuration file.");
836 /* Parse the SOCKS proxy host[:port] */
839 if (strcmp(p, ".") != 0)
841 cur_fwd->gateway_host = strdup(p);
843 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
846 cur_fwd->gateway_port = atoi(p);
848 if (cur_fwd->gateway_port <= 0)
850 cur_fwd->gateway_port = 1080;
854 /* Parse the parent HTTP proxy host[:port] */
857 if (strcmp(p, ".") != 0)
859 cur_fwd->forward_host = strdup(p);
861 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
864 cur_fwd->forward_port = atoi(p);
867 if (cur_fwd->forward_port <= 0)
869 cur_fwd->forward_port = 8000;
874 cur_fwd->next = config->forward;
875 config->forward = cur_fwd;
879 /* *************************************************************************
880 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
881 * *************************************************************************/
882 case hash_forward_socks4a:
883 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
887 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
888 "forward-socks4a directive in configuration file.");
889 string_append(&config->proxy_args,
890 "<br>\nWARNING: Wrong number of parameters for "
891 "forward-socks4a directive in configuration file.");
895 /* allocate a new node */
896 cur_fwd = zalloc(sizeof(*cur_fwd));
899 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
900 /* Never get here - LOG_LEVEL_FATAL causes program exit */
904 cur_fwd->type = SOCKS_4A;
906 /* Save the URL pattern */
907 if (create_url_spec(cur_fwd->url, vec[0]))
909 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
910 "directive in configuration file.");
911 string_append(&config->proxy_args,
912 "<br>\nWARNING: Bad URL specifier for "
913 "forward-socks4a directive in configuration file.");
917 /* Parse the SOCKS proxy host[:port] */
920 cur_fwd->gateway_host = strdup(p);
922 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
925 cur_fwd->gateway_port = atoi(p);
927 if (cur_fwd->gateway_port <= 0)
929 cur_fwd->gateway_port = 1080;
932 /* Parse the parent HTTP proxy host[:port] */
935 if (strcmp(p, ".") != 0)
937 cur_fwd->forward_host = strdup(p);
939 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
942 cur_fwd->forward_port = atoi(p);
945 if (cur_fwd->forward_port <= 0)
947 cur_fwd->forward_port = 8000;
952 cur_fwd->next = config->forward;
953 config->forward = cur_fwd;
957 /* *************************************************************************
958 * jarfile jar-file-name
959 * In logdir by default
960 * *************************************************************************/
961 #ifdef FEATURE_COOKIE_JAR
963 freez(config->jarfile);
964 config->jarfile = make_path(config->logdir, arg);
966 #endif /* def FEATURE_COOKIE_JAR */
968 /* *************************************************************************
969 * listen-address [ip][:port]
970 * *************************************************************************/
971 case hash_listen_address :
972 freez(config->haddr);
973 config->haddr = strdup(arg);
976 /* *************************************************************************
977 * logdir directory-name
978 * *************************************************************************/
980 freez(config->logdir);
981 config->logdir = make_path(NULL, arg);
984 /* *************************************************************************
985 * logfile log-file-name
986 * In logdir by default
987 * *************************************************************************/
989 freez(config->logfile);
990 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
993 /* *************************************************************************
994 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
995 * *************************************************************************/
997 case hash_permit_access:
998 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1000 if ((vec_count != 1) && (vec_count != 2))
1002 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1003 "permit-access directive in configuration file.");
1004 string_append(&config->proxy_args,
1005 "<br>\nWARNING: Wrong number of parameters for "
1006 "permit-access directive in configuration file.<br><br>\n");
1011 /* allocate a new node */
1012 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1014 if (cur_acl == NULL)
1016 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1017 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1020 cur_acl->action = ACL_PERMIT;
1022 if (acl_addr(vec[0], cur_acl->src) < 0)
1024 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1025 "directive in configuration file: \"%s\"", vec[0]);
1026 string_append(&config->proxy_args,
1027 "<br>\nWARNING: Invalid source IP for permit-access directive"
1028 " in configuration file: \"");
1029 string_append(&config->proxy_args,
1031 string_append(&config->proxy_args,
1038 if (acl_addr(vec[1], cur_acl->dst) < 0)
1040 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1041 "permit-access directive in configuration file: \"%s\"",
1043 string_append(&config->proxy_args,
1044 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1045 " in configuration file: \"");
1046 string_append(&config->proxy_args,
1048 string_append(&config->proxy_args,
1056 * Add it to the list. Note we reverse the list to get the
1057 * behaviour the user expects. With both the ACL and
1058 * actions file, the last match wins. However, the internal
1059 * implementations are different: The actions file is stored
1060 * in the same order as the file, and scanned completely.
1061 * With the ACL, we reverse the order as we load it, then
1062 * when we scan it we stop as soon as we get a match.
1064 cur_acl->next = config->acl;
1065 config->acl = cur_acl;
1068 #endif /* def FEATURE_ACL */
1070 /* *************************************************************************
1071 * proxy-info-url url
1072 * *************************************************************************/
1073 case hash_proxy_info_url :
1074 freez(config->proxy_info_url);
1075 config->proxy_info_url = strdup(arg);
1078 /* *************************************************************************
1079 * re_filterfile file-name
1080 * In confdir by default.
1081 * *************************************************************************/
1082 case hash_re_filterfile :
1083 freez(config->re_filterfile);
1084 config->re_filterfile = make_path(config->confdir, arg);
1087 /* *************************************************************************
1089 * *************************************************************************/
1090 case hash_single_threaded :
1091 config->multi_threaded = 0;
1094 /* *************************************************************************
1096 * *************************************************************************/
1097 #ifdef FEATURE_TOGGLE
1099 g_bToggleIJB = atoi(arg);
1101 #endif /* def FEATURE_TOGGLE */
1103 /* *************************************************************************
1104 * trust-info-url url
1105 * *************************************************************************/
1106 #ifdef FEATURE_TRUST
1107 case hash_trust_info_url :
1108 enlist(config->trust_info, arg);
1110 #endif /* def FEATURE_TRUST */
1112 /* *************************************************************************
1113 * trustfile filename
1114 * (In confdir by default.)
1115 * *************************************************************************/
1116 #ifdef FEATURE_TRUST
1117 case hash_trustfile :
1118 freez(config->trustfile);
1119 config->trustfile = make_path(config->confdir, arg);
1121 #endif /* def FEATURE_TRUST */
1124 /* *************************************************************************
1125 * Win32 Console options:
1126 * *************************************************************************/
1128 /* *************************************************************************
1130 * *************************************************************************/
1132 case hash_hide_console :
1135 #endif /*def _WIN_CONSOLE*/
1138 /* *************************************************************************
1139 * Win32 GUI options:
1140 * *************************************************************************/
1142 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1143 /* *************************************************************************
1144 * activity-animation (0|1)
1145 * *************************************************************************/
1146 case hash_activity_animation :
1147 g_bShowActivityAnimation = atoi(arg);
1150 /* *************************************************************************
1151 * close-button-minimizes (0|1)
1152 * *************************************************************************/
1153 case hash_close_button_minimizes :
1154 g_bCloseHidesWindow = atoi(arg);
1157 /* *************************************************************************
1158 * log-buffer-size (0|1)
1159 * *************************************************************************/
1160 case hash_log_buffer_size :
1161 g_bLimitBufferSize = atoi(arg);
1164 /* *************************************************************************
1165 * log-font-name fontnane
1166 * *************************************************************************/
1167 case hash_log_font_name :
1168 strcpy( g_szFontFaceName, arg );
1171 /* *************************************************************************
1173 * *************************************************************************/
1174 case hash_log_font_size :
1175 g_nFontSize = atoi(arg);
1178 /* *************************************************************************
1179 * log-highlight-messages (0|1)
1180 * *************************************************************************/
1181 case hash_log_highlight_messages :
1182 g_bHighlightMessages = atoi(arg);
1185 /* *************************************************************************
1187 * *************************************************************************/
1188 case hash_log_max_lines :
1189 g_nMaxBufferLines = atoi(arg);
1192 /* *************************************************************************
1193 * log-messages (0|1)
1194 * *************************************************************************/
1195 case hash_log_messages :
1196 g_bLogMessages = atoi(arg);
1199 /* *************************************************************************
1200 * show-on-task-bar (0|1)
1201 * *************************************************************************/
1202 case hash_show_on_task_bar :
1203 g_bShowOnTaskBar = atoi(arg);
1206 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1209 /* *************************************************************************
1210 * Warnings about unsupported features
1211 * *************************************************************************/
1213 case hash_deny_access:
1214 #endif /* ndef FEATURE_ACL */
1215 #ifndef FEATURE_CGI_EDIT_ACTIONS
1216 case hash_enable_edit_actions:
1217 case hash_enable_remote_toggle:
1218 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1219 #ifndef FEATURE_COOKIE_JAR
1221 #endif /* ndef FEATURE_COOKIE_JAR */
1223 case hash_permit_access:
1224 #endif /* ndef FEATURE_ACL */
1225 #ifndef FEATURE_TOGGLE
1227 #endif /* ndef FEATURE_TOGGLE */
1228 #ifndef FEATURE_TRUST
1229 case hash_trustfile :
1230 case hash_trust_info_url :
1231 #endif /* ndef FEATURE_TRUST */
1233 #ifndef _WIN_CONSOLE
1234 case hash_hide_console :
1235 #endif /* ndef _WIN_CONSOLE */
1237 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1238 case hash_activity_animation :
1239 case hash_close_button_minimizes :
1240 case hash_log_buffer_size :
1241 case hash_log_font_name :
1242 case hash_log_font_size :
1243 case hash_log_highlight_messages :
1244 case hash_log_max_lines :
1245 case hash_log_messages :
1246 case hash_show_on_task_bar :
1247 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1248 /* These warnings are annoying - so hide them. -- Jon */
1249 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1252 /* *************************************************************************/
1254 /* *************************************************************************/
1256 * I decided that I liked this better as a warning than an
1257 * error. To change back to an error, just change log level
1258 * to LOG_LEVEL_FATAL.
1260 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1261 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1262 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1263 string_append(&config->proxy_args, buf);
1264 string_append(&config->proxy_args, "<br><br>\n");
1267 /* *************************************************************************/
1268 } /* end switch( hash_string(cmd) ) */
1269 } /* end while ( read_config_line(...) ) */
1273 if (NULL == config->proxy_args)
1275 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1278 init_error_log(Argv[0], config->logfile, config->debug);
1280 if (config->actions_file)
1282 add_loader(load_actions_file, config);
1285 if (config->re_filterfile)
1287 add_loader(load_re_filterfile, config);
1290 #ifdef FEATURE_TRUST
1291 if (config->trustfile)
1293 add_loader(load_trustfile, config);
1295 #endif /* def FEATURE_TRUST */
1297 #ifdef FEATURE_COOKIE_JAR
1298 if ( NULL != config->jarfile )
1300 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1302 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1303 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1305 setbuf(config->jar, NULL);
1307 #endif /* def FEATURE_COOKIE_JAR */
1309 if ( NULL == config->haddr )
1311 config->haddr = strdup( HADDR_DEFAULT );
1314 if ( NULL != config->haddr )
1316 if (NULL != (p = strchr(config->haddr, ':')))
1321 config->hport = atoi(p);
1325 if (config->hport <= 0)
1328 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1329 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1331 if (*config->haddr == '\0')
1333 config->haddr = NULL;
1338 * Want to run all the loaders once now.
1340 * Need to set up a fake csp, so they can get to the config.
1342 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1343 fake_csp->config = config;
1345 if (run_loader(fake_csp))
1348 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1349 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1353 /* FIXME: this is a kludge for win32 */
1354 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1356 g_actions_file = config->actions_file;
1357 g_re_filterfile = config->re_filterfile;
1359 #ifdef FEATURE_TRUST
1360 g_trustfile = config->trustfile;
1361 #endif /* def FEATURE_TRUST */
1364 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1365 /* FIXME: end kludge */
1368 config->need_bind = 1;
1370 if (current_configfile)
1372 struct configuration_spec * oldcfg = (struct configuration_spec *)
1373 current_configfile->f;
1375 * Check if config->haddr,hport == oldcfg->haddr,hport
1377 * The following could be written more compactly as a single,
1378 * (unreadably long) if statement.
1380 config->need_bind = 0;
1381 if (config->hport != oldcfg->hport)
1383 config->need_bind = 1;
1385 else if (config->haddr == NULL)
1387 if (oldcfg->haddr != NULL)
1389 config->need_bind = 1;
1392 else if (oldcfg->haddr == NULL)
1394 config->need_bind = 1;
1396 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1398 config->need_bind = 1;
1401 current_configfile->unloader = unload_configfile;
1404 fs->next = files->next;
1407 current_configfile = fs;
1413 /*********************************************************************
1415 * Function : savearg
1417 * Description : Called from `load_config'. It saves each non-empty
1418 * and non-comment line from config into
1419 * config->proxy_args. This is used to create the
1420 * show-proxy-args page. On error, frees
1421 * config->proxy_args and sets it to NULL
1424 * 1 : command = config setting that was found
1425 * 2 : argument = the setting's argument (if any)
1429 *********************************************************************/
1430 static void savearg(char *command, char *argument, struct configuration_spec * config)
1441 s = html_encode(command);
1445 freez(config->proxy_args);
1448 string_append(&buf, "<a href=\"" REDIRECT_URL "option#");
1449 string_append(&buf, s);
1450 string_append(&buf, "\">");
1451 string_join (&buf, s);
1452 string_append(&buf, "</a> ");
1454 if ( (NULL != argument) && ('\0' != *argument) )
1456 s = html_encode(argument);
1460 freez(config->proxy_args);
1464 if (strncmpic(argument, "http://", 7) == 0)
1466 string_append(&buf, "<a href=\"");
1467 string_append(&buf, s);
1468 string_append(&buf, "\">");
1469 string_join (&buf, s);
1470 string_append(&buf, "</a>");
1474 string_join (&buf, s);
1478 string_append(&buf, "<br>\n");
1480 string_join(&config->proxy_args, buf);