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