1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.30 2002/01/22 23:31:43 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.30 2002/01/22 23:31:43 jongfoster
39 * Replacing strsav() with string_append()
41 * Revision 1.29 2002/01/17 21:02:30 jongfoster
42 * Moving all our URL and URL pattern parsing code to urlmatch.c.
44 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
46 * Revision 1.28 2001/12/30 14:07:32 steudten
47 * - Add signal handling (unix)
48 * - Add SIGHUP handler (unix)
49 * - Add creation of pidfile (unix)
50 * - Add action 'top' in rc file (RH)
51 * - Add entry 'SIGNALS' to manpage
52 * - Add exit message to logfile (unix)
54 * Revision 1.27 2001/11/07 00:02:13 steudten
55 * Add line number in error output for lineparsing for
56 * actionsfile and configfile.
57 * Special handling for CLF added.
59 * Revision 1.26 2001/11/05 21:41:43 steudten
60 * Add changes to be a real daemon just for unix os.
61 * (change cwd to /, detach from controlling tty, set
62 * process group and session leader to the own process.
64 * Add some fatal-error log message for failed malloc().
65 * Add '-d' if compiled with 'configure --with-debug' to
66 * enable debug output.
68 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
69 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
70 * threads to call select() simultaneously. So, it's time to do a real, live,
71 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
72 * (native). Both versions will work, but using __OS2__ offers multi-threading.
74 * Revision 1.24 2001/10/23 21:40:30 jongfoster
75 * Added support for enable-edit-actions and enable-remote-toggle config
78 * Revision 1.23 2001/10/07 15:36:00 oes
79 * Introduced new config option "buffer-limit"
81 * Revision 1.22 2001/09/22 16:36:59 jongfoster
82 * Removing unused parameter fs from read_config_line()
84 * Revision 1.21 2001/09/16 17:10:43 jongfoster
85 * Moving function savearg() here, since it was the only thing left in
88 * Revision 1.20 2001/07/30 22:08:36 jongfoster
89 * Tidying up #defines:
90 * - All feature #defines are now of the form FEATURE_xxx
91 * - Permanently turned off WIN_GUI_EDIT
92 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
94 * Revision 1.19 2001/07/15 17:45:16 jongfoster
95 * Removing some unused #includes
97 * Revision 1.18 2001/07/13 14:01:14 oes
98 * - Removed all #ifdef PCRS
99 * - Removed vim-settings
101 * Revision 1.17 2001/06/29 13:31:03 oes
102 * - Improved comments
103 * - Fixed (actionsfile) and sorted hashes
104 * - Introduced admin_address and proxy-info-url
105 * as config parameters
106 * - Renamed config->proxy_args_invocation (which didn't have
107 * the invocation but the options!) to config->proxy_args
108 * - Various adaptions
109 * - Removed logentry from cancelled commit
111 * Revision 1.16 2001/06/09 10:55:28 jongfoster
112 * Changing BUFSIZ ==> BUFFER_SIZE
114 * Revision 1.15 2001/06/07 23:13:40 jongfoster
115 * Merging ACL and forward files into config file.
116 * Cosmetic: Sorting config file options alphabetically.
117 * Cosmetic: Adding brief syntax comments to config file options.
119 * Revision 1.14 2001/06/07 14:46:25 joergs
120 * Missing make_path() added for re_filterfile.
122 * Revision 1.13 2001/06/05 22:33:54 jongfoster
124 * Fixed minor memory leak.
125 * Also now uses make_path to prepend the pathnames.
127 * Revision 1.12 2001/06/05 20:04:09 jongfoster
128 * Now uses _snprintf() in place of snprintf() under Win32.
130 * Revision 1.11 2001/06/04 18:31:58 swa
131 * files are now prefixed with either `confdir' or `logdir'.
132 * `make redhat-dist' replaces both entries confdir and logdir
135 * Revision 1.10 2001/06/03 19:11:54 oes
136 * introduced confdir option
138 * Revision 1.9 2001/06/01 20:06:24 jongfoster
139 * Removed support for "tinygif" option - moved to actions file.
141 * Revision 1.8 2001/05/31 21:27:13 jongfoster
142 * Removed many options from the config file and into the
143 * "actions" file: add_forwarded, suppress_vanilla_wafer,
144 * wafer, add_header, user_agent, referer, from
145 * Also globally replaced "permission" with "action".
147 * Revision 1.7 2001/05/29 09:50:24 jongfoster
148 * Unified blocklist/imagelist/permissionslist.
149 * File format is still under discussion, but the internal changes
152 * Also modified interceptor behaviour:
153 * - We now intercept all URLs beginning with one of the following
154 * prefixes (and *only* these prefixes):
156 * * http://ijbswa.sf.net/config/
157 * * http://ijbswa.sourceforge.net/config/
158 * - New interceptors "home page" - go to http://i.j.b/ to see it.
159 * - Internal changes so that intercepted and fast redirect pages
160 * are not replaced with an image.
161 * - Interceptors now have the option to send a binary page direct
162 * to the client. (i.e. ijb-send-banner uses this)
163 * - Implemented show-url-info interceptor. (Which is why I needed
164 * the above interceptors changes - a typical URL is
165 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
166 * The previous mechanism would not have intercepted that, and
167 * if it had been intercepted then it then it would have replaced
170 * Revision 1.6 2001/05/26 00:28:36 jongfoster
171 * Automatic reloading of config file.
172 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
173 * Most of the global variables have been moved to a new
174 * struct configuration_spec, accessed through csp->config->globalname
175 * Most of the globals remaining are used by the Win32 GUI.
177 * Revision 1.5 2001/05/25 22:34:30 jongfoster
180 * Revision 1.4 2001/05/22 18:46:04 oes
182 * - Enabled filtering banners by size rather than URL
183 * by adding patterns that replace all standard banner
184 * sizes with the "Junkbuster" gif to the re_filterfile
186 * - Enabled filtering WebBugs by providing a pattern
187 * which kills all 1x1 images
189 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
190 * which is selected by the (nonstandard and therefore
191 * capital) letter 'U' in the option string.
192 * It causes the quantifiers to be ungreedy by default.
193 * Appending a ? turns back to greedy (!).
195 * - Added a new interceptor ijb-send-banner, which
196 * sends back the "Junkbuster" gif. Without imagelist or
197 * MSIE detection support, or if tinygif = 1, or the
198 * URL isn't recognized as an imageurl, a lame HTML
199 * explanation is sent instead.
201 * - Added new feature, which permits blocking remote
202 * script redirects and firing back a local redirect
204 * The feature is conditionally compiled, i.e. it
205 * can be disabled with --disable-fast-redirects,
206 * plus it must be activated by a "fast-redirects"
207 * line in the config file, has its own log level
208 * and of course wants to be displayed by show-proxy-args
209 * Note: Boy, all the #ifdefs in 1001 locations and
210 * all the fumbling with configure.in and acconfig.h
211 * were *way* more work than the feature itself :-(
213 * - Because a generic redirect template was needed for
214 * this, tinygif = 3 now uses the same.
216 * - Moved GIFs, and other static HTTP response templates
221 * - Removed some >400 CRs again (Jon, you really worked
224 * Revision 1.3 2001/05/20 01:21:20 jongfoster
225 * Version 2.9.4 checkin.
226 * - Merged popupfile and cookiefile, and added control over PCRS
227 * filtering, in new "permissionsfile".
228 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
229 * file error you now get a message box (in the Win32 GUI) rather
230 * than the program exiting with no explanation.
231 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
233 * - Removed tabs from "config"
234 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
235 * - Bumped up version number.
237 * Revision 1.2 2001/05/17 23:01:01 oes
238 * - Cleaned CRLF's from the sources and related files
240 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
241 * Initial import of version 2.9.3 source tree
244 *********************************************************************/
250 #include <sys/types.h>
261 # include <windows.h>
264 # ifndef _WIN_CONSOLE
266 # endif /* ndef _WIN_CONSOLE */
268 /* VC++ has "_snprintf", not "snprintf" */
269 #define snprintf _snprintf
271 #else /* ifndef _WIN32 */
275 # include <sys/wait.h>
277 # include <sys/time.h>
278 # include <sys/stat.h>
288 #include "miscutil.h"
292 #include "urlmatch.h"
294 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
297 * Fix a problem with Solaris. There should be no effect on other
299 * Solaris's isspace() is a macro which uses it's argument directly
300 * as an array index. Therefore we need to make sure that high-bit
301 * characters generate +ve values, and ideally we also want to make
302 * the argument match the declared parameter type of "int".
304 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
305 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
307 #ifdef FEATURE_TOGGLE
308 /* by haroon - indicates if ijb is enabled */
309 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
310 #endif /* def FEATURE_TOGGLE */
312 /* The filename of the configfile */
313 const char *configfile = NULL;
316 * CGI functions will later need access to the invocation args,
317 * so we will make argc and argv global.
320 const char **Argv = NULL;
322 static struct file_list *current_configfile = NULL;
326 * This takes the "cryptic" hash of each keyword and aliases them to
327 * something a little more readable. This also makes changing the
328 * hash values easier if they should change or the hash algorthm changes.
329 * Use the included "hash" program to find out what the hash will be
330 * for any string supplied on the command line. (Or just put it in the
331 * config file and read the number from the error message in the log).
333 * Please keep this list sorted alphabetically (but with the Windows
334 * console and GUI specific options last).
337 #define hash_actions_file 1196306641ul /* "actionsfile" */
338 #define hash_admin_address 4112573064ul /* "admin-address" */
339 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
340 #define hash_confdir 1978389ul /* "confdir" */
341 #define hash_debug 78263ul /* "debug" */
342 #define hash_deny_access 1227333715ul /* "deny-access" */
343 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
344 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
345 #define hash_forward 2029845ul /* "forward" */
346 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
347 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
348 #define hash_jarfile 2046641ul /* "jarfile" */
349 #define hash_listen_address 1255650842ul /* "listen-address" */
350 #define hash_logdir 422889ul /* "logdir" */
351 #define hash_logfile 2114766ul /* "logfile" */
352 #define hash_permit_access 3587953268ul /* "permit-access" */
353 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
354 #define hash_re_filterfile 3877522444ul /* "re_filterfile" */
355 #define hash_single_threaded 4250084780ul /* "single-threaded" */
356 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
357 #define hash_toggle 447966ul /* "toggle" */
358 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
359 #define hash_trustfile 56494766ul /* "trustfile" */
361 #define hash_activity_animation 1817904738ul /* "activity-animation" */
362 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
363 #define hash_hide_console 2048809870ul /* "hide-console" */
364 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
365 #define hash_log_font_name 2866730124ul /* "log-font-name" */
366 #define hash_log_font_size 2866731014ul /* "log-font-size" */
367 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
368 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
369 #define hash_log_messages 2291744899ul /* "log-messages" */
370 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
373 static void savearg(char *command, char *argument, struct configuration_spec * config);
375 /*********************************************************************
377 * Function : unload_configfile
379 * Description : Free the config structure and all components.
382 * 1 : data: struct configuration_spec to unload
386 *********************************************************************/
387 void unload_configfile (void * data)
389 struct configuration_spec * config = (struct configuration_spec *)data;
390 struct forward_spec *cur_fwd = config->forward;
392 struct access_control_list *cur_acl = config->acl;
394 while (cur_acl != NULL)
396 struct access_control_list * next_acl = cur_acl->next;
401 #endif /* def FEATURE_ACL */
403 while (cur_fwd != NULL)
405 struct forward_spec * next_fwd = cur_fwd->next;
406 free_url_spec(cur_fwd->url);
408 freez(cur_fwd->gateway_host);
409 freez(cur_fwd->forward_host);
413 config->forward = NULL;
415 #ifdef FEATURE_COOKIE_JAR
416 if ( NULL != config->jar )
418 fclose( config->jar );
421 #endif /* def FEATURE_COOKIE_JAR */
423 freez(config->confdir);
424 freez(config->logdir);
426 freez(config->haddr);
427 freez(config->logfile);
429 freez(config->actions_file);
430 freez(config->admin_address);
431 freez(config->proxy_info_url);
432 freez(config->proxy_args);
434 #ifdef FEATURE_COOKIE_JAR
435 freez(config->jarfile);
436 #endif /* def FEATURE_COOKIE_JAR */
438 freez(config->re_filterfile);
443 /*********************************************************************
445 * Function : load_config
447 * Description : Load the config file and all parameters.
450 * 1 : csp = Client state (the config member will be
451 * filled in by this function).
453 * Returns : 0 => Ok, everything else is an error.
455 *********************************************************************/
456 struct configuration_spec * load_config(void)
458 char buf[BUFFER_SIZE];
460 FILE *configfp = NULL;
461 struct configuration_spec * config = NULL;
462 struct client_state * fake_csp;
463 struct file_list *fs;
464 unsigned long linenum = 0;
466 DBG(1, ("load_config() entered..\n") );
467 if ( !check_file_changed(current_configfile, configfile, &fs))
469 /* No need to load */
470 return ((struct configuration_spec *)current_configfile->f);
474 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
479 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
482 #ifdef FEATURE_TOGGLE
484 #endif /* def FEATURE_TOGGLE */
486 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
492 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
493 /* Never get here - LOG_LEVEL_FATAL causes program exit */
497 * This is backwards from how it's usually done.
498 * Following the usual pattern, "fs" would be stored in a member
499 * variable in "csp", and then we'd access "config" from "fs->f",
500 * using a cast. However, "config" is used so often that a
501 * cast each time would be very ugly, and the extra indirection
502 * would waste CPU cycles. Therefore we store "config" in
503 * "csp->config", and "fs" in "csp->config->config_file_list".
505 config->config_file_list = fs;
511 config->multi_threaded = 1;
512 config->hport = HADDR_PORT;
513 config->buffer_limit = 4096 * 1024;
514 config->proxy_args = strdup("");
516 if ((configfp = fopen(configfile, "r")) == NULL)
518 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
520 /* Never get here - LOG_LEVEL_FATAL causes program exit */
523 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
525 char cmd[BUFFER_SIZE];
526 char arg[BUFFER_SIZE];
527 char tmp[BUFFER_SIZE];
529 struct access_control_list *cur_acl;
530 #endif /* def FEATURE_ACL */
531 struct forward_spec *cur_fwd;
537 /* Copy command (i.e. up to space or tab) into cmd */
540 while (*p && (*p != ' ') && (*p != '\t'))
546 /* Skip over the whitespace in buf */
547 while (*p && ((*p == ' ') || (*p == '\t')))
552 /* Copy the argument into arg */
555 /* Should never happen, but check this anyway */
561 /* Make sure the command field is lower case */
566 *p = ijb_tolower(*p);
570 /* Save the argument for show-proxy-args */
571 savearg(cmd, arg, config);
574 switch( hash_string( cmd ) )
576 /****************************************************************************
577 * actionsfile actions-file-name
578 * In confdir by default
579 ****************************************************************************/
580 case hash_actions_file :
581 freez(config->actions_file);
582 config->actions_file = make_path(config->confdir, arg);
585 /****************************************************************************
586 * admin-address email-address
587 ****************************************************************************/
588 case hash_admin_address :
589 freez(config->admin_address);
590 config->admin_address = strdup(arg);
593 /****************************************************************************
595 ****************************************************************************/
596 case hash_buffer_limit :
597 config->buffer_limit = (size_t) 1024 * atoi(arg);
600 /****************************************************************************
601 * confdir directory-name
602 ****************************************************************************/
604 freez(config->confdir);
605 config->confdir = make_path( NULL, arg);
608 /****************************************************************************
610 * Specifies debug level, multiple values are ORed together.
611 ****************************************************************************/
613 config->debug |= atoi(arg);
616 /****************************************************************************
617 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
618 ****************************************************************************/
620 case hash_deny_access:
621 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
623 if ((vec_count != 1) && (vec_count != 2))
625 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
626 "deny-access directive in configuration file.");
627 string_append(&config->proxy_args,
628 "<br>\nWARNING: Wrong number of parameters for "
629 "deny-access directive in configuration file.<br><br>\n");
633 /* allocate a new node */
634 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
638 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
639 /* Never get here - LOG_LEVEL_FATAL causes program exit */
642 cur_acl->action = ACL_DENY;
644 if (acl_addr(vec[0], cur_acl->src) < 0)
646 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
647 "directive in configuration file: \"%s\"", vec[0]);
648 string_append(&config->proxy_args,
649 "<br>\nWARNING: Invalid source IP for deny-access directive"
650 " in configuration file: \"");
651 string_append(&config->proxy_args,
653 string_append(&config->proxy_args,
660 if (acl_addr(vec[1], cur_acl->dst) < 0)
662 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
663 "directive in configuration file: \"%s\"", vec[0]);
664 string_append(&config->proxy_args,
665 "<br>\nWARNING: Invalid destination IP for deny-access directive"
666 " in configuration file: \"");
667 string_append(&config->proxy_args,
669 string_append(&config->proxy_args,
677 * Add it to the list. Note we reverse the list to get the
678 * behaviour the user expects. With both the ACL and
679 * actions file, the last match wins. However, the internal
680 * implementations are different: The actions file is stored
681 * in the same order as the file, and scanned completely.
682 * With the ACL, we reverse the order as we load it, then
683 * when we scan it we stop as soon as we get a match.
685 cur_acl->next = config->acl;
686 config->acl = cur_acl;
689 #endif /* def FEATURE_ACL */
691 /****************************************************************************
692 * enable-edit-actions 0|1
693 ****************************************************************************/
694 #ifdef FEATURE_CGI_EDIT_ACTIONS
695 case hash_enable_edit_actions:
696 if ((*arg != '\0') && (0 != atoi(arg)))
698 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
702 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
705 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
707 /****************************************************************************
708 * enable-remote-toggle 0|1
709 ****************************************************************************/
710 #ifdef FEATURE_CGI_EDIT_ACTIONS
711 case hash_enable_remote_toggle:
712 if ((*arg != '\0') && (0 != atoi(arg)))
714 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
718 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
721 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
723 /****************************************************************************
724 * forward url-pattern (.|http-proxy-host[:port])
725 ****************************************************************************/
727 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
731 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
732 "directive in configuration file.");
733 string_append(&config->proxy_args,
734 "<br>\nWARNING: Wrong number of parameters for "
735 "forward directive in configuration file.");
739 /* allocate a new node */
740 cur_fwd = zalloc(sizeof(*cur_fwd));
743 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
744 /* Never get here - LOG_LEVEL_FATAL causes program exit */
748 cur_fwd->type = SOCKS_NONE;
750 /* Save the URL pattern */
751 if (create_url_spec(cur_fwd->url, vec[0]))
753 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
754 "directive in configuration file.");
755 string_append(&config->proxy_args,
756 "<br>\nWARNING: Bad URL specifier for "
757 "forward directive in configuration file.");
761 /* Parse the parent HTTP proxy host:port */
764 if (strcmp(p, ".") != 0)
766 cur_fwd->forward_host = strdup(p);
768 if ((p = strchr(cur_fwd->forward_host, ':')))
771 cur_fwd->forward_port = atoi(p);
774 if (cur_fwd->forward_port <= 0)
776 cur_fwd->forward_port = 8000;
781 cur_fwd->next = config->forward;
782 config->forward = cur_fwd;
786 /****************************************************************************
787 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
788 ****************************************************************************/
789 case hash_forward_socks4:
790 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
794 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
795 "forward-socks4 directive in configuration file.");
796 string_append(&config->proxy_args,
797 "<br>\nWARNING: Wrong number of parameters for "
798 "forward-socks4 directive in configuration file.");
802 /* allocate a new node */
803 cur_fwd = zalloc(sizeof(*cur_fwd));
806 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
807 /* Never get here - LOG_LEVEL_FATAL causes program exit */
811 cur_fwd->type = SOCKS_4;
813 /* Save the URL pattern */
814 if (create_url_spec(cur_fwd->url, vec[0]))
816 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
817 "directive in configuration file.");
818 string_append(&config->proxy_args,
819 "<br>\nWARNING: Bad URL specifier for "
820 "forward-socks4 directive in configuration file.");
824 /* Parse the SOCKS proxy host[:port] */
827 if (strcmp(p, ".") != 0)
829 cur_fwd->gateway_host = strdup(p);
831 if ((p = strchr(cur_fwd->gateway_host, ':')))
834 cur_fwd->gateway_port = atoi(p);
836 if (cur_fwd->gateway_port <= 0)
838 cur_fwd->gateway_port = 1080;
842 /* Parse the parent HTTP proxy host[:port] */
845 if (strcmp(p, ".") != 0)
847 cur_fwd->forward_host = strdup(p);
849 if ((p = strchr(cur_fwd->forward_host, ':')))
852 cur_fwd->forward_port = atoi(p);
855 if (cur_fwd->forward_port <= 0)
857 cur_fwd->forward_port = 8000;
862 cur_fwd->next = config->forward;
863 config->forward = cur_fwd;
867 /****************************************************************************
868 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
869 ****************************************************************************/
870 case hash_forward_socks4a:
871 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
875 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
876 "forward-socks4a directive in configuration file.");
877 string_append(&config->proxy_args,
878 "<br>\nWARNING: Wrong number of parameters for "
879 "forward-socks4a directive in configuration file.");
883 /* allocate a new node */
884 cur_fwd = zalloc(sizeof(*cur_fwd));
887 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
888 /* Never get here - LOG_LEVEL_FATAL causes program exit */
892 cur_fwd->type = SOCKS_4A;
894 /* Save the URL pattern */
895 if (create_url_spec(cur_fwd->url, vec[0]))
897 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
898 "directive in configuration file.");
899 string_append(&config->proxy_args,
900 "<br>\nWARNING: Bad URL specifier for "
901 "forward-socks4a directive in configuration file.");
905 /* Parse the SOCKS proxy host[:port] */
908 cur_fwd->gateway_host = strdup(p);
910 if ((p = strchr(cur_fwd->gateway_host, ':')))
913 cur_fwd->gateway_port = atoi(p);
915 if (cur_fwd->gateway_port <= 0)
917 cur_fwd->gateway_port = 1080;
920 /* Parse the parent HTTP proxy host[:port] */
923 if (strcmp(p, ".") != 0)
925 cur_fwd->forward_host = strdup(p);
927 if ((p = strchr(cur_fwd->forward_host, ':')))
930 cur_fwd->forward_port = atoi(p);
933 if (cur_fwd->forward_port <= 0)
935 cur_fwd->forward_port = 8000;
940 cur_fwd->next = config->forward;
941 config->forward = cur_fwd;
945 /****************************************************************************
946 * jarfile jar-file-name
947 * In logdir by default
948 ****************************************************************************/
949 #ifdef FEATURE_COOKIE_JAR
951 freez(config->jarfile);
952 config->jarfile = make_path(config->logdir, arg);
954 #endif /* def FEATURE_COOKIE_JAR */
956 /****************************************************************************
957 * listen-address [ip][:port]
958 ****************************************************************************/
959 case hash_listen_address :
960 freez(config->haddr);
961 config->haddr = strdup(arg);
964 /****************************************************************************
965 * logdir directory-name
966 ****************************************************************************/
968 freez(config->logdir);
969 config->logdir = make_path(NULL, arg);
972 /****************************************************************************
973 * logfile log-file-name
974 * In logdir by default
975 ****************************************************************************/
977 freez(config->logfile);
978 config->logfile = make_path(config->logdir, arg);
981 /****************************************************************************
982 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
983 ****************************************************************************/
985 case hash_permit_access:
986 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
988 if ((vec_count != 1) && (vec_count != 2))
990 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
991 "permit-access directive in configuration file.");
992 string_append(&config->proxy_args,
993 "<br>\nWARNING: Wrong number of parameters for "
994 "permit-access directive in configuration file.<br><br>\n");
999 /* allocate a new node */
1000 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1002 if (cur_acl == NULL)
1004 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1005 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1008 cur_acl->action = ACL_PERMIT;
1010 if (acl_addr(vec[0], cur_acl->src) < 0)
1012 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1013 "directive in configuration file: \"%s\"", vec[0]);
1014 string_append(&config->proxy_args,
1015 "<br>\nWARNING: Invalid source IP for permit-access directive"
1016 " in configuration file: \"");
1017 string_append(&config->proxy_args,
1019 string_append(&config->proxy_args,
1026 if (acl_addr(vec[1], cur_acl->dst) < 0)
1028 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1029 "permit-access directive in configuration file: \"%s\"",
1031 string_append(&config->proxy_args,
1032 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1033 " in configuration file: \"");
1034 string_append(&config->proxy_args,
1036 string_append(&config->proxy_args,
1044 * Add it to the list. Note we reverse the list to get the
1045 * behaviour the user expects. With both the ACL and
1046 * actions file, the last match wins. However, the internal
1047 * implementations are different: The actions file is stored
1048 * in the same order as the file, and scanned completely.
1049 * With the ACL, we reverse the order as we load it, then
1050 * when we scan it we stop as soon as we get a match.
1052 cur_acl->next = config->acl;
1053 config->acl = cur_acl;
1056 #endif /* def FEATURE_ACL */
1058 /****************************************************************************
1059 * proxy-info-url url
1060 ****************************************************************************/
1061 case hash_proxy_info_url :
1062 freez(config->proxy_info_url);
1063 config->proxy_info_url = strdup(arg);
1066 /****************************************************************************
1067 * re_filterfile file-name
1068 * In confdir by default.
1069 ****************************************************************************/
1070 case hash_re_filterfile :
1071 freez(config->re_filterfile);
1072 config->re_filterfile = make_path(config->confdir, arg);
1075 /****************************************************************************
1077 ****************************************************************************/
1078 case hash_single_threaded :
1079 config->multi_threaded = 0;
1082 /****************************************************************************
1084 ****************************************************************************/
1085 #ifdef FEATURE_TOGGLE
1087 g_bToggleIJB = atoi(arg);
1089 #endif /* def FEATURE_TOGGLE */
1091 /****************************************************************************
1092 * trust-info-url url
1093 ****************************************************************************/
1094 #ifdef FEATURE_TRUST
1095 case hash_trust_info_url :
1096 enlist(config->trust_info, arg);
1098 #endif /* def FEATURE_TRUST */
1100 /****************************************************************************
1101 * trustfile filename
1102 * (In confdir by default.)
1103 ****************************************************************************/
1104 #ifdef FEATURE_TRUST
1105 case hash_trustfile :
1106 freez(config->trustfile);
1107 config->trustfile = make_path(config->confdir, arg);
1109 #endif /* def FEATURE_TRUST */
1112 /****************************************************************************
1113 * Win32 Console options:
1114 ****************************************************************************/
1116 /****************************************************************************
1118 ****************************************************************************/
1120 case hash_hide_console :
1123 #endif /*def _WIN_CONSOLE*/
1126 /****************************************************************************
1127 * Win32 GUI options:
1128 ****************************************************************************/
1130 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1131 /****************************************************************************
1132 * activity-animation (0|1)
1133 ****************************************************************************/
1134 case hash_activity_animation :
1135 g_bShowActivityAnimation = atoi(arg);
1138 /****************************************************************************
1139 * close-button-minimizes (0|1)
1140 ****************************************************************************/
1141 case hash_close_button_minimizes :
1142 g_bCloseHidesWindow = atoi(arg);
1145 /****************************************************************************
1146 * log-buffer-size (0|1)
1147 ****************************************************************************/
1148 case hash_log_buffer_size :
1149 g_bLimitBufferSize = atoi(arg);
1152 /****************************************************************************
1153 * log-font-name fontnane
1154 ****************************************************************************/
1155 case hash_log_font_name :
1156 strcpy( g_szFontFaceName, arg );
1159 /****************************************************************************
1161 ****************************************************************************/
1162 case hash_log_font_size :
1163 g_nFontSize = atoi(arg);
1166 /****************************************************************************
1167 * log-highlight-messages (0|1)
1168 ****************************************************************************/
1169 case hash_log_highlight_messages :
1170 g_bHighlightMessages = atoi(arg);
1173 /****************************************************************************
1175 ****************************************************************************/
1176 case hash_log_max_lines :
1177 g_nMaxBufferLines = atoi(arg);
1180 /****************************************************************************
1181 * log-messages (0|1)
1182 ****************************************************************************/
1183 case hash_log_messages :
1184 g_bLogMessages = atoi(arg);
1187 /****************************************************************************
1188 * show-on-task-bar (0|1)
1189 ****************************************************************************/
1190 case hash_show_on_task_bar :
1191 g_bShowOnTaskBar = atoi(arg);
1194 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1197 /****************************************************************************/
1198 /* Warnings about unsupported features */
1199 /****************************************************************************/
1201 case hash_deny_access:
1202 #endif /* ndef FEATURE_ACL */
1203 #ifndef FEATURE_CGI_EDIT_ACTIONS
1204 case hash_enable_edit_actions:
1205 case hash_enable_remote_toggle:
1206 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1207 #ifndef FEATURE_COOKIE_JAR
1209 #endif /* ndef FEATURE_COOKIE_JAR */
1211 case hash_permit_access:
1212 #endif /* ndef FEATURE_ACL */
1213 #ifndef FEATURE_TOGGLE
1215 #endif /* ndef FEATURE_TOGGLE */
1216 #ifndef FEATURE_TRUST
1217 case hash_trustfile :
1218 case hash_trust_info_url :
1219 #endif /* ndef FEATURE_TRUST */
1221 #ifndef _WIN_CONSOLE
1222 case hash_hide_console :
1223 #endif /* ndef _WIN_CONSOLE */
1225 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1226 case hash_activity_animation :
1227 case hash_close_button_minimizes :
1228 case hash_log_buffer_size :
1229 case hash_log_font_name :
1230 case hash_log_font_size :
1231 case hash_log_highlight_messages :
1232 case hash_log_max_lines :
1233 case hash_log_messages :
1234 case hash_show_on_task_bar :
1235 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1236 /* These warnings are annoying - so hide them. -- Jon */
1237 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1240 /****************************************************************************/
1242 /****************************************************************************/
1244 * I decided that I liked this better as a warning than an
1245 * error. To change back to an error, just change log level
1246 * to LOG_LEVEL_FATAL.
1248 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' in line %lu in "
1249 "configuration file (%s).", buf, linenum, configfile);
1250 /* log_error(LOG_LEVEL_ERROR, "Unrecognized directive (%luul) in "
1251 "configuration file: \"%s\"", hash_string( cmd ), buf);
1253 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1254 string_append(&config->proxy_args, buf);
1255 string_append(&config->proxy_args, "<br><br>\n");
1258 /****************************************************************************/
1259 } /* end switch( hash_string(cmd) ) */
1260 } /* end while ( read_config_line(...) ) */
1264 if (NULL == config->proxy_args)
1266 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1269 init_error_log(Argv[0], config->logfile, config->debug);
1271 if (config->actions_file)
1273 add_loader(load_actions_file, config);
1276 if (config->re_filterfile)
1278 add_loader(load_re_filterfile, config);
1281 #ifdef FEATURE_TRUST
1282 if (config->trustfile)
1284 add_loader(load_trustfile, config);
1286 #endif /* def FEATURE_TRUST */
1288 #ifdef FEATURE_COOKIE_JAR
1289 if ( NULL != config->jarfile )
1291 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1293 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1294 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1296 setbuf(config->jar, NULL);
1298 #endif /* def FEATURE_COOKIE_JAR */
1300 if ( NULL == config->haddr )
1302 config->haddr = strdup( HADDR_DEFAULT );
1305 if ( NULL != config->haddr )
1307 if ((p = strchr(config->haddr, ':')))
1312 config->hport = atoi(p);
1316 if (config->hport <= 0)
1319 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1320 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1322 if (*config->haddr == '\0')
1324 config->haddr = NULL;
1329 * Want to run all the loaders once now.
1331 * Need to set up a fake csp, so they can get to the config.
1333 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1334 fake_csp->config = config;
1336 if (run_loader(fake_csp))
1339 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1340 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1344 /* FIXME: this is a kludge for win32 */
1345 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1347 g_actions_file = config->actions_file;
1348 g_re_filterfile = config->re_filterfile;
1350 #ifdef FEATURE_TRUST
1351 g_trustfile = config->trustfile;
1352 #endif /* def FEATURE_TRUST */
1355 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1356 /* FIXME: end kludge */
1359 config->need_bind = 1;
1361 if (current_configfile)
1363 struct configuration_spec * oldcfg = (struct configuration_spec *)
1364 current_configfile->f;
1366 * Check if config->haddr,hport == oldcfg->haddr,hport
1368 * The following could be written more compactly as a single,
1369 * (unreadably long) if statement.
1371 config->need_bind = 0;
1372 if (config->hport != oldcfg->hport)
1374 config->need_bind = 1;
1376 else if (config->haddr == NULL)
1378 if (oldcfg->haddr != NULL)
1380 config->need_bind = 1;
1383 else if (oldcfg->haddr == NULL)
1385 config->need_bind = 1;
1387 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1389 config->need_bind = 1;
1392 current_configfile->unloader = unload_configfile;
1395 fs->next = files->next;
1398 current_configfile = fs;
1404 /*********************************************************************
1406 * Function : savearg
1408 * Description : Called from `load_config'. It saves each non-empty
1409 * and non-comment line from config into
1410 * config->proxy_args. This is used to create the
1411 * show-proxy-args page. On error, frees
1412 * config->proxy_args and sets it to NULL
1415 * 1 : command = config setting that was found
1416 * 2 : argument = the setting's argument (if any)
1420 *********************************************************************/
1421 static void savearg(char *command, char *argument, struct configuration_spec * config)
1432 s = html_encode(command);
1436 freez(config->proxy_args);
1439 string_append(&buf, "<a href=\"" REDIRECT_URL "option#");
1440 string_append(&buf, s);
1441 string_append(&buf, "\">");
1442 string_join (&buf, s);
1443 string_append(&buf, "</a> ");
1445 if ( (NULL != argument) && ('\0' != *argument) )
1447 s = html_encode(argument);
1451 freez(config->proxy_args);
1455 if (strncmpic(argument, "http://", 7) == 0)
1457 string_append(&buf, "<a href=\"");
1458 string_append(&buf, s);
1459 string_append(&buf, "\">");
1460 string_join (&buf, s);
1461 string_append(&buf, "</a>");
1465 string_join (&buf, s);
1469 string_append(&buf, "<br>\n");
1471 string_join(&config->proxy_args, buf);