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