1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.6 2001/05/26 00:28:36 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.6 2001/05/26 00:28:36 jongfoster
39 * Automatic reloading of config file.
40 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
41 * Most of the global variables have been moved to a new
42 * struct configuration_spec, accessed through csp->config->globalname
43 * Most of the globals remaining are used by the Win32 GUI.
45 * Revision 1.5 2001/05/25 22:34:30 jongfoster
48 * Revision 1.4 2001/05/22 18:46:04 oes
50 * - Enabled filtering banners by size rather than URL
51 * by adding patterns that replace all standard banner
52 * sizes with the "Junkbuster" gif to the re_filterfile
54 * - Enabled filtering WebBugs by providing a pattern
55 * which kills all 1x1 images
57 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
58 * which is selected by the (nonstandard and therefore
59 * capital) letter 'U' in the option string.
60 * It causes the quantifiers to be ungreedy by default.
61 * Appending a ? turns back to greedy (!).
63 * - Added a new interceptor ijb-send-banner, which
64 * sends back the "Junkbuster" gif. Without imagelist or
65 * MSIE detection support, or if tinygif = 1, or the
66 * URL isn't recognized as an imageurl, a lame HTML
67 * explanation is sent instead.
69 * - Added new feature, which permits blocking remote
70 * script redirects and firing back a local redirect
72 * The feature is conditionally compiled, i.e. it
73 * can be disabled with --disable-fast-redirects,
74 * plus it must be activated by a "fast-redirects"
75 * line in the config file, has its own log level
76 * and of course wants to be displayed by show-proxy-args
77 * Note: Boy, all the #ifdefs in 1001 locations and
78 * all the fumbling with configure.in and acconfig.h
79 * were *way* more work than the feature itself :-(
81 * - Because a generic redirect template was needed for
82 * this, tinygif = 3 now uses the same.
84 * - Moved GIFs, and other static HTTP response templates
89 * - Removed some >400 CRs again (Jon, you really worked
92 * Revision 1.3 2001/05/20 01:21:20 jongfoster
93 * Version 2.9.4 checkin.
94 * - Merged popupfile and cookiefile, and added control over PCRS
95 * filtering, in new "permissionsfile".
96 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
97 * file error you now get a message box (in the Win32 GUI) rather
98 * than the program exiting with no explanation.
99 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
101 * - Removed tabs from "config"
102 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
103 * - Bumped up version number.
105 * Revision 1.2 2001/05/17 23:01:01 oes
106 * - Cleaned CRLF's from the sources and related files
108 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
109 * Initial import of version 2.9.3 source tree
112 *********************************************************************/
118 #include <sys/types.h>
128 # include <sys/timeb.h>
129 # include <windows.h>
131 # include <process.h>
134 # endif /* def TOGGLE */
137 # ifndef _WIN_CONSOLE
139 # endif /* ndef _WIN_CONSOLE */
141 #else /* ifndef _WIN32 */
144 # include <sys/time.h>
145 # include <sys/wait.h>
146 # include <sys/stat.h>
155 #include "showargs.h"
157 #include "killpopup.h"
158 #include "miscutil.h"
160 #include "jbsockets.h"
163 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
166 * Fix a problem with Solaris. There should be no effect on other
168 * Solaris's isspace() is a macro which uses it's argument directly
169 * as an array index. Therefore we need to make sure that high-bit
170 * characters generate +ve values, and ideally we also want to make
171 * the argument match the declared parameter type of "int".
173 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
174 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
176 static const char VANILLA_WAFER[] =
177 "NOTICE=TO_WHOM_IT_MAY_CONCERN_"
178 "Do_not_send_me_any_copyrighted_information_other_than_the_"
179 "document_that_I_am_requesting_or_any_of_its_necessary_components._"
180 "In_particular_do_not_send_me_any_cookies_that_"
181 "are_subject_to_a_claim_of_copyright_by_anybody._"
182 "Take_notice_that_I_refuse_to_be_bound_by_any_license_condition_"
183 "(copyright_or_otherwise)_applying_to_any_cookie._";
186 /* by haroon - indicates if ijb is enabled */
187 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
190 /* The filename of the configfile */
191 const char *configfile = NULL;
194 * The load_config function is now going to call `init_proxy_args',
195 * so it will need argc and argv. So we need to have these
196 * globally available.
199 const char **Argv = NULL;
201 static struct file_list *current_configfile = NULL;
205 * This takes the "cryptic" hash of each keyword and aliases them to
206 * something a little more readable. This also makes changing the
207 * hash values easier if they should change or the hash algorthm changes.
208 * Use the included "hash" program to find out what the hash will be
209 * for any string supplied on the command line. (Or just put it in the
210 * config file and read the number from the error message in the log).
213 #define hash_trustfile 56494766ul
214 #define hash_trust_info_url 449869467ul
215 #define hash_debug 78263ul
216 #define hash_tinygif 2227702ul
217 #define hash_add_forwarded_header 3191044770ul
218 #define hash_single_threaded 4250084780ul
219 #define hash_suppress_vanilla_wafer 3121233547ul
220 #define hash_wafer 89669ul
221 #define hash_add_header 237434619ul
222 #define hash_permissions_file 3825730796lu /* "permissionsfile" */
223 #define hash_logfile 2114766ul
224 #define hash_blockfile 48845391ul
225 #define hash_imagefile 51447891ul
226 #define hash_jarfile 2046641ul
227 #define hash_listen_address 1255650842ul
228 #define hash_forwardfile 1268669141ul
229 #define hash_aclfile 1908516ul
230 #define hash_re_filterfile 3877522444ul
231 #define hash_user_agent 283326691ul
232 #define hash_referrer 10883969ul
233 #define hash_referer 2176719ul
234 #define hash_from 16264ul
235 #define hash_fast_redirects 464873764lu
236 #define hash_hide_console 2048809870ul
237 #define hash_include_stats 2174146548ul
238 #define hash_suppress_blocklists 1948693308ul
239 #define hash_toggle 447966ul
241 #define hash_activity_animation 1817904738ul
242 #define hash_log_messages 2291744899ul
243 #define hash_log_highlight_messages 4032101240ul
244 #define hash_log_buffer_size 2918070425ul
245 #define hash_log_max_lines 2868344173ul
246 #define hash_log_font_name 2866730124ul
247 #define hash_log_font_size 2866731014ul
248 #define hash_show_on_task_bar 215410365ul
249 #define hash_close_button_minimizes 3651284693ul
253 /*********************************************************************
255 * Function : unload_configfile
257 * Description : Free the config structure and all components.
260 * 1 : data: struct configuration_spec to unload
264 *********************************************************************/
265 void unload_configfile (void * data)
267 struct configuration_spec * config = (struct configuration_spec *)data;
270 if ( NULL != config->jar )
272 fclose( config->jar );
275 #endif /* def JAR_FILES */
276 #ifdef IMAGE_BLOCKING
277 freez((char *)config->tinygifurl);
278 #endif /* def IMAGE_BLOCKING */
280 freez((char *)config->from);
281 freez((char *)config->haddr);
282 freez((char *)config->uagent);
283 freez((char *)config->referrer);
284 freez((char *)config->logfile);
286 freez((char *)config->permissions_file);
287 freez((char *)config->forwardfile);
290 freez((char *)config->aclfile);
291 #endif /* def ACL_FILES */
294 freez((char *)config->jarfile);
295 #endif /* def JAR_FILES */
297 #ifndef SPLIT_PROXY_ARGS
298 freez((char *)config->suppress_message);
299 #endif /* ndef SPLIT_PROXY_ARGS */
302 freez((char *)config->re_filterfile);
303 #endif /* def PCRS */
308 /*********************************************************************
310 * Function : load_config
312 * Description : Load the config file and all parameters.
315 * 1 : csp = Client state (the config member will be
316 * filled in by this function).
318 * Returns : 0 => Ok, everything else is an error.
320 *********************************************************************/
321 struct configuration_spec * load_config(void)
325 FILE *configfp = NULL;
326 struct configuration_spec * config = NULL;
327 int suppress_vanilla_wafer;
328 struct client_state * fake_csp;
330 struct file_list *fs;
332 if (!check_file_changed(current_configfile, configfile, &fs))
334 /* No need to load */
335 return ((struct configuration_spec *)current_configfile->f);
339 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
343 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
349 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
355 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
356 /* Never get here - LOG_LEVEL_FATAL causes program exit */
360 * This is backwards from how it's usually done.
361 * Following the usual pattern, "fs" would be stored in a member
362 * variable in "csp", and then we'd access "config" from "fs->f",
363 * using a cast. However, "config" is used so often that a
364 * cast each time would be very ugly, and the extra indirection
365 * would waste CPU cycles. Therefore we store "config" in
366 * "csp->config", and "fs" in "csp->config->config_file_list".
368 config->config_file_list = fs;
370 init_proxy_args(Argc, Argv, config);
376 config->multi_threaded = 1;
377 config->default_permissions = PERMIT_RE_FILTER | PERMIT_FAST_REDIRECTS;
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_add_forwarded_header :
485 config->add_forwarded = 1;
488 case hash_single_threaded :
489 config->multi_threaded = 0;
492 case hash_suppress_vanilla_wafer :
493 suppress_vanilla_wafer = 1;
497 enlist(config->wafer_list, arg);
500 case hash_add_header :
501 enlist(config->xtra_list, arg);
504 case hash_permissions_file :
505 freez((char *)config->permissions_file);
506 config->permissions_file = strdup(arg);
510 freez((char *)config->logfile);
511 config->logfile = strdup(arg);
516 freez((char *)config->jarfile);
517 config->jarfile = strdup(arg);
519 #endif /* def JAR_FILES */
521 case hash_listen_address :
522 freez((char *)config->haddr);
523 config->haddr = strdup(arg);
526 case hash_forwardfile :
527 freez((char *)config->forwardfile);
528 config->forwardfile = strdup(arg);
533 freez((char *)config->aclfile);
534 config->aclfile = strdup(arg);
536 #endif /* def ACL_FILES */
539 case hash_re_filterfile :
540 freez((char *)config->re_filterfile);
541 config->re_filterfile = strdup(arg);
543 #endif /* def PCRS */
545 case hash_user_agent :
546 freez((char *)config->uagent);
547 config->uagent = strdup(arg);
551 * Offer choice of correct spelling according to dictionary,
552 * or the misspelling used in the HTTP spec.
556 freez((char *)config->referrer);
557 config->referrer = strdup(arg);
561 freez((char *)config->from);
562 config->from = strdup(arg);
565 case hash_hide_console :
568 #endif /*def _WIN_CONSOLE*/
570 #ifndef SPLIT_PROXY_ARGS
571 case hash_suppress_blocklists :
574 config->suppress_message = strdup(arg);
578 /* There will be NO reference in proxy-args. */
579 config->suppress_message = NULL;
582 config->suppress_blocklists = 1;
584 #endif /* ndef SPLIT_PROXY_ARGS */
588 g_bToggleIJB = atoi(arg);
590 #endif /* def TOGGLE */
592 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
593 case hash_activity_animation :
594 g_bShowActivityAnimation = atoi(arg);
597 case hash_log_messages :
598 g_bLogMessages = atoi(arg);
601 case hash_log_highlight_messages :
602 g_bHighlightMessages = atoi(arg);
605 case hash_log_buffer_size :
606 g_bLimitBufferSize = atoi(arg);
609 case hash_log_max_lines :
610 g_nMaxBufferLines = atoi(arg);
613 case hash_log_font_name :
614 strcpy( g_szFontFaceName, arg );
617 case hash_log_font_size :
618 g_nFontSize = atoi(arg);
621 case hash_show_on_task_bar :
622 g_bShowOnTaskBar = atoi(arg);
625 case hash_close_button_minimizes :
626 g_bCloseHidesWindow = atoi(arg);
628 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
630 /* Warnings about unsupported features */
632 case hash_blockfile :
633 case hash_imagefile :
634 case hash_fast_redirects :
636 case hash_re_filterfile :
637 #endif /* ndef PCRS */
640 #endif /* ndef TOGGLE */
641 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
642 case hash_activity_animation :
643 case hash_log_messages :
644 case hash_log_highlight_messages :
645 case hash_log_buffer_size :
646 case hash_log_max_lines :
647 case hash_log_font_name :
648 case hash_log_font_size :
649 case hash_show_on_task_bar :
650 case hash_close_button_minimizes :
651 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
653 case hash_hide_console :
654 #endif /* ndef _WIN_CONSOLE */
655 #ifndef IMAGE_BLOCKING
657 #endif /* def IMAGE_BLOCKING */
660 #endif /* ndef JAR_FILES */
663 #endif /* ndef ACL_FILES */
664 #ifdef SPLIT_PROXY_ARGS
665 case hash_suppress_blocklists :
666 #endif /* def SPLIT_PROXY_ARGS */
667 log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd);
672 * I decided that I liked this better as a warning than an
673 * error. To change back to an error, just change log level
674 * to LOG_LEVEL_FATAL.
676 log_error(LOG_LEVEL_ERROR, "Unrecognized directive (%lulu) in "
677 "configuration file: \"%s\"", hash_string( cmd ), buf);
678 p = malloc( BUFSIZ );
681 sprintf( p, "<br>\nWARNING: unrecognized directive : %s<br><br>\n", buf );
682 config->proxy_args_invocation = strsav( config->proxy_args_invocation, p );
686 } /* end switch( hash_string(cmd) ) */
687 } /* end while ( read_config_line(...) ) */
691 init_error_log(Argv[0], config->logfile, config->debug);
693 if (config->permissions_file)
695 add_loader(load_permissions_file, config);
698 if (config->forwardfile)
700 add_loader(load_forwardfile, config);
706 add_loader(load_aclfile, config);
708 #endif /* def ACL_FILES */
711 if (config->re_filterfile)
713 add_loader(load_re_filterfile, config);
715 #endif /* def PCRS */
718 if (config->trustfile)
720 add_loader(load_trustfile, config);
725 if ( NULL != config->jarfile )
727 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
729 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
730 /* Never get here - LOG_LEVEL_FATAL causes program exit */
732 setbuf(config->jar, NULL);
734 #endif /* def JAR_FILES */
736 if ( NULL == config->haddr )
738 config->haddr = strdup( HADDR_DEFAULT );
741 if ( NULL != config->haddr )
743 if ((p = strchr(config->haddr, ':')))
748 config->hport = atoi(p);
752 if (config->hport <= 0)
755 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
756 /* Never get here - LOG_LEVEL_FATAL causes program exit */
758 if (*config->haddr == '\0')
760 config->haddr = NULL;
765 * Want to run all the loaders once now.
767 * Need to set up a fake csp, so they can get to the config.
769 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
770 fake_csp->config = config;
772 if (run_loader(fake_csp))
775 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
776 /* Never get here - LOG_LEVEL_FATAL causes program exit */
782 * If we're logging cookies in a cookie jar, and the user has not
783 * supplied any wafers, and the user has not told us to suppress the
784 * vanilla wafer, then send the vanilla wafer.
786 if ((config->jarfile != NULL)
787 && (config->wafer_list->next == NULL)
788 && (suppress_vanilla_wafer == 0))
790 enlist(config->wafer_list, VANILLA_WAFER);
792 #endif /* def JAR_FILES */
794 end_proxy_args(config);
796 #ifndef SPLIT_PROXY_ARGS
797 if (!suppress_blocklists)
799 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
801 #endif /* ndef SPLIT_PROXY_ARGS */
803 /* FIXME: this is a kludge for win32 */
804 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
806 g_permissions_file = config->permissions_file;
807 g_forwardfile = config->forwardfile;
809 g_aclfile = config->aclfile;
810 #endif /* def ACL_FILES */
812 g_re_filterfile = config->re_filterfile;
815 g_trustfile = config->trustfile;
819 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
820 /* FIXME: end kludge */
823 config->need_bind = 1;
825 if (current_configfile)
827 struct configuration_spec * oldcfg = (struct configuration_spec *)
828 current_configfile->f;
830 * Check if config->haddr,hport == oldcfg->haddr,hport
832 * The following could be written more compactly as a single,
833 * (unreadably long) if statement.
835 config->need_bind = 0;
836 if (config->hport != oldcfg->hport)
838 config->need_bind = 1;
840 else if (config->haddr == NULL)
842 if (oldcfg->haddr != NULL)
844 config->need_bind = 1;
847 else if (oldcfg->haddr == NULL)
849 config->need_bind = 1;
851 else if (0 != strcmp(config->haddr, oldcfg->haddr))
853 config->need_bind = 1;
856 current_configfile->unloader = unload_configfile;
859 fs->next = files->next;
862 current_configfile = fs;