1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.8 2001/05/31 21:27:13 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.8 2001/05/31 21:27:13 jongfoster
39 * Removed many options from the config file and into the
40 * "actions" file: add_forwarded, suppress_vanilla_wafer,
41 * wafer, add_header, user_agent, referer, from
42 * Also globally replaced "permission" with "action".
44 * Revision 1.7 2001/05/29 09:50:24 jongfoster
45 * Unified blocklist/imagelist/permissionslist.
46 * File format is still under discussion, but the internal changes
49 * Also modified interceptor behaviour:
50 * - We now intercept all URLs beginning with one of the following
51 * prefixes (and *only* these prefixes):
53 * * http://ijbswa.sf.net/config/
54 * * http://ijbswa.sourceforge.net/config/
55 * - New interceptors "home page" - go to http://i.j.b/ to see it.
56 * - Internal changes so that intercepted and fast redirect pages
57 * are not replaced with an image.
58 * - Interceptors now have the option to send a binary page direct
59 * to the client. (i.e. ijb-send-banner uses this)
60 * - Implemented show-url-info interceptor. (Which is why I needed
61 * the above interceptors changes - a typical URL is
62 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
63 * The previous mechanism would not have intercepted that, and
64 * if it had been intercepted then it then it would have replaced
67 * Revision 1.6 2001/05/26 00:28:36 jongfoster
68 * Automatic reloading of config file.
69 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
70 * Most of the global variables have been moved to a new
71 * struct configuration_spec, accessed through csp->config->globalname
72 * Most of the globals remaining are used by the Win32 GUI.
74 * Revision 1.5 2001/05/25 22:34:30 jongfoster
77 * Revision 1.4 2001/05/22 18:46:04 oes
79 * - Enabled filtering banners by size rather than URL
80 * by adding patterns that replace all standard banner
81 * sizes with the "Junkbuster" gif to the re_filterfile
83 * - Enabled filtering WebBugs by providing a pattern
84 * which kills all 1x1 images
86 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
87 * which is selected by the (nonstandard and therefore
88 * capital) letter 'U' in the option string.
89 * It causes the quantifiers to be ungreedy by default.
90 * Appending a ? turns back to greedy (!).
92 * - Added a new interceptor ijb-send-banner, which
93 * sends back the "Junkbuster" gif. Without imagelist or
94 * MSIE detection support, or if tinygif = 1, or the
95 * URL isn't recognized as an imageurl, a lame HTML
96 * explanation is sent instead.
98 * - Added new feature, which permits blocking remote
99 * script redirects and firing back a local redirect
101 * The feature is conditionally compiled, i.e. it
102 * can be disabled with --disable-fast-redirects,
103 * plus it must be activated by a "fast-redirects"
104 * line in the config file, has its own log level
105 * and of course wants to be displayed by show-proxy-args
106 * Note: Boy, all the #ifdefs in 1001 locations and
107 * all the fumbling with configure.in and acconfig.h
108 * were *way* more work than the feature itself :-(
110 * - Because a generic redirect template was needed for
111 * this, tinygif = 3 now uses the same.
113 * - Moved GIFs, and other static HTTP response templates
118 * - Removed some >400 CRs again (Jon, you really worked
121 * Revision 1.3 2001/05/20 01:21:20 jongfoster
122 * Version 2.9.4 checkin.
123 * - Merged popupfile and cookiefile, and added control over PCRS
124 * filtering, in new "permissionsfile".
125 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
126 * file error you now get a message box (in the Win32 GUI) rather
127 * than the program exiting with no explanation.
128 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
130 * - Removed tabs from "config"
131 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
132 * - Bumped up version number.
134 * Revision 1.2 2001/05/17 23:01:01 oes
135 * - Cleaned CRLF's from the sources and related files
137 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
138 * Initial import of version 2.9.3 source tree
141 *********************************************************************/
147 #include <sys/types.h>
157 # include <sys/timeb.h>
158 # include <windows.h>
160 # include <process.h>
163 # endif /* def TOGGLE */
166 # ifndef _WIN_CONSOLE
168 # endif /* ndef _WIN_CONSOLE */
170 #else /* ifndef _WIN32 */
173 # include <sys/time.h>
174 # include <sys/wait.h>
175 # include <sys/stat.h>
185 #include "showargs.h"
187 #include "killpopup.h"
188 #include "miscutil.h"
190 #include "jbsockets.h"
193 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
196 * Fix a problem with Solaris. There should be no effect on other
198 * Solaris's isspace() is a macro which uses it's argument directly
199 * as an array index. Therefore we need to make sure that high-bit
200 * characters generate +ve values, and ideally we also want to make
201 * the argument match the declared parameter type of "int".
203 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
204 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
207 /* by haroon - indicates if ijb is enabled */
208 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
211 /* The filename of the configfile */
212 const char *configfile = NULL;
215 * The load_config function is now going to call `init_proxy_args',
216 * so it will need argc and argv. So we need to have these
217 * globally available.
220 const char **Argv = NULL;
222 static struct file_list *current_configfile = NULL;
226 * This takes the "cryptic" hash of each keyword and aliases them to
227 * something a little more readable. This also makes changing the
228 * hash values easier if they should change or the hash algorthm changes.
229 * Use the included "hash" program to find out what the hash will be
230 * for any string supplied on the command line. (Or just put it in the
231 * config file and read the number from the error message in the log).
235 #define hash_aclfile 1908516ul
236 #define hash_actions_file 3825730796ul /* FIXME "permissionsfile" */
237 #define hash_debug 78263ul
238 #define hash_forwardfile 1268669141ul
239 #define hash_jarfile 2046641ul
240 #define hash_listen_address 1255650842ul
241 #define hash_logfile 2114766ul
242 #define hash_re_filterfile 3877522444ul
243 #define hash_single_threaded 4250084780ul
244 #define hash_suppress_blocklists 1948693308ul
245 #define hash_toggle 447966ul
246 #define hash_trust_info_url 449869467ul
247 #define hash_trustfile 56494766ul
249 #define hash_hide_console 2048809870ul
251 #define hash_activity_animation 1817904738ul
252 #define hash_close_button_minimizes 3651284693ul
253 #define hash_log_buffer_size 2918070425ul
254 #define hash_log_font_name 2866730124ul
255 #define hash_log_font_size 2866731014ul
256 #define hash_log_highlight_messages 4032101240ul
257 #define hash_log_max_lines 2868344173ul
258 #define hash_log_messages 2291744899ul
259 #define hash_show_on_task_bar 215410365ul
263 /*********************************************************************
265 * Function : unload_configfile
267 * Description : Free the config structure and all components.
270 * 1 : data: struct configuration_spec to unload
274 *********************************************************************/
275 void unload_configfile (void * data)
277 struct configuration_spec * config = (struct configuration_spec *)data;
280 if ( NULL != config->jar )
282 fclose( config->jar );
285 #endif /* def JAR_FILES */
287 freez((char *)config->haddr);
288 freez((char *)config->logfile);
290 freez((char *)config->actions_file);
291 freez((char *)config->forwardfile);
294 freez((char *)config->aclfile);
295 #endif /* def ACL_FILES */
298 freez((char *)config->jarfile);
299 #endif /* def JAR_FILES */
301 #ifndef SPLIT_PROXY_ARGS
302 freez((char *)config->suppress_message);
303 #endif /* ndef SPLIT_PROXY_ARGS */
306 freez((char *)config->re_filterfile);
307 #endif /* def PCRS */
312 /*********************************************************************
314 * Function : load_config
316 * Description : Load the config file and all parameters.
319 * 1 : csp = Client state (the config member will be
320 * filled in by this function).
322 * Returns : 0 => Ok, everything else is an error.
324 *********************************************************************/
325 struct configuration_spec * load_config(void)
329 FILE *configfp = NULL;
330 struct configuration_spec * config = NULL;
331 struct client_state * fake_csp;
333 struct file_list *fs;
335 if (!check_file_changed(current_configfile, configfile, &fs))
337 /* No need to load */
338 return ((struct configuration_spec *)current_configfile->f);
342 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
346 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
352 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
358 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
359 /* Never get here - LOG_LEVEL_FATAL causes program exit */
363 * This is backwards from how it's usually done.
364 * Following the usual pattern, "fs" would be stored in a member
365 * variable in "csp", and then we'd access "config" from "fs->f",
366 * using a cast. However, "config" is used so often that a
367 * cast each time would be very ugly, and the extra indirection
368 * would waste CPU cycles. Therefore we store "config" in
369 * "csp->config", and "fs" in "csp->config->config_file_list".
371 config->config_file_list = fs;
373 init_proxy_args(Argc, Argv, config);
379 config->multi_threaded = 1;
380 config->hport = HADDR_PORT;
382 if ((configfp = fopen(configfile, "r")) == NULL)
384 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
386 /* Never get here - LOG_LEVEL_FATAL causes program exit */
389 while (read_config_line(buf, sizeof(buf), configfp, fs) != NULL)
397 /* Copy command (i.e. up to space or tab) into cmd */
400 while (*p && (*p != ' ') && (*p != '\t'))
406 /* Skip over the whitespace in buf */
407 while (*p && ((*p == ' ') || (*p == '\t')))
412 /* Copy the argument into arg */
415 /* Should never happen, but check this anyway */
421 /* Make sure the command field is lower case */
426 *p = ijb_tolower(*p);
430 /* Save the argument for show-proxy-args */
431 savearg(cmd, arg, config);
434 switch( hash_string( cmd ) )
437 case hash_trustfile :
438 freez((char *)config->trustfile);
439 config->trustfile = strdup(arg);
442 case hash_trust_info_url :
443 enlist(config->trust_info, arg);
445 #endif /* def TRUST_FILES */
448 config->debug |= atoi(arg);
451 case hash_single_threaded :
452 config->multi_threaded = 0;
455 case hash_actions_file :
456 freez((char *)config->actions_file);
457 config->actions_file = strdup(arg);
461 freez((char *)config->logfile);
462 config->logfile = strdup(arg);
467 freez((char *)config->jarfile);
468 config->jarfile = strdup(arg);
470 #endif /* def JAR_FILES */
472 case hash_listen_address :
473 freez((char *)config->haddr);
474 config->haddr = strdup(arg);
477 case hash_forwardfile :
478 freez((char *)config->forwardfile);
479 config->forwardfile = strdup(arg);
484 freez((char *)config->aclfile);
485 config->aclfile = strdup(arg);
487 #endif /* def ACL_FILES */
490 case hash_re_filterfile :
491 freez((char *)config->re_filterfile);
492 config->re_filterfile = strdup(arg);
494 #endif /* def PCRS */
497 case hash_hide_console :
500 #endif /*def _WIN_CONSOLE*/
502 #ifndef SPLIT_PROXY_ARGS
503 case hash_suppress_blocklists :
506 config->suppress_message = strdup(arg);
510 /* There will be NO reference in proxy-args. */
511 config->suppress_message = NULL;
514 config->suppress_blocklists = 1;
516 #endif /* ndef SPLIT_PROXY_ARGS */
520 g_bToggleIJB = atoi(arg);
522 #endif /* def TOGGLE */
524 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
525 case hash_activity_animation :
526 g_bShowActivityAnimation = atoi(arg);
529 case hash_log_messages :
530 g_bLogMessages = atoi(arg);
533 case hash_log_highlight_messages :
534 g_bHighlightMessages = atoi(arg);
537 case hash_log_buffer_size :
538 g_bLimitBufferSize = atoi(arg);
541 case hash_log_max_lines :
542 g_nMaxBufferLines = atoi(arg);
545 case hash_log_font_name :
546 strcpy( g_szFontFaceName, arg );
549 case hash_log_font_size :
550 g_nFontSize = atoi(arg);
553 case hash_show_on_task_bar :
554 g_bShowOnTaskBar = atoi(arg);
557 case hash_close_button_minimizes :
558 g_bCloseHidesWindow = atoi(arg);
560 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
562 /* Warnings about unsupported features */
565 case hash_re_filterfile :
566 #endif /* ndef PCRS */
569 #endif /* ndef TOGGLE */
570 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
571 case hash_activity_animation :
572 case hash_log_messages :
573 case hash_log_highlight_messages :
574 case hash_log_buffer_size :
575 case hash_log_max_lines :
576 case hash_log_font_name :
577 case hash_log_font_size :
578 case hash_show_on_task_bar :
579 case hash_close_button_minimizes :
580 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
582 case hash_hide_console :
583 #endif /* ndef _WIN_CONSOLE */
586 #endif /* ndef JAR_FILES */
589 #endif /* ndef ACL_FILES */
590 #ifdef SPLIT_PROXY_ARGS
591 case hash_suppress_blocklists :
592 #endif /* def SPLIT_PROXY_ARGS */
593 log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd);
598 * I decided that I liked this better as a warning than an
599 * error. To change back to an error, just change log level
600 * to LOG_LEVEL_FATAL.
602 log_error(LOG_LEVEL_ERROR, "Unrecognized directive (%lulu) in "
603 "configuration file: \"%s\"", hash_string( cmd ), buf);
604 p = malloc( BUFSIZ );
607 sprintf( p, "<br>\nWARNING: unrecognized directive : %s<br><br>\n", buf );
608 config->proxy_args_invocation = strsav( config->proxy_args_invocation, p );
612 } /* end switch( hash_string(cmd) ) */
613 } /* end while ( read_config_line(...) ) */
617 init_error_log(Argv[0], config->logfile, config->debug);
619 if (config->actions_file)
621 add_loader(load_actions_file, config);
624 if (config->forwardfile)
626 add_loader(load_forwardfile, config);
632 add_loader(load_aclfile, config);
634 #endif /* def ACL_FILES */
637 if (config->re_filterfile)
639 add_loader(load_re_filterfile, config);
641 #endif /* def PCRS */
644 if (config->trustfile)
646 add_loader(load_trustfile, config);
651 if ( NULL != config->jarfile )
653 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
655 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
656 /* Never get here - LOG_LEVEL_FATAL causes program exit */
658 setbuf(config->jar, NULL);
660 #endif /* def JAR_FILES */
662 if ( NULL == config->haddr )
664 config->haddr = strdup( HADDR_DEFAULT );
667 if ( NULL != config->haddr )
669 if ((p = strchr(config->haddr, ':')))
674 config->hport = atoi(p);
678 if (config->hport <= 0)
681 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
682 /* Never get here - LOG_LEVEL_FATAL causes program exit */
684 if (*config->haddr == '\0')
686 config->haddr = NULL;
691 * Want to run all the loaders once now.
693 * Need to set up a fake csp, so they can get to the config.
695 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
696 fake_csp->config = config;
698 if (run_loader(fake_csp))
701 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
702 /* Never get here - LOG_LEVEL_FATAL causes program exit */
706 end_proxy_args(config);
708 #ifndef SPLIT_PROXY_ARGS
709 if (!suppress_blocklists)
711 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
713 #endif /* ndef SPLIT_PROXY_ARGS */
715 /* FIXME: this is a kludge for win32 */
716 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
718 g_actions_file = config->actions_file;
719 g_forwardfile = config->forwardfile;
721 g_aclfile = config->aclfile;
722 #endif /* def ACL_FILES */
724 g_re_filterfile = config->re_filterfile;
727 g_trustfile = config->trustfile;
731 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
732 /* FIXME: end kludge */
735 config->need_bind = 1;
737 if (current_configfile)
739 struct configuration_spec * oldcfg = (struct configuration_spec *)
740 current_configfile->f;
742 * Check if config->haddr,hport == oldcfg->haddr,hport
744 * The following could be written more compactly as a single,
745 * (unreadably long) if statement.
747 config->need_bind = 0;
748 if (config->hport != oldcfg->hport)
750 config->need_bind = 1;
752 else if (config->haddr == NULL)
754 if (oldcfg->haddr != NULL)
756 config->need_bind = 1;
759 else if (oldcfg->haddr == NULL)
761 config->need_bind = 1;
763 else if (0 != strcmp(config->haddr, oldcfg->haddr))
765 config->need_bind = 1;
768 current_configfile->unloader = unload_configfile;
771 fs->next = files->next;
774 current_configfile = fs;