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