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