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