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