1 const char errlog_rcs[] = "$Id: errlog.c,v 1.17 2001/09/10 10:17:13 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.17 2001/09/10 10:17:13 oes
37 * Removed unused variable; Fixed sprintf format
39 * Revision 1.16 2001/07/30 22:08:36 jongfoster
40 * Tidying up #defines:
41 * - All feature #defines are now of the form FEATURE_xxx
42 * - Permanently turned off WIN_GUI_EDIT
43 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
45 * Revision 1.15 2001/07/29 17:41:10 jongfoster
46 * Now prints thread ID for each message (pthreads only)
48 * Revision 1.14 2001/07/19 19:03:48 haroon
49 * - Added case for LOG_LEVEL_POPUPS
51 * Revision 1.13 2001/07/13 13:58:58 oes
52 * - Added case for LOG_LEVEL_DEANIMATE
53 * - Removed all #ifdef PCRS
55 * Revision 1.12 2001/06/09 10:55:28 jongfoster
56 * Changing BUFSIZ ==> BUFFER_SIZE
58 * Revision 1.11 2001/06/01 18:14:49 jongfoster
59 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
60 * in config.h if appropriate) rather than the NO_STRERR macro.
62 * Revision 1.10 2001/05/29 11:52:21 oes
63 * Conditional compilation of w32_socket_error
65 * Revision 1.9 2001/05/28 16:15:17 jongfoster
66 * Improved reporting of errors under Win32.
68 * Revision 1.8 2001/05/26 17:25:14 jongfoster
69 * Added support for CLF (Common Log Format) and fixed LOG_LEVEL_LOG
71 * Revision 1.7 2001/05/26 15:21:28 jongfoster
72 * Activity animation in Win32 GUI now works even if debug==0
74 * Revision 1.6 2001/05/25 21:55:08 jongfoster
75 * Now cleans up properly on FATAL (removes taskbar icon etc)
77 * Revision 1.5 2001/05/22 18:46:04 oes
79 * - Enabled filtering banners by size rather than URL
80 * by adding patterns that replace all standard banner
81 * sizes with the "Junkbuster" gif to the re_filterfile
83 * - Enabled filtering WebBugs by providing a pattern
84 * which kills all 1x1 images
86 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
87 * which is selected by the (nonstandard and therefore
88 * capital) letter 'U' in the option string.
89 * It causes the quantifiers to be ungreedy by default.
90 * Appending a ? turns back to greedy (!).
92 * - Added a new interceptor ijb-send-banner, which
93 * sends back the "Junkbuster" gif. Without imagelist or
94 * MSIE detection support, or if tinygif = 1, or the
95 * URL isn't recognized as an imageurl, a lame HTML
96 * explanation is sent instead.
98 * - Added new feature, which permits blocking remote
99 * script redirects and firing back a local redirect
101 * The feature is conditionally compiled, i.e. it
102 * can be disabled with --disable-fast-redirects,
103 * plus it must be activated by a "fast-redirects"
104 * line in the config file, has its own log level
105 * and of course wants to be displayed by show-proxy-args
106 * Note: Boy, all the #ifdefs in 1001 locations and
107 * all the fumbling with configure.in and acconfig.h
108 * were *way* more work than the feature itself :-(
110 * - Because a generic redirect template was needed for
111 * this, tinygif = 3 now uses the same.
113 * - Moved GIFs, and other static HTTP response templates
118 * - Removed some >400 CRs again (Jon, you really worked
121 * Revision 1.4 2001/05/21 19:32:54 jongfoster
122 * Added another #ifdef _WIN_CONSOLE
124 * Revision 1.3 2001/05/20 01:11:40 jongfoster
125 * Added support for LOG_LEVEL_FATAL
126 * Renamed LOG_LEVEL_FRC to LOG_LEVEL_FORCE,
127 * and LOG_LEVEL_REF to LOG_LEVEL_RE_FILTER
129 * Revision 1.2 2001/05/17 22:42:01 oes
130 * - Cleaned CRLF's from the sources and related files
131 * - Repaired logging for REF and FRC
133 * Revision 1.1.1.1 2001/05/15 13:58:51 oes
134 * Initial import of version 2.9.3 source tree
137 *********************************************************************/
149 #endif /* ndef _WIN32 */
152 #ifdef FEATURE_PTHREAD
154 #endif /* def FEATURE_PTHREAD */
160 #endif /* ndef _WIN_CONSOLE */
161 #endif /* def _WIN32 */
166 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
170 * LOG_LEVEL_FATAL cannot be turned off. (There are
171 * some exceptional situations where we need to get a
172 * message to the user).
174 #define LOG_LEVEL_MINIMUM LOG_LEVEL_FATAL
176 /* where to log (default: stderr) */
177 static FILE *logfp = NULL;
179 /* logging detail level. */
180 static int debug = (LOG_LEVEL_FATAL | LOG_LEVEL_ERROR | LOG_LEVEL_INFO);
182 /* static functions */
183 static void fatal_error(const char * error_message);
185 static char *w32_socket_strerr(int errcode, char *tmp_buf);
188 /*********************************************************************
190 * Function : fatal_error
192 * Description : Displays a fatal error to standard error (or, on
193 * a WIN32 GUI, to a dialog box), and exits
194 * JunkBuster with status code 1.
197 * 1 : error_message = The error message to display.
199 * Returns : Does not return.
201 *********************************************************************/
202 static void fatal_error(const char * error_message)
204 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
205 MessageBox(g_hwndLogFrame, error_message, "Internet JunkBuster Error",
206 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
208 /* Cleanup - remove taskbar icon etc. */
211 #else /* if !defined(_WIN32) || defined(_WIN_CONSOLE) */
212 fputs(error_message, stderr);
213 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
219 /*********************************************************************
221 * Function : init_errlog
223 * Description : Initializes the logging module. Must call before
227 * 1 : prog_name = The program name.
228 * 2 : logfname = The logfile name, or NULL for stderr.
229 * 3 : debuglevel = The debugging level.
233 *********************************************************************/
234 void init_error_log(const char *prog_name, const char *logfname, int debuglevel)
238 /* FIXME RACE HAZARD: should start critical section error_log_use here */
240 /* set the logging detail level */
241 debug = debuglevel | LOG_LEVEL_MINIMUM;
243 if ((logfp != NULL) && (logfp != stderr))
249 /* set the designated log file */
252 if( !(fp = fopen(logfname, "a")) )
254 log_error(LOG_LEVEL_FATAL, "init_errlog(): can't open logfile: %s", logfname);
257 /* set logging to be completely unbuffered */
263 log_error(LOG_LEVEL_INFO, "Internet JunkBuster version " VERSION);
264 if (prog_name != NULL)
266 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
269 /* FIXME RACE HAZARD: should end critical section error_log_use here */
271 } /* init_error_log */
274 /*********************************************************************
276 * Function : log_error
278 * Description : This is the error-reporting and logging function.
281 * 1 : loglevel = the type of message to be logged
282 * 2 : fmt = the main string we want logged, printf-like
283 * 3 : ... = arguments to be inserted in fmt (printf-like).
287 *********************************************************************/
288 void log_error(int loglevel, char *fmt, ...)
291 char outbuf[BUFFER_SIZE];
294 long this_thread = 1; /* was: pthread_t this_thread;*/
296 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
298 * Irrespective of debug setting, a GET/POST/CONNECT makes
299 * the taskbar icon animate. (There is an option to disable
300 * this but checking that is handled inside LogShowActivity()).
302 if (loglevel == LOG_LEVEL_GPC)
306 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
308 /* verify if loglevel applies to current settings and bail out if negative */
309 if ((loglevel & debug) == 0)
314 /* FIXME get current thread id */
315 #ifdef FEATURE_PTHREAD
316 this_thread = (long)pthread_self();
317 #endif /* def FEATURE_PTHREAD */
321 case LOG_LEVEL_ERROR:
322 outc = sprintf(outbuf, "IJB(%ld) Error: ", this_thread);
324 case LOG_LEVEL_FATAL:
325 outc = sprintf(outbuf, "IJB(%ld) Fatal error: ", this_thread);
328 outc = sprintf(outbuf, "IJB(%ld) Request: ", this_thread);
330 case LOG_LEVEL_CONNECT:
331 outc = sprintf(outbuf, "IJB(%ld) Connect: ", this_thread);
334 outc = sprintf(outbuf, "IJB(%ld) Writing: ", this_thread);
336 case LOG_LEVEL_HEADER:
337 outc = sprintf(outbuf, "IJB(%ld) Header: ", this_thread);
340 outc = sprintf(outbuf, "IJB(%ld) Info: ", this_thread);
342 case LOG_LEVEL_RE_FILTER:
343 outc = sprintf(outbuf, "IJB(%ld) Re-Filter: ", this_thread);
345 #ifdef FEATURE_FORCE_LOAD
346 case LOG_LEVEL_FORCE:
347 outc = sprintf(outbuf, "IJB(%ld) Force: ", this_thread);
349 #endif /* def FEATURE_FORCE_LOAD */
350 #ifdef FEATURE_FAST_REDIRECTS
351 case LOG_LEVEL_REDIRECTS:
352 outc = sprintf(outbuf, "IJB(%ld) Redirect: ", this_thread);
354 #endif /* def FEATURE_FAST_REDIRECTS */
355 case LOG_LEVEL_DEANIMATE:
356 outc = sprintf(outbuf, "IJB(%ld) Gif-Deanimate: ", this_thread);
362 #ifdef FEATURE_KILL_POPUPS
363 case LOG_LEVEL_POPUPS:
364 outc = sprintf(outbuf, "IJB(%ld) Kill-Popups: ", this_thread);
366 #endif /* def FEATURE_KILL_POPUPS */
368 outc = sprintf(outbuf, "IJB(%ld) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
372 /* get ready to scan var. args. */
375 /* build formatted message from fmt and var-args */
376 while ((*src) && (outc < BUFFER_SIZE-2))
378 char tempbuf[BUFFER_SIZE];
397 outbuf[outc++] = '%';
400 ival = va_arg( ap, int );
402 outc += sprintf(tempbuf, "%d", ival);
403 if (outc < BUFFER_SIZE-1)
405 strcpy(outbuf + oldoutc, tempbuf);
409 outbuf[oldoutc] = '\0';
413 uval = va_arg( ap, unsigned );
415 outc += sprintf(tempbuf, "%u", uval);
416 if (outc < BUFFER_SIZE-1)
418 strcpy(outbuf + oldoutc, tempbuf);
422 outbuf[oldoutc] = '\0';
426 /* this is a modifier that must be followed by u or d */
430 lval = va_arg( ap, long );
432 outc += sprintf(tempbuf, "%ld", lval);
436 ulval = va_arg( ap, unsigned long );
438 outc += sprintf(tempbuf, "%lu", ulval);
443 sprintf(outbuf, "IJB(%ld) Error: log_error(): Bad format string:\n"
445 "Exiting.", this_thread, fmt);
446 /* FIXME RACE HAZARD: should start critical section error_log_use here */
451 fputs(outbuf, logfp);
452 /* FIXME RACE HAZARD: should end critical section error_log_use here */
457 if (outc < BUFFER_SIZE-1)
459 strcpy(outbuf + oldoutc, tempbuf);
463 outbuf[oldoutc] = '\0';
468 * Note that char paramaters are converted to int, so we need to
469 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
471 outbuf[outc++] = (char) va_arg( ap, int );
474 sval = va_arg( ap, char * );
476 outc += strlen(sval);
477 if (outc < BUFFER_SIZE-1)
479 strcpy(outbuf + oldoutc, sval);
483 outbuf[oldoutc] = '\0';
487 /* Non-standard: Print a counted string. Takes 2 parameters:
488 * int length, const char * string
490 ival = va_arg( ap, int );
491 sval = va_arg( ap, char * );
498 if (outc < BUFFER_SIZE-1)
500 memcpy(outbuf + oldoutc, sval, ival);
504 outbuf[oldoutc] = '\0';
508 /* Non-standard: Print error code from errno */
510 ival = WSAGetLastError();
511 sval = w32_socket_strerr(ival, tempbuf);
512 #else /* ifndef _WIN32 */
515 sval = strerror(ival);
516 #else /* ifndef HAVE_STRERROR */
518 #endif /* ndef HAVE_STRERROR */
521 sprintf(tempbuf, "(errno = %d)", ival);
524 #endif /* ndef _WIN32 */
526 outc += strlen(sval);
527 if (outc < BUFFER_SIZE-1)
529 strcpy(outbuf + oldoutc, sval);
533 outbuf[oldoutc] = '\0';
537 /* Non-standard: Print a Common Log File timestamp */
540 * Write timestamp into tempbuf.
542 * Complex because not all OSs have tm_gmtoff or
543 * the %z field in strftime()
550 gmt = *gmtime (&now);
551 tm_now = localtime (&now);
552 days = tm_now->tm_yday - gmt.tm_yday;
553 hrs = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + tm_now->tm_hour - gmt.tm_hour);
554 mins = hrs * 60 + tm_now->tm_min - gmt.tm_min;
555 strftime (tempbuf, BUFFER_SIZE-6, "%d/%b/%Y:%H:%M:%S ", tm_now);
556 sprintf (tempbuf + strlen(tempbuf), "%+03d%02d", mins / 60, abs(mins) % 60);
559 outc += strlen(tempbuf);
560 if (outc < BUFFER_SIZE-1)
562 strcpy(outbuf + oldoutc, tempbuf);
566 outbuf[oldoutc] = '\0';
570 sprintf(outbuf, "IJB(%ld) Error: log_error(): Bad format string:\n"
572 "Exiting.", this_thread, fmt);
573 /* FIXME RACE HAZARD: should start critical section error_log_use here */
578 fputs(outbuf, logfp);
579 /* FIXME RACE HAZARD: should end critical section error_log_use here */
588 /* done with var. args */
591 if (outc >= BUFFER_SIZE-2)
593 /* insufficient room for newline and trailing null. */
595 static const char warning[] = "... [too long, truncated]\n";
597 if (outc < BUFFER_SIZE)
599 /* Need to add terminating null in this case. */
603 /* Truncate output */
604 outbuf[BUFFER_SIZE - sizeof(warning)] = '\0';
607 strcat(outbuf, warning);
611 /* Add terminating newline and null */
612 outbuf[outc++] = '\n';
616 /* FIXME RACE HAZARD: should start critical section error_log_use here */
618 /* deal with glibc stupidity - it won't let you initialize logfp */
624 fputs(outbuf, logfp);
626 if (loglevel == LOG_LEVEL_FATAL)
632 /* FIXME RACE HAZARD: should end critical section error_log_use here */
634 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
635 /* Write to display */
636 LogPutString(outbuf);
637 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
643 /*********************************************************************
645 * Function : w32_socket_strerr
647 * Description : Translate the return value from WSAGetLastError()
651 * 1 : errcode = The return value from WSAGetLastError().
652 * 2 : tmp_buf = A temporary buffer that might be used to
655 * Returns : String representing the error code. This may be
656 * a global string constant or a string stored in
659 *********************************************************************/
660 static char *w32_socket_strerr(int errcode, char *tmp_buf)
662 #define TEXT_FOR_ERROR(code,text) \
663 if (errcode == code) \
665 return #code " - " text; \
668 TEXT_FOR_ERROR(WSAEACCES, "Permission denied")
669 TEXT_FOR_ERROR(WSAEADDRINUSE, "Address already in use.")
670 TEXT_FOR_ERROR(WSAEADDRNOTAVAIL, "Cannot assign requested address.");
671 TEXT_FOR_ERROR(WSAEAFNOSUPPORT, "Address family not supported by protocol family.");
672 TEXT_FOR_ERROR(WSAEALREADY, "Operation already in progress.");
673 TEXT_FOR_ERROR(WSAECONNABORTED, "Software caused connection abort.");
674 TEXT_FOR_ERROR(WSAECONNREFUSED, "Connection refused.");
675 TEXT_FOR_ERROR(WSAECONNRESET, "Connection reset by peer.");
676 TEXT_FOR_ERROR(WSAEDESTADDRREQ, "Destination address required.");
677 TEXT_FOR_ERROR(WSAEFAULT, "Bad address.");
678 TEXT_FOR_ERROR(WSAEHOSTDOWN, "Host is down.");
679 TEXT_FOR_ERROR(WSAEHOSTUNREACH, "No route to host.");
680 TEXT_FOR_ERROR(WSAEINPROGRESS, "Operation now in progress.");
681 TEXT_FOR_ERROR(WSAEINTR, "Interrupted function call.");
682 TEXT_FOR_ERROR(WSAEINVAL, "Invalid argument.");
683 TEXT_FOR_ERROR(WSAEISCONN, "Socket is already connected.");
684 TEXT_FOR_ERROR(WSAEMFILE, "Too many open sockets.");
685 TEXT_FOR_ERROR(WSAEMSGSIZE, "Message too long.");
686 TEXT_FOR_ERROR(WSAENETDOWN, "Network is down.");
687 TEXT_FOR_ERROR(WSAENETRESET, "Network dropped connection on reset.");
688 TEXT_FOR_ERROR(WSAENETUNREACH, "Network is unreachable.");
689 TEXT_FOR_ERROR(WSAENOBUFS, "No buffer space available.");
690 TEXT_FOR_ERROR(WSAENOPROTOOPT, "Bad protocol option.");
691 TEXT_FOR_ERROR(WSAENOTCONN, "Socket is not connected.");
692 TEXT_FOR_ERROR(WSAENOTSOCK, "Socket operation on non-socket.");
693 TEXT_FOR_ERROR(WSAEOPNOTSUPP, "Operation not supported.");
694 TEXT_FOR_ERROR(WSAEPFNOSUPPORT, "Protocol family not supported.");
695 TEXT_FOR_ERROR(WSAEPROCLIM, "Too many processes.");
696 TEXT_FOR_ERROR(WSAEPROTONOSUPPORT, "Protocol not supported.");
697 TEXT_FOR_ERROR(WSAEPROTOTYPE, "Protocol wrong type for socket.");
698 TEXT_FOR_ERROR(WSAESHUTDOWN, "Cannot send after socket shutdown.");
699 TEXT_FOR_ERROR(WSAESOCKTNOSUPPORT, "Socket type not supported.");
700 TEXT_FOR_ERROR(WSAETIMEDOUT, "Connection timed out.");
701 TEXT_FOR_ERROR(WSAEWOULDBLOCK, "Resource temporarily unavailable.");
702 TEXT_FOR_ERROR(WSAHOST_NOT_FOUND, "Host not found.");
703 TEXT_FOR_ERROR(WSANOTINITIALISED, "Successful WSAStartup not yet performed.");
704 TEXT_FOR_ERROR(WSANO_DATA, "Valid name, no data record of requested type.");
705 TEXT_FOR_ERROR(WSANO_RECOVERY, "This is a non-recoverable error.");
706 TEXT_FOR_ERROR(WSASYSNOTREADY, "Network subsystem is unavailable.");
707 TEXT_FOR_ERROR(WSATRY_AGAIN, "Non-authoritative host not found.");
708 TEXT_FOR_ERROR(WSAVERNOTSUPPORTED, "WINSOCK.DLL version out of range.");
709 TEXT_FOR_ERROR(WSAEDISCON, "Graceful shutdown in progress.");
711 * The following error codes are documented in the Microsoft WinSock
712 * reference guide, but don't actually exist.
714 * TEXT_FOR_ERROR(WSA_INVALID_HANDLE, "Specified event object handle is invalid.");
715 * TEXT_FOR_ERROR(WSA_INVALID_PARAMETER, "One or more parameters are invalid.");
716 * TEXT_FOR_ERROR(WSAINVALIDPROCTABLE, "Invalid procedure table from service provider.");
717 * TEXT_FOR_ERROR(WSAINVALIDPROVIDER, "Invalid service provider version number.");
718 * TEXT_FOR_ERROR(WSA_IO_PENDING, "Overlapped operations will complete later.");
719 * TEXT_FOR_ERROR(WSA_IO_INCOMPLETE, "Overlapped I/O event object not in signaled state.");
720 * TEXT_FOR_ERROR(WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available.");
721 * TEXT_FOR_ERROR(WSAPROVIDERFAILEDINIT, "Unable to initialize a service provider.");
722 * TEXT_FOR_ERROR(WSASYSCALLFAILURE, "System call failure.");
723 * TEXT_FOR_ERROR(WSA_OPERATION_ABORTED, "Overlapped operation aborted.");
726 sprintf(tmp_buf, "(error number %d)", errcode);
729 #endif /* def _WIN32 */