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