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