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