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