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