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