1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.7 2001/05/29 09:50:24 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.7 2001/05/29 09:50:24 jongfoster
39 * Unified blocklist/imagelist/permissionslist.
40 * File format is still under discussion, but the internal changes
43 * Also modified interceptor behaviour:
44 * - We now intercept all URLs beginning with one of the following
45 * prefixes (and *only* these prefixes):
47 * * http://ijbswa.sf.net/config/
48 * * http://ijbswa.sourceforge.net/config/
49 * - New interceptors "home page" - go to http://i.j.b/ to see it.
50 * - Internal changes so that intercepted and fast redirect pages
51 * are not replaced with an image.
52 * - Interceptors now have the option to send a binary page direct
53 * to the client. (i.e. ijb-send-banner uses this)
54 * - Implemented show-url-info interceptor. (Which is why I needed
55 * the above interceptors changes - a typical URL is
56 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
57 * The previous mechanism would not have intercepted that, and
58 * if it had been intercepted then it then it would have replaced
61 * Revision 1.6 2001/05/26 00:28:36 jongfoster
62 * Automatic reloading of config file.
63 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
64 * Most of the global variables have been moved to a new
65 * struct configuration_spec, accessed through csp->config->globalname
66 * Most of the globals remaining are used by the Win32 GUI.
68 * Revision 1.5 2001/05/25 22:34:30 jongfoster
71 * Revision 1.4 2001/05/22 18:46:04 oes
73 * - Enabled filtering banners by size rather than URL
74 * by adding patterns that replace all standard banner
75 * sizes with the "Junkbuster" gif to the re_filterfile
77 * - Enabled filtering WebBugs by providing a pattern
78 * which kills all 1x1 images
80 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
81 * which is selected by the (nonstandard and therefore
82 * capital) letter 'U' in the option string.
83 * It causes the quantifiers to be ungreedy by default.
84 * Appending a ? turns back to greedy (!).
86 * - Added a new interceptor ijb-send-banner, which
87 * sends back the "Junkbuster" gif. Without imagelist or
88 * MSIE detection support, or if tinygif = 1, or the
89 * URL isn't recognized as an imageurl, a lame HTML
90 * explanation is sent instead.
92 * - Added new feature, which permits blocking remote
93 * script redirects and firing back a local redirect
95 * The feature is conditionally compiled, i.e. it
96 * can be disabled with --disable-fast-redirects,
97 * plus it must be activated by a "fast-redirects"
98 * line in the config file, has its own log level
99 * and of course wants to be displayed by show-proxy-args
100 * Note: Boy, all the #ifdefs in 1001 locations and
101 * all the fumbling with configure.in and acconfig.h
102 * were *way* more work than the feature itself :-(
104 * - Because a generic redirect template was needed for
105 * this, tinygif = 3 now uses the same.
107 * - Moved GIFs, and other static HTTP response templates
112 * - Removed some >400 CRs again (Jon, you really worked
115 * Revision 1.3 2001/05/20 01:21:20 jongfoster
116 * Version 2.9.4 checkin.
117 * - Merged popupfile and cookiefile, and added control over PCRS
118 * filtering, in new "permissionsfile".
119 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
120 * file error you now get a message box (in the Win32 GUI) rather
121 * than the program exiting with no explanation.
122 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
124 * - Removed tabs from "config"
125 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
126 * - Bumped up version number.
128 * Revision 1.2 2001/05/17 23:01:01 oes
129 * - Cleaned CRLF's from the sources and related files
131 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
132 * Initial import of version 2.9.3 source tree
135 *********************************************************************/
141 #include <sys/types.h>
151 # include <sys/timeb.h>
152 # include <windows.h>
154 # include <process.h>
157 # endif /* def TOGGLE */
160 # ifndef _WIN_CONSOLE
162 # endif /* ndef _WIN_CONSOLE */
164 #else /* ifndef _WIN32 */
167 # include <sys/time.h>
168 # include <sys/wait.h>
169 # include <sys/stat.h>
179 #include "showargs.h"
181 #include "killpopup.h"
182 #include "miscutil.h"
184 #include "jbsockets.h"
187 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
190 * Fix a problem with Solaris. There should be no effect on other
192 * Solaris's isspace() is a macro which uses it's argument directly
193 * as an array index. Therefore we need to make sure that high-bit
194 * characters generate +ve values, and ideally we also want to make
195 * the argument match the declared parameter type of "int".
197 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
198 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
201 /* by haroon - indicates if ijb is enabled */
202 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
205 /* The filename of the configfile */
206 const char *configfile = NULL;
209 * The load_config function is now going to call `init_proxy_args',
210 * so it will need argc and argv. So we need to have these
211 * globally available.
214 const char **Argv = NULL;
216 static struct file_list *current_configfile = NULL;
220 * This takes the "cryptic" hash of each keyword and aliases them to
221 * something a little more readable. This also makes changing the
222 * hash values easier if they should change or the hash algorthm changes.
223 * Use the included "hash" program to find out what the hash will be
224 * for any string supplied on the command line. (Or just put it in the
225 * config file and read the number from the error message in the log).
229 #define hash_aclfile 1908516ul
230 #define hash_actions_file 3825730796ul /* FIXME "permissionsfile" */
231 #define hash_debug 78263ul
232 #define hash_forwardfile 1268669141ul
233 #define hash_jarfile 2046641ul
234 #define hash_listen_address 1255650842ul
235 #define hash_logfile 2114766ul
236 #define hash_re_filterfile 3877522444ul
237 #define hash_single_threaded 4250084780ul
238 #define hash_suppress_blocklists 1948693308ul
239 #define hash_tinygif 2227702ul /* FIXME should be in actions file */
240 #define hash_toggle 447966ul
241 #define hash_trust_info_url 449869467ul
242 #define hash_trustfile 56494766ul
244 #define hash_hide_console 2048809870ul
246 #define hash_activity_animation 1817904738ul
247 #define hash_close_button_minimizes 3651284693ul
248 #define hash_log_buffer_size 2918070425ul
249 #define hash_log_font_name 2866730124ul
250 #define hash_log_font_size 2866731014ul
251 #define hash_log_highlight_messages 4032101240ul
252 #define hash_log_max_lines 2868344173ul
253 #define hash_log_messages 2291744899ul
254 #define hash_show_on_task_bar 215410365ul
258 /*********************************************************************
260 * Function : unload_configfile
262 * Description : Free the config structure and all components.
265 * 1 : data: struct configuration_spec to unload
269 *********************************************************************/
270 void unload_configfile (void * data)
272 struct configuration_spec * config = (struct configuration_spec *)data;
275 if ( NULL != config->jar )
277 fclose( config->jar );
280 #endif /* def JAR_FILES */
281 #ifdef IMAGE_BLOCKING
282 freez((char *)config->tinygifurl);
283 #endif /* def IMAGE_BLOCKING */
285 freez((char *)config->haddr);
286 freez((char *)config->logfile);
288 freez((char *)config->actions_file);
289 freez((char *)config->forwardfile);
292 freez((char *)config->aclfile);
293 #endif /* def ACL_FILES */
296 freez((char *)config->jarfile);
297 #endif /* def JAR_FILES */
299 #ifndef SPLIT_PROXY_ARGS
300 freez((char *)config->suppress_message);
301 #endif /* ndef SPLIT_PROXY_ARGS */
304 freez((char *)config->re_filterfile);
305 #endif /* def PCRS */
310 /*********************************************************************
312 * Function : load_config
314 * Description : Load the config file and all parameters.
317 * 1 : csp = Client state (the config member will be
318 * filled in by this function).
320 * Returns : 0 => Ok, everything else is an error.
322 *********************************************************************/
323 struct configuration_spec * load_config(void)
327 FILE *configfp = NULL;
328 struct configuration_spec * config = NULL;
329 struct client_state * fake_csp;
331 struct file_list *fs;
333 if (!check_file_changed(current_configfile, configfile, &fs))
335 /* No need to load */
336 return ((struct configuration_spec *)current_configfile->f);
340 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
344 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
350 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
356 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
357 /* Never get here - LOG_LEVEL_FATAL causes program exit */
361 * This is backwards from how it's usually done.
362 * Following the usual pattern, "fs" would be stored in a member
363 * variable in "csp", and then we'd access "config" from "fs->f",
364 * using a cast. However, "config" is used so often that a
365 * cast each time would be very ugly, and the extra indirection
366 * would waste CPU cycles. Therefore we store "config" in
367 * "csp->config", and "fs" in "csp->config->config_file_list".
369 config->config_file_list = fs;
371 init_proxy_args(Argc, Argv, config);
377 config->multi_threaded = 1;
378 config->hport = HADDR_PORT;
380 if ((configfp = fopen(configfile, "r")) == NULL)
382 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
384 /* Never get here - LOG_LEVEL_FATAL causes program exit */
387 while (read_config_line(buf, sizeof(buf), configfp, fs) != NULL)
395 /* Copy command (i.e. up to space or tab) into cmd */
398 while (*p && (*p != ' ') && (*p != '\t'))
404 /* Skip over the whitespace in buf */
405 while (*p && ((*p == ' ') || (*p == '\t')))
410 /* Copy the argument into arg */
413 /* Should never happen, but check this anyway */
419 /* Make sure the command field is lower case */
424 *p = ijb_tolower(*p);
428 /* Save the argument for show-proxy-args */
429 savearg(cmd, arg, config);
432 switch( hash_string( cmd ) )
435 case hash_trustfile :
436 freez((char *)config->trustfile);
437 config->trustfile = strdup(arg);
440 case hash_trust_info_url :
441 enlist(config->trust_info, arg);
443 #endif /* def TRUST_FILES */
446 config->debug |= atoi(arg);
449 #ifdef IMAGE_BLOCKING
451 freez((char *)config->tinygifurl);
452 config->tinygif = atoi(arg);
453 if(3 == config->tinygif)
456 while((*p >= '0') && (*p <= '9'))
460 while((*p == ' ') || (*p == '\t'))
466 q = malloc(strlen(p) + 5);
470 strcat(q, "\r\n\r\n");
471 config->tinygifurl = q;
475 if ((config->tinygif != 1) &&
476 (config->tinygif != 2) &&
477 ((config->tinygif != 3) || (config->tinygifurl==NULL)) )
479 log_error(LOG_LEVEL_ERROR, "tinygif setting invalid.");
482 #endif /* def IMAGE_BLOCKING */
484 case hash_single_threaded :
485 config->multi_threaded = 0;
488 case hash_actions_file :
489 freez((char *)config->actions_file);
490 config->actions_file = strdup(arg);
494 freez((char *)config->logfile);
495 config->logfile = strdup(arg);
500 freez((char *)config->jarfile);
501 config->jarfile = strdup(arg);
503 #endif /* def JAR_FILES */
505 case hash_listen_address :
506 freez((char *)config->haddr);
507 config->haddr = strdup(arg);
510 case hash_forwardfile :
511 freez((char *)config->forwardfile);
512 config->forwardfile = strdup(arg);
517 freez((char *)config->aclfile);
518 config->aclfile = strdup(arg);
520 #endif /* def ACL_FILES */
523 case hash_re_filterfile :
524 freez((char *)config->re_filterfile);
525 config->re_filterfile = strdup(arg);
527 #endif /* def PCRS */
530 case hash_hide_console :
533 #endif /*def _WIN_CONSOLE*/
535 #ifndef SPLIT_PROXY_ARGS
536 case hash_suppress_blocklists :
539 config->suppress_message = strdup(arg);
543 /* There will be NO reference in proxy-args. */
544 config->suppress_message = NULL;
547 config->suppress_blocklists = 1;
549 #endif /* ndef SPLIT_PROXY_ARGS */
553 g_bToggleIJB = atoi(arg);
555 #endif /* def TOGGLE */
557 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
558 case hash_activity_animation :
559 g_bShowActivityAnimation = atoi(arg);
562 case hash_log_messages :
563 g_bLogMessages = atoi(arg);
566 case hash_log_highlight_messages :
567 g_bHighlightMessages = atoi(arg);
570 case hash_log_buffer_size :
571 g_bLimitBufferSize = atoi(arg);
574 case hash_log_max_lines :
575 g_nMaxBufferLines = atoi(arg);
578 case hash_log_font_name :
579 strcpy( g_szFontFaceName, arg );
582 case hash_log_font_size :
583 g_nFontSize = atoi(arg);
586 case hash_show_on_task_bar :
587 g_bShowOnTaskBar = atoi(arg);
590 case hash_close_button_minimizes :
591 g_bCloseHidesWindow = atoi(arg);
593 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
595 /* Warnings about unsupported features */
598 case hash_re_filterfile :
599 #endif /* ndef PCRS */
602 #endif /* ndef TOGGLE */
603 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
604 case hash_activity_animation :
605 case hash_log_messages :
606 case hash_log_highlight_messages :
607 case hash_log_buffer_size :
608 case hash_log_max_lines :
609 case hash_log_font_name :
610 case hash_log_font_size :
611 case hash_show_on_task_bar :
612 case hash_close_button_minimizes :
613 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
615 case hash_hide_console :
616 #endif /* ndef _WIN_CONSOLE */
617 #ifndef IMAGE_BLOCKING
619 #endif /* def IMAGE_BLOCKING */
622 #endif /* ndef JAR_FILES */
625 #endif /* ndef ACL_FILES */
626 #ifdef SPLIT_PROXY_ARGS
627 case hash_suppress_blocklists :
628 #endif /* def SPLIT_PROXY_ARGS */
629 log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd);
634 * I decided that I liked this better as a warning than an
635 * error. To change back to an error, just change log level
636 * to LOG_LEVEL_FATAL.
638 log_error(LOG_LEVEL_ERROR, "Unrecognized directive (%lulu) in "
639 "configuration file: \"%s\"", hash_string( cmd ), buf);
640 p = malloc( BUFSIZ );
643 sprintf( p, "<br>\nWARNING: unrecognized directive : %s<br><br>\n", buf );
644 config->proxy_args_invocation = strsav( config->proxy_args_invocation, p );
648 } /* end switch( hash_string(cmd) ) */
649 } /* end while ( read_config_line(...) ) */
653 init_error_log(Argv[0], config->logfile, config->debug);
655 if (config->actions_file)
657 add_loader(load_actions_file, config);
660 if (config->forwardfile)
662 add_loader(load_forwardfile, config);
668 add_loader(load_aclfile, config);
670 #endif /* def ACL_FILES */
673 if (config->re_filterfile)
675 add_loader(load_re_filterfile, config);
677 #endif /* def PCRS */
680 if (config->trustfile)
682 add_loader(load_trustfile, config);
687 if ( NULL != config->jarfile )
689 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
691 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
692 /* Never get here - LOG_LEVEL_FATAL causes program exit */
694 setbuf(config->jar, NULL);
696 #endif /* def JAR_FILES */
698 if ( NULL == config->haddr )
700 config->haddr = strdup( HADDR_DEFAULT );
703 if ( NULL != config->haddr )
705 if ((p = strchr(config->haddr, ':')))
710 config->hport = atoi(p);
714 if (config->hport <= 0)
717 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
718 /* Never get here - LOG_LEVEL_FATAL causes program exit */
720 if (*config->haddr == '\0')
722 config->haddr = NULL;
727 * Want to run all the loaders once now.
729 * Need to set up a fake csp, so they can get to the config.
731 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
732 fake_csp->config = config;
734 if (run_loader(fake_csp))
737 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
738 /* Never get here - LOG_LEVEL_FATAL causes program exit */
742 end_proxy_args(config);
744 #ifndef SPLIT_PROXY_ARGS
745 if (!suppress_blocklists)
747 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
749 #endif /* ndef SPLIT_PROXY_ARGS */
751 /* FIXME: this is a kludge for win32 */
752 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
754 g_actions_file = config->actions_file;
755 g_forwardfile = config->forwardfile;
757 g_aclfile = config->aclfile;
758 #endif /* def ACL_FILES */
760 g_re_filterfile = config->re_filterfile;
763 g_trustfile = config->trustfile;
767 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
768 /* FIXME: end kludge */
771 config->need_bind = 1;
773 if (current_configfile)
775 struct configuration_spec * oldcfg = (struct configuration_spec *)
776 current_configfile->f;
778 * Check if config->haddr,hport == oldcfg->haddr,hport
780 * The following could be written more compactly as a single,
781 * (unreadably long) if statement.
783 config->need_bind = 0;
784 if (config->hport != oldcfg->hport)
786 config->need_bind = 1;
788 else if (config->haddr == NULL)
790 if (oldcfg->haddr != NULL)
792 config->need_bind = 1;
795 else if (oldcfg->haddr == NULL)
797 config->need_bind = 1;
799 else if (0 != strcmp(config->haddr, oldcfg->haddr))
801 config->need_bind = 1;
804 current_configfile->unloader = unload_configfile;
807 fs->next = files->next;
810 current_configfile = fs;