Ditch cast from (char **) to (char **).
[privoxy.git] / parsers.c
1 const char parsers_rcs[] = "$Id: parsers.c,v 1.132 2008/05/21 15:47:14 fabiankeil Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/parsers.c,v $
5  *
6  * Purpose     :  Declares functions to parse/crunch headers and pages.
7  *                Functions declared include:
8  *                   `add_to_iob', `client_cookie_adder', `client_from',
9  *                   `client_referrer', `client_send_cookie', `client_ua',
10  *                   `client_uagent', `client_x_forwarded',
11  *                   `client_x_forwarded_adder', `client_xtra_adder',
12  *                   `content_type', `crumble', `destroy_list', `enlist',
13  *                   `flush_socket', ``get_header', `sed', `filter_header'
14  *                   `server_content_encoding', `server_content_disposition',
15  *                   `server_last_modified', `client_accept_language',
16  *                   `crunch_client_header', `client_if_modified_since',
17  *                   `client_if_none_match', `get_destination_from_headers',
18  *                   `parse_header_time', `decompress_iob' and `server_set_cookie'.
19  *
20  * Copyright   :  Written by and Copyright (C) 2001-2008 the SourceForge
21  *                Privoxy team. http://www.privoxy.org/
22  *
23  *                Based on the Internet Junkbuster originally written
24  *                by and Copyright (C) 1997 Anonymous Coders and
25  *                Junkbusters Corporation.  http://www.junkbusters.com
26  *
27  *                This program is free software; you can redistribute it
28  *                and/or modify it under the terms of the GNU General
29  *                Public License as published by the Free Software
30  *                Foundation; either version 2 of the License, or (at
31  *                your option) any later version.
32  *
33  *                This program is distributed in the hope that it will
34  *                be useful, but WITHOUT ANY WARRANTY; without even the
35  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
36  *                PARTICULAR PURPOSE.  See the GNU General Public
37  *                License for more details.
38  *
39  *                The GNU General Public License should be included with
40  *                this file.  If not, you can view it at
41  *                http://www.gnu.org/copyleft/gpl.html
42  *                or write to the Free Software Foundation, Inc., 59
43  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
44  *
45  * Revisions   :
46  *    $Log: parsers.c,v $
47  *    Revision 1.132  2008/05/21 15:47:14  fabiankeil
48  *    Streamline sed()'s prototype and declare
49  *    the header parse and add structures static.
50  *
51  *    Revision 1.131  2008/05/20 20:13:30  fabiankeil
52  *    Factor update_server_headers() out of sed(), ditch the
53  *    first_run hack and make server_patterns_light static.
54  *
55  *    Revision 1.130  2008/05/19 17:18:04  fabiankeil
56  *    Wrap memmove() calls in string_move()
57  *    to document the purpose in one place.
58  *
59  *    Revision 1.129  2008/05/17 14:02:07  fabiankeil
60  *    Normalize linear header white space.
61  *
62  *    Revision 1.128  2008/05/16 16:39:03  fabiankeil
63  *    If a header is split across multiple lines,
64  *    merge them to a single line before parsing them.
65  *
66  *    Revision 1.127  2008/05/10 13:23:38  fabiankeil
67  *    Don't provide get_header() with the whole client state
68  *    structure when it only needs access to csp->iob.
69  *
70  *    Revision 1.126  2008/05/03 16:40:45  fabiankeil
71  *    Change content_filters_enabled()'s parameter from
72  *    csp->action to action so it can be also used in the
73  *    CGI code. Don't bother checking if there are filters
74  *    loaded, as that's somewhat besides the point.
75  *
76  *    Revision 1.125  2008/04/17 14:40:49  fabiankeil
77  *    Provide get_http_time() with the buffer size so it doesn't
78  *    have to blindly assume that the buffer is big enough.
79  *
80  *    Revision 1.124  2008/04/16 16:38:21  fabiankeil
81  *    Don't pass the whole csp structure to flush_socket()
82  *    when it only needs a file descriptor and a buffer.
83  *
84  *    Revision 1.123  2008/03/29 12:13:46  fabiankeil
85  *    Remove send-wafer and send-vanilla-wafer actions.
86  *
87  *    Revision 1.122  2008/03/28 15:13:39  fabiankeil
88  *    Remove inspect-jpegs action.
89  *
90  *    Revision 1.121  2008/01/05 21:37:03  fabiankeil
91  *    Let client_range() also handle Request-Range headers
92  *    which apparently are still supported by many servers.
93  *
94  *    Revision 1.120  2008/01/04 17:43:45  fabiankeil
95  *    Improve the warning messages that get logged if the action files
96  *    "enable" filters but no filters of that type have been loaded.
97  *
98  *    Revision 1.119  2007/12/28 18:32:51  fabiankeil
99  *    In server_content_type():
100  *    - Don't require leading white space when detecting image content types.
101  *    - Change '... not replaced ...' message to sound less crazy if the text
102  *      type actually is 'text/plain'.
103  *    - Mark the 'text/plain == binary data' assumption for removal.
104  *    - Remove a bunch of trailing white space.
105  *
106  *    Revision 1.118  2007/12/28 16:56:35  fabiankeil
107  *    Minor server_content_disposition() changes:
108  *    - Don't regenerate the header name all lower-case.
109  *    - Some white space fixes.
110  *    - Remove useless log message in case of ENOMEM.
111  *
112  *    Revision 1.117  2007/12/06 18:11:50  fabiankeil
113  *    Garbage-collect the code to add a X-Forwarded-For
114  *    header as it seems to be mostly used by accident.
115  *
116  *    Revision 1.116  2007/12/01 13:04:22  fabiankeil
117  *    Fix a crash on mingw32 with some Last Modified times in the future.
118  *
119  *    Revision 1.115  2007/11/02 16:52:50  fabiankeil
120  *    Remove a "can't happen" error block which, over
121  *    time, mutated into a "guaranteed to happen" block.
122  *
123  *    Revision 1.114  2007/10/19 16:56:26  fabiankeil
124  *    - Downgrade "Buffer limit reached" message to LOG_LEVEL_INFO.
125  *    - Use shiny new content_filters_enabled() in client_range().
126  *
127  *    Revision 1.113  2007/10/10 17:29:57  fabiankeil
128  *    I forgot about Poland.
129  *
130  *    Revision 1.112  2007/10/09 16:38:40  fabiankeil
131  *    Remove Range and If-Range headers if content filtering is enabled.
132  *
133  *    Revision 1.111  2007/10/04 18:07:00  fabiankeil
134  *    Move ACTION_VANILLA_WAFER handling from jcc's chat() into
135  *    client_cookie_adder() to make sure send-vanilla-wafer can be
136  *    controlled through tags (and thus regression-tested).
137  *
138  *    Revision 1.110  2007/09/29 10:42:37  fabiankeil
139  *    - Remove "scanning headers for" log message again.
140  *    - Some more whitespace fixes.
141  *
142  *    Revision 1.109  2007/09/08 14:25:48  fabiankeil
143  *    Refactor client_referrer() and add conditional-forge parameter.
144  *
145  *    Revision 1.108  2007/08/28 18:21:03  fabiankeil
146  *    A bunch of whitespace fixes, pointy hat to me.
147  *
148  *    Revision 1.107  2007/08/28 18:16:32  fabiankeil
149  *    Fix possible memory corruption in server_http, make sure it's not
150  *    executed for ordinary server headers and mark some problems for later.
151  *
152  *    Revision 1.106  2007/08/18 14:30:32  fabiankeil
153  *    Let content-type-overwrite{} honour force-text-mode again.
154  *
155  *    Revision 1.105  2007/08/11 14:49:49  fabiankeil
156  *    - Add prototpyes for the header parsers and make them static.
157  *    - Comment out client_accept_encoding_adder() which isn't used right now.
158  *
159  *    Revision 1.104  2007/07/14 07:38:19  fabiankeil
160  *    Move the ACTION_FORCE_TEXT_MODE check out of
161  *    server_content_type(). Signal other functions
162  *    whether or not a content type has been declared.
163  *    Part of the fix for BR#1750917.
164  *
165  *    Revision 1.103  2007/06/01 16:31:54  fabiankeil
166  *    Change sed() to return a jb_err in preparation for forward-override{}.
167  *
168  *    Revision 1.102  2007/05/27 12:39:32  fabiankeil
169  *    Adjust "X-Filter: No" to disable dedicated header filters.
170  *
171  *    Revision 1.101  2007/05/14 10:16:41  fabiankeil
172  *    Streamline client_cookie_adder().
173  *
174  *    Revision 1.100  2007/04/30 15:53:11  fabiankeil
175  *    Make sure filters with dynamic jobs actually use them.
176  *
177  *    Revision 1.99  2007/04/30 15:06:26  fabiankeil
178  *    - Introduce dynamic pcrs jobs that can resolve variables.
179  *    - Remove unnecessary update_action_bits_for_all_tags() call.
180  *
181  *    Revision 1.98  2007/04/17 18:32:10  fabiankeil
182  *    - Make tagging based on tags set by earlier taggers
183  *      of the same kind possible.
184  *    - Log whether or not new tags cause action bits updates
185  *      (in which case a matching tag-pattern section exists).
186  *    - Log if the user tries to set a tag that is already set.
187  *
188  *    Revision 1.97  2007/04/15 16:39:21  fabiankeil
189  *    Introduce tags as alternative way to specify which
190  *    actions apply to a request. At the moment tags can be
191  *    created based on client and server headers.
192  *
193  *    Revision 1.96  2007/04/12 12:53:58  fabiankeil
194  *    Log a warning if the content is compressed, filtering is
195  *    enabled and Privoxy was compiled without zlib support.
196  *    Closes FR#1673938.
197  *
198  *    Revision 1.95  2007/03/25 14:26:40  fabiankeil
199  *    - Fix warnings when compiled with glibc.
200  *    - Don't use crumble() for cookie crunching.
201  *    - Move cookie time parsing into parse_header_time().
202  *    - Let parse_header_time() return a jb_err code
203  *      instead of a pointer that can only be used to
204  *      check for NULL anyway.
205  *
206  *    Revision 1.94  2007/03/21 12:23:53  fabiankeil
207  *    - Add better protection against malicious gzip headers.
208  *    - Stop logging the first hundred bytes of decompressed content.
209  *      It looks like it's working and there is always debug 16.
210  *    - Log the content size after decompression in decompress_iob()
211  *      instead of pcrs_filter_response().
212  *
213  *    Revision 1.93  2007/03/20 15:21:44  fabiankeil
214  *    - Use dedicated header filter actions instead of abusing "filter".
215  *      Replace "filter-client-headers" and "filter-client-headers"
216  *      with "server-header-filter" and "client-header-filter".
217  *    - Remove filter_client_header() and filter_client_header(),
218  *      filter_header() now checks the shiny new
219  *      CSP_FLAG_CLIENT_HEADER_PARSING_DONE flag instead.
220  *
221  *    Revision 1.92  2007/03/05 13:25:32  fabiankeil
222  *    - Cosmetical changes for LOG_LEVEL_RE_FILTER messages.
223  *    - Handle "Cookie:" and "Connection:" headers a bit smarter
224  *      (don't crunch them just to recreate them later on).
225  *    - Add another non-standard time format for the cookie
226  *      expiration date detection.
227  *    - Fix a valgrind warning.
228  *
229  *    Revision 1.91  2007/02/24 12:27:32  fabiankeil
230  *    Improve cookie expiration date detection.
231  *
232  *    Revision 1.90  2007/02/08 19:12:35  fabiankeil
233  *    Don't run server_content_length() the first time
234  *    sed() parses server headers; only adjust the
235  *    Content-Length header if the page was modified.
236  *
237  *    Revision 1.89  2007/02/07 16:52:11  fabiankeil
238  *    Fix log messages regarding the cookie time format
239  *    (cookie and request URL were mixed up).
240  *
241  *    Revision 1.88  2007/02/07 11:27:12  fabiankeil
242  *    - Let decompress_iob()
243  *      - not corrupt the content if decompression fails
244  *        early. (the first byte(s) were lost).
245  *      - use pointer arithmetics with defined outcome for
246  *        a change.
247  *    - Use a different kludge to remember a failed decompression.
248  *
249  *    Revision 1.87  2007/01/31 16:21:38  fabiankeil
250  *    Search for Max-Forwards headers case-insensitive,
251  *    don't generate the "501 unsupported" message for invalid
252  *    Max-Forwards values and don't increase negative ones.
253  *
254  *    Revision 1.86  2007/01/30 13:05:26  fabiankeil
255  *    - Let server_set_cookie() check the expiration date
256  *      of cookies and don't touch the ones that are already
257  *      expired. Fixes problems with low quality web applications
258  *      as described in BR 932612.
259  *
260  *    - Adjust comment in client_max_forwards to reality;
261  *      remove invalid Max-Forwards headers.
262  *
263  *    Revision 1.85  2007/01/26 15:33:46  fabiankeil
264  *    Stop filter_header() from unintentionally removing
265  *    empty header lines that were enlisted by the continue
266  *    hack.
267  *
268  *    Revision 1.84  2007/01/24 12:56:52  fabiankeil
269  *    - Repeat the request URL before logging any headers.
270  *      Makes reading the log easier in case of simultaneous requests.
271  *    - If there are more than one Content-Type headers in one request,
272  *      use the first one and remove the others.
273  *    - Remove "newval" variable in server_content_type().
274  *      It's only used once.
275  *
276  *    Revision 1.83  2007/01/12 15:03:02  fabiankeil
277  *    Correct a cast, check inflateEnd() exit code
278  *    to see if we have to, replace sprintf calls
279  *    with snprintf.
280  *
281  *    Revision 1.82  2007/01/01 19:36:37  fabiankeil
282  *    Integrate a modified version of Wil Mahan's
283  *    zlib patch (PR #895531).
284  *
285  *    Revision 1.81  2006/12/31 22:21:33  fabiankeil
286  *    Skip empty filter files in filter_header()
287  *    but don't ignore the ones that come afterwards.
288  *    Fixes BR 1619208, this time for real.
289  *
290  *    Revision 1.80  2006/12/29 19:08:22  fabiankeil
291  *    Reverted parts of my last commit
292  *    to keep error handling working.
293  *
294  *    Revision 1.79  2006/12/29 18:04:40  fabiankeil
295  *    Fixed gcc43 conversion warnings.
296  *
297  *    Revision 1.78  2006/12/26 17:19:20  fabiankeil
298  *    Bringing back the "useless" localtime() call
299  *    I removed in revision 1.67. On some platforms
300  *    it's necessary to prevent time zone offsets.
301  *
302  *    Revision 1.77  2006/12/07 18:44:26  fabiankeil
303  *    Rebuild request URL in get_destination_from_headers()
304  *    to make sure redirect{pcrs command} works as expected
305  *    for intercepted requests.
306  *
307  *    Revision 1.76  2006/12/06 19:52:25  fabiankeil
308  *    Added get_destination_from_headers().
309  *
310  *    Revision 1.75  2006/11/13 19:05:51  fabiankeil
311  *    Make pthread mutex locking more generic. Instead of
312  *    checking for OSX and OpenBSD, check for FEATURE_PTHREAD
313  *    and use mutex locking unless there is an _r function
314  *    available. Better safe than sorry.
315  *
316  *    Fixes "./configure --disable-pthread" and should result
317  *    in less threading-related problems on pthread-using platforms,
318  *    but it still doesn't fix BR#1122404.
319  *
320  *    Revision 1.74  2006/10/02 16:59:12  fabiankeil
321  *    The special header "X-Filter: No" now disables
322  *    header filtering as well.
323  *
324  *    Revision 1.73  2006/09/23 13:26:38  roro
325  *    Replace TABs by spaces in source code.
326  *
327  *    Revision 1.72  2006/09/23 12:37:21  fabiankeil
328  *    Don't print a log message every time filter_headers is
329  *    entered or left. It only creates noise without any real
330  *    information.
331  *
332  *    Revision 1.71  2006/09/21 19:55:17  fabiankeil
333  *    Fix +hide-if-modified-since{-n}.
334  *
335  *    Revision 1.70  2006/09/08 12:06:34  fabiankeil
336  *    Have hide-if-modified-since interpret the random
337  *    range value as minutes instead of hours. Allows
338  *    more fine-grained configuration.
339  *
340  *    Revision 1.69  2006/09/06 16:25:51  fabiankeil
341  *    Always have parse_header_time return a pointer
342  *    that actual makes sense, even though we currently
343  *    only need it to detect problems.
344  *
345  *    Revision 1.68  2006/09/06 10:43:32  fabiankeil
346  *    Added config option enable-remote-http-toggle
347  *    to specify if Privoxy should recognize special
348  *    headers (currently only X-Filter) to change its
349  *    behaviour. Disabled by default.
350  *
351  *    Revision 1.67  2006/09/04 11:01:26  fabiankeil
352  *    After filtering de-chunked instances, remove
353  *    "Transfer-Encoding" header entirely instead of changing
354  *    it to "Transfer-Encoding: identity", which is invalid.
355  *    Thanks Michael Shields <shields@msrl.com>. Fixes PR 1318658.
356  *
357  *    Don't use localtime in parse_header_time. An empty time struct
358  *    is good enough, it gets overwritten by strptime anyway.
359  *
360  *    Revision 1.66  2006/09/03 19:38:28  fabiankeil
361  *    Use gmtime_r if available, fallback to gmtime with mutex
362  *    protection for MacOSX and use vanilla gmtime for the rest.
363  *
364  *    Revision 1.65  2006/08/22 10:55:56  fabiankeil
365  *    Changed client_referrer to use the right type (size_t) for
366  *    hostlenght and to shorten the temporary referrer string with
367  *    '\0' instead of adding a useless line break.
368  *
369  *    Revision 1.64  2006/08/17 17:15:10  fabiankeil
370  *    - Back to timegm() using GnuPG's replacement if necessary.
371  *      Using mktime() and localtime() could add a on hour offset if
372  *      the randomize factor was big enough to lead to a summer/wintertime
373  *      switch.
374  *
375  *    - Removed now-useless Privoxy 3.0.3 compatibility glue.
376  *
377  *    - Moved randomization code into pick_from_range().
378  *
379  *    - Changed parse_header_time definition.
380  *      time_t isn't guaranteed to be signed and
381  *      if it isn't, -1 isn't available as error code.
382  *      Changed some variable types in client_if_modified_since()
383  *      because of the same reason.
384  *
385  *    Revision 1.63  2006/08/14 13:18:08  david__schmidt
386  *    OS/2 compilation compatibility fixups
387  *
388  *    Revision 1.62  2006/08/14 08:58:42  fabiankeil
389  *    Changed include from strptime.c to strptime.h
390  *
391  *    Revision 1.61  2006/08/14 08:25:19  fabiankeil
392  *    Split filter-headers{} into filter-client-headers{}
393  *    and filter-server-headers{}.
394  *    Added parse_header_time() to share some code.
395  *    Replaced timegm() with mktime().
396  *
397  *    Revision 1.60  2006/08/12 03:54:37  david__schmidt
398  *    Windows service integration
399  *
400  *    Revision 1.59  2006/08/03 02:46:41  david__schmidt
401  *    Incorporate Fabian Keil's patch work:\rhttp://www.fabiankeil.de/sourcecode/privoxy/
402  *
403  *    Revision 1.58  2006/07/18 14:48:47  david__schmidt
404  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
405  *    with what was really the latest development (the v_3_0_branch branch)
406  *
407  *    Revision 1.56.2.10  2006/01/21 16:16:08  david__schmidt
408  *    Thanks to  Edward Carrel for his patch to modernize OSX's\rpthreads support.  See bug #1409623.
409  *
410  *    Revision 1.56.2.9  2004/10/03 12:53:45  david__schmidt
411  *    Add the ability to check jpeg images for invalid
412  *    lengths of comment blocks.  Defensive strategy
413  *    against the exploit:
414  *       Microsoft Security Bulletin MS04-028
415  *       Buffer Overrun in JPEG Processing (GDI+) Could
416  *       Allow Code Execution (833987)
417  *    Enabled with +inspect-jpegs in actions files.
418  *
419  *    Revision 1.56.2.8  2003/07/11 13:21:25  oes
420  *    Excluded text/plain objects from filtering. This fixes a
421  *    couple of client-crashing, download corruption and
422  *    Privoxy performance issues, whose root cause lies in
423  *    web servers labelling content of unknown type as text/plain.
424  *
425  *    Revision 1.56.2.7  2003/05/06 12:07:26  oes
426  *    Fixed bug #729900: Suspicious HOST: headers are now killed and regenerated if necessary
427  *
428  *    Revision 1.56.2.6  2003/04/14 21:28:30  oes
429  *    Completing the previous change
430  *
431  *    Revision 1.56.2.5  2003/04/14 12:08:16  oes
432  *    Added temporary workaround for bug in PHP < 4.2.3
433  *
434  *    Revision 1.56.2.4  2003/03/07 03:41:05  david__schmidt
435  *    Wrapping all *_r functions (the non-_r versions of them) with mutex semaphores for OSX.  Hopefully this will take care of all of those pesky crash reports.
436  *
437  *    Revision 1.56.2.3  2002/11/10 04:20:02  hal9
438  *    Fix typo: supressed -> suppressed
439  *
440  *    Revision 1.56.2.2  2002/09/25 14:59:53  oes
441  *    Improved cookie logging
442  *
443  *    Revision 1.56.2.1  2002/09/25 14:52:45  oes
444  *    Added basic support for OPTIONS and TRACE HTTP methods:
445  *     - New parser function client_max_forwards which decrements
446  *       the Max-Forwards HTTP header field of OPTIONS and TRACE
447  *       requests by one before forwarding
448  *     - New parser function client_host which extracts the host
449  *       and port information from the HTTP header field if the
450  *       request URI was not absolute
451  *     - Don't crumble and re-add the Host: header, but only generate
452  *       and append if missing
453  *
454  *    Revision 1.56  2002/05/12 15:34:22  jongfoster
455  *    Fixing typo in a comment
456  *
457  *    Revision 1.55  2002/05/08 16:01:07  oes
458  *    Optimized add_to_iob:
459  *     - Use realloc instead of malloc(), memcpy(), free()
460  *     - Expand to powers of two if possible, to get
461  *       O(log n) reallocs instead of O(n).
462  *     - Moved check for buffer limit here from chat
463  *     - Report failure via returncode
464  *
465  *    Revision 1.54  2002/04/02 15:03:16  oes
466  *    Tiny code cosmetics
467  *
468  *    Revision 1.53  2002/03/26 22:29:55  swa
469  *    we have a new homepage!
470  *
471  *    Revision 1.52  2002/03/24 13:25:43  swa
472  *    name change related issues
473  *
474  *    Revision 1.51  2002/03/13 00:27:05  jongfoster
475  *    Killing warnings
476  *
477  *    Revision 1.50  2002/03/12 01:45:35  oes
478  *    More verbose logging
479  *
480  *    Revision 1.49  2002/03/09 20:03:52  jongfoster
481  *    - Making various functions return int rather than size_t.
482  *      (Undoing a recent change).  Since size_t is unsigned on
483  *      Windows, functions like read_socket that return -1 on
484  *      error cannot return a size_t.
485  *
486  *      THIS WAS A MAJOR BUG - it caused frequent, unpredictable
487  *      crashes, and also frequently caused JB to jump to 100%
488  *      CPU and stay there.  (Because it thought it had just
489  *      read ((unsigned)-1) == 4Gb of data...)
490  *
491  *    - The signature of write_socket has changed, it now simply
492  *      returns success=0/failure=nonzero.
493  *
494  *    - Trying to get rid of a few warnings --with-debug on
495  *      Windows, I've introduced a new type "jb_socket".  This is
496  *      used for the socket file descriptors.  On Windows, this
497  *      is SOCKET (a typedef for unsigned).  Everywhere else, it's
498  *      an int.  The error value can't be -1 any more, so it's
499  *      now JB_INVALID_SOCKET (which is -1 on UNIX, and in
500  *      Windows it maps to the #define INVALID_SOCKET.)
501  *
502  *    - The signature of bind_port has changed.
503  *
504  *    Revision 1.48  2002/03/07 03:46:53  oes
505  *    Fixed compiler warnings etc
506  *
507  *    Revision 1.47  2002/02/20 23:15:13  jongfoster
508  *    Parsing functions now handle out-of-memory gracefully by returning
509  *    an error code.
510  *
511  *    Revision 1.46  2002/01/17 21:03:47  jongfoster
512  *    Moving all our URL and URL pattern parsing code to urlmatch.c.
513  *
514  *    Revision 1.45  2002/01/09 14:33:03  oes
515  *    Added support for localtime_r.
516  *
517  *    Revision 1.44  2001/12/14 01:22:54  steudten
518  *    Remove 'user:pass@' from 'proto://user:pass@host' for the
519  *    new added header 'Host: ..'. (See Req ID 491818)
520  *
521  *    Revision 1.43  2001/11/23 00:26:38  jongfoster
522  *    Fixing two really stupid errors in my previous commit
523  *
524  *    Revision 1.42  2001/11/22 21:59:30  jongfoster
525  *    Adding code to handle +no-cookies-keep
526  *
527  *    Revision 1.41  2001/11/05 23:43:05  steudten
528  *    Add time+date to log files.
529  *
530  *    Revision 1.40  2001/10/26 20:13:09  jongfoster
531  *    ctype.h is needed in Windows, too.
532  *
533  *    Revision 1.39  2001/10/26 17:40:04  oes
534  *    Introduced get_header_value()
535  *    Removed http->user_agent, csp->referrer and csp->accept_types
536  *    Removed client_accept()
537  *
538  *    Revision 1.38  2001/10/25 03:40:48  david__schmidt
539  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
540  *    threads to call select() simultaneously.  So, it's time to do a real, live,
541  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
542  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
543  *
544  *    Revision 1.37  2001/10/23 21:36:02  jongfoster
545  *    Documenting sed()'s error behaviou (doc change only)
546  *
547  *    Revision 1.36  2001/10/13 12:51:51  joergs
548  *    Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
549  *    force-load), instead crumble Host: and add it (again) in client_host_adder
550  *    (in case we get a HTTP/1.0 request without Host: header and forward it to
551  *    a HTTP/1.1 server/proxy).
552  *
553  *    Revision 1.35  2001/10/09 22:39:21  jongfoster
554  *    assert.h is also required under Win32, so moving out of #ifndef _WIN32
555  *    block.
556  *
557  *    Revision 1.34  2001/10/07 18:50:55  oes
558  *    Added server_content_encoding, renamed server_transfer_encoding
559  *
560  *    Revision 1.33  2001/10/07 18:04:49  oes
561  *    Changed server_http11 to server_http and its pattern to "HTTP".
562  *      Additional functionality: it now saves the HTTP status into
563  *      csp->http->status and sets CT_TABOO for Status 206 (partial range)
564  *
565  *    Revision 1.32  2001/10/07 15:43:28  oes
566  *    Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
567  *       client_te and client_accept_encoding_adder, triggered by the new
568  *       +no-compression action. For HTTP/1.1 the Accept-Encoding header is
569  *       changed to allow only identity and chunked, and the TE header is
570  *       crunched. For HTTP/1.0, Accept-Encoding is crunched.
571  *
572  *    parse_http_request no longer does anything than parsing. The rewriting
573  *      of http->cmd and version mangling are gone. It now also recognizes
574  *      the put and delete methods and saves the url in http->url. Removed
575  *      unused variable.
576  *
577  *    renamed content_type and content_length to have the server_ prefix
578  *
579  *    server_content_type now only works if csp->content_type != CT_TABOO
580  *
581  *    added server_transfer_encoding, which
582  *      - Sets CT_TABOO to prohibit filtering if encoding compresses
583  *      - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
584  *      - Change from "chunked" to "identity" if body was chunked
585  *        but has been de-chunked for filtering.
586  *
587  *    added server_content_md5 which crunches any Content-MD5 headers
588  *      if the body was modified.
589  *
590  *    made server_http11 conditional on +downgrade action
591  *
592  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
593  *
594  *    Revision 1.31  2001/10/05 14:25:02  oes
595  *    Crumble Keep-Alive from Server
596  *
597  *    Revision 1.30  2001/09/29 12:56:03  joergs
598  *    IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
599  *
600  *    Revision 1.29  2001/09/24 21:09:24  jongfoster
601  *    Fixing 2 memory leaks that Guy spotted, where the paramater to
602  *    enlist() was not being free()d.
603  *
604  *    Revision 1.28  2001/09/22 16:32:28  jongfoster
605  *    Removing unused #includes.
606  *
607  *    Revision 1.27  2001/09/20 15:45:25  steudten
608  *
609  *    add casting from size_t to int for printf()
610  *    remove local variable shadow s2
611  *
612  *    Revision 1.26  2001/09/16 17:05:14  jongfoster
613  *    Removing unused #include showarg.h
614  *
615  *    Revision 1.25  2001/09/16 13:21:27  jongfoster
616  *    Changes to use new list functions.
617  *
618  *    Revision 1.24  2001/09/13 23:05:50  jongfoster
619  *    Changing the string paramater to the header parsers a "const".
620  *
621  *    Revision 1.23  2001/09/12 18:08:19  steudten
622  *
623  *    In parse_http_request() header rewriting miss the host value, so
624  *    from http://www.mydomain.com the result was just " / " not
625  *    http://www.mydomain.com/ in case we forward.
626  *
627  *    Revision 1.22  2001/09/10 10:58:53  oes
628  *    Silenced compiler warnings
629  *
630  *    Revision 1.21  2001/07/31 14:46:00  oes
631  *     - Persistant connections now suppressed
632  *     - sed() no longer appends empty header to csp->headers
633  *
634  *    Revision 1.20  2001/07/30 22:08:36  jongfoster
635  *    Tidying up #defines:
636  *    - All feature #defines are now of the form FEATURE_xxx
637  *    - Permanently turned off WIN_GUI_EDIT
638  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
639  *
640  *    Revision 1.19  2001/07/25 17:21:54  oes
641  *    client_uagent now saves copy of User-Agent: header value
642  *
643  *    Revision 1.18  2001/07/13 14:02:46  oes
644  *     - Included fix to repair broken HTTP requests that
645  *       don't contain a path, not even '/'.
646  *     - Removed all #ifdef PCRS
647  *     - content_type now always inspected and classified as
648  *       text, gif or other.
649  *     - formatting / comments
650  *
651  *    Revision 1.17  2001/06/29 21:45:41  oes
652  *    Indentation, CRLF->LF, Tab-> Space
653  *
654  *    Revision 1.16  2001/06/29 13:32:42  oes
655  *    - Fixed a comment
656  *    - Adapted free_http_request
657  *    - Removed logentry from cancelled commit
658  *
659  *    Revision 1.15  2001/06/03 19:12:38  oes
660  *    deleted const struct interceptors
661  *
662  *    Revision 1.14  2001/06/01 18:49:17  jongfoster
663  *    Replaced "list_share" with "list" - the tiny memory gain was not
664  *    worth the extra complexity.
665  *
666  *    Revision 1.13  2001/05/31 21:30:33  jongfoster
667  *    Removed list code - it's now in list.[ch]
668  *    Renamed "permission" to "action", and changed many features
669  *    to use the actions file rather than the global config.
670  *
671  *    Revision 1.12  2001/05/31 17:33:13  oes
672  *
673  *    CRLF -> LF
674  *
675  *    Revision 1.11  2001/05/29 20:11:19  joergs
676  *    '/ * inside comment' warning removed.
677  *
678  *    Revision 1.10  2001/05/29 09:50:24  jongfoster
679  *    Unified blocklist/imagelist/permissionslist.
680  *    File format is still under discussion, but the internal changes
681  *    are (mostly) done.
682  *
683  *    Also modified interceptor behaviour:
684  *    - We now intercept all URLs beginning with one of the following
685  *      prefixes (and *only* these prefixes):
686  *        * http://i.j.b/
687  *        * http://ijbswa.sf.net/config/
688  *        * http://ijbswa.sourceforge.net/config/
689  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
690  *    - Internal changes so that intercepted and fast redirect pages
691  *      are not replaced with an image.
692  *    - Interceptors now have the option to send a binary page direct
693  *      to the client. (i.e. ijb-send-banner uses this)
694  *    - Implemented show-url-info interceptor.  (Which is why I needed
695  *      the above interceptors changes - a typical URL is
696  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
697  *      The previous mechanism would not have intercepted that, and
698  *      if it had been intercepted then it then it would have replaced
699  *      it with an image.)
700  *
701  *    Revision 1.9  2001/05/28 17:26:33  jongfoster
702  *    Fixing segfault if last header was crunched.
703  *    Fixing Windows build (snprintf() is _snprintf() under Win32, but we
704  *    can use the cross-platform sprintf() instead.)
705  *
706  *    Revision 1.8  2001/05/27 22:17:04  oes
707  *
708  *    - re_process_buffer no longer writes the modified buffer
709  *      to the client, which was very ugly. It now returns the
710  *      buffer, which it is then written by chat.
711  *
712  *    - content_length now adjusts the Content-Length: header
713  *      for modified documents rather than crunch()ing it.
714  *      (Length info in csp->content_length, which is 0 for
715  *      unmodified documents)
716  *
717  *    - For this to work, sed() is called twice when filtering.
718  *
719  *    Revision 1.7  2001/05/27 13:19:06  oes
720  *    Patched Joergs solution for the content-length in.
721  *
722  *    Revision 1.6  2001/05/26 13:39:32  jongfoster
723  *    Only crunches Content-Length header if applying RE filtering.
724  *    Without this fix, Microsoft Windows Update wouldn't work.
725  *
726  *    Revision 1.5  2001/05/26 00:28:36  jongfoster
727  *    Automatic reloading of config file.
728  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
729  *    Most of the global variables have been moved to a new
730  *    struct configuration_spec, accessed through csp->config->globalname
731  *    Most of the globals remaining are used by the Win32 GUI.
732  *
733  *    Revision 1.4  2001/05/22 18:46:04  oes
734  *
735  *    - Enabled filtering banners by size rather than URL
736  *      by adding patterns that replace all standard banner
737  *      sizes with the "Junkbuster" gif to the re_filterfile
738  *
739  *    - Enabled filtering WebBugs by providing a pattern
740  *      which kills all 1x1 images
741  *
742  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
743  *      which is selected by the (nonstandard and therefore
744  *      capital) letter 'U' in the option string.
745  *      It causes the quantifiers to be ungreedy by default.
746  *      Appending a ? turns back to greedy (!).
747  *
748  *    - Added a new interceptor ijb-send-banner, which
749  *      sends back the "Junkbuster" gif. Without imagelist or
750  *      MSIE detection support, or if tinygif = 1, or the
751  *      URL isn't recognized as an imageurl, a lame HTML
752  *      explanation is sent instead.
753  *
754  *    - Added new feature, which permits blocking remote
755  *      script redirects and firing back a local redirect
756  *      to the browser.
757  *      The feature is conditionally compiled, i.e. it
758  *      can be disabled with --disable-fast-redirects,
759  *      plus it must be activated by a "fast-redirects"
760  *      line in the config file, has its own log level
761  *      and of course wants to be displayed by show-proxy-args
762  *      Note: Boy, all the #ifdefs in 1001 locations and
763  *      all the fumbling with configure.in and acconfig.h
764  *      were *way* more work than the feature itself :-(
765  *
766  *    - Because a generic redirect template was needed for
767  *      this, tinygif = 3 now uses the same.
768  *
769  *    - Moved GIFs, and other static HTTP response templates
770  *      to project.h
771  *
772  *    - Some minor fixes
773  *
774  *    - Removed some >400 CRs again (Jon, you really worked
775  *      a lot! ;-)
776  *
777  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
778  *    Version 2.9.4 checkin.
779  *    - Merged popupfile and cookiefile, and added control over PCRS
780  *      filtering, in new "permissionsfile".
781  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
782  *      file error you now get a message box (in the Win32 GUI) rather
783  *      than the program exiting with no explanation.
784  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
785  *      skipping.
786  *    - Removed tabs from "config"
787  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
788  *    - Bumped up version number.
789  *
790  *    Revision 1.2  2001/05/17 23:02:36  oes
791  *     - Made referrer option accept 'L' as a substitute for '§'
792  *
793  *    Revision 1.1.1.1  2001/05/15 13:59:01  oes
794  *    Initial import of version 2.9.3 source tree
795  *
796  *
797  *********************************************************************/
798 \f
799
800 #include "config.h"
801
802 #ifndef _WIN32
803 #include <stdio.h>
804 #include <sys/types.h>
805 #endif
806
807 #include <stdlib.h>
808 #include <ctype.h>
809 #include <assert.h>
810 #include <string.h>
811
812 #ifdef __GLIBC__
813 /*
814  * Convince GNU's libc to provide a strptime prototype.
815  */
816 #define __USE_XOPEN
817 #endif /*__GLIBC__ */
818 #include <time.h>
819
820 #ifdef FEATURE_ZLIB
821 #include <zlib.h>
822 #endif
823
824 #if !defined(_WIN32) && !defined(__OS2__)
825 #include <unistd.h>
826 #endif
827
828 #include "project.h"
829
830 #ifdef FEATURE_PTHREAD
831 #include "jcc.h"
832 /* jcc.h is for mutex semapores only */
833 #endif /* def FEATURE_PTHREAD */
834 #include "list.h"
835 #include "parsers.h"
836 #include "encode.h"
837 #include "ssplit.h"
838 #include "errlog.h"
839 #include "jbsockets.h"
840 #include "miscutil.h"
841 #include "list.h"
842 #include "actions.h"
843 #include "filters.h"
844
845 #ifndef HAVE_STRPTIME
846 #include "strptime.h"
847 #endif
848
849 const char parsers_h_rcs[] = PARSERS_H_VERSION;
850
851 /* Fix a problem with Solaris.  There should be no effect on other
852  * platforms.
853  * Solaris's isspace() is a macro which uses its argument directly
854  * as an array index.  Therefore we need to make sure that high-bit
855  * characters generate +ve values, and ideally we also want to make
856  * the argument match the declared parameter type of "int".
857  *
858  * Why did they write a character function that can't take a simple
859  * "char" argument?  Doh!
860  */
861 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
862 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
863
864 static char *get_header_line(struct iob *iob);
865 static jb_err scan_headers(struct client_state *csp);
866 static jb_err header_tagger(struct client_state *csp, char *header);
867 static jb_err parse_header_time(const char *header_time, time_t *result);
868
869 static jb_err crumble                   (struct client_state *csp, char **header);
870 static jb_err connection                (struct client_state *csp, char **header);
871 static jb_err filter_header             (struct client_state *csp, char **header);
872 static jb_err client_referrer           (struct client_state *csp, char **header);
873 static jb_err client_uagent             (struct client_state *csp, char **header);
874 static jb_err client_ua                 (struct client_state *csp, char **header);
875 static jb_err client_from               (struct client_state *csp, char **header);
876 static jb_err client_send_cookie        (struct client_state *csp, char **header);
877 static jb_err client_x_forwarded        (struct client_state *csp, char **header);
878 static jb_err client_accept_encoding    (struct client_state *csp, char **header);
879 static jb_err client_te                 (struct client_state *csp, char **header);
880 static jb_err client_max_forwards       (struct client_state *csp, char **header);
881 static jb_err client_host               (struct client_state *csp, char **header);
882 static jb_err client_if_modified_since  (struct client_state *csp, char **header);
883 static jb_err client_accept_language    (struct client_state *csp, char **header);
884 static jb_err client_if_none_match      (struct client_state *csp, char **header);
885 static jb_err crunch_client_header      (struct client_state *csp, char **header);
886 static jb_err client_x_filter           (struct client_state *csp, char **header);
887 static jb_err client_range              (struct client_state *csp, char **header);
888 static jb_err server_set_cookie         (struct client_state *csp, char **header);
889 static jb_err server_content_type       (struct client_state *csp, char **header);
890 static jb_err server_content_length     (struct client_state *csp, char **header);
891 static jb_err server_content_md5        (struct client_state *csp, char **header);
892 static jb_err server_content_encoding   (struct client_state *csp, char **header);
893 static jb_err server_transfer_coding    (struct client_state *csp, char **header);
894 static jb_err server_http               (struct client_state *csp, char **header);
895 static jb_err crunch_server_header      (struct client_state *csp, char **header);
896 static jb_err server_last_modified      (struct client_state *csp, char **header);
897 static jb_err server_content_disposition(struct client_state *csp, char **header);
898
899 static jb_err client_host_adder       (struct client_state *csp);
900 static jb_err client_xtra_adder       (struct client_state *csp);
901 static jb_err connection_close_adder  (struct client_state *csp); 
902
903 static jb_err create_forged_referrer(char **header, const char *hostport);
904 static jb_err create_fake_referrer(char **header, const char *fake_referrer);
905 static jb_err handle_conditional_hide_referrer_parameter(char **header,
906    const char *host, const int parameter_conditional_block);
907
908 /*
909  * List of functions to run on a list of headers.
910  */
911 struct parsers
912 {
913    /** The header prefix to match */
914    const char *str;
915    
916    /** The length of the prefix to match */
917    const size_t len;
918    
919    /** The function to apply to this line */
920    const parser_func_ptr parser;
921 };
922
923 static const struct parsers client_patterns[] = {
924    { "referer:",                  8,   client_referrer },
925    { "user-agent:",              11,   client_uagent },
926    { "ua-",                       3,   client_ua },
927    { "from:",                     5,   client_from },
928    { "cookie:",                   7,   client_send_cookie },
929    { "x-forwarded-for:",         16,   client_x_forwarded },
930    { "Accept-Encoding:",         16,   client_accept_encoding },
931    { "TE:",                       3,   client_te },
932    { "Host:",                     5,   client_host },
933    { "if-modified-since:",       18,   client_if_modified_since },
934    { "Keep-Alive:",              11,   crumble },
935    { "connection:",              11,   connection },
936    { "proxy-connection:",        17,   crumble },
937    { "max-forwards:",            13,   client_max_forwards },
938    { "Accept-Language:",         16,   client_accept_language },
939    { "if-none-match:",           14,   client_if_none_match },
940    { "Range:",                    6,   client_range },
941    { "Request-Range:",           14,   client_range },
942    { "If-Range:",                 9,   client_range },
943    { "X-Filter:",                 9,   client_x_filter },
944    { "*",                         0,   crunch_client_header },
945    { "*",                         0,   filter_header },
946    { NULL,                        0,   NULL }
947 };
948
949 static const struct parsers server_patterns[] = {
950    { "HTTP/",                     5, server_http },
951    { "set-cookie:",              11, server_set_cookie },
952    { "connection:",              11, connection },
953    { "Content-Type:",            13, server_content_type },
954    { "Content-MD5:",             12, server_content_md5 },
955    { "Content-Encoding:",        17, server_content_encoding },
956    { "Transfer-Encoding:",       18, server_transfer_coding },
957    { "Keep-Alive:",              11, crumble },
958    { "content-disposition:",     20, server_content_disposition },
959    { "Last-Modified:",           14, server_last_modified },
960    { "*",                         0, crunch_server_header },
961    { "*",                         0, filter_header },
962    { NULL, 0, NULL }
963 };
964
965 static const add_header_func_ptr add_client_headers[] = {
966    client_host_adder,
967    client_xtra_adder,
968    /* Temporarily disabled:    client_accept_encoding_adder, */
969    connection_close_adder,
970    NULL
971 };
972
973 static const add_header_func_ptr add_server_headers[] = {
974    connection_close_adder,
975    NULL
976 };
977
978 /*********************************************************************
979  *
980  * Function    :  flush_socket
981  *
982  * Description :  Write any pending "buffered" content.
983  *
984  * Parameters  :
985  *          1  :  fd = file descriptor of the socket to read
986  *          2  :  iob = The I/O buffer to flush, usually csp->iob.
987  *
988  * Returns     :  On success, the number of bytes written are returned (zero
989  *                indicates nothing was written).  On error, -1 is returned,
990  *                and errno is set appropriately.  If count is zero and the
991  *                file descriptor refers to a regular file, 0 will be
992  *                returned without causing any other effect.  For a special
993  *                file, the results are not portable.
994  *
995  *********************************************************************/
996 int flush_socket(jb_socket fd, struct iob *iob)
997 {
998    int len = iob->eod - iob->cur;
999
1000    if (len <= 0)
1001    {
1002       return(0);
1003    }
1004
1005    if (write_socket(fd, iob->cur, (size_t)len))
1006    {
1007       return(-1);
1008    }
1009    iob->eod = iob->cur = iob->buf;
1010    return(len);
1011
1012 }
1013
1014
1015 /*********************************************************************
1016  *
1017  * Function    :  add_to_iob
1018  *
1019  * Description :  Add content to the buffered page, expanding the
1020  *                buffer if necessary.
1021  *
1022  * Parameters  :
1023  *          1  :  csp = Current client state (buffers, headers, etc...)
1024  *          2  :  buf = holds the content to be added to the page
1025  *          3  :  n = number of bytes to be added
1026  *
1027  * Returns     :  JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
1028  *                or buffer limit reached.
1029  *
1030  *********************************************************************/
1031 jb_err add_to_iob(struct client_state *csp, char *buf, int n)
1032 {
1033    struct iob *iob = csp->iob;
1034    size_t used, offset, need, want;
1035    char *p;
1036
1037    if (n <= 0) return JB_ERR_OK;
1038
1039    used   = (size_t)(iob->eod - iob->buf);
1040    offset = (size_t)(iob->cur - iob->buf);
1041    need   = used + (size_t)n + 1;
1042
1043    /*
1044     * If the buffer can't hold the new data, extend it first.
1045     * Use the next power of two if possible, else use the actual need.
1046     */
1047    if (need > csp->config->buffer_limit)
1048    {
1049       log_error(LOG_LEVEL_INFO, "Buffer limit reached while extending the buffer (iob)");
1050       return JB_ERR_MEMORY;
1051    }
1052
1053    if (need > iob->size)
1054    {
1055       for (want = csp->iob->size ? csp->iob->size : 512; want <= need;) want *= 2;
1056       
1057       if (want <= csp->config->buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
1058       {
1059          iob->size = want;
1060       }
1061       else if (NULL != (p = (char *)realloc(iob->buf, need)))
1062       {
1063          iob->size = need;
1064       }
1065       else
1066       {
1067          log_error(LOG_LEVEL_ERROR, "Extending the buffer (iob) failed: %E");
1068          return JB_ERR_MEMORY;
1069       }
1070
1071       /* Update the iob pointers */
1072       iob->cur = p + offset;
1073       iob->eod = p + used;
1074       iob->buf = p;
1075    }
1076
1077    /* copy the new data into the iob buffer */
1078    memcpy(iob->eod, buf, (size_t)n);
1079
1080    /* point to the end of the data */
1081    iob->eod += n;
1082
1083    /* null terminate == cheap insurance */
1084    *iob->eod = '\0';
1085
1086    return JB_ERR_OK;
1087
1088 }
1089
1090
1091 #ifdef FEATURE_ZLIB
1092 /*********************************************************************
1093  *
1094  * Function    :  decompress_iob
1095  *
1096  * Description :  Decompress buffered page, expanding the
1097  *                buffer as necessary.  csp->iob->cur
1098  *                should point to the the beginning of the
1099  *                compressed data block.
1100  *
1101  * Parameters  :
1102  *          1  :  csp = Current client state (buffers, headers, etc...)
1103  *
1104  * Returns     :  JB_ERR_OK on success,
1105  *                JB_ERR_MEMORY if out-of-memory limit reached, and
1106  *                JB_ERR_COMPRESS if error decompressing buffer.
1107  *
1108  *********************************************************************/
1109 jb_err decompress_iob(struct client_state *csp)
1110 {
1111    char  *buf;       /* new, uncompressed buffer */
1112    char  *cur;       /* Current iob position (to keep the original 
1113                       * iob->cur unmodified if we return early) */
1114    size_t bufsize;   /* allocated size of the new buffer */
1115    size_t old_size;  /* Content size before decompression */
1116    size_t skip_size; /* Number of bytes at the beginning of the iob
1117                         that we should NOT decompress. */
1118    int status;       /* return status of the inflate() call */
1119    z_stream zstr;    /* used by calls to zlib */
1120
1121    assert(csp->iob->cur - csp->iob->buf > 0);
1122    assert(csp->iob->eod - csp->iob->cur > 0);
1123
1124    bufsize = csp->iob->size;
1125    skip_size = (size_t)(csp->iob->cur - csp->iob->buf);
1126    old_size = (size_t)(csp->iob->eod - csp->iob->cur);
1127
1128    cur = csp->iob->cur;
1129
1130    if (bufsize < 10)
1131    {
1132       /*
1133        * This is to protect the parsing of gzipped data,
1134        * but it should(?) be valid for deflated data also.
1135        */
1136       log_error(LOG_LEVEL_ERROR, "Buffer too small decompressing iob");
1137       return JB_ERR_COMPRESS;
1138    }
1139
1140    if (csp->content_type & CT_GZIP)
1141    {
1142       /*
1143        * Our task is slightly complicated by the facts that data
1144        * compressed by gzip does not include a zlib header, and
1145        * that there is no easily accessible interface in zlib to
1146        * handle a gzip header. We strip off the gzip header by
1147        * hand, and later inform zlib not to expect a header.
1148        */
1149
1150       /*
1151        * Strip off the gzip header. Please see RFC 1952 for more
1152        * explanation of the appropriate fields.
1153        */
1154       if ((*cur++ != (char)0x1f)
1155        || (*cur++ != (char)0x8b)
1156        || (*cur++ != Z_DEFLATED))
1157       {
1158          log_error(LOG_LEVEL_ERROR, "Invalid gzip header when decompressing");
1159          return JB_ERR_COMPRESS;
1160       }
1161       else
1162       {
1163          int flags = *cur++;
1164          /*
1165           * XXX: These magic numbers should be replaced
1166           * with macros to give a better idea what they do.
1167           */
1168          if (flags & 0xe0)
1169          {
1170             /* The gzip header has reserved bits set; bail out. */
1171             log_error(LOG_LEVEL_ERROR, "Invalid gzip header flags when decompressing");
1172             return JB_ERR_COMPRESS;
1173          }
1174          cur += 6;
1175
1176          /* Skip extra fields if necessary. */
1177          if (flags & 0x04)
1178          {
1179             /*
1180              * Skip a given number of bytes, specified
1181              * as a 16-bit little-endian value.
1182              */
1183             /*
1184              * XXX: This code used to be:
1185              * 
1186              * csp->iob->cur += *csp->iob->cur++ + (*csp->iob->cur++ << 8);
1187              *
1188              * which I had to change into:
1189              *
1190              * cur += *cur++ + (*cur++ << 8);
1191              *
1192              * at which point gcc43 finally noticed that the value
1193              * of cur is undefined (it depends on which of the
1194              * summands is evaluated first).
1195              *
1196              * I haven't come across a site where this
1197              * code is actually executed yet, but I hope
1198              * it works anyway.
1199              */
1200             int skip_bytes;
1201             skip_bytes = *cur++;
1202             skip_bytes = *cur++ << 8;
1203
1204             assert(skip_bytes == *csp->iob->cur - 2 + ((*csp->iob->cur - 1) << 8));
1205
1206             /*
1207              * The number of bytes to skip should be positive
1208              * and we'd like to stay in the buffer.
1209              */
1210             if ((skip_bytes < 0) || (skip_bytes >= (csp->iob->eod - cur)))
1211             {
1212                log_error(LOG_LEVEL_ERROR,
1213                   "Unreasonable amount of bytes to skip (%d). Stopping decompression",
1214                   skip_bytes);
1215                return JB_ERR_COMPRESS;
1216             }
1217             log_error(LOG_LEVEL_INFO,
1218                "Skipping %d bytes for gzip compression. Does this sound right?",
1219                skip_bytes);
1220             cur += skip_bytes;
1221          }
1222
1223          /* Skip the filename if necessary. */
1224          if (flags & 0x08)
1225          {
1226             /* A null-terminated string is supposed to follow. */
1227             while (*cur++ && (cur < csp->iob->eod));
1228
1229          }
1230
1231          /* Skip the comment if necessary. */
1232          if (flags & 0x10)
1233          {
1234             /* A null-terminated string is supposed to follow. */
1235             while (*cur++ && (cur < csp->iob->eod));
1236          }
1237
1238          /* Skip the CRC if necessary. */
1239          if (flags & 0x02)
1240          {
1241             cur += 2;
1242          }
1243
1244          if (cur >= csp->iob->eod)
1245          {
1246             /*
1247              * If the current position pointer reached or passed
1248              * the buffer end, we were obviously tricked to skip
1249              * too much.
1250              */
1251             log_error(LOG_LEVEL_ERROR,
1252                "Malformed gzip header detected. Aborting decompression.");
1253             return JB_ERR_COMPRESS;
1254          }
1255       }
1256    }
1257    else if (csp->content_type & CT_DEFLATE)
1258    {
1259       /*
1260        * XXX: The debug level should be lowered
1261        * before the next stable release.
1262        */
1263       log_error(LOG_LEVEL_INFO, "Decompressing deflated iob: %d", *cur);
1264       /*
1265        * In theory (that is, according to RFC 1950), deflate-compressed
1266        * data should begin with a two-byte zlib header and have an
1267        * adler32 checksum at the end. It seems that in practice only
1268        * the raw compressed data is sent. Note that this means that
1269        * we are not RFC 1950-compliant here, but the advantage is that
1270        * this actually works. :)
1271        *
1272        * We add a dummy null byte to tell zlib where the data ends,
1273        * and later inform it not to expect a header.
1274        *
1275        * Fortunately, add_to_iob() has thoughtfully null-terminated
1276        * the buffer; we can just increment the end pointer to include
1277        * the dummy byte.  
1278        */
1279       csp->iob->eod++;
1280    }
1281    else
1282    {
1283       log_error(LOG_LEVEL_ERROR,
1284          "Unable to determine compression format for decompression");
1285       return JB_ERR_COMPRESS;
1286    }
1287
1288    /* Set up the fields required by zlib. */
1289    zstr.next_in  = (Bytef *)cur;
1290    zstr.avail_in = (unsigned int)(csp->iob->eod - cur);
1291    zstr.zalloc   = Z_NULL;
1292    zstr.zfree    = Z_NULL;
1293    zstr.opaque   = Z_NULL;
1294
1295    /*
1296     * Passing -MAX_WBITS to inflateInit2 tells the library
1297     * that there is no zlib header.
1298     */
1299    if (inflateInit2 (&zstr, -MAX_WBITS) != Z_OK)
1300    {
1301       log_error(LOG_LEVEL_ERROR, "Error initializing decompression");
1302       return JB_ERR_COMPRESS;
1303    }
1304
1305    /*
1306     * Next, we allocate new storage for the inflated data.
1307     * We don't modify the existing iob yet, so in case there
1308     * is error in decompression we can recover gracefully.
1309     */
1310    buf = zalloc(bufsize);
1311    if (NULL == buf)
1312    {
1313       log_error(LOG_LEVEL_ERROR, "Out of memory decompressing iob");
1314       return JB_ERR_MEMORY;
1315    }
1316
1317    assert(bufsize >= skip_size);
1318    memcpy(buf, csp->iob->buf, skip_size);
1319    zstr.avail_out = bufsize - skip_size;
1320    zstr.next_out  = (Bytef *)buf + skip_size;
1321
1322    /* Try to decompress the whole stream in one shot. */
1323    while (Z_BUF_ERROR == (status = inflate(&zstr, Z_FINISH)))
1324    {
1325       /* We need to allocate more memory for the output buffer. */
1326
1327       char *tmpbuf;                /* used for realloc'ing the buffer */
1328       size_t oldbufsize = bufsize; /* keep track of the old bufsize */
1329
1330       /*
1331        * If zlib wants more data then there's a problem, because
1332        * the complete compressed file should have been buffered.
1333        */
1334       if (0 == zstr.avail_in)
1335       {
1336          log_error(LOG_LEVEL_ERROR, "Unexpected end of compressed iob");
1337          return JB_ERR_COMPRESS;
1338       }
1339
1340       /*
1341        * If we tried the limit and still didn't have enough
1342        * memory, just give up.
1343        */
1344       if (bufsize == csp->config->buffer_limit)
1345       {
1346          log_error(LOG_LEVEL_ERROR, "Buffer limit reached while decompressing iob");
1347          return JB_ERR_MEMORY;
1348       }
1349
1350       /* Try doubling the buffer size each time. */
1351       bufsize *= 2;
1352
1353       /* Don't exceed the buffer limit. */
1354       if (bufsize > csp->config->buffer_limit)
1355       {
1356          bufsize = csp->config->buffer_limit;
1357       }
1358     
1359       /* Try to allocate the new buffer. */
1360       tmpbuf = realloc(buf, bufsize);
1361       if (NULL == tmpbuf)
1362       {
1363          log_error(LOG_LEVEL_ERROR, "Out of memory decompressing iob");
1364          freez(buf);
1365          return JB_ERR_MEMORY;
1366       }
1367       else
1368       {
1369          char *oldnext_out = (char *)zstr.next_out;
1370
1371          /*
1372           * Update the fields for inflate() to use the new
1373           * buffer, which may be in a location different from
1374           * the old one.
1375           */
1376          zstr.avail_out += bufsize - oldbufsize;
1377          zstr.next_out   = (Bytef *)tmpbuf + bufsize - zstr.avail_out;
1378
1379          /*
1380           * Compare with an uglier method of calculating these values
1381           * that doesn't require the extra oldbufsize variable.
1382           */
1383          assert(zstr.avail_out == tmpbuf + bufsize - (char *)zstr.next_out);
1384          assert((char *)zstr.next_out == tmpbuf + ((char *)oldnext_out - buf));
1385          assert(zstr.avail_out > 0);
1386
1387          buf = tmpbuf;
1388       }
1389    }
1390
1391    if (Z_STREAM_ERROR == inflateEnd(&zstr))
1392    {
1393       log_error(LOG_LEVEL_ERROR,
1394          "Inconsistent stream state after decompression: %s", zstr.msg);
1395       /*
1396        * XXX: Intentionally no return.
1397        *
1398        * According to zlib.h, Z_STREAM_ERROR is returned
1399        * "if the stream state was inconsistent".
1400        *
1401        * I assume in this case inflate()'s status
1402        * would also be something different than Z_STREAM_END
1403        * so this check should be redundant, but lets see.
1404        */
1405    }
1406
1407    if (status != Z_STREAM_END)
1408    {
1409       /* We failed to decompress the stream. */
1410       log_error(LOG_LEVEL_ERROR,
1411          "Error in decompressing to the buffer (iob): %s", zstr.msg);
1412       return JB_ERR_COMPRESS;
1413    }
1414
1415    /*
1416     * Finally, we can actually update the iob, since the
1417     * decompression was successful. First, free the old
1418     * buffer.
1419     */
1420    freez(csp->iob->buf);
1421
1422    /* Now, update the iob to use the new buffer. */
1423    csp->iob->buf  = buf;
1424    csp->iob->cur  = csp->iob->buf + skip_size;
1425    csp->iob->eod  = (char *)zstr.next_out;
1426    csp->iob->size = bufsize;
1427   
1428    /*
1429     * Make sure the new uncompressed iob obeys some minimal
1430     * consistency conditions.
1431     */
1432    if ((csp->iob->buf <  csp->iob->cur)
1433     && (csp->iob->cur <= csp->iob->eod)
1434     && (csp->iob->eod <= csp->iob->buf + csp->iob->size))
1435    {
1436       const size_t new_size = (size_t)(csp->iob->eod - csp->iob->cur);
1437       if (new_size > 0)
1438       {
1439          log_error(LOG_LEVEL_RE_FILTER,
1440             "Decompression successful. Old size: %d, new size: %d.",
1441             old_size, new_size);
1442       }
1443       else
1444       {
1445          /* zlib thinks this is OK, so lets do the same. */
1446          log_error(LOG_LEVEL_INFO, "Decompression didn't result in any content.");
1447       }
1448    }
1449    else
1450    {
1451       /* It seems that zlib did something weird. */
1452       log_error(LOG_LEVEL_ERROR,
1453          "Unexpected error decompressing the buffer (iob): %d==%d, %d>%d, %d<%d",
1454          csp->iob->cur, csp->iob->buf + skip_size, csp->iob->eod, csp->iob->buf,
1455          csp->iob->eod, csp->iob->buf + csp->iob->size);
1456       return JB_ERR_COMPRESS;
1457    }
1458
1459    return JB_ERR_OK;
1460
1461 }
1462 #endif /* defined(FEATURE_ZLIB) */
1463
1464
1465 /*********************************************************************
1466  *
1467  * Function    :  string_move
1468  *
1469  * Description :  memmove wrapper to move the last part of a string
1470  *                towards the beginning, overwriting the part in
1471  *                the middle. strlcpy() can't be used here as the
1472  *                strings overlap.
1473  *
1474  * Parameters  :
1475  *          1  :  dst = Destination to overwrite
1476  *          2  :  src = Source to move.
1477  *
1478  * Returns     :  N/A
1479  *
1480  *********************************************************************/
1481 static void string_move(char *dst, char *src)
1482 {
1483    assert(dst < src);
1484
1485    /* +1 to copy the terminating nul as well. */
1486    memmove(dst, src, strlen(src)+1);
1487 }
1488
1489
1490 /*********************************************************************
1491  *
1492  * Function    :  normalize_lws
1493  *
1494  * Description :  Reduces unquoted linear white space in headers
1495  *                to a single space in accordance with RFC 2616 2.2.
1496  *                This simplifies parsing and filtering later on.
1497  *
1498  *                XXX: Remove log messages before
1499  *                     the next stable release?
1500  *
1501  * Parameters  :
1502  *          1  :  header = A header with linear white space to reduce.
1503  *
1504  * Returns     :  N/A
1505  *
1506  *********************************************************************/
1507 static void normalize_lws(char *header)
1508 {
1509    char *p = header;
1510
1511    while (*p != '\0')
1512    {
1513       if (ijb_isspace(*p) && ijb_isspace(*(p+1)))
1514       {
1515          char *q = p+1;
1516
1517          while (ijb_isspace(*q))
1518          {
1519             q++;
1520          }
1521          log_error(LOG_LEVEL_HEADER, "Reducing white space in '%s'", header);
1522          string_move(p+1, q);
1523       }
1524
1525       if (*p == '\t')
1526       {
1527          log_error(LOG_LEVEL_HEADER,
1528             "Converting tab to space in '%s'", header);
1529          *p = ' ';
1530       }
1531       else if (*p == '"')
1532       {
1533          char *end_of_token = strstr(p+1, "\"");
1534
1535          if (NULL != end_of_token)
1536          {
1537             /* Don't mess with quoted text. */
1538             p = end_of_token;
1539          }
1540          else
1541          {
1542             log_error(LOG_LEVEL_HEADER,
1543                "Ignoring single quote in '%s'", header);
1544          }
1545       }
1546       p++;
1547    }
1548
1549    p = strchr(header, ':');
1550    if ((p != NULL) && (p != header) && ijb_isspace(*(p-1)))
1551    {
1552       /*
1553        * There's still space before the colon.
1554        * We don't want it.
1555        */
1556       string_move(p-1, p);
1557    }
1558 }
1559
1560
1561 /*********************************************************************
1562  *
1563  * Function    :  get_header
1564  *
1565  * Description :  This (odd) routine will parse the csp->iob
1566  *                to get the next complete header.
1567  *
1568  * Parameters  :
1569  *          1  :  iob = The I/O buffer to parse, usually csp->iob.
1570  *
1571  * Returns     :  Any one of the following:
1572  *
1573  * 1) a pointer to a dynamically allocated string that contains a header line
1574  * 2) NULL  indicating that the end of the header was reached
1575  * 3) ""    indicating that the end of the iob was reached before finding
1576  *          a complete header line.
1577  *
1578  *********************************************************************/
1579 char *get_header(struct iob *iob)
1580 {
1581    char *header;
1582
1583    header = get_header_line(iob);
1584
1585    if ((header == NULL) || (*header == '\0'))
1586    {
1587       /*
1588        * No complete header read yet, tell the client.
1589        */
1590       return header;
1591    }
1592
1593    while ((iob->cur[0] == ' ') || (iob->cur[0] == '\t'))
1594    {
1595       /*
1596        * Header spans multiple lines, append the next one.
1597        */
1598       char *continued_header;
1599       
1600       continued_header = get_header_line(iob);
1601       if ((continued_header == NULL) || (*continued_header == '\0'))
1602       {
1603          /*
1604           * No complete header read yet, return what we got.
1605           * XXX: Should "unread" header instead.
1606           */
1607          log_error(LOG_LEVEL_INFO,
1608             "Failed to read a multi-line header properly: '%s'",
1609             header);
1610          break;
1611       }
1612
1613       if (JB_ERR_OK != string_join(&header, continued_header))
1614       {
1615          log_error(LOG_LEVEL_FATAL,
1616             "Out of memory while appending multiple headers.");
1617       }
1618       else
1619       {
1620          /* XXX: remove before next stable release. */
1621          log_error(LOG_LEVEL_HEADER,
1622             "Merged multiple header lines to: '%s'",
1623             header);
1624       }
1625    }
1626
1627    normalize_lws(header);
1628
1629    return header;
1630
1631 }
1632
1633
1634 /*********************************************************************
1635  *
1636  * Function    :  get_header_line
1637  *
1638  * Description :  This (odd) routine will parse the csp->iob
1639  *                to get the next header line.
1640  *
1641  * Parameters  :
1642  *          1  :  iob = The I/O buffer to parse, usually csp->iob.
1643  *
1644  * Returns     :  Any one of the following:
1645  *
1646  * 1) a pointer to a dynamically allocated string that contains a header line
1647  * 2) NULL  indicating that the end of the header was reached
1648  * 3) ""    indicating that the end of the iob was reached before finding
1649  *          a complete header line.
1650  *
1651  *********************************************************************/
1652 static char *get_header_line(struct iob *iob)
1653 {
1654    char *p, *q, *ret;
1655
1656    if ((iob->cur == NULL)
1657       || ((p = strchr(iob->cur, '\n')) == NULL))
1658    {
1659       return(""); /* couldn't find a complete header */
1660    }
1661
1662    *p = '\0';
1663
1664    ret = strdup(iob->cur);
1665    if (ret == NULL)
1666    {
1667       /* FIXME No way to handle error properly */
1668       log_error(LOG_LEVEL_FATAL, "Out of memory in get_header_line()");
1669    }
1670
1671    iob->cur = p+1;
1672
1673    if ((q = strchr(ret, '\r')) != NULL) *q = '\0';
1674
1675    /* is this a blank line (i.e. the end of the header) ? */
1676    if (*ret == '\0')
1677    {
1678       freez(ret);
1679       return(NULL);
1680    }
1681
1682    return(ret);
1683
1684 }
1685
1686
1687 /*********************************************************************
1688  *
1689  * Function    :  get_header_value
1690  *
1691  * Description :  Get the value of a given header from a chained list
1692  *                of header lines or return NULL if no such header is
1693  *                present in the list.
1694  *
1695  * Parameters  :
1696  *          1  :  header_list = pointer to list
1697  *          2  :  header_name = string with name of header to look for.
1698  *                              Trailing colon required, capitalization
1699  *                              doesn't matter.
1700  *
1701  * Returns     :  NULL if not found, else value of header
1702  *
1703  *********************************************************************/
1704 char *get_header_value(const struct list *header_list, const char *header_name)
1705 {
1706    struct list_entry *cur_entry;
1707    char *ret = NULL;
1708    size_t length = 0;
1709
1710    assert(header_list);
1711    assert(header_name);
1712    length = strlen(header_name);
1713
1714    for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
1715    {
1716       if (cur_entry->str)
1717       {
1718          if (!strncmpic(cur_entry->str, header_name, length))
1719          {
1720             /*
1721              * Found: return pointer to start of value
1722              */
1723             ret = (char *) (cur_entry->str + length);
1724             while (*ret && ijb_isspace(*ret)) ret++;
1725             return(ret);
1726          }
1727       }
1728    }
1729
1730    /* 
1731     * Not found
1732     */
1733    return NULL;
1734
1735 }
1736
1737
1738 /*********************************************************************
1739  *
1740  * Function    :  scan_headers
1741  *
1742  * Description :  Scans headers, applies tags and updates action bits. 
1743  *
1744  * Parameters  :
1745  *          1  :  csp = Current client state (buffers, headers, etc...)
1746  *
1747  * Returns     :  JB_ERR_OK
1748  *
1749  *********************************************************************/
1750 static jb_err scan_headers(struct client_state *csp)
1751 {
1752    struct list_entry *h; /* Header */
1753    jb_err err = JB_ERR_OK;
1754
1755    for (h = csp->headers->first; (err == JB_ERR_OK) && (h != NULL) ; h = h->next)
1756    {
1757       /* Header crunch()ed in previous run? -> ignore */
1758       if (h->str == NULL) continue;
1759       log_error(LOG_LEVEL_HEADER, "scan: %s", h->str);
1760       err = header_tagger(csp, h->str);
1761    }
1762
1763    return err;
1764 }
1765
1766
1767 /*********************************************************************
1768  *
1769  * Function    :  sed
1770  *
1771  * Description :  add, delete or modify lines in the HTTP header streams.
1772  *                On entry, it receives a linked list of headers space
1773  *                that was allocated dynamically (both the list nodes
1774  *                and the header contents).
1775  *
1776  *                As a side effect it frees the space used by the original
1777  *                header lines.
1778  *
1779  * Parameters  :
1780  *          1  :  csp = Current client state (buffers, headers, etc...)
1781  *          2  :  filter_server_headers = Boolean to switch between
1782  *                                        server and header filtering.
1783  *
1784  * Returns     :  JB_ERR_OK in case off success, or
1785  *                JB_ERR_MEMORY on out-of-memory error.
1786  *
1787  *********************************************************************/
1788 jb_err sed(struct client_state *csp, int filter_server_headers)
1789 {
1790    /* XXX: use more descriptive names. */
1791    struct list_entry *p;
1792    const struct parsers *v;
1793    const add_header_func_ptr *f;
1794    jb_err err = JB_ERR_OK;
1795
1796    if (filter_server_headers)
1797    {
1798       v = server_patterns;
1799       f = add_server_headers;
1800    }
1801    else
1802    {
1803       v = client_patterns;
1804       f = add_client_headers;
1805    }
1806
1807    scan_headers(csp);
1808
1809    while ((err == JB_ERR_OK) && (v->str != NULL))
1810    {
1811       for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL); p = p->next)
1812       {
1813          /* Header crunch()ed in previous run? -> ignore */
1814          if (p->str == NULL) continue;
1815
1816          /* Does the current parser handle this header? */
1817          if ((strncmpic(p->str, v->str, v->len) == 0) ||
1818              (v->len == CHECK_EVERY_HEADER_REMAINING))
1819          {
1820             err = v->parser(csp, &(p->str));
1821          }
1822       }
1823       v++;
1824    }
1825
1826    /* place additional headers on the csp->headers list */
1827    while ((err == JB_ERR_OK) && (*f))
1828    {
1829       err = (*f)(csp);
1830       f++;
1831    }
1832
1833    return err;
1834 }
1835
1836
1837 /*********************************************************************
1838  *
1839  * Function    :  update_server_headers
1840  *
1841  * Description :  Updates server headers after the body has been modified.
1842  *
1843  * Parameters  :
1844  *          1  :  csp = Current client state (buffers, headers, etc...)
1845  *
1846  * Returns     :  JB_ERR_OK in case off success, or
1847  *                JB_ERR_MEMORY on out-of-memory error.
1848  *
1849  *********************************************************************/
1850 jb_err update_server_headers(struct client_state *csp)
1851 {
1852    jb_err err = JB_ERR_OK;
1853
1854    static const struct parsers server_patterns_light[] = {
1855       { "Content-Length:",    15, server_content_length },
1856       { "Transfer-Encoding:", 18, server_transfer_coding },
1857 #ifdef FEATURE_ZLIB
1858       { "Content-Encoding:",  17, server_content_encoding },
1859 #endif /* def FEATURE_ZLIB */
1860       { NULL, 0, NULL }
1861    };
1862
1863    if (strncmpic(csp->http->cmd, "HEAD", 4))
1864    {
1865       const struct parsers *v;
1866       struct list_entry *p;
1867
1868       for (v = server_patterns_light; (err == JB_ERR_OK) && (v->str != NULL); v++)
1869       {
1870          for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL); p = p->next)
1871          {
1872             /* Header crunch()ed in previous run? -> ignore */
1873             if (p->str == NULL) continue;
1874
1875             /* Does the current parser handle this header? */
1876             if (strncmpic(p->str, v->str, v->len) == 0)
1877             {
1878                err = v->parser(csp, (char **)&(p->str));
1879             }
1880          }
1881       }
1882    }
1883
1884    return err;
1885 }
1886
1887
1888 /*********************************************************************
1889  *
1890  * Function    :  header_tagger
1891  *
1892  * Description :  Executes all text substitutions from applying
1893  *                tag actions and saves the result as tag.
1894  *
1895  *                XXX: Shares enough code with filter_header() and
1896  *                pcrs_filter_response() to warrant some helper functions.
1897  *
1898  * Parameters  :
1899  *          1  :  csp = Current client state (buffers, headers, etc...)
1900  *          2  :  header = Header that is used as tagger input
1901  *
1902  * Returns     :  JB_ERR_OK on success and always succeeds
1903  *
1904  *********************************************************************/
1905 static jb_err header_tagger(struct client_state *csp, char *header)
1906 {
1907    int wanted_filter_type;
1908    int multi_action_index;
1909    int i;
1910    pcrs_job *job;
1911
1912    struct file_list *fl;
1913    struct re_filterfile_spec *b;
1914    struct list_entry *tag_name;
1915
1916    int found_filters = 0;
1917    const size_t header_length = strlen(header);
1918
1919    if (csp->flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
1920    {
1921       wanted_filter_type = FT_SERVER_HEADER_TAGGER;
1922       multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
1923    }
1924    else
1925    {
1926       wanted_filter_type = FT_CLIENT_HEADER_TAGGER;
1927       multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
1928    }
1929
1930    /* Check if there are any filters */
1931    for (i = 0; i < MAX_AF_FILES; i++)
1932    {
1933       fl = csp->rlist[i];
1934       if (NULL != fl)
1935       {
1936          if (NULL != fl->f)
1937          {
1938            found_filters = 1;
1939            break;
1940          }
1941       }
1942    }
1943
1944    if (0 == found_filters)
1945    {
1946       log_error(LOG_LEVEL_ERROR, "Inconsistent configuration: "
1947          "tagging enabled, but no taggers available.");
1948       return(JB_ERR_OK);
1949    }
1950
1951    for (i = 0; i < MAX_AF_FILES; i++)
1952    {
1953       fl = csp->rlist[i];
1954       if ((NULL == fl) || (NULL == fl->f))
1955       {
1956          /*
1957           * Either there are no filter files
1958           * left, or this filter file just
1959           * contains no valid filters.
1960           *
1961           * Continue to be sure we don't miss
1962           * valid filter files that are chained
1963           * after empty or invalid ones.
1964           */
1965          continue;
1966       }
1967
1968       /* For all filters, */
1969       for (b = fl->f; b; b = b->next)
1970       {
1971          if (b->type != wanted_filter_type)
1972          {
1973             /* skip the ones we don't care about, */
1974             continue;
1975          }
1976          /* leaving only taggers that could apply, of which we use the ones, */
1977          for (tag_name = csp->action->multi[multi_action_index]->first;
1978               NULL != tag_name; tag_name = tag_name->next)
1979          {
1980             /* that do apply, and */
1981             if (strcmp(b->name, tag_name->str) == 0)
1982             {
1983                char *modified_tag = NULL;
1984                char *tag = header;
1985                size_t size = header_length;
1986                pcrs_job *joblist = b->joblist;
1987
1988                if (b->dynamic) joblist = compile_dynamic_pcrs_job_list(csp, b);
1989
1990                if (NULL == joblist)
1991                {
1992                   log_error(LOG_LEVEL_RE_FILTER,
1993                      "Tagger %s has empty joblist. Nothing to do.", b->name);
1994                   continue;
1995                }
1996
1997                /* execute their pcrs_joblist on the header. */
1998                for (job = joblist; NULL != job; job = job->next)
1999                {
2000                   const int hits = pcrs_execute(job, tag, size, &modified_tag, &size);
2001
2002                   if (0 < hits)
2003                   {
2004                      /* Success, continue with the modified version. */
2005                      if (tag != header)
2006                      {
2007                         freez(tag);
2008                      }
2009                      tag = modified_tag;
2010                   }
2011                   else
2012                   {
2013                      /* Tagger doesn't match */
2014                      if (0 > hits)
2015                      {
2016                         /* Regex failure, log it but continue anyway. */
2017                         log_error(LOG_LEVEL_ERROR,
2018                            "Problems with tagger \'%s\' and header \'%s\': %s",
2019                            b->name, *header, pcrs_strerror(hits));
2020                      }
2021                      freez(modified_tag);
2022                   }
2023                }
2024
2025                if (b->dynamic) pcrs_free_joblist(joblist);
2026
2027                /* If this tagger matched */
2028                if (tag != header)
2029                {
2030                   if (0 == size)
2031                   {
2032                      /*
2033                       * There is to technical limitation which makes
2034                       * it impossible to use empty tags, but I assume
2035                       * no one would do it intentionally.
2036                       */
2037                      freez(tag);
2038                      log_error(LOG_LEVEL_INFO,
2039                         "Tagger \'%s\' created an empty tag. Ignored.",
2040                         b->name);
2041                      continue;
2042                   }
2043  
2044                   if (!list_contains_item(csp->tags, tag))
2045                   {
2046                      if (JB_ERR_OK != enlist(csp->tags, tag))
2047                      {
2048                         log_error(LOG_LEVEL_ERROR,
2049                            "Insufficient memory to add tag \'%s\', "
2050                            "based on tagger \'%s\' and header \'%s\'",
2051                            tag, b->name, *header);
2052                      }
2053                      else
2054                      {
2055                         char *action_message;
2056                         /*
2057                          * update the action bits right away, to make
2058                          * tagging based on tags set by earlier taggers
2059                          * of the same kind possible.
2060                          */
2061                         if (update_action_bits_for_tag(csp, tag))
2062                         {
2063                            action_message = "Action bits updated accordingly.";
2064                         }
2065                         else
2066                         {
2067                            action_message = "No action bits update necessary.";
2068                         }
2069
2070                         log_error(LOG_LEVEL_HEADER,
2071                            "Tagger \'%s\' added tag \'%s\'. %s",
2072                            b->name, tag, action_message);
2073                      }
2074                   }
2075                   else
2076                   {
2077                      /* XXX: Is this log-worthy? */
2078                      log_error(LOG_LEVEL_HEADER,
2079                         "Tagger \'%s\' didn't add tag \'%s\'. "
2080                         "Tag already present", b->name, tag);
2081                   }
2082                   freez(tag);
2083                } /* if the tagger matched */
2084             } /* if the tagger applies */
2085          } /* for every tagger that could apply */
2086       } /* for all filters */
2087    } /* for all filter files */
2088
2089    return JB_ERR_OK;
2090 }
2091
2092 /* here begins the family of parser functions that reformat header lines */
2093
2094 /*********************************************************************
2095  *
2096  * Function    :  filter_header
2097  *
2098  * Description :  Executes all text substitutions from all applying
2099  *                +(server|client)-header-filter actions on the header.
2100  *                Most of the code was copied from pcrs_filter_response,
2101  *                including the rather short variable names
2102  *
2103  * Parameters  :
2104  *          1  :  csp = Current client state (buffers, headers, etc...)
2105  *          2  :  header = On input, pointer to header to modify.
2106  *                On output, pointer to the modified header, or NULL
2107  *                to remove the header.  This function frees the
2108  *                original string if necessary.
2109  *
2110  * Returns     :  JB_ERR_OK on success and always succeeds
2111  *
2112  *********************************************************************/
2113 static jb_err filter_header(struct client_state *csp, char **header)
2114 {
2115    int hits=0;
2116    int matches;
2117    size_t size = strlen(*header);
2118
2119    char *newheader = NULL;
2120    pcrs_job *job;
2121
2122    struct file_list *fl;
2123    struct re_filterfile_spec *b;
2124    struct list_entry *filtername;
2125
2126    int i, found_filters = 0;
2127    int wanted_filter_type;
2128    int multi_action_index;
2129
2130    if (csp->flags & CSP_FLAG_NO_FILTERING)
2131    {
2132       return JB_ERR_OK;
2133    }
2134
2135    if (csp->flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
2136    {
2137       wanted_filter_type = FT_SERVER_HEADER_FILTER;
2138       multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
2139    }
2140    else
2141    {
2142       wanted_filter_type = FT_CLIENT_HEADER_FILTER;
2143       multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
2144    }
2145
2146    /*
2147     * Need to check the set of re_filterfiles...
2148     */
2149    for (i = 0; i < MAX_AF_FILES; i++)
2150    {
2151       fl = csp->rlist[i];
2152       if (NULL != fl)
2153       {
2154          if (NULL != fl->f)
2155          {
2156            found_filters = 1;
2157            break;
2158          }
2159       }
2160    }
2161
2162    if (0 == found_filters)
2163    {
2164       log_error(LOG_LEVEL_ERROR, "Inconsistent configuration: "
2165          "header filtering enabled, but no matching filters available.");
2166       return(JB_ERR_OK);
2167    }
2168
2169    for (i = 0; i < MAX_AF_FILES; i++)
2170    {
2171       fl = csp->rlist[i];
2172       if ((NULL == fl) || (NULL == fl->f))
2173       {
2174          /*
2175           * Either there are no filter files
2176           * left, or this filter file just
2177           * contains no valid filters.
2178           *
2179           * Continue to be sure we don't miss
2180           * valid filter files that are chained
2181           * after empty or invalid ones.
2182           */
2183          continue;
2184       }
2185       /*
2186        * For all applying +filter actions, look if a filter by that
2187        * name exists and if yes, execute its pcrs_joblist on the
2188        * buffer.
2189        */
2190       for (b = fl->f; b; b = b->next)
2191       {
2192          if (b->type != wanted_filter_type)
2193          {
2194             /* Skip other filter types */
2195             continue;
2196          }
2197
2198          for (filtername = csp->action->multi[multi_action_index]->first;
2199               filtername ; filtername = filtername->next)
2200          {
2201             if (strcmp(b->name, filtername->str) == 0)
2202             {
2203                int current_hits = 0;
2204                pcrs_job *joblist = b->joblist;
2205
2206                if (b->dynamic) joblist = compile_dynamic_pcrs_job_list(csp, b);
2207
2208                if (NULL == joblist)
2209                {
2210                   log_error(LOG_LEVEL_RE_FILTER, "Filter %s has empty joblist. Nothing to do.", b->name);
2211                   continue;
2212                }
2213
2214                log_error(LOG_LEVEL_RE_FILTER, "filtering \'%s\' (size %d) with \'%s\' ...",
2215                          *header, size, b->name);
2216
2217                /* Apply all jobs from the joblist */
2218                for (job = joblist; NULL != job; job = job->next)
2219                {
2220                   matches = pcrs_execute(job, *header, size, &newheader, &size);
2221                   if ( 0 < matches )
2222                   {
2223                      current_hits += matches; 
2224                      log_error(LOG_LEVEL_HEADER, "Transforming \"%s\" to \"%s\"", *header, newheader);
2225                      freez(*header);
2226                      *header = newheader;
2227                   }
2228                   else if ( 0 == matches )
2229                   {
2230                      /* Filter doesn't change header */
2231                      freez(newheader);
2232                   }
2233                   else
2234                   {
2235                      /* RegEx failure */
2236                      log_error(LOG_LEVEL_ERROR, "Filtering \'%s\' with \'%s\' didn't work out: %s",
2237                         *header, b->name, pcrs_strerror(matches));
2238                      if (newheader != NULL)
2239                      {
2240                         log_error(LOG_LEVEL_ERROR, "Freeing what's left: %s", newheader);
2241                         freez(newheader);
2242                      }
2243                   }
2244                }
2245
2246                if (b->dynamic) pcrs_free_joblist(joblist);
2247
2248                log_error(LOG_LEVEL_RE_FILTER, "... produced %d hits (new size %d).", current_hits, size);
2249                hits += current_hits;
2250             }
2251          }
2252       }
2253    }
2254
2255    /*
2256     * Additionally checking for hits is important because if
2257     * the continue hack is triggered, server headers can
2258     * arrive empty to separate multiple heads from each other.
2259     */
2260    if ((0 == size) && hits)
2261    {
2262       log_error(LOG_LEVEL_HEADER, "Removing empty header %s", *header);
2263       freez(*header);
2264    }
2265
2266    return(JB_ERR_OK);
2267 }
2268
2269
2270 /*********************************************************************
2271  *
2272  * Function    :  connection
2273  *
2274  * Description :  Makes sure that the value of the Connection: header
2275  *                is "close" and signals connection_close_adder 
2276  *                to do nothing.
2277  *
2278  * Parameters  :
2279  *          1  :  csp = Current client state (buffers, headers, etc...)
2280  *          2  :  header = On input, pointer to header to modify.
2281  *                On output, pointer to the modified header, or NULL
2282  *                to remove the header.  This function frees the
2283  *                original string if necessary.
2284  *
2285  * Returns     :  JB_ERR_OK on success, or
2286  *                JB_ERR_MEMORY on out-of-memory error.
2287  *
2288  *********************************************************************/
2289 static jb_err connection(struct client_state *csp, char **header)
2290 {
2291    char *old_header = *header;
2292
2293    /* Do we have a 'Connection: close' header? */
2294    if (strcmpic(*header, "Connection: close"))
2295    {
2296       /* No, create one */
2297       *header = strdup("Connection: close");
2298       if (header == NULL)
2299       { 
2300          return JB_ERR_MEMORY;
2301       }
2302       log_error(LOG_LEVEL_HEADER, "Replaced: \'%s\' with \'%s\'", old_header, *header);
2303       freez(old_header);
2304    }
2305
2306    /* Signal connection_close_adder() to return early. */
2307    if (csp->flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
2308    {
2309       csp->flags |= CSP_FLAG_SERVER_CONNECTION_CLOSE_SET;
2310    }
2311    else
2312    {
2313       csp->flags |= CSP_FLAG_CLIENT_CONNECTION_CLOSE_SET;
2314    }
2315
2316    return JB_ERR_OK;
2317 }
2318
2319
2320 /*********************************************************************
2321  *
2322  * Function    :  crumble
2323  *
2324  * Description :  This is called if a header matches a pattern to "crunch"
2325  *
2326  * Parameters  :
2327  *          1  :  csp = Current client state (buffers, headers, etc...)
2328  *          2  :  header = On input, pointer to header to modify.
2329  *                On output, pointer to the modified header, or NULL
2330  *                to remove the header.  This function frees the
2331  *                original string if necessary.
2332  *
2333  * Returns     :  JB_ERR_OK on success, or
2334  *                JB_ERR_MEMORY on out-of-memory error.
2335  *
2336  *********************************************************************/
2337 static jb_err crumble(struct client_state *csp, char **header)
2338 {
2339    log_error(LOG_LEVEL_HEADER, "crumble crunched: %s!", *header);
2340    freez(*header);
2341    return JB_ERR_OK;
2342 }
2343
2344
2345 /*********************************************************************
2346  *
2347  * Function    :  crunch_server_header
2348  *
2349  * Description :  Crunch server header if it matches a string supplied by the
2350  *                user. Called from `sed'.
2351  *
2352  * Parameters  :
2353  *          1  :  csp = Current client state (buffers, headers, etc...)
2354  *          2  :  header = On input, pointer to header to modify.
2355  *                On output, pointer to the modified header, or NULL
2356  *                to remove the header.  This function frees the
2357  *                original string if necessary.
2358  *
2359  * Returns     :  JB_ERR_OK on success and always succeeds
2360  *
2361  *********************************************************************/
2362 static jb_err crunch_server_header(struct client_state *csp, char **header)
2363 {
2364    const char *crunch_pattern;
2365
2366    /* Do we feel like crunching? */
2367    if ((csp->action->flags & ACTION_CRUNCH_SERVER_HEADER))
2368    {
2369       crunch_pattern = csp->action->string[ACTION_STRING_SERVER_HEADER];
2370
2371       /* Is the current header the lucky one? */
2372       if (strstr(*header, crunch_pattern))
2373       {
2374          log_error(LOG_LEVEL_HEADER, "Crunching server header: %s (contains: %s)", *header, crunch_pattern);  
2375          freez(*header);
2376       }
2377    }
2378
2379    return JB_ERR_OK;
2380 }
2381
2382
2383 /*********************************************************************
2384  *
2385  * Function    :  server_content_type
2386  *
2387  * Description :  Set the content-type for filterable types (text/.*,
2388  *                .*xml.*, javascript and image/gif) unless filtering has been
2389  *                forbidden (CT_TABOO) while parsing earlier headers.
2390  *                NOTE: Since text/plain is commonly used by web servers
2391  *                      for files whose correct type is unknown, we don't
2392  *                      set CT_TEXT for it.
2393  *
2394  * Parameters  :
2395  *          1  :  csp = Current client state (buffers, headers, etc...)
2396  *          2  :  header = On input, pointer to header to modify.
2397  *                On output, pointer to the modified header, or NULL
2398  *                to remove the header.  This function frees the
2399  *                original string if necessary.
2400  *
2401  * Returns     :  JB_ERR_OK on success, or
2402  *                JB_ERR_MEMORY on out-of-memory error.
2403  *
2404  *********************************************************************/
2405 static jb_err server_content_type(struct client_state *csp, char **header)
2406 {
2407    /* Remove header if it isn't the first Content-Type header */
2408    if ((csp->content_type & CT_DECLARED))
2409    {
2410      /*
2411       * Another, slightly slower, way to see if
2412       * we already parsed another Content-Type header.
2413       */
2414       assert(NULL != get_header_value(csp->headers, "Content-Type:"));
2415
2416       log_error(LOG_LEVEL_ERROR,
2417          "Multiple Content-Type headers. Removing and ignoring: \'%s\'",
2418          *header);
2419       freez(*header);
2420
2421       return JB_ERR_OK;
2422    }
2423
2424    /*
2425     * Signal that the Content-Type has been set.
2426     */
2427    csp->content_type |= CT_DECLARED;
2428
2429    if (!(csp->content_type & CT_TABOO))
2430    {
2431       /*
2432        * XXX: The assumption that text/plain is a sign of
2433        * binary data seems to be somewhat unreasonable nowadays
2434        * and should be dropped after 3.0.8 is out.
2435        */
2436       if ((strstr(*header, "text/") && !strstr(*header, "plain"))
2437         || strstr(*header, "xml")
2438         || strstr(*header, "application/x-javascript"))
2439       {
2440          csp->content_type |= CT_TEXT;
2441       }
2442       else if (strstr(*header, "image/gif"))
2443       {
2444          csp->content_type |= CT_GIF;
2445       }
2446    }
2447
2448    /*
2449     * Are we messing with the content type?
2450     */
2451    if (csp->action->flags & ACTION_CONTENT_TYPE_OVERWRITE)
2452    {
2453       /*
2454        * Make sure the user doesn't accidently
2455        * change the content type of binary documents. 
2456        */
2457       if ((csp->content_type & CT_TEXT) || (csp->action->flags & ACTION_FORCE_TEXT_MODE))
2458       {
2459          freez(*header);
2460          *header = strdup("Content-Type: ");
2461          string_append(header, csp->action->string[ACTION_STRING_CONTENT_TYPE]);
2462
2463          if (header == NULL)
2464          {
2465             log_error(LOG_LEVEL_HEADER, "Insufficient memory to replace Content-Type!");
2466             return JB_ERR_MEMORY;
2467          }
2468          log_error(LOG_LEVEL_HEADER, "Modified: %s!", *header);
2469       }
2470       else
2471       {
2472          log_error(LOG_LEVEL_HEADER, "%s not replaced. "
2473             "It doesn't look like a content type that should be filtered. "
2474             "Enable force-text-mode if you know what you're doing.", *header);
2475       }
2476    }
2477
2478    return JB_ERR_OK;
2479 }
2480
2481
2482 /*********************************************************************
2483  *
2484  * Function    :  server_transfer_coding
2485  *
2486  * Description :  - Prohibit filtering (CT_TABOO) if transfer coding compresses
2487  *                - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
2488  *                - Remove header if body was chunked but has been
2489  *                  de-chunked for filtering.
2490  *
2491  * Parameters  :
2492  *          1  :  csp = Current client state (buffers, headers, etc...)
2493  *          2  :  header = On input, pointer to header to modify.
2494  *                On output, pointer to the modified header, or NULL
2495  *                to remove the header.  This function frees the
2496  *                original string if necessary.
2497  *
2498  * Returns     :  JB_ERR_OK on success, or
2499  *                JB_ERR_MEMORY on out-of-memory error.
2500  *
2501  *********************************************************************/
2502 static jb_err server_transfer_coding(struct client_state *csp, char **header)
2503 {
2504    /*
2505     * Turn off pcrs and gif filtering if body compressed
2506     */
2507    if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
2508    {
2509 #ifdef FEATURE_ZLIB
2510       /*
2511        * XXX: Added to test if we could use CT_GZIP and CT_DEFLATE here.
2512        */
2513       log_error(LOG_LEVEL_INFO, "Marking content type for %s as CT_TABOO because of %s.",
2514          csp->http->cmd, *header);
2515 #endif /* def FEATURE_ZLIB */
2516       csp->content_type = CT_TABOO;
2517    }
2518
2519    /*
2520     * Raise flag if body chunked
2521     */
2522    if (strstr(*header, "chunked"))
2523    {
2524       csp->flags |= CSP_FLAG_CHUNKED;
2525
2526       /*
2527        * If the body was modified, it has been de-chunked first
2528        * and the header must be removed.
2529        *
2530        * FIXME: If there is more than one transfer encoding,
2531        * only the "chunked" part should be removed here.
2532        */
2533       if (csp->flags & CSP_FLAG_MODIFIED)
2534       {
2535          log_error(LOG_LEVEL_HEADER, "Removing: %s", *header);
2536          freez(*header);
2537       }
2538    }
2539
2540    return JB_ERR_OK;
2541 }
2542
2543
2544 /*********************************************************************
2545  *
2546  * Function    :  server_content_encoding
2547  *
2548  * Description :  This function is run twice for each request,
2549  *                unless FEATURE_ZLIB and filtering are disabled.
2550  *
2551  *                The first run is used to check if the content
2552  *                is compressed, if FEATURE_ZLIB is disabled
2553  *                filtering is then disabled as well, if FEATURE_ZLIB
2554  *                is enabled the content is marked for decompression.
2555  *                
2556  *                The second run is used to remove the Content-Encoding
2557  *                header if the decompression was successful.
2558  *
2559  * Parameters  :
2560  *          1  :  csp = Current client state (buffers, headers, etc...)
2561  *          2  :  header = On input, pointer to header to modify.
2562  *                On output, pointer to the modified header, or NULL
2563  *                to remove the header.  This function frees the
2564  *                original string if necessary.
2565  *
2566  * Returns     :  JB_ERR_OK on success, or
2567  *                JB_ERR_MEMORY on out-of-memory error.
2568  *
2569  *********************************************************************/
2570 static jb_err server_content_encoding(struct client_state *csp, char **header)
2571 {
2572 #ifdef FEATURE_ZLIB
2573    if ((csp->flags & CSP_FLAG_MODIFIED)
2574     && (csp->content_type & (CT_GZIP | CT_DEFLATE)))
2575    {
2576       /*
2577        * We successfully decompressed the content,
2578        * and have to clean the header now, so the
2579        * client no longer expects compressed data..
2580        *
2581        * XXX: There is a difference between cleaning
2582        * and removing it completely.
2583        */
2584       log_error(LOG_LEVEL_HEADER, "Crunching: %s", *header);
2585       freez(*header);
2586    }
2587    else if (strstr(*header, "gzip"))
2588    {
2589       /* Mark for gzip decompression */
2590       csp->content_type |= CT_GZIP;
2591    }
2592    else if (strstr(*header, "deflate"))
2593    {
2594       /* Mark for zlib decompression */
2595       csp->content_type |= CT_DEFLATE;
2596    }
2597    else if (strstr(*header, "compress"))
2598    {
2599       /*
2600        * We can't decompress this; therefore we can't filter
2601        * it either.
2602        */
2603       csp->content_type |= CT_TABOO;
2604    }
2605 #else /* !defined(FEATURE_ZLIB) */
2606    if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
2607    {
2608       /*
2609        * Body is compressed, turn off pcrs and gif filtering.
2610        */
2611       csp->content_type |= CT_TABOO;
2612
2613       /*
2614        * Log a warning if the user expects the content to be filtered.
2615        */
2616       if ((csp->rlist != NULL) &&
2617          (!list_is_empty(csp->action->multi[ACTION_MULTI_FILTER])))
2618       {
2619          log_error(LOG_LEVEL_INFO,
2620             "Compressed content detected, content filtering disabled. "
2621             "Consider recompiling Privoxy with zlib support or "
2622             "enable the prevent-compression action.");
2623       }
2624    }
2625 #endif /* defined(FEATURE_ZLIB) */
2626
2627    return JB_ERR_OK;
2628
2629 }
2630
2631
2632 /*********************************************************************
2633  *
2634  * Function    :  server_content_length
2635  *
2636  * Description :  Adjust Content-Length header if we modified
2637  *                the body.
2638  *
2639  * Parameters  :
2640  *          1  :  csp = Current client state (buffers, headers, etc...)
2641  *          2  :  header = On input, pointer to header to modify.
2642  *                On output, pointer to the modified header, or NULL
2643  *                to remove the header.  This function frees the
2644  *                original string if necessary.
2645  *
2646  * Returns     :  JB_ERR_OK on success, or
2647  *                JB_ERR_MEMORY on out-of-memory error.
2648  *
2649  *********************************************************************/
2650 static jb_err server_content_length(struct client_state *csp, char **header)
2651 {
2652    const size_t max_header_length = 80;
2653
2654    /* Regenerate header if the content was modified. */
2655    if (csp->flags & CSP_FLAG_MODIFIED)
2656    {
2657       freez(*header);
2658       *header = (char *) zalloc(max_header_length);
2659       if (*header == NULL)
2660       {
2661          return JB_ERR_MEMORY;
2662       }
2663
2664       snprintf(*header, max_header_length, "Content-Length: %d",
2665          (int)csp->content_length);
2666       log_error(LOG_LEVEL_HEADER, "Adjusted Content-Length to %d",
2667          (int)csp->content_length);
2668    }
2669
2670    return JB_ERR_OK;
2671 }
2672
2673
2674 /*********************************************************************
2675  *
2676  * Function    :  server_content_md5
2677  *
2678  * Description :  Crumble any Content-MD5 headers if the document was
2679  *                modified. FIXME: Should we re-compute instead?
2680  *
2681  * Parameters  :
2682  *          1  :  csp = Current client state (buffers, headers, etc...)
2683  *          2  :  header = On input, pointer to header to modify.
2684  *                On output, pointer to the modified header, or NULL
2685  *                to remove the header.  This function frees the
2686  *                original string if necessary.
2687  *
2688  * Returns     :  JB_ERR_OK on success, or
2689  *                JB_ERR_MEMORY on out-of-memory error.
2690  *
2691  *********************************************************************/
2692 static jb_err server_content_md5(struct client_state *csp, char **header)
2693 {
2694    if (csp->flags & CSP_FLAG_MODIFIED)
2695    {
2696       log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
2697       freez(*header);
2698    }
2699
2700    return JB_ERR_OK;
2701 }
2702
2703
2704 /*********************************************************************
2705  *
2706  * Function    :  server_content_disposition
2707  *
2708  * Description :  If enabled, blocks or modifies the "Content-Disposition" header.
2709  *                Called from `sed'.
2710  *
2711  * Parameters  :
2712  *          1  :  csp = Current client state (buffers, headers, etc...)
2713  *          2  :  header = On input, pointer to header to modify.
2714  *                On output, pointer to the modified header, or NULL
2715  *                to remove the header.  This function frees the
2716  *                original string if necessary.
2717  *
2718  * Returns     :  JB_ERR_OK on success, or
2719  *                JB_ERR_MEMORY on out-of-memory error.
2720  *
2721  *********************************************************************/
2722 static jb_err server_content_disposition(struct client_state *csp, char **header)
2723 {
2724    const char *newval;
2725
2726    /*
2727     * Are we messing with the Content-Disposition header?
2728     */
2729    if ((csp->action->flags & ACTION_HIDE_CONTENT_DISPOSITION) == 0)
2730    {
2731       /* Me tinks not */
2732       return JB_ERR_OK;
2733    }
2734
2735    newval = csp->action->string[ACTION_STRING_CONTENT_DISPOSITION];
2736
2737    if ((newval == NULL) || (0 == strcmpic(newval, "block")))
2738    {
2739       /*
2740        * Blocking content-disposition header
2741        */
2742       log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
2743       freez(*header);
2744       return JB_ERR_OK;
2745    }
2746    else
2747    {  
2748       /*
2749        * Replacing Content-Disposition header
2750        */
2751       freez(*header);
2752       *header = strdup("Content-Disposition: ");
2753       string_append(header, newval);
2754
2755       if (*header != NULL)
2756       {
2757          log_error(LOG_LEVEL_HEADER,
2758             "Content-Disposition header crunched and replaced with: %s", *header);
2759       }
2760    }
2761    return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2762 }
2763
2764
2765 /*********************************************************************
2766  *
2767  * Function    :  server_last_modified
2768  *
2769  * Description :  Changes Last-Modified header to the actual date
2770  *                to help hide-if-modified-since.
2771  *                Called from `sed'.
2772  *
2773  * Parameters  :
2774  *          1  :  csp = Current client state (buffers, headers, etc...)
2775  *          2  :  header = On input, pointer to header to modify.
2776  *                On output, pointer to the modified header, or NULL
2777  *                to remove the header.  This function frees the
2778  *                original string if necessary.
2779  *
2780  * Returns     :  JB_ERR_OK on success, or
2781  *                JB_ERR_MEMORY on out-of-memory error.
2782  *
2783  *********************************************************************/
2784 static jb_err server_last_modified(struct client_state *csp, char **header)
2785 {
2786    const char *newval;
2787    char buf[BUFFER_SIZE];
2788
2789    char newheader[50];
2790 #ifdef HAVE_GMTIME_R
2791    struct tm gmt;
2792 #endif
2793    struct tm *timeptr = NULL;
2794    time_t now, last_modified;                  
2795    long int rtime;
2796    long int days, hours, minutes, seconds;
2797    
2798    /*
2799     * Are we messing with the Last-Modified header?
2800     */
2801    if ((csp->action->flags & ACTION_OVERWRITE_LAST_MODIFIED) == 0)
2802    {
2803       /*Nope*/
2804       return JB_ERR_OK;
2805    }
2806
2807    newval = csp->action->string[ACTION_STRING_LAST_MODIFIED];
2808
2809    if (0 == strcmpic(newval, "block") )
2810    {
2811       /*
2812        * Blocking Last-Modified header. Useless but why not.
2813        */
2814       log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
2815       freez(*header);
2816       return JB_ERR_OK;
2817    }
2818    else if (0 == strcmpic(newval, "reset-to-request-time"))
2819    {  
2820       /*
2821        * Setting Last-Modified Header to now.
2822        */
2823       get_http_time(0, buf, sizeof(buf));
2824       freez(*header);
2825       *header = strdup("Last-Modified: ");
2826       string_append(header, buf);   
2827
2828       if (*header == NULL)
2829       {
2830          log_error(LOG_LEVEL_HEADER, "Insufficent memory. Last-Modified header got lost, boohoo.");  
2831       }
2832       else
2833       {
2834          log_error(LOG_LEVEL_HEADER, "Reset to present time: %s", *header);
2835       }
2836    }
2837    else if (0 == strcmpic(newval, "randomize"))
2838    {
2839       const char *header_time = *header + sizeof("Last-Modified:");
2840
2841       log_error(LOG_LEVEL_HEADER, "Randomizing: %s", *header);
2842       now = time(NULL);
2843 #ifdef HAVE_GMTIME_R
2844       timeptr = gmtime_r(&now, &gmt);
2845 #elif FEATURE_PTHREAD
2846       pthread_mutex_lock(&gmtime_mutex);
2847       timeptr = gmtime(&now);
2848       pthread_mutex_unlock(&gmtime_mutex);
2849 #else
2850       timeptr = gmtime(&now);
2851 #endif
2852       if (JB_ERR_OK != parse_header_time(header_time, &last_modified))
2853       {
2854          log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s in %s (crunching!)", header_time, *header);
2855          freez(*header);
2856       }
2857       else
2858       {
2859          rtime = (long int)difftime(now, last_modified);
2860          if (rtime)
2861          {
2862             int negative = 0;
2863
2864             if (rtime < 0)
2865             {
2866                rtime *= -1; 
2867                negative = 1;
2868                log_error(LOG_LEVEL_HEADER, "Server time in the future.");
2869             }
2870             rtime = pick_from_range(rtime);
2871             if (negative) rtime *= -1;
2872             last_modified += rtime;
2873 #ifdef HAVE_GMTIME_R
2874             timeptr = gmtime_r(&last_modified, &gmt);
2875 #elif FEATURE_PTHREAD
2876             pthread_mutex_lock(&gmtime_mutex);
2877             timeptr = gmtime(&last_modified);
2878             pthread_mutex_unlock(&gmtime_mutex);
2879 #else
2880             timeptr = gmtime(&last_modified);
2881 #endif
2882             strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
2883             freez(*header);
2884             *header = strdup("Last-Modified: ");
2885             string_append(header, newheader);
2886
2887             if (*header == NULL)
2888             {
2889                log_error(LOG_LEVEL_ERROR, "Insufficent memory, header crunched without replacement.");
2890                return JB_ERR_MEMORY;  
2891             }
2892
2893             if (LOG_LEVEL_HEADER & debug) /* Save cycles if the user isn't interested. */
2894             {
2895                days    = rtime / (3600 * 24);
2896                hours   = rtime / 3600 % 24;
2897                minutes = rtime / 60 % 60;
2898                seconds = rtime % 60;            
2899
2900                log_error(LOG_LEVEL_HEADER, "Randomized:  %s (added %d da%s %d hou%s %d minut%s %d second%s",
2901                   *header, days, (days == 1) ? "y" : "ys", hours, (hours == 1) ? "r" : "rs",
2902                   minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
2903             }
2904          }
2905          else
2906          {
2907             log_error(LOG_LEVEL_HEADER, "Randomized ... or not. No time difference to work with.");
2908          }
2909       }
2910    }
2911
2912    return JB_ERR_OK;
2913 }
2914
2915
2916 /*********************************************************************
2917  *
2918  * Function    :  client_accept_encoding
2919  *
2920  * Description :  Rewrite the client's Accept-Encoding header so that
2921  *                if doesn't allow compression, if the action applies.
2922  *                Note: For HTTP/1.0 the absence of the header is enough.
2923  *
2924  * Parameters  :
2925  *          1  :  csp = Current client state (buffers, headers, etc...)
2926  *          2  :  header = On input, pointer to header to modify.
2927  *                On output, pointer to the modified header, or NULL
2928  *                to remove the header.  This function frees the
2929  *                original string if necessary.
2930  *
2931  * Returns     :  JB_ERR_OK on success, or
2932  *                JB_ERR_MEMORY on out-of-memory error.
2933  *
2934  *********************************************************************/
2935 static jb_err client_accept_encoding(struct client_state *csp, char **header)
2936 {
2937    if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
2938    {
2939       log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
2940
2941       freez(*header);
2942
2943       /* Temporarily disable the correct behaviour to
2944        * work around a PHP bug. 
2945        *
2946        * if (!strcmpic(csp->http->ver, "HTTP/1.1"))
2947        * {
2948        *    *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
2949        *    if (*header == NULL)
2950        *    {
2951        *       return JB_ERR_MEMORY;
2952        *    }
2953        * }
2954        * 
2955        */
2956    }
2957
2958    return JB_ERR_OK;
2959 }
2960
2961
2962 /*********************************************************************
2963  *
2964  * Function    :  client_te
2965  *
2966  * Description :  Rewrite the client's TE header so that
2967  *                if doesn't allow compression, if the action applies.
2968  *
2969  * Parameters  :
2970  *          1  :  csp = Current client state (buffers, headers, etc...)
2971  *          2  :  header = On input, pointer to header to modify.
2972  *                On output, pointer to the modified header, or NULL
2973  *                to remove the header.  This function frees the
2974  *                original string if necessary.
2975  *
2976  * Returns     :  JB_ERR_OK on success, or
2977  *                JB_ERR_MEMORY on out-of-memory error.
2978  *
2979  *********************************************************************/
2980 static jb_err client_te(struct client_state *csp, char **header)
2981 {
2982    if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
2983    {
2984       freez(*header);
2985       log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
2986    }
2987
2988    return JB_ERR_OK;
2989 }
2990
2991
2992 /*********************************************************************
2993  *
2994  * Function    :  client_referrer
2995  *
2996  * Description :  Handle the "referer" config setting properly.
2997  *                Called from `sed'.
2998  *
2999  * Parameters  :
3000  *          1  :  csp = Current client state (buffers, headers, etc...)
3001  *          2  :  header = On input, pointer to header to modify.
3002  *                On output, pointer to the modified header, or NULL
3003  *                to remove the header.  This function frees the
3004  *                original string if necessary.
3005  *
3006  * Returns     :  JB_ERR_OK on success, or
3007  *                JB_ERR_MEMORY on out-of-memory error.
3008  *
3009  *********************************************************************/
3010 static jb_err client_referrer(struct client_state *csp, char **header)
3011 {
3012    const char *parameter;
3013    /* booleans for parameters we have to check multiple times */
3014    int parameter_conditional_block;
3015    int parameter_conditional_forge;
3016  
3017 #ifdef FEATURE_FORCE_LOAD
3018    /*
3019     * Since the referrer can include the prefix even
3020     * if the request itself is non-forced, we must
3021     * clean it unconditionally.
3022     *
3023     * XXX: strclean is too broad
3024     */
3025    strclean(*header, FORCE_PREFIX);
3026 #endif /* def FEATURE_FORCE_LOAD */
3027
3028    if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
3029    {
3030       /* Nothing left to do */
3031       return JB_ERR_OK;
3032    }
3033
3034    parameter = csp->action->string[ACTION_STRING_REFERER];
3035    assert(parameter != NULL);
3036    parameter_conditional_block = (0 == strcmpic(parameter, "conditional-block"));
3037    parameter_conditional_forge = (0 == strcmpic(parameter, "conditional-forge"));
3038
3039    if (!parameter_conditional_block && !parameter_conditional_forge)
3040    {
3041       /*
3042        * As conditional-block and conditional-forge are the only
3043        * parameters that rely on the original referrer, we can
3044        * remove it now for all the others.
3045        */
3046       freez(*header);
3047    }
3048
3049    if (0 == strcmpic(parameter, "block"))
3050    {
3051       log_error(LOG_LEVEL_HEADER, "Referer crunched!");
3052       return JB_ERR_OK;
3053    }
3054    else if (parameter_conditional_block || parameter_conditional_forge)
3055    {
3056       return handle_conditional_hide_referrer_parameter(header,
3057          csp->http->hostport, parameter_conditional_block);
3058    }
3059    else if (0 == strcmpic(parameter, "forge"))
3060    {
3061       return create_forged_referrer(header, csp->http->hostport);
3062    }
3063    else
3064    {
3065       /* interpret parameter as user-supplied referer to fake */
3066       return create_fake_referrer(header, parameter);
3067    }
3068 }
3069
3070
3071 /*********************************************************************
3072  *
3073  * Function    :  client_accept_language
3074  *
3075  * Description :  Handle the "Accept-Language" config setting properly.
3076  *                Called from `sed'.
3077  *
3078  * Parameters  :
3079  *          1  :  csp = Current client state (buffers, headers, etc...)
3080  *          2  :  header = On input, pointer to header to modify.
3081  *                On output, pointer to the modified header, or NULL
3082  *                to remove the header.  This function frees the
3083  *                original string if necessary.
3084  *
3085  * Returns     :  JB_ERR_OK on success, or
3086  *                JB_ERR_MEMORY on out-of-memory error.
3087  *
3088  *********************************************************************/
3089 static jb_err client_accept_language(struct client_state *csp, char **header)
3090 {
3091    const char *newval;
3092
3093    /*
3094     * Are we messing with the Accept-Language?
3095     */
3096    if ((csp->action->flags & ACTION_HIDE_ACCEPT_LANGUAGE) == 0)
3097    {
3098       /*I don't think so*/
3099       return JB_ERR_OK;
3100    }
3101
3102    newval = csp->action->string[ACTION_STRING_LANGUAGE];
3103
3104    if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
3105    {
3106       /*
3107        * Blocking Accept-Language header
3108        */
3109       log_error(LOG_LEVEL_HEADER, "Crunching Accept-Language!");
3110       freez(*header);
3111       return JB_ERR_OK;
3112    }
3113    else
3114    {  
3115       /*
3116        * Replacing Accept-Language header
3117        */
3118       freez(*header);
3119       *header = strdup("Accept-Language: ");
3120       string_append(header, newval);   
3121
3122       if (*header == NULL)
3123       {
3124          log_error(LOG_LEVEL_ERROR,
3125             "Insufficent memory. Accept-Language header crunched without replacement.");  
3126       }
3127       else
3128       {
3129          log_error(LOG_LEVEL_HEADER,
3130             "Accept-Language header crunched and replaced with: %s", *header);
3131       }
3132    }
3133    return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
3134 }
3135
3136
3137 /*********************************************************************
3138  *
3139  * Function    :  crunch_client_header
3140  *
3141  * Description :  Crunch client header if it matches a string supplied by the
3142  *                user. Called from `sed'.
3143  *
3144  * Parameters  :
3145  *          1  :  csp = Current client state (buffers, headers, etc...)
3146  *          2  :  header = On input, pointer to header to modify.
3147  *                On output, pointer to the modified header, or NULL
3148  *                to remove the header.  This function frees the
3149  *                original string if necessary.
3150  *
3151  * Returns     :  JB_ERR_OK on success and always succeeds
3152  *
3153  *********************************************************************/
3154 static jb_err crunch_client_header(struct client_state *csp, char **header)
3155 {
3156    const char *crunch_pattern;
3157
3158    /* Do we feel like crunching? */
3159    if ((csp->action->flags & ACTION_CRUNCH_CLIENT_HEADER))
3160    {
3161       crunch_pattern = csp->action->string[ACTION_STRING_CLIENT_HEADER];
3162
3163       /* Is the current header the lucky one? */
3164       if (strstr(*header, crunch_pattern))
3165       {
3166          log_error(LOG_LEVEL_HEADER, "Crunching client header: %s (contains: %s)", *header, crunch_pattern);  
3167          freez(*header);
3168       }
3169    }
3170    return JB_ERR_OK;
3171 }
3172
3173
3174 /*********************************************************************
3175  *
3176  * Function    :  client_uagent
3177  *
3178  * Description :  Handle the "user-agent" config setting properly
3179  *                and remember its original value to enable browser
3180  *                bug workarounds. Called from `sed'.
3181  *
3182  * Parameters  :
3183  *          1  :  csp = Current client state (buffers, headers, etc...)
3184  *          2  :  header = On input, pointer to header to modify.
3185  *                On output, pointer to the modified header, or NULL
3186  *                to remove the header.  This function frees the
3187  *                original string if necessary.
3188  *
3189  * Returns     :  JB_ERR_OK on success, or
3190  *                JB_ERR_MEMORY on out-of-memory error.
3191  *
3192  *********************************************************************/
3193 static jb_err client_uagent(struct client_state *csp, char **header)
3194 {
3195    const char *newval;
3196
3197    if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
3198    {
3199       return JB_ERR_OK;
3200    }
3201
3202    newval = csp->action->string[ACTION_STRING_USER_AGENT];
3203    if (newval == NULL)
3204    {
3205       return JB_ERR_OK;
3206    }
3207
3208    freez(*header);
3209    *header = strdup("User-Agent: ");
3210    string_append(header, newval);
3211
3212    log_error(LOG_LEVEL_HEADER, "Modified: %s", *header);
3213
3214    return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
3215 }
3216
3217
3218 /*********************************************************************
3219  *
3220  * Function    :  client_ua
3221  *
3222  * Description :  Handle "ua-" headers properly.  Called from `sed'.
3223  *
3224  * Parameters  :
3225  *          1  :  csp = Current client state (buffers, headers, etc...)
3226  *          2  :  header = On input, pointer to header to modify.
3227  *                On output, pointer to the modified header, or NULL
3228  *                to remove the header.  This function frees the
3229  *                original string if necessary.
3230  *
3231  * Returns     :  JB_ERR_OK on success, or
3232  *                JB_ERR_MEMORY on out-of-memory error.
3233  *
3234  *********************************************************************/
3235 static jb_err client_ua(struct client_state *csp, char **header)
3236 {
3237    if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
3238    {
3239       log_error(LOG_LEVEL_HEADER, "crunched User-Agent!");
3240       freez(*header);
3241    }
3242
3243    return JB_ERR_OK;
3244 }
3245
3246
3247 /*********************************************************************
3248  *
3249  * Function    :  client_from
3250  *
3251  * Description :  Handle the "from" config setting properly.
3252  *                Called from `sed'.
3253  *
3254  * Parameters  :
3255  *          1  :  csp = Current client state (buffers, headers, etc...)
3256  *          2  :  header = On input, pointer to header to modify.
3257  *                On output, pointer to the modified header, or NULL
3258  *                to remove the header.  This function frees the
3259  *                original string if necessary.
3260  *
3261  * Returns     :  JB_ERR_OK on success, or
3262  *                JB_ERR_MEMORY on out-of-memory error.
3263  *
3264  *********************************************************************/
3265 static jb_err client_from(struct client_state *csp, char **header)
3266 {
3267    const char *newval;
3268
3269    if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
3270    {
3271       return JB_ERR_OK;
3272    }
3273
3274    freez(*header);
3275
3276    newval = csp->action->string[ACTION_STRING_FROM];
3277
3278    /*
3279     * Are we blocking the e-mail address?
3280     */
3281    if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
3282    {
3283       log_error(LOG_LEVEL_HEADER, "crunched From!");
3284       return JB_ERR_OK;
3285    }
3286
3287    log_error(LOG_LEVEL_HEADER, " modified");
3288
3289    *header = strdup("From: ");
3290    string_append(header, newval);
3291
3292    return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
3293 }
3294
3295
3296 /*********************************************************************
3297  *
3298  * Function    :  client_send_cookie
3299  *
3300  * Description :  Crunches the "cookie" header if necessary.
3301  *                Called from `sed'.
3302  *
3303  *                XXX: Stupid name, doesn't send squat.
3304  *
3305  * Parameters  :
3306  *          1  :  csp = Current client state (buffers, headers, etc...)
3307  *          2  :  header = On input, pointer to header to modify.
3308  *                On output, pointer to the modified header, or NULL
3309  *                to remove the header.  This function frees the
3310  *                original string if necessary.
3311  *
3312  * Returns     :  JB_ERR_OK on success, or
3313  *                JB_ERR_MEMORY on out-of-memory error.
3314  *
3315  *********************************************************************/
3316 static jb_err client_send_cookie(struct client_state *csp, char **header)
3317 {
3318    if (csp->action->flags & ACTION_NO_COOKIE_READ)
3319    {
3320       log_error(LOG_LEVEL_HEADER, "Crunched outgoing cookie: %s", *header);
3321       freez(*header);
3322    }
3323
3324    return JB_ERR_OK;
3325 }
3326
3327
3328 /*********************************************************************
3329  *
3330  * Function    :  client_x_forwarded
3331  *
3332  * Description :  Handle the "x-forwarded-for" config setting properly,
3333  *                also used in the add_client_headers list.  Called from `sed'.
3334  *
3335  * Parameters  :
3336  *          1  :  csp = Current client state (buffers, headers, etc...)
3337  *          2  :  header = On input, pointer to header to modify.
3338  *                On output, pointer to the modified header, or NULL
3339  *                to remove the header.  This function frees the
3340  *                original string if necessary.
3341  *
3342  * Returns     :  JB_ERR_OK on success, or
3343  *                JB_ERR_MEMORY on out-of-memory error.
3344  *
3345  *********************************************************************/
3346 jb_err client_x_forwarded(struct client_state *csp, char **header)
3347 {
3348    if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
3349    {
3350       freez(*header);
3351       log_error(LOG_LEVEL_HEADER, "crunched x-forwarded-for!");
3352    }
3353
3354    return JB_ERR_OK;
3355 }
3356
3357
3358 /*********************************************************************
3359  *
3360  * Function    :  client_max_forwards
3361  *
3362  * Description :  If the HTTP method is OPTIONS or TRACE, subtract one
3363  *                from the value of the Max-Forwards header field.
3364  *
3365  * Parameters  :
3366  *          1  :  csp = Current client state (buffers, headers, etc...)
3367  *          2  :  header = On input, pointer to header to modify.
3368  *                On output, pointer to the modified header, or NULL
3369  *                to remove the header.  This function frees the
3370  *                original string if necessary.
3371  *
3372  * Returns     :  JB_ERR_OK on success, or
3373  *                JB_ERR_MEMORY on out-of-memory error.
3374  *
3375  *********************************************************************/
3376 static jb_err client_max_forwards(struct client_state *csp, char **header)
3377 {
3378    int max_forwards;
3379
3380    if ((0 == strcmpic(csp->http->gpc, "trace")) ||
3381        (0 == strcmpic(csp->http->gpc, "options")))
3382    {
3383       assert(*(*header+12) == ':');
3384       if (1 == sscanf(*header+12, ": %u", &max_forwards))
3385       {
3386          if (max_forwards > 0)
3387          {
3388             snprintf(*header, strlen(*header)+1, "Max-Forwards: %u", --max_forwards);
3389             log_error(LOG_LEVEL_HEADER, "Max-Forwards value for %s request reduced to %u.",
3390                csp->http->gpc, max_forwards);
3391          }
3392          else if (max_forwards < 0)
3393          {
3394             log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
3395             freez(*header);
3396          }
3397       }
3398       else
3399       {
3400          log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
3401          freez(*header);
3402       }
3403    }
3404
3405    return JB_ERR_OK;
3406 }
3407
3408
3409 /*********************************************************************
3410  *
3411  * Function    :  client_host
3412  *
3413  * Description :  If the request URI did not contain host and
3414  *                port information, parse and evaluate the Host
3415  *                header field.
3416  *
3417  *                Also, kill ill-formed HOST: headers as sent by
3418  *                Apple's iTunes software when used with a proxy.
3419  *
3420  * Parameters  :
3421  *          1  :  csp = Current client state (buffers, headers, etc...)
3422  *          2  :  header = On input, pointer to header to modify.
3423  *                On output, pointer to the modified header, or NULL
3424  *                to remove the header.  This function frees the
3425  *                original string if necessary.
3426  *
3427  * Returns     :  JB_ERR_OK on success, or
3428  *                JB_ERR_MEMORY on out-of-memory error.
3429  *
3430  *********************************************************************/
3431 static jb_err client_host(struct client_state *csp, char **header)
3432 {
3433    char *p, *q;
3434
3435    /*
3436     * If the header field name is all upper-case, chances are that it's
3437     * an ill-formed one from iTunes. BTW, killing innocent headers here is
3438     * not a problem -- they are regenerated later.
3439     */
3440    if ((*header)[1] == 'O')
3441    {
3442       log_error(LOG_LEVEL_HEADER, "Killed all-caps Host header line: %s", *header);
3443       freez(*header);
3444       return JB_ERR_OK;
3445    }
3446
3447    if (!csp->http->hostport || (*csp->http->hostport == '*') ||  
3448        *csp->http->hostport == ' ' || *csp->http->hostport == '\0')
3449    {
3450       
3451       if (NULL == (p = strdup((*header)+6)))
3452       {
3453          return JB_ERR_MEMORY;
3454       }
3455       chomp(p);
3456       if (NULL == (q = strdup(p)))
3457       {
3458          freez(p);
3459          return JB_ERR_MEMORY;
3460       }
3461
3462       freez(csp->http->hostport);
3463       csp->http->hostport = p;
3464       freez(csp->http->host);
3465       csp->http->host = q;
3466       q = strchr(csp->http->host, ':');
3467       if (q != NULL)
3468       {
3469          /* Terminate hostname and evaluate port string */
3470          *q++ = '\0';
3471          csp->http->port = atoi(q);
3472       }
3473       else
3474       {
3475          csp->http->port = csp->http->ssl ? 443 : 80;
3476       }
3477
3478       log_error(LOG_LEVEL_HEADER, "New host and port from Host field: %s = %s:%d",
3479                 csp->http->hostport, csp->http->host, csp->http->port);
3480    }
3481
3482    /* Signal client_host_adder() to return right away */
3483    csp->flags |= CSP_FLAG_HOST_HEADER_IS_SET;
3484
3485    return JB_ERR_OK;
3486 }
3487
3488
3489 /*********************************************************************
3490  *
3491  * Function    :  client_if_modified_since
3492  *
3493  * Description :  Remove or modify the If-Modified-Since header.
3494  *
3495  * Parameters  :
3496  *          1  :  csp = Current client state (buffers, headers, etc...)
3497  *          2  :  header = On input, pointer to header to modify.
3498  *                On output, pointer to the modified header, or NULL
3499  *                to remove the header.  This function frees the
3500  *                original string if necessary.
3501  *
3502  * Returns     :  JB_ERR_OK on success, or
3503  *                JB_ERR_MEMORY on out-of-memory error.
3504  *
3505  *********************************************************************/
3506 static jb_err client_if_modified_since(struct client_state *csp, char **header)
3507 {
3508    char newheader[50];
3509 #ifdef HAVE_GMTIME_R
3510    struct tm gmt;
3511 #endif
3512    struct tm *timeptr = NULL;
3513    time_t tm = 0;                  
3514    const char *newval;
3515    long int rtime;
3516    long int hours, minutes, seconds;
3517    int negative = 0;
3518    char * endptr;
3519    
3520    if ( 0 == strcmpic(*header, "If-Modified-Since: Wed, 08 Jun 1955 12:00:00 GMT"))
3521    {
3522       /* 
3523        * The client got an error message because of a temporary problem,
3524        * the problem is gone and the client now tries to revalidate our
3525        * error message on the real server. The revalidation would always
3526        * end with the transmission of the whole document and there is
3527        * no need to expose the bogus If-Modified-Since header.
3528        */
3529       log_error(LOG_LEVEL_HEADER, "Crunching useless If-Modified-Since header.");
3530       freez(*header);
3531    }
3532    else if (csp->action->flags & ACTION_HIDE_IF_MODIFIED_SINCE)
3533    {
3534       newval = csp->action->string[ACTION_STRING_IF_MODIFIED_SINCE];
3535
3536       if ((0 == strcmpic(newval, "block")))
3537       {
3538          log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
3539          freez(*header);
3540       }
3541       else /* add random value */
3542       {
3543          const char *header_time = *header + sizeof("If-Modified-Since:");
3544
3545          if (JB_ERR_OK != parse_header_time(header_time, &tm))
3546          {
3547             log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s in %s (crunching!)", header_time, *header);
3548             freez(*header);
3549          }
3550          else
3551          {
3552             rtime = strtol(newval, &endptr, 0);
3553             if (rtime)
3554             {
3555                log_error(LOG_LEVEL_HEADER, "Randomizing: %s (random range: %d minut%s)",
3556                   *header, rtime, (rtime == 1 || rtime == -1) ? "e": "es");
3557                if (rtime < 0)
3558                {
3559                   rtime *= -1; 
3560                   negative = 1;
3561                }
3562                rtime *= 60;
3563                rtime = pick_from_range(rtime);
3564             }
3565             else
3566             {
3567                log_error(LOG_LEVEL_ERROR, "Random range is 0. Assuming time transformation test.",
3568                   *header);
3569             }
3570             tm += rtime * (negative ? -1 : 1);
3571 #ifdef HAVE_GMTIME_R
3572             timeptr = gmtime_r(&tm, &gmt);
3573 #elif FEATURE_PTHREAD
3574             pthread_mutex_lock(&gmtime_mutex);
3575             timeptr = gmtime(&tm);
3576             pthread_mutex_unlock(&gmtime_mutex);
3577 #else
3578             timeptr = gmtime(&tm);
3579 #endif
3580             strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
3581
3582             freez(*header);
3583             *header = strdup("If-Modified-Since: ");
3584             string_append(header, newheader);
3585
3586             if (*header == NULL)
3587             {
3588                log_error(LOG_LEVEL_HEADER, "Insufficent memory, header crunched without replacement.");
3589                return JB_ERR_MEMORY;  
3590             }
3591
3592             if (LOG_LEVEL_HEADER & debug) /* Save cycles if the user isn't interested. */
3593             {
3594                hours   = rtime / 3600;
3595                minutes = rtime / 60 % 60;
3596                seconds = rtime % 60;            
3597
3598                log_error(LOG_LEVEL_HEADER, "Randomized:  %s (%s %d hou%s %d minut%s %d second%s",
3599                   *header, (negative) ? "subtracted" : "added", hours, (hours == 1) ? "r" : "rs",
3600                   minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
3601             }
3602          }
3603       }
3604    }
3605
3606    return JB_ERR_OK;
3607 }
3608
3609
3610 /*********************************************************************
3611  *
3612  * Function    :  client_if_none_match
3613  *
3614  * Description :  Remove the If-None-Match header.
3615  *
3616  * Parameters  :
3617  *          1  :  csp = Current client state (buffers, headers, etc...)
3618  *          2  :  header = On input, pointer to header to modify.
3619  *                On output, pointer to the modified header, or NULL
3620  *                to remove the header.  This function frees the
3621  *                original string if necessary.
3622  *
3623  * Returns     :  JB_ERR_OK on success, or
3624  *                JB_ERR_MEMORY on out-of-memory error.
3625  *
3626  *********************************************************************/
3627 static jb_err client_if_none_match(struct client_state *csp, char **header)
3628 {
3629    if (csp->action->flags & ACTION_CRUNCH_IF_NONE_MATCH)
3630    {  
3631       log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
3632       freez(*header);
3633    }
3634
3635    return JB_ERR_OK;
3636 }
3637
3638
3639 /*********************************************************************
3640  *
3641  * Function    :  client_x_filter
3642  *
3643  * Description :  Disables filtering if the client set "X-Filter: No".
3644  *                Called from `sed'.
3645  *
3646  * Parameters  :
3647  *          1  :  csp = Current client state (buffers, headers, etc...)
3648  *          2  :  header = On input, pointer to header to modify.
3649  *                On output, pointer to the modified header, or NULL
3650  *                to remove the header.  This function frees the
3651  *                original string if necessary.
3652  *
3653  * Returns     :  JB_ERR_OK on success
3654  *
3655  *********************************************************************/
3656 jb_err client_x_filter(struct client_state *csp, char **header)
3657 {
3658    if ( 0 == strcmpic(*header, "X-Filter: No"))
3659    {
3660       if (!(csp->config->feature_flags & RUNTIME_FEATURE_HTTP_TOGGLE))
3661       {
3662          log_error(LOG_LEVEL_INFO, "Ignored the client's request to fetch without filtering.");
3663       }
3664       else
3665       {
3666          if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
3667          {
3668             log_error(LOG_LEVEL_HEADER,
3669                "force-text-mode overruled the client's request to fetch without filtering!");
3670          }
3671          else
3672          {  
3673             csp->content_type = CT_TABOO; /* XXX: This hack shouldn't be necessary */
3674             csp->flags |= CSP_FLAG_NO_FILTERING;
3675             log_error(LOG_LEVEL_HEADER, "Accepted the client's request to fetch without filtering.");
3676          }
3677          log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
3678          freez(*header);
3679       }
3680    }
3681    return JB_ERR_OK; 
3682 }
3683
3684
3685 /*********************************************************************
3686  *
3687  * Function    :  client_range
3688  *
3689  * Description :  Removes Range, Request-Range and If-Range headers if
3690  *                content filtering is enabled. If the client's version
3691  *                of the document has been altered by Privoxy, the server
3692  *                could interpret the range differently than the client
3693  *                intended in which case the user could end up with
3694  *                corrupted content.
3695  *
3696  * Parameters  :
3697  *          1  :  csp = Current client state (buffers, headers, etc...)
3698  *          2  :  header = On input, pointer to header to modify.
3699  *                On output, pointer to the modified header, or NULL
3700  *                to remove the header.  This function frees the
3701  *                original string if necessary.
3702  *
3703  * Returns     :  JB_ERR_OK
3704  *
3705  *********************************************************************/
3706 static jb_err client_range(struct client_state *csp, char **header)
3707 {
3708    if (content_filters_enabled(csp->action))
3709    {
3710       log_error(LOG_LEVEL_HEADER, "Content filtering is enabled."
3711          " Crunching: \'%s\' to prevent range-mismatch problems.", *header);
3712       freez(*header);
3713    }
3714
3715    return JB_ERR_OK; 
3716 }
3717
3718 /* the following functions add headers directly to the header list */
3719
3720 /*********************************************************************
3721  *
3722  * Function    :  client_host_adder
3723  *
3724  * Description :  Adds the Host: header field if it is missing.
3725  *                Called from `sed'.
3726  *
3727  * Parameters  :
3728  *          1  :  csp = Current client state (buffers, headers, etc...)
3729  *
3730  * Returns     :  JB_ERR_OK on success, or
3731  *                JB_ERR_MEMORY on out-of-memory error.
3732  *
3733  *********************************************************************/
3734 static jb_err client_host_adder(struct client_state *csp)
3735 {
3736    char *p;
3737    jb_err err;
3738
3739    if (csp->flags & CSP_FLAG_HOST_HEADER_IS_SET)
3740    {
3741       /* Header already set by the client, nothing to do. */
3742       return JB_ERR_OK;
3743    }
3744
3745    if ( !csp->http->hostport || !*(csp->http->hostport))
3746    {
3747       /* XXX: When does this happen and why is it OK? */
3748       log_error(LOG_LEVEL_INFO, "Weirdness in client_host_adder detected and ignored.");
3749       return JB_ERR_OK;
3750    }
3751
3752    /*
3753     * remove 'user:pass@' from 'proto://user:pass@host'
3754     */
3755    if ( (p = strchr( csp->http->hostport, '@')) != NULL )
3756    {
3757       p++;
3758    }
3759    else
3760    {
3761       p = csp->http->hostport;
3762    }
3763
3764    /* XXX: Just add it, we already made sure that it will be unique */
3765    log_error(LOG_LEVEL_HEADER, "addh-unique: Host: %s", p);
3766    err = enlist_unique_header(csp->headers, "Host", p);
3767    return err;
3768
3769 }
3770
3771
3772 #if 0
3773 /*********************************************************************
3774  *
3775  * Function    :  client_accept_encoding_adder
3776  *
3777  * Description :  Add an Accept-Encoding header to the client's request
3778  *                that disables compression if the action applies, and
3779  *                the header is not already there. Called from `sed'.
3780  *                Note: For HTTP/1.0, the absence of the header is enough.
3781  *
3782  * Parameters  :
3783  *          1  :  csp = Current client state (buffers, headers, etc...)
3784  *
3785  * Returns     :  JB_ERR_OK on success, or
3786  *                JB_ERR_MEMORY on out-of-memory error.
3787  *
3788  *********************************************************************/
3789 static jb_err client_accept_encoding_adder(struct client_state *csp)
3790 {
3791    if (   ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
3792        && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
3793    {
3794       return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
3795    }
3796
3797    return JB_ERR_OK;
3798 }
3799 #endif
3800
3801
3802 /*********************************************************************
3803  *
3804  * Function    :  client_xtra_adder
3805  *
3806  * Description :  Used in the add_client_headers list.  Called from `sed'.
3807  *
3808  * Parameters  :
3809  *          1  :  csp = Current client state (buffers, headers, etc...)
3810  *
3811  * Returns     :  JB_ERR_OK on success, or
3812  *                JB_ERR_MEMORY on out-of-memory error.
3813  *
3814  *********************************************************************/
3815 static jb_err client_xtra_adder(struct client_state *csp)
3816 {
3817    struct list_entry *lst;
3818    jb_err err;
3819
3820    for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
3821         lst ; lst = lst->next)
3822    {
3823       log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
3824       err = enlist(csp->headers, lst->str);
3825       if (err)
3826       {
3827          return err;
3828       }
3829
3830    }
3831
3832    return JB_ERR_OK;
3833 }
3834
3835
3836 /*********************************************************************
3837  *
3838  * Function    :  connection_close_adder
3839  *
3840  * Description :  "Temporary" fix for the needed but missing HTTP/1.1
3841  *                support. Adds a "Connection: close" header to csp->headers
3842  *                unless the header was already present. Called from `sed'.
3843  *
3844  *                FIXME: This whole function shouldn't be neccessary!
3845  *
3846  * Parameters  :
3847  *          1  :  csp = Current client state (buffers, headers, etc...)
3848  *
3849  * Returns     :  JB_ERR_OK on success, or
3850  *                JB_ERR_MEMORY on out-of-memory error.
3851  *
3852  *********************************************************************/
3853 static jb_err connection_close_adder(struct client_state *csp)
3854 {
3855    const unsigned int flags = csp->flags;
3856
3857    /*
3858     * Return right away if
3859     *
3860     * - we're parsing server headers and the server header
3861     *   "Connection: close" is already set, or if
3862     *
3863     * - we're parsing client headers and the client header 
3864     *   "Connection: close" is already set.
3865     */
3866    if ((flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE
3867      && flags & CSP_FLAG_SERVER_CONNECTION_CLOSE_SET)
3868    ||(!(flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
3869      && flags & CSP_FLAG_CLIENT_CONNECTION_CLOSE_SET))
3870    {
3871       return JB_ERR_OK;
3872    }
3873
3874    log_error(LOG_LEVEL_HEADER, "Adding: Connection: close");
3875
3876    return enlist(csp->headers, "Connection: close");
3877 }
3878
3879
3880 /*********************************************************************
3881  *
3882  * Function    :  server_http
3883  *
3884  * Description :  - Save the HTTP Status into csp->http->status
3885  *                - Set CT_TABOO to prevent filtering if the answer
3886  *                  is a partial range (HTTP status 206)
3887  *                - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
3888  *                  action applies.
3889  *
3890  * Parameters  :
3891  *          1  :  csp = Current client state (buffers, headers, etc...)
3892  *          2  :  header = On input, pointer to header to modify.
3893  *                On output, pointer to the modified header, or NULL
3894  *                to remove the header.  This function frees the
3895  *                original string if necessary.
3896  *
3897  * Returns     :  JB_ERR_OK on success, or
3898  *                JB_ERR_MEMORY on out-of-memory error.
3899  *
3900  *********************************************************************/
3901 static jb_err server_http(struct client_state *csp, char **header)
3902 {
3903    sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
3904    if (csp->http->status == 206)
3905    {
3906       csp->content_type = CT_TABOO;
3907    }
3908
3909    if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
3910    {
3911       /* XXX: Should we do a real validity check here? */
3912       if (strlen(*header) > 8)
3913       {
3914          (*header)[7] = '0';
3915          log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
3916       }
3917       else
3918       {
3919          /*
3920           * XXX: Should we block the request or
3921           * enlist a valid status code line here?
3922           */
3923          log_error(LOG_LEVEL_INFO, "Malformed server response detected. "
3924             "Downgrading to HTTP/1.0 impossible.");
3925       }
3926    }
3927
3928    return JB_ERR_OK;
3929 }
3930
3931
3932 /*********************************************************************
3933  *
3934  * Function    :  server_set_cookie
3935  *
3936  * Description :  Handle the server "cookie" header properly.
3937  *                Log cookie to the jar file.  Then "crunch",
3938  *                accept or rewrite it to a session cookie.
3939  *                Called from `sed'.
3940  *
3941  *                TODO: Allow the user to specify a new expiration
3942  *                time to cause the cookie to expire even before the
3943  *                browser is closed.
3944  *
3945  * Parameters  :
3946  *          1  :  csp = Current client state (buffers, headers, etc...)
3947  *          2  :  header = On input, pointer to header to modify.
3948  *                On output, pointer to the modified header, or NULL
3949  *                to remove the header.  This function frees the
3950  *                original string if necessary.
3951  *
3952  * Returns     :  JB_ERR_OK on success, or
3953  *                JB_ERR_MEMORY on out-of-memory error.
3954  *
3955  *********************************************************************/
3956 static jb_err server_set_cookie(struct client_state *csp, char **header)
3957 {
3958    time_t now;
3959    time_t cookie_time; 
3960    struct tm tm_now; 
3961    time(&now);
3962
3963 #ifdef FEATURE_COOKIE_JAR
3964    if (csp->config->jar)
3965    {
3966       /*
3967        * Write timestamp into outbuf.
3968        *
3969        * Complex because not all OSs have tm_gmtoff or
3970        * the %z field in strftime()
3971        */
3972       char tempbuf[ BUFFER_SIZE ];
3973  
3974 #ifdef HAVE_LOCALTIME_R
3975       tm_now = *localtime_r(&now, &tm_now);
3976 #elif FEATURE_PTHREAD
3977       pthread_mutex_lock(&localtime_mutex);
3978       tm_now = *localtime (&now);
3979       pthread_mutex_unlock(&localtime_mutex);
3980 #else
3981       tm_now = *localtime (&now);
3982 #endif
3983       strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now); 
3984
3985       /* strlen("set-cookie: ") = 12 */
3986       fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, *header + 12);
3987    }
3988 #endif /* def FEATURE_COOKIE_JAR */
3989
3990    if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
3991    {
3992       log_error(LOG_LEVEL_HEADER, "Crunching incoming cookie: %s", *header);
3993       freez(*header);
3994    }
3995    else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
3996    {
3997       /* Flag whether or not to log a message */
3998       int changed = 0;
3999
4000       /* A variable to store the tag we're working on */
4001       char *cur_tag;
4002
4003       /* Skip "Set-Cookie:" (11 characters) in header */
4004       cur_tag = *header + 11;
4005
4006       /* skip whitespace between "Set-Cookie:" and value */
4007       while (*cur_tag && ijb_isspace(*cur_tag))
4008       {
4009          cur_tag++;
4010       }
4011
4012       /* Loop through each tag in the cookie */
4013       while (*cur_tag)
4014       {
4015          /* Find next tag */
4016          char *next_tag = strchr(cur_tag, ';');
4017          if (next_tag != NULL)
4018          {
4019             /* Skip the ';' character itself */
4020             next_tag++;
4021
4022             /* skip whitespace ";" and start of tag */
4023             while (*next_tag && ijb_isspace(*next_tag))
4024             {
4025                next_tag++;
4026             }
4027          }
4028          else
4029          {
4030             /* "Next tag" is the end of the string */
4031             next_tag = cur_tag + strlen(cur_tag);
4032          }
4033
4034          /*
4035           * Check the expiration date to see
4036           * if the cookie is still valid, if yes,
4037           * rewrite it to a session cookie.
4038           */
4039          if ((strncmpic(cur_tag, "expires=", 8) == 0) && *(cur_tag + 8))
4040          {
4041             char *expiration_date = cur_tag + 8; /* Skip "[Ee]xpires=" */
4042
4043             /* Did we detect the date properly? */
4044             if (JB_ERR_OK != parse_header_time(expiration_date, &cookie_time))
4045             {
4046                /*
4047                 * Nope, treat it as if it was still valid.
4048                 *
4049                 * XXX: Should we remove the whole cookie instead?
4050                 */
4051                log_error(LOG_LEVEL_ERROR,
4052                   "Can't parse \'%s\', send by %s. Unsupported time format?", cur_tag, csp->http->url);
4053                string_move(cur_tag, next_tag);
4054                changed = 1;
4055             }
4056             else
4057             {
4058                /*
4059                 * Yes. Check if the cookie is still valid.
4060                 *
4061                 * If the cookie is already expired it's probably
4062                 * a delete cookie and even if it isn't, the browser
4063                 * will discard it anyway.
4064                 */
4065
4066                /*
4067                 * XXX: timegm() isn't available on some AmigaOS
4068                 * versions and our replacement doesn't work.
4069                 *
4070                 * Our options are to either:
4071                 *
4072                 * - disable session-cookies-only completely if timegm
4073                 *   is missing,
4074                 *
4075                 * - to simply remove all expired tags, like it has
4076                 *   been done until Privoxy 3.0.6 and to live with
4077                 *    the consequence that it can cause login/logout
4078                 *   problems on servers that don't validate their
4079                 *   input properly, or
4080                 *
4081                 * - to replace it with mktime in which
4082                 *   case there is a slight chance of valid cookies
4083                 *   passing as already expired.
4084                 *
4085                 *   This is the way it's currently done and it's not
4086                 *   as bad as it sounds. If the missing GMT offset is
4087                 *   enough to change the result of the expiration check
4088                 *   the cookie will be only valid for a few hours
4089                 *   anyway, which in many cases will be shorter
4090                 *   than a browser session.
4091                 */
4092                if (cookie_time - now < 0)
4093                {
4094                   log_error(LOG_LEVEL_HEADER,
4095                      "Cookie \'%s\' is already expired and can pass unmodified.", *header);
4096                   /* Just in case some clown sets more then one expiration date */
4097                   cur_tag = next_tag;
4098                }
4099                else
4100                {
4101                   /*
4102                    * Still valid, delete expiration date by copying
4103                    * the rest of the string over it.
4104                    */
4105                   string_move(cur_tag, next_tag);
4106
4107                   /* That changed the header, need to issue a log message */
4108                   changed = 1;
4109
4110                   /*
4111                    * Note that the next tag has now been moved to *cur_tag,
4112                    * so we do not need to update the cur_tag pointer.
4113                    */
4114                }
4115             }
4116
4117          }
4118          else
4119          {
4120             /* Move on to next cookie tag */
4121             cur_tag = next_tag;
4122          }
4123       }
4124
4125       if (changed)
4126       {
4127          assert(NULL != *header);
4128          log_error(LOG_LEVEL_HEADER, "Cookie rewritten to a temporary one: %s",
4129             *header);
4130       }
4131    }
4132
4133    return JB_ERR_OK;
4134 }
4135
4136
4137 #ifdef FEATURE_FORCE_LOAD
4138 /*********************************************************************
4139  *
4140  * Function    :  strclean
4141  *
4142  * Description :  In-Situ-Eliminate all occurances of substring in
4143  *                string
4144  *
4145  * Parameters  :
4146  *          1  :  string = string to clean
4147  *          2  :  substring = substring to eliminate
4148  *
4149  * Returns     :  Number of eliminations
4150  *
4151  *********************************************************************/
4152 int strclean(const char *string, const char *substring)
4153 {
4154    int hits = 0;
4155    size_t len;
4156    char *pos, *p;
4157
4158    len = strlen(substring);
4159
4160    while((pos = strstr(string, substring)) != NULL)
4161    {
4162       p = pos + len;
4163       do
4164       {
4165          *(p - len) = *p;
4166       }
4167       while (*p++ != '\0');
4168
4169       hits++;
4170    }
4171
4172    return(hits);
4173 }
4174 #endif /* def FEATURE_FORCE_LOAD */
4175
4176
4177 /*********************************************************************
4178  *
4179  * Function    :  parse_header_time
4180  *
4181  * Description :  Parses time formats used in HTTP header strings
4182  *                to get the numerical respresentation.
4183  *
4184  * Parameters  :
4185  *          1  :  header_time = HTTP header time as string. 
4186  *          2  :  result = storage for header_time in seconds
4187  *
4188  * Returns     :  JB_ERR_OK if the time format was recognized, or
4189  *                JB_ERR_PARSE otherwise.
4190  *
4191  *********************************************************************/
4192 static jb_err parse_header_time(const char *header_time, time_t *result)
4193 {
4194    struct tm gmt;
4195
4196    /*
4197     * Zero out gmt to prevent time zone offsets.
4198     *
4199     * While this is only necessary on some platforms
4200     * (mingw32 for example), I don't know how to
4201     * detect these automatically and doing it everywhere
4202     * shouldn't hurt.
4203     */
4204    memset(&gmt, 0, sizeof(gmt));
4205
4206                             /* Tue, 02 Jun 2037 20:00:00 */
4207    if ((NULL == strptime(header_time, "%a, %d %b %Y %H:%M:%S", &gmt))
4208                             /* Tue, 02-Jun-2037 20:00:00 */
4209     && (NULL == strptime(header_time, "%a, %d-%b-%Y %H:%M:%S", &gmt))
4210                             /* Tue, 02-Jun-37 20:00:00 */
4211     && (NULL == strptime(header_time, "%a, %d-%b-%y %H:%M:%S", &gmt))
4212                         /* Tuesday, 02-Jun-2037 20:00:00 */
4213     && (NULL == strptime(header_time, "%A, %d-%b-%Y %H:%M:%S", &gmt))
4214                         /* Tuesday Jun 02 20:00:00 2037 */
4215     && (NULL == strptime(header_time, "%A %b %d %H:%M:%S %Y", &gmt)))
4216    {
4217       return JB_ERR_PARSE;
4218    }
4219
4220    *result = timegm(&gmt);
4221
4222    return JB_ERR_OK;
4223
4224 }
4225
4226
4227 /*********************************************************************
4228  *
4229  * Function    :  get_destination_from_headers
4230  *
4231  * Description :  Parse the "Host:" header to get the request's destination.
4232  *                Only needed if the client's request was forcefully
4233  *                redirected into Privoxy.
4234  *
4235  *                Code mainly copied from client_host() which is currently
4236  *                run too late for this purpose.
4237  *
4238  * Parameters  :
4239  *          1  :  headers = List of headers (one of them hopefully being
4240  *                the "Host:" header)
4241  *          2  :  http = storage for the result (host, port and hostport). 
4242  *
4243  * Returns     :  JB_ERR_MEMORY in case of memory problems,
4244  *                JB_ERR_PARSE if the host header couldn't be found,
4245  *                JB_ERR_OK otherwise.
4246  *
4247  *********************************************************************/
4248 jb_err get_destination_from_headers(const struct list *headers, struct http_request *http)
4249 {
4250    char *q;
4251    char *p;
4252    char *host;
4253
4254    host = get_header_value(headers, "Host:");
4255
4256    if (NULL == host)
4257    {
4258       log_error(LOG_LEVEL_ERROR, "No \"Host:\" header found.");
4259       return JB_ERR_PARSE;
4260    }
4261
4262    if (NULL == (p = strdup((host))))
4263    {
4264       log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
4265       return JB_ERR_MEMORY;
4266    }
4267    chomp(p);
4268    if (NULL == (q = strdup(p)))
4269    {
4270       freez(p);
4271       log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
4272       return JB_ERR_MEMORY;
4273    }
4274
4275    freez(http->hostport);
4276    http->hostport = p;
4277    freez(http->host);
4278    http->host = q;
4279    q = strchr(http->host, ':');
4280    if (q != NULL)
4281    {
4282       /* Terminate hostname and evaluate port string */
4283       *q++ = '\0';
4284       http->port = atoi(q);
4285    }
4286    else
4287    {
4288       http->port = http->ssl ? 443 : 80;
4289    }
4290
4291    /* Rebuild request URL */
4292    freez(http->url);
4293    http->url = strdup(http->ssl ? "https://" : "http://");
4294    string_append(&http->url, http->hostport);
4295    string_append(&http->url, http->path);
4296    if (http->url == NULL)
4297    {
4298       return JB_ERR_MEMORY;
4299    }
4300
4301    log_error(LOG_LEVEL_HEADER, "Destination extracted from \"Host:\" header. New request URL: %s",
4302       http->url);
4303
4304    return JB_ERR_OK;
4305
4306 }
4307
4308
4309 /*********************************************************************
4310  *
4311  * Function    :  create_forged_referrer
4312  *
4313  * Description :  Helper for client_referrer to forge a referer as
4314  *                'http://[hostname:port/' to fool stupid
4315  *                checks for in-site links 
4316  *
4317  * Parameters  :
4318  *          1  :  header   = Pointer to header pointer
4319  *          2  :  hostport = Host and optionally port as string
4320  *
4321  * Returns     :  JB_ERR_OK in case of success, or
4322  *                JB_ERR_MEMORY in case of memory problems.
4323  *
4324  *********************************************************************/
4325 static jb_err create_forged_referrer(char **header, const char *hostport)
4326 {
4327     assert(NULL == *header);
4328
4329     *header = strdup("Referer: http://");
4330     string_append(header, hostport);
4331     string_append(header, "/");
4332
4333     if (NULL == *header)
4334     {
4335        return JB_ERR_MEMORY;
4336     }
4337
4338     log_error(LOG_LEVEL_HEADER, "Referer forged to: %s", *header);
4339
4340     return JB_ERR_OK;
4341
4342 }
4343
4344
4345 /*********************************************************************
4346  *
4347  * Function    :  create_fake_referrer
4348  *
4349  * Description :  Helper for client_referrer to create a fake referrer
4350  *                based on a string supplied by the user.
4351  *
4352  * Parameters  :
4353  *          1  :  header   = Pointer to header pointer
4354  *          2  :  hosthost = Referrer to fake
4355  *
4356  * Returns     :  JB_ERR_OK in case of success, or
4357  *                JB_ERR_MEMORY in case of memory problems.
4358  *
4359  *********************************************************************/
4360 static jb_err create_fake_referrer(char **header, const char *fake_referrer)
4361 {
4362    assert(NULL == *header);
4363
4364    if ((0 != strncmpic(fake_referrer, "http://", 7)) && (0 != strncmpic(fake_referrer, "https://", 8)))
4365    {
4366       log_error(LOG_LEVEL_HEADER,
4367          "Parameter: +hide-referrer{%s} is a bad idea, but I don't care.", fake_referrer);
4368    }
4369    *header = strdup("Referer: ");
4370    string_append(header, fake_referrer);
4371
4372    if (NULL == *header)
4373    {
4374       return JB_ERR_MEMORY;
4375    }
4376
4377    log_error(LOG_LEVEL_HEADER, "Referer replaced with: %s", *header);
4378
4379    return JB_ERR_OK;
4380
4381 }
4382
4383
4384 /*********************************************************************
4385  *
4386  * Function    :  handle_conditional_hide_referrer_parameter
4387  *
4388  * Description :  Helper for client_referrer to crunch or forge
4389  *                the referrer header if the host has changed.
4390  *
4391  * Parameters  :
4392  *          1  :  header = Pointer to header pointer
4393  *          2  :  host   = The target host (may include the port)
4394  *          3  :  parameter_conditional_block = Boolean to signal
4395  *                if we're in conditional-block mode. If not set,
4396  *                we're in conditional-forge mode.
4397  *
4398  * Returns     :  JB_ERR_OK in case of success, or
4399  *                JB_ERR_MEMORY in case of memory problems.
4400  *
4401  *********************************************************************/
4402 static jb_err handle_conditional_hide_referrer_parameter(char **header,
4403    const char *host, const int parameter_conditional_block)
4404 {
4405    char *referer = strdup(*header);
4406    const size_t hostlenght = strlen(host);
4407
4408    if (NULL == referer)
4409    {
4410       freez(*header);
4411       return JB_ERR_MEMORY;
4412    }
4413
4414    /* referer begins with 'Referer: http[s]://' */
4415    if (hostlenght < (strlen(referer)-17))
4416    {
4417       /*
4418        * Shorten referer to make sure the referer is blocked
4419        * if www.example.org/www.example.com-shall-see-the-referer/
4420        * links to www.example.com/
4421        */
4422       referer[hostlenght+17] = '\0';
4423    }
4424    if (NULL == strstr(referer, host))
4425    {
4426       /* Host has changed */
4427       if (parameter_conditional_block)
4428       {
4429          log_error(LOG_LEVEL_HEADER, "New host is: %s. Crunching %s!", host, *header);
4430          freez(*header);
4431       }
4432       else
4433       {
4434          freez(*header);
4435          freez(referer);
4436          return create_forged_referrer(header, host);
4437       }
4438    }
4439    freez(referer);
4440
4441    return JB_ERR_OK;
4442
4443 }
4444
4445 /*
4446   Local Variables:
4447   tab-width: 3
4448   end:
4449 */