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