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