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