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