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