1 const char jcc_rcs[] = "$Id: jcc.c,v 1.36 2001/09/10 10:56:15 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $
6 * Purpose : Main file. Contains main() method, main loop, and
7 * the main connection-handling function.
9 * Copyright : Written by and Copyright (C) 2001 the SourceForge
10 * IJBSWA team. http://ijbswa.sourceforge.net
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
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.
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.
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.
36 * Revision 1.36 2001/09/10 10:56:15 oes
37 * Silenced compiler warnings
39 * Revision 1.35 2001/07/31 14:44:22 oes
40 * Deleted unused size parameter from filter_popups()
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
48 * Revision 1.33 2001/07/29 19:32:00 jongfoster
49 * Renaming _main() [mingw32 only] to real_main(), for ANSI compliance.
51 * Revision 1.32 2001/07/29 18:47:05 jongfoster
52 * Adding missing #include "loadcfg.h"
54 * Revision 1.31 2001/07/29 12:17:48 oes
55 * Applied pthread fix by Paul Lieverse
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.
62 * Revision 1.29 2001/07/24 12:47:06 oes
63 * Applied BeOS support update by Eugenia
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
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.
76 * Note 2001/07/20: No, the other content modification mechanisms will process
77 * the whole iob later anyway. --oes
79 * Revision 1.26 2001/07/18 12:31:36 oes
82 * Revision 1.25 2001/07/15 19:43:49 jongfoster
83 * Supports POSIX threads.
84 * Also removed some unused #includes.
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
99 * - Removed all #ifdef PCRS
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.
106 * Revision 1.22 2001/06/29 21:45:41 oes
107 * Indentation, CRLF->LF, Tab-> Space
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
123 * Revision 1.20 2001/06/09 10:55:28 jongfoster
124 * Changing BUFSIZ ==> BUFFER_SIZE
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
131 * Revision 1.18 2001/06/03 19:12:16 oes
132 * introduced new cgi handling
134 * Revision 1.17 2001/06/01 20:07:23 jongfoster
135 * Now uses action +image-blocker{} rather than config->tinygif
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.
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.
147 * Revision 1.14 2001/05/29 20:14:01 joergs
148 * AmigaOS bugfix: PCRS needs a lot of stack, stacksize for child threads
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
156 * Also modified interceptor behaviour:
157 * - We now intercept all URLs beginning with one of the following
158 * prefixes (and *only* these prefixes):
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
174 * Revision 1.12 2001/05/27 22:17:04 oes
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.
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)
185 * - For this to work, sed() is called twice when filtering.
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.
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.
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.
202 * Revision 1.8 2001/05/25 22:43:18 jongfoster
203 * Fixing minor memory leak and buffer overflow.
205 * Revision 1.7 2001/05/25 22:34:30 jongfoster
208 * Revision 1.6 2001/05/23 00:13:58 joergs
209 * AmigaOS support fixed.
211 * Revision 1.5 2001/05/22 18:46:04 oes
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
217 * - Enabled filtering WebBugs by providing a pattern
218 * which kills all 1x1 images
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 (!).
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.
232 * - Added new feature, which permits blocking remote
233 * script redirects and firing back a local redirect
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 :-(
244 * - Because a generic redirect template was needed for
245 * this, tinygif = 3 now uses the same.
247 * - Moved GIFs, and other static HTTP response templates
252 * - Removed some >400 CRs again (Jon, you really worked
255 * Revision 1.4 2001/05/21 19:34:01 jongfoster
256 * Made failure to bind() a fatal error.
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
267 * - Removed tabs from "config"
268 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
269 * - Bumped up version number.
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
276 * Revision 1.1.1.1 2001/05/15 13:58:56 oes
277 * Initial import of version 2.9.3 source tree
280 *********************************************************************/
286 #include <sys/types.h>
293 #ifdef FEATURE_PTHREAD
295 #endif /* def FEATURE_PTHREAD */
298 # ifndef FEATURE_PTHREAD
299 # include <windows.h>
300 # include <process.h>
301 # endif /* ndef FEATURE_PTHREAD */
304 # ifndef _WIN_CONSOLE
306 # endif /* ndef _WIN_CONSOLE */
308 #else /* ifndef _WIN32 */
311 # include <sys/time.h>
312 # include <sys/wait.h>
313 # include <sys/stat.h>
317 # include <socket.h> /* BeOS has select() for sockets only. */
318 # include <OS.h> /* declarations for threads and stuff. */
332 #include "showargs.h"
334 #include "killpopup.h"
335 #include "miscutil.h"
337 #include "jbsockets.h"
343 const char jcc_h_rcs[] = JCC_H_VERSION;
344 const char project_h_rcs[] = PROJECT_H_VERSION;
346 struct client_state clients[1];
347 struct file_list files[1];
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 */
355 static void listen_loop(void);
356 static void chat(struct client_state *csp);
358 void serve(struct client_state *csp);
359 #else /* ifndef AMIGA */
360 static void serve(struct client_state *csp);
361 #endif /* def AMIGA */
364 static int32 server_thread(void *data);
365 #endif /* def __BEOS__ */
368 #define sleep(N) Sleep(((N) * 1000))
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._";
383 /*********************************************************************
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
394 * 1 : csp = Current client state (buffers, headers, etc...)
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.
403 *********************************************************************/
404 static void chat(struct client_state *csp)
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
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 */
423 #define IS_ENABLED_AND IS_TOGGLED_ON_AND IS_NOT_FORCED_AND
425 char buf[BUFFER_SIZE];
428 int n, maxfd, server_body;
429 int ms_iis5_hack = 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 */
438 int pcrs_filter; /* bool, 1==will filter through pcrs */
439 int gif_deanimate; /* bool, 1==will deanimate gifs */
441 /* Function that does the content filtering for the current request */
442 char *(*content_filter)() = NULL;
444 /* Skeleton for HTTP response, if we should intercept the request */
445 struct http_response *rsp;
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!
456 n = read_socket(csp->cfd, buf, sizeof(buf));
458 if (n <= 0) break; /* error! */
460 add_to_iob(csp, buf, n);
462 req = get_header(csp);
466 break; /* no HTTP request! */
471 continue; /* more to come! */
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
479 if (strstr(req, FORCE_PREFIX))
481 strclean(req, FORCE_PREFIX);
482 log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", req);
489 #endif /* def FEATURE_FORCE_LOAD */
491 parse_http_request(req, http, csp);
496 if (http->cmd == NULL)
498 strcpy(buf, CHEADER);
499 write_socket(csp->cfd, buf, strlen(buf));
501 log_error(LOG_LEVEL_CLF, "%s - - [%T] \" \" 400 0", csp->ip_addr_str);
506 /* decide how to route the HTTP request */
508 if ((fwd = forward_url(http, csp)) == NULL)
510 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
511 /* Never get here - LOG_LEVEL_FATAL causes program exit */
514 /* build the http request to send to the server
515 * we have to do one of the following:
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
522 * drop = drop the HTTP request
527 * +--------+--------+
529 * 0 | create | drop |
531 * Forwarding +--------+--------+
535 * +--------+--------+
539 if (fwd->forward_host)
541 /* if forwarding, just pass the request as is */
542 enlist(csp->headers, http->cmd);
548 /* otherwise elide the host information from the url */
550 p = strsav(p, http->gpc);
552 p = strsav(p, http->path);
554 p = strsav(p, http->ver);
555 enlist(csp->headers, p);
560 /* decide what we're to do with cookies */
562 #ifdef FEATURE_TOGGLE
563 if (!csp->toggled_on)
565 /* Most compatible set of actions (i.e. none) */
566 init_current_action(csp->action);
569 #endif /* ndef FEATURE_TOGGLE */
571 url_actions(http, csp);
574 #ifdef FEATURE_COOKIE_JAR
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.
580 if ((csp->config->jarfile != NULL)
581 && (csp->action->multi[ACTION_MULTI_WAFER]->next == NULL)
582 && ((csp->action->flags & ACTION_VANILLA_WAFER) != 0))
584 enlist(csp->action->multi[ACTION_MULTI_WAFER], VANILLA_WAFER);
586 #endif /* def FEATURE_COOKIE_JAR */
588 #ifdef FEATURE_KILL_POPUPS
589 block_popups = ((csp->action->flags & ACTION_NO_POPUPS) != 0);
590 #endif /* def FEATURE_KILL_POPUPS */
592 pcrs_filter = (csp->rlist != NULL) && /* There are expressions to be used */
593 ((csp->action->flags & ACTION_FILTER) != 0);
595 gif_deanimate = ((csp->action->flags & ACTION_DEANIMATE) != 0);
597 /* grab the rest of the client's headers */
601 if ( ( p = get_header(csp) ) && ( *p == '\0' ) )
603 n = read_socket(csp->cfd, buf, sizeof(buf));
606 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
609 add_to_iob(csp, buf, n);
613 if (p == NULL) break;
615 enlist(csp->headers, p);
619 /* We have a request. */
621 hdr = sed(client_patterns, add_client_headers, csp);
622 destroy_list(csp->headers);
625 * Now, check to see if we need to intercept it, i.e.
630 /* a CGI call was detected and answered */
631 (NULL != (rsp = dispatch_cgi(csp)))
633 /* or we are enabled and... */
636 /* ..the request was blocked */
637 ( NULL != (rsp = block_url(csp)))
641 || ( NULL != (rsp = trust_url(csp)))
642 #endif /* def FEATURE_TRUST */
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 */
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))
656 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
659 #ifdef FEATURE_STATISTICS
660 /* Count as a rejected request */
662 #endif /* def FEATURE_STATISTICS */
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);
668 /* Clean up and return */
669 free_http_response(rsp);
674 log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
676 if (fwd->forward_host)
678 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
679 fwd->forward_host, fwd->forward_port, http->hostport);
683 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
686 /* here we connect to the server, gateway, or the forwarder */
688 csp->sfd = forwarded_connect(fwd, http, csp);
692 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
697 rsp = error_response(csp, "no-such-domain", errno);
699 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 404 0",
700 csp->ip_addr_str, http->cmd);
704 rsp = error_response(csp, "connect-failed", errno);
706 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
707 csp->ip_addr_str, http->cmd);
710 /* Write the answer to the client */
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))
716 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
720 free_http_response(rsp);
725 log_error(LOG_LEVEL_CONNECT, "OK");
727 if (fwd->forward_host || (http->ssl == 0))
729 /* write the client's (modified) header to the server
730 * (along with anything else that may be in the buffer)
735 if ((write_socket(csp->sfd, hdr, n) != n)
736 || (flush_socket(csp->sfd, csp ) < 0))
738 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
741 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
742 csp->ip_addr_str, http->cmd);
744 rsp = error_response(csp, "connect-failed", errno);
748 if ((write_socket(csp->cfd, rsp->head, n) != n)
749 || (write_socket(csp->cfd, rsp->body, rsp->content_length) != rsp->content_length))
751 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
755 free_http_response(rsp);
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.
767 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 2\n",
768 csp->ip_addr_str, http->cmd);
770 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0)
778 /* we're finished with the client's header */
781 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
783 /* pass data between the client and server
784 * until one or the other shuts down the connection.
793 FD_SET(csp->cfd, &rfds);
794 FD_SET(csp->sfd, &rfds);
796 n = select(maxfd+1, &rfds, NULL, NULL, NULL);
800 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
804 /* this is the body of the browser's request
805 * just read it and write it.
808 if (FD_ISSET(csp->cfd, &rfds))
810 n = read_socket(csp->cfd, buf, sizeof(buf));
814 break; /* "game over, man" */
817 if (write_socket(csp->sfd, buf, n) != n)
819 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
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.
832 if (FD_ISSET(csp->sfd, &rfds))
835 n = read_socket(csp->sfd, buf, sizeof(buf) - 1);
839 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
841 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
842 csp->ip_addr_str, http->cmd);
844 rsp = error_response(csp, "connect-failed", errno);
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))
851 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
855 free_http_response(rsp);
859 /* Add a trailing zero. This lets filter_popups
860 * use string operations.
864 #ifdef FEATURE_KILL_POPUPS
865 /* Filter the popups on this read. */
866 if (block_popups_now)
870 #endif /* def FEATURE_KILL_POPUPS */
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.
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.
893 if (server_body || http->ssl)
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.
903 * If the content filter fails, use the original
905 * (see p != NULL ? p : csp->iob->cur below)
907 if (NULL == (p = (*content_filter)(csp)))
909 csp->content_length = csp->iob->eod - csp->iob->cur;
912 hdr = sed(server_patterns, add_server_headers, csp);
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))
918 log_error(LOG_LEVEL_CONNECT, "write modified content to client failed: %E");
926 break; /* "game over, man" */
930 * This is NOT the body, so
931 * Let's pretend the server just sent us a blank line.
933 n = sprintf(buf, "\r\n");
936 * Now, let the normal header parsing algorithm below do its
937 * job. If it fails, we'll exit instead of continuing.
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
949 if (server_body || http->ssl)
953 add_to_iob(csp, buf, n);
957 if (write_socket(csp->cfd, buf, n) != n)
959 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
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" ?
973 /* buffer up the data we just read */
974 add_to_iob(csp, buf, n);
976 /* get header lines from the iob */
978 while ((p = get_header(csp)))
982 /* see following note */
985 enlist(csp->headers, p);
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.
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.
1009 /* Since we have to wait for
1010 * more from the server before
1011 * we can parse the headers
1012 * we just continue here.
1018 /* we have now received the entire header.
1019 * filter it and send the result to the client
1022 hdr = sed(server_patterns, add_server_headers, csp);
1025 /* write the server's (modified) header to
1026 * the client (along with anything else that
1027 * may be in the buffer)
1030 #ifdef FEATURE_KILL_POPUPS
1031 /* Start blocking popups if appropriate. */
1033 if ((csp->content_type & CT_TEXT) && /* It's a text / * MIME-Type */
1034 !http->ssl && /* We talk plaintext */
1035 block_popups) /* Policy allows */
1037 block_popups_now = 1;
1039 * Filter the part of the body that came in the same read
1040 * as the last headers:
1042 filter_popups(csp->iob->cur);
1045 #endif /* def FEATURE_KILL_POPUPS */
1047 /* Buffer and pcrs filter this if appropriate. */
1049 if ((csp->content_type & CT_TEXT) && /* It's a text / * MIME-Type */
1050 !http->ssl && /* We talk plaintext */
1051 pcrs_filter) /* Policy allows */
1053 content_filter = pcrs_filter_response;
1056 /* Buffer and gif_deanimate this if appropriate. */
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 */
1062 content_filter = gif_deanimate_response;
1067 * Only write if we're not buffering for content modification
1069 if (!content_filter && ((write_socket(csp->cfd, hdr, n) != n)
1070 || (n = flush_socket(csp->cfd, csp) < 0)))
1072 log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
1074 /* the write failed, so don't bother
1075 * mentioning it to the client...
1076 * it probably can't hear us anyway.
1082 if(!content_filter) byte_count += n;
1084 /* we're finished with the server's header */
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
1102 return; /* huh? we should never get here */
1105 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %d",
1106 csp->ip_addr_str, http->cmd, byte_count);
1110 /*********************************************************************
1114 * Description : This is little more than chat. We only "serve" to
1115 * to close any socket that chat may have opened.
1118 * 1 : csp = Current client state (buffers, headers, etc...)
1122 *********************************************************************/
1124 void serve(struct client_state *csp)
1125 #else /* ifndef AMIGA */
1126 static void serve(struct client_state *csp)
1127 #endif /* def AMIGA */
1130 close_socket(csp->cfd);
1134 close_socket(csp->sfd);
1143 /*********************************************************************
1145 * Function : server_thread
1147 * Description : We only exist to call `serve' in a threaded environment.
1150 * 1 : data = Current client state (buffers, headers, etc...)
1152 * Returns : Always 0.
1154 *********************************************************************/
1155 static int32 server_thread(void *data)
1157 serve((struct client_state *) data);
1164 /*********************************************************************
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.
1174 * 1 : argc = Number of parameters (including $0).
1175 * 2 : argv = Array of (char *)'s to the parameters.
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.
1182 * Else main never returns, the process must be signaled
1183 * to terminate execution. Or, on Windows, use the
1184 * "File", "Exit" menu option.
1186 *********************************************************************/
1188 int real_main(int argc, const char *argv[])
1190 int main(int argc, const char *argv[])
1195 "AmiTCP:db/junkbuster/config"
1196 #elif !defined(_WIN32)
1203 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
1204 if ((argc >= 2) && (strcmp(argv[1], "--help")==0))
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",
1213 if ((argc >= 2) && (strcmp(argv[1], "--version")==0))
1215 printf(VERSION "\n");
1218 #endif /* !defined(_WIN32) || defined(_WIN_CONSOLE) */
1225 configfile = argv[1];
1232 #elif defined(_WIN32)
1238 signal(SIGPIPE, SIG_IGN);
1239 signal(SIGCHLD, SIG_IGN);
1241 #else /* ifdef _WIN32 */
1242 # ifdef _WIN_CONSOLE
1244 * We *are* in a windows console app.
1245 * Print a verbose messages about FAQ's and such
1247 printf(win32_blurb);
1248 # endif /* def _WIN_CONSOLE */
1249 #endif /* def _WIN32 */
1260 /*********************************************************************
1262 * Function : listen_loop
1264 * Description : bind the listen port and enter a "FOREVER" listening loop.
1270 *********************************************************************/
1271 static void listen_loop(void)
1273 struct client_state *csp = NULL;
1275 struct configuration_spec * config;
1277 config = load_config();
1279 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1280 config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1282 bfd = bind_port(config->haddr, config->hport);
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
1292 /* shouldn't get here */
1296 config->need_bind = 0;
1301 #if !defined(FEATURE_PTHREAD) && !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1302 while (waitpid(-1, NULL, WNOHANG) > 0)
1304 /* zombie children */
1306 #endif /* !defined(FEATURE_PTHREAD) && !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1309 if ( NULL == (csp = (struct client_state *) zalloc(sizeof(*csp))) )
1311 log_error(LOG_LEVEL_FATAL, "malloc(%d) for csp failed: %E", sizeof(*csp));
1315 memset(csp, '\0', sizeof(*csp));
1320 csp->config = config = load_config();
1322 if ( config->need_bind )
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
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.
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);
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
1351 /* shouldn't get here */
1355 config->need_bind = 0;
1358 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1360 if (!accept_connection(csp, bfd))
1362 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1375 log_error(LOG_LEVEL_CONNECT, "OK");
1378 #ifdef FEATURE_TOGGLE
1379 /* by haroon - most of credit to srt19170 */
1380 csp->toggled_on = g_bToggleIJB;
1381 #endif /* def FEATURE_TOGGLE */
1383 if (run_loader(csp))
1385 log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
1386 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1390 if (block_acl(NULL,csp))
1392 log_error(LOG_LEVEL_CONNECT, "Connection dropped due to ACL");
1393 close_socket(csp->cfd);
1397 #endif /* def FEATURE_ACL */
1399 /* add it to the list of clients */
1400 csp->next = clients->next;
1401 clients->next = csp;
1403 if (config->multi_threaded)
1407 /* this is a switch () statment in the C preprocessor - ugh */
1408 #undef SELECTED_ONE_OPTION
1410 /* Use Pthreads in preference to native code */
1411 #if defined(FEATURE_PTHREAD) && !defined(SELECTED_ONE_OPTION)
1412 #define SELECTED_ONE_OPTION
1414 pthread_t the_thread;
1415 pthread_attr_t attrs;
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);
1425 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1426 #define SELECTED_ONE_OPTION
1427 child_id = _beginthread(
1433 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1434 #define SELECTED_ONE_OPTION
1436 thread_id tid = spawn_thread
1437 (server_thread, "server", B_NORMAL_PRIORITY, csp);
1439 if ((tid >= 0) && (resume_thread(tid) == B_OK))
1441 child_id = (int) tid;
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,
1462 ((struct Task *)child_id)->tc_UserData = csp;
1463 Signal((struct Task *)child_id, SIGF_SINGLE);
1468 #if !defined(SELECTED_ONE_OPTION)
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().
1475 if (child_id == 0) /* child */
1481 else if (child_id > 0) /* parent */
1483 /* in a fork()'d environment, the parent's
1484 * copy of the client socket and the CSP
1488 #if !defined(_WIN32) && defined(__CYGWIN__)
1490 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1491 close_socket(csp->cfd);
1496 #undef SELECTED_ONE_OPTION
1497 /* end of cpp switch () */
1499 if (child_id < 0) /* failed */
1501 char buf[BUFFER_SIZE];
1503 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1505 sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
1507 write_socket(csp->cfd, buf, strlen(buf));
1508 close_socket(csp->cfd);