1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.3 2001/05/20 01:21:20 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.3 2001/05/20 01:21:20 jongfoster
39 * Version 2.9.4 checkin.
40 * - Merged popupfile and cookiefile, and added control over PCRS
41 * filtering, in new "permissionsfile".
42 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
43 * file error you now get a message box (in the Win32 GUI) rather
44 * than the program exiting with no explanation.
45 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
47 * - Removed tabs from "config"
48 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
49 * - Bumped up version number.
51 * Revision 1.2 2001/05/17 23:01:01 oes
52 * - Cleaned CRLF's from the sources and related files
54 * Revision 1.1.1.1 2001/05/15 13:58:58 oes
55 * Initial import of version 2.9.3 source tree
58 *********************************************************************/
64 #include <sys/types.h>
74 # include <sys/timeb.h>
80 # endif /* def TOGGLE */
85 # endif /* ndef _WIN_CONSOLE */
87 #else /* ifndef _WIN32 */
90 # include <sys/time.h>
91 # include <sys/wait.h>
92 # include <sys/stat.h>
101 #include "showargs.h"
103 #include "killpopup.h"
104 #include "miscutil.h"
106 #include "jbsockets.h"
109 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
112 * Fix a problem with Solaris. There should be no effect on other
114 * Solaris's isspace() is a macro which uses it's argument directly
115 * as an array index. Therefore we need to make sure that high-bit
116 * characters generate +ve values, and ideally we also want to make
117 * the argument match the declared parameter type of "int".
119 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
120 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
122 static const char VANILLA_WAFER[] =
123 "NOTICE=TO_WHOM_IT_MAY_CONCERN_"
124 "Do_not_send_me_any_copyrighted_information_other_than_the_"
125 "document_that_I_am_requesting_or_any_of_its_necessary_components._"
126 "In_particular_do_not_send_me_any_cookies_that_"
127 "are_subject_to_a_claim_of_copyright_by_anybody._"
128 "Take_notice_that_I_refuse_to_be_bound_by_any_license_condition_"
129 "(copyright_or_otherwise)_applying_to_any_cookie._";
132 /* by haroon - indicates if ijb is enabled */
133 int g_bToggleIJB = 1; /* JunkBusters is enabled by default. */
137 int multi_threaded = 1;
139 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
141 const char *tinygifurl = NULL;
142 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
144 const char *logfile = NULL;
146 const char *configfile = NULL;
148 const char *blockfile = NULL;
149 const char *permissions_file = NULL;
150 const char *forwardfile = NULL;
153 const char *aclfile = NULL;
154 #endif /* def ACL_FILES */
156 #ifdef USE_IMAGE_LIST
157 const char *imagefile = NULL;
158 #endif /* def USE_IMAGE_LIST */
161 * Permissions to use for URLs not in the permissions list.
163 int default_permissions = PERMIT_RE_FILTER;
166 const char *re_filterfile = NULL;
167 #endif /* def PCRS */
169 #ifdef FAST_REDIRECTS
170 int fast_redirects = 0;
171 #endif /* def FAST_REDIRECTS */
174 const char *trustfile = NULL;
175 #endif /* def TRUST_FILES */
178 const char *jarfile = NULL;
180 #endif /* def JAR_FILES */
182 const char *referrer = NULL;
183 const char *uagent = NULL;
184 const char *from = NULL;
186 #ifndef SPLIT_PROXY_ARGS
187 const char *suppress_message = NULL;
188 #endif /* ndef SPLIT_PROXY_ARGS */
190 int suppress_vanilla_wafer = 0;
191 int add_forwarded = 0;
193 struct list wafer_list[1];
194 struct list xtra_list[1];
197 struct list trust_info[1];
198 struct url_spec *trust_list[64];
199 #endif /* def TRUST_FILES */
202 * Port and IP to bind to.
203 * Defaults to HADDR_DEFAULT:HADDR_PORT == 127.0.0.1:8000
205 const char *haddr = NULL;
208 #ifndef SPLIT_PROXY_ARGS
209 int suppress_blocklists = 0; /* suppress listing sblock and simage */
210 #endif /* ndef SPLIT_PROXY_ARGS */
212 struct proxy_args proxy_args[1];
215 int config_changed = 0;
219 * The load_config function is now going to call `init_proxy_args',
220 * so it will need argc and argv. Since load_config will also be
221 * a signal handler, we need to have these globally available.
224 const char **Argv = NULL;
228 * This takes the "cryptic" hash of each keyword and aliases them to
229 * something a little more readable. This also makes changing the
230 * hash values easier if they should change or the hash algorthm changes.
231 * Use the included "hash" program to find out what the hash will be
232 * for any string supplied on the command line. (Or just put it in the
233 * config file and read the number from the error message in the log).
236 #define hash_trustfile 56494766ul
237 #define hash_trust_info_url 449869467ul
238 #define hash_debug 78263ul
239 #define hash_tinygif 2227702ul
240 #define hash_add_forwarded_header 3191044770ul
241 #define hash_single_threaded 4250084780ul
242 #define hash_suppress_vanilla_wafer 3121233547ul
243 #define hash_wafer 89669ul
244 #define hash_add_header 237434619ul
245 #define hash_permissions_file 3825730796lu /* "permissionsfile" */
246 #define hash_logfile 2114766ul
247 #define hash_blockfile 48845391ul
248 #define hash_imagefile 51447891ul
249 #define hash_jarfile 2046641ul
250 #define hash_listen_address 1255650842ul
251 #define hash_forwardfile 1268669141ul
252 #define hash_aclfile 1908516ul
253 #define hash_re_filterfile 3877522444ul
254 #define hash_user_agent 283326691ul
255 #define hash_referrer 10883969ul
256 #define hash_referer 2176719ul
257 #define hash_from 16264ul
258 #define hash_fast_redirects 464873764lu
259 #define hash_hide_console 2048809870ul
260 #define hash_include_stats 2174146548ul
261 #define hash_suppress_blocklists 1948693308ul
262 #define hash_toggle 447966ul
264 #define hash_activity_animation 1817904738ul
265 #define hash_log_messages 2291744899ul
266 #define hash_log_highlight_messages 4032101240ul
267 #define hash_log_buffer_size 2918070425ul
268 #define hash_log_max_lines 2868344173ul
269 #define hash_log_font_name 2866730124ul
270 #define hash_log_font_size 2866731014ul
271 #define hash_show_on_task_bar 215410365ul
272 #define hash_close_button_minimizes 3651284693ul
275 /*********************************************************************
277 * Function : load_config
279 * Description : Load the config file and all parameters.
282 * 1 : signum : this can be the signal SIGHUP or 0 (if from main).
283 * In any case, we just ignore this and reload the config file.
285 * Returns : configret : 0 => Ok, everything else is an error.
286 * Note: we use configret since a signal handler cannot
287 * return a value, and this function does double duty.
288 * Ie. Is is called from main and from signal( SIGHUP );
290 *********************************************************************/
291 void load_config( int signum )
295 FILE *configfp = NULL;
297 configret = 0; /* FIXME: This is obsolete, always 0. */
300 log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
302 init_proxy_args(Argc, Argv);
305 /* (Waste of memory [not quite a "leak"] here. The
306 * last blockfile/permissions file/... etc will not be
307 * unloaded until we load a new one. If the
308 * block/... feature has been disabled in
309 * the new config file, then we're wasting some
310 * memory we could otherwise reclaim.
313 /* Disable all loaders. */
314 remove_all_loaders();
317 * Reset to as close to startup state as we can.
318 * But leave changing the logfile until after we're done loading.
327 #endif /* def JAR_FILES */
332 default_permissions = PERMIT_RE_FILTER;
334 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
336 freez((char *)tinygifurl);
337 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
339 suppress_vanilla_wafer = 0;
345 #endif /*def _WIN_CONSOLE*/
354 #endif /* def STATISTICS */
356 #ifndef SPLIT_PROXY_ARGS
357 suppress_blocklists = 0;
358 #endif /* ndef SPLIT_PROXY_ARGS */
361 freez((char *)haddr);
362 freez((char *)uagent);
363 freez((char *)referrer);
364 freez((char *)logfile);
367 freez((char *)blockfile);
368 freez((char *)permissions_file);
369 freez((char *)forwardfile);
372 freez((char *)aclfile);
373 #endif /* def ACL_FILES */
375 #ifdef USE_IMAGE_LIST
376 freez((char *)imagefile);
377 #endif /* def USE_IMAGE_LIST */
380 freez((char *)jarfile);
381 #endif /* def JAR_FILES */
383 #ifndef SPLIT_PROXY_ARGS
384 freez((char *)suppress_message);
385 #endif /* ndef SPLIT_PROXY_ARGS */
388 freez((char *)trustfile);
389 #endif /* def TRUST_FILES */
392 freez((char *)re_filterfile);
393 #endif /* def PCRS */
395 #ifdef FAST_REDIRECTS
397 #endif /* def FAST_REDIRECTS */
399 if (NULL != configfile)
401 if ((configfp = fopen(configfile, "r")) == NULL)
403 log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s': %E",
405 /* Never get here - LOG_LEVEL_FATAL causes program exit */
409 if (NULL != configfp)
411 memset (buf, 'j', sizeof(buf));
412 while (read_config_line(buf, sizeof(buf), configfp, NULL) != NULL)
420 /* Copy command (i.e. up to space or tab) into cmd */
423 while (*p && (*p != ' ') && (*p != '\t'))
429 /* Skip over the whitespace in buf */
430 while (*p && ((*p == ' ') || (*p == '\t')))
435 /* Copy the argument into arg */
438 /* Should never happen, but check this anyway */
444 /* Make sure the command field is lower case */
449 *p = ijb_tolower(*p);
453 /* Save the argument for show-proxy-args */
457 switch( hash_string( cmd ) )
460 case hash_trustfile :
461 freez((char *)trustfile);
462 trustfile = strdup(arg);
465 case hash_trust_info_url :
466 enlist(trust_info, arg);
468 #endif /* def TRUST_FILES */
474 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
476 freez((char *)tinygifurl);
481 while((*p >= '0') && (*p <= '9'))
485 while((*p == ' ') || (*p == '\t'))
491 q = malloc(strlen(p) + 5);
495 strcat(q, "\r\n\r\n");
500 if ((tinygif != 1) &&
502 ((tinygif != 3) || (tinygifurl==NULL)) )
504 log_error(LOG_LEVEL_ERROR, "tinygif setting invalid.");
507 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
509 case hash_add_forwarded_header :
513 case hash_single_threaded :
517 case hash_suppress_vanilla_wafer :
518 suppress_vanilla_wafer = 1;
522 enlist(wafer_list, arg);
525 case hash_add_header :
526 enlist(xtra_list, arg);
529 case hash_permissions_file :
530 freez((char *)permissions_file);
531 permissions_file = strdup(arg);
535 freez((char *)logfile);
536 logfile = strdup(arg);
539 case hash_blockfile :
540 freez((char *)blockfile);
541 blockfile = strdup(arg);
544 #ifdef USE_IMAGE_LIST
545 case hash_imagefile :
546 freez((char *)imagefile);
547 imagefile = strdup(arg);
549 #endif /* def USE_IMAGE_LIST */
553 freez((char *)jarfile);
554 jarfile = strdup(arg);
556 #endif /* def JAR_FILES */
558 case hash_listen_address :
559 freez((char *)haddr);
563 case hash_forwardfile :
564 freez((char *)forwardfile);
565 forwardfile = strdup(arg);
570 freez((char *)aclfile);
571 aclfile = strdup(arg);
573 #endif /* def ACL_FILES */
576 case hash_re_filterfile :
577 freez((char *)re_filterfile);
578 re_filterfile = strdup(arg);
580 #endif /* def PCRS */
582 case hash_user_agent :
583 freez((char *)uagent);
584 uagent = strdup(arg);
588 * Offer choice of correct spelling according to dictionary,
589 * or the misspelling used in the HTTP spec.
593 freez((char *)referrer);
594 referrer = strdup(arg);
602 #ifdef FAST_REDIRECTS
603 case hash_fast_redirects :
606 #endif /* def FAST_REDIRECTS */
609 case hash_hide_console :
612 #endif /*def _WIN_CONSOLE*/
614 #ifndef SPLIT_PROXY_ARGS
615 case hash_suppress_blocklists :
618 suppress_message = strdup(arg);
622 /* There will be NO reference in proxy-args. */
623 suppress_message = NULL;
626 suppress_blocklists = 1;
628 #endif /* ndef SPLIT_PROXY_ARGS */
632 g_bToggleIJB = atoi(arg);
634 #endif /* def TOGGLE */
636 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
637 case hash_activity_animation :
638 g_bShowActivityAnimation = atoi(arg);
641 case hash_log_messages :
642 g_bLogMessages = atoi(arg);
645 case hash_log_highlight_messages :
646 g_bHighlightMessages = atoi(arg);
649 case hash_log_buffer_size :
650 g_bLimitBufferSize = atoi(arg);
653 case hash_log_max_lines :
654 g_nMaxBufferLines = atoi(arg);
657 case hash_log_font_name :
658 strcpy( g_szFontFaceName, arg );
661 case hash_log_font_size :
662 g_nFontSize = atoi(arg);
665 case hash_show_on_task_bar :
666 g_bShowOnTaskBar = atoi(arg);
669 case hash_close_button_minimizes :
670 g_bCloseHidesWindow = atoi(arg);
672 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
674 /* Warnings about unsupported features */
677 case hash_trustfile :
678 case hash_trust_info_url :
679 #endif /* ndef TRUST_FILES */
680 #ifndef USE_IMAGE_LIST
681 case hash_imagefile :
682 #endif /* ndef USE_IMAGE_LIST */
684 case hash_re_filterfile :
685 #endif /* ndef PCRS */
688 #endif /* ndef TOGGLE */
689 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
690 case hash_activity_animation :
691 case hash_log_messages :
692 case hash_log_highlight_messages :
693 case hash_log_buffer_size :
694 case hash_log_max_lines :
695 case hash_log_font_name :
696 case hash_log_font_size :
697 case hash_show_on_task_bar :
698 case hash_close_button_minimizes :
699 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
701 case hash_hide_console :
702 #endif /* ndef _WIN_CONSOLE */
703 #if !defined(DETECT_MSIE_IMAGES) && !defined(USE_IMAGE_LIST)
705 #endif /* !defined(DETECT_MSIE_IMAGES) && !defined(USE_IMAGE_LIST) */
708 #endif /* ndef JAR_FILES */
711 #endif /* ndef ACL_FILES */
713 #ifdef SPLIT_PROXY_ARGS
714 case hash_suppress_blocklists :
715 #endif /* def SPLIT_PROXY_ARGS */
716 log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd);
721 * I decided that I liked this better as a warning than an
722 * error. To change back to an error, just change log level
723 * to LOG_LEVEL_FATAL.
725 log_error(LOG_LEVEL_ERROR, "Unrecognized directive (%lulu) in "
726 "configuration file: \"%s\"", hash_string( cmd ), buf);
727 p = malloc( BUFSIZ );
730 sprintf( p, "<br>\nWARNING: unrecognized directive : %s<br><br>\n", buf );
731 proxy_args->invocation = strsav( proxy_args->invocation, p );
740 init_error_log(Argv[0], logfile, debug);
742 if (permissions_file)
744 add_loader(load_permissions_file);
749 add_loader(load_blockfile);
752 #ifdef USE_IMAGE_LIST
755 add_loader(load_imagefile);
757 #endif /* def USE_IMAGE_LIST */
762 add_loader(load_trustfile);
764 #endif /* def TRUST_FILES */
768 add_loader(load_forwardfile);
774 add_loader(load_aclfile);
776 #endif /* def ACL_FILES */
781 add_loader(load_re_filterfile);
783 #endif /* def PCRS */
786 if ( NULL != jarfile )
788 if ( NULL == (jar = fopen(jarfile, "a")) )
790 log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", jarfile);
791 /* Never get here - LOG_LEVEL_FATAL causes program exit */
795 #endif /* def JAR_FILES */
799 haddr = strdup( HADDR_DEFAULT );
804 if ((p = strchr(haddr, ':')))
816 log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", haddr);
817 /* Never get here - LOG_LEVEL_FATAL causes program exit */
825 if (run_loader(NULL))
827 log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
828 /* Never get here - LOG_LEVEL_FATAL causes program exit */
833 * If we're logging cookies in a cookie jar, and the user has not
834 * supplied any wafers, and the user has not told us to suppress the
835 * vanilla wafer, then send the vanilla wafer.
837 if ((jarfile != NULL)
838 && (wafer_list->next == NULL)
839 && (suppress_vanilla_wafer == 0))
841 enlist(wafer_list, VANILLA_WAFER);
843 #endif /* def JAR_FILES */