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