1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.12 2001/06/05 20:04:09 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/loadcfg.c,v $
6 * Purpose : Loads settings from the configuration file into
7 * global variables. This file contains both the
8 * routine to load the configuration and the global
9 * variables it writes to.
11 * Copyright : Written by and Copyright (C) 2001 the SourceForge
12 * IJBSWA team. http://ijbswa.sourceforge.net
14 * Based on the Internet Junkbuster originally written
15 * by and Copyright (C) 1997 Anonymous Coders and
16 * Junkbusters Corporation. http://www.junkbusters.com
18 * This program is free software; you can redistribute it
19 * and/or modify it under the terms of the GNU General
20 * Public License as published by the Free Software
21 * Foundation; either version 2 of the License, or (at
22 * your option) any later version.
24 * This program is distributed in the hope that it will
25 * be useful, but WITHOUT ANY WARRANTY; without even the
26 * implied warranty of MERCHANTABILITY or FITNESS FOR A
27 * PARTICULAR PURPOSE. See the GNU General Public
28 * License for more details.
30 * The GNU General Public License should be included with
31 * this file. If not, you can view it at
32 * http://www.gnu.org/copyleft/gpl.html
33 * or write to the Free Software Foundation, Inc., 59
34 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 * Revision 1.12 2001/06/05 20:04:09 jongfoster
39 * Now uses _snprintf() in place of snprintf() under Win32.
41 * Revision 1.11 2001/06/04 18:31:58 swa
42 * files are now prefixed with either `confdir' or `logdir'.
43 * `make redhat-dist' replaces both entries confdir and logdir
46 * Revision 1.10 2001/06/03 19:11:54 oes
47 * introduced confdir option
49 * Revision 1.10 2001/06/03 11:03:48 oes
56 * adapted to new enlist_unique arg format
60 * introduced confdir option
62 * filters.c filtrers.h
64 * extracted-CGI relevant stuff
72 * support for new cgi mechansim
76 * functions for new list type: "map"
77 * extended enlist_unique
84 * deleted const struct interceptors
92 * added struct http_response
93 * changes struct interceptors to struct cgi_dispatcher
94 * moved HTML stuff to cgi.h
103 * Revision 1.9 2001/06/01 20:06:24 jongfoster
104 * Removed support for "tinygif" option - moved to actions file.
106 * Revision 1.8 2001/05/31 21:27:13 jongfoster
107 * Removed many options from the config file and into the
108 * "actions" file: add_forwarded, suppress_vanilla_wafer,
109 * wafer, add_header, user_agent, referer, from
110 * Also globally replaced "permission" with "action".
112 * Revision 1.7 2001/05/29 09:50:24 jongfoster
113 * Unified blocklist/imagelist/permissionslist.
114 * File format is still under discussion, but the internal changes
117 * Also modified interceptor behaviour:
118 * - We now intercept all URLs beginning with one of the following
119 * prefixes (and *only* these prefixes):
121 * * http://ijbswa.sf.net/config/
122 * * http://ijbswa.sourceforge.net/config/
123 * - New interceptors "home page" - go to http://i.j.b/ to see it.
124 * - Internal changes so that intercepted and fast redirect pages
125 * are not replaced with an image.
126 * - Interceptors now have the option to send a binary page direct
127 * to the client. (i.e. ijb-send-banner uses this)
128 * - Implemented show-url-info interceptor. (Which is why I needed
129 * the above interceptors changes - a typical URL is
130 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
131 * The previous mechanism would not have intercepted that, and
132 * if it had been intercepted then it then it would have replaced
135 * Revision 1.6 2001/05/26 00:28:36 jongfoster
136 * Automatic reloading of config file.
137 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
138 * Most of the global variables have been moved to a new
139 * struct configuration_spec, accessed through csp->config->globalname
140 * Most of the globals remaining are used by the Win32 GUI.
142 * Revision 1.5 2001/05/25 22:34:30 jongfoster
145 * Revision 1.4 2001/05/22 18:46:04 oes
147 * - Enabled filtering banners by size rather than URL
148 * by adding patterns that replace all standard banner
149 * sizes with the "Junkbuster" gif to the re_filterfile
151 * - Enabled filtering WebBugs by providing a pattern
152 * which kills all 1x1 images
154 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
155 * which is selected by the (nonstandard and therefore
156 * capital) letter 'U' in the option string.
157 * It causes the quantifiers to be ungreedy by default.
158 * Appending a ? turns back to greedy (!).
160 * - Added a new interceptor ijb-send-banner, which
161 * sends back the "Junkbuster" gif. Without imagelist or
162 * MSIE detection support, or if tinygif = 1, or the
163 * URL isn't recognized as an imageurl, a lame HTML
164 * explanation is sent instead.
166 * - Added new feature, which permits blocking remote
167 * script redirects and firing back a local redirect
169 * The feature is conditionally compiled, i.e. it
170 * can be disabled with --disable-fast-redirects,
171 * plus it must be activated by a "fast-redirects"
172 * line in the config file, has its own log level
173 * and of course wants to be displayed by show-proxy-args
174 * Note: Boy, all the #ifdefs in 1001 locations and
175 * all the fumbling with configure.in and acconfig.h
176 * were *way* more work than the feature itself :-(
178 * - Because a generic redirect template was needed for
179 * this, tinygif = 3 now uses the same.
181 * - Moved GIFs, and other static HTTP response templates
186 * - Removed some >400 CRs again (Jon, you really worked
189 * Revision 1.3 2001/05/20 01:21:20 jongfoster
190 * Version 2.9.4 checkin.
191 * - Merged popupfile and cookiefile, and added control over PCRS
192 * filtering, in new "permissionsfile".
193 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
194 * file error you now get a message box (in the Win32 GUI) rather
195 * than the program exiting with no explanation.
196 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
198 * - Removed tabs from "config"
199 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
200 * - Bumped up version number.
202 * Revision 1.2 2001/05/17 23:01:01 oes
203 * - Cleaned CRLF's from the sources and related files
205 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
206 * Initial import of version 2.9.3 source tree
209 *********************************************************************/
215 #include <sys/types.h>
225 # include <sys/timeb.h>
226 # include <windows.h>
228 # include <process.h>
231 # endif /* def TOGGLE */
234 # ifndef _WIN_CONSOLE
236 # endif /* ndef _WIN_CONSOLE */
238 /* VC++ has "_snprintf", not "snprintf" */
239 #define snprintf _snprintf
241 #else /* ifndef _WIN32 */
244 # include <sys/time.h>
245 # include <sys/wait.h>
246 # include <sys/stat.h>
256 #include "showargs.h"
258 #include "killpopup.h"
259 #include "miscutil.h"
261 #include "jbsockets.h"
264 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
267 * Fix a problem with Solaris. There should be no effect on other
269 * Solaris's isspace() is a macro which uses it's argument directly
270 * as an array index. Therefore we need to make sure that high-bit
271 * characters generate +ve values, and ideally we also want to make
272 * the argument match the declared parameter type of "int".
274 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
275 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
278 /* by haroon - indicates if ijb is enabled */
279 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
282 /* The filename of the configfile */
283 const char *configfile = NULL;
286 * The load_config function is now going to call `init_proxy_args',
287 * so it will need argc and argv. So we need to have these
288 * globally available.
291 const char **Argv = NULL;
293 static struct file_list *current_configfile = NULL;
297 * This takes the "cryptic" hash of each keyword and aliases them to
298 * something a little more readable. This also makes changing the
299 * hash values easier if they should change or the hash algorthm changes.
300 * Use the included "hash" program to find out what the hash will be
301 * for any string supplied on the command line. (Or just put it in the
302 * config file and read the number from the error message in the log).
306 #define hash_aclfile 1908516ul
307 #define hash_actions_file 3825730796ul /* FIXME "permissionsfile" */
308 #define hash_debug 78263ul
309 #define hash_confdir 1978389lu
310 #define hash_logdir 422889lu
311 #define hash_forwardfile 1268669141ul
312 #define hash_jarfile 2046641ul
313 #define hash_listen_address 1255650842ul
314 #define hash_logfile 2114766ul
315 #define hash_re_filterfile 3877522444ul
316 #define hash_single_threaded 4250084780ul
317 #define hash_suppress_blocklists 1948693308ul
318 #define hash_toggle 447966ul
319 #define hash_trust_info_url 449869467ul
320 #define hash_trustfile 56494766ul
322 #define hash_hide_console 2048809870ul
324 #define hash_activity_animation 1817904738ul
325 #define hash_close_button_minimizes 3651284693ul
326 #define hash_log_buffer_size 2918070425ul
327 #define hash_log_font_name 2866730124ul
328 #define hash_log_font_size 2866731014ul
329 #define hash_log_highlight_messages 4032101240ul
330 #define hash_log_max_lines 2868344173ul
331 #define hash_log_messages 2291744899ul
332 #define hash_show_on_task_bar 215410365ul
335 /*********************************************************************
337 * Function : unload_configfile
339 * Description : Free the config structure and all components.
342 * 1 : data: struct configuration_spec to unload
346 *********************************************************************/
347 void unload_configfile (void * data)
349 struct configuration_spec * config = (struct configuration_spec *)data;
352 if ( NULL != config->jar )
354 fclose( config->jar );
357 #endif /* def JAR_FILES */
359 freez((char *)config->confdir);
360 freez((char *)config->logdir);
362 freez((char *)config->haddr);
363 freez((char *)config->logfile);
365 freez((char *)config->actions_file);
366 freez((char *)config->forwardfile);
369 freez((char *)config->aclfile);
370 #endif /* def ACL_FILES */
373 freez((char *)config->jarfile);
374 #endif /* def JAR_FILES */
376 #ifndef SPLIT_PROXY_ARGS
377 freez((char *)config->suppress_message);
378 #endif /* ndef SPLIT_PROXY_ARGS */
381 freez((char *)config->re_filterfile);
382 #endif /* def PCRS */
387 /*********************************************************************
389 * Function : load_config
391 * Description : Load the config file and all parameters.
394 * 1 : csp = Client state (the config member will be
395 * filled in by this function).
397 * Returns : 0 => Ok, everything else is an error.
399 *********************************************************************/
400 struct configuration_spec * load_config(void)
404 FILE *configfp = NULL;
405 struct configuration_spec * config = NULL;
406 struct client_state * fake_csp;
407 struct file_list *fs;
409 if (!check_file_changed(current_configfile, configfile, &fs))
411 /* No need to load */
412 return ((struct configuration_spec *)current_configfile->f);
416 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
420 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
426 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
432 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
433 /* Never get here - LOG_LEVEL_FATAL causes program exit */
437 * This is backwards from how it's usually done.
438 * Following the usual pattern, "fs" would be stored in a member
439 * variable in "csp", and then we'd access "config" from "fs->f",
440 * using a cast. However, "config" is used so often that a
441 * cast each time would be very ugly, and the extra indirection
442 * would waste CPU cycles. Therefore we store "config" in
443 * "csp->config", and "fs" in "csp->config->config_file_list".
445 config->config_file_list = fs;
447 init_proxy_args(Argc, Argv, config);
453 config->multi_threaded = 1;
454 config->hport = HADDR_PORT;
456 if ((configfp = fopen(configfile, "r")) == NULL)
458 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
460 /* Never get here - LOG_LEVEL_FATAL causes program exit */
463 while (read_config_line(buf, sizeof(buf), configfp, fs) != NULL)
471 /* Copy command (i.e. up to space or tab) into cmd */
474 while (*p && (*p != ' ') && (*p != '\t'))
480 /* Skip over the whitespace in buf */
481 while (*p && ((*p == ' ') || (*p == '\t')))
486 /* Copy the argument into arg */
489 /* Should never happen, but check this anyway */
495 /* Make sure the command field is lower case */
500 *p = ijb_tolower(*p);
504 /* Save the argument for show-proxy-args */
505 savearg(cmd, arg, config);
508 switch( hash_string( cmd ) )
511 case hash_trustfile :
512 freez((char *)config->trustfile);
513 config->trustfile = make_path(config->confdir, arg);
516 case hash_trust_info_url :
517 enlist(config->trust_info, arg);
519 #endif /* def TRUST_FILES */
522 config->debug |= atoi(arg);
526 freez((char *)config->confdir);
527 config->confdir = strdup(arg);
531 freez((char *)config->logdir);
532 config->logdir = strdup(arg);
535 case hash_single_threaded :
536 config->multi_threaded = 0;
539 case hash_actions_file :
540 freez((char *)config->actions_file);
541 config->actions_file = make_path(config->confdir, arg);
545 freez((char *)config->logfile);
546 config->logfile = make_path(config->logdir, arg);
551 freez((char *)config->jarfile);
552 config->jarfile = make_path(config->logdir, arg);
554 #endif /* def JAR_FILES */
556 case hash_listen_address :
557 freez((char *)config->haddr);
558 config->haddr = strdup(arg);
561 case hash_forwardfile :
562 freez((char *)config->forwardfile);
563 config->forwardfile = make_path(config->confdir, arg);
568 freez((char *)config->aclfile);
569 config->aclfile = make_path(config->confdir, arg);
571 #endif /* def ACL_FILES */
574 case hash_re_filterfile :
575 freez((char *)config->re_filterfile);
576 config->re_filterfile = strdup(arg);
578 #endif /* def PCRS */
581 case hash_hide_console :
584 #endif /*def _WIN_CONSOLE*/
586 #ifndef SPLIT_PROXY_ARGS
587 case hash_suppress_blocklists :
590 config->suppress_message = strdup(arg);
594 /* There will be NO reference in proxy-args. */
595 config->suppress_message = NULL;
598 config->suppress_blocklists = 1;
600 #endif /* ndef SPLIT_PROXY_ARGS */
604 g_bToggleIJB = atoi(arg);
606 #endif /* def TOGGLE */
608 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
609 case hash_activity_animation :
610 g_bShowActivityAnimation = atoi(arg);
613 case hash_log_messages :
614 g_bLogMessages = atoi(arg);
617 case hash_log_highlight_messages :
618 g_bHighlightMessages = atoi(arg);
621 case hash_log_buffer_size :
622 g_bLimitBufferSize = atoi(arg);
625 case hash_log_max_lines :
626 g_nMaxBufferLines = atoi(arg);
629 case hash_log_font_name :
630 strcpy( g_szFontFaceName, arg );
633 case hash_log_font_size :
634 g_nFontSize = atoi(arg);
637 case hash_show_on_task_bar :
638 g_bShowOnTaskBar = atoi(arg);
641 case hash_close_button_minimizes :
642 g_bCloseHidesWindow = atoi(arg);
644 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
646 /* Warnings about unsupported features */
649 case hash_re_filterfile :
650 #endif /* ndef PCRS */
653 #endif /* ndef TOGGLE */
654 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
655 case hash_activity_animation :
656 case hash_log_messages :
657 case hash_log_highlight_messages :
658 case hash_log_buffer_size :
659 case hash_log_max_lines :
660 case hash_log_font_name :
661 case hash_log_font_size :
662 case hash_show_on_task_bar :
663 case hash_close_button_minimizes :
664 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
666 case hash_hide_console :
667 #endif /* ndef _WIN_CONSOLE */
670 #endif /* ndef JAR_FILES */
673 #endif /* ndef ACL_FILES */
674 #ifdef SPLIT_PROXY_ARGS
675 case hash_suppress_blocklists :
676 #endif /* def SPLIT_PROXY_ARGS */
677 log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd);
682 * I decided that I liked this better as a warning than an
683 * error. To change back to an error, just change log level
684 * to LOG_LEVEL_FATAL.
686 log_error(LOG_LEVEL_ERROR, "Unrecognized directive (%lulu) in "
687 "configuration file: \"%s\"", hash_string( cmd ), buf);
688 p = malloc( BUFSIZ );
691 sprintf( p, "<br>\nWARNING: unrecognized directive : %s<br><br>\n", buf );
692 config->proxy_args_invocation = strsav( config->proxy_args_invocation, p );
696 } /* end switch( hash_string(cmd) ) */
697 } /* end while ( read_config_line(...) ) */
701 init_error_log(Argv[0], config->logfile, config->debug);
703 if (config->actions_file)
705 add_loader(load_actions_file, config);
708 if (config->forwardfile)
710 add_loader(load_forwardfile, config);
716 add_loader(load_aclfile, config);
718 #endif /* def ACL_FILES */
721 if (config->re_filterfile)
723 add_loader(load_re_filterfile, config);
725 #endif /* def PCRS */
728 if (config->trustfile)
730 add_loader(load_trustfile, config);
735 if ( NULL != config->jarfile )
737 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
739 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
740 /* Never get here - LOG_LEVEL_FATAL causes program exit */
742 setbuf(config->jar, NULL);
744 #endif /* def JAR_FILES */
746 if ( NULL == config->haddr )
748 config->haddr = strdup( HADDR_DEFAULT );
751 if ( NULL != config->haddr )
753 if ((p = strchr(config->haddr, ':')))
758 config->hport = atoi(p);
762 if (config->hport <= 0)
765 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
766 /* Never get here - LOG_LEVEL_FATAL causes program exit */
768 if (*config->haddr == '\0')
770 config->haddr = NULL;
775 * Want to run all the loaders once now.
777 * Need to set up a fake csp, so they can get to the config.
779 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
780 fake_csp->config = config;
782 if (run_loader(fake_csp))
785 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
786 /* Never get here - LOG_LEVEL_FATAL causes program exit */
790 #ifndef SPLIT_PROXY_ARGS
791 if (!suppress_blocklists)
793 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
795 #endif /* ndef SPLIT_PROXY_ARGS */
797 /* FIXME: this is a kludge for win32 */
798 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
800 g_actions_file = config->actions_file;
801 g_forwardfile = config->forwardfile;
803 g_aclfile = config->aclfile;
804 #endif /* def ACL_FILES */
806 g_re_filterfile = config->re_filterfile;
809 g_trustfile = config->trustfile;
813 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
814 /* FIXME: end kludge */
817 config->need_bind = 1;
819 if (current_configfile)
821 struct configuration_spec * oldcfg = (struct configuration_spec *)
822 current_configfile->f;
824 * Check if config->haddr,hport == oldcfg->haddr,hport
826 * The following could be written more compactly as a single,
827 * (unreadably long) if statement.
829 config->need_bind = 0;
830 if (config->hport != oldcfg->hport)
832 config->need_bind = 1;
834 else if (config->haddr == NULL)
836 if (oldcfg->haddr != NULL)
838 config->need_bind = 1;
841 else if (oldcfg->haddr == NULL)
843 config->need_bind = 1;
845 else if (0 != strcmp(config->haddr, oldcfg->haddr))
847 config->need_bind = 1;
850 current_configfile->unloader = unload_configfile;
853 fs->next = files->next;
856 current_configfile = fs;