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