1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.47 2002/05/12 21:36:29 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 * Privoxy team. http://www.privoxy.org/
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.47 2002/05/12 21:36:29 jongfoster
39 * Correcting function comments
41 * Revision 1.46 2002/04/26 12:55:14 oes
42 * - New option "user-manual", defaults to our site
43 * via project.h #define
44 * - savearg now embeds option names in help links
46 * Revision 1.45 2002/04/24 02:11:54 oes
47 * Jon's multiple AF patch: Allow up to MAX_ACTION_FILES actionsfile options
49 * Revision 1.44 2002/04/08 20:37:13 swa
52 * Revision 1.43 2002/04/08 20:36:50 swa
55 * Revision 1.42 2002/04/05 15:50:15 oes
56 * fix for invalid HTML proxy_args
58 * Revision 1.41 2002/03/31 17:19:00 jongfoster
59 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
61 * Revision 1.40 2002/03/26 22:29:55 swa
62 * we have a new homepage!
64 * Revision 1.39 2002/03/24 13:25:43 swa
65 * name change related issues
67 * Revision 1.38 2002/03/24 13:05:48 jongfoster
68 * Renaming re_filterfile to filterfile
70 * Revision 1.37 2002/03/16 23:54:06 jongfoster
71 * Adding graceful termination feature, to help look for memory leaks.
72 * If you enable this (which, by design, has to be done by hand
73 * editing config.h) and then go to http://i.j.b/die, then the program
74 * will exit cleanly after the *next* request. It should free all the
75 * memory that was used.
77 * Revision 1.36 2002/03/13 00:27:05 jongfoster
80 * Revision 1.35 2002/03/07 03:52:44 oes
81 * Set logging to tty for --no-daemon mode
83 * Revision 1.34 2002/03/06 23:14:35 jongfoster
84 * Trivial cosmetic changes to make function comments easier to find.
86 * Revision 1.33 2002/03/05 04:52:42 oes
87 * Deleted non-errlog debugging code
89 * Revision 1.32 2002/03/04 18:24:53 oes
90 * Re-enabled output of unknown config directive hash
92 * Revision 1.31 2002/03/03 15:07:20 oes
93 * Re-enabled automatic config reloading
95 * Revision 1.30 2002/01/22 23:31:43 jongfoster
96 * Replacing strsav() with string_append()
98 * Revision 1.29 2002/01/17 21:02:30 jongfoster
99 * Moving all our URL and URL pattern parsing code to urlmatch.c.
101 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
103 * Revision 1.28 2001/12/30 14:07:32 steudten
104 * - Add signal handling (unix)
105 * - Add SIGHUP handler (unix)
106 * - Add creation of pidfile (unix)
107 * - Add action 'top' in rc file (RH)
108 * - Add entry 'SIGNALS' to manpage
109 * - Add exit message to logfile (unix)
111 * Revision 1.27 2001/11/07 00:02:13 steudten
112 * Add line number in error output for lineparsing for
113 * actionsfile and configfile.
114 * Special handling for CLF added.
116 * Revision 1.26 2001/11/05 21:41:43 steudten
117 * Add changes to be a real daemon just for unix os.
118 * (change cwd to /, detach from controlling tty, set
119 * process group and session leader to the own process.
121 * Add some fatal-error log message for failed malloc().
122 * Add '-d' if compiled with 'configure --with-debug' to
123 * enable debug output.
125 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
126 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
127 * threads to call select() simultaneously. So, it's time to do a real, live,
128 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
129 * (native). Both versions will work, but using __OS2__ offers multi-threading.
131 * Revision 1.24 2001/10/23 21:40:30 jongfoster
132 * Added support for enable-edit-actions and enable-remote-toggle config
135 * Revision 1.23 2001/10/07 15:36:00 oes
136 * Introduced new config option "buffer-limit"
138 * Revision 1.22 2001/09/22 16:36:59 jongfoster
139 * Removing unused parameter fs from read_config_line()
141 * Revision 1.21 2001/09/16 17:10:43 jongfoster
142 * Moving function savearg() here, since it was the only thing left in
145 * Revision 1.20 2001/07/30 22:08:36 jongfoster
146 * Tidying up #defines:
147 * - All feature #defines are now of the form FEATURE_xxx
148 * - Permanently turned off WIN_GUI_EDIT
149 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
151 * Revision 1.19 2001/07/15 17:45:16 jongfoster
152 * Removing some unused #includes
154 * Revision 1.18 2001/07/13 14:01:14 oes
155 * - Removed all #ifdef PCRS
156 * - Removed vim-settings
158 * Revision 1.17 2001/06/29 13:31:03 oes
159 * - Improved comments
160 * - Fixed (actionsfile) and sorted hashes
161 * - Introduced admin_address and proxy-info-url
162 * as config parameters
163 * - Renamed config->proxy_args_invocation (which didn't have
164 * the invocation but the options!) to config->proxy_args
165 * - Various adaptions
166 * - Removed logentry from cancelled commit
168 * Revision 1.16 2001/06/09 10:55:28 jongfoster
169 * Changing BUFSIZ ==> BUFFER_SIZE
171 * Revision 1.15 2001/06/07 23:13:40 jongfoster
172 * Merging ACL and forward files into config file.
173 * Cosmetic: Sorting config file options alphabetically.
174 * Cosmetic: Adding brief syntax comments to config file options.
176 * Revision 1.14 2001/06/07 14:46:25 joergs
177 * Missing make_path() added for re_filterfile.
179 * Revision 1.13 2001/06/05 22:33:54 jongfoster
181 * Fixed minor memory leak.
182 * Also now uses make_path to prepend the pathnames.
184 * Revision 1.12 2001/06/05 20:04:09 jongfoster
185 * Now uses _snprintf() in place of snprintf() under Win32.
187 * Revision 1.11 2001/06/04 18:31:58 swa
188 * files are now prefixed with either `confdir' or `logdir'.
189 * `make redhat-dist' replaces both entries confdir and logdir
192 * Revision 1.10 2001/06/03 19:11:54 oes
193 * introduced confdir option
195 * Revision 1.9 2001/06/01 20:06:24 jongfoster
196 * Removed support for "tinygif" option - moved to actions file.
198 * Revision 1.8 2001/05/31 21:27:13 jongfoster
199 * Removed many options from the config file and into the
200 * "actions" file: add_forwarded, suppress_vanilla_wafer,
201 * wafer, add_header, user_agent, referer, from
202 * Also globally replaced "permission" with "action".
204 * Revision 1.7 2001/05/29 09:50:24 jongfoster
205 * Unified blocklist/imagelist/permissionslist.
206 * File format is still under discussion, but the internal changes
209 * Also modified interceptor behaviour:
210 * - We now intercept all URLs beginning with one of the following
211 * prefixes (and *only* these prefixes):
213 * * http://ijbswa.sf.net/config/
214 * * http://ijbswa.sourceforge.net/config/
215 * - New interceptors "home page" - go to http://i.j.b/ to see it.
216 * - Internal changes so that intercepted and fast redirect pages
217 * are not replaced with an image.
218 * - Interceptors now have the option to send a binary page direct
219 * to the client. (i.e. ijb-send-banner uses this)
220 * - Implemented show-url-info interceptor. (Which is why I needed
221 * the above interceptors changes - a typical URL is
222 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
223 * The previous mechanism would not have intercepted that, and
224 * if it had been intercepted then it then it would have replaced
227 * Revision 1.6 2001/05/26 00:28:36 jongfoster
228 * Automatic reloading of config file.
229 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
230 * Most of the global variables have been moved to a new
231 * struct configuration_spec, accessed through csp->config->globalname
232 * Most of the globals remaining are used by the Win32 GUI.
234 * Revision 1.5 2001/05/25 22:34:30 jongfoster
237 * Revision 1.4 2001/05/22 18:46:04 oes
239 * - Enabled filtering banners by size rather than URL
240 * by adding patterns that replace all standard banner
241 * sizes with the "Junkbuster" gif to the re_filterfile
243 * - Enabled filtering WebBugs by providing a pattern
244 * which kills all 1x1 images
246 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
247 * which is selected by the (nonstandard and therefore
248 * capital) letter 'U' in the option string.
249 * It causes the quantifiers to be ungreedy by default.
250 * Appending a ? turns back to greedy (!).
252 * - Added a new interceptor ijb-send-banner, which
253 * sends back the "Junkbuster" gif. Without imagelist or
254 * MSIE detection support, or if tinygif = 1, or the
255 * URL isn't recognized as an imageurl, a lame HTML
256 * explanation is sent instead.
258 * - Added new feature, which permits blocking remote
259 * script redirects and firing back a local redirect
261 * The feature is conditionally compiled, i.e. it
262 * can be disabled with --disable-fast-redirects,
263 * plus it must be activated by a "fast-redirects"
264 * line in the config file, has its own log level
265 * and of course wants to be displayed by show-proxy-args
266 * Note: Boy, all the #ifdefs in 1001 locations and
267 * all the fumbling with configure.in and acconfig.h
268 * were *way* more work than the feature itself :-(
270 * - Because a generic redirect template was needed for
271 * this, tinygif = 3 now uses the same.
273 * - Moved GIFs, and other static HTTP response templates
278 * - Removed some >400 CRs again (Jon, you really worked
281 * Revision 1.3 2001/05/20 01:21:20 jongfoster
282 * Version 2.9.4 checkin.
283 * - Merged popupfile and cookiefile, and added control over PCRS
284 * filtering, in new "permissionsfile".
285 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
286 * file error you now get a message box (in the Win32 GUI) rather
287 * than the program exiting with no explanation.
288 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
290 * - Removed tabs from "config"
291 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
292 * - Bumped up version number.
294 * Revision 1.2 2001/05/17 23:01:01 oes
295 * - Cleaned CRLF's from the sources and related files
297 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
298 * Initial import of version 2.9.3 source tree
301 *********************************************************************/
307 #include <sys/types.h>
321 # include <windows.h>
324 # ifndef _WIN_CONSOLE
326 # endif /* ndef _WIN_CONSOLE */
328 /* VC++ has "_snprintf", not "snprintf" */
329 #define snprintf _snprintf
331 #else /* ifndef _WIN32 */
335 # include <sys/wait.h>
337 # include <sys/time.h>
338 # include <sys/stat.h>
348 #include "miscutil.h"
352 #include "urlmatch.h"
355 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
358 * Fix a problem with Solaris. There should be no effect on other
360 * Solaris's isspace() is a macro which uses it's argument directly
361 * as an array index. Therefore we need to make sure that high-bit
362 * characters generate +ve values, and ideally we also want to make
363 * the argument match the declared parameter type of "int".
365 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
366 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
368 #ifdef FEATURE_TOGGLE
369 /* by haroon - indicates if ijb is enabled */
370 int g_bToggleIJB = 1; /* Privoxy is enabled by default. */
371 #endif /* def FEATURE_TOGGLE */
373 /* The filename of the configfile */
374 const char *configfile = NULL;
377 * CGI functions will later need access to the invocation args,
378 * so we will make argc and argv global.
381 const char **Argv = NULL;
383 static struct file_list *current_configfile = NULL;
387 * This takes the "cryptic" hash of each keyword and aliases them to
388 * something a little more readable. This also makes changing the
389 * hash values easier if they should change or the hash algorthm changes.
390 * Use the included "hash" program to find out what the hash will be
391 * for any string supplied on the command line. (Or just put it in the
392 * config file and read the number from the error message in the log).
394 * Please keep this list sorted alphabetically (but with the Windows
395 * console and GUI specific options last).
398 #define hash_actions_file 1196306641ul /* "actionsfile" */
399 #define hash_admin_address 4112573064ul /* "admin-address" */
400 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
401 #define hash_confdir 1978389ul /* "confdir" */
402 #define hash_debug 78263ul /* "debug" */
403 #define hash_deny_access 1227333715ul /* "deny-access" */
404 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
405 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
406 #define hash_filterfile 250887266ul /* "filterfile" */
407 #define hash_forward 2029845ul /* "forward" */
408 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
409 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
410 #define hash_jarfile 2046641ul /* "jarfile" */
411 #define hash_listen_address 1255650842ul /* "listen-address" */
412 #define hash_logdir 422889ul /* "logdir" */
413 #define hash_logfile 2114766ul /* "logfile" */
414 #define hash_permit_access 3587953268ul /* "permit-access" */
415 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
416 #define hash_single_threaded 4250084780ul /* "single-threaded" */
417 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
418 #define hash_toggle 447966ul /* "toggle" */
419 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
420 #define hash_trustfile 56494766ul /* "trustfile" */
421 #define hash_usermanual 1416668518ul /* "user-manual" */
422 #define hash_activity_animation 1817904738ul /* "activity-animation" */
423 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
424 #define hash_hide_console 2048809870ul /* "hide-console" */
425 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
426 #define hash_log_font_name 2866730124ul /* "log-font-name" */
427 #define hash_log_font_size 2866731014ul /* "log-font-size" */
428 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
429 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
430 #define hash_log_messages 2291744899ul /* "log-messages" */
431 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
434 static void savearg(char *command, char *argument, struct configuration_spec * config);
436 /*********************************************************************
438 * Function : unload_configfile
440 * Description : Free the config structure and all components.
443 * 1 : data: struct configuration_spec to unload
447 *********************************************************************/
448 void unload_configfile (void * data)
450 struct configuration_spec * config = (struct configuration_spec *)data;
451 struct forward_spec *cur_fwd = config->forward;
453 struct access_control_list *cur_acl = config->acl;
456 while (cur_acl != NULL)
458 struct access_control_list * next_acl = cur_acl->next;
463 #endif /* def FEATURE_ACL */
465 while (cur_fwd != NULL)
467 struct forward_spec * next_fwd = cur_fwd->next;
468 free_url_spec(cur_fwd->url);
470 freez(cur_fwd->gateway_host);
471 freez(cur_fwd->forward_host);
475 config->forward = NULL;
477 #ifdef FEATURE_COOKIE_JAR
478 if ( NULL != config->jar )
480 fclose( config->jar );
483 #endif /* def FEATURE_COOKIE_JAR */
485 freez(config->confdir);
486 freez(config->logdir);
488 freez(config->haddr);
489 freez(config->logfile);
491 for (i = 0; i < MAX_ACTION_FILES; i++)
493 freez(config->actions_file_short[i]);
494 freez(config->actions_file[i]);
497 freez(config->admin_address);
498 freez(config->proxy_info_url);
499 freez(config->proxy_args);
500 freez(config->usermanual);
502 #ifdef FEATURE_COOKIE_JAR
503 freez(config->jarfile);
504 #endif /* def FEATURE_COOKIE_JAR */
506 freez(config->re_filterfile);
511 #ifdef FEATURE_GRACEFUL_TERMINATION
512 /*********************************************************************
514 * Function : unload_current_config_file
516 * Description : Unloads current config file - reset to state at
517 * beginning of program.
523 *********************************************************************/
524 void unload_current_config_file(void)
526 if (current_configfile)
528 current_configfile->unloader = unload_configfile;
529 current_configfile = NULL;
535 /*********************************************************************
537 * Function : load_config
539 * Description : Load the config file and all parameters.
543 * Returns : The configuration_spec, or NULL on error.
545 *********************************************************************/
546 struct configuration_spec * load_config(void)
548 char buf[BUFFER_SIZE];
550 FILE *configfp = NULL;
551 struct configuration_spec * config = NULL;
552 struct client_state * fake_csp;
553 struct file_list *fs;
554 unsigned long linenum = 0;
557 if ( !check_file_changed(current_configfile, configfile, &fs))
559 /* No need to load */
560 return ((struct configuration_spec *)current_configfile->f);
564 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
568 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
570 #ifdef FEATURE_TOGGLE
572 #endif /* def FEATURE_TOGGLE */
574 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
580 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
581 /* Never get here - LOG_LEVEL_FATAL causes program exit */
585 * This is backwards from how it's usually done.
586 * Following the usual pattern, "fs" would be stored in a member
587 * variable in "csp", and then we'd access "config" from "fs->f",
588 * using a cast. However, "config" is used so often that a
589 * cast each time would be very ugly, and the extra indirection
590 * would waste CPU cycles. Therefore we store "config" in
591 * "csp->config", and "fs" in "csp->config->config_file_list".
593 config->config_file_list = fs;
598 config->multi_threaded = 1;
599 config->hport = HADDR_PORT;
600 config->buffer_limit = 4096 * 1024;
601 config->usermanual = strdup(USER_MANUAL_URL);
602 config->proxy_args = strdup("");
604 if ((configfp = fopen(configfile, "r")) == NULL)
606 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
608 /* Never get here - LOG_LEVEL_FATAL causes program exit */
611 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
613 char cmd[BUFFER_SIZE];
614 char arg[BUFFER_SIZE];
615 char tmp[BUFFER_SIZE];
617 struct access_control_list *cur_acl;
618 #endif /* def FEATURE_ACL */
619 struct forward_spec *cur_fwd;
625 /* Copy command (i.e. up to space or tab) into cmd */
628 while (*p && (*p != ' ') && (*p != '\t'))
634 /* Skip over the whitespace in buf */
635 while (*p && ((*p == ' ') || (*p == '\t')))
640 /* Copy the argument into arg */
643 /* Should never happen, but check this anyway */
649 /* Make sure the command field is lower case */
654 *p = ijb_tolower(*p);
658 /* Save the argument for show-proxy-args */
659 savearg(cmd, arg, config);
662 switch( hash_string( cmd ) )
664 /* *************************************************************************
665 * actionsfile actions-file-name
666 * In confdir by default
667 * *************************************************************************/
668 case hash_actions_file :
670 while ((i < MAX_ACTION_FILES) && (NULL != config->actions_file[i]))
675 if (i >= MAX_ACTION_FILES)
677 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
678 "(You can increase this limit by changing MAX_ACTION_FILES in project.h and recompiling).",
681 config->actions_file_short[i] = strdup(arg);
682 p = malloc(strlen(arg) + sizeof(".action"));
685 log_error(LOG_LEVEL_FATAL, "Out of memory");
688 strcat(p, ".action");
689 config->actions_file[i] = make_path(config->confdir, p);
693 /* *************************************************************************
694 * admin-address email-address
695 * *************************************************************************/
696 case hash_admin_address :
697 freez(config->admin_address);
698 config->admin_address = strdup(arg);
701 /* *************************************************************************
703 * *************************************************************************/
704 case hash_buffer_limit :
705 config->buffer_limit = (size_t) 1024 * atoi(arg);
708 /* *************************************************************************
709 * confdir directory-name
710 * *************************************************************************/
712 freez(config->confdir);
713 config->confdir = make_path( NULL, arg);
716 /* *************************************************************************
718 * Specifies debug level, multiple values are ORed together.
719 * *************************************************************************/
721 config->debug |= atoi(arg);
724 /* *************************************************************************
725 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
726 * *************************************************************************/
728 case hash_deny_access:
729 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
731 if ((vec_count != 1) && (vec_count != 2))
733 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
734 "deny-access directive in configuration file.");
735 string_append(&config->proxy_args,
736 "<br>\nWARNING: Wrong number of parameters for "
737 "deny-access directive in configuration file.<br><br>\n");
741 /* allocate a new node */
742 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
746 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
747 /* Never get here - LOG_LEVEL_FATAL causes program exit */
750 cur_acl->action = ACL_DENY;
752 if (acl_addr(vec[0], cur_acl->src) < 0)
754 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
755 "directive in configuration file: \"%s\"", vec[0]);
756 string_append(&config->proxy_args,
757 "<br>\nWARNING: Invalid source IP for deny-access directive"
758 " in configuration file: \"");
759 string_append(&config->proxy_args,
761 string_append(&config->proxy_args,
768 if (acl_addr(vec[1], cur_acl->dst) < 0)
770 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
771 "directive in configuration file: \"%s\"", vec[0]);
772 string_append(&config->proxy_args,
773 "<br>\nWARNING: Invalid destination IP for deny-access directive"
774 " in configuration file: \"");
775 string_append(&config->proxy_args,
777 string_append(&config->proxy_args,
785 * Add it to the list. Note we reverse the list to get the
786 * behaviour the user expects. With both the ACL and
787 * actions file, the last match wins. However, the internal
788 * implementations are different: The actions file is stored
789 * in the same order as the file, and scanned completely.
790 * With the ACL, we reverse the order as we load it, then
791 * when we scan it we stop as soon as we get a match.
793 cur_acl->next = config->acl;
794 config->acl = cur_acl;
797 #endif /* def FEATURE_ACL */
799 /* *************************************************************************
800 * enable-edit-actions 0|1
801 * *************************************************************************/
802 #ifdef FEATURE_CGI_EDIT_ACTIONS
803 case hash_enable_edit_actions:
804 if ((*arg != '\0') && (0 != atoi(arg)))
806 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
810 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
813 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
815 /* *************************************************************************
816 * enable-remote-toggle 0|1
817 * *************************************************************************/
818 #ifdef FEATURE_CGI_EDIT_ACTIONS
819 case hash_enable_remote_toggle:
820 if ((*arg != '\0') && (0 != atoi(arg)))
822 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
826 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
829 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
831 /* *************************************************************************
832 * forward url-pattern (.|http-proxy-host[:port])
833 * *************************************************************************/
835 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
839 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
840 "directive in configuration file.");
841 string_append(&config->proxy_args,
842 "<br>\nWARNING: Wrong number of parameters for "
843 "forward directive in configuration file.");
847 /* allocate a new node */
848 cur_fwd = zalloc(sizeof(*cur_fwd));
851 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
852 /* Never get here - LOG_LEVEL_FATAL causes program exit */
856 cur_fwd->type = SOCKS_NONE;
858 /* Save the URL pattern */
859 if (create_url_spec(cur_fwd->url, vec[0]))
861 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
862 "directive in configuration file.");
863 string_append(&config->proxy_args,
864 "<br>\nWARNING: Bad URL specifier for "
865 "forward directive in configuration file.");
869 /* Parse the parent HTTP proxy host:port */
872 if (strcmp(p, ".") != 0)
874 cur_fwd->forward_host = strdup(p);
876 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
879 cur_fwd->forward_port = atoi(p);
882 if (cur_fwd->forward_port <= 0)
884 cur_fwd->forward_port = 8000;
889 cur_fwd->next = config->forward;
890 config->forward = cur_fwd;
894 /* *************************************************************************
895 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
896 * *************************************************************************/
897 case hash_forward_socks4:
898 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
902 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
903 "forward-socks4 directive in configuration file.");
904 string_append(&config->proxy_args,
905 "<br>\nWARNING: Wrong number of parameters for "
906 "forward-socks4 directive in configuration file.");
910 /* allocate a new node */
911 cur_fwd = zalloc(sizeof(*cur_fwd));
914 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
915 /* Never get here - LOG_LEVEL_FATAL causes program exit */
919 cur_fwd->type = SOCKS_4;
921 /* Save the URL pattern */
922 if (create_url_spec(cur_fwd->url, vec[0]))
924 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
925 "directive in configuration file.");
926 string_append(&config->proxy_args,
927 "<br>\nWARNING: Bad URL specifier for "
928 "forward-socks4 directive in configuration file.");
932 /* Parse the SOCKS proxy host[:port] */
935 if (strcmp(p, ".") != 0)
937 cur_fwd->gateway_host = strdup(p);
939 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
942 cur_fwd->gateway_port = atoi(p);
944 if (cur_fwd->gateway_port <= 0)
946 cur_fwd->gateway_port = 1080;
950 /* Parse the parent HTTP proxy host[:port] */
953 if (strcmp(p, ".") != 0)
955 cur_fwd->forward_host = strdup(p);
957 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
960 cur_fwd->forward_port = atoi(p);
963 if (cur_fwd->forward_port <= 0)
965 cur_fwd->forward_port = 8000;
970 cur_fwd->next = config->forward;
971 config->forward = cur_fwd;
975 /* *************************************************************************
976 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
977 * *************************************************************************/
978 case hash_forward_socks4a:
979 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
983 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
984 "forward-socks4a directive in configuration file.");
985 string_append(&config->proxy_args,
986 "<br>\nWARNING: Wrong number of parameters for "
987 "forward-socks4a directive in configuration file.");
991 /* allocate a new node */
992 cur_fwd = zalloc(sizeof(*cur_fwd));
995 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
996 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1000 cur_fwd->type = SOCKS_4A;
1002 /* Save the URL pattern */
1003 if (create_url_spec(cur_fwd->url, vec[0]))
1005 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
1006 "directive in configuration file.");
1007 string_append(&config->proxy_args,
1008 "<br>\nWARNING: Bad URL specifier for "
1009 "forward-socks4a directive in configuration file.");
1013 /* Parse the SOCKS proxy host[:port] */
1016 cur_fwd->gateway_host = strdup(p);
1018 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1021 cur_fwd->gateway_port = atoi(p);
1023 if (cur_fwd->gateway_port <= 0)
1025 cur_fwd->gateway_port = 1080;
1028 /* Parse the parent HTTP proxy host[:port] */
1031 if (strcmp(p, ".") != 0)
1033 cur_fwd->forward_host = strdup(p);
1035 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1038 cur_fwd->forward_port = atoi(p);
1041 if (cur_fwd->forward_port <= 0)
1043 cur_fwd->forward_port = 8000;
1048 cur_fwd->next = config->forward;
1049 config->forward = cur_fwd;
1053 /* *************************************************************************
1054 * jarfile jar-file-name
1055 * In logdir by default
1056 * *************************************************************************/
1057 #ifdef FEATURE_COOKIE_JAR
1059 freez(config->jarfile);
1060 config->jarfile = make_path(config->logdir, arg);
1062 #endif /* def FEATURE_COOKIE_JAR */
1064 /* *************************************************************************
1065 * listen-address [ip][:port]
1066 * *************************************************************************/
1067 case hash_listen_address :
1068 freez(config->haddr);
1069 config->haddr = strdup(arg);
1072 /* *************************************************************************
1073 * logdir directory-name
1074 * *************************************************************************/
1076 freez(config->logdir);
1077 config->logdir = make_path(NULL, arg);
1080 /* *************************************************************************
1081 * logfile log-file-name
1082 * In logdir by default
1083 * *************************************************************************/
1085 freez(config->logfile);
1086 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1089 /* *************************************************************************
1090 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1091 * *************************************************************************/
1093 case hash_permit_access:
1094 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1096 if ((vec_count != 1) && (vec_count != 2))
1098 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1099 "permit-access directive in configuration file.");
1100 string_append(&config->proxy_args,
1101 "<br>\nWARNING: Wrong number of parameters for "
1102 "permit-access directive in configuration file.<br><br>\n");
1107 /* allocate a new node */
1108 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1110 if (cur_acl == NULL)
1112 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1113 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1116 cur_acl->action = ACL_PERMIT;
1118 if (acl_addr(vec[0], cur_acl->src) < 0)
1120 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1121 "directive in configuration file: \"%s\"", vec[0]);
1122 string_append(&config->proxy_args,
1123 "<br>\nWARNING: Invalid source IP for permit-access directive"
1124 " in configuration file: \"");
1125 string_append(&config->proxy_args,
1127 string_append(&config->proxy_args,
1134 if (acl_addr(vec[1], cur_acl->dst) < 0)
1136 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1137 "permit-access directive in configuration file: \"%s\"",
1139 string_append(&config->proxy_args,
1140 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1141 " in configuration file: \"");
1142 string_append(&config->proxy_args,
1144 string_append(&config->proxy_args,
1152 * Add it to the list. Note we reverse the list to get the
1153 * behaviour the user expects. With both the ACL and
1154 * actions file, the last match wins. However, the internal
1155 * implementations are different: The actions file is stored
1156 * in the same order as the file, and scanned completely.
1157 * With the ACL, we reverse the order as we load it, then
1158 * when we scan it we stop as soon as we get a match.
1160 cur_acl->next = config->acl;
1161 config->acl = cur_acl;
1164 #endif /* def FEATURE_ACL */
1166 /* *************************************************************************
1167 * proxy-info-url url
1168 * *************************************************************************/
1169 case hash_proxy_info_url :
1170 freez(config->proxy_info_url);
1171 config->proxy_info_url = strdup(arg);
1174 /* *************************************************************************
1175 * re_filterfile file-name
1176 * In confdir by default.
1177 * *************************************************************************/
1178 case hash_filterfile :
1179 freez(config->re_filterfile);
1180 config->re_filterfile = make_path(config->confdir, arg);
1183 /* *************************************************************************
1185 * *************************************************************************/
1186 case hash_single_threaded :
1187 config->multi_threaded = 0;
1190 /* *************************************************************************
1192 * *************************************************************************/
1193 #ifdef FEATURE_TOGGLE
1195 g_bToggleIJB = atoi(arg);
1197 #endif /* def FEATURE_TOGGLE */
1199 /* *************************************************************************
1200 * trust-info-url url
1201 * *************************************************************************/
1202 #ifdef FEATURE_TRUST
1203 case hash_trust_info_url :
1204 enlist(config->trust_info, arg);
1206 #endif /* def FEATURE_TRUST */
1208 /* *************************************************************************
1209 * trustfile filename
1210 * (In confdir by default.)
1211 * *************************************************************************/
1212 #ifdef FEATURE_TRUST
1213 case hash_trustfile :
1214 freez(config->trustfile);
1215 config->trustfile = make_path(config->confdir, arg);
1217 #endif /* def FEATURE_TRUST */
1219 /* *************************************************************************
1221 * *************************************************************************/
1222 case hash_usermanual :
1223 freez(config->usermanual);
1224 config->usermanual = strdup(arg);
1227 /* *************************************************************************
1228 * Win32 Console options:
1229 * *************************************************************************/
1231 /* *************************************************************************
1233 * *************************************************************************/
1235 case hash_hide_console :
1238 #endif /*def _WIN_CONSOLE*/
1241 /* *************************************************************************
1242 * Win32 GUI options:
1243 * *************************************************************************/
1245 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1246 /* *************************************************************************
1247 * activity-animation (0|1)
1248 * *************************************************************************/
1249 case hash_activity_animation :
1250 g_bShowActivityAnimation = atoi(arg);
1253 /* *************************************************************************
1254 * close-button-minimizes (0|1)
1255 * *************************************************************************/
1256 case hash_close_button_minimizes :
1257 g_bCloseHidesWindow = atoi(arg);
1260 /* *************************************************************************
1261 * log-buffer-size (0|1)
1262 * *************************************************************************/
1263 case hash_log_buffer_size :
1264 g_bLimitBufferSize = atoi(arg);
1267 /* *************************************************************************
1268 * log-font-name fontnane
1269 * *************************************************************************/
1270 case hash_log_font_name :
1271 strcpy( g_szFontFaceName, arg );
1274 /* *************************************************************************
1276 * *************************************************************************/
1277 case hash_log_font_size :
1278 g_nFontSize = atoi(arg);
1281 /* *************************************************************************
1282 * log-highlight-messages (0|1)
1283 * *************************************************************************/
1284 case hash_log_highlight_messages :
1285 g_bHighlightMessages = atoi(arg);
1288 /* *************************************************************************
1290 * *************************************************************************/
1291 case hash_log_max_lines :
1292 g_nMaxBufferLines = atoi(arg);
1295 /* *************************************************************************
1296 * log-messages (0|1)
1297 * *************************************************************************/
1298 case hash_log_messages :
1299 g_bLogMessages = atoi(arg);
1302 /* *************************************************************************
1303 * show-on-task-bar (0|1)
1304 * *************************************************************************/
1305 case hash_show_on_task_bar :
1306 g_bShowOnTaskBar = atoi(arg);
1309 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1312 /* *************************************************************************
1313 * Warnings about unsupported features
1314 * *************************************************************************/
1316 case hash_deny_access:
1317 #endif /* ndef FEATURE_ACL */
1318 #ifndef FEATURE_CGI_EDIT_ACTIONS
1319 case hash_enable_edit_actions:
1320 case hash_enable_remote_toggle:
1321 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1322 #ifndef FEATURE_COOKIE_JAR
1324 #endif /* ndef FEATURE_COOKIE_JAR */
1326 case hash_permit_access:
1327 #endif /* ndef FEATURE_ACL */
1328 #ifndef FEATURE_TOGGLE
1330 #endif /* ndef FEATURE_TOGGLE */
1331 #ifndef FEATURE_TRUST
1332 case hash_trustfile :
1333 case hash_trust_info_url :
1334 #endif /* ndef FEATURE_TRUST */
1336 #ifndef _WIN_CONSOLE
1337 case hash_hide_console :
1338 #endif /* ndef _WIN_CONSOLE */
1340 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1341 case hash_activity_animation :
1342 case hash_close_button_minimizes :
1343 case hash_log_buffer_size :
1344 case hash_log_font_name :
1345 case hash_log_font_size :
1346 case hash_log_highlight_messages :
1347 case hash_log_max_lines :
1348 case hash_log_messages :
1349 case hash_show_on_task_bar :
1350 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1351 /* These warnings are annoying - so hide them. -- Jon */
1352 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1355 /* *************************************************************************/
1357 /* *************************************************************************/
1359 * I decided that I liked this better as a warning than an
1360 * error. To change back to an error, just change log level
1361 * to LOG_LEVEL_FATAL.
1363 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1364 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1365 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1366 string_append(&config->proxy_args, buf);
1367 string_append(&config->proxy_args, "<br><br>\n");
1370 /* *************************************************************************/
1371 } /* end switch( hash_string(cmd) ) */
1372 } /* end while ( read_config_line(...) ) */
1376 if (NULL == config->proxy_args)
1378 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1381 init_error_log(Argv[0], config->logfile, config->debug);
1383 if (config->actions_file[0])
1385 add_loader(load_actions_file, config);
1388 if (config->re_filterfile)
1390 add_loader(load_re_filterfile, config);
1393 #ifdef FEATURE_TRUST
1394 if (config->trustfile)
1396 add_loader(load_trustfile, config);
1398 #endif /* def FEATURE_TRUST */
1400 #ifdef FEATURE_COOKIE_JAR
1401 if ( NULL != config->jarfile )
1403 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1405 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1406 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1408 setbuf(config->jar, NULL);
1410 #endif /* def FEATURE_COOKIE_JAR */
1412 if ( NULL == config->haddr )
1414 config->haddr = strdup( HADDR_DEFAULT );
1417 if ( NULL != config->haddr )
1419 if (NULL != (p = strchr(config->haddr, ':')))
1424 config->hport = atoi(p);
1428 if (config->hport <= 0)
1431 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1432 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1434 if (*config->haddr == '\0')
1436 config->haddr = NULL;
1441 * Want to run all the loaders once now.
1443 * Need to set up a fake csp, so they can get to the config.
1445 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1446 fake_csp->config = config;
1448 if (run_loader(fake_csp))
1451 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1452 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1456 /* FIXME: this is a kludge for win32 */
1457 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1459 g_actions_file = config->actions_file[0]; /* FIXME only works for first action file */
1460 g_re_filterfile = config->re_filterfile;
1462 #ifdef FEATURE_TRUST
1463 g_trustfile = config->trustfile;
1464 #endif /* def FEATURE_TRUST */
1467 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1468 /* FIXME: end kludge */
1471 config->need_bind = 1;
1473 if (current_configfile)
1475 struct configuration_spec * oldcfg = (struct configuration_spec *)
1476 current_configfile->f;
1478 * Check if config->haddr,hport == oldcfg->haddr,hport
1480 * The following could be written more compactly as a single,
1481 * (unreadably long) if statement.
1483 config->need_bind = 0;
1484 if (config->hport != oldcfg->hport)
1486 config->need_bind = 1;
1488 else if (config->haddr == NULL)
1490 if (oldcfg->haddr != NULL)
1492 config->need_bind = 1;
1495 else if (oldcfg->haddr == NULL)
1497 config->need_bind = 1;
1499 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1501 config->need_bind = 1;
1504 current_configfile->unloader = unload_configfile;
1507 fs->next = files->next;
1510 current_configfile = fs;
1516 /*********************************************************************
1518 * Function : savearg
1520 * Description : Called from `load_config'. It saves each non-empty
1521 * and non-comment line from config into
1522 * config->proxy_args. This is used to create the
1523 * show-proxy-args page. On error, frees
1524 * config->proxy_args and sets it to NULL
1527 * 1 : command = config setting that was found
1528 * 2 : argument = the setting's argument (if any)
1529 * 3 : config = Configuration to save into.
1533 *********************************************************************/
1534 static void savearg(char *command, char *argument, struct configuration_spec * config)
1544 * Add config option name embedded in
1545 * link to it's section in the user-manual
1547 buf = strdup("<a href=\"");
1548 string_append(&buf, config->usermanual);
1549 string_append(&buf, CONFIG_HELP_PREFIX);
1550 string_join (&buf, string_toupper(command));
1551 string_append(&buf, "\">");
1552 string_append(&buf, command);
1553 string_append(&buf, "</a> ");
1557 freez(config->proxy_args);
1561 if ( (NULL != argument) && ('\0' != *argument) )
1563 s = html_encode(argument);
1567 freez(config->proxy_args);
1571 if (strncmpic(argument, "http://", 7) == 0)
1573 string_append(&buf, "<a href=\"");
1574 string_append(&buf, s);
1575 string_append(&buf, "\">");
1576 string_join (&buf, s);
1577 string_append(&buf, "</a>");
1581 string_join (&buf, s);
1585 string_append(&buf, "<br>\n");
1587 string_join(&config->proxy_args, buf);