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