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