Workaround for firefox hanging on blocked javascript pages
[privoxy.git] / filters.c
1 const char filters_rcs[] = "$Id: filters.c,v 1.125 2009/10/25 15:23:40 ler762 Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/filters.c,v $
5  *
6  * Purpose     :  Declares functions to parse/crunch headers and pages.
7  *                Functions declared include:
8  *                   `acl_addr', `add_stats', `block_acl', `block_imageurl',
9  *                   `block_url', `url_actions', `domain_split',
10  *                   `filter_popups', `forward_url', 'redirect_url',
11  *                   `ij_untrusted_url', `intercept_url', `pcrs_filter_respose',
12  *                   `ijb_send_banner', `trust_url', `gif_deanimate_response',
13  *                   `execute_single_pcrs_command', `rewrite_url',
14  *                   `get_last_url'
15  *
16  * Copyright   :  Written by and Copyright (C) 2001, 2004-2009 the
17  *                Privoxy team. http://www.privoxy.org/
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  *********************************************************************/
42
43
44 #include "config.h"
45
46 #include <stdio.h>
47 #include <sys/types.h>
48 #include <stdlib.h>
49 #include <ctype.h>
50 #include <string.h>
51 #include <assert.h>
52
53 #ifdef HAVE_RFC2553
54 #include <netdb.h>
55 #include <sys/socket.h>
56 #endif /* def HAVE_RFC2553 */
57
58 #ifndef _WIN32
59 #ifndef __OS2__
60 #include <unistd.h>
61 #endif /* ndef __OS2__ */
62 #include <netinet/in.h>
63 #else
64 #include <winsock2.h>
65 #endif /* ndef _WIN32 */
66
67 #ifdef __OS2__
68 #include <utils.h>
69 #endif /* def __OS2__ */
70
71 #include "project.h"
72 #include "filters.h"
73 #include "encode.h"
74 #include "parsers.h"
75 #include "ssplit.h"
76 #include "errlog.h"
77 #include "jbsockets.h"
78 #include "miscutil.h"
79 #include "actions.h"
80 #include "cgi.h"
81 #include "list.h"
82 #include "deanimate.h"
83 #include "urlmatch.h"
84 #include "loaders.h"
85
86 #ifdef _WIN32
87 #include "win32.h"
88 #endif
89
90 const char filters_h_rcs[] = FILTERS_H_VERSION;
91
92 /* Fix a problem with Solaris.  There should be no effect on other
93  * platforms.
94  * Solaris's isspace() is a macro which uses it's argument directly
95  * as an array index.  Therefore we need to make sure that high-bit
96  * characters generate +ve values, and ideally we also want to make
97  * the argument match the declared parameter type of "int".
98  */
99 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
100
101 static jb_err remove_chunked_transfer_coding(char *buffer, size_t *size);
102 static jb_err prepare_for_filtering(struct client_state *csp);
103
104 #ifdef FEATURE_ACL
105 #ifdef HAVE_RFC2553
106 /*********************************************************************
107  *
108  * Function    :  sockaddr_storage_to_ip
109  *
110  * Description :  Access internal structure of sockaddr_storage
111  *
112  * Parameters  :
113  *          1  :  addr = socket address
114  *          2  :  ip   = IP address as array of octets in network order
115  *                       (it points into addr)
116  *          3  :  len  = length of IP address in octets
117  *          4  :  port = port number in network order;
118  *
119  * Returns     :  0 = no errror; -1 otherwise.
120  *
121  *********************************************************************/
122 static int sockaddr_storage_to_ip(const struct sockaddr_storage *addr,
123                                   uint8_t **ip, unsigned int *len,
124                                   in_port_t **port)
125 {
126    if (NULL == addr)
127    {
128       return(-1);
129    }
130
131    switch (addr->ss_family)
132    {
133       case AF_INET:
134          if (NULL != len)
135          {
136             *len = 4;
137          }
138          if (NULL != ip)
139          {
140             *ip = (uint8_t *)
141                &(((struct sockaddr_in *)addr)->sin_addr.s_addr);
142          }
143          if (NULL != port)
144          {
145             *port = &((struct sockaddr_in *)addr)->sin_port;
146          }
147          break;
148
149       case AF_INET6:
150          if (NULL != len)
151          {
152             *len = 16;
153          }
154          if (NULL != ip)
155          {
156             *ip = ((struct sockaddr_in6 *)addr)->sin6_addr.s6_addr;
157          }
158          if (NULL != port)
159          {
160             *port = &((struct sockaddr_in6 *)addr)->sin6_port;
161          }
162          break;
163
164       default:
165          /* Unsupported address family */
166          return(-1);
167    }
168
169    return(0);
170 }
171
172
173 /*********************************************************************
174  *
175  * Function    :  match_sockaddr
176  *
177  * Description :  Check whether address matches network (IP address and port)
178  *
179  * Parameters  :
180  *          1  :  network = socket address of subnework
181  *          2  :  netmask = network mask as socket address
182  *          3  :  address = checked socket address against given network
183  *
184  * Returns     :  0 = doesn't match; 1 = does match
185  *
186  *********************************************************************/
187 static int match_sockaddr(const struct sockaddr_storage *network,
188                           const struct sockaddr_storage *netmask,
189                           const struct sockaddr_storage *address)
190 {
191    uint8_t *network_addr, *netmask_addr, *address_addr;
192    unsigned int addr_len;
193    in_port_t *network_port, *netmask_port, *address_port;
194    int i;
195
196    if (network->ss_family != netmask->ss_family)
197    {
198       /* This should never happen */
199       log_error(LOG_LEVEL_ERROR,
200          "Internal error at %s:%llu: network and netmask differ in family",
201          __FILE__, __LINE__);
202       return 0;
203    }
204
205    sockaddr_storage_to_ip(network, &network_addr, &addr_len, &network_port);
206    sockaddr_storage_to_ip(netmask, &netmask_addr, NULL, &netmask_port);
207    sockaddr_storage_to_ip(address, &address_addr, NULL, &address_port);
208
209    /* Check for family */
210    if ((network->ss_family == AF_INET) && (address->ss_family == AF_INET6)
211       && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)address_addr))
212    {
213       /* Map AF_INET6 V4MAPPED address into AF_INET */
214       address_addr += 12;
215       addr_len = 4;
216    }
217    else if ((network->ss_family == AF_INET6) && (address->ss_family == AF_INET)
218       && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)network_addr))
219    {
220       /* Map AF_INET6 V4MAPPED network into AF_INET */
221       network_addr += 12;
222       netmask_addr += 12;
223       addr_len = 4;
224    }
225    else if (network->ss_family != address->ss_family)
226    {
227       return 0;
228    }
229
230    /* XXX: Port check is signaled in netmask */
231    if (*netmask_port && *network_port != *address_port)
232    {
233       return 0;
234    }
235
236    /* TODO: Optimize by checking by words insted of octets */
237    for (i = 0; (i < addr_len) && netmask_addr[i]; i++)
238    {
239       if ((network_addr[i] & netmask_addr[i]) !=
240           (address_addr[i] & netmask_addr[i]))
241       {
242          return 0;
243       }
244    }
245
246    return 1;
247 }
248 #endif /* def HAVE_RFC2553 */
249
250
251 /*********************************************************************
252  *
253  * Function    :  block_acl
254  *
255  * Description :  Block this request?
256  *                Decide yes or no based on ACL file.
257  *
258  * Parameters  :
259  *          1  :  dst = The proxy or gateway address this is going to.
260  *                      Or NULL to check all possible targets.
261  *          2  :  csp = Current client state (buffers, headers, etc...)
262  *                      Also includes the client IP address.
263  *
264  * Returns     : 0 = FALSE (don't block) and 1 = TRUE (do block)
265  *
266  *********************************************************************/
267 int block_acl(const struct access_control_addr *dst, const struct client_state *csp)
268 {
269    struct access_control_list *acl = csp->config->acl;
270
271    /* if not using an access control list, then permit the connection */
272    if (acl == NULL)
273    {
274       return(0);
275    }
276
277    /* search the list */
278    while (acl != NULL)
279    {
280       if (
281 #ifdef HAVE_RFC2553
282             match_sockaddr(&acl->src->addr, &acl->src->mask, &csp->tcp_addr)
283 #else
284             (csp->ip_addr_long & acl->src->mask) == acl->src->addr
285 #endif
286             )
287       {
288          if (dst == NULL)
289          {
290             /* Just want to check if they have any access */
291             if (acl->action == ACL_PERMIT)
292             {
293                return(0);
294             }
295          }
296          else if (
297 #ifdef HAVE_RFC2553
298                /*
299                 * XXX: An undefined acl->dst is full of zeros and should be
300                 * considered a wildcard address. sockaddr_storage_to_ip()
301                 * fails on such destinations because of unknown sa_familly
302                 * (glibc only?). However this test is not portable.
303                 *
304                 * So, we signal the acl->dst is wildcard in wildcard_dst.
305                 */
306                acl->wildcard_dst ||
307                   match_sockaddr(&acl->dst->addr, &acl->dst->mask, &dst->addr)
308 #else
309                ((dst->addr & acl->dst->mask) == acl->dst->addr)
310            && ((dst->port == acl->dst->port) || (acl->dst->port == 0))
311 #endif
312            )
313          {
314             if (acl->action == ACL_PERMIT)
315             {
316                return(0);
317             }
318             else
319             {
320                return(1);
321             }
322          }
323       }
324       acl = acl->next;
325    }
326
327    return(1);
328
329 }
330
331
332 /*********************************************************************
333  *
334  * Function    :  acl_addr
335  *
336  * Description :  Called from `load_config' to parse an ACL address.
337  *
338  * Parameters  :
339  *          1  :  aspec = String specifying ACL address.
340  *          2  :  aca = struct access_control_addr to fill in.
341  *
342  * Returns     :  0 => Ok, everything else is an error.
343  *
344  *********************************************************************/
345 int acl_addr(const char *aspec, struct access_control_addr *aca)
346 {
347    int i, masklength;
348 #ifdef HAVE_RFC2553
349    struct addrinfo hints, *result;
350    uint8_t *mask_data;
351    in_port_t *mask_port;
352    unsigned int addr_len;
353 #else
354    long port;
355 #endif /* def HAVE_RFC2553 */
356    char *p;
357    char *acl_spec = NULL;
358
359 #ifdef HAVE_RFC2553
360    /* XXX: Depend on ai_family */
361    masklength = 128;
362 #else
363    masklength = 32;
364    port       =  0;
365 #endif
366
367    /*
368     * Use a temporary acl spec copy so we can log
369     * the unmodified original in case of parse errors.
370     */
371    acl_spec = strdup(aspec);
372    if (acl_spec == NULL)
373    {
374       /* XXX: This will be logged as parse error. */
375       return(-1);
376    }
377
378    if ((p = strchr(acl_spec, '/')) != NULL)
379    {
380       *p++ = '\0';
381       if (ijb_isdigit(*p) == 0)
382       {
383          freez(acl_spec);
384          return(-1);
385       }
386       masklength = atoi(p);
387    }
388
389    if ((masklength < 0) ||
390 #ifdef HAVE_RFC2553
391          (masklength > 128)
392 #else
393          (masklength > 32)
394 #endif
395          )
396    {
397       freez(acl_spec);
398       return(-1);
399    }
400
401    if ((*acl_spec == '[') && (NULL != (p = strchr(acl_spec, ']'))))
402    {
403       *p = '\0';
404       memmove(acl_spec, acl_spec + 1, (size_t)(p - acl_spec));
405
406       if (*++p != ':')
407       {
408          p = NULL;
409       }
410    }
411    else
412    {
413       p = strchr(acl_spec, ':');
414    }
415
416 #ifdef HAVE_RFC2553
417    memset(&hints, 0, sizeof(struct addrinfo));
418    hints.ai_family = AF_UNSPEC;
419    hints.ai_socktype = SOCK_STREAM;
420
421    i = getaddrinfo(acl_spec, ((p) ? ++p : NULL), &hints, &result);
422
423    if (i != 0)
424    {
425       log_error(LOG_LEVEL_ERROR, "Can not resolve [%s]:%s: %s",
426          acl_spec, p, gai_strerror(i));
427       freez(acl_spec);
428       return(-1);
429    }
430    freez(acl_spec);
431
432    /* TODO: Allow multihomed hostnames */
433    memcpy(&(aca->addr), result->ai_addr, result->ai_addrlen);
434    freeaddrinfo(result);
435 #else
436    if (p != NULL)
437    {
438       char *endptr;
439
440       *p++ = '\0';
441       port = strtol(p, &endptr, 10);
442
443       if (port <= 0 || port > 65535 || *endptr != '\0')
444       {
445          freez(acl_spec);
446          return(-1);
447       }
448    }
449
450    aca->port = (unsigned long)port;
451
452    aca->addr = ntohl(resolve_hostname_to_ip(acl_spec));
453    freez(acl_spec);
454
455    if (aca->addr == INADDR_NONE)
456    {
457       /* XXX: This will be logged as parse error. */
458       return(-1);
459    }
460 #endif /* def HAVE_RFC2553 */
461
462    /* build the netmask */
463 #ifdef HAVE_RFC2553
464    /* Clip masklength according to current family. */
465    if ((aca->addr.ss_family == AF_INET) && (masklength > 32))
466    {
467       masklength = 32;
468    }
469
470    aca->mask.ss_family = aca->addr.ss_family;
471    if (sockaddr_storage_to_ip(&aca->mask, &mask_data, &addr_len, &mask_port))
472    {
473       return(-1);
474    }
475
476    if (p)
477    {
478       /* ACL contains a port number, check ports in the future. */
479       *mask_port = 1;
480    }
481
482    /*
483     * XXX: This could be optimized to operate on whole words instead
484     * of octets (128-bit CPU could do it in one iteration).
485     */
486    /*
487     * Octets after prefix can be ommitted because of
488     * previous initialization to zeros.
489     */
490    for (i = 0; (i < addr_len) && masklength; i++)
491    {
492       if (masklength >= 8)
493       {
494          mask_data[i] = 0xFF;
495          masklength -= 8;
496       }
497       else
498       {
499          /*
500           * XXX: This assumes MSB of octet is on the left side.
501           * This should be true for all architectures or solved
502           * by the link layer.
503           */
504          mask_data[i] = (uint8_t)~((1 << (8 - masklength)) - 1);
505          masklength = 0;
506       }
507    }
508
509 #else
510    aca->mask = 0;
511    for (i=1; i <= masklength ; i++)
512    {
513       aca->mask |= (1U << (32 - i));
514    }
515
516    /* now mask off the host portion of the ip address
517     * (i.e. save on the network portion of the address).
518     */
519    aca->addr = aca->addr & aca->mask;
520 #endif /* def HAVE_RFC2553 */
521
522    return(0);
523
524 }
525 #endif /* def FEATURE_ACL */
526
527
528 /*********************************************************************
529  *
530  * Function    :  connect_port_is_forbidden
531  *
532  * Description :  Check to see if CONNECT requests to the destination
533  *                port of this request are forbidden. The check is
534  *                independend of the actual request method.
535  *
536  * Parameters  :
537  *          1  :  csp = Current client state (buffers, headers, etc...)
538  *
539  * Returns     :  True if yes, false otherwise.
540  *
541  *********************************************************************/
542 int connect_port_is_forbidden(const struct client_state *csp)
543 {
544    return ((csp->action->flags & ACTION_LIMIT_CONNECT) &&
545      !match_portlist(csp->action->string[ACTION_STRING_LIMIT_CONNECT],
546         csp->http->port));
547 }
548
549
550 /*********************************************************************
551  *
552  * Function    :  block_url
553  *
554  * Description :  Called from `chat'.  Check to see if we need to block this.
555  *
556  * Parameters  :
557  *          1  :  csp = Current client state (buffers, headers, etc...)
558  *
559  * Returns     :  NULL => unblocked, else HTTP block response
560  *
561  *********************************************************************/
562 struct http_response *block_url(struct client_state *csp)
563 {
564    struct http_response *rsp;
565    const char *new_content_type = NULL;
566
567    /*
568     * If it's not blocked, don't block it ;-)
569     */
570    if ((csp->action->flags & ACTION_BLOCK) == 0)
571    {
572       return NULL;
573    }
574    if (csp->action->flags & ACTION_REDIRECT)
575    {
576       log_error(LOG_LEVEL_ERROR, "redirect{} overruled by block.");     
577    }
578    /*
579     * Else, prepare a response
580     */
581    if (NULL == (rsp = alloc_http_response()))
582    {
583       return cgi_error_memory();
584    }
585
586    /*
587     * If it's an image-url, send back an image or redirect
588     * as specified by the relevant +image action
589     */
590 #ifdef FEATURE_IMAGE_BLOCKING
591    if (((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
592         && is_imageurl(csp))
593    {
594       char *p;
595       /* determine HOW images should be blocked */
596       p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
597
598       if(csp->action->flags & ACTION_HANDLE_AS_EMPTY_DOCUMENT)
599       {
600          log_error(LOG_LEVEL_ERROR, "handle-as-empty-document overruled by handle-as-image.");
601       }
602 #if 1 /* Two alternative strategies, use this one for now: */
603
604       /* and handle accordingly: */
605       if ((p == NULL) || (0 == strcmpic(p, "pattern")))
606       {
607          rsp->status = strdup("403 Request blocked by Privoxy");
608          if (rsp->status == NULL)
609          {
610             free_http_response(rsp);
611             return cgi_error_memory();
612          }
613          rsp->body = bindup(image_pattern_data, image_pattern_length);
614          if (rsp->body == NULL)
615          {
616             free_http_response(rsp);
617             return cgi_error_memory();
618          }
619          rsp->content_length = image_pattern_length;
620
621          if (enlist_unique_header(rsp->headers, "Content-Type", BUILTIN_IMAGE_MIMETYPE))
622          {
623             free_http_response(rsp);
624             return cgi_error_memory();
625          }
626       }
627
628       else if (0 == strcmpic(p, "blank"))
629       {
630          rsp->status = strdup("403 Request blocked by Privoxy");
631          if (rsp->status == NULL)
632          {
633             free_http_response(rsp);
634             return cgi_error_memory();
635          }
636          rsp->body = bindup(image_blank_data, image_blank_length);
637          if (rsp->body == NULL)
638          {
639             free_http_response(rsp);
640             return cgi_error_memory();
641          }
642          rsp->content_length = image_blank_length;
643
644          if (enlist_unique_header(rsp->headers, "Content-Type", BUILTIN_IMAGE_MIMETYPE))
645          {
646             free_http_response(rsp);
647             return cgi_error_memory();
648          }
649       }
650
651       else
652       {
653          rsp->status = strdup("302 Local Redirect from Privoxy");
654          if (rsp->status == NULL)
655          {
656             free_http_response(rsp);
657             return cgi_error_memory();
658          }
659
660          if (enlist_unique_header(rsp->headers, "Location", p))
661          {
662             free_http_response(rsp);
663             return cgi_error_memory();
664          }
665       }
666
667 #else /* Following code is disabled for now */
668
669       /* and handle accordingly: */
670       if ((p == NULL) || (0 == strcmpic(p, "pattern")))
671       {
672          p = CGI_PREFIX "send-banner?type=pattern";
673       }
674       else if (0 == strcmpic(p, "blank"))
675       {
676          p = CGI_PREFIX "send-banner?type=blank";
677       }
678       rsp->status = strdup("302 Local Redirect from Privoxy");
679       if (rsp->status == NULL)
680       {
681          free_http_response(rsp);
682          return cgi_error_memory();
683       }
684
685       if (enlist_unique_header(rsp->headers, "Location", p))
686       {
687          free_http_response(rsp);
688          return cgi_error_memory();
689       }
690 #endif /* Preceeding code is disabled for now */
691    }
692    else
693 #endif /* def FEATURE_IMAGE_BLOCKING */
694    if(csp->action->flags & ACTION_HANDLE_AS_EMPTY_DOCUMENT)
695    {
696      /*
697       *  Send empty document.               
698       */
699       new_content_type = csp->action->string[ACTION_STRING_CONTENT_TYPE];
700
701       freez(rsp->body);
702       rsp->body = strdup(" ");
703       rsp->content_length = 1;
704
705       if (csp->config->feature_flags & RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK)
706       {
707          /*
708           * Workaround for firefox bug 492459
709           *   https://bugzilla.mozilla.org/show_bug.cgi?id=492459
710           * Return a 200 OK status for pages blocked with +handle-as-empty-document
711           * if the "handle-as-empty-doc-returns-ok" runtime config option is set.
712           */
713          rsp->status = strdup("200 Request blocked by Privoxy");
714       }
715       else
716       {
717          rsp->status = strdup("403 Request blocked by Privoxy");
718       }
719
720       if (rsp->status == NULL)
721       {
722          free_http_response(rsp);
723          return cgi_error_memory();
724       }
725       if (new_content_type != 0)
726       {
727          log_error(LOG_LEVEL_HEADER, "Overwriting Content-Type with %s", new_content_type);
728          if (enlist_unique_header(rsp->headers, "Content-Type", new_content_type))
729          {
730             free_http_response(rsp);
731             return cgi_error_memory();
732          }
733       }
734    }
735    else
736
737    /*
738     * Else, generate an HTML "blocked" message:
739     */
740    {
741       jb_err err;
742       struct map * exports;
743       char *p;
744
745       /*
746        * Workaround for stupid Netscape bug which prevents
747        * pages from being displayed if loading a referenced
748        * JavaScript or style sheet fails. So make it appear
749        * as if it succeeded.
750        */
751       if ( NULL != (p = get_header_value(csp->headers, "User-Agent:"))
752            && !strncmpic(p, "mozilla", 7) /* Catch Netscape but */
753            && !strstr(p, "Gecko")         /* save Mozilla, */
754            && !strstr(p, "compatible")    /* MSIE */
755            && !strstr(p, "Opera"))        /* and Opera. */
756       {
757          rsp->status = strdup("200 Request for blocked URL");
758       }
759       else
760       {
761          rsp->status = strdup("403 Request for blocked URL");
762       }
763
764       if (rsp->status == NULL)
765       {
766          free_http_response(rsp);
767          return cgi_error_memory();
768       }
769
770       exports = default_exports(csp, NULL);
771       if (exports == NULL)
772       {
773          free_http_response(rsp);
774          return cgi_error_memory();
775       }
776
777 #ifdef FEATURE_FORCE_LOAD
778       err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
779       /*
780        * Export the force conditional block killer if
781        *
782        * - Privoxy was compiled without FEATURE_FORCE_LOAD, or
783        * - Privoxy is configured to enforce blocks, or
784        * - it's a CONNECT request and enforcing wouldn't work anyway.
785        */
786       if ((csp->config->feature_flags & RUNTIME_FEATURE_ENFORCE_BLOCKS)
787        || (0 == strcmpic(csp->http->gpc, "connect")))
788 #endif /* ndef FEATURE_FORCE_LOAD */
789       {
790          err = map_block_killer(exports, "force-support");
791       }
792
793       if (!err) err = map(exports, "protocol", 1, csp->http->ssl ? "https://" : "http://", 1);
794       if (!err) err = map(exports, "hostport", 1, html_encode(csp->http->hostport), 0);
795       if (!err) err = map(exports, "path", 1, html_encode(csp->http->path), 0);
796       if (!err) err = map(exports, "path-ue", 1, url_encode(csp->http->path), 0);
797       if (!err)
798       {
799          const char *block_reason;
800          if (csp->action->string[ACTION_STRING_BLOCK] != NULL)
801          {
802             block_reason = csp->action->string[ACTION_STRING_BLOCK];
803          }
804          else
805          {
806             assert(connect_port_is_forbidden(csp));
807             block_reason = "Forbidden CONNECT port.";
808          }
809          err = map(exports, "block-reason", 1, html_encode(block_reason), 0);
810       }
811       if (err)
812       {
813          free_map(exports);
814          free_http_response(rsp);
815          return cgi_error_memory();
816       }
817
818       err = template_fill_for_cgi(csp, "blocked", exports, rsp);
819       if (err)
820       {
821          free_http_response(rsp);
822          return cgi_error_memory();
823       }
824    }
825    rsp->reason = RSP_REASON_BLOCKED;
826
827    return finish_http_response(csp, rsp);
828
829 }
830
831
832 #ifdef FEATURE_TRUST
833 /*********************************************************************
834  *
835  * Function    :  trust_url FIXME: I should be called distrust_url
836  *
837  * Description :  Calls is_untrusted_url to determine if the URL is trusted
838  *                and if not, returns a HTTP 403 response with a reject message.
839  *
840  * Parameters  :
841  *          1  :  csp = Current client state (buffers, headers, etc...)
842  *
843  * Returns     :  NULL => trusted, else http_response.
844  *
845  *********************************************************************/
846 struct http_response *trust_url(struct client_state *csp)
847 {
848    struct http_response *rsp;
849    struct map * exports;
850    char buf[BUFFER_SIZE];
851    char *p;
852    struct url_spec **tl;
853    struct url_spec *t;
854    jb_err err;
855
856    /*
857     * Don't bother to work on trusted URLs
858     */
859    if (!is_untrusted_url(csp))
860    {
861       return NULL;
862    }
863
864    /*
865     * Else, prepare a response:
866     */
867    if (NULL == (rsp = alloc_http_response()))
868    {
869       return cgi_error_memory();
870    }
871
872    rsp->status = strdup("403 Request blocked by Privoxy");
873    exports = default_exports(csp, NULL);
874    if (exports == NULL || rsp->status == NULL)
875    {
876       free_http_response(rsp);
877       return cgi_error_memory();
878    }
879
880    /*
881     * Export the protocol, host, port, and referrer information
882     */
883    err = map(exports, "hostport", 1, csp->http->hostport, 1);
884    if (!err) err = map(exports, "protocol", 1, csp->http->ssl ? "https://" : "http://", 1); 
885    if (!err) err = map(exports, "path", 1, csp->http->path, 1);
886
887    if (NULL != (p = get_header_value(csp->headers, "Referer:")))
888    {
889       if (!err) err = map(exports, "referrer", 1, html_encode(p), 0);
890    }
891    else
892    {
893       if (!err) err = map(exports, "referrer", 1, "none set", 1);
894    }
895
896    if (err)
897    {
898       free_map(exports);
899       free_http_response(rsp);
900       return cgi_error_memory();
901    }
902
903    /*
904     * Export the trust list
905     */
906    p = strdup("");
907    for (tl = csp->config->trust_list; (t = *tl) != NULL ; tl++)
908    {
909       snprintf(buf, sizeof(buf), "<li>%s</li>\n", t->spec);
910       string_append(&p, buf);
911    }
912    err = map(exports, "trusted-referrers", 1, p, 0);
913
914    if (err)
915    {
916       free_map(exports);
917       free_http_response(rsp);
918       return cgi_error_memory();
919    }
920
921    /*
922     * Export the trust info, if available
923     */
924    if (csp->config->trust_info->first)
925    {
926       struct list_entry *l;
927
928       p = strdup("");
929       for (l = csp->config->trust_info->first; l ; l = l->next)
930       {
931          snprintf(buf, sizeof(buf), "<li> <a href=\"%s\">%s</a><br>\n", l->str, l->str);
932          string_append(&p, buf);
933       }
934       err = map(exports, "trust-info", 1, p, 0);
935    }
936    else
937    {
938       err = map_block_killer(exports, "have-trust-info");
939    }
940
941    if (err)
942    {
943       free_map(exports);
944       free_http_response(rsp);
945       return cgi_error_memory();
946    }
947
948    /*
949     * Export the force conditional block killer if
950     *
951     * - Privoxy was compiled without FEATURE_FORCE_LOAD, or
952     * - Privoxy is configured to enforce blocks, or
953     * - it's a CONNECT request and enforcing wouldn't work anyway.
954     */
955 #ifdef FEATURE_FORCE_LOAD
956    if ((csp->config->feature_flags & RUNTIME_FEATURE_ENFORCE_BLOCKS)
957     || (0 == strcmpic(csp->http->gpc, "connect")))
958    {
959       err = map_block_killer(exports, "force-support");
960    }
961    else
962    {
963       err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
964    }
965 #else /* ifndef FEATURE_FORCE_LOAD */
966    err = map_block_killer(exports, "force-support");
967 #endif /* ndef FEATURE_FORCE_LOAD */
968
969    if (err)
970    {
971       free_map(exports);
972       free_http_response(rsp);
973       return cgi_error_memory();
974    }
975
976    /*
977     * Build the response
978     */
979    err = template_fill_for_cgi(csp, "untrusted", exports, rsp);
980    if (err)
981    {
982       free_http_response(rsp);
983       return cgi_error_memory();
984    }
985    rsp->reason = RSP_REASON_UNTRUSTED;
986
987    return finish_http_response(csp, rsp);
988 }
989 #endif /* def FEATURE_TRUST */
990
991
992 /*********************************************************************
993  *
994  * Function    :  compile_dynamic_pcrs_job_list
995  *
996  * Description :  Compiles a dynamic pcrs job list (one with variables
997  *                resolved at request time)
998  *
999  * Parameters  :
1000  *          1  :  csp = Current client state (buffers, headers, etc...)
1001  *          2  :  b = The filter list to compile
1002  *
1003  * Returns     :  NULL in case of errors, otherwise the
1004  *                pcrs job list.  
1005  *
1006  *********************************************************************/
1007 pcrs_job *compile_dynamic_pcrs_job_list(const struct client_state *csp, const struct re_filterfile_spec *b)
1008 {
1009    struct list_entry *pattern;
1010    pcrs_job *job_list = NULL;
1011    pcrs_job *dummy = NULL;
1012    pcrs_job *lastjob = NULL;
1013    int error = 0;
1014
1015    const struct pcrs_variable variables[] =
1016    {
1017       {"url",    csp->http->url,   1},
1018       {"path",   csp->http->path,  1},
1019       {"host",   csp->http->host,  1},
1020       {"origin", csp->ip_addr_str, 1},
1021       {NULL,     NULL,             1}
1022    };
1023
1024    for (pattern = b->patterns->first; pattern != NULL; pattern = pattern->next)
1025    {
1026       assert(pattern->str != NULL);
1027
1028       dummy = pcrs_compile_dynamic_command(pattern->str, variables, &error);
1029       if (NULL == dummy)
1030       {
1031          assert(error < 0);
1032          log_error(LOG_LEVEL_ERROR,
1033             "Adding filter job \'%s\' to dynamic filter %s failed: %s",
1034             pattern->str, b->name, pcrs_strerror(error));
1035          continue;
1036       }
1037       else
1038       {
1039          if (error == PCRS_WARN_TRUNCATION)
1040          {
1041             log_error(LOG_LEVEL_ERROR,
1042                "At least one of the variables in \'%s\' had to "
1043                "be truncated before compilation", pattern->str);
1044          }
1045          if (job_list == NULL)
1046          {
1047             job_list = dummy;
1048          }
1049          else
1050          {
1051             lastjob->next = dummy;
1052          }
1053          lastjob = dummy;
1054       }
1055    }
1056
1057    return job_list;
1058 }
1059
1060
1061 /*********************************************************************
1062  *
1063  * Function    :  rewrite_url
1064  *
1065  * Description :  Rewrites a URL with a single pcrs command
1066  *                and returns the result if it differs from the
1067  *                original and isn't obviously invalid.
1068  *
1069  * Parameters  :
1070  *          1  :  old_url = URL to rewrite.
1071  *          2  :  pcrs_command = pcrs command formatted as string (s@foo@bar@)
1072  *
1073  *
1074  * Returns     :  NULL if the pcrs_command didn't change the url, or 
1075  *                the result of the modification.
1076  *
1077  *********************************************************************/
1078 char *rewrite_url(char *old_url, const char *pcrs_command)
1079 {
1080    char *new_url = NULL;
1081    int hits;
1082
1083    assert(old_url);
1084    assert(pcrs_command);
1085
1086    new_url = pcrs_execute_single_command(old_url, pcrs_command, &hits);
1087
1088    if (hits == 0)
1089    {
1090       log_error(LOG_LEVEL_REDIRECTS,
1091          "pcrs command \"%s\" didn't change \"%s\".",
1092          pcrs_command, old_url);
1093       freez(new_url);
1094    }
1095    else if (hits < 0)
1096    {
1097       log_error(LOG_LEVEL_REDIRECTS,
1098          "executing pcrs command \"%s\" to rewrite %s failed: %s",
1099          pcrs_command, old_url, pcrs_strerror(hits));
1100       freez(new_url);
1101    }
1102    else if (strncmpic(new_url, "http://", 7) && strncmpic(new_url, "https://", 8))
1103    {
1104       log_error(LOG_LEVEL_ERROR,
1105          "pcrs command \"%s\" changed \"%s\" to \"%s\" (%u hi%s), "
1106          "but the result doesn't look like a valid URL and will be ignored.",
1107          pcrs_command, old_url, new_url, hits, (hits == 1) ? "t" : "ts");
1108       freez(new_url);
1109    }
1110    else
1111    {
1112       log_error(LOG_LEVEL_REDIRECTS,
1113          "pcrs command \"%s\" changed \"%s\" to \"%s\" (%u hi%s).",
1114          pcrs_command, old_url, new_url, hits, (hits == 1) ? "t" : "ts");
1115    }
1116
1117    return new_url;
1118
1119 }
1120
1121
1122 #ifdef FEATURE_FAST_REDIRECTS
1123 /*********************************************************************
1124  *
1125  * Function    :  get_last_url
1126  *
1127  * Description :  Search for the last URL inside a string.
1128  *                If the string already is a URL, it will
1129  *                be the first URL found.
1130  *
1131  * Parameters  :
1132  *          1  :  subject = the string to check
1133  *          2  :  redirect_mode = +fast-redirect{} mode 
1134  *
1135  * Returns     :  NULL if no URL was found, or
1136  *                the last URL found.
1137  *
1138  *********************************************************************/
1139 char *get_last_url(char *subject, const char *redirect_mode)
1140 {
1141    char *new_url = NULL;
1142    char *tmp;
1143
1144    assert(subject);
1145    assert(redirect_mode);
1146
1147    subject = strdup(subject);
1148    if (subject == NULL)
1149    {
1150       log_error(LOG_LEVEL_ERROR, "Out of memory while searching for redirects.");
1151       return NULL;
1152    }
1153
1154    if (0 == strcmpic(redirect_mode, "check-decoded-url"))
1155    {  
1156       log_error(LOG_LEVEL_REDIRECTS, "Decoding \"%s\" if necessary.", subject);
1157       new_url = url_decode(subject);
1158       if (new_url != NULL)
1159       {
1160          freez(subject);
1161          subject = new_url;
1162       }
1163       else
1164       {
1165          log_error(LOG_LEVEL_ERROR, "Unable to decode \"%s\".", subject);
1166       }
1167    }
1168
1169    log_error(LOG_LEVEL_REDIRECTS, "Checking \"%s\" for redirects.", subject);
1170
1171    /*
1172     * Find the last URL encoded in the request
1173     */
1174    tmp = subject;
1175    while ((tmp = strstr(tmp, "http://")) != NULL)
1176    {
1177       new_url = tmp++;
1178    }
1179    tmp = (new_url != NULL) ? new_url : subject;
1180    while ((tmp = strstr(tmp, "https://")) != NULL)
1181    {
1182       new_url = tmp++;
1183    }
1184
1185    if ((new_url != NULL)
1186       && (  (new_url != subject)
1187          || (0 == strncmpic(subject, "http://", 7))
1188          || (0 == strncmpic(subject, "https://", 8))
1189          ))
1190    {
1191       /*
1192        * Return new URL if we found a redirect 
1193        * or if the subject already was a URL.
1194        *
1195        * The second case makes sure that we can
1196        * chain get_last_url after another redirection check
1197        * (like rewrite_url) without losing earlier redirects.
1198        */
1199       new_url = strdup(new_url);
1200       freez(subject);
1201       return new_url;
1202    }
1203
1204    freez(subject);
1205    return NULL;
1206
1207 }
1208 #endif /* def FEATURE_FAST_REDIRECTS */
1209
1210
1211 /*********************************************************************
1212  *
1213  * Function    :  redirect_url
1214  *
1215  * Description :  Checks if Privoxy should answer the request with
1216  *                a HTTP redirect and generates the redirect if
1217  *                necessary.
1218  *
1219  * Parameters  :
1220  *          1  :  csp = Current client state (buffers, headers, etc...)
1221  *
1222  * Returns     :  NULL if the request can pass, HTTP redirect otherwise.
1223  *
1224  *********************************************************************/
1225 struct http_response *redirect_url(struct client_state *csp)
1226 {
1227    struct http_response *rsp;
1228 #ifdef FEATURE_FAST_REDIRECTS
1229    /*
1230     * XXX: Do we still need FEATURE_FAST_REDIRECTS
1231     * as compile-time option? The user can easily disable
1232     * it in his action file.
1233     */
1234    char * redirect_mode;
1235 #endif /* def FEATURE_FAST_REDIRECTS */
1236    char *old_url = NULL;
1237    char *new_url = NULL;
1238    char *redirection_string;
1239
1240    if ((csp->action->flags & ACTION_REDIRECT))
1241    {
1242       redirection_string = csp->action->string[ACTION_STRING_REDIRECT];
1243
1244       /*
1245        * If the redirection string begins with 's',
1246        * assume it's a pcrs command, otherwise treat it as
1247        * properly formatted URL and use it for the redirection
1248        * directly.
1249        *
1250        * According to RFC 2616 section 14.30 the URL
1251        * has to be absolute and if the user tries:
1252        * +redirect{shit/this/will/be/parsed/as/pcrs_command.html}
1253        * she would get undefined results anyway.
1254        *
1255        */
1256
1257       if (*redirection_string == 's')
1258       {
1259          old_url = csp->http->url;
1260          new_url = rewrite_url(old_url, redirection_string);
1261       }
1262       else
1263       {
1264          log_error(LOG_LEVEL_REDIRECTS,
1265             "No pcrs command recognized, assuming that \"%s\" is already properly formatted.",
1266             redirection_string);
1267          new_url = strdup(redirection_string);
1268       }
1269    }
1270
1271 #ifdef FEATURE_FAST_REDIRECTS
1272    if ((csp->action->flags & ACTION_FAST_REDIRECTS))
1273    {
1274       redirect_mode = csp->action->string[ACTION_STRING_FAST_REDIRECTS];
1275
1276       /*
1277        * If it exists, use the previously rewritten URL as input
1278        * otherwise just use the old path.
1279        */
1280       old_url = (new_url != NULL) ? new_url : strdup(csp->http->path);
1281       new_url = get_last_url(old_url, redirect_mode);
1282       freez(old_url);
1283    }
1284
1285    /*
1286     * Disable redirect checkers, so that they
1287     * will be only run more than once if the user
1288     * also enables them through tags.
1289     *
1290     * From a performance point of view
1291     * it doesn't matter, but the duplicated
1292     * log messages are annoying.
1293     */
1294    csp->action->flags &= ~ACTION_FAST_REDIRECTS;
1295 #endif /* def FEATURE_FAST_REDIRECTS */
1296    csp->action->flags &= ~ACTION_REDIRECT;
1297
1298    /* Did any redirect action trigger? */   
1299    if (new_url)
1300    {
1301       if (0 == strcmpic(new_url, csp->http->url))
1302       {
1303          log_error(LOG_LEVEL_ERROR,
1304             "New URL \"%s\" and old URL \"%s\" are the same. Redirection loop prevented.",
1305             csp->http->url, new_url);
1306             freez(new_url);
1307       }
1308       else
1309       {
1310          log_error(LOG_LEVEL_REDIRECTS, "New URL is: %s", new_url);
1311
1312          if (NULL == (rsp = alloc_http_response()))
1313          {
1314             freez(new_url);
1315             return cgi_error_memory();
1316          }
1317
1318          if ( enlist_unique_header(rsp->headers, "Location", new_url)
1319            || (NULL == (rsp->status = strdup("302 Local Redirect from Privoxy"))) )
1320          {
1321             freez(new_url);
1322             free_http_response(rsp);
1323             return cgi_error_memory();
1324          }
1325          rsp->reason = RSP_REASON_REDIRECTED;
1326          freez(new_url);
1327
1328          return finish_http_response(csp, rsp);
1329       }
1330    }
1331
1332    /* Only reached if no redirect is required */
1333    return NULL;
1334
1335 }
1336
1337
1338 #ifdef FEATURE_IMAGE_BLOCKING
1339 /*********************************************************************
1340  *
1341  * Function    :  is_imageurl
1342  *
1343  * Description :  Given a URL, decide whether it is an image or not,
1344  *                using either the info from a previous +image action
1345  *                or, #ifdef FEATURE_IMAGE_DETECT_MSIE, and the browser
1346  *                is MSIE and not on a Mac, tell from the browser's accept
1347  *                header.
1348  *
1349  * Parameters  :
1350  *          1  :  csp = Current client state (buffers, headers, etc...)
1351  *
1352  * Returns     :  True (nonzero) if URL is an image, false (0)
1353  *                otherwise
1354  *
1355  *********************************************************************/
1356 int is_imageurl(const struct client_state *csp)
1357 {
1358 #ifdef FEATURE_IMAGE_DETECT_MSIE
1359    char *tmp;
1360
1361    tmp = get_header_value(csp->headers, "User-Agent:");
1362    if (tmp && strstr(tmp, "MSIE") && !strstr(tmp, "Mac_"))
1363    {
1364       tmp = get_header_value(csp->headers, "Accept:");
1365       if (tmp && strstr(tmp, "image/gif"))
1366       {
1367          /* Client will accept HTML.  If this seems counterintuitive,
1368           * blame Microsoft.
1369           */
1370          return(0);
1371       }
1372       else
1373       {
1374          return(1);
1375       }
1376    }
1377 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1378
1379    return ((csp->action->flags & ACTION_IMAGE) != 0);
1380
1381 }
1382 #endif /* def FEATURE_IMAGE_BLOCKING */
1383
1384
1385 #ifdef FEATURE_TRUST
1386 /*********************************************************************
1387  *
1388  * Function    :  is_untrusted_url
1389  *
1390  * Description :  Should we "distrust" this URL (and block it)?
1391  *
1392  *                Yes if it matches a line in the trustfile, or if the
1393  *                    referrer matches a line starting with "+" in the
1394  *                    trustfile.
1395  *                No  otherwise.
1396  *
1397  * Parameters  :
1398  *          1  :  csp = Current client state (buffers, headers, etc...)
1399  *
1400  * Returns     :  0 => trusted, 1 => untrusted
1401  *
1402  *********************************************************************/
1403 int is_untrusted_url(const struct client_state *csp)
1404 {
1405    struct file_list *fl;
1406    struct block_spec *b;
1407    struct url_spec **trusted_url;
1408    struct http_request rhttp[1];
1409    const char * referer;
1410    jb_err err;
1411
1412    /*
1413     * If we don't have a trustlist, we trust everybody
1414     */
1415    if (((fl = csp->tlist) == NULL) || ((b  = fl->f) == NULL))
1416    {
1417       return 0;
1418    }
1419
1420    memset(rhttp, '\0', sizeof(*rhttp));
1421
1422    /*
1423     * Do we trust the request URL itself?
1424     */
1425    for (b = b->next; b ; b = b->next)
1426    {
1427       if (url_match(b->url, csp->http))
1428       {
1429          return b->reject;
1430       }
1431    }
1432
1433    if (NULL == (referer = get_header_value(csp->headers, "Referer:")))
1434    {
1435       /* no referrer was supplied */
1436       return 1;
1437    }
1438
1439
1440    /*
1441     * If not, do we maybe trust its referrer?
1442     */
1443    err = parse_http_url(referer, rhttp, REQUIRE_PROTOCOL);
1444    if (err)
1445    {
1446       return 1;
1447    }
1448
1449    for (trusted_url = csp->config->trust_list; *trusted_url != NULL; trusted_url++)
1450    {
1451       if (url_match(*trusted_url, rhttp))
1452       {
1453          /* if the URL's referrer is from a trusted referrer, then
1454           * add the target spec to the trustfile as an unblocked
1455           * domain and return 0 (which means it's OK).
1456           */
1457
1458          FILE *fp;
1459
1460          if (NULL != (fp = fopen(csp->config->trustfile, "a")))
1461          {
1462             char * path;
1463             char * path_end;
1464             char * new_entry = strdup("~");
1465
1466             string_append(&new_entry, csp->http->hostport);
1467
1468             path = csp->http->path;
1469             if ( (path[0] == '/')
1470               && (path[1] == '~')
1471               && ((path_end = strchr(path + 2, '/')) != NULL))
1472             {
1473                /* since this path points into a user's home space
1474                 * be sure to include this spec in the trustfile.
1475                 */
1476                long path_len = path_end - path; /* save offset */
1477                path = strdup(path); /* Copy string */
1478                if (path != NULL)
1479                {
1480                   path_end = path + path_len; /* regenerate ptr to new buffer */
1481                   *(path_end + 1) = '\0'; /* Truncate path after '/' */
1482                }
1483                string_join(&new_entry, path);
1484             }
1485
1486             /*
1487              * Give a reason for generating this entry.
1488              */
1489             string_append(&new_entry, " # Trusted referrer was: ");
1490             string_append(&new_entry, referer);
1491
1492             if (new_entry != NULL)
1493             {
1494                if (-1 == fprintf(fp, "%s\n", new_entry))
1495                {
1496                   log_error(LOG_LEVEL_ERROR, "Failed to append \'%s\' to trustfile \'%s\': %E",
1497                      new_entry, csp->config->trustfile);
1498                }
1499                freez(new_entry);
1500             }
1501             else
1502             {
1503                /* FIXME: No way to handle out-of memory, so mostly ignoring it */
1504                log_error(LOG_LEVEL_ERROR, "Out of memory adding pattern to trust file");
1505             }
1506
1507             fclose(fp);
1508          }
1509          else
1510          {
1511             log_error(LOG_LEVEL_ERROR, "Failed to append new entry for \'%s\' to trustfile \'%s\': %E",
1512                csp->http->hostport, csp->config->trustfile);
1513          }
1514          return 0;
1515       }
1516    }
1517
1518    return 1;
1519 }
1520 #endif /* def FEATURE_TRUST */
1521
1522
1523 /*********************************************************************
1524  *
1525  * Function    :  pcrs_filter_response
1526  *
1527  * Description :  Execute all text substitutions from all applying
1528  *                +filter actions on the text buffer that's been
1529  *                accumulated in csp->iob->buf.
1530  *
1531  * Parameters  :
1532  *          1  :  csp = Current client state (buffers, headers, etc...)
1533  *
1534  * Returns     :  a pointer to the (newly allocated) modified buffer.
1535  *                or NULL if there were no hits or something went wrong
1536  *
1537  *********************************************************************/
1538 static char *pcrs_filter_response(struct client_state *csp)
1539 {
1540    int hits=0;
1541    size_t size, prev_size;
1542
1543    char *old = NULL;
1544    char *new = NULL;
1545    pcrs_job *job;
1546
1547    struct file_list *fl;
1548    struct re_filterfile_spec *b;
1549    struct list_entry *filtername;
1550
1551    int i, found_filters = 0;
1552
1553    /* 
1554     * Sanity first
1555     */
1556    if (csp->iob->cur >= csp->iob->eod)
1557    {
1558       return(NULL);
1559    }
1560
1561    /*
1562     * Need to check the set of re_filterfiles...
1563     */
1564    for (i = 0; i < MAX_AF_FILES; i++)
1565    {
1566       fl = csp->rlist[i];
1567       if (NULL != fl)
1568       {
1569          if (NULL != fl->f)
1570          {
1571            found_filters = 1;
1572            break;
1573          }
1574       }
1575    }
1576
1577    if (0 == found_filters)
1578    {
1579       log_error(LOG_LEVEL_ERROR, "Inconsistent configuration: "
1580          "content filtering enabled, but no content filters available.");
1581       return(NULL);
1582    }
1583
1584    size = (size_t)(csp->iob->eod - csp->iob->cur);
1585    old = csp->iob->cur;
1586
1587    for (i = 0; i < MAX_AF_FILES; i++)
1588    {
1589      fl = csp->rlist[i];
1590      if ((NULL == fl) || (NULL == fl->f))
1591      {
1592         /*
1593          * Either there are no filter files
1594          * left, or this filter file just
1595          * contains no valid filters.
1596          *
1597          * Continue to be sure we don't miss
1598          * valid filter files that are chained
1599          * after empty or invalid ones.
1600          */
1601         continue;
1602      }
1603    /*
1604     * For all applying +filter actions, look if a filter by that
1605     * name exists and if yes, execute it's pcrs_joblist on the
1606     * buffer.
1607     */
1608    for (b = fl->f; b; b = b->next)
1609    {
1610       if (b->type != FT_CONTENT_FILTER)
1611       {
1612          /* Skip header filters */
1613          continue;
1614       }
1615
1616       for (filtername = csp->action->multi[ACTION_MULTI_FILTER]->first;
1617            filtername ; filtername = filtername->next)
1618       {
1619          if (strcmp(b->name, filtername->str) == 0)
1620          {
1621             int current_hits = 0; /* Number of hits caused by this filter */
1622             int job_number   = 0; /* Which job we're currently executing  */
1623             int job_hits     = 0; /* How many hits the current job caused */
1624             pcrs_job *joblist = b->joblist;
1625
1626             if (b->dynamic) joblist = compile_dynamic_pcrs_job_list(csp, b);
1627
1628             if (NULL == joblist)
1629             {
1630                log_error(LOG_LEVEL_RE_FILTER, "Filter %s has empty joblist. Nothing to do.", b->name);
1631                continue;
1632             }
1633
1634             prev_size = size;
1635             /* Apply all jobs from the joblist */
1636             for (job = joblist; NULL != job; job = job->next)
1637             {
1638                job_number++;
1639                job_hits = pcrs_execute(job, old, size, &new, &size);
1640
1641                if (job_hits >= 0)
1642                {
1643                   /*
1644                    * That went well. Continue filtering
1645                    * and use the result of this job as
1646                    * input for the next one.
1647                    */
1648                   current_hits += job_hits;
1649                   if (old != csp->iob->cur)
1650                   {
1651                      freez(old);
1652                   }
1653                   old = new;
1654                }
1655                else
1656                {
1657                   /*
1658                    * This job caused an unexpected error. Inform the user
1659                    * and skip the rest of the jobs in this filter. We could
1660                    * continue with the next job, but usually the jobs
1661                    * depend on each other or are similar enough to
1662                    * fail for the same reason.
1663                    *
1664                    * At the moment our pcrs expects the error codes of pcre 3.4,
1665                    * but newer pcre versions can return additional error codes.
1666                    * As a result pcrs_strerror()'s error message might be
1667                    * "Unknown error ...", therefore we print the numerical value
1668                    * as well.
1669                    *
1670                    * XXX: Is this important enough for LOG_LEVEL_ERROR or
1671                    * should we use LOG_LEVEL_RE_FILTER instead?
1672                    */
1673                   log_error(LOG_LEVEL_ERROR, "Skipped filter \'%s\' after job number %u: %s (%d)",
1674                      b->name, job_number, pcrs_strerror(job_hits), job_hits);
1675                   break;
1676                }
1677             }
1678
1679             if (b->dynamic) pcrs_free_joblist(joblist);
1680
1681             log_error(LOG_LEVEL_RE_FILTER,
1682                "filtering %s%s (size %d) with \'%s\' produced %d hits (new size %d).",
1683                csp->http->hostport, csp->http->path, prev_size, b->name, current_hits, size);
1684
1685             hits += current_hits;
1686          }
1687       }
1688    }
1689    }
1690
1691    /*
1692     * If there were no hits, destroy our copy and let
1693     * chat() use the original in csp->iob
1694     */
1695    if (!hits)
1696    {
1697       freez(new);
1698       return(NULL);
1699    }
1700
1701    csp->flags |= CSP_FLAG_MODIFIED;
1702    csp->content_length = size;
1703    IOB_RESET(csp);
1704
1705    return(new);
1706
1707 }
1708
1709
1710 /*********************************************************************
1711  *
1712  * Function    :  gif_deanimate_response
1713  *
1714  * Description :  Deanimate the GIF image that has been accumulated in
1715  *                csp->iob->buf, set csp->content_length to the modified
1716  *                size and raise the CSP_FLAG_MODIFIED flag.
1717  *
1718  * Parameters  :
1719  *          1  :  csp = Current client state (buffers, headers, etc...)
1720  *
1721  * Returns     :  a pointer to the (newly allocated) modified buffer.
1722  *                or NULL in case something went wrong.
1723  *
1724  *********************************************************************/
1725 static char *gif_deanimate_response(struct client_state *csp)
1726 {
1727    struct binbuffer *in, *out;
1728    char *p;
1729    size_t size;
1730
1731    size = (size_t)(csp->iob->eod - csp->iob->cur);
1732
1733    if (  (NULL == (in =  (struct binbuffer *)zalloc(sizeof *in )))
1734       || (NULL == (out = (struct binbuffer *)zalloc(sizeof *out))) )
1735    {
1736       log_error(LOG_LEVEL_DEANIMATE, "failed! (no mem)");
1737       return NULL;
1738    }
1739
1740    in->buffer = csp->iob->cur;
1741    in->size = size;
1742
1743    if (gif_deanimate(in, out, strncmp("last", csp->action->string[ACTION_STRING_DEANIMATE], 4)))
1744    {
1745       log_error(LOG_LEVEL_DEANIMATE, "failed! (gif parsing)");
1746       freez(in);
1747       buf_free(out);
1748       return(NULL);
1749    }
1750    else
1751    {
1752       if ((int)size == out->offset)
1753       {
1754          log_error(LOG_LEVEL_DEANIMATE, "GIF not changed.");
1755       }
1756       else
1757       {
1758          log_error(LOG_LEVEL_DEANIMATE, "Success! GIF shrunk from %d bytes to %d.", size, out->offset);
1759       }
1760       csp->content_length = out->offset;
1761       csp->flags |= CSP_FLAG_MODIFIED;
1762       p = out->buffer;
1763       freez(in);
1764       freez(out);
1765       return(p);
1766    }
1767
1768 }
1769
1770
1771 /*********************************************************************
1772  *
1773  * Function    :  get_filter_function
1774  *
1775  * Description :  Decides which content filter function has
1776  *                to be applied (if any).
1777  *
1778  * Parameters  :
1779  *          1  :  csp = Current client state (buffers, headers, etc...)
1780  *
1781  * Returns     :  The content filter function to run, or
1782  *                NULL if no content filter is active
1783  *
1784  *********************************************************************/
1785 filter_function_ptr get_filter_function(struct client_state *csp)
1786 {
1787    filter_function_ptr filter_function = NULL;
1788
1789    if ((csp->content_type & CT_TABOO)
1790       && !(csp->action->flags & ACTION_FORCE_TEXT_MODE))
1791    {
1792       return NULL;
1793    }
1794
1795    /*
1796     * Are we enabling text mode by force?
1797     */
1798    if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
1799    {
1800       /*
1801        * Do we really have to?
1802        */
1803       if (csp->content_type & CT_TEXT)
1804       {
1805          log_error(LOG_LEVEL_HEADER, "Text mode is already enabled.");   
1806       }
1807       else
1808       {
1809          csp->content_type |= CT_TEXT;
1810          log_error(LOG_LEVEL_HEADER, "Text mode enabled by force. Take cover!");   
1811       }
1812    }
1813
1814    if (!(csp->content_type & CT_DECLARED))
1815    {
1816       /*
1817        * The server didn't bother to declare a MIME-Type.
1818        * Assume it's text that can be filtered.
1819        *
1820        * This also regulary happens with 304 responses,
1821        * therefore logging anything here would cause
1822        * too much noise.
1823        */
1824       csp->content_type |= CT_TEXT;
1825    }
1826
1827    /*
1828     * Choose the applying filter function based on
1829     * the content type and action settings.
1830     */
1831    if ((csp->content_type & CT_TEXT) &&
1832        (csp->rlist != NULL) &&
1833        (!list_is_empty(csp->action->multi[ACTION_MULTI_FILTER])))
1834    {
1835       filter_function = pcrs_filter_response;
1836    }
1837    else if ((csp->content_type & CT_GIF)  &&
1838             (csp->action->flags & ACTION_DEANIMATE))
1839    {
1840       filter_function = gif_deanimate_response;
1841    }
1842
1843    return filter_function;
1844 }
1845
1846
1847 /*********************************************************************
1848  *
1849  * Function    :  remove_chunked_transfer_coding
1850  *
1851  * Description :  In-situ remove the "chunked" transfer coding as defined
1852  *                in rfc2616 from a buffer.
1853  *
1854  * Parameters  :
1855  *          1  :  buffer = Pointer to the text buffer
1856  *          2  :  size =  In: Number of bytes to be processed,
1857  *                       Out: Number of bytes after de-chunking.
1858  *                       (undefined in case of errors)
1859  *
1860  * Returns     :  JB_ERR_OK for success,
1861  *                JB_ERR_PARSE otherwise
1862  *
1863  *********************************************************************/
1864 static jb_err remove_chunked_transfer_coding(char *buffer, size_t *size)
1865 {
1866    size_t newsize = 0;
1867    unsigned int chunksize = 0;
1868    char *from_p, *to_p;
1869
1870    assert(buffer);
1871    from_p = to_p = buffer;
1872
1873    if (sscanf(buffer, "%x", &chunksize) != 1)
1874    {
1875       log_error(LOG_LEVEL_ERROR, "Invalid first chunksize while stripping \"chunked\" transfer coding");
1876       return JB_ERR_PARSE;
1877    }
1878
1879    while (chunksize > 0U)
1880    {
1881       if (NULL == (from_p = strstr(from_p, "\r\n")))
1882       {
1883          log_error(LOG_LEVEL_ERROR, "Parse error while stripping \"chunked\" transfer coding");
1884          return JB_ERR_PARSE;
1885       }
1886
1887       if ((newsize += chunksize) >= *size)
1888       {
1889          log_error(LOG_LEVEL_ERROR,
1890             "Chunk size %d exceeds buffer size %d in  \"chunked\" transfer coding",
1891             chunksize, *size);
1892          return JB_ERR_PARSE;
1893       }
1894       from_p += 2;
1895
1896       memmove(to_p, from_p, (size_t) chunksize);
1897       to_p = buffer + newsize;
1898       from_p += chunksize + 2;
1899
1900       if (sscanf(from_p, "%x", &chunksize) != 1)
1901       {
1902          log_error(LOG_LEVEL_INFO, "Invalid \"chunked\" transfer encoding detected and ignored.");
1903          break;
1904       }
1905    }
1906    
1907    /* XXX: Should get its own loglevel. */
1908    log_error(LOG_LEVEL_RE_FILTER, "De-chunking successful. Shrunk from %d to %d", *size, newsize);
1909
1910    *size = newsize;
1911
1912    return JB_ERR_OK;
1913
1914 }
1915
1916
1917 /*********************************************************************
1918  *
1919  * Function    :  prepare_for_filtering
1920  *
1921  * Description :  If necessary, de-chunks and decompresses
1922  *                the content so it can get filterd.
1923  *
1924  * Parameters  :
1925  *          1  :  csp = Current client state (buffers, headers, etc...)
1926  *
1927  * Returns     :  JB_ERR_OK for success,
1928  *                JB_ERR_PARSE otherwise
1929  *
1930  *********************************************************************/
1931 static jb_err prepare_for_filtering(struct client_state *csp)
1932 {
1933    jb_err err = JB_ERR_OK;
1934
1935    /*
1936     * If the body has a "chunked" transfer-encoding,
1937     * get rid of it, adjusting size and iob->eod
1938     */
1939    if (csp->flags & CSP_FLAG_CHUNKED)
1940    {
1941       size_t size = (size_t)(csp->iob->eod - csp->iob->cur);
1942
1943       log_error(LOG_LEVEL_RE_FILTER, "Need to de-chunk first");
1944       err = remove_chunked_transfer_coding(csp->iob->cur, &size);
1945       if (JB_ERR_OK == err)
1946       {
1947          csp->iob->eod = csp->iob->cur + size;
1948          csp->flags |= CSP_FLAG_MODIFIED;
1949       }
1950       else
1951       {
1952          return JB_ERR_PARSE;
1953       }
1954    }
1955
1956 #ifdef FEATURE_ZLIB
1957    /*
1958     * If the body has a supported transfer-encoding,
1959     * decompress it, adjusting size and iob->eod.
1960     */
1961    if (csp->content_type & (CT_GZIP|CT_DEFLATE))
1962    {
1963       if (0 == csp->iob->eod - csp->iob->cur)
1964       {
1965          /* Nothing left after de-chunking. */
1966          return JB_ERR_OK;
1967       }
1968
1969       err = decompress_iob(csp);
1970
1971       if (JB_ERR_OK == err)
1972       {
1973          csp->flags |= CSP_FLAG_MODIFIED;
1974          csp->content_type &= ~CT_TABOO;
1975       }
1976       else
1977       {
1978          /*
1979           * Unset CT_GZIP and CT_DEFLATE to remember not
1980           * to modify the Content-Encoding header later.
1981           */
1982          csp->content_type &= ~CT_GZIP;
1983          csp->content_type &= ~CT_DEFLATE;
1984       }
1985    }
1986 #endif
1987
1988    return err;
1989 }
1990
1991
1992 /*********************************************************************
1993  *
1994  * Function    :  execute_content_filter
1995  *
1996  * Description :  Executes a given content filter.
1997  *
1998  * Parameters  :
1999  *          1  :  csp = Current client state (buffers, headers, etc...)
2000  *          2  :  content_filter = The filter function to execute
2001  *
2002  * Returns     :  Pointer to the modified buffer, or
2003  *                NULL if filtering failed or wasn't necessary.
2004  *
2005  *********************************************************************/
2006 char *execute_content_filter(struct client_state *csp, filter_function_ptr content_filter)
2007 {
2008    if (0 == csp->iob->eod - csp->iob->cur)
2009    {
2010       /*
2011        * No content (probably status code 301, 302 ...),
2012        * no filtering necessary.
2013        */
2014       return NULL;
2015    }
2016
2017    if (JB_ERR_OK != prepare_for_filtering(csp))
2018    {
2019       /*
2020        * failed to de-chunk or decompress.
2021        */
2022       return NULL;
2023    }
2024
2025    if (0 == csp->iob->eod - csp->iob->cur)
2026    {
2027       /*
2028        * Clown alarm: chunked and/or compressed nothing delivered.
2029        */
2030       return NULL;
2031    }
2032
2033    return ((*content_filter)(csp));
2034 }
2035
2036
2037 /*********************************************************************
2038  *
2039  * Function    :  get_url_actions
2040  *
2041  * Description :  Gets the actions for this URL.
2042  *
2043  * Parameters  :
2044  *          1  :  csp = Current client state (buffers, headers, etc...)
2045  *          2  :  http = http_request request for blocked URLs
2046  *
2047  * Returns     :  N/A
2048  *
2049  *********************************************************************/
2050 void get_url_actions(struct client_state *csp, struct http_request *http)
2051 {
2052    struct file_list *fl;
2053    struct url_actions *b;
2054    int i;
2055
2056    init_current_action(csp->action);
2057
2058    for (i = 0; i < MAX_AF_FILES; i++)
2059    {
2060       if (((fl = csp->actions_list[i]) == NULL) || ((b = fl->f) == NULL))
2061       {
2062          return;
2063       }
2064
2065       apply_url_actions(csp->action, http, b);
2066    }
2067
2068    return;
2069 }
2070
2071
2072 /*********************************************************************
2073  *
2074  * Function    :  apply_url_actions
2075  *
2076  * Description :  Applies a list of URL actions.
2077  *
2078  * Parameters  :
2079  *          1  :  action = Destination.
2080  *          2  :  http = Current URL
2081  *          3  :  b = list of URL actions to apply
2082  *
2083  * Returns     :  N/A
2084  *
2085  *********************************************************************/
2086 void apply_url_actions(struct current_action_spec *action,
2087                        struct http_request *http,
2088                        struct url_actions *b)
2089 {
2090    if (b == NULL)
2091    {
2092       /* Should never happen */
2093       return;
2094    }
2095
2096    for (b = b->next; NULL != b; b = b->next)
2097    {
2098       if (url_match(b->url, http))
2099       {
2100          merge_current_action(action, b->action);
2101       }
2102    }
2103 }
2104
2105
2106 /*********************************************************************
2107  *
2108  * Function    :  get_forward_override_settings
2109  *
2110  * Description :  Returns forward settings as specified with the
2111  *                forward-override{} action. forward-override accepts
2112  *                forward lines similar to the one used in the
2113  *                configuration file, but without the URL pattern.
2114  *
2115  *                For example:
2116  *
2117  *                   forward / .
2118  *
2119  *                in the configuration file can be replaced with
2120  *                the action section:
2121  *
2122  *                 {+forward-override{forward .}}
2123  *                 /
2124  *
2125  * Parameters  :
2126  *          1  :  csp = Current client state (buffers, headers, etc...)
2127  *
2128  * Returns     :  Pointer to forwarding structure in case of success.
2129  *                Invalid syntax is fatal.
2130  *
2131  *********************************************************************/
2132 const static struct forward_spec *get_forward_override_settings(struct client_state *csp)
2133 {
2134    const char *forward_override_line = csp->action->string[ACTION_STRING_FORWARD_OVERRIDE];
2135    char forward_settings[BUFFER_SIZE];
2136    char *http_parent = NULL;
2137    /* variable names were chosen for consistency reasons. */
2138    struct forward_spec *fwd = NULL;
2139    int vec_count;
2140    char *vec[3];
2141
2142    assert(csp->action->flags & ACTION_FORWARD_OVERRIDE);
2143    /* Should be enforced by load_one_actions_file() */
2144    assert(strlen(forward_override_line) < sizeof(forward_settings) - 1);
2145
2146    /* Create a copy ssplit can modify */
2147    strlcpy(forward_settings, forward_override_line, sizeof(forward_settings));
2148
2149    if (NULL != csp->fwd)
2150    {
2151       /*
2152        * XXX: Currently necessary to prevent memory
2153        * leaks when the show-url-info cgi page is visited.
2154        */
2155       unload_forward_spec(csp->fwd);
2156    }
2157
2158    /*
2159     * allocate a new forward node, valid only for
2160     * the lifetime of this request. Save its location
2161     * in csp as well, so sweep() can free it later on.
2162     */
2163    fwd = csp->fwd = zalloc(sizeof(*fwd));
2164    if (NULL == fwd)
2165    {
2166       log_error(LOG_LEVEL_FATAL,
2167          "can't allocate memory for forward-override{%s}", forward_override_line);
2168       /* Never get here - LOG_LEVEL_FATAL causes program exit */
2169       return NULL;
2170    }
2171
2172    vec_count = ssplit(forward_settings, " \t", vec, SZ(vec), 1, 1);
2173    if ((vec_count == 2) && !strcasecmp(vec[0], "forward"))
2174    {
2175       fwd->type = SOCKS_NONE;
2176
2177       /* Parse the parent HTTP proxy host:port */
2178       http_parent = vec[1];
2179
2180    }
2181    else if (vec_count == 3)
2182    {
2183       char *socks_proxy = NULL;
2184
2185       if  (!strcasecmp(vec[0], "forward-socks4"))
2186       {
2187          fwd->type = SOCKS_4;
2188          socks_proxy = vec[1];
2189       }
2190       else if (!strcasecmp(vec[0], "forward-socks4a"))
2191       {
2192          fwd->type = SOCKS_4A;
2193          socks_proxy = vec[1];
2194       }
2195       else if (!strcasecmp(vec[0], "forward-socks5"))
2196       {
2197          fwd->type = SOCKS_5;
2198          socks_proxy = vec[1];
2199       }
2200
2201       if (NULL != socks_proxy)
2202       {
2203          /* Parse the SOCKS proxy host[:port] */
2204          fwd->gateway_port = 1080;
2205          parse_forwarder_address(socks_proxy,
2206             &fwd->gateway_host, &fwd->gateway_port);
2207
2208          http_parent = vec[2];
2209       }
2210    }
2211
2212    if (NULL == http_parent)
2213    {
2214       log_error(LOG_LEVEL_FATAL,
2215          "Invalid forward-override syntax in: %s", forward_override_line);
2216       /* Never get here - LOG_LEVEL_FATAL causes program exit */
2217    }
2218
2219    /* Parse http forwarding settings */
2220    if (strcmp(http_parent, ".") != 0)
2221    {
2222       fwd->forward_port = 8000;
2223       parse_forwarder_address(http_parent,
2224          &fwd->forward_host, &fwd->forward_port);
2225    }
2226
2227    assert (NULL != fwd);
2228
2229    log_error(LOG_LEVEL_CONNECT,
2230       "Overriding forwarding settings based on \'%s\'", forward_override_line);
2231
2232    return fwd;
2233 }
2234
2235
2236 /*********************************************************************
2237  *
2238  * Function    :  forward_url
2239  *
2240  * Description :  Should we forward this to another proxy?
2241  *
2242  * Parameters  :
2243  *          1  :  csp = Current client state (buffers, headers, etc...)
2244  *          2  :  http = http_request request for current URL
2245  *
2246  * Returns     :  Pointer to forwarding information.
2247  *
2248  *********************************************************************/
2249 const struct forward_spec *forward_url(struct client_state *csp,
2250                                        const struct http_request *http)
2251 {
2252    static const struct forward_spec fwd_default[1] = { FORWARD_SPEC_INITIALIZER };
2253    struct forward_spec *fwd = csp->config->forward;
2254
2255    if (csp->action->flags & ACTION_FORWARD_OVERRIDE)
2256    {
2257       return get_forward_override_settings(csp);
2258    }
2259
2260    if (fwd == NULL)
2261    {
2262       return fwd_default;
2263    }
2264
2265    while (fwd != NULL)
2266    {
2267       if (url_match(fwd->url, http))
2268       {
2269          return fwd;
2270       }
2271       fwd = fwd->next;
2272    }
2273
2274    return fwd_default;
2275 }
2276
2277
2278 /*********************************************************************
2279  *
2280  * Function    :  direct_response 
2281  *
2282  * Description :  Check if Max-Forwards == 0 for an OPTIONS or TRACE
2283  *                request and if so, return a HTTP 501 to the client.
2284  *
2285  *                FIXME: I have a stupid name and I should handle the
2286  *                requests properly. Still, what we do here is rfc-
2287  *                compliant, whereas ignoring or forwarding are not.
2288  *
2289  * Parameters  :  
2290  *          1  :  csp = Current client state (buffers, headers, etc...)
2291  *
2292  * Returns     :  http_response if , NULL if nonmatch or handler fail
2293  *
2294  *********************************************************************/
2295 struct http_response *direct_response(struct client_state *csp)
2296 {
2297    struct http_response *rsp;
2298    struct list_entry *p;
2299
2300    if ((0 == strcmpic(csp->http->gpc, "trace"))
2301       || (0 == strcmpic(csp->http->gpc, "options")))
2302    {
2303       for (p = csp->headers->first; (p != NULL) ; p = p->next)
2304       {
2305          if (!strncmpic("Max-Forwards:", p->str, 13))
2306          {
2307             unsigned int max_forwards;
2308
2309             /*
2310              * If it's a Max-Forwards value of zero,
2311              * we have to intercept the request.
2312              */
2313             if (1 == sscanf(p->str+12, ": %u", &max_forwards) && max_forwards == 0)
2314             {
2315                /*
2316                 * FIXME: We could handle at least TRACE here,
2317                 * but that would require a verbatim copy of
2318                 * the request which we don't have anymore
2319                 */
2320                 log_error(LOG_LEVEL_HEADER,
2321                   "Detected header \'%s\' in OPTIONS or TRACE request. Returning 501.",
2322                   p->str);
2323
2324                /* Get mem for response or fail*/
2325                if (NULL == (rsp = alloc_http_response()))
2326                {
2327                   return cgi_error_memory();
2328                }
2329             
2330                if (NULL == (rsp->status = strdup("501 Not Implemented")))
2331                {
2332                   free_http_response(rsp);
2333                   return cgi_error_memory();
2334                }
2335
2336                rsp->is_static = 1;
2337                rsp->reason = RSP_REASON_UNSUPPORTED;
2338
2339                return(finish_http_response(csp, rsp));
2340             }
2341          }
2342       }
2343    }
2344    return NULL;
2345 }
2346
2347
2348 /*********************************************************************
2349  *
2350  * Function    :  content_filters_enabled
2351  *
2352  * Description :  Checks whether there are any content filters
2353  *                enabled for the current request.
2354  *
2355  * Parameters  :  
2356  *          1  :  action = Action spec to check.
2357  *
2358  * Returns     :  TRUE for yes, FALSE otherwise
2359  *
2360  *********************************************************************/
2361 int content_filters_enabled(const struct current_action_spec *action)
2362 {
2363    return ((action->flags & ACTION_DEANIMATE) ||
2364       !list_is_empty(action->multi[ACTION_MULTI_FILTER]));
2365 }
2366
2367 /*
2368   Local Variables:
2369   tab-width: 3
2370   end:
2371 */