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