new section
[privoxy.git] / project.h
1 #ifndef PROJECT_H_INCLUDED
2 #define PROJECT_H_INCLUDED
3 #define PROJECT_H_VERSION "$Id: project.h,v 1.53 2002/03/08 16:48:55 oes Exp $"
4 /*********************************************************************
5  *
6  * File        :  $Source: /cvsroot/ijbswa/current/project.h,v $
7  *
8  * Purpose     :  Defines data structures which are widely used in the
9  *                project.  Does not define any variables or functions
10  *                (though it does declare some macros).
11  *
12  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
13  *                IJBSWA team.  http://ijbswa.sourceforge.net
14  *
15  *                Based on the Internet Junkbuster originally written
16  *                by and Copyright (C) 1997 Anonymous Coders and
17  *                Junkbusters Corporation.  http://www.junkbusters.com
18  *
19  *                This program is free software; you can redistribute it
20  *                and/or modify it under the terms of the GNU General
21  *                Public License as published by the Free Software
22  *                Foundation; either version 2 of the License, or (at
23  *                your option) any later version.
24  *
25  *                This program is distributed in the hope that it will
26  *                be useful, but WITHOUT ANY WARRANTY; without even the
27  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
28  *                PARTICULAR PURPOSE.  See the GNU General Public
29  *                License for more details.
30  *
31  *                The GNU General Public License should be included with
32  *                this file.  If not, you can view it at
33  *                http://www.gnu.org/copyleft/gpl.html
34  *                or write to the Free Software Foundation, Inc., 59
35  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
36  *
37  * Revisions   :
38  *    $Log: project.h,v $
39  *    Revision 1.53  2002/03/08 16:48:55  oes
40  *    Added FEATURE_NO_GIFS and BUILTIN_IMAGE_MIMETYPE
41  *
42  *    Revision 1.52  2002/03/07 03:46:17  oes
43  *    Fixed compiler warnings
44  *
45  *    Revision 1.51  2002/03/05 04:52:42  oes
46  *    Deleted non-errlog debugging code
47  *
48  *    Revision 1.50  2002/03/04 19:32:07  oes
49  *    Changed default port to 8118
50  *
51  *    Revision 1.49  2002/03/04 18:28:55  oes
52  *    Deleted PID_FILE_NAME
53  *
54  *    Revision 1.48  2002/03/03 14:50:40  oes
55  *    Fixed CLF logging: Added ocmd member for client's request to struct http_request
56  *
57  *    Revision 1.47  2002/02/20 23:15:13  jongfoster
58  *    Parsing functions now handle out-of-memory gracefully by returning
59  *    an error code.
60  *
61  *    Revision 1.46  2002/01/17 21:06:09  jongfoster
62  *    Now #defining the URLs of the config interface
63  *
64  *    Minor changes to struct http_request and struct url_spec due to
65  *    standardizing that struct http_request is used to represent a URL, and
66  *    struct url_spec is used to represent a URL pattern.  (Before, URLs were
67  *    represented as seperate variables and a partially-filled-in url_spec).
68  *
69  *    Revision 1.45  2002/01/09 14:33:27  oes
70  *    Added HOSTENT_BUFFER_SIZE
71  *
72  *    Revision 1.44  2001/12/30 14:07:32  steudten
73  *    - Add signal handling (unix)
74  *    - Add SIGHUP handler (unix)
75  *    - Add creation of pidfile (unix)
76  *    - Add action 'top' in rc file (RH)
77  *    - Add entry 'SIGNALS' to manpage
78  *    - Add exit message to logfile (unix)
79  *
80  *    Revision 1.43  2001/11/22 21:57:51  jongfoster
81  *    Making action_spec->flags into an unsigned long rather than just an
82  *    unsigned int.
83  *    Adding ACTION_NO_COOKIE_KEEP
84  *
85  *    Revision 1.42  2001/11/05 21:42:41  steudten
86  *    Include DBG() macro.
87  *
88  *    Revision 1.41  2001/10/28 19:12:06  jongfoster
89  *    Adding ijb_toupper()
90  *
91  *    Revision 1.40  2001/10/26 17:40:47  oes
92  *    Moved ijb_isspace and ijb_tolower to project.h
93  *    Removed http->user_agent, csp->referrer and csp->accept_types
94  *
95  *    Revision 1.39  2001/10/25 03:45:02  david__schmidt
96  *    Adding a (void*) cast to freez() because Visual Age C++ won't expand the
97  *    macro when called with a cast; so moving the cast to the macro def'n
98  *    seems to both eliminate compiler warnings (on darwin and OS/2, anyway) and
99  *    doesn't make macro expansion complain.  Hope this works for everyone else
100  *    too...
101  *
102  *    Revision 1.38  2001/10/23 21:19:04  jongfoster
103  *    New error-handling support: jb_err type and JB_ERR_xxx constants
104  *    CGI functions now return a jb_err, and their parameters map is const.
105  *    Support for RUNTIME_FEATUREs to enable/disable config editor
106  *    Adding a few comments
107  *
108  *    Revision 1.37  2001/10/14 22:14:01  jongfoster
109  *    Removing name_length field from struct cgi_dispatcher, as this is
110  *    now calculated at runtime from the "name" field.
111  *
112  *    Revision 1.36  2001/10/10 16:45:15  oes
113  *    Added LIMIT_CONNECT action and string
114  *    Fixed HTTP message line termination
115  *    Added CFORBIDDEN HTTP message
116  *
117  *    Revision 1.35  2001/10/07 18:06:43  oes
118  *    Added status member to struct http_request
119  *
120  *    Revision 1.34  2001/10/07 15:45:25  oes
121  *    Added url member to struct http_request and commented all
122  *      members
123  *
124  *    Added CT_TABOO
125  *
126  *    Added ACTION_DOWNGRADE and ACTION_NO_COMPRESSION
127  *
128  *    Replaced struct client_state members rejected,
129  *      force, active and toggled_on with "flags" bitmap.
130  *
131  *    Added CSP_FLAG_MODIFIED and CSP_FLAG_CHUNKED
132  *
133  *    Added buffer_limit to struct configuration_spec
134  *
135  *    Revision 1.33  2001/09/20 13:30:08  steudten
136  *
137  *    Make freez() more secure in case of: if (exp) { free(z) ; a=*z }
138  *    Last case will set z to NULL in free(z) and thats bad..
139  *
140  *    Revision 1.32  2001/09/16 23:02:51  jongfoster
141  *    Fixing warning
142  *
143  *    Revision 1.31  2001/09/16 13:20:29  jongfoster
144  *    Rewrite of list library.  Now has seperate header and list_entry
145  *    structures.  Also added a large sprinking of assert()s to the list
146  *    code.
147  *
148  *    Revision 1.30  2001/09/13 23:52:00  jongfoster
149  *    Support for both static and dynamically generated CGI pages
150  *
151  *    Revision 1.29  2001/09/13 23:29:43  jongfoster
152  *    Defining FORWARD_SPEC_INITIALIZER
153  *
154  *    Revision 1.28  2001/09/13 23:05:50  jongfoster
155  *    Changing the string paramater to the header parsers a "const".
156  *
157  *    Revision 1.27  2001/08/05 16:06:20  jongfoster
158  *    Modifiying "struct map" so that there are now separate header and
159  *    "map_entry" structures.  This means that functions which modify a
160  *    map no longer need to return a pointer to the modified map.
161  *    Also, it no longer reverses the order of the entries (which may be
162  *    important with some advanced template substitutions).
163  *
164  *    Revision 1.26  2001/07/30 22:08:36  jongfoster
165  *    Tidying up #defines:
166  *    - All feature #defines are now of the form FEATURE_xxx
167  *    - Permanently turned off WIN_GUI_EDIT
168  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
169  *
170  *    Revision 1.25  2001/07/29 18:43:08  jongfoster
171  *    Changing #ifdef _FILENAME_H to FILENAME_H_INCLUDED, to conform to
172  *    ANSI C rules.
173  *
174  *    Revision 1.24  2001/07/25 17:20:27  oes
175  *    Introduced http->user_agent
176  *
177  *    Revision 1.23  2001/07/18 12:32:23  oes
178  *    - Added ACTION_STRING_DEANIMATE
179  *    - moved #define freez from jcc.h to project.h
180  *
181  *    Revision 1.22  2001/07/15 17:51:41  jongfoster
182  *    Renaming #define STATIC to STATIC_PCRE
183  *
184  *    Revision 1.21  2001/07/13 14:03:19  oes
185  *     - Reorganized regex header inclusion and #defines to
186  *       comply to the scheme in configure.in
187  *     - Added csp->content_type and its CT_* keys
188  *     - Added ACTION_DEANIMATE
189  *     - Removed all #ifdef PCRS
190  *
191  *    Revision 1.20  2001/06/29 21:45:41  oes
192  *    Indentation, CRLF->LF, Tab-> Space
193  *
194  *    Revision 1.19  2001/06/29 13:33:36  oes
195  *    - Improved comments
196  *    - Introduced http_request.host_ip_addr_str
197  *    - Introduced http_response.head_length
198  *    - Introduced config.my_ip_addr_str, config.my_hostname,
199  *      config.admin_address and config.proxy_info_url
200  *    - Removed config.proxy_args_header and config.proxy_args_trailer,
201  *      renamed config.proxy_args_invocation to config.proxy_args
202  *    - Removed HTML snipplets and GIFs
203  *    - Removed logentry from cancelled commit
204  *
205  *    Revision 1.18  2001/06/09 10:57:39  jongfoster
206  *    Adding definition of BUFFER_SIZE.
207  *    Changing struct cgi_dispatcher to use "const" strings.
208  *
209  *    Revision 1.17  2001/06/07 23:15:09  jongfoster
210  *    Merging ACL and forward files into config file.
211  *    Moving struct gateway members into struct forward_spec
212  *    Removing config->proxy_args_gateways
213  *    Cosmetic: Adding a few comments
214  *
215  *    Revision 1.16  2001/06/04 18:31:58  swa
216  *    files are now prefixed with either `confdir' or `logdir'.
217  *    `make redhat-dist' replaces both entries confdir and logdir
218  *    with redhat values
219  *
220  *    Revision 1.15  2001/06/04 11:28:53  swa
221  *    redirect did not work due to missing /
222  *
223  *    Revision 1.14  2001/06/03 11:03:48  oes
224  *    Added struct map,
225  *    added struct http_response,
226  *    changed struct interceptors to struct cgi_dispatcher,
227  *    moved HTML stuff to cgi.h
228  *
229  *    Revision 1.13  2001/06/01 20:05:36  jongfoster
230  *    Support for +image-blocker{}: added ACTION_IMAGE_BLOCKER
231  *    constant, and removed csp->tinygif.
232  *
233  *    Revision 1.12  2001/06/01 18:49:17  jongfoster
234  *    Replaced "list_share" with "list" - the tiny memory gain was not
235  *    worth the extra complexity.
236  *
237  *    Revision 1.11  2001/06/01 10:32:47  oes
238  *    Added constants for anchoring selection bitmap
239  *
240  *    Revision 1.10  2001/05/31 21:33:53  jongfoster
241  *    Changes for new actions file, replacing permissionsfile
242  *    and parts of the config file.  Also added support for
243  *    list_shared.
244  *
245  *    Revision 1.9  2001/05/31 17:32:31  oes
246  *
247  *     - Enhanced domain part globbing with infix and prefix asterisk
248  *       matching and optional unanchored operation
249  *
250  *    Revision 1.8  2001/05/29 20:09:15  joergs
251  *    HTTP_REDIRECT_TEMPLATE fixed.
252  *
253  *    Revision 1.7  2001/05/29 09:50:24  jongfoster
254  *    Unified blocklist/imagelist/actionslist.
255  *    File format is still under discussion, but the internal changes
256  *    are (mostly) done.
257  *
258  *    Also modified interceptor behaviour:
259  *    - We now intercept all URLs beginning with one of the following
260  *      prefixes (and *only* these prefixes):
261  *        * http://i.j.b/
262  *        * http://ijbswa.sf.net/config/
263  *        * http://ijbswa.sourceforge.net/config/
264  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
265  *    - Internal changes so that intercepted and fast redirect pages
266  *      are not replaced with an image.
267  *    - Interceptors now have the option to send a binary page direct
268  *      to the client. (i.e. ijb-send-banner uses this)
269  *    - Implemented show-url-info interceptor.  (Which is why I needed
270  *      the above interceptors changes - a typical URL is
271  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
272  *      The previous mechanism would not have intercepted that, and
273  *      if it had been intercepted then it then it would have replaced
274  *      it with an image.)
275  *
276  *    Revision 1.6  2001/05/27 22:17:04  oes
277  *
278  *    - re_process_buffer no longer writes the modified buffer
279  *      to the client, which was very ugly. It now returns the
280  *      buffer, which it is then written by chat.
281  *
282  *    - content_length now adjusts the Content-Length: header
283  *      for modified documents rather than crunch()ing it.
284  *      (Length info in csp->content_length, which is 0 for
285  *      unmodified documents)
286  *
287  *    - For this to work, sed() is called twice when filtering.
288  *
289  *    Revision 1.5  2001/05/26 00:28:36  jongfoster
290  *    Automatic reloading of config file.
291  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
292  *    Most of the global variables have been moved to a new
293  *    struct configuration_spec, accessed through csp->config->globalname
294  *    Most of the globals remaining are used by the Win32 GUI.
295  *
296  *    Revision 1.4  2001/05/22 18:46:04  oes
297  *
298  *    - Enabled filtering banners by size rather than URL
299  *      by adding patterns that replace all standard banner
300  *      sizes with the "Junkbuster" gif to the re_filterfile
301  *
302  *    - Enabled filtering WebBugs by providing a pattern
303  *      which kills all 1x1 images
304  *
305  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
306  *      which is selected by the (nonstandard and therefore
307  *      capital) letter 'U' in the option string.
308  *      It causes the quantifiers to be ungreedy by default.
309  *      Appending a ? turns back to greedy (!).
310  *
311  *    - Added a new interceptor ijb-send-banner, which
312  *      sends back the "Junkbuster" gif. Without imagelist or
313  *      MSIE detection support, or if tinygif = 1, or the
314  *      URL isn't recognized as an imageurl, a lame HTML
315  *      explanation is sent instead.
316  *
317  *    - Added new feature, which permits blocking remote
318  *      script redirects and firing back a local redirect
319  *      to the browser.
320  *      The feature is conditionally compiled, i.e. it
321  *      can be disabled with --disable-fast-redirects,
322  *      plus it must be activated by a "fast-redirects"
323  *      line in the config file, has its own log level
324  *      and of course wants to be displayed by show-proxy-args
325  *      Note: Boy, all the #ifdefs in 1001 locations and
326  *      all the fumbling with configure.in and acconfig.h
327  *      were *way* more work than the feature itself :-(
328  *
329  *    - Because a generic redirect template was needed for
330  *      this, tinygif = 3 now uses the same.
331  *
332  *    - Moved GIFs, and other static HTTP response templates
333  *      to project.h
334  *
335  *    - Some minor fixes
336  *
337  *    - Removed some >400 CRs again (Jon, you really worked
338  *      a lot! ;-)
339  *
340  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
341  *    Version 2.9.4 checkin.
342  *    - Merged popupfile and cookiefile, and added control over PCRS
343  *      filtering, in new "actionsfile".
344  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
345  *      file error you now get a message box (in the Win32 GUI) rather
346  *      than the program exiting with no explanation.
347  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
348  *      skipping.
349  *    - Removed tabs from "config"
350  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
351  *    - Bumped up version number.
352  *
353  *    Revision 1.2  2001/05/17 23:01:01  oes
354  *     - Cleaned CRLF's from the sources and related files
355  *
356  *    Revision 1.1.1.1  2001/05/15 13:59:03  oes
357  *    Initial import of version 2.9.3 source tree
358  *
359  *
360  *********************************************************************/
361 \f
362
363 /* Declare struct FILE for vars and funcs. */
364 #include <stdio.h>
365
366 /* Need time_t for file_list */
367 #include <time.h>
368
369 /*
370  * Include appropriate regular expression libraries.
371  * Note that pcrs and pcre (native) are needed for cgi
372  * and are included anyway.
373  */
374
375 #if defined(REGEX_PCRE) || defined (REGEX_GNU)
376 # define REGEX
377 #endif /* defined(REGEX_PCRE) || defined (REGEX_GNU) */
378
379 #ifdef STATIC_PCRE
380 #  include "pcre.h"
381 #else
382 #  include <pcre.h>
383 #endif
384
385 #ifdef STATIC_PCRS
386 #  include "pcrs.h"
387 #else
388 #  include <pcrs.h>
389 #endif
390
391 #if defined(REGEX_PCRE)
392 #  ifdef STATIC_PCRE
393 #    include "pcreposix.h"
394 #  else
395 #    include <pcreposix.h>
396 #  endif
397 #endif /* defined(REGEX_PCRE) */
398
399 #if defined(REGEX_GNU)
400 #  include "gnu_regex.h"
401 #endif
402
403 #ifdef AMIGA
404 #include "amiga.h"
405 #endif /* def AMIGA */
406
407 #ifdef _WIN32
408 /*
409  * I don't want to have to #include all this just for the declaration
410  * of SOCKET.  However, it looks like we have to...
411  */
412 #include <windows.h>
413 #endif
414
415
416 #ifdef __cplusplus
417 extern "C" {
418 #endif
419
420 /*
421  * The type used by sockets.  On UNIX it's an int.  Microsoft decided to
422  * make it an unsigned.
423  */
424 #ifdef _WIN32
425 typedef SOCKET jb_socket;
426 #define JB_INVALID_SOCKET INVALID_SOCKET
427 #else /* ndef _WIN32 */
428 typedef int jb_socket;
429 #define JB_INVALID_SOCKET (-1)
430 #endif /* ndef _WIN32 */
431
432
433 /*
434  * Error codes.  Functions returning these should return a jb_err
435  */
436 #define JB_ERR_OK         0 /* Success, no error                        */
437 #define JB_ERR_MEMORY     1 /* Out of memory                            */
438 #define JB_ERR_CGI_PARAMS 2 /* Missing or corrupt CGI parameters        */
439 #define JB_ERR_FILE       3 /* Error opening, reading or writing a file */
440 #define JB_ERR_PARSE      4 /* Error parsing file                       */
441 #define JB_ERR_MODIFIED   5 /* File has been modified outside of the    */
442                             /* CGI actions editor.                      */
443 typedef int jb_err;
444
445
446 /*
447  * This macro is used to free a pointer that may be NULL
448  */
449 #define freez(X)  { if(X) { free((void*)X); X = NULL ; } }
450
451
452 /* Fix a problem with Solaris.  There should be no effect on other
453  * platforms.
454  * Solaris's isspace() is a macro which uses it's argument directly
455  * as an array index.  Therefore we need to make sure that high-bit
456  * characters generate +ve values, and ideally we also want to make
457  * the argument match the declared parameter type of "int".
458  *
459  * Note: Remember to #include <ctype.h> if you use these macros.
460  */
461 #define ijb_toupper(__X) toupper((int)(unsigned char)(__X))
462 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
463 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))  
464
465 /*
466  * Use for statically allocated buffers if you have no other choice.
467  * Remember to check the length of what you write into the buffer
468  * - we don't want any buffer overflows!
469  */
470 #define BUFFER_SIZE 5000
471
472 /*
473  * Buffer size for capturing struct hostent data in the
474  * gethostby(name|addr)_r library calls. Since we don't
475  * loop over gethostbyname_r, the buffer must be sufficient
476  * to accomodate multiple IN A RRs, as used in DNS round robin
477  * load balancing. W3C's wwwlib uses 1K, so that should be
478  * good enough for us, too.
479  */
480 #define HOSTENT_BUFFER_SIZE 1024
481
482 /*
483  * So you can say "while (FOREVER) { ...do something... }"
484  */
485 #define FOREVER 1
486
487 /* Default IP and port to listen on */
488 #define HADDR_DEFAULT   "127.0.0.1"
489 #define HADDR_PORT      8118
490
491 /* Forward defs for various structures */
492
493 /* Need this for struct client_state */
494 struct configuration_spec;
495
496
497 /* Generic linked list of strings */
498
499 struct list_entry
500 {
501    const char *str;
502    struct list_entry *next;
503 };
504
505 struct list
506 {
507    struct list_entry *first;
508    struct list_entry *last;
509 };
510
511
512 /* A map from a string to another string */
513
514 struct map_entry
515 {
516    const char *name;
517    const char *value;
518    struct map_entry *next;
519 };
520
521 struct map
522 {
523    struct map_entry *first;
524    struct map_entry *last;
525 };
526
527
528 struct http_request
529 {
530    char *cmd;      /* Whole command line: method, URL, Version */
531    char *ocmd;     /* Backup of original cmd for CLF logging */
532    char *gpc;      /* HTTP method: GET, POST, .. */
533    char *url;      /* The URL */
534    char *ver;      /* Protocol version */
535    int status;     /* HTTP Status */
536
537    char *host;     /* Host part of URL */
538    int   port;     /* Port of URL or 80 (default) */
539    char *path;     /* Path of URL */
540    char *hostport; /* host[:port] */
541    int   ssl;      /* Flag if protocol is https */
542
543    char *host_ip_addr_str; /* String with dotted decimal representation
544                             * of host's IP. NULL before connect_to() */
545
546    char  *dbuffer;     /* Buffer with '\0'-delimited domain name.           */
547    char **dvec;        /* List of pointers to the strings in dbuffer.       */
548    int    dcount;      /* How many parts to this domain? (length of dvec)   */
549 };
550
551 /* 
552  * Response generated by CGI, blocker, or error handler
553  */
554 struct http_response
555 {
556   char  *status;          /* HTTP status (string) */
557   struct list headers[1]; /* List of header lines */
558   char  *head;            /* Formatted http response head */
559   size_t head_length;     /* Length of http response head */
560   char  *body;            /* HTTP document body */
561   size_t content_length;  /* Length of body, REQUIRED if binary body */
562   int    is_static;       /* Nonzero if the content will never change and
563                            * should be cached by the brwoser (e.g. images) */
564 };
565
566 /* A URL pattern */
567 struct url_spec
568 {
569    char  *spec;        /* The string which was parsed to produce this       */
570                        /* url_spec.  Used for debugging or display only.    */
571
572    /* Hostname matching, or dbuffer == NULL to match all hosts */
573    char  *dbuffer;     /* Buffer with '\0'-delimited domain name.           */
574    char **dvec;        /* List of pointers to the strings in dbuffer.       */
575    int    dcount;      /* How many parts to this domain? (length of dvec)   */
576    int    unanchored;  /* Bitmap - flags are ANCHOR_LEFT and ANCHOR_RIGHT.  */
577
578    /* Port matching: */
579    int   port;         /* The port number, or 0 to match all ports.         */
580
581    /* Path matching: */
582    char *path;         /* The path prefix (if not using regex), or source   */
583                        /* for the regex.                                    */
584    int   pathlen;      /* ==strlen(path).  Needed for prefix matching.      */
585 #ifdef REGEX
586    regex_t *preg;      /* Regex for matching path part                      */
587 #endif
588 };
589 #ifdef REGEX
590 #define URL_SPEC_INITIALIZER { NULL, NULL, NULL, 0, 0, 0, NULL, 0, NULL }
591 #else /* ifndef REGEX */
592 #define URL_SPEC_INITIALIZER { NULL, NULL, NULL, 0, 0, 0, NULL, 0 }
593 #endif /* ndef REGEX */
594
595 /* Constants for host part matching in URLs */
596 #define ANCHOR_LEFT  1
597 #define ANCHOR_RIGHT 2
598
599
600 /* An I/O buffer */
601 struct iob
602 {
603    char *buf;
604    char *cur;
605    char *eod;
606 };
607
608
609 #define IOB_PEEK(CSP) ((CSP->iob->cur > CSP->iob->eod) ? (CSP->iob->eod - CSP->iob->cur) : 0)
610 #define IOB_RESET(CSP) if(CSP->iob->buf) free(CSP->iob->buf); memset(CSP->iob, '\0', sizeof(CSP->iob));
611
612 /* Keys for csp->content_type */
613 #define CT_TEXT   1 /* Suitable for pcrs filtering */
614 #define CT_GIF    2 /* Suitable for GIF filtering */
615 #define CT_TABOO  3 /* DONT filter */
616
617 #define ACTION_MASK_ALL        (~0U)
618
619 #define ACTION_MOST_COMPATIBLE 0x00000000UL
620
621 #define ACTION_BLOCK           0x00000001UL
622 #define ACTION_DEANIMATE       0x00000002UL
623 #define ACTION_DOWNGRADE       0x00000004UL
624 #define ACTION_FAST_REDIRECTS  0x00000008UL
625 #define ACTION_FILTER          0x00000010UL
626 #define ACTION_HIDE_FORWARDED  0x00000020UL
627 #define ACTION_HIDE_FROM       0x00000040UL
628 #define ACTION_HIDE_REFERER    0x00000080UL /* sic - follow HTTP, not English */
629 #define ACTION_HIDE_USER_AGENT 0x00000100UL
630 #define ACTION_IMAGE           0x00000200UL
631 #define ACTION_IMAGE_BLOCKER   0x00000400UL
632 #define ACTION_NO_COMPRESSION  0x00000800UL
633 #define ACTION_NO_COOKIE_KEEP  0x00001000UL
634 #define ACTION_NO_COOKIE_READ  0x00002000UL
635 #define ACTION_NO_COOKIE_SET   0x00004000UL
636 #define ACTION_NO_POPUPS       0x00008000UL
637 #define ACTION_VANILLA_WAFER   0x00010000UL
638 #define ACTION_LIMIT_CONNECT   0x00020000UL
639
640 #define ACTION_STRING_DEANIMATE     0
641 #define ACTION_STRING_FROM          1
642 #define ACTION_STRING_IMAGE_BLOCKER 2
643 #define ACTION_STRING_REFERER       3
644 #define ACTION_STRING_USER_AGENT    4
645 #define ACTION_STRING_LIMIT_CONNECT 5
646 #define ACTION_STRING_COUNT         6
647
648 #define ACTION_MULTI_ADD_HEADER     0
649 #define ACTION_MULTI_WAFER          1
650 #define ACTION_MULTI_COUNT          2
651
652 /*
653  * This structure contains a list of actions to apply to a URL.
654  * It only contains positive instructions - no "-" options.
655  * It is not used to store the actions list itself, only for
656  * url_actions() to return the current values.
657  */
658 struct current_action_spec
659 {
660    unsigned long flags;    /* a bit set to "1" = add action    */
661
662    /* For those actions that require parameters: */
663
664    /* each entry is valid if & only if corresponding entry in "add" set. */
665    char * string[ACTION_STRING_COUNT];
666
667    /* Strings to add */
668    struct list multi[ACTION_MULTI_COUNT][1];
669 };
670
671
672 /*
673  * This structure contains a set of changes to actions.
674  * It can contain both positive and negative instructions.
675  * It is used to store an entry in the actions list.
676  */
677 struct action_spec
678 {
679    unsigned long mask;   /* a bit set to "0" = remove action */
680    unsigned long add;    /* a bit set to "1" = add action    */
681
682    /* For those actions that require parameters: */
683
684    /* each entry is valid if & only if corresponding entry in "add" set. */
685    char * string[ACTION_STRING_COUNT];
686
687    /* Strings to remove. */
688    struct list multi_remove[ACTION_MULTI_COUNT][1];
689
690    /* If nonzero, remove *all* strings. */
691    int         multi_remove_all[ACTION_MULTI_COUNT];
692
693    /* Strings to add */
694    struct list multi_add[ACTION_MULTI_COUNT][1];
695 };
696
697 /*
698  * This structure is used to store the actions list.
699  *
700  * It contains a URL pattern, and the chages to the actions.
701  * It is a linked list.
702  */
703 struct url_actions
704 {
705    struct url_spec url[1];
706
707    struct action_spec action[1];
708
709    struct url_actions * next;
710 };
711
712
713 /*
714  * Flags for use in csp->flags
715  */
716 #define CSP_FLAG_ACTIVE     0x01 /* Set if this client is processing data.
717                                   * Cleared when the thread associated with
718                                   * this structure dies. */
719 #define CSP_FLAG_CHUNKED    0x02 /* Set if the server's reply is in "chunked"
720                                   * transfer encoding */
721 #define CSP_FLAG_FORCED     0x04 /* Set if this request was enforced, although
722                                   * it would normally have been blocked. */
723 #define CSP_FLAG_MODIFIED   0x08 /* Set if any modification to the body was done */
724 #define CSP_FLAG_REJECTED   0x10 /* Set if request was blocked.  */
725 #define CSP_FLAG_TOGGLED_ON 0x20 /* Set if we are toggled on (FEATURE_TOGGLE) */
726
727 /*
728  * The state of a JunkBuster processing thread.
729  */
730 struct client_state
731 {
732    /* The proxy's configuration */
733    struct configuration_spec * config;
734
735    /* The actions to perform on the current request */
736    struct current_action_spec  action[1];
737
738    /* socket to talk to client (web browser) */
739    jb_socket cfd;
740
741    /* socket to talk to server (web server or proxy) */
742    jb_socket sfd;
743
744    /* Multi-purpose flag container, see CSP_FLAG_* above */
745    unsigned short int flags;
746
747    /*
748     * Client PC's IP address, as reported by the accept()_ function.
749     * Both as string and number
750     */
751    char *ip_addr_str;
752    long  ip_addr_long;
753
754
755    /* Our IP address and hostname, i.e. the IP address that
756       the client used to reach us, and the associated hostname,
757       both as strings
758     */
759    char *my_ip_addr_str;
760    char *my_hostname;
761
762    /* The URL that was requested */
763    struct http_request http[1];
764
765    /* An I/O buffer used for buffering data read from the client */
766    struct iob iob[1];
767
768    /* List of all headers for this request */
769    struct list headers[1];
770
771    /* List of all cookies for this request */
772    struct list cookie_list[1];
773
774    /* MIME-Type key, see CT_* above */
775    unsigned short int content_type;
776
777    /* The "X-Forwarded-For:" header sent by the client */
778    char   *x_forwarded;
779
780    /* files associated with this client */
781    struct file_list *actions_list;
782
783    struct file_list *rlist;   /* pcrs job file */
784    size_t content_length;     /* Length after content modification */
785
786 #ifdef FEATURE_TRUST
787    struct file_list *tlist;   /* trustfile */
788 #endif /* def FEATURE_TRUST */
789
790    struct client_state *next;
791 };
792
793
794 /*
795  * A function to add a header
796  */
797 typedef jb_err (*add_header_func_ptr)(struct client_state *);
798
799 /*
800  * A function to process a header
801  */
802 typedef jb_err (*parser_func_ptr    )(struct client_state *, char **);
803
804 /*
805  * List of functions to run on a list of headers
806  */
807 struct parsers
808 {
809    char   *str;
810    size_t len;
811    parser_func_ptr parser;
812 };
813
814
815 /*
816  * List of available CGI functions.
817  */
818 struct cgi_dispatcher
819 {
820    const char * const name;
821    jb_err    (* const handler)(struct client_state *csp, struct http_response *rsp, const struct map *parameters);
822    const char * const description;
823 };
824
825
826 /*
827  * A data file used by JunkBuster.  Kept in a linked list.
828  */
829 struct file_list
830 {
831    /*
832     * this is a pointer to the data structures associated with the file.
833     * Read-only once the structure has been created.
834     */
835    void *f;
836
837    /* Normally NULL.  When we are finished with file (i.e. when we have
838     * loaded a new one), set to a pointer to an unloader function.
839     * Unloader will be called by sweep() (called from main loop) when
840     * all clients using this file are done.  This prevents threading
841     * problems.
842     */
843    void (*unloader)(void *);
844
845    /* Used internally by sweep().  Do not access from elsewhere. */
846    int active;
847
848    /* Following variables allow us to check if file has been changed.
849     * Read-only once the structure has been created.
850     */
851    time_t lastmodified;
852    char * filename;
853
854    /* Pointer to next entry in the linked list of all "file_list"s.
855     * This linked list is so that sweep() can navigate it.
856     * Since sweep() can remove items from the list, we must be careful
857     * to only access this value from main thread (when we know sweep
858     * won't be running).
859     */
860    struct file_list *next;
861 };
862
863
864 #ifdef FEATURE_TRUST
865 struct block_spec
866 {
867    struct url_spec url[1];
868    int    reject;
869    struct block_spec *next;
870 };
871 #endif /* def FEATURE_TRUST */
872
873
874 #define SOCKS_NONE    0    /* Don't use a SOCKS server */
875 #define SOCKS_4      40    /* original SOCKS 4 protocol */
876 #define SOCKS_4A     41    /* as modified for hosts w/o external DNS */
877
878 struct forward_spec
879 {
880    struct url_spec url[1];
881
882    /* Connection type - must be a SOCKS_xxx constant */
883    int   type;
884
885    /* SOCKS server */
886    char *gateway_host;
887    int   gateway_port;
888
889    /* Parent HTTP proxy */
890    char *forward_host;
891    int   forward_port;
892
893    /* For the linked list */
894    struct forward_spec *next;
895 };
896 #define FORWARD_SPEC_INITIALIZER { { URL_SPEC_INITIALIZER }, 0, NULL, 0, NULL, 0, NULL }
897
898
899 struct re_filterfile_spec
900 {
901    char *username;
902    char *filtername;
903    struct list patterns[1];
904    pcrs_job *joblist;
905 };
906
907 #ifdef FEATURE_ACL
908 #define ACL_PERMIT   1  /* accept connection request */
909 #define ACL_DENY     2  /* reject connection request */
910
911 struct access_control_addr
912 {
913    unsigned long addr;
914    unsigned long mask;
915    unsigned long port;
916 };
917
918 struct access_control_list
919 {
920    struct access_control_addr src[1];
921    struct access_control_addr dst[1];
922
923    short action;
924    struct access_control_list *next;
925 };
926 #endif /* def FEATURE_ACL */
927
928
929 /* Maximum number of loaders (actions, re_filter, ...) */
930 #define NLOADERS 8
931
932
933 #define RUNTIME_FEATURE_CGI_EDIT_ACTIONS  1
934 #define RUNTIME_FEATURE_CGI_TOGGLE        2
935
936
937 /*
938  * Data loaded from the configuration file.
939  *
940  * (Anomaly: toggle is still handled through a global, not this structure)
941  */
942 struct configuration_spec
943 {
944    int debug;
945    int multi_threaded;
946
947    /* Features that can be enabled/disabled throuigh the config file */
948    unsigned feature_flags;
949
950    const char *logfile;
951
952    const char *confdir;
953    const char *logdir;
954    const char *actions_file;
955
956    /* The administrator's email address */
957    char *admin_address;
958
959    /* A URL with info on this proxy */
960    char *proxy_info_url;
961
962    const char *re_filterfile;
963
964 #ifdef FEATURE_COOKIE_JAR
965    const char * jarfile;
966    FILE * jar;
967 #endif /* def FEATURE_COOKIE_JAR */
968
969    /*
970     * Port and IP to bind to.
971     * Defaults to HADDR_DEFAULT:HADDR_PORT == 127.0.0.1:8118
972     */
973    const char *haddr;
974    int         hport;
975
976    /* Size limit for IOB */
977    size_t buffer_limit;
978
979 #ifdef FEATURE_TRUST
980    const char * trustfile;
981
982    struct list trust_info[1];
983    struct url_spec *trust_list[64];
984 #endif /* def FEATURE_TRUST */
985
986 #ifdef FEATURE_ACL
987    struct access_control_list *acl;
988 #endif /* def FEATURE_ACL */
989
990    struct forward_spec *forward;
991
992    /* All options from the config file, HTML-formatted */
993    char *proxy_args;
994
995    /* the configuration file object. */
996    struct file_list *config_file_list;
997
998    /* List of loaders */
999    int (*loaders[NLOADERS])(struct client_state *);
1000
1001    /* bool, nonzero if we need to bind() to the new port */
1002    int need_bind;
1003 };
1004
1005
1006 #define SZ(X)  (sizeof(X) / sizeof(*X))
1007
1008 #ifdef FEATURE_FORCE_LOAD
1009 #define FORCE_PREFIX "/IJB-FORCE-LOAD"
1010 #endif /* def FEATURE_FORCE_LOAD */
1011
1012 #ifdef FEATURE_NO_GIFS
1013 #define BUILTIN_IMAGE_MIMETYPE "image/png"
1014 #else
1015 #define BUILTIN_IMAGE_MIMETYPE "image/gif"
1016 #endif /* def FEATURE_NO_GIFS */
1017
1018
1019 /* Hardwired URLs */
1020 #define HOME_PAGE_URL       "http://ijbswa.sourceforge.net"
1021 #define REDIRECT_URL        HOME_PAGE_URL "/redirect.php?v=" VERSION "&to="
1022
1023 /*
1024  * The "hosts" to intercept and display CGI pages.
1025  * First one is a hostname only, second one can specify host and path.
1026  *
1027  * Notes:
1028  * 1) Do not specify the http: prefix
1029  * 2) CGI_SITE_2_PATH must not end with /, one will be added automatically.
1030  * 3) CGI_SITE_2_PATH must start with /, unless it is the empty string.
1031  */
1032 #define CGI_SITE_1_HOST "i.j.b"
1033 #define CGI_SITE_2_HOST "ijbswa.sourceforge.net"
1034 #define CGI_SITE_2_PATH "/config"
1035
1036 /*
1037  * The prefix for CGI pages.  Written out in generated HTML.
1038  * INCLUDES the trailing slash.
1039  */
1040 #define CGI_PREFIX  "http://" CGI_SITE_2_HOST CGI_SITE_2_PATH "/"
1041
1042
1043 /* HTTP snipplets */
1044 static const char CSUCCEED[] =
1045    "HTTP/1.0 200 Connection established\n"
1046    "Proxy-Agent: IJ/" VERSION "\r\n\r\n";
1047
1048 static const char CHEADER[] =
1049    "HTTP/1.0 400 Invalid header received from browser\r\n\r\n";
1050
1051 static const char CFORBIDDEN[] =
1052    "HTTP/1.0 403 Connection not allowable\r\nX-Hint: If you read this message interactively, then you know why this happens ,-)\r\n\r\n";
1053
1054 #ifdef __cplusplus
1055 } /* extern "C" */
1056 #endif
1057
1058 #endif /* ndef PROJECT_H_INCLUDED */
1059
1060 /*
1061   Local Variables:
1062   tab-width: 3
1063   end:
1064 */