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