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