1 const char jcc_rcs[] = "$Id: jcc.c,v 2.4 2002/12/28 03:58:19 david__schmidt Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/src/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 * Privoxy team. http://www.privoxy.org/
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 2.4 2002/12/28 03:58:19 david__schmidt
37 * Initial drop of dashboard instrumentation - enabled with
38 * --enable-activity-console
40 * Revision 2.3 2002/07/18 22:06:12 jongfoster
41 * Trivial formatting changes
43 * Revision 2.2 2002/07/12 04:26:17 agotneja
44 * Re-factored 'chat()' to become understandable and maintainable as
45 * a first step in adding Transparent Proxy functionality.
47 * Added several new static functions in jcc.c, and moved some data
48 * parameters up into project.h to allow them to be passed between
51 * Revision 1.91 2002/04/08 20:35:58 swa
54 * Revision 1.90 2002/04/02 14:57:28 oes
55 * Made sending wafers independent of FEATURE_COOKIE_JAR
57 * Revision 1.89 2002/03/31 17:18:59 jongfoster
58 * Win32 only: Enabling STRICT to fix a VC++ compile warning.
60 * Revision 1.88 2002/03/27 14:32:43 david__schmidt
61 * More compiler warning message maintenance
63 * Revision 1.87 2002/03/26 22:29:54 swa
64 * we have a new homepage!
66 * Revision 1.86 2002/03/25 17:04:55 david__schmidt
67 * Workaround for closing the jarfile before load_config() comes around again
69 * Revision 1.85 2002/03/24 15:23:33 jongfoster
72 * Revision 1.84 2002/03/24 13:25:43 swa
73 * name change related issues
75 * Revision 1.83 2002/03/16 23:54:06 jongfoster
76 * Adding graceful termination feature, to help look for memory leaks.
77 * If you enable this (which, by design, has to be done by hand
78 * editing config.h) and then go to http://i.j.b/die, then the program
79 * will exit cleanly after the *next* request. It should free all the
80 * memory that was used.
82 * Revision 1.82 2002/03/13 00:27:05 jongfoster
85 * Revision 1.81 2002/03/12 01:42:50 oes
86 * Introduced modular filters
88 * Revision 1.80 2002/03/11 22:07:05 david__schmidt
89 * OS/2 port maintenance:
90 * - Fixed EMX build - it had decayed a little
91 * - Fixed inexplicable crash during FD_ZERO - must be due to a bad macro.
92 * substituted a memset for now.
94 * Revision 1.79 2002/03/09 20:03:52 jongfoster
95 * - Making various functions return int rather than size_t.
96 * (Undoing a recent change). Since size_t is unsigned on
97 * Windows, functions like read_socket that return -1 on
98 * error cannot return a size_t.
100 * THIS WAS A MAJOR BUG - it caused frequent, unpredictable
101 * crashes, and also frequently caused JB to jump to 100%
102 * CPU and stay there. (Because it thought it had just
103 * read ((unsigned)-1) == 4Gb of data...)
105 * - The signature of write_socket has changed, it now simply
106 * returns success=0/failure=nonzero.
108 * - Trying to get rid of a few warnings --with-debug on
109 * Windows, I've introduced a new type "jb_socket". This is
110 * used for the socket file descriptors. On Windows, this
111 * is SOCKET (a typedef for unsigned). Everywhere else, it's
112 * an int. The error value can't be -1 any more, so it's
113 * now JB_INVALID_SOCKET (which is -1 on UNIX, and in
114 * Windows it maps to the #define INVALID_SOCKET.)
116 * - The signature of bind_port has changed.
118 * Revision 1.78 2002/03/08 21:35:04 oes
119 * Added optional group supplement to --user option. Will now use default group of user if no group given
121 * Revision 1.77 2002/03/07 03:52:06 oes
122 * - Fixed compiler warnings etc
123 * - Improved handling of failed DNS lookups
125 * Revision 1.76 2002/03/06 22:54:35 jongfoster
126 * Automated function-comment nitpicking.
128 * Revision 1.75 2002/03/06 10:02:19 oes
129 * Fixed stupid bug when --user was not given
131 * Revision 1.74 2002/03/06 00:49:31 jongfoster
132 * Fixing warning on Windows
133 * Making #ifdefs that refer to the same variable consistently
134 * use #ifdef unix rather than mixing #ifdef unix & #ifndef OS2
136 * Revision 1.73 2002/03/05 23:57:30 hal9
137 * Stray character 's' on line 1618 was breaking build.
139 * Revision 1.72 2002/03/05 21:33:45 david__schmidt
140 * - Re-enable OS/2 building after new parms were added
141 * - Fix false out of memory report when resolving CGI templates when no IP
142 * address is available of failed attempt (a la no such domain)
144 * Revision 1.71 2002/03/05 18:13:56 oes
145 * Added --user option
147 * Revision 1.70 2002/03/05 04:52:42 oes
148 * Deleted non-errlog debugging code
150 * Revision 1.69 2002/03/04 23:50:00 jongfoster
151 * Splitting off bind_port() call into bind_port_helper(), with
154 * Revision 1.68 2002/03/04 20:17:32 oes
157 * Revision 1.67 2002/03/04 18:18:57 oes
158 * - Removed _DEBUG mode
159 * - Cleand up cmdline parsing
160 * - Introduced --no-daemon, --pidfile options
161 * - Cleaned up signal handling:
162 * - Terminate cleanly on INT, TERM and ABRT
163 * - Schedule logfile for re-opening on HUP
164 * - Ignore CHLD and PIPE
165 * - Leave the rest with their default handlers
166 * - Uniform handler registration
167 * - Added usage() function
168 * - Played styleguide police
170 * Revision 1.66 2002/03/03 15:06:55 oes
171 * Re-enabled automatic config reloading
173 * Revision 1.65 2002/03/03 14:49:11 oes
174 * Fixed CLF logging: Now uses client's original HTTP request
176 * Revision 1.64 2002/03/03 09:18:03 joergs
177 * Made jumbjuster work on AmigaOS again.
179 * Revision 1.63 2002/03/02 04:14:50 david__schmidt
180 * Clean up a little CRLF unpleasantness that suddenly appeared
182 * Revision 1.62 2002/02/20 23:17:23 jongfoster
183 * Detecting some out-of memory conditions and exiting with a log message.
185 * Revision 1.61 2002/01/17 21:01:52 jongfoster
186 * Moving all our URL and URL pattern parsing code to urlmatch.c.
188 * Revision 1.60 2001/12/30 14:07:32 steudten
189 * - Add signal handling (unix)
190 * - Add SIGHUP handler (unix)
191 * - Add creation of pidfile (unix)
192 * - Add action 'top' in rc file (RH)
193 * - Add entry 'SIGNALS' to manpage
194 * - Add exit message to logfile (unix)
196 * Revision 1.59 2001/12/13 14:07:18 oes
197 * Fixed Bug: 503 error page now sent OK
199 * Revision 1.58 2001/11/30 23:37:24 jongfoster
200 * Renaming the Win32 config file to config.txt - this is almost the
201 * same as the corresponding UNIX name "config"
203 * Revision 1.57 2001/11/16 00:47:43 jongfoster
204 * Changing the tty-disconnection code to use setsid().
206 * Revision 1.56 2001/11/13 20:20:54 jongfoster
207 * Tabs->spaces, fixing a bug with missing {} around an if()
209 * Revision 1.55 2001/11/13 20:14:53 jongfoster
210 * Patch for FreeBSD setpgrp() as suggested by Alexander Lazic
212 * Revision 1.54 2001/11/07 00:03:14 steudten
213 * Give reliable return value if an error
214 * occurs not just 0 with new daemon mode.
216 * Revision 1.53 2001/11/05 21:41:43 steudten
217 * Add changes to be a real daemon just for unix os.
218 * (change cwd to /, detach from controlling tty, set
219 * process group and session leader to the own process.
221 * Add some fatal-error log message for failed malloc().
222 * Add '-d' if compiled with 'configure --with-debug' to
223 * enable debug output.
225 * Revision 1.52 2001/10/26 20:11:20 jongfoster
226 * Fixing type mismatch
228 * Revision 1.51 2001/10/26 17:38:28 oes
231 * Revision 1.50 2001/10/25 03:40:48 david__schmidt
232 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
233 * threads to call select() simultaneously. So, it's time to do a real, live,
234 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
235 * (native). Both versions will work, but using __OS2__ offers multi-threading.
237 * Revision 1.49 2001/10/23 21:41:35 jongfoster
238 * Added call to initialize the (statically-allocated of course)
239 * "out of memory" CGI response.
241 * Revision 1.48 2001/10/10 19:56:46 jongfoster
242 * Moving some code that wasn't cookie-related out of an #ifdef
245 * Revision 1.47 2001/10/10 16:44:36 oes
246 * Added CONNECT destination port limitation check
248 * Revision 1.46 2001/10/08 15:17:41 oes
249 * Re-enabled SSL forwarding
251 * Revision 1.45 2001/10/07 15:42:11 oes
252 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
254 * Moved downgrading of the HTTP version from parse_http_request to
255 * chat(), since we can't decide if it is necessary before we have
256 * determined the actions for the URL. The HTTP command is now
257 * *always* re-built so the repairs need no longer be special-cased.
259 * filter_popups now gets a csp pointer so it can raise the new
260 * CSP_FLAG_MODIFIED flag.
264 * Added configurable size limit for the IOB. If the IOB grows so
265 * large that the next read would exceed the limit, the header
266 * is generated, and the header & unfiltered buffer are flushed
267 * to the client. Chat then continues in non-buffering,
268 * non-filtering body mode.
270 * Revision 1.44 2001/10/02 18:13:57 oes
273 * Revision 1.43 2001/10/02 15:32:13 oes
274 * Moved generation of hdr
276 * Revision 1.42 2001/09/21 23:02:02 david__schmidt
277 * Cleaning up 2 compiler warnings on OS/2.
279 * Revision 1.41 2001/09/16 17:05:14 jongfoster
280 * Removing unused #include showarg.h
282 * Revision 1.40 2001/09/16 15:41:45 jongfoster
283 * Fixing signed/unsigned comparison warning.
285 * Revision 1.39 2001/09/16 13:21:27 jongfoster
286 * Changes to use new list functions.
288 * Revision 1.38 2001/09/16 13:01:46 jongfoster
289 * Removing redundant function call that zeroed zalloc()'d memory.
291 * Revision 1.37 2001/09/10 11:12:24 oes
292 * Deleted unused variable
294 * Revision 1.36 2001/09/10 10:56:15 oes
295 * Silenced compiler warnings
297 * Revision 1.35 2001/07/31 14:44:22 oes
298 * Deleted unused size parameter from filter_popups()
300 * Revision 1.34 2001/07/30 22:08:36 jongfoster
301 * Tidying up #defines:
302 * - All feature #defines are now of the form FEATURE_xxx
303 * - Permanently turned off WIN_GUI_EDIT
304 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
306 * Revision 1.33 2001/07/29 19:32:00 jongfoster
307 * Renaming _main() [mingw32 only] to real_main(), for ANSI compliance.
309 * Revision 1.32 2001/07/29 18:47:05 jongfoster
310 * Adding missing #include "loadcfg.h"
312 * Revision 1.31 2001/07/29 12:17:48 oes
313 * Applied pthread fix by Paul Lieverse
315 * Revision 1.30 2001/07/25 22:57:13 jongfoster
316 * __BEOS__ no longer overrides FEATURE_PTHREAD.
317 * This is because FEATURE_PTHREAD will soon be widely used, so I
318 * want to keep it simple.
320 * Revision 1.29 2001/07/24 12:47:06 oes
321 * Applied BeOS support update by Eugenia
323 * Revision 1.28 2001/07/23 13:26:12 oes
324 * Fixed bug in popup-killing for the first read that caused binary garbage to be sent between headers and body
326 * Revision 1.27 2001/07/19 19:09:47 haroon
327 * - Added code to take care of the situation where while processing the first
328 * server response (which includes the server header), after finding the end
329 * of the headers we were not looking past the end of the headers for
330 * content modification. I enabled it for filter_popups.
331 * Someone else should look to see if other similar operations should be
332 * done to the discarded portion of the buffer.
334 * Note 2001/07/20: No, the other content modification mechanisms will process
335 * the whole iob later anyway. --oes
337 * Revision 1.26 2001/07/18 12:31:36 oes
340 * Revision 1.25 2001/07/15 19:43:49 jongfoster
341 * Supports POSIX threads.
342 * Also removed some unused #includes.
344 * Revision 1.24 2001/07/13 14:00:40 oes
345 * - Generic content modification scheme:
346 * Each feature has its own applicability flag that is set
347 * from csp->action->flags.
348 * Replaced the "filtering" int flag , by a function pointer
349 * "content_filter" to the function that will do the content
350 * modification. If it is != NULL, the document will be buffered
351 * and processed through *content_filter, which must set
352 * csp->content_length and return a modified copy of the body
353 * or return NULL (on failiure).
354 * - Changed csp->is_text to the more generic bitmap csp->content_type
355 * which can currently take the valued CT_TEXT or CT_GIF
357 * - Removed all #ifdef PCRS
359 * Revision 1.23 2001/07/02 02:28:25 iwanttokeepanon
360 * Added "#ifdef ACL_FILES" conditional compilation to line 1291 to exclude
361 * the `block_acl' call. This prevents a compilation error when the user
362 * does not wish to use the "ACL" feature.
364 * Revision 1.22 2001/06/29 21:45:41 oes
365 * Indentation, CRLF->LF, Tab-> Space
367 * Revision 1.21 2001/06/29 13:29:36 oes
368 * - Cleaned up, improved comments
369 * - Unified all possible interceptors (CGI,
370 * block, trust, fast_redirect) in one
371 * place, with one (CGI) answer generation
372 * mechansim. Much clearer now.
373 * - Removed the GIF image generation, which
374 * is now done in filters.c:block_url()
375 * - Made error conditions like domain lookup
376 * failiure or (various) problems while talking
377 * to the server use cgi.c:error_response()
378 * instead of generating HTML/HTTP in chat() (yuck!)
379 * - Removed logentry from cancelled commit
381 * Revision 1.20 2001/06/09 10:55:28 jongfoster
382 * Changing BUFSIZ ==> BUFFER_SIZE
384 * Revision 1.19 2001/06/07 23:12:52 jongfoster
385 * Replacing function pointer in struct gateway with a directly
386 * called function forwarded_connect().
387 * Replacing struct gateway with struct forward_spec
389 * Revision 1.18 2001/06/03 19:12:16 oes
390 * introduced new cgi handling
392 * Revision 1.17 2001/06/01 20:07:23 jongfoster
393 * Now uses action +image-blocker{} rather than config->tinygif
395 * Revision 1.16 2001/06/01 18:49:17 jongfoster
396 * Replaced "list_share" with "list" - the tiny memory gain was not
397 * worth the extra complexity.
399 * Revision 1.15 2001/05/31 21:24:47 jongfoster
400 * Changed "permission" to "action" throughout.
401 * Removed DEFAULT_USER_AGENT - it must now be specified manually.
402 * Moved vanilla wafer check into chat(), since we must now
403 * decide whether or not to add it based on the URL.
405 * Revision 1.14 2001/05/29 20:14:01 joergs
406 * AmigaOS bugfix: PCRS needs a lot of stack, stacksize for child threads
409 * Revision 1.13 2001/05/29 09:50:24 jongfoster
410 * Unified blocklist/imagelist/permissionslist.
411 * File format is still under discussion, but the internal changes
414 * Also modified interceptor behaviour:
415 * - We now intercept all URLs beginning with one of the following
416 * prefixes (and *only* these prefixes):
418 * * http://ijbswa.sf.net/config/
419 * * http://ijbswa.sourceforge.net/config/
420 * - New interceptors "home page" - go to http://i.j.b/ to see it.
421 * - Internal changes so that intercepted and fast redirect pages
422 * are not replaced with an image.
423 * - Interceptors now have the option to send a binary page direct
424 * to the client. (i.e. ijb-send-banner uses this)
425 * - Implemented show-url-info interceptor. (Which is why I needed
426 * the above interceptors changes - a typical URL is
427 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
428 * The previous mechanism would not have intercepted that, and
429 * if it had been intercepted then it then it would have replaced
432 * Revision 1.12 2001/05/27 22:17:04 oes
434 * - re_process_buffer no longer writes the modified buffer
435 * to the client, which was very ugly. It now returns the
436 * buffer, which it is then written by chat.
438 * - content_length now adjusts the Content-Length: header
439 * for modified documents rather than crunch()ing it.
440 * (Length info in csp->content_length, which is 0 for
441 * unmodified documents)
443 * - For this to work, sed() is called twice when filtering.
445 * Revision 1.11 2001/05/26 17:27:53 jongfoster
446 * Added support for CLF and fixed LOG_LEVEL_LOG.
447 * Also did CRLF->LF fix of my previous patch.
449 * Revision 1.10 2001/05/26 15:26:15 jongfoster
450 * ACL feature now provides more security by immediately dropping
451 * connections from untrusted hosts.
453 * Revision 1.9 2001/05/26 00:28:36 jongfoster
454 * Automatic reloading of config file.
455 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
456 * Most of the global variables have been moved to a new
457 * struct configuration_spec, accessed through csp->config->globalname
458 * Most of the globals remaining are used by the Win32 GUI.
460 * Revision 1.8 2001/05/25 22:43:18 jongfoster
461 * Fixing minor memory leak and buffer overflow.
463 * Revision 1.7 2001/05/25 22:34:30 jongfoster
466 * Revision 1.6 2001/05/23 00:13:58 joergs
467 * AmigaOS support fixed.
469 * Revision 1.5 2001/05/22 18:46:04 oes
471 * - Enabled filtering banners by size rather than URL
472 * by adding patterns that replace all standard banner
473 * sizes with the "Junkbuster" gif to the re_filterfile
475 * - Enabled filtering WebBugs by providing a pattern
476 * which kills all 1x1 images
478 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
479 * which is selected by the (nonstandard and therefore
480 * capital) letter 'U' in the option string.
481 * It causes the quantifiers to be ungreedy by default.
482 * Appending a ? turns back to greedy (!).
484 * - Added a new interceptor ijb-send-banner, which
485 * sends back the "Junkbuster" gif. Without imagelist or
486 * MSIE detection support, or if tinygif = 1, or the
487 * URL isn't recognized as an imageurl, a lame HTML
488 * explanation is sent instead.
490 * - Added new feature, which permits blocking remote
491 * script redirects and firing back a local redirect
493 * The feature is conditionally compiled, i.e. it
494 * can be disabled with --disable-fast-redirects,
495 * plus it must be activated by a "fast-redirects"
496 * line in the config file, has its own log level
497 * and of course wants to be displayed by show-proxy-args
498 * Note: Boy, all the #ifdefs in 1001 locations and
499 * all the fumbling with configure.in and acconfig.h
500 * were *way* more work than the feature itself :-(
502 * - Because a generic redirect template was needed for
503 * this, tinygif = 3 now uses the same.
505 * - Moved GIFs, and other static HTTP response templates
510 * - Removed some >400 CRs again (Jon, you really worked
513 * Revision 1.4 2001/05/21 19:34:01 jongfoster
514 * Made failure to bind() a fatal error.
516 * Revision 1.3 2001/05/20 01:21:20 jongfoster
517 * Version 2.9.4 checkin.
518 * - Merged popupfile and cookiefile, and added control over PCRS
519 * filtering, in new "permissionsfile".
520 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
521 * file error you now get a message box (in the Win32 GUI) rather
522 * than the program exiting with no explanation.
523 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
525 * - Removed tabs from "config"
526 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
527 * - Bumped up version number.
529 * Revision 1.2 2001/05/17 22:34:44 oes
530 * - Added hint on GIF char array generation to jcc.c
531 * - Cleaned CRLF's from the sources and related files
532 * - Repaired logging for REF and FRC
534 * Revision 1.1.1.1 2001/05/15 13:58:56 oes
535 * Initial import of version 2.9.3 source tree
538 *********************************************************************/
544 #include <sys/types.h>
551 #ifdef FEATURE_PTHREAD
553 #endif /* def FEATURE_PTHREAD */
556 # ifndef FEATURE_PTHREAD
560 # include <windows.h>
561 # include <process.h>
562 # endif /* ndef FEATURE_PTHREAD */
565 # ifndef _WIN_CONSOLE
567 # endif /* ndef _WIN_CONSOLE */
569 #else /* ifndef _WIN32 */
571 # if !defined (__OS2__)
573 # include <sys/wait.h>
574 # endif /* ndef __OS2__ */
575 # include <sys/time.h>
576 # include <sys/stat.h>
577 # include <sys/ioctl.h>
580 #include <sys/termios.h>
591 # include <socket.h> /* BeOS has select() for sockets only. */
592 # include <OS.h> /* declarations for threads and stuff. */
595 # if defined(__EMX__) || defined(__OS2__)
596 # include <sys/select.h> /* OS/2/EMX needs a little help with select */
601 #define bzero(B,N) memset(B,0x00,n)
616 #include "killpopup.h"
617 #include "miscutil.h"
619 #include "jbsockets.h"
624 #include "urlmatch.h"
625 #ifdef FEATURE_ACTIVITY_CONSOLE
627 #endif /* def FEATURE_ACTIVITY_CONSOLE */
629 const char jcc_h_rcs[] = JCC_H_VERSION;
630 const char project_h_rcs[] = PROJECT_H_VERSION;
633 struct client_state clients[1];
634 struct file_list files[1];
636 #ifdef FEATURE_STATISTICS
637 int urls_read = 0; /* total nr of urls read inc rejected */
638 int urls_rejected = 0; /* total nr of urls rejected */
639 #endif /* def FEATURE_STATISTICS */
641 #ifdef FEATURE_GRACEFUL_TERMINATION
645 static void listen_loop(void);
646 static void chat(struct client_state *csp);
647 static jb_err relay_server_traffic( struct client_state *csp ) ;
648 static jb_err read_client_headers( struct client_state *csp, struct http_request *http ) ;
649 static jb_err process_client_headers( struct client_state *csp, struct http_request *http ) ;
650 static jb_err intercept_page( struct client_state *csp, struct http_request *http );
651 static jb_err open_forwarding_connection( struct client_state *csp );
652 static jb_err send_client_headers_to_server( struct client_state *csp, struct http_request *http, char* hdr );
653 static jb_err is_connect_request_allowed( struct client_state *csp ) ;
657 void serve(struct client_state *csp);
658 #else /* ifndef AMIGA */
659 static void serve(struct client_state *csp);
660 #endif /* def AMIGA */
663 static int32 server_thread(void *data);
664 #endif /* def __BEOS__ */
667 #define sleep(N) Sleep(((N) * 1000))
671 #define sleep(N) DosSleep(((N) * 100))
674 #if defined(unix) || defined(__EMX__)
676 const char *pidfile = NULL;
677 int received_hup_signal = 0;
678 #endif /* defined unix */
680 /* The vanilla wafer. */
681 static const char VANILLA_WAFER[] =
682 "NOTICE=TO_WHOM_IT_MAY_CONCERN_"
683 "Do_not_send_me_any_copyrighted_information_other_than_the_"
684 "document_that_I_am_requesting_or_any_of_its_necessary_components._"
685 "In_particular_do_not_send_me_any_cookies_that_"
686 "are_subject_to_a_claim_of_copyright_by_anybody._"
687 "Take_notice_that_I_refuse_to_be_bound_by_any_license_condition_"
688 "(copyright_or_otherwise)_applying_to_any_cookie._";
691 * HTTP header sent when doing HTTPS tunnelling ("CONNECT" method).
693 static const char CSUCCEED[] =
694 "HTTP/1.0 200 Connection established\n"
695 "Proxy-Agent: Privoxy/" VERSION "\r\n\r\n";
699 * HTTP reply sent when the browser sends something unintelligible.
701 static const char CHEADER[] =
702 "HTTP/1.0 400 Invalid header received from browser\r\n\r\n";
706 * HTTP reply sent when the browser tries to tunnel ("CONNECT") to something
707 * other than a HTTPS port (as defined by +limit-connect).
709 static const char CFORBIDDEN[] =
710 "HTTP/1.0 403 Connection not allowable\r\nX-Hint: If you read this message interactively, then you know why this happens ,-)\r\n\r\n";
712 #if !defined(_WIN32) && !defined(__OS2__) && !defined(AMIGA)
713 /*********************************************************************
715 * Function : sig_handler
717 * Description : Signal handler for different signals.
718 * Exit gracefully on ABRT, TERM and INT
719 * or set a flag that will cause the errlog
720 * to be reopened by the main thread on HUP.
723 * 1 : the_signal = the signal cause this function to call
727 *********************************************************************/
728 static void sig_handler(int the_signal)
735 log_error(LOG_LEVEL_INFO, "exiting by signal %d .. bye", the_signal);
743 received_hup_signal = 1;
748 * We shouldn't be here, unless we catch signals
749 * in main() that we can't handle here!
751 log_error(LOG_LEVEL_FATAL, "sig_handler: exiting on unexpected signal %d", the_signal);
759 /*********************************************************************
763 * Description : Once a connection to the client has been accepted,
764 * this function is called (via serve()) to handle the
765 * main business of the communication. When this
766 * function returns, the caller must close the client
770 * 1 : csp = Current client state (buffers, headers, etc...)
774 *********************************************************************/
775 static void chat(struct client_state *csp)
779 char buf[BUFFER_SIZE];
780 int len; /* for buffer sizes */
786 struct http_request *http;
790 if (read_client_headers(csp, http ) != JB_ERR_OK )
794 /* Process the client headers */
795 if (process_client_headers(csp, http) != JB_ERR_OK)
799 /* Check if this connection request is permitted */
800 if (is_connect_request_allowed(csp) != JB_ERR_OK)
804 * We have a valid and legit request. Now, check to see if we need to
807 if (intercept_page(csp, http) != JB_ERR_OK)
811 * The page passed the intercept routine, so open a forwarding
812 * connection for conversation
814 if (open_forwarding_connection(csp) != JB_ERR_OK )
819 hdr = sed(client_patterns, add_client_headers, csp);
822 /* FIXME Should handle error properly */
823 log_error(LOG_LEVEL_FATAL, "Out of memory parsing client header");
826 /* FIXME - where should this go, logically ? */
827 list_remove_all(csp->headers);
830 if (send_client_headers_to_server(csp, http, hdr) != JB_ERR_OK)
834 /* we're finished with the client's header */
837 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
839 /* pass data between the client and server
840 * until one or the other shuts down the connection.
843 /* Set the flag for reading headers to False */
844 /* This is used in relay_server_traffic */
845 csp->all_headers_read = 0 ;
849 jb_err tmp_ret_val = JB_ERR_GENERIC ;
853 * FD_ZERO here seems to point to an errant macro which crashes.
854 * So do this by hand for now...
856 memset(&rfds,0x00,sizeof(fd_set));
860 FD_SET(csp->cfd, &rfds);
861 FD_SET(csp->sfd, &rfds);
863 n = select((int)maxfd+1, &rfds, NULL, NULL, NULL);
867 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
872 * This is the body of the browser's request.
873 * Just read it and write it.
876 if (FD_ISSET(csp->cfd, &rfds))
878 len = read_socket(csp->cfd, buf, sizeof(buf));
882 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %d",
883 csp->ip_addr_str, http->ocmd, byte_count);
887 if (write_socket(csp->sfd, buf, (size_t)len))
889 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
896 * Check if the server wants to talk, and if so, converse.
899 if (FD_ISSET(csp->sfd, &rfds))
900 tmp_ret_val = relay_server_traffic(csp) ;
903 if (tmp_ret_val == JB_ERR_OK)
908 /* We should never get here! */
909 log_error(LOG_LEVEL_FATAL, "chat() : logic error in for() loop" ) ;
913 /*********************************************************************
917 * Description : This is little more than chat. We only "serve" to
918 * to close any socket that chat may have opened.
921 * 1 : csp = Current client state (buffers, headers, etc...)
925 *********************************************************************/
927 void serve(struct client_state *csp)
928 #else /* ifndef AMIGA */
929 static void serve(struct client_state *csp)
930 #endif /* def AMIGA */
933 close_socket(csp->cfd);
935 if (csp->sfd != JB_INVALID_SOCKET)
937 close_socket(csp->sfd);
940 csp->flags &= ~CSP_FLAG_ACTIVE;
946 /*********************************************************************
948 * Function : server_thread
950 * Description : We only exist to call `serve' in a threaded environment.
953 * 1 : data = Current client state (buffers, headers, etc...)
955 * Returns : Always 0.
957 *********************************************************************/
958 static int32 server_thread(void *data)
960 serve((struct client_state *) data);
967 /*********************************************************************
971 * Description : Print usage info & exit.
973 * Parameters : Pointer to argv[0] for identifying ourselves
977 *********************************************************************/
978 void usage(const char *myname)
980 printf("Privoxy version " VERSION " (" HOME_PAGE_URL ")\n"
981 "Usage: %s [--help] [--version] [--no-daemon] [--pidfile pidfile] [--user user[.group]] [configfile]\n"
982 "Aborting.\n", myname);
989 /*********************************************************************
993 * Description : Load the config file and start the listen loop.
994 * This function is a lot more *sane* with the `load_config'
995 * and `listen_loop' functions; although it stills does
996 * a *little* too much for my taste.
999 * 1 : argc = Number of parameters (including $0).
1000 * 2 : argv = Array of (char *)'s to the parameters.
1002 * Returns : 1 if : can't open config file, unrecognized directive,
1003 * stats requested in multi-thread mode, can't open the
1004 * log file, can't open the jar file, listen port is invalid,
1005 * any load fails, and can't bind port.
1007 * Else main never returns, the process must be signaled
1008 * to terminate execution. Or, on Windows, use the
1009 * "File", "Exit" menu option.
1011 *********************************************************************/
1013 int real_main(int argc, const char *argv[])
1015 int main(int argc, const char *argv[])
1020 struct passwd *pw = NULL;
1021 struct group *grp = NULL;
1029 #if !defined(_WIN32)
1037 * Parse the command line arguments
1039 while (++argc_pos < argc)
1041 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
1043 if (strcmp(argv[argc_pos], "--help") == 0)
1048 else if(strcmp(argv[argc_pos], "--version") == 0)
1050 printf("Privoxy version " VERSION " (" HOME_PAGE_URL ")\n");
1054 else if (strcmp(argv[argc_pos], "--no-daemon" ) == 0)
1059 else if (strcmp(argv[argc_pos], "--pidfile" ) == 0)
1061 if (++argc_pos == argc) usage(argv[0]);
1062 pidfile = strdup(argv[argc_pos]);
1065 else if (strcmp(argv[argc_pos], "--user" ) == 0)
1067 if (++argc_pos == argc) usage(argv[argc_pos]);
1069 if ((NULL != (p = strchr(argv[argc_pos], '.'))) && *(p + 1) != '0')
1072 if (NULL == (grp = getgrnam(p)))
1074 log_error(LOG_LEVEL_FATAL, "Group %s not found.", p);
1078 if (NULL == (pw = getpwnam(argv[argc_pos])))
1080 log_error(LOG_LEVEL_FATAL, "User %s not found.", argv[argc_pos]);
1083 if (p != NULL) *--p = '\0';
1085 #endif /* defined(unix) */
1087 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
1089 configfile = argv[argc_pos];
1092 } /* -END- while (more arguments) */
1095 if ( *configfile != '/' )
1099 /* make config-filename absolute here */
1100 if ( !(basedir = getcwd( NULL, 1024 )))
1102 perror("get working dir failed");
1106 if ( !(abs_file = malloc( strlen( basedir ) + strlen( configfile ) + 5 )))
1108 perror("malloc failed");
1111 strcpy( abs_file, basedir );
1112 strcat( abs_file, "/" );
1113 strcat( abs_file, configfile );
1114 configfile = abs_file;
1116 #endif /* defined unix */
1123 #elif defined(_WIN32)
1128 * Unix signal handling
1130 * Catch the abort, interrupt and terminate signals for a graceful exit
1131 * Catch the hangup signal so the errlog can be reopened.
1132 * Ignore the broken pipe and child signals
1133 * FIXME: Isn't ignoring the default for SIGCHLD anyway and why ignore SIGPIPE?
1135 #if !defined(_WIN32) && !defined(__OS2__) && !defined(AMIGA)
1138 const int catched_signals[] = { SIGABRT, SIGTERM, SIGINT, SIGHUP, 0 };
1139 const int ignored_signals[] = { SIGPIPE, SIGCHLD, 0 };
1141 for (idx = 0; catched_signals[idx] != 0; idx++)
1143 if (signal(catched_signals[idx], sig_handler) == SIG_ERR)
1145 log_error(LOG_LEVEL_FATAL, "Can't set signal-handler for signal %d: %E", catched_signals[idx]);
1149 for (idx = 0; ignored_signals[idx] != 0; idx++)
1151 if (signal(ignored_signals[idx], SIG_IGN) == SIG_ERR)
1153 log_error(LOG_LEVEL_FATAL, "Can't set ignore-handler for signal %d: %E", ignored_signals[idx]);
1158 #else /* ifdef _WIN32 */
1159 # ifdef _WIN_CONSOLE
1161 * We *are* in a windows console app.
1162 * Print a verbose messages about FAQ's and such
1164 printf(win32_blurb);
1165 # endif /* def _WIN_CONSOLE */
1166 #endif /* def _WIN32 */
1169 /* Initialize the CGI subsystem */
1170 cgi_init_error_messages();
1173 * If runnig on unix and without the --nodaemon
1174 * option, become a daemon. I.e. fork, detach
1175 * from tty and get process group leadership
1188 if ( pid < 0 ) /* error */
1193 else if ( pid != 0 ) /* parent */
1198 * must check for errors
1199 * child died due to missing files aso
1202 wpid = waitpid( pid, &status, WNOHANG );
1211 /* Should be more portable, but not as well tested */
1216 #else /* ndef __FreeBSD__ */
1218 #endif /* ndef __FreeBSD__ */
1219 fd = open("/dev/tty", O_RDONLY);
1222 /* no error check here */
1223 ioctl( fd, TIOCNOTTY,0 );
1227 /* FIXME: should close stderr (fd 2) here too, but the test
1229 * and load config file is done in listen_loop() and puts
1230 * some messages on stderr there.
1237 } /* -END- if (!no_daemon) */
1240 * As soon as we have written the PID file, we can switch
1241 * to the user and group ID indicated by the --user option
1247 if (((NULL != grp) && setgid(grp->gr_gid)) || (setgid(pw->pw_gid)))
1249 log_error(LOG_LEVEL_FATAL, "Cannot setgid(): Insufficient permissions.");
1251 if (setuid(pw->pw_uid))
1253 log_error(LOG_LEVEL_FATAL, "Cannot setuid(): Insufficient permissions.");
1257 #endif /* defined unix */
1267 /*********************************************************************
1269 * Function : bind_port_helper
1271 * Description : Bind the listen port. Handles logging, and aborts
1275 * 1 : config = Privoxy configuration. Specifies port
1278 * Returns : Port that was opened.
1280 *********************************************************************/
1281 static jb_socket bind_port_helper(struct configuration_spec * config)
1286 if ( (config->haddr != NULL)
1287 && (config->haddr[0] == '1')
1288 && (config->haddr[1] == '2')
1289 && (config->haddr[2] == '7')
1290 && (config->haddr[3] == '.') )
1292 log_error(LOG_LEVEL_INFO, "Listening on port %d for local connections only",
1295 else if (config->haddr == NULL)
1297 log_error(LOG_LEVEL_INFO, "Listening on port %d on all IP addresses",
1302 log_error(LOG_LEVEL_INFO, "Listening on port %d on IP address %s",
1303 config->hport, config->haddr);
1306 result = bind_port(config->haddr, config->hport, &bfd);
1313 log_error(LOG_LEVEL_FATAL, "can't bind to %s:%d: "
1314 "There may be another Privoxy or some other "
1315 "proxy running on port %d",
1316 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1317 config->hport, config->hport);
1320 log_error(LOG_LEVEL_FATAL, "can't bind to %s:%d: "
1321 "The hostname is not resolvable",
1322 (NULL != config->haddr) ? config->haddr : "INADDR_ANY", config->hport);
1325 log_error(LOG_LEVEL_FATAL, "can't bind to %s:%d: because %E",
1326 (NULL != config->haddr) ? config->haddr : "INADDR_ANY", config->hport);
1329 /* shouldn't get here */
1330 return JB_INVALID_SOCKET;
1333 config->need_bind = 0;
1339 /*********************************************************************
1341 * Function : listen_loop
1343 * Description : bind the listen port and enter a "FOREVER" listening loop.
1349 *********************************************************************/
1350 static void listen_loop(void)
1352 struct client_state *csp = NULL;
1354 struct configuration_spec * config;
1356 config = load_config();
1358 bfd = bind_port_helper(config);
1360 #ifdef FEATURE_ACTIVITY_CONSOLE
1361 init_stats_config(config);
1362 #endif /* def FEATURE_ACTIVITY_CONSOLE */
1364 #ifdef FEATURE_GRACEFUL_TERMINATION
1365 while (!g_terminate)
1370 #if !defined(FEATURE_PTHREAD) && !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
1371 while (waitpid(-1, NULL, WNOHANG) > 0)
1373 /* zombie children */
1375 #endif /* !defined(FEATURE_PTHREAD) && !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
1378 * Free data that was used by died threads
1384 * Re-open the errlog after HUP signal
1386 if (received_hup_signal)
1388 init_error_log(Argv[0], config->logfile, config->debug);
1389 received_hup_signal = 0;
1394 #ifdef FEATURE_COOKIE_JAR
1396 * Need a workaround here: we have to fclose() the jarfile, or we die because it's
1397 * already open. I think unload_configfile() is not being run, which should do
1398 * this work. Until that can get resolved, we'll use this workaround.
1400 * [Note from Jon: We don't unload_configfile() until all existing threads have
1401 * exited - they're likely to hang aroung transferring data to the browser.]
1405 if (csp->config->jar)
1407 fclose(csp->config->jar);
1408 csp->config->jar = NULL;
1410 #endif /* FEATURE_COOKIE_JAR */
1411 #endif /* __OS2__ */
1413 if ( NULL == (csp = (struct client_state *) zalloc(sizeof(*csp))) )
1415 log_error(LOG_LEVEL_FATAL, "malloc(%d) for csp failed: %E", sizeof(*csp));
1419 csp->flags |= CSP_FLAG_ACTIVE;
1420 csp->sfd = JB_INVALID_SOCKET;
1422 csp->config = config = load_config();
1424 if ( config->need_bind )
1427 * Since we were listening to the "old port", we will not see
1428 * a "listen" param change until the next IJB request. So, at
1429 * least 1 more request must be made for us to find the new
1430 * setting. I am simply closing the old socket and binding the
1433 * Which-ever is correct, we will serve 1 more page via the
1434 * old settings. This should probably be a "show-proxy-args"
1435 * request. This should not be a so common of an operation
1436 * that this will hurt people's feelings.
1441 bfd = bind_port_helper(config);
1444 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
1446 if (!accept_connection(csp, bfd))
1448 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
1461 log_error(LOG_LEVEL_CONNECT, "OK");
1464 #ifdef FEATURE_TOGGLE
1467 csp->flags |= CSP_FLAG_TOGGLED_ON;
1469 #endif /* def FEATURE_TOGGLE */
1471 #ifdef FEATURE_ACTIVITY_CONSOLE
1472 update_stats_config(config);
1473 #endif /* def FEATURE_ACTIVITY_CONSOLE */
1475 if (run_loader(csp))
1477 log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
1478 /* Never get here - LOG_LEVEL_FATAL causes program exit */
1482 if (block_acl(NULL,csp))
1484 #ifdef FEATURE_ACTIVITY_CONSOLE
1485 accumulate_stats(STATS_ACL_RESTRICT,1);
1486 #endif /* def FEATURE_ACTIVITY_CONSOLE */
1487 log_error(LOG_LEVEL_CONNECT, "Connection dropped due to ACL");
1488 close_socket(csp->cfd);
1492 #endif /* def FEATURE_ACL */
1494 /* add it to the list of clients */
1495 csp->next = clients->next;
1496 clients->next = csp;
1498 if (config->multi_threaded)
1502 /* this is a switch () statment in the C preprocessor - ugh */
1503 #undef SELECTED_ONE_OPTION
1505 /* Use Pthreads in preference to native code */
1506 #if defined(FEATURE_PTHREAD) && !defined(SELECTED_ONE_OPTION)
1507 #define SELECTED_ONE_OPTION
1509 pthread_t the_thread;
1510 pthread_attr_t attrs;
1512 pthread_attr_init(&attrs);
1513 pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
1514 child_id = (pthread_create(&the_thread, &attrs,
1515 (void*)serve, csp) ? -1 : 0);
1516 pthread_attr_destroy(&attrs);
1520 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
1521 #define SELECTED_ONE_OPTION
1522 child_id = _beginthread(
1523 (void (*)(void *))serve,
1528 #if defined(__OS2__) && !defined(SELECTED_ONE_OPTION)
1529 #define SELECTED_ONE_OPTION
1530 child_id = _beginthread(
1531 (void(* _Optlink)(void*))serve,
1537 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
1538 #define SELECTED_ONE_OPTION
1540 thread_id tid = spawn_thread
1541 (server_thread, "server", B_NORMAL_PRIORITY, csp);
1543 if ((tid >= 0) && (resume_thread(tid) == B_OK))
1545 child_id = (int) tid;
1554 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
1555 #define SELECTED_ONE_OPTION
1556 csp->cfd = ReleaseSocket(csp->cfd, -1);
1557 if((child_id = (int)CreateNewProcTags(
1558 NP_Entry, (ULONG)server_thread,
1559 NP_Output, Output(),
1560 NP_CloseOutput, FALSE,
1561 NP_Name, (ULONG)"privoxy child",
1562 NP_StackSize, 200*1024,
1566 ((struct Task *)child_id)->tc_UserData = csp;
1567 Signal((struct Task *)child_id, SIGF_SINGLE);
1572 #if !defined(SELECTED_ONE_OPTION)
1575 /* This block is only needed when using fork().
1576 * When using threads, the server thread was
1577 * created and run by the call to _beginthread().
1579 if (child_id == 0) /* child */
1585 else if (child_id > 0) /* parent */
1587 /* in a fork()'d environment, the parent's
1588 * copy of the client socket and the CSP
1592 #if !defined(_WIN32) && defined(__CYGWIN__)
1594 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
1595 close_socket(csp->cfd);
1596 csp->flags &= ~CSP_FLAG_ACTIVE;
1600 #undef SELECTED_ONE_OPTION
1601 /* end of cpp switch () */
1603 if (child_id < 0) /* failed */
1605 char buf[BUFFER_SIZE];
1607 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
1609 sprintf(buf , "Privoxy: can't fork: errno = %d", errno);
1611 write_socket(csp->cfd, buf, strlen(buf));
1612 close_socket(csp->cfd);
1613 csp->flags &= ~CSP_FLAG_ACTIVE;
1624 /* NOTREACHED unless FEATURE_GRACEFUL_TERMINATION is defined */
1626 /* Clean up. Aim: free all memory (no leaks) */
1627 #ifdef FEATURE_GRACEFUL_TERMINATION
1629 log_error(LOG_LEVEL_ERROR, "Graceful termination requested");
1631 unload_current_config_file();
1632 unload_current_actions_file();
1633 unload_current_re_filterfile();
1634 #ifdef FEATURE_TRUST
1635 unload_current_trust_file();
1638 if (config->multi_threaded)
1645 } while ((clients->next != NULL) && (--i > 0));
1649 log_error(LOG_LEVEL_ERROR, "Graceful termination failed - still some live clients after 1 minute wait.");
1658 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
1659 /* Cleanup - remove taskbar icon etc. */
1664 #endif /* FEATURE_GRACEFUL_TERMINATION */
1668 /*********************************************************************
1670 * Function : relay_server_traffic
1672 * Description : receive traffic from the server and relay it
1673 * to the client, with some processing
1676 * 1 : client_state structure
1679 * Returns : JB_ERR_OK if there may be more work
1680 * : other values for errors
1682 **********************************************************************/
1684 static jb_err relay_server_traffic( struct client_state *csp )
1686 #ifdef FEATURE_KILL_POPUPS
1687 int block_popups; /* bool, 1==will block popups */
1688 int block_popups_now = 0; /* bool, 1==currently blocking popups */
1689 #endif /* def FEATURE_KILL_POPUPS */
1691 char buf[BUFFER_SIZE];
1692 int len; /* for buffer sizes */
1693 int ms_iis5_hack = 0;
1698 /* Skeleton for HTTP response, if we should intercept the request */
1699 struct http_response *rsp;
1701 int pcrs_filter; /* bool, 1==will filter through pcrs */
1702 int gif_deanimate; /* bool, 1==will deanimate gifs */
1704 #ifdef FEATURE_KILL_POPUPS
1705 block_popups = ((csp->action->flags & ACTION_NO_POPUPS) != 0);
1706 #endif /* def FEATURE_KILL_POPUPS */
1708 pcrs_filter = (csp->rlist != NULL) && /* There are expressions to be used */
1709 (!list_is_empty(csp->action->multi[ACTION_MULTI_FILTER]));
1711 gif_deanimate = ((csp->action->flags & ACTION_DEANIMATE) != 0);
1715 len = read_socket (csp->sfd, buf, sizeof (buf) - 1);
1719 log_error (LOG_LEVEL_ERROR, "read from: %s failed: %E", csp->http->host);
1721 log_error (LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
1722 csp->ip_addr_str, csp->http->ocmd);
1724 rsp = error_response (csp, "connect-failed", errno);
1728 if (write_socket (csp->cfd, rsp->head, rsp->head_length)
1729 || write_socket (csp->cfd, rsp->body, rsp->content_length))
1731 log_error (LOG_LEVEL_ERROR, "write to: %s failed: %E",
1736 free_http_response (rsp);
1737 return JB_ERR_GENERIC;
1740 /* Add a trailing zero. This lets filter_popups
1741 * use string operations.
1745 #ifdef FEATURE_KILL_POPUPS
1746 /* Filter the popups on this read. */
1747 if (block_popups_now)
1749 filter_popups (buf, csp);
1751 #endif /* def FEATURE_KILL_POPUPS */
1754 /* Normally, this would indicate that we've read
1755 * as much as the server has sent us and we can
1756 * close the client connection. However, Microsoft
1757 * in its wisdom has released IIS/5 with a bug that
1758 * prevents it from sending the trailing \r\n in
1759 * a 302 redirect header (and possibly other headers).
1760 * To work around this if we've haven't parsed
1761 * a full header we'll append a trailing \r\n
1762 * and see if this now generates a valid one.
1764 * This hack shouldn't have any impacts. If we've
1765 * already transmitted the header or if this is a
1766 * SSL connection, then we won't bother with this
1767 * hack. So we only work on partially received
1768 * headers. If we append a \r\n and this still
1769 * doesn't generate a valid header, then we won't
1770 * transmit anything to the client.
1775 if (csp->all_headers_read || csp->http->ssl)
1778 * If we have been buffering up the document,
1779 * now is the time to apply content modification
1780 * and send the result to the client.
1782 if (csp->content_filter)
1785 * If the content filter fails, use the original
1786 * buffer and length.
1787 * (see p != NULL ? p : csp->iob->cur below)
1789 if (NULL == (p = (*csp->content_filter) (csp)))
1791 csp->content_length = csp->iob->eod - csp->iob->cur;
1794 hdr = sed (server_patterns, add_server_headers, csp);
1797 /* FIXME Should handle error properly */
1798 log_error (LOG_LEVEL_FATAL,
1799 "Out of memory parsing server header");
1803 if (write_socket (csp->cfd, hdr, strlen (hdr))
1804 || write_socket (csp->cfd, p != NULL ? p : csp->iob->cur,
1805 csp->content_length))
1807 log_error (LOG_LEVEL_ERROR,
1808 "write modified content to client failed: %E");
1809 return JB_ERR_GENERIC;
1818 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %d",
1819 csp->ip_addr_str, csp->http->ocmd, byte_count);
1820 return JB_ERR_GENERIC;
1824 * This is NOT the body, so
1825 * Let's pretend the server just sent us a blank line.
1827 len = sprintf (buf, "\r\n");
1830 * Now, let the normal header parsing algorithm below do its
1831 * job. If it fails, we'll exit instead of continuing.
1838 * If this is an SSL connection or we're in the body
1839 * of the server document, just write it to the client,
1840 * unless we need to buffer the body for later content-filtering
1843 if (csp->all_headers_read || csp->http->ssl)
1845 if (csp->content_filter)
1847 add_to_iob (csp, buf, len);
1850 * If the buffer limit will be reached on the next read,
1851 * switch to non-filtering mode, i.e. make & write the
1852 * header, flush the socket and get out of the way.
1854 if (((size_t) (csp->iob->eod - csp->iob->buf)) +
1855 (size_t) BUFFER_SIZE > csp->config->buffer_limit)
1859 log_error (LOG_LEVEL_ERROR,
1860 "Buffer size limit reached! Flushing and stepping back.");
1862 hdr = sed (server_patterns, add_server_headers, csp);
1865 /* FIXME Should handle error properly */
1866 log_error (LOG_LEVEL_FATAL,
1867 "Out of memory parsing server header");
1870 hdrlen = strlen (hdr);
1871 byte_count += hdrlen;
1873 if (write_socket (csp->cfd, hdr, hdrlen)
1874 || ((len = flush_socket (csp->cfd, csp)) < 0))
1876 log_error (LOG_LEVEL_CONNECT,
1877 "write header to client failed: %E");
1880 return JB_ERR_GENERIC;
1886 csp->content_filter = NULL;
1887 csp->all_headers_read = 1;
1893 if (write_socket (csp->cfd, buf, (size_t) len))
1895 log_error (LOG_LEVEL_ERROR, "write to client failed: %E");
1896 return JB_ERR_GENERIC;
1905 * we're still looking for the end of the
1906 * server's header ... (does that make header
1907 * parsing an "out of body experience" ?
1910 /* buffer up the data we just read */
1911 add_to_iob (csp, buf, len);
1913 /* get header lines from the iob */
1915 while ((p = get_header (csp)) != NULL)
1919 /* see following note */
1920 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %d",
1921 csp->ip_addr_str, csp->http->ocmd, byte_count);
1922 return JB_ERR_GENERIC;
1924 enlist (csp->headers, p);
1928 /* NOTE: there are no "empty" headers so
1929 * if the pointer `p' is not NULL we must
1930 * assume that we reached the end of the
1931 * buffer before we hit the end of the header.
1938 /* Well, we tried our MS IIS/5
1939 * hack and it didn't work.
1940 * The header is incomplete
1941 * and there isn't anything
1942 * we can do about it.
1944 log_error(LOG_LEVEL_CLF, "Incomplete Server Header: %s - - [%T] \"%s\" 200 %d",
1945 csp->ip_addr_str, csp->http->ocmd, byte_count);
1946 return JB_ERR_GENERIC;
1950 /* Since we have to wait for
1951 * more from the server before
1952 * we can parse the headers
1953 * we just continue here.
1959 /* we have now received the entire header.
1960 * filter it and send the result to the client
1963 hdr = sed (server_patterns, add_server_headers, csp);
1966 /* FIXME Should handle error properly */
1967 log_error (LOG_LEVEL_FATAL, "Out of memory parsing server header");
1972 #ifdef FEATURE_KILL_POPUPS
1973 /* Start blocking popups if appropriate. */
1975 if ((csp->content_type & CT_TEXT) && /* It's a text / * MIME-Type */
1976 !csp->http->ssl && /* We talk plaintext */
1977 block_popups) /* Policy allows */
1979 block_popups_now = 1;
1981 * Filter the part of the body that came in the same read
1982 * as the last headers:
1984 filter_popups (csp->iob->cur, csp);
1987 #endif /* def FEATURE_KILL_POPUPS */
1989 /* Buffer and pcrs filter this if appropriate. */
1991 if ((csp->content_type & CT_TEXT) && /* It's a text / * MIME-Type */
1992 !csp->http->ssl && /* We talk plaintext */
1993 pcrs_filter) /* Policy allows */
1995 csp->content_filter = pcrs_filter_response;
1998 /* Buffer and gif_deanimate this if appropriate. */
2000 if ((csp->content_type & CT_GIF) && /* It's a image/gif MIME-Type */
2001 !csp->http->ssl && /* We talk plaintext */
2002 gif_deanimate) /* Policy allows */
2004 csp->content_filter = gif_deanimate_response;
2007 * Only write if we're not buffering for content modification
2009 if (!csp->content_filter)
2011 /* write the server's (modified) header to
2012 * the client (along with anything else that
2013 * may be in the buffer)
2015 if (write_socket (csp->cfd, hdr, strlen (hdr))
2016 || ((len = flush_socket (csp->cfd, csp)) < 0))
2018 log_error (LOG_LEVEL_CONNECT,
2019 "write header to client failed: %E");
2021 /* the write failed, so don't bother
2022 * mentioning it to the client...
2023 * it probably can't hear us anyway.
2026 return JB_ERR_GENERIC;
2032 /* we're finished with the server's header */
2035 csp->all_headers_read = 1;
2037 /* If this was a MS IIS/5 hack then it means
2038 * the server has already closed the
2039 * connection. Nothing more to read. Time
2044 return JB_ERR_GENERIC ;
2048 } /* END relay_server_traffic() */
2051 /*********************************************************************
2053 * Function : read_client_headers
2055 * Description : read all the client headers, and fill in the
2059 * 1 : client_state structure
2060 * 2 : http_request structure
2063 * Returns : JB_ERR_OK - headers read and parsed ok
2064 * : other values - an error occurred.
2065 *********************************************************************/
2067 static jb_err read_client_headers( struct client_state *csp, struct http_request *http )
2072 char buf[BUFFER_SIZE];
2073 int len; /* for buffer sizes */
2076 * Read the client's request. Note that since we're not using select() we
2077 * could get blocked here if a client connected, then didn't say anything!
2079 * This will read all the client headers.
2085 len = read_socket(csp->cfd, buf, sizeof(buf));
2087 if (len <= 0) break; /* error! */
2089 add_to_iob(csp, buf, len);
2091 req = get_header(csp);
2095 break; /* no HTTP request! */
2100 continue; /* more to come! */
2103 /* When we get here we have read one header which is enough */
2104 /* to check whether the connection is allowed */
2112 if ( ( ( p = get_header(csp) ) != NULL) && ( *p == '\0' ) )
2114 len = read_socket(csp->cfd, buf, sizeof(buf));
2117 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
2118 return JB_ERR_GENERIC;
2120 add_to_iob(csp, buf, len);
2124 if (p == NULL) break;
2126 enlist(csp->headers, p);
2131 parse_http_request(req, http, csp);
2134 if (http->cmd == NULL)
2136 strcpy(buf, CHEADER);
2137 write_socket(csp->cfd, buf, strlen(buf));
2139 log_error(LOG_LEVEL_CLF, "%s - - [%T] \" \" 400 0", csp->ip_addr_str);
2141 return JB_ERR_GENERIC;
2147 } /* END read_client_headers */
2150 /*********************************************************************
2152 * Function : process_client_headers
2154 * Description : Process client headers
2155 * FIXME: Add description of what we do here!
2158 * 1 : client_state structure
2159 * 2 : http_request structure
2162 * Returns : JB_ERR_OK - headers processed ok
2163 * : other values - an error occurred.
2164 *********************************************************************/
2166 static jb_err process_client_headers( struct client_state *csp, struct http_request *http )
2170 #ifdef FEATURE_FORCE_LOAD
2171 /* If this request contains the FORCE_PREFIX,
2172 * better get rid of it now and set the force flag --oes
2173 * Changed to use the http structure rather than the req field --jaa
2176 if (strstr(http->url, FORCE_PREFIX))
2178 strclean(http->url, FORCE_PREFIX);
2179 log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", http->url);
2180 csp->flags |= CSP_FLAG_FORCED;
2183 #endif /* def FEATURE_FORCE_LOAD */
2186 /* decide how to route the HTTP request */
2188 if ((http->fwd = forward_url(http, csp)) == NULL)
2190 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
2191 /* Never get here - LOG_LEVEL_FATAL causes program exit */
2194 /* build the http request to send to the server
2195 * we have to do one of the following:
2197 * create = use the original HTTP request to create a new
2198 * HTTP request that has either the path component
2199 * without the http://domainspec (w/path) or the
2200 * full orininal URL (w/url)
2201 * Note that the path and/or the HTTP version may
2202 * have been altered by now.
2204 * connect = Open a socket to the host:port of the server
2205 * and short-circuit server and client socket.
2207 * pass = Pass the request unchanged if forwarding a CONNECT
2208 * request to a parent proxy. Note that we'll be sending
2209 * the CFAIL message ourselves if connecting to the parent
2210 * fails, but we won't send a CSUCCEED message if it works,
2211 * since that would result in a double message (ours and the
2212 * parent's). After sending the request to the parent, we simply
2215 * here's the matrix:
2218 * +--------+--------+
2220 * 0 | create | connect|
2222 * Forwarding +--------+--------+
2224 * 1 | create | pass |
2226 * +--------+--------+
2231 * Determine the actions for this URL
2233 #ifdef FEATURE_TOGGLE
2234 if (!(csp->flags & CSP_FLAG_TOGGLED_ON))
2236 /* Most compatible set of actions (i.e. none) */
2237 init_current_action(csp->action);
2240 #endif /* ndef FEATURE_TOGGLE */
2242 url_actions(http, csp);
2250 * Downgrade http version from 1.1 to 1.0 if +downgrade
2253 if ( (http->ssl == 0)
2254 && (!strcmpic(http->ver, "HTTP/1.1"))
2255 && (csp->action->flags & ACTION_DOWNGRADE))
2258 http->ver = strdup("HTTP/1.0");
2260 if (http->ver == NULL)
2262 log_error(LOG_LEVEL_FATAL, "Out of memory downgrading HTTP version");
2267 * Save a copy of the original request for logging
2269 http->ocmd = strdup(http->cmd);
2271 if (http->ocmd == NULL)
2273 log_error(LOG_LEVEL_FATAL, "Out of memory copying HTTP request line");
2277 * (Re)build the HTTP request for non-SSL requests.
2278 * If forwarding, use the whole URL, else, use only the path.
2284 http->cmd = strdup(http->gpc);
2285 string_append(&http->cmd, " ");
2287 if (http->fwd->forward_host)
2289 string_append(&http->cmd, http->url);
2293 string_append(&http->cmd, http->path);
2296 string_append(&http->cmd, " ");
2297 string_append(&http->cmd, http->ver);
2299 if (http->cmd == NULL)
2301 log_error(LOG_LEVEL_FATAL, "Out of memory rewiting SSL command");
2304 enlist_first(csp->headers, http->cmd);
2308 * If the user has not supplied any wafers, and the user has not
2309 * told us to suppress the vanilla wafer, then send the vanilla wafer.
2311 if (list_is_empty(csp->action->multi[ACTION_MULTI_WAFER])
2312 && ((csp->action->flags & ACTION_VANILLA_WAFER) != 0))
2314 enlist(csp->action->multi[ACTION_MULTI_WAFER], VANILLA_WAFER);
2318 } /* process_client_headers */
2320 /*********************************************************************
2322 * Function : intercept_page
2324 * Description : Check whether to intercept page
2327 * 1 : client_state structure
2328 * 2 : http_request structure
2331 * Returns : JB_ERR_OK - Do NOT intercept this page
2332 * : JB_ERR_INTERCEPT - intercept this page
2333 * : all other values - an error occurred.
2334 *********************************************************************/
2335 static jb_err intercept_page( struct client_state *csp, struct http_request *http )
2338 /* Skeleton for HTTP response, if we should intercept the request */
2339 struct http_response *rsp;
2343 * These next lines are a little ugly, but they simplifies the if statements
2344 * below. Basically if TOGGLE, then we want the if to test if the
2345 * CSP_FLAG_TOGGLED_ON flag ist set, else we don't. And if FEATURE_FORCE_LOAD,
2346 * then we want the if to test for CSP_FLAG_FORCED , else we don't
2348 #ifdef FEATURE_TOGGLE
2349 # define IS_TOGGLED_ON_AND (csp->flags & CSP_FLAG_TOGGLED_ON) &&
2350 #else /* ifndef FEATURE_TOGGLE */
2351 # define IS_TOGGLED_ON_AND
2352 #endif /* ndef FEATURE_TOGGLE */
2353 #ifdef FEATURE_FORCE_LOAD
2354 # define IS_NOT_FORCED_AND !(csp->flags & CSP_FLAG_FORCED) &&
2355 #else /* ifndef FEATURE_FORCE_LOAD */
2356 # define IS_NOT_FORCED_AND
2357 #endif /* def FEATURE_FORCE_LOAD */
2359 #define IS_ENABLED_AND IS_TOGGLED_ON_AND IS_NOT_FORCED_AND
2362 /* a CGI call was detected and answered */
2363 (NULL != (rsp = dispatch_cgi(csp)))
2365 /* or we are enabled and... */
2366 || (IS_ENABLED_AND (
2368 /* ..the request was blocked */
2369 ( NULL != (rsp = block_url(csp)))
2371 /* ..or untrusted */
2372 #ifdef FEATURE_TRUST
2373 || ( NULL != (rsp = trust_url(csp)))
2374 #endif /* def FEATURE_TRUST */
2376 /* ..or a fast redirect kicked in */
2377 #ifdef FEATURE_FAST_REDIRECTS
2378 || (((csp->action->flags & ACTION_FAST_REDIRECTS) != 0) &&
2379 (NULL != (rsp = redirect_url(csp))))
2380 #endif /* def FEATURE_FAST_REDIRECTS */
2384 /* Write the answer to the client */
2385 if (write_socket(csp->cfd, rsp->head, rsp->head_length)
2386 || write_socket(csp->cfd, rsp->body, rsp->content_length))
2388 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
2391 #ifdef FEATURE_STATISTICS
2392 /* Count as a rejected request */
2393 csp->flags |= CSP_FLAG_REJECTED;
2394 #endif /* def FEATURE_STATISTICS */
2396 /* Log (FIXME: All intercept reasons apprear as "crunch" with Status 200) */
2397 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
2398 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3", csp->ip_addr_str, http->ocmd);
2400 /* Clean up and return */
2401 free_http_response(rsp);
2402 return JB_ERR_INTERCEPT;
2405 /* When we get here the request doesn't need to be intercepted */
2409 /*********************************************************************
2411 * Function : open_forwarding_connection
2413 * Description : Check whether to intercept page
2416 * 1 : client_state structure
2417 * 2 : http_request structure
2419 * Returns : JB_ERR_OK - Connection opened ok
2420 * : all other values - an error occurred.
2421 *********************************************************************/
2422 static jb_err open_forwarding_connection(struct client_state *csp )
2424 struct http_response *rsp;
2426 #ifdef FEATURE_ACTIVITY_CONSOLE
2427 accumulate_stats(STATS_REQUEST, 1);
2428 #endif /* def FEATURE_ACTIVITY_CONSOLE */
2429 log_error(LOG_LEVEL_GPC, "%s%s", csp->http->hostport, csp->http->path);
2431 if (csp->http->fwd->forward_host)
2433 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
2434 csp->http->fwd->forward_host, csp->http->fwd->forward_port,
2435 csp->http->hostport);
2439 log_error(LOG_LEVEL_CONNECT, "to %s", csp->http->hostport);
2442 /* here we connect to the server, gateway, or the forwarder */
2444 csp->sfd = forwarded_connect(csp->http->fwd, csp->http, csp);
2446 if (csp->sfd == JB_INVALID_SOCKET)
2448 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
2449 csp->http->hostport);
2451 if (errno == EINVAL)
2453 rsp = error_response(csp, "no-such-domain", errno);
2455 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 404 0",
2456 csp->ip_addr_str, csp->http->ocmd);
2460 rsp = error_response(csp, "connect-failed", errno);
2462 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
2463 csp->ip_addr_str, csp->http->ocmd);
2467 /* Write the answer to the client */
2470 if (write_socket(csp->cfd, rsp->head, rsp->head_length)
2471 || write_socket(csp->cfd, rsp->body, rsp->content_length))
2473 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", csp->http->host);
2477 free_http_response(rsp);
2478 return JB_ERR_GENERIC;
2481 log_error(LOG_LEVEL_CONNECT, "OK");
2484 } /* END open_forwarding_connection() */
2487 /*********************************************************************
2489 * Function : send_client_headers_to_server
2491 * Description : send the client headers (possibly modified) to the server
2494 * 1 : client_state structure
2495 * 2 : http_request structure
2498 * Returns : JB_ERR_OK - headers sent ok
2499 * : other values - an error occurred.
2500 *********************************************************************/
2502 static jb_err send_client_headers_to_server( struct client_state *csp, struct http_request *http, char *hdr )
2505 struct http_response *rsp;
2507 if (http->fwd->forward_host || (http->ssl == 0))
2509 /* write the client's (modified) header to the server
2510 * (along with anything else that may be in the buffer)
2513 if (write_socket(csp->sfd, hdr, strlen(hdr))
2514 || (flush_socket(csp->sfd, csp) < 0))
2516 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
2519 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
2520 csp->ip_addr_str, http->ocmd);
2522 rsp = error_response(csp, "connect-failed", errno);
2526 if (write_socket(csp->cfd, rsp->head, rsp->head_length)
2527 || write_socket(csp->cfd, rsp->body, rsp->content_length))
2529 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
2533 free_http_response(rsp);
2534 return JB_ERR_GENERIC;
2540 * We're running an SSL tunnel and we're not forwarding,
2541 * so just send the "connect succeeded" message to the
2542 * client, flush the rest, and get out of the way.
2544 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 2\n",
2545 csp->ip_addr_str, http->ocmd);
2547 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1))
2555 } /* END send_client_headers_to_server */
2558 /*********************************************************************
2560 * Function : is_connect_request_allowed
2562 * Description : send the client headers (possibly modified) to the server
2565 * 1 : client_state structure
2569 * Returns : JB_ERR_OK - connect request allowed
2570 * : other values - connect request not allowed
2571 *********************************************************************/
2572 static jb_err is_connect_request_allowed( struct client_state *csp )
2575 * Check if a CONNECT request is allowable:
2576 * In the absence of a +limit-connect action, allow only port 443.
2577 * If there is an action, allow whatever matches the specificaton.
2582 if( ( !(csp->action->flags & ACTION_LIMIT_CONNECT) && csp->http->port != 443)
2583 || (csp->action->flags & ACTION_LIMIT_CONNECT
2584 && !match_portlist(csp->action->string[ACTION_STRING_LIMIT_CONNECT], csp->http->port)) )
2586 char buf[BUFFER_SIZE];
2587 strcpy(buf, CFORBIDDEN);
2588 write_socket(csp->cfd, buf, strlen(buf));
2590 log_error(LOG_LEVEL_CONNECT, "Denying suspicious CONNECT request from %s", csp->ip_addr_str);
2591 log_error(LOG_LEVEL_CLF, "%s - - [%T] \" \" 403 0", csp->ip_addr_str);
2593 return JB_ERR_GENERIC;
2598 } /* END is_connect_request_allowed */