1 const char jcc_rcs[] = "$Id: jcc.c,v 1.23 2001/07/02 02:28:25 iwanttokeepanon 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.23 2001/07/02 02:28:25 iwanttokeepanon
37 * Added "#ifdef ACL_FILES" conditional compilation to line 1291 to exclude
38 * the `block_acl' call. This prevents a compilation error when the user
39 * does not wish to use the "ACL" feature.
41 * Revision 1.22 2001/06/29 21:45:41 oes
42 * Indentation, CRLF->LF, Tab-> Space
44 * Revision 1.21 2001/06/29 13:29:36 oes
45 * - Cleaned up, improved comments
46 * - Unified all possible interceptors (CGI,
47 * block, trust, fast_redirect) in one
48 * place, with one (CGI) answer generation
49 * mechansim. Much clearer now.
50 * - Removed the GIF image generation, which
51 * is now done in filters.c:block_url()
52 * - Made error conditions like domain lookup
53 * failiure or (various) problems while talking
54 * to the server use cgi.c:error_response()
55 * instead of generating HTML/HTTP in chat() (yuck!)
56 * - Removed logentry from cancelled commit
58 * Revision 1.20 2001/06/09 10:55:28 jongfoster
59 * Changing BUFSIZ ==> BUFFER_SIZE
61 * Revision 1.19 2001/06/07 23:12:52 jongfoster
62 * Replacing function pointer in struct gateway with a directly
63 * called function forwarded_connect().
64 * Replacing struct gateway with struct forward_spec
66 * Revision 1.18 2001/06/03 19:12:16 oes
67 * introduced new cgi handling
69 * Revision 1.17 2001/06/01 20:07:23 jongfoster
70 * Now uses action +image-blocker{} rather than config->tinygif
72 * Revision 1.16 2001/06/01 18:49:17 jongfoster
73 * Replaced "list_share" with "list" - the tiny memory gain was not
74 * worth the extra complexity.
76 * Revision 1.15 2001/05/31 21:24:47 jongfoster
77 * Changed "permission" to "action" throughout.
78 * Removed DEFAULT_USER_AGENT - it must now be specified manually.
79 * Moved vanilla wafer check into chat(), since we must now
80 * decide whether or not to add it based on the URL.
82 * Revision 1.14 2001/05/29 20:14:01 joergs
83 * AmigaOS bugfix: PCRS needs a lot of stack, stacksize for child threads
86 * Revision 1.13 2001/05/29 09:50:24 jongfoster
87 * Unified blocklist/imagelist/permissionslist.
88 * File format is still under discussion, but the internal changes
91 * Also modified interceptor behaviour:
92 * - We now intercept all URLs beginning with one of the following
93 * prefixes (and *only* these prefixes):
95 * * http://ijbswa.sf.net/config/
96 * * http://ijbswa.sourceforge.net/config/
97 * - New interceptors "home page" - go to http://i.j.b/ to see it.
98 * - Internal changes so that intercepted and fast redirect pages
99 * are not replaced with an image.
100 * - Interceptors now have the option to send a binary page direct
101 * to the client. (i.e. ijb-send-banner uses this)
102 * - Implemented show-url-info interceptor. (Which is why I needed
103 * the above interceptors changes - a typical URL is
104 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
105 * The previous mechanism would not have intercepted that, and
106 * if it had been intercepted then it then it would have replaced
109 * Revision 1.12 2001/05/27 22:17:04 oes
111 * - re_process_buffer no longer writes the modified buffer
112 * to the client, which was very ugly. It now returns the
113 * buffer, which it is then written by chat.
115 * - content_length now adjusts the Content-Length: header
116 * for modified documents rather than crunch()ing it.
117 * (Length info in csp->content_length, which is 0 for
118 * unmodified documents)
120 * - For this to work, sed() is called twice when filtering.
122 * Revision 1.11 2001/05/26 17:27:53 jongfoster
123 * Added support for CLF and fixed LOG_LEVEL_LOG.
124 * Also did CRLF->LF fix of my previous patch.
126 * Revision 1.10 2001/05/26 15:26:15 jongfoster
127 * ACL feature now provides more security by immediately dropping
128 * connections from untrusted hosts.
130 * Revision 1.9 2001/05/26 00:28:36 jongfoster
131 * Automatic reloading of config file.
132 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
133 * Most of the global variables have been moved to a new
134 * struct configuration_spec, accessed through csp->config->globalname
135 * Most of the globals remaining are used by the Win32 GUI.
137 * Revision 1.8 2001/05/25 22:43:18 jongfoster
138 * Fixing minor memory leak and buffer overflow.
140 * Revision 1.7 2001/05/25 22:34:30 jongfoster
143 * Revision 1.6 2001/05/23 00:13:58 joergs
144 * AmigaOS support fixed.
146 * Revision 1.5 2001/05/22 18:46:04 oes
148 * - Enabled filtering banners by size rather than URL
149 * by adding patterns that replace all standard banner
150 * sizes with the "Junkbuster" gif to the re_filterfile
152 * - Enabled filtering WebBugs by providing a pattern
153 * which kills all 1x1 images
155 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
156 * which is selected by the (nonstandard and therefore
157 * capital) letter 'U' in the option string.
158 * It causes the quantifiers to be ungreedy by default.
159 * Appending a ? turns back to greedy (!).
161 * - Added a new interceptor ijb-send-banner, which
162 * sends back the "Junkbuster" gif. Without imagelist or
163 * MSIE detection support, or if tinygif = 1, or the
164 * URL isn't recognized as an imageurl, a lame HTML
165 * explanation is sent instead.
167 * - Added new feature, which permits blocking remote
168 * script redirects and firing back a local redirect
170 * The feature is conditionally compiled, i.e. it
171 * can be disabled with --disable-fast-redirects,
172 * plus it must be activated by a "fast-redirects"
173 * line in the config file, has its own log level
174 * and of course wants to be displayed by show-proxy-args
175 * Note: Boy, all the #ifdefs in 1001 locations and
176 * all the fumbling with configure.in and acconfig.h
177 * were *way* more work than the feature itself :-(
179 * - Because a generic redirect template was needed for
180 * this, tinygif = 3 now uses the same.
182 * - Moved GIFs, and other static HTTP response templates
187 * - Removed some >400 CRs again (Jon, you really worked
190 * Revision 1.4 2001/05/21 19:34:01 jongfoster
191 * Made failure to bind() a fatal error.
193 * Revision 1.3 2001/05/20 01:21:20 jongfoster
194 * Version 2.9.4 checkin.
195 * - Merged popupfile and cookiefile, and added control over PCRS
196 * filtering, in new "permissionsfile".
197 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
198 * file error you now get a message box (in the Win32 GUI) rather
199 * than the program exiting with no explanation.
200 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
202 * - Removed tabs from "config"
203 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
204 * - Bumped up version number.
206 * Revision 1.2 2001/05/17 22:34:44 oes
207 * - Added hint on GIF char array generation to jcc.c
208 * - Cleaned CRLF's from the sources and related files
209 * - Repaired logging for REF and FRC
211 * Revision 1.1.1.1 2001/05/15 13:58:56 oes
212 * Initial import of version 2.9.3 source tree
215 *********************************************************************/
221 #include <sys/types.h>
230 # include <sys/timeb.h>
231 # include <windows.h>
233 # include <process.h>
236 # endif /* def TOGGLE */
239 # ifndef _WIN_CONSOLE
241 # endif /* ndef _WIN_CONSOLE */
243 #else /* ifndef _WIN32 */
246 # include <sys/time.h>
247 # include <sys/wait.h>
248 # include <sys/stat.h>
252 # include <socket.h> /* BeOS has select() for sockets only. */
253 # include <OS.h> /* declarations for threads and stuff. */
267 #include "showargs.h"
269 #include "killpopup.h"
270 #include "miscutil.h"
272 #include "jbsockets.h"
277 const char jcc_h_rcs[] = JCC_H_VERSION;
278 const char project_h_rcs[] = PROJECT_H_VERSION;
280 struct client_state clients[1];
281 struct file_list files[1];
284 int urls_read = 0; /* total nr of urls read inc rejected */
285 int urls_rejected = 0; /* total nr of urls rejected */
286 #endif /* def STATISTICS */
289 static void listen_loop(void);
290 static void chat(struct client_state *csp);
292 void serve(struct client_state *csp);
293 #else /* ifndef AMIGA */
294 static void serve(struct client_state *csp);
295 #endif /* def AMIGA */
298 static int32 server_thread(void *data);
299 #endif /* def __BEOS__ */
302 #define sleep(N) Sleep(((N) * 1000))
306 /* The vanilla wafer. */
307 static const char VANILLA_WAFER[] =
308 "NOTICE=TO_WHOM_IT_MAY_CONCERN_"
309 "Do_not_send_me_any_copyrighted_information_other_than_the_"
310 "document_that_I_am_requesting_or_any_of_its_necessary_components._"
311 "In_particular_do_not_send_me_any_cookies_that_"
312 "are_subject_to_a_claim_of_copyright_by_anybody._"
313 "Take_notice_that_I_refuse_to_be_bound_by_any_license_condition_"
314 "(copyright_or_otherwise)_applying_to_any_cookie._";
317 /*********************************************************************
321 * Description : Once a connection to the client has been accepted,
322 * this function is called (via serve()) to handle the
323 * main business of the communication. When this
324 * function returns, the caller must close the client
328 * 1 : csp = Current client state (buffers, headers, etc...)
330 * Returns : On success, the number of bytes written are returned (zero
331 * indicates nothing was written). On error, -1 is returned,
332 * and errno is set appropriately. If count is zero and the
333 * file descriptor refers to a regular file, 0 will be
334 * returned without causing any other effect. For a special
335 * file, the results are not portable.
337 *********************************************************************/
338 static void chat(struct client_state *csp)
341 * This next lines are a little ugly, but they simplifies the if statements below.
342 * Basically if TOGGLE, then we want the if to test "csp->toggled_on", else we don't
343 * And if FORCE_LOAD, then we want the if to test "csp->toggled_on", else we don't
346 # define IS_TOGGLED_ON_AND (csp->toggled_on) &&
347 #else /* ifndef TOGGLE */
348 # define IS_TOGGLED_ON_AND
349 #endif /* ndef TOGGLE */
351 # define IS_NOT_FORCED_AND (!csp->force) &&
352 #else /* ifndef FORCE_LOAD */
353 # define IS_NOT_FORCED_AND
354 #endif /* def FORCE_LOAD */
356 #define IS_ENABLED_AND IS_TOGGLED_ON_AND IS_NOT_FORCED_AND
358 char buf[BUFFER_SIZE];
362 int n, maxfd, server_body;
363 int ms_iis5_hack = 0;
365 const struct forward_spec * fwd;
366 struct http_request *http;
368 int block_popups; /* bool, 1==will block popups */
369 int block_popups_now = 0; /* bool, 1==currently blocking popups */
370 #endif /* def KILLPOPUPS */
372 int pcrs_filter; /* bool, 1==will filter through pcrs */
373 int gif_deanimate; /* bool, 1==will deanimate gifs */
375 /* Function that does the content filtering for the current request */
376 char *(*content_filter)() = NULL;
378 /* Skeleton for HTTP response, if we should intercept the request */
379 struct http_response *rsp;
384 * Read the client's request. Note that since we're not using select() we
385 * could get blocked here if a client connected, then didn't say anything!
390 n = read_socket(csp->cfd, buf, sizeof(buf));
392 if (n <= 0) break; /* error! */
394 add_to_iob(csp, buf, n);
396 req = get_header(csp);
400 break; /* no HTTP request! */
405 continue; /* more to come! */
409 /* If this request contains the FORCE_PREFIX,
410 * better get rid of it now and set the force flag --oes
413 if (strstr(req, FORCE_PREFIX))
415 strclean(req, FORCE_PREFIX);
416 log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", req);
423 #endif /* def FORCE_LOAD */
425 parse_http_request(req, http, csp);
430 if (http->cmd == NULL)
432 strcpy(buf, CHEADER);
433 write_socket(csp->cfd, buf, strlen(buf));
435 log_error(LOG_LEVEL_CLF, "%s - - [%T] \" \" 400 0", csp->ip_addr_str);
440 /* decide how to route the HTTP request */
442 if ((fwd = forward_url(http, csp)) == NULL)
444 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
445 /* Never get here - LOG_LEVEL_FATAL causes program exit */
448 /* build the http request to send to the server
449 * we have to do one of the following:
451 * create = use the original HTTP request to create a new
452 * HTTP request that has only the path component
453 * without the http://domainspec
454 * pass = pass the original HTTP request unchanged
456 * drop = drop the HTTP request
461 * +--------+--------+
463 * 0 | create | drop |
465 * Forwarding +--------+--------+
469 * +--------+--------+
473 if (fwd->forward_host)
475 /* if forwarding, just pass the request as is */
476 enlist(csp->headers, http->cmd);
482 /* otherwise elide the host information from the url */
484 p = strsav(p, http->gpc);
486 p = strsav(p, http->path);
488 p = strsav(p, http->ver);
489 enlist(csp->headers, p);
494 /* decide what we're to do with cookies */
497 if (!csp->toggled_on)
499 /* Most compatible set of actions (i.e. none) */
500 init_current_action(csp->action);
503 #endif /* ndef TOGGLE */
505 url_actions(http, csp);
510 * If we're logging cookies in a cookie jar, and the user has not
511 * supplied any wafers, and the user has not told us to suppress the
512 * vanilla wafer, then send the vanilla wafer.
514 if ((csp->config->jarfile != NULL)
515 && (csp->action->multi[ACTION_MULTI_WAFER]->next == NULL)
516 && ((csp->action->flags & ACTION_VANILLA_WAFER) != 0))
518 enlist(csp->action->multi[ACTION_MULTI_WAFER], VANILLA_WAFER);
520 #endif /* def JAR_FILES */
523 block_popups = ((csp->action->flags & ACTION_NO_POPUPS) != 0);
524 #endif /* def KILLPOPUPS */
526 pcrs_filter = (csp->rlist != NULL) && /* There are expressions to be used */
527 ((csp->action->flags & ACTION_FILTER) != 0);
529 gif_deanimate = ((csp->action->flags & ACTION_DEANIMATE) != 0);
531 /* grab the rest of the client's headers */
535 if ( ( p = get_header(csp) ) && ( *p == '\0' ) )
537 n = read_socket(csp->cfd, buf, sizeof(buf));
540 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
543 add_to_iob(csp, buf, n);
547 if (p == NULL) break;
549 enlist(csp->headers, p);
553 /* We have a request. */
555 hdr = sed(client_patterns, add_client_headers, csp);
556 destroy_list(csp->headers);
559 * Now, check to see if we need to intercept it, i.e.
564 /* a CGI call was detected and answered */
565 (NULL != (rsp = dispatch_cgi(csp)))
567 /* or we are enabled and... */
570 /* ..the request was blocked */
571 ( NULL != (rsp = block_url(csp)))
575 || ( NULL != (rsp = trust_url(csp)))
578 /* ..or a fast redirect kicked in */
579 #ifdef FAST_REDIRECTS
580 || (((csp->action->flags & ACTION_FAST_REDIRECTS) != 0) &&
581 (NULL != (rsp = redirect_url(csp))))
582 #endif /* def FAST_REDIRECTS */
586 /* Write the answer to the client */
587 if ((write_socket(csp->cfd, rsp->head, rsp->head_length) != rsp->head_length)
588 || (write_socket(csp->cfd, rsp->body, rsp->content_length) != rsp->content_length))
590 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
594 /* Count as a rejected request */
596 #endif /* def STATISTICS */
598 /* Log (FIXME: All intercept reasons apprear as "crunch" with Status 200) */
599 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
600 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3", csp->ip_addr_str, http->cmd);
602 /* Clean up and return */
603 free_http_response(rsp);
608 log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
610 if (fwd->forward_host)
612 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
613 fwd->forward_host, fwd->forward_port, http->hostport);
617 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
620 /* here we connect to the server, gateway, or the forwarder */
622 csp->sfd = forwarded_connect(fwd, http, csp);
626 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
631 rsp = error_response(csp, "no-such-domain", errno);
633 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 404 0",
634 csp->ip_addr_str, http->cmd);
638 rsp = error_response(csp, "connect-failed", errno);
640 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
641 csp->ip_addr_str, http->cmd);
644 /* Write the answer to the client */
647 if ((write_socket(csp->cfd, rsp->head, rsp->head_length) != rsp->head_length)
648 || (write_socket(csp->cfd, rsp->body, rsp->content_length) != rsp->content_length))
650 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
654 free_http_response(rsp);
659 log_error(LOG_LEVEL_CONNECT, "OK");
661 if (fwd->forward_host || (http->ssl == 0))
663 /* write the client's (modified) header to the server
664 * (along with anything else that may be in the buffer)
669 if ((write_socket(csp->sfd, hdr, n) != n)
670 || (flush_socket(csp->sfd, csp ) < 0))
672 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
675 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
676 csp->ip_addr_str, http->cmd);
678 rsp = error_response(csp, "connect-failed", errno);
682 if ((write_socket(csp->cfd, rsp->head, n) != n)
683 || (write_socket(csp->cfd, rsp->body, rsp->content_length) != rsp->content_length))
685 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
689 free_http_response(rsp);
697 * We're running an SSL tunnel and we're not forwarding,
698 * so just send the "connect succeeded" message to the
699 * client, flush the rest, and get out of the way.
701 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 2\n",
702 csp->ip_addr_str, http->cmd);
704 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0)
712 /* we're finished with the client's header */
715 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
717 /* pass data between the client and server
718 * until one or the other shuts down the connection.
727 FD_SET(csp->cfd, &rfds);
728 FD_SET(csp->sfd, &rfds);
730 n = select(maxfd+1, &rfds, NULL, NULL, NULL);
734 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
738 /* this is the body of the browser's request
739 * just read it and write it.
742 if (FD_ISSET(csp->cfd, &rfds))
744 n = read_socket(csp->cfd, buf, sizeof(buf));
748 break; /* "game over, man" */
751 if (write_socket(csp->sfd, buf, n) != n)
753 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
760 * The server wants to talk. It could be the header or the body.
761 * If `hdr' is null, then it's the header otherwise it's the body.
762 * FIXME: Does `hdr' really mean `host'? No.
766 if (FD_ISSET(csp->sfd, &rfds))
769 n = read_socket(csp->sfd, buf, sizeof(buf) - 1);
773 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
775 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
776 csp->ip_addr_str, http->cmd);
778 rsp = error_response(csp, "connect-failed", errno);
782 if ((write_socket(csp->cfd, rsp->head, rsp->head_length) != rsp->head_length)
783 || (write_socket(csp->cfd, rsp->body, rsp->content_length) != rsp->content_length))
785 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
789 free_http_response(rsp);
793 /* Add a trailing zero. This lets filter_popups
794 * use string operations.
799 /* Filter the popups on this read. */
800 if (block_popups_now)
802 filter_popups(buf, n);
804 #endif /* def KILLPOPUPS */
806 /* Normally, this would indicate that we've read
807 * as much as the server has sent us and we can
808 * close the client connection. However, Microsoft
809 * in its wisdom has released IIS/5 with a bug that
810 * prevents it from sending the trailing \r\n in
811 * a 302 redirect header (and possibly other headers).
812 * To work around this if we've haven't parsed
813 * a full header we'll append a trailing \r\n
814 * and see if this now generates a valid one.
816 * This hack shouldn't have any impacts. If we've
817 * already transmitted the header or if this is a
818 * SSL connection, then we won't bother with this
819 * hack. So we only work on partially received
820 * headers. If we append a \r\n and this still
821 * doesn't generate a valid header, then we won't
822 * transmit anything to the client.
827 if (server_body || http->ssl)
830 * If we have been buffering up the document,
831 * now is the time to apply content modification
832 * and send the result to the client.
837 * If the content filter fails, use the original
839 * (see p != NULL ? p : csp->iob->cur below)
841 if (NULL == (p = (*content_filter)(csp)))
843 csp->content_length = csp->iob->eod - csp->iob->cur;
846 hdr = sed(server_patterns, add_server_headers, csp);
849 if ((write_socket(csp->cfd, hdr, n) != n)
850 || (write_socket(csp->cfd, p != NULL ? p : csp->iob->cur, csp->content_length) != csp->content_length))
852 log_error(LOG_LEVEL_CONNECT, "write modified content to client failed: %E");
860 break; /* "game over, man" */
864 * This is NOT the body, so
865 * Let's pretend the server just sent us a blank line.
867 n = sprintf(buf, "\r\n");
870 * Now, let the normal header parsing algorithm below do its
871 * job. If it fails, we'll exit instead of continuing.
878 * If this is an SSL connection or we're in the body
879 * of the server document, just write it to the client.
882 if (server_body || http->ssl)
887 add_to_iob(csp, buf, n); /* Buffer the body for filtering */
893 if (write_socket(csp->cfd, buf, n) != n)
895 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
904 /* we're still looking for the end of the
905 * server's header ... (does that make header
906 * parsing an "out of body experience" ?
909 /* buffer up the data we just read */
910 add_to_iob(csp, buf, n);
912 /* get header lines from the iob */
914 while ((p = get_header(csp)))
918 /* see following note */
921 enlist(csp->headers, p);
925 /* NOTE: there are no "empty" headers so
926 * if the pointer `p' is not NULL we must
927 * assume that we reached the end of the
928 * buffer before we hit the end of the header.
935 /* Well, we tried our MS IIS/5
936 * hack and it didn't work.
937 * The header is incomplete
938 * and there isn't anything
939 * we can do about it.
945 /* Since we have to wait for
946 * more from the server before
947 * we can parse the headers
948 * we just continue here.
954 /* we have now received the entire header.
955 * filter it and send the result to the client
958 hdr = sed(server_patterns, add_server_headers, csp);
961 /* write the server's (modified) header to
962 * the client (along with anything else that
963 * may be in the buffer)
967 /* Start blocking popups if appropriate. */
969 if ((csp->content_type & CT_TEXT) && /* It's a text / * MIME-Type */
970 !http->ssl && /* We talk plaintext */
971 block_popups) /* Policy allows */
973 block_popups_now = 1;
976 #endif /* def KILLPOPUPS */
978 /* Buffer and pcrs filter this if appropriate. */
980 if ((csp->content_type & CT_TEXT) && /* It's a text / * MIME-Type */
981 !http->ssl && /* We talk plaintext */
982 pcrs_filter) /* Policy allows */
984 content_filter = pcrs_filter_response;
987 /* Buffer and gif_deanimate this if appropriate. */
989 if ((csp->content_type & CT_GIF) && /* It's a image/gif MIME-Type */
990 !http->ssl && /* We talk plaintext */
991 gif_deanimate) /* Policy allows */
993 content_filter = gif_deanimate_response;
998 * Only write if we're not buffering for content modification
1000 if (!content_filter && ((write_socket(csp->cfd, hdr, n) != n)
1001 || (n = flush_socket(csp->cfd, csp) < 0)))
1003 log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
1005 /* the write failed, so don't bother
1006 * mentioning it to the client...
1007 * it probably can't hear us anyway.
1013 !content_filter && (byte_count += n);
1015 /* we're finished with the server's header */
1020 /* If this was a MS IIS/5 hack then it means
1021 * the server has already closed the
1022 * connection. Nothing more to read. Time
1033 return; /* huh? we should never get here */
1036 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %d",
1037 csp->ip_addr_str, http->cmd, byte_count);
1041 /*********************************************************************
1045 * Description : This is little more than chat. We only "serve" to
1046 * to close any socket that chat may have opened.
1049 * 1 : csp = Current client state (buffers, headers, etc...)
1053 *********************************************************************/
1055 void serve(struct client_state *csp)
1056 #else /* ifndef AMIGA */
1057 static void serve(struct client_state *csp)
1058 #endif /* def AMIGA */
1061 close_socket(csp->cfd);
1065 close_socket(csp->sfd);
1074 /*********************************************************************
1076 * Function : server_thread
1078 * Description : We only exist to call `serve' in a threaded environment.
1081 * 1 : data = Current client state (buffers, headers, etc...)
1083 * Returns : Always 0.
1085 *********************************************************************/
1086 static int32 server_thread(void *data)
1088 serve((struct client_state *) data);
1095 /*********************************************************************
1099 * Description : Load the config file and start the listen loop.
1100 * This function is a lot more *sane* with the `load_config'
1101 * and `listen_loop' functions; although it stills does
1102 * a *little* too much for my taste.
1105 * 1 : argc = Number of parameters (including $0).
1106 * 2 : argv = Array of (char *)'s to the parameters.
1108 * Returns : 1 if : can't open config file, unrecognized directive,
1109 * stats requested in multi-thread mode, can't open the
1110 * log file, can't open the jar file, listen port is invalid,
1111 * any load fails, and can't bind port.
1113 * Else main never returns, the process must be signaled
1114 * to terminate execution. Or, on Windows, use the
1115 * "File", "Exit" menu option.
1117 *********************************************************************/
1119 int _main(int argc, const char *argv[])
1121 int main(int argc, const char *argv[])
1126 "AmiTCP:db/junkbuster/config"
1127 #elif !defined(_WIN32)
1134 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
1135 if ((argc >= 2) && (strcmp(argv[1], "--help")==0))
1137 printf("JunkBuster proxy version " VERSION ".\n\n"
1138 "Usage: %s [configfile]\n\n"
1139 "See " HOME_PAGE_URL " for details.\n"
1140 "This program is distributed under the GNU GPL, version 2 or later.\n",
1144 if ((argc >= 2) && (strcmp(argv[1], "--version")==0))
1146 printf(VERSION "\n");
1149 #endif /* !defined(_WIN32) || defined(_WIN_CONSOLE) */
1156 configfile = argv[1];
1163 #elif defined(_WIN32)
1169 signal(SIGPIPE, SIG_IGN);
1170 signal(SIGCHLD, SIG_IGN);
1172 #else /* ifdef _WIN32 */
1173 # ifdef _WIN_CONSOLE
1175 * We *are* in a windows console app.
1176 * Print a verbose messages about FAQ's and such
1178 printf(win32_blurb);
1179 # endif /* def _WIN_CONSOLE */
1180 #endif /* def _WIN32 */
1191 /*********************************************************************
1193 * Function : listen_loop
1195 * Description : bind the listen port and enter a "FOREVER" listening loop.
1201 *********************************************************************/
1202 static void listen_loop(void)
1204 struct client_state *csp = NULL;
1206 struct configuration_spec * config;
1208 config = load_config();
1210 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1211 config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1213 bfd = bind_port(config->haddr, config->hport);
1217 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1218 "- There may be another junkbuster or some other "
1219 "proxy running on port %d",
1220 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1221 config->hport, config->hport
1223 /* shouldn't get here */
1227 config->need_bind = 0;
1232 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1233 while (waitpid(-1, NULL, WNOHANG) > 0)
1235 /* zombie children */
1237 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1240 if ( NULL == (csp = (struct client_state *) zalloc(sizeof(*csp))) )
1242 log_error(LOG_LEVEL_FATAL, "malloc(%d) for csp failed: %E", sizeof(*csp));
1246 memset(csp, '\0', sizeof(*csp));
1251 csp->config = config = load_config();
1253 if ( config->need_bind )
1256 * Since we were listening to the "old port", we will not see
1257 * a "listen" param change until the next IJB request. So, at
1258 * least 1 more request must be made for us to find the new
1259 * setting. I am simply closing the old socket and binding the
1262 * Which-ever is correct, we will serve 1 more page via the
1263 * old settings. This should probably be a "show-proxy-args"
1264 * request. This should not be a so common of an operation
1265 * that this will hurt people's feelings.
1270 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1271 config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1272 bfd = bind_port(config->haddr, config->hport);
1276 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1277 "- There may be another junkbuster or some other "
1278 "proxy running on port %d",
1279 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1280 config->hport, config->hport
1282 /* shouldn't get here */
1286 config->need_bind = 0;
1289 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1291 if (!accept_connection(csp, bfd))
1293 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1306 log_error(LOG_LEVEL_CONNECT, "OK");
1310 /* by haroon - most of credit to srt19170 */
1311 csp->toggled_on = g_bToggleIJB;
1314 if (run_loader(csp))
1316 log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
1317 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1321 if (block_acl(NULL,csp))
1323 log_error(LOG_LEVEL_CONNECT, "Connection dropped due to ACL");
1324 close_socket(csp->cfd);
1328 #endif /* def ACL_FILES */
1330 /* add it to the list of clients */
1331 csp->next = clients->next;
1332 clients->next = csp;
1334 if (config->multi_threaded)
1338 /* this is a switch () statment in the C preprocessor - ugh */
1339 #undef SELECTED_ONE_OPTION
1341 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1342 #define SELECTED_ONE_OPTION
1343 child_id = _beginthread(
1349 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1350 #define SELECTED_ONE_OPTION
1352 thread_id tid = spawn_thread
1353 (server_thread, "server", B_NORMAL_PRIORITY, csp);
1355 if ((tid >= 0) && (resume_thread(tid) == B_OK))
1357 child_id = (int) tid;
1366 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
1367 #define SELECTED_ONE_OPTION
1368 csp->cfd = ReleaseSocket(csp->cfd, -1);
1369 if((child_id = (int)CreateNewProcTags(
1370 NP_Entry, (ULONG)server_thread,
1371 NP_Output, Output(),
1372 NP_CloseOutput, FALSE,
1373 NP_Name, (ULONG)"junkbuster child",
1374 NP_StackSize, 200*1024,
1378 ((struct Task *)child_id)->tc_UserData = csp;
1379 Signal((struct Task *)child_id, SIGF_SINGLE);
1384 #if !defined(SELECTED_ONE_OPTION)
1388 #undef SELECTED_ONE_OPTION
1389 /* end of cpp switch () */
1391 if (child_id < 0) /* failed */
1393 char buf[BUFFER_SIZE];
1395 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1397 sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
1399 write_socket(csp->cfd, buf, strlen(buf));
1400 close_socket(csp->cfd);
1406 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1407 /* This block is only needed when using fork().
1408 * When using threads, the server thread was
1409 * created and run by the call to _beginthread().
1411 if (child_id == 0) /* child */
1419 /* in a fork()'d environment, the parent's
1420 * copy of the client socket and the CSP
1424 #if !defined(_WIN32) && defined(__CYGWIN__)
1426 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1427 close_socket(csp->cfd);
1430 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */