1 const char jcc_rcs[] = "$Id: jcc.c,v 1.18 2001/06/03 19:12:16 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.18 2001/06/03 19:12:16 oes
37 * introduced new cgi handling
39 * Revision 1.18 2001/06/03 11:03:48 oes
46 * adapted to new enlist_unique arg format
50 * introduced confdir option
52 * filters.c filtrers.h
54 * extracted-CGI relevant stuff
62 * support for new cgi mechansim
66 * functions for new list type: "map"
67 * extended enlist_unique
74 * deleted const struct interceptors
82 * added struct http_response
83 * changes struct interceptors to struct cgi_dispatcher
84 * moved HTML stuff to cgi.h
93 * Revision 1.17 2001/06/01 20:07:23 jongfoster
94 * Now uses action +image-blocker{} rather than config->tinygif
96 * Revision 1.16 2001/06/01 18:49:17 jongfoster
97 * Replaced "list_share" with "list" - the tiny memory gain was not
98 * worth the extra complexity.
100 * Revision 1.15 2001/05/31 21:24:47 jongfoster
101 * Changed "permission" to "action" throughout.
102 * Removed DEFAULT_USER_AGENT - it must now be specified manually.
103 * Moved vanilla wafer check into chat(), since we must now
104 * decide whether or not to add it based on the URL.
106 * Revision 1.14 2001/05/29 20:14:01 joergs
107 * AmigaOS bugfix: PCRS needs a lot of stack, stacksize for child threads
110 * Revision 1.13 2001/05/29 09:50:24 jongfoster
111 * Unified blocklist/imagelist/permissionslist.
112 * File format is still under discussion, but the internal changes
115 * Also modified interceptor behaviour:
116 * - We now intercept all URLs beginning with one of the following
117 * prefixes (and *only* these prefixes):
119 * * http://ijbswa.sf.net/config/
120 * * http://ijbswa.sourceforge.net/config/
121 * - New interceptors "home page" - go to http://i.j.b/ to see it.
122 * - Internal changes so that intercepted and fast redirect pages
123 * are not replaced with an image.
124 * - Interceptors now have the option to send a binary page direct
125 * to the client. (i.e. ijb-send-banner uses this)
126 * - Implemented show-url-info interceptor. (Which is why I needed
127 * the above interceptors changes - a typical URL is
128 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
129 * The previous mechanism would not have intercepted that, and
130 * if it had been intercepted then it then it would have replaced
133 * Revision 1.12 2001/05/27 22:17:04 oes
135 * - re_process_buffer no longer writes the modified buffer
136 * to the client, which was very ugly. It now returns the
137 * buffer, which it is then written by chat.
139 * - content_length now adjusts the Content-Length: header
140 * for modified documents rather than crunch()ing it.
141 * (Length info in csp->content_length, which is 0 for
142 * unmodified documents)
144 * - For this to work, sed() is called twice when filtering.
146 * Revision 1.11 2001/05/26 17:27:53 jongfoster
147 * Added support for CLF and fixed LOG_LEVEL_LOG.
148 * Also did CRLF->LF fix of my previous patch.
150 * Revision 1.10 2001/05/26 15:26:15 jongfoster
151 * ACL feature now provides more security by immediately dropping
152 * connections from untrusted hosts.
154 * Revision 1.9 2001/05/26 00:28:36 jongfoster
155 * Automatic reloading of config file.
156 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
157 * Most of the global variables have been moved to a new
158 * struct configuration_spec, accessed through csp->config->globalname
159 * Most of the globals remaining are used by the Win32 GUI.
161 * Revision 1.8 2001/05/25 22:43:18 jongfoster
162 * Fixing minor memory leak and buffer overflow.
164 * Revision 1.7 2001/05/25 22:34:30 jongfoster
167 * Revision 1.6 2001/05/23 00:13:58 joergs
168 * AmigaOS support fixed.
170 * Revision 1.5 2001/05/22 18:46:04 oes
172 * - Enabled filtering banners by size rather than URL
173 * by adding patterns that replace all standard banner
174 * sizes with the "Junkbuster" gif to the re_filterfile
176 * - Enabled filtering WebBugs by providing a pattern
177 * which kills all 1x1 images
179 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
180 * which is selected by the (nonstandard and therefore
181 * capital) letter 'U' in the option string.
182 * It causes the quantifiers to be ungreedy by default.
183 * Appending a ? turns back to greedy (!).
185 * - Added a new interceptor ijb-send-banner, which
186 * sends back the "Junkbuster" gif. Without imagelist or
187 * MSIE detection support, or if tinygif = 1, or the
188 * URL isn't recognized as an imageurl, a lame HTML
189 * explanation is sent instead.
191 * - Added new feature, which permits blocking remote
192 * script redirects and firing back a local redirect
194 * The feature is conditionally compiled, i.e. it
195 * can be disabled with --disable-fast-redirects,
196 * plus it must be activated by a "fast-redirects"
197 * line in the config file, has its own log level
198 * and of course wants to be displayed by show-proxy-args
199 * Note: Boy, all the #ifdefs in 1001 locations and
200 * all the fumbling with configure.in and acconfig.h
201 * were *way* more work than the feature itself :-(
203 * - Because a generic redirect template was needed for
204 * this, tinygif = 3 now uses the same.
206 * - Moved GIFs, and other static HTTP response templates
211 * - Removed some >400 CRs again (Jon, you really worked
214 * Revision 1.4 2001/05/21 19:34:01 jongfoster
215 * Made failure to bind() a fatal error.
217 * Revision 1.3 2001/05/20 01:21:20 jongfoster
218 * Version 2.9.4 checkin.
219 * - Merged popupfile and cookiefile, and added control over PCRS
220 * filtering, in new "permissionsfile".
221 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
222 * file error you now get a message box (in the Win32 GUI) rather
223 * than the program exiting with no explanation.
224 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
226 * - Removed tabs from "config"
227 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
228 * - Bumped up version number.
230 * Revision 1.2 2001/05/17 22:34:44 oes
231 * - Added hint on GIF char array generation to jcc.c
232 * - Cleaned CRLF's from the sources and related files
233 * - Repaired logging for REF and FRC
235 * Revision 1.1.1.1 2001/05/15 13:58:56 oes
236 * Initial import of version 2.9.3 source tree
239 *********************************************************************/
245 #include <sys/types.h>
254 # include <sys/timeb.h>
255 # include <windows.h>
257 # include <process.h>
260 # endif /* def TOGGLE */
263 # ifndef _WIN_CONSOLE
265 # endif /* ndef _WIN_CONSOLE */
267 #else /* ifndef _WIN32 */
270 # include <sys/time.h>
271 # include <sys/wait.h>
272 # include <sys/stat.h>
276 # include <socket.h> /* BeOS has select() for sockets only. */
277 # include <OS.h> /* declarations for threads and stuff. */
291 #include "showargs.h"
293 #include "killpopup.h"
294 #include "miscutil.h"
296 #include "jbsockets.h"
301 const char jcc_h_rcs[] = JCC_H_VERSION;
302 const char project_h_rcs[] = PROJECT_H_VERSION;
304 struct client_state clients[1];
305 struct file_list files[1];
308 int urls_read = 0; /* total nr of urls read inc rejected */
309 int urls_rejected = 0; /* total nr of urls rejected */
310 #endif /* def STATISTICS */
313 static void listen_loop(void);
314 static void chat(struct client_state *csp);
316 void serve(struct client_state *csp);
317 #else /* ifndef AMIGA */
318 static void serve(struct client_state *csp);
319 #endif /* def AMIGA */
322 static int32 server_thread(void *data);
323 #endif /* def __BEOS__ */
326 #define sleep(N) Sleep(((N) * 1000))
330 /* The vanilla wafer. */
331 static const char VANILLA_WAFER[] =
332 "NOTICE=TO_WHOM_IT_MAY_CONCERN_"
333 "Do_not_send_me_any_copyrighted_information_other_than_the_"
334 "document_that_I_am_requesting_or_any_of_its_necessary_components._"
335 "In_particular_do_not_send_me_any_cookies_that_"
336 "are_subject_to_a_claim_of_copyright_by_anybody._"
337 "Take_notice_that_I_refuse_to_be_bound_by_any_license_condition_"
338 "(copyright_or_otherwise)_applying_to_any_cookie._";
341 /*********************************************************************
345 * Description : Once a connection to the client has been accepted,
346 * this function is called (via serve()) to handle the
347 * main business of the communication. When this
348 * function returns, the caller must close the client
352 * 1 : csp = Current client state (buffers, headers, etc...)
354 * Returns : On success, the number of bytes written are returned (zero
355 * indicates nothing was written). On error, -1 is returned,
356 * and errno is set appropriately. If count is zero and the
357 * file descriptor refers to a regular file, 0 will be
358 * returned without causing any other effect. For a special
359 * file, the results are not portable.
361 *********************************************************************/
362 static void chat(struct client_state *csp)
365 * This next lines are a little ugly, but they simplifies the if statements below.
366 * Basically if TOGGLE, then we want the if to test "csp->toggled_on", else we don't
367 * And if FORCE_LOAD, then we want the if to test "csp->toggled_on", else we don't
370 # define IS_TOGGLED_ON_AND (csp->toggled_on) &&
371 #else /* ifndef TOGGLE */
372 # define IS_TOGGLED_ON_AND
373 #endif /* ndef TOGGLE */
375 # define IS_NOT_FORCED_AND (!csp->force) &&
376 #else /* ifndef FORCE_LOAD */
377 # define IS_NOT_FORCED_AND
378 #endif /* def FORCE_LOAD */
380 #define IS_ENABLED_AND IS_TOGGLED_ON_AND IS_NOT_FORCED_AND
382 char buf[BUFSIZ], *hdr, *p, *req;
386 int n, maxfd, server_body;
387 int ms_iis5_hack = 0;
389 const struct forward_spec * fwd;
390 struct http_request *http;
392 int block_popups; /* bool, 1==will block popups */
393 int block_popups_now = 0; /* bool, 1==currently blocking popups */
394 #endif /* def KILLPOPUPS */
396 int pcrs_filter; /* bool, 1==will filter through pcrs */
397 int filtering = 0; /* bool, 1==currently filtering through pcrs */
398 #endif /* def PCRS */
399 struct http_response *rsp;
404 * Read the client's request. Note that since we're not using select() we
405 * could get blocked here if a client connected, then didn't say anything!
410 n = read_socket(csp->cfd, buf, sizeof(buf));
412 if (n <= 0) break; /* error! */
414 add_to_iob(csp, buf, n);
416 req = get_header(csp);
420 break; /* no HTTP request! */
425 continue; /* more to come! */
429 /* If this request contains the FORCE_PREFIX,
430 * better get rid of it now and set the force flag --oes
433 if (strstr(req, FORCE_PREFIX))
435 strclean(req, FORCE_PREFIX);
436 log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", req);
443 #endif /* def FORCE_LOAD */
445 parse_http_request(req, http, csp);
450 if (http->cmd == NULL)
452 strcpy(buf, CHEADER);
453 write_socket(csp->cfd, buf, strlen(buf));
455 log_error(LOG_LEVEL_CLF, "%s - - [%T] \" \" 400 0", csp->ip_addr_str);
460 /* decide how to route the HTTP request */
462 if ((fwd = forward_url(http, csp)) == NULL)
464 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
465 /* Never get here - LOG_LEVEL_FATAL causes program exit */
468 /* build the http request to send to the server
469 * we have to do one of the following:
471 * create = use the original HTTP request to create a new
472 * HTTP request that has only the path component
473 * without the http://domainspec
474 * pass = pass the original HTTP request unchanged
476 * drop = drop the HTTP request
481 * +--------+--------+
483 * 0 | create | drop |
485 * Forwarding +--------+--------+
489 * +--------+--------+
493 if (fwd->forward_host)
495 /* if forwarding, just pass the request as is */
496 enlist(csp->headers, http->cmd);
502 /* otherwise elide the host information from the url */
504 p = strsav(p, http->gpc);
506 p = strsav(p, http->path);
508 p = strsav(p, http->ver);
509 enlist(csp->headers, p);
514 /* decide what we're to do with cookies */
517 if (!csp->toggled_on)
519 /* Most compatible set of actions (i.e. none) */
520 init_current_action(csp->action);
523 #endif /* ndef TOGGLE */
525 url_actions(http, csp);
530 * If we're logging cookies in a cookie jar, and the user has not
531 * supplied any wafers, and the user has not told us to suppress the
532 * vanilla wafer, then send the vanilla wafer.
534 if ((csp->config->jarfile != NULL)
535 && (csp->action->multi[ACTION_MULTI_WAFER]->next == NULL)
536 && ((csp->action->flags & ACTION_VANILLA_WAFER) != 0))
538 enlist(csp->action->multi[ACTION_MULTI_WAFER], VANILLA_WAFER);
540 #endif /* def JAR_FILES */
543 block_popups = ((csp->action->flags & ACTION_NO_POPUPS) != 0);
544 #endif /* def KILLPOPUPS */
546 pcrs_filter = (csp->rlist != NULL) && /* There are expressions to be used */
547 ((csp->action->flags & ACTION_FILTER) != 0);
548 #endif /* def PCRS */
551 /* grab the rest of the client's headers */
555 if ( ( p = get_header(csp) ) && ( *p == '\0' ) )
557 n = read_socket(csp->cfd, buf, sizeof(buf));
560 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
563 add_to_iob(csp, buf, n);
567 if (p == NULL) break;
569 enlist(csp->headers, p);
573 /* filter it as required */
575 hdr = sed(client_patterns, add_client_headers, csp);
577 destroy_list(csp->headers);
579 /* Check the request against all rules, unless
580 * we're toggled off or in force mode.
583 if (NULL != (rsp = cgi_dispatch(csp)))
585 if(0 != (n = make_http_response(rsp)))
587 if ((write_socket(csp->cfd, rsp->head, n) != n)
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);
597 #endif /* def STATISTICS */
599 free_http_response(rsp);
604 #ifdef FAST_REDIRECTS
605 else if (IS_ENABLED_AND
606 ((csp->action->flags & ACTION_FAST_REDIRECTS) != 0) &&
607 (p = redirect_url(http, csp)))
609 /* This must be blocked as HTML */
612 #endif /* def STATISTICS */
614 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
616 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3",
617 csp->ip_addr_str, http->cmd);
619 /* Send HTML redirection result */
620 write_socket(csp->cfd, p, strlen(p));
626 #endif /* def FAST_REDIRECTS */
628 else if (IS_ENABLED_AND (
630 (p = trust_url(http, csp)) ||
631 #endif /* def TRUST_FILES */
632 (p = block_url(http, csp)) ))
634 /* Block as HTML or image */
637 #endif /* def STATISTICS */
639 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
641 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 1",
642 csp->ip_addr_str, http->cmd);
644 #ifdef IMAGE_BLOCKING
645 /* Block as image? */
646 if ( ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
647 && block_imageurl(http, csp) )
649 /* Send "blocked" image */
650 const char * blocker = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
652 log_error(LOG_LEVEL_GPC, "%s%s image crunch! --> %s",
653 http->hostport, http->path, (blocker ? blocker : "logo"));
655 if ((blocker == NULL) || (0 == strcmpic(blocker, "logo")))
657 write_socket(csp->cfd, JBGIF, sizeof(JBGIF)-1);
659 else if (0 == strcmpic(blocker, "blank"))
661 write_socket(csp->cfd, BLANKGIF, sizeof(BLANKGIF)-1);
666 p = (char *)malloc(sizeof(HTTP_REDIRECT_TEMPLATE) + strlen(blocker));
667 sprintf(p, HTTP_REDIRECT_TEMPLATE, blocker);
668 write_socket(csp->cfd, p, strlen(p));
672 #endif /* def IMAGE_BLOCKING */
675 /* Send HTML "blocked" message */
676 write_socket(csp->cfd, p, strlen(p));
684 log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
686 if (fwd->forward_host)
688 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
689 fwd->forward_host, fwd->forward_port, http->hostport);
693 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
696 /* here we connect to the server, gateway, or the forwarder */
698 csp->sfd = forwarded_connect(fwd, http, csp);
702 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
707 err = zalloc(strlen(CNXDOM) + strlen(http->host));
708 sprintf(err, CNXDOM, http->host);
710 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 404 0",
711 csp->ip_addr_str, http->cmd);
715 eno = safe_strerror(errno);
716 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
717 sprintf(err, CFAIL, http->hostport, eno);
719 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
720 csp->ip_addr_str, http->cmd);
723 write_socket(csp->cfd, err, strlen(err));
730 log_error(LOG_LEVEL_CONNECT, "OK");
732 if (fwd->forward_host || (http->ssl == 0))
734 /* write the client's (modified) header to the server
735 * (along with anything else that may be in the buffer)
740 if ((write_socket(csp->sfd, hdr, n) != n)
741 || (flush_socket(csp->sfd, csp ) < 0))
743 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
746 eno = safe_strerror(errno);
747 err = zalloc(strlen(CFAIL) + strlen(http->hostport) + strlen(eno));
748 sprintf(err, CFAIL, http->hostport, eno);
749 write_socket(csp->cfd, err, strlen(err));
751 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
752 csp->ip_addr_str, http->cmd);
762 * We're running an SSL tunnel and we're not forwarding,
763 * so just send the "connect succeeded" message to the
764 * client, flush the rest, and get out of the way.
766 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 2\n",
767 csp->ip_addr_str, http->cmd);
769 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0)
777 /* we're finished with the client's header */
780 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
782 /* pass data between the client and server
783 * until one or the other shuts down the connection.
792 FD_SET(csp->cfd, &rfds);
793 FD_SET(csp->sfd, &rfds);
795 n = select(maxfd+1, &rfds, NULL, NULL, NULL);
799 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
803 /* this is the body of the browser's request
804 * just read it and write it.
807 if (FD_ISSET(csp->cfd, &rfds))
809 n = read_socket(csp->cfd, buf, sizeof(buf));
813 break; /* "game over, man" */
816 if (write_socket(csp->sfd, buf, n) != n)
818 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
825 * The server wants to talk. It could be the header or the body.
826 * If `hdr' is null, then it's the header otherwise it's the body.
827 * FIXME: Does `hdr' really mean `host'?
831 if (FD_ISSET(csp->sfd, &rfds))
834 n = read_socket(csp->sfd, buf, sizeof(buf) - 1);
838 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
840 eno = safe_strerror(errno);
841 sprintf(buf, CFAIL, http->hostport, eno);
844 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
845 csp->ip_addr_str, http->cmd);
847 write_socket(csp->cfd, buf, strlen(buf));
851 /* Add a trailing zero. This lets filter_popups
852 * use string operations.
857 /* Filter the popups on this read. */
858 if (block_popups_now)
860 filter_popups(buf, n);
862 #endif /* def KILLPOPUPS */
864 /* Normally, this would indicate that we've read
865 * as much as the server has sent us and we can
866 * close the client connection. However, Microsoft
867 * in its wisdom has released IIS/5 with a bug that
868 * prevents it from sending the trailing \r\n in
869 * a 302 redirect header (and possibly other headers).
870 * To work around this if we've haven't parsed
871 * a full header we'll append a trailing \r\n
872 * and see if this now generates a valid one.
874 * This hack shouldn't have any impacts. If we've
875 * already transmitted the header or if this is a
876 * SSL connection, then we won't bother with this
877 * hack. So we only work on partially received
878 * headers. If we append a \r\n and this still
879 * doesn't generate a valid header, then we won't
880 * transmit anything to the client.
884 /* This hack must only be enforced for headers. */
885 if (server_body || http->ssl)
890 p = re_process_buffer(csp);
891 hdr = sed(server_patterns, add_server_headers, csp);
893 if ((write_socket(csp->cfd, hdr, n) != n)
894 || (write_socket(csp->cfd, p, csp->content_length) != csp->content_length))
896 log_error(LOG_LEVEL_CONNECT, "write modified content to client failed: %E");
902 #endif /* def PCRS */
903 break; /* "game over, man" */
906 /* Let's pretend the server just sent us a blank line. */
907 n = sprintf(buf, "\r\n");
910 * Now, let the normal header parsing algorithm below do its
911 * job. If it fails, we'll exit instead of continuing.
918 * If this is an SSL connection or we're in the body
919 * of the server document, just write it to the client.
922 if (server_body || http->ssl)
927 add_to_iob(csp, buf, n); /* Buffer the body for filtering */
930 #endif /* def PCRS */
933 if (write_socket(csp->cfd, buf, n) != n)
935 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
944 /* we're still looking for the end of the
945 * server's header ... (does that make header
946 * parsing an "out of body experience" ?
949 /* buffer up the data we just read */
950 add_to_iob(csp, buf, n);
952 /* get header lines from the iob */
954 while ((p = get_header(csp)))
958 /* see following note */
961 enlist(csp->headers, p);
965 /* NOTE: there are no "empty" headers so
966 * if the pointer `p' is not NULL we must
967 * assume that we reached the end of the
968 * buffer before we hit the end of the header.
975 /* Well, we tried our MS IIS/5
976 * hack and it didn't work.
977 * The header is incomplete
978 * and there isn't anything
979 * we can do about it.
985 /* Since we have to wait for
986 * more from the server before
987 * we can parse the headers
988 * we just continue here.
994 /* we have now received the entire header.
995 * filter it and send the result to the client
998 hdr = sed(server_patterns, add_server_headers, csp);
1001 /* write the server's (modified) header to
1002 * the client (along with anything else that
1003 * may be in the buffer)
1007 /* Start blocking popups if appropriate. */
1009 if (csp->is_text && /* It's a text / * MIME-Type */
1010 !http->ssl && /* We talk plaintext */
1011 block_popups) /* Policy allows */
1013 block_popups_now = 1;
1016 #endif /* def KILLPOPUPS */
1019 /* Start re_filtering this if appropriate. */
1021 if (csp->is_text && /* It's a text / * MIME-Type */
1022 !http->ssl && /* We talk plaintext */
1023 pcrs_filter) /* Policy allows */
1028 /* This next line is a little ugly, but it simplifies the if statement below. */
1029 /* Basically if using PCRS, we want the OR condition to require "!filtering" */
1030 #define NOT_FILTERING_AND !filtering &&
1032 #else /* not def PCRS */
1034 #define NOT_FILTERING_AND
1036 #endif /* def PCRS */
1038 if (NOT_FILTERING_AND ((write_socket(csp->cfd, hdr, n) != n)
1039 || (n = flush_socket(csp->cfd, csp) < 0)))
1041 log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
1043 /* the write failed, so don't bother
1044 * mentioning it to the client...
1045 * it probably can't hear us anyway.
1051 NOT_FILTERING_AND (byte_count += n);
1053 /* we're finished with the server's header */
1058 /* If this was a MS IIS/5 hack then it means
1059 * the server has already closed the
1060 * connection. Nothing more to read. Time
1071 return; /* huh? we should never get here */
1074 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %d",
1075 csp->ip_addr_str, http->cmd, byte_count);
1079 /*********************************************************************
1083 * Description : This is little more than chat. We only "serve" to
1084 * to close any socket that chat may have opened.
1087 * 1 : csp = Current client state (buffers, headers, etc...)
1091 *********************************************************************/
1093 void serve(struct client_state *csp)
1094 #else /* ifndef AMIGA */
1095 static void serve(struct client_state *csp)
1096 #endif /* def AMIGA */
1099 close_socket(csp->cfd);
1103 close_socket(csp->sfd);
1113 /*********************************************************************
1115 * Function : server_thread
1117 * Description : We only exist to call `serve' in a threaded environment.
1120 * 1 : data = Current client state (buffers, headers, etc...)
1122 * Returns : Always 0.
1124 *********************************************************************/
1125 static int32 server_thread(void *data)
1127 serve((struct client_state *) data);
1135 /*********************************************************************
1139 * Description : Load the config file and start the listen loop.
1140 * This function is a lot more *sane* with the `load_config'
1141 * and `listen_loop' functions; although it stills does
1142 * a *little* too much for my taste.
1145 * 1 : argc = Number of parameters (including $0).
1146 * 2 : argv = Array of (char *)'s to the parameters.
1148 * Returns : 1 if : can't open config file, unrecognized directive,
1149 * stats requested in multi-thread mode, can't open the
1150 * log file, can't open the jar file, listen port is invalid,
1151 * any load fails, and can't bind port.
1153 * Else main never returns, the process must be signaled
1154 * to terminate execution. Or, on Windows, use the
1155 * "File", "Exit" menu option.
1157 *********************************************************************/
1159 int _main(int argc, const char *argv[])
1161 int main(int argc, const char *argv[])
1166 "AmiTCP:db/junkbuster/config"
1167 #elif !defined(_WIN32)
1174 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
1175 if ((argc >= 2) && (strcmp(argv[1], "--help")==0))
1177 printf("JunkBuster proxy version " VERSION ".\n\n"
1178 "Usage: %s [configfile]\n\n"
1179 "See " HOME_PAGE_URL " for details.\n"
1180 "This program is distributed under the GNU GPL, version 2 or later.\n",
1184 if ((argc >= 2) && (strcmp(argv[1], "--version")==0))
1186 printf(VERSION "\n");
1189 #endif /* !defined(_WIN32) || defined(_WIN_CONSOLE) */
1196 configfile = argv[1];
1203 #elif defined(_WIN32)
1209 signal(SIGPIPE, SIG_IGN);
1210 signal(SIGCHLD, SIG_IGN);
1212 #else /* ifdef _WIN32 */
1213 # ifdef _WIN_CONSOLE
1215 * We *are* in a windows console app.
1216 * Print a verbose messages about FAQ's and such
1218 printf(win32_blurb);
1219 # endif /* def _WIN_CONSOLE */
1220 #endif /* def _WIN32 */
1231 /*********************************************************************
1233 * Function : listen_loop
1235 * Description : bind the listen port and enter a "FOREVER" listening loop.
1241 *********************************************************************/
1242 static void listen_loop(void)
1244 struct client_state *csp = NULL;
1246 struct configuration_spec * config;
1248 config = load_config();
1250 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1251 config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1253 bfd = bind_port(config->haddr, config->hport);
1257 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1258 "- There may be another junkbuster or some other "
1259 "proxy running on port %d",
1260 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1261 config->hport, config->hport
1263 /* shouldn't get here */
1267 config->need_bind = 0;
1272 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1273 while (waitpid(-1, NULL, WNOHANG) > 0)
1275 /* zombie children */
1277 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1280 if ( NULL == (csp = (struct client_state *) zalloc(sizeof(*csp))) )
1282 log_error(LOG_LEVEL_FATAL, "malloc(%d) for csp failed: %E", sizeof(*csp));
1286 memset(csp, '\0', sizeof(*csp));
1291 csp->config = config = load_config();
1293 if ( config->need_bind )
1296 * Since we were listening to the "old port", we will not see
1297 * a "listen" param change until the next IJB request. So, at
1298 * least 1 more request must be made for us to find the new
1299 * setting. I am simply closing the old socket and binding the
1302 * Which-ever is correct, we will serve 1 more page via the
1303 * old settings. This should probably be a "show-proxy-args"
1304 * request. This should not be a so common of an operation
1305 * that this will hurt people's feelings.
1310 log_error(LOG_LEVEL_CONNECT, "bind (%s, %d)",
1311 config->haddr ? config->haddr : "INADDR_ANY", config->hport);
1312 bfd = bind_port(config->haddr, config->hport);
1316 log_error(LOG_LEVEL_FATAL, "can't bind %s:%d: %E "
1317 "- There may be another junkbuster or some other "
1318 "proxy running on port %d",
1319 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1320 config->hport, config->hport
1322 /* shouldn't get here */
1326 config->need_bind = 0;
1329 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1331 if (!accept_connection(csp, bfd))
1333 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1346 log_error(LOG_LEVEL_CONNECT, "OK");
1350 /* by haroon - most of credit to srt19170 */
1351 csp->toggled_on = g_bToggleIJB;
1354 if (run_loader(csp))
1356 log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
1357 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1360 if (block_acl(NULL,csp))
1362 log_error(LOG_LEVEL_CONNECT, "Connection dropped due to ACL");
1363 close_socket(csp->cfd);
1368 /* add it to the list of clients */
1369 csp->next = clients->next;
1370 clients->next = csp;
1372 if (config->multi_threaded)
1376 /* this is a switch () statment in the C preprocessor - ugh */
1377 #undef SELECTED_ONE_OPTION
1379 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1380 #define SELECTED_ONE_OPTION
1381 child_id = _beginthread(
1387 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1388 #define SELECTED_ONE_OPTION
1390 thread_id tid = spawn_thread
1391 (server_thread, "server", B_NORMAL_PRIORITY, csp);
1393 if ((tid >= 0) && (resume_thread(tid) == B_OK))
1395 child_id = (int) tid;
1404 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
1405 #define SELECTED_ONE_OPTION
1406 csp->cfd = ReleaseSocket(csp->cfd, -1);
1407 if((child_id = (int)CreateNewProcTags(
1408 NP_Entry, (ULONG)server_thread,
1409 NP_Output, Output(),
1410 NP_CloseOutput, FALSE,
1411 NP_Name, (ULONG)"junkbuster child",
1412 NP_StackSize, 200*1024,
1416 ((struct Task *)child_id)->tc_UserData = csp;
1417 Signal((struct Task *)child_id, SIGF_SINGLE);
1422 #if !defined(SELECTED_ONE_OPTION)
1426 #undef SELECTED_ONE_OPTION
1427 /* end of cpp switch () */
1429 if (child_id < 0) /* failed */
1433 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1435 sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
1437 write_socket(csp->cfd, buf, strlen(buf));
1438 close_socket(csp->cfd);
1444 #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA)
1445 /* This block is only needed when using fork().
1446 * When using threads, the server thread was
1447 * created and run by the call to _beginthread().
1449 if (child_id == 0) /* child */
1457 /* in a fork()'d environment, the parent's
1458 * copy of the client socket and the CSP
1462 #if !defined(_WIN32) && defined(__CYGWIN__)
1464 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1465 close_socket(csp->cfd);
1468 #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */