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