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