Introduced modular filters
[privoxy.git] / parsers.c
1 const char parsers_rcs[] = "$Id: parsers.c,v 1.48 2002/03/07 03:46:53 oes 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',
14  *                   and `server_set_cookie'.
15  *
16  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
17  *                IJBSWA team.  http://ijbswa.sourceforge.net
18  *
19  *                Based on the Internet Junkbuster originally written
20  *                by and Copyright (C) 1997 Anonymous Coders and
21  *                Junkbusters Corporation.  http://www.junkbusters.com
22  *
23  *                This program is free software; you can redistribute it
24  *                and/or modify it under the terms of the GNU General
25  *                Public License as published by the Free Software
26  *                Foundation; either version 2 of the License, or (at
27  *                your option) any later version.
28  *
29  *                This program is distributed in the hope that it will
30  *                be useful, but WITHOUT ANY WARRANTY; without even the
31  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
32  *                PARTICULAR PURPOSE.  See the GNU General Public
33  *                License for more details.
34  *
35  *                The GNU General Public License should be included with
36  *                this file.  If not, you can view it at
37  *                http://www.gnu.org/copyleft/gpl.html
38  *                or write to the Free Software Foundation, Inc., 59
39  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
40  *
41  * Revisions   :
42  *    $Log: parsers.c,v $
43  *    Revision 1.48  2002/03/07 03:46:53  oes
44  *    Fixed compiler warnings etc
45  *
46  *    Revision 1.47  2002/02/20 23:15:13  jongfoster
47  *    Parsing functions now handle out-of-memory gracefully by returning
48  *    an error code.
49  *
50  *    Revision 1.46  2002/01/17 21:03:47  jongfoster
51  *    Moving all our URL and URL pattern parsing code to urlmatch.c.
52  *
53  *    Revision 1.45  2002/01/09 14:33:03  oes
54  *    Added support for localtime_r.
55  *
56  *    Revision 1.44  2001/12/14 01:22:54  steudten
57  *    Remove 'user:pass@' from 'proto://user:pass@host' for the
58  *    new added header 'Host: ..'. (See Req ID 491818)
59  *
60  *    Revision 1.43  2001/11/23 00:26:38  jongfoster
61  *    Fixing two really stupid errors in my previous commit
62  *
63  *    Revision 1.42  2001/11/22 21:59:30  jongfoster
64  *    Adding code to handle +no-cookies-keep
65  *
66  *    Revision 1.41  2001/11/05 23:43:05  steudten
67  *    Add time+date to log files.
68  *
69  *    Revision 1.40  2001/10/26 20:13:09  jongfoster
70  *    ctype.h is needed in Windows, too.
71  *
72  *    Revision 1.39  2001/10/26 17:40:04  oes
73  *    Introduced get_header_value()
74  *    Removed http->user_agent, csp->referrer and csp->accept_types
75  *    Removed client_accept()
76  *
77  *    Revision 1.38  2001/10/25 03:40:48  david__schmidt
78  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
79  *    threads to call select() simultaneously.  So, it's time to do a real, live,
80  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
81  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
82  *
83  *    Revision 1.37  2001/10/23 21:36:02  jongfoster
84  *    Documenting sed()'s error behaviou (doc change only)
85  *
86  *    Revision 1.36  2001/10/13 12:51:51  joergs
87  *    Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
88  *    force-load), instead crumble Host: and add it (again) in client_host_adder
89  *    (in case we get a HTTP/1.0 request without Host: header and forward it to
90  *    a HTTP/1.1 server/proxy).
91  *
92  *    Revision 1.35  2001/10/09 22:39:21  jongfoster
93  *    assert.h is also required under Win32, so moving out of #ifndef _WIN32
94  *    block.
95  *
96  *    Revision 1.34  2001/10/07 18:50:55  oes
97  *    Added server_content_encoding, renamed server_transfer_encoding
98  *
99  *    Revision 1.33  2001/10/07 18:04:49  oes
100  *    Changed server_http11 to server_http and its pattern to "HTTP".
101  *      Additional functionality: it now saves the HTTP status into
102  *      csp->http->status and sets CT_TABOO for Status 206 (partial range)
103  *
104  *    Revision 1.32  2001/10/07 15:43:28  oes
105  *    Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
106  *       client_te and client_accept_encoding_adder, triggered by the new
107  *       +no-compression action. For HTTP/1.1 the Accept-Encoding header is
108  *       changed to allow only identity and chunked, and the TE header is
109  *       crunched. For HTTP/1.0, Accept-Encoding is crunched.
110  *
111  *    parse_http_request no longer does anything than parsing. The rewriting
112  *      of http->cmd and version mangling are gone. It now also recognizes
113  *      the put and delete methods and saves the url in http->url. Removed
114  *      unused variable.
115  *
116  *    renamed content_type and content_length to have the server_ prefix
117  *
118  *    server_content_type now only works if csp->content_type != CT_TABOO
119  *
120  *    added server_transfer_encoding, which
121  *      - Sets CT_TABOO to prohibit filtering if encoding compresses
122  *      - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
123  *      - Change from "chunked" to "identity" if body was chunked
124  *        but has been de-chunked for filtering.
125  *
126  *    added server_content_md5 which crunches any Content-MD5 headers
127  *      if the body was modified.
128  *
129  *    made server_http11 conditional on +downgrade action
130  *
131  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
132  *
133  *    Revision 1.31  2001/10/05 14:25:02  oes
134  *    Crumble Keep-Alive from Server
135  *
136  *    Revision 1.30  2001/09/29 12:56:03  joergs
137  *    IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
138  *
139  *    Revision 1.29  2001/09/24 21:09:24  jongfoster
140  *    Fixing 2 memory leaks that Guy spotted, where the paramater to
141  *    enlist() was not being free()d.
142  *
143  *    Revision 1.28  2001/09/22 16:32:28  jongfoster
144  *    Removing unused #includes.
145  *
146  *    Revision 1.27  2001/09/20 15:45:25  steudten
147  *
148  *    add casting from size_t to int for printf()
149  *    remove local variable shadow s2
150  *
151  *    Revision 1.26  2001/09/16 17:05:14  jongfoster
152  *    Removing unused #include showarg.h
153  *
154  *    Revision 1.25  2001/09/16 13:21:27  jongfoster
155  *    Changes to use new list functions.
156  *
157  *    Revision 1.24  2001/09/13 23:05:50  jongfoster
158  *    Changing the string paramater to the header parsers a "const".
159  *
160  *    Revision 1.23  2001/09/12 18:08:19  steudten
161  *
162  *    In parse_http_request() header rewriting miss the host value, so
163  *    from http://www.mydomain.com the result was just " / " not
164  *    http://www.mydomain.com/ in case we forward.
165  *
166  *    Revision 1.22  2001/09/10 10:58:53  oes
167  *    Silenced compiler warnings
168  *
169  *    Revision 1.21  2001/07/31 14:46:00  oes
170  *     - Persistant connections now suppressed
171  *     - sed() no longer appends empty header to csp->headers
172  *
173  *    Revision 1.20  2001/07/30 22:08:36  jongfoster
174  *    Tidying up #defines:
175  *    - All feature #defines are now of the form FEATURE_xxx
176  *    - Permanently turned off WIN_GUI_EDIT
177  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
178  *
179  *    Revision 1.19  2001/07/25 17:21:54  oes
180  *    client_uagent now saves copy of User-Agent: header value
181  *
182  *    Revision 1.18  2001/07/13 14:02:46  oes
183  *     - Included fix to repair broken HTTP requests that
184  *       don't contain a path, not even '/'.
185  *     - Removed all #ifdef PCRS
186  *     - content_type now always inspected and classified as
187  *       text, gif or other.
188  *     - formatting / comments
189  *
190  *    Revision 1.17  2001/06/29 21:45:41  oes
191  *    Indentation, CRLF->LF, Tab-> Space
192  *
193  *    Revision 1.16  2001/06/29 13:32:42  oes
194  *    - Fixed a comment
195  *    - Adapted free_http_request
196  *    - Removed logentry from cancelled commit
197  *
198  *    Revision 1.15  2001/06/03 19:12:38  oes
199  *    deleted const struct interceptors
200  *
201  *    Revision 1.14  2001/06/01 18:49:17  jongfoster
202  *    Replaced "list_share" with "list" - the tiny memory gain was not
203  *    worth the extra complexity.
204  *
205  *    Revision 1.13  2001/05/31 21:30:33  jongfoster
206  *    Removed list code - it's now in list.[ch]
207  *    Renamed "permission" to "action", and changed many features
208  *    to use the actions file rather than the global config.
209  *
210  *    Revision 1.12  2001/05/31 17:33:13  oes
211  *
212  *    CRLF -> LF
213  *
214  *    Revision 1.11  2001/05/29 20:11:19  joergs
215  *    '/ * inside comment' warning removed.
216  *
217  *    Revision 1.10  2001/05/29 09:50:24  jongfoster
218  *    Unified blocklist/imagelist/permissionslist.
219  *    File format is still under discussion, but the internal changes
220  *    are (mostly) done.
221  *
222  *    Also modified interceptor behaviour:
223  *    - We now intercept all URLs beginning with one of the following
224  *      prefixes (and *only* these prefixes):
225  *        * http://i.j.b/
226  *        * http://ijbswa.sf.net/config/
227  *        * http://ijbswa.sourceforge.net/config/
228  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
229  *    - Internal changes so that intercepted and fast redirect pages
230  *      are not replaced with an image.
231  *    - Interceptors now have the option to send a binary page direct
232  *      to the client. (i.e. ijb-send-banner uses this)
233  *    - Implemented show-url-info interceptor.  (Which is why I needed
234  *      the above interceptors changes - a typical URL is
235  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
236  *      The previous mechanism would not have intercepted that, and
237  *      if it had been intercepted then it then it would have replaced
238  *      it with an image.)
239  *
240  *    Revision 1.9  2001/05/28 17:26:33  jongfoster
241  *    Fixing segfault if last header was crunched.
242  *    Fixing Windows build (snprintf() is _snprintf() under Win32, but we
243  *    can use the cross-platform sprintf() instead.)
244  *
245  *    Revision 1.8  2001/05/27 22:17:04  oes
246  *
247  *    - re_process_buffer no longer writes the modified buffer
248  *      to the client, which was very ugly. It now returns the
249  *      buffer, which it is then written by chat.
250  *
251  *    - content_length now adjusts the Content-Length: header
252  *      for modified documents rather than crunch()ing it.
253  *      (Length info in csp->content_length, which is 0 for
254  *      unmodified documents)
255  *
256  *    - For this to work, sed() is called twice when filtering.
257  *
258  *    Revision 1.7  2001/05/27 13:19:06  oes
259  *    Patched Joergs solution for the content-length in.
260  *
261  *    Revision 1.6  2001/05/26 13:39:32  jongfoster
262  *    Only crunches Content-Length header if applying RE filtering.
263  *    Without this fix, Microsoft Windows Update wouldn't work.
264  *
265  *    Revision 1.5  2001/05/26 00:28:36  jongfoster
266  *    Automatic reloading of config file.
267  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
268  *    Most of the global variables have been moved to a new
269  *    struct configuration_spec, accessed through csp->config->globalname
270  *    Most of the globals remaining are used by the Win32 GUI.
271  *
272  *    Revision 1.4  2001/05/22 18:46:04  oes
273  *
274  *    - Enabled filtering banners by size rather than URL
275  *      by adding patterns that replace all standard banner
276  *      sizes with the "Junkbuster" gif to the re_filterfile
277  *
278  *    - Enabled filtering WebBugs by providing a pattern
279  *      which kills all 1x1 images
280  *
281  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
282  *      which is selected by the (nonstandard and therefore
283  *      capital) letter 'U' in the option string.
284  *      It causes the quantifiers to be ungreedy by default.
285  *      Appending a ? turns back to greedy (!).
286  *
287  *    - Added a new interceptor ijb-send-banner, which
288  *      sends back the "Junkbuster" gif. Without imagelist or
289  *      MSIE detection support, or if tinygif = 1, or the
290  *      URL isn't recognized as an imageurl, a lame HTML
291  *      explanation is sent instead.
292  *
293  *    - Added new feature, which permits blocking remote
294  *      script redirects and firing back a local redirect
295  *      to the browser.
296  *      The feature is conditionally compiled, i.e. it
297  *      can be disabled with --disable-fast-redirects,
298  *      plus it must be activated by a "fast-redirects"
299  *      line in the config file, has its own log level
300  *      and of course wants to be displayed by show-proxy-args
301  *      Note: Boy, all the #ifdefs in 1001 locations and
302  *      all the fumbling with configure.in and acconfig.h
303  *      were *way* more work than the feature itself :-(
304  *
305  *    - Because a generic redirect template was needed for
306  *      this, tinygif = 3 now uses the same.
307  *
308  *    - Moved GIFs, and other static HTTP response templates
309  *      to project.h
310  *
311  *    - Some minor fixes
312  *
313  *    - Removed some >400 CRs again (Jon, you really worked
314  *      a lot! ;-)
315  *
316  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
317  *    Version 2.9.4 checkin.
318  *    - Merged popupfile and cookiefile, and added control over PCRS
319  *      filtering, in new "permissionsfile".
320  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
321  *      file error you now get a message box (in the Win32 GUI) rather
322  *      than the program exiting with no explanation.
323  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
324  *      skipping.
325  *    - Removed tabs from "config"
326  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
327  *    - Bumped up version number.
328  *
329  *    Revision 1.2  2001/05/17 23:02:36  oes
330  *     - Made referrer option accept 'L' as a substitute for '§'
331  *
332  *    Revision 1.1.1.1  2001/05/15 13:59:01  oes
333  *    Initial import of version 2.9.3 source tree
334  *
335  *
336  *********************************************************************/
337 \f
338
339 #include "config.h"
340
341 #ifndef _WIN32
342 #include <stdio.h>
343 #include <sys/types.h>
344 #endif
345
346 #include <stdlib.h>
347 #include <ctype.h>
348 #include <assert.h>
349 #include <string.h>
350
351 #if !defined(_WIN32) && !defined(__OS2__)
352 #include <unistd.h>
353 #endif
354
355 #include "project.h"
356 #include "list.h"
357 #include "parsers.h"
358 #include "encode.h"
359 #include "ssplit.h"
360 #include "errlog.h"
361 #include "jbsockets.h"
362 #include "miscutil.h"
363 #include "list.h"
364
365 const char parsers_h_rcs[] = PARSERS_H_VERSION;
366
367 /* Fix a problem with Solaris.  There should be no effect on other
368  * platforms.
369  * Solaris's isspace() is a macro which uses it's argument directly
370  * as an array index.  Therefore we need to make sure that high-bit
371  * characters generate +ve values, and ideally we also want to make
372  * the argument match the declared parameter type of "int".
373  *
374  * Why did they write a character function that can't take a simple
375  * "char" argument?  Doh!
376  */
377 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
378 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
379
380
381 const struct parsers client_patterns[] = {
382    { "referer:",                 8,    client_referrer },
383    { "user-agent:",              11,   client_uagent },
384    { "ua-",                      3,    client_ua },
385    { "from:",                    5,    client_from },
386    { "cookie:",                  7,    client_send_cookie },
387    { "x-forwarded-for:",         16,   client_x_forwarded },
388    { "Accept-Encoding:",         16,   client_accept_encoding },
389    { "TE:",                      3,    client_te },
390    { "Host:",                     5,   crumble },
391 /* { "if-modified-since:",       18,   crumble }, */
392    { "Keep-Alive:",              11,   crumble },
393    { "connection:",              11,   crumble },
394    { "proxy-connection:",        17,   crumble },
395    { NULL,                       0,    NULL }
396 };
397
398
399 const struct parsers server_patterns[] = {
400    { "HTTP",                4, server_http },
401    { "set-cookie:",        11, server_set_cookie },
402    { "connection:",        11, crumble },
403    { "Content-Type:",      13, server_content_type },
404    { "Content-Length:",    15, server_content_length },
405    { "Content-MD5:",       12, server_content_md5 },
406    { "Content-Encoding:",  17, server_content_encoding },
407    { "Transfer-Encoding:", 18, server_transfer_coding },
408    { "Keep-Alive:",        11, crumble },
409    { NULL, 0, NULL }
410 };
411
412
413 const add_header_func_ptr add_client_headers[] = {
414    client_host_adder,
415    client_cookie_adder,
416    client_x_forwarded_adder,
417    client_xtra_adder,
418    client_accept_encoding_adder,
419    connection_close_adder,
420    NULL
421 };
422
423
424 const add_header_func_ptr add_server_headers[] = {
425    connection_close_adder,
426    NULL
427 };
428
429
430 /*********************************************************************
431  *
432  * Function    :  flush_socket
433  *
434  * Description :  Write any pending "buffered" content.
435  *
436  * Parameters  :
437  *          1  :  fd = file descriptor of the socket to read
438  *          2  :  csp = Current client state (buffers, headers, etc...)
439  *
440  * Returns     :  On success, the number of bytes written are returned (zero
441  *                indicates nothing was written).  On error, -1 is returned,
442  *                and errno is set appropriately.  If count is zero and the
443  *                file descriptor refers to a regular file, 0 will be
444  *                returned without causing any other effect.  For a special
445  *                file, the results are not portable.
446  *
447  *********************************************************************/
448 int flush_socket(jb_socket fd, struct client_state *csp)
449 {
450    struct iob *iob = csp->iob;
451    int len = iob->eod - iob->cur;
452
453    if (len <= 0)
454    {
455       return(0);
456    }
457
458    if (write_socket(fd, iob->cur, len))
459    {
460       return(-1);
461    }
462    iob->eod = iob->cur = iob->buf;
463    return(len);
464
465 }
466
467
468 /*********************************************************************
469  *
470  * Function    :  add_to_iob
471  *
472  * Description :  Add content to the buffered page.
473  *
474  * Parameters  :
475  *          1  :  csp = Current client state (buffers, headers, etc...)
476  *          2  :  buf = holds the content to be added to the page
477  *          3  :  n = number of bytes to be added
478  *
479  * Returns     :  Number of bytes in the content buffer.
480  *
481  *********************************************************************/
482 size_t add_to_iob(struct client_state *csp, char *buf, size_t n)
483 {
484    struct iob *iob = csp->iob;
485    size_t have, need;
486    char *p;
487
488    have = iob->eod - iob->cur;
489
490    if (n <= 0)
491    {
492       return(have);
493    }
494
495    need = have + n;
496
497    if ((p = (char *)malloc(need + 1)) == NULL)
498    {
499       log_error(LOG_LEVEL_FATAL, "malloc() iob failed: %E");
500    }
501
502    if (have)
503    {
504       /* there is something in the buffer - save it */
505       memcpy(p, iob->cur, have);
506
507       /* replace the buffer with the new space */
508       freez(iob->buf);
509       iob->buf = p;
510
511       /* point to the end of the data */
512       p += have;
513    }
514    else
515    {
516       /* the buffer is empty, free it and reinitialize */
517       freez(iob->buf);
518       iob->buf = p;
519    }
520
521    /* copy the new data into the iob buffer */
522    memcpy(p, buf, n);
523
524    /* point to the end of the data */
525    p += n;
526
527    /* null terminate == cheap insurance */
528    *p = '\0';
529
530    /* set the pointers to the new values */
531    iob->cur = iob->buf;
532    iob->eod = p;
533
534    return(need);
535
536 }
537
538
539 /*********************************************************************
540  *
541  * Function    :  get_header
542  *
543  * Description :  This (odd) routine will parse the csp->iob
544  *
545  * Parameters  :
546  *          1  :  csp = Current client state (buffers, headers, etc...)
547  *
548  * Returns     :  Any one of the following:
549  *
550  * 1) a pointer to a dynamically allocated string that contains a header line
551  * 2) NULL  indicating that the end of the header was reached
552  * 3) ""    indicating that the end of the iob was reached before finding
553  *          a complete header line.
554  *
555  *********************************************************************/
556 char *get_header(struct client_state *csp)
557 {
558    struct iob *iob;
559    char *p, *q, *ret;
560    iob = csp->iob;
561
562    if ((iob->cur == NULL)
563       || ((p = strchr(iob->cur, '\n')) == NULL))
564    {
565       return(""); /* couldn't find a complete header */
566    }
567
568    *p = '\0';
569
570    ret = strdup(iob->cur);
571    if (ret == NULL)
572    {
573       /* FIXME No way to handle error properly */
574       log_error(LOG_LEVEL_FATAL, "Out of memory in get_header()");
575    }
576
577    iob->cur = p+1;
578
579    if ((q = strchr(ret, '\r'))) *q = '\0';
580
581    /* is this a blank linke (i.e. the end of the header) ? */
582    if (*ret == '\0')
583    {
584       freez(ret);
585       return(NULL);
586    }
587
588    return(ret);
589
590 }
591
592
593 /*********************************************************************
594  *
595  * Function    :  get_header_value
596  *
597  * Description :  Get the value of a given header from a chained list
598  *                of header lines or return NULL if no such header is
599  *                present in the list.
600  *
601  * Parameters  :
602  *          1  :  header_list = pointer to list
603  *          2  :  header_name = string with name of header to look for.
604  *                              Trailing colon required, capitalization
605  *                              doesn't matter.
606  *
607  * Returns     :  NULL if not found, else value of header
608  *
609  *********************************************************************/
610 char *get_header_value(const struct list *header_list, const char *header_name)
611 {
612    struct list_entry *cur_entry;
613    char *ret = NULL;
614    size_t length = 0;
615
616    assert(header_list);
617    assert(header_name);
618    length = strlen(header_name);
619
620    for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
621    {
622       if (cur_entry->str)
623       {
624          if (!strncmpic(cur_entry->str, header_name, length))
625          {
626             /*
627              * Found: return pointer to start of value
628              */
629             ret = (char *) (cur_entry->str + length);
630             while (*ret && ijb_isspace(*ret)) ret++;
631             return(ret);
632          }
633       }
634    }
635
636    /* 
637     * Not found
638     */
639    return NULL;
640
641 }
642
643 /*********************************************************************
644  *
645  * Function    :  sed
646  *
647  * Description :  add, delete or modify lines in the HTTP header streams.
648  *                On entry, it receives a linked list of headers space
649  *                that was allocated dynamically (both the list nodes
650  *                and the header contents).
651  *
652  *                As a side effect it frees the space used by the original
653  *                header lines.
654  *
655  * Parameters  :
656  *          1  :  pats = list of patterns to match against headers
657  *          2  :  more_headers = list of functions to add more
658  *                headers (client or server)
659  *          3  :  csp = Current client state (buffers, headers, etc...)
660  *
661  * Returns     :  Single pointer to a fully formed header, or NULL
662  *                on out-of-memory error.
663  *
664  *********************************************************************/
665 char *sed(const struct parsers pats[],
666           const add_header_func_ptr more_headers[],
667           struct client_state *csp)
668 {
669    struct list_entry *p;
670    const struct parsers *v;
671    const add_header_func_ptr *f;
672    jb_err err = JB_ERR_OK;
673
674    for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
675    {
676       for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
677       {
678          /* Header crunch()ed in previous run? -> ignore */
679          if (p->str == NULL) continue;
680
681          if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
682
683          if (strncmpic(p->str, v->str, v->len) == 0)
684          {
685             err = v->parser(csp, (char **)&(p->str));
686          }
687       }
688    }
689
690    /* place any additional headers on the csp->headers list */
691    for (f = more_headers; (err == JB_ERR_OK) && (*f) ; f++)
692    {
693       err = (*f)(csp);
694    }
695
696    if (err != JB_ERR_OK)
697    {
698       return NULL;
699    }
700
701    return list_to_text(csp->headers);
702 }
703
704
705 /* here begins the family of parser functions that reformat header lines */
706
707
708 /*********************************************************************
709  *
710  * Function    :  crumble
711  *
712  * Description :  This is called if a header matches a pattern to "crunch"
713  *
714  * Parameters  :
715  *          1  :  csp = Current client state (buffers, headers, etc...)
716  *          2  :  header = On input, pointer to header to modify.
717  *                On output, pointer to the modified header, or NULL
718  *                to remove the header.  This function frees the
719  *                original string if necessary.
720  *
721  * Returns     :  JB_ERR_OK on success, or
722  *                JB_ERR_MEMORY on out-of-memory error.
723  *
724  *********************************************************************/
725 jb_err crumble(struct client_state *csp, char **header)
726 {
727    log_error(LOG_LEVEL_HEADER, "crunch!");
728    freez(*header);
729    return JB_ERR_OK;
730 }
731
732
733 /*********************************************************************
734  *
735  * Function    :  server_content_type
736  *
737  * Description :  Set the content-type for filterable types (text/.*,
738  *                javascript and image/gif) unless filtering has been
739  *                forbidden (CT_TABOO) while parsing earlier headers.
740  *
741  * Parameters  :
742  *          1  :  csp = Current client state (buffers, headers, etc...)
743  *          2  :  header = On input, pointer to header to modify.
744  *                On output, pointer to the modified header, or NULL
745  *                to remove the header.  This function frees the
746  *                original string if necessary.
747  *
748  * Returns     :  JB_ERR_OK on success, or
749  *                JB_ERR_MEMORY on out-of-memory error.
750  *
751  *********************************************************************/
752 jb_err server_content_type(struct client_state *csp, char **header)
753 {
754    if (csp->content_type != CT_TABOO)
755    {
756       if (strstr(*header, " text/")
757        || strstr(*header, "application/x-javascript"))
758          csp->content_type = CT_TEXT;
759       else if (strstr(*header, " image/gif"))
760          csp->content_type = CT_GIF;
761       else
762          csp->content_type = 0;
763    }
764
765    return JB_ERR_OK;
766 }
767
768
769 /*********************************************************************
770  *
771  * Function    :  server_transfer_coding
772  *
773  * Description :  - Prohibit filtering (CT_TABOO) if transfer coding compresses
774  *                - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
775  *                - Change from "chunked" to "identity" if body was chunked
776  *                  but has been de-chunked for filtering.
777  *
778  * Parameters  :
779  *          1  :  csp = Current client state (buffers, headers, etc...)
780  *          2  :  header = On input, pointer to header to modify.
781  *                On output, pointer to the modified header, or NULL
782  *                to remove the header.  This function frees the
783  *                original string if necessary.
784  *
785  * Returns     :  JB_ERR_OK on success, or
786  *                JB_ERR_MEMORY on out-of-memory error.
787  *
788  *********************************************************************/
789 jb_err server_transfer_coding(struct client_state *csp, char **header)
790 {
791    /*
792     * Turn off pcrs and gif filtering if body compressed
793     */
794    if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
795    {
796       csp->content_type = CT_TABOO;
797    }
798
799    /*
800     * Raise flag if body chunked
801     */
802    if (strstr(*header, "chunked"))
803    {
804       csp->flags |= CSP_FLAG_CHUNKED;
805
806       /*
807        * If the body was modified, it has been
808        * de-chunked first, so adjust the header:
809        */
810       if (csp->flags & CSP_FLAG_MODIFIED)
811       {
812          freez(*header);
813          *header = strdup("Transfer-Encoding: identity");
814          return (header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
815       }
816    }
817
818    return JB_ERR_OK;
819 }
820
821
822 /*********************************************************************
823  *
824  * Function    :  server_content_encoding
825  *
826  * Description :  Prohibit filtering (CT_TABOO) if content encoding compresses
827  *
828  * Parameters  :
829  *          1  :  csp = Current client state (buffers, headers, etc...)
830  *          2  :  header = On input, pointer to header to modify.
831  *                On output, pointer to the modified header, or NULL
832  *                to remove the header.  This function frees the
833  *                original string if necessary.
834  *
835  * Returns     :  JB_ERR_OK on success, or
836  *                JB_ERR_MEMORY on out-of-memory error.
837  *
838  *********************************************************************/
839 jb_err server_content_encoding(struct client_state *csp, char **header)
840 {
841    /*
842     * Turn off pcrs and gif filtering if body compressed
843     */
844    if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
845    {
846       csp->content_type = CT_TABOO;
847    }
848
849    return JB_ERR_OK;
850
851 }
852
853
854 /*********************************************************************
855  *
856  * Function    :  server_content_length
857  *
858  * Description :  Adjust Content-Length header if we modified
859  *                the body.
860  *
861  * Parameters  :
862  *          1  :  csp = Current client state (buffers, headers, etc...)
863  *          2  :  header = On input, pointer to header to modify.
864  *                On output, pointer to the modified header, or NULL
865  *                to remove the header.  This function frees the
866  *                original string if necessary.
867  *
868  * Returns     :  JB_ERR_OK on success, or
869  *                JB_ERR_MEMORY on out-of-memory error.
870  *
871  *********************************************************************/
872 jb_err server_content_length(struct client_state *csp, char **header)
873 {
874    if (csp->content_length != 0) /* Content length has been modified */
875    {
876       freez(*header);
877       *header = (char *) zalloc(100);
878       if (*header == NULL)
879       {
880          return JB_ERR_MEMORY;
881       }
882
883       sprintf(*header, "Content-Length: %d", (int) csp->content_length);
884
885       log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
886    }
887
888    return JB_ERR_OK;
889 }
890
891
892 /*********************************************************************
893  *
894  * Function    :  server_content_md5
895  *
896  * Description :  Crumble any Content-MD5 headers if the document was
897  *                modified. FIXME: Should we re-compute instead?
898  *
899  * Parameters  :
900  *          1  :  csp = Current client state (buffers, headers, etc...)
901  *          2  :  header = On input, pointer to header to modify.
902  *                On output, pointer to the modified header, or NULL
903  *                to remove the header.  This function frees the
904  *                original string if necessary.
905  *
906  * Returns     :  JB_ERR_OK on success, or
907  *                JB_ERR_MEMORY on out-of-memory error.
908  *
909  *********************************************************************/
910 jb_err server_content_md5(struct client_state *csp, char **header)
911 {
912    if (csp->flags & CSP_FLAG_MODIFIED)
913    {
914       log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
915       freez(*header);
916    }
917
918    return JB_ERR_OK;
919 }
920
921
922 /*********************************************************************
923  *
924  * Function    :  client_accept_encoding
925  *
926  * Description :  Rewrite the client's Accept-Encoding header so that
927  *                if doesn't allow compression, if the action applies.
928  *                Note: For HTTP/1.0 the absence of the header is enough.
929  *
930  * Parameters  :
931  *          1  :  csp = Current client state (buffers, headers, etc...)
932  *          2  :  header = On input, pointer to header to modify.
933  *                On output, pointer to the modified header, or NULL
934  *                to remove the header.  This function frees the
935  *                original string if necessary.
936  *
937  * Returns     :  JB_ERR_OK on success, or
938  *                JB_ERR_MEMORY on out-of-memory error.
939  *
940  *********************************************************************/
941 jb_err client_accept_encoding(struct client_state *csp, char **header)
942 {
943    if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
944    {
945       log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
946
947       freez(*header);
948       if (!strcmpic(csp->http->ver, "HTTP/1.1"))
949       {
950          *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
951          if (*header == NULL)
952          {
953             return JB_ERR_MEMORY;
954          }
955       }
956    }
957
958    return JB_ERR_OK;
959 }
960
961
962 /*********************************************************************
963  *
964  * Function    :  client_te
965  *
966  * Description :  Rewrite the client's TE header so that
967  *                if doesn't allow compression, if the action applies.
968  *
969  * Parameters  :
970  *          1  :  csp = Current client state (buffers, headers, etc...)
971  *          2  :  header = On input, pointer to header to modify.
972  *                On output, pointer to the modified header, or NULL
973  *                to remove the header.  This function frees the
974  *                original string if necessary.
975  *
976  * Returns     :  JB_ERR_OK on success, or
977  *                JB_ERR_MEMORY on out-of-memory error.
978  *
979  *********************************************************************/
980 jb_err client_te(struct client_state *csp, char **header)
981 {
982    if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
983    {
984       freez(*header);
985       log_error(LOG_LEVEL_HEADER, "Supressed offer to compress transfer");
986    }
987
988    return JB_ERR_OK;
989 }
990
991 /*********************************************************************
992  *
993  * Function    :  client_referrer
994  *
995  * Description :  Handle the "referer" config setting properly.
996  *                Called from `sed'.
997  *
998  * Parameters  :
999  *          1  :  csp = Current client state (buffers, headers, etc...)
1000  *          2  :  header = On input, pointer to header to modify.
1001  *                On output, pointer to the modified header, or NULL
1002  *                to remove the header.  This function frees the
1003  *                original string if necessary.
1004  *
1005  * Returns     :  JB_ERR_OK on success, or
1006  *                JB_ERR_MEMORY on out-of-memory error.
1007  *
1008  *********************************************************************/
1009 jb_err client_referrer(struct client_state *csp, char **header)
1010 {
1011    const char * newval;
1012
1013 #ifdef FEATURE_FORCE_LOAD
1014    /* Since the referrer can include the prefix even
1015     * even if the request itself is non-forced, we must
1016     * clean it unconditionally
1017     */
1018    strclean(*header, FORCE_PREFIX);
1019 #endif /* def FEATURE_FORCE_LOAD */
1020
1021    /*
1022     * Are we sending referer?
1023     */
1024    if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1025    {
1026       return JB_ERR_OK;
1027    }
1028
1029    freez(*header);
1030
1031    newval = csp->action->string[ACTION_STRING_REFERER];
1032
1033    if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1034    {
1035       /*
1036        * Blocking referer
1037        */
1038       log_error(LOG_LEVEL_HEADER, "crunch!");
1039       return JB_ERR_OK;
1040    }
1041    else if (0 == strncmpic(newval, "http://", 7))
1042    {
1043       /*
1044        * We have a specific (fixed) referer we want to send.
1045        */
1046       log_error(LOG_LEVEL_HEADER, "modified");
1047
1048       *header = strdup("Referer: ");
1049       string_append(header, newval);
1050
1051       return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1052    }
1053    else
1054    {
1055       /*
1056        * Forge a referer as http://[hostname:port of REQUEST]/
1057        * to fool stupid checks for in-site links
1058        */
1059       if (0 != strcmpic(newval, "forge"))
1060       {
1061          /*
1062           * Invalid choice - but forge is probably the best default.
1063           */
1064          log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1065       }
1066
1067       log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1068       *header = strdup("Referer: http://");
1069       string_append(header, csp->http->hostport);
1070       string_append(header, "/");
1071
1072       return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1073    }
1074 }
1075
1076
1077 /*********************************************************************
1078  *
1079  * Function    :  client_uagent
1080  *
1081  * Description :  Handle the "user-agent" config setting properly
1082  *                and remember its original value to enable browser
1083  *                bug workarounds. Called from `sed'.
1084  *
1085  * Parameters  :
1086  *          1  :  csp = Current client state (buffers, headers, etc...)
1087  *          2  :  header = On input, pointer to header to modify.
1088  *                On output, pointer to the modified header, or NULL
1089  *                to remove the header.  This function frees the
1090  *                original string if necessary.
1091  *
1092  * Returns     :  JB_ERR_OK on success, or
1093  *                JB_ERR_MEMORY on out-of-memory error.
1094  *
1095  *********************************************************************/
1096 jb_err client_uagent(struct client_state *csp, char **header)
1097 {
1098    const char * newval;
1099
1100    if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1101    {
1102       return JB_ERR_OK;
1103    }
1104
1105    newval = csp->action->string[ACTION_STRING_USER_AGENT];
1106    if (newval == NULL)
1107    {
1108       return JB_ERR_OK;
1109    }
1110
1111    log_error(LOG_LEVEL_HEADER, "modified");
1112
1113    freez(*header);
1114    *header = strdup("User-Agent: ");
1115    string_append(header, newval);
1116
1117    return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1118 }
1119
1120
1121 /*********************************************************************
1122  *
1123  * Function    :  client_ua
1124  *
1125  * Description :  Handle "ua-" headers properly.  Called from `sed'.
1126  *
1127  * Parameters  :
1128  *          1  :  csp = Current client state (buffers, headers, etc...)
1129  *          2  :  header = On input, pointer to header to modify.
1130  *                On output, pointer to the modified header, or NULL
1131  *                to remove the header.  This function frees the
1132  *                original string if necessary.
1133  *
1134  * Returns     :  JB_ERR_OK on success, or
1135  *                JB_ERR_MEMORY on out-of-memory error.
1136  *
1137  *********************************************************************/
1138 jb_err client_ua(struct client_state *csp, char **header)
1139 {
1140    if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
1141    {
1142       log_error(LOG_LEVEL_HEADER, "crunch!");
1143       freez(*header);
1144    }
1145
1146    return JB_ERR_OK;
1147 }
1148
1149
1150 /*********************************************************************
1151  *
1152  * Function    :  client_from
1153  *
1154  * Description :  Handle the "from" config setting properly.
1155  *                Called from `sed'.
1156  *
1157  * Parameters  :
1158  *          1  :  csp = Current client state (buffers, headers, etc...)
1159  *          2  :  header = On input, pointer to header to modify.
1160  *                On output, pointer to the modified header, or NULL
1161  *                to remove the header.  This function frees the
1162  *                original string if necessary.
1163  *
1164  * Returns     :  JB_ERR_OK on success, or
1165  *                JB_ERR_MEMORY on out-of-memory error.
1166  *
1167  *********************************************************************/
1168 jb_err client_from(struct client_state *csp, char **header)
1169 {
1170    const char * newval;
1171
1172    if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1173    {
1174       return JB_ERR_OK;
1175    }
1176
1177    freez(*header);
1178
1179    newval = csp->action->string[ACTION_STRING_FROM];
1180
1181    /*
1182     * Are we blocking the e-mail address?
1183     */
1184    if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1185    {
1186       log_error(LOG_LEVEL_HEADER, "crunch!");
1187       return JB_ERR_OK;
1188    }
1189
1190    log_error(LOG_LEVEL_HEADER, " modified");
1191
1192    *header = strdup("From: ");
1193    string_append(header, newval);
1194
1195    return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1196 }
1197
1198
1199 /*********************************************************************
1200  *
1201  * Function    :  client_send_cookie
1202  *
1203  * Description :  Handle the "cookie" header properly.  Called from `sed'.
1204  *                If cookie is accepted, add it to the cookie_list,
1205  *                else we crunch it.  Mmmmmmmmmmm ... cookie ......
1206  *
1207  * Parameters  :
1208  *          1  :  csp = Current client state (buffers, headers, etc...)
1209  *          2  :  header = On input, pointer to header to modify.
1210  *                On output, pointer to the modified header, or NULL
1211  *                to remove the header.  This function frees the
1212  *                original string if necessary.
1213  *
1214  * Returns     :  JB_ERR_OK on success, or
1215  *                JB_ERR_MEMORY on out-of-memory error.
1216  *
1217  *********************************************************************/
1218 jb_err client_send_cookie(struct client_state *csp, char **header)
1219 {
1220    jb_err result = JB_ERR_OK;
1221
1222    if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1223    {
1224       /* strlen("cookie: ") == 8 */
1225       result = enlist(csp->cookie_list, *header + 8);
1226    }
1227    else
1228    {
1229       log_error(LOG_LEVEL_HEADER, " crunch!");
1230    }
1231
1232    /*
1233     * Always remove the cookie here.  The cookie header
1234     * will be sent at the end of the header.
1235     */
1236    freez(*header);
1237
1238    return result;
1239 }
1240
1241
1242 /*********************************************************************
1243  *
1244  * Function    :  client_x_forwarded
1245  *
1246  * Description :  Handle the "x-forwarded-for" config setting properly,
1247  *                also used in the add_client_headers list.  Called from `sed'.
1248  *
1249  * Parameters  :
1250  *          1  :  csp = Current client state (buffers, headers, etc...)
1251  *          2  :  header = On input, pointer to header to modify.
1252  *                On output, pointer to the modified header, or NULL
1253  *                to remove the header.  This function frees the
1254  *                original string if necessary.
1255  *
1256  * Returns     :  JB_ERR_OK on success, or
1257  *                JB_ERR_MEMORY on out-of-memory error.
1258  *
1259  *********************************************************************/
1260 jb_err client_x_forwarded(struct client_state *csp, char **header)
1261 {
1262    if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1263    {
1264       /* Save it so we can re-add it later */
1265       freez(csp->x_forwarded);
1266       csp->x_forwarded = *header;
1267
1268       /*
1269        * Always set *header = NULL, since this information
1270        * will be sent at the end of the header.
1271        */
1272       *header = NULL;
1273    }
1274    else
1275    {
1276       freez(*header);
1277       log_error(LOG_LEVEL_HEADER, " crunch!");
1278    }
1279
1280    return JB_ERR_OK;
1281 }
1282
1283 /* the following functions add headers directly to the header list */
1284
1285 /*********************************************************************
1286  *
1287  * Function    :  client_host_adder
1288  *
1289  * Description :  (re)adds the host header. Called from `sed'.
1290  *
1291  * Parameters  :
1292  *          1  :  csp = Current client state (buffers, headers, etc...)
1293  *
1294  * Returns     :  JB_ERR_OK on success, or
1295  *                JB_ERR_MEMORY on out-of-memory error.
1296  *
1297  *********************************************************************/
1298 jb_err client_host_adder(struct client_state *csp)
1299 {
1300    char *p;
1301    char *pos;
1302    jb_err err;
1303
1304    if ( !csp->http->hostport || !*(csp->http->hostport))
1305    {
1306       return JB_ERR_OK;
1307    }
1308
1309    p = strdup("Host: ");
1310    /*
1311    ** remove 'user:pass@' from 'proto://user:pass@host'
1312    */
1313    if ( (pos = strchr( csp->http->hostport, '@')) != NULL )
1314    {
1315        string_append(&p, pos+1);
1316    }
1317    else
1318    {
1319       string_append(&p, csp->http->hostport);
1320    }
1321
1322    if (p == NULL)
1323    {
1324       return JB_ERR_MEMORY;
1325    }
1326
1327    log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1328
1329    err = enlist(csp->headers, p);
1330
1331    freez(p);
1332
1333    return err;
1334 }
1335
1336
1337 /*********************************************************************
1338  *
1339  * Function    :  client_cookie_adder
1340  *
1341  * Description :  Used in the add_client_headers list.  Called from `sed'.
1342  *
1343  * Parameters  :
1344  *          1  :  csp = Current client state (buffers, headers, etc...)
1345  *
1346  * Returns     :  JB_ERR_OK on success, or
1347  *                JB_ERR_MEMORY on out-of-memory error.
1348  *
1349  *********************************************************************/
1350 jb_err client_cookie_adder(struct client_state *csp)
1351 {
1352    struct list_entry *lst;
1353    char *tmp;
1354    struct list_entry *list1 = csp->cookie_list->first;
1355    struct list_entry *list2 = csp->action->multi[ACTION_MULTI_WAFER]->first;
1356    int first_cookie = 1;
1357    jb_err err;
1358
1359    if ((list1 == NULL) && (list2 == NULL))
1360    {
1361       /* Nothing to do */
1362       return JB_ERR_OK;
1363    }
1364
1365    tmp = strdup("Cookie: ");
1366
1367    for (lst = list1; lst ; lst = lst->next)
1368    {
1369       if (first_cookie)
1370       {
1371          first_cookie = 0;
1372       }
1373       else
1374       {
1375          string_append(&tmp, "; ");
1376       }
1377       string_append(&tmp, lst->str);
1378    }
1379
1380    for (lst = list2;  lst ; lst = lst->next)
1381    {
1382       if (first_cookie)
1383       {
1384          first_cookie = 0;
1385       }
1386       else
1387       {
1388          string_append(&tmp, "; ");
1389       }
1390       string_join(&tmp, cookie_encode(lst->str));
1391    }
1392
1393    if (tmp == NULL)
1394    {
1395       return JB_ERR_MEMORY;
1396    }
1397
1398    log_error(LOG_LEVEL_HEADER, "addh: %s", tmp);
1399    err = enlist(csp->headers, tmp);
1400    free(tmp);
1401    return err;
1402 }
1403
1404
1405 /*********************************************************************
1406  *
1407  * Function    :  client_accept_encoding_adder
1408  *
1409  * Description :  Add an Accept-Encoding header to the client's request
1410  *                that disables compression if the action applies, and
1411  *                the header is not already there. Called from `sed'.
1412  *                Note: For HTTP/1.0, the absence of the header is enough.
1413  *
1414  * Parameters  :
1415  *          1  :  csp = Current client state (buffers, headers, etc...)
1416  *
1417  * Returns     :  JB_ERR_OK on success, or
1418  *                JB_ERR_MEMORY on out-of-memory error.
1419  *
1420  *********************************************************************/
1421 jb_err client_accept_encoding_adder(struct client_state *csp)
1422 {
1423    if (   ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1424        && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1425    {
1426       return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1427    }
1428
1429    return JB_ERR_OK;
1430 }
1431
1432
1433 /*********************************************************************
1434  *
1435  * Function    :  client_xtra_adder
1436  *
1437  * Description :  Used in the add_client_headers list.  Called from `sed'.
1438  *
1439  * Parameters  :
1440  *          1  :  csp = Current client state (buffers, headers, etc...)
1441  *
1442  * Returns     :  JB_ERR_OK on success, or
1443  *                JB_ERR_MEMORY on out-of-memory error.
1444  *
1445  *********************************************************************/
1446 jb_err client_xtra_adder(struct client_state *csp)
1447 {
1448    struct list_entry *lst;
1449    jb_err err;
1450
1451    for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1452         lst ; lst = lst->next)
1453    {
1454       log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1455       err = enlist(csp->headers, lst->str);
1456       if (err)
1457       {
1458          return err;
1459       }
1460
1461    }
1462
1463    return JB_ERR_OK;
1464 }
1465
1466
1467 /*********************************************************************
1468  *
1469  * Function    :  client_x_forwarded_adder
1470  *
1471  * Description :  Used in the add_client_headers list.  Called from `sed'.
1472  *
1473  * Parameters  :
1474  *          1  :  csp = Current client state (buffers, headers, etc...)
1475  *
1476  * Returns     :  JB_ERR_OK on success, or
1477  *                JB_ERR_MEMORY on out-of-memory error.
1478  *
1479  *********************************************************************/
1480 jb_err client_x_forwarded_adder(struct client_state *csp)
1481 {
1482    char *p = NULL;
1483    jb_err err;
1484
1485    if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1486    {
1487       return JB_ERR_OK;
1488    }
1489
1490    if (csp->x_forwarded)
1491    {
1492       p = strdup(csp->x_forwarded);
1493       string_append(&p, ", ");
1494    }
1495    else
1496    {
1497       p = strdup("X-Forwarded-For: ");
1498    }
1499    string_append(&p, csp->ip_addr_str);
1500
1501    if (p == NULL)
1502    {
1503       return JB_ERR_MEMORY;
1504    }
1505
1506    log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1507    err = enlist(csp->headers, p);
1508    free(p);
1509
1510    return err;
1511 }
1512
1513
1514 /*********************************************************************
1515  *
1516  * Function    :  connection_close_adder
1517  *
1518  * Description :  Adds a "Connection: close" header to csp->headers
1519  *                as a temporary fix for the needed but missing HTTP/1.1
1520  *                support. Called from `sed'.
1521  *                FIXME: This whole function shouldn't be neccessary!
1522  *
1523  * Parameters  :
1524  *          1  :  csp = Current client state (buffers, headers, etc...)
1525  *
1526  * Returns     :  JB_ERR_OK on success, or
1527  *                JB_ERR_MEMORY on out-of-memory error.
1528  *
1529  *********************************************************************/
1530 jb_err connection_close_adder(struct client_state *csp)
1531 {
1532    return enlist(csp->headers, "Connection: close");
1533 }
1534
1535
1536 /*********************************************************************
1537  *
1538  * Function    :  server_http
1539  *
1540  * Description :  - Save the HTTP Status into csp->http->status
1541  *                - Set CT_TABOO to prevent filtering if the answer
1542  *                  is a partial range (HTTP status 206)
1543  *                - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1544  *                  action applies.
1545  *
1546  * Parameters  :
1547  *          1  :  csp = Current client state (buffers, headers, etc...)
1548  *          2  :  header = On input, pointer to header to modify.
1549  *                On output, pointer to the modified header, or NULL
1550  *                to remove the header.  This function frees the
1551  *                original string if necessary.
1552  *
1553  * Returns     :  JB_ERR_OK on success, or
1554  *                JB_ERR_MEMORY on out-of-memory error.
1555  *
1556  *********************************************************************/
1557 jb_err server_http(struct client_state *csp, char **header)
1558 {
1559    sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
1560    if (csp->http->status == 206)
1561    {
1562       csp->content_type = CT_TABOO;
1563    }
1564
1565    if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1566    {
1567       (*header)[7] = '0';
1568       log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1569    }
1570
1571    return JB_ERR_OK;
1572 }
1573
1574
1575 /*********************************************************************
1576  *
1577  * Function    :  server_set_cookie
1578  *
1579  * Description :  Handle the server "cookie" header properly.
1580  *                Log cookie to the jar file.  Then "crunch" it,
1581  *                or accept it.  Called from `sed'.
1582  *
1583  * Parameters  :
1584  *          1  :  csp = Current client state (buffers, headers, etc...)
1585  *          2  :  header = On input, pointer to header to modify.
1586  *                On output, pointer to the modified header, or NULL
1587  *                to remove the header.  This function frees the
1588  *                original string if necessary.
1589  *
1590  * Returns     :  JB_ERR_OK on success, or
1591  *                JB_ERR_MEMORY on out-of-memory error.
1592  *
1593  *********************************************************************/
1594 jb_err server_set_cookie(struct client_state *csp, char **header)
1595 {
1596 #ifdef FEATURE_COOKIE_JAR
1597    if (csp->config->jar)
1598    {
1599       /*
1600        * Write timestamp into outbuf.
1601        *
1602        * Complex because not all OSs have tm_gmtoff or
1603        * the %z field in strftime()
1604        */
1605       char tempbuf[ BUFFER_SIZE ];
1606       time_t now; 
1607       struct tm tm_now; 
1608       time (&now); 
1609 #ifdef HAVE_LOCALTIME_R
1610       tm_now = *localtime_r(&now, &tm_now);
1611 #else
1612       tm_now = *localtime (&now); 
1613 #endif
1614       strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now); 
1615
1616       /* strlen("set-cookie: ") = 12 */
1617       fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, *header + 12);
1618    }
1619 #endif /* def FEATURE_COOKIE_JAR */
1620
1621    if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1622    {
1623       return crumble(csp, header);
1624    }
1625    else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
1626    {
1627       /* Flag whether or not to log a message */
1628       int changed = 0;
1629
1630       /* A variable to store the tag we're working on */
1631       char * cur_tag;
1632
1633       /* Skip "Set-Cookie:" (11 characters) in header */
1634       cur_tag = *header + 11;
1635
1636       /* skip whitespace between "Set-Cookie:" and value */
1637       while (*cur_tag && ijb_isspace(*cur_tag))
1638       {
1639          cur_tag++;
1640       }
1641
1642       /* Loop through each tag in the cookie */
1643       while (*cur_tag)
1644       {
1645          /* Find next tag */
1646          char * next_tag = strchr(cur_tag, ';');
1647          if (next_tag != NULL)
1648          {
1649             /* Skip the ';' character itself */
1650             next_tag++;
1651
1652             /* skip whitespace ";" and start of tag */
1653             while (*next_tag && ijb_isspace(*next_tag))
1654             {
1655                next_tag++;
1656             }
1657          }
1658          else
1659          {
1660             /* "Next tag" is the end of the string */
1661             next_tag = cur_tag + strlen(cur_tag);
1662          }
1663
1664          /* Is this the "Expires" tag? */
1665          if (strncmpic(cur_tag, "expires=", 8) == 0)
1666          {
1667             /* Delete the tag by copying the rest of the string over it.
1668              * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
1669              * since the behaviour of strcpy is undefined for overlapping
1670              * strings.)
1671              */
1672             memmove(cur_tag, next_tag, strlen(next_tag) + 1);
1673
1674             /* That changed the header, need to issue a log message */
1675             changed = 1;
1676
1677             /* Note that the next tag has now been moved to *cur_tag,
1678              * so we do not need to update the cur_tag pointer.
1679              */
1680          }
1681          else
1682          {
1683             /* Move on to next cookie tag */
1684             cur_tag = next_tag;
1685          }
1686       }
1687
1688       if (changed)
1689       {
1690          log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
1691       }
1692    }
1693
1694    return JB_ERR_OK;
1695 }
1696
1697
1698 #ifdef FEATURE_FORCE_LOAD
1699 /*********************************************************************
1700  *
1701  * Function    :  strclean
1702  *
1703  * Description :  In-Situ-Eliminate all occurances of substring in
1704  *                string
1705  *
1706  * Parameters  :
1707  *          1  :  string = string to clean
1708  *          2  :  substring = substring to eliminate
1709  *
1710  * Returns     :  Number of eliminations
1711  *
1712  *********************************************************************/
1713 int strclean(const char *string, const char *substring)
1714 {
1715    int hits = 0, len = strlen(substring);
1716    char *pos, *p;
1717
1718    while((pos = strstr(string, substring)))
1719    {
1720       p = pos + len;
1721       do
1722       {
1723          *(p - len) = *p;
1724       }
1725       while (*p++ != '\0');
1726
1727       hits++;
1728    }
1729
1730    return(hits);
1731 }
1732 #endif /* def FEATURE_FORCE_LOAD */
1733
1734
1735 /*
1736   Local Variables:
1737   tab-width: 3
1738   end:
1739 */