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