1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.50 2006/07/18 14:48:46 david__schmidt 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.50 2006/07/18 14:48:46 david__schmidt
39 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
40 * with what was really the latest development (the v_3_0_branch branch)
42 * Revision 1.48.2.7 2006/02/02 17:29:16 david__schmidt
43 * Don't forget to malloc space for the null terminator...
45 * Revision 1.48.2.6 2006/01/29 23:10:56 david__schmidt
46 * Multiple filter file support
48 * Revision 1.48.2.5 2003/05/08 15:17:25 oes
49 * Closed two memory leaks; hopefully the last remaining ones
50 * (in the main execution paths, anyway).
52 * Revision 1.48.2.4 2003/04/11 12:06:14 oes
53 * Addressed bug #719435
54 * - Extraneous filterfile directives now logged as errors
55 * - This and unrecnonised directives now really obvious on status page
57 * Revision 1.48.2.3 2003/03/11 11:53:59 oes
58 * Cosmetic: Renamed cryptic variable
60 * Revision 1.48.2.2 2002/11/12 16:28:20 oes
61 * Move unrelated variable declaration out of #ifdef FEATURE_ACL; fixes bug #636655
63 * Revision 1.48.2.1 2002/08/21 17:58:05 oes
64 * Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
66 * Revision 1.48 2002/05/14 21:30:38 oes
67 * savearg now uses own linking code instead of (now special-cased) add_help_link
69 * Revision 1.47 2002/05/12 21:36:29 jongfoster
70 * Correcting function comments
72 * Revision 1.46 2002/04/26 12:55:14 oes
73 * - New option "user-manual", defaults to our site
74 * via project.h #define
75 * - savearg now embeds option names in help links
77 * Revision 1.45 2002/04/24 02:11:54 oes
78 * Jon's multiple AF patch: Allow up to MAX_AF_FILES actionsfile options
80 * Revision 1.44 2002/04/08 20:37:13 swa
83 * Revision 1.43 2002/04/08 20:36:50 swa
86 * Revision 1.42 2002/04/05 15:50:15 oes
87 * fix for invalid HTML proxy_args
89 * Revision 1.41 2002/03/31 17:19:00 jongfoster
90 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
92 * Revision 1.40 2002/03/26 22:29:55 swa
93 * we have a new homepage!
95 * Revision 1.39 2002/03/24 13:25:43 swa
96 * name change related issues
98 * Revision 1.38 2002/03/24 13:05:48 jongfoster
99 * Renaming re_filterfile to filterfile
101 * Revision 1.37 2002/03/16 23:54:06 jongfoster
102 * Adding graceful termination feature, to help look for memory leaks.
103 * If you enable this (which, by design, has to be done by hand
104 * editing config.h) and then go to http://i.j.b/die, then the program
105 * will exit cleanly after the *next* request. It should free all the
106 * memory that was used.
108 * Revision 1.36 2002/03/13 00:27:05 jongfoster
111 * Revision 1.35 2002/03/07 03:52:44 oes
112 * Set logging to tty for --no-daemon mode
114 * Revision 1.34 2002/03/06 23:14:35 jongfoster
115 * Trivial cosmetic changes to make function comments easier to find.
117 * Revision 1.33 2002/03/05 04:52:42 oes
118 * Deleted non-errlog debugging code
120 * Revision 1.32 2002/03/04 18:24:53 oes
121 * Re-enabled output of unknown config directive hash
123 * Revision 1.31 2002/03/03 15:07:20 oes
124 * Re-enabled automatic config reloading
126 * Revision 1.30 2002/01/22 23:31:43 jongfoster
127 * Replacing strsav() with string_append()
129 * Revision 1.29 2002/01/17 21:02:30 jongfoster
130 * Moving all our URL and URL pattern parsing code to urlmatch.c.
132 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
134 * Revision 1.28 2001/12/30 14:07:32 steudten
135 * - Add signal handling (unix)
136 * - Add SIGHUP handler (unix)
137 * - Add creation of pidfile (unix)
138 * - Add action 'top' in rc file (RH)
139 * - Add entry 'SIGNALS' to manpage
140 * - Add exit message to logfile (unix)
142 * Revision 1.27 2001/11/07 00:02:13 steudten
143 * Add line number in error output for lineparsing for
144 * actionsfile and configfile.
145 * Special handling for CLF added.
147 * Revision 1.26 2001/11/05 21:41:43 steudten
148 * Add changes to be a real daemon just for unix os.
149 * (change cwd to /, detach from controlling tty, set
150 * process group and session leader to the own process.
152 * Add some fatal-error log message for failed malloc().
153 * Add '-d' if compiled with 'configure --with-debug' to
154 * enable debug output.
156 * Revision 1.25 2001/10/25 03:40:48 david__schmidt
157 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
158 * threads to call select() simultaneously. So, it's time to do a real, live,
159 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
160 * (native). Both versions will work, but using __OS2__ offers multi-threading.
162 * Revision 1.24 2001/10/23 21:40:30 jongfoster
163 * Added support for enable-edit-actions and enable-remote-toggle config
166 * Revision 1.23 2001/10/07 15:36:00 oes
167 * Introduced new config option "buffer-limit"
169 * Revision 1.22 2001/09/22 16:36:59 jongfoster
170 * Removing unused parameter fs from read_config_line()
172 * Revision 1.21 2001/09/16 17:10:43 jongfoster
173 * Moving function savearg() here, since it was the only thing left in
176 * Revision 1.20 2001/07/30 22:08:36 jongfoster
177 * Tidying up #defines:
178 * - All feature #defines are now of the form FEATURE_xxx
179 * - Permanently turned off WIN_GUI_EDIT
180 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
182 * Revision 1.19 2001/07/15 17:45:16 jongfoster
183 * Removing some unused #includes
185 * Revision 1.18 2001/07/13 14:01:14 oes
186 * - Removed all #ifdef PCRS
187 * - Removed vim-settings
189 * Revision 1.17 2001/06/29 13:31:03 oes
190 * - Improved comments
191 * - Fixed (actionsfile) and sorted hashes
192 * - Introduced admin_address and proxy-info-url
193 * as config parameters
194 * - Renamed config->proxy_args_invocation (which didn't have
195 * the invocation but the options!) to config->proxy_args
196 * - Various adaptions
197 * - Removed logentry from cancelled commit
199 * Revision 1.16 2001/06/09 10:55:28 jongfoster
200 * Changing BUFSIZ ==> BUFFER_SIZE
202 * Revision 1.15 2001/06/07 23:13:40 jongfoster
203 * Merging ACL and forward files into config file.
204 * Cosmetic: Sorting config file options alphabetically.
205 * Cosmetic: Adding brief syntax comments to config file options.
207 * Revision 1.14 2001/06/07 14:46:25 joergs
208 * Missing make_path() added for re_filterfile.
210 * Revision 1.13 2001/06/05 22:33:54 jongfoster
212 * Fixed minor memory leak.
213 * Also now uses make_path to prepend the pathnames.
215 * Revision 1.12 2001/06/05 20:04:09 jongfoster
216 * Now uses _snprintf() in place of snprintf() under Win32.
218 * Revision 1.11 2001/06/04 18:31:58 swa
219 * files are now prefixed with either `confdir' or `logdir'.
220 * `make redhat-dist' replaces both entries confdir and logdir
223 * Revision 1.10 2001/06/03 19:11:54 oes
224 * introduced confdir option
226 * Revision 1.9 2001/06/01 20:06:24 jongfoster
227 * Removed support for "tinygif" option - moved to actions file.
229 * Revision 1.8 2001/05/31 21:27:13 jongfoster
230 * Removed many options from the config file and into the
231 * "actions" file: add_forwarded, suppress_vanilla_wafer,
232 * wafer, add_header, user_agent, referer, from
233 * Also globally replaced "permission" with "action".
235 * Revision 1.7 2001/05/29 09:50:24 jongfoster
236 * Unified blocklist/imagelist/permissionslist.
237 * File format is still under discussion, but the internal changes
240 * Also modified interceptor behaviour:
241 * - We now intercept all URLs beginning with one of the following
242 * prefixes (and *only* these prefixes):
244 * * http://ijbswa.sf.net/config/
245 * * http://ijbswa.sourceforge.net/config/
246 * - New interceptors "home page" - go to http://i.j.b/ to see it.
247 * - Internal changes so that intercepted and fast redirect pages
248 * are not replaced with an image.
249 * - Interceptors now have the option to send a binary page direct
250 * to the client. (i.e. ijb-send-banner uses this)
251 * - Implemented show-url-info interceptor. (Which is why I needed
252 * the above interceptors changes - a typical URL is
253 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
254 * The previous mechanism would not have intercepted that, and
255 * if it had been intercepted then it then it would have replaced
258 * Revision 1.6 2001/05/26 00:28:36 jongfoster
259 * Automatic reloading of config file.
260 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
261 * Most of the global variables have been moved to a new
262 * struct configuration_spec, accessed through csp->config->globalname
263 * Most of the globals remaining are used by the Win32 GUI.
265 * Revision 1.5 2001/05/25 22:34:30 jongfoster
268 * Revision 1.4 2001/05/22 18:46:04 oes
270 * - Enabled filtering banners by size rather than URL
271 * by adding patterns that replace all standard banner
272 * sizes with the "Junkbuster" gif to the re_filterfile
274 * - Enabled filtering WebBugs by providing a pattern
275 * which kills all 1x1 images
277 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
278 * which is selected by the (nonstandard and therefore
279 * capital) letter 'U' in the option string.
280 * It causes the quantifiers to be ungreedy by default.
281 * Appending a ? turns back to greedy (!).
283 * - Added a new interceptor ijb-send-banner, which
284 * sends back the "Junkbuster" gif. Without imagelist or
285 * MSIE detection support, or if tinygif = 1, or the
286 * URL isn't recognized as an imageurl, a lame HTML
287 * explanation is sent instead.
289 * - Added new feature, which permits blocking remote
290 * script redirects and firing back a local redirect
292 * The feature is conditionally compiled, i.e. it
293 * can be disabled with --disable-fast-redirects,
294 * plus it must be activated by a "fast-redirects"
295 * line in the config file, has its own log level
296 * and of course wants to be displayed by show-proxy-args
297 * Note: Boy, all the #ifdefs in 1001 locations and
298 * all the fumbling with configure.in and acconfig.h
299 * were *way* more work than the feature itself :-(
301 * - Because a generic redirect template was needed for
302 * this, tinygif = 3 now uses the same.
304 * - Moved GIFs, and other static HTTP response templates
309 * - Removed some >400 CRs again (Jon, you really worked
312 * Revision 1.3 2001/05/20 01:21:20 jongfoster
313 * Version 2.9.4 checkin.
314 * - Merged popupfile and cookiefile, and added control over PCRS
315 * filtering, in new "permissionsfile".
316 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
317 * file error you now get a message box (in the Win32 GUI) rather
318 * than the program exiting with no explanation.
319 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
321 * - Removed tabs from "config"
322 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
323 * - Bumped up version number.
325 * Revision 1.2 2001/05/17 23:01:01 oes
326 * - Cleaned CRLF's from the sources and related files
328 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
329 * Initial import of version 2.9.3 source tree
332 *********************************************************************/
338 #include <sys/types.h>
352 # include <windows.h>
355 # ifndef _WIN_CONSOLE
357 # endif /* ndef _WIN_CONSOLE */
359 /* VC++ has "_snprintf", not "snprintf" */
360 #define snprintf _snprintf
362 #else /* ifndef _WIN32 */
366 # include <sys/wait.h>
368 # include <sys/time.h>
369 # include <sys/stat.h>
379 #include "miscutil.h"
383 #include "urlmatch.h"
386 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
389 * Fix a problem with Solaris. There should be no effect on other
391 * Solaris's isspace() is a macro which uses it's argument directly
392 * as an array index. Therefore we need to make sure that high-bit
393 * characters generate +ve values, and ideally we also want to make
394 * the argument match the declared parameter type of "int".
396 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
397 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
399 #ifdef FEATURE_TOGGLE
400 /* Privoxy is enabled by default. */
401 int global_toggle_state = 1;
402 #endif /* def FEATURE_TOGGLE */
404 /* The filename of the configfile */
405 const char *configfile = NULL;
408 * CGI functions will later need access to the invocation args,
409 * so we will make argc and argv global.
412 const char **Argv = NULL;
414 static struct file_list *current_configfile = NULL;
418 * This takes the "cryptic" hash of each keyword and aliases them to
419 * something a little more readable. This also makes changing the
420 * hash values easier if they should change or the hash algorthm changes.
421 * Use the included "hash" program to find out what the hash will be
422 * for any string supplied on the command line. (Or just put it in the
423 * config file and read the number from the error message in the log).
425 * Please keep this list sorted alphabetically (but with the Windows
426 * console and GUI specific options last).
429 #define hash_actions_file 1196306641ul /* "actionsfile" */
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_forwarded_connect_retries 101465292ul /* "forwarded-connect-retries" */
442 #define hash_jarfile 2046641ul /* "jarfile" */
443 #define hash_listen_address 1255650842ul /* "listen-address" */
444 #define hash_logdir 422889ul /* "logdir" */
445 #define hash_logfile 2114766ul /* "logfile" */
446 #define hash_permit_access 3587953268ul /* "permit-access" */
447 #define hash_proxy_info_url 3903079059ul /* "proxy-info-url" */
448 #define hash_single_threaded 4250084780ul /* "single-threaded" */
449 #define hash_suppress_blocklists 1948693308ul /* "suppress-blocklists" */
450 #define hash_toggle 447966ul /* "toggle" */
451 #define hash_trust_info_url 430331967ul /* "trust-info-url" */
452 #define hash_trustfile 56494766ul /* "trustfile" */
453 #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" */
466 static void savearg(char *command, char *argument, struct configuration_spec * config);
468 /*********************************************************************
470 * Function : unload_configfile
472 * Description : Free the config structure and all components.
475 * 1 : data: struct configuration_spec to unload
479 *********************************************************************/
480 void unload_configfile (void * data)
482 struct configuration_spec * config = (struct configuration_spec *)data;
483 struct forward_spec *cur_fwd = config->forward;
487 struct access_control_list *cur_acl = config->acl;
489 while (cur_acl != NULL)
491 struct access_control_list * next_acl = cur_acl->next;
496 #endif /* def FEATURE_ACL */
498 while (cur_fwd != NULL)
500 struct forward_spec * next_fwd = cur_fwd->next;
501 free_url_spec(cur_fwd->url);
503 freez(cur_fwd->gateway_host);
504 freez(cur_fwd->forward_host);
508 config->forward = NULL;
510 #ifdef FEATURE_COOKIE_JAR
511 if ( NULL != config->jar )
513 fclose( config->jar );
516 #endif /* def FEATURE_COOKIE_JAR */
518 freez(config->confdir);
519 freez(config->logdir);
521 freez(config->haddr);
522 freez(config->logfile);
524 for (i = 0; i < MAX_AF_FILES; i++)
526 freez(config->actions_file_short[i]);
527 freez(config->actions_file[i]);
530 freez(config->admin_address);
531 freez(config->proxy_info_url);
532 freez(config->proxy_args);
533 freez(config->usermanual);
535 #ifdef FEATURE_COOKIE_JAR
536 freez(config->jarfile);
537 #endif /* def FEATURE_COOKIE_JAR */
540 freez(config->trustfile);
541 list_remove_all(config->trust_info);
542 #endif /* def FEATURE_TRUST */
544 for (i = 0; i < MAX_AF_FILES; i++)
546 freez(config->re_filterfile[i]);
553 #ifdef FEATURE_GRACEFUL_TERMINATION
554 /*********************************************************************
556 * Function : unload_current_config_file
558 * Description : Unloads current config file - reset to state at
559 * beginning of program.
565 *********************************************************************/
566 void unload_current_config_file(void)
568 if (current_configfile)
570 current_configfile->unloader = unload_configfile;
571 current_configfile = NULL;
577 /*********************************************************************
579 * Function : load_config
581 * Description : Load the config file and all parameters.
585 * Returns : The configuration_spec, or NULL on error.
587 *********************************************************************/
588 struct configuration_spec * load_config(void)
590 char buf[BUFFER_SIZE];
592 FILE *configfp = NULL;
593 struct configuration_spec * config = NULL;
594 struct client_state * fake_csp;
595 struct file_list *fs;
596 unsigned long linenum = 0;
599 if ( !check_file_changed(current_configfile, configfile, &fs))
601 /* No need to load */
602 return ((struct configuration_spec *)current_configfile->f);
606 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
610 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
612 #ifdef FEATURE_TOGGLE
613 global_toggle_state = 1;
614 #endif /* def FEATURE_TOGGLE */
616 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
622 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
623 /* Never get here - LOG_LEVEL_FATAL causes program exit */
627 * This is backwards from how it's usually done.
628 * Following the usual pattern, "fs" would be stored in a member
629 * variable in "csp", and then we'd access "config" from "fs->f",
630 * using a cast. However, "config" is used so often that a
631 * cast each time would be very ugly, and the extra indirection
632 * would waste CPU cycles. Therefore we store "config" in
633 * "csp->config", and "fs" in "csp->config->config_file_list".
635 config->config_file_list = fs;
640 config->multi_threaded = 1;
641 config->hport = HADDR_PORT;
642 config->buffer_limit = 4096 * 1024;
643 config->usermanual = strdup(USER_MANUAL_URL);
644 config->proxy_args = strdup("");
645 config->forwarded_connect_retries = 0;
647 if ((configfp = fopen(configfile, "r")) == NULL)
649 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
651 /* Never get here - LOG_LEVEL_FATAL causes program exit */
654 while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
656 char cmd[BUFFER_SIZE];
657 char arg[BUFFER_SIZE];
658 char tmp[BUFFER_SIZE];
660 struct access_control_list *cur_acl;
661 #endif /* def FEATURE_ACL */
662 struct forward_spec *cur_fwd;
668 /* Copy command (i.e. up to space or tab) into cmd */
671 while (*p && (*p != ' ') && (*p != '\t'))
677 /* Skip over the whitespace in buf */
678 while (*p && ((*p == ' ') || (*p == '\t')))
683 /* Copy the argument into arg */
686 /* Should never happen, but check this anyway */
692 /* Make sure the command field is lower case */
697 *p = ijb_tolower(*p);
701 /* Save the argument for show-proxy-args */
702 savearg(cmd, arg, config);
705 switch( hash_string( cmd ) )
707 /* *************************************************************************
708 * actionsfile actions-file-name
709 * In confdir by default
710 * *************************************************************************/
711 case hash_actions_file :
713 while ((i < MAX_AF_FILES) && (NULL != config->actions_file[i]))
718 if (i >= MAX_AF_FILES)
720 log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
721 "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
724 config->actions_file_short[i] = strdup(arg);
725 p = malloc(strlen(arg) + sizeof(".action"));
728 log_error(LOG_LEVEL_FATAL, "Out of memory");
731 strcat(p, ".action");
732 config->actions_file[i] = make_path(config->confdir, p);
736 /* *************************************************************************
737 * admin-address email-address
738 * *************************************************************************/
739 case hash_admin_address :
740 freez(config->admin_address);
741 config->admin_address = strdup(arg);
744 /* *************************************************************************
746 * *************************************************************************/
747 case hash_buffer_limit :
748 config->buffer_limit = (size_t) 1024 * atoi(arg);
751 /* *************************************************************************
752 * confdir directory-name
753 * *************************************************************************/
755 freez(config->confdir);
756 config->confdir = make_path( NULL, arg);
759 /* *************************************************************************
761 * Specifies debug level, multiple values are ORed together.
762 * *************************************************************************/
764 config->debug |= atoi(arg);
767 /* *************************************************************************
768 * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
769 * *************************************************************************/
771 case hash_deny_access:
772 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
774 if ((vec_count != 1) && (vec_count != 2))
776 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
777 "deny-access directive in configuration file.");
778 string_append(&config->proxy_args,
779 "<br>\nWARNING: Wrong number of parameters for "
780 "deny-access directive in configuration file.<br><br>\n");
784 /* allocate a new node */
785 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
789 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
790 /* Never get here - LOG_LEVEL_FATAL causes program exit */
793 cur_acl->action = ACL_DENY;
795 if (acl_addr(vec[0], cur_acl->src) < 0)
797 log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
798 "directive in configuration file: \"%s\"", vec[0]);
799 string_append(&config->proxy_args,
800 "<br>\nWARNING: Invalid source IP for deny-access directive"
801 " in configuration file: \"");
802 string_append(&config->proxy_args,
804 string_append(&config->proxy_args,
811 if (acl_addr(vec[1], cur_acl->dst) < 0)
813 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
814 "directive in configuration file: \"%s\"", vec[0]);
815 string_append(&config->proxy_args,
816 "<br>\nWARNING: Invalid destination IP for deny-access directive"
817 " in configuration file: \"");
818 string_append(&config->proxy_args,
820 string_append(&config->proxy_args,
828 * Add it to the list. Note we reverse the list to get the
829 * behaviour the user expects. With both the ACL and
830 * actions file, the last match wins. However, the internal
831 * implementations are different: The actions file is stored
832 * in the same order as the file, and scanned completely.
833 * With the ACL, we reverse the order as we load it, then
834 * when we scan it we stop as soon as we get a match.
836 cur_acl->next = config->acl;
837 config->acl = cur_acl;
840 #endif /* def FEATURE_ACL */
842 /* *************************************************************************
843 * enable-edit-actions 0|1
844 * *************************************************************************/
845 #ifdef FEATURE_CGI_EDIT_ACTIONS
846 case hash_enable_edit_actions:
847 if ((*arg != '\0') && (0 != atoi(arg)))
849 config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
853 config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
856 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
858 /* *************************************************************************
859 * enable-remote-toggle 0|1
860 * *************************************************************************/
861 #ifdef FEATURE_CGI_EDIT_ACTIONS
862 case hash_enable_remote_toggle:
863 if ((*arg != '\0') && (0 != atoi(arg)))
865 config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
869 config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
872 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
874 /* *************************************************************************
875 * filterfile file-name
876 * In confdir by default.
877 * *************************************************************************/
878 case hash_filterfile :
880 while ((i < MAX_AF_FILES) && (NULL != config->re_filterfile[i]))
885 if (i >= MAX_AF_FILES)
887 log_error(LOG_LEVEL_FATAL, "Too many 'filterfile' directives in config file - limit is %d.\n"
888 "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
891 config->re_filterfile_short[i] = strdup(arg);
892 p = malloc(strlen(arg) + 1);
895 log_error(LOG_LEVEL_FATAL, "Out of memory");
898 config->re_filterfile[i] = make_path(config->confdir, p);
902 /* *************************************************************************
903 * forward url-pattern (.|http-proxy-host[:port])
904 * *************************************************************************/
906 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
910 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
911 "directive in configuration file.");
912 string_append(&config->proxy_args,
913 "<br>\nWARNING: Wrong number of parameters for "
914 "forward directive in configuration file.");
918 /* allocate a new node */
919 cur_fwd = zalloc(sizeof(*cur_fwd));
922 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
923 /* Never get here - LOG_LEVEL_FATAL causes program exit */
927 cur_fwd->type = SOCKS_NONE;
929 /* Save the URL pattern */
930 if (create_url_spec(cur_fwd->url, vec[0]))
932 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
933 "directive in configuration file.");
934 string_append(&config->proxy_args,
935 "<br>\nWARNING: Bad URL specifier for "
936 "forward directive in configuration file.");
940 /* Parse the parent HTTP proxy host:port */
943 if (strcmp(p, ".") != 0)
945 cur_fwd->forward_host = strdup(p);
947 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
950 cur_fwd->forward_port = atoi(p);
953 if (cur_fwd->forward_port <= 0)
955 cur_fwd->forward_port = 8000;
960 cur_fwd->next = config->forward;
961 config->forward = cur_fwd;
965 /* *************************************************************************
966 * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
967 * *************************************************************************/
968 case hash_forward_socks4:
969 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
973 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
974 "forward-socks4 directive in configuration file.");
975 string_append(&config->proxy_args,
976 "<br>\nWARNING: Wrong number of parameters for "
977 "forward-socks4 directive in configuration file.");
981 /* allocate a new node */
982 cur_fwd = zalloc(sizeof(*cur_fwd));
985 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
986 /* Never get here - LOG_LEVEL_FATAL causes program exit */
990 cur_fwd->type = SOCKS_4;
992 /* Save the URL pattern */
993 if (create_url_spec(cur_fwd->url, vec[0]))
995 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
996 "directive in configuration file.");
997 string_append(&config->proxy_args,
998 "<br>\nWARNING: Bad URL specifier for "
999 "forward-socks4 directive in configuration file.");
1003 /* Parse the SOCKS proxy host[:port] */
1006 if (strcmp(p, ".") != 0)
1008 cur_fwd->gateway_host = strdup(p);
1010 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1013 cur_fwd->gateway_port = atoi(p);
1015 if (cur_fwd->gateway_port <= 0)
1017 cur_fwd->gateway_port = 1080;
1021 /* Parse the parent HTTP proxy host[:port] */
1024 if (strcmp(p, ".") != 0)
1026 cur_fwd->forward_host = strdup(p);
1028 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1031 cur_fwd->forward_port = atoi(p);
1034 if (cur_fwd->forward_port <= 0)
1036 cur_fwd->forward_port = 8000;
1041 cur_fwd->next = config->forward;
1042 config->forward = cur_fwd;
1046 /* *************************************************************************
1047 * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
1048 * *************************************************************************/
1049 case hash_forward_socks4a:
1050 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1054 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1055 "forward-socks4a directive in configuration file.");
1056 string_append(&config->proxy_args,
1057 "<br>\nWARNING: Wrong number of parameters for "
1058 "forward-socks4a directive in configuration file.");
1062 /* allocate a new node */
1063 cur_fwd = zalloc(sizeof(*cur_fwd));
1064 if (cur_fwd == NULL)
1066 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1067 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1071 cur_fwd->type = SOCKS_4A;
1073 /* Save the URL pattern */
1074 if (create_url_spec(cur_fwd->url, vec[0]))
1076 log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
1077 "directive in configuration file.");
1078 string_append(&config->proxy_args,
1079 "<br>\nWARNING: Bad URL specifier for "
1080 "forward-socks4a directive in configuration file.");
1084 /* Parse the SOCKS proxy host[:port] */
1087 cur_fwd->gateway_host = strdup(p);
1089 if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1092 cur_fwd->gateway_port = atoi(p);
1094 if (cur_fwd->gateway_port <= 0)
1096 cur_fwd->gateway_port = 1080;
1099 /* Parse the parent HTTP proxy host[:port] */
1102 if (strcmp(p, ".") != 0)
1104 cur_fwd->forward_host = strdup(p);
1106 if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1109 cur_fwd->forward_port = atoi(p);
1112 if (cur_fwd->forward_port <= 0)
1114 cur_fwd->forward_port = 8000;
1119 cur_fwd->next = config->forward;
1120 config->forward = cur_fwd;
1124 /* *************************************************************************
1125 * forwarded-connect-retries n
1126 * *************************************************************************/
1127 case hash_forwarded_connect_retries :
1128 config->forwarded_connect_retries = atoi(arg);
1131 /* *************************************************************************
1132 * jarfile jar-file-name
1133 * In logdir by default
1134 * *************************************************************************/
1135 #ifdef FEATURE_COOKIE_JAR
1137 freez(config->jarfile);
1138 config->jarfile = make_path(config->logdir, arg);
1140 #endif /* def FEATURE_COOKIE_JAR */
1142 /* *************************************************************************
1143 * listen-address [ip][:port]
1144 * *************************************************************************/
1145 case hash_listen_address :
1146 freez(config->haddr);
1147 config->haddr = strdup(arg);
1150 /* *************************************************************************
1151 * logdir directory-name
1152 * *************************************************************************/
1154 freez(config->logdir);
1155 config->logdir = make_path(NULL, arg);
1158 /* *************************************************************************
1159 * logfile log-file-name
1160 * In logdir by default
1161 * *************************************************************************/
1163 freez(config->logfile);
1164 config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1167 /* *************************************************************************
1168 * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1169 * *************************************************************************/
1171 case hash_permit_access:
1172 vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1174 if ((vec_count != 1) && (vec_count != 2))
1176 log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1177 "permit-access directive in configuration file.");
1178 string_append(&config->proxy_args,
1179 "<br>\nWARNING: Wrong number of parameters for "
1180 "permit-access directive in configuration file.<br><br>\n");
1185 /* allocate a new node */
1186 cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1188 if (cur_acl == NULL)
1190 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1191 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1194 cur_acl->action = ACL_PERMIT;
1196 if (acl_addr(vec[0], cur_acl->src) < 0)
1198 log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1199 "directive in configuration file: \"%s\"", vec[0]);
1200 string_append(&config->proxy_args,
1201 "<br>\nWARNING: Invalid source IP for permit-access directive"
1202 " in configuration file: \"");
1203 string_append(&config->proxy_args,
1205 string_append(&config->proxy_args,
1212 if (acl_addr(vec[1], cur_acl->dst) < 0)
1214 log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1215 "permit-access directive in configuration file: \"%s\"",
1217 string_append(&config->proxy_args,
1218 "<br>\nWARNING: Invalid destination IP for permit-access directive"
1219 " in configuration file: \"");
1220 string_append(&config->proxy_args,
1222 string_append(&config->proxy_args,
1230 * Add it to the list. Note we reverse the list to get the
1231 * behaviour the user expects. With both the ACL and
1232 * actions file, the last match wins. However, the internal
1233 * implementations are different: The actions file is stored
1234 * in the same order as the file, and scanned completely.
1235 * With the ACL, we reverse the order as we load it, then
1236 * when we scan it we stop as soon as we get a match.
1238 cur_acl->next = config->acl;
1239 config->acl = cur_acl;
1242 #endif /* def FEATURE_ACL */
1244 /* *************************************************************************
1245 * proxy-info-url url
1246 * *************************************************************************/
1247 case hash_proxy_info_url :
1248 freez(config->proxy_info_url);
1249 config->proxy_info_url = strdup(arg);
1252 /* *************************************************************************
1254 * *************************************************************************/
1255 case hash_single_threaded :
1256 config->multi_threaded = 0;
1259 /* *************************************************************************
1261 * *************************************************************************/
1262 #ifdef FEATURE_TOGGLE
1264 global_toggle_state = atoi(arg);
1266 #endif /* def FEATURE_TOGGLE */
1268 /* *************************************************************************
1269 * trust-info-url url
1270 * *************************************************************************/
1271 #ifdef FEATURE_TRUST
1272 case hash_trust_info_url :
1273 enlist(config->trust_info, arg);
1275 #endif /* def FEATURE_TRUST */
1277 /* *************************************************************************
1278 * trustfile filename
1279 * (In confdir by default.)
1280 * *************************************************************************/
1281 #ifdef FEATURE_TRUST
1282 case hash_trustfile :
1283 freez(config->trustfile);
1284 config->trustfile = make_path(config->confdir, arg);
1286 #endif /* def FEATURE_TRUST */
1288 /* *************************************************************************
1290 * *************************************************************************/
1291 case hash_usermanual :
1292 freez(config->usermanual);
1293 config->usermanual = strdup(arg);
1296 /* *************************************************************************
1297 * Win32 Console options:
1298 * *************************************************************************/
1300 /* *************************************************************************
1302 * *************************************************************************/
1304 case hash_hide_console :
1307 #endif /*def _WIN_CONSOLE*/
1310 /* *************************************************************************
1311 * Win32 GUI options:
1312 * *************************************************************************/
1314 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1315 /* *************************************************************************
1316 * activity-animation (0|1)
1317 * *************************************************************************/
1318 case hash_activity_animation :
1319 g_bShowActivityAnimation = atoi(arg);
1322 /* *************************************************************************
1323 * close-button-minimizes (0|1)
1324 * *************************************************************************/
1325 case hash_close_button_minimizes :
1326 g_bCloseHidesWindow = atoi(arg);
1329 /* *************************************************************************
1330 * log-buffer-size (0|1)
1331 * *************************************************************************/
1332 case hash_log_buffer_size :
1333 g_bLimitBufferSize = atoi(arg);
1336 /* *************************************************************************
1337 * log-font-name fontnane
1338 * *************************************************************************/
1339 case hash_log_font_name :
1340 strcpy( g_szFontFaceName, arg );
1343 /* *************************************************************************
1345 * *************************************************************************/
1346 case hash_log_font_size :
1347 g_nFontSize = atoi(arg);
1350 /* *************************************************************************
1351 * log-highlight-messages (0|1)
1352 * *************************************************************************/
1353 case hash_log_highlight_messages :
1354 g_bHighlightMessages = atoi(arg);
1357 /* *************************************************************************
1359 * *************************************************************************/
1360 case hash_log_max_lines :
1361 g_nMaxBufferLines = atoi(arg);
1364 /* *************************************************************************
1365 * log-messages (0|1)
1366 * *************************************************************************/
1367 case hash_log_messages :
1368 g_bLogMessages = atoi(arg);
1371 /* *************************************************************************
1372 * show-on-task-bar (0|1)
1373 * *************************************************************************/
1374 case hash_show_on_task_bar :
1375 g_bShowOnTaskBar = atoi(arg);
1378 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1381 /* *************************************************************************
1382 * Warnings about unsupported features
1383 * *************************************************************************/
1385 case hash_deny_access:
1386 #endif /* ndef FEATURE_ACL */
1387 #ifndef FEATURE_CGI_EDIT_ACTIONS
1388 case hash_enable_edit_actions:
1389 case hash_enable_remote_toggle:
1390 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1391 #ifndef FEATURE_COOKIE_JAR
1393 #endif /* ndef FEATURE_COOKIE_JAR */
1395 case hash_permit_access:
1396 #endif /* ndef FEATURE_ACL */
1397 #ifndef FEATURE_TOGGLE
1399 #endif /* ndef FEATURE_TOGGLE */
1400 #ifndef FEATURE_TRUST
1401 case hash_trustfile :
1402 case hash_trust_info_url :
1403 #endif /* ndef FEATURE_TRUST */
1405 #ifndef _WIN_CONSOLE
1406 case hash_hide_console :
1407 #endif /* ndef _WIN_CONSOLE */
1409 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1410 case hash_activity_animation :
1411 case hash_close_button_minimizes :
1412 case hash_log_buffer_size :
1413 case hash_log_font_name :
1414 case hash_log_font_size :
1415 case hash_log_highlight_messages :
1416 case hash_log_max_lines :
1417 case hash_log_messages :
1418 case hash_show_on_task_bar :
1419 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1420 /* These warnings are annoying - so hide them. -- Jon */
1421 /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1424 /* *************************************************************************/
1426 /* *************************************************************************/
1428 * I decided that I liked this better as a warning than an
1429 * error. To change back to an error, just change log level
1430 * to LOG_LEVEL_FATAL.
1432 log_error(LOG_LEVEL_ERROR, "Ignoring unrecognized directive '%s' (%luul) in line %lu "
1433 "in configuration file (%s).", buf, hash_string(cmd), linenum, configfile);
1434 string_append(&config->proxy_args,
1435 " <b><font color=\"red\">WARNING: unrecognized directive, ignored</font></b>");
1438 /* *************************************************************************/
1439 } /* end switch( hash_string(cmd) ) */
1440 } /* end while ( read_config_line(...) ) */
1444 if (NULL == config->proxy_args)
1446 log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1449 init_error_log(Argv[0], config->logfile, config->debug);
1451 if (config->actions_file[0])
1453 add_loader(load_actions_file, config);
1456 if (config->re_filterfile)
1458 add_loader(load_re_filterfile, config);
1461 #ifdef FEATURE_TRUST
1462 if (config->trustfile)
1464 add_loader(load_trustfile, config);
1466 #endif /* def FEATURE_TRUST */
1468 #ifdef FEATURE_COOKIE_JAR
1469 if ( NULL != config->jarfile )
1471 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1473 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1474 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1476 setbuf(config->jar, NULL);
1478 #endif /* def FEATURE_COOKIE_JAR */
1480 if ( NULL == config->haddr )
1482 config->haddr = strdup( HADDR_DEFAULT );
1485 if ( NULL != config->haddr )
1487 if (NULL != (p = strchr(config->haddr, ':')))
1492 config->hport = atoi(p);
1496 if (config->hport <= 0)
1499 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1500 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1502 if (*config->haddr == '\0')
1504 config->haddr = NULL;
1509 * Want to run all the loaders once now.
1511 * Need to set up a fake csp, so they can get to the config.
1513 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1514 fake_csp->config = config;
1516 if (run_loader(fake_csp))
1519 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1520 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1524 /* FIXME: this is a kludge for win32 */
1525 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1527 g_default_actions_file = config->actions_file[1]; /* FIXME Hope this is default.action */
1528 g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1529 g_re_filterfile = config->re_filterfile;
1531 #ifdef FEATURE_TRUST
1532 g_trustfile = config->trustfile;
1533 #endif /* def FEATURE_TRUST */
1536 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1537 /* FIXME: end kludge */
1540 config->need_bind = 1;
1542 if (current_configfile)
1544 struct configuration_spec * oldcfg = (struct configuration_spec *)
1545 current_configfile->f;
1547 * Check if config->haddr,hport == oldcfg->haddr,hport
1549 * The following could be written more compactly as a single,
1550 * (unreadably long) if statement.
1552 config->need_bind = 0;
1553 if (config->hport != oldcfg->hport)
1555 config->need_bind = 1;
1557 else if (config->haddr == NULL)
1559 if (oldcfg->haddr != NULL)
1561 config->need_bind = 1;
1564 else if (oldcfg->haddr == NULL)
1566 config->need_bind = 1;
1568 else if (0 != strcmp(config->haddr, oldcfg->haddr))
1570 config->need_bind = 1;
1573 current_configfile->unloader = unload_configfile;
1576 fs->next = files->next;
1579 current_configfile = fs;
1585 /*********************************************************************
1587 * Function : savearg
1589 * Description : Called from `load_config'. It saves each non-empty
1590 * and non-comment line from config into
1591 * config->proxy_args. This is used to create the
1592 * show-proxy-args page. On error, frees
1593 * config->proxy_args and sets it to NULL
1596 * 1 : command = config setting that was found
1597 * 2 : argument = the setting's argument (if any)
1598 * 3 : config = Configuration to save into.
1602 *********************************************************************/
1603 static void savearg(char *command, char *argument, struct configuration_spec * config)
1613 * Add config option name embedded in
1614 * link to it's section in the user-manual
1616 buf = strdup("\n<br><a href=\"");
1617 string_append(&buf, config->usermanual);
1618 string_append(&buf, CONFIG_HELP_PREFIX);
1619 string_join (&buf, string_toupper(command));
1620 string_append(&buf, "\">");
1621 string_append(&buf, command);
1622 string_append(&buf, "</a> ");
1626 freez(config->proxy_args);
1630 if ( (NULL != argument) && ('\0' != *argument) )
1632 s = html_encode(argument);
1636 freez(config->proxy_args);
1640 if (strncmpic(argument, "http://", 7) == 0)
1642 string_append(&buf, "<a href=\"");
1643 string_append(&buf, s);
1644 string_append(&buf, "\">");
1645 string_join (&buf, s);
1646 string_append(&buf, "</a>");
1650 string_join (&buf, s);
1654 string_join(&config->proxy_args, buf);