integrate three pieces of documentation.
[privoxy.git] / jcc.c
1 const char jcc_rcs[] = "$Id: jcc.c,v 1.36 2001/09/10 10:56:15 oes Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
5  *
6  * Purpose     :  Main file.  Contains main() method, main loop, and 
7  *                the main connection-handling function.
8  *
9  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
10  *                IJBSWA team.  http://ijbswa.sourceforge.net
11  *
12  *                Based on the Internet Junkbuster originally written
13  *                by and Copyright (C) 1997 Anonymous Coders and 
14  *                Junkbusters Corporation.  http://www.junkbusters.com
15  *
16  *                This program is free software; you can redistribute it 
17  *                and/or modify it under the terms of the GNU General
18  *                Public License as published by the Free Software
19  *                Foundation; either version 2 of the License, or (at
20  *                your option) any later version.
21  *
22  *                This program is distributed in the hope that it will
23  *                be useful, but WITHOUT ANY WARRANTY; without even the
24  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
25  *                PARTICULAR PURPOSE.  See the GNU General Public
26  *                License for more details.
27  *
28  *                The GNU General Public License should be included with
29  *                this file.  If not, you can view it at
30  *                http://www.gnu.org/copyleft/gpl.html
31  *                or write to the Free Software Foundation, Inc., 59
32  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
33  *
34  * Revisions   :
35  *    $Log: jcc.c,v $
36  *    Revision 1.36  2001/09/10 10:56:15  oes
37  *    Silenced compiler warnings
38  *
39  *    Revision 1.35  2001/07/31 14:44:22  oes
40  *    Deleted unused size parameter from filter_popups()
41  *
42  *    Revision 1.34  2001/07/30 22:08:36  jongfoster
43  *    Tidying up #defines:
44  *    - All feature #defines are now of the form FEATURE_xxx
45  *    - Permanently turned off WIN_GUI_EDIT
46  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
47  *
48  *    Revision 1.33  2001/07/29 19:32:00  jongfoster
49  *    Renaming _main() [mingw32 only] to real_main(), for ANSI compliance.
50  *
51  *    Revision 1.32  2001/07/29 18:47:05  jongfoster
52  *    Adding missing #include "loadcfg.h"
53  *
54  *    Revision 1.31  2001/07/29 12:17:48  oes
55  *    Applied pthread fix by Paul Lieverse
56  *
57  *    Revision 1.30  2001/07/25 22:57:13  jongfoster
58  *    __BEOS__ no longer overrides FEATURE_PTHREAD.
59  *    This is because FEATURE_PTHREAD will soon be widely used, so I
60  *    want to keep it simple.
61  *
62  *    Revision 1.29  2001/07/24 12:47:06  oes
63  *    Applied BeOS support update by Eugenia
64  *
65  *    Revision 1.28  2001/07/23 13:26:12  oes
66  *    Fixed bug in popup-killing for the first read that caused binary garbage to be sent between headers and body
67  *
68  *    Revision 1.27  2001/07/19 19:09:47  haroon
69  *    - Added code to take care of the situation where while processing the first
70  *      server response (which includes the server header), after finding the end
71  *      of the headers we were not looking past the end of the headers for
72  *      content modification. I enabled it for filter_popups.
73  *      Someone else should look to see if other similar operations should be
74  *      done to the discarded portion of the buffer.
75  *
76  *      Note 2001/07/20: No, the other content modification mechanisms will process
77  *                       the whole iob later anyway. --oes
78  *
79  *    Revision 1.26  2001/07/18 12:31:36  oes
80  *    cosmetics
81  *
82  *    Revision 1.25  2001/07/15 19:43:49  jongfoster
83  *    Supports POSIX threads.
84  *    Also removed some unused #includes.
85  *
86  *    Revision 1.24  2001/07/13 14:00:40  oes
87  *     - Generic content modification scheme:
88  *       Each feature has its own applicability flag that is set
89  *       from csp->action->flags.
90  *       Replaced the "filtering" int flag , by a function pointer
91  *       "content_filter" to the function that will do the content
92  *       modification. If it is != NULL, the document will be buffered
93  *       and processed through *content_filter, which must set
94  *       csp->content_length and return a modified copy of the body
95  *       or return NULL (on failiure).
96  *     - Changed csp->is_text to the more generic bitmap csp->content_type
97  *       which can currently take the valued CT_TEXT or CT_GIF
98  *     - Reformatting etc
99  *     - Removed all #ifdef PCRS
100  *
101  *    Revision 1.23  2001/07/02 02:28:25  iwanttokeepanon
102  *    Added "#ifdef ACL_FILES" conditional compilation to line 1291 to exclude
103  *    the `block_acl' call.  This prevents a compilation error when the user
104  *    does not wish to use the "ACL" feature.
105  *
106  *    Revision 1.22  2001/06/29 21:45:41  oes
107  *    Indentation, CRLF->LF, Tab-> Space
108  *
109  *    Revision 1.21  2001/06/29 13:29:36  oes
110  *    - Cleaned up, improved comments
111  *    - Unified all possible interceptors (CGI,
112  *      block, trust, fast_redirect) in one
113  *      place, with one (CGI) answer generation
114  *      mechansim. Much clearer now.
115  *    - Removed the GIF image generation, which
116  *      is now done in filters.c:block_url()
117  *    - Made error conditions like domain lookup
118  *      failiure or (various) problems while talking
119  *      to the server use cgi.c:error_response()
120  *      instead of generating HTML/HTTP in chat() (yuck!)
121  *    - Removed logentry from cancelled commit
122  *
123  *    Revision 1.20  2001/06/09 10:55:28  jongfoster
124  *    Changing BUFSIZ ==> BUFFER_SIZE
125  *
126  *    Revision 1.19  2001/06/07 23:12:52  jongfoster
127  *    Replacing function pointer in struct gateway with a directly
128  *    called function forwarded_connect().
129  *    Replacing struct gateway with struct forward_spec
130  *
131  *    Revision 1.18  2001/06/03 19:12:16  oes
132  *    introduced new cgi handling
133  *
134  *    Revision 1.17  2001/06/01 20:07:23  jongfoster
135  *    Now uses action +image-blocker{} rather than config->tinygif
136  *
137  *    Revision 1.16  2001/06/01 18:49:17  jongfoster
138  *    Replaced "list_share" with "list" - the tiny memory gain was not
139  *    worth the extra complexity.
140  *
141  *    Revision 1.15  2001/05/31 21:24:47  jongfoster
142  *    Changed "permission" to "action" throughout.
143  *    Removed DEFAULT_USER_AGENT - it must now be specified manually.
144  *    Moved vanilla wafer check into chat(), since we must now
145  *    decide whether or not to add it based on the URL.
146  *
147  *    Revision 1.14  2001/05/29 20:14:01  joergs
148  *    AmigaOS bugfix: PCRS needs a lot of stack, stacksize for child threads
149  *    increased.
150  *
151  *    Revision 1.13  2001/05/29 09:50:24  jongfoster
152  *    Unified blocklist/imagelist/permissionslist.
153  *    File format is still under discussion, but the internal changes
154  *    are (mostly) done.
155  *
156  *    Also modified interceptor behaviour:
157  *    - We now intercept all URLs beginning with one of the following
158  *      prefixes (and *only* these prefixes):
159  *        * http://i.j.b/
160  *        * http://ijbswa.sf.net/config/
161  *        * http://ijbswa.sourceforge.net/config/
162  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
163  *    - Internal changes so that intercepted and fast redirect pages
164  *      are not replaced with an image.
165  *    - Interceptors now have the option to send a binary page direct
166  *      to the client. (i.e. ijb-send-banner uses this)
167  *    - Implemented show-url-info interceptor.  (Which is why I needed
168  *      the above interceptors changes - a typical URL is
169  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
170  *      The previous mechanism would not have intercepted that, and
171  *      if it had been intercepted then it then it would have replaced
172  *      it with an image.)
173  *
174  *    Revision 1.12  2001/05/27 22:17:04  oes
175  *
176  *    - re_process_buffer no longer writes the modified buffer
177  *      to the client, which was very ugly. It now returns the
178  *      buffer, which it is then written by chat.
179  *
180  *    - content_length now adjusts the Content-Length: header
181  *      for modified documents rather than crunch()ing it.
182  *      (Length info in csp->content_length, which is 0 for
183  *      unmodified documents)
184  *
185  *    - For this to work, sed() is called twice when filtering.
186  *
187  *    Revision 1.11  2001/05/26 17:27:53  jongfoster
188  *    Added support for CLF and fixed LOG_LEVEL_LOG.
189  *    Also did CRLF->LF fix of my previous patch.
190  *
191  *    Revision 1.10  2001/05/26 15:26:15  jongfoster
192  *    ACL feature now provides more security by immediately dropping
193  *    connections from untrusted hosts.
194  *
195  *    Revision 1.9  2001/05/26 00:28:36  jongfoster
196  *    Automatic reloading of config file.
197  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
198  *    Most of the global variables have been moved to a new
199  *    struct configuration_spec, accessed through csp->config->globalname
200  *    Most of the globals remaining are used by the Win32 GUI.
201  *
202  *    Revision 1.8  2001/05/25 22:43:18  jongfoster
203  *    Fixing minor memory leak and buffer overflow.
204  *
205  *    Revision 1.7  2001/05/25 22:34:30  jongfoster
206  *    Hard tabs->Spaces
207  *
208  *    Revision 1.6  2001/05/23 00:13:58  joergs
209  *    AmigaOS support fixed.
210  *
211  *    Revision 1.5  2001/05/22 18:46:04  oes
212  *
213  *    - Enabled filtering banners by size rather than URL
214  *      by adding patterns that replace all standard banner
215  *      sizes with the "Junkbuster" gif to the re_filterfile
216  *
217  *    - Enabled filtering WebBugs by providing a pattern
218  *      which kills all 1x1 images
219  *
220  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
221  *      which is selected by the (nonstandard and therefore
222  *      capital) letter 'U' in the option string.
223  *      It causes the quantifiers to be ungreedy by default.
224  *      Appending a ? turns back to greedy (!).
225  *
226  *    - Added a new interceptor ijb-send-banner, which
227  *      sends back the "Junkbuster" gif. Without imagelist or
228  *      MSIE detection support, or if tinygif = 1, or the
229  *      URL isn't recognized as an imageurl, a lame HTML
230  *      explanation is sent instead.
231  *
232  *    - Added new feature, which permits blocking remote
233  *      script redirects and firing back a local redirect
234  *      to the browser.
235  *      The feature is conditionally compiled, i.e. it
236  *      can be disabled with --disable-fast-redirects,
237  *      plus it must be activated by a "fast-redirects"
238  *      line in the config file, has its own log level
239  *      and of course wants to be displayed by show-proxy-args
240  *      Note: Boy, all the #ifdefs in 1001 locations and
241  *      all the fumbling with configure.in and acconfig.h
242  *      were *way* more work than the feature itself :-(
243  *
244  *    - Because a generic redirect template was needed for
245  *      this, tinygif = 3 now uses the same.
246  *
247  *    - Moved GIFs, and other static HTTP response templates
248  *      to project.h
249  *
250  *    - Some minor fixes
251  *
252  *    - Removed some >400 CRs again (Jon, you really worked
253  *      a lot! ;-)
254  *
255  *    Revision 1.4  2001/05/21 19:34:01  jongfoster
256  *    Made failure to bind() a fatal error.
257  *
258  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
259  *    Version 2.9.4 checkin.
260  *    - Merged popupfile and cookiefile, and added control over PCRS
261  *      filtering, in new "permissionsfile".
262  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
263  *      file error you now get a message box (in the Win32 GUI) rather
264  *      than the program exiting with no explanation.
265  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
266  *      skipping.
267  *    - Removed tabs from "config"
268  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
269  *    - Bumped up version number.
270  *
271  *    Revision 1.2  2001/05/17 22:34:44  oes
272  *     - Added hint on GIF char array generation to jcc.c
273  *     - Cleaned CRLF's from the sources and related files
274  *     - Repaired logging for REF and FRC
275  *
276  *    Revision 1.1.1.1  2001/05/15 13:58:56  oes
277  *    Initial import of version 2.9.3 source tree
278  *
279  *
280  *********************************************************************/
281 \f
282
283 #include "config.h"
284
285 #include <stdio.h>
286 #include <sys/types.h>
287 #include <stdlib.h>
288 #include <string.h>
289 #include <signal.h>
290 #include <fcntl.h>
291 #include <errno.h>
292
293 #ifdef FEATURE_PTHREAD
294 #include <pthread.h>
295 #endif /* def FEATURE_PTHREAD */
296
297 #ifdef _WIN32
298 # ifndef FEATURE_PTHREAD
299 #  include <windows.h>
300 #  include <process.h>
301 # endif /* ndef FEATURE_PTHREAD */
302
303 # include "win32.h"
304 # ifndef _WIN_CONSOLE
305 #  include "w32log.h"
306 # endif /* ndef _WIN_CONSOLE */
307
308 #else /* ifndef _WIN32 */
309
310 # include <unistd.h>
311 # include <sys/time.h>
312 # include <sys/wait.h>
313 # include <sys/stat.h>
314 # include <signal.h>
315
316 # ifdef __BEOS__
317 #  include <socket.h>  /* BeOS has select() for sockets only. */
318 #  include <OS.h>      /* declarations for threads and stuff. */
319 # endif
320
321 # ifndef FD_ZERO
322 #  include <select.h>
323 # endif
324
325 #endif
326
327 #include "project.h"
328 #include "list.h"
329 #include "jcc.h"
330 #include "filters.h"
331 #include "loaders.h"
332 #include "showargs.h"
333 #include "parsers.h"
334 #include "killpopup.h"
335 #include "miscutil.h"
336 #include "errlog.h"
337 #include "jbsockets.h"
338 #include "gateway.h"
339 #include "actions.h"
340 #include "cgi.h"
341 #include "loadcfg.h"
342
343 const char jcc_h_rcs[] = JCC_H_VERSION;
344 const char project_h_rcs[] = PROJECT_H_VERSION;
345
346 struct client_state  clients[1];
347 struct file_list     files[1];
348
349 #ifdef FEATURE_STATISTICS
350 int urls_read     = 0;     /* total nr of urls read inc rejected */
351 int urls_rejected = 0;     /* total nr of urls rejected */
352 #endif /* def FEATURE_STATISTICS */
353
354
355 static void listen_loop(void);
356 static void chat(struct client_state *csp);
357 #ifdef AMIGA
358 void serve(struct client_state *csp);
359 #else /* ifndef AMIGA */
360 static void serve(struct client_state *csp);
361 #endif /* def AMIGA */
362
363 #ifdef __BEOS__
364 static int32 server_thread(void *data);
365 #endif /* def __BEOS__ */
366
367 #ifdef _WIN32
368 #define sleep(N)  Sleep(((N) * 1000))
369 #endif
370
371
372 /* The vanilla wafer. */
373 static const char VANILLA_WAFER[] =
374    "NOTICE=TO_WHOM_IT_MAY_CONCERN_"
375    "Do_not_send_me_any_copyrighted_information_other_than_the_"
376    "document_that_I_am_requesting_or_any_of_its_necessary_components._"
377    "In_particular_do_not_send_me_any_cookies_that_"
378    "are_subject_to_a_claim_of_copyright_by_anybody._"
379    "Take_notice_that_I_refuse_to_be_bound_by_any_license_condition_"
380    "(copyright_or_otherwise)_applying_to_any_cookie._";
381
382
383 /*********************************************************************
384  *
385  * Function    :  chat
386  *
387  * Description :  Once a connection to the client has been accepted,
388  *                this function is called (via serve()) to handle the
389  *                main business of the communication.  When this 
390  *                function returns, the caller must close the client
391  *                socket handle.
392  *
393  * Parameters  :
394  *          1  :  csp = Current client state (buffers, headers, etc...)
395  *
396  * Returns     :  On success, the number of bytes written are returned (zero
397  *                indicates nothing was written).  On error, -1 is returned,
398  *                and errno is set appropriately.  If count is zero and the
399  *                file descriptor refers to a regular file, 0 will be
400  *                returned without causing any other effect.  For a special
401  *                file, the results are not portable.
402  *
403  *********************************************************************/
404 static void chat(struct client_state *csp)
405 {
406 /*
407  * This next lines are a little ugly, but they simplifies the if statements
408  * below.  Basically if TOGGLE, then we want the if to test "csp->toggled_on",
409  * else we don't.  And if FEATURE_FORCE_LOAD, then we want the if to test
410  * "csp->toggled_on", else we don't
411  */
412 #ifdef FEATURE_TOGGLE
413 #   define IS_TOGGLED_ON_AND (csp->toggled_on) &&
414 #else /* ifndef FEATURE_TOGGLE */
415 #   define IS_TOGGLED_ON_AND
416 #endif /* ndef FEATURE_TOGGLE */
417 #ifdef FEATURE_FORCE_LOAD
418 #   define IS_NOT_FORCED_AND (!csp->force) && 
419 #else /* ifndef FEATURE_FORCE_LOAD */
420 #   define IS_NOT_FORCED_AND
421 #endif /* def FEATURE_FORCE_LOAD */
422
423 #define IS_ENABLED_AND   IS_TOGGLED_ON_AND IS_NOT_FORCED_AND
424
425    char buf[BUFFER_SIZE];
426    char *hdr, *p, *req;
427    fd_set rfds;
428    int n, maxfd, server_body;
429    int ms_iis5_hack = 0;
430    int byte_count = 0;
431    const struct forward_spec * fwd;
432    struct http_request *http;
433 #ifdef FEATURE_KILL_POPUPS
434    int block_popups;         /* bool, 1==will block popups */
435    int block_popups_now = 0; /* bool, 1==currently blocking popups */
436 #endif /* def FEATURE_KILL_POPUPS */
437
438    int pcrs_filter;        /* bool, 1==will filter through pcrs */
439    int gif_deanimate;      /* bool, 1==will deanimate gifs */
440
441    /* Function that does the content filtering for the current request */
442    char *(*content_filter)() = NULL; 
443
444    /* Skeleton for HTTP response, if we should intercept the request */
445    struct http_response *rsp;
446
447    http = csp->http;
448
449    /*
450     * Read the client's request.  Note that since we're not using select() we
451     * could get blocked here if a client connected, then didn't say anything!
452     */
453
454    while (FOREVER)
455    {
456       n = read_socket(csp->cfd, buf, sizeof(buf));
457
458       if (n <= 0) break;      /* error! */
459
460       add_to_iob(csp, buf, n);
461
462       req = get_header(csp);
463
464       if (req == NULL)
465       {
466          break;    /* no HTTP request! */
467       }
468
469       if (*req == '\0')
470       {
471          continue;   /* more to come! */
472       }
473  
474 #ifdef FEATURE_FORCE_LOAD
475       /* If this request contains the FORCE_PREFIX,
476        * better get rid of it now and set the force flag --oes
477        */
478
479       if (strstr(req, FORCE_PREFIX))
480       {
481          strclean(req, FORCE_PREFIX);
482          log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", req);
483          csp->force = 1;
484       } 
485       else
486       {
487          csp->force = 0;
488       }
489 #endif /* def FEATURE_FORCE_LOAD */
490   
491       parse_http_request(req, http, csp);
492       freez(req);
493       break;
494    }
495
496    if (http->cmd == NULL)
497    {
498       strcpy(buf, CHEADER);
499       write_socket(csp->cfd, buf, strlen(buf));
500
501       log_error(LOG_LEVEL_CLF, "%s - - [%T] \" \" 400 0", csp->ip_addr_str);
502
503       return;
504    }
505
506    /* decide how to route the HTTP request */
507
508    if ((fwd = forward_url(http, csp)) == NULL)
509    {
510       log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!?  This can't happen!");
511       /* Never get here - LOG_LEVEL_FATAL causes program exit */
512    }
513
514    /* build the http request to send to the server
515     * we have to do one of the following:
516     *
517     * create = use the original HTTP request to create a new
518     *          HTTP request that has only the path component
519     *          without the http://domainspec
520     * pass   = pass the original HTTP request unchanged
521     *
522     * drop   = drop the HTTP request
523     *
524     * here's the matrix:
525     *                        SSL
526     *                    0        1
527     *                +--------+--------+
528     *                |        |        |
529     *             0  | create | drop   |
530     *                |        |        |
531     *  Forwarding    +--------+--------+
532     *                |        |        |
533     *             1  | pass   | pass   |
534     *                |        |        |
535     *                +--------+--------+
536     *
537     */
538
539    if (fwd->forward_host)
540    {
541       /* if forwarding, just pass the request as is */
542       enlist(csp->headers, http->cmd);
543    }
544    else
545    {
546       if (http->ssl == 0)
547       {
548          /* otherwise elide the host information from the url */
549          p = NULL;
550          p = strsav(p, http->gpc);
551          p = strsav(p, " ");
552          p = strsav(p, http->path);
553          p = strsav(p, " ");
554          p = strsav(p, http->ver);
555          enlist(csp->headers, p);
556          freez(p);
557       }
558    }
559
560    /* decide what we're to do with cookies */
561
562 #ifdef FEATURE_TOGGLE
563    if (!csp->toggled_on)
564    {
565       /* Most compatible set of actions (i.e. none) */
566       init_current_action(csp->action);
567    }
568    else
569 #endif /* ndef FEATURE_TOGGLE */
570    {
571       url_actions(http, csp);
572    }
573
574 #ifdef FEATURE_COOKIE_JAR
575    /*
576     * If we're logging cookies in a cookie jar, and the user has not
577     * supplied any wafers, and the user has not told us to suppress the
578     * vanilla wafer, then send the vanilla wafer.
579     */
580    if ((csp->config->jarfile != NULL)
581        && (csp->action->multi[ACTION_MULTI_WAFER]->next == NULL)
582        && ((csp->action->flags & ACTION_VANILLA_WAFER) != 0))
583    {
584       enlist(csp->action->multi[ACTION_MULTI_WAFER], VANILLA_WAFER);
585    }
586 #endif /* def FEATURE_COOKIE_JAR */
587
588 #ifdef FEATURE_KILL_POPUPS
589    block_popups               = ((csp->action->flags & ACTION_NO_POPUPS) != 0);
590 #endif /* def FEATURE_KILL_POPUPS */
591
592    pcrs_filter                = (csp->rlist != NULL) &&  /* There are expressions to be used */
593                                 ((csp->action->flags & ACTION_FILTER) != 0);
594
595    gif_deanimate              = ((csp->action->flags & ACTION_DEANIMATE) != 0);
596
597    /* grab the rest of the client's headers */
598
599    while (FOREVER)
600    {
601       if ( ( p = get_header(csp) ) && ( *p == '\0' ) )
602       {
603          n = read_socket(csp->cfd, buf, sizeof(buf));
604          if (n <= 0)
605          {
606             log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
607             return;
608          }
609          add_to_iob(csp, buf, n);
610          continue;
611       }
612
613       if (p == NULL) break;
614
615       enlist(csp->headers, p);
616       freez(p);
617    }
618
619    /* We have a request. */
620
621    hdr = sed(client_patterns, add_client_headers, csp);
622    destroy_list(csp->headers);
623
624    /* 
625     * Now, check to see if we need to intercept it, i.e.
626     * If
627     */
628  
629    if (
630        /* a CGI call was detected and answered */
631          (NULL != (rsp = dispatch_cgi(csp))) 
632
633        /* or we are enabled and... */
634        || (IS_ENABLED_AND (
635
636             /* ..the request was blocked */
637             ( NULL != (rsp = block_url(csp)))
638
639           /* ..or untrusted */
640 #ifdef FEATURE_TRUST
641           || ( NULL != (rsp = trust_url(csp)))
642 #endif /* def FEATURE_TRUST */
643
644           /* ..or a fast redirect kicked in */
645 #ifdef FEATURE_FAST_REDIRECTS
646           || (((csp->action->flags & ACTION_FAST_REDIRECTS) != 0) && 
647                      (NULL != (rsp = redirect_url(csp))))
648 #endif /* def FEATURE_FAST_REDIRECTS */
649                  ))
650         )
651    {
652       /* Write the answer to the client */
653       if ((write_socket(csp->cfd, rsp->head, rsp->head_length) != rsp->head_length)
654              || (write_socket(csp->cfd, rsp->body, rsp->content_length) != rsp->content_length))
655       { 
656          log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
657       }
658
659 #ifdef FEATURE_STATISTICS
660       /* Count as a rejected request */
661       csp->rejected = 1;
662 #endif /* def FEATURE_STATISTICS */
663
664       /* Log (FIXME: All intercept reasons apprear as "crunch" with Status 200) */
665       log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
666       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3", csp->ip_addr_str, http->cmd); 
667
668       /* Clean up and return */
669       free_http_response(rsp);
670       freez(hdr);
671       return;
672    }
673
674    log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
675
676    if (fwd->forward_host)
677    {
678       log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
679                fwd->forward_host, fwd->forward_port, http->hostport);
680    }
681    else
682    {
683       log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
684    }
685
686    /* here we connect to the server, gateway, or the forwarder */
687
688    csp->sfd = forwarded_connect(fwd, http, csp);
689
690    if (csp->sfd < 0)
691    {
692       log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
693                 http->hostport);
694
695       if (errno == EINVAL)
696       {
697            rsp = error_response(csp, "no-such-domain", errno);
698
699          log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 404 0", 
700                    csp->ip_addr_str, http->cmd);
701       }
702       else
703       {
704            rsp = error_response(csp, "connect-failed", errno);
705
706          log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0", 
707                    csp->ip_addr_str, http->cmd);
708       }
709
710       /* Write the answer to the client */
711       if(rsp)
712         {
713          if ((write_socket(csp->cfd, rsp->head, rsp->head_length) != rsp->head_length)
714                 || (write_socket(csp->cfd, rsp->body, rsp->content_length) != rsp->content_length))
715          { 
716             log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
717          }
718       }
719
720       free_http_response(rsp);
721       freez(hdr);
722       return;
723    }
724
725    log_error(LOG_LEVEL_CONNECT, "OK");
726
727    if (fwd->forward_host || (http->ssl == 0))
728    {
729       /* write the client's (modified) header to the server
730        * (along with anything else that may be in the buffer)
731        */
732
733       n = strlen(hdr);
734
735       if ((write_socket(csp->sfd, hdr, n) != n)
736           || (flush_socket(csp->sfd, csp   ) <  0))
737       {
738          log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
739                     http->hostport);
740
741          log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0", 
742                    csp->ip_addr_str, http->cmd); 
743
744          rsp = error_response(csp, "connect-failed", errno);
745
746          if(rsp)
747          {
748             if ((write_socket(csp->cfd, rsp->head, n) != n)
749                 || (write_socket(csp->cfd, rsp->body, rsp->content_length) != rsp->content_length))
750             { 
751                log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
752             }
753          }
754
755          free_http_response(rsp);
756          freez(hdr);
757          return;
758       }
759    }
760    else
761    {
762       /*
763        * We're running an SSL tunnel and we're not forwarding,
764        * so just send the "connect succeeded" message to the
765        * client, flush the rest, and get out of the way.
766        */
767       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 2\n", 
768                 csp->ip_addr_str, http->cmd); 
769
770       if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0)
771       {
772          freez(hdr);
773          return;
774       }
775       IOB_RESET(csp);
776    }
777
778    /* we're finished with the client's header */
779    freez(hdr);
780
781    maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
782
783    /* pass data between the client and server
784     * until one or the other shuts down the connection.
785     */
786
787    server_body = 0;
788
789    while (FOREVER)
790    {
791       FD_ZERO(&rfds);
792
793       FD_SET(csp->cfd, &rfds);
794       FD_SET(csp->sfd, &rfds);
795
796       n = select(maxfd+1, &rfds, NULL, NULL, NULL);
797
798       if (n < 0)
799       {
800          log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
801          return;
802       }
803
804       /* this is the body of the browser's request
805        * just read it and write it.
806        */
807
808       if (FD_ISSET(csp->cfd, &rfds))
809       {
810          n = read_socket(csp->cfd, buf, sizeof(buf));
811
812          if (n <= 0)
813          {
814             break; /* "game over, man" */
815          }
816
817          if (write_socket(csp->sfd, buf, n) != n)
818          {
819             log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
820             return;
821          }
822          continue;
823       }
824
825       /*
826        * The server wants to talk.  It could be the header or the body.
827        * If `hdr' is null, then it's the header otherwise it's the body.
828        * FIXME: Does `hdr' really mean `host'? No.
829        */
830
831
832       if (FD_ISSET(csp->sfd, &rfds))
833       {
834          fflush( 0 );
835          n = read_socket(csp->sfd, buf, sizeof(buf) - 1);
836
837          if (n < 0)
838          {
839             log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
840
841             log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0", 
842                       csp->ip_addr_str, http->cmd); 
843
844             rsp = error_response(csp, "connect-failed", errno);
845
846             if(rsp)
847             {
848                if ((write_socket(csp->cfd, rsp->head, rsp->head_length) != rsp->head_length)
849                     || (write_socket(csp->cfd, rsp->body, rsp->content_length) != rsp->content_length))
850                { 
851                   log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
852                            }
853                         }
854
855             free_http_response(rsp);
856             return;
857          }
858
859          /* Add a trailing zero.  This lets filter_popups
860           * use string operations.
861           */
862          buf[n] = '\0';
863
864 #ifdef FEATURE_KILL_POPUPS
865          /* Filter the popups on this read. */
866          if (block_popups_now)
867          {
868             filter_popups(buf);
869          }
870 #endif /* def FEATURE_KILL_POPUPS */
871
872          /* Normally, this would indicate that we've read
873           * as much as the server has sent us and we can
874           * close the client connection.  However, Microsoft
875           * in its wisdom has released IIS/5 with a bug that
876           * prevents it from sending the trailing \r\n in
877           * a 302 redirect header (and possibly other headers).
878           * To work around this if we've haven't parsed
879           * a full header we'll append a trailing \r\n
880           * and see if this now generates a valid one.
881           *
882           * This hack shouldn't have any impacts.  If we've
883           * already transmitted the header or if this is a
884           * SSL connection, then we won't bother with this
885           * hack.  So we only work on partially received
886           * headers.  If we append a \r\n and this still
887           * doesn't generate a valid header, then we won't
888           * transmit anything to the client.
889           */
890          if (n == 0)
891          {
892             
893             if (server_body || http->ssl)
894             {
895                /*
896                 * If we have been buffering up the document,
897                 * now is the time to apply content modification
898                 * and send the result to the client.
899                 */
900                if (content_filter)
901                {
902                   /*
903                    * If the content filter fails, use the original
904                    * buffer and length.
905                    * (see p != NULL ? p : csp->iob->cur below)
906                    */
907                   if (NULL == (p = (*content_filter)(csp)))
908                   {
909                      csp->content_length = csp->iob->eod - csp->iob->cur;
910                   }
911
912                   hdr = sed(server_patterns, add_server_headers, csp);
913                   n = strlen(hdr);
914
915                   if ((write_socket(csp->cfd, hdr, n) != n)
916                       || (write_socket(csp->cfd, p != NULL ? p : csp->iob->cur, csp->content_length) != csp->content_length))
917                   {
918                      log_error(LOG_LEVEL_CONNECT, "write modified content to client failed: %E");
919                      return;
920                   }
921
922                   freez(hdr);
923                   freez(p);
924                }
925
926                break; /* "game over, man" */
927             }
928
929             /*
930              * This is NOT the body, so 
931              * Let's pretend the server just sent us a blank line.
932              */
933             n = sprintf(buf, "\r\n");
934
935             /*
936              * Now, let the normal header parsing algorithm below do its
937              * job.  If it fails, we'll exit instead of continuing.
938              */
939
940             ms_iis5_hack = 1;
941          }
942
943          /*
944           * If this is an SSL connection or we're in the body
945           * of the server document, just write it to the client,
946           * unless we need to buffer the body for later content-filtering
947           */
948
949          if (server_body || http->ssl)
950          {
951             if (content_filter)
952             {
953                add_to_iob(csp, buf, n); 
954             }
955             else
956             {
957                if (write_socket(csp->cfd, buf, n) != n)
958                {
959                   log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
960                   return;
961                }
962             }
963             byte_count += n;
964             continue;
965          }
966          else
967          {
968             /* we're still looking for the end of the
969              * server's header ... (does that make header
970              * parsing an "out of body experience" ?
971              */
972
973             /* buffer up the data we just read */
974             add_to_iob(csp, buf, n);
975
976             /* get header lines from the iob */
977
978             while ((p = get_header(csp)))
979             {
980                if (*p == '\0')
981                {
982                   /* see following note */
983                   break;
984                }
985                enlist(csp->headers, p);
986                freez(p);
987             }
988
989             /* NOTE: there are no "empty" headers so
990              * if the pointer `p' is not NULL we must
991              * assume that we reached the end of the
992              * buffer before we hit the end of the header.
993              */
994
995             if (p)
996             {
997                if (ms_iis5_hack)
998                {
999                   /* Well, we tried our MS IIS/5
1000                    * hack and it didn't work.
1001                    * The header is incomplete
1002                    * and there isn't anything
1003                    * we can do about it.
1004                    */
1005                   break;
1006                }
1007                else
1008                {
1009                   /* Since we have to wait for
1010                    * more from the server before
1011                    * we can parse the headers
1012                    * we just continue here.
1013                    */
1014                   continue;
1015                }
1016             }
1017
1018             /* we have now received the entire header.
1019              * filter it and send the result to the client
1020              */
1021
1022             hdr = sed(server_patterns, add_server_headers, csp);
1023             n   = strlen(hdr);
1024
1025             /* write the server's (modified) header to
1026              * the client (along with anything else that
1027              * may be in the buffer)
1028              */
1029
1030 #ifdef FEATURE_KILL_POPUPS
1031             /* Start blocking popups if appropriate. */
1032
1033             if ((csp->content_type & CT_TEXT) &&  /* It's a text / * MIME-Type */
1034                 !http->ssl    &&                  /* We talk plaintext */
1035                 block_popups)                     /* Policy allows */
1036             {
1037                block_popups_now = 1;
1038                /*
1039                 * Filter the part of the body that came in the same read
1040                 * as the last headers:
1041                 */
1042                filter_popups(csp->iob->cur);
1043             }
1044
1045 #endif /* def FEATURE_KILL_POPUPS */
1046
1047             /* Buffer and pcrs filter this if appropriate. */
1048
1049             if ((csp->content_type & CT_TEXT) &&  /* It's a text / * MIME-Type */
1050                 !http->ssl    &&                  /* We talk plaintext */
1051                 pcrs_filter)                      /* Policy allows */
1052             {
1053                content_filter = pcrs_filter_response;
1054             }
1055
1056             /* Buffer and gif_deanimate this if appropriate. */
1057
1058             if ((csp->content_type & CT_GIF)  &&  /* It's a image/gif MIME-Type */
1059                 !http->ssl    &&                  /* We talk plaintext */
1060                 gif_deanimate)                    /* Policy allows */
1061             {
1062                content_filter = gif_deanimate_response;
1063             }
1064
1065
1066             /*
1067              * Only write if we're not buffering for content modification
1068              */
1069             if (!content_filter && ((write_socket(csp->cfd, hdr, n) != n)
1070                 || (n = flush_socket(csp->cfd, csp) < 0)))
1071             {
1072                log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
1073
1074                /* the write failed, so don't bother
1075                 * mentioning it to the client...
1076                 * it probably can't hear us anyway.
1077                 */
1078                freez(hdr);
1079                return;
1080             }
1081
1082             if(!content_filter) byte_count += n;
1083
1084             /* we're finished with the server's header */
1085
1086             freez(hdr);
1087             server_body = 1;
1088
1089             /* If this was a MS IIS/5 hack then it means
1090              * the server has already closed the
1091              * connection.  Nothing more to read.  Time
1092              * to bail.
1093              */
1094             if (ms_iis5_hack)
1095             {
1096                break;
1097             }
1098          }
1099          continue;
1100       }
1101
1102       return; /* huh? we should never get here */
1103    }
1104
1105    log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %d", 
1106              csp->ip_addr_str, http->cmd, byte_count); 
1107 }
1108
1109
1110 /*********************************************************************
1111  *
1112  * Function    :  serve
1113  *
1114  * Description :  This is little more than chat.  We only "serve" to
1115  *                to close any socket that chat may have opened.
1116  *
1117  * Parameters  :
1118  *          1  :  csp = Current client state (buffers, headers, etc...)
1119  *
1120  * Returns     :  N/A
1121  *
1122  *********************************************************************/
1123 #ifdef AMIGA
1124 void serve(struct client_state *csp)
1125 #else /* ifndef AMIGA */
1126 static void serve(struct client_state *csp)
1127 #endif /* def AMIGA */
1128 {
1129    chat(csp);
1130    close_socket(csp->cfd);
1131
1132    if (csp->sfd >= 0)
1133    {
1134       close_socket(csp->sfd);
1135    }
1136
1137    csp->active = 0;
1138
1139 }
1140
1141
1142 #ifdef __BEOS__
1143 /*********************************************************************
1144  *
1145  * Function    :  server_thread
1146  *
1147  * Description :  We only exist to call `serve' in a threaded environment.
1148  *
1149  * Parameters  :
1150  *          1  :  data = Current client state (buffers, headers, etc...)
1151  *
1152  * Returns     :  Always 0.
1153  *
1154  *********************************************************************/
1155 static int32 server_thread(void *data)
1156 {
1157    serve((struct client_state *) data);
1158    return 0;
1159
1160 }
1161 #endif
1162
1163
1164 /*********************************************************************
1165  *
1166  * Function    :  main
1167  *
1168  * Description :  Load the config file and start the listen loop.
1169  *                This function is a lot more *sane* with the `load_config'
1170  *                and `listen_loop' functions; although it stills does
1171  *                a *little* too much for my taste.
1172  *
1173  * Parameters  :
1174  *          1  :  argc = Number of parameters (including $0).
1175  *          2  :  argv = Array of (char *)'s to the parameters.
1176  *
1177  * Returns     :  1 if : can't open config file, unrecognized directive,
1178  *                stats requested in multi-thread mode, can't open the
1179  *                log file, can't open the jar file, listen port is invalid,
1180  *                any load fails, and can't bind port.
1181  *
1182  *                Else main never returns, the process must be signaled
1183  *                to terminate execution.  Or, on Windows, use the 
1184  *                "File", "Exit" menu option.
1185  *
1186  *********************************************************************/
1187 #ifdef __MINGW32__
1188 int real_main(int argc, const char *argv[])
1189 #else
1190 int main(int argc, const char *argv[])
1191 #endif
1192 {
1193    configfile =
1194 #ifdef AMIGA
1195    "AmiTCP:db/junkbuster/config"
1196 #elif !defined(_WIN32)
1197    "config"
1198 #else
1199    "junkbstr.txt"
1200 #endif
1201       ;
1202
1203 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
1204    if ((argc >= 2) && (strcmp(argv[1], "--help")==0))
1205    {
1206       printf("JunkBuster proxy version " VERSION ".\n\n"
1207          "Usage: %s [configfile]\n\n"
1208          "See " HOME_PAGE_URL " for details.\n"
1209          "This program is distributed under the GNU GPL, version 2 or later.\n",
1210          argv[0]);
1211       exit(2);
1212    }
1213    if ((argc >= 2) && (strcmp(argv[1], "--version")==0))
1214    {
1215       printf(VERSION "\n");
1216       exit(2);
1217    }
1218 #endif /* !defined(_WIN32) || defined(_WIN_CONSOLE) */
1219
1220    Argc = argc;
1221    Argv = argv;
1222
1223    if (argc > 1)
1224    {
1225       configfile = argv[1];
1226    }
1227
1228    files->next = NULL;
1229
1230 #ifdef AMIGA
1231    InitAmiga();
1232 #elif defined(_WIN32)
1233    InitWin32();
1234 #endif
1235
1236
1237 #ifndef _WIN32
1238    signal(SIGPIPE, SIG_IGN);
1239    signal(SIGCHLD, SIG_IGN);
1240
1241 #else /* ifdef _WIN32 */
1242 # ifdef _WIN_CONSOLE
1243    /*
1244     * We *are* in a windows console app.
1245     * Print a verbose messages about FAQ's and such
1246     */
1247    printf(win32_blurb);
1248 # endif /* def _WIN_CONSOLE */
1249 #endif /* def _WIN32 */
1250
1251
1252    listen_loop();
1253
1254    /* NOTREACHED */
1255    return(-1);
1256
1257 }
1258
1259
1260 /*********************************************************************
1261  *
1262  * Function    :  listen_loop
1263  *
1264  * Description :  bind the listen port and enter a "FOREVER" listening loop.
1265  *
1266  * Parameters  :  N/A
1267  *
1268  * Returns     :  Never.
1269  *
1270  *********************************************************************/
1271 static void listen_loop(void)
1272 {
1273    struct client_state *csp = NULL;
1274    int bfd;
1275    struct configuration_spec * config;
1276
1277    config = load_config();
1278
1279    log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1280              config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1281
1282    bfd = bind_port(config->haddr, config->hport);
1283
1284    if (bfd < 0)
1285    {
1286       log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1287          "- There may be another junkbuster or some other "
1288          "proxy running on port %d", 
1289          (NULL != config->haddr) ? config->haddr : "INADDR_ANY", 
1290          config->hport, config->hport
1291       );
1292       /* shouldn't get here */
1293       return;
1294    }
1295
1296    config->need_bind = 0;
1297
1298
1299    while (FOREVER)
1300    {
1301 #if !defined(FEATURE_PTHREAD) && !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1302       while (waitpid(-1, NULL, WNOHANG) > 0)
1303       {
1304          /* zombie children */
1305       }
1306 #endif /* !defined(FEATURE_PTHREAD) && !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1307       sweep();
1308
1309       if ( NULL == (csp = (struct client_state *) zalloc(sizeof(*csp))) )
1310       {
1311          log_error(LOG_LEVEL_FATAL, "malloc(%d) for csp failed: %E", sizeof(*csp));
1312          continue;
1313       }
1314
1315       memset(csp, '\0', sizeof(*csp));
1316
1317       csp->active = 1;
1318       csp->sfd    = -1;
1319
1320       csp->config = config = load_config();
1321
1322       if ( config->need_bind )
1323       {
1324          /*
1325           * Since we were listening to the "old port", we will not see
1326           * a "listen" param change until the next IJB request.  So, at
1327           * least 1 more request must be made for us to find the new
1328           * setting.  I am simply closing the old socket and binding the
1329           * new one.
1330           *
1331           * Which-ever is correct, we will serve 1 more page via the
1332           * old settings.  This should probably be a "show-proxy-args"
1333           * request.  This should not be a so common of an operation
1334           * that this will hurt people's feelings.
1335           */
1336
1337          close_socket(bfd);
1338
1339          log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1340                    config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1341          bfd = bind_port(config->haddr, config->hport);
1342
1343          if (bfd < 0)
1344          {
1345             log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1346                "- There may be another junkbuster or some other "
1347                "proxy running on port %d", 
1348                (NULL != config->haddr) ? config->haddr : "INADDR_ANY", 
1349                config->hport, config->hport
1350             );
1351             /* shouldn't get here */
1352             return;
1353          }
1354
1355          config->need_bind = 0;
1356       }
1357
1358       log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1359
1360       if (!accept_connection(csp, bfd))
1361       {
1362          log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1363
1364 #ifdef AMIGA
1365          if(!childs)
1366          {
1367             exit(1); 
1368          }
1369 #endif
1370          freez(csp);
1371          continue;
1372       }
1373       else
1374       {
1375          log_error(LOG_LEVEL_CONNECT, "OK");
1376       }
1377
1378 #ifdef FEATURE_TOGGLE
1379       /* by haroon - most of credit to srt19170 */
1380       csp->toggled_on = g_bToggleIJB;
1381 #endif /* def FEATURE_TOGGLE */
1382
1383       if (run_loader(csp))
1384       {
1385          log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
1386          /* Never get here - LOG_LEVEL_FATAL causes program exit */
1387       }
1388
1389 #ifdef FEATURE_ACL
1390       if (block_acl(NULL,csp))
1391       {
1392          log_error(LOG_LEVEL_CONNECT, "Connection dropped due to ACL");
1393          close_socket(csp->cfd);
1394          freez(csp);
1395          continue;
1396       }
1397 #endif /* def FEATURE_ACL */
1398
1399       /* add it to the list of clients */
1400       csp->next = clients->next;
1401       clients->next = csp;
1402
1403       if (config->multi_threaded)
1404       {
1405          int child_id;
1406
1407 /* this is a switch () statment in the C preprocessor - ugh */
1408 #undef SELECTED_ONE_OPTION
1409
1410 /* Use Pthreads in preference to native code */
1411 #if defined(FEATURE_PTHREAD) && !defined(SELECTED_ONE_OPTION)
1412 #define SELECTED_ONE_OPTION
1413          {
1414             pthread_t the_thread;
1415             pthread_attr_t attrs;
1416
1417             pthread_attr_init(&attrs);
1418             pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
1419             child_id = (pthread_create(&the_thread, &attrs,
1420                (void*)serve, csp) ? -1 : 0);
1421             pthread_attr_destroy(&attrs);
1422          }
1423 #endif
1424
1425 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1426 #define SELECTED_ONE_OPTION
1427          child_id = _beginthread(
1428             (void*)serve,
1429             64 * 1024,
1430             csp);
1431 #endif
1432
1433 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1434 #define SELECTED_ONE_OPTION
1435          {
1436             thread_id tid = spawn_thread
1437                (server_thread, "server", B_NORMAL_PRIORITY, csp);
1438
1439             if ((tid >= 0) && (resume_thread(tid) == B_OK))
1440             {
1441                child_id = (int) tid;
1442             }
1443             else
1444             {
1445                child_id = -1;
1446             }
1447          }
1448 #endif
1449
1450 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
1451 #define SELECTED_ONE_OPTION
1452          csp->cfd = ReleaseSocket(csp->cfd, -1);
1453          if((child_id = (int)CreateNewProcTags(
1454             NP_Entry, (ULONG)server_thread,
1455             NP_Output, Output(),
1456             NP_CloseOutput, FALSE,
1457             NP_Name, (ULONG)"junkbuster child",
1458             NP_StackSize, 200*1024,
1459             TAG_DONE)))
1460          {
1461             childs++;
1462             ((struct Task *)child_id)->tc_UserData = csp;
1463             Signal((struct Task *)child_id, SIGF_SINGLE);
1464             Wait(SIGF_SINGLE);
1465          }
1466 #endif
1467
1468 #if !defined(SELECTED_ONE_OPTION)
1469          child_id = fork();
1470
1471          /* This block is only needed when using fork().
1472           * When using threads, the server thread was
1473           * created and run by the call to _beginthread().
1474           */
1475          if (child_id == 0)   /* child */
1476          {
1477             serve(csp);
1478             _exit(0);
1479
1480          }
1481          else if (child_id > 0) /* parent */
1482          {
1483             /* in a fork()'d environment, the parent's
1484              * copy of the client socket and the CSP
1485              * are not used.
1486              */
1487
1488 #if !defined(_WIN32) && defined(__CYGWIN__)
1489             wait( NULL );
1490 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1491             close_socket(csp->cfd);
1492             csp->active = 0;
1493          }
1494 #endif
1495
1496 #undef SELECTED_ONE_OPTION
1497 /* end of cpp switch () */
1498
1499          if (child_id < 0) /* failed */
1500          {
1501             char buf[BUFFER_SIZE];
1502
1503             log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1504
1505             sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
1506
1507             write_socket(csp->cfd, buf, strlen(buf));
1508             close_socket(csp->cfd);
1509             csp->active = 0;
1510             sleep(5);
1511             continue;
1512          }
1513       }
1514       else
1515       {
1516          serve(csp);
1517       }
1518    }
1519    /* NOTREACHED */
1520
1521 }
1522
1523
1524 /*
1525   Local Variables:
1526   tab-width: 3
1527   end:
1528 */