1 const char errlog_rcs[] = "$Id: errlog.c,v 1.18 2001/09/10 11:27:24 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/errlog.c,v $
6 * Purpose : Log errors to a designated destination in an elegant,
9 * Copyright : Written by and Copyright (C) 2001 the SourceForge
10 * IJBSWA team. http://ijbswa.sourceforge.net
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will
23 * be useful, but WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A
25 * PARTICULAR PURPOSE. See the GNU General Public
26 * License for more details.
28 * The GNU General Public License should be included with
29 * this file. If not, you can view it at
30 * http://www.gnu.org/copyleft/gpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 * Revision 1.18 2001/09/10 11:27:24 oes
37 * Declaration of w32_socket_strerr now conditional
39 * Revision 1.17 2001/09/10 10:17:13 oes
40 * Removed unused variable; Fixed sprintf format
42 * Revision 1.16 2001/07/30 22:08:36 jongfoster
43 * Tidying up #defines:
44 * - All feature #defines are now of the form FEATURE_xxx
45 * - Permanently turned off WIN_GUI_EDIT
46 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
48 * Revision 1.15 2001/07/29 17:41:10 jongfoster
49 * Now prints thread ID for each message (pthreads only)
51 * Revision 1.14 2001/07/19 19:03:48 haroon
52 * - Added case for LOG_LEVEL_POPUPS
54 * Revision 1.13 2001/07/13 13:58:58 oes
55 * - Added case for LOG_LEVEL_DEANIMATE
56 * - Removed all #ifdef PCRS
58 * Revision 1.12 2001/06/09 10:55:28 jongfoster
59 * Changing BUFSIZ ==> BUFFER_SIZE
61 * Revision 1.11 2001/06/01 18:14:49 jongfoster
62 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
63 * in config.h if appropriate) rather than the NO_STRERR macro.
65 * Revision 1.10 2001/05/29 11:52:21 oes
66 * Conditional compilation of w32_socket_error
68 * Revision 1.9 2001/05/28 16:15:17 jongfoster
69 * Improved reporting of errors under Win32.
71 * Revision 1.8 2001/05/26 17:25:14 jongfoster
72 * Added support for CLF (Common Log Format) and fixed LOG_LEVEL_LOG
74 * Revision 1.7 2001/05/26 15:21:28 jongfoster
75 * Activity animation in Win32 GUI now works even if debug==0
77 * Revision 1.6 2001/05/25 21:55:08 jongfoster
78 * Now cleans up properly on FATAL (removes taskbar icon etc)
80 * Revision 1.5 2001/05/22 18:46:04 oes
82 * - Enabled filtering banners by size rather than URL
83 * by adding patterns that replace all standard banner
84 * sizes with the "Junkbuster" gif to the re_filterfile
86 * - Enabled filtering WebBugs by providing a pattern
87 * which kills all 1x1 images
89 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
90 * which is selected by the (nonstandard and therefore
91 * capital) letter 'U' in the option string.
92 * It causes the quantifiers to be ungreedy by default.
93 * Appending a ? turns back to greedy (!).
95 * - Added a new interceptor ijb-send-banner, which
96 * sends back the "Junkbuster" gif. Without imagelist or
97 * MSIE detection support, or if tinygif = 1, or the
98 * URL isn't recognized as an imageurl, a lame HTML
99 * explanation is sent instead.
101 * - Added new feature, which permits blocking remote
102 * script redirects and firing back a local redirect
104 * The feature is conditionally compiled, i.e. it
105 * can be disabled with --disable-fast-redirects,
106 * plus it must be activated by a "fast-redirects"
107 * line in the config file, has its own log level
108 * and of course wants to be displayed by show-proxy-args
109 * Note: Boy, all the #ifdefs in 1001 locations and
110 * all the fumbling with configure.in and acconfig.h
111 * were *way* more work than the feature itself :-(
113 * - Because a generic redirect template was needed for
114 * this, tinygif = 3 now uses the same.
116 * - Moved GIFs, and other static HTTP response templates
121 * - Removed some >400 CRs again (Jon, you really worked
124 * Revision 1.4 2001/05/21 19:32:54 jongfoster
125 * Added another #ifdef _WIN_CONSOLE
127 * Revision 1.3 2001/05/20 01:11:40 jongfoster
128 * Added support for LOG_LEVEL_FATAL
129 * Renamed LOG_LEVEL_FRC to LOG_LEVEL_FORCE,
130 * and LOG_LEVEL_REF to LOG_LEVEL_RE_FILTER
132 * Revision 1.2 2001/05/17 22:42:01 oes
133 * - Cleaned CRLF's from the sources and related files
134 * - Repaired logging for REF and FRC
136 * Revision 1.1.1.1 2001/05/15 13:58:51 oes
137 * Initial import of version 2.9.3 source tree
140 *********************************************************************/
152 #endif /* ndef _WIN32 */
155 #ifdef FEATURE_PTHREAD
157 #endif /* def FEATURE_PTHREAD */
163 #endif /* ndef _WIN_CONSOLE */
164 #endif /* def _WIN32 */
169 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
173 * LOG_LEVEL_FATAL cannot be turned off. (There are
174 * some exceptional situations where we need to get a
175 * message to the user).
177 #define LOG_LEVEL_MINIMUM LOG_LEVEL_FATAL
179 /* where to log (default: stderr) */
180 static FILE *logfp = NULL;
182 /* logging detail level. */
183 static int debug = (LOG_LEVEL_FATAL | LOG_LEVEL_ERROR | LOG_LEVEL_INFO);
185 /* static functions */
186 static void fatal_error(const char * error_message);
188 static char *w32_socket_strerr(int errcode, char *tmp_buf);
191 /*********************************************************************
193 * Function : fatal_error
195 * Description : Displays a fatal error to standard error (or, on
196 * a WIN32 GUI, to a dialog box), and exits
197 * JunkBuster with status code 1.
200 * 1 : error_message = The error message to display.
202 * Returns : Does not return.
204 *********************************************************************/
205 static void fatal_error(const char * error_message)
207 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
208 MessageBox(g_hwndLogFrame, error_message, "Internet JunkBuster Error",
209 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
211 /* Cleanup - remove taskbar icon etc. */
214 #else /* if !defined(_WIN32) || defined(_WIN_CONSOLE) */
215 fputs(error_message, stderr);
216 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
222 /*********************************************************************
224 * Function : init_errlog
226 * Description : Initializes the logging module. Must call before
230 * 1 : prog_name = The program name.
231 * 2 : logfname = The logfile name, or NULL for stderr.
232 * 3 : debuglevel = The debugging level.
236 *********************************************************************/
237 void init_error_log(const char *prog_name, const char *logfname, int debuglevel)
241 /* FIXME RACE HAZARD: should start critical section error_log_use here */
243 /* set the logging detail level */
244 debug = debuglevel | LOG_LEVEL_MINIMUM;
246 if ((logfp != NULL) && (logfp != stderr))
252 /* set the designated log file */
255 if( !(fp = fopen(logfname, "a")) )
257 log_error(LOG_LEVEL_FATAL, "init_errlog(): can't open logfile: %s", logfname);
260 /* set logging to be completely unbuffered */
266 log_error(LOG_LEVEL_INFO, "Internet JunkBuster version " VERSION);
267 if (prog_name != NULL)
269 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
272 /* FIXME RACE HAZARD: should end critical section error_log_use here */
274 } /* init_error_log */
277 /*********************************************************************
279 * Function : log_error
281 * Description : This is the error-reporting and logging function.
284 * 1 : loglevel = the type of message to be logged
285 * 2 : fmt = the main string we want logged, printf-like
286 * 3 : ... = arguments to be inserted in fmt (printf-like).
290 *********************************************************************/
291 void log_error(int loglevel, char *fmt, ...)
294 char outbuf[BUFFER_SIZE];
297 long this_thread = 1; /* was: pthread_t this_thread;*/
299 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
301 * Irrespective of debug setting, a GET/POST/CONNECT makes
302 * the taskbar icon animate. (There is an option to disable
303 * this but checking that is handled inside LogShowActivity()).
305 if (loglevel == LOG_LEVEL_GPC)
309 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
311 /* verify if loglevel applies to current settings and bail out if negative */
312 if ((loglevel & debug) == 0)
317 /* FIXME get current thread id */
318 #ifdef FEATURE_PTHREAD
319 this_thread = (long)pthread_self();
320 #endif /* def FEATURE_PTHREAD */
324 case LOG_LEVEL_ERROR:
325 outc = sprintf(outbuf, "IJB(%ld) Error: ", this_thread);
327 case LOG_LEVEL_FATAL:
328 outc = sprintf(outbuf, "IJB(%ld) Fatal error: ", this_thread);
331 outc = sprintf(outbuf, "IJB(%ld) Request: ", this_thread);
333 case LOG_LEVEL_CONNECT:
334 outc = sprintf(outbuf, "IJB(%ld) Connect: ", this_thread);
337 outc = sprintf(outbuf, "IJB(%ld) Writing: ", this_thread);
339 case LOG_LEVEL_HEADER:
340 outc = sprintf(outbuf, "IJB(%ld) Header: ", this_thread);
343 outc = sprintf(outbuf, "IJB(%ld) Info: ", this_thread);
345 case LOG_LEVEL_RE_FILTER:
346 outc = sprintf(outbuf, "IJB(%ld) Re-Filter: ", this_thread);
348 #ifdef FEATURE_FORCE_LOAD
349 case LOG_LEVEL_FORCE:
350 outc = sprintf(outbuf, "IJB(%ld) Force: ", this_thread);
352 #endif /* def FEATURE_FORCE_LOAD */
353 #ifdef FEATURE_FAST_REDIRECTS
354 case LOG_LEVEL_REDIRECTS:
355 outc = sprintf(outbuf, "IJB(%ld) Redirect: ", this_thread);
357 #endif /* def FEATURE_FAST_REDIRECTS */
358 case LOG_LEVEL_DEANIMATE:
359 outc = sprintf(outbuf, "IJB(%ld) Gif-Deanimate: ", this_thread);
365 #ifdef FEATURE_KILL_POPUPS
366 case LOG_LEVEL_POPUPS:
367 outc = sprintf(outbuf, "IJB(%ld) Kill-Popups: ", this_thread);
369 #endif /* def FEATURE_KILL_POPUPS */
371 outc = sprintf(outbuf, "IJB(%d) CGI: ", this_thread);
374 outc = sprintf(outbuf, "IJB(%ld) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
378 /* get ready to scan var. args. */
381 /* build formatted message from fmt and var-args */
382 while ((*src) && (outc < BUFFER_SIZE-2))
384 char tempbuf[BUFFER_SIZE];
403 outbuf[outc++] = '%';
406 ival = va_arg( ap, int );
408 outc += sprintf(tempbuf, "%d", ival);
409 if (outc < BUFFER_SIZE-1)
411 strcpy(outbuf + oldoutc, tempbuf);
415 outbuf[oldoutc] = '\0';
419 uval = va_arg( ap, unsigned );
421 outc += sprintf(tempbuf, "%u", uval);
422 if (outc < BUFFER_SIZE-1)
424 strcpy(outbuf + oldoutc, tempbuf);
428 outbuf[oldoutc] = '\0';
432 /* this is a modifier that must be followed by u or d */
436 lval = va_arg( ap, long );
438 outc += sprintf(tempbuf, "%ld", lval);
442 ulval = va_arg( ap, unsigned long );
444 outc += sprintf(tempbuf, "%lu", ulval);
449 sprintf(outbuf, "IJB(%ld) Error: log_error(): Bad format string:\n"
451 "Exiting.", this_thread, fmt);
452 /* FIXME RACE HAZARD: should start critical section error_log_use here */
457 fputs(outbuf, logfp);
458 /* FIXME RACE HAZARD: should end critical section error_log_use here */
463 if (outc < BUFFER_SIZE-1)
465 strcpy(outbuf + oldoutc, tempbuf);
469 outbuf[oldoutc] = '\0';
474 * Note that char paramaters are converted to int, so we need to
475 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
477 outbuf[outc++] = (char) va_arg( ap, int );
480 sval = va_arg( ap, char * );
482 outc += strlen(sval);
483 if (outc < BUFFER_SIZE-1)
485 strcpy(outbuf + oldoutc, sval);
489 outbuf[oldoutc] = '\0';
493 /* Non-standard: Print a counted string. Takes 2 parameters:
494 * int length, const char * string
496 ival = va_arg( ap, int );
497 sval = va_arg( ap, char * );
504 if (outc < BUFFER_SIZE-1)
506 memcpy(outbuf + oldoutc, sval, ival);
510 outbuf[oldoutc] = '\0';
514 /* Non-standard: Print error code from errno */
516 ival = WSAGetLastError();
517 sval = w32_socket_strerr(ival, tempbuf);
518 #else /* ifndef _WIN32 */
521 sval = strerror(ival);
522 #else /* ifndef HAVE_STRERROR */
524 #endif /* ndef HAVE_STRERROR */
527 sprintf(tempbuf, "(errno = %d)", ival);
530 #endif /* ndef _WIN32 */
532 outc += strlen(sval);
533 if (outc < BUFFER_SIZE-1)
535 strcpy(outbuf + oldoutc, sval);
539 outbuf[oldoutc] = '\0';
543 /* Non-standard: Print a Common Log File timestamp */
546 * Write timestamp into tempbuf.
548 * Complex because not all OSs have tm_gmtoff or
549 * the %z field in strftime()
556 gmt = *gmtime (&now);
557 tm_now = localtime (&now);
558 days = tm_now->tm_yday - gmt.tm_yday;
559 hrs = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + tm_now->tm_hour - gmt.tm_hour);
560 mins = hrs * 60 + tm_now->tm_min - gmt.tm_min;
561 strftime (tempbuf, BUFFER_SIZE-6, "%d/%b/%Y:%H:%M:%S ", tm_now);
562 sprintf (tempbuf + strlen(tempbuf), "%+03d%02d", mins / 60, abs(mins) % 60);
565 outc += strlen(tempbuf);
566 if (outc < BUFFER_SIZE-1)
568 strcpy(outbuf + oldoutc, tempbuf);
572 outbuf[oldoutc] = '\0';
576 sprintf(outbuf, "IJB(%ld) Error: log_error(): Bad format string:\n"
578 "Exiting.", this_thread, fmt);
579 /* FIXME RACE HAZARD: should start critical section error_log_use here */
584 fputs(outbuf, logfp);
585 /* FIXME RACE HAZARD: should end critical section error_log_use here */
594 /* done with var. args */
597 if (outc >= BUFFER_SIZE-2)
599 /* insufficient room for newline and trailing null. */
601 static const char warning[] = "... [too long, truncated]\n";
603 if (outc < BUFFER_SIZE)
605 /* Need to add terminating null in this case. */
609 /* Truncate output */
610 outbuf[BUFFER_SIZE - sizeof(warning)] = '\0';
613 strcat(outbuf, warning);
617 /* Add terminating newline and null */
618 outbuf[outc++] = '\n';
622 /* FIXME RACE HAZARD: should start critical section error_log_use here */
624 /* deal with glibc stupidity - it won't let you initialize logfp */
630 fputs(outbuf, logfp);
632 if (loglevel == LOG_LEVEL_FATAL)
638 /* FIXME RACE HAZARD: should end critical section error_log_use here */
640 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
641 /* Write to display */
642 LogPutString(outbuf);
643 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
649 /*********************************************************************
651 * Function : w32_socket_strerr
653 * Description : Translate the return value from WSAGetLastError()
657 * 1 : errcode = The return value from WSAGetLastError().
658 * 2 : tmp_buf = A temporary buffer that might be used to
661 * Returns : String representing the error code. This may be
662 * a global string constant or a string stored in
665 *********************************************************************/
666 static char *w32_socket_strerr(int errcode, char *tmp_buf)
668 #define TEXT_FOR_ERROR(code,text) \
669 if (errcode == code) \
671 return #code " - " text; \
674 TEXT_FOR_ERROR(WSAEACCES, "Permission denied")
675 TEXT_FOR_ERROR(WSAEADDRINUSE, "Address already in use.")
676 TEXT_FOR_ERROR(WSAEADDRNOTAVAIL, "Cannot assign requested address.");
677 TEXT_FOR_ERROR(WSAEAFNOSUPPORT, "Address family not supported by protocol family.");
678 TEXT_FOR_ERROR(WSAEALREADY, "Operation already in progress.");
679 TEXT_FOR_ERROR(WSAECONNABORTED, "Software caused connection abort.");
680 TEXT_FOR_ERROR(WSAECONNREFUSED, "Connection refused.");
681 TEXT_FOR_ERROR(WSAECONNRESET, "Connection reset by peer.");
682 TEXT_FOR_ERROR(WSAEDESTADDRREQ, "Destination address required.");
683 TEXT_FOR_ERROR(WSAEFAULT, "Bad address.");
684 TEXT_FOR_ERROR(WSAEHOSTDOWN, "Host is down.");
685 TEXT_FOR_ERROR(WSAEHOSTUNREACH, "No route to host.");
686 TEXT_FOR_ERROR(WSAEINPROGRESS, "Operation now in progress.");
687 TEXT_FOR_ERROR(WSAEINTR, "Interrupted function call.");
688 TEXT_FOR_ERROR(WSAEINVAL, "Invalid argument.");
689 TEXT_FOR_ERROR(WSAEISCONN, "Socket is already connected.");
690 TEXT_FOR_ERROR(WSAEMFILE, "Too many open sockets.");
691 TEXT_FOR_ERROR(WSAEMSGSIZE, "Message too long.");
692 TEXT_FOR_ERROR(WSAENETDOWN, "Network is down.");
693 TEXT_FOR_ERROR(WSAENETRESET, "Network dropped connection on reset.");
694 TEXT_FOR_ERROR(WSAENETUNREACH, "Network is unreachable.");
695 TEXT_FOR_ERROR(WSAENOBUFS, "No buffer space available.");
696 TEXT_FOR_ERROR(WSAENOPROTOOPT, "Bad protocol option.");
697 TEXT_FOR_ERROR(WSAENOTCONN, "Socket is not connected.");
698 TEXT_FOR_ERROR(WSAENOTSOCK, "Socket operation on non-socket.");
699 TEXT_FOR_ERROR(WSAEOPNOTSUPP, "Operation not supported.");
700 TEXT_FOR_ERROR(WSAEPFNOSUPPORT, "Protocol family not supported.");
701 TEXT_FOR_ERROR(WSAEPROCLIM, "Too many processes.");
702 TEXT_FOR_ERROR(WSAEPROTONOSUPPORT, "Protocol not supported.");
703 TEXT_FOR_ERROR(WSAEPROTOTYPE, "Protocol wrong type for socket.");
704 TEXT_FOR_ERROR(WSAESHUTDOWN, "Cannot send after socket shutdown.");
705 TEXT_FOR_ERROR(WSAESOCKTNOSUPPORT, "Socket type not supported.");
706 TEXT_FOR_ERROR(WSAETIMEDOUT, "Connection timed out.");
707 TEXT_FOR_ERROR(WSAEWOULDBLOCK, "Resource temporarily unavailable.");
708 TEXT_FOR_ERROR(WSAHOST_NOT_FOUND, "Host not found.");
709 TEXT_FOR_ERROR(WSANOTINITIALISED, "Successful WSAStartup not yet performed.");
710 TEXT_FOR_ERROR(WSANO_DATA, "Valid name, no data record of requested type.");
711 TEXT_FOR_ERROR(WSANO_RECOVERY, "This is a non-recoverable error.");
712 TEXT_FOR_ERROR(WSASYSNOTREADY, "Network subsystem is unavailable.");
713 TEXT_FOR_ERROR(WSATRY_AGAIN, "Non-authoritative host not found.");
714 TEXT_FOR_ERROR(WSAVERNOTSUPPORTED, "WINSOCK.DLL version out of range.");
715 TEXT_FOR_ERROR(WSAEDISCON, "Graceful shutdown in progress.");
717 * The following error codes are documented in the Microsoft WinSock
718 * reference guide, but don't actually exist.
720 * TEXT_FOR_ERROR(WSA_INVALID_HANDLE, "Specified event object handle is invalid.");
721 * TEXT_FOR_ERROR(WSA_INVALID_PARAMETER, "One or more parameters are invalid.");
722 * TEXT_FOR_ERROR(WSAINVALIDPROCTABLE, "Invalid procedure table from service provider.");
723 * TEXT_FOR_ERROR(WSAINVALIDPROVIDER, "Invalid service provider version number.");
724 * TEXT_FOR_ERROR(WSA_IO_PENDING, "Overlapped operations will complete later.");
725 * TEXT_FOR_ERROR(WSA_IO_INCOMPLETE, "Overlapped I/O event object not in signaled state.");
726 * TEXT_FOR_ERROR(WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available.");
727 * TEXT_FOR_ERROR(WSAPROVIDERFAILEDINIT, "Unable to initialize a service provider.");
728 * TEXT_FOR_ERROR(WSASYSCALLFAILURE, "System call failure.");
729 * TEXT_FOR_ERROR(WSA_OPERATION_ABORTED, "Overlapped operation aborted.");
732 sprintf(tmp_buf, "(error number %d)", errcode);
735 #endif /* def _WIN32 */