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