Synced with the stable branch:
[privoxy.git] / src / loadcfg.c
1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 2.1 2002/06/04 17:22:36 jongfoster Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa//current/src/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  *                Privoxy team. http://www.privoxy.org/
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 2.1  2002/06/04 17:22:36  jongfoster
39  *    Adding comments
40  *
41  *    Revision 2.0  2002/06/04 14:34:21  jongfoster
42  *    Moving source files to src/
43  *
44  *    Revision 1.48  2002/05/14 21:30:38  oes
45  *    savearg now uses own linking code instead of (now special-cased) add_help_link
46  *
47  *    Revision 1.47  2002/05/12 21:36:29  jongfoster
48  *    Correcting function comments
49  *
50  *    Revision 1.46  2002/04/26 12:55:14  oes
51  *     - New option "user-manual", defaults to our site
52  *       via project.h #define
53  *     - savearg now embeds option names in help links
54  *
55  *    Revision 1.45  2002/04/24 02:11:54  oes
56  *    Jon's multiple AF patch: Allow up to MAX_ACTION_FILES actionsfile options
57  *
58  *    Revision 1.44  2002/04/08 20:37:13  swa
59  *    fixed JB spelling
60  *
61  *    Revision 1.43  2002/04/08 20:36:50  swa
62  *    fixed JB spelling
63  *
64  *    Revision 1.42  2002/04/05 15:50:15  oes
65  *    fix for invalid HTML proxy_args
66  *
67  *    Revision 1.41  2002/03/31 17:19:00  jongfoster
68  *    Win32 only: Enabling STRICT to fix a VC++ compile warning.
69  *
70  *    Revision 1.40  2002/03/26 22:29:55  swa
71  *    we have a new homepage!
72  *
73  *    Revision 1.39  2002/03/24 13:25:43  swa
74  *    name change related issues
75  *
76  *    Revision 1.38  2002/03/24 13:05:48  jongfoster
77  *    Renaming re_filterfile to filterfile
78  *
79  *    Revision 1.37  2002/03/16 23:54:06  jongfoster
80  *    Adding graceful termination feature, to help look for memory leaks.
81  *    If you enable this (which, by design, has to be done by hand
82  *    editing config.h) and then go to http://i.j.b/die, then the program
83  *    will exit cleanly after the *next* request.  It should free all the
84  *    memory that was used.
85  *
86  *    Revision 1.36  2002/03/13 00:27:05  jongfoster
87  *    Killing warnings
88  *
89  *    Revision 1.35  2002/03/07 03:52:44  oes
90  *    Set logging to tty for --no-daemon mode
91  *
92  *    Revision 1.34  2002/03/06 23:14:35  jongfoster
93  *    Trivial cosmetic changes to make function comments easier to find.
94  *
95  *    Revision 1.33  2002/03/05 04:52:42  oes
96  *    Deleted non-errlog debugging code
97  *
98  *    Revision 1.32  2002/03/04 18:24:53  oes
99  *    Re-enabled output of unknown config directive hash
100  *
101  *    Revision 1.31  2002/03/03 15:07:20  oes
102  *    Re-enabled automatic config reloading
103  *
104  *    Revision 1.30  2002/01/22 23:31:43  jongfoster
105  *    Replacing strsav() with string_append()
106  *
107  *    Revision 1.29  2002/01/17 21:02:30  jongfoster
108  *    Moving all our URL and URL pattern parsing code to urlmatch.c.
109  *
110  *    Renaming free_url to free_url_spec, since it frees a struct url_spec.
111  *
112  *    Revision 1.28  2001/12/30 14:07:32  steudten
113  *    - Add signal handling (unix)
114  *    - Add SIGHUP handler (unix)
115  *    - Add creation of pidfile (unix)
116  *    - Add action 'top' in rc file (RH)
117  *    - Add entry 'SIGNALS' to manpage
118  *    - Add exit message to logfile (unix)
119  *
120  *    Revision 1.27  2001/11/07 00:02:13  steudten
121  *    Add line number in error output for lineparsing for
122  *    actionsfile and configfile.
123  *    Special handling for CLF added.
124  *
125  *    Revision 1.26  2001/11/05 21:41:43  steudten
126  *    Add changes to be a real daemon just for unix os.
127  *    (change cwd to /, detach from controlling tty, set
128  *    process group and session leader to the own process.
129  *    Add DBG() Macro.
130  *    Add some fatal-error log message for failed malloc().
131  *    Add '-d' if compiled with 'configure --with-debug' to
132  *    enable debug output.
133  *
134  *    Revision 1.25  2001/10/25 03:40:48  david__schmidt
135  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
136  *    threads to call select() simultaneously.  So, it's time to do a real, live,
137  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
138  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
139  *
140  *    Revision 1.24  2001/10/23 21:40:30  jongfoster
141  *    Added support for enable-edit-actions and enable-remote-toggle config
142  *    file options.
143  *
144  *    Revision 1.23  2001/10/07 15:36:00  oes
145  *    Introduced new config option "buffer-limit"
146  *
147  *    Revision 1.22  2001/09/22 16:36:59  jongfoster
148  *    Removing unused parameter fs from read_config_line()
149  *
150  *    Revision 1.21  2001/09/16 17:10:43  jongfoster
151  *    Moving function savearg() here, since it was the only thing left in
152  *    showargs.c.
153  *
154  *    Revision 1.20  2001/07/30 22:08:36  jongfoster
155  *    Tidying up #defines:
156  *    - All feature #defines are now of the form FEATURE_xxx
157  *    - Permanently turned off WIN_GUI_EDIT
158  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
159  *
160  *    Revision 1.19  2001/07/15 17:45:16  jongfoster
161  *    Removing some unused #includes
162  *
163  *    Revision 1.18  2001/07/13 14:01:14  oes
164  *     - Removed all #ifdef PCRS
165  *     - Removed vim-settings
166  *
167  *    Revision 1.17  2001/06/29 13:31:03  oes
168  *    - Improved comments
169  *    - Fixed (actionsfile) and sorted hashes
170  *    - Introduced admin_address and proxy-info-url
171  *      as config parameters
172  *    - Renamed config->proxy_args_invocation (which didn't have
173  *      the invocation but the options!) to config->proxy_args
174  *    - Various adaptions
175  *    - Removed logentry from cancelled commit
176  *
177  *    Revision 1.16  2001/06/09 10:55:28  jongfoster
178  *    Changing BUFSIZ ==> BUFFER_SIZE
179  *
180  *    Revision 1.15  2001/06/07 23:13:40  jongfoster
181  *    Merging ACL and forward files into config file.
182  *    Cosmetic: Sorting config file options alphabetically.
183  *    Cosmetic: Adding brief syntax comments to config file options.
184  *
185  *    Revision 1.14  2001/06/07 14:46:25  joergs
186  *    Missing make_path() added for re_filterfile.
187  *
188  *    Revision 1.13  2001/06/05 22:33:54  jongfoster
189  *
190  *    Fixed minor memory leak.
191  *    Also now uses make_path to prepend the pathnames.
192  *
193  *    Revision 1.12  2001/06/05 20:04:09  jongfoster
194  *    Now uses _snprintf() in place of snprintf() under Win32.
195  *
196  *    Revision 1.11  2001/06/04 18:31:58  swa
197  *    files are now prefixed with either `confdir' or `logdir'.
198  *    `make redhat-dist' replaces both entries confdir and logdir
199  *    with redhat values
200  *
201  *    Revision 1.10  2001/06/03 19:11:54  oes
202  *    introduced confdir option
203  *
204  *    Revision 1.9  2001/06/01 20:06:24  jongfoster
205  *    Removed support for "tinygif" option - moved to actions file.
206  *
207  *    Revision 1.8  2001/05/31 21:27:13  jongfoster
208  *    Removed many options from the config file and into the
209  *    "actions" file: add_forwarded, suppress_vanilla_wafer,
210  *    wafer, add_header, user_agent, referer, from
211  *    Also globally replaced "permission" with "action".
212  *
213  *    Revision 1.7  2001/05/29 09:50:24  jongfoster
214  *    Unified blocklist/imagelist/permissionslist.
215  *    File format is still under discussion, but the internal changes
216  *    are (mostly) done.
217  *
218  *    Also modified interceptor behaviour:
219  *    - We now intercept all URLs beginning with one of the following
220  *      prefixes (and *only* these prefixes):
221  *        * http://i.j.b/
222  *        * http://ijbswa.sf.net/config/
223  *        * http://ijbswa.sourceforge.net/config/
224  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
225  *    - Internal changes so that intercepted and fast redirect pages
226  *      are not replaced with an image.
227  *    - Interceptors now have the option to send a binary page direct
228  *      to the client. (i.e. ijb-send-banner uses this)
229  *    - Implemented show-url-info interceptor.  (Which is why I needed
230  *      the above interceptors changes - a typical URL is
231  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
232  *      The previous mechanism would not have intercepted that, and
233  *      if it had been intercepted then it then it would have replaced
234  *      it with an image.)
235  *
236  *    Revision 1.6  2001/05/26 00:28:36  jongfoster
237  *    Automatic reloading of config file.
238  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
239  *    Most of the global variables have been moved to a new
240  *    struct configuration_spec, accessed through csp->config->globalname
241  *    Most of the globals remaining are used by the Win32 GUI.
242  *
243  *    Revision 1.5  2001/05/25 22:34:30  jongfoster
244  *    Hard tabs->Spaces
245  *
246  *    Revision 1.4  2001/05/22 18:46:04  oes
247  *
248  *    - Enabled filtering banners by size rather than URL
249  *      by adding patterns that replace all standard banner
250  *      sizes with the "Junkbuster" gif to the re_filterfile
251  *
252  *    - Enabled filtering WebBugs by providing a pattern
253  *      which kills all 1x1 images
254  *
255  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
256  *      which is selected by the (nonstandard and therefore
257  *      capital) letter 'U' in the option string.
258  *      It causes the quantifiers to be ungreedy by default.
259  *      Appending a ? turns back to greedy (!).
260  *
261  *    - Added a new interceptor ijb-send-banner, which
262  *      sends back the "Junkbuster" gif. Without imagelist or
263  *      MSIE detection support, or if tinygif = 1, or the
264  *      URL isn't recognized as an imageurl, a lame HTML
265  *      explanation is sent instead.
266  *
267  *    - Added new feature, which permits blocking remote
268  *      script redirects and firing back a local redirect
269  *      to the browser.
270  *      The feature is conditionally compiled, i.e. it
271  *      can be disabled with --disable-fast-redirects,
272  *      plus it must be activated by a "fast-redirects"
273  *      line in the config file, has its own log level
274  *      and of course wants to be displayed by show-proxy-args
275  *      Note: Boy, all the #ifdefs in 1001 locations and
276  *      all the fumbling with configure.in and acconfig.h
277  *      were *way* more work than the feature itself :-(
278  *
279  *    - Because a generic redirect template was needed for
280  *      this, tinygif = 3 now uses the same.
281  *
282  *    - Moved GIFs, and other static HTTP response templates
283  *      to project.h
284  *
285  *    - Some minor fixes
286  *
287  *    - Removed some >400 CRs again (Jon, you really worked
288  *      a lot! ;-)
289  *
290  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
291  *    Version 2.9.4 checkin.
292  *    - Merged popupfile and cookiefile, and added control over PCRS
293  *      filtering, in new "permissionsfile".
294  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
295  *      file error you now get a message box (in the Win32 GUI) rather
296  *      than the program exiting with no explanation.
297  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
298  *      skipping.
299  *    - Removed tabs from "config"
300  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
301  *    - Bumped up version number.
302  *
303  *    Revision 1.2  2001/05/17 23:01:01  oes
304  *     - Cleaned CRLF's from the sources and related files
305  *
306  *    Revision 1.1.1.1  2001/05/15 13:58:58  oes
307  *    Initial import of version 2.9.3 source tree
308  *
309  *
310  *********************************************************************/
311 \f
312
313 #include "config.h"
314
315 #include <stdio.h>
316 #include <sys/types.h>
317 #include <stdlib.h>
318 #include <string.h>
319 #include <signal.h>
320 #include <fcntl.h>
321 #include <errno.h>
322 #include <ctype.h>
323 #include <assert.h>
324
325 #ifdef _WIN32
326
327 # ifndef STRICT
328 #  define STRICT
329 # endif
330 # include <windows.h>
331
332 # include "win32.h"
333 # ifndef _WIN_CONSOLE
334 #  include "w32log.h"
335 # endif /* ndef _WIN_CONSOLE */
336
337 /* VC++ has "_snprintf", not "snprintf" */
338 #define snprintf _snprintf
339
340 #else /* ifndef _WIN32 */
341
342 #ifndef __OS2__
343 # include <unistd.h>
344 # include <sys/wait.h>
345 #endif
346 # include <sys/time.h>
347 # include <sys/stat.h>
348 # include <signal.h>
349
350 #endif
351
352 #include "loadcfg.h"
353 #include "list.h"
354 #include "jcc.h"
355 #include "filters.h"
356 #include "loaders.h"
357 #include "miscutil.h"
358 #include "errlog.h"
359 #include "ssplit.h"
360 #include "encode.h"
361 #include "urlmatch.h"
362 #include "cgi.h"
363
364 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
365
366 /*
367  * Fix a problem with Solaris.  There should be no effect on other
368  * platforms.
369  * Solaris's isspace() is a macro which uses it's argument directly
370  * as an array index.  Therefore we need to make sure that high-bit
371  * characters generate +ve values, and ideally we also want to make
372  * the argument match the declared parameter type of "int".
373  */
374 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
375 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
376
377 #ifdef FEATURE_TOGGLE
378 /**
379  * Indicates if Privoxy is currently enabled.
380  */
381 int g_bToggleIJB        = 1;   /* Privoxy is enabled by default. */
382 #endif /* def FEATURE_TOGGLE */
383
384 /**
385  * The filename of the config file
386  */
387 const char *configfile  = NULL;
388
389 /**
390  * Global argument count, so that CGI functions can access the
391  * command line.
392  */
393 int Argc = 0;
394
395 /**
396  * Global argument list, so that CGI functions can access the
397  * command line.
398  */
399 const char **Argv = NULL;
400
401 /**
402  * The configuration file which is currently loaded.
403  */
404 static struct file_list *current_configfile = NULL;
405
406
407 /*
408  * This takes the "cryptic" hash of each keyword and aliases them to
409  * something a little more readable.  This also makes changing the
410  * hash values easier if they should change or the hash algorthm changes.
411  * Use the included "hash" program to find out what the hash will be
412  * for any string supplied on the command line.  (Or just put it in the
413  * config file and read the number from the error message in the log).
414  *
415  * Please keep this list sorted alphabetically (but with the Windows
416  * console and GUI specific options last).
417  */
418
419 #define hash_actions_file              1196306641ul /**< "actionsfile" */
420 #define hash_admin_address             4112573064ul /**< "admin-address" */
421 #define hash_buffer_limit              1881726070ul /**< "buffer-limit */
422 #define hash_confdir                      1978389ul /**< "confdir" */
423 #define hash_debug                          78263ul /**< "debug" */
424 #define hash_deny_access               1227333715ul /**< "deny-access" */
425 #define hash_enable_edit_actions       2517097536ul /**< "enable-edit-actions" */
426 #define hash_enable_remote_toggle      2979744683ul /**< "enable-remote-toggle" */
427 #define hash_filterfile                 250887266ul /**< "filterfile" */
428 #define hash_forward                      2029845ul /**< "forward" */
429 #define hash_forward_socks4            3963965521ul /**< "forward-socks4" */
430 #define hash_forward_socks4a           2639958518ul /**< "forward-socks4a" */
431 #define hash_jarfile                      2046641ul /**< "jarfile" */
432 #define hash_listen_address            1255650842ul /**< "listen-address" */
433 #define hash_logdir                        422889ul /**< "logdir" */
434 #define hash_logfile                      2114766ul /**< "logfile" */
435 #define hash_permit_access             3587953268ul /**< "permit-access" */
436 #define hash_proxy_info_url            3903079059ul /**< "proxy-info-url" */
437 #define hash_single_threaded           4250084780ul /**< "single-threaded" */
438 #define hash_suppress_blocklists       1948693308ul /**< "suppress-blocklists" */
439 #define hash_toggle                        447966ul /**< "toggle" */
440 #define hash_trust_info_url             430331967ul /**< "trust-info-url" */
441 #define hash_trustfile                   56494766ul /**< "trustfile" */
442 #define hash_usermanual                1416668518ul /**< "user-manual" */
443
444 #define hash_activity_animation        1817904738ul /**< "activity-animation" */
445 #define hash_close_button_minimizes    3651284693ul /**< "close-button-minimizes" */
446 #define hash_hide_console              2048809870ul /**< "hide-console" */
447 #define hash_log_buffer_size           2918070425ul /**< "log-buffer-size" */
448 #define hash_log_font_name             2866730124ul /**< "log-font-name" */
449 #define hash_log_font_size             2866731014ul /**< "log-font-size" */
450 #define hash_log_highlight_messages    4032101240ul /**< "log-highlight-messages" */
451 #define hash_log_max_lines             2868344173ul /**< "log-max-lines" */
452 #define hash_log_messages              2291744899ul /**< "log-messages" */
453 #define hash_show_on_task_bar           215410365ul /**< "show-on-task-bar" */
454
455
456 static void savearg(char *command, char *argument, struct configuration_spec * config);
457
458 /*********************************************************************
459  *
460  * Function    :  unload_configfile
461  *
462  * Description :  Free the config structure and all components.
463  *
464  * Parameters  :
465  *          1  :  data: struct configuration_spec to unload
466  *
467  * Returns     :  N/A
468  *
469  *********************************************************************/
470 void unload_configfile (void * data)
471 {
472    struct configuration_spec * config = (struct configuration_spec *)data;
473    struct forward_spec *cur_fwd = config->forward;
474 #ifdef FEATURE_ACL
475    struct access_control_list *cur_acl = config->acl;
476    int i;
477
478    while (cur_acl != NULL)
479    {
480       struct access_control_list * next_acl = cur_acl->next;
481       free(cur_acl);
482       cur_acl = next_acl;
483    }
484    config->acl = NULL;
485 #endif /* def FEATURE_ACL */
486
487    while (cur_fwd != NULL)
488    {
489       struct forward_spec * next_fwd = cur_fwd->next;
490       free_url_spec(cur_fwd->url);
491
492       freez(cur_fwd->gateway_host);
493       freez(cur_fwd->forward_host);
494       free(cur_fwd);
495       cur_fwd = next_fwd;
496    }
497    config->forward = NULL;
498
499 #ifdef FEATURE_COOKIE_JAR
500    if ( NULL != config->jar )
501    {
502       fclose( config->jar );
503       config->jar = NULL;
504    }
505 #endif /* def FEATURE_COOKIE_JAR */
506
507    freez(config->confdir);
508    freez(config->logdir);
509
510    freez(config->haddr);
511    freez(config->logfile);
512
513    for (i = 0; i < MAX_ACTION_FILES; i++)
514    {
515       freez(config->actions_file_short[i]);
516       freez(config->actions_file[i]);
517    }
518
519    freez(config->admin_address);
520    freez(config->proxy_info_url);
521    freez(config->proxy_args);
522    freez(config->usermanual);
523
524 #ifdef FEATURE_COOKIE_JAR
525    freez(config->jarfile);
526 #endif /* def FEATURE_COOKIE_JAR */
527
528    freez(config->re_filterfile);
529
530 }
531
532
533 #ifdef FEATURE_GRACEFUL_TERMINATION
534 /*********************************************************************
535  *
536  * Function    :  unload_current_config_file
537  *
538  * Description :  Unloads current config file - reset to state at
539  *                beginning of program.
540  *
541  * Parameters  :  None
542  *
543  * Returns     :  N/A
544  *
545  *********************************************************************/
546 void unload_current_config_file(void)
547 {
548    if (current_configfile)
549    {
550       current_configfile->unloader = unload_configfile;
551       current_configfile = NULL;
552    }
553 }
554 #endif
555
556
557 /*********************************************************************
558  *
559  * Function    :  load_config
560  *
561  * Description :  Load the config file and all parameters.
562  *
563  * Parameters  :  None
564  *
565  * Returns     :  The configuration_spec, or NULL on error.
566  *
567  *********************************************************************/
568 struct configuration_spec * load_config(void)
569 {
570    char buf[BUFFER_SIZE];
571    char *p, *q;
572    FILE *configfp = NULL;
573    struct configuration_spec * config = NULL;
574    struct client_state * fake_csp;
575    struct file_list *fs;
576    unsigned long linenum = 0;
577    int i;
578
579    if ( !check_file_changed(current_configfile, configfile, &fs))
580    {
581       /* No need to load */
582       return ((struct configuration_spec *)current_configfile->f);
583    }
584    if (!fs)
585    {
586       log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s':  %E",
587                 configfile);
588    }
589
590    log_error(LOG_LEVEL_INFO, "loading configuration file '%s':", configfile);
591
592 #ifdef FEATURE_TOGGLE
593    g_bToggleIJB      = 1;
594 #endif /* def FEATURE_TOGGLE */
595
596    fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
597
598    if (config==NULL)
599    {
600       freez(fs->filename);
601       freez(fs);
602       log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
603       /* Never get here - LOG_LEVEL_FATAL causes program exit */
604    }
605
606    /*
607     * This is backwards from how it's usually done.
608     * Following the usual pattern, "fs" would be stored in a member
609     * variable in "csp", and then we'd access "config" from "fs->f",
610     * using a cast.  However, "config" is used so often that a
611     * cast each time would be very ugly, and the extra indirection
612     * would waste CPU cycles.  Therefore we store "config" in
613     * "csp->config", and "fs" in "csp->config->config_file_list".
614     */
615    config->config_file_list = fs;
616
617    /*
618     * Set to defaults
619     */
620    config->multi_threaded    = 1;
621    config->hport             = HADDR_PORT;
622    config->buffer_limit      = 4096 * 1024;
623    config->usermanual        = strdup(USER_MANUAL_URL);
624    config->proxy_args        = strdup("");
625
626    if ((configfp = fopen(configfile, "r")) == NULL)
627    {
628       log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s':  %E",
629               configfile);
630       /* Never get here - LOG_LEVEL_FATAL causes program exit */
631    }
632
633    while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
634    {
635       char cmd[BUFFER_SIZE];
636       char arg[BUFFER_SIZE];
637       char tmp[BUFFER_SIZE];
638 #ifdef FEATURE_ACL
639       struct access_control_list *cur_acl;
640 #endif /* def FEATURE_ACL */
641       struct forward_spec *cur_fwd;
642       int vec_count;
643       char *vec[3];
644
645       strcpy(tmp, buf);
646
647       /* Copy command (i.e. up to space or tab) into cmd */
648       p = buf;
649       q = cmd;
650       while (*p && (*p != ' ') && (*p != '\t'))
651       {
652          *q++ = *p++;
653       }
654       *q = '\0';
655
656       /* Skip over the whitespace in buf */
657       while (*p && ((*p == ' ') || (*p == '\t')))
658       {
659          p++;
660       }
661
662       /* Copy the argument into arg */
663       strcpy(arg, p);
664
665       /* Should never happen, but check this anyway */
666       if (*cmd == '\0')
667       {
668          continue;
669       }
670
671       /* Make sure the command field is lower case */
672       for (p=cmd; *p; p++)
673       {
674          if (ijb_isupper(*p))
675          {
676             *p = ijb_tolower(*p);
677          }
678       }
679
680       /* Save the argument for show-proxy-args */
681       savearg(cmd, arg, config);
682
683
684       switch( hash_string( cmd ) )
685       {
686 /* *************************************************************************
687  * actionsfile actions-file-name
688  * In confdir by default
689  * *************************************************************************/
690          case hash_actions_file :
691             i = 0;
692             while ((i < MAX_ACTION_FILES) && (NULL != config->actions_file[i]))
693             {
694                i++;
695             }
696
697             if (i >= MAX_ACTION_FILES)
698             {
699                log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
700                   "(You can increase this limit by changing MAX_ACTION_FILES in project.h and recompiling).",
701                   MAX_ACTION_FILES);
702             }
703             config->actions_file_short[i] = strdup(arg);
704             p = malloc(strlen(arg) + sizeof(".action"));
705             if (p == NULL)
706             {
707                log_error(LOG_LEVEL_FATAL, "Out of memory");
708             }
709             strcpy(p, arg);
710             strcat(p, ".action");
711             config->actions_file[i] = make_path(config->confdir, p);
712             free(p);
713             continue;
714
715 /* *************************************************************************
716  * admin-address email-address
717  * *************************************************************************/
718          case hash_admin_address :
719             freez(config->admin_address);
720             config->admin_address = strdup(arg);
721             continue;
722
723 /* *************************************************************************
724  * buffer-limit n
725  * *************************************************************************/
726          case hash_buffer_limit :
727             config->buffer_limit = (size_t) 1024 * atoi(arg);
728             continue;
729
730 /* *************************************************************************
731  * confdir directory-name
732  * *************************************************************************/
733          case hash_confdir :
734             freez(config->confdir);
735             config->confdir = make_path( NULL, arg);
736             continue;
737
738 /* *************************************************************************
739  * debug n
740  * Specifies debug level, multiple values are ORed together.
741  * *************************************************************************/
742          case hash_debug :
743             config->debug |= atoi(arg);
744             continue;
745
746 /* *************************************************************************
747  * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
748  * *************************************************************************/
749 #ifdef FEATURE_ACL
750          case hash_deny_access:
751             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
752
753             if ((vec_count != 1) && (vec_count != 2))
754             {
755                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
756                      "deny-access directive in configuration file.");
757                string_append(&config->proxy_args,
758                   "<br>\nWARNING: Wrong number of parameters for "
759                   "deny-access directive in configuration file.<br><br>\n");
760                continue;
761             }
762
763             /* allocate a new node */
764             cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
765
766             if (cur_acl == NULL)
767             {
768                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
769                /* Never get here - LOG_LEVEL_FATAL causes program exit */
770                continue;
771             }
772             cur_acl->action = ACL_DENY;
773
774             if (acl_addr(vec[0], cur_acl->src) < 0)
775             {
776                log_error(LOG_LEVEL_ERROR, "Invalid source IP for deny-access "
777                      "directive in configuration file: \"%s\"", vec[0]);
778                string_append(&config->proxy_args,
779                   "<br>\nWARNING: Invalid source IP for deny-access directive"
780                   " in configuration file: \"");
781                string_append(&config->proxy_args,
782                   vec[0]);
783                string_append(&config->proxy_args,
784                   "\"<br><br>\n");
785                freez(cur_acl);
786                continue;
787             }
788             if (vec_count == 2)
789             {
790                if (acl_addr(vec[1], cur_acl->dst) < 0)
791                {
792                   log_error(LOG_LEVEL_ERROR, "Invalid destination IP for deny-access "
793                         "directive in configuration file: \"%s\"", vec[0]);
794                   string_append(&config->proxy_args,
795                      "<br>\nWARNING: Invalid destination IP for deny-access directive"
796                      " in configuration file: \"");
797                   string_append(&config->proxy_args,
798                      vec[0]);
799                   string_append(&config->proxy_args,
800                      "\"<br><br>\n");
801                   freez(cur_acl);
802                   continue;
803                }
804             }
805
806             /*
807              * Add it to the list.  Note we reverse the list to get the
808              * behaviour the user expects.  With both the ACL and
809              * actions file, the last match wins.  However, the internal
810              * implementations are different:  The actions file is stored
811              * in the same order as the file, and scanned completely.
812              * With the ACL, we reverse the order as we load it, then
813              * when we scan it we stop as soon as we get a match.
814              */
815             cur_acl->next  = config->acl;
816             config->acl = cur_acl;
817
818             continue;
819 #endif /* def FEATURE_ACL */
820
821 /* *************************************************************************
822  * enable-edit-actions 0|1
823  * *************************************************************************/
824 #ifdef FEATURE_CGI_EDIT_ACTIONS
825          case hash_enable_edit_actions:
826             if ((*arg != '\0') && (0 != atoi(arg)))
827             {
828                config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
829             }
830             else
831             {
832                config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
833             }
834             continue;
835 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
836
837 /* *************************************************************************
838  * enable-remote-toggle 0|1
839  * *************************************************************************/
840 #ifdef FEATURE_CGI_EDIT_ACTIONS
841          case hash_enable_remote_toggle:
842             if ((*arg != '\0') && (0 != atoi(arg)))
843             {
844                config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
845             }
846             else
847             {
848                config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
849             }
850             continue;
851 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
852
853 /* *************************************************************************
854  * forward url-pattern (.|http-proxy-host[:port])
855  * *************************************************************************/
856          case hash_forward:
857             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
858
859             if (vec_count != 2)
860             {
861                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
862                      "directive in configuration file.");
863                string_append(&config->proxy_args,
864                   "<br>\nWARNING: Wrong number of parameters for "
865                   "forward directive in configuration file.");
866                continue;
867             }
868
869             /* allocate a new node */
870             cur_fwd = zalloc(sizeof(*cur_fwd));
871             if (cur_fwd == NULL)
872             {
873                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
874                /* Never get here - LOG_LEVEL_FATAL causes program exit */
875                continue;
876             }
877
878             cur_fwd->type = SOCKS_NONE;
879
880             /* Save the URL pattern */
881             if (create_url_spec(cur_fwd->url, vec[0]))
882             {
883                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
884                      "directive in configuration file.");
885                string_append(&config->proxy_args,
886                   "<br>\nWARNING: Bad URL specifier for "
887                   "forward directive in configuration file.");
888                continue;
889             }
890
891             /* Parse the parent HTTP proxy host:port */
892             p = vec[1];
893
894             if (strcmp(p, ".") != 0)
895             {
896                cur_fwd->forward_host = strdup(p);
897
898                if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
899                {
900                   *p++ = '\0';
901                   cur_fwd->forward_port = atoi(p);
902                }
903
904                if (cur_fwd->forward_port <= 0)
905                {
906                   cur_fwd->forward_port = 8000;
907                }
908             }
909
910             /* Add to list. */
911             cur_fwd->next = config->forward;
912             config->forward = cur_fwd;
913
914             continue;
915
916 /* *************************************************************************
917  * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
918  * *************************************************************************/
919          case hash_forward_socks4:
920             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
921
922             if (vec_count != 3)
923             {
924                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
925                      "forward-socks4 directive in configuration file.");
926                string_append(&config->proxy_args,
927                   "<br>\nWARNING: Wrong number of parameters for "
928                   "forward-socks4 directive in configuration file.");
929                continue;
930             }
931
932             /* allocate a new node */
933             cur_fwd = zalloc(sizeof(*cur_fwd));
934             if (cur_fwd == NULL)
935             {
936                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
937                /* Never get here - LOG_LEVEL_FATAL causes program exit */
938                continue;
939             }
940
941             cur_fwd->type = SOCKS_4;
942
943             /* Save the URL pattern */
944             if (create_url_spec(cur_fwd->url, vec[0]))
945             {
946                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
947                      "directive in configuration file.");
948                string_append(&config->proxy_args,
949                   "<br>\nWARNING: Bad URL specifier for "
950                   "forward-socks4 directive in configuration file.");
951                continue;
952             }
953
954             /* Parse the SOCKS proxy host[:port] */
955             p = vec[1];
956
957             if (strcmp(p, ".") != 0)
958             {
959                cur_fwd->gateway_host = strdup(p);
960
961                if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
962                {
963                   *p++ = '\0';
964                   cur_fwd->gateway_port = atoi(p);
965                }
966                if (cur_fwd->gateway_port <= 0)
967                {
968                   cur_fwd->gateway_port = 1080;
969                }
970             }
971
972             /* Parse the parent HTTP proxy host[:port] */
973             p = vec[2];
974
975             if (strcmp(p, ".") != 0)
976             {
977                cur_fwd->forward_host = strdup(p);
978
979                if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
980                {
981                   *p++ = '\0';
982                   cur_fwd->forward_port = atoi(p);
983                }
984
985                if (cur_fwd->forward_port <= 0)
986                {
987                   cur_fwd->forward_port = 8000;
988                }
989             }
990
991             /* Add to list. */
992             cur_fwd->next = config->forward;
993             config->forward = cur_fwd;
994
995             continue;
996
997 /* *************************************************************************
998  * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
999  * *************************************************************************/
1000          case hash_forward_socks4a:
1001             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1002
1003             if (vec_count != 3)
1004             {
1005                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1006                      "forward-socks4a directive in configuration file.");
1007                string_append(&config->proxy_args,
1008                   "<br>\nWARNING: Wrong number of parameters for "
1009                   "forward-socks4a directive in configuration file.");
1010                continue;
1011             }
1012
1013             /* allocate a new node */
1014             cur_fwd = zalloc(sizeof(*cur_fwd));
1015             if (cur_fwd == NULL)
1016             {
1017                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1018                /* Never get here - LOG_LEVEL_FATAL causes program exit */
1019                continue;
1020             }
1021
1022             cur_fwd->type = SOCKS_4A;
1023
1024             /* Save the URL pattern */
1025             if (create_url_spec(cur_fwd->url, vec[0]))
1026             {
1027                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
1028                      "directive in configuration file.");
1029                string_append(&config->proxy_args,
1030                   "<br>\nWARNING: Bad URL specifier for "
1031                   "forward-socks4a directive in configuration file.");
1032                continue;
1033             }
1034
1035             /* Parse the SOCKS proxy host[:port] */
1036             p = vec[1];
1037
1038             cur_fwd->gateway_host = strdup(p);
1039
1040             if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1041             {
1042                *p++ = '\0';
1043                cur_fwd->gateway_port = atoi(p);
1044             }
1045             if (cur_fwd->gateway_port <= 0)
1046             {
1047                cur_fwd->gateway_port = 1080;
1048             }
1049
1050             /* Parse the parent HTTP proxy host[:port] */
1051             p = vec[2];
1052
1053             if (strcmp(p, ".") != 0)
1054             {
1055                cur_fwd->forward_host = strdup(p);
1056
1057                if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1058                {
1059                   *p++ = '\0';
1060                   cur_fwd->forward_port = atoi(p);
1061                }
1062
1063                if (cur_fwd->forward_port <= 0)
1064                {
1065                   cur_fwd->forward_port = 8000;
1066                }
1067             }
1068
1069             /* Add to list. */
1070             cur_fwd->next = config->forward;
1071             config->forward = cur_fwd;
1072
1073             continue;
1074
1075 /* *************************************************************************
1076  * jarfile jar-file-name
1077  * In logdir by default
1078  * *************************************************************************/
1079 #ifdef FEATURE_COOKIE_JAR
1080          case hash_jarfile :
1081             freez(config->jarfile);
1082             config->jarfile = make_path(config->logdir, arg);
1083             continue;
1084 #endif /* def FEATURE_COOKIE_JAR */
1085
1086 /* *************************************************************************
1087  * listen-address [ip][:port]
1088  * *************************************************************************/
1089          case hash_listen_address :
1090             freez(config->haddr);
1091             config->haddr = strdup(arg);
1092             continue;
1093
1094 /* *************************************************************************
1095  * logdir directory-name
1096  * *************************************************************************/
1097          case hash_logdir :
1098             freez(config->logdir);
1099             config->logdir = make_path(NULL, arg);
1100             continue;
1101
1102 /* *************************************************************************
1103  * logfile log-file-name
1104  * In logdir by default
1105  * *************************************************************************/
1106          case hash_logfile :
1107             freez(config->logfile);
1108             config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1109             continue;
1110
1111 /* *************************************************************************
1112  * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1113  * *************************************************************************/
1114 #ifdef FEATURE_ACL
1115          case hash_permit_access:
1116             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1117
1118             if ((vec_count != 1) && (vec_count != 2))
1119             {
1120                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1121                      "permit-access directive in configuration file.");
1122                string_append(&config->proxy_args,
1123                   "<br>\nWARNING: Wrong number of parameters for "
1124                   "permit-access directive in configuration file.<br><br>\n");
1125
1126                continue;
1127             }
1128
1129             /* allocate a new node */
1130             cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1131
1132             if (cur_acl == NULL)
1133             {
1134                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1135                /* Never get here - LOG_LEVEL_FATAL causes program exit */
1136                continue;
1137             }
1138             cur_acl->action = ACL_PERMIT;
1139
1140             if (acl_addr(vec[0], cur_acl->src) < 0)
1141             {
1142                log_error(LOG_LEVEL_ERROR, "Invalid source IP for permit-access "
1143                      "directive in configuration file: \"%s\"", vec[0]);
1144                string_append(&config->proxy_args,
1145                   "<br>\nWARNING: Invalid source IP for permit-access directive"
1146                   " in configuration file: \"");
1147                string_append(&config->proxy_args,
1148                   vec[0]);
1149                string_append(&config->proxy_args,
1150                   "\"<br><br>\n");
1151                freez(cur_acl);
1152                continue;
1153             }
1154             if (vec_count == 2)
1155             {
1156                if (acl_addr(vec[1], cur_acl->dst) < 0)
1157                {
1158                   log_error(LOG_LEVEL_ERROR, "Invalid destination IP for "
1159                         "permit-access directive in configuration file: \"%s\"",
1160                         vec[0]);
1161                   string_append(&config->proxy_args,
1162                      "<br>\nWARNING: Invalid destination IP for permit-access directive"
1163                      " in configuration file: \"");
1164                   string_append(&config->proxy_args,
1165                      vec[0]);
1166                   string_append(&config->proxy_args,
1167                      "\"<br><br>\n");
1168                   freez(cur_acl);
1169                   continue;
1170                }
1171             }
1172
1173             /*
1174              * Add it to the list.  Note we reverse the list to get the
1175              * behaviour the user expects.  With both the ACL and
1176              * actions file, the last match wins.  However, the internal
1177              * implementations are different:  The actions file is stored
1178              * in the same order as the file, and scanned completely.
1179              * With the ACL, we reverse the order as we load it, then
1180              * when we scan it we stop as soon as we get a match.
1181              */
1182             cur_acl->next  = config->acl;
1183             config->acl = cur_acl;
1184
1185             continue;
1186 #endif /* def FEATURE_ACL */
1187
1188 /* *************************************************************************
1189  * proxy-info-url url
1190  * *************************************************************************/
1191          case hash_proxy_info_url :
1192             freez(config->proxy_info_url);
1193             config->proxy_info_url = strdup(arg);
1194             continue;
1195
1196 /* *************************************************************************
1197  * re_filterfile file-name
1198  * In confdir by default.
1199  * *************************************************************************/
1200          case hash_filterfile :
1201             freez(config->re_filterfile);
1202             config->re_filterfile = make_path(config->confdir, arg);
1203             continue;
1204
1205 /* *************************************************************************
1206  * single-threaded
1207  * *************************************************************************/
1208          case hash_single_threaded :
1209             config->multi_threaded = 0;
1210             continue;
1211
1212 /* *************************************************************************
1213  * toggle (0|1)
1214  * *************************************************************************/
1215 #ifdef FEATURE_TOGGLE
1216          case hash_toggle :
1217             g_bToggleIJB = atoi(arg);
1218             continue;
1219 #endif /* def FEATURE_TOGGLE */
1220
1221 /* *************************************************************************
1222  * trust-info-url url
1223  * *************************************************************************/
1224 #ifdef FEATURE_TRUST
1225          case hash_trust_info_url :
1226             enlist(config->trust_info, arg);
1227             continue;
1228 #endif /* def FEATURE_TRUST */
1229
1230 /* *************************************************************************
1231  * trustfile filename
1232  * (In confdir by default.)
1233  * *************************************************************************/
1234 #ifdef FEATURE_TRUST
1235          case hash_trustfile :
1236             freez(config->trustfile);
1237             config->trustfile = make_path(config->confdir, arg);
1238             continue;
1239 #endif /* def FEATURE_TRUST */
1240
1241 /* *************************************************************************
1242  * usermanual url
1243  * *************************************************************************/
1244          case hash_usermanual :
1245             freez(config->usermanual);
1246             config->usermanual = strdup(arg);
1247             continue;
1248
1249 /* *************************************************************************
1250  * Win32 Console options:
1251  * *************************************************************************/
1252
1253 /* *************************************************************************
1254  * hide-console
1255  * *************************************************************************/
1256 #ifdef _WIN_CONSOLE
1257          case hash_hide_console :
1258             hideConsole = 1;
1259             continue;
1260 #endif /*def _WIN_CONSOLE*/
1261
1262
1263 /* *************************************************************************
1264  * Win32 GUI options:
1265  * *************************************************************************/
1266
1267 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1268 /* *************************************************************************
1269  * activity-animation (0|1)
1270  * *************************************************************************/
1271          case hash_activity_animation :
1272             g_bShowActivityAnimation = atoi(arg);
1273             continue;
1274
1275 /* *************************************************************************
1276  *  close-button-minimizes (0|1)
1277  * *************************************************************************/
1278          case hash_close_button_minimizes :
1279             g_bCloseHidesWindow = atoi(arg);
1280             continue;
1281
1282 /* *************************************************************************
1283  * log-buffer-size (0|1)
1284  * *************************************************************************/
1285          case hash_log_buffer_size :
1286             g_bLimitBufferSize = atoi(arg);
1287             continue;
1288
1289 /* *************************************************************************
1290  * log-font-name fontnane
1291  * *************************************************************************/
1292          case hash_log_font_name :
1293             strcpy( g_szFontFaceName, arg );
1294             continue;
1295
1296 /* *************************************************************************
1297  * log-font-size n
1298  * *************************************************************************/
1299          case hash_log_font_size :
1300             g_nFontSize = atoi(arg);
1301             continue;
1302
1303 /* *************************************************************************
1304  * log-highlight-messages (0|1)
1305  * *************************************************************************/
1306          case hash_log_highlight_messages :
1307             g_bHighlightMessages = atoi(arg);
1308             continue;
1309
1310 /* *************************************************************************
1311  * log-max-lines n
1312  * *************************************************************************/
1313          case hash_log_max_lines :
1314             g_nMaxBufferLines = atoi(arg);
1315             continue;
1316
1317 /* *************************************************************************
1318  * log-messages (0|1)
1319  * *************************************************************************/
1320          case hash_log_messages :
1321             g_bLogMessages = atoi(arg);
1322             continue;
1323
1324 /* *************************************************************************
1325  * show-on-task-bar (0|1)
1326  * *************************************************************************/
1327          case hash_show_on_task_bar :
1328             g_bShowOnTaskBar = atoi(arg);
1329             continue;
1330
1331 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1332
1333
1334 /* *************************************************************************
1335  * Warnings about unsupported features
1336  * *************************************************************************/
1337 #ifndef FEATURE_ACL
1338          case hash_deny_access:
1339 #endif /* ndef FEATURE_ACL */
1340 #ifndef FEATURE_CGI_EDIT_ACTIONS
1341          case hash_enable_edit_actions:
1342          case hash_enable_remote_toggle:
1343 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1344 #ifndef FEATURE_COOKIE_JAR
1345          case hash_jarfile :
1346 #endif /* ndef FEATURE_COOKIE_JAR */
1347 #ifndef FEATURE_ACL
1348          case hash_permit_access:
1349 #endif /* ndef FEATURE_ACL */
1350 #ifndef FEATURE_TOGGLE
1351          case hash_toggle :
1352 #endif /* ndef FEATURE_TOGGLE */
1353 #ifndef FEATURE_TRUST
1354          case hash_trustfile :
1355          case hash_trust_info_url :
1356 #endif /* ndef FEATURE_TRUST */
1357
1358 #ifndef _WIN_CONSOLE
1359          case hash_hide_console :
1360 #endif /* ndef _WIN_CONSOLE */
1361
1362 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1363          case hash_activity_animation :
1364          case hash_close_button_minimizes :
1365          case hash_log_buffer_size :
1366          case hash_log_font_name :
1367          case hash_log_font_size :
1368          case hash_log_highlight_messages :
1369          case hash_log_max_lines :
1370          case hash_log_messages :
1371          case hash_show_on_task_bar :
1372 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1373             /* These warnings are annoying - so hide them. -- Jon */
1374             /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1375             continue;
1376
1377 /* *************************************************************************/
1378          default :
1379 /* *************************************************************************/
1380             /*
1381              * I decided that I liked this better as a warning than an
1382              * error.  To change back to an error, just change log level
1383              * to LOG_LEVEL_FATAL.
1384              */
1385             log_error(LOG_LEVEL_ERROR, "Unrecognized directive '%s' (%luul) in line %lu in "
1386                   "configuration file (%s).",  buf, hash_string(cmd), linenum, configfile);
1387             string_append(&config->proxy_args, "<br>\nWARNING: unrecognized directive : ");
1388             string_append(&config->proxy_args, buf);
1389             string_append(&config->proxy_args, "<br><br>\n");
1390             continue;
1391
1392 /* *************************************************************************/
1393       } /* end switch( hash_string(cmd) ) */
1394    } /* end while ( read_config_line(...) ) */
1395
1396    fclose(configfp);
1397
1398    if (NULL == config->proxy_args)
1399    {
1400       log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1401    }
1402
1403    init_error_log(Argv[0], config->logfile, config->debug);
1404
1405    if (config->actions_file[0])
1406    {
1407       add_loader(load_actions_file, config);
1408    }
1409
1410    if (config->re_filterfile)
1411    {
1412       add_loader(load_re_filterfile, config);
1413    }
1414
1415 #ifdef FEATURE_TRUST
1416    if (config->trustfile)
1417    {
1418       add_loader(load_trustfile, config);
1419    }
1420 #endif /* def FEATURE_TRUST */
1421
1422 #ifdef FEATURE_COOKIE_JAR
1423    if ( NULL != config->jarfile )
1424    {
1425       if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1426       {
1427          log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1428          /* Never get here - LOG_LEVEL_FATAL causes program exit */
1429       }
1430       setbuf(config->jar, NULL);
1431    }
1432 #endif /* def FEATURE_COOKIE_JAR */
1433
1434    if ( NULL == config->haddr )
1435    {
1436       config->haddr = strdup( HADDR_DEFAULT );
1437    }
1438
1439    if ( NULL != config->haddr )
1440    {
1441       if (NULL != (p = strchr(config->haddr, ':')))
1442       {
1443          *p++ = '\0';
1444          if (*p)
1445          {
1446             config->hport = atoi(p);
1447          }
1448       }
1449
1450       if (config->hport <= 0)
1451       {
1452          *--p = ':';
1453          log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1454          /* Never get here - LOG_LEVEL_FATAL causes program exit */
1455       }
1456       if (*config->haddr == '\0')
1457       {
1458          config->haddr = NULL;
1459       }
1460    }
1461
1462    /*
1463     * Want to run all the loaders once now.
1464     *
1465     * Need to set up a fake csp, so they can get to the config.
1466     */
1467    fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1468    fake_csp->config = config;
1469
1470    if (run_loader(fake_csp))
1471    {
1472       freez(fake_csp);
1473       log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1474       /* Never get here - LOG_LEVEL_FATAL causes program exit */
1475    }
1476    freez(fake_csp);
1477
1478 /* FIXME: this is a kludge for win32 */
1479 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1480
1481    g_default_actions_file  = config->actions_file[1]; /* FIXME Hope this is default.action */
1482    g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1483    g_re_filterfile    = config->re_filterfile;
1484
1485 #ifdef FEATURE_TRUST
1486    g_trustfile        = config->trustfile;
1487 #endif /* def FEATURE_TRUST */
1488
1489
1490 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1491 /* FIXME: end kludge */
1492
1493
1494    config->need_bind = 1;
1495
1496    if (current_configfile)
1497    {
1498       struct configuration_spec * oldcfg = (struct configuration_spec *)
1499                                            current_configfile->f;
1500       /*
1501        * Check if config->haddr,hport == oldcfg->haddr,hport
1502        *
1503        * The following could be written more compactly as a single,
1504        * (unreadably long) if statement.
1505        */
1506       config->need_bind = 0;
1507       if (config->hport != oldcfg->hport)
1508       {
1509          config->need_bind = 1;
1510       }
1511       else if (config->haddr == NULL)
1512       {
1513          if (oldcfg->haddr != NULL)
1514          {
1515             config->need_bind = 1;
1516          }
1517       }
1518       else if (oldcfg->haddr == NULL)
1519       {
1520          config->need_bind = 1;
1521       }
1522       else if (0 != strcmp(config->haddr, oldcfg->haddr))
1523       {
1524          config->need_bind = 1;
1525       }
1526
1527       current_configfile->unloader = unload_configfile;
1528    }
1529
1530    fs->next = files->next;
1531    files->next = fs;
1532
1533    current_configfile = fs;
1534
1535    return (config);
1536 }
1537
1538
1539 /*********************************************************************
1540  *
1541  * Function    :  savearg
1542  *
1543  * Description :  Called from `load_config'.  It saves each non-empty
1544  *                and non-comment line from config into
1545  *                config->proxy_args.  This is used to create the
1546  *                show-proxy-args page.  On error, frees
1547  *                config->proxy_args and sets it to NULL
1548  *
1549  * Parameters  :
1550  *          1  :  command = config setting that was found
1551  *          2  :  argument = the setting's argument (if any)
1552  *          3  :  config = Configuration to save into.
1553  *
1554  * Returns     :  N/A
1555  *
1556  *********************************************************************/
1557 static void savearg(char *command, char *argument, struct configuration_spec * config)
1558 {
1559    char * buf;
1560    char * s;
1561
1562    assert(command);
1563    assert(*command);
1564    assert(argument);
1565
1566    /*
1567     * Add config option name embedded in
1568     * link to it's section in the user-manual
1569     */
1570    buf = strdup("<a href=\"");
1571    string_append(&buf, config->usermanual);
1572    string_append(&buf, CONFIG_HELP_PREFIX);
1573    string_join  (&buf, string_toupper(command));
1574    string_append(&buf, "\">");
1575    string_append(&buf, command);
1576    string_append(&buf, "</a> ");
1577
1578    if (NULL == buf)
1579    {
1580       freez(config->proxy_args);
1581       return;
1582    }
1583
1584    if ( (NULL != argument) && ('\0' != *argument) )
1585    {
1586       s = html_encode(argument);
1587       if (NULL == s)
1588       {
1589          freez(buf);
1590          freez(config->proxy_args);
1591          return;
1592       }
1593
1594       if (strncmpic(argument, "http://", 7) == 0)
1595       {
1596          string_append(&buf, "<a href=\"");
1597          string_append(&buf, s);
1598          string_append(&buf, "\">");
1599          string_join  (&buf, s);
1600          string_append(&buf, "</a>");
1601       }
1602       else
1603       {
1604          string_join  (&buf, s);
1605       }
1606    }
1607
1608    string_append(&buf, "<br>\n");
1609
1610    string_join(&config->proxy_args, buf);
1611 }
1612
1613
1614 /*
1615   Local Variables:
1616   tab-width: 3
1617   end:
1618 */