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