1 const char jcc_rcs[] = "$Id: jcc.c,v 1.84 2002/03/24 13:25:43 swa 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 * Privoxy 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.84 2002/03/24 13:25:43 swa
37 * name change related issues
39 * Revision 1.83 2002/03/16 23:54:06 jongfoster
40 * Adding graceful termination feature, to help look for memory leaks.
41 * If you enable this (which, by design, has to be done by hand
42 * editing config.h) and then go to http://i.j.b/die, then the program
43 * will exit cleanly after the *next* request. It should free all the
44 * memory that was used.
46 * Revision 1.82 2002/03/13 00:27:05 jongfoster
49 * Revision 1.81 2002/03/12 01:42:50 oes
50 * Introduced modular filters
52 * Revision 1.80 2002/03/11 22:07:05 david__schmidt
53 * OS/2 port maintenance:
54 * - Fixed EMX build - it had decayed a little
55 * - Fixed inexplicable crash during FD_ZERO - must be due to a bad macro.
56 * substituted a memset for now.
58 * Revision 1.79 2002/03/09 20:03:52 jongfoster
59 * - Making various functions return int rather than size_t.
60 * (Undoing a recent change). Since size_t is unsigned on
61 * Windows, functions like read_socket that return -1 on
62 * error cannot return a size_t.
64 * THIS WAS A MAJOR BUG - it caused frequent, unpredictable
65 * crashes, and also frequently caused JB to jump to 100%
66 * CPU and stay there. (Because it thought it had just
67 * read ((unsigned)-1) == 4Gb of data...)
69 * - The signature of write_socket has changed, it now simply
70 * returns success=0/failure=nonzero.
72 * - Trying to get rid of a few warnings --with-debug on
73 * Windows, I've introduced a new type "jb_socket". This is
74 * used for the socket file descriptors. On Windows, this
75 * is SOCKET (a typedef for unsigned). Everywhere else, it's
76 * an int. The error value can't be -1 any more, so it's
77 * now JB_INVALID_SOCKET (which is -1 on UNIX, and in
78 * Windows it maps to the #define INVALID_SOCKET.)
80 * - The signature of bind_port has changed.
82 * Revision 1.78 2002/03/08 21:35:04 oes
83 * Added optional group supplement to --user option. Will now use default group of user if no group given
85 * Revision 1.77 2002/03/07 03:52:06 oes
86 * - Fixed compiler warnings etc
87 * - Improved handling of failed DNS lookups
89 * Revision 1.76 2002/03/06 22:54:35 jongfoster
90 * Automated function-comment nitpicking.
92 * Revision 1.75 2002/03/06 10:02:19 oes
93 * Fixed stupid bug when --user was not given
95 * Revision 1.74 2002/03/06 00:49:31 jongfoster
96 * Fixing warning on Windows
97 * Making #ifdefs that refer to the same variable consistently
98 * use #ifdef unix rather than mixing #ifdef unix & #ifndef OS2
100 * Revision 1.73 2002/03/05 23:57:30 hal9
101 * Stray character 's' on line 1618 was breaking build.
103 * Revision 1.72 2002/03/05 21:33:45 david__schmidt
104 * - Re-enable OS/2 building after new parms were added
105 * - Fix false out of memory report when resolving CGI templates when no IP
106 * address is available of failed attempt (a la no such domain)
108 * Revision 1.71 2002/03/05 18:13:56 oes
109 * Added --user option
111 * Revision 1.70 2002/03/05 04:52:42 oes
112 * Deleted non-errlog debugging code
114 * Revision 1.69 2002/03/04 23:50:00 jongfoster
115 * Splitting off bind_port() call into bind_port_helper(), with
118 * Revision 1.68 2002/03/04 20:17:32 oes
121 * Revision 1.67 2002/03/04 18:18:57 oes
122 * - Removed _DEBUG mode
123 * - Cleand up cmdline parsing
124 * - Introduced --no-daemon, --pidfile options
125 * - Cleaned up signal handling:
126 * - Terminate cleanly on INT, TERM and ABRT
127 * - Schedule logfile for re-opening on HUP
128 * - Ignore CHLD and PIPE
129 * - Leave the rest with their default handlers
130 * - Uniform handler registration
131 * - Added usage() function
132 * - Played styleguide police
134 * Revision 1.66 2002/03/03 15:06:55 oes
135 * Re-enabled automatic config reloading
137 * Revision 1.65 2002/03/03 14:49:11 oes
138 * Fixed CLF logging: Now uses client's original HTTP request
140 * Revision 1.64 2002/03/03 09:18:03 joergs
141 * Made jumbjuster work on AmigaOS again.
143 * Revision 1.63 2002/03/02 04:14:50 david__schmidt
144 * Clean up a little CRLF unpleasantness that suddenly appeared
146 * Revision 1.62 2002/02/20 23:17:23 jongfoster
147 * Detecting some out-of memory conditions and exiting with a log message.
149 * Revision 1.61 2002/01/17 21:01:52 jongfoster
150 * Moving all our URL and URL pattern parsing code to urlmatch.c.
152 * Revision 1.60 2001/12/30 14:07:32 steudten
153 * - Add signal handling (unix)
154 * - Add SIGHUP handler (unix)
155 * - Add creation of pidfile (unix)
156 * - Add action 'top' in rc file (RH)
157 * - Add entry 'SIGNALS' to manpage
158 * - Add exit message to logfile (unix)
160 * Revision 1.59 2001/12/13 14:07:18 oes
161 * Fixed Bug: 503 error page now sent OK
163 * Revision 1.58 2001/11/30 23:37:24 jongfoster
164 * Renaming the Win32 config file to config.txt - this is almost the
165 * same as the corresponding UNIX name "config"
167 * Revision 1.57 2001/11/16 00:47:43 jongfoster
168 * Changing the tty-disconnection code to use setsid().
170 * Revision 1.56 2001/11/13 20:20:54 jongfoster
171 * Tabs->spaces, fixing a bug with missing {} around an if()
173 * Revision 1.55 2001/11/13 20:14:53 jongfoster
174 * Patch for FreeBSD setpgrp() as suggested by Alexander Lazic
176 * Revision 1.54 2001/11/07 00:03:14 steudten
177 * Give reliable return value if an error
178 * occurs not just 0 with new daemon mode.
180 * Revision 1.53 2001/11/05 21:41:43 steudten
181 * Add changes to be a real daemon just for unix os.
182 * (change cwd to /, detach from controlling tty, set
183 * process group and session leader to the own process.
185 * Add some fatal-error log message for failed malloc().
186 * Add '-d' if compiled with 'configure --with-debug' to
187 * enable debug output.
189 * Revision 1.52 2001/10/26 20:11:20 jongfoster
190 * Fixing type mismatch
192 * Revision 1.51 2001/10/26 17:38:28 oes
195 * Revision 1.50 2001/10/25 03:40:48 david__schmidt
196 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
197 * threads to call select() simultaneously. So, it's time to do a real, live,
198 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
199 * (native). Both versions will work, but using __OS2__ offers multi-threading.
201 * Revision 1.49 2001/10/23 21:41:35 jongfoster
202 * Added call to initialize the (statically-allocated of course)
203 * "out of memory" CGI response.
205 * Revision 1.48 2001/10/10 19:56:46 jongfoster
206 * Moving some code that wasn't cookie-related out of an #ifdef
209 * Revision 1.47 2001/10/10 16:44:36 oes
210 * Added CONNECT destination port limitation check
212 * Revision 1.46 2001/10/08 15:17:41 oes
213 * Re-enabled SSL forwarding
215 * Revision 1.45 2001/10/07 15:42:11 oes
216 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
218 * Moved downgrading of the HTTP version from parse_http_request to
219 * chat(), since we can't decide if it is necessary before we have
220 * determined the actions for the URL. The HTTP command is now
221 * *always* re-built so the repairs need no longer be special-cased.
223 * filter_popups now gets a csp pointer so it can raise the new
224 * CSP_FLAG_MODIFIED flag.
228 * Added configurable size limit for the IOB. If the IOB grows so
229 * large that the next read would exceed the limit, the header
230 * is generated, and the header & unfiltered buffer are flushed
231 * to the client. Chat then continues in non-buffering,
232 * non-filtering body mode.
234 * Revision 1.44 2001/10/02 18:13:57 oes
237 * Revision 1.43 2001/10/02 15:32:13 oes
238 * Moved generation of hdr
240 * Revision 1.42 2001/09/21 23:02:02 david__schmidt
241 * Cleaning up 2 compiler warnings on OS/2.
243 * Revision 1.41 2001/09/16 17:05:14 jongfoster
244 * Removing unused #include showarg.h
246 * Revision 1.40 2001/09/16 15:41:45 jongfoster
247 * Fixing signed/unsigned comparison warning.
249 * Revision 1.39 2001/09/16 13:21:27 jongfoster
250 * Changes to use new list functions.
252 * Revision 1.38 2001/09/16 13:01:46 jongfoster
253 * Removing redundant function call that zeroed zalloc()'d memory.
255 * Revision 1.37 2001/09/10 11:12:24 oes
256 * Deleted unused variable
258 * Revision 1.36 2001/09/10 10:56:15 oes
259 * Silenced compiler warnings
261 * Revision 1.35 2001/07/31 14:44:22 oes
262 * Deleted unused size parameter from filter_popups()
264 * Revision 1.34 2001/07/30 22:08:36 jongfoster
265 * Tidying up #defines:
266 * - All feature #defines are now of the form FEATURE_xxx
267 * - Permanently turned off WIN_GUI_EDIT
268 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
270 * Revision 1.33 2001/07/29 19:32:00 jongfoster
271 * Renaming _main() [mingw32 only] to real_main(), for ANSI compliance.
273 * Revision 1.32 2001/07/29 18:47:05 jongfoster
274 * Adding missing #include "loadcfg.h"
276 * Revision 1.31 2001/07/29 12:17:48 oes
277 * Applied pthread fix by Paul Lieverse
279 * Revision 1.30 2001/07/25 22:57:13 jongfoster
280 * __BEOS__ no longer overrides FEATURE_PTHREAD.
281 * This is because FEATURE_PTHREAD will soon be widely used, so I
282 * want to keep it simple.
284 * Revision 1.29 2001/07/24 12:47:06 oes
285 * Applied BeOS support update by Eugenia
287 * Revision 1.28 2001/07/23 13:26:12 oes
288 * Fixed bug in popup-killing for the first read that caused binary garbage to be sent between headers and body
290 * Revision 1.27 2001/07/19 19:09:47 haroon
291 * - Added code to take care of the situation where while processing the first
292 * server response (which includes the server header), after finding the end
293 * of the headers we were not looking past the end of the headers for
294 * content modification. I enabled it for filter_popups.
295 * Someone else should look to see if other similar operations should be
296 * done to the discarded portion of the buffer.
298 * Note 2001/07/20: No, the other content modification mechanisms will process
299 * the whole iob later anyway. --oes
301 * Revision 1.26 2001/07/18 12:31:36 oes
304 * Revision 1.25 2001/07/15 19:43:49 jongfoster
305 * Supports POSIX threads.
306 * Also removed some unused #includes.
308 * Revision 1.24 2001/07/13 14:00:40 oes
309 * - Generic content modification scheme:
310 * Each feature has its own applicability flag that is set
311 * from csp->action->flags.
312 * Replaced the "filtering" int flag , by a function pointer
313 * "content_filter" to the function that will do the content
314 * modification. If it is != NULL, the document will be buffered
315 * and processed through *content_filter, which must set
316 * csp->content_length and return a modified copy of the body
317 * or return NULL (on failiure).
318 * - Changed csp->is_text to the more generic bitmap csp->content_type
319 * which can currently take the valued CT_TEXT or CT_GIF
321 * - Removed all #ifdef PCRS
323 * Revision 1.23 2001/07/02 02:28:25 iwanttokeepanon
324 * Added "#ifdef ACL_FILES" conditional compilation to line 1291 to exclude
325 * the `block_acl' call. This prevents a compilation error when the user
326 * does not wish to use the "ACL" feature.
328 * Revision 1.22 2001/06/29 21:45:41 oes
329 * Indentation, CRLF->LF, Tab-> Space
331 * Revision 1.21 2001/06/29 13:29:36 oes
332 * - Cleaned up, improved comments
333 * - Unified all possible interceptors (CGI,
334 * block, trust, fast_redirect) in one
335 * place, with one (CGI) answer generation
336 * mechansim. Much clearer now.
337 * - Removed the GIF image generation, which
338 * is now done in filters.c:block_url()
339 * - Made error conditions like domain lookup
340 * failiure or (various) problems while talking
341 * to the server use cgi.c:error_response()
342 * instead of generating HTML/HTTP in chat() (yuck!)
343 * - Removed logentry from cancelled commit
345 * Revision 1.20 2001/06/09 10:55:28 jongfoster
346 * Changing BUFSIZ ==> BUFFER_SIZE
348 * Revision 1.19 2001/06/07 23:12:52 jongfoster
349 * Replacing function pointer in struct gateway with a directly
350 * called function forwarded_connect().
351 * Replacing struct gateway with struct forward_spec
353 * Revision 1.18 2001/06/03 19:12:16 oes
354 * introduced new cgi handling
356 * Revision 1.17 2001/06/01 20:07:23 jongfoster
357 * Now uses action +image-blocker{} rather than config->tinygif
359 * Revision 1.16 2001/06/01 18:49:17 jongfoster
360 * Replaced "list_share" with "list" - the tiny memory gain was not
361 * worth the extra complexity.
363 * Revision 1.15 2001/05/31 21:24:47 jongfoster
364 * Changed "permission" to "action" throughout.
365 * Removed DEFAULT_USER_AGENT - it must now be specified manually.
366 * Moved vanilla wafer check into chat(), since we must now
367 * decide whether or not to add it based on the URL.
369 * Revision 1.14 2001/05/29 20:14:01 joergs
370 * AmigaOS bugfix: PCRS needs a lot of stack, stacksize for child threads
373 * Revision 1.13 2001/05/29 09:50:24 jongfoster
374 * Unified blocklist/imagelist/permissionslist.
375 * File format is still under discussion, but the internal changes
378 * Also modified interceptor behaviour:
379 * - We now intercept all URLs beginning with one of the following
380 * prefixes (and *only* these prefixes):
382 * * http://ijbswa.sf.net/config/
383 * * http://ijbswa.sourceforge.net/config/
384 * - New interceptors "home page" - go to http://i.j.b/ to see it.
385 * - Internal changes so that intercepted and fast redirect pages
386 * are not replaced with an image.
387 * - Interceptors now have the option to send a binary page direct
388 * to the client. (i.e. ijb-send-banner uses this)
389 * - Implemented show-url-info interceptor. (Which is why I needed
390 * the above interceptors changes - a typical URL is
391 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
392 * The previous mechanism would not have intercepted that, and
393 * if it had been intercepted then it then it would have replaced
396 * Revision 1.12 2001/05/27 22:17:04 oes
398 * - re_process_buffer no longer writes the modified buffer
399 * to the client, which was very ugly. It now returns the
400 * buffer, which it is then written by chat.
402 * - content_length now adjusts the Content-Length: header
403 * for modified documents rather than crunch()ing it.
404 * (Length info in csp->content_length, which is 0 for
405 * unmodified documents)
407 * - For this to work, sed() is called twice when filtering.
409 * Revision 1.11 2001/05/26 17:27:53 jongfoster
410 * Added support for CLF and fixed LOG_LEVEL_LOG.
411 * Also did CRLF->LF fix of my previous patch.
413 * Revision 1.10 2001/05/26 15:26:15 jongfoster
414 * ACL feature now provides more security by immediately dropping
415 * connections from untrusted hosts.
417 * Revision 1.9 2001/05/26 00:28:36 jongfoster
418 * Automatic reloading of config file.
419 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
420 * Most of the global variables have been moved to a new
421 * struct configuration_spec, accessed through csp->config->globalname
422 * Most of the globals remaining are used by the Win32 GUI.
424 * Revision 1.8 2001/05/25 22:43:18 jongfoster
425 * Fixing minor memory leak and buffer overflow.
427 * Revision 1.7 2001/05/25 22:34:30 jongfoster
430 * Revision 1.6 2001/05/23 00:13:58 joergs
431 * AmigaOS support fixed.
433 * Revision 1.5 2001/05/22 18:46:04 oes
435 * - Enabled filtering banners by size rather than URL
436 * by adding patterns that replace all standard banner
437 * sizes with the "Junkbuster" gif to the re_filterfile
439 * - Enabled filtering WebBugs by providing a pattern
440 * which kills all 1x1 images
442 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
443 * which is selected by the (nonstandard and therefore
444 * capital) letter 'U' in the option string.
445 * It causes the quantifiers to be ungreedy by default.
446 * Appending a ? turns back to greedy (!).
448 * - Added a new interceptor ijb-send-banner, which
449 * sends back the "Junkbuster" gif. Without imagelist or
450 * MSIE detection support, or if tinygif = 1, or the
451 * URL isn't recognized as an imageurl, a lame HTML
452 * explanation is sent instead.
454 * - Added new feature, which permits blocking remote
455 * script redirects and firing back a local redirect
457 * The feature is conditionally compiled, i.e. it
458 * can be disabled with --disable-fast-redirects,
459 * plus it must be activated by a "fast-redirects"
460 * line in the config file, has its own log level
461 * and of course wants to be displayed by show-proxy-args
462 * Note: Boy, all the #ifdefs in 1001 locations and
463 * all the fumbling with configure.in and acconfig.h
464 * were *way* more work than the feature itself :-(
466 * - Because a generic redirect template was needed for
467 * this, tinygif = 3 now uses the same.
469 * - Moved GIFs, and other static HTTP response templates
474 * - Removed some >400 CRs again (Jon, you really worked
477 * Revision 1.4 2001/05/21 19:34:01 jongfoster
478 * Made failure to bind() a fatal error.
480 * Revision 1.3 2001/05/20 01:21:20 jongfoster
481 * Version 2.9.4 checkin.
482 * - Merged popupfile and cookiefile, and added control over PCRS
483 * filtering, in new "permissionsfile".
484 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
485 * file error you now get a message box (in the Win32 GUI) rather
486 * than the program exiting with no explanation.
487 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
489 * - Removed tabs from "config"
490 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
491 * - Bumped up version number.
493 * Revision 1.2 2001/05/17 22:34:44 oes
494 * - Added hint on GIF char array generation to jcc.c
495 * - Cleaned CRLF's from the sources and related files
496 * - Repaired logging for REF and FRC
498 * Revision 1.1.1.1 2001/05/15 13:58:56 oes
499 * Initial import of version 2.9.3 source tree
502 *********************************************************************/
508 #include <sys/types.h>
515 #ifdef FEATURE_PTHREAD
517 #endif /* def FEATURE_PTHREAD */
520 # ifndef FEATURE_PTHREAD
521 # include <windows.h>
522 # include <process.h>
523 # endif /* ndef FEATURE_PTHREAD */
526 # ifndef _WIN_CONSOLE
528 # endif /* ndef _WIN_CONSOLE */
530 #else /* ifndef _WIN32 */
532 # if !defined (__OS2__)
534 # include <sys/wait.h>
535 # endif /* ndef __OS2__ */
536 # include <sys/time.h>
537 # include <sys/stat.h>
538 # include <sys/ioctl.h>
541 #include <sys/termios.h>
552 # include <socket.h> /* BeOS has select() for sockets only. */
553 # include <OS.h> /* declarations for threads and stuff. */
556 # if defined(__EMX__) || defined(__OS2__)
557 # include <sys/select.h> /* OS/2/EMX needs a little help with select */
562 #define bzero(B,N) memset(B,0x00,n)
577 #include "killpopup.h"
578 #include "miscutil.h"
580 #include "jbsockets.h"
585 #include "urlmatch.h"
587 const char jcc_h_rcs[] = JCC_H_VERSION;
588 const char project_h_rcs[] = PROJECT_H_VERSION;
591 struct client_state clients[1];
592 struct file_list files[1];
594 #ifdef FEATURE_STATISTICS
595 int urls_read = 0; /* total nr of urls read inc rejected */
596 int urls_rejected = 0; /* total nr of urls rejected */
597 #endif /* def FEATURE_STATISTICS */
599 #ifdef FEATURE_GRACEFUL_TERMINATION
603 static void listen_loop(void);
604 static void chat(struct client_state *csp);
606 void serve(struct client_state *csp);
607 #else /* ifndef AMIGA */
608 static void serve(struct client_state *csp);
609 #endif /* def AMIGA */
612 static int32 server_thread(void *data);
613 #endif /* def __BEOS__ */
616 #define sleep(N) Sleep(((N) * 1000))
620 #define sleep(N) DosSleep(((N) * 100))
623 #if defined(unix) || defined(__EMX__)
625 const char *pidfile = NULL;
626 int received_hup_signal = 0;
627 #endif /* defined unix */
629 /* The vanilla wafer. */
630 static const char VANILLA_WAFER[] =
631 "NOTICE=TO_WHOM_IT_MAY_CONCERN_"
632 "Do_not_send_me_any_copyrighted_information_other_than_the_"
633 "document_that_I_am_requesting_or_any_of_its_necessary_components._"
634 "In_particular_do_not_send_me_any_cookies_that_"
635 "are_subject_to_a_claim_of_copyright_by_anybody._"
636 "Take_notice_that_I_refuse_to_be_bound_by_any_license_condition_"
637 "(copyright_or_otherwise)_applying_to_any_cookie._";
640 #if !defined(_WIN32) && !defined(__OS2__) && !defined(AMIGA)
641 /*********************************************************************
643 * Function : sig_handler
645 * Description : Signal handler for different signals.
646 * Exit gracefully on ABRT, TERM and INT
647 * or set a flag that will cause the errlog
648 * to be reopened by the main thread on HUP.
651 * 1 : the_signal = the signal cause this function to call
655 *********************************************************************/
656 static void sig_handler(int the_signal)
663 log_error(LOG_LEVEL_INFO, "exiting by signal %d .. bye", the_signal);
671 received_hup_signal = 1;
676 * We shouldn't be here, unless we catch signals
677 * in main() that we can't handle here!
679 log_error(LOG_LEVEL_FATAL, "sig_handler: exiting on unexpected signal %d", the_signal);
687 /*********************************************************************
691 * Description : Once a connection to the client has been accepted,
692 * this function is called (via serve()) to handle the
693 * main business of the communication. When this
694 * function returns, the caller must close the client
698 * 1 : csp = Current client state (buffers, headers, etc...)
700 * Returns : On success, the number of bytes written are returned (zero
701 * indicates nothing was written). On error, -1 is returned,
702 * and errno is set appropriately. If count is zero and the
703 * file descriptor refers to a regular file, 0 will be
704 * returned without causing any other effect. For a special
705 * file, the results are not portable.
707 *********************************************************************/
708 static void chat(struct client_state *csp)
711 * This next lines are a little ugly, but they simplifies the if statements
712 * below. Basically if TOGGLE, then we want the if to test if the
713 * CSP_FLAG_TOGGLED_ON flag ist set, else we don't. And if FEATURE_FORCE_LOAD,
714 * then we want the if to test for CSP_FLAG_FORCED , else we don't
716 #ifdef FEATURE_TOGGLE
717 # define IS_TOGGLED_ON_AND (csp->flags & CSP_FLAG_TOGGLED_ON) &&
718 #else /* ifndef FEATURE_TOGGLE */
719 # define IS_TOGGLED_ON_AND
720 #endif /* ndef FEATURE_TOGGLE */
721 #ifdef FEATURE_FORCE_LOAD
722 # define IS_NOT_FORCED_AND !(csp->flags & CSP_FLAG_FORCED) &&
723 #else /* ifndef FEATURE_FORCE_LOAD */
724 # define IS_NOT_FORCED_AND
725 #endif /* def FEATURE_FORCE_LOAD */
727 #define IS_ENABLED_AND IS_TOGGLED_ON_AND IS_NOT_FORCED_AND
729 char buf[BUFFER_SIZE];
737 int ms_iis5_hack = 0;
739 const struct forward_spec * fwd;
740 struct http_request *http;
741 int len; /* for buffer sizes */
742 #ifdef FEATURE_KILL_POPUPS
743 int block_popups; /* bool, 1==will block popups */
744 int block_popups_now = 0; /* bool, 1==currently blocking popups */
745 #endif /* def FEATURE_KILL_POPUPS */
747 int pcrs_filter; /* bool, 1==will filter through pcrs */
748 int gif_deanimate; /* bool, 1==will deanimate gifs */
750 /* Function that does the content filtering for the current request */
751 char *(*content_filter)() = NULL;
753 /* Skeleton for HTTP response, if we should intercept the request */
754 struct http_response *rsp;
759 * Read the client's request. Note that since we're not using select() we
760 * could get blocked here if a client connected, then didn't say anything!
765 len = read_socket(csp->cfd, buf, sizeof(buf));
767 if (len <= 0) break; /* error! */
769 add_to_iob(csp, buf, len);
771 req = get_header(csp);
775 break; /* no HTTP request! */
780 continue; /* more to come! */
783 #ifdef FEATURE_FORCE_LOAD
784 /* If this request contains the FORCE_PREFIX,
785 * better get rid of it now and set the force flag --oes
788 if (strstr(req, FORCE_PREFIX))
790 strclean(req, FORCE_PREFIX);
791 log_error(LOG_LEVEL_FORCE, "Enforcing request \"%s\".\n", req);
792 csp->flags |= CSP_FLAG_FORCED;
795 #endif /* def FEATURE_FORCE_LOAD */
797 parse_http_request(req, http, csp);
802 if (http->cmd == NULL)
804 strcpy(buf, CHEADER);
805 write_socket(csp->cfd, buf, strlen(buf));
807 log_error(LOG_LEVEL_CLF, "%s - - [%T] \" \" 400 0", csp->ip_addr_str);
812 /* decide how to route the HTTP request */
814 if ((fwd = forward_url(http, csp)) == NULL)
816 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
817 /* Never get here - LOG_LEVEL_FATAL causes program exit */
820 /* build the http request to send to the server
821 * we have to do one of the following:
823 * create = use the original HTTP request to create a new
824 * HTTP request that has either the path component
825 * without the http://domainspec (w/path) or the
826 * full orininal URL (w/url)
827 * Note that the path and/or the HTTP version may
828 * have been altered by now.
830 * connect = Open a socket to the host:port of the server
831 * and short-circuit server and client socket.
833 * pass = Pass the request unchanged if forwarding a CONNECT
834 * request to a parent proxy. Note that we'll be sending
835 * the CFAIL message ourselves if connecting to the parent
836 * fails, but we won't send a CSUCCEED message if it works,
837 * since that would result in a double message (ours and the
838 * parent's). After sending the request to the parent, we simply
844 * +--------+--------+
846 * 0 | create | connect|
848 * Forwarding +--------+--------+
850 * 1 | create | pass |
852 * +--------+--------+
857 * Determine the actions for this URL
859 #ifdef FEATURE_TOGGLE
860 if (!(csp->flags & CSP_FLAG_TOGGLED_ON))
862 /* Most compatible set of actions (i.e. none) */
863 init_current_action(csp->action);
866 #endif /* ndef FEATURE_TOGGLE */
868 url_actions(http, csp);
873 * Check if a CONNECT request is allowable:
874 * In the absence of a +limit-connect action, allow only port 443.
875 * If there is an action, allow whatever matches the specificaton.
879 if( ( !(csp->action->flags & ACTION_LIMIT_CONNECT) && csp->http->port != 443)
880 || (csp->action->flags & ACTION_LIMIT_CONNECT
881 && !match_portlist(csp->action->string[ACTION_STRING_LIMIT_CONNECT], csp->http->port)) )
883 strcpy(buf, CFORBIDDEN);
884 write_socket(csp->cfd, buf, strlen(buf));
886 log_error(LOG_LEVEL_CONNECT, "Denying suspicious CONNECT request from %s", csp->ip_addr_str);
887 log_error(LOG_LEVEL_CLF, "%s - - [%T] \" \" 403 0", csp->ip_addr_str);
895 * Downgrade http version from 1.1 to 1.0 if +downgrade
898 if ( (http->ssl == 0)
899 && (!strcmpic(http->ver, "HTTP/1.1"))
900 && (csp->action->flags & ACTION_DOWNGRADE))
903 http->ver = strdup("HTTP/1.0");
905 if (http->ver == NULL)
907 log_error(LOG_LEVEL_FATAL, "Out of memory downgrading HTTP version");
912 * Save a copy of the original request for logging
914 http->ocmd = strdup(http->cmd);
916 if (http->ocmd == NULL)
918 log_error(LOG_LEVEL_FATAL, "Out of memory copying HTTP request line");
922 * (Re)build the HTTP request for non-SSL requests.
923 * If forwarding, use the whole URL, else, use only the path.
929 http->cmd = strdup(http->gpc);
930 string_append(&http->cmd, " ");
932 if (fwd->forward_host)
934 string_append(&http->cmd, http->url);
938 string_append(&http->cmd, http->path);
941 string_append(&http->cmd, " ");
942 string_append(&http->cmd, http->ver);
944 if (http->cmd == NULL)
946 log_error(LOG_LEVEL_FATAL, "Out of memory rewiting SSL command");
949 enlist(csp->headers, http->cmd);
952 #ifdef FEATURE_COOKIE_JAR
954 * If we're logging cookies in a cookie jar, and the user has not
955 * supplied any wafers, and the user has not told us to suppress the
956 * vanilla wafer, then send the vanilla wafer.
958 if ((csp->config->jarfile != NULL)
959 && list_is_empty(csp->action->multi[ACTION_MULTI_WAFER])
960 && ((csp->action->flags & ACTION_VANILLA_WAFER) != 0))
962 enlist(csp->action->multi[ACTION_MULTI_WAFER], VANILLA_WAFER);
964 #endif /* def FEATURE_COOKIE_JAR */
967 #ifdef FEATURE_KILL_POPUPS
968 block_popups = ((csp->action->flags & ACTION_NO_POPUPS) != 0);
969 #endif /* def FEATURE_KILL_POPUPS */
971 pcrs_filter = (csp->rlist != NULL) && /* There are expressions to be used */
972 (!list_is_empty(csp->action->multi[ACTION_MULTI_FILTER]));
974 gif_deanimate = ((csp->action->flags & ACTION_DEANIMATE) != 0);
976 /* grab the rest of the client's headers */
980 if ( ( ( p = get_header(csp) ) != NULL) && ( *p == '\0' ) )
982 len = read_socket(csp->cfd, buf, sizeof(buf));
985 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
988 add_to_iob(csp, buf, len);
992 if (p == NULL) break;
994 enlist(csp->headers, p);
998 * We have a request. Now, check to see if we need to
999 * intercept it, i.e. If ..
1003 /* a CGI call was detected and answered */
1004 (NULL != (rsp = dispatch_cgi(csp)))
1006 /* or we are enabled and... */
1007 || (IS_ENABLED_AND (
1009 /* ..the request was blocked */
1010 ( NULL != (rsp = block_url(csp)))
1012 /* ..or untrusted */
1013 #ifdef FEATURE_TRUST
1014 || ( NULL != (rsp = trust_url(csp)))
1015 #endif /* def FEATURE_TRUST */
1017 /* ..or a fast redirect kicked in */
1018 #ifdef FEATURE_FAST_REDIRECTS
1019 || (((csp->action->flags & ACTION_FAST_REDIRECTS) != 0) &&
1020 (NULL != (rsp = redirect_url(csp))))
1021 #endif /* def FEATURE_FAST_REDIRECTS */
1025 /* Write the answer to the client */
1026 if (write_socket(csp->cfd, rsp->head, rsp->head_length)
1027 || write_socket(csp->cfd, rsp->body, rsp->content_length))
1029 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
1032 #ifdef FEATURE_STATISTICS
1033 /* Count as a rejected request */
1034 csp->flags |= CSP_FLAG_REJECTED;
1035 #endif /* def FEATURE_STATISTICS */
1037 /* Log (FIXME: All intercept reasons apprear as "crunch" with Status 200) */
1038 log_error(LOG_LEVEL_GPC, "%s%s crunch!", http->hostport, http->path);
1039 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3", csp->ip_addr_str, http->ocmd);
1041 /* Clean up and return */
1042 free_http_response(rsp);
1046 log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
1048 if (fwd->forward_host)
1050 log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
1051 fwd->forward_host, fwd->forward_port, http->hostport);
1055 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
1058 /* here we connect to the server, gateway, or the forwarder */
1060 csp->sfd = forwarded_connect(fwd, http, csp);
1062 if (csp->sfd == JB_INVALID_SOCKET)
1064 log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
1067 if (errno == EINVAL)
1069 rsp = error_response(csp, "no-such-domain", errno);
1071 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 404 0",
1072 csp->ip_addr_str, http->ocmd);
1076 rsp = error_response(csp, "connect-failed", errno);
1078 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
1079 csp->ip_addr_str, http->ocmd);
1083 /* Write the answer to the client */
1086 if (write_socket(csp->cfd, rsp->head, rsp->head_length)
1087 || write_socket(csp->cfd, rsp->body, rsp->content_length))
1089 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
1093 free_http_response(rsp);
1097 log_error(LOG_LEVEL_CONNECT, "OK");
1099 hdr = sed(client_patterns, add_client_headers, csp);
1102 /* FIXME Should handle error properly */
1103 log_error(LOG_LEVEL_FATAL, "Out of memory parsing client header");
1106 list_remove_all(csp->headers);
1108 if (fwd->forward_host || (http->ssl == 0))
1110 /* write the client's (modified) header to the server
1111 * (along with anything else that may be in the buffer)
1114 if (write_socket(csp->sfd, hdr, strlen(hdr))
1115 || (flush_socket(csp->sfd, csp) < 0))
1117 log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E",
1120 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
1121 csp->ip_addr_str, http->ocmd);
1123 rsp = error_response(csp, "connect-failed", errno);
1127 if (write_socket(csp->cfd, rsp->head, rsp->head_length)
1128 || write_socket(csp->cfd, rsp->body, rsp->content_length))
1130 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
1134 free_http_response(rsp);
1142 * We're running an SSL tunnel and we're not forwarding,
1143 * so just send the "connect succeeded" message to the
1144 * client, flush the rest, and get out of the way.
1146 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 2\n",
1147 csp->ip_addr_str, http->ocmd);
1149 if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1))
1157 /* we're finished with the client's header */
1160 maxfd = ( csp->cfd > csp->sfd ) ? csp->cfd : csp->sfd;
1162 /* pass data between the client and server
1163 * until one or the other shuts down the connection.
1172 * FD_ZERO here seems to point to an errant macro which crashes.
1173 * So do this by hand for now...
1175 memset(&rfds,0x00,sizeof(fd_set));
1179 FD_SET(csp->cfd, &rfds);
1180 FD_SET(csp->sfd, &rfds);
1182 n = select((int)maxfd+1, &rfds, NULL, NULL, NULL);
1186 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
1190 /* this is the body of the browser's request
1191 * just read it and write it.
1194 if (FD_ISSET(csp->cfd, &rfds))
1196 len = read_socket(csp->cfd, buf, sizeof(buf));
1200 break; /* "game over, man" */
1203 if (write_socket(csp->sfd, buf, (size_t)len))
1205 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
1212 * The server wants to talk. It could be the header or the body.
1213 * If `hdr' is null, then it's the header otherwise it's the body.
1214 * FIXME: Does `hdr' really mean `host'? No.
1218 if (FD_ISSET(csp->sfd, &rfds))
1221 len = read_socket(csp->sfd, buf, sizeof(buf) - 1);
1225 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
1227 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 503 0",
1228 csp->ip_addr_str, http->ocmd);
1230 rsp = error_response(csp, "connect-failed", errno);
1234 if (write_socket(csp->cfd, rsp->head, rsp->head_length)
1235 || write_socket(csp->cfd, rsp->body, rsp->content_length))
1237 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
1241 free_http_response(rsp);
1245 /* Add a trailing zero. This lets filter_popups
1246 * use string operations.
1250 #ifdef FEATURE_KILL_POPUPS
1251 /* Filter the popups on this read. */
1252 if (block_popups_now)
1254 filter_popups(buf, csp);
1256 #endif /* def FEATURE_KILL_POPUPS */
1258 /* Normally, this would indicate that we've read
1259 * as much as the server has sent us and we can
1260 * close the client connection. However, Microsoft
1261 * in its wisdom has released IIS/5 with a bug that
1262 * prevents it from sending the trailing \r\n in
1263 * a 302 redirect header (and possibly other headers).
1264 * To work around this if we've haven't parsed
1265 * a full header we'll append a trailing \r\n
1266 * and see if this now generates a valid one.
1268 * This hack shouldn't have any impacts. If we've
1269 * already transmitted the header or if this is a
1270 * SSL connection, then we won't bother with this
1271 * hack. So we only work on partially received
1272 * headers. If we append a \r\n and this still
1273 * doesn't generate a valid header, then we won't
1274 * transmit anything to the client.
1279 if (server_body || http->ssl)
1282 * If we have been buffering up the document,
1283 * now is the time to apply content modification
1284 * and send the result to the client.
1289 * If the content filter fails, use the original
1290 * buffer and length.
1291 * (see p != NULL ? p : csp->iob->cur below)
1293 if (NULL == (p = (*content_filter)(csp)))
1295 csp->content_length = csp->iob->eod - csp->iob->cur;
1298 hdr = sed(server_patterns, add_server_headers, csp);
1301 /* FIXME Should handle error properly */
1302 log_error(LOG_LEVEL_FATAL, "Out of memory parsing server header");
1305 if (write_socket(csp->cfd, hdr, strlen(hdr))
1306 || write_socket(csp->cfd, p != NULL ? p : csp->iob->cur, csp->content_length))
1308 log_error(LOG_LEVEL_ERROR, "write modified content to client failed: %E");
1318 break; /* "game over, man" */
1322 * This is NOT the body, so
1323 * Let's pretend the server just sent us a blank line.
1325 len = sprintf(buf, "\r\n");
1328 * Now, let the normal header parsing algorithm below do its
1329 * job. If it fails, we'll exit instead of continuing.
1336 * If this is an SSL connection or we're in the body
1337 * of the server document, just write it to the client,
1338 * unless we need to buffer the body for later content-filtering
1341 if (server_body || http->ssl)
1345 add_to_iob(csp, buf, len);
1348 * If the buffer limit will be reached on the next read,
1349 * switch to non-filtering mode, i.e. make & write the
1350 * header, flush the socket and get out of the way.
1352 if (((size_t)(csp->iob->eod - csp->iob->buf)) + (size_t)BUFFER_SIZE > csp->config->buffer_limit)
1356 log_error(LOG_LEVEL_ERROR, "Buffer size limit reached! Flushing and stepping back.");
1358 hdr = sed(server_patterns, add_server_headers, csp);
1361 /* FIXME Should handle error properly */
1362 log_error(LOG_LEVEL_FATAL, "Out of memory parsing server header");
1365 hdrlen = strlen(hdr);
1366 byte_count += hdrlen;
1368 if (write_socket(csp->cfd, hdr, hdrlen)
1369 || ((len = flush_socket(csp->cfd, csp)) < 0))
1371 log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
1380 content_filter = NULL;
1387 if (write_socket(csp->cfd, buf, (size_t)len))
1389 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
1398 /* we're still looking for the end of the
1399 * server's header ... (does that make header
1400 * parsing an "out of body experience" ?
1403 /* buffer up the data we just read */
1404 add_to_iob(csp, buf, len);
1406 /* get header lines from the iob */
1408 while ((p = get_header(csp)) != NULL)
1412 /* see following note */
1415 enlist(csp->headers, p);
1419 /* NOTE: there are no "empty" headers so
1420 * if the pointer `p' is not NULL we must
1421 * assume that we reached the end of the
1422 * buffer before we hit the end of the header.
1429 /* Well, we tried our MS IIS/5
1430 * hack and it didn't work.
1431 * The header is incomplete
1432 * and there isn't anything
1433 * we can do about it.
1439 /* Since we have to wait for
1440 * more from the server before
1441 * we can parse the headers
1442 * we just continue here.
1448 /* we have now received the entire header.
1449 * filter it and send the result to the client
1452 hdr = sed(server_patterns, add_server_headers, csp);
1455 /* FIXME Should handle error properly */
1456 log_error(LOG_LEVEL_FATAL, "Out of memory parsing server header");
1459 #ifdef FEATURE_KILL_POPUPS
1460 /* Start blocking popups if appropriate. */
1462 if ((csp->content_type & CT_TEXT) && /* It's a text / * MIME-Type */
1463 !http->ssl && /* We talk plaintext */
1464 block_popups) /* Policy allows */
1466 block_popups_now = 1;
1468 * Filter the part of the body that came in the same read
1469 * as the last headers:
1471 filter_popups(csp->iob->cur, csp);
1474 #endif /* def FEATURE_KILL_POPUPS */
1476 /* Buffer and pcrs filter this if appropriate. */
1478 if ((csp->content_type & CT_TEXT) && /* It's a text / * MIME-Type */
1479 !http->ssl && /* We talk plaintext */
1480 pcrs_filter) /* Policy allows */
1482 content_filter = pcrs_filter_response;
1485 /* Buffer and gif_deanimate this if appropriate. */
1487 if ((csp->content_type & CT_GIF) && /* It's a image/gif MIME-Type */
1488 !http->ssl && /* We talk plaintext */
1489 gif_deanimate) /* Policy allows */
1491 content_filter = gif_deanimate_response;
1495 * Only write if we're not buffering for content modification
1497 if (!content_filter)
1499 /* write the server's (modified) header to
1500 * the client (along with anything else that
1501 * may be in the buffer)
1504 if (write_socket(csp->cfd, hdr, strlen(hdr))
1505 || ((len = flush_socket(csp->cfd, csp)) < 0))
1507 log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E");
1509 /* the write failed, so don't bother
1510 * mentioning it to the client...
1511 * it probably can't hear us anyway.
1520 /* we're finished with the server's header */
1525 /* If this was a MS IIS/5 hack then it means
1526 * the server has already closed the
1527 * connection. Nothing more to read. Time
1538 return; /* huh? we should never get here */
1541 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %d",
1542 csp->ip_addr_str, http->ocmd, byte_count);
1546 /*********************************************************************
1550 * Description : This is little more than chat. We only "serve" to
1551 * to close any socket that chat may have opened.
1554 * 1 : csp = Current client state (buffers, headers, etc...)
1558 *********************************************************************/
1560 void serve(struct client_state *csp)
1561 #else /* ifndef AMIGA */
1562 static void serve(struct client_state *csp)
1563 #endif /* def AMIGA */
1566 close_socket(csp->cfd);
1568 if (csp->sfd != JB_INVALID_SOCKET)
1570 close_socket(csp->sfd);
1573 csp->flags &= ~CSP_FLAG_ACTIVE;
1579 /*********************************************************************
1581 * Function : server_thread
1583 * Description : We only exist to call `serve' in a threaded environment.
1586 * 1 : data = Current client state (buffers, headers, etc...)
1588 * Returns : Always 0.
1590 *********************************************************************/
1591 static int32 server_thread(void *data)
1593 serve((struct client_state *) data);
1600 /*********************************************************************
1604 * Description : Print usage info & exit.
1606 * Parameters : Pointer to argv[0] for identifying ourselves
1610 *********************************************************************/
1611 void usage(const char *myname)
1613 printf("Privoxy version " VERSION " (" HOME_PAGE_URL ")\n"
1614 "Usage: %s [--help] [--version] [--no-daemon] [--pidfile pidfile] [--user user[.group]] [configfile]\n"
1615 "Aborting.\n", myname);
1622 /*********************************************************************
1626 * Description : Load the config file and start the listen loop.
1627 * This function is a lot more *sane* with the `load_config'
1628 * and `listen_loop' functions; although it stills does
1629 * a *little* too much for my taste.
1632 * 1 : argc = Number of parameters (including $0).
1633 * 2 : argv = Array of (char *)'s to the parameters.
1635 * Returns : 1 if : can't open config file, unrecognized directive,
1636 * stats requested in multi-thread mode, can't open the
1637 * log file, can't open the jar file, listen port is invalid,
1638 * any load fails, and can't bind port.
1640 * Else main never returns, the process must be signaled
1641 * to terminate execution. Or, on Windows, use the
1642 * "File", "Exit" menu option.
1644 *********************************************************************/
1646 int real_main(int argc, const char *argv[])
1648 int main(int argc, const char *argv[])
1653 struct passwd *pw = NULL;
1654 struct group *grp = NULL;
1662 #if !defined(_WIN32)
1670 * Parse the command line arguments
1672 while (++argc_pos < argc)
1674 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
1676 if (strcmp(argv[argc_pos], "--help") == 0)
1681 else if(strcmp(argv[argc_pos], "--version") == 0)
1683 printf("Privoxy version " VERSION " (" HOME_PAGE_URL ")\n");
1687 else if (strcmp(argv[argc_pos], "--no-daemon" ) == 0)
1692 else if (strcmp(argv[argc_pos], "--pidfile" ) == 0)
1694 if (++argc_pos == argc) usage(argv[0]);
1695 pidfile = strdup(argv[argc_pos]);
1698 else if (strcmp(argv[argc_pos], "--user" ) == 0)
1700 if (++argc_pos == argc) usage(argv[argc_pos]);
1702 if ((NULL != (p = strchr(argv[argc_pos], '.'))) && *(p + 1) != '0')
1705 if (NULL == (grp = getgrnam(p)))
1707 log_error(LOG_LEVEL_FATAL, "Group %s not found.", p);
1711 if (NULL == (pw = getpwnam(argv[argc_pos])))
1713 log_error(LOG_LEVEL_FATAL, "User %s not found.", argv[argc_pos]);
1716 if (p != NULL) *--p = '\0';
1718 #endif /* defined(unix) */
1720 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
1722 configfile = argv[argc_pos];
1725 } /* -END- while (more arguments) */
1728 if ( *configfile != '/' )
1732 /* make config-filename absolute here */
1733 if ( !(basedir = getcwd( NULL, 1024 )))
1735 perror("get working dir failed");
1739 if ( !(abs_file = malloc( strlen( basedir ) + strlen( configfile ) + 5 )))
1741 perror("malloc failed");
1744 strcpy( abs_file, basedir );
1745 strcat( abs_file, "/" );
1746 strcat( abs_file, configfile );
1747 configfile = abs_file;
1749 #endif /* defined unix */
1756 #elif defined(_WIN32)
1761 * Unix signal handling
1763 * Catch the abort, interrupt and terminate signals for a graceful exit
1764 * Catch the hangup signal so the errlog can be reopened.
1765 * Ignore the broken pipe and child signals
1766 * FIXME: Isn't ignoring the default for SIGCHLD anyway and why ignore SIGPIPE?
1768 #if !defined(_WIN32) && !defined(__OS2__) && !defined(AMIGA)
1771 const int catched_signals[] = { SIGABRT, SIGTERM, SIGINT, SIGHUP, 0 };
1772 const int ignored_signals[] = { SIGPIPE, SIGCHLD, 0 };
1774 for (idx = 0; catched_signals[idx] != 0; idx++)
1776 if (signal(catched_signals[idx], sig_handler) == SIG_ERR)
1778 log_error(LOG_LEVEL_FATAL, "Can't set signal-handler for signal %d: %E", catched_signals[idx]);
1782 for (idx = 0; ignored_signals[idx] != 0; idx++)
1784 if (signal(ignored_signals[idx], SIG_IGN) == SIG_ERR)
1786 log_error(LOG_LEVEL_FATAL, "Can't set ignore-handler for signal %d: %E", ignored_signals[idx]);
1791 #else /* ifdef _WIN32 */
1792 # ifdef _WIN_CONSOLE
1794 * We *are* in a windows console app.
1795 * Print a verbose messages about FAQ's and such
1797 printf(win32_blurb);
1798 # endif /* def _WIN_CONSOLE */
1799 #endif /* def _WIN32 */
1802 /* Initialize the CGI subsystem */
1803 cgi_init_error_messages();
1806 * If runnig on unix and without the --nodaemon
1807 * option, become a daemon. I.e. fork, detach
1808 * from tty and get process group leadership
1821 if ( pid < 0 ) /* error */
1826 else if ( pid != 0 ) /* parent */
1831 * must check for errors
1832 * child died due to missing files aso
1835 wpid = waitpid( pid, &status, WNOHANG );
1844 /* Should be more portable, but not as well tested */
1849 #else /* ndef __FreeBSD__ */
1851 #endif /* ndef __FreeBSD__ */
1852 fd = open("/dev/tty", O_RDONLY);
1855 /* no error check here */
1856 ioctl( fd, TIOCNOTTY,0 );
1860 /* FIXME: should close stderr (fd 2) here too, but the test
1862 * and load config file is done in listen_loop() and puts
1863 * some messages on stderr there.
1870 } /* -END- if (!no_daemon) */
1873 * As soon as we have written the PID file, we can switch
1874 * to the user and group ID indicated by the --user option
1880 if (((NULL != grp) && setgid(grp->gr_gid)) || (setgid(pw->pw_gid)))
1882 log_error(LOG_LEVEL_FATAL, "Cannot setgid(): Insufficient permissions.");
1884 if (setuid(pw->pw_uid))
1886 log_error(LOG_LEVEL_FATAL, "Cannot setuid(): Insufficient permissions.");
1890 #endif /* defined unix */
1900 /*********************************************************************
1902 * Function : bind_port_helper
1904 * Description : Bind the listen port. Handles logging, and aborts
1908 * 1 : config = Privoxy configuration. Specifies port
1911 * Returns : Port that was opened.
1913 *********************************************************************/
1914 static jb_socket bind_port_helper(struct configuration_spec * config)
1919 if ( (config->haddr != NULL)
1920 && (config->haddr[0] == '1')
1921 && (config->haddr[1] == '2')
1922 && (config->haddr[2] == '7')
1923 && (config->haddr[3] == '.') )
1925 log_error(LOG_LEVEL_INFO, "Listening on port %d for local connections only",
1928 else if (config->haddr == NULL)
1930 log_error(LOG_LEVEL_INFO, "Listening on port %d on all IP addresses",
1935 log_error(LOG_LEVEL_INFO, "Listening on port %d on IP address %s",
1936 config->hport, config->haddr);
1939 result = bind_port(config->haddr, config->hport, &bfd);
1946 log_error(LOG_LEVEL_FATAL, "can't bind to %s:%d: "
1947 "There may be another Privoxy or some other "
1948 "proxy running on port %d",
1949 (NULL != config->haddr) ? config->haddr : "INADDR_ANY",
1950 config->hport, config->hport);
1953 log_error(LOG_LEVEL_FATAL, "can't bind to %s:%d: "
1954 "The hostname is not resolvable",
1955 (NULL != config->haddr) ? config->haddr : "INADDR_ANY", config->hport);
1958 log_error(LOG_LEVEL_FATAL, "can't bind to %s:%d: because %E",
1959 (NULL != config->haddr) ? config->haddr : "INADDR_ANY", config->hport);
1962 /* shouldn't get here */
1963 return JB_INVALID_SOCKET;
1966 config->need_bind = 0;
1972 /*********************************************************************
1974 * Function : listen_loop
1976 * Description : bind the listen port and enter a "FOREVER" listening loop.
1982 *********************************************************************/
1983 static void listen_loop(void)
1985 struct client_state *csp = NULL;
1987 struct configuration_spec * config;
1989 config = load_config();
1991 bfd = bind_port_helper(config);
1993 #ifdef FEATURE_GRACEFUL_TERMINATION
1994 while (!g_terminate)
1999 #if !defined(FEATURE_PTHREAD) && !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__)
2000 while (waitpid(-1, NULL, WNOHANG) > 0)
2002 /* zombie children */
2004 #endif /* !defined(FEATURE_PTHREAD) && !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) */
2007 * Free data that was used by died threads
2013 * Re-open the errlog after HUP signal
2015 if (received_hup_signal)
2017 init_error_log(Argv[0], config->logfile, config->debug);
2018 received_hup_signal = 0;
2022 if ( NULL == (csp = (struct client_state *) zalloc(sizeof(*csp))) )
2024 log_error(LOG_LEVEL_FATAL, "malloc(%d) for csp failed: %E", sizeof(*csp));
2028 csp->flags |= CSP_FLAG_ACTIVE;
2029 csp->sfd = JB_INVALID_SOCKET;
2031 csp->config = config = load_config();
2033 if ( config->need_bind )
2036 * Since we were listening to the "old port", we will not see
2037 * a "listen" param change until the next IJB request. So, at
2038 * least 1 more request must be made for us to find the new
2039 * setting. I am simply closing the old socket and binding the
2042 * Which-ever is correct, we will serve 1 more page via the
2043 * old settings. This should probably be a "show-proxy-args"
2044 * request. This should not be a so common of an operation
2045 * that this will hurt people's feelings.
2050 bfd = bind_port_helper(config);
2053 log_error(LOG_LEVEL_CONNECT, "accept connection ... ");
2055 if (!accept_connection(csp, bfd))
2057 log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
2070 log_error(LOG_LEVEL_CONNECT, "OK");
2073 #ifdef FEATURE_TOGGLE
2076 csp->flags |= CSP_FLAG_TOGGLED_ON;
2078 #endif /* def FEATURE_TOGGLE */
2080 if (run_loader(csp))
2082 log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
2083 /* Never get here - LOG_LEVEL_FATAL causes program exit */
2087 if (block_acl(NULL,csp))
2089 log_error(LOG_LEVEL_CONNECT, "Connection dropped due to ACL");
2090 close_socket(csp->cfd);
2094 #endif /* def FEATURE_ACL */
2096 /* add it to the list of clients */
2097 csp->next = clients->next;
2098 clients->next = csp;
2100 if (config->multi_threaded)
2104 /* this is a switch () statment in the C preprocessor - ugh */
2105 #undef SELECTED_ONE_OPTION
2107 /* Use Pthreads in preference to native code */
2108 #if defined(FEATURE_PTHREAD) && !defined(SELECTED_ONE_OPTION)
2109 #define SELECTED_ONE_OPTION
2111 pthread_t the_thread;
2112 pthread_attr_t attrs;
2114 pthread_attr_init(&attrs);
2115 pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
2116 child_id = (pthread_create(&the_thread, &attrs,
2117 (void*)serve, csp) ? -1 : 0);
2118 pthread_attr_destroy(&attrs);
2122 #if defined(_WIN32) && !defined(_CYGWIN) && !defined(SELECTED_ONE_OPTION)
2123 #define SELECTED_ONE_OPTION
2124 child_id = _beginthread(
2125 (void (*)(void *))serve,
2130 #if defined(__OS2__) && !defined(SELECTED_ONE_OPTION)
2131 #define SELECTED_ONE_OPTION
2132 child_id = _beginthread(
2139 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
2140 #define SELECTED_ONE_OPTION
2142 thread_id tid = spawn_thread
2143 (server_thread, "server", B_NORMAL_PRIORITY, csp);
2145 if ((tid >= 0) && (resume_thread(tid) == B_OK))
2147 child_id = (int) tid;
2156 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
2157 #define SELECTED_ONE_OPTION
2158 csp->cfd = ReleaseSocket(csp->cfd, -1);
2159 if((child_id = (int)CreateNewProcTags(
2160 NP_Entry, (ULONG)server_thread,
2161 NP_Output, Output(),
2162 NP_CloseOutput, FALSE,
2163 NP_Name, (ULONG)"privoxy child",
2164 NP_StackSize, 200*1024,
2168 ((struct Task *)child_id)->tc_UserData = csp;
2169 Signal((struct Task *)child_id, SIGF_SINGLE);
2174 #if !defined(SELECTED_ONE_OPTION)
2177 /* This block is only needed when using fork().
2178 * When using threads, the server thread was
2179 * created and run by the call to _beginthread().
2181 if (child_id == 0) /* child */
2187 else if (child_id > 0) /* parent */
2189 /* in a fork()'d environment, the parent's
2190 * copy of the client socket and the CSP
2194 #if !defined(_WIN32) && defined(__CYGWIN__)
2196 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
2197 close_socket(csp->cfd);
2198 csp->flags &= ~CSP_FLAG_ACTIVE;
2202 #undef SELECTED_ONE_OPTION
2203 /* end of cpp switch () */
2205 if (child_id < 0) /* failed */
2207 char buf[BUFFER_SIZE];
2209 log_error(LOG_LEVEL_ERROR, "can't fork: %E");
2211 sprintf(buf , "JunkBuster: can't fork: errno = %d", errno);
2213 write_socket(csp->cfd, buf, strlen(buf));
2214 close_socket(csp->cfd);
2215 csp->flags &= ~CSP_FLAG_ACTIVE;
2226 /* NOTREACHED unless FEATURE_GRACEFUL_TERMINATION is defined */
2228 /* Clean up. Aim: free all memory (no leaks) */
2229 #ifdef FEATURE_GRACEFUL_TERMINATION
2231 log_error(LOG_LEVEL_ERROR, "Graceful termination requested");
2233 unload_current_config_file();
2234 unload_current_actions_file();
2235 unload_current_re_filterfile();
2236 #ifdef FEATURE_TRUST
2237 unload_current_trust_file();
2240 if (config->multi_threaded)
2247 } while ((clients->next != NULL) && (--i > 0));
2251 log_error(LOG_LEVEL_ERROR, "Graceful termination failed - still some live clients after 1 minute wait.");
2260 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
2261 /* Cleanup - remove taskbar icon etc. */
2266 #endif /* FEATURE_GRACEFUL_TERMINATION */