1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.10 2001/06/03 11:03:48 oes 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.10 2001/06/03 11:03:48 oes
45 * adapted to new enlist_unique arg format
49 * introduced confdir option
51 * filters.c filtrers.h
53 * extracted-CGI relevant stuff
61 * support for new cgi mechansim
65 * functions for new list type: "map"
66 * extended enlist_unique
73 * deleted const struct interceptors
81 * added struct http_response
82 * changes struct interceptors to struct cgi_dispatcher
83 * moved HTML stuff to cgi.h
92 * Revision 1.9 2001/06/01 20:06:24 jongfoster
93 * Removed support for "tinygif" option - moved to actions file.
95 * Revision 1.8 2001/05/31 21:27:13 jongfoster
96 * Removed many options from the config file and into the
97 * "actions" file: add_forwarded, suppress_vanilla_wafer,
98 * wafer, add_header, user_agent, referer, from
99 * Also globally replaced "permission" with "action".
101 * Revision 1.7 2001/05/29 09:50:24 jongfoster
102 * Unified blocklist/imagelist/permissionslist.
103 * File format is still under discussion, but the internal changes
106 * Also modified interceptor behaviour:
107 * - We now intercept all URLs beginning with one of the following
108 * prefixes (and *only* these prefixes):
110 * * http://ijbswa.sf.net/config/
111 * * http://ijbswa.sourceforge.net/config/
112 * - New interceptors "home page" - go to http://i.j.b/ to see it.
113 * - Internal changes so that intercepted and fast redirect pages
114 * are not replaced with an image.
115 * - Interceptors now have the option to send a binary page direct
116 * to the client. (i.e. ijb-send-banner uses this)
117 * - Implemented show-url-info interceptor. (Which is why I needed
118 * the above interceptors changes - a typical URL is
119 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
120 * The previous mechanism would not have intercepted that, and
121 * if it had been intercepted then it then it would have replaced
124 * Revision 1.6 2001/05/26 00:28:36 jongfoster
125 * Automatic reloading of config file.
126 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
127 * Most of the global variables have been moved to a new
128 * struct configuration_spec, accessed through csp->config->globalname
129 * Most of the globals remaining are used by the Win32 GUI.
131 * Revision 1.5 2001/05/25 22:34:30 jongfoster
134 * Revision 1.4 2001/05/22 18:46:04 oes
136 * - Enabled filtering banners by size rather than URL
137 * by adding patterns that replace all standard banner
138 * sizes with the "Junkbuster" gif to the re_filterfile
140 * - Enabled filtering WebBugs by providing a pattern
141 * which kills all 1x1 images
143 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
144 * which is selected by the (nonstandard and therefore
145 * capital) letter 'U' in the option string.
146 * It causes the quantifiers to be ungreedy by default.
147 * Appending a ? turns back to greedy (!).
149 * - Added a new interceptor ijb-send-banner, which
150 * sends back the "Junkbuster" gif. Without imagelist or
151 * MSIE detection support, or if tinygif = 1, or the
152 * URL isn't recognized as an imageurl, a lame HTML
153 * explanation is sent instead.
155 * - Added new feature, which permits blocking remote
156 * script redirects and firing back a local redirect
158 * The feature is conditionally compiled, i.e. it
159 * can be disabled with --disable-fast-redirects,
160 * plus it must be activated by a "fast-redirects"
161 * line in the config file, has its own log level
162 * and of course wants to be displayed by show-proxy-args
163 * Note: Boy, all the #ifdefs in 1001 locations and
164 * all the fumbling with configure.in and acconfig.h
165 * were *way* more work than the feature itself :-(
167 * - Because a generic redirect template was needed for
168 * this, tinygif = 3 now uses the same.
170 * - Moved GIFs, and other static HTTP response templates
175 * - Removed some >400 CRs again (Jon, you really worked
178 * Revision 1.3 2001/05/20 01:21:20 jongfoster
179 * Version 2.9.4 checkin.
180 * - Merged popupfile and cookiefile, and added control over PCRS
181 * filtering, in new "permissionsfile".
182 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
183 * file error you now get a message box (in the Win32 GUI) rather
184 * than the program exiting with no explanation.
185 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
187 * - Removed tabs from "config"
188 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
189 * - Bumped up version number.
191 * Revision 1.2 2001/05/17 23:01:01 oes
192 * - Cleaned CRLF's from the sources and related files
194 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
195 * Initial import of version 2.9.3 source tree
198 *********************************************************************/
204 #include <sys/types.h>
214 # include <sys/timeb.h>
215 # include <windows.h>
217 # include <process.h>
220 # endif /* def TOGGLE */
223 # ifndef _WIN_CONSOLE
225 # endif /* ndef _WIN_CONSOLE */
227 #else /* ifndef _WIN32 */
230 # include <sys/time.h>
231 # include <sys/wait.h>
232 # include <sys/stat.h>
242 #include "showargs.h"
244 #include "killpopup.h"
245 #include "miscutil.h"
247 #include "jbsockets.h"
250 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
253 * Fix a problem with Solaris. There should be no effect on other
255 * Solaris's isspace() is a macro which uses it's argument directly
256 * as an array index. Therefore we need to make sure that high-bit
257 * characters generate +ve values, and ideally we also want to make
258 * the argument match the declared parameter type of "int".
260 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
261 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
264 /* by haroon - indicates if ijb is enabled */
265 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
268 /* The filename of the configfile */
269 const char *configfile = NULL;
272 * The load_config function is now going to call `init_proxy_args',
273 * so it will need argc and argv. So we need to have these
274 * globally available.
277 const char **Argv = NULL;
279 static struct file_list *current_configfile = NULL;
283 * This takes the "cryptic" hash of each keyword and aliases them to
284 * something a little more readable. This also makes changing the
285 * hash values easier if they should change or the hash algorthm changes.
286 * Use the included "hash" program to find out what the hash will be
287 * for any string supplied on the command line. (Or just put it in the
288 * config file and read the number from the error message in the log).
292 #define hash_aclfile 1908516ul
293 #define hash_actions_file 3825730796ul /* FIXME "permissionsfile" */
294 #define hash_debug 78263ul
295 #define hash_confdir 1978389lu
296 #define hash_forwardfile 1268669141ul
297 #define hash_jarfile 2046641ul
298 #define hash_listen_address 1255650842ul
299 #define hash_logfile 2114766ul
300 #define hash_re_filterfile 3877522444ul
301 #define hash_single_threaded 4250084780ul
302 #define hash_suppress_blocklists 1948693308ul
303 #define hash_toggle 447966ul
304 #define hash_trust_info_url 449869467ul
305 #define hash_trustfile 56494766ul
307 #define hash_hide_console 2048809870ul
309 #define hash_activity_animation 1817904738ul
310 #define hash_close_button_minimizes 3651284693ul
311 #define hash_log_buffer_size 2918070425ul
312 #define hash_log_font_name 2866730124ul
313 #define hash_log_font_size 2866731014ul
314 #define hash_log_highlight_messages 4032101240ul
315 #define hash_log_max_lines 2868344173ul
316 #define hash_log_messages 2291744899ul
317 #define hash_show_on_task_bar 215410365ul
321 /*********************************************************************
323 * Function : unload_configfile
325 * Description : Free the config structure and all components.
328 * 1 : data: struct configuration_spec to unload
332 *********************************************************************/
333 void unload_configfile (void * data)
335 struct configuration_spec * config = (struct configuration_spec *)data;
338 if ( NULL != config->jar )
340 fclose( config->jar );
343 #endif /* def JAR_FILES */
345 freez((char *)config->haddr);
346 freez((char *)config->logfile);
348 freez((char *)config->actions_file);
349 freez((char *)config->forwardfile);
352 freez((char *)config->aclfile);
353 #endif /* def ACL_FILES */
356 freez((char *)config->jarfile);
357 #endif /* def JAR_FILES */
359 #ifndef SPLIT_PROXY_ARGS
360 freez((char *)config->suppress_message);
361 #endif /* ndef SPLIT_PROXY_ARGS */
364 freez((char *)config->re_filterfile);
365 #endif /* def PCRS */
370 /*********************************************************************
372 * Function : load_config
374 * Description : Load the config file and all parameters.
377 * 1 : csp = Client state (the config member will be
378 * filled in by this function).
380 * Returns : 0 => Ok, everything else is an error.
382 *********************************************************************/
383 struct configuration_spec * load_config(void)
387 FILE *configfp = NULL;
388 struct configuration_spec * config = NULL;
389 struct client_state * fake_csp;
391 struct file_list *fs;
393 if (!check_file_changed(current_configfile, configfile, &fs))
395 /* No need to load */
396 return ((struct configuration_spec *)current_configfile->f);
400 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
404 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
410 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
416 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
417 /* Never get here - LOG_LEVEL_FATAL causes program exit */
421 * This is backwards from how it's usually done.
422 * Following the usual pattern, "fs" would be stored in a member
423 * variable in "csp", and then we'd access "config" from "fs->f",
424 * using a cast. However, "config" is used so often that a
425 * cast each time would be very ugly, and the extra indirection
426 * would waste CPU cycles. Therefore we store "config" in
427 * "csp->config", and "fs" in "csp->config->config_file_list".
429 config->config_file_list = fs;
431 init_proxy_args(Argc, Argv, config);
437 config->multi_threaded = 1;
438 config->hport = HADDR_PORT;
440 if ((configfp = fopen(configfile, "r")) == NULL)
442 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
444 /* Never get here - LOG_LEVEL_FATAL causes program exit */
447 while (read_config_line(buf, sizeof(buf), configfp, fs) != NULL)
455 /* Copy command (i.e. up to space or tab) into cmd */
458 while (*p && (*p != ' ') && (*p != '\t'))
464 /* Skip over the whitespace in buf */
465 while (*p && ((*p == ' ') || (*p == '\t')))
470 /* Copy the argument into arg */
473 /* Should never happen, but check this anyway */
479 /* Make sure the command field is lower case */
484 *p = ijb_tolower(*p);
488 /* Save the argument for show-proxy-args */
489 savearg(cmd, arg, config);
492 switch( hash_string( cmd ) )
495 case hash_trustfile :
496 freez((char *)config->trustfile);
497 config->trustfile = strdup(arg);
500 case hash_trust_info_url :
501 enlist(config->trust_info, arg);
503 #endif /* def TRUST_FILES */
506 config->debug |= atoi(arg);
510 config->confdir = strdup(arg);
513 case hash_single_threaded :
514 config->multi_threaded = 0;
517 case hash_actions_file :
518 freez((char *)config->actions_file);
519 config->actions_file = strdup(arg);
523 freez((char *)config->logfile);
524 config->logfile = strdup(arg);
529 freez((char *)config->jarfile);
530 config->jarfile = strdup(arg);
532 #endif /* def JAR_FILES */
534 case hash_listen_address :
535 freez((char *)config->haddr);
536 config->haddr = strdup(arg);
539 case hash_forwardfile :
540 freez((char *)config->forwardfile);
541 config->forwardfile = strdup(arg);
546 freez((char *)config->aclfile);
547 config->aclfile = strdup(arg);
549 #endif /* def ACL_FILES */
552 case hash_re_filterfile :
553 freez((char *)config->re_filterfile);
554 config->re_filterfile = strdup(arg);
556 #endif /* def PCRS */
559 case hash_hide_console :
562 #endif /*def _WIN_CONSOLE*/
564 #ifndef SPLIT_PROXY_ARGS
565 case hash_suppress_blocklists :
568 config->suppress_message = strdup(arg);
572 /* There will be NO reference in proxy-args. */
573 config->suppress_message = NULL;
576 config->suppress_blocklists = 1;
578 #endif /* ndef SPLIT_PROXY_ARGS */
582 g_bToggleIJB = atoi(arg);
584 #endif /* def TOGGLE */
586 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
587 case hash_activity_animation :
588 g_bShowActivityAnimation = atoi(arg);
591 case hash_log_messages :
592 g_bLogMessages = atoi(arg);
595 case hash_log_highlight_messages :
596 g_bHighlightMessages = atoi(arg);
599 case hash_log_buffer_size :
600 g_bLimitBufferSize = atoi(arg);
603 case hash_log_max_lines :
604 g_nMaxBufferLines = atoi(arg);
607 case hash_log_font_name :
608 strcpy( g_szFontFaceName, arg );
611 case hash_log_font_size :
612 g_nFontSize = atoi(arg);
615 case hash_show_on_task_bar :
616 g_bShowOnTaskBar = atoi(arg);
619 case hash_close_button_minimizes :
620 g_bCloseHidesWindow = atoi(arg);
622 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
624 /* Warnings about unsupported features */
627 case hash_re_filterfile :
628 #endif /* ndef PCRS */
631 #endif /* ndef TOGGLE */
632 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
633 case hash_activity_animation :
634 case hash_log_messages :
635 case hash_log_highlight_messages :
636 case hash_log_buffer_size :
637 case hash_log_max_lines :
638 case hash_log_font_name :
639 case hash_log_font_size :
640 case hash_show_on_task_bar :
641 case hash_close_button_minimizes :
642 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
644 case hash_hide_console :
645 #endif /* ndef _WIN_CONSOLE */
648 #endif /* ndef JAR_FILES */
651 #endif /* ndef ACL_FILES */
652 #ifdef SPLIT_PROXY_ARGS
653 case hash_suppress_blocklists :
654 #endif /* def SPLIT_PROXY_ARGS */
655 log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd);
660 * I decided that I liked this better as a warning than an
661 * error. To change back to an error, just change log level
662 * to LOG_LEVEL_FATAL.
664 log_error(LOG_LEVEL_ERROR, "Unrecognized directive (%lulu) in "
665 "configuration file: \"%s\"", hash_string( cmd ), buf);
666 p = malloc( BUFSIZ );
669 sprintf( p, "<br>\nWARNING: unrecognized directive : %s<br><br>\n", buf );
670 config->proxy_args_invocation = strsav( config->proxy_args_invocation, p );
674 } /* end switch( hash_string(cmd) ) */
675 } /* end while ( read_config_line(...) ) */
679 init_error_log(Argv[0], config->logfile, config->debug);
681 if (config->actions_file)
683 add_loader(load_actions_file, config);
686 if (config->forwardfile)
688 add_loader(load_forwardfile, config);
694 add_loader(load_aclfile, config);
696 #endif /* def ACL_FILES */
699 if (config->re_filterfile)
701 add_loader(load_re_filterfile, config);
703 #endif /* def PCRS */
706 if (config->trustfile)
708 add_loader(load_trustfile, config);
713 if ( NULL != config->jarfile )
715 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
717 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
718 /* Never get here - LOG_LEVEL_FATAL causes program exit */
720 setbuf(config->jar, NULL);
722 #endif /* def JAR_FILES */
724 if ( NULL == config->haddr )
726 config->haddr = strdup( HADDR_DEFAULT );
729 if ( NULL != config->haddr )
731 if ((p = strchr(config->haddr, ':')))
736 config->hport = atoi(p);
740 if (config->hport <= 0)
743 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
744 /* Never get here - LOG_LEVEL_FATAL causes program exit */
746 if (*config->haddr == '\0')
748 config->haddr = NULL;
753 * Want to run all the loaders once now.
755 * Need to set up a fake csp, so they can get to the config.
757 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
758 fake_csp->config = config;
760 if (run_loader(fake_csp))
763 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
764 /* Never get here - LOG_LEVEL_FATAL causes program exit */
768 #ifndef SPLIT_PROXY_ARGS
769 if (!suppress_blocklists)
771 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
773 #endif /* ndef SPLIT_PROXY_ARGS */
775 /* FIXME: this is a kludge for win32 */
776 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
778 g_actions_file = config->actions_file;
779 g_forwardfile = config->forwardfile;
781 g_aclfile = config->aclfile;
782 #endif /* def ACL_FILES */
784 g_re_filterfile = config->re_filterfile;
787 g_trustfile = config->trustfile;
791 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
792 /* FIXME: end kludge */
795 config->need_bind = 1;
797 if (current_configfile)
799 struct configuration_spec * oldcfg = (struct configuration_spec *)
800 current_configfile->f;
802 * Check if config->haddr,hport == oldcfg->haddr,hport
804 * The following could be written more compactly as a single,
805 * (unreadably long) if statement.
807 config->need_bind = 0;
808 if (config->hport != oldcfg->hport)
810 config->need_bind = 1;
812 else if (config->haddr == NULL)
814 if (oldcfg->haddr != NULL)
816 config->need_bind = 1;
819 else if (oldcfg->haddr == NULL)
821 config->need_bind = 1;
823 else if (0 != strcmp(config->haddr, oldcfg->haddr))
825 config->need_bind = 1;
828 current_configfile->unloader = unload_configfile;
831 fs->next = files->next;
834 current_configfile = fs;