Conditional compilation of w32_socket_error
[privoxy.git] / project.h
1 #ifndef _PROJECT_H
2 #define _PROJECT_H
3 #define PROJECT_H_VERSION "$Id: project.h,v 1.6 2001/05/27 22:17:04 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.6  2001/05/27 22:17:04  oes
40  *
41  *    - re_process_buffer no longer writes the modified buffer
42  *      to the client, which was very ugly. It now returns the
43  *      buffer, which it is then written by chat.
44  *
45  *    - content_length now adjusts the Content-Length: header
46  *      for modified documents rather than crunch()ing it.
47  *      (Length info in csp->content_length, which is 0 for
48  *      unmodified documents)
49  *
50  *    - For this to work, sed() is called twice when filtering.
51  *
52  *    Revision 1.5  2001/05/26 00:28:36  jongfoster
53  *    Automatic reloading of config file.
54  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
55  *    Most of the global variables have been moved to a new
56  *    struct configuration_spec, accessed through csp->config->globalname
57  *    Most of the globals remaining are used by the Win32 GUI.
58  *
59  *    Revision 1.4  2001/05/22 18:46:04  oes
60  *
61  *    - Enabled filtering banners by size rather than URL
62  *      by adding patterns that replace all standard banner
63  *      sizes with the "Junkbuster" gif to the re_filterfile
64  *
65  *    - Enabled filtering WebBugs by providing a pattern
66  *      which kills all 1x1 images
67  *
68  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
69  *      which is selected by the (nonstandard and therefore
70  *      capital) letter 'U' in the option string.
71  *      It causes the quantifiers to be ungreedy by default.
72  *      Appending a ? turns back to greedy (!).
73  *
74  *    - Added a new interceptor ijb-send-banner, which
75  *      sends back the "Junkbuster" gif. Without imagelist or
76  *      MSIE detection support, or if tinygif = 1, or the
77  *      URL isn't recognized as an imageurl, a lame HTML
78  *      explanation is sent instead.
79  *
80  *    - Added new feature, which permits blocking remote
81  *      script redirects and firing back a local redirect
82  *      to the browser.
83  *      The feature is conditionally compiled, i.e. it
84  *      can be disabled with --disable-fast-redirects,
85  *      plus it must be activated by a "fast-redirects"
86  *      line in the config file, has its own log level
87  *      and of course wants to be displayed by show-proxy-args
88  *      Note: Boy, all the #ifdefs in 1001 locations and
89  *      all the fumbling with configure.in and acconfig.h
90  *      were *way* more work than the feature itself :-(
91  *
92  *    - Because a generic redirect template was needed for
93  *      this, tinygif = 3 now uses the same.
94  *
95  *    - Moved GIFs, and other static HTTP response templates
96  *      to project.h
97  *
98  *    - Some minor fixes
99  *
100  *    - Removed some >400 CRs again (Jon, you really worked
101  *      a lot! ;-)
102  *
103  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
104  *    Version 2.9.4 checkin.
105  *    - Merged popupfile and cookiefile, and added control over PCRS
106  *      filtering, in new "permissionsfile".
107  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
108  *      file error you now get a message box (in the Win32 GUI) rather
109  *      than the program exiting with no explanation.
110  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
111  *      skipping.
112  *    - Removed tabs from "config"
113  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
114  *    - Bumped up version number.
115  *
116  *    Revision 1.2  2001/05/17 23:01:01  oes
117  *     - Cleaned CRLF's from the sources and related files
118  *
119  *    Revision 1.1.1.1  2001/05/15 13:59:03  oes
120  *    Initial import of version 2.9.3 source tree
121  *
122  *
123  *********************************************************************/
124 \f
125
126 /* Declare struct FILE for vars and funcs. */
127 #include <stdio.h>
128
129 /* Need time_t for file_list */
130 #include <time.h>
131
132 /*
133  * Include appropriate regular expression libraries.
134  *
135  * PCRS           ==> Include pcre
136  * REGEX && PCRE  ==> Include pcre and pcreposix
137  * REGEX && !PCRE ==> Include gnu_regex
138  *
139  * STATIC  ==> Use  #include "pcre.h"  (compiling at same time)
140  * !STATIC ==> Use  #include <pcre.h>  (System library)
141  *
142  */
143 #if (defined(REGEX) && defined(PCRE)) || defined(PCRS)
144 #  ifdef STATIC
145 #    include "pcre.h"
146 #  else
147 #    include <pcre.h>
148 #  endif
149 #endif /* (defined(REGEX) && defined(PCRE)) || defined(PCRS) */
150
151 #if defined(REGEX) && defined(PCRE)
152 #  ifdef STATIC
153 #    include "pcreposix.h"
154 #  else
155 #    include <pcreposix.h>
156 #  endif
157 #endif /* defined(REGEX) && defined(PCRE) */
158
159 #if defined(REGEX) && !defined(PCRE)
160 #  include "gnu_regex.h"
161 #endif
162
163 #ifdef PCRS
164 #include "pcrs.h"
165 #endif /* def PCRS */
166
167 #ifdef AMIGA 
168 #include "amiga.h" 
169 #endif /* def AMIGA */
170
171 #ifdef __cplusplus
172 extern "C" {
173 #endif
174
175 #define FOREVER 1
176
177 /* Default IP and port to listen on */
178 #define HADDR_DEFAULT   "127.0.0.1"
179 #define HADDR_PORT      8000
180
181
182 /* Need this for struct gateway */
183 struct client_state;
184
185 /* Need this for struct client_state */
186 struct configuration_spec;
187
188
189 struct http_request
190 {
191    char *cmd;
192    char *gpc;
193    char *host;
194    int   port;
195    char *path;
196    char *ver;
197    char *hostport; /* "host[:port]" */
198    int   ssl;
199 };
200
201 struct gateway
202 {
203    /* generic attributes */
204    char *name;
205    int (*conn)(const struct gateway *, struct http_request *, struct client_state *);
206    int   type;
207
208    /* domain specific attributes */
209    char *gateway_host;
210    int   gateway_port;
211
212    char *forward_host;
213    int   forward_port;
214 };
215
216
217 /* Generic linked list of strings */
218 struct list
219 {
220    char *str;
221    struct list *last;
222    struct list *next;
223 };
224
225
226 /* An I/O buffer */
227 struct iob
228 {
229    char *buf;
230    char *cur;
231    char *eod;
232 };
233
234
235 #define IOB_PEEK(CSP) ((CSP->iob->cur > CSP->iob->eod) ? (CSP->iob->eod - CSP->iob->cur) : 0)
236 #define IOB_RESET(CSP) if(CSP->iob->buf) free(CSP->iob->buf); memset(CSP->iob, '\0', sizeof(CSP->iob));
237
238
239 /* Constants defining bitmask for csp->accept_types */
240
241 #ifdef DETECT_MSIE_IMAGES
242
243 /* MSIE detected by user-agent string */
244 #define ACCEPT_TYPE_IS_MSIE     0x0001
245
246 /*
247  * *If* this is MSIE, it wants an image.  (Or this is a shift-reload, or
248  * it's got an image from this URL before...  yuck!)
249  * Only meaningful if ACCEPT_TYPE_IS_MSIE set 
250  */
251 #define ACCEPT_TYPE_MSIE_IMAGE  0x0002
252
253 /*
254  * *If* this is MSIE, it wants a HTML document.
255  * Only meaningful if ACCEPT_TYPE_IS_MSIE set
256  */
257 #define ACCEPT_TYPE_MSIE_HTML   0x0004
258
259 #endif /* def DETECT_MSIE_IMAGES */
260
261
262 struct client_state
263 {
264    /* The proxy's configuration */
265    struct configuration_spec * config;
266
267
268    /* The permissions that the current URL has */
269    int  permissions;
270
271
272    /* socket to talk to client (web browser) */
273    int  cfd;
274
275    /* socket to talk to server (web server or proxy) */
276    int  sfd;
277
278
279 #ifdef STATISTICS
280    /* 1 if this URL was rejected, 0 otherwise. Allows actual stats inc to 
281     * occur in main thread only for thread-safety. 
282     */
283    int  rejected;
284 #endif /* def STATISTICS */
285
286 #ifdef FORCE_LOAD
287    int force;
288 #endif /* def FORCE_LOAD */
289
290 #ifdef TOGGLE
291    int   toggled_on;
292 #endif /* def TOGGLE */
293
294    /*
295     * Client PC's IP address, as reported by the accept()_ function.
296     * Both as string and number
297     */
298    char *ip_addr_str;
299    long  ip_addr_long;
300
301 #ifdef TRUST_FILES
302    /* The referer in this request, if one was specified. */
303    char *referrer;
304 #endif /* def TRUST_FILES */
305
306 #if defined(DETECT_MSIE_IMAGES)
307    /* Types the client will accept.
308     * Bitmask - see ACCEPT_TYPE_XXX constants.
309     */
310    int accept_types;
311 #endif /* defined(DETECT_MSIE_IMAGES) */
312
313    /* The URL that was requested */
314    struct http_request http[1];
315
316    /* An I/O buffer used for buffering data read from the client */
317    struct iob iob[1];
318
319    /* List of all headers for this request */
320    struct list headers[1];
321
322    /* List of all cookies for this request */
323    struct list cookie_list[1];
324
325 #if defined(PCRS) || defined(KILLPOPUPS)
326    /* Nonzero if this has a text MIME type */
327    int is_text;
328 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
329
330    /* The "X-Forwarded-For:" header sent by the client */
331    char   *x_forwarded;
332
333    /*
334     * Nonzero if this client is processing data.
335     * Set to zero when the thread associated with this structure dies.
336     */
337    int active;
338
339    /* files associated with this client */
340    struct file_list *flist;   /* forwardfile */
341    struct file_list *permissions_list;
342
343
344 #ifdef ACL_FILES
345    struct file_list *alist;   /* aclfile */
346 #endif /* def ACL_FILES */
347
348 #ifdef PCRS
349      struct file_list *rlist;   /* Perl re_filterfile */
350      size_t content_length;     /* Length after processing */ 
351 #endif /* def PCRS */
352
353 #ifdef TRUST_FILES
354    struct file_list *tlist;   /* trustfile */
355 #endif /* def TRUST_FILES */
356
357    struct client_state *next;
358 };
359
360
361 struct parsers
362 {
363    char *str;
364    char  len;
365    char *(*parser)(const struct parsers *, char *, struct client_state *);
366 };
367
368
369 struct interceptors
370 {
371    char *str;
372    char  len;
373    char *(*interceptor)(struct http_request *http, struct client_state *csp);
374 };
375
376
377 /* A URL pattern */
378 struct url_spec
379 {
380    char  *spec;
381    char  *domain;
382    char  *dbuf;
383    char **dvec;
384    int    dcnt;
385    int    toplevel;
386
387    char *path;
388    int   pathlen;
389    int   port;
390 #ifdef REGEX
391    regex_t *preg;
392 #endif
393 };
394
395
396 struct file_list
397 {
398    /*
399     * this is a pointer to the data structures associated with the file.
400     * Read-only once the structure has been created.
401     */
402    void *f;
403    
404    /* Normally NULL.  When we are finished with file (i.e. when we have
405     * loaded a new one), set to a pointer to an unloader function.
406     * Unloader will be called by sweep() (called from main loop) when
407     * all clients using this file are done.  This prevents threading 
408     * problems.
409     */
410    void (*unloader)(void *);
411
412    /* Used internally by sweep().  Do not access from elsewhere. */
413    int active;
414
415 #ifndef SPLIT_PROXY_ARGS
416    /* String to be displayed as part of show-proxy-args display.
417     * Read-only once the structure has been created.
418     */
419    char *proxy_args;
420 #endif /* ndef SPLIT_PROXY_ARGS */
421
422    /* Following variables allow us to check if file has been changed.
423     * Read-only once the structure has been created.
424     */
425    time_t lastmodified;
426    char * filename;
427
428    /* Pointer to next entry in the linked list of all "file_list"s.
429     * This linked list is so that sweep() can navigate it.
430     * Since sweep() can remove items from the list, we must be careful
431     * to only access this value from main thread (when we know sweep
432     * won't be running).
433     */
434    struct file_list *next;
435 };
436
437
438 #ifdef TRUST_FILES
439 struct block_spec
440 {
441    struct url_spec url[1];
442    int    reject;
443    struct block_spec *next;
444 };
445 #endif /* def TRUST_FILES */
446
447
448 #define PERMIT_COOKIE_SET      0x0001U
449 #define PERMIT_COOKIE_READ     0x0002U
450 #define PERMIT_RE_FILTER       0x0004U
451 #define PERMIT_POPUPS          0x0008U
452 #define PERMIT_REFERER         0x0010U /* sic - follow HTTP, not English */
453 #define PERMIT_FAST_REDIRECTS  0x0020U
454 #define PERMIT_BLOCK           0x0040U
455 #define PERMIT_IMAGE           0x0080U
456
457 #define PERMIT_USER_AGENT      PERMIT_COOKIE_SET /* FIXME  Alias this for now */
458
459 #define PERMIT_MASK_ALL        (~0U)
460
461 #define PERMIT_MOST_COMPATIBLE (PERMIT_COOKIE_SET | PERMIT_COOKIE_READ | \
462    PERMIT_REFERER | PERMIT_POPUPS | PERMIT_USER_AGENT)
463
464 struct permissions_spec
465 {
466    struct url_spec url[1];
467    unsigned mask;   /* a bit set to "0" = remove permission */
468    unsigned add;    /* a bit set to "1" = add permission */
469    struct permissions_spec * next;
470 };
471
472
473 struct forward_spec
474 {
475    struct url_spec url[1];
476    struct gateway gw[1];
477    struct forward_spec *next;
478 };
479
480
481 #ifdef PCRS
482 struct re_filterfile_spec
483 {
484    struct list patterns[1];
485    /* See README.re_filter */
486    pcrs_job *joblist;
487 };
488 #endif /* def PCRS */
489
490
491 #ifdef ACL_FILES
492 #define ACL_PERMIT   1  /* accept connection request */
493 #define ACL_DENY     2  /* reject connection request */
494
495 struct access_control_addr
496 {
497    unsigned long addr;
498    unsigned long mask;
499    unsigned long port;
500 };
501
502 struct access_control_list
503 {
504    struct access_control_addr src[1];
505    struct access_control_addr dst[1];
506
507    short action;
508    struct access_control_list *next;
509 };
510 #endif /* def ACL_FILES */
511
512
513 /* Maximum number of loaders (permissions, block, forward, acl...) */
514 #define NLOADERS 8
515
516 /*
517  * Data loaded from the configuration file.
518  *
519  * (Anomaly: toggle is still handled through a global, not this structure)
520  */
521 struct configuration_spec
522 {
523    int debug;
524    int multi_threaded;
525
526 #ifdef IMAGE_BLOCKING
527    int tinygif;
528    const char *tinygifurl;
529 #endif /* def IMAGE_BLOCKING */
530
531    const char *logfile;
532
533    const char *permissions_file;
534    const char *forwardfile;
535
536 #ifdef ACL_FILES
537    const char *aclfile;
538 #endif /* def ACL_FILES */
539
540 #ifdef PCRS
541    const char *re_filterfile;
542 #endif /* def PCRS */
543
544    /*
545     * Permissions to use for URLs not in the permissions list.
546     */
547    int default_permissions;
548
549 #ifdef JAR_FILES
550    const char * jarfile;
551    FILE * jar;
552 #endif /* def JAR_FILES */
553
554    const char *referrer;
555    const char *uagent;
556    const char *from;
557
558    int add_forwarded;
559
560    struct list wafer_list[1];
561    struct list xtra_list[1];
562
563    /*
564     * Port and IP to bind to.
565     * Defaults to HADDR_DEFAULT:HADDR_PORT == 127.0.0.1:8000
566     */
567    const char *haddr;
568    int         hport;
569
570 #ifndef SPLIT_PROXY_ARGS
571    const char *suppress_message;
572 #endif /* ndef SPLIT_PROXY_ARGS */
573
574 #ifndef SPLIT_PROXY_ARGS
575    /* suppress listing config files */
576    int suppress_blocklists;
577 #endif /* ndef SPLIT_PROXY_ARGS */
578
579 #ifdef TRUST_FILES
580    const char * trustfile;
581
582    struct list trust_info[1];
583    struct url_spec *trust_list[64];
584 #endif /* def TRUST_FILES */
585
586    /* Various strings for show-proxy-args */
587    char *proxy_args_header;
588    char *proxy_args_invocation;
589    char *proxy_args_gateways;
590    char *proxy_args_trailer;
591
592    /* the configuration file object. */
593    struct file_list *config_file_list;
594
595    /* List of loaders */
596    int (*loaders[NLOADERS])(struct client_state *);
597
598    /* bool, nonzero if we need to bind() to the new port */
599    int need_bind;
600 };
601
602
603 #define SZ(X)  (sizeof(X) / sizeof(*X))
604
605 #define WHITEBG   "<body bgcolor=\"#ffffff\" link=\"#000078\" alink=\"#ff0022\" vlink=\"#787878\">\n"
606 #define BODY      "<body bgcolor=\"#f8f8f0\" link=\"#000078\" alink=\"#ff0022\" vlink=\"#787878\">\n"
607 #define BANNER    "<strong>Internet J<small>UNK<i><font color=\"red\">BUSTER</font></i></small></strong>"
608
609 #ifdef FORCE_LOAD
610 #define FORCE_PREFIX "/IJB-FORCE-LOAD"
611 #endif /* def FORCE_LOAD */
612
613 #define HOME_PAGE_URL  "http://ijbswa.sourceforge.net/"
614 #define REDIRECT_URL HOME_PAGE_URL "redirect.php?v=" VERSION "&to="
615
616 static const char CFAIL[] =
617    "HTTP/1.0 503 Connect failed\n"
618    "Content-Type: text/html\n\n"
619    "<html>\n"
620    "<head>\n"
621    "<title>Internet Junkbuster: Connect failed</title>\n"
622    "</head>\n"
623    BODY
624    "<h1><center>"
625    BANNER
626    "</center></h1>"
627    "TCP connection to '%s' failed: %s.\n<br>"
628    "</body>\n"
629    "</html>\n";
630
631 static const char CNXDOM[] =
632    "HTTP/1.0 404 Non-existent domain\n"
633    "Content-Type: text/html\n\n"
634    "<html>\n"
635    "<head>\n"
636    "<title>Internet Junkbuster: Non-existent domain</title>\n"
637    "</head>\n"
638    BODY
639    "<h1><center>"
640    BANNER
641    "</center></h1>"
642    "No such domain: %s\n"
643    "</body>\n"
644    "</html>\n";
645
646 static const char CNOBANNER[] =
647    "HTTP/1.0 200 No Banner\n"
648    "Content-Type: text/html\n\n"
649    "<html>\n"
650    "<head>\n"
651    "<title>Internet Junkbuster: No Banner</title>\n"
652    "</head>\n"
653    BODY
654    "<h1><center>"
655    BANNER
656    "</h1>"
657    "You asked for a banner that this proxy can't produce because either configuration does not permit.\n<br>"
658    "or the URL didn't end with .gif\n"
659    "</center></body>\n"
660    "</html>\n";
661
662 static const char CSUCCEED[] =
663    "HTTP/1.0 200 Connection established\n"
664    "Proxy-Agent: IJ/" VERSION "\n\n";
665
666 static const char CHEADER[] =
667    "HTTP/1.0 400 Invalid header received from browser\n\n";
668
669 static const char SHEADER[] =
670    "HTTP/1.0 502 Invalid header received from server\n\n";
671
672 #ifdef IMAGE_BLOCKING
673
674 /*
675  * Hint: You can encode your own GIFs like that:
676  * perl -e 'while (read STDIN, $c, 1) { printf("\\%.3o,", unpack("C", $c)); }'
677  */
678
679 static const char BLANKGIF[] =
680    "HTTP/1.0 200 OK\r\n"
681    "Pragma: no-cache\r\n"
682    "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
683    "Expires:       Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
684    "Content-type: image/gif\r\n\r\n"
685    "GIF89a\001\000\001\000\200\000\000\377\377\377\000\000"
686    "\000!\371\004\001\000\000\000\000,\000\000\000\000\001"
687    "\000\001\000\000\002\002D\001\000;";
688
689 static const char JBGIF[] =
690    "HTTP/1.0 200 OK\r\n"
691    "Pragma: no-cache\r\n"
692    "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
693    "Expires:       Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
694    "Content-type: image/gif\r\n\r\n"
695    "GIF89aD\000\013\000\360\000\000\000\000\000\377\377\377!"
696    "\371\004\001\000\000\001\000,\000\000\000\000D\000\013\000"
697    "\000\002a\214\217\251\313\355\277\000\200G&K\025\316hC\037"
698    "\200\234\230Y\2309\235S\230\266\206\372J\253<\3131\253\271"
699    "\270\215\342\254\013\203\371\202\264\334P\207\332\020o\266"
700    "N\215I\332=\211\312\3513\266:\026AK)\364\370\365aobr\305"
701    "\372\003S\275\274k2\354\254z\347?\335\274x\306^9\374\276"
702    "\037Q\000\000;";
703
704 #endif /* def IMAGE_BLOCKING */
705
706 #if defined(FAST_REDIRECTS) || defined(IMAGE_BLOCKING)
707
708 static const char HTTP_REDIRECT_TEMPLATE[] =
709       "HTTP/1.0 302 Local Redirect from Junkbuster\r\n" 
710       "Pragma: no-cache\r\n"
711       "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
712       "Expires:       Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
713       "Location: %s\r\n";
714
715 #endif /*  defined(FAST_REDIRECTS) || defined(IMAGE_BLOCKING) */
716
717 #ifdef __cplusplus
718 } /* extern "C" */
719 #endif
720
721 #endif /* ndef _PROJECT_H */
722
723 /*
724   Local Variables:
725   tab-width: 3
726   end:
727 */