1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.29 2002/01/17 21:02:30 jongfoster 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.29 2002/01/17 21:02:30 jongfoster
39 * Moving all our URL and URL pattern parsing code to urlmatch.c.
41 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
43 * Revision 1.28 2001/12/30 14:07:32 steudten
44 * - Add signal handling (unix)
45 * - Add SIGHUP handler (unix)
46 * - Add creation of pidfile (unix)
47 * - Add action 'top' in rc file (RH)
48 * - Add entry 'SIGNALS' to manpage
49 * - Add exit message to logfile (unix)
51 * Revision 1.27 2001/11/07 00:02:13 steudten
52 * Add line number in error output for lineparsing for
53 * actionsfile and configfile.
54 * Special handling for CLF added.
56 * Revision 1.26 2001/11/05 21:41:43 steudten
57 * Add changes to be a real daemon just for unix os.
58 * (change cwd to /, detach from controlling tty, set
59 * process group and session leader to the own process.
61 * Add some fatal-error log message for failed malloc().
62 * Add '-d' if compiled with 'configure --with-debug' to
63 * enable debug output.
65 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
66 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
67 * threads to call select() simultaneously. So, it's time to do a real, live,
68 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
69 * (native). Both versions will work, but using __OS2__ offers multi-threading.
71 * Revision 1.24 2001/10/23 21:40:30 jongfoster
72 * Added support for enable-edit-actions and enable-remote-toggle config
75 * Revision 1.23 2001/10/07 15:36:00 oes
76 * Introduced new config option "buffer-limit"
78 * Revision 1.22 2001/09/22 16:36:59 jongfoster
79 * Removing unused parameter fs from read_config_line()
81 * Revision 1.21 2001/09/16 17:10:43 jongfoster
82 * Moving function savearg() here, since it was the only thing left in
85 * Revision 1.20 2001/07/30 22:08:36 jongfoster
86 * Tidying up #defines:
87 * - All feature #defines are now of the form FEATURE_xxx
88 * - Permanently turned off WIN_GUI_EDIT
89 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
91 * Revision 1.19 2001/07/15 17:45:16 jongfoster
92 * Removing some unused #includes
94 * Revision 1.18 2001/07/13 14:01:14 oes
95 * - Removed all #ifdef PCRS
96 * - Removed vim-settings
98 * Revision 1.17 2001/06/29 13:31:03 oes
100 * - Fixed (actionsfile) and sorted hashes
101 * - Introduced admin_address and proxy-info-url
102 * as config parameters
103 * - Renamed config->proxy_args_invocation (which didn't have
104 * the invocation but the options!) to config->proxy_args
105 * - Various adaptions
106 * - Removed logentry from cancelled commit
108 * Revision 1.16 2001/06/09 10:55:28 jongfoster
109 * Changing BUFSIZ ==> BUFFER_SIZE
111 * Revision 1.15 2001/06/07 23:13:40 jongfoster
112 * Merging ACL and forward files into config file.
113 * Cosmetic: Sorting config file options alphabetically.
114 * Cosmetic: Adding brief syntax comments to config file options.
116 * Revision 1.14 2001/06/07 14:46:25 joergs
117 * Missing make_path() added for re_filterfile.
119 * Revision 1.13 2001/06/05 22:33:54 jongfoster
121 * Fixed minor memory leak.
122 * Also now uses make_path to prepend the pathnames.
124 * Revision 1.12 2001/06/05 20:04:09 jongfoster
125 * Now uses _snprintf() in place of snprintf() under Win32.
127 * Revision 1.11 2001/06/04 18:31:58 swa
128 * files are now prefixed with either `confdir' or `logdir'.
129 * `make redhat-dist' replaces both entries confdir and logdir
132 * Revision 1.10 2001/06/03 19:11:54 oes
133 * introduced confdir option
135 * Revision 1.9 2001/06/01 20:06:24 jongfoster
136 * Removed support for "tinygif" option - moved to actions file.
138 * Revision 1.8 2001/05/31 21:27:13 jongfoster
139 * Removed many options from the config file and into the
140 * "actions" file: add_forwarded, suppress_vanilla_wafer,
141 * wafer, add_header, user_agent, referer, from
142 * Also globally replaced "permission" with "action".
144 * Revision 1.7 2001/05/29 09:50:24 jongfoster
145 * Unified blocklist/imagelist/permissionslist.
146 * File format is still under discussion, but the internal changes
149 * Also modified interceptor behaviour:
150 * - We now intercept all URLs beginning with one of the following
151 * prefixes (and *only* these prefixes):
153 * * http://ijbswa.sf.net/config/
154 * * http://ijbswa.sourceforge.net/config/
155 * - New interceptors "home page" - go to http://i.j.b/ to see it.
156 * - Internal changes so that intercepted and fast redirect pages
157 * are not replaced with an image.
158 * - Interceptors now have the option to send a binary page direct
159 * to the client. (i.e. ijb-send-banner uses this)
160 * - Implemented show-url-info interceptor. (Which is why I needed
161 * the above interceptors changes - a typical URL is
162 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
163 * The previous mechanism would not have intercepted that, and
164 * if it had been intercepted then it then it would have replaced
167 * Revision 1.6 2001/05/26 00:28:36 jongfoster
168 * Automatic reloading of config file.
169 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
170 * Most of the global variables have been moved to a new
171 * struct configuration_spec, accessed through csp->config->globalname
172 * Most of the globals remaining are used by the Win32 GUI.
174 * Revision 1.5 2001/05/25 22:34:30 jongfoster
177 * Revision 1.4 2001/05/22 18:46:04 oes
179 * - Enabled filtering banners by size rather than URL
180 * by adding patterns that replace all standard banner
181 * sizes with the "Junkbuster" gif to the re_filterfile
183 * - Enabled filtering WebBugs by providing a pattern
184 * which kills all 1x1 images
186 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
187 * which is selected by the (nonstandard and therefore
188 * capital) letter 'U' in the option string.
189 * It causes the quantifiers to be ungreedy by default.
190 * Appending a ? turns back to greedy (!).
192 * - Added a new interceptor ijb-send-banner, which
193 * sends back the "Junkbuster" gif. Without imagelist or
194 * MSIE detection support, or if tinygif = 1, or the
195 * URL isn't recognized as an imageurl, a lame HTML
196 * explanation is sent instead.
198 * - Added new feature, which permits blocking remote
199 * script redirects and firing back a local redirect
201 * The feature is conditionally compiled, i.e. it
202 * can be disabled with --disable-fast-redirects,
203 * plus it must be activated by a "fast-redirects"
204 * line in the config file, has its own log level
205 * and of course wants to be displayed by show-proxy-args
206 * Note: Boy, all the #ifdefs in 1001 locations and
207 * all the fumbling with configure.in and acconfig.h
208 * were *way* more work than the feature itself :-(
210 * - Because a generic redirect template was needed for
211 * this, tinygif = 3 now uses the same.
213 * - Moved GIFs, and other static HTTP response templates
218 * - Removed some >400 CRs again (Jon, you really worked
221 * Revision 1.3 2001/05/20 01:21:20 jongfoster
222 * Version 2.9.4 checkin.
223 * - Merged popupfile and cookiefile, and added control over PCRS
224 * filtering, in new "permissionsfile".
225 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
226 * file error you now get a message box (in the Win32 GUI) rather
227 * than the program exiting with no explanation.
228 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
230 * - Removed tabs from "config"
231 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
232 * - Bumped up version number.
234 * Revision 1.2 2001/05/17 23:01:01 oes
235 * - Cleaned CRLF's from the sources and related files
237 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
238 * Initial import of version 2.9.3 source tree
241 *********************************************************************/
247 #include <sys/types.h>
258 # include <windows.h>
261 # ifndef _WIN_CONSOLE
263 # endif /* ndef _WIN_CONSOLE */
265 /* VC++ has "_snprintf", not "snprintf" */
266 #define snprintf _snprintf
268 #else /* ifndef _WIN32 */
272 # include <sys/wait.h>
274 # include <sys/time.h>
275 # include <sys/stat.h>
285 #include "miscutil.h"
289 #include "urlmatch.h"
291 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
294 * Fix a problem with Solaris. There should be no effect on other
296 * Solaris's isspace() is a macro which uses it's argument directly
297 * as an array index. Therefore we need to make sure that high-bit
298 * characters generate +ve values, and ideally we also want to make
299 * the argument match the declared parameter type of "int".
301 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
302 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
304 #ifdef FEATURE_TOGGLE
305 /* by haroon - indicates if ijb is enabled */
306 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
307 #endif /* def FEATURE_TOGGLE */
309 /* The filename of the configfile */
310 const char *configfile = NULL;
313 * CGI functions will later need access to the invocation args,
314 * so we will make argc and argv global.
317 const char **Argv = NULL;
319 static struct file_list *current_configfile = NULL;
323 * This takes the "cryptic" hash of each keyword and aliases them to
324 * something a little more readable. This also makes changing the
325 * hash values easier if they should change or the hash algorthm changes.
326 * Use the included "hash" program to find out what the hash will be
327 * for any string supplied on the command line. (Or just put it in the
328 * config file and read the number from the error message in the log).
330 * Please keep this list sorted alphabetically (but with the Windows
331 * console and GUI specific options last).
334 #define hash_actions_file 1196306641ul /* "actionsfile" */
335 #define hash_admin_address 4112573064ul /* "admin-address" */
336 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
337 #define hash_confdir 1978389ul /* "confdir" */
338 #define hash_debug 78263ul /* "debug" */
339 #define hash_deny_access 1227333715ul /* "deny-access" */
340 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
341 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
342 #define hash_forward 2029845ul /* "forward" */
343 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
344 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
345 #define hash_jarfile 2046641ul /* "jarfile" */
346 #define hash_listen_address 1255650842ul /* "listen-address" */
347 #define hash_logdir 422889ul /* "logdir" */
348 #define hash_logfile 2114766ul /* "logfile" */
349 #define hash_permit_access 3587953268ul /* "permit-access" */
350 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
351 #define hash_re_filterfile 3877522444ul /* "re_filterfile" */
352 #define hash_single_threaded 4250084780ul /* "single-threaded" */
353 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
354 #define hash_toggle 447966ul /* "toggle" */
355 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
356 #define hash_trustfile 56494766ul /* "trustfile" */
358 #define hash_activity_animation 1817904738ul /* "activity-animation" */
359 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
360 #define hash_hide_console 2048809870ul /* "hide-console" */
361 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
362 #define hash_log_font_name 2866730124ul /* "log-font-name" */
363 #define hash_log_font_size 2866731014ul /* "log-font-size" */
364 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
365 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
366 #define hash_log_messages 2291744899ul /* "log-messages" */
367 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
370 static void savearg(char *command, char *argument, struct configuration_spec * config);
372 /*********************************************************************
374 * Function : unload_configfile
376 * Description : Free the config structure and all components.
379 * 1 : data: struct configuration_spec to unload
383 *********************************************************************/
384 void unload_configfile (void * data)
386 struct configuration_spec * config = (struct configuration_spec *)data;
387 struct forward_spec *cur_fwd = config->forward;
389 struct access_control_list *cur_acl = config->acl;
391 while (cur_acl != NULL)
393 struct access_control_list * next_acl = cur_acl->next;
398 #endif /* def FEATURE_ACL */
400 while (cur_fwd != NULL)
402 struct forward_spec * next_fwd = cur_fwd->next;
403 free_url_spec(cur_fwd->url);
405 freez(cur_fwd->gateway_host);
406 freez(cur_fwd->forward_host);
410 config->forward = NULL;
412 #ifdef FEATURE_COOKIE_JAR
413 if ( NULL != config->jar )
415 fclose( config->jar );
418 #endif /* def FEATURE_COOKIE_JAR */
420 freez(config->confdir);
421 freez(config->logdir);
423 freez(config->haddr);
424 freez(config->logfile);
426 freez(config->actions_file);
427 freez(config->admin_address);
428 freez(config->proxy_info_url);
429 freez(config->proxy_args);
431 #ifdef FEATURE_COOKIE_JAR
432 freez(config->jarfile);
433 #endif /* def FEATURE_COOKIE_JAR */
435 freez(config->re_filterfile);
440 /*********************************************************************
442 * Function : load_config
444 * Description : Load the config file and all parameters.
447 * 1 : csp = Client state (the config member will be
448 * filled in by this function).
450 * Returns : 0 => Ok, everything else is an error.
452 *********************************************************************/
453 struct configuration_spec * load_config(void)
455 char buf[BUFFER_SIZE];
457 FILE *configfp = NULL;
458 struct configuration_spec * config = NULL;
459 struct client_state * fake_csp;
460 struct file_list *fs;
461 unsigned long linenum = 0;
463 DBG(1, ("load_config() entered..\n") );
464 if ( !check_file_changed(current_configfile, configfile, &fs))
466 /* No need to load */
467 return ((struct configuration_spec *)current_configfile->f);
471 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
476 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
479 #ifdef FEATURE_TOGGLE
481 #endif /* def FEATURE_TOGGLE */
483 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
489 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
490 /* Never get here - LOG_LEVEL_FATAL causes program exit */
494 * This is backwards from how it's usually done.
495 * Following the usual pattern, "fs" would be stored in a member
496 * variable in "csp", and then we'd access "config" from "fs->f",
497 * using a cast. However, "config" is used so often that a
498 * cast each time would be very ugly, and the extra indirection
499 * would waste CPU cycles. Therefore we store "config" in
500 * "csp->config", and "fs" in "csp->config->config_file_list".
502 config->config_file_list = fs;
508 config->multi_threaded = 1;
509 config->hport = HADDR_PORT;
510 config->buffer_limit = 4096 * 1024;
511 config->proxy_args = strdup("");
513 if ((configfp = fopen(configfile, "r")) == NULL)
515 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
517 /* Never get here - LOG_LEVEL_FATAL causes program exit */
520 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
522 char cmd[BUFFER_SIZE];
523 char arg[BUFFER_SIZE];
524 char tmp[BUFFER_SIZE];
526 struct access_control_list *cur_acl;
527 #endif /* def FEATURE_ACL */
528 struct forward_spec *cur_fwd;
534 /* Copy command (i.e. up to space or tab) into cmd */
537 while (*p && (*p != ' ') && (*p != '\t'))
543 /* Skip over the whitespace in buf */
544 while (*p && ((*p == ' ') || (*p == '\t')))
549 /* Copy the argument into arg */
552 /* Should never happen, but check this anyway */
558 /* Make sure the command field is lower case */
563 *p = ijb_tolower(*p);
567 /* Save the argument for show-proxy-args */
568 savearg(cmd, arg, config);
571 switch( hash_string( cmd ) )
573 /****************************************************************************
574 * actionsfile actions-file-name
575 * In confdir by default
576 ****************************************************************************/
577 case hash_actions_file :
578 freez(config->actions_file);
579 config->actions_file = make_path(config->confdir, arg);
582 /****************************************************************************
583 * admin-address email-address
584 ****************************************************************************/
585 case hash_admin_address :
586 freez(config->admin_address);
587 config->admin_address = strdup(arg);
590 /****************************************************************************
592 ****************************************************************************/
593 case hash_buffer_limit :
594 config->buffer_limit = (size_t) 1024 * atoi(arg);
597 /****************************************************************************
598 * confdir directory-name
599 ****************************************************************************/
601 freez(config->confdir);
602 config->confdir = make_path( NULL, arg);
605 /****************************************************************************
607 * Specifies debug level, multiple values are ORed together.
608 ****************************************************************************/
610 config->debug |= atoi(arg);
613 /****************************************************************************
614 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
615 ****************************************************************************/
617 case hash_deny_access:
618 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
620 if ((vec_count != 1) && (vec_count != 2))
622 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
623 "deny-access directive in configuration file.");
624 string_append(&config->proxy_args,
625 "<br>\nWARNING: Wrong number of parameters for "
626 "deny-access directive in configuration file.<br><br>\n");
630 /* allocate a new node */
631 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
635 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
636 /* Never get here - LOG_LEVEL_FATAL causes program exit */
639 cur_acl->action = ACL_DENY;
641 if (acl_addr(vec[0], cur_acl->src) < 0)
643 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
644 "directive in configuration file: \"%s\"", vec[0]);
645 string_append(&config->proxy_args,
646 "<br>\nWARNING: Invalid source IP for deny-access directive"
647 " in configuration file: \"");
648 string_append(&config->proxy_args,
650 string_append(&config->proxy_args,
657 if (acl_addr(vec[1], cur_acl->dst) < 0)
659 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
660 "directive in configuration file: \"%s\"", vec[0]);
661 string_append(&config->proxy_args,
662 "<br>\nWARNING: Invalid destination IP for deny-access directive"
663 " in configuration file: \"");
664 string_append(&config->proxy_args,
666 string_append(&config->proxy_args,
674 * Add it to the list. Note we reverse the list to get the
675 * behaviour the user expects. With both the ACL and
676 * actions file, the last match wins. However, the internal
677 * implementations are different: The actions file is stored
678 * in the same order as the file, and scanned completely.
679 * With the ACL, we reverse the order as we load it, then
680 * when we scan it we stop as soon as we get a match.
682 cur_acl->next = config->acl;
683 config->acl = cur_acl;
686 #endif /* def FEATURE_ACL */
688 /****************************************************************************
689 * enable-edit-actions 0|1
690 ****************************************************************************/
691 #ifdef FEATURE_CGI_EDIT_ACTIONS
692 case hash_enable_edit_actions:
693 if ((*arg != '\0') && (0 != atoi(arg)))
695 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
699 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
702 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
704 /****************************************************************************
705 * enable-remote-toggle 0|1
706 ****************************************************************************/
707 #ifdef FEATURE_CGI_EDIT_ACTIONS
708 case hash_enable_remote_toggle:
709 if ((*arg != '\0') && (0 != atoi(arg)))
711 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
715 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
718 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
720 /****************************************************************************
721 * forward url-pattern (.|http-proxy-host[:port])
722 ****************************************************************************/
724 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
728 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
729 "directive in configuration file.");
730 string_append(&config->proxy_args,
731 "<br>\nWARNING: Wrong number of parameters for "
732 "forward directive in configuration file.");
736 /* allocate a new node */
737 cur_fwd = zalloc(sizeof(*cur_fwd));
740 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
741 /* Never get here - LOG_LEVEL_FATAL causes program exit */
745 cur_fwd->type = SOCKS_NONE;
747 /* Save the URL pattern */
748 if (create_url_spec(cur_fwd->url, vec[0]))
750 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
751 "directive in configuration file.");
752 string_append(&config->proxy_args,
753 "<br>\nWARNING: Bad URL specifier for "
754 "forward directive in configuration file.");
758 /* Parse the parent HTTP proxy host:port */
761 if (strcmp(p, ".") != 0)
763 cur_fwd->forward_host = strdup(p);
765 if ((p = strchr(cur_fwd->forward_host, ':')))
768 cur_fwd->forward_port = atoi(p);
771 if (cur_fwd->forward_port <= 0)
773 cur_fwd->forward_port = 8000;
778 cur_fwd->next = config->forward;
779 config->forward = cur_fwd;
783 /****************************************************************************
784 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
785 ****************************************************************************/
786 case hash_forward_socks4:
787 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
791 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
792 "forward-socks4 directive in configuration file.");
793 string_append(&config->proxy_args,
794 "<br>\nWARNING: Wrong number of parameters for "
795 "forward-socks4 directive in configuration file.");
799 /* allocate a new node */
800 cur_fwd = zalloc(sizeof(*cur_fwd));
803 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
804 /* Never get here - LOG_LEVEL_FATAL causes program exit */
808 cur_fwd->type = SOCKS_4;
810 /* Save the URL pattern */
811 if (create_url_spec(cur_fwd->url, vec[0]))
813 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
814 "directive in configuration file.");
815 string_append(&config->proxy_args,
816 "<br>\nWARNING: Bad URL specifier for "
817 "forward-socks4 directive in configuration file.");
821 /* Parse the SOCKS proxy host[:port] */
824 if (strcmp(p, ".") != 0)
826 cur_fwd->gateway_host = strdup(p);
828 if ((p = strchr(cur_fwd->gateway_host, ':')))
831 cur_fwd->gateway_port = atoi(p);
833 if (cur_fwd->gateway_port <= 0)
835 cur_fwd->gateway_port = 1080;
839 /* Parse the parent HTTP proxy host[:port] */
842 if (strcmp(p, ".") != 0)
844 cur_fwd->forward_host = strdup(p);
846 if ((p = strchr(cur_fwd->forward_host, ':')))
849 cur_fwd->forward_port = atoi(p);
852 if (cur_fwd->forward_port <= 0)
854 cur_fwd->forward_port = 8000;
859 cur_fwd->next = config->forward;
860 config->forward = cur_fwd;
864 /****************************************************************************
865 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
866 ****************************************************************************/
867 case hash_forward_socks4a:
868 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
872 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
873 "forward-socks4a directive in configuration file.");
874 string_append(&config->proxy_args,
875 "<br>\nWARNING: Wrong number of parameters for "
876 "forward-socks4a directive in configuration file.");
880 /* allocate a new node */
881 cur_fwd = zalloc(sizeof(*cur_fwd));
884 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
885 /* Never get here - LOG_LEVEL_FATAL causes program exit */
889 cur_fwd->type = SOCKS_4A;
891 /* Save the URL pattern */
892 if (create_url_spec(cur_fwd->url, vec[0]))
894 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
895 "directive in configuration file.");
896 string_append(&config->proxy_args,
897 "<br>\nWARNING: Bad URL specifier for "
898 "forward-socks4a directive in configuration file.");
902 /* Parse the SOCKS proxy host[:port] */
905 cur_fwd->gateway_host = strdup(p);
907 if ((p = strchr(cur_fwd->gateway_host, ':')))
910 cur_fwd->gateway_port = atoi(p);
912 if (cur_fwd->gateway_port <= 0)
914 cur_fwd->gateway_port = 1080;
917 /* Parse the parent HTTP proxy host[:port] */
920 if (strcmp(p, ".") != 0)
922 cur_fwd->forward_host = strdup(p);
924 if ((p = strchr(cur_fwd->forward_host, ':')))
927 cur_fwd->forward_port = atoi(p);
930 if (cur_fwd->forward_port <= 0)
932 cur_fwd->forward_port = 8000;
937 cur_fwd->next = config->forward;
938 config->forward = cur_fwd;
942 /****************************************************************************
943 * jarfile jar-file-name
944 * In logdir by default
945 ****************************************************************************/
946 #ifdef FEATURE_COOKIE_JAR
948 freez(config->jarfile);
949 config->jarfile = make_path(config->logdir, arg);
951 #endif /* def FEATURE_COOKIE_JAR */
953 /****************************************************************************
954 * listen-address [ip][:port]
955 ****************************************************************************/
956 case hash_listen_address :
957 freez(config->haddr);
958 config->haddr = strdup(arg);
961 /****************************************************************************
962 * logdir directory-name
963 ****************************************************************************/
965 freez(config->logdir);
966 config->logdir = make_path(NULL, arg);
969 /****************************************************************************
970 * logfile log-file-name
971 * In logdir by default
972 ****************************************************************************/
974 freez(config->logfile);
975 config->logfile = make_path(config->logdir, arg);
978 /****************************************************************************
979 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
980 ****************************************************************************/
982 case hash_permit_access:
983 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
985 if ((vec_count != 1) && (vec_count != 2))
987 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
988 "permit-access directive in configuration file.");
989 string_append(&config->proxy_args,
990 "<br>\nWARNING: Wrong number of parameters for "
991 "permit-access directive in configuration file.<br><br>\n");
996 /* allocate a new node */
997 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1001 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1002 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1005 cur_acl->action = ACL_PERMIT;
1007 if (acl_addr(vec[0], cur_acl->src) < 0)
1009 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1010 "directive in configuration file: \"%s\"", vec[0]);
1011 string_append(&config->proxy_args,
1012 "<br>\nWARNING: Invalid source IP for permit-access directive"
1013 " in configuration file: \"");
1014 string_append(&config->proxy_args,
1016 string_append(&config->proxy_args,
1023 if (acl_addr(vec[1], cur_acl->dst) < 0)
1025 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1026 "permit-access directive in configuration file: \"%s\"",
1028 string_append(&config->proxy_args,
1029 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1030 " in configuration file: \"");
1031 string_append(&config->proxy_args,
1033 string_append(&config->proxy_args,
1041 * Add it to the list. Note we reverse the list to get the
1042 * behaviour the user expects. With both the ACL and
1043 * actions file, the last match wins. However, the internal
1044 * implementations are different: The actions file is stored
1045 * in the same order as the file, and scanned completely.
1046 * With the ACL, we reverse the order as we load it, then
1047 * when we scan it we stop as soon as we get a match.
1049 cur_acl->next = config->acl;
1050 config->acl = cur_acl;
1053 #endif /* def FEATURE_ACL */
1055 /****************************************************************************
1056 * proxy-info-url url
1057 ****************************************************************************/
1058 case hash_proxy_info_url :
1059 freez(config->proxy_info_url);
1060 config->proxy_info_url = strdup(arg);
1063 /****************************************************************************
1064 * re_filterfile file-name
1065 * In confdir by default.
1066 ****************************************************************************/
1067 case hash_re_filterfile :
1068 freez(config->re_filterfile);
1069 config->re_filterfile = make_path(config->confdir, arg);
1072 /****************************************************************************
1074 ****************************************************************************/
1075 case hash_single_threaded :
1076 config->multi_threaded = 0;
1079 /****************************************************************************
1081 ****************************************************************************/
1082 #ifdef FEATURE_TOGGLE
1084 g_bToggleIJB = atoi(arg);
1086 #endif /* def FEATURE_TOGGLE */
1088 /****************************************************************************
1089 * trust-info-url url
1090 ****************************************************************************/
1091 #ifdef FEATURE_TRUST
1092 case hash_trust_info_url :
1093 enlist(config->trust_info, arg);
1095 #endif /* def FEATURE_TRUST */
1097 /****************************************************************************
1098 * trustfile filename
1099 * (In confdir by default.)
1100 ****************************************************************************/
1101 #ifdef FEATURE_TRUST
1102 case hash_trustfile :
1103 freez(config->trustfile);
1104 config->trustfile = make_path(config->confdir, arg);
1106 #endif /* def FEATURE_TRUST */
1109 /****************************************************************************
1110 * Win32 Console options:
1111 ****************************************************************************/
1113 /****************************************************************************
1115 ****************************************************************************/
1117 case hash_hide_console :
1120 #endif /*def _WIN_CONSOLE*/
1123 /****************************************************************************
1124 * Win32 GUI options:
1125 ****************************************************************************/
1127 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1128 /****************************************************************************
1129 * activity-animation (0|1)
1130 ****************************************************************************/
1131 case hash_activity_animation :
1132 g_bShowActivityAnimation = atoi(arg);
1135 /****************************************************************************
1136 * close-button-minimizes (0|1)
1137 ****************************************************************************/
1138 case hash_close_button_minimizes :
1139 g_bCloseHidesWindow = atoi(arg);
1142 /****************************************************************************
1143 * log-buffer-size (0|1)
1144 ****************************************************************************/
1145 case hash_log_buffer_size :
1146 g_bLimitBufferSize = atoi(arg);
1149 /****************************************************************************
1150 * log-font-name fontnane
1151 ****************************************************************************/
1152 case hash_log_font_name :
1153 strcpy( g_szFontFaceName, arg );
1156 /****************************************************************************
1158 ****************************************************************************/
1159 case hash_log_font_size :
1160 g_nFontSize = atoi(arg);
1163 /****************************************************************************
1164 * log-highlight-messages (0|1)
1165 ****************************************************************************/
1166 case hash_log_highlight_messages :
1167 g_bHighlightMessages = atoi(arg);
1170 /****************************************************************************
1172 ****************************************************************************/
1173 case hash_log_max_lines :
1174 g_nMaxBufferLines = atoi(arg);
1177 /****************************************************************************
1178 * log-messages (0|1)
1179 ****************************************************************************/
1180 case hash_log_messages :
1181 g_bLogMessages = atoi(arg);
1184 /****************************************************************************
1185 * show-on-task-bar (0|1)
1186 ****************************************************************************/
1187 case hash_show_on_task_bar :
1188 g_bShowOnTaskBar = atoi(arg);
1191 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1194 /****************************************************************************/
1195 /* Warnings about unsupported features */
1196 /****************************************************************************/
1198 case hash_deny_access:
1199 #endif /* ndef FEATURE_ACL */
1200 #ifndef FEATURE_CGI_EDIT_ACTIONS
1201 case hash_enable_edit_actions:
1202 case hash_enable_remote_toggle:
1203 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1204 #ifndef FEATURE_COOKIE_JAR
1206 #endif /* ndef FEATURE_COOKIE_JAR */
1208 case hash_permit_access:
1209 #endif /* ndef FEATURE_ACL */
1210 #ifndef FEATURE_TOGGLE
1212 #endif /* ndef FEATURE_TOGGLE */
1213 #ifndef FEATURE_TRUST
1214 case hash_trustfile :
1215 case hash_trust_info_url :
1216 #endif /* ndef FEATURE_TRUST */
1218 #ifndef _WIN_CONSOLE
1219 case hash_hide_console :
1220 #endif /* ndef _WIN_CONSOLE */
1222 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1223 case hash_activity_animation :
1224 case hash_close_button_minimizes :
1225 case hash_log_buffer_size :
1226 case hash_log_font_name :
1227 case hash_log_font_size :
1228 case hash_log_highlight_messages :
1229 case hash_log_max_lines :
1230 case hash_log_messages :
1231 case hash_show_on_task_bar :
1232 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1233 /* These warnings are annoying - so hide them. -- Jon */
1234 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1237 /****************************************************************************/
1239 /****************************************************************************/
1241 * I decided that I liked this better as a warning than an
1242 * error. To change back to an error, just change log level
1243 * to LOG_LEVEL_FATAL.
1245 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' in line %lu in "
1246 "configuration file (%s).", buf, linenum, configfile);
1247 /* log_error(LOG_LEVEL_ERROR, "Unrecognized directive (%luul) in "
1248 "configuration file: \"%s\"", hash_string( cmd ), buf);
1250 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1251 string_append(&config->proxy_args, buf);
1252 string_append(&config->proxy_args, "<br><br>\n");
1255 /****************************************************************************/
1256 } /* end switch( hash_string(cmd) ) */
1257 } /* end while ( read_config_line(...) ) */
1261 if (NULL == config->proxy_args)
1263 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1266 init_error_log(Argv[0], config->logfile, config->debug);
1268 if (config->actions_file)
1270 add_loader(load_actions_file, config);
1273 if (config->re_filterfile)
1275 add_loader(load_re_filterfile, config);
1278 #ifdef FEATURE_TRUST
1279 if (config->trustfile)
1281 add_loader(load_trustfile, config);
1283 #endif /* def FEATURE_TRUST */
1285 #ifdef FEATURE_COOKIE_JAR
1286 if ( NULL != config->jarfile )
1288 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1290 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1291 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1293 setbuf(config->jar, NULL);
1295 #endif /* def FEATURE_COOKIE_JAR */
1297 if ( NULL == config->haddr )
1299 config->haddr = strdup( HADDR_DEFAULT );
1302 if ( NULL != config->haddr )
1304 if ((p = strchr(config->haddr, ':')))
1309 config->hport = atoi(p);
1313 if (config->hport <= 0)
1316 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1317 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1319 if (*config->haddr == '\0')
1321 config->haddr = NULL;
1326 * Want to run all the loaders once now.
1328 * Need to set up a fake csp, so they can get to the config.
1330 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1331 fake_csp->config = config;
1333 if (run_loader(fake_csp))
1336 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1337 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1341 /* FIXME: this is a kludge for win32 */
1342 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1344 g_actions_file = config->actions_file;
1345 g_re_filterfile = config->re_filterfile;
1347 #ifdef FEATURE_TRUST
1348 g_trustfile = config->trustfile;
1349 #endif /* def FEATURE_TRUST */
1352 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1353 /* FIXME: end kludge */
1356 config->need_bind = 1;
1358 if (current_configfile)
1360 struct configuration_spec * oldcfg = (struct configuration_spec *)
1361 current_configfile->f;
1363 * Check if config->haddr,hport == oldcfg->haddr,hport
1365 * The following could be written more compactly as a single,
1366 * (unreadably long) if statement.
1368 config->need_bind = 0;
1369 if (config->hport != oldcfg->hport)
1371 config->need_bind = 1;
1373 else if (config->haddr == NULL)
1375 if (oldcfg->haddr != NULL)
1377 config->need_bind = 1;
1380 else if (oldcfg->haddr == NULL)
1382 config->need_bind = 1;
1384 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1386 config->need_bind = 1;
1389 current_configfile->unloader = unload_configfile;
1392 fs->next = files->next;
1395 current_configfile = fs;
1402 /*********************************************************************
1404 * Function : savearg
1406 * Description : Called from `load_config'. It saves each non-empty
1407 * and non-comment line from config into
1408 * config->proxy_args. This is used to create the
1409 * show-proxy-args page. On error, frees
1410 * config->proxy_args and sets it to NULL
1413 * 1 : command = config setting that was found
1414 * 2 : argument = the setting's argument (if any)
1418 *********************************************************************/
1419 static void savearg(char *command, char *argument, struct configuration_spec * config)
1430 s = html_encode(command);
1434 freez(config->proxy_args);
1437 string_append(&buf, "<a href=\"" REDIRECT_URL "option#");
1438 string_append(&buf, s);
1439 string_append(&buf, "\">");
1440 string_join (&buf, s);
1441 string_append(&buf, "</a> ");
1443 if ( (NULL != argument) && ('\0' != *argument) )
1445 s = html_encode(argument);
1449 freez(config->proxy_args);
1453 if (strncmpic(argument, "http://", 7) == 0)
1455 string_append(&buf, "<a href=\"");
1456 string_append(&buf, s);
1457 string_append(&buf, "\">");
1458 string_join (&buf, s);
1459 string_append(&buf, "</a>");
1463 string_join (&buf, s);
1467 string_append(&buf, "<br>\n");
1469 string_join(&config->proxy_args, buf);