1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 2.3 2002/09/19 03:48:29 iwanttokeepanon Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/src/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 2.3 2002/09/19 03:48:29 iwanttokeepanon
39 * Just moved "int i" up 3 lines in function unload_configfile, out of the "ifdef FEATURE_ACL" clause. I disable ACL and it was not compiling because "int i" was ifdef(d) out. I noticed this in the past, but am just now in a spot where I can change/commit stuff ... long live broadband!
41 * Revision 2.2 2002/09/04 15:48:33 oes
42 * Synced with the stable branch:
43 * Revision 1.48.2.1 2002/08/21 17:58:05 oes
44 * Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
46 * Revision 2.1 2002/06/04 17:22:36 jongfoster
49 * Revision 2.0 2002/06/04 14:34:21 jongfoster
50 * Moving source files to src/
52 * Revision 1.48 2002/05/14 21:30:38 oes
53 * savearg now uses own linking code instead of (now special-cased) add_help_link
55 * Revision 1.47 2002/05/12 21:36:29 jongfoster
56 * Correcting function comments
58 * Revision 1.46 2002/04/26 12:55:14 oes
59 * - New option "user-manual", defaults to our site
60 * via project.h #define
61 * - savearg now embeds option names in help links
63 * Revision 1.45 2002/04/24 02:11:54 oes
64 * Jon's multiple AF patch: Allow up to MAX_ACTION_FILES actionsfile options
66 * Revision 1.44 2002/04/08 20:37:13 swa
69 * Revision 1.43 2002/04/08 20:36:50 swa
72 * Revision 1.42 2002/04/05 15:50:15 oes
73 * fix for invalid HTML proxy_args
75 * Revision 1.41 2002/03/31 17:19:00 jongfoster
76 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
78 * Revision 1.40 2002/03/26 22:29:55 swa
79 * we have a new homepage!
81 * Revision 1.39 2002/03/24 13:25:43 swa
82 * name change related issues
84 * Revision 1.38 2002/03/24 13:05:48 jongfoster
85 * Renaming re_filterfile to filterfile
87 * Revision 1.37 2002/03/16 23:54:06 jongfoster
88 * Adding graceful termination feature, to help look for memory leaks.
89 * If you enable this (which, by design, has to be done by hand
90 * editing config.h) and then go to http://i.j.b/die, then the program
91 * will exit cleanly after the *next* request. It should free all the
92 * memory that was used.
94 * Revision 1.36 2002/03/13 00:27:05 jongfoster
97 * Revision 1.35 2002/03/07 03:52:44 oes
98 * Set logging to tty for --no-daemon mode
100 * Revision 1.34 2002/03/06 23:14:35 jongfoster
101 * Trivial cosmetic changes to make function comments easier to find.
103 * Revision 1.33 2002/03/05 04:52:42 oes
104 * Deleted non-errlog debugging code
106 * Revision 1.32 2002/03/04 18:24:53 oes
107 * Re-enabled output of unknown config directive hash
109 * Revision 1.31 2002/03/03 15:07:20 oes
110 * Re-enabled automatic config reloading
112 * Revision 1.30 2002/01/22 23:31:43 jongfoster
113 * Replacing strsav() with string_append()
115 * Revision 1.29 2002/01/17 21:02:30 jongfoster
116 * Moving all our URL and URL pattern parsing code to urlmatch.c.
118 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
120 * Revision 1.28 2001/12/30 14:07:32 steudten
121 * - Add signal handling (unix)
122 * - Add SIGHUP handler (unix)
123 * - Add creation of pidfile (unix)
124 * - Add action 'top' in rc file (RH)
125 * - Add entry 'SIGNALS' to manpage
126 * - Add exit message to logfile (unix)
128 * Revision 1.27 2001/11/07 00:02:13 steudten
129 * Add line number in error output for lineparsing for
130 * actionsfile and configfile.
131 * Special handling for CLF added.
133 * Revision 1.26 2001/11/05 21:41:43 steudten
134 * Add changes to be a real daemon just for unix os.
135 * (change cwd to /, detach from controlling tty, set
136 * process group and session leader to the own process.
138 * Add some fatal-error log message for failed malloc().
139 * Add '-d' if compiled with 'configure --with-debug' to
140 * enable debug output.
142 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
143 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
144 * threads to call select() simultaneously. So, it's time to do a real, live,
145 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
146 * (native). Both versions will work, but using __OS2__ offers multi-threading.
148 * Revision 1.24 2001/10/23 21:40:30 jongfoster
149 * Added support for enable-edit-actions and enable-remote-toggle config
152 * Revision 1.23 2001/10/07 15:36:00 oes
153 * Introduced new config option "buffer-limit"
155 * Revision 1.22 2001/09/22 16:36:59 jongfoster
156 * Removing unused parameter fs from read_config_line()
158 * Revision 1.21 2001/09/16 17:10:43 jongfoster
159 * Moving function savearg() here, since it was the only thing left in
162 * Revision 1.20 2001/07/30 22:08:36 jongfoster
163 * Tidying up #defines:
164 * - All feature #defines are now of the form FEATURE_xxx
165 * - Permanently turned off WIN_GUI_EDIT
166 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
168 * Revision 1.19 2001/07/15 17:45:16 jongfoster
169 * Removing some unused #includes
171 * Revision 1.18 2001/07/13 14:01:14 oes
172 * - Removed all #ifdef PCRS
173 * - Removed vim-settings
175 * Revision 1.17 2001/06/29 13:31:03 oes
176 * - Improved comments
177 * - Fixed (actionsfile) and sorted hashes
178 * - Introduced admin_address and proxy-info-url
179 * as config parameters
180 * - Renamed config->proxy_args_invocation (which didn't have
181 * the invocation but the options!) to config->proxy_args
182 * - Various adaptions
183 * - Removed logentry from cancelled commit
185 * Revision 1.16 2001/06/09 10:55:28 jongfoster
186 * Changing BUFSIZ ==> BUFFER_SIZE
188 * Revision 1.15 2001/06/07 23:13:40 jongfoster
189 * Merging ACL and forward files into config file.
190 * Cosmetic: Sorting config file options alphabetically.
191 * Cosmetic: Adding brief syntax comments to config file options.
193 * Revision 1.14 2001/06/07 14:46:25 joergs
194 * Missing make_path() added for re_filterfile.
196 * Revision 1.13 2001/06/05 22:33:54 jongfoster
198 * Fixed minor memory leak.
199 * Also now uses make_path to prepend the pathnames.
201 * Revision 1.12 2001/06/05 20:04:09 jongfoster
202 * Now uses _snprintf() in place of snprintf() under Win32.
204 * Revision 1.11 2001/06/04 18:31:58 swa
205 * files are now prefixed with either `confdir' or `logdir'.
206 * `make redhat-dist' replaces both entries confdir and logdir
209 * Revision 1.10 2001/06/03 19:11:54 oes
210 * introduced confdir option
212 * Revision 1.9 2001/06/01 20:06:24 jongfoster
213 * Removed support for "tinygif" option - moved to actions file.
215 * Revision 1.8 2001/05/31 21:27:13 jongfoster
216 * Removed many options from the config file and into the
217 * "actions" file: add_forwarded, suppress_vanilla_wafer,
218 * wafer, add_header, user_agent, referer, from
219 * Also globally replaced "permission" with "action".
221 * Revision 1.7 2001/05/29 09:50:24 jongfoster
222 * Unified blocklist/imagelist/permissionslist.
223 * File format is still under discussion, but the internal changes
226 * Also modified interceptor behaviour:
227 * - We now intercept all URLs beginning with one of the following
228 * prefixes (and *only* these prefixes):
230 * * http://ijbswa.sf.net/config/
231 * * http://ijbswa.sourceforge.net/config/
232 * - New interceptors "home page" - go to http://i.j.b/ to see it.
233 * - Internal changes so that intercepted and fast redirect pages
234 * are not replaced with an image.
235 * - Interceptors now have the option to send a binary page direct
236 * to the client. (i.e. ijb-send-banner uses this)
237 * - Implemented show-url-info interceptor. (Which is why I needed
238 * the above interceptors changes - a typical URL is
239 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
240 * The previous mechanism would not have intercepted that, and
241 * if it had been intercepted then it then it would have replaced
244 * Revision 1.6 2001/05/26 00:28:36 jongfoster
245 * Automatic reloading of config file.
246 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
247 * Most of the global variables have been moved to a new
248 * struct configuration_spec, accessed through csp->config->globalname
249 * Most of the globals remaining are used by the Win32 GUI.
251 * Revision 1.5 2001/05/25 22:34:30 jongfoster
254 * Revision 1.4 2001/05/22 18:46:04 oes
256 * - Enabled filtering banners by size rather than URL
257 * by adding patterns that replace all standard banner
258 * sizes with the "Junkbuster" gif to the re_filterfile
260 * - Enabled filtering WebBugs by providing a pattern
261 * which kills all 1x1 images
263 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
264 * which is selected by the (nonstandard and therefore
265 * capital) letter 'U' in the option string.
266 * It causes the quantifiers to be ungreedy by default.
267 * Appending a ? turns back to greedy (!).
269 * - Added a new interceptor ijb-send-banner, which
270 * sends back the "Junkbuster" gif. Without imagelist or
271 * MSIE detection support, or if tinygif = 1, or the
272 * URL isn't recognized as an imageurl, a lame HTML
273 * explanation is sent instead.
275 * - Added new feature, which permits blocking remote
276 * script redirects and firing back a local redirect
278 * The feature is conditionally compiled, i.e. it
279 * can be disabled with --disable-fast-redirects,
280 * plus it must be activated by a "fast-redirects"
281 * line in the config file, has its own log level
282 * and of course wants to be displayed by show-proxy-args
283 * Note: Boy, all the #ifdefs in 1001 locations and
284 * all the fumbling with configure.in and acconfig.h
285 * were *way* more work than the feature itself :-(
287 * - Because a generic redirect template was needed for
288 * this, tinygif = 3 now uses the same.
290 * - Moved GIFs, and other static HTTP response templates
295 * - Removed some >400 CRs again (Jon, you really worked
298 * Revision 1.3 2001/05/20 01:21:20 jongfoster
299 * Version 2.9.4 checkin.
300 * - Merged popupfile and cookiefile, and added control over PCRS
301 * filtering, in new "permissionsfile".
302 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
303 * file error you now get a message box (in the Win32 GUI) rather
304 * than the program exiting with no explanation.
305 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
307 * - Removed tabs from "config"
308 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
309 * - Bumped up version number.
311 * Revision 1.2 2001/05/17 23:01:01 oes
312 * - Cleaned CRLF's from the sources and related files
314 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
315 * Initial import of version 2.9.3 source tree
318 *********************************************************************/
324 #include <sys/types.h>
338 # include <windows.h>
341 # ifndef _WIN_CONSOLE
343 # endif /* ndef _WIN_CONSOLE */
345 /* VC++ has "_snprintf", not "snprintf" */
346 #define snprintf _snprintf
348 #else /* ifndef _WIN32 */
352 # include <sys/wait.h>
354 # include <sys/time.h>
355 # include <sys/stat.h>
365 #include "miscutil.h"
369 #include "urlmatch.h"
372 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
375 * Fix a problem with Solaris. There should be no effect on other
377 * Solaris's isspace() is a macro which uses it's argument directly
378 * as an array index. Therefore we need to make sure that high-bit
379 * characters generate +ve values, and ideally we also want to make
380 * the argument match the declared parameter type of "int".
382 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
383 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
385 #ifdef FEATURE_TOGGLE
387 * Indicates if Privoxy is currently enabled.
389 int g_bToggleIJB = 1; /* Privoxy is enabled by default. */
390 #endif /* def FEATURE_TOGGLE */
393 * The filename of the config file
395 const char *configfile = NULL;
398 * Global argument count, so that CGI functions can access the
404 * Global argument list, so that CGI functions can access the
407 const char **Argv = NULL;
410 * The configuration file which is currently loaded.
412 static struct file_list *current_configfile = NULL;
416 * This takes the "cryptic" hash of each keyword and aliases them to
417 * something a little more readable. This also makes changing the
418 * hash values easier if they should change or the hash algorthm changes.
419 * Use the included "hash" program to find out what the hash will be
420 * for any string supplied on the command line. (Or just put it in the
421 * config file and read the number from the error message in the log).
423 * Please keep this list sorted alphabetically (but with the Windows
424 * console and GUI specific options last).
427 #define hash_actions_file 1196306641ul /**< "actionsfile" */
428 #define hash_activity_console_address 18904208ul /**< "activity-console-address" */
429 #define hash_activity_console_update_freq 3442780376ul /**< "activity-console-update-freq" */
430 #define hash_admin_address 4112573064ul /**< "admin-address" */
431 #define hash_buffer_limit 1881726070ul /**< "buffer-limit */
432 #define hash_confdir 1978389ul /**< "confdir" */
433 #define hash_debug 78263ul /**< "debug" */
434 #define hash_deny_access 1227333715ul /**< "deny-access" */
435 #define hash_enable_edit_actions 2517097536ul /**< "enable-edit-actions" */
436 #define hash_enable_remote_toggle 2979744683ul /**< "enable-remote-toggle" */
437 #define hash_filterfile 250887266ul /**< "filterfile" */
438 #define hash_forward 2029845ul /**< "forward" */
439 #define hash_forward_socks4 3963965521ul /**< "forward-socks4" */
440 #define hash_forward_socks4a 2639958518ul /**< "forward-socks4a" */
441 #define hash_jarfile 2046641ul /**< "jarfile" */
442 #define hash_listen_address 1255650842ul /**< "listen-address" */
443 #define hash_logdir 422889ul /**< "logdir" */
444 #define hash_logfile 2114766ul /**< "logfile" */
445 #define hash_permit_access 3587953268ul /**< "permit-access" */
446 #define hash_proxy_info_url 3903079059ul /**< "proxy-info-url" */
447 #define hash_single_threaded 4250084780ul /**< "single-threaded" */
448 #define hash_suppress_blocklists 1948693308ul /**< "suppress-blocklists" */
449 #define hash_toggle 447966ul /**< "toggle" */
450 #define hash_trust_info_url 430331967ul /**< "trust-info-url" */
451 #define hash_trustfile 56494766ul /**< "trustfile" */
452 #define hash_usermanual 1416668518ul /**< "user-manual" */
454 #define hash_activity_animation 1817904738ul /**< "activity-animation" */
455 #define hash_close_button_minimizes 3651284693ul /**< "close-button-minimizes" */
456 #define hash_hide_console 2048809870ul /**< "hide-console" */
457 #define hash_log_buffer_size 2918070425ul /**< "log-buffer-size" */
458 #define hash_log_font_name 2866730124ul /**< "log-font-name" */
459 #define hash_log_font_size 2866731014ul /**< "log-font-size" */
460 #define hash_log_highlight_messages 4032101240ul /**< "log-highlight-messages" */
461 #define hash_log_max_lines 2868344173ul /**< "log-max-lines" */
462 #define hash_log_messages 2291744899ul /**< "log-messages" */
463 #define hash_show_on_task_bar 215410365ul /**< "show-on-task-bar" */
465 static void savearg(char *command, char *argument, struct configuration_spec * config);
467 /*********************************************************************
469 * Function : unload_configfile
471 * Description : Free the config structure and all components.
474 * 1 : data: struct configuration_spec to unload
478 *********************************************************************/
479 void unload_configfile (void * data)
481 struct configuration_spec * config = (struct configuration_spec *)data;
482 struct forward_spec *cur_fwd = config->forward;
485 struct access_control_list *cur_acl = config->acl;
487 while (cur_acl != NULL)
489 struct access_control_list * next_acl = cur_acl->next;
494 #endif /* def FEATURE_ACL */
496 while (cur_fwd != NULL)
498 struct forward_spec * next_fwd = cur_fwd->next;
499 free_url_spec(cur_fwd->url);
501 freez(cur_fwd->gateway_host);
502 freez(cur_fwd->forward_host);
506 config->forward = NULL;
508 #ifdef FEATURE_COOKIE_JAR
509 if ( NULL != config->jar )
511 fclose( config->jar );
514 #endif /* def FEATURE_COOKIE_JAR */
516 freez(config->confdir);
517 freez(config->logdir);
519 freez(config->haddr);
520 freez(config->logfile);
522 for (i = 0; i < MAX_ACTION_FILES; i++)
524 freez(config->actions_file_short[i]);
525 freez(config->actions_file[i]);
528 freez(config->admin_address);
529 freez(config->proxy_info_url);
530 freez(config->proxy_args);
531 freez(config->usermanual);
533 #ifdef FEATURE_COOKIE_JAR
534 freez(config->jarfile);
535 #endif /* def FEATURE_COOKIE_JAR */
537 freez(config->re_filterfile);
542 #ifdef FEATURE_GRACEFUL_TERMINATION
543 /*********************************************************************
545 * Function : unload_current_config_file
547 * Description : Unloads current config file - reset to state at
548 * beginning of program.
554 *********************************************************************/
555 void unload_current_config_file(void)
557 if (current_configfile)
559 current_configfile->unloader = unload_configfile;
560 current_configfile = NULL;
566 /*********************************************************************
568 * Function : load_config
570 * Description : Load the config file and all parameters.
574 * Returns : The configuration_spec, or NULL on error.
576 *********************************************************************/
577 struct configuration_spec * load_config(void)
579 char buf[BUFFER_SIZE];
581 FILE *configfp = NULL;
582 struct configuration_spec * config = NULL;
583 struct client_state * fake_csp;
584 struct file_list *fs;
585 unsigned long linenum = 0;
588 if ( !check_file_changed(current_configfile, configfile, &fs))
590 /* No need to load */
591 return ((struct configuration_spec *)current_configfile->f);
595 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
599 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
601 #ifdef FEATURE_TOGGLE
603 #endif /* def FEATURE_TOGGLE */
605 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
611 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
612 /* Never get here - LOG_LEVEL_FATAL causes program exit */
616 * This is backwards from how it's usually done.
617 * Following the usual pattern, "fs" would be stored in a member
618 * variable in "csp", and then we'd access "config" from "fs->f",
619 * using a cast. However, "config" is used so often that a
620 * cast each time would be very ugly, and the extra indirection
621 * would waste CPU cycles. Therefore we store "config" in
622 * "csp->config", and "fs" in "csp->config->config_file_list".
624 config->config_file_list = fs;
629 config->multi_threaded = 1;
630 config->hport = HADDR_PORT;
631 config->buffer_limit = 4096 * 1024;
632 config->usermanual = strdup(USER_MANUAL_URL);
633 config->proxy_args = strdup("");
634 #ifdef FEATURE_ACTIVITY_CONSOLE
635 config->activity_port = ACTIVTY_ADDR_PORT;
636 config->activity_freq = 5;
637 #endif /* def FEATURE_ACTIVITY_CONSOLE */
639 if ((configfp = fopen(configfile, "r")) == NULL)
641 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
643 /* Never get here - LOG_LEVEL_FATAL causes program exit */
646 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
648 char cmd[BUFFER_SIZE];
649 char arg[BUFFER_SIZE];
650 char tmp[BUFFER_SIZE];
652 struct access_control_list *cur_acl;
653 #endif /* def FEATURE_ACL */
654 struct forward_spec *cur_fwd;
660 /* Copy command (i.e. up to space or tab) into cmd */
663 while (*p && (*p != ' ') && (*p != '\t'))
669 /* Skip over the whitespace in buf */
670 while (*p && ((*p == ' ') || (*p == '\t')))
675 /* Copy the argument into arg */
678 /* Should never happen, but check this anyway */
684 /* Make sure the command field is lower case */
689 *p = ijb_tolower(*p);
693 /* Save the argument for show-proxy-args */
694 savearg(cmd, arg, config);
697 switch( hash_string( cmd ) )
699 /* *************************************************************************
700 * actionsfile actions-file-name
701 * In confdir by default
702 * *************************************************************************/
703 case hash_actions_file :
705 while ((i < MAX_ACTION_FILES) && (NULL != config->actions_file[i]))
710 if (i >= MAX_ACTION_FILES)
712 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
713 "(You can increase this limit by changing MAX_ACTION_FILES in project.h and recompiling).",
716 config->actions_file_short[i] = strdup(arg);
717 p = malloc(strlen(arg) + sizeof(".action"));
720 log_error(LOG_LEVEL_FATAL, "Out of memory");
723 strcat(p, ".action");
724 config->actions_file[i] = make_path(config->confdir, p);
728 /* *************************************************************************
729 * activity-console-address [ip][:port]
730 * *************************************************************************/
731 #ifdef FEATURE_ACTIVITY_CONSOLE
732 case hash_activity_console_address :
733 freez(config->activity_address);
734 config->activity_address = strdup(arg);
736 #endif /* def FEATURE_ACTIVITY_CONSOLE */
738 /* *************************************************************************
739 * activity-console-update-freq
740 * *************************************************************************/
741 #ifdef FEATURE_ACTIVITY_CONSOLE
742 case hash_activity_console_update_freq :
743 config->activity_freq = atoi(arg);
744 if (config->activity_freq < 1)
745 config->activity_freq = 5;
747 #endif /* def FEATURE_ACTIVITY_CONSOLE */
749 /* *************************************************************************
750 * admin-address email-address
751 * *************************************************************************/
752 case hash_admin_address :
753 freez(config->admin_address);
754 config->admin_address = strdup(arg);
757 /* *************************************************************************
759 * *************************************************************************/
760 case hash_buffer_limit :
761 config->buffer_limit = (size_t) 1024 * atoi(arg);
764 /* *************************************************************************
765 * confdir directory-name
766 * *************************************************************************/
768 freez(config->confdir);
769 config->confdir = make_path( NULL, arg);
772 /* *************************************************************************
774 * Specifies debug level, multiple values are ORed together.
775 * *************************************************************************/
777 config->debug |= atoi(arg);
780 /* *************************************************************************
781 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
782 * *************************************************************************/
784 case hash_deny_access:
785 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
787 if ((vec_count != 1) && (vec_count != 2))
789 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
790 "deny-access directive in configuration file.");
791 string_append(&config->proxy_args,
792 "<br>\nWARNING: Wrong number of parameters for "
793 "deny-access directive in configuration file.<br><br>\n");
797 /* allocate a new node */
798 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
802 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
803 /* Never get here - LOG_LEVEL_FATAL causes program exit */
806 cur_acl->action = ACL_DENY;
808 if (acl_addr(vec[0], cur_acl->src) < 0)
810 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
811 "directive in configuration file: \"%s\"", vec[0]);
812 string_append(&config->proxy_args,
813 "<br>\nWARNING: Invalid source IP for deny-access directive"
814 " in configuration file: \"");
815 string_append(&config->proxy_args,
817 string_append(&config->proxy_args,
824 if (acl_addr(vec[1], cur_acl->dst) < 0)
826 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
827 "directive in configuration file: \"%s\"", vec[0]);
828 string_append(&config->proxy_args,
829 "<br>\nWARNING: Invalid destination IP for deny-access directive"
830 " in configuration file: \"");
831 string_append(&config->proxy_args,
833 string_append(&config->proxy_args,
841 * Add it to the list. Note we reverse the list to get the
842 * behaviour the user expects. With both the ACL and
843 * actions file, the last match wins. However, the internal
844 * implementations are different: The actions file is stored
845 * in the same order as the file, and scanned completely.
846 * With the ACL, we reverse the order as we load it, then
847 * when we scan it we stop as soon as we get a match.
849 cur_acl->next = config->acl;
850 config->acl = cur_acl;
853 #endif /* def FEATURE_ACL */
855 /* *************************************************************************
856 * enable-edit-actions 0|1
857 * *************************************************************************/
858 #ifdef FEATURE_CGI_EDIT_ACTIONS
859 case hash_enable_edit_actions:
860 if ((*arg != '\0') && (0 != atoi(arg)))
862 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
866 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
869 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
871 /* *************************************************************************
872 * enable-remote-toggle 0|1
873 * *************************************************************************/
874 #ifdef FEATURE_CGI_EDIT_ACTIONS
875 case hash_enable_remote_toggle:
876 if ((*arg != '\0') && (0 != atoi(arg)))
878 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
882 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
885 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
887 /* *************************************************************************
888 * forward url-pattern (.|http-proxy-host[:port])
889 * *************************************************************************/
891 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
895 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
896 "directive in configuration file.");
897 string_append(&config->proxy_args,
898 "<br>\nWARNING: Wrong number of parameters for "
899 "forward directive in configuration file.");
903 /* allocate a new node */
904 cur_fwd = zalloc(sizeof(*cur_fwd));
907 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
908 /* Never get here - LOG_LEVEL_FATAL causes program exit */
912 cur_fwd->type = SOCKS_NONE;
914 /* Save the URL pattern */
915 if (create_url_spec(cur_fwd->url, vec[0]))
917 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
918 "directive in configuration file.");
919 string_append(&config->proxy_args,
920 "<br>\nWARNING: Bad URL specifier for "
921 "forward directive in configuration file.");
925 /* Parse the parent HTTP proxy host:port */
928 if (strcmp(p, ".") != 0)
930 cur_fwd->forward_host = strdup(p);
932 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
935 cur_fwd->forward_port = atoi(p);
938 if (cur_fwd->forward_port <= 0)
940 cur_fwd->forward_port = 8000;
945 cur_fwd->next = config->forward;
946 config->forward = cur_fwd;
950 /* *************************************************************************
951 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
952 * *************************************************************************/
953 case hash_forward_socks4:
954 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
958 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
959 "forward-socks4 directive in configuration file.");
960 string_append(&config->proxy_args,
961 "<br>\nWARNING: Wrong number of parameters for "
962 "forward-socks4 directive in configuration file.");
966 /* allocate a new node */
967 cur_fwd = zalloc(sizeof(*cur_fwd));
970 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
971 /* Never get here - LOG_LEVEL_FATAL causes program exit */
975 cur_fwd->type = SOCKS_4;
977 /* Save the URL pattern */
978 if (create_url_spec(cur_fwd->url, vec[0]))
980 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
981 "directive in configuration file.");
982 string_append(&config->proxy_args,
983 "<br>\nWARNING: Bad URL specifier for "
984 "forward-socks4 directive in configuration file.");
988 /* Parse the SOCKS proxy host[:port] */
991 if (strcmp(p, ".") != 0)
993 cur_fwd->gateway_host = strdup(p);
995 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
998 cur_fwd->gateway_port = atoi(p);
1000 if (cur_fwd->gateway_port <= 0)
1002 cur_fwd->gateway_port = 1080;
1006 /* Parse the parent HTTP proxy host[:port] */
1009 if (strcmp(p, ".") != 0)
1011 cur_fwd->forward_host = strdup(p);
1013 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1016 cur_fwd->forward_port = atoi(p);
1019 if (cur_fwd->forward_port <= 0)
1021 cur_fwd->forward_port = 8000;
1026 cur_fwd->next = config->forward;
1027 config->forward = cur_fwd;
1031 /* *************************************************************************
1032 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
1033 * *************************************************************************/
1034 case hash_forward_socks4a:
1035 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1039 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1040 "forward-socks4a directive in configuration file.");
1041 string_append(&config->proxy_args,
1042 "<br>\nWARNING: Wrong number of parameters for "
1043 "forward-socks4a directive in configuration file.");
1047 /* allocate a new node */
1048 cur_fwd = zalloc(sizeof(*cur_fwd));
1049 if (cur_fwd == NULL)
1051 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1052 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1056 cur_fwd->type = SOCKS_4A;
1058 /* Save the URL pattern */
1059 if (create_url_spec(cur_fwd->url, vec[0]))
1061 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
1062 "directive in configuration file.");
1063 string_append(&config->proxy_args,
1064 "<br>\nWARNING: Bad URL specifier for "
1065 "forward-socks4a directive in configuration file.");
1069 /* Parse the SOCKS proxy host[:port] */
1072 cur_fwd->gateway_host = strdup(p);
1074 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1077 cur_fwd->gateway_port = atoi(p);
1079 if (cur_fwd->gateway_port <= 0)
1081 cur_fwd->gateway_port = 1080;
1084 /* Parse the parent HTTP proxy host[:port] */
1087 if (strcmp(p, ".") != 0)
1089 cur_fwd->forward_host = strdup(p);
1091 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1094 cur_fwd->forward_port = atoi(p);
1097 if (cur_fwd->forward_port <= 0)
1099 cur_fwd->forward_port = 8000;
1104 cur_fwd->next = config->forward;
1105 config->forward = cur_fwd;
1109 /* *************************************************************************
1110 * jarfile jar-file-name
1111 * In logdir by default
1112 * *************************************************************************/
1113 #ifdef FEATURE_COOKIE_JAR
1115 freez(config->jarfile);
1116 config->jarfile = make_path(config->logdir, arg);
1118 #endif /* def FEATURE_COOKIE_JAR */
1120 /* *************************************************************************
1121 * listen-address [ip][:port]
1122 * *************************************************************************/
1123 case hash_listen_address :
1124 freez(config->haddr);
1125 config->haddr = strdup(arg);
1128 /* *************************************************************************
1129 * logdir directory-name
1130 * *************************************************************************/
1132 freez(config->logdir);
1133 config->logdir = make_path(NULL, arg);
1136 /* *************************************************************************
1137 * logfile log-file-name
1138 * In logdir by default
1139 * *************************************************************************/
1141 freez(config->logfile);
1142 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1145 /* *************************************************************************
1146 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1147 * *************************************************************************/
1149 case hash_permit_access:
1150 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1152 if ((vec_count != 1) && (vec_count != 2))
1154 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1155 "permit-access directive in configuration file.");
1156 string_append(&config->proxy_args,
1157 "<br>\nWARNING: Wrong number of parameters for "
1158 "permit-access directive in configuration file.<br><br>\n");
1163 /* allocate a new node */
1164 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1166 if (cur_acl == NULL)
1168 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1169 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1172 cur_acl->action = ACL_PERMIT;
1174 if (acl_addr(vec[0], cur_acl->src) < 0)
1176 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1177 "directive in configuration file: \"%s\"", vec[0]);
1178 string_append(&config->proxy_args,
1179 "<br>\nWARNING: Invalid source IP for permit-access directive"
1180 " in configuration file: \"");
1181 string_append(&config->proxy_args,
1183 string_append(&config->proxy_args,
1190 if (acl_addr(vec[1], cur_acl->dst) < 0)
1192 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1193 "permit-access directive in configuration file: \"%s\"",
1195 string_append(&config->proxy_args,
1196 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1197 " in configuration file: \"");
1198 string_append(&config->proxy_args,
1200 string_append(&config->proxy_args,
1208 * Add it to the list. Note we reverse the list to get the
1209 * behaviour the user expects. With both the ACL and
1210 * actions file, the last match wins. However, the internal
1211 * implementations are different: The actions file is stored
1212 * in the same order as the file, and scanned completely.
1213 * With the ACL, we reverse the order as we load it, then
1214 * when we scan it we stop as soon as we get a match.
1216 cur_acl->next = config->acl;
1217 config->acl = cur_acl;
1220 #endif /* def FEATURE_ACL */
1222 /* *************************************************************************
1223 * proxy-info-url url
1224 * *************************************************************************/
1225 case hash_proxy_info_url :
1226 freez(config->proxy_info_url);
1227 config->proxy_info_url = strdup(arg);
1230 /* *************************************************************************
1231 * re_filterfile file-name
1232 * In confdir by default.
1233 * *************************************************************************/
1234 case hash_filterfile :
1235 freez(config->re_filterfile);
1236 config->re_filterfile = make_path(config->confdir, arg);
1239 /* *************************************************************************
1241 * *************************************************************************/
1242 case hash_single_threaded :
1243 config->multi_threaded = 0;
1246 /* *************************************************************************
1248 * *************************************************************************/
1249 #ifdef FEATURE_TOGGLE
1251 g_bToggleIJB = atoi(arg);
1253 #endif /* def FEATURE_TOGGLE */
1255 /* *************************************************************************
1256 * trust-info-url url
1257 * *************************************************************************/
1258 #ifdef FEATURE_TRUST
1259 case hash_trust_info_url :
1260 enlist(config->trust_info, arg);
1262 #endif /* def FEATURE_TRUST */
1264 /* *************************************************************************
1265 * trustfile filename
1266 * (In confdir by default.)
1267 * *************************************************************************/
1268 #ifdef FEATURE_TRUST
1269 case hash_trustfile :
1270 freez(config->trustfile);
1271 config->trustfile = make_path(config->confdir, arg);
1273 #endif /* def FEATURE_TRUST */
1275 /* *************************************************************************
1277 * *************************************************************************/
1278 case hash_usermanual :
1279 freez(config->usermanual);
1280 config->usermanual = strdup(arg);
1283 /* *************************************************************************
1284 * Win32 Console options:
1285 * *************************************************************************/
1287 /* *************************************************************************
1289 * *************************************************************************/
1291 case hash_hide_console :
1294 #endif /*def _WIN_CONSOLE*/
1297 /* *************************************************************************
1298 * Win32 GUI options:
1299 * *************************************************************************/
1301 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1302 /* *************************************************************************
1303 * activity-animation (0|1)
1304 * *************************************************************************/
1305 case hash_activity_animation :
1306 g_bShowActivityAnimation = atoi(arg);
1309 /* *************************************************************************
1310 * close-button-minimizes (0|1)
1311 * *************************************************************************/
1312 case hash_close_button_minimizes :
1313 g_bCloseHidesWindow = atoi(arg);
1316 /* *************************************************************************
1317 * log-buffer-size (0|1)
1318 * *************************************************************************/
1319 case hash_log_buffer_size :
1320 g_bLimitBufferSize = atoi(arg);
1323 /* *************************************************************************
1324 * log-font-name fontnane
1325 * *************************************************************************/
1326 case hash_log_font_name :
1327 strcpy( g_szFontFaceName, arg );
1330 /* *************************************************************************
1332 * *************************************************************************/
1333 case hash_log_font_size :
1334 g_nFontSize = atoi(arg);
1337 /* *************************************************************************
1338 * log-highlight-messages (0|1)
1339 * *************************************************************************/
1340 case hash_log_highlight_messages :
1341 g_bHighlightMessages = atoi(arg);
1344 /* *************************************************************************
1346 * *************************************************************************/
1347 case hash_log_max_lines :
1348 g_nMaxBufferLines = atoi(arg);
1351 /* *************************************************************************
1352 * log-messages (0|1)
1353 * *************************************************************************/
1354 case hash_log_messages :
1355 g_bLogMessages = atoi(arg);
1358 /* *************************************************************************
1359 * show-on-task-bar (0|1)
1360 * *************************************************************************/
1361 case hash_show_on_task_bar :
1362 g_bShowOnTaskBar = atoi(arg);
1365 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1368 /* *************************************************************************
1369 * Warnings about unsupported features
1370 * *************************************************************************/
1372 case hash_deny_access:
1373 #endif /* ndef FEATURE_ACL */
1374 #ifndef FEATURE_CGI_EDIT_ACTIONS
1375 case hash_enable_edit_actions:
1376 case hash_enable_remote_toggle:
1377 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1378 #ifndef FEATURE_COOKIE_JAR
1380 #endif /* ndef FEATURE_COOKIE_JAR */
1382 case hash_permit_access:
1383 #endif /* ndef FEATURE_ACL */
1384 #ifndef FEATURE_TOGGLE
1386 #endif /* ndef FEATURE_TOGGLE */
1387 #ifndef FEATURE_TRUST
1388 case hash_trustfile :
1389 case hash_trust_info_url :
1390 #endif /* ndef FEATURE_TRUST */
1392 #ifndef _WIN_CONSOLE
1393 case hash_hide_console :
1394 #endif /* ndef _WIN_CONSOLE */
1396 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1397 case hash_activity_animation :
1398 case hash_close_button_minimizes :
1399 case hash_log_buffer_size :
1400 case hash_log_font_name :
1401 case hash_log_font_size :
1402 case hash_log_highlight_messages :
1403 case hash_log_max_lines :
1404 case hash_log_messages :
1405 case hash_show_on_task_bar :
1406 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1407 /* These warnings are annoying - so hide them. -- Jon */
1408 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1411 /* *************************************************************************/
1413 /* *************************************************************************/
1415 * I decided that I liked this better as a warning than an
1416 * error. To change back to an error, just change log level
1417 * to LOG_LEVEL_FATAL.
1419 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1420 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1421 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1422 string_append(&config->proxy_args, buf);
1423 string_append(&config->proxy_args, "<br><br>\n");
1426 /* *************************************************************************/
1427 } /* end switch( hash_string(cmd) ) */
1428 } /* end while ( read_config_line(...) ) */
1432 if (NULL == config->proxy_args)
1434 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1437 init_error_log(Argv[0], config->logfile, config->debug);
1439 if (config->actions_file[0])
1441 add_loader(load_actions_file, config);
1444 if (config->re_filterfile)
1446 add_loader(load_re_filterfile, config);
1449 #ifdef FEATURE_TRUST
1450 if (config->trustfile)
1452 add_loader(load_trustfile, config);
1454 #endif /* def FEATURE_TRUST */
1456 #ifdef FEATURE_COOKIE_JAR
1457 if ( NULL != config->jarfile )
1459 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1461 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1462 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1464 setbuf(config->jar, NULL);
1466 #endif /* def FEATURE_COOKIE_JAR */
1468 #ifdef FEATURE_ACTIVITY_CONSOLE
1469 if ( NULL == config->activity_address )
1471 config->activity_address = strdup( ACTIVTY_ADDR_DEFAULT );
1474 if ( NULL != config->activity_address )
1476 if (NULL != (p = strchr(config->activity_address, ':')))
1481 config->activity_port = atoi(p);
1485 if (config->activity_port <= 0)
1488 log_error(LOG_LEVEL_ERROR, "invalid activity port spec %s", config->activity_address);
1490 if (*config->activity_address == '\0')
1492 config->activity_address = NULL;
1495 log_error(LOG_LEVEL_INFO, "Sending statistics updates to %s, port %d.", config->activity_address, config->activity_port);
1496 #endif /* def FEATURE_ACTIVITY_CONSOLE */
1498 if ( NULL == config->haddr )
1500 config->haddr = strdup( HADDR_DEFAULT );
1503 if ( NULL != config->haddr )
1505 if (NULL != (p = strchr(config->haddr, ':')))
1510 config->hport = atoi(p);
1514 if (config->hport <= 0)
1517 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1518 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1520 if (*config->haddr == '\0')
1522 config->haddr = NULL;
1527 * Want to run all the loaders once now.
1529 * Need to set up a fake csp, so they can get to the config.
1531 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1532 fake_csp->config = config;
1534 if (run_loader(fake_csp))
1537 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1538 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1542 /* FIXME: this is a kludge for win32 */
1543 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1545 g_default_actions_file = config->actions_file[1]; /* FIXME Hope this is default.action */
1546 g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1547 g_re_filterfile = config->re_filterfile;
1549 #ifdef FEATURE_TRUST
1550 g_trustfile = config->trustfile;
1551 #endif /* def FEATURE_TRUST */
1554 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1555 /* FIXME: end kludge */
1558 config->need_bind = 1;
1560 if (current_configfile)
1562 struct configuration_spec * oldcfg = (struct configuration_spec *)
1563 current_configfile->f;
1565 * Check if config->haddr,hport == oldcfg->haddr,hport
1567 * The following could be written more compactly as a single,
1568 * (unreadably long) if statement.
1570 config->need_bind = 0;
1571 if (config->hport != oldcfg->hport)
1573 config->need_bind = 1;
1575 else if (config->haddr == NULL)
1577 if (oldcfg->haddr != NULL)
1579 config->need_bind = 1;
1582 else if (oldcfg->haddr == NULL)
1584 config->need_bind = 1;
1586 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1588 config->need_bind = 1;
1591 current_configfile->unloader = unload_configfile;
1594 fs->next = files->next;
1597 current_configfile = fs;
1603 /*********************************************************************
1605 * Function : savearg
1607 * Description : Called from `load_config'. It saves each non-empty
1608 * and non-comment line from config into
1609 * config->proxy_args. This is used to create the
1610 * show-proxy-args page. On error, frees
1611 * config->proxy_args and sets it to NULL
1614 * 1 : command = config setting that was found
1615 * 2 : argument = the setting's argument (if any)
1616 * 3 : config = Configuration to save into.
1620 *********************************************************************/
1621 static void savearg(char *command, char *argument, struct configuration_spec * config)
1631 * Add config option name embedded in
1632 * link to it's section in the user-manual
1634 buf = strdup("<a href=\"");
1635 string_append(&buf, config->usermanual);
1636 string_append(&buf, CONFIG_HELP_PREFIX);
1637 string_join (&buf, string_toupper(command));
1638 string_append(&buf, "\">");
1639 string_append(&buf, command);
1640 string_append(&buf, "</a> ");
1644 freez(config->proxy_args);
1648 if ( (NULL != argument) && ('\0' != *argument) )
1650 s = html_encode(argument);
1654 freez(config->proxy_args);
1658 if (strncmpic(argument, "http://", 7) == 0)
1660 string_append(&buf, "<a href=\"");
1661 string_append(&buf, s);
1662 string_append(&buf, "\">");
1663 string_join (&buf, s);
1664 string_append(&buf, "</a>");
1668 string_join (&buf, s);
1672 string_append(&buf, "<br>\n");
1674 string_join(&config->proxy_args, buf);