correct feedback channels
[privoxy.git] / filters.c
1 const char filters_rcs[] = "$Id: filters.c,v 1.44 2002/03/07 03:49:31 oes 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', and `trust_url'
13  *
14  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
15  *                IJBSWA team.  http://ijbswa.sourceforge.net
16  *
17  *                Based on the Internet Junkbuster originally written
18  *                by and Copyright (C) 1997 Anonymous Coders and
19  *                Junkbusters Corporation.  http://www.junkbusters.com
20  *
21  *                This program is free software; you can redistribute it
22  *                and/or modify it under the terms of the GNU General
23  *                Public License as published by the Free Software
24  *                Foundation; either version 2 of the License, or (at
25  *                your option) any later version.
26  *
27  *                This program is distributed in the hope that it will
28  *                be useful, but WITHOUT ANY WARRANTY; without even the
29  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
30  *                PARTICULAR PURPOSE.  See the GNU General Public
31  *                License for more details.
32  *
33  *                The GNU General Public License should be included with
34  *                this file.  If not, you can view it at
35  *                http://www.gnu.org/copyleft/gpl.html
36  *                or write to the Free Software Foundation, Inc., 59
37  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
38  *
39  * Revisions   :
40  *    $Log: filters.c,v $
41  *    Revision 1.44  2002/03/07 03:49:31  oes
42  *     - Fixed compiler warnings etc
43  *     - Changed built-in images from GIF to PNG
44  *       (with regard to Unisys patent issue)
45  *     - Added a 4x4 pattern PNG which is less intrusive
46  *       than the logo but also clearly marks the deleted banners
47  *
48  *    Revision 1.43  2002/01/22 23:51:59  jongfoster
49  *    Replacing strsav() with the safer string_append().
50  *
51  *    Adding missing html_encode() to error message generators.  Where encoded
52  *    and unencoded versions of a string were provided, removing the unencoded
53  *    one.
54  *
55  *    Revision 1.42  2002/01/17 21:00:32  jongfoster
56  *    Moving all our URL and URL pattern parsing code to urlmatch.c.
57  *
58  *    Using a single, simple url_match(pattern,url) function - rather than
59  *    the 3-line match routine which was repeated all over the place.
60  *
61  *    Renaming free_url to free_url_spec, since it frees a struct url_spec.
62  *
63  *    Using parse_http_url() to parse URLs without faking a HTTP
64  *    request line for parse_http_request().
65  *
66  *    Revision 1.41  2001/11/13 00:14:07  jongfoster
67  *    Fixing stupid bug now I've figured out what || means.
68  *    (It always returns 0 or 1, not one of it's paramaters.)
69  *
70  *    Revision 1.40  2001/10/26 17:37:55  oes
71  *    - Re-enabled Netscape 200/404 bug workaround in block_url():
72  *      - Removed OS/2 special case
73  *      - Made block_url() independant from sed() having been run
74  *    - Made trust_url independant from sed() having been run
75  *    - Made is_imageurl independant from sed() having been run.
76  *      It now checks User-Agent: and Accept: by itself.
77  *
78  *
79  *    Revision 1.39  2001/10/25 03:40:48  david__schmidt
80  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
81  *    threads to call select() simultaneously.  So, it's time to do a real, live,
82  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
83  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
84  *
85  *    Revision 1.38  2001/10/23 21:32:33  jongfoster
86  *    Adding error-checking to selected functions
87  *
88  *    Revision 1.37  2001/10/22 15:33:56  david__schmidt
89  *    Special-cased OS/2 out of the Netscape-abort-on-404-in-js problem in
90  *    filters.c.  Added a FIXME in front of the offending code.  I'll gladly
91  *    put in a better/more robust fix for all parties if one is presented...
92  *    It seems that just returning 200 instead of 404 would pretty much fix
93  *    it for everyone, but I don't know all the history of the problem.
94  *
95  *    Revision 1.36  2001/10/10 16:44:16  oes
96  *    Added match_portlist function
97  *
98  *    Revision 1.35  2001/10/07 15:41:23  oes
99  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
100  *
101  *    New function remove_chunked_transfer_coding that strips chunked
102  *      transfer coding to plain and is called by pcrs_filter_response
103  *      and gif_deanimate_response if neccessary
104  *
105  *    Improved handling of zero-change re_filter runs
106  *
107  *    pcrs_filter_response and gif_deanimate_response now remove
108  *      chunked transfer codeing before processing the body.
109  *
110  *    Revision 1.34  2001/09/20 15:49:36  steudten
111  *
112  *    Fix BUG: Change int size to size_t size in pcrs_filter_response().
113  *    See cgi.c fill_template().
114  *
115  *    Revision 1.33  2001/09/16 17:05:14  jongfoster
116  *    Removing unused #include showarg.h
117  *
118  *    Revision 1.32  2001/09/16 13:21:27  jongfoster
119  *    Changes to use new list functions.
120  *
121  *    Revision 1.31  2001/09/16 11:38:02  jongfoster
122  *    Splitting fill_template() into 2 functions:
123  *    template_load() loads the file
124  *    template_fill() performs the PCRS regexps.
125  *    This is because the CGI edit interface has a "table row"
126  *    template which is used many times in the page - this
127  *    change means it's only loaded from disk once.
128  *
129  *    Revision 1.30  2001/09/16 11:00:10  jongfoster
130  *    New function alloc_http_response, for symmetry with free_http_response
131  *
132  *    Revision 1.29  2001/09/13 23:32:40  jongfoster
133  *    Moving image data to cgi.c rather than cgi.h
134  *    Fixing a GPF under Win32 (and any other OS that protects global
135  *    constants from being written to).
136  *
137  *    Revision 1.28  2001/09/10 10:18:51  oes
138  *    Silenced compiler warnings
139  *
140  *    Revision 1.27  2001/08/05 16:06:20  jongfoster
141  *    Modifiying "struct map" so that there are now separate header and
142  *    "map_entry" structures.  This means that functions which modify a
143  *    map no longer need to return a pointer to the modified map.
144  *    Also, it no longer reverses the order of the entries (which may be
145  *    important with some advanced template substitutions).
146  *
147  *    Revision 1.26  2001/07/30 22:08:36  jongfoster
148  *    Tidying up #defines:
149  *    - All feature #defines are now of the form FEATURE_xxx
150  *    - Permanently turned off WIN_GUI_EDIT
151  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
152  *
153  *    Revision 1.25  2001/07/26 10:09:46  oes
154  *    Made browser detection a little less naive
155  *
156  *    Revision 1.24  2001/07/25 17:22:51  oes
157  *    Added workaround for Netscape bug that prevents display of page when loading a component fails.
158  *
159  *    Revision 1.23  2001/07/23 13:40:12  oes
160  *    Fixed bug that caused document body to be dropped when pcrs joblist was empty.
161  *
162  *    Revision 1.22  2001/07/18 12:29:34  oes
163  *    - Made gif_deanimate_response respect
164  *      csp->action->string[ACTION_STRING_DEANIMATE]
165  *    - Logging cosmetics
166  *
167  *    Revision 1.21  2001/07/13 13:59:53  oes
168  *     - Introduced gif_deanimate_response which shares the
169  *       generic content modification interface of pcrs_filter_response
170  *       and acts as a wrapper to deanimate.c:gif_deanimate()
171  *     - Renamed re_process_buffer to pcrs_filter_response
172  *     - pcrs_filter_response now returns NULL on failiure
173  *     - Removed all #ifdef PCRS
174  *
175  *    Revision 1.20  2001/07/01 17:01:04  oes
176  *    Added comments and missing return statement in is_untrusted_url()
177  *
178  *    Revision 1.19  2001/06/29 21:45:41  oes
179  *    Indentation, CRLF->LF, Tab-> Space
180  *
181  *    Revision 1.18  2001/06/29 13:27:38  oes
182  *    - Cleaned up, renamed and reorderd functions
183  *      and improved comments
184  *
185  *    - block_url:
186  *      - Ported to CGI platform. Now delivers
187  *        http_response or NULL
188  *      - Unified HTML and GIF generation (moved image detection
189  *        and GIF generation here from jcc.c:chat())
190  *      - Fixed HTTP status to:
191  *       -  403 (Forbidden) for the "blocked" HTML message
192  *       -  200 (OK) for GIF answers
193  *       -  302 (Redirect) for redirect to GIF
194  *
195  *    - trust_url:
196  *      - Ported to CGI platform. Now delivers
197  *        http_response or NULL
198  *      - Separated detection of untrusted URL into
199  *        (bool)is_untrusted_url
200  *      - Added enforcement of untrusted requests
201  *
202  *    - Moved redirect_url() from cgi.c to here
203  *      and ported it to the CGI platform
204  *
205  *    - Removed logentry from cancelled commit
206  *
207  *    Revision 1.17  2001/06/09 10:55:28  jongfoster
208  *    Changing BUFSIZ ==> BUFFER_SIZE
209  *
210  *    Revision 1.16  2001/06/07 23:10:26  jongfoster
211  *    Allowing unanchored domain patterns to back off and retry
212  *    if they partially match.  Optimized right-anchored patterns.
213  *    Moving ACL and forward files into config file.
214  *    Replacing struct gateway with struct forward_spec
215  *
216  *    Revision 1.15  2001/06/03 19:12:00  oes
217  *    extracted-CGI relevant stuff
218  *
219  *    Revision 1.14  2001/06/01 10:30:55  oes
220  *    Added optional left-anchoring to domaincmp
221  *
222  *    Revision 1.13  2001/05/31 21:21:30  jongfoster
223  *    Permissionsfile / actions file changes:
224  *    - Changed "permission" to "action" throughout
225  *    - changes to file format to allow string parameters
226  *    - Moved helper functions to actions.c
227  *
228  *    Revision 1.12  2001/05/31 17:35:20  oes
229  *
230  *     - Enhanced domain part globbing with infix and prefix asterisk
231  *       matching and optional unanchored operation
232  *
233  *    Revision 1.11  2001/05/29 11:53:23  oes
234  *    "See why" link added to "blocked" page
235  *
236  *    Revision 1.10  2001/05/29 09:50:24  jongfoster
237  *    Unified blocklist/imagelist/permissionslist.
238  *    File format is still under discussion, but the internal changes
239  *    are (mostly) done.
240  *
241  *    Also modified interceptor behaviour:
242  *    - We now intercept all URLs beginning with one of the following
243  *      prefixes (and *only* these prefixes):
244  *        * http://i.j.b/
245  *        * http://ijbswa.sf.net/config/
246  *        * http://ijbswa.sourceforge.net/config/
247  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
248  *    - Internal changes so that intercepted and fast redirect pages
249  *      are not replaced with an image.
250  *    - Interceptors now have the option to send a binary page direct
251  *      to the client. (i.e. ijb-send-banner uses this)
252  *    - Implemented show-url-info interceptor.  (Which is why I needed
253  *      the above interceptors changes - a typical URL is
254  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
255  *      The previous mechanism would not have intercepted that, and
256  *      if it had been intercepted then it then it would have replaced
257  *      it with an image.)
258  *
259  *    Revision 1.9  2001/05/27 22:17:04  oes
260  *
261  *    - re_process_buffer no longer writes the modified buffer
262  *      to the client, which was very ugly. It now returns the
263  *      buffer, which it is then written by chat.
264  *
265  *    - content_length now adjusts the Content-Length: header
266  *      for modified documents rather than crunch()ing it.
267  *      (Length info in csp->content_length, which is 0 for
268  *      unmodified documents)
269  *
270  *    - For this to work, sed() is called twice when filtering.
271  *
272  *    Revision 1.8  2001/05/26 17:13:28  jongfoster
273  *    Filled in a function comment.
274  *
275  *    Revision 1.7  2001/05/26 15:26:15  jongfoster
276  *    ACL feature now provides more security by immediately dropping
277  *    connections from untrusted hosts.
278  *
279  *    Revision 1.6  2001/05/26 00:28:36  jongfoster
280  *    Automatic reloading of config file.
281  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
282  *    Most of the global variables have been moved to a new
283  *    struct configuration_spec, accessed through csp->config->globalname
284  *    Most of the globals remaining are used by the Win32 GUI.
285  *
286  *    Revision 1.5  2001/05/25 22:34:30  jongfoster
287  *    Hard tabs->Spaces
288  *
289  *    Revision 1.4  2001/05/22 18:46:04  oes
290  *
291  *    - Enabled filtering banners by size rather than URL
292  *      by adding patterns that replace all standard banner
293  *      sizes with the "Junkbuster" gif to the re_filterfile
294  *
295  *    - Enabled filtering WebBugs by providing a pattern
296  *      which kills all 1x1 images
297  *
298  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
299  *      which is selected by the (nonstandard and therefore
300  *      capital) letter 'U' in the option string.
301  *      It causes the quantifiers to be ungreedy by default.
302  *      Appending a ? turns back to greedy (!).
303  *
304  *    - Added a new interceptor ijb-send-banner, which
305  *      sends back the "Junkbuster" gif. Without imagelist or
306  *      MSIE detection support, or if tinygif = 1, or the
307  *      URL isn't recognized as an imageurl, a lame HTML
308  *      explanation is sent instead.
309  *
310  *    - Added new feature, which permits blocking remote
311  *      script redirects and firing back a local redirect
312  *      to the browser.
313  *      The feature is conditionally compiled, i.e. it
314  *      can be disabled with --disable-fast-redirects,
315  *      plus it must be activated by a "fast-redirects"
316  *      line in the config file, has its own log level
317  *      and of course wants to be displayed by show-proxy-args
318  *      Note: Boy, all the #ifdefs in 1001 locations and
319  *      all the fumbling with configure.in and acconfig.h
320  *      were *way* more work than the feature itself :-(
321  *
322  *    - Because a generic redirect template was needed for
323  *      this, tinygif = 3 now uses the same.
324  *
325  *    - Moved GIFs, and other static HTTP response templates
326  *      to project.h
327  *
328  *    - Some minor fixes
329  *
330  *    - Removed some >400 CRs again (Jon, you really worked
331  *      a lot! ;-)
332  *
333  *    Revision 1.3  2001/05/20 16:44:47  jongfoster
334  *    Removing last hardcoded JunkBusters.com URLs.
335  *
336  *    Revision 1.2  2001/05/20 01:21:20  jongfoster
337  *    Version 2.9.4 checkin.
338  *    - Merged popupfile and cookiefile, and added control over PCRS
339  *      filtering, in new "permissionsfile".
340  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
341  *      file error you now get a message box (in the Win32 GUI) rather
342  *      than the program exiting with no explanation.
343  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
344  *      skipping.
345  *    - Removed tabs from "config"
346  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
347  *    - Bumped up version number.
348  *
349  *    Revision 1.1.1.1  2001/05/15 13:58:52  oes
350  *    Initial import of version 2.9.3 source tree
351  *
352  *
353  *********************************************************************/
354 \f
355
356 #include "config.h"
357
358 #include <stdio.h>
359 #include <sys/types.h>
360 #include <stdlib.h>
361 #include <ctype.h>
362 #include <string.h>
363 #include <assert.h>
364
365 #ifndef _WIN32
366 #ifndef __OS2__
367 #include <unistd.h>
368 #endif /* ndef __OS2__ */
369 #include <netinet/in.h>
370 #else
371 #include <winsock2.h>
372 #endif /* ndef _WIN32 */
373
374 #ifdef __OS2__
375 #include <utils.h>
376 #endif /* def __OS2__ */
377
378 #include "project.h"
379 #include "filters.h"
380 #include "encode.h"
381 #include "parsers.h"
382 #include "ssplit.h"
383 #include "errlog.h"
384 #include "jbsockets.h"
385 #include "miscutil.h"
386 #include "actions.h"
387 #include "cgi.h"
388 #include "list.h"
389 #include "deanimate.h"
390 #include "urlmatch.h"
391
392 #ifdef _WIN32
393 #include "win32.h"
394 #endif
395
396 const char filters_h_rcs[] = FILTERS_H_VERSION;
397
398 /* Fix a problem with Solaris.  There should be no effect on other
399  * platforms.
400  * Solaris's isspace() is a macro which uses it's argument directly
401  * as an array index.  Therefore we need to make sure that high-bit
402  * characters generate +ve values, and ideally we also want to make
403  * the argument match the declared parameter type of "int".
404  */
405 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
406
407
408 #ifdef FEATURE_ACL
409 /*********************************************************************
410  *
411  * Function    :  block_acl
412  *
413  * Description :  Block this request?
414  *                Decide yes or no based on ACL file.
415  *
416  * Parameters  :
417  *          1  :  dst = The proxy or gateway address this is going to.
418  *                      Or NULL to check all possible targets.
419  *          2  :  csp = Current client state (buffers, headers, etc...)
420  *                      Also includes the client IP address.
421  *
422  * Returns     : 0 = FALSE (don't block) and 1 = TRUE (do block)
423  *
424  *********************************************************************/
425 int block_acl(struct access_control_addr *dst, struct client_state *csp)
426 {
427    struct access_control_list *acl = csp->config->acl;
428
429    /* if not using an access control list, then permit the connection */
430    if (acl == NULL)
431    {
432       return(0);
433    }
434
435    /* search the list */
436    while (acl != NULL)
437    {
438       if ((csp->ip_addr_long & acl->src->mask) == acl->src->addr)
439       {
440          if (dst == NULL)
441          {
442             /* Just want to check if they have any access */
443             if (acl->action == ACL_PERMIT)
444             {
445                return(0);
446             }
447          }
448          else if ( ((dst->addr & acl->dst->mask) == acl->dst->addr)
449            && ((dst->port == acl->dst->port) || (acl->dst->port == 0)))
450          {
451             if (acl->action == ACL_PERMIT)
452             {
453                return(0);
454             }
455             else
456             {
457                return(1);
458             }
459          }
460       }
461       acl = acl->next;
462    }
463
464    return(1);
465
466 }
467
468
469 /*********************************************************************
470  *
471  * Function    :  acl_addr
472  *
473  * Description :  Called from `load_config' to parse an ACL address.
474  *
475  * Parameters  :
476  *          1  :  aspec = String specifying ACL address.
477  *          2  :  aca = struct access_control_addr to fill in.
478  *
479  * Returns     :  0 => Ok, everything else is an error.
480  *
481  *********************************************************************/
482 int acl_addr(char *aspec, struct access_control_addr *aca)
483 {
484    int i, masklength, port;
485    char *p;
486
487    masklength = 32;
488    port       =  0;
489
490    if ((p = strchr(aspec, '/')))
491    {
492       *p++ = '\0';
493
494       if (ijb_isdigit(*p) == 0)
495       {
496          return(-1);
497       }
498       masklength = atoi(p);
499    }
500
501    if ((masklength < 0) || (masklength > 32))
502    {
503       return(-1);
504    }
505
506    if ((p = strchr(aspec, ':')))
507    {
508       *p++ = '\0';
509
510       if (ijb_isdigit(*p) == 0)
511       {
512          return(-1);
513       }
514       port = atoi(p);
515    }
516
517    aca->port = port;
518
519    aca->addr = ntohl(resolve_hostname_to_ip(aspec));
520
521    if (aca->addr == INADDR_NONE)
522    {
523       return(-1);
524    }
525
526    /* build the netmask */
527    aca->mask = 0;
528    for (i=1; i <= masklength ; i++)
529    {
530       aca->mask |= (1 << (32 - i));
531    }
532
533    /* now mask off the host portion of the ip address
534     * (i.e. save on the network portion of the address).
535     */
536    aca->addr = aca->addr & aca->mask;
537
538    return(0);
539
540 }
541 #endif /* def FEATURE_ACL */
542
543
544 /*********************************************************************
545  *
546  * Function    :  match_portlist
547  *
548  * Description :  Check if a given number is covered by a comma
549  *                separated list of numbers and ranges (a,b-c,d,..)
550  *
551  * Parameters  :
552  *          1  :  portlist = String with list
553  *          2  :  port = port to check
554  *
555  * Returns     :  0 => no match
556  *                1 => match
557  *
558  *********************************************************************/
559 int match_portlist(const char *portlist, int port)
560 {
561    char *min, *max, *next, *portlist_copy;
562
563    min = next = portlist_copy = strdup(portlist);
564
565    /*
566     * Zero-terminate first item and remember offset for next
567     */
568    if (NULL != (next = strchr(portlist_copy, (int) ',')))
569    {
570       *next++ = '\0';
571    }
572
573    /*
574     * Loop through all items, checking for match
575     */
576    while(min)
577    {
578       if (NULL == (max = strchr(min, (int) '-')))
579       {
580          /*
581           * No dash, check for equality
582           */
583          if (port == atoi(min))
584          {
585             free(portlist_copy);
586             return(1);
587          }
588       }
589       else
590       {
591          /*
592           * This is a range, so check if between min and max,
593           * or, if max was omitted, between min and 65K
594           */
595          *max++ = '\0';
596          if(port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
597          {
598             free(portlist_copy);
599             return(1);
600          }
601
602       }
603
604       /*
605        * Jump to next item
606        */
607       min = next;
608
609       /*
610        * Zero-terminate next item and remember offset for n+1
611        */
612       if ((NULL != next) && (NULL != (next = strchr(next, (int) ','))))
613       {
614          *next++ = '\0';
615       }
616    }
617
618    free(portlist_copy);
619    return 0;
620
621 }
622
623
624 /*********************************************************************
625  *
626  * Function    :  block_url
627  *
628  * Description :  Called from `chat'.  Check to see if we need to block this.
629  *
630  * Parameters  :
631  *          1  :  csp = Current client state (buffers, headers, etc...)
632  *
633  * Returns     :  NULL => unblocked, else HTTP block response
634  *
635  *********************************************************************/
636 struct http_response *block_url(struct client_state *csp)
637 {
638 #ifdef FEATURE_IMAGE_BLOCKING
639    char *p;
640 #endif /* def FEATURE_IMAGE_BLOCKING */
641    struct http_response *rsp;
642
643    /*
644     * If it's not blocked, don't block it ;-)
645     */
646    if ((csp->action->flags & ACTION_BLOCK) == 0)
647    {
648       return NULL;
649    }
650
651    /*
652     * Else, prepare a response
653     */
654    if (NULL == (rsp = alloc_http_response()))
655    {
656       return cgi_error_memory();
657    }
658
659    /*
660     * If it's an image-url, send back an image or redirect
661     * as specified by the relevant +image action
662     */
663 #ifdef FEATURE_IMAGE_BLOCKING
664    if (((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
665         && is_imageurl(csp))
666    {
667       /* determine HOW images should be blocked */
668       p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
669
670       /* and handle accordingly: */
671       if ((p == NULL) || (0 == strcmpic(p, "logo")))
672       {
673          rsp->body = bindup(image_logo_data, image_logo_length);
674          if (rsp->body == NULL)
675          {
676             free_http_response(rsp);
677             return cgi_error_memory();
678          }
679          rsp->content_length = image_logo_length;
680
681          if (enlist_unique_header(rsp->headers, "Content-Type", BUILTIN_IMAGE_MIMETYPE))
682          {
683             free_http_response(rsp);
684             return cgi_error_memory();
685          }
686       }
687
688       else if (0 == strcmpic(p, "blank"))
689       {
690          rsp->body = bindup(image_blank_data, image_blank_length);
691          if (rsp->body == NULL)
692          {
693             free_http_response(rsp);
694             return cgi_error_memory();
695          }
696          rsp->content_length = image_blank_length;
697
698          if (enlist_unique_header(rsp->headers, "Content-Type", BUILTIN_IMAGE_MIMETYPE))
699          {
700             free_http_response(rsp);
701             return cgi_error_memory();
702          }
703       }
704
705       else if (0 == strcmpic(p, "pattern"))
706       {
707          rsp->body = bindup(image_pattern_data, image_pattern_length);
708          if (rsp->body == NULL)
709          {
710             free_http_response(rsp);
711             return cgi_error_memory();
712          }
713          rsp->content_length = image_pattern_length;
714
715          if (enlist_unique_header(rsp->headers, "Content-Type", BUILTIN_IMAGE_MIMETYPE))
716          {
717             free_http_response(rsp);
718             return cgi_error_memory();
719          }
720       }
721
722       else
723       {
724          rsp->status = strdup("302 Local Redirect from Junkbuster");
725          if (rsp->status == NULL)
726          {
727             free_http_response(rsp);
728             return cgi_error_memory();
729          }
730
731          if (enlist_unique_header(rsp->headers, "Location", p))
732          {
733             free_http_response(rsp);
734             return cgi_error_memory();
735          }
736       }
737    }
738    else
739 #endif /* def FEATURE_IMAGE_BLOCKING */
740
741    /*
742     * Else, generate an HTML "blocked" message:
743     */
744    {
745       jb_err err;
746       struct map * exports;
747
748       /*
749        * Workaround for stupid Netscape bug which prevents
750        * pages from being displayed if loading a referenced
751        * JavaScript or style sheet fails. So make it appear
752        * as if it succeeded.
753        */
754       if ( NULL != (p = get_header_value(csp->headers, "User-Agent:"))
755            && !strncmpic(p, "mozilla", 7) /* Catch Netscape but */
756            && !strstr(p, "Gecko")         /* save Mozilla, */
757            && !strstr(p, "compatible")    /* MSIE */
758            && !strstr(p, "Opera"))        /* and Opera. */
759       {
760          rsp->status = strdup("200 Request for blocked URL");
761       }
762       else
763       {
764          rsp->status = strdup("404 Request for blocked URL");
765       }
766
767       if (rsp->status == NULL)
768       {
769          free_http_response(rsp);
770          return cgi_error_memory();
771       }
772
773       exports = default_exports(csp, NULL);
774       if (exports == NULL)
775       {
776          free_http_response(rsp);
777          return cgi_error_memory();
778       }
779
780 #ifdef FEATURE_FORCE_LOAD
781       err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
782 #else /* ifndef FEATURE_FORCE_LOAD */
783       err = map_block_killer(exports, "force-support");
784 #endif /* ndef FEATURE_FORCE_LOAD */
785
786       if (!err) err = map(exports, "hostport", 1, html_encode(csp->http->hostport), 0);
787       if (!err) err = map(exports, "path", 1, html_encode(csp->http->path), 0);
788
789       if (err)
790       {
791          free_map(exports);
792          free_http_response(rsp);
793          return cgi_error_memory();
794       }
795
796       err = template_fill_for_cgi(csp, "blocked", exports, rsp);
797       if (err)
798       {
799          free_http_response(rsp);
800          return cgi_error_memory();
801       }
802    }
803
804    return finish_http_response(rsp);
805
806 }
807
808
809 #ifdef FEATURE_TRUST
810 /*********************************************************************
811  *
812  * Function    :  trust_url FIXME: I should be called distrust_url
813  *
814  * Description :  Calls is_untrusted_url to determine if the URL is trusted
815  *                and if not, returns a HTTP 304 response with a reject message.
816  *
817  * Parameters  :
818  *          1  :  csp = Current client state (buffers, headers, etc...)
819  *
820  * Returns     :  NULL => trusted, else http_response.
821  *
822  *********************************************************************/
823 struct http_response *trust_url(struct client_state *csp)
824 {
825    struct http_response *rsp;
826    struct map * exports;
827    char buf[BUFFER_SIZE];
828    char *p;
829    struct url_spec **tl;
830    struct url_spec *t;
831    jb_err err;
832
833    /*
834     * Don't bother to work on trusted URLs
835     */
836    if (!is_untrusted_url(csp))
837    {
838       return NULL;
839    }
840
841    /*
842     * Else, prepare a response:
843     */
844    if (NULL == (rsp = alloc_http_response()))
845    {
846       return cgi_error_memory();
847    }
848
849    exports = default_exports(csp, NULL);
850    if (exports == NULL)
851    {
852       free_http_response(rsp);
853       return cgi_error_memory();
854    }
855
856    /*
857     * Export the host, port, and referrer information
858     */
859    err = map(exports, "hostport", 1, csp->http->hostport, 1);
860    if (!err) err = map(exports, "path", 1, csp->http->path, 1);
861
862    if (NULL != (p = get_header_value(csp->headers, "Referer:")))
863    {
864       if (!err) err = map(exports, "referrer", 1, html_encode(p), 0);
865    }
866    else
867    {
868       if (!err) err = map(exports, "referrer", 1, "unknown", 1);
869    }
870
871    if (err)
872    {
873       free_map(exports);
874       free_http_response(rsp);
875       return cgi_error_memory();
876    }
877
878    /*
879     * Export the trust list
880     */
881    p = strdup("");
882    for (tl = csp->config->trust_list; (t = *tl) ; tl++)
883    {
884       sprintf(buf, "<li>%s</li>\n", t->spec);
885       string_append(&p, buf);
886    }
887    err = map(exports, "trusted-referrers", 1, p, 0);
888
889    if (err)
890    {
891       free_map(exports);
892       free_http_response(rsp);
893       return cgi_error_memory();
894    }
895
896    /*
897     * Export the trust info, if available
898     */
899    if (csp->config->trust_info->first)
900    {
901       struct list_entry *l;
902
903       p = strdup("");
904       for (l = csp->config->trust_info->first; l ; l = l->next)
905       {
906          sprintf(buf, "<li> <a href=%s>%s</a><br>\n",l->str, l->str);
907          string_append(&p, buf);
908       }
909       err = map(exports, "trust-info", 1, p, 0);
910    }
911    else
912    {
913       err = map_block_killer(exports, "have-trust-info");
914    }
915
916    if (err)
917    {
918       free_map(exports);
919       free_http_response(rsp);
920       return cgi_error_memory();
921    }
922
923    /*
924     * Export the force prefix or the force conditional block killer
925     */
926 #ifdef FEATURE_FORCE_LOAD
927    err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
928 #else /* ifndef FEATURE_FORCE_LOAD */
929    err = map_block_killer(exports, "force-support");
930 #endif /* ndef FEATURE_FORCE_LOAD */
931
932    if (err)
933    {
934       free_map(exports);
935       free_http_response(rsp);
936       return cgi_error_memory();
937    }
938
939    /*
940     * Build the response
941     */
942    err = template_fill_for_cgi(csp, "untrusted", exports, rsp);
943    if (err)
944    {
945       free_http_response(rsp);
946       return cgi_error_memory();
947    }
948
949    return finish_http_response(rsp);
950 }
951 #endif /* def FEATURE_TRUST */
952
953
954 #ifdef FEATURE_FAST_REDIRECTS
955 /*********************************************************************
956  *
957  * Function    :  redirect_url
958  *
959  * Description :  Checks for redirection URLs and returns a HTTP redirect
960  *                to the destination URL, if necessary
961  *
962  * Parameters  :
963  *          1  :  csp = Current client state (buffers, headers, etc...)
964  *
965  * Returns     :  NULL if URL was clean, HTTP redirect otherwise.
966  *
967  *********************************************************************/
968 struct http_response *redirect_url(struct client_state *csp)
969 {
970    char *p, *q;
971    struct http_response *rsp;
972
973    p = q = csp->http->path;
974    log_error(LOG_LEVEL_REDIRECTS, "checking path for redirects: %s", p);
975
976    /*
977     * find the last URL encoded in the request
978     */
979    while ((p = strstr(p, "http://")))
980    {
981       q = p++;
982    }
983
984    /*
985     * if there was any, generate and return a HTTP redirect
986     */
987    if (q != csp->http->path)
988    {
989       log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
990
991       if (NULL == (rsp = alloc_http_response()))
992       {
993          return cgi_error_memory();
994       }
995
996       if ( enlist_unique_header(rsp->headers, "Location", q)
997         || (NULL == (rsp->status = strdup("302 Local Redirect from Junkbuster"))) )
998       {
999          free_http_response(rsp);
1000          return cgi_error_memory();
1001       }
1002
1003       return finish_http_response(rsp);
1004    }
1005    else
1006    {
1007       return NULL;
1008    }
1009
1010 }
1011 #endif /* def FEATURE_FAST_REDIRECTS */
1012
1013
1014 #ifdef FEATURE_IMAGE_BLOCKING
1015 /*********************************************************************
1016  *
1017  * Function    :  is_imageurl
1018  *
1019  * Description :  Given a URL, decide whether it is an image or not,
1020  *                using either the info from a previous +image action
1021  *                or, #ifdef FEATURE_IMAGE_DETECT_MSIE, the info from
1022  *                the browser's accept header.
1023  *
1024  * Parameters  :
1025  *          1  :  csp = Current client state (buffers, headers, etc...)
1026  *
1027  * Returns     :  True (nonzero) if URL is an image, false (0)
1028  *                otherwise
1029  *
1030  *********************************************************************/
1031 int is_imageurl(struct client_state *csp)
1032 {
1033 #ifdef FEATURE_IMAGE_DETECT_MSIE
1034    char *tmp;
1035
1036    tmp = get_header_value(csp->headers, "User-Agent:");
1037    if (tmp && strstr(tmp, "MSIE"))
1038    {
1039       tmp = get_header_value(csp->headers, "Accept:");
1040       if (tmp && strstr(tmp, "image/gif"))
1041       {
1042          /* Client will accept HTML.  If this seems counterintuitive,
1043           * blame Microsoft.
1044           */
1045          return(0);
1046       }
1047       else
1048       {
1049          return(1);
1050       }
1051    }
1052 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1053
1054    return ((csp->action->flags & ACTION_IMAGE) != 0);
1055
1056 }
1057 #endif /* def FEATURE_IMAGE_BLOCKING */
1058
1059
1060 #ifdef FEATURE_COOKIE_JAR
1061 /*********************************************************************
1062  *
1063  * Function    :  is_untrusted_url
1064  *
1065  * Description :  Should we "distrust" this URL (and block it)?
1066  *
1067  *                Yes if it matches a line in the trustfile, or if the
1068  *                    referrer matches a line starting with "+" in the
1069  *                    trustfile.
1070  *                No  otherwise.
1071  *
1072  * Parameters  :
1073  *          1  :  csp = Current client state (buffers, headers, etc...)
1074  *
1075  * Returns     :  0 => trusted, 1 => untrusted
1076  *
1077  *********************************************************************/
1078 int is_untrusted_url(struct client_state *csp)
1079 {
1080    struct file_list *fl;
1081    struct block_spec *b;
1082    struct url_spec **trusted_url;
1083    struct http_request rhttp[1];
1084    const char * referer;
1085    jb_err err;
1086
1087    /*
1088     * If we don't have a trustlist, we trust everybody
1089     */
1090    if (((fl = csp->tlist) == NULL) || ((b  = fl->f) == NULL))
1091    {
1092       return 0;
1093    }
1094
1095    memset(rhttp, '\0', sizeof(*rhttp));
1096
1097    /*
1098     * Do we trust the request URL itself?
1099     */
1100    for (b = b->next; b ; b = b->next)
1101    {
1102       if (url_match(b->url, csp->http))
1103       {
1104          return b->reject;
1105       }
1106    }
1107
1108    if (NULL == (referer = get_header_value(csp->headers, "Referer:")))
1109    {
1110       /* no referrer was supplied */
1111       return 1;
1112    }
1113
1114    /*
1115     * If not, do we maybe trust its referrer?
1116     */
1117
1118
1119    /*
1120     * Parse the URL from the referrer
1121     */
1122
1123    err = parse_http_url(referer, rhttp, csp);
1124    if (err)
1125    {
1126       return 1;
1127    }
1128
1129    for (trusted_url = csp->config->trust_list; *trusted_url != NULL; trusted_url++)
1130    {
1131       if (url_match(*trusted_url, rhttp))
1132       {
1133          /* if the URL's referrer is from a trusted referrer, then
1134           * add the target spec to the trustfile as an unblocked
1135           * domain and return NULL (which means it's OK).
1136           */
1137
1138          FILE *fp;
1139
1140          if ((fp = fopen(csp->config->trustfile, "a")))
1141          {
1142             char * path;
1143             char * path_end;
1144             char * new_entry = strdup("~");
1145
1146             string_append(&new_entry, csp->http->hostport);
1147
1148             path = csp->http->path;
1149             if ( (path[0] == '/')
1150               && (path[1] == '~')
1151               && ((path_end = strchr(path + 2, '/')) != NULL))
1152             {
1153                /* since this path points into a user's home space
1154                 * be sure to include this spec in the trustfile.
1155                 */
1156                int path_len = path_end - path; /* save offset */
1157                path = strdup(path); /* Copy string */
1158                if (path != NULL)
1159                {
1160                   path_end = path + path_len; /* regenerate ptr to new buffer */
1161                   *(path_end + 1) = '\0'; /* Truncate path after '/' */
1162                }
1163                string_join(&new_entry, path);
1164             }
1165
1166             if (new_entry != NULL)
1167             {
1168                fprintf(fp, "%s\n", new_entry);
1169                free(new_entry);
1170             }
1171             else
1172             {
1173                /* FIXME: No way to handle out-of memory, so mostly ignoring it */
1174                log_error(LOG_LEVEL_ERROR, "Out of memory adding pattern to trust file");
1175             }
1176
1177             fclose(fp);
1178          }
1179          return 0;
1180       }
1181    }
1182    return 1;
1183 }
1184 #endif /* def FEATURE_COOKIE_JAR */
1185
1186
1187 /*********************************************************************
1188  *
1189  * Function    :  pcrs_filter_response
1190  *
1191  * Description :  Apply all the pcrs jobs from the joblist (re_filterfile)
1192  *                to the text buffer that's been accumulated in
1193  *                csp->iob->buf and set csp->content_length to the modified
1194  *                size and raise the CSP_FLAG_MODIFIED flag if appropriate.
1195  *
1196  * Parameters  :
1197  *          1  :  csp = Current client state (buffers, headers, etc...)
1198  *
1199  * Returns     :  a pointer to the (newly allocated) modified buffer.
1200  *                or NULL in case something went wrong
1201  *
1202  *********************************************************************/
1203 char *pcrs_filter_response(struct client_state *csp)
1204 {
1205    int hits=0;
1206    size_t size;
1207
1208    char *old = csp->iob->cur, *new = NULL;
1209    pcrs_job *job;
1210
1211    struct file_list *fl;
1212    struct re_filterfile_spec *b;
1213
1214    /* Sanity first */
1215    if (csp->iob->cur >= csp->iob->eod)
1216    {
1217       return(NULL);
1218    }
1219    size = csp->iob->eod - csp->iob->cur;
1220
1221    /*
1222     * If the body has a "chunked" transfer-encoding,
1223     * get rid of it first, adjusting size and iob->eod
1224     */
1225    if (csp->flags & CSP_FLAG_CHUNKED)
1226    {
1227       log_error(LOG_LEVEL_RE_FILTER, "Need to de-chunk first");
1228       if (0 == (size = remove_chunked_transfer_coding(csp->iob->cur, size)))
1229       {
1230          return(NULL);
1231       }
1232       csp->iob->eod = csp->iob->cur + size;
1233       csp->flags |= CSP_FLAG_MODIFIED;
1234    }
1235
1236    if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
1237    {
1238       log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
1239       return(NULL);
1240    }
1241
1242    if ( NULL == b->joblist )
1243    {
1244       log_error(LOG_LEVEL_RE_FILTER, "Empty joblist. Nothing to do.");
1245       return(NULL);
1246    }
1247
1248    log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
1249               csp->http->hostport, csp->http->path, size);
1250
1251    /* Apply all jobs from the joblist */
1252    for (job = b->joblist; NULL != job; job = job->next)
1253    {
1254       hits += pcrs_execute(job, old, size, &new, &size);
1255       if (old != csp->iob->cur) free(old);
1256       old=new;
1257    }
1258
1259    log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
1260
1261    /*
1262     * If there were no hits, destroy our copy and let
1263     * chat() use the original in csp->iob
1264     */
1265    if (!hits)
1266    {
1267       free(new);
1268       return(NULL);
1269    }
1270
1271    csp->flags |= CSP_FLAG_MODIFIED;
1272    csp->content_length = size;
1273    IOB_RESET(csp);
1274
1275    return(new);
1276
1277 }
1278
1279
1280 /*********************************************************************
1281  *
1282  * Function    :  gif_deanimate_response
1283  *
1284  * Description :  Deanimate the GIF image that has been accumulated in
1285  *                csp->iob->buf, set csp->content_length to the modified
1286  *                size and raise the CSP_FLAG_MODIFIED flag.
1287  *
1288  * Parameters  :
1289  *          1  :  csp = Current client state (buffers, headers, etc...)
1290  *
1291  * Returns     :  a pointer to the (newly allocated) modified buffer.
1292  *                or NULL in case something went wrong.
1293  *
1294  *********************************************************************/
1295 char *gif_deanimate_response(struct client_state *csp)
1296 {
1297    struct binbuffer *in, *out;
1298    char *p;
1299    size_t size = csp->iob->eod - csp->iob->cur;
1300
1301    /*
1302     * If the body has a "chunked" transfer-encoding,
1303     * get rid of it first, adjusting size and iob->eod
1304     */
1305    if (csp->flags & CSP_FLAG_CHUNKED)
1306    {
1307       log_error(LOG_LEVEL_DEANIMATE, "Need to de-chunk first");
1308       if (0 == (size = remove_chunked_transfer_coding(csp->iob->cur, size)))
1309       {
1310          return(NULL);
1311       }
1312       csp->iob->eod = csp->iob->cur + size;
1313       csp->flags |= CSP_FLAG_MODIFIED;
1314    }
1315
1316    if (  (NULL == (in =  (struct binbuffer *)zalloc(sizeof *in )))
1317       || (NULL == (out = (struct binbuffer *)zalloc(sizeof *out))) )
1318    {
1319       log_error(LOG_LEVEL_DEANIMATE, "failed! (no mem)");
1320       return NULL;
1321    }
1322
1323    in->buffer = csp->iob->cur;
1324    in->size = size;
1325
1326    if (gif_deanimate(in, out, strncmp("last", csp->action->string[ACTION_STRING_DEANIMATE], 4)))
1327    {
1328       log_error(LOG_LEVEL_DEANIMATE, "failed! (gif parsing)");
1329       free(in);
1330       buf_free(out);
1331       return(NULL);
1332    }
1333    else
1334    {
1335       log_error(LOG_LEVEL_DEANIMATE, "Success! GIF shrunk from %d bytes to %d.", size, out->offset);
1336       csp->content_length = out->offset;
1337       csp->flags |= CSP_FLAG_MODIFIED;
1338       p = out->buffer;
1339       free(in);
1340       free(out);
1341       return(p);
1342    }
1343
1344 }
1345
1346
1347 /*********************************************************************
1348  *
1349  * Function    :  remove_chunked_transfer_coding
1350  *
1351  * Description :  In-situ remove the "chunked" transfer coding as defined
1352  *                in rfc2616 from a buffer.
1353  *
1354  * Parameters  :
1355  *          1  :  buffer = Pointer to the text buffer
1356  *          2  :  size = Number of bytes to be processed
1357  *
1358  * Returns     :  The new size, i.e. the number of bytes from buffer which
1359  *                are occupied by the stripped body, or 0 in case something
1360  *                went wrong
1361  *
1362  *********************************************************************/
1363 int remove_chunked_transfer_coding(char *buffer, const size_t size)
1364 {
1365    size_t newsize = 0;
1366    unsigned int chunksize = 0;
1367    char *from_p, *to_p;
1368
1369    assert(buffer);
1370    from_p = to_p = buffer;
1371
1372    if (sscanf(buffer, "%x", &chunksize) != 1)
1373    {
1374       log_error(LOG_LEVEL_ERROR, "Invalid first chunksize while stripping \"chunked\" transfer coding");
1375       return(0);
1376    }
1377
1378    while (chunksize > 0)
1379    {
1380       if (NULL == (from_p = strstr(from_p, "\r\n")))
1381       {
1382          log_error(LOG_LEVEL_ERROR, "Parse error while stripping \"chunked\" transfer coding");
1383          return(0);
1384       }
1385       newsize += chunksize;
1386       from_p += 2;
1387
1388       memmove(to_p, from_p, (size_t) chunksize);
1389       to_p = buffer + newsize;
1390       from_p += chunksize + 2;
1391
1392       if (sscanf(from_p, "%x", &chunksize) != 1)
1393       {
1394          log_error(LOG_LEVEL_ERROR, "Parse error while stripping \"chunked\" transfer coding");
1395          return(0);
1396       }
1397    }
1398
1399    /* FIXME: Should this get its own loglevel? */
1400    log_error(LOG_LEVEL_RE_FILTER, "De-chunking successful. Shrunk from %d to %d\n", size, newsize);
1401    return(newsize);
1402
1403 }
1404
1405
1406 /*********************************************************************
1407  *
1408  * Function    :  url_actions
1409  *
1410  * Description :  Gets the actions for this URL.
1411  *
1412  * Parameters  :
1413  *          1  :  http = http_request request for blocked URLs
1414  *          2  :  csp = Current client state (buffers, headers, etc...)
1415  *
1416  * Returns     :  N/A
1417  *
1418  *********************************************************************/
1419 void url_actions(struct http_request *http,
1420                  struct client_state *csp)
1421 {
1422    struct file_list *fl;
1423    struct url_actions *b;
1424
1425    init_current_action(csp->action);
1426
1427    if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
1428    {
1429       return;
1430    }
1431
1432    apply_url_actions(csp->action, http, b);
1433
1434 }
1435
1436
1437 /*********************************************************************
1438  *
1439  * Function    :  apply_url_actions
1440  *
1441  * Description :  Applies a list of URL actions.
1442  *
1443  * Parameters  :
1444  *          1  :  action = Destination.
1445  *          2  :  http = Current URL
1446  *          3  :  b = list of URL actions to apply
1447  *
1448  * Returns     :  N/A
1449  *
1450  *********************************************************************/
1451 void apply_url_actions(struct current_action_spec *action,
1452                        struct http_request *http,
1453                        struct url_actions *b)
1454 {
1455    if (b == NULL)
1456    {
1457       /* Should never happen */
1458       return;
1459    }
1460
1461    for (b = b->next; NULL != b; b = b->next)
1462    {
1463       if (url_match(b->url, http))
1464       {
1465          merge_current_action(action, b->action);
1466       }
1467    }
1468 }
1469
1470
1471 /*********************************************************************
1472  *
1473  * Function    :  forward_url
1474  *
1475  * Description :  Should we forward this to another proxy?
1476  *
1477  * Parameters  :
1478  *          1  :  http = http_request request for current URL
1479  *          2  :  csp = Current client state (buffers, headers, etc...)
1480  *
1481  * Returns     :  Pointer to forwarding information.
1482  *
1483  *********************************************************************/
1484 const struct forward_spec * forward_url(struct http_request *http,
1485                                         struct client_state *csp)
1486 {
1487    static const struct forward_spec fwd_default[1] = { FORWARD_SPEC_INITIALIZER };
1488    struct forward_spec *fwd = csp->config->forward;
1489
1490    if (fwd == NULL)
1491    {
1492       return fwd_default;
1493    }
1494
1495    while (fwd != NULL)
1496    {
1497       if (url_match(fwd->url, http))
1498       {
1499          return fwd;
1500       }
1501       fwd = fwd->next;
1502    }
1503
1504    return fwd_default;
1505 }
1506
1507
1508 /*
1509   Local Variables:
1510   tab-width: 3
1511   end:
1512 */