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