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