type with status file. remove forward et. al from file list.
[privoxy.git] / loadcfg.c
1 /* vim:ts=3: */
2 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.14 2001/06/07 14:46:25 joergs Exp $";
3 /*********************************************************************
4  *
5  * File        :  $Source: /cvsroot/ijbswa/current/loadcfg.c,v $
6  *
7  * Purpose     :  Loads settings from the configuration file into
8  *                global variables.  This file contains both the 
9  *                routine to load the configuration and the global
10  *                variables it writes to.
11  *
12  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
13  *                IJBSWA team.  http://ijbswa.sourceforge.net
14  *
15  *                Based on the Internet Junkbuster originally written
16  *                by and Copyright (C) 1997 Anonymous Coders and 
17  *                Junkbusters Corporation.  http://www.junkbusters.com
18  *
19  *                This program is free software; you can redistribute it 
20  *                and/or modify it under the terms of the GNU General
21  *                Public License as published by the Free Software
22  *                Foundation; either version 2 of the License, or (at
23  *                your option) any later version.
24  *
25  *                This program is distributed in the hope that it will
26  *                be useful, but WITHOUT ANY WARRANTY; without even the
27  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
28  *                PARTICULAR PURPOSE.  See the GNU General Public
29  *                License for more details.
30  *
31  *                The GNU General Public License should be included with
32  *                this file.  If not, you can view it at
33  *                http://www.gnu.org/copyleft/gpl.html
34  *                or write to the Free Software Foundation, Inc., 59
35  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
36  *
37  * Revisions   :
38  *    $Log: loadcfg.c,v $
39  *    Revision 1.14  2001/06/07 14:46:25  joergs
40  *    Missing make_path() added for re_filterfile.
41  *
42  *    Revision 1.13  2001/06/05 22:33:54  jongfoster
43  *
44  *    Fixed minor memory leak.
45  *    Also now uses make_path to prepend the pathnames.
46  *
47  *    Revision 1.12  2001/06/05 20:04:09  jongfoster
48  *    Now uses _snprintf() in place of snprintf() under Win32.
49  *
50  *    Revision 1.11  2001/06/04 18:31:58  swa
51  *    files are now prefixed with either `confdir' or `logdir'.
52  *    `make redhat-dist' replaces both entries confdir and logdir
53  *    with redhat values
54  *
55  *    Revision 1.10  2001/06/03 19:11:54  oes
56  *    introduced confdir option
57  *
58  *    Revision 1.10  2001/06/03 11:03:48  oes
59  *    Makefile/in
60  *
61  *    introduced cgi.c
62  *
63  *    actions.c:
64  *
65  *    adapted to new enlist_unique arg format
66  *
67  *    conf loadcfg.c
68  *
69  *    introduced confdir option
70  *
71  *    filters.c filtrers.h
72  *
73  *     extracted-CGI relevant stuff
74  *
75  *    jbsockets.c
76  *
77  *     filled comment
78  *
79  *    jcc.c
80  *
81  *     support for new cgi mechansim
82  *
83  *    list.c list.h
84  *
85  *    functions for new list type: "map"
86  *    extended enlist_unique
87  *
88  *    miscutil.c .h
89  *    introduced bindup()
90  *
91  *    parsers.c parsers.h
92  *
93  *    deleted const struct interceptors
94  *
95  *    pcrs.c
96  *    added FIXME
97  *
98  *    project.h
99  *
100  *    added struct map
101  *    added struct http_response
102  *    changes struct interceptors to struct cgi_dispatcher
103  *    moved HTML stuff to cgi.h
104  *
105  *    re_filterfile:
106  *
107  *    changed
108  *
109  *    showargs.c
110  *    NO TIME LEFT
111  *
112  *    Revision 1.9  2001/06/01 20:06:24  jongfoster
113  *    Removed support for "tinygif" option - moved to actions file.
114  *
115  *    Revision 1.8  2001/05/31 21:27:13  jongfoster
116  *    Removed many options from the config file and into the
117  *    "actions" file: add_forwarded, suppress_vanilla_wafer,
118  *    wafer, add_header, user_agent, referer, from
119  *    Also globally replaced "permission" with "action".
120  *
121  *    Revision 1.7  2001/05/29 09:50:24  jongfoster
122  *    Unified blocklist/imagelist/permissionslist.
123  *    File format is still under discussion, but the internal changes
124  *    are (mostly) done.
125  *
126  *    Also modified interceptor behaviour:
127  *    - We now intercept all URLs beginning with one of the following
128  *      prefixes (and *only* these prefixes):
129  *        * http://i.j.b/
130  *        * http://ijbswa.sf.net/config/
131  *        * http://ijbswa.sourceforge.net/config/
132  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
133  *    - Internal changes so that intercepted and fast redirect pages
134  *      are not replaced with an image.
135  *    - Interceptors now have the option to send a binary page direct
136  *      to the client. (i.e. ijb-send-banner uses this)
137  *    - Implemented show-url-info interceptor.  (Which is why I needed
138  *      the above interceptors changes - a typical URL is
139  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
140  *      The previous mechanism would not have intercepted that, and
141  *      if it had been intercepted then it then it would have replaced
142  *      it with an image.)
143  *
144  *    Revision 1.6  2001/05/26 00:28:36  jongfoster
145  *    Automatic reloading of config file.
146  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
147  *    Most of the global variables have been moved to a new
148  *    struct configuration_spec, accessed through csp->config->globalname
149  *    Most of the globals remaining are used by the Win32 GUI.
150  *
151  *    Revision 1.5  2001/05/25 22:34:30  jongfoster
152  *    Hard tabs->Spaces
153  *
154  *    Revision 1.4  2001/05/22 18:46:04  oes
155  *
156  *    - Enabled filtering banners by size rather than URL
157  *      by adding patterns that replace all standard banner
158  *      sizes with the "Junkbuster" gif to the re_filterfile
159  *
160  *    - Enabled filtering WebBugs by providing a pattern
161  *      which kills all 1x1 images
162  *
163  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
164  *      which is selected by the (nonstandard and therefore
165  *      capital) letter 'U' in the option string.
166  *      It causes the quantifiers to be ungreedy by default.
167  *      Appending a ? turns back to greedy (!).
168  *
169  *    - Added a new interceptor ijb-send-banner, which
170  *      sends back the "Junkbuster" gif. Without imagelist or
171  *      MSIE detection support, or if tinygif = 1, or the
172  *      URL isn't recognized as an imageurl, a lame HTML
173  *      explanation is sent instead.
174  *
175  *    - Added new feature, which permits blocking remote
176  *      script redirects and firing back a local redirect
177  *      to the browser.
178  *      The feature is conditionally compiled, i.e. it
179  *      can be disabled with --disable-fast-redirects,
180  *      plus it must be activated by a "fast-redirects"
181  *      line in the config file, has its own log level
182  *      and of course wants to be displayed by show-proxy-args
183  *      Note: Boy, all the #ifdefs in 1001 locations and
184  *      all the fumbling with configure.in and acconfig.h
185  *      were *way* more work than the feature itself :-(
186  *
187  *    - Because a generic redirect template was needed for
188  *      this, tinygif = 3 now uses the same.
189  *
190  *    - Moved GIFs, and other static HTTP response templates
191  *      to project.h
192  *
193  *    - Some minor fixes
194  *
195  *    - Removed some >400 CRs again (Jon, you really worked
196  *      a lot! ;-)
197  *
198  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
199  *    Version 2.9.4 checkin.
200  *    - Merged popupfile and cookiefile, and added control over PCRS
201  *      filtering, in new "permissionsfile".
202  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
203  *      file error you now get a message box (in the Win32 GUI) rather
204  *      than the program exiting with no explanation.
205  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
206  *      skipping.
207  *    - Removed tabs from "config"
208  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
209  *    - Bumped up version number.
210  *
211  *    Revision 1.2  2001/05/17 23:01:01  oes
212  *     - Cleaned CRLF's from the sources and related files
213  *
214  *    Revision 1.1.1.1  2001/05/15 13:58:58  oes
215  *    Initial import of version 2.9.3 source tree
216  *
217  *
218  *********************************************************************/
219 \f
220
221 #include "config.h"
222
223 #include <stdio.h>
224 #include <sys/types.h>
225 #include <stdlib.h>
226 #include <string.h>
227 #include <signal.h>
228 #include <fcntl.h>
229 #include <errno.h>
230 #include <ctype.h>
231
232 #ifdef _WIN32
233
234 # include <sys/timeb.h>
235 # include <windows.h>
236 # include <io.h>
237 # include <process.h>
238 # ifdef TOGGLE
239 #  include <time.h>
240 # endif /* def TOGGLE */
241
242 # include "win32.h"
243 # ifndef _WIN_CONSOLE
244 #  include "w32log.h"
245 # endif /* ndef _WIN_CONSOLE */
246
247 /* VC++ has "_snprintf", not "snprintf" */
248 #define snprintf _snprintf
249
250 #else /* ifndef _WIN32 */
251
252 # include <unistd.h>
253 # include <sys/time.h>
254 # include <sys/wait.h>
255 # include <sys/stat.h>
256 # include <signal.h>
257
258 #endif
259
260 #include "loadcfg.h"
261 #include "list.h"
262 #include "jcc.h"
263 #include "filters.h"
264 #include "loaders.h"
265 #include "showargs.h"
266 #include "parsers.h"
267 #include "killpopup.h"
268 #include "miscutil.h"
269 #include "errlog.h"
270 #include "jbsockets.h"
271 #include "gateway.h"
272 #include "ssplit.h"
273
274 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
275
276 /*
277  * Fix a problem with Solaris.  There should be no effect on other
278  * platforms.
279  * Solaris's isspace() is a macro which uses it's argument directly
280  * as an array index.  Therefore we need to make sure that high-bit
281  * characters generate +ve values, and ideally we also want to make
282  * the argument match the declared parameter type of "int".
283  */
284 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
285 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
286
287 #ifdef TOGGLE
288 /* by haroon - indicates if ijb is enabled */
289 int g_bToggleIJB        = 1;   /* JunkBusters is enabled by default. */
290 #endif
291
292 /* The filename of the configfile */
293 const char *configfile  = NULL;
294
295 /*
296  * The load_config function is now going to call `init_proxy_args',
297  * so it will need argc and argv.  So we need to have these
298  * globally available.
299  */
300 int Argc = 0;
301 const char **Argv = NULL;
302
303 static struct file_list *current_configfile = NULL;
304
305
306 /*
307  * This takes the "cryptic" hash of each keyword and aliases them to
308  * something a little more readable.  This also makes changing the
309  * hash values easier if they should change or the hash algorthm changes.
310  * Use the included "hash" program to find out what the hash will be
311  * for any string supplied on the command line.  (Or just put it in the
312  * config file and read the number from the error message in the log).
313  *
314  * Please keep this list sorted alphabetically (but with the Windows
315  * console and GUI specific options last).
316  */
317
318 #define hash_actions_file              3825730796ul /* FIXME "permissionsfile" */
319 #define hash_confdir                      1978389ul /* "confdir" */
320 #define hash_debug                          78263ul /* "debug" */
321 #define hash_deny_access               1227333715ul /* "deny-access" */
322 #define hash_forward                      2029845ul /* "forward" */
323 #define hash_forward_socks4            3963965521ul /* "forward-socks4" */
324 #define hash_forward_socks4a           2639958518ul /* "forward-socks4a" */
325 #define hash_jarfile                      2046641ul /* "jarfile" */
326 #define hash_listen_address            1255650842ul /* "listen-address" */
327 #define hash_logdir                        422889ul /* "logdir" */
328 #define hash_logfile                      2114766ul /* "logfile" */
329 #define hash_permit_access             3587953268ul /* "permit-access" */
330 #define hash_re_filterfile             3877522444ul /* "re_filterfile" */
331 #define hash_single_threaded           4250084780ul /* "single-threaded" */
332 #define hash_suppress_blocklists       1948693308ul /* ??? */
333 #define hash_toggle                        447966ul /* "toggle" */
334 #define hash_trust_info_url             449869467ul /* ??? */
335 #define hash_trustfile                   56494766ul /* "trustfile" */
336
337 #define hash_hide_console              2048809870ul /* "hide-console" */
338
339 #define hash_activity_animation        1817904738ul /* "activity-animation" */
340 #define hash_close_button_minimizes    3651284693ul /* "close-button-minimizes" */
341 #define hash_log_buffer_size           2918070425ul /* "log-buffer-size" */
342 #define hash_log_font_name             2866730124ul /* "log-font-name" */
343 #define hash_log_font_size             2866731014ul /* "log-font-size" */
344 #define hash_log_highlight_messages    4032101240ul /* "log-highlight-messages" */
345 #define hash_log_max_lines             2868344173ul /* "log-max-lines" */
346 #define hash_log_messages              2291744899ul /* "log-messages" */
347 #define hash_show_on_task_bar           215410365ul /* "show-on-task-bar" */
348
349
350 /*********************************************************************
351  *
352  * Function    :  unload_configfile
353  *
354  * Description :  Free the config structure and all components.
355  *
356  * Parameters  :
357  *          1  :  data: struct configuration_spec to unload
358  *
359  * Returns     :  N/A
360  *
361  *********************************************************************/
362 void unload_configfile (void * data)
363 {
364    struct configuration_spec * config = (struct configuration_spec *)data;
365    struct forward_spec *cur_fwd = config->forward;
366 #ifdef ACL_FILES
367    struct access_control_list *cur_acl = config->acl;
368
369    while (cur_acl != NULL)
370    {
371       struct access_control_list * next_acl = cur_acl->next;
372       free(cur_acl);
373       cur_acl = next_acl;
374    }
375    config->acl = NULL;
376 #endif /* def ACL_FILES */
377
378    while (cur_fwd != NULL)
379    {
380       struct forward_spec * next_fwd = cur_fwd->next;
381       free_url(cur_fwd->url);
382
383       freez(cur_fwd->gateway_host);
384       freez(cur_fwd->forward_host);
385       free(cur_fwd);
386       cur_fwd = next_fwd;
387    }
388    config->forward = NULL;
389    
390 #ifdef JAR_FILES
391    if ( NULL != config->jar )
392    {
393       fclose( config->jar );
394       config->jar = NULL;
395    }
396 #endif /* def JAR_FILES */
397
398    freez((char *)config->confdir);
399    freez((char *)config->logdir);
400
401    freez((char *)config->haddr);
402    freez((char *)config->logfile);
403
404    freez((char *)config->actions_file);
405
406 #ifdef JAR_FILES
407    freez((char *)config->jarfile);
408 #endif /* def JAR_FILES */
409
410 #ifndef SPLIT_PROXY_ARGS
411    freez((char *)config->suppress_message);
412 #endif /* ndef SPLIT_PROXY_ARGS */
413
414 #ifdef PCRS
415    freez((char *)config->re_filterfile);
416 #endif /* def PCRS */
417
418 }
419
420
421 /*********************************************************************
422  *
423  * Function    :  load_config
424  *
425  * Description :  Load the config file and all parameters.
426  *
427  * Parameters  :
428  *          1  :  csp = Client state (the config member will be 
429  *                filled in by this function).
430  *
431  * Returns     :  0 => Ok, everything else is an error.
432  *
433  *********************************************************************/
434 struct configuration_spec * load_config(void)
435 {
436    char buf[BUFSIZ];
437    char *p, *q;
438    FILE *configfp = NULL;
439    struct configuration_spec * config = NULL;
440    struct client_state * fake_csp;
441    struct file_list *fs;
442
443    if (!check_file_changed(current_configfile, configfile, &fs))
444    {
445       /* No need to load */
446       return ((struct configuration_spec *)current_configfile->f);
447    }
448    if (!fs)
449    {
450       log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s':  %E",
451                 configfile);
452    }
453
454    log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
455
456 #ifdef TOGGLE
457    g_bToggleIJB      = 1;
458 #endif
459
460    fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
461
462    if (config==NULL)
463    {
464       freez(fs->filename);
465       freez(fs);
466       log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
467       /* Never get here - LOG_LEVEL_FATAL causes program exit */
468    }
469
470    /*
471     * This is backwards from how it's usually done.
472     * Following the usual pattern, "fs" would be stored in a member 
473     * variable in "csp", and then we'd access "config" from "fs->f",
474     * using a cast.  However, "config" is used so often that a 
475     * cast each time would be very ugly, and the extra indirection
476     * would waste CPU cycles.  Therefore we store "config" in
477     * "csp->config", and "fs" in "csp->config->config_file_list".
478     */
479    config->config_file_list = fs;
480
481    init_proxy_args(Argc, Argv, config);
482
483    /*
484     * Set to defaults
485     */
486
487    config->multi_threaded    = 1;
488    config->hport             = HADDR_PORT;
489
490    if ((configfp = fopen(configfile, "r")) == NULL)
491    {
492       log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s':  %E",
493               configfile);
494       /* Never get here - LOG_LEVEL_FATAL causes program exit */
495    }
496
497    while (read_config_line(buf, sizeof(buf), configfp, fs) != NULL)
498    {
499       char cmd[BUFSIZ];
500       char arg[BUFSIZ];
501       char tmp[BUFSIZ];
502 #ifdef ACL_FILES
503       struct access_control_list *cur_acl;
504 #endif /* def ACL_FILES */
505       struct forward_spec *cur_fwd;
506       int vec_count;
507       char *vec[3];
508
509       strcpy(tmp, buf);
510
511       /* Copy command (i.e. up to space or tab) into cmd */
512       p = buf;
513       q = cmd;
514       while (*p && (*p != ' ') && (*p != '\t'))
515       {
516          *q++ = *p++;
517       }
518       *q = '\0';
519
520       /* Skip over the whitespace in buf */
521       while (*p && ((*p == ' ') || (*p == '\t')))
522       {
523          p++;
524       }
525
526       /* Copy the argument into arg */
527       strcpy(arg, p);
528
529       /* Should never happen, but check this anyway */
530       if (*cmd == '\0')
531       {
532          continue;
533       }
534
535       /* Make sure the command field is lower case */
536       for (p=cmd; *p; p++)
537       {
538          if (ijb_isupper(*p))
539          {
540             *p = ijb_tolower(*p);
541          }
542       }
543
544       /* Save the argument for show-proxy-args */
545       savearg(cmd, arg, config);
546
547
548       switch( hash_string( cmd ) )
549       {
550 /****************************************************************************
551  * permissionsfile actions-file-name
552  * In confdir by default
553  ****************************************************************************/
554          case hash_actions_file :
555             freez((char *)config->actions_file);
556             config->actions_file = make_path(config->confdir, arg);
557             continue;
558
559 /****************************************************************************
560  * confdir directory-name
561  ****************************************************************************/
562          case hash_confdir :
563             freez((char *)config->confdir);
564             config->confdir = strdup(arg);
565             continue;            
566
567 /****************************************************************************
568  * debug n
569  * Specifies debug level, multiple values are ORed together.
570  ****************************************************************************/
571          case hash_debug :
572             config->debug |= atoi(arg);
573             continue;
574
575 /****************************************************************************
576  * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
577  ****************************************************************************/
578 #ifdef ACL_FILES
579          case hash_deny_access:
580             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
581
582             if ((vec_count != 1) && (vec_count != 2))
583             {
584                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
585                      "deny-access directive in configuration file.");
586                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
587                   "<br>\nWARNING: Wrong number of parameters for "
588                   "deny-access directive in configuration file.<br><br>\n");
589                continue;
590             }
591
592             /* allocate a new node */
593             cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
594
595             if (cur_acl == NULL)
596             {
597                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
598                /* Never get here - LOG_LEVEL_FATAL causes program exit */
599                continue;
600             }
601             cur_acl->action = ACL_DENY;
602
603             if (acl_addr(vec[0], cur_acl->src) < 0)
604             {
605                log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
606                      "directive in configuration file: \"%s\"", vec[0]);
607                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
608                   "<br>\nWARNING: Invalid source IP for deny-access directive"
609                   " in configuration file: \"");
610                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
611                   vec[0]);
612                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
613                   "\"<br><br>\n");
614                freez(cur_acl);
615                continue;
616             }
617             if (vec_count == 2)
618             {
619                if (acl_addr(vec[1], cur_acl->dst) < 0)
620                {
621                   log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
622                         "directive in configuration file: \"%s\"", vec[0]);
623                   config->proxy_args_invocation = strsav( config->proxy_args_invocation,
624                      "<br>\nWARNING: Invalid destination IP for deny-access directive"
625                      " in configuration file: \"");
626                   config->proxy_args_invocation = strsav( config->proxy_args_invocation,
627                      vec[0]);
628                   config->proxy_args_invocation = strsav( config->proxy_args_invocation,
629                      "\"<br><br>\n");
630                   freez(cur_acl);
631                   continue;
632                }
633             }
634
635             /*
636              * Add it to the list.  Note we reverse the list to get the
637              * behaviour the user expects.  With both the ACL and
638              * actions file, the last match wins.  However, the internal
639              * implementations are different:  The actions file is stored
640              * in the same order as the file, and scanned completely.
641              * With the ACL, we reverse the order as we load it, then 
642              * when we scan it we stop as soon as we get a match.
643              */
644             cur_acl->next  = config->acl;
645             config->acl = cur_acl;
646
647             continue;
648 #endif /* def ACL_FILES */
649
650 /****************************************************************************
651  * forward url-pattern (.|http-proxy-host[:port])
652  ****************************************************************************/
653          case hash_forward:
654             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
655
656             if (vec_count != 2)
657             {
658                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
659                      "directive in configuration file.");
660                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
661                   "<br>\nWARNING: Wrong number of parameters for "
662                   "forward directive in configuration file.");
663                continue;
664             }
665
666             /* allocate a new node */
667             cur_fwd = zalloc(sizeof(*cur_fwd));
668             if (cur_fwd == NULL)
669             {
670                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
671                /* Never get here - LOG_LEVEL_FATAL causes program exit */
672                continue;
673             }
674
675             cur_fwd->type = SOCKS_NONE;
676
677             /* Save the URL pattern */
678             if (create_url_spec(cur_fwd->url, vec[0]))
679             {
680                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
681                      "directive in configuration file.");
682                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
683                   "<br>\nWARNING: Bad URL specifier for "
684                   "forward directive in configuration file.");
685                continue;
686             }
687
688             /* Parse the parent HTTP proxy host:port */
689             p = vec[1];
690
691             if (strcmp(p, ".") != 0)
692             {
693                cur_fwd->forward_host = strdup(p);
694
695                if ((p = strchr(cur_fwd->forward_host, ':')))
696                {
697                   *p++ = '\0';
698                   cur_fwd->forward_port = atoi(p);
699                }
700
701                if (cur_fwd->forward_port <= 0)
702                {
703                   cur_fwd->forward_port = 8000;
704                }
705             }
706
707             /* Add to list. */
708             cur_fwd->next = config->forward;
709             config->forward = cur_fwd;
710
711             continue;
712
713 /****************************************************************************
714  * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
715  ****************************************************************************/
716          case hash_forward_socks4:
717             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
718
719             if (vec_count != 3)
720             {
721                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
722                      "forward-socks4 directive in configuration file.");
723                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
724                   "<br>\nWARNING: Wrong number of parameters for "
725                   "forward-socks4 directive in configuration file.");
726                continue;
727             }
728
729             /* allocate a new node */
730             cur_fwd = zalloc(sizeof(*cur_fwd));
731             if (cur_fwd == NULL)
732             {
733                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
734                /* Never get here - LOG_LEVEL_FATAL causes program exit */
735                continue;
736             }
737
738             cur_fwd->type = SOCKS_4;
739
740             /* Save the URL pattern */
741             if (create_url_spec(cur_fwd->url, vec[0]))
742             {
743                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
744                      "directive in configuration file.");
745                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
746                   "<br>\nWARNING: Bad URL specifier for "
747                   "forward-socks4 directive in configuration file.");
748                continue;
749             }
750
751             /* Parse the SOCKS proxy host[:port] */
752             p = vec[1];
753
754             if (strcmp(p, ".") != 0)
755             {
756                cur_fwd->gateway_host = strdup(p);
757
758                if ((p = strchr(cur_fwd->gateway_host, ':')))
759                {
760                   *p++ = '\0';
761                   cur_fwd->gateway_port = atoi(p);
762                }
763                if (cur_fwd->gateway_port <= 0)
764                {
765                   cur_fwd->gateway_port = 1080;
766                }
767             }
768
769             /* Parse the parent HTTP proxy host[:port] */
770             p = vec[2];
771
772             if (strcmp(p, ".") != 0)
773             {
774                cur_fwd->forward_host = strdup(p);
775
776                if ((p = strchr(cur_fwd->forward_host, ':')))
777                {
778                   *p++ = '\0';
779                   cur_fwd->forward_port = atoi(p);
780                }
781
782                if (cur_fwd->forward_port <= 0)
783                {
784                   cur_fwd->forward_port = 8000;
785                }
786             }
787
788             /* Add to list. */
789             cur_fwd->next = config->forward;
790             config->forward = cur_fwd;
791             
792             continue;
793
794 /****************************************************************************
795  * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
796  ****************************************************************************/
797          case hash_forward_socks4a:
798             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
799
800             if (vec_count != 3)
801             {
802                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
803                      "forward-socks4a directive in configuration file.");
804                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
805                   "<br>\nWARNING: Wrong number of parameters for "
806                   "forward-socks4a directive in configuration file.");
807                continue;
808             }
809
810             /* allocate a new node */
811             cur_fwd = zalloc(sizeof(*cur_fwd));
812             if (cur_fwd == NULL)
813             {
814                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
815                /* Never get here - LOG_LEVEL_FATAL causes program exit */
816                continue;
817             }
818
819             cur_fwd->type = SOCKS_4A;
820
821             /* Save the URL pattern */
822             if (create_url_spec(cur_fwd->url, vec[0]))
823             {
824                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
825                      "directive in configuration file.");
826                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
827                   "<br>\nWARNING: Bad URL specifier for "
828                   "forward-socks4a directive in configuration file.");
829                continue;
830             }
831
832             /* Parse the SOCKS proxy host[:port] */
833             p = vec[1];
834
835             cur_fwd->gateway_host = strdup(p);
836
837             if ((p = strchr(cur_fwd->gateway_host, ':')))
838             {
839                *p++ = '\0';
840                cur_fwd->gateway_port = atoi(p);
841             }
842             if (cur_fwd->gateway_port <= 0)
843             {
844                cur_fwd->gateway_port = 1080;
845             }
846
847             /* Parse the parent HTTP proxy host[:port] */
848             p = vec[2];
849
850             if (strcmp(p, ".") != 0)
851             {
852                cur_fwd->forward_host = strdup(p);
853
854                if ((p = strchr(cur_fwd->forward_host, ':')))
855                {
856                   *p++ = '\0';
857                   cur_fwd->forward_port = atoi(p);
858                }
859
860                if (cur_fwd->forward_port <= 0)
861                {
862                   cur_fwd->forward_port = 8000;
863                }
864             }
865
866             /* Add to list. */
867             cur_fwd->next = config->forward;
868             config->forward = cur_fwd;
869             
870             continue;
871
872 /****************************************************************************
873  * jarfile jar-file-name
874  * In logdir by default
875  ****************************************************************************/
876 #ifdef JAR_FILES
877          case hash_jarfile :
878             freez((char *)config->jarfile);
879             config->jarfile = make_path(config->logdir, arg);
880             continue;
881 #endif /* def JAR_FILES */
882
883 /****************************************************************************
884  * listen-address [ip][:port]
885  ****************************************************************************/
886          case hash_listen_address :
887             freez((char *)config->haddr);
888             config->haddr = strdup(arg);
889             continue;
890
891 /****************************************************************************
892  * logdir directory-name
893  ****************************************************************************/
894          case hash_logdir :
895             freez((char *)config->logdir);
896             config->logdir = strdup(arg);
897             continue;            
898
899 /****************************************************************************
900  * logfile log-file-name
901  * In logdir by default
902  ****************************************************************************/
903          case hash_logfile :
904             freez((char *)config->logfile);
905             config->logfile = make_path(config->logdir, arg);
906             continue;
907
908 /****************************************************************************
909  * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
910  ****************************************************************************/
911 #ifdef ACL_FILES
912          case hash_permit_access:
913             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
914
915             if ((vec_count != 1) && (vec_count != 2))
916             {
917                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
918                      "permit-access directive in configuration file.");
919                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
920                   "<br>\nWARNING: Wrong number of parameters for "
921                   "permit-access directive in configuration file.<br><br>\n");
922
923                continue;
924             }
925
926             /* allocate a new node */
927             cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
928
929             if (cur_acl == NULL)
930             {
931                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
932                /* Never get here - LOG_LEVEL_FATAL causes program exit */
933                continue;
934             }
935             cur_acl->action = ACL_PERMIT;
936
937             if (acl_addr(vec[0], cur_acl->src) < 0)
938             {
939                log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
940                      "directive in configuration file: \"%s\"", vec[0]);
941                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
942                   "<br>\nWARNING: Invalid source IP for permit-access directive"
943                   " in configuration file: \"");
944                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
945                   vec[0]);
946                config->proxy_args_invocation = strsav( config->proxy_args_invocation,
947                   "\"<br><br>\n");
948                freez(cur_acl);
949                continue;
950             }
951             if (vec_count == 2)
952             {
953                if (acl_addr(vec[1], cur_acl->dst) < 0)
954                {
955                   log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
956                         "permit-access directive in configuration file: \"%s\"",
957                         vec[0]);
958                   config->proxy_args_invocation = strsav( config->proxy_args_invocation,
959                      "<br>\nWARNING: Invalid destination IP for permit-access directive"
960                      " in configuration file: \"");
961                   config->proxy_args_invocation = strsav( config->proxy_args_invocation,
962                      vec[0]);
963                   config->proxy_args_invocation = strsav( config->proxy_args_invocation,
964                      "\"<br><br>\n");
965                   freez(cur_acl);
966                   continue;
967                }
968             }
969
970             /*
971              * Add it to the list.  Note we reverse the list to get the
972              * behaviour the user expects.  With both the ACL and
973              * actions file, the last match wins.  However, the internal
974              * implementations are different:  The actions file is stored
975              * in the same order as the file, and scanned completely.
976              * With the ACL, we reverse the order as we load it, then 
977              * when we scan it we stop as soon as we get a match.
978              */
979             cur_acl->next  = config->acl;
980             config->acl = cur_acl;
981
982             continue;
983 #endif /* def ACL_FILES */
984
985 /****************************************************************************
986  * re_filterfile file-name
987  * In confdir by default.
988  ****************************************************************************/
989 #ifdef PCRS
990          case hash_re_filterfile :
991             freez((char *)config->re_filterfile);
992             config->re_filterfile = make_path(config->confdir, arg);
993             continue;
994 #endif /* def PCRS */
995
996 /****************************************************************************
997  * single-threaded
998  ****************************************************************************/
999          case hash_single_threaded :
1000             config->multi_threaded = 0;
1001             continue;
1002
1003 /****************************************************************************
1004  * FIXME: Document this
1005  ****************************************************************************/
1006 #ifndef SPLIT_PROXY_ARGS
1007          case hash_suppress_blocklists :
1008             if (arg[0] != '\0')
1009             {
1010                config->suppress_message = strdup(arg);
1011             }
1012             else
1013             {
1014                /* There will be NO reference in proxy-args. */
1015                config->suppress_message = NULL;
1016             }
1017
1018             config->suppress_blocklists = 1;
1019             continue;
1020 #endif /* ndef SPLIT_PROXY_ARGS */
1021
1022 /****************************************************************************
1023  * toggle (0|1)
1024  ****************************************************************************/
1025 #ifdef TOGGLE
1026          case hash_toggle :
1027             g_bToggleIJB = atoi(arg);
1028             continue;
1029 #endif /* def TOGGLE */
1030
1031 /****************************************************************************
1032  * FIXME: Please document this!
1033  ****************************************************************************/
1034 #ifdef TRUST_FILES
1035          case hash_trust_info_url :
1036             enlist(config->trust_info, arg);
1037             continue;
1038 #endif /* def TRUST_FILES */
1039
1040 /****************************************************************************
1041  * trustfile filename
1042  * (In confdir by default.)
1043  ****************************************************************************/
1044 #ifdef TRUST_FILES
1045          case hash_trustfile :
1046             freez((char *)config->trustfile);
1047             config->trustfile = make_path(config->confdir, arg);
1048             continue;
1049 #endif /* def TRUST_FILES */
1050
1051
1052 /****************************************************************************
1053  * Win32 Console options:
1054  ****************************************************************************/
1055
1056 /****************************************************************************
1057  * hide-console
1058  ****************************************************************************/
1059 #ifdef _WIN_CONSOLE
1060          case hash_hide_console :
1061             hideConsole = 1;
1062             continue;
1063 #endif /*def _WIN_CONSOLE*/
1064
1065
1066 /****************************************************************************
1067  * Win32 GUI options:
1068  ****************************************************************************/
1069
1070 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1071 /****************************************************************************
1072  * activity-animation (0|1)
1073  ****************************************************************************/
1074          case hash_activity_animation :
1075             g_bShowActivityAnimation = atoi(arg);
1076             continue;
1077
1078 /****************************************************************************
1079  *  close-button-minimizes (0|1)
1080  ****************************************************************************/
1081          case hash_close_button_minimizes :
1082             g_bCloseHidesWindow = atoi(arg);
1083             continue;
1084
1085 /****************************************************************************
1086  * log-buffer-size (0|1)
1087  ****************************************************************************/
1088          case hash_log_buffer_size :
1089             g_bLimitBufferSize = atoi(arg);
1090             continue;
1091
1092 /****************************************************************************
1093  * log-font-name fontnane
1094  ****************************************************************************/
1095          case hash_log_font_name :
1096             strcpy( g_szFontFaceName, arg );
1097             continue;
1098
1099 /****************************************************************************
1100  * log-font-size n
1101  ****************************************************************************/
1102          case hash_log_font_size :
1103             g_nFontSize = atoi(arg);
1104             continue;
1105
1106 /****************************************************************************
1107  * log-highlight-messages (0|1)
1108  ****************************************************************************/
1109          case hash_log_highlight_messages :
1110             g_bHighlightMessages = atoi(arg);
1111             continue;
1112
1113 /****************************************************************************
1114  * log-max-lines n
1115  ****************************************************************************/
1116          case hash_log_max_lines :
1117             g_nMaxBufferLines = atoi(arg);
1118             continue;
1119
1120 /****************************************************************************
1121  * log-messages (0|1)
1122  ****************************************************************************/
1123          case hash_log_messages :
1124             g_bLogMessages = atoi(arg);
1125             continue;
1126
1127 /****************************************************************************
1128  * show-on-task-bar (0|1)
1129  ****************************************************************************/
1130          case hash_show_on_task_bar :
1131             g_bShowOnTaskBar = atoi(arg);
1132             continue;
1133
1134 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1135
1136
1137 /****************************************************************************/
1138 /* Warnings about unsupported features                                      */
1139 /****************************************************************************/
1140 #ifndef ACL_FILES
1141          case hash_deny_access:
1142 #endif /* ndef ACL_FILES */
1143 #ifndef JAR_FILES
1144          case hash_jarfile :
1145 #endif /* ndef JAR_FILES */
1146 #ifndef ACL_FILES
1147          case hash_permit_access:
1148 #endif /* ndef ACL_FILES */
1149 #ifndef PCRS
1150          case hash_re_filterfile :
1151 #endif /* ndef PCRS */
1152 #ifdef SPLIT_PROXY_ARGS
1153          case hash_suppress_blocklists :
1154 #endif /* def SPLIT_PROXY_ARGS */
1155 #ifndef TOGGLE
1156          case hash_toggle :
1157 #endif /* ndef TOGGLE */
1158 #ifndef TRUST_FILES
1159          case hash_trustfile :
1160          case hash_trust_info_url :
1161 #endif /* ndef TRUST_FILES */
1162
1163 #ifndef _WIN_CONSOLE
1164          case hash_hide_console :
1165 #endif /* ndef _WIN_CONSOLE */
1166
1167 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1168          case hash_activity_animation :
1169          case hash_close_button_minimizes :
1170          case hash_log_buffer_size :
1171          case hash_log_font_name :
1172          case hash_log_font_size :
1173          case hash_log_highlight_messages :
1174          case hash_log_max_lines :
1175          case hash_log_messages :
1176          case hash_show_on_task_bar :
1177 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1178             /* These warnings are annoying - so hide them. -- Jon */
1179             /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1180             continue;
1181
1182 /****************************************************************************/
1183          default :
1184 /****************************************************************************/
1185             /*
1186              * I decided that I liked this better as a warning than an
1187              * error.  To change back to an error, just change log level
1188              * to LOG_LEVEL_FATAL.
1189              */
1190             log_error(LOG_LEVEL_ERROR, "Unrecognized directive (%luul) in "
1191                   "configuration file: \"%s\"", hash_string( cmd ), buf);
1192             config->proxy_args_invocation = strsav( config->proxy_args_invocation, "<br>\nWARNING: unrecognized directive : ");
1193             config->proxy_args_invocation = strsav( config->proxy_args_invocation, buf);
1194             config->proxy_args_invocation = strsav( config->proxy_args_invocation, "<br><br>\n");
1195             continue;
1196
1197 /****************************************************************************/
1198       } /* end switch( hash_string(cmd) ) */
1199    } /* end while ( read_config_line(...) ) */
1200
1201    fclose(configfp);
1202
1203    init_error_log(Argv[0], config->logfile, config->debug);
1204
1205    if (config->actions_file)
1206    {
1207       add_loader(load_actions_file, config);
1208    }
1209
1210 #ifdef PCRS
1211    if (config->re_filterfile)
1212    {
1213       add_loader(load_re_filterfile, config);
1214    }
1215 #endif /* def PCRS */
1216
1217 #ifdef TRUST_FILES
1218    if (config->trustfile)
1219    {
1220       add_loader(load_trustfile, config);
1221    }
1222 #endif
1223
1224 #ifdef JAR_FILES
1225    if ( NULL != config->jarfile )
1226    {
1227       if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1228       {
1229          log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1230          /* Never get here - LOG_LEVEL_FATAL causes program exit */
1231       }
1232       setbuf(config->jar, NULL);
1233    }
1234 #endif /* def JAR_FILES */
1235
1236    if ( NULL == config->haddr )
1237    {
1238       config->haddr = strdup( HADDR_DEFAULT );
1239    }
1240
1241    if ( NULL != config->haddr )
1242    {
1243       if ((p = strchr(config->haddr, ':')))
1244       {
1245          *p++ = '\0';
1246          if (*p)
1247          {
1248             config->hport = atoi(p);
1249          }
1250       }
1251
1252       if (config->hport <= 0)
1253       {
1254          *--p = ':';
1255          log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1256          /* Never get here - LOG_LEVEL_FATAL causes program exit */
1257       }
1258       if (*config->haddr == '\0')
1259       {
1260          config->haddr = NULL;
1261       }
1262    }
1263
1264    /*
1265     * Want to run all the loaders once now.
1266     *
1267     * Need to set up a fake csp, so they can get to the config.
1268     */
1269    fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1270    fake_csp->config = config;
1271
1272    if (run_loader(fake_csp))
1273    {
1274       freez(fake_csp);
1275       log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1276       /* Never get here - LOG_LEVEL_FATAL causes program exit */
1277    }
1278    freez(fake_csp);
1279
1280 #ifndef SPLIT_PROXY_ARGS
1281    if (!suppress_blocklists)
1282    {
1283       fs->proxy_args = strsav(fs->proxy_args, "</pre>");
1284    }
1285 #endif /* ndef SPLIT_PROXY_ARGS */
1286
1287 /* FIXME: this is a kludge for win32 */
1288 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1289
1290    g_actions_file     = config->actions_file;
1291 #ifdef PCRS
1292    g_re_filterfile    = config->re_filterfile;
1293 #endif
1294 #ifdef TRUST_FILES
1295    g_trustfile        = config->trustfile;
1296 #endif
1297    
1298
1299 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1300 /* FIXME: end kludge */
1301
1302
1303    config->need_bind = 1;
1304
1305    if (current_configfile)
1306    {
1307       struct configuration_spec * oldcfg = (struct configuration_spec *)
1308                                            current_configfile->f;
1309       /*
1310        * Check if config->haddr,hport == oldcfg->haddr,hport
1311        *
1312        * The following could be written more compactly as a single,
1313        * (unreadably long) if statement.
1314        */
1315       config->need_bind = 0;
1316       if (config->hport != oldcfg->hport)
1317       {
1318          config->need_bind = 1;
1319       }
1320       else if (config->haddr == NULL)
1321       {
1322          if (oldcfg->haddr != NULL)
1323          {
1324             config->need_bind = 1;
1325          }
1326       }
1327       else if (oldcfg->haddr == NULL)
1328       {
1329          config->need_bind = 1;
1330       }
1331       else if (0 != strcmp(config->haddr, oldcfg->haddr))
1332       {
1333          config->need_bind = 1;
1334       }
1335
1336       current_configfile->unloader = unload_configfile;
1337    }
1338
1339    fs->next = files->next;
1340    files->next = fs;
1341
1342    current_configfile = fs;
1343
1344    return (config);
1345 }
1346
1347
1348 /*
1349   Local Variables:
1350   tab-width: 3
1351   end:
1352 */