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