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