We need <sys/time.h> for gettimeofday(), not <time.h>.
[privoxy.git] / project.h
1 #ifndef PROJECT_H_INCLUDED
2 #define PROJECT_H_INCLUDED
3 /** Version string. */
4 #define PROJECT_H_VERSION "$Id: project.h,v 1.92 2007/03/17 15:20:05 fabiankeil Exp $"
5 /*********************************************************************
6  *
7  * File        :  $Source: /cvsroot/ijbswa/current/project.h,v $
8  *
9  * Purpose     :  Defines data structures which are widely used in the
10  *                project.  Does not define any variables or functions
11  *                (though it does declare some macros).
12  *
13  * Copyright   :  Written by and Copyright (C) 2001 - 2007 the SourceForge
14  *                Privoxy team. http://www.privoxy.org/
15  *
16  *                Based on the Internet Junkbuster originally written
17  *                by and Copyright (C) 1997 Anonymous Coders and
18  *                Junkbusters Corporation.  http://www.junkbusters.com
19  *
20  *                This program is free software; you can redistribute it
21  *                and/or modify it under the terms of the GNU General
22  *                Public License as published by the Free Software
23  *                Foundation; either version 2 of the License, or (at
24  *                your option) any later version.
25  *
26  *                This program is distributed in the hope that it will
27  *                be useful, but WITHOUT ANY WARRANTY; without even the
28  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
29  *                PARTICULAR PURPOSE.  See the GNU General Public
30  *                License for more details.
31  *
32  *                The GNU General Public License should be included with
33  *                this file.  If not, you can view it at
34  *                http://www.gnu.org/copyleft/gpl.html
35  *                or write to the Free Software Foundation, Inc., 59
36  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
37  *
38  * Revisions   :
39  *    $Log: project.h,v $
40  *    Revision 1.92  2007/03/17 15:20:05  fabiankeil
41  *    New config option: enforce-blocks.
42  *
43  *    Revision 1.91  2007/03/05 13:28:03  fabiankeil
44  *    Add some CSP_FLAGs for the header parsers.
45  *
46  *    Revision 1.90  2007/02/07 10:36:16  fabiankeil
47  *    Add new http_response member to save
48  *    the reason why the response was generated.
49  *
50  *    Revision 1.89  2007/01/27 13:09:16  fabiankeil
51  *    Add new config option "templdir" to
52  *    change the templates directory.
53  *
54  *    Revision 1.88  2007/01/25 13:36:59  fabiankeil
55  *    Add csp->error_message for failure reasons
56  *    that should be embedded into the CGI pages.
57  *
58  *    Revision 1.87  2007/01/01 19:36:37  fabiankeil
59  *    Integrate a modified version of Wil Mahan's
60  *    zlib patch (PR #895531).
61  *
62  *    Revision 1.86  2006/12/31 17:56:37  fabiankeil
63  *    Added config option accept-intercepted-requests
64  *    and disabled it by default.
65  *
66  *    Revision 1.85  2006/12/31 15:03:31  fabiankeil
67  *    Fix gcc43 compiler warnings and a comment.
68  *
69  *    Revision 1.84  2006/12/21 12:57:48  fabiankeil
70  *    Add config option "split-large-forms"
71  *    to work around the browser bug reported
72  *    in BR #1570678.
73  *
74  *    Revision 1.83  2006/12/06 19:26:29  fabiankeil
75  *    Moved HTTP snipplets into jcc.c. They aren't
76  *    used anywhere else.
77  *
78  *    Revision 1.82  2006/09/20 15:50:31  fabiankeil
79  *    Doubled size of HOSTENT_BUFFER_SIZE to mask
80  *    problems with gethostbyname_r and some
81  *    /etc/hosts configurations. Only a workaround
82  *    until we get the real fix ready.
83  *    Thanks FĂ©lix Rauch for reporting.
84  *
85  *    Increased value of MAX_TRUSTED_REFERRERS from 64 to 512.
86  *
87  *    Revision 1.81  2006/09/06 13:03:04  fabiankeil
88  *    Respond with 400 and a short text message
89  *    if the client tries to use Privoxy as FTP proxy.
90  *
91  *    Revision 1.80  2006/09/06 10:43:32  fabiankeil
92  *    Added config option enable-remote-http-toggle
93  *    to specify if Privoxy should recognize special
94  *    headers (currently only X-Filter) to change its
95  *    behaviour. Disabled by default.
96  *
97  *    Revision 1.79  2006/09/06 09:23:37  fabiankeil
98  *    Make number of retries in case of forwarded-connect problems
99  *    a config file option (forwarded-connect-retries) and use 0 as
100  *    default.
101  *
102  *    Revision 1.78  2006/08/31 16:25:06  fabiankeil
103  *    Work around a buffer overflow that caused Privoxy to
104  *    segfault if too many trusted referrers were used. Good
105  *    enough for now, but should be replaced with a real
106  *    solution after the next release.
107  *
108  *    Revision 1.77  2006/08/21 12:50:51  david__schmidt
109  *    Formatting cleanup
110  *
111  *    Revision 1.76  2006/08/14 08:25:19  fabiankeil
112  *    Split filter-headers{} into filter-client-headers{}
113  *    and filter-server-headers{}.
114  *    Added parse_header_time() to share some code.
115  *    Replaced timegm() with mktime().
116  *
117  *    Revision 1.75  2006/08/03 02:46:41  david__schmidt
118  *    Incorporate Fabian Keil's patch work:\r *    http://www.fabiankeil.de/sourcecode/privoxy/
119  *
120  *    Revision 1.74  2006/07/18 14:48:47  david__schmidt
121  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
122  *    with what was really the latest development (the v_3_0_branch branch)
123  *
124  *    Revision 1.72.2.7  2006/01/29 23:10:56  david__schmidt
125  *    Multiple filter file support
126  *
127  *    Revision 1.72.2.6  2004/10/03 12:53:46  david__schmidt
128  *    Add the ability to check jpeg images for invalid
129  *    lengths of comment blocks.  Defensive strategy
130  *    against the exploit:
131  *       Microsoft Security Bulletin MS04-028
132  *       Buffer Overrun in JPEG Processing (GDI+) Could
133  *       Allow Code Execution (833987)
134  *    Enabled with +inspect-jpegs in actions files.
135  *
136  *    Revision 1.72.2.5  2004/01/30 15:29:29  oes
137  *    Updated the copyright note
138  *
139  *    Revision 1.72.2.4  2004/01/13 16:12:14  oes
140  *    Fixed double slash in USER_MANUAL_URL. Closes BR #867088.
141  *
142  *    Revision 1.72.2.3  2003/03/11 11:54:37  oes
143  *    Introduced RC_FLAG_* flags for use in child process return code
144  *
145  *    Revision 1.72.2.2  2002/11/28 18:15:44  oes
146  *    Added flag to each cgi_dispatcher that allows or denies
147  *    external linking and removed const qualifier from
148  *    struct list_entry.str.
149  *
150  *    Revision 1.72.2.1  2002/08/10 11:25:18  oes
151  *    - Include config.h for access to config data
152  *    - Include <pcre*.h> depending on where they are
153  *
154  *    Revision 1.72  2002/05/14 21:35:49  oes
155  *    Split HELP_LINK_PREFIX into ACTIONS_HELP_PREFIX and CONFIG_HELP_PREFIX
156  *    because of split in user-manual
157  *
158  *    Revision 1.71  2002/05/12 21:39:36  jongfoster
159  *    - Adding Doxygen-style comments to structures and #defines.
160  *
161  *    Revision 1.70  2002/05/12 16:05:50  jongfoster
162  *    Fixing ACTION_MASK_ALL to be unsigned long rather than
163  *    just unsigned int.  I don't know if anyone is porting
164  *    Privoxy to 16-bit platforms, but if so, +limit-connect
165  *    wouldn't have worked because of this bug.
166  *
167  *    Revision 1.69  2002/05/08 16:00:16  oes
168  *    Added size member to struct iob, so it can
169  *    be alloced larger than needed.
170  *
171  *    Revision 1.68  2002/04/26 12:56:00  oes
172  *    Killed REDIRECT_URL, added USER_MANUAL_URL and HELP_LINK_PREFIX
173  *
174  *    Revision 1.67  2002/04/24 02:12:43  oes
175  *     - Jon's multiple AF patch:
176  *       - Make csp->actions_list an array
177  *       - #define MAX_AF_FILES
178  *     - Moved CGI_PARAM_LEN_MAX (500) here
179  *
180  *    Revision 1.66  2002/04/15 19:06:43  jongfoster
181  *    Typos
182  *
183  *    Revision 1.65  2002/04/04 00:36:36  gliptak
184  *    always use pcre for matching
185  *
186  *    Revision 1.64  2002/04/03 22:28:03  gliptak
187  *    Removed references to gnu_regex
188  *
189  *    Revision 1.63  2002/03/31 17:19:00  jongfoster
190  *    Win32 only: Enabling STRICT to fix a VC++ compile warning.
191  *
192  *    Revision 1.62  2002/03/26 22:48:49  swa
193  *    new homepage url
194  *
195  *    Revision 1.61  2002/03/26 22:29:55  swa
196  *    we have a new homepage!
197  *
198  *    Revision 1.60  2002/03/24 15:52:17  jongfoster
199  *    Changing CGI URL prefixes for new name
200  *
201  *    Revision 1.59  2002/03/24 15:23:33  jongfoster
202  *    Name changes
203  *
204  *    Revision 1.58  2002/03/24 13:25:43  swa
205  *    name change related issues
206  *
207  *    Revision 1.57  2002/03/16 20:28:34  oes
208  *    Added descriptions to the filters so users will know what they select in the cgi editor
209  *
210  *    Revision 1.56  2002/03/13 20:27:30  oes
211  *    Fixing bug with CT_TABOO
212  *
213  *    Revision 1.55  2002/03/12 01:42:50  oes
214  *    Introduced modular filters
215  *
216  *    Revision 1.54  2002/03/09 20:03:52  jongfoster
217  *    - Making various functions return int rather than size_t.
218  *      (Undoing a recent change).  Since size_t is unsigned on
219  *      Windows, functions like read_socket that return -1 on
220  *      error cannot return a size_t.
221  *
222  *      THIS WAS A MAJOR BUG - it caused frequent, unpredictable
223  *      crashes, and also frequently caused JB to jump to 100%
224  *      CPU and stay there.  (Because it thought it had just
225  *      read ((unsigned)-1) == 4Gb of data...)
226  *
227  *    - The signature of write_socket has changed, it now simply
228  *      returns success=0/failure=nonzero.
229  *
230  *    - Trying to get rid of a few warnings --with-debug on
231  *      Windows, I've introduced a new type "jb_socket".  This is
232  *      used for the socket file descriptors.  On Windows, this
233  *      is SOCKET (a typedef for unsigned).  Everywhere else, it's
234  *      an int.  The error value can't be -1 any more, so it's
235  *      now JB_INVALID_SOCKET (which is -1 on UNIX, and in
236  *      Windows it maps to the #define INVALID_SOCKET.)
237  *
238  *    - The signature of bind_port has changed.
239  *
240  *    Revision 1.53  2002/03/08 16:48:55  oes
241  *    Added FEATURE_NO_GIFS and BUILTIN_IMAGE_MIMETYPE
242  *
243  *    Revision 1.52  2002/03/07 03:46:17  oes
244  *    Fixed compiler warnings
245  *
246  *    Revision 1.51  2002/03/05 04:52:42  oes
247  *    Deleted non-errlog debugging code
248  *
249  *    Revision 1.50  2002/03/04 19:32:07  oes
250  *    Changed default port to 8118
251  *
252  *    Revision 1.49  2002/03/04 18:28:55  oes
253  *    Deleted PID_FILE_NAME
254  *
255  *    Revision 1.48  2002/03/03 14:50:40  oes
256  *    Fixed CLF logging: Added ocmd member for client's request to struct http_request
257  *
258  *    Revision 1.47  2002/02/20 23:15:13  jongfoster
259  *    Parsing functions now handle out-of-memory gracefully by returning
260  *    an error code.
261  *
262  *    Revision 1.46  2002/01/17 21:06:09  jongfoster
263  *    Now #defining the URLs of the config interface
264  *
265  *    Minor changes to struct http_request and struct url_spec due to
266  *    standardizing that struct http_request is used to represent a URL, and
267  *    struct url_spec is used to represent a URL pattern.  (Before, URLs were
268  *    represented as seperate variables and a partially-filled-in url_spec).
269  *
270  *    Revision 1.45  2002/01/09 14:33:27  oes
271  *    Added HOSTENT_BUFFER_SIZE
272  *
273  *    Revision 1.44  2001/12/30 14:07:32  steudten
274  *    - Add signal handling (unix)
275  *    - Add SIGHUP handler (unix)
276  *    - Add creation of pidfile (unix)
277  *    - Add action 'top' in rc file (RH)
278  *    - Add entry 'SIGNALS' to manpage
279  *    - Add exit message to logfile (unix)
280  *
281  *    Revision 1.43  2001/11/22 21:57:51  jongfoster
282  *    Making action_spec->flags into an unsigned long rather than just an
283  *    unsigned int.
284  *    Adding ACTION_NO_COOKIE_KEEP
285  *
286  *    Revision 1.42  2001/11/05 21:42:41  steudten
287  *    Include DBG() macro.
288  *
289  *    Revision 1.41  2001/10/28 19:12:06  jongfoster
290  *    Adding ijb_toupper()
291  *
292  *    Revision 1.40  2001/10/26 17:40:47  oes
293  *    Moved ijb_isspace and ijb_tolower to project.h
294  *    Removed http->user_agent, csp->referrer and csp->accept_types
295  *
296  *    Revision 1.39  2001/10/25 03:45:02  david__schmidt
297  *    Adding a (void*) cast to freez() because Visual Age C++ won't expand the
298  *    macro when called with a cast; so moving the cast to the macro def'n
299  *    seems to both eliminate compiler warnings (on darwin and OS/2, anyway) and
300  *    doesn't make macro expansion complain.  Hope this works for everyone else
301  *    too...
302  *
303  *    Revision 1.38  2001/10/23 21:19:04  jongfoster
304  *    New error-handling support: jb_err type and JB_ERR_xxx constants
305  *    CGI functions now return a jb_err, and their parameters map is const.
306  *    Support for RUNTIME_FEATUREs to enable/disable config editor
307  *    Adding a few comments
308  *
309  *    Revision 1.37  2001/10/14 22:14:01  jongfoster
310  *    Removing name_length field from struct cgi_dispatcher, as this is
311  *    now calculated at runtime from the "name" field.
312  *
313  *    Revision 1.36  2001/10/10 16:45:15  oes
314  *    Added LIMIT_CONNECT action and string
315  *    Fixed HTTP message line termination
316  *    Added CFORBIDDEN HTTP message
317  *
318  *    Revision 1.35  2001/10/07 18:06:43  oes
319  *    Added status member to struct http_request
320  *
321  *    Revision 1.34  2001/10/07 15:45:25  oes
322  *    Added url member to struct http_request and commented all
323  *      members
324  *
325  *    Added CT_TABOO
326  *
327  *    Added ACTION_DOWNGRADE and ACTION_NO_COMPRESSION
328  *
329  *    Replaced struct client_state members rejected,
330  *      force, active and toggled_on with "flags" bitmap.
331  *
332  *    Added CSP_FLAG_MODIFIED and CSP_FLAG_CHUNKED
333  *
334  *    Added buffer_limit to struct configuration_spec
335  *
336  *    Revision 1.33  2001/09/20 13:30:08  steudten
337  *
338  *    Make freez() more secure in case of: if (exp) { free(z) ; a=*z }
339  *    Last case will set z to NULL in free(z) and thats bad..
340  *
341  *    Revision 1.32  2001/09/16 23:02:51  jongfoster
342  *    Fixing warning
343  *
344  *    Revision 1.31  2001/09/16 13:20:29  jongfoster
345  *    Rewrite of list library.  Now has seperate header and list_entry
346  *    structures.  Also added a large sprinking of assert()s to the list
347  *    code.
348  *
349  *    Revision 1.30  2001/09/13 23:52:00  jongfoster
350  *    Support for both static and dynamically generated CGI pages
351  *
352  *    Revision 1.29  2001/09/13 23:29:43  jongfoster
353  *    Defining FORWARD_SPEC_INITIALIZER
354  *
355  *    Revision 1.28  2001/09/13 23:05:50  jongfoster
356  *    Changing the string paramater to the header parsers a "const".
357  *
358  *    Revision 1.27  2001/08/05 16:06:20  jongfoster
359  *    Modifiying "struct map" so that there are now separate header and
360  *    "map_entry" structures.  This means that functions which modify a
361  *    map no longer need to return a pointer to the modified map.
362  *    Also, it no longer reverses the order of the entries (which may be
363  *    important with some advanced template substitutions).
364  *
365  *    Revision 1.26  2001/07/30 22:08:36  jongfoster
366  *    Tidying up #defines:
367  *    - All feature #defines are now of the form FEATURE_xxx
368  *    - Permanently turned off WIN_GUI_EDIT
369  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
370  *
371  *    Revision 1.25  2001/07/29 18:43:08  jongfoster
372  *    Changing #ifdef _FILENAME_H to FILENAME_H_INCLUDED, to conform to
373  *    ANSI C rules.
374  *
375  *    Revision 1.24  2001/07/25 17:20:27  oes
376  *    Introduced http->user_agent
377  *
378  *    Revision 1.23  2001/07/18 12:32:23  oes
379  *    - Added ACTION_STRING_DEANIMATE
380  *    - moved #define freez from jcc.h to project.h
381  *
382  *    Revision 1.22  2001/07/15 17:51:41  jongfoster
383  *    Renaming #define STATIC to STATIC_PCRE
384  *
385  *    Revision 1.21  2001/07/13 14:03:19  oes
386  *     - Reorganized regex header inclusion and #defines to
387  *       comply to the scheme in configure.in
388  *     - Added csp->content_type and its CT_* keys
389  *     - Added ACTION_DEANIMATE
390  *     - Removed all #ifdef PCRS
391  *
392  *    Revision 1.20  2001/06/29 21:45:41  oes
393  *    Indentation, CRLF->LF, Tab-> Space
394  *
395  *    Revision 1.19  2001/06/29 13:33:36  oes
396  *    - Improved comments
397  *    - Introduced http_request.host_ip_addr_str
398  *    - Introduced http_response.head_length
399  *    - Introduced config.my_ip_addr_str, config.my_hostname,
400  *      config.admin_address and config.proxy_info_url
401  *    - Removed config.proxy_args_header and config.proxy_args_trailer,
402  *      renamed config.proxy_args_invocation to config.proxy_args
403  *    - Removed HTML snipplets and GIFs
404  *    - Removed logentry from cancelled commit
405  *
406  *    Revision 1.18  2001/06/09 10:57:39  jongfoster
407  *    Adding definition of BUFFER_SIZE.
408  *    Changing struct cgi_dispatcher to use "const" strings.
409  *
410  *    Revision 1.17  2001/06/07 23:15:09  jongfoster
411  *    Merging ACL and forward files into config file.
412  *    Moving struct gateway members into struct forward_spec
413  *    Removing config->proxy_args_gateways
414  *    Cosmetic: Adding a few comments
415  *
416  *    Revision 1.16  2001/06/04 18:31:58  swa
417  *    files are now prefixed with either `confdir' or `logdir'.
418  *    `make redhat-dist' replaces both entries confdir and logdir
419  *    with redhat values
420  *
421  *    Revision 1.15  2001/06/04 11:28:53  swa
422  *    redirect did not work due to missing /
423  *
424  *    Revision 1.14  2001/06/03 11:03:48  oes
425  *    Added struct map,
426  *    added struct http_response,
427  *    changed struct interceptors to struct cgi_dispatcher,
428  *    moved HTML stuff to cgi.h
429  *
430  *    Revision 1.13  2001/06/01 20:05:36  jongfoster
431  *    Support for +image-blocker{}: added ACTION_IMAGE_BLOCKER
432  *    constant, and removed csp->tinygif.
433  *
434  *    Revision 1.12  2001/06/01 18:49:17  jongfoster
435  *    Replaced "list_share" with "list" - the tiny memory gain was not
436  *    worth the extra complexity.
437  *
438  *    Revision 1.11  2001/06/01 10:32:47  oes
439  *    Added constants for anchoring selection bitmap
440  *
441  *    Revision 1.10  2001/05/31 21:33:53  jongfoster
442  *    Changes for new actions file, replacing permissionsfile
443  *    and parts of the config file.  Also added support for
444  *    list_shared.
445  *
446  *    Revision 1.9  2001/05/31 17:32:31  oes
447  *
448  *     - Enhanced domain part globbing with infix and prefix asterisk
449  *       matching and optional unanchored operation
450  *
451  *    Revision 1.8  2001/05/29 20:09:15  joergs
452  *    HTTP_REDIRECT_TEMPLATE fixed.
453  *
454  *    Revision 1.7  2001/05/29 09:50:24  jongfoster
455  *    Unified blocklist/imagelist/actionslist.
456  *    File format is still under discussion, but the internal changes
457  *    are (mostly) done.
458  *
459  *    Also modified interceptor behaviour:
460  *    - We now intercept all URLs beginning with one of the following
461  *      prefixes (and *only* these prefixes):
462  *        * http://i.j.b/
463  *        * http://ijbswa.sf.net/config/
464  *        * http://ijbswa.sourceforge.net/config/
465  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
466  *    - Internal changes so that intercepted and fast redirect pages
467  *      are not replaced with an image.
468  *    - Interceptors now have the option to send a binary page direct
469  *      to the client. (i.e. ijb-send-banner uses this)
470  *    - Implemented show-url-info interceptor.  (Which is why I needed
471  *      the above interceptors changes - a typical URL is
472  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
473  *      The previous mechanism would not have intercepted that, and
474  *      if it had been intercepted then it then it would have replaced
475  *      it with an image.)
476  *
477  *    Revision 1.6  2001/05/27 22:17:04  oes
478  *
479  *    - re_process_buffer no longer writes the modified buffer
480  *      to the client, which was very ugly. It now returns the
481  *      buffer, which it is then written by chat.
482  *
483  *    - content_length now adjusts the Content-Length: header
484  *      for modified documents rather than crunch()ing it.
485  *      (Length info in csp->content_length, which is 0 for
486  *      unmodified documents)
487  *
488  *    - For this to work, sed() is called twice when filtering.
489  *
490  *    Revision 1.5  2001/05/26 00:28:36  jongfoster
491  *    Automatic reloading of config file.
492  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
493  *    Most of the global variables have been moved to a new
494  *    struct configuration_spec, accessed through csp->config->globalname
495  *    Most of the globals remaining are used by the Win32 GUI.
496  *
497  *    Revision 1.4  2001/05/22 18:46:04  oes
498  *
499  *    - Enabled filtering banners by size rather than URL
500  *      by adding patterns that replace all standard banner
501  *      sizes with the "Junkbuster" gif to the re_filterfile
502  *
503  *    - Enabled filtering WebBugs by providing a pattern
504  *      which kills all 1x1 images
505  *
506  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
507  *      which is selected by the (nonstandard and therefore
508  *      capital) letter 'U' in the option string.
509  *      It causes the quantifiers to be ungreedy by default.
510  *      Appending a ? turns back to greedy (!).
511  *
512  *    - Added a new interceptor ijb-send-banner, which
513  *      sends back the "Junkbuster" gif. Without imagelist or
514  *      MSIE detection support, or if tinygif = 1, or the
515  *      URL isn't recognized as an imageurl, a lame HTML
516  *      explanation is sent instead.
517  *
518  *    - Added new feature, which permits blocking remote
519  *      script redirects and firing back a local redirect
520  *      to the browser.
521  *      The feature is conditionally compiled, i.e. it
522  *      can be disabled with --disable-fast-redirects,
523  *      plus it must be activated by a "fast-redirects"
524  *      line in the config file, has its own log level
525  *      and of course wants to be displayed by show-proxy-args
526  *      Note: Boy, all the #ifdefs in 1001 locations and
527  *      all the fumbling with configure.in and acconfig.h
528  *      were *way* more work than the feature itself :-(
529  *
530  *    - Because a generic redirect template was needed for
531  *      this, tinygif = 3 now uses the same.
532  *
533  *    - Moved GIFs, and other static HTTP response templates
534  *      to project.h
535  *
536  *    - Some minor fixes
537  *
538  *    - Removed some >400 CRs again (Jon, you really worked
539  *      a lot! ;-)
540  *
541  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
542  *    Version 2.9.4 checkin.
543  *    - Merged popupfile and cookiefile, and added control over PCRS
544  *      filtering, in new "actionsfile".
545  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
546  *      file error you now get a message box (in the Win32 GUI) rather
547  *      than the program exiting with no explanation.
548  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
549  *      skipping.
550  *    - Removed tabs from "config"
551  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
552  *    - Bumped up version number.
553  *
554  *    Revision 1.2  2001/05/17 23:01:01  oes
555  *     - Cleaned CRLF's from the sources and related files
556  *
557  *    Revision 1.1.1.1  2001/05/15 13:59:03  oes
558  *    Initial import of version 2.9.3 source tree
559  *
560  *
561  *********************************************************************/
562 \f
563
564 /* Declare struct FILE for vars and funcs. */
565 #include <stdio.h>
566
567 /* Need time_t for file_list */
568 #include <time.h>
569 /* Needed for pcre choice */
570 #include "config.h"
571
572 /*
573  * Include appropriate regular expression libraries.
574  * Note that pcrs and pcre (native) are needed for cgi
575  * and are included anyway.
576  */
577
578 #ifdef STATIC_PCRE
579 #  include "pcre.h"
580 #else
581 #  ifdef PCRE_H_IN_SUBDIR
582 #    include <pcre/pcre.h>
583 #  else
584 #    include <pcre.h>
585 #  endif
586 #endif
587
588 #ifdef STATIC_PCRS
589 #  include "pcrs.h"
590 #else
591 #  include <pcrs.h>
592 #endif
593
594 #ifdef STATIC_PCRE
595 #  include "pcreposix.h"
596 #else
597 #  ifdef PCRE_H_IN_SUBDIR
598 #    include <pcre/pcreposix.h>
599 #  else
600 #    include <pcreposix.h>
601 #  endif
602 #endif
603
604 #ifdef AMIGA
605 #include "amiga.h"
606 #endif /* def AMIGA */
607
608 #ifdef _WIN32
609 /*
610  * I don't want to have to #include all this just for the declaration
611  * of SOCKET.  However, it looks like we have to...
612  */
613 #ifndef STRICT
614 #define STRICT
615 #endif
616 #include <windows.h>
617 #endif
618
619
620 #ifdef __cplusplus
621 extern "C" {
622 #endif
623
624 #ifdef _WIN32
625
626 typedef SOCKET jb_socket;
627
628 #define JB_INVALID_SOCKET INVALID_SOCKET
629
630 #else /* ndef _WIN32 */
631
632 /**
633  * The type used by sockets.  On UNIX it's an int.  Microsoft decided to
634  * make it an unsigned.
635  */
636 typedef int jb_socket;
637
638 /**
639  * The error value used for variables of type jb_socket.  On UNIX this
640  * is -1, however Microsoft decided to make socket handles unsigned, so
641  * they use a different value.
642  */
643
644 #define JB_INVALID_SOCKET (-1)
645
646 #endif /* ndef _WIN32 */
647
648
649 /**
650  * A standard error code.  This should be JB_ERR_OK or one of the JB_ERR_xxx
651  * series of errors.
652  */
653 typedef int jb_err;
654
655 #define JB_ERR_OK         0 /**< Success, no error                        */
656 #define JB_ERR_MEMORY     1 /**< Out of memory                            */
657 #define JB_ERR_CGI_PARAMS 2 /**< Missing or corrupt CGI parameters        */
658 #define JB_ERR_FILE       3 /**< Error opening, reading or writing a file */
659 #define JB_ERR_PARSE      4 /**< Error parsing file                       */
660 #define JB_ERR_MODIFIED   5 /**< File has been modified outside of the  
661                                  CGI actions editor.                      */
662 #define JB_ERR_COMPRESS   6 /**< Error on decompression                   */
663
664 /**
665  * This macro is used to free a pointer that may be NULL.
666  * It also sets the variable to NULL after it's been freed.
667  * The paramater should be a simple variable without side effects.
668  */
669 #define freez(X)  { if(X) { free((void*)X); X = NULL ; } }
670
671
672 /**
673  * Fix a problem with Solaris.  There should be no effect on other
674  * platforms.
675  *
676  * Solaris's isspace() is a macro which uses it's argument directly
677  * as an array index.  Therefore we need to make sure that high-bit
678  * characters generate +ve values, and ideally we also want to make
679  * the argument match the declared parameter type of "int".
680  *
681  * Note: Remember to #include <ctype.h> if you use these macros.
682  */
683 #define ijb_toupper(__X) toupper((int)(unsigned char)(__X))
684 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
685 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))  
686
687 /**
688  * Use for statically allocated buffers if you have no other choice.
689  * Remember to check the length of what you write into the buffer
690  * - we don't want any buffer overflows!
691  */
692 #define BUFFER_SIZE 5000
693
694 /**
695  * Max length of CGI parameters (arbitrary limit).
696  */
697 #define CGI_PARAM_LEN_MAX 500
698
699 /**
700  * Buffer size for capturing struct hostent data in the
701  * gethostby(name|addr)_r library calls. Since we don't
702  * loop over gethostbyname_r, the buffer must be sufficient
703  * to accomodate multiple IN A RRs, as used in DNS round robin
704  * load balancing. W3C's wwwlib uses 1K, so that should be
705  * good enough for us, too.
706  */
707 /**
708  * XXX: Temporary doubled, for some configurations
709  * 1K is still too small and we didn't get the
710  * real fix ready for inclusion.
711  */
712 #define HOSTENT_BUFFER_SIZE 2048
713
714 /**
715  * Do not use.  Originally this was so that you can
716  * say "while (FOREVER) { ...do something... }".
717  * However, this gives a warning with some compilers (e.g. VC++).
718  * Instead, use "for (;;) { ...do something... }".
719  */
720 #define FOREVER 1
721
722 /**
723  * Default IP address to listen on, as a string.
724  * Set to "127.0.0.1".
725  */
726 #define HADDR_DEFAULT   "127.0.0.1"
727
728 /**
729  * Default port to listen on, as a number.
730  * Set to 8118.
731  */
732 #define HADDR_PORT      8118
733
734
735 /* Forward def for struct client_state */
736 struct configuration_spec;
737
738
739 /**
740  * Entry in a linked list of strings.
741  */
742 struct list_entry
743 {
744    /**
745     * The string pointer. It must point to a dynamically malloc()ed
746     * string or be NULL for the list functions to work. In the latter
747     * case, just be careful next time you iterate through the list in
748     * your own code.
749     */
750    char *str;
751    
752    /** Next entry in the linked list, or NULL if no more. */
753    struct list_entry *next;
754 };
755
756 /**
757  * A header for a linked list of strings.
758  */
759 struct list
760 {
761    /** First entry in the list, or NULL if the list is empty. */
762    struct list_entry *first;
763
764    /** Last entry in the list, or NULL if the list is empty. */
765    struct list_entry *last;
766 };
767
768
769 /**
770  * An entry in a map.  This is a name=value pair.
771  */
772 struct map_entry
773 {
774    /** The key for the map. */
775    const char *name;
776    /** The value associated with that key. */
777    const char *value;
778    /** The next map entry, or NULL if none. */
779    struct map_entry *next;
780 };
781
782 /**
783  * A map from a string to another string.
784  * This is used for the paramaters passed in a HTTP GET request, and
785  * to store the exports when the CGI interface is filling in a template.
786  */
787 struct map
788 {
789    /** The first map entry, or NULL if the map is empty. */
790    struct map_entry *first;
791    /** The last map entry, or NULL if the map is empty. */
792    struct map_entry *last;
793 };
794
795
796 /**
797  * A HTTP request.  This includes the method (GET, POST) and
798  * the parsed URL.
799  *
800  * This is also used whenever we want to match a URL against a
801  * URL pattern.  This always contains the URL to match, and never
802  * a URL pattern.  (See struct url_spec).
803  */
804 struct http_request
805 {
806    char *cmd;      /**< Whole command line: method, URL, Version */
807    char *ocmd;     /**< Backup of original cmd for CLF logging */
808    char *gpc;      /**< HTTP method: GET, POST, ... */
809    char *url;      /**< The URL */
810    char *ver;      /**< Protocol version */
811    int status;     /**< HTTP Status */
812
813    char *host;     /**< Host part of URL */
814    int   port;     /**< Port of URL or 80 (default) */
815    char *path;     /**< Path of URL */
816    char *hostport; /**< host[:port] */
817    int   ssl;      /**< Flag if protocol is https */
818
819    char *host_ip_addr_str; /**< String with dotted decimal representation
820                                 of host's IP. NULL before connect_to() */
821
822    char  *dbuffer; /**< Buffer with '\0'-delimited domain name.           */
823    char **dvec;    /**< List of pointers to the strings in dbuffer.       */
824    int    dcount;  /**< How many parts to this domain? (length of dvec)   */
825 };
826
827 /**
828  * Reasons for generating a http_response instead of delivering
829  * the requested resource. Mostly ordered the way they are checked
830  * for in chat().
831  */
832 #define RSP_REASON_UNSUPPORTED        1
833 #define RSP_REASON_BLOCKED            2
834 #define RSP_REASON_UNTRUSTED          3
835 #define RSP_REASON_REDIRECTED         4
836 #define RSP_REASON_CGI_CALL           5
837 #define RSP_REASON_NO_SUCH_DOMAIN     6
838 #define RSP_REASON_FORWARDING_FAILED  7
839 #define RSP_REASON_CONNECT_FAILED     8
840 #define RSP_REASON_OUT_OF_MEMORY      9
841 #define RSP_REASON_INTERNAL_ERROR     10
842
843 /**
844  * Response generated by CGI, blocker, or error handler
845  */
846 struct http_response
847 {
848   char  *status;          /**< HTTP status (string). */
849   struct list headers[1]; /**< List of header lines. */
850   char  *head;            /**< Formatted http response head. */
851   size_t head_length;     /**< Length of http response head. */
852   char  *body;            /**< HTTP document body. */
853   size_t content_length;  /**< Length of body, REQUIRED if binary body. */
854   int    is_static;       /**< Nonzero if the content will never change and
855                                should be cached by the browser (e.g. images). */
856   int reason;             /**< Why the response was generated in the first place. */
857 };
858
859 /**
860  * A URL pattern.
861  */
862 struct url_spec
863 {
864    /** The string which was parsed to produce this url_spec.
865        Used for debugging or display only.  */
866    char  *spec;
867
868    char  *dbuffer;     /**< Buffer with '\0'-delimited domain name, or NULL to match all hosts. */
869    char **dvec;        /**< List of pointers to the strings in dbuffer.       */
870    int    dcount;      /**< How many parts to this domain? (length of dvec)   */
871    int    unanchored;  /**< Bitmap - flags are ANCHOR_LEFT and ANCHOR_RIGHT.  */
872
873    int   port;         /**< The port number, or 0 to match all ports.         */
874
875    char *path;         /**< The source for the regex.                         */
876    size_t pathlen;     /**< ==strlen(path).  Needed for prefix matching.  FIXME: Now obsolete?     */
877    regex_t *preg;      /**< Regex for matching path part                      */
878 };
879
880 /**
881  * If you declare a static url_spec, this is the value to initialize it to zero.
882  */
883 #define URL_SPEC_INITIALIZER { NULL, NULL, NULL, 0, 0, 0, NULL, 0, NULL }
884
885 /**
886  * Constant for host part matching in URLs.  If set, indicates that the start of
887  * the pattern must match the start of the URL.  E.g. this is not set for the
888  * pattern ".example.com", so that it will match both "example.com" and
889  * "www.example.com".  It is set for the pattern "example.com", which makes it
890  * match "example.com" only, not "www.example.com".
891  */
892 #define ANCHOR_LEFT  1
893
894 /**
895  * Constant for host part matching in URLs.  If set, indicates that the end of
896  * the pattern must match the end of the URL.  E.g. this is not set for the
897  * pattern "ad.", so that it will match any host called "ad", irrespective
898  * of how many subdomains are in the fully-qualified domain name.
899  */
900 #define ANCHOR_RIGHT 2
901
902
903 /**
904  * An I/O buffer.  Holds a string which can be appended to, and can have data
905  * removed from the beginning.
906  */
907 struct iob
908 {
909    char *buf;    /**< Start of buffer        */
910    char *cur;    /**< Start of relevant data */
911    char *eod;    /**< End of relevant data   */
912    size_t size;  /**< Size as malloc()ed     */
913 };
914
915
916 /**
917  * Return the number of bytes in the I/O buffer associated with the passed
918  * client_state pointer.
919  * May be zero.
920  */
921 #define IOB_PEEK(CSP) ((CSP->iob->cur > CSP->iob->eod) ? (CSP->iob->eod - CSP->iob->cur) : 0)
922
923
924 /**
925  * Remove any data in the I/O buffer associated with the passed
926  * client_state pointer.
927  */
928 #define IOB_RESET(CSP) if(CSP->iob->buf) free(CSP->iob->buf); memset(CSP->iob, '\0', sizeof(CSP->iob));
929
930 /* Bits for csp->content_type bitmask: */
931 #define CT_TEXT    0x0001U /**< Suitable for pcrs filtering. */
932 #define CT_GIF     0x0002U /**< Suitable for GIF filtering.  */
933 #define CT_TABOO   0x0004U /**< DO NOT filter, irrespective of other flags. */
934 #define CT_JPEG    0x0008U /**< Suitable for JPEG filtering.  */
935
936 /* Although these are not, strictly speaking, content types
937  * (they are content encodings), it is simple to handle them
938  * as such.
939  */
940 #define CT_GZIP    0x0010U /**< gzip-compressed data. */
941 #define CT_DEFLATE 0x0020U /**< zlib-compressed data. */
942
943 /**
944  * The mask which includes all actions.
945  */
946 #define ACTION_MASK_ALL        (~0UL)
947
948 /**
949  * The most compatible set of actions - i.e. none.
950  */
951 #define ACTION_MOST_COMPATIBLE                       0x00000000UL
952
953 /** Action bitmap: Block the request. */
954 #define ACTION_BLOCK                                 0x00000001UL
955 /** Action bitmap: Deanimate if it's a GIF. */
956 #define ACTION_DEANIMATE                             0x00000002UL
957 /** Action bitmap: Downgrade HTTP/1.1 to 1.0. */
958 #define ACTION_DOWNGRADE                             0x00000004UL
959 /** Action bitmap: Fast redirects. */
960 #define ACTION_FAST_REDIRECTS                        0x00000008UL
961 /** Action bitmap: Remove existing "Forwarded" header, and do not add another. */
962 #define ACTION_HIDE_FORWARDED                        0x00000010UL
963 /** Action bitmap: Hide "From" header. */
964 #define ACTION_HIDE_FROM                             0x00000020UL
965 /** Action bitmap: Hide "Referer" header.  (sic - follow HTTP, not English). */
966 #define ACTION_HIDE_REFERER                          0x00000040UL
967 /** Action bitmap: Hide "User-Agent" and similar headers. */
968 #define ACTION_HIDE_USER_AGENT                       0x00000080UL
969 /** Action bitmap: This is an image. */
970 #define ACTION_IMAGE                                 0x00000100UL
971 /** Action bitmap: Sets the image blocker. */
972 #define ACTION_IMAGE_BLOCKER                         0x00000200UL
973 /** Action bitmap: Prevent compression. */
974 #define ACTION_NO_COMPRESSION                        0x00000400UL
975 /** Action bitmap: Change cookies to session only cookies. */
976 #define ACTION_NO_COOKIE_KEEP                        0x00000800UL
977 /** Action bitmap: Block rending cookies. */
978 #define ACTION_NO_COOKIE_READ                        0x00001000UL
979 /** Action bitmap: Block setting cookies. */
980 #define ACTION_NO_COOKIE_SET                         0x00002000UL
981 /** Action bitmap: Filter out popups. */
982 #define ACTION_NO_POPUPS                             0x00004000UL
983 /** Action bitmap: Send a vanilla wafer. */
984 #define ACTION_VANILLA_WAFER                         0x00008000UL
985 /** Action bitmap: Limit CONNECT requests to safe ports. */
986 #define ACTION_LIMIT_CONNECT                         0x00010000UL
987 /** Action bitmap: Inspect if it's a JPEG. */
988 #define ACTION_JPEG_INSPECT                          0x00020000UL
989 /** Action bitmap: Crunch or modify "if-modified-since" header. */
990 #define ACTION_HIDE_IF_MODIFIED_SINCE                0x00040000UL
991 /** Action bitmap: Overwrite Content-Type header. */
992 #define ACTION_CONTENT_TYPE_OVERWRITE                0x00080000UL
993 /** Action bitmap: Crunch specified server header. */
994 #define ACTION_CRUNCH_SERVER_HEADER                  0x00100000UL
995 /** Action bitmap: Crunch specified client header */
996 #define ACTION_CRUNCH_CLIENT_HEADER                  0x00200000UL
997 /** Action bitmap: Enable text mode by force */
998 #define ACTION_FORCE_TEXT_MODE                       0x00400000UL
999 /** Action bitmap: Enable text mode by force */
1000 #define ACTION_CRUNCH_IF_NONE_MATCH                  0x00800000UL
1001 /** Action bitmap: Enable content-dispostion crunching */
1002 #define ACTION_HIDE_CONTENT_DISPOSITION              0x01000000UL
1003 /** Action bitmap: Replace or block Last-Modified header */
1004 #define ACTION_OVERWRITE_LAST_MODIFIED               0x02000000UL
1005 /** Action bitmap: Replace or block Accept-Language header */
1006 #define ACTION_HIDE_ACCEPT_LANGUAGE                  0x04000000UL
1007 /** Action bitmap: Block as empty document */
1008 #define  ACTION_HANDLE_AS_EMPTY_DOCUMENT             0x08000000UL
1009 /** Action bitmap: Redirect request. */
1010 #define  ACTION_REDIRECT                             0x10000000UL
1011 /** Action bitmap: Answer blocked Connects verbosely */
1012 #define ACTION_TREAT_FORBIDDEN_CONNECTS_LIKE_BLOCKS  0x20000000UL
1013 /** Action bitmap: Filter server headers with pcre */
1014 #define ACTION_FILTER_SERVER_HEADERS                 0x40000000UL
1015 /** Action bitmap: Filter client headers with pcre */
1016 #define ACTION_FILTER_CLIENT_HEADERS                 0x80000000UL
1017
1018
1019 /** Action string index: How to deanimate GIFs */
1020 #define ACTION_STRING_DEANIMATE             0
1021 /** Action string index: Replacement for "From:" header */
1022 #define ACTION_STRING_FROM                  1
1023 /** Action string index: How to block images */
1024 #define ACTION_STRING_IMAGE_BLOCKER         2
1025 /** Action string index: Replacement for "Referer:" header */
1026 #define ACTION_STRING_REFERER               3
1027 /** Action string index: Replacement for "User-Agent:" header */
1028 #define ACTION_STRING_USER_AGENT            4
1029 /** Action string index: Legal CONNECT ports. */
1030 #define ACTION_STRING_LIMIT_CONNECT         5
1031 /** Action string index: Server headers containing this pattern are crunched*/
1032 #define ACTION_STRING_SERVER_HEADER         6
1033 /** Action string index: Client headers containing this pattern are crunched*/
1034 #define ACTION_STRING_CLIENT_HEADER         7
1035 /** Action string index: Replacement for the "Accept-Language:" header*/
1036 #define ACTION_STRING_LANGUAGE              8
1037 /** Action string index: Replacement for the "Content-Type:" header*/
1038 #define ACTION_STRING_CONTENT_TYPE          9
1039 /** Action string index: Replacement for the "content-dispostion:" header*/
1040 #define ACTION_STRING_CONTENT_DISPOSITION  10
1041 /** Action string index: Replacement for the "If-Modified-Since:" header*/
1042 #define ACTION_STRING_IF_MODIFIED_SINCE    11
1043 /** Action string index: Replacement for the "Last-Modified:" header. */
1044 #define ACTION_STRING_LAST_MODIFIED        12
1045 /** Action string index: Redirect URL */
1046 #define ACTION_STRING_REDIRECT             13
1047 /** Action string index: Decode before redirect? */
1048 #define ACTION_STRING_FAST_REDIRECTS       14
1049 /** Number of string actions. */
1050 #define ACTION_STRING_COUNT                15
1051
1052
1053 /* To make the ugly hack in sed easier to understand */
1054 #define CHECK_EVERY_HEADER_REMAINING 0
1055
1056
1057 /** Index into current_action_spec::multi[] for headers to add. */
1058 #define ACTION_MULTI_ADD_HEADER              0
1059 /** Index into current_action_spec::multi[] for headers to add. */
1060 #define ACTION_MULTI_WAFER                   1
1061 /** Index into current_action_spec::multi[] for content filters to apply. */
1062 #define ACTION_MULTI_FILTER                  2
1063 /** Index into current_action_spec::multi[] for server-header filters to apply. */
1064 #define ACTION_MULTI_SERVER_HEADER_FILTER    3
1065 /** Index into current_action_spec::multi[] for client-header filters to apply. */
1066 #define ACTION_MULTI_CLIENT_HEADER_FILTER    4
1067 /** Number of multi-string actions. */
1068 #define ACTION_MULTI_COUNT                   5
1069
1070
1071 /**
1072  * This structure contains a list of actions to apply to a URL.
1073  * It only contains positive instructions - no "-" options.
1074  * It is not used to store the actions list itself, only for
1075  * url_actions() to return the current values.
1076  */
1077 struct current_action_spec
1078 {
1079    /** Actions to apply.  A bit set to "1" means perform the action. */
1080    unsigned long flags;
1081
1082    /**
1083     * Paramaters for those actions that require them.
1084     * Each entry is valid if & only if the corresponding entry in "flags" is
1085     * set.
1086     */
1087    char * string[ACTION_STRING_COUNT];
1088
1089    /** Lists of strings for multi-string actions. */
1090    struct list multi[ACTION_MULTI_COUNT][1];
1091 };
1092
1093
1094 /**
1095  * This structure contains a set of changes to actions.
1096  * It can contain both positive and negative instructions.
1097  * It is used to store an entry in the actions list.
1098  */
1099 struct action_spec
1100 {
1101    unsigned long mask; /**< Actions to keep. A bit set to "0" means remove action. */
1102    unsigned long add;  /**< Actions to add.  A bit set to "1" means add action.    */
1103
1104    /**
1105     * Paramaters for those actions that require them.
1106     * Each entry is valid if & only if the corresponding entry in "flags" is
1107     * set.
1108     */
1109    char * string[ACTION_STRING_COUNT];
1110
1111    /** Lists of strings to remove, for multi-string actions. */
1112    struct list multi_remove[ACTION_MULTI_COUNT][1];
1113
1114    /** If nonzero, remove *all* strings from the multi-string action. */
1115    int         multi_remove_all[ACTION_MULTI_COUNT];
1116
1117    /** Lists of strings to add, for multi-string actions. */
1118    struct list multi_add[ACTION_MULTI_COUNT][1];
1119 };
1120
1121
1122 /**
1123  * This structure is used to store the actions list.
1124  *
1125  * It contains a URL pattern, and the chages to the actions.
1126  * It is a linked list.
1127  */
1128 struct url_actions
1129 {
1130    struct url_spec url[1];        /**< URL pattern. */
1131
1132    struct action_spec action[1];  /**< Actions. */
1133
1134    struct url_actions * next;     /**< Next action in file, or NULL. */
1135 };
1136
1137
1138 /*
1139  * Flags for use in csp->flags
1140  */
1141  
1142 /**
1143  * Flag for csp->flags: Set if this client is processing data.
1144  * Cleared when the thread associated with this structure dies.
1145  */
1146 #define CSP_FLAG_ACTIVE     0x01
1147
1148 /**
1149  * Flag for csp->flags: Set if the server's reply is in "chunked"
1150  * transfer encoding
1151  */
1152 #define CSP_FLAG_CHUNKED    0x02
1153
1154 /**
1155  * Flag for csp->flags: Set if this request was enforced, although it would
1156  * normally have been blocked.
1157  */
1158 #define CSP_FLAG_FORCED     0x04
1159
1160 /**
1161  * Flag for csp->flags: Set if any modification to the body was done.
1162  */
1163 #define CSP_FLAG_MODIFIED   0x08
1164
1165 /**
1166  * Flag for csp->flags: Set if request was blocked.
1167  */
1168 #define CSP_FLAG_REJECTED   0x10
1169
1170 /**
1171  * Flag for csp->flags: Set if we are toggled on (FEATURE_TOGGLE).
1172  */
1173 #define CSP_FLAG_TOGGLED_ON 0x20
1174
1175 /**
1176  * Flag for csp->flags: Set if adding the 'Connection: close' header
1177  * for the client isn't necessary.
1178  */
1179 #define CSP_FLAG_CLIENT_CONNECTION_CLOSE_SET   0x00000040UL
1180
1181 /**
1182  * Flag for csp->flags: Set if adding the 'Connection: close' header
1183  * for the server isn't necessary.
1184  */
1185 #define CSP_FLAG_SERVER_CONNECTION_CLOSE_SET   0x00000080UL
1186
1187 /**
1188  * Flag for csp->flags: Signals header parsers whether they
1189  * are parsing server or client headers.
1190  */
1191 #define CSP_FLAG_CLIENT_HEADER_PARSING_DONE    0x00000100UL
1192
1193 /**
1194  * Flag for csp->flags: Set if adding the Host: header
1195  * isn't necessary.
1196  */
1197 #define CSP_FLAG_HOST_HEADER_IS_SET            0x00000200UL
1198
1199
1200 /*
1201  * Flags for use in return codes of child processes
1202  */
1203
1204 /**
1205  * Flag for process return code: Set if exiting porcess has been toggled
1206  * during its lifetime.
1207  */
1208 #define RC_FLAG_TOGGLED   0x10
1209
1210 /**
1211  * Flag for process return code: Set if exiting porcess has blocked its
1212  * request.
1213  */
1214 #define RC_FLAG_BLOCKED   0x20
1215
1216 /**
1217  * Maximum number of actions/filter files.  This limit is arbitrary - it's just used
1218  * to size an array.
1219  */
1220 #define MAX_AF_FILES 10
1221
1222 /**
1223  * The state of a Privoxy processing thread.
1224  */
1225 struct client_state
1226 {
1227    /** The proxy's configuration */
1228    struct configuration_spec * config;
1229
1230    /** The actions to perform on the current request */
1231    struct current_action_spec  action[1];
1232
1233    /** socket to talk to client (web browser) */
1234    jb_socket cfd;
1235
1236    /** socket to talk to server (web server or proxy) */
1237    jb_socket sfd;
1238
1239    /** Multi-purpose flag container, see CSP_FLAG_* above */
1240    unsigned int flags;
1241
1242    /** Client PC's IP address, as reported by the accept() function.
1243        As a string. */
1244    char *ip_addr_str;
1245    /** Client PC's IP address, as reported by the accept() function.
1246        As a number. */
1247    long  ip_addr_long;
1248
1249    /** Our IP address. I.e. the IP address that the client used to reach us,
1250        as a string. */
1251    char *my_ip_addr_str;
1252
1253    /** Our hostname. I.e. the reverse DNS of the IP address that the client
1254        used to reach us, as a string. */
1255    char *my_hostname;
1256
1257    /** The URL that was requested */
1258    struct http_request http[1];
1259
1260    /** An I/O buffer used for buffering data read from the network */
1261    struct iob iob[1];
1262
1263    /** List of all headers for this request */
1264    struct list headers[1];
1265
1266    /** List of all cookies for this request */
1267    struct list cookie_list[1];
1268
1269    /** MIME-Type key, see CT_* above */
1270    unsigned int content_type;
1271
1272    /** The "X-Forwarded-For:" header sent by the client */
1273    char   *x_forwarded;
1274
1275    /** Actions files associated with this client */
1276    struct file_list *actions_list[MAX_AF_FILES];
1277
1278    /** pcrs job files. */
1279    struct file_list *rlist[MAX_AF_FILES];
1280
1281    /** Length after content modification. */
1282    size_t content_length;
1283
1284 #ifdef FEATURE_TRUST
1285
1286    /** Trust file. */
1287    struct file_list *tlist;
1288
1289 #endif /* def FEATURE_TRUST */
1290
1291    /**
1292     * Failure reason to embedded in the CGI error page,
1293     * or NULL. Currently only used for socks errors.
1294     */
1295    char *error_message;
1296
1297    /** Next thread in linked list. Only read or modify from the main thread! */
1298    struct client_state *next;
1299 };
1300
1301
1302 /**
1303  * A function to add a header
1304  */
1305 typedef jb_err (*add_header_func_ptr)(struct client_state *);
1306
1307 /**
1308  * A function to process a header
1309  */
1310 typedef jb_err (*parser_func_ptr    )(struct client_state *, char **);
1311
1312
1313 /**
1314  * List of functions to run on a list of headers
1315  */
1316 struct parsers
1317 {
1318    /** The header prefix to match */
1319    char   *str;
1320    
1321    /** The length of the prefix to match */
1322    size_t len;
1323    
1324    /** The function to apply to this line */
1325    parser_func_ptr parser;
1326 };
1327
1328
1329 /**
1330  * List of available CGI functions.
1331  */
1332 struct cgi_dispatcher
1333 {
1334    /** The URL of the CGI, relative to the CGI root. */
1335    const char * const name;
1336
1337    /** The handler function for the CGI */
1338    jb_err    (* const handler)(struct client_state *csp, struct http_response *rsp, const struct map *parameters);
1339
1340    /** The description of the CGI, to appear on the main menu, or NULL to hide it. */
1341    const char * const description;
1342
1343    /** A flag that indicates whether unintentional calls to this CGI can cause damage */
1344    int harmless;
1345 };
1346
1347
1348 /**
1349  * A data file used by Privoxy.  Kept in a linked list.
1350  */
1351 struct file_list
1352 {
1353    /**
1354     * This is a pointer to the data structures associated with the file.
1355     * Read-only once the structure has been created.
1356     */
1357    void *f;
1358
1359    /**
1360     * The unloader function.
1361     * Normally NULL.  When we are finished with file (i.e. when we have
1362     * loaded a new one), set to a pointer to an unloader function.
1363     * Unloader will be called by sweep() (called from main loop) when
1364     * all clients using this file are done.  This prevents threading
1365     * problems.
1366     */
1367    void (*unloader)(void *);
1368
1369    /**
1370     * Used internally by sweep().  Do not access from elsewhere.
1371     */
1372    int active;
1373
1374    /**
1375     * File last-modified time, so we can check if file has been changed.
1376     * Read-only once the structure has been created.
1377     */
1378    time_t lastmodified;
1379    
1380    /**
1381     * The full filename.
1382     */
1383    char * filename;
1384
1385    /**
1386     * Pointer to next entry in the linked list of all "file_list"s.
1387     * This linked list is so that sweep() can navigate it.
1388     * Since sweep() can remove items from the list, we must be careful
1389     * to only access this value from main thread (when we know sweep
1390     * won't be running).
1391     */
1392    struct file_list *next;
1393 };
1394
1395
1396 #ifdef FEATURE_TRUST
1397
1398 /**
1399  * The format of a trust file when loaded into memory.
1400  */
1401 struct block_spec
1402 {
1403    struct url_spec url[1];   /**< The URL pattern              */
1404    int    reject;            /**< FIXME: Please document this! */
1405    struct block_spec *next;  /**< Next entry in linked list    */
1406 };
1407
1408 /**
1409  * Arbitrary limit for the number of trusted referrers.
1410  */
1411 #define MAX_TRUSTED_REFERRERS 512
1412
1413 #endif /* def FEATURE_TRUST */
1414
1415
1416 #define SOCKS_NONE    0    /**< Don't use a SOCKS server               */
1417 #define SOCKS_4      40    /**< original SOCKS 4 protocol              */
1418 #define SOCKS_4A     41    /**< as modified for hosts w/o external DNS */
1419
1420
1421 /**
1422  * How to forward a connection to a parent proxy.
1423  */
1424 struct forward_spec
1425 {
1426    /** URL pattern that this forward_spec is for. */
1427    struct url_spec url[1];
1428
1429    /** Connection type.  Must be SOCKS_NONE, SOCKS_4, or SOCKS_4A. */
1430    int   type;
1431
1432    /** SOCKS server hostname.  Only valid if "type" is SOCKS_4 or SOCKS_4A. */
1433    char *gateway_host;
1434
1435    /** SOCKS server port. */
1436    int   gateway_port;
1437
1438    /** Parent HTTP proxy hostname, or NULL for none. */
1439    char *forward_host;
1440
1441    /** Parent HTTP proxy port. */
1442    int   forward_port;
1443
1444    /** Next entry in the linked list. */
1445    struct forward_spec *next;
1446 };
1447
1448
1449 /**
1450  * Initializer for a static struct forward_spec.
1451  */
1452 #define FORWARD_SPEC_INITIALIZER { { URL_SPEC_INITIALIZER }, 0, NULL, 0, NULL, 0, NULL }
1453
1454 /* Supported filter types */
1455 #define FT_CONTENT_FILTER       1
1456 #define FT_CLIENT_HEADER_FILTER 2
1457 #define FT_SERVER_HEADER_FILTER 3
1458
1459 /**
1460  * This struct represents one filter (one block) from
1461  * the re_filterfile. If there is more than one filter
1462  * in the file, the file will be represented by a
1463  * chained list of re_filterfile specs.
1464  */
1465 struct re_filterfile_spec
1466 {
1467    char *name;                      /**< Name from FILTER: statement in re_filterfile. */
1468    char *description;               /**< Description from FILTER: statement in re_filterfile. */
1469    struct list patterns[1];         /**< The patterns from the re_filterfile. */
1470    pcrs_job *joblist;               /**< The resulting compiled pcrs_jobs. */
1471    int type;                        /**< Filter type (content, client-header, server-header). */
1472    struct re_filterfile_spec *next; /**< The pointer for chaining. */
1473 };
1474
1475
1476 #ifdef FEATURE_ACL
1477
1478 #define ACL_PERMIT   1  /**< Accept connection request */
1479 #define ACL_DENY     2  /**< Reject connection request */
1480
1481 /**
1482  * An IP address pattern.  Used to specify networks in the ACL.
1483  */
1484 struct access_control_addr
1485 {
1486    unsigned long addr;  /**< The IP address as an integer. */
1487    unsigned long mask;  /**< The network mask as an integer. */
1488    unsigned long port;  /**< The port number. */
1489 };
1490
1491 /**
1492  * An access control list (ACL) entry.
1493  *
1494  * This is a linked list.
1495  */
1496 struct access_control_list
1497 {
1498    struct access_control_addr src[1];  /**< Client IP address */
1499    struct access_control_addr dst[1];  /**< Website or parent proxy IP address */
1500
1501    short action;                       /**< ACL_PERMIT or ACL_DENY */
1502    struct access_control_list *next;   /**< The next entry in the ACL. */
1503 };
1504
1505 #endif /* def FEATURE_ACL */
1506
1507
1508 /** Maximum number of loaders (actions, re_filter, ...) */
1509 #define NLOADERS 8
1510
1511
1512 /** configuration_spec::feature_flags: CGI actions editor. */
1513 #define RUNTIME_FEATURE_CGI_EDIT_ACTIONS             1
1514
1515 /** configuration_spec::feature_flags: Web-based toggle. */
1516 #define RUNTIME_FEATURE_CGI_TOGGLE                   2
1517
1518 /** configuration_spec::feature_flags: HTTP-header-based toggle. */
1519 #define RUNTIME_FEATURE_HTTP_TOGGLE                  4
1520
1521 /** configuration_spec::feature_flags: Split large forms to limit the number of GET arguments. */
1522 #define RUNTIME_FEATURE_SPLIT_LARGE_FORMS            8
1523
1524 /** configuration_spec::feature_flags: Check the host header for requests with host-less request lines. */
1525 #define RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS 16
1526
1527 /** configuration_spec::feature_flags: Don't allow to circumvent blocks with the force prefix. */
1528 #define RUNTIME_FEATURE_ENFORCE_BLOCKS              32
1529
1530
1531 /**
1532  * Data loaded from the configuration file.
1533  *
1534  * (Anomaly: toggle is still handled through a global, not this structure)
1535  */
1536 struct configuration_spec
1537 {
1538    /** What to log */
1539    int debug;
1540    
1541    /** Nonzero to enable multithreading. */
1542    int multi_threaded;
1543
1544    /**
1545     * Bitmask of features that can be enabled/disabled through the config
1546     * file.  Currently defined bits:
1547     *
1548     * - RUNTIME_FEATURE_CGI_EDIT_ACTIONS
1549     * - RUNTIME_FEATURE_CGI_TOGGLE
1550     * - RUNTIME_FEATURE_HTTP_TOGGLE
1551     * - RUNTIME_FEATURE_SPLIT_LARGE_FORMS
1552     */
1553    unsigned feature_flags;
1554
1555    /** The log file name. */
1556    const char *logfile;
1557
1558    /** The config file directory. */
1559    const char *confdir;
1560
1561    /** The directory for customized CGI templates. */
1562    const char *templdir;
1563
1564    /** The log file directory. */
1565    const char *logdir;
1566
1567    /** The full paths to the actions files. */
1568    const char *actions_file[MAX_AF_FILES];
1569
1570    /** The short names of the actions files. */
1571    const char *actions_file_short[MAX_AF_FILES];
1572
1573    /** The administrator's email address */
1574    char *admin_address;
1575
1576    /** A URL with info on this proxy */
1577    char *proxy_info_url;
1578
1579    /** URL to the user manual (on our website or local copy) */
1580    char *usermanual;
1581
1582    /** The file names of the pcre filter files. */
1583    const char *re_filterfile[MAX_AF_FILES];
1584
1585    /** The short names of the pcre filter files. */
1586    const char *re_filterfile_short[MAX_AF_FILES];
1587
1588 #ifdef FEATURE_COOKIE_JAR
1589
1590    /** The file name of the cookie jar file */
1591    const char * jarfile;
1592
1593    /** The handle to the cookie jar file */
1594    FILE * jar;
1595
1596 #endif /* def FEATURE_COOKIE_JAR */
1597
1598    /** IP address to bind to.  Defaults to HADDR_DEFAULT == 127.0.0.1. */
1599    const char *haddr;
1600
1601    /** Port to bind to.  Defaults to HADDR_PORT == 8118. */
1602    int         hport;
1603
1604    /** Size limit for IOB */
1605    size_t buffer_limit;
1606
1607 #ifdef FEATURE_TRUST
1608
1609    /** The file name of the trust file. */
1610    const char * trustfile;
1611
1612    /** FIXME: DOCME: Document this. */
1613    struct list trust_info[1];
1614
1615    /** FIXME: DOCME: Document this. */
1616    struct url_spec *trust_list[MAX_TRUSTED_REFERRERS];
1617
1618 #endif /* def FEATURE_TRUST */
1619
1620 #ifdef FEATURE_ACL
1621
1622    /** The access control list (ACL). */
1623    struct access_control_list *acl;
1624
1625 #endif /* def FEATURE_ACL */
1626
1627    /** Information about parent proxies (forwarding). */
1628    struct forward_spec *forward;
1629
1630    /** Number of retries in case a forwarded connection attempt fails */
1631    int         forwarded_connect_retries;
1632
1633    /** All options from the config file, HTML-formatted. */
1634    char *proxy_args;
1635
1636    /** The configuration file object. */
1637    struct file_list *config_file_list;
1638
1639    /** List of loaders */
1640    int (*loaders[NLOADERS])(struct client_state *);
1641
1642    /** Nonzero if we need to bind() to the new port. */
1643    int need_bind;
1644 };
1645
1646 /** Calculates the number of elements in an array, using sizeof. */
1647 #define SZ(X)  (sizeof(X) / sizeof(*X))
1648
1649 #ifdef FEATURE_FORCE_LOAD
1650 /** The force load URL prefix. */
1651 #define FORCE_PREFIX "/PRIVOXY-FORCE"
1652 #endif /* def FEATURE_FORCE_LOAD */
1653
1654 #ifdef FEATURE_NO_GIFS
1655 /** The MIME type for images ("image/png" or "image/gif"). */
1656 #define BUILTIN_IMAGE_MIMETYPE "image/png"
1657 #else
1658 #define BUILTIN_IMAGE_MIMETYPE "image/gif"
1659 #endif /* def FEATURE_NO_GIFS */
1660
1661
1662 /* 
1663  * Hardwired URLs
1664  */
1665
1666 /** URL for the Privoxy home page. */
1667 #define HOME_PAGE_URL     "http://www.privoxy.org/"
1668
1669 /** URL for the Privoxy user manual. */
1670 #define USER_MANUAL_URL   HOME_PAGE_URL VERSION "/user-manual/"
1671
1672 /** Prefix for actions help links  (append to USER_MANUAL_URL). */
1673 #define ACTIONS_HELP_PREFIX "actions-file.html#"
1674
1675 /** Prefix for config option help links (append to USER_MANUAL_URL). */
1676 #define CONFIG_HELP_PREFIX  "config.html#"
1677
1678 /*
1679  * The "hosts" to intercept and display CGI pages.
1680  * First one is a hostname only, second one can specify host and path.
1681  *
1682  * Notes:
1683  * 1) Do not specify the http: prefix
1684  * 2) CGI_SITE_2_PATH must not end with /, one will be added automatically.
1685  * 3) CGI_SITE_2_PATH must start with /, unless it is the empty string.
1686  */
1687 #define CGI_SITE_1_HOST "p.p"
1688 #define CGI_SITE_2_HOST "config.privoxy.org"
1689 #define CGI_SITE_2_PATH ""
1690
1691 /**
1692  * The prefix for CGI pages.  Written out in generated HTML.
1693  * INCLUDES the trailing slash.
1694  */
1695 #define CGI_PREFIX  "http://" CGI_SITE_2_HOST CGI_SITE_2_PATH "/"
1696
1697 #ifdef __cplusplus
1698 } /* extern "C" */
1699 #endif
1700
1701 #endif /* ndef PROJECT_H_INCLUDED */
1702
1703 /*
1704   Local Variables:
1705   tab-width: 3
1706   end:
1707 */