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