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