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