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