Now gives a warning under Windows if you try to bind
[privoxy.git] / project.h
1 #ifndef _PROJECT_H
2 #define _PROJECT_H
3 #define PROJECT_H_VERSION "$Id: project.h,v 1.3 2001/05/20 01:21:20 jongfoster 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.3  2001/05/20 01:21:20  jongfoster
40  *    Version 2.9.4 checkin.
41  *    - Merged popupfile and cookiefile, and added control over PCRS
42  *      filtering, in new "permissionsfile".
43  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
44  *      file error you now get a message box (in the Win32 GUI) rather
45  *      than the program exiting with no explanation.
46  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
47  *      skipping.
48  *    - Removed tabs from "config"
49  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
50  *    - Bumped up version number.
51  *
52  *    Revision 1.2  2001/05/17 23:01:01  oes
53  *     - Cleaned CRLF's from the sources and related files
54  *
55  *    Revision 1.1.1.1  2001/05/15 13:59:03  oes
56  *    Initial import of version 2.9.3 source tree
57  *
58  *
59  *********************************************************************/
60 \f
61
62 /* Declare struct FILE for vars and funcs. */
63 #include <stdio.h>
64
65 /* Need time_t for file_list */
66 #include <time.h>
67
68 /*
69  * Include appropriate regular expression libraries.
70  *
71  * PCRS           ==> Include pcre
72  * REGEX && PCRE  ==> Include pcre and pcreposix
73  * REGEX && !PCRE ==> Include gnu_regex
74  *
75  * STATIC  ==> Use  #include "pcre.h"  (compiling at same time)
76  * !STATIC ==> Use  #include <pcre.h>  (System library)
77  *
78  */
79 #if (defined(REGEX) && defined(PCRE)) || defined(PCRS)
80 #  ifdef STATIC
81 #    include "pcre.h"
82 #  else
83 #    include <pcre.h>
84 #  endif
85 #endif /* (defined(REGEX) && defined(PCRE)) || defined(PCRS) */
86
87 #if defined(REGEX) && defined(PCRE)
88 #  ifdef STATIC
89 #    include "pcreposix.h"
90 #  else
91 #    include <pcreposix.h>
92 #  endif
93 #endif /* defined(REGEX) && defined(PCRE) */
94
95 #if defined(REGEX) && !defined(PCRE)
96 #  include "gnu_regex.h"
97 #endif
98
99 #ifdef PCRS
100 #include "pcrs.h"
101 #endif /* def PCRS */
102
103 #ifdef AMIGA 
104 #include "amiga.h" 
105 #endif /* def AMIGA */
106
107 #ifdef __cplusplus
108 extern "C" {
109 #endif
110
111 #define FOREVER 1
112
113 /* Default IP and port to listen on */
114 #define HADDR_DEFAULT   "127.0.0.1"
115 #define HADDR_PORT      8000
116
117
118 /* Need this for struct gateway */
119 struct client_state;
120
121
122 struct http_request
123 {
124    char *cmd;
125    char *gpc;
126    char *host;
127    int   port;
128    char *path;
129    char *ver;
130    char *hostport; /* "host[:port]" */
131    int   ssl;
132 };
133
134 struct gateway
135 {
136    /* generic attributes */
137    char *name;
138    int (*conn)(const struct gateway *, struct http_request *, struct client_state *);
139    int   type;
140
141    /* domain specific attributes */
142    char *gateway_host;
143    int   gateway_port;
144
145    char *forward_host;
146    int   forward_port;
147 };
148
149
150 struct proxy_args
151 {
152    char *header;
153    char *invocation;
154    char *gateways;
155    char *trailer;
156 };
157
158
159 struct iob
160 {
161    char *buf;
162    char *cur;
163    char *eod;
164 };
165
166
167 struct list
168 {
169    char *str;
170    struct list *last;
171    struct list *next;
172 };
173
174 #define IOB_PEEK(CSP) ((CSP->iob->cur > CSP->iob->eod) ? (CSP->iob->eod - CSP->iob->cur) : 0)
175 #define IOB_RESET(CSP) if(CSP->iob->buf) free(CSP->iob->buf); memset(CSP->iob, '\0', sizeof(CSP->iob));
176
177
178 /* Constants defining bitmask for csp->accept_types */
179
180 #ifdef DETECT_MSIE_IMAGES
181
182 /* MSIE detected by user-agent string */
183 #define ACCEPT_TYPE_IS_MSIE     0x0001
184
185 /*
186  * *If* this is MSIE, it wants an image.  (Or this is a shift-reload, or
187  * it's got an image from this URL before...  yuck!)
188  * Only meaningful if ACCEPT_TYPE_IS_MSIE set 
189  */
190 #define ACCEPT_TYPE_MSIE_IMAGE  0x0002
191
192 /*
193  * *If* this is MSIE, it wants a HTML document.
194  * Only meaningful if ACCEPT_TYPE_IS_MSIE set
195  */
196 #define ACCEPT_TYPE_MSIE_HTML   0x0004
197
198 #endif /* def DETECT_MSIE_IMAGES */
199
200
201 struct client_state
202 {
203    int  permissions;
204    
205    int  cfd;
206    int  sfd;
207
208 #ifdef STATISTICS
209    /* 1 if this URL was rejected, 0 otherwise. Allows actual stats inc to 
210     * occur in main thread only for thread-safety. 
211     */
212    int  rejected;
213 #endif /* def STATISTICS */
214
215 #ifdef FORCE_LOAD
216    int force;
217 #endif /* def FORCE_LOAD */
218
219 #ifdef TOGGLE
220    /*
221     * by haroon - most of credit to srt19170
222     * We add an "on/off" toggle here that is used to effectively toggle
223     * the Junkbuster off or on
224     */
225    int   toggled_on;
226 #endif
227
228    char *ip_addr_str;
229    long  ip_addr_long;
230    char *referrer;
231
232 #if defined(DETECT_MSIE_IMAGES)
233    /* Types the client will accept.
234     * Bitmask - see ACCEPT_TYPE_XXX constants.
235     */
236    int accept_types;
237 #endif /* defined(DETECT_MSIE_IMAGES) */
238
239    const struct gateway *gw;
240    struct http_request http[1];
241
242    struct iob iob[1];
243
244    struct list headers[1];
245    struct list cookie_list[1];
246 #if defined(PCRS) || defined(KILLPOPUPS)
247    int is_text;
248 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
249
250    char   *x_forwarded;
251
252    int active;
253
254    /* files associated with this client */
255    struct file_list *blist;   /* blockfile */
256    struct file_list *flist;   /* forwardfile */
257    struct file_list *permissions_list;
258
259
260 #ifdef ACL_FILES
261    struct file_list *alist;   /* aclfile */
262 #endif /* def ACL_FILES */
263
264 #ifdef USE_IMAGE_LIST
265    struct file_list *ilist;   /* imagefile */
266 #endif /* def USE_IMAGE_LIST */
267
268 #ifdef PCRS
269      struct file_list *rlist;   /* Perl re_filterfile */
270 #endif /* def PCRS */
271
272 #ifdef TRUST_FILES
273    struct file_list *tlist;   /* trustfile */
274 #endif /* def TRUST_FILES */
275
276    struct client_state *next;
277 };
278
279
280 struct parsers
281 {
282    char *str;
283    char  len;
284    char *(*parser)(const struct parsers *, char *, struct client_state *);
285 };
286
287
288 struct interceptors
289 {
290    char *str;
291    char  len;
292    char *(*interceptor)(struct http_request *http, struct client_state *csp);
293 };
294
295
296 /* this allows the proxy to permit/block access to any host and/or path */
297
298 struct url_spec
299 {
300    char  *spec;
301    char  *domain;
302    char  *dbuf;
303    char **dvec;
304    int    dcnt;
305    int    toplevel;
306
307    char *path;
308    int   pathlen;
309    int   port;
310 #ifdef REGEX
311    regex_t *preg;
312 #endif
313 };
314
315
316 struct file_list
317 {
318    /*
319     * this is a pointer to the data structures associated with the file.
320     * Read-only once the structure has been created.
321     */
322    void *f;
323    
324    /* Normally NULL.  When we are finished with file (i.e. when we have
325     * loaded a new one), set to a pointer to an unloader function.
326     * Unloader will be called by sweep() (called from main loop) when
327     * all clients using this file are done.  This prevents threading 
328     * problems.
329     */
330    void (*unloader)(void *);
331
332    /* Used internally by sweep().  Do not access from elsewhere. */
333    int active;
334
335 #ifndef SPLIT_PROXY_ARGS
336    /* String to be displayed as part of show-proxy-args display.
337     * Read-only once the structure has been created.
338     */
339    char *proxy_args;
340 #endif /* ndef SPLIT_PROXY_ARGS */
341
342    /* Following variables allow us to check if file has been changed.
343     * Read-only once the structure has been created.
344     */
345    time_t lastmodified;
346    char * filename;
347
348    /* Pointer to next entry in the linked list of all "file_list"s.
349     * This linked list is so that sweep() can navigate it.
350     * Since sweep() can remove items from the list, we must be careful
351     * to only access this value from main thread (when we know sweep
352     * won't be running).
353     */
354    struct file_list *next;
355 };
356
357
358 struct block_spec
359 {
360    struct url_spec url[1];
361    int    reject;
362    struct block_spec *next;
363 };
364
365
366 #define PERMIT_COOKIE_SET    0x0001
367 #define PERMIT_COOKIE_READ   0x0002
368 #define PERMIT_RE_FILTER     0x0004
369 #define PERMIT_POPUPS        0x0008
370
371 struct permissions_spec
372 {
373    struct url_spec           url[1];
374    int                       permissions;
375    struct permissions_spec * next;
376 };
377
378 struct forward_spec
379 {
380    struct url_spec url[1];
381    int   reject;
382    struct gateway gw[1];
383    struct forward_spec *next;
384 };
385
386
387 #ifdef PCRS
388 struct re_filterfile_spec
389 {
390    struct list patterns[1];
391    /* See README.re_filter */
392    pcrs_job *joblist;
393 };
394 #endif /* def PCRS */
395
396
397 #ifdef ACL_FILES
398 #define ACL_PERMIT   1  /* accept connection request */
399 #define ACL_DENY     2  /* reject connection request */
400
401 struct access_control_addr
402 {
403    unsigned long addr;
404    unsigned long mask;
405    unsigned long port;
406 };
407
408 struct access_control_list
409 {
410    struct access_control_addr src[1];
411    struct access_control_addr dst[1];
412
413    short action;
414    struct access_control_list *next;
415 };
416 #endif /* def ACL_FILES */
417
418 #define SZ(X)  (sizeof(X) / sizeof(*X))
419
420 #define WHITEBG   "<body bgcolor=\"#ffffff\" link=\"#000078\" alink=\"#ff0022\" vlink=\"#787878\">\n"
421 #define BODY      "<body bgcolor=\"#f8f8f0\" link=\"#000078\" alink=\"#ff0022\" vlink=\"#787878\">\n"
422 #define BANNER    "<strong>Internet J<small>UNK<i><font color=\"red\">BUSTER</font></i></small></strong>"
423
424 #ifdef FORCE_LOAD
425 #define FORCE_PREFIX "/IJB-FORCE-LOAD"
426 #endif /* def FORCE_LOAD */
427
428 #define HOME_PAGE_URL  "http://ijbswa.sourceforge.net/"
429 #define REDIRECT_URL HOME_PAGE_URL "redirect.php?v=" VERSION "&to="
430
431 static const char CFAIL[] =
432    "HTTP/1.0 503 Connect failed\n"
433    "Content-Type: text/html\n\n"
434    "<html>\n"
435    "<head>\n"
436    "<title>Internet Junkbuster: Connect failed</title>\n"
437    "</head>\n"
438    BODY
439    "<h1><center>"
440    BANNER
441    "</center></h1>"
442    "TCP connection to '%s' failed: %s.\n<br>"
443    "</body>\n"
444    "</html>\n";
445
446 static const char CNXDOM[] =
447    "HTTP/1.0 404 Non-existent domain\n"
448    "Content-Type: text/html\n\n"
449    "<html>\n"
450    "<head>\n"
451    "<title>Internet Junkbuster: Non-existent domain</title>\n"
452    "</head>\n"
453    BODY
454    "<h1><center>"
455    BANNER
456    "</center></h1>"
457    "No such domain: %s\n"
458    "</body>\n"
459    "</html>\n";
460
461 static const char CNOBANNER[] =
462    "HTTP/1.0 200 No Banner\n"
463    "Content-Type: text/html\n\n"
464    "<html>\n"
465    "<head>\n"
466    "<title>Internet Junkbuster: No Banner</title>\n"
467    "</head>\n"
468    BODY
469    "<h1><center>"
470    BANNER
471    "</h1>"
472    "You asked for a banner that this proxy can't produce because either configuration does not permit.\n<br>"
473    "or the URL didn't end with .gif\n"
474    "</center></body>\n"
475    "</html>\n";
476
477 static const char CSUCCEED[] =
478    "HTTP/1.0 200 Connection established\n"
479    "Proxy-Agent: IJ/" VERSION "\n\n";
480
481 static const char CHEADER[] =
482    "HTTP/1.0 400 Invalid header received from browser\n\n";
483
484 static const char SHEADER[] =
485    "HTTP/1.0 502 Invalid header received from server\n\n";
486
487 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
488
489 /*
490  * Hint: You can encode your own GIFs like that:
491  * perl -e 'while (read STDIN, $c, 1) { printf("\\%.3o,", unpack("C", $c)); }'
492  */
493
494 static const char BLANKGIF[] =
495    "HTTP/1.0 200 OK\r\n"
496    "Pragma: no-cache\r\n"
497    "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
498    "Expires:       Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
499    "Content-type: image/gif\r\n\r\n"
500    "GIF89a\001\000\001\000\200\000\000\377\377\377\000\000"
501    "\000!\371\004\001\000\000\000\000,\000\000\000\000\001"
502    "\000\001\000\000\002\002D\001\000;";
503
504 static const char JBGIF[] =
505    "HTTP/1.0 200 OK\r\n"
506    "Pragma: no-cache\r\n"
507    "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
508    "Expires:       Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
509    "Content-type: image/gif\r\n\r\n"
510    "GIF89aD\000\013\000\360\000\000\000\000\000\377\377\377!"
511    "\371\004\001\000\000\001\000,\000\000\000\000D\000\013\000"
512    "\000\002a\214\217\251\313\355\277\000\200G&K\025\316hC\037"
513    "\200\234\230Y\2309\235S\230\266\206\372J\253<\3131\253\271"
514    "\270\215\342\254\013\203\371\202\264\334P\207\332\020o\266"
515    "N\215I\332=\211\312\3513\266:\026AK)\364\370\365aobr\305"
516    "\372\003S\275\274k2\354\254z\347?\335\274x\306^9\374\276"
517    "\037Q\000\000;";
518
519 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
520
521 #if defined(FAST_REDIRECTS) || defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
522
523 static const char HTTP_REDIRECT_TEMPLATE[] =
524       "HTTP/1.0 302 Local Redirect from Junkbuster\r\n" 
525       "Pragma: no-cache\r\n"
526       "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
527       "Expires:       Thu Jul 31, 1997 07:42:22 pm GMT\r\n"
528       "Location: %s\r\n";
529
530 #endif /*  defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
531
532 #ifdef __cplusplus
533 } /* extern "C" */
534 #endif
535
536 #endif /* ndef _PROJECT_H */
537
538 /*
539   Local Variables:
540   tab-width: 3
541   end:
542 */