1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.10 2001/06/03 19:11:54 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 19:11:54 oes
39 * introduced confdir option
41 * Revision 1.10 2001/06/03 11:03:48 oes
48 * adapted to new enlist_unique arg format
52 * introduced confdir option
54 * filters.c filtrers.h
56 * extracted-CGI relevant stuff
64 * support for new cgi mechansim
68 * functions for new list type: "map"
69 * extended enlist_unique
76 * deleted const struct interceptors
84 * added struct http_response
85 * changes struct interceptors to struct cgi_dispatcher
86 * moved HTML stuff to cgi.h
95 * Revision 1.9 2001/06/01 20:06:24 jongfoster
96 * Removed support for "tinygif" option - moved to actions file.
98 * Revision 1.8 2001/05/31 21:27:13 jongfoster
99 * Removed many options from the config file and into the
100 * "actions" file: add_forwarded, suppress_vanilla_wafer,
101 * wafer, add_header, user_agent, referer, from
102 * Also globally replaced "permission" with "action".
104 * Revision 1.7 2001/05/29 09:50:24 jongfoster
105 * Unified blocklist/imagelist/permissionslist.
106 * File format is still under discussion, but the internal changes
109 * Also modified interceptor behaviour:
110 * - We now intercept all URLs beginning with one of the following
111 * prefixes (and *only* these prefixes):
113 * * http://ijbswa.sf.net/config/
114 * * http://ijbswa.sourceforge.net/config/
115 * - New interceptors "home page" - go to http://i.j.b/ to see it.
116 * - Internal changes so that intercepted and fast redirect pages
117 * are not replaced with an image.
118 * - Interceptors now have the option to send a binary page direct
119 * to the client. (i.e. ijb-send-banner uses this)
120 * - Implemented show-url-info interceptor. (Which is why I needed
121 * the above interceptors changes - a typical URL is
122 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
123 * The previous mechanism would not have intercepted that, and
124 * if it had been intercepted then it then it would have replaced
127 * Revision 1.6 2001/05/26 00:28:36 jongfoster
128 * Automatic reloading of config file.
129 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
130 * Most of the global variables have been moved to a new
131 * struct configuration_spec, accessed through csp->config->globalname
132 * Most of the globals remaining are used by the Win32 GUI.
134 * Revision 1.5 2001/05/25 22:34:30 jongfoster
137 * Revision 1.4 2001/05/22 18:46:04 oes
139 * - Enabled filtering banners by size rather than URL
140 * by adding patterns that replace all standard banner
141 * sizes with the "Junkbuster" gif to the re_filterfile
143 * - Enabled filtering WebBugs by providing a pattern
144 * which kills all 1x1 images
146 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
147 * which is selected by the (nonstandard and therefore
148 * capital) letter 'U' in the option string.
149 * It causes the quantifiers to be ungreedy by default.
150 * Appending a ? turns back to greedy (!).
152 * - Added a new interceptor ijb-send-banner, which
153 * sends back the "Junkbuster" gif. Without imagelist or
154 * MSIE detection support, or if tinygif = 1, or the
155 * URL isn't recognized as an imageurl, a lame HTML
156 * explanation is sent instead.
158 * - Added new feature, which permits blocking remote
159 * script redirects and firing back a local redirect
161 * The feature is conditionally compiled, i.e. it
162 * can be disabled with --disable-fast-redirects,
163 * plus it must be activated by a "fast-redirects"
164 * line in the config file, has its own log level
165 * and of course wants to be displayed by show-proxy-args
166 * Note: Boy, all the #ifdefs in 1001 locations and
167 * all the fumbling with configure.in and acconfig.h
168 * were *way* more work than the feature itself :-(
170 * - Because a generic redirect template was needed for
171 * this, tinygif = 3 now uses the same.
173 * - Moved GIFs, and other static HTTP response templates
178 * - Removed some >400 CRs again (Jon, you really worked
181 * Revision 1.3 2001/05/20 01:21:20 jongfoster
182 * Version 2.9.4 checkin.
183 * - Merged popupfile and cookiefile, and added control over PCRS
184 * filtering, in new "permissionsfile".
185 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
186 * file error you now get a message box (in the Win32 GUI) rather
187 * than the program exiting with no explanation.
188 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
190 * - Removed tabs from "config"
191 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
192 * - Bumped up version number.
194 * Revision 1.2 2001/05/17 23:01:01 oes
195 * - Cleaned CRLF's from the sources and related files
197 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
198 * Initial import of version 2.9.3 source tree
201 *********************************************************************/
207 #include <sys/types.h>
217 # include <sys/timeb.h>
218 # include <windows.h>
220 # include <process.h>
223 # endif /* def TOGGLE */
226 # ifndef _WIN_CONSOLE
228 # endif /* ndef _WIN_CONSOLE */
230 #else /* ifndef _WIN32 */
233 # include <sys/time.h>
234 # include <sys/wait.h>
235 # include <sys/stat.h>
245 #include "showargs.h"
247 #include "killpopup.h"
248 #include "miscutil.h"
250 #include "jbsockets.h"
253 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
256 * Fix a problem with Solaris. There should be no effect on other
258 * Solaris's isspace() is a macro which uses it's argument directly
259 * as an array index. Therefore we need to make sure that high-bit
260 * characters generate +ve values, and ideally we also want to make
261 * the argument match the declared parameter type of "int".
263 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
264 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
267 /* by haroon - indicates if ijb is enabled */
268 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
271 /* The filename of the configfile */
272 const char *configfile = NULL;
275 * The load_config function is now going to call `init_proxy_args',
276 * so it will need argc and argv. So we need to have these
277 * globally available.
280 const char **Argv = NULL;
282 static struct file_list *current_configfile = NULL;
286 * This takes the "cryptic" hash of each keyword and aliases them to
287 * something a little more readable. This also makes changing the
288 * hash values easier if they should change or the hash algorthm changes.
289 * Use the included "hash" program to find out what the hash will be
290 * for any string supplied on the command line. (Or just put it in the
291 * config file and read the number from the error message in the log).
295 #define hash_aclfile 1908516ul
296 #define hash_actions_file 3825730796ul /* FIXME "permissionsfile" */
297 #define hash_debug 78263ul
298 #define hash_confdir 1978389lu
299 #define hash_logdir 422889lu
300 #define hash_forwardfile 1268669141ul
301 #define hash_jarfile 2046641ul
302 #define hash_listen_address 1255650842ul
303 #define hash_logfile 2114766ul
304 #define hash_re_filterfile 3877522444ul
305 #define hash_single_threaded 4250084780ul
306 #define hash_suppress_blocklists 1948693308ul
307 #define hash_toggle 447966ul
308 #define hash_trust_info_url 449869467ul
309 #define hash_trustfile 56494766ul
311 #define hash_hide_console 2048809870ul
313 #define hash_activity_animation 1817904738ul
314 #define hash_close_button_minimizes 3651284693ul
315 #define hash_log_buffer_size 2918070425ul
316 #define hash_log_font_name 2866730124ul
317 #define hash_log_font_size 2866731014ul
318 #define hash_log_highlight_messages 4032101240ul
319 #define hash_log_max_lines 2868344173ul
320 #define hash_log_messages 2291744899ul
321 #define hash_show_on_task_bar 215410365ul
325 /*********************************************************************
327 * Function : unload_configfile
329 * Description : Free the config structure and all components.
332 * 1 : data: struct configuration_spec to unload
336 *********************************************************************/
337 void unload_configfile (void * data)
339 struct configuration_spec * config = (struct configuration_spec *)data;
342 if ( NULL != config->jar )
344 fclose( config->jar );
347 #endif /* def JAR_FILES */
349 freez((char *)config->haddr);
350 freez((char *)config->logfile);
352 freez((char *)config->actions_file);
353 freez((char *)config->forwardfile);
356 freez((char *)config->aclfile);
357 #endif /* def ACL_FILES */
360 freez((char *)config->jarfile);
361 #endif /* def JAR_FILES */
363 #ifndef SPLIT_PROXY_ARGS
364 freez((char *)config->suppress_message);
365 #endif /* ndef SPLIT_PROXY_ARGS */
368 freez((char *)config->re_filterfile);
369 #endif /* def PCRS */
374 /*********************************************************************
376 * Function : load_config
378 * Description : Load the config file and all parameters.
381 * 1 : csp = Client state (the config member will be
382 * filled in by this function).
384 * Returns : 0 => Ok, everything else is an error.
386 *********************************************************************/
387 struct configuration_spec * load_config(void)
391 FILE *configfp = NULL;
392 struct configuration_spec * config = NULL;
393 struct client_state * fake_csp;
395 struct file_list *fs;
397 if (!check_file_changed(current_configfile, configfile, &fs))
399 /* No need to load */
400 return ((struct configuration_spec *)current_configfile->f);
404 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
408 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
414 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
420 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
421 /* Never get here - LOG_LEVEL_FATAL causes program exit */
425 * This is backwards from how it's usually done.
426 * Following the usual pattern, "fs" would be stored in a member
427 * variable in "csp", and then we'd access "config" from "fs->f",
428 * using a cast. However, "config" is used so often that a
429 * cast each time would be very ugly, and the extra indirection
430 * would waste CPU cycles. Therefore we store "config" in
431 * "csp->config", and "fs" in "csp->config->config_file_list".
433 config->config_file_list = fs;
435 init_proxy_args(Argc, Argv, config);
441 config->multi_threaded = 1;
442 config->hport = HADDR_PORT;
444 if ((configfp = fopen(configfile, "r")) == NULL)
446 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
448 /* Never get here - LOG_LEVEL_FATAL causes program exit */
451 while (read_config_line(buf, sizeof(buf), configfp, fs) != NULL)
461 /* Copy command (i.e. up to space or tab) into cmd */
464 while (*p && (*p != ' ') && (*p != '\t'))
470 /* Skip over the whitespace in buf */
471 while (*p && ((*p == ' ') || (*p == '\t')))
476 /* Copy the argument into arg */
479 /* Should never happen, but check this anyway */
485 /* Make sure the command field is lower case */
490 *p = ijb_tolower(*p);
494 /* Save the argument for show-proxy-args */
495 savearg(cmd, arg, config);
498 switch( hash_string( cmd ) )
501 case hash_trustfile :
502 freez((char *)config->trustfile);
503 snprintf(arg2, BUFSIZ, "%s/%s", config->confdir, arg);
504 config->trustfile = strdup(arg2);
507 case hash_trust_info_url :
508 enlist(config->trust_info, arg);
510 #endif /* def TRUST_FILES */
513 config->debug |= atoi(arg);
517 config->confdir = strdup(arg);
521 config->logdir = strdup(arg);
524 case hash_single_threaded :
525 config->multi_threaded = 0;
528 case hash_actions_file :
529 freez((char *)config->actions_file);
530 snprintf(arg2, BUFSIZ, "%s/%s", config->confdir, arg);
531 config->actions_file = strdup(arg2);
535 freez((char *)config->logfile);
536 snprintf(arg2, BUFSIZ, "%s/%s", config->logdir, arg);
537 config->logfile = strdup(arg2);
542 freez((char *)config->jarfile);
543 snprintf(arg2, BUFSIZ, "%s/%s", config->logdir, arg);
544 config->jarfile = strdup(arg2);
546 #endif /* def JAR_FILES */
548 case hash_listen_address :
549 freez((char *)config->haddr);
550 config->haddr = strdup(arg);
553 case hash_forwardfile :
554 freez((char *)config->forwardfile);
555 snprintf(arg2, BUFSIZ, "%s/%s", config->confdir, arg);
556 config->forwardfile = strdup(arg2);
561 freez((char *)config->aclfile);
562 snprintf(arg2, BUFSIZ, "%s/%s", config->confdir, arg);
563 config->aclfile = strdup(arg2);
565 #endif /* def ACL_FILES */
568 case hash_re_filterfile :
569 freez((char *)config->re_filterfile);
570 config->re_filterfile = strdup(arg);
572 #endif /* def PCRS */
575 case hash_hide_console :
578 #endif /*def _WIN_CONSOLE*/
580 #ifndef SPLIT_PROXY_ARGS
581 case hash_suppress_blocklists :
584 config->suppress_message = strdup(arg);
588 /* There will be NO reference in proxy-args. */
589 config->suppress_message = NULL;
592 config->suppress_blocklists = 1;
594 #endif /* ndef SPLIT_PROXY_ARGS */
598 g_bToggleIJB = atoi(arg);
600 #endif /* def TOGGLE */
602 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
603 case hash_activity_animation :
604 g_bShowActivityAnimation = atoi(arg);
607 case hash_log_messages :
608 g_bLogMessages = atoi(arg);
611 case hash_log_highlight_messages :
612 g_bHighlightMessages = atoi(arg);
615 case hash_log_buffer_size :
616 g_bLimitBufferSize = atoi(arg);
619 case hash_log_max_lines :
620 g_nMaxBufferLines = atoi(arg);
623 case hash_log_font_name :
624 strcpy( g_szFontFaceName, arg );
627 case hash_log_font_size :
628 g_nFontSize = atoi(arg);
631 case hash_show_on_task_bar :
632 g_bShowOnTaskBar = atoi(arg);
635 case hash_close_button_minimizes :
636 g_bCloseHidesWindow = atoi(arg);
638 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
640 /* Warnings about unsupported features */
643 case hash_re_filterfile :
644 #endif /* ndef PCRS */
647 #endif /* ndef TOGGLE */
648 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
649 case hash_activity_animation :
650 case hash_log_messages :
651 case hash_log_highlight_messages :
652 case hash_log_buffer_size :
653 case hash_log_max_lines :
654 case hash_log_font_name :
655 case hash_log_font_size :
656 case hash_show_on_task_bar :
657 case hash_close_button_minimizes :
658 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
660 case hash_hide_console :
661 #endif /* ndef _WIN_CONSOLE */
664 #endif /* ndef JAR_FILES */
667 #endif /* ndef ACL_FILES */
668 #ifdef SPLIT_PROXY_ARGS
669 case hash_suppress_blocklists :
670 #endif /* def SPLIT_PROXY_ARGS */
671 log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd);
676 * I decided that I liked this better as a warning than an
677 * error. To change back to an error, just change log level
678 * to LOG_LEVEL_FATAL.
680 log_error(LOG_LEVEL_ERROR, "Unrecognized directive (%lulu) in "
681 "configuration file: \"%s\"", hash_string( cmd ), buf);
682 p = malloc( BUFSIZ );
685 sprintf( p, "<br>\nWARNING: unrecognized directive : %s<br><br>\n", buf );
686 config->proxy_args_invocation = strsav( config->proxy_args_invocation, p );
690 } /* end switch( hash_string(cmd) ) */
691 } /* end while ( read_config_line(...) ) */
695 init_error_log(Argv[0], config->logfile, config->debug);
697 if (config->actions_file)
699 add_loader(load_actions_file, config);
702 if (config->forwardfile)
704 add_loader(load_forwardfile, config);
710 add_loader(load_aclfile, config);
712 #endif /* def ACL_FILES */
715 if (config->re_filterfile)
717 add_loader(load_re_filterfile, config);
719 #endif /* def PCRS */
722 if (config->trustfile)
724 add_loader(load_trustfile, config);
729 if ( NULL != config->jarfile )
731 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
733 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
734 /* Never get here - LOG_LEVEL_FATAL causes program exit */
736 setbuf(config->jar, NULL);
738 #endif /* def JAR_FILES */
740 if ( NULL == config->haddr )
742 config->haddr = strdup( HADDR_DEFAULT );
745 if ( NULL != config->haddr )
747 if ((p = strchr(config->haddr, ':')))
752 config->hport = atoi(p);
756 if (config->hport <= 0)
759 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
760 /* Never get here - LOG_LEVEL_FATAL causes program exit */
762 if (*config->haddr == '\0')
764 config->haddr = NULL;
769 * Want to run all the loaders once now.
771 * Need to set up a fake csp, so they can get to the config.
773 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
774 fake_csp->config = config;
776 if (run_loader(fake_csp))
779 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
780 /* Never get here - LOG_LEVEL_FATAL causes program exit */
784 #ifndef SPLIT_PROXY_ARGS
785 if (!suppress_blocklists)
787 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
789 #endif /* ndef SPLIT_PROXY_ARGS */
791 /* FIXME: this is a kludge for win32 */
792 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
794 g_actions_file = config->actions_file;
795 g_forwardfile = config->forwardfile;
797 g_aclfile = config->aclfile;
798 #endif /* def ACL_FILES */
800 g_re_filterfile = config->re_filterfile;
803 g_trustfile = config->trustfile;
807 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
808 /* FIXME: end kludge */
811 config->need_bind = 1;
813 if (current_configfile)
815 struct configuration_spec * oldcfg = (struct configuration_spec *)
816 current_configfile->f;
818 * Check if config->haddr,hport == oldcfg->haddr,hport
820 * The following could be written more compactly as a single,
821 * (unreadably long) if statement.
823 config->need_bind = 0;
824 if (config->hport != oldcfg->hport)
826 config->need_bind = 1;
828 else if (config->haddr == NULL)
830 if (oldcfg->haddr != NULL)
832 config->need_bind = 1;
835 else if (oldcfg->haddr == NULL)
837 config->need_bind = 1;
839 else if (0 != strcmp(config->haddr, oldcfg->haddr))
841 config->need_bind = 1;
844 current_configfile->unloader = unload_configfile;
847 fs->next = files->next;
850 current_configfile = fs;