1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.44 2002/04/08 20:37:13 swa 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.44 2002/04/08 20:37:13 swa
41 * Revision 1.43 2002/04/08 20:36:50 swa
44 * Revision 1.42 2002/04/05 15:50:15 oes
45 * fix for invalid HTML proxy_args
47 * Revision 1.41 2002/03/31 17:19:00 jongfoster
48 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
50 * Revision 1.40 2002/03/26 22:29:55 swa
51 * we have a new homepage!
53 * Revision 1.39 2002/03/24 13:25:43 swa
54 * name change related issues
56 * Revision 1.38 2002/03/24 13:05:48 jongfoster
57 * Renaming re_filterfile to filterfile
59 * Revision 1.37 2002/03/16 23:54:06 jongfoster
60 * Adding graceful termination feature, to help look for memory leaks.
61 * If you enable this (which, by design, has to be done by hand
62 * editing config.h) and then go to http://i.j.b/die, then the program
63 * will exit cleanly after the *next* request. It should free all the
64 * memory that was used.
66 * Revision 1.36 2002/03/13 00:27:05 jongfoster
69 * Revision 1.35 2002/03/07 03:52:44 oes
70 * Set logging to tty for --no-daemon mode
72 * Revision 1.34 2002/03/06 23:14:35 jongfoster
73 * Trivial cosmetic changes to make function comments easier to find.
75 * Revision 1.33 2002/03/05 04:52:42 oes
76 * Deleted non-errlog debugging code
78 * Revision 1.32 2002/03/04 18:24:53 oes
79 * Re-enabled output of unknown config directive hash
81 * Revision 1.31 2002/03/03 15:07:20 oes
82 * Re-enabled automatic config reloading
84 * Revision 1.30 2002/01/22 23:31:43 jongfoster
85 * Replacing strsav() with string_append()
87 * Revision 1.29 2002/01/17 21:02:30 jongfoster
88 * Moving all our URL and URL pattern parsing code to urlmatch.c.
90 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
92 * Revision 1.28 2001/12/30 14:07:32 steudten
93 * - Add signal handling (unix)
94 * - Add SIGHUP handler (unix)
95 * - Add creation of pidfile (unix)
96 * - Add action 'top' in rc file (RH)
97 * - Add entry 'SIGNALS' to manpage
98 * - Add exit message to logfile (unix)
100 * Revision 1.27 2001/11/07 00:02:13 steudten
101 * Add line number in error output for lineparsing for
102 * actionsfile and configfile.
103 * Special handling for CLF added.
105 * Revision 1.26 2001/11/05 21:41:43 steudten
106 * Add changes to be a real daemon just for unix os.
107 * (change cwd to /, detach from controlling tty, set
108 * process group and session leader to the own process.
110 * Add some fatal-error log message for failed malloc().
111 * Add '-d' if compiled with 'configure --with-debug' to
112 * enable debug output.
114 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
115 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
116 * threads to call select() simultaneously. So, it's time to do a real, live,
117 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
118 * (native). Both versions will work, but using __OS2__ offers multi-threading.
120 * Revision 1.24 2001/10/23 21:40:30 jongfoster
121 * Added support for enable-edit-actions and enable-remote-toggle config
124 * Revision 1.23 2001/10/07 15:36:00 oes
125 * Introduced new config option "buffer-limit"
127 * Revision 1.22 2001/09/22 16:36:59 jongfoster
128 * Removing unused parameter fs from read_config_line()
130 * Revision 1.21 2001/09/16 17:10:43 jongfoster
131 * Moving function savearg() here, since it was the only thing left in
134 * Revision 1.20 2001/07/30 22:08:36 jongfoster
135 * Tidying up #defines:
136 * - All feature #defines are now of the form FEATURE_xxx
137 * - Permanently turned off WIN_GUI_EDIT
138 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
140 * Revision 1.19 2001/07/15 17:45:16 jongfoster
141 * Removing some unused #includes
143 * Revision 1.18 2001/07/13 14:01:14 oes
144 * - Removed all #ifdef PCRS
145 * - Removed vim-settings
147 * Revision 1.17 2001/06/29 13:31:03 oes
148 * - Improved comments
149 * - Fixed (actionsfile) and sorted hashes
150 * - Introduced admin_address and proxy-info-url
151 * as config parameters
152 * - Renamed config->proxy_args_invocation (which didn't have
153 * the invocation but the options!) to config->proxy_args
154 * - Various adaptions
155 * - Removed logentry from cancelled commit
157 * Revision 1.16 2001/06/09 10:55:28 jongfoster
158 * Changing BUFSIZ ==> BUFFER_SIZE
160 * Revision 1.15 2001/06/07 23:13:40 jongfoster
161 * Merging ACL and forward files into config file.
162 * Cosmetic: Sorting config file options alphabetically.
163 * Cosmetic: Adding brief syntax comments to config file options.
165 * Revision 1.14 2001/06/07 14:46:25 joergs
166 * Missing make_path() added for re_filterfile.
168 * Revision 1.13 2001/06/05 22:33:54 jongfoster
170 * Fixed minor memory leak.
171 * Also now uses make_path to prepend the pathnames.
173 * Revision 1.12 2001/06/05 20:04:09 jongfoster
174 * Now uses _snprintf() in place of snprintf() under Win32.
176 * Revision 1.11 2001/06/04 18:31:58 swa
177 * files are now prefixed with either `confdir' or `logdir'.
178 * `make redhat-dist' replaces both entries confdir and logdir
181 * Revision 1.10 2001/06/03 19:11:54 oes
182 * introduced confdir option
184 * Revision 1.9 2001/06/01 20:06:24 jongfoster
185 * Removed support for "tinygif" option - moved to actions file.
187 * Revision 1.8 2001/05/31 21:27:13 jongfoster
188 * Removed many options from the config file and into the
189 * "actions" file: add_forwarded, suppress_vanilla_wafer,
190 * wafer, add_header, user_agent, referer, from
191 * Also globally replaced "permission" with "action".
193 * Revision 1.7 2001/05/29 09:50:24 jongfoster
194 * Unified blocklist/imagelist/permissionslist.
195 * File format is still under discussion, but the internal changes
198 * Also modified interceptor behaviour:
199 * - We now intercept all URLs beginning with one of the following
200 * prefixes (and *only* these prefixes):
202 * * http://ijbswa.sf.net/config/
203 * * http://ijbswa.sourceforge.net/config/
204 * - New interceptors "home page" - go to http://i.j.b/ to see it.
205 * - Internal changes so that intercepted and fast redirect pages
206 * are not replaced with an image.
207 * - Interceptors now have the option to send a binary page direct
208 * to the client. (i.e. ijb-send-banner uses this)
209 * - Implemented show-url-info interceptor. (Which is why I needed
210 * the above interceptors changes - a typical URL is
211 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
212 * The previous mechanism would not have intercepted that, and
213 * if it had been intercepted then it then it would have replaced
216 * Revision 1.6 2001/05/26 00:28:36 jongfoster
217 * Automatic reloading of config file.
218 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
219 * Most of the global variables have been moved to a new
220 * struct configuration_spec, accessed through csp->config->globalname
221 * Most of the globals remaining are used by the Win32 GUI.
223 * Revision 1.5 2001/05/25 22:34:30 jongfoster
226 * Revision 1.4 2001/05/22 18:46:04 oes
228 * - Enabled filtering banners by size rather than URL
229 * by adding patterns that replace all standard banner
230 * sizes with the "Junkbuster" gif to the re_filterfile
232 * - Enabled filtering WebBugs by providing a pattern
233 * which kills all 1x1 images
235 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
236 * which is selected by the (nonstandard and therefore
237 * capital) letter 'U' in the option string.
238 * It causes the quantifiers to be ungreedy by default.
239 * Appending a ? turns back to greedy (!).
241 * - Added a new interceptor ijb-send-banner, which
242 * sends back the "Junkbuster" gif. Without imagelist or
243 * MSIE detection support, or if tinygif = 1, or the
244 * URL isn't recognized as an imageurl, a lame HTML
245 * explanation is sent instead.
247 * - Added new feature, which permits blocking remote
248 * script redirects and firing back a local redirect
250 * The feature is conditionally compiled, i.e. it
251 * can be disabled with --disable-fast-redirects,
252 * plus it must be activated by a "fast-redirects"
253 * line in the config file, has its own log level
254 * and of course wants to be displayed by show-proxy-args
255 * Note: Boy, all the #ifdefs in 1001 locations and
256 * all the fumbling with configure.in and acconfig.h
257 * were *way* more work than the feature itself :-(
259 * - Because a generic redirect template was needed for
260 * this, tinygif = 3 now uses the same.
262 * - Moved GIFs, and other static HTTP response templates
267 * - Removed some >400 CRs again (Jon, you really worked
270 * Revision 1.3 2001/05/20 01:21:20 jongfoster
271 * Version 2.9.4 checkin.
272 * - Merged popupfile and cookiefile, and added control over PCRS
273 * filtering, in new "permissionsfile".
274 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
275 * file error you now get a message box (in the Win32 GUI) rather
276 * than the program exiting with no explanation.
277 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
279 * - Removed tabs from "config"
280 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
281 * - Bumped up version number.
283 * Revision 1.2 2001/05/17 23:01:01 oes
284 * - Cleaned CRLF's from the sources and related files
286 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
287 * Initial import of version 2.9.3 source tree
290 *********************************************************************/
296 #include <sys/types.h>
310 # include <windows.h>
313 # ifndef _WIN_CONSOLE
315 # endif /* ndef _WIN_CONSOLE */
317 /* VC++ has "_snprintf", not "snprintf" */
318 #define snprintf _snprintf
320 #else /* ifndef _WIN32 */
324 # include <sys/wait.h>
326 # include <sys/time.h>
327 # include <sys/stat.h>
337 #include "miscutil.h"
341 #include "urlmatch.h"
343 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
346 * Fix a problem with Solaris. There should be no effect on other
348 * Solaris's isspace() is a macro which uses it's argument directly
349 * as an array index. Therefore we need to make sure that high-bit
350 * characters generate +ve values, and ideally we also want to make
351 * the argument match the declared parameter type of "int".
353 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
354 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
356 #ifdef FEATURE_TOGGLE
357 /* by haroon - indicates if ijb is enabled */
358 int g_bToggleIJB = 1; /* Privoxy is enabled by default. */
359 #endif /* def FEATURE_TOGGLE */
361 /* The filename of the configfile */
362 const char *configfile = NULL;
365 * CGI functions will later need access to the invocation args,
366 * so we will make argc and argv global.
369 const char **Argv = NULL;
371 static struct file_list *current_configfile = NULL;
375 * This takes the "cryptic" hash of each keyword and aliases them to
376 * something a little more readable. This also makes changing the
377 * hash values easier if they should change or the hash algorthm changes.
378 * Use the included "hash" program to find out what the hash will be
379 * for any string supplied on the command line. (Or just put it in the
380 * config file and read the number from the error message in the log).
382 * Please keep this list sorted alphabetically (but with the Windows
383 * console and GUI specific options last).
386 #define hash_actions_file 1196306641ul /* "actionsfile" */
387 #define hash_admin_address 4112573064ul /* "admin-address" */
388 #define hash_buffer_limit 1881726070ul /* "buffer-limit */
389 #define hash_confdir 1978389ul /* "confdir" */
390 #define hash_debug 78263ul /* "debug" */
391 #define hash_deny_access 1227333715ul /* "deny-access" */
392 #define hash_enable_edit_actions 2517097536ul /* "enable-edit-actions" */
393 #define hash_enable_remote_toggle 2979744683ul /* "enable-remote-toggle" */
394 #define hash_filterfile 250887266ul /* "filterfile" */
395 #define hash_forward 2029845ul /* "forward" */
396 #define hash_forward_socks4 3963965521ul /* "forward-socks4" */
397 #define hash_forward_socks4a 2639958518ul /* "forward-socks4a" */
398 #define hash_jarfile 2046641ul /* "jarfile" */
399 #define hash_listen_address 1255650842ul /* "listen-address" */
400 #define hash_logdir 422889ul /* "logdir" */
401 #define hash_logfile 2114766ul /* "logfile" */
402 #define hash_permit_access 3587953268ul /* "permit-access" */
403 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
404 #define hash_single_threaded 4250084780ul /* "single-threaded" */
405 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
406 #define hash_toggle 447966ul /* "toggle" */
407 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
408 #define hash_trustfile 56494766ul /* "trustfile" */
410 #define hash_activity_animation 1817904738ul /* "activity-animation" */
411 #define hash_close_button_minimizes 3651284693ul /* "close-button-minimizes" */
412 #define hash_hide_console 2048809870ul /* "hide-console" */
413 #define hash_log_buffer_size 2918070425ul /* "log-buffer-size" */
414 #define hash_log_font_name 2866730124ul /* "log-font-name" */
415 #define hash_log_font_size 2866731014ul /* "log-font-size" */
416 #define hash_log_highlight_messages 4032101240ul /* "log-highlight-messages" */
417 #define hash_log_max_lines 2868344173ul /* "log-max-lines" */
418 #define hash_log_messages 2291744899ul /* "log-messages" */
419 #define hash_show_on_task_bar 215410365ul /* "show-on-task-bar" */
422 static void savearg(char *command, char *argument, struct configuration_spec * config);
424 /*********************************************************************
426 * Function : unload_configfile
428 * Description : Free the config structure and all components.
431 * 1 : data: struct configuration_spec to unload
435 *********************************************************************/
436 void unload_configfile (void * data)
438 struct configuration_spec * config = (struct configuration_spec *)data;
439 struct forward_spec *cur_fwd = config->forward;
441 struct access_control_list *cur_acl = config->acl;
444 while (cur_acl != NULL)
446 struct access_control_list * next_acl = cur_acl->next;
451 #endif /* def FEATURE_ACL */
453 while (cur_fwd != NULL)
455 struct forward_spec * next_fwd = cur_fwd->next;
456 free_url_spec(cur_fwd->url);
458 freez(cur_fwd->gateway_host);
459 freez(cur_fwd->forward_host);
463 config->forward = NULL;
465 #ifdef FEATURE_COOKIE_JAR
466 if ( NULL != config->jar )
468 fclose( config->jar );
471 #endif /* def FEATURE_COOKIE_JAR */
473 freez(config->confdir);
474 freez(config->logdir);
476 freez(config->haddr);
477 freez(config->logfile);
479 for (i = 0; i < MAX_ACTION_FILES; i++)
481 freez(config->actions_file_short[i]);
482 freez(config->actions_file[i]);
485 freez(config->admin_address);
486 freez(config->proxy_info_url);
487 freez(config->proxy_args);
489 #ifdef FEATURE_COOKIE_JAR
490 freez(config->jarfile);
491 #endif /* def FEATURE_COOKIE_JAR */
493 freez(config->re_filterfile);
498 #ifdef FEATURE_GRACEFUL_TERMINATION
499 /*********************************************************************
501 * Function : unload_current_config_file
503 * Description : Unloads current config file - reset to state at
504 * beginning of program.
510 *********************************************************************/
511 void unload_current_config_file(void)
513 if (current_configfile)
515 current_configfile->unloader = unload_configfile;
516 current_configfile = NULL;
522 /*********************************************************************
524 * Function : load_config
526 * Description : Load the config file and all parameters.
529 * 1 : csp = Client state (the config member will be
530 * filled in by this function).
532 * Returns : 0 => Ok, everything else is an error.
534 *********************************************************************/
535 struct configuration_spec * load_config(void)
537 char buf[BUFFER_SIZE];
539 FILE *configfp = NULL;
540 struct configuration_spec * config = NULL;
541 struct client_state * fake_csp;
542 struct file_list *fs;
543 unsigned long linenum = 0;
546 if ( !check_file_changed(current_configfile, configfile, &fs))
548 /* No need to load */
549 return ((struct configuration_spec *)current_configfile->f);
553 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
557 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
559 #ifdef FEATURE_TOGGLE
561 #endif /* def FEATURE_TOGGLE */
563 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
569 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
570 /* Never get here - LOG_LEVEL_FATAL causes program exit */
574 * This is backwards from how it's usually done.
575 * Following the usual pattern, "fs" would be stored in a member
576 * variable in "csp", and then we'd access "config" from "fs->f",
577 * using a cast. However, "config" is used so often that a
578 * cast each time would be very ugly, and the extra indirection
579 * would waste CPU cycles. Therefore we store "config" in
580 * "csp->config", and "fs" in "csp->config->config_file_list".
582 config->config_file_list = fs;
588 config->multi_threaded = 1;
589 config->hport = HADDR_PORT;
590 config->buffer_limit = 4096 * 1024;
591 config->proxy_args = strdup("");
593 if ((configfp = fopen(configfile, "r")) == NULL)
595 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
597 /* Never get here - LOG_LEVEL_FATAL causes program exit */
600 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
602 char cmd[BUFFER_SIZE];
603 char arg[BUFFER_SIZE];
604 char tmp[BUFFER_SIZE];
606 struct access_control_list *cur_acl;
607 #endif /* def FEATURE_ACL */
608 struct forward_spec *cur_fwd;
614 /* Copy command (i.e. up to space or tab) into cmd */
617 while (*p && (*p != ' ') && (*p != '\t'))
623 /* Skip over the whitespace in buf */
624 while (*p && ((*p == ' ') || (*p == '\t')))
629 /* Copy the argument into arg */
632 /* Should never happen, but check this anyway */
638 /* Make sure the command field is lower case */
643 *p = ijb_tolower(*p);
647 /* Save the argument for show-proxy-args */
648 savearg(cmd, arg, config);
651 switch( hash_string( cmd ) )
653 /* *************************************************************************
654 * actionsfile actions-file-name
655 * In confdir by default
656 * *************************************************************************/
657 case hash_actions_file :
659 while ((i < MAX_ACTION_FILES) && (NULL != config->actions_file[i]))
664 if (i >= MAX_ACTION_FILES)
666 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
667 "(You can increase this limit by changing MAX_ACTION_FILES in project.h and recompiling).",
670 config->actions_file_short[i] = strdup(arg);
671 p = malloc(strlen(arg) + sizeof(".action"));
674 log_error(LOG_LEVEL_FATAL, "Out of memory");
677 strcat(p, ".action");
678 config->actions_file[i] = make_path(config->confdir, p);
682 /* *************************************************************************
683 * admin-address email-address
684 * *************************************************************************/
685 case hash_admin_address :
686 freez(config->admin_address);
687 config->admin_address = strdup(arg);
690 /* *************************************************************************
692 * *************************************************************************/
693 case hash_buffer_limit :
694 config->buffer_limit = (size_t) 1024 * atoi(arg);
697 /* *************************************************************************
698 * confdir directory-name
699 * *************************************************************************/
701 freez(config->confdir);
702 config->confdir = make_path( NULL, arg);
705 /* *************************************************************************
707 * Specifies debug level, multiple values are ORed together.
708 * *************************************************************************/
710 config->debug |= atoi(arg);
713 /* *************************************************************************
714 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
715 * *************************************************************************/
717 case hash_deny_access:
718 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
720 if ((vec_count != 1) && (vec_count != 2))
722 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
723 "deny-access directive in configuration file.");
724 string_append(&config->proxy_args,
725 "<br>\nWARNING: Wrong number of parameters for "
726 "deny-access directive in configuration file.<br><br>\n");
730 /* allocate a new node */
731 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
735 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
736 /* Never get here - LOG_LEVEL_FATAL causes program exit */
739 cur_acl->action = ACL_DENY;
741 if (acl_addr(vec[0], cur_acl->src) < 0)
743 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
744 "directive in configuration file: \"%s\"", vec[0]);
745 string_append(&config->proxy_args,
746 "<br>\nWARNING: Invalid source IP for deny-access directive"
747 " in configuration file: \"");
748 string_append(&config->proxy_args,
750 string_append(&config->proxy_args,
757 if (acl_addr(vec[1], cur_acl->dst) < 0)
759 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
760 "directive in configuration file: \"%s\"", vec[0]);
761 string_append(&config->proxy_args,
762 "<br>\nWARNING: Invalid destination IP for deny-access directive"
763 " in configuration file: \"");
764 string_append(&config->proxy_args,
766 string_append(&config->proxy_args,
774 * Add it to the list. Note we reverse the list to get the
775 * behaviour the user expects. With both the ACL and
776 * actions file, the last match wins. However, the internal
777 * implementations are different: The actions file is stored
778 * in the same order as the file, and scanned completely.
779 * With the ACL, we reverse the order as we load it, then
780 * when we scan it we stop as soon as we get a match.
782 cur_acl->next = config->acl;
783 config->acl = cur_acl;
786 #endif /* def FEATURE_ACL */
788 /* *************************************************************************
789 * enable-edit-actions 0|1
790 * *************************************************************************/
791 #ifdef FEATURE_CGI_EDIT_ACTIONS
792 case hash_enable_edit_actions:
793 if ((*arg != '\0') && (0 != atoi(arg)))
795 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
799 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
802 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
804 /* *************************************************************************
805 * enable-remote-toggle 0|1
806 * *************************************************************************/
807 #ifdef FEATURE_CGI_EDIT_ACTIONS
808 case hash_enable_remote_toggle:
809 if ((*arg != '\0') && (0 != atoi(arg)))
811 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
815 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
818 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
820 /* *************************************************************************
821 * forward url-pattern (.|http-proxy-host[:port])
822 * *************************************************************************/
824 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
828 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
829 "directive in configuration file.");
830 string_append(&config->proxy_args,
831 "<br>\nWARNING: Wrong number of parameters for "
832 "forward directive in configuration file.");
836 /* allocate a new node */
837 cur_fwd = zalloc(sizeof(*cur_fwd));
840 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
841 /* Never get here - LOG_LEVEL_FATAL causes program exit */
845 cur_fwd->type = SOCKS_NONE;
847 /* Save the URL pattern */
848 if (create_url_spec(cur_fwd->url, vec[0]))
850 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
851 "directive in configuration file.");
852 string_append(&config->proxy_args,
853 "<br>\nWARNING: Bad URL specifier for "
854 "forward directive in configuration file.");
858 /* Parse the parent HTTP proxy host:port */
861 if (strcmp(p, ".") != 0)
863 cur_fwd->forward_host = strdup(p);
865 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
868 cur_fwd->forward_port = atoi(p);
871 if (cur_fwd->forward_port <= 0)
873 cur_fwd->forward_port = 8000;
878 cur_fwd->next = config->forward;
879 config->forward = cur_fwd;
883 /* *************************************************************************
884 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
885 * *************************************************************************/
886 case hash_forward_socks4:
887 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
891 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
892 "forward-socks4 directive in configuration file.");
893 string_append(&config->proxy_args,
894 "<br>\nWARNING: Wrong number of parameters for "
895 "forward-socks4 directive in configuration file.");
899 /* allocate a new node */
900 cur_fwd = zalloc(sizeof(*cur_fwd));
903 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
904 /* Never get here - LOG_LEVEL_FATAL causes program exit */
908 cur_fwd->type = SOCKS_4;
910 /* Save the URL pattern */
911 if (create_url_spec(cur_fwd->url, vec[0]))
913 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
914 "directive in configuration file.");
915 string_append(&config->proxy_args,
916 "<br>\nWARNING: Bad URL specifier for "
917 "forward-socks4 directive in configuration file.");
921 /* Parse the SOCKS proxy host[:port] */
924 if (strcmp(p, ".") != 0)
926 cur_fwd->gateway_host = strdup(p);
928 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
931 cur_fwd->gateway_port = atoi(p);
933 if (cur_fwd->gateway_port <= 0)
935 cur_fwd->gateway_port = 1080;
939 /* Parse the parent HTTP proxy host[:port] */
942 if (strcmp(p, ".") != 0)
944 cur_fwd->forward_host = strdup(p);
946 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
949 cur_fwd->forward_port = atoi(p);
952 if (cur_fwd->forward_port <= 0)
954 cur_fwd->forward_port = 8000;
959 cur_fwd->next = config->forward;
960 config->forward = cur_fwd;
964 /* *************************************************************************
965 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
966 * *************************************************************************/
967 case hash_forward_socks4a:
968 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
972 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
973 "forward-socks4a directive in configuration file.");
974 string_append(&config->proxy_args,
975 "<br>\nWARNING: Wrong number of parameters for "
976 "forward-socks4a directive in configuration file.");
980 /* allocate a new node */
981 cur_fwd = zalloc(sizeof(*cur_fwd));
984 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
985 /* Never get here - LOG_LEVEL_FATAL causes program exit */
989 cur_fwd->type = SOCKS_4A;
991 /* Save the URL pattern */
992 if (create_url_spec(cur_fwd->url, vec[0]))
994 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
995 "directive in configuration file.");
996 string_append(&config->proxy_args,
997 "<br>\nWARNING: Bad URL specifier for "
998 "forward-socks4a directive in configuration file.");
1002 /* Parse the SOCKS proxy host[:port] */
1005 cur_fwd->gateway_host = strdup(p);
1007 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1010 cur_fwd->gateway_port = atoi(p);
1012 if (cur_fwd->gateway_port <= 0)
1014 cur_fwd->gateway_port = 1080;
1017 /* Parse the parent HTTP proxy host[:port] */
1020 if (strcmp(p, ".") != 0)
1022 cur_fwd->forward_host = strdup(p);
1024 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1027 cur_fwd->forward_port = atoi(p);
1030 if (cur_fwd->forward_port <= 0)
1032 cur_fwd->forward_port = 8000;
1037 cur_fwd->next = config->forward;
1038 config->forward = cur_fwd;
1042 /* *************************************************************************
1043 * jarfile jar-file-name
1044 * In logdir by default
1045 * *************************************************************************/
1046 #ifdef FEATURE_COOKIE_JAR
1048 freez(config->jarfile);
1049 config->jarfile = make_path(config->logdir, arg);
1051 #endif /* def FEATURE_COOKIE_JAR */
1053 /* *************************************************************************
1054 * listen-address [ip][:port]
1055 * *************************************************************************/
1056 case hash_listen_address :
1057 freez(config->haddr);
1058 config->haddr = strdup(arg);
1061 /* *************************************************************************
1062 * logdir directory-name
1063 * *************************************************************************/
1065 freez(config->logdir);
1066 config->logdir = make_path(NULL, arg);
1069 /* *************************************************************************
1070 * logfile log-file-name
1071 * In logdir by default
1072 * *************************************************************************/
1074 freez(config->logfile);
1075 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1078 /* *************************************************************************
1079 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1080 * *************************************************************************/
1082 case hash_permit_access:
1083 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1085 if ((vec_count != 1) && (vec_count != 2))
1087 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1088 "permit-access directive in configuration file.");
1089 string_append(&config->proxy_args,
1090 "<br>\nWARNING: Wrong number of parameters for "
1091 "permit-access directive in configuration file.<br><br>\n");
1096 /* allocate a new node */
1097 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1099 if (cur_acl == NULL)
1101 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1102 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1105 cur_acl->action = ACL_PERMIT;
1107 if (acl_addr(vec[0], cur_acl->src) < 0)
1109 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1110 "directive in configuration file: \"%s\"", vec[0]);
1111 string_append(&config->proxy_args,
1112 "<br>\nWARNING: Invalid source IP for permit-access directive"
1113 " in configuration file: \"");
1114 string_append(&config->proxy_args,
1116 string_append(&config->proxy_args,
1123 if (acl_addr(vec[1], cur_acl->dst) < 0)
1125 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1126 "permit-access directive in configuration file: \"%s\"",
1128 string_append(&config->proxy_args,
1129 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1130 " in configuration file: \"");
1131 string_append(&config->proxy_args,
1133 string_append(&config->proxy_args,
1141 * Add it to the list. Note we reverse the list to get the
1142 * behaviour the user expects. With both the ACL and
1143 * actions file, the last match wins. However, the internal
1144 * implementations are different: The actions file is stored
1145 * in the same order as the file, and scanned completely.
1146 * With the ACL, we reverse the order as we load it, then
1147 * when we scan it we stop as soon as we get a match.
1149 cur_acl->next = config->acl;
1150 config->acl = cur_acl;
1153 #endif /* def FEATURE_ACL */
1155 /* *************************************************************************
1156 * proxy-info-url url
1157 * *************************************************************************/
1158 case hash_proxy_info_url :
1159 freez(config->proxy_info_url);
1160 config->proxy_info_url = strdup(arg);
1163 /* *************************************************************************
1164 * re_filterfile file-name
1165 * In confdir by default.
1166 * *************************************************************************/
1167 case hash_filterfile :
1168 freez(config->re_filterfile);
1169 config->re_filterfile = make_path(config->confdir, arg);
1172 /* *************************************************************************
1174 * *************************************************************************/
1175 case hash_single_threaded :
1176 config->multi_threaded = 0;
1179 /* *************************************************************************
1181 * *************************************************************************/
1182 #ifdef FEATURE_TOGGLE
1184 g_bToggleIJB = atoi(arg);
1186 #endif /* def FEATURE_TOGGLE */
1188 /* *************************************************************************
1189 * trust-info-url url
1190 * *************************************************************************/
1191 #ifdef FEATURE_TRUST
1192 case hash_trust_info_url :
1193 enlist(config->trust_info, arg);
1195 #endif /* def FEATURE_TRUST */
1197 /* *************************************************************************
1198 * trustfile filename
1199 * (In confdir by default.)
1200 * *************************************************************************/
1201 #ifdef FEATURE_TRUST
1202 case hash_trustfile :
1203 freez(config->trustfile);
1204 config->trustfile = make_path(config->confdir, arg);
1206 #endif /* def FEATURE_TRUST */
1209 /* *************************************************************************
1210 * Win32 Console options:
1211 * *************************************************************************/
1213 /* *************************************************************************
1215 * *************************************************************************/
1217 case hash_hide_console :
1220 #endif /*def _WIN_CONSOLE*/
1223 /* *************************************************************************
1224 * Win32 GUI options:
1225 * *************************************************************************/
1227 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1228 /* *************************************************************************
1229 * activity-animation (0|1)
1230 * *************************************************************************/
1231 case hash_activity_animation :
1232 g_bShowActivityAnimation = atoi(arg);
1235 /* *************************************************************************
1236 * close-button-minimizes (0|1)
1237 * *************************************************************************/
1238 case hash_close_button_minimizes :
1239 g_bCloseHidesWindow = atoi(arg);
1242 /* *************************************************************************
1243 * log-buffer-size (0|1)
1244 * *************************************************************************/
1245 case hash_log_buffer_size :
1246 g_bLimitBufferSize = atoi(arg);
1249 /* *************************************************************************
1250 * log-font-name fontnane
1251 * *************************************************************************/
1252 case hash_log_font_name :
1253 strcpy( g_szFontFaceName, arg );
1256 /* *************************************************************************
1258 * *************************************************************************/
1259 case hash_log_font_size :
1260 g_nFontSize = atoi(arg);
1263 /* *************************************************************************
1264 * log-highlight-messages (0|1)
1265 * *************************************************************************/
1266 case hash_log_highlight_messages :
1267 g_bHighlightMessages = atoi(arg);
1270 /* *************************************************************************
1272 * *************************************************************************/
1273 case hash_log_max_lines :
1274 g_nMaxBufferLines = atoi(arg);
1277 /* *************************************************************************
1278 * log-messages (0|1)
1279 * *************************************************************************/
1280 case hash_log_messages :
1281 g_bLogMessages = atoi(arg);
1284 /* *************************************************************************
1285 * show-on-task-bar (0|1)
1286 * *************************************************************************/
1287 case hash_show_on_task_bar :
1288 g_bShowOnTaskBar = atoi(arg);
1291 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1294 /* *************************************************************************
1295 * Warnings about unsupported features
1296 * *************************************************************************/
1298 case hash_deny_access:
1299 #endif /* ndef FEATURE_ACL */
1300 #ifndef FEATURE_CGI_EDIT_ACTIONS
1301 case hash_enable_edit_actions:
1302 case hash_enable_remote_toggle:
1303 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1304 #ifndef FEATURE_COOKIE_JAR
1306 #endif /* ndef FEATURE_COOKIE_JAR */
1308 case hash_permit_access:
1309 #endif /* ndef FEATURE_ACL */
1310 #ifndef FEATURE_TOGGLE
1312 #endif /* ndef FEATURE_TOGGLE */
1313 #ifndef FEATURE_TRUST
1314 case hash_trustfile :
1315 case hash_trust_info_url :
1316 #endif /* ndef FEATURE_TRUST */
1318 #ifndef _WIN_CONSOLE
1319 case hash_hide_console :
1320 #endif /* ndef _WIN_CONSOLE */
1322 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1323 case hash_activity_animation :
1324 case hash_close_button_minimizes :
1325 case hash_log_buffer_size :
1326 case hash_log_font_name :
1327 case hash_log_font_size :
1328 case hash_log_highlight_messages :
1329 case hash_log_max_lines :
1330 case hash_log_messages :
1331 case hash_show_on_task_bar :
1332 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1333 /* These warnings are annoying - so hide them. -- Jon */
1334 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1337 /* *************************************************************************/
1339 /* *************************************************************************/
1341 * I decided that I liked this better as a warning than an
1342 * error. To change back to an error, just change log level
1343 * to LOG_LEVEL_FATAL.
1345 log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1346 "configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1347 string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1348 string_append(&config->proxy_args, buf);
1349 string_append(&config->proxy_args, "<br><br>\n");
1352 /* *************************************************************************/
1353 } /* end switch( hash_string(cmd) ) */
1354 } /* end while ( read_config_line(...) ) */
1358 if (NULL == config->proxy_args)
1360 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1363 init_error_log(Argv[0], config->logfile, config->debug);
1365 if (config->actions_file[0])
1367 add_loader(load_actions_file, config);
1370 if (config->re_filterfile)
1372 add_loader(load_re_filterfile, config);
1375 #ifdef FEATURE_TRUST
1376 if (config->trustfile)
1378 add_loader(load_trustfile, config);
1380 #endif /* def FEATURE_TRUST */
1382 #ifdef FEATURE_COOKIE_JAR
1383 if ( NULL != config->jarfile )
1385 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1387 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1388 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1390 setbuf(config->jar, NULL);
1392 #endif /* def FEATURE_COOKIE_JAR */
1394 if ( NULL == config->haddr )
1396 config->haddr = strdup( HADDR_DEFAULT );
1399 if ( NULL != config->haddr )
1401 if (NULL != (p = strchr(config->haddr, ':')))
1406 config->hport = atoi(p);
1410 if (config->hport <= 0)
1413 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1414 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1416 if (*config->haddr == '\0')
1418 config->haddr = NULL;
1423 * Want to run all the loaders once now.
1425 * Need to set up a fake csp, so they can get to the config.
1427 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1428 fake_csp->config = config;
1430 if (run_loader(fake_csp))
1433 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1434 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1438 /* FIXME: this is a kludge for win32 */
1439 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1441 g_actions_file = config->actions_file[0]; /* FIXME only works for first action file */
1442 g_re_filterfile = config->re_filterfile;
1444 #ifdef FEATURE_TRUST
1445 g_trustfile = config->trustfile;
1446 #endif /* def FEATURE_TRUST */
1449 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1450 /* FIXME: end kludge */
1453 config->need_bind = 1;
1455 if (current_configfile)
1457 struct configuration_spec * oldcfg = (struct configuration_spec *)
1458 current_configfile->f;
1460 * Check if config->haddr,hport == oldcfg->haddr,hport
1462 * The following could be written more compactly as a single,
1463 * (unreadably long) if statement.
1465 config->need_bind = 0;
1466 if (config->hport != oldcfg->hport)
1468 config->need_bind = 1;
1470 else if (config->haddr == NULL)
1472 if (oldcfg->haddr != NULL)
1474 config->need_bind = 1;
1477 else if (oldcfg->haddr == NULL)
1479 config->need_bind = 1;
1481 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1483 config->need_bind = 1;
1486 current_configfile->unloader = unload_configfile;
1489 fs->next = files->next;
1492 current_configfile = fs;
1498 /*********************************************************************
1500 * Function : savearg
1502 * Description : Called from `load_config'. It saves each non-empty
1503 * and non-comment line from config into
1504 * config->proxy_args. This is used to create the
1505 * show-proxy-args page. On error, frees
1506 * config->proxy_args and sets it to NULL
1509 * 1 : command = config setting that was found
1510 * 2 : argument = the setting's argument (if any)
1514 *********************************************************************/
1515 static void savearg(char *command, char *argument, struct configuration_spec * config)
1526 s = html_encode(command);
1530 freez(config->proxy_args);
1533 string_append(&buf, "<a href=\"");
1534 string_append(&buf, html_encode(REDIRECT_URL));
1535 string_append(&buf, "option#");
1536 string_append(&buf, s);
1537 string_append(&buf, "\">");
1538 string_join (&buf, s);
1539 string_append(&buf, "</a> ");
1541 if ( (NULL != argument) && ('\0' != *argument) )
1543 s = html_encode(argument);
1547 freez(config->proxy_args);
1551 if (strncmpic(argument, "http://", 7) == 0)
1553 string_append(&buf, "<a href=\"");
1554 string_append(&buf, s);
1555 string_append(&buf, "\">");
1556 string_join (&buf, s);
1557 string_append(&buf, "</a>");
1561 string_join (&buf, s);
1565 string_append(&buf, "<br>\n");
1567 string_join(&config->proxy_args, buf);