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