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