1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.5 2001/05/25 22:34:30 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.5 2001/05/25 22:34:30 jongfoster
41 * Revision 1.4 2001/05/22 18:46:04 oes
43 * - Enabled filtering banners by size rather than URL
44 * by adding patterns that replace all standard banner
45 * sizes with the "Junkbuster" gif to the re_filterfile
47 * - Enabled filtering WebBugs by providing a pattern
48 * which kills all 1x1 images
50 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
51 * which is selected by the (nonstandard and therefore
52 * capital) letter 'U' in the option string.
53 * It causes the quantifiers to be ungreedy by default.
54 * Appending a ? turns back to greedy (!).
56 * - Added a new interceptor ijb-send-banner, which
57 * sends back the "Junkbuster" gif. Without imagelist or
58 * MSIE detection support, or if tinygif = 1, or the
59 * URL isn't recognized as an imageurl, a lame HTML
60 * explanation is sent instead.
62 * - Added new feature, which permits blocking remote
63 * script redirects and firing back a local redirect
65 * The feature is conditionally compiled, i.e. it
66 * can be disabled with --disable-fast-redirects,
67 * plus it must be activated by a "fast-redirects"
68 * line in the config file, has its own log level
69 * and of course wants to be displayed by show-proxy-args
70 * Note: Boy, all the #ifdefs in 1001 locations and
71 * all the fumbling with configure.in and acconfig.h
72 * were *way* more work than the feature itself :-(
74 * - Because a generic redirect template was needed for
75 * this, tinygif = 3 now uses the same.
77 * - Moved GIFs, and other static HTTP response templates
82 * - Removed some >400 CRs again (Jon, you really worked
85 * Revision 1.3 2001/05/20 01:21:20 jongfoster
86 * Version 2.9.4 checkin.
87 * - Merged popupfile and cookiefile, and added control over PCRS
88 * filtering, in new "permissionsfile".
89 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
90 * file error you now get a message box (in the Win32 GUI) rather
91 * than the program exiting with no explanation.
92 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
94 * - Removed tabs from "config"
95 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
96 * - Bumped up version number.
98 * Revision 1.2 2001/05/17 23:01:01 oes
99 * - Cleaned CRLF's from the sources and related files
101 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
102 * Initial import of version 2.9.3 source tree
105 *********************************************************************/
111 #include <sys/types.h>
121 # include <sys/timeb.h>
122 # include <windows.h>
124 # include <process.h>
127 # endif /* def TOGGLE */
130 # ifndef _WIN_CONSOLE
132 # endif /* ndef _WIN_CONSOLE */
134 #else /* ifndef _WIN32 */
137 # include <sys/time.h>
138 # include <sys/wait.h>
139 # include <sys/stat.h>
148 #include "showargs.h"
150 #include "killpopup.h"
151 #include "miscutil.h"
153 #include "jbsockets.h"
156 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
159 * Fix a problem with Solaris. There should be no effect on other
161 * Solaris's isspace() is a macro which uses it's argument directly
162 * as an array index. Therefore we need to make sure that high-bit
163 * characters generate +ve values, and ideally we also want to make
164 * the argument match the declared parameter type of "int".
166 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
167 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
169 static const char VANILLA_WAFER[] =
170 "NOTICE=TO_WHOM_IT_MAY_CONCERN_"
171 "Do_not_send_me_any_copyrighted_information_other_than_the_"
172 "document_that_I_am_requesting_or_any_of_its_necessary_components._"
173 "In_particular_do_not_send_me_any_cookies_that_"
174 "are_subject_to_a_claim_of_copyright_by_anybody._"
175 "Take_notice_that_I_refuse_to_be_bound_by_any_license_condition_"
176 "(copyright_or_otherwise)_applying_to_any_cookie._";
179 /* by haroon - indicates if ijb is enabled */
180 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
183 /* The filename of the configfile */
184 const char *configfile = NULL;
187 * The load_config function is now going to call `init_proxy_args',
188 * so it will need argc and argv. So we need to have these
189 * globally available.
192 const char **Argv = NULL;
194 static struct file_list *current_configfile = NULL;
198 * This takes the "cryptic" hash of each keyword and aliases them to
199 * something a little more readable. This also makes changing the
200 * hash values easier if they should change or the hash algorthm changes.
201 * Use the included "hash" program to find out what the hash will be
202 * for any string supplied on the command line. (Or just put it in the
203 * config file and read the number from the error message in the log).
206 #define hash_trustfile 56494766ul
207 #define hash_trust_info_url 449869467ul
208 #define hash_debug 78263ul
209 #define hash_tinygif 2227702ul
210 #define hash_add_forwarded_header 3191044770ul
211 #define hash_single_threaded 4250084780ul
212 #define hash_suppress_vanilla_wafer 3121233547ul
213 #define hash_wafer 89669ul
214 #define hash_add_header 237434619ul
215 #define hash_permissions_file 3825730796lu /* "permissionsfile" */
216 #define hash_logfile 2114766ul
217 #define hash_blockfile 48845391ul
218 #define hash_imagefile 51447891ul
219 #define hash_jarfile 2046641ul
220 #define hash_listen_address 1255650842ul
221 #define hash_forwardfile 1268669141ul
222 #define hash_aclfile 1908516ul
223 #define hash_re_filterfile 3877522444ul
224 #define hash_user_agent 283326691ul
225 #define hash_referrer 10883969ul
226 #define hash_referer 2176719ul
227 #define hash_from 16264ul
228 #define hash_fast_redirects 464873764lu
229 #define hash_hide_console 2048809870ul
230 #define hash_include_stats 2174146548ul
231 #define hash_suppress_blocklists 1948693308ul
232 #define hash_toggle 447966ul
234 #define hash_activity_animation 1817904738ul
235 #define hash_log_messages 2291744899ul
236 #define hash_log_highlight_messages 4032101240ul
237 #define hash_log_buffer_size 2918070425ul
238 #define hash_log_max_lines 2868344173ul
239 #define hash_log_font_name 2866730124ul
240 #define hash_log_font_size 2866731014ul
241 #define hash_show_on_task_bar 215410365ul
242 #define hash_close_button_minimizes 3651284693ul
246 /*********************************************************************
248 * Function : unload_configfile
250 * Description : Free the config structure and all components.
253 * 1 : data: struct configuration_spec to unload
257 *********************************************************************/
258 void unload_configfile (void * data)
260 struct configuration_spec * config = (struct configuration_spec *)data;
263 if ( NULL != config->jar )
265 fclose( config->jar );
268 #endif /* def JAR_FILES */
269 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
270 freez((char *)config->tinygifurl);
271 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
273 freez((char *)config->from);
274 freez((char *)config->haddr);
275 freez((char *)config->uagent);
276 freez((char *)config->referrer);
277 freez((char *)config->logfile);
279 freez((char *)config->blockfile);
280 freez((char *)config->permissions_file);
281 freez((char *)config->forwardfile);
284 freez((char *)config->aclfile);
285 #endif /* def ACL_FILES */
287 #ifdef USE_IMAGE_LIST
288 freez((char *)config->imagefile);
289 #endif /* def USE_IMAGE_LIST */
292 freez((char *)config->jarfile);
293 #endif /* def JAR_FILES */
295 #ifndef SPLIT_PROXY_ARGS
296 freez((char *)config->suppress_message);
297 #endif /* ndef SPLIT_PROXY_ARGS */
300 freez((char *)config->re_filterfile);
301 #endif /* def PCRS */
306 /*********************************************************************
308 * Function : load_config
310 * Description : Load the config file and all parameters.
313 * 1 : csp = Client state (the config member will be
314 * filled in by this function).
316 * Returns : 0 => Ok, everything else is an error.
318 *********************************************************************/
319 struct configuration_spec * load_config(void)
323 FILE *configfp = NULL;
324 struct configuration_spec * config = NULL;
325 int suppress_vanilla_wafer;
326 struct client_state * fake_csp;
328 struct file_list *fs;
330 if (!check_file_changed(current_configfile, configfile, &fs))
332 /* No need to load */
333 return ((struct configuration_spec *)current_configfile->f);
337 log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s': %E",
341 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
347 fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
353 log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
354 /* Never get here - LOG_LEVEL_FATAL causes program exit */
358 * This is backwards from how it's usually done.
359 * Following the usual pattern, "fs" would be stored in a member
360 * variable in "csp", and then we'd access "config" from "fs->f",
361 * using a cast. However, "config" is used so often that a
362 * cast each time would be very ugly, and the extra indirection
363 * would waste CPU cycles. Therefore we store "config" in
364 * "csp->config", and "fs" in "csp->config->config_file_list".
366 config->config_file_list = fs;
368 init_proxy_args(Argc, Argv, config);
374 config->multi_threaded = 1;
375 config->default_permissions = PERMIT_RE_FILTER;
376 config->hport = HADDR_PORT;
378 if ((configfp = fopen(configfile, "r")) == NULL)
380 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
382 /* Never get here - LOG_LEVEL_FATAL causes program exit */
385 while (read_config_line(buf, sizeof(buf), configfp, fs) != NULL)
393 /* Copy command (i.e. up to space or tab) into cmd */
396 while (*p && (*p != ' ') && (*p != '\t'))
402 /* Skip over the whitespace in buf */
403 while (*p && ((*p == ' ') || (*p == '\t')))
408 /* Copy the argument into arg */
411 /* Should never happen, but check this anyway */
417 /* Make sure the command field is lower case */
422 *p = ijb_tolower(*p);
426 /* Save the argument for show-proxy-args */
427 savearg(cmd, arg, config);
430 switch( hash_string( cmd ) )
433 case hash_trustfile :
434 freez((char *)config->trustfile);
435 config->trustfile = strdup(arg);
438 case hash_trust_info_url :
439 enlist(config->trust_info, arg);
441 #endif /* def TRUST_FILES */
444 config->debug |= atoi(arg);
447 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
449 freez((char *)config->tinygifurl);
450 config->tinygif = atoi(arg);
451 if(3 == config->tinygif)
454 while((*p >= '0') && (*p <= '9'))
458 while((*p == ' ') || (*p == '\t'))
464 q = malloc(strlen(p) + 5);
468 strcat(q, "\r\n\r\n");
469 config->tinygifurl = q;
473 if ((config->tinygif != 1) &&
474 (config->tinygif != 2) &&
475 ((config->tinygif != 3) || (config->tinygifurl==NULL)) )
477 log_error(LOG_LEVEL_ERROR, "tinygif setting invalid.");
480 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
482 case hash_add_forwarded_header :
483 config->add_forwarded = 1;
486 case hash_single_threaded :
487 config->multi_threaded = 0;
490 case hash_suppress_vanilla_wafer :
491 suppress_vanilla_wafer = 1;
495 enlist(config->wafer_list, arg);
498 case hash_add_header :
499 enlist(config->xtra_list, arg);
502 case hash_permissions_file :
503 freez((char *)config->permissions_file);
504 config->permissions_file = strdup(arg);
508 freez((char *)config->logfile);
509 config->logfile = strdup(arg);
512 case hash_blockfile :
513 freez((char *)config->blockfile);
514 config->blockfile = strdup(arg);
517 #ifdef USE_IMAGE_LIST
518 case hash_imagefile :
519 freez((char *)config->imagefile);
520 config->imagefile = strdup(arg);
522 #endif /* def USE_IMAGE_LIST */
526 freez((char *)config->jarfile);
527 config->jarfile = strdup(arg);
529 #endif /* def JAR_FILES */
531 case hash_listen_address :
532 freez((char *)config->haddr);
533 config->haddr = strdup(arg);
536 case hash_forwardfile :
537 freez((char *)config->forwardfile);
538 config->forwardfile = strdup(arg);
543 freez((char *)config->aclfile);
544 config->aclfile = strdup(arg);
546 #endif /* def ACL_FILES */
549 case hash_re_filterfile :
550 freez((char *)config->re_filterfile);
551 config->re_filterfile = strdup(arg);
553 #endif /* def PCRS */
555 case hash_user_agent :
556 freez((char *)config->uagent);
557 config->uagent = strdup(arg);
561 * Offer choice of correct spelling according to dictionary,
562 * or the misspelling used in the HTTP spec.
566 freez((char *)config->referrer);
567 config->referrer = strdup(arg);
571 freez((char *)config->from);
572 config->from = strdup(arg);
575 #ifdef FAST_REDIRECTS
576 case hash_fast_redirects :
577 config->fast_redirects = 1;
579 #endif /* def FAST_REDIRECTS */
582 case hash_hide_console :
585 #endif /*def _WIN_CONSOLE*/
587 #ifndef SPLIT_PROXY_ARGS
588 case hash_suppress_blocklists :
591 config->suppress_message = strdup(arg);
595 /* There will be NO reference in proxy-args. */
596 config->suppress_message = NULL;
599 config->suppress_blocklists = 1;
601 #endif /* ndef SPLIT_PROXY_ARGS */
605 g_bToggleIJB = atoi(arg);
607 #endif /* def TOGGLE */
609 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
610 case hash_activity_animation :
611 g_bShowActivityAnimation = atoi(arg);
614 case hash_log_messages :
615 g_bLogMessages = atoi(arg);
618 case hash_log_highlight_messages :
619 g_bHighlightMessages = atoi(arg);
622 case hash_log_buffer_size :
623 g_bLimitBufferSize = atoi(arg);
626 case hash_log_max_lines :
627 g_nMaxBufferLines = atoi(arg);
630 case hash_log_font_name :
631 strcpy( g_szFontFaceName, arg );
634 case hash_log_font_size :
635 g_nFontSize = atoi(arg);
638 case hash_show_on_task_bar :
639 g_bShowOnTaskBar = atoi(arg);
642 case hash_close_button_minimizes :
643 g_bCloseHidesWindow = atoi(arg);
645 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
647 /* Warnings about unsupported features */
649 #ifndef USE_IMAGE_LIST
650 case hash_imagefile :
651 #endif /* ndef USE_IMAGE_LIST */
653 case hash_re_filterfile :
654 #endif /* ndef PCRS */
657 #endif /* ndef TOGGLE */
658 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
659 case hash_activity_animation :
660 case hash_log_messages :
661 case hash_log_highlight_messages :
662 case hash_log_buffer_size :
663 case hash_log_max_lines :
664 case hash_log_font_name :
665 case hash_log_font_size :
666 case hash_show_on_task_bar :
667 case hash_close_button_minimizes :
668 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
670 case hash_hide_console :
671 #endif /* ndef _WIN_CONSOLE */
672 #if !defined(DETECT_MSIE_IMAGES) && !defined(USE_IMAGE_LIST)
674 #endif /* !defined(DETECT_MSIE_IMAGES) && !defined(USE_IMAGE_LIST) */
677 #endif /* ndef JAR_FILES */
680 #endif /* ndef ACL_FILES */
681 #ifndef FAST_REDIRECTS
682 case hash_fast_redirects :
683 #endif /* ndef FAST_REDIRECTS */
684 #ifdef SPLIT_PROXY_ARGS
685 case hash_suppress_blocklists :
686 #endif /* def SPLIT_PROXY_ARGS */
687 log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd);
692 * I decided that I liked this better as a warning than an
693 * error. To change back to an error, just change log level
694 * to LOG_LEVEL_FATAL.
696 log_error(LOG_LEVEL_ERROR, "Unrecognized directive (%lulu) in "
697 "configuration file: \"%s\"", hash_string( cmd ), buf);
698 p = malloc( BUFSIZ );
701 sprintf( p, "<br>\nWARNING: unrecognized directive : %s<br><br>\n", buf );
702 config->proxy_args->invocation = strsav( config->proxy_args->invocation, p );
706 } /* end switch( hash_string(cmd) ) */
707 } /* end while ( read_config_line(...) ) */
711 init_error_log(Argv[0], config->logfile, config->debug);
713 if (config->permissions_file)
715 add_loader(load_permissions_file, config);
718 if (config->blockfile)
720 add_loader(load_blockfile, config);
723 #ifdef USE_IMAGE_LIST
724 if (config->imagefile)
726 add_loader(load_imagefile, config);
728 #endif /* def USE_IMAGE_LIST */
730 if (config->forwardfile)
732 add_loader(load_forwardfile, config);
738 add_loader(load_aclfile, config);
740 #endif /* def ACL_FILES */
743 if (config->re_filterfile)
745 add_loader(load_re_filterfile, config);
747 #endif /* def PCRS */
750 if (config->trustfile)
\r
752 add_loader(load_trustfile, config);
\r
757 if ( NULL != config->jarfile )
759 if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
761 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
762 /* Never get here - LOG_LEVEL_FATAL causes program exit */
764 setbuf(config->jar, NULL);
766 #endif /* def JAR_FILES */
768 if ( NULL == config->haddr )
770 config->haddr = strdup( HADDR_DEFAULT );
773 if ( NULL != config->haddr )
775 if ((p = strchr(config->haddr, ':')))
780 config->hport = atoi(p);
784 if (config->hport <= 0)
787 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
788 /* Never get here - LOG_LEVEL_FATAL causes program exit */
790 if (*config->haddr == '\0')
792 config->haddr = NULL;
797 * Want to run all the loaders once now.
799 * Need to set up a fake csp, so they can get to the config.
801 fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
802 fake_csp->config = config;
804 if (run_loader(fake_csp))
807 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
808 /* Never get here - LOG_LEVEL_FATAL causes program exit */
814 * If we're logging cookies in a cookie jar, and the user has not
815 * supplied any wafers, and the user has not told us to suppress the
816 * vanilla wafer, then send the vanilla wafer.
818 if ((config->jarfile != NULL)
819 && (config->wafer_list->next == NULL)
820 && (suppress_vanilla_wafer == 0))
822 enlist(config->wafer_list, VANILLA_WAFER);
824 #endif /* def JAR_FILES */
826 end_proxy_args(config);
828 #ifndef SPLIT_PROXY_ARGS
829 if (!suppress_blocklists)
831 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
833 #endif /* ndef SPLIT_PROXY_ARGS */
835 /* FIXME: this is a kludge for win32 */
836 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
838 g_blockfile = config->blockfile;
839 g_permissions_file = config->permissions_file;
840 g_forwardfile = config->forwardfile;
842 g_aclfile = config->aclfile;
843 #endif /* def ACL_FILES */
844 #ifdef USE_IMAGE_LIST
845 g_imagefile = config->imagefile;
846 #endif /* def USE_IMAGE_LIST */
848 g_re_filterfile = config->re_filterfile;
\r
851 g_trustfile = config->trustfile;
\r
855 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
856 /* FIXME: end kludge */
859 config->need_bind = 1;
861 if (current_configfile)
863 struct configuration_spec * oldcfg = (struct configuration_spec *)
864 current_configfile->f;
866 * Check if config->haddr,hport == oldcfg->haddr,hport
868 * The following could be written more compactly as a single,
869 * (unreadably long) if statement.
871 config->need_bind = 0;
872 if (config->hport != oldcfg->hport)
874 config->need_bind = 1;
876 else if (config->haddr == NULL)
878 if (oldcfg->haddr != NULL)
880 config->need_bind = 1;
883 else if (oldcfg->haddr == NULL)
885 config->need_bind = 1;
887 else if (0 != strcmp(config->haddr, oldcfg->haddr))
889 config->need_bind = 1;
892 current_configfile->unloader = unload_configfile;
895 fs->next = files->next;
898 current_configfile = fs;