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