1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.31 2002/03/03 15:07:20 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.31 2002/03/03 15:07:20 oes
39 * Re-enabled automatic config reloading
41 * Revision 1.30 2002/01/22 23:31:43 jongfoster
42 * Replacing strsav() with string_append()
44 * Revision 1.29 2002/01/17 21:02:30 jongfoster
45 * Moving all our URL and URL pattern parsing code to urlmatch.c.
47 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
49 * Revision 1.28 2001/12/30 14:07:32 steudten
50 * - Add signal handling (unix)
51 * - Add SIGHUP handler (unix)
52 * - Add creation of pidfile (unix)
53 * - Add action 'top' in rc file (RH)
54 * - Add entry 'SIGNALS' to manpage
55 * - Add exit message to logfile (unix)
57 * Revision 1.27 2001/11/07 00:02:13 steudten
58 * Add line number in error output for lineparsing for
59 * actionsfile and configfile.
60 * Special handling for CLF added.
62 * Revision 1.26 2001/11/05 21:41:43 steudten
63 * Add changes to be a real daemon just for unix os.
64 * (change cwd to /, detach from controlling tty, set
65 * process group and session leader to the own process.
67 * Add some fatal-error log message for failed malloc().
68 * Add '-d' if compiled with 'configure --with-debug' to
69 * enable debug output.
71 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
72 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
73 * threads to call select() simultaneously. So, it's time to do a real, live,
74 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
75 * (native). Both versions will work, but using __OS2__ offers multi-threading.
77 * Revision 1.24 2001/10/23 21:40:30 jongfoster
78 * Added support for enable-edit-actions and enable-remote-toggle config
81 * Revision 1.23 2001/10/07 15:36:00 oes
82 * Introduced new config option "buffer-limit"
84 * Revision 1.22 2001/09/22 16:36:59 jongfoster
85 * Removing unused parameter fs from read_config_line()
87 * Revision 1.21 2001/09/16 17:10:43 jongfoster
88 * Moving function savearg() here, since it was the only thing left in
91 * Revision 1.20 2001/07/30 22:08:36 jongfoster
92 * Tidying up #defines:
93 * - All feature #defines are now of the form FEATURE_xxx
94 * - Permanently turned off WIN_GUI_EDIT
95 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
97 * Revision 1.19 2001/07/15 17:45:16 jongfoster
98 * Removing some unused #includes
100 * Revision 1.18 2001/07/13 14:01:14 oes
101 * - Removed all #ifdef PCRS
102 * - Removed vim-settings
104 * Revision 1.17 2001/06/29 13:31:03 oes
105 * - Improved comments
106 * - Fixed (actionsfile) and sorted hashes
107 * - Introduced admin_address and proxy-info-url
108 * as config parameters
109 * - Renamed config->proxy_args_invocation (which didn't have
110 * the invocation but the options!) to config->proxy_args
111 * - Various adaptions
112 * - Removed logentry from cancelled commit
114 * Revision 1.16 2001/06/09 10:55:28 jongfoster
115 * Changing BUFSIZ ==> BUFFER_SIZE
117 * Revision 1.15 2001/06/07 23:13:40 jongfoster
118 * Merging ACL and forward files into config file.
119 * Cosmetic: Sorting config file options alphabetically.
120 * Cosmetic: Adding brief syntax comments to config file options.
122 * Revision 1.14 2001/06/07 14:46:25 joergs
123 * Missing make_path() added for re_filterfile.
125 * Revision 1.13 2001/06/05 22:33:54 jongfoster
127 * Fixed minor memory leak.
128 * Also now uses make_path to prepend the pathnames.
130 * Revision 1.12 2001/06/05 20:04:09 jongfoster
131 * Now uses _snprintf() in place of snprintf() under Win32.
133 * Revision 1.11 2001/06/04 18:31:58 swa
134 * files are now prefixed with either `confdir' or `logdir'.
135 * `make redhat-dist' replaces both entries confdir and logdir
138 * Revision 1.10 2001/06/03 19:11:54 oes
139 * introduced confdir option
141 * Revision 1.9 2001/06/01 20:06:24 jongfoster
142 * Removed support for "tinygif" option - moved to actions file.
144 * Revision 1.8 2001/05/31 21:27:13 jongfoster
145 * Removed many options from the config file and into the
146 * "actions" file: add_forwarded, suppress_vanilla_wafer,
147 * wafer, add_header, user_agent, referer, from
148 * Also globally replaced "permission" with "action".
150 * Revision 1.7 2001/05/29 09:50:24 jongfoster
151 * Unified blocklist/imagelist/permissionslist.
152 * File format is still under discussion, but the internal changes
155 * Also modified interceptor behaviour:
156 * - We now intercept all URLs beginning with one of the following
157 * prefixes (and *only* these prefixes):
159 * * http://ijbswa.sf.net/config/
160 * * http://ijbswa.sourceforge.net/config/
161 * - New interceptors "home page" - go to http://i.j.b/ to see it.
162 * - Internal changes so that intercepted and fast redirect pages
163 * are not replaced with an image.
164 * - Interceptors now have the option to send a binary page direct
165 * to the client. (i.e. ijb-send-banner uses this)
166 * - Implemented show-url-info interceptor. (Which is why I needed
167 * the above interceptors changes - a typical URL is
168 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
169 * The previous mechanism would not have intercepted that, and
170 * if it had been intercepted then it then it would have replaced
173 * Revision 1.6 2001/05/26 00:28:36 jongfoster
174 * Automatic reloading of config file.
175 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
176 * Most of the global variables have been moved to a new
177 * struct configuration_spec, accessed through csp->config->globalname
178 * Most of the globals remaining are used by the Win32 GUI.
180 * Revision 1.5 2001/05/25 22:34:30 jongfoster
183 * Revision 1.4 2001/05/22 18:46:04 oes
185 * - Enabled filtering banners by size rather than URL
186 * by adding patterns that replace all standard banner
187 * sizes with the "Junkbuster" gif to the re_filterfile
189 * - Enabled filtering WebBugs by providing a pattern
190 * which kills all 1x1 images
192 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
193 * which is selected by the (nonstandard and therefore
194 * capital) letter 'U' in the option string.
195 * It causes the quantifiers to be ungreedy by default.
196 * Appending a ? turns back to greedy (!).
198 * - Added a new interceptor ijb-send-banner, which
199 * sends back the "Junkbuster" gif. Without imagelist or
200 * MSIE detection support, or if tinygif = 1, or the
201 * URL isn't recognized as an imageurl, a lame HTML
202 * explanation is sent instead.
204 * - Added new feature, which permits blocking remote
205 * script redirects and firing back a local redirect
207 * The feature is conditionally compiled, i.e. it
208 * can be disabled with --disable-fast-redirects,
209 * plus it must be activated by a "fast-redirects"
210 * line in the config file, has its own log level
211 * and of course wants to be displayed by show-proxy-args
212 * Note: Boy, all the #ifdefs in 1001 locations and
213 * all the fumbling with configure.in and acconfig.h
214 * were *way* more work than the feature itself :-(
216 * - Because a generic redirect template was needed for
217 * this, tinygif = 3 now uses the same.
219 * - Moved GIFs, and other static HTTP response templates
224 * - Removed some >400 CRs again (Jon, you really worked
227 * Revision 1.3 2001/05/20 01:21:20 jongfoster
228 * Version 2.9.4 checkin.
229 * - Merged popupfile and cookiefile, and added control over PCRS
230 * filtering, in new "permissionsfile".
231 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
232 * file error you now get a message box (in the Win32 GUI) rather
233 * than the program exiting with no explanation.
234 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
236 * - Removed tabs from "config"
237 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
238 * - Bumped up version number.
240 * Revision 1.2 2001/05/17 23:01:01 oes
241 * - Cleaned CRLF's from the sources and related files
243 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
244 * Initial import of version 2.9.3 source tree
247 *********************************************************************/
253 #include <sys/types.h>
264 # include <windows.h>
267 # ifndef _WIN_CONSOLE
269 # endif /* ndef _WIN_CONSOLE */
271 /* VC++ has "_snprintf", not "snprintf" */
272 #define snprintf _snprintf
274 #else /* ifndef _WIN32 */
278 # include <sys/wait.h>
280 # include <sys/time.h>
281 # include <sys/stat.h>
291 #include "miscutil.h"
295 #include "urlmatch.h"
297 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
300 * Fix a problem with Solaris. There should be no effect on other
302 * Solaris's isspace() is a macro which uses it's argument directly
303 * as an array index. Therefore we need to make sure that high-bit
304 * characters generate +ve values, and ideally we also want to make
305 * the argument match the declared parameter type of "int".
307 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
308 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
310 #ifdef FEATURE_TOGGLE
311 /* by haroon - indicates if ijb is enabled */
312 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
313 #endif /* def FEATURE_TOGGLE */
315 /* The filename of the configfile */
316 const char *configfile = NULL;
319 * CGI functions will later need access to the invocation args,
320 * so we will make argc and argv global.
323 const char **Argv = NULL;
325 static struct file_list *current_configfile = NULL;
329 * This takes the "cryptic" hash of each keyword and aliases them to
330 * something a little more readable. This also makes changing the
331 * hash values easier if they should change or the hash algorthm changes.
332 * Use the included "hash" program to find out what the hash will be
333 * for any string supplied on the command line. (Or just put it in the
334 * config file and read the number from the error message in the log).
336 * Please keep this list sorted alphabetically (but with the Windows
337 * console and GUI specific options last).
340 #define hash_actions_file 1196306641ul /* "actionsfile" */
341 #define hash_admin_address 4112573064ul /* "admin-address" */
342 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
343 #define hash_confdir 1978389ul /* "confdir" */
344 #define hash_debug 78263ul /* "debug" */
345 #define hash_deny_access 1227333715ul /* "deny-access" */
346 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
347 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
348 #define hash_forward 2029845ul /* "forward" */
349 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
350 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
351 #define hash_jarfile 2046641ul /* "jarfile" */
352 #define hash_listen_address 1255650842ul /* "listen-address" */
353 #define hash_logdir 422889ul /* "logdir" */
354 #define hash_logfile 2114766ul /* "logfile" */
355 #define hash_permit_access 3587953268ul /* "permit-access" */
356 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
357 #define hash_re_filterfile 3877522444ul /* "re_filterfile" */
358 #define hash_single_threaded 4250084780ul /* "single-threaded" */
359 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
360 #define hash_toggle 447966ul /* "toggle" */
361 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
362 #define hash_trustfile 56494766ul /* "trustfile" */
364 #define hash_activity_animation 1817904738ul /* "activity-animation" */
365 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
366 #define hash_hide_console 2048809870ul /* "hide-console" */
367 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
368 #define hash_log_font_name 2866730124ul /* "log-font-name" */
369 #define hash_log_font_size 2866731014ul /* "log-font-size" */
370 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
371 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
372 #define hash_log_messages 2291744899ul /* "log-messages" */
373 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
376 static void savearg(char *command, char *argument, struct configuration_spec * config);
378 /*********************************************************************
380 * Function : unload_configfile
382 * Description : Free the config structure and all components.
385 * 1 : data: struct configuration_spec to unload
389 *********************************************************************/
390 void unload_configfile (void * data)
392 struct configuration_spec * config = (struct configuration_spec *)data;
393 struct forward_spec *cur_fwd = config->forward;
395 struct access_control_list *cur_acl = config->acl;
397 while (cur_acl != NULL)
399 struct access_control_list * next_acl = cur_acl->next;
404 #endif /* def FEATURE_ACL */
406 while (cur_fwd != NULL)
408 struct forward_spec * next_fwd = cur_fwd->next;
409 free_url_spec(cur_fwd->url);
411 freez(cur_fwd->gateway_host);
412 freez(cur_fwd->forward_host);
416 config->forward = NULL;
418 #ifdef FEATURE_COOKIE_JAR
419 if ( NULL != config->jar )
421 fclose( config->jar );
424 #endif /* def FEATURE_COOKIE_JAR */
426 freez(config->confdir);
427 freez(config->logdir);
429 freez(config->haddr);
430 freez(config->logfile);
432 freez(config->actions_file);
433 freez(config->admin_address);
434 freez(config->proxy_info_url);
435 freez(config->proxy_args);
437 #ifdef FEATURE_COOKIE_JAR
438 freez(config->jarfile);
439 #endif /* def FEATURE_COOKIE_JAR */
441 freez(config->re_filterfile);
446 /*********************************************************************
448 * Function : load_config
450 * Description : Load the config file and all parameters.
453 * 1 : csp = Client state (the config member will be
454 * filled in by this function).
456 * Returns : 0 => Ok, everything else is an error.
458 *********************************************************************/
459 struct configuration_spec * load_config(void)
461 char buf[BUFFER_SIZE];
463 FILE *configfp = NULL;
464 struct configuration_spec * config = NULL;
465 struct client_state * fake_csp;
466 struct file_list *fs;
467 unsigned long linenum = 0;
469 DBG(1, ("load_config() entered..\n") );
470 if ( !check_file_changed(current_configfile, configfile, &fs))
472 /* No need to load */
473 return ((struct configuration_spec *)current_configfile->f);
477 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
481 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
483 #ifdef FEATURE_TOGGLE
485 #endif /* def FEATURE_TOGGLE */
487 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
493 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
494 /* Never get here - LOG_LEVEL_FATAL causes program exit */
498 * This is backwards from how it's usually done.
499 * Following the usual pattern, "fs" would be stored in a member
500 * variable in "csp", and then we'd access "config" from "fs->f",
501 * using a cast. However, "config" is used so often that a
502 * cast each time would be very ugly, and the extra indirection
503 * would waste CPU cycles. Therefore we store "config" in
504 * "csp->config", and "fs" in "csp->config->config_file_list".
506 config->config_file_list = fs;
512 config->multi_threaded = 1;
513 config->hport = HADDR_PORT;
514 config->buffer_limit = 4096 * 1024;
515 config->proxy_args = strdup("");
517 if ((configfp = fopen(configfile, "r")) == NULL)
519 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
521 /* Never get here - LOG_LEVEL_FATAL causes program exit */
524 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
526 char cmd[BUFFER_SIZE];
527 char arg[BUFFER_SIZE];
528 char tmp[BUFFER_SIZE];
530 struct access_control_list *cur_acl;
531 #endif /* def FEATURE_ACL */
532 struct forward_spec *cur_fwd;
538 /* Copy command (i.e. up to space or tab) into cmd */
541 while (*p && (*p != ' ') && (*p != '\t'))
547 /* Skip over the whitespace in buf */
548 while (*p && ((*p == ' ') || (*p == '\t')))
553 /* Copy the argument into arg */
556 /* Should never happen, but check this anyway */
562 /* Make sure the command field is lower case */
567 *p = ijb_tolower(*p);
571 /* Save the argument for show-proxy-args */
572 savearg(cmd, arg, config);
575 switch( hash_string( cmd ) )
577 /****************************************************************************
578 * actionsfile actions-file-name
579 * In confdir by default
580 ****************************************************************************/
581 case hash_actions_file :
582 freez(config->actions_file);
583 config->actions_file = make_path(config->confdir, arg);
586 /****************************************************************************
587 * admin-address email-address
588 ****************************************************************************/
589 case hash_admin_address :
590 freez(config->admin_address);
591 config->admin_address = strdup(arg);
594 /****************************************************************************
596 ****************************************************************************/
597 case hash_buffer_limit :
598 config->buffer_limit = (size_t) 1024 * atoi(arg);
601 /****************************************************************************
602 * confdir directory-name
603 ****************************************************************************/
605 freez(config->confdir);
606 config->confdir = make_path( NULL, arg);
609 /****************************************************************************
611 * Specifies debug level, multiple values are ORed together.
612 ****************************************************************************/
614 config->debug |= atoi(arg);
617 /****************************************************************************
618 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
619 ****************************************************************************/
621 case hash_deny_access:
622 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
624 if ((vec_count != 1) && (vec_count != 2))
626 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
627 "deny-access directive in configuration file.");
628 string_append(&config->proxy_args,
629 "<br>\nWARNING: Wrong number of parameters for "
630 "deny-access directive in configuration file.<br><br>\n");
634 /* allocate a new node */
635 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
639 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
640 /* Never get here - LOG_LEVEL_FATAL causes program exit */
643 cur_acl->action = ACL_DENY;
645 if (acl_addr(vec[0], cur_acl->src) < 0)
647 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
648 "directive in configuration file: \"%s\"", vec[0]);
649 string_append(&config->proxy_args,
650 "<br>\nWARNING: Invalid source IP for deny-access directive"
651 " in configuration file: \"");
652 string_append(&config->proxy_args,
654 string_append(&config->proxy_args,
661 if (acl_addr(vec[1], cur_acl->dst) < 0)
663 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
664 "directive in configuration file: \"%s\"", vec[0]);
665 string_append(&config->proxy_args,
666 "<br>\nWARNING: Invalid destination IP for deny-access directive"
667 " in configuration file: \"");
668 string_append(&config->proxy_args,
670 string_append(&config->proxy_args,
678 * Add it to the list. Note we reverse the list to get the
679 * behaviour the user expects. With both the ACL and
680 * actions file, the last match wins. However, the internal
681 * implementations are different: The actions file is stored
682 * in the same order as the file, and scanned completely.
683 * With the ACL, we reverse the order as we load it, then
684 * when we scan it we stop as soon as we get a match.
686 cur_acl->next = config->acl;
687 config->acl = cur_acl;
690 #endif /* def FEATURE_ACL */
692 /****************************************************************************
693 * enable-edit-actions 0|1
694 ****************************************************************************/
695 #ifdef FEATURE_CGI_EDIT_ACTIONS
696 case hash_enable_edit_actions:
697 if ((*arg != '\0') && (0 != atoi(arg)))
699 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
703 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
706 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
708 /****************************************************************************
709 * enable-remote-toggle 0|1
710 ****************************************************************************/
711 #ifdef FEATURE_CGI_EDIT_ACTIONS
712 case hash_enable_remote_toggle:
713 if ((*arg != '\0') && (0 != atoi(arg)))
715 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
719 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
722 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
724 /****************************************************************************
725 * forward url-pattern (.|http-proxy-host[:port])
726 ****************************************************************************/
728 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
732 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
733 "directive in configuration file.");
734 string_append(&config->proxy_args,
735 "<br>\nWARNING: Wrong number of parameters for "
736 "forward directive in configuration file.");
740 /* allocate a new node */
741 cur_fwd = zalloc(sizeof(*cur_fwd));
744 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
745 /* Never get here - LOG_LEVEL_FATAL causes program exit */
749 cur_fwd->type = SOCKS_NONE;
751 /* Save the URL pattern */
752 if (create_url_spec(cur_fwd->url, vec[0]))
754 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
755 "directive in configuration file.");
756 string_append(&config->proxy_args,
757 "<br>\nWARNING: Bad URL specifier for "
758 "forward directive in configuration file.");
762 /* Parse the parent HTTP proxy host:port */
765 if (strcmp(p, ".") != 0)
767 cur_fwd->forward_host = strdup(p);
769 if ((p = strchr(cur_fwd->forward_host, ':')))
772 cur_fwd->forward_port = atoi(p);
775 if (cur_fwd->forward_port <= 0)
777 cur_fwd->forward_port = 8000;
782 cur_fwd->next = config->forward;
783 config->forward = cur_fwd;
787 /****************************************************************************
788 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
789 ****************************************************************************/
790 case hash_forward_socks4:
791 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
795 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
796 "forward-socks4 directive in configuration file.");
797 string_append(&config->proxy_args,
798 "<br>\nWARNING: Wrong number of parameters for "
799 "forward-socks4 directive in configuration file.");
803 /* allocate a new node */
804 cur_fwd = zalloc(sizeof(*cur_fwd));
807 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
808 /* Never get here - LOG_LEVEL_FATAL causes program exit */
812 cur_fwd->type = SOCKS_4;
814 /* Save the URL pattern */
815 if (create_url_spec(cur_fwd->url, vec[0]))
817 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
818 "directive in configuration file.");
819 string_append(&config->proxy_args,
820 "<br>\nWARNING: Bad URL specifier for "
821 "forward-socks4 directive in configuration file.");
825 /* Parse the SOCKS proxy host[:port] */
828 if (strcmp(p, ".") != 0)
830 cur_fwd->gateway_host = strdup(p);
832 if ((p = strchr(cur_fwd->gateway_host, ':')))
835 cur_fwd->gateway_port = atoi(p);
837 if (cur_fwd->gateway_port <= 0)
839 cur_fwd->gateway_port = 1080;
843 /* Parse the parent HTTP proxy host[:port] */
846 if (strcmp(p, ".") != 0)
848 cur_fwd->forward_host = strdup(p);
850 if ((p = strchr(cur_fwd->forward_host, ':')))
853 cur_fwd->forward_port = atoi(p);
856 if (cur_fwd->forward_port <= 0)
858 cur_fwd->forward_port = 8000;
863 cur_fwd->next = config->forward;
864 config->forward = cur_fwd;
868 /****************************************************************************
869 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
870 ****************************************************************************/
871 case hash_forward_socks4a:
872 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
876 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
877 "forward-socks4a directive in configuration file.");
878 string_append(&config->proxy_args,
879 "<br>\nWARNING: Wrong number of parameters for "
880 "forward-socks4a directive in configuration file.");
884 /* allocate a new node */
885 cur_fwd = zalloc(sizeof(*cur_fwd));
888 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
889 /* Never get here - LOG_LEVEL_FATAL causes program exit */
893 cur_fwd->type = SOCKS_4A;
895 /* Save the URL pattern */
896 if (create_url_spec(cur_fwd->url, vec[0]))
898 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
899 "directive in configuration file.");
900 string_append(&config->proxy_args,
901 "<br>\nWARNING: Bad URL specifier for "
902 "forward-socks4a directive in configuration file.");
906 /* Parse the SOCKS proxy host[:port] */
909 cur_fwd->gateway_host = strdup(p);
911 if ((p = strchr(cur_fwd->gateway_host, ':')))
914 cur_fwd->gateway_port = atoi(p);
916 if (cur_fwd->gateway_port <= 0)
918 cur_fwd->gateway_port = 1080;
921 /* Parse the parent HTTP proxy host[:port] */
924 if (strcmp(p, ".") != 0)
926 cur_fwd->forward_host = strdup(p);
928 if ((p = strchr(cur_fwd->forward_host, ':')))
931 cur_fwd->forward_port = atoi(p);
934 if (cur_fwd->forward_port <= 0)
936 cur_fwd->forward_port = 8000;
941 cur_fwd->next = config->forward;
942 config->forward = cur_fwd;
946 /****************************************************************************
947 * jarfile jar-file-name
948 * In logdir by default
949 ****************************************************************************/
950 #ifdef FEATURE_COOKIE_JAR
952 freez(config->jarfile);
953 config->jarfile = make_path(config->logdir, arg);
955 #endif /* def FEATURE_COOKIE_JAR */
957 /****************************************************************************
958 * listen-address [ip][:port]
959 ****************************************************************************/
960 case hash_listen_address :
961 freez(config->haddr);
962 config->haddr = strdup(arg);
965 /****************************************************************************
966 * logdir directory-name
967 ****************************************************************************/
969 freez(config->logdir);
970 config->logdir = make_path(NULL, arg);
973 /****************************************************************************
974 * logfile log-file-name
975 * In logdir by default
976 ****************************************************************************/
978 freez(config->logfile);
979 config->logfile = make_path(config->logdir, arg);
982 /****************************************************************************
983 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
984 ****************************************************************************/
986 case hash_permit_access:
987 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
989 if ((vec_count != 1) && (vec_count != 2))
991 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
992 "permit-access directive in configuration file.");
993 string_append(&config->proxy_args,
994 "<br>\nWARNING: Wrong number of parameters for "
995 "permit-access directive in configuration file.<br><br>\n");
1000 /* allocate a new node */
1001 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1003 if (cur_acl == NULL)
1005 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1006 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1009 cur_acl->action = ACL_PERMIT;
1011 if (acl_addr(vec[0], cur_acl->src) < 0)
1013 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1014 "directive in configuration file: \"%s\"", vec[0]);
1015 string_append(&config->proxy_args,
1016 "<br>\nWARNING: Invalid source IP for permit-access directive"
1017 " in configuration file: \"");
1018 string_append(&config->proxy_args,
1020 string_append(&config->proxy_args,
1027 if (acl_addr(vec[1], cur_acl->dst) < 0)
1029 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1030 "permit-access directive in configuration file: \"%s\"",
1032 string_append(&config->proxy_args,
1033 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1034 " in configuration file: \"");
1035 string_append(&config->proxy_args,
1037 string_append(&config->proxy_args,
1045 * Add it to the list. Note we reverse the list to get the
1046 * behaviour the user expects. With both the ACL and
1047 * actions file, the last match wins. However, the internal
1048 * implementations are different: The actions file is stored
1049 * in the same order as the file, and scanned completely.
1050 * With the ACL, we reverse the order as we load it, then
1051 * when we scan it we stop as soon as we get a match.
1053 cur_acl->next = config->acl;
1054 config->acl = cur_acl;
1057 #endif /* def FEATURE_ACL */
1059 /****************************************************************************
1060 * proxy-info-url url
1061 ****************************************************************************/
1062 case hash_proxy_info_url :
1063 freez(config->proxy_info_url);
1064 config->proxy_info_url = strdup(arg);
1067 /****************************************************************************
1068 * re_filterfile file-name
1069 * In confdir by default.
1070 ****************************************************************************/
1071 case hash_re_filterfile :
1072 freez(config->re_filterfile);
1073 config->re_filterfile = make_path(config->confdir, arg);
1076 /****************************************************************************
1078 ****************************************************************************/
1079 case hash_single_threaded :
1080 config->multi_threaded = 0;
1083 /****************************************************************************
1085 ****************************************************************************/
1086 #ifdef FEATURE_TOGGLE
1088 g_bToggleIJB = atoi(arg);
1090 #endif /* def FEATURE_TOGGLE */
1092 /****************************************************************************
1093 * trust-info-url url
1094 ****************************************************************************/
1095 #ifdef FEATURE_TRUST
1096 case hash_trust_info_url :
1097 enlist(config->trust_info, arg);
1099 #endif /* def FEATURE_TRUST */
1101 /****************************************************************************
1102 * trustfile filename
1103 * (In confdir by default.)
1104 ****************************************************************************/
1105 #ifdef FEATURE_TRUST
1106 case hash_trustfile :
1107 freez(config->trustfile);
1108 config->trustfile = make_path(config->confdir, arg);
1110 #endif /* def FEATURE_TRUST */
1113 /****************************************************************************
1114 * Win32 Console options:
1115 ****************************************************************************/
1117 /****************************************************************************
1119 ****************************************************************************/
1121 case hash_hide_console :
1124 #endif /*def _WIN_CONSOLE*/
1127 /****************************************************************************
1128 * Win32 GUI options:
1129 ****************************************************************************/
1131 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1132 /****************************************************************************
1133 * activity-animation (0|1)
1134 ****************************************************************************/
1135 case hash_activity_animation :
1136 g_bShowActivityAnimation = atoi(arg);
1139 /****************************************************************************
1140 * close-button-minimizes (0|1)
1141 ****************************************************************************/
1142 case hash_close_button_minimizes :
1143 g_bCloseHidesWindow = atoi(arg);
1146 /****************************************************************************
1147 * log-buffer-size (0|1)
1148 ****************************************************************************/
1149 case hash_log_buffer_size :
1150 g_bLimitBufferSize = atoi(arg);
1153 /****************************************************************************
1154 * log-font-name fontnane
1155 ****************************************************************************/
1156 case hash_log_font_name :
1157 strcpy( g_szFontFaceName, arg );
1160 /****************************************************************************
1162 ****************************************************************************/
1163 case hash_log_font_size :
1164 g_nFontSize = atoi(arg);
1167 /****************************************************************************
1168 * log-highlight-messages (0|1)
1169 ****************************************************************************/
1170 case hash_log_highlight_messages :
1171 g_bHighlightMessages = atoi(arg);
1174 /****************************************************************************
1176 ****************************************************************************/
1177 case hash_log_max_lines :
1178 g_nMaxBufferLines = atoi(arg);
1181 /****************************************************************************
1182 * log-messages (0|1)
1183 ****************************************************************************/
1184 case hash_log_messages :
1185 g_bLogMessages = atoi(arg);
1188 /****************************************************************************
1189 * show-on-task-bar (0|1)
1190 ****************************************************************************/
1191 case hash_show_on_task_bar :
1192 g_bShowOnTaskBar = atoi(arg);
1195 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1198 /****************************************************************************/
1199 /* Warnings about unsupported features */
1200 /****************************************************************************/
1202 case hash_deny_access:
1203 #endif /* ndef FEATURE_ACL */
1204 #ifndef FEATURE_CGI_EDIT_ACTIONS
1205 case hash_enable_edit_actions:
1206 case hash_enable_remote_toggle:
1207 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1208 #ifndef FEATURE_COOKIE_JAR
1210 #endif /* ndef FEATURE_COOKIE_JAR */
1212 case hash_permit_access:
1213 #endif /* ndef FEATURE_ACL */
1214 #ifndef FEATURE_TOGGLE
1216 #endif /* ndef FEATURE_TOGGLE */
1217 #ifndef FEATURE_TRUST
1218 case hash_trustfile :
1219 case hash_trust_info_url :
1220 #endif /* ndef FEATURE_TRUST */
1222 #ifndef _WIN_CONSOLE
1223 case hash_hide_console :
1224 #endif /* ndef _WIN_CONSOLE */
1226 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1227 case hash_activity_animation :
1228 case hash_close_button_minimizes :
1229 case hash_log_buffer_size :
1230 case hash_log_font_name :
1231 case hash_log_font_size :
1232 case hash_log_highlight_messages :
1233 case hash_log_max_lines :
1234 case hash_log_messages :
1235 case hash_show_on_task_bar :
1236 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1237 /* These warnings are annoying - so hide them. -- Jon */
1238 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1241 /****************************************************************************/
1243 /****************************************************************************/
1245 * I decided that I liked this better as a warning than an
1246 * error. To change back to an error, just change log level
1247 * to LOG_LEVEL_FATAL.
1249 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1250 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1251 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1252 string_append(&config->proxy_args, buf);
1253 string_append(&config->proxy_args, "<br><br>\n");
1256 /****************************************************************************/
1257 } /* end switch( hash_string(cmd) ) */
1258 } /* end while ( read_config_line(...) ) */
1262 if (NULL == config->proxy_args)
1264 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1267 init_error_log(Argv[0], config->logfile, config->debug);
1269 if (config->actions_file)
1271 add_loader(load_actions_file, config);
1274 if (config->re_filterfile)
1276 add_loader(load_re_filterfile, config);
1279 #ifdef FEATURE_TRUST
1280 if (config->trustfile)
1282 add_loader(load_trustfile, config);
1284 #endif /* def FEATURE_TRUST */
1286 #ifdef FEATURE_COOKIE_JAR
1287 if ( NULL != config->jarfile )
1289 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1291 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1292 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1294 setbuf(config->jar, NULL);
1296 #endif /* def FEATURE_COOKIE_JAR */
1298 if ( NULL == config->haddr )
1300 config->haddr = strdup( HADDR_DEFAULT );
1303 if ( NULL != config->haddr )
1305 if ((p = strchr(config->haddr, ':')))
1310 config->hport = atoi(p);
1314 if (config->hport <= 0)
1317 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1318 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1320 if (*config->haddr == '\0')
1322 config->haddr = NULL;
1327 * Want to run all the loaders once now.
1329 * Need to set up a fake csp, so they can get to the config.
1331 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1332 fake_csp->config = config;
1334 if (run_loader(fake_csp))
1337 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1338 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1342 /* FIXME: this is a kludge for win32 */
1343 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1345 g_actions_file = config->actions_file;
1346 g_re_filterfile = config->re_filterfile;
1348 #ifdef FEATURE_TRUST
1349 g_trustfile = config->trustfile;
1350 #endif /* def FEATURE_TRUST */
1353 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1354 /* FIXME: end kludge */
1357 config->need_bind = 1;
1359 if (current_configfile)
1361 struct configuration_spec * oldcfg = (struct configuration_spec *)
1362 current_configfile->f;
1364 * Check if config->haddr,hport == oldcfg->haddr,hport
1366 * The following could be written more compactly as a single,
1367 * (unreadably long) if statement.
1369 config->need_bind = 0;
1370 if (config->hport != oldcfg->hport)
1372 config->need_bind = 1;
1374 else if (config->haddr == NULL)
1376 if (oldcfg->haddr != NULL)
1378 config->need_bind = 1;
1381 else if (oldcfg->haddr == NULL)
1383 config->need_bind = 1;
1385 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1387 config->need_bind = 1;
1390 current_configfile->unloader = unload_configfile;
1393 fs->next = files->next;
1396 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);