1 const char errlog_rcs[] = "$Id: errlog.c,v 1.20 2001/09/16 23:04:34 jongfoster 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.20 2001/09/16 23:04:34 jongfoster
39 * Revision 1.19 2001/09/13 20:08:06 jongfoster
40 * Adding support for LOG_LEVEL_CGI
42 * Revision 1.18 2001/09/10 11:27:24 oes
43 * Declaration of w32_socket_strerr now conditional
45 * Revision 1.17 2001/09/10 10:17:13 oes
46 * Removed unused variable; Fixed sprintf format
48 * Revision 1.16 2001/07/30 22:08:36 jongfoster
49 * Tidying up #defines:
50 * - All feature #defines are now of the form FEATURE_xxx
51 * - Permanently turned off WIN_GUI_EDIT
52 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
54 * Revision 1.15 2001/07/29 17:41:10 jongfoster
55 * Now prints thread ID for each message (pthreads only)
57 * Revision 1.14 2001/07/19 19:03:48 haroon
58 * - Added case for LOG_LEVEL_POPUPS
60 * Revision 1.13 2001/07/13 13:58:58 oes
61 * - Added case for LOG_LEVEL_DEANIMATE
62 * - Removed all #ifdef PCRS
64 * Revision 1.12 2001/06/09 10:55:28 jongfoster
65 * Changing BUFSIZ ==> BUFFER_SIZE
67 * Revision 1.11 2001/06/01 18:14:49 jongfoster
68 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
69 * in config.h if appropriate) rather than the NO_STRERR macro.
71 * Revision 1.10 2001/05/29 11:52:21 oes
72 * Conditional compilation of w32_socket_error
74 * Revision 1.9 2001/05/28 16:15:17 jongfoster
75 * Improved reporting of errors under Win32.
77 * Revision 1.8 2001/05/26 17:25:14 jongfoster
78 * Added support for CLF (Common Log Format) and fixed LOG_LEVEL_LOG
80 * Revision 1.7 2001/05/26 15:21:28 jongfoster
81 * Activity animation in Win32 GUI now works even if debug==0
83 * Revision 1.6 2001/05/25 21:55:08 jongfoster
84 * Now cleans up properly on FATAL (removes taskbar icon etc)
86 * Revision 1.5 2001/05/22 18:46:04 oes
88 * - Enabled filtering banners by size rather than URL
89 * by adding patterns that replace all standard banner
90 * sizes with the "Junkbuster" gif to the re_filterfile
92 * - Enabled filtering WebBugs by providing a pattern
93 * which kills all 1x1 images
95 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
96 * which is selected by the (nonstandard and therefore
97 * capital) letter 'U' in the option string.
98 * It causes the quantifiers to be ungreedy by default.
99 * Appending a ? turns back to greedy (!).
101 * - Added a new interceptor ijb-send-banner, which
102 * sends back the "Junkbuster" gif. Without imagelist or
103 * MSIE detection support, or if tinygif = 1, or the
104 * URL isn't recognized as an imageurl, a lame HTML
105 * explanation is sent instead.
107 * - Added new feature, which permits blocking remote
108 * script redirects and firing back a local redirect
110 * The feature is conditionally compiled, i.e. it
111 * can be disabled with --disable-fast-redirects,
112 * plus it must be activated by a "fast-redirects"
113 * line in the config file, has its own log level
114 * and of course wants to be displayed by show-proxy-args
115 * Note: Boy, all the #ifdefs in 1001 locations and
116 * all the fumbling with configure.in and acconfig.h
117 * were *way* more work than the feature itself :-(
119 * - Because a generic redirect template was needed for
120 * this, tinygif = 3 now uses the same.
122 * - Moved GIFs, and other static HTTP response templates
127 * - Removed some >400 CRs again (Jon, you really worked
130 * Revision 1.4 2001/05/21 19:32:54 jongfoster
131 * Added another #ifdef _WIN_CONSOLE
133 * Revision 1.3 2001/05/20 01:11:40 jongfoster
134 * Added support for LOG_LEVEL_FATAL
135 * Renamed LOG_LEVEL_FRC to LOG_LEVEL_FORCE,
136 * and LOG_LEVEL_REF to LOG_LEVEL_RE_FILTER
138 * Revision 1.2 2001/05/17 22:42:01 oes
139 * - Cleaned CRLF's from the sources and related files
140 * - Repaired logging for REF and FRC
142 * Revision 1.1.1.1 2001/05/15 13:58:51 oes
143 * Initial import of version 2.9.3 source tree
146 *********************************************************************/
156 #if !defined(_WIN32) && !defined(__OS2__)
158 #endif /* !defined(_WIN32) && !defined(__OS2__) */
162 #ifdef FEATURE_PTHREAD
164 #endif /* def FEATURE_PTHREAD */
170 #endif /* ndef _WIN_CONSOLE */
171 #endif /* def _WIN32 */
181 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
185 * LOG_LEVEL_FATAL cannot be turned off. (There are
186 * some exceptional situations where we need to get a
187 * message to the user).
189 #define LOG_LEVEL_MINIMUM LOG_LEVEL_FATAL
191 /* where to log (default: stderr) */
192 static FILE *logfp = NULL;
194 /* logging detail level. */
195 static int debug = (LOG_LEVEL_FATAL | LOG_LEVEL_ERROR | LOG_LEVEL_INFO);
197 /* static functions */
198 static void fatal_error(const char * error_message);
200 static char *w32_socket_strerr(int errcode, char *tmp_buf);
203 /*********************************************************************
205 * Function : fatal_error
207 * Description : Displays a fatal error to standard error (or, on
208 * a WIN32 GUI, to a dialog box), and exits
209 * JunkBuster with status code 1.
212 * 1 : error_message = The error message to display.
214 * Returns : Does not return.
216 *********************************************************************/
217 static void fatal_error(const char * error_message)
219 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
220 MessageBox(g_hwndLogFrame, error_message, "Internet JunkBuster Error",
221 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
223 /* Cleanup - remove taskbar icon etc. */
226 #else /* if !defined(_WIN32) || defined(_WIN_CONSOLE) */
227 fputs(error_message, stderr);
228 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
234 /*********************************************************************
236 * Function : init_errlog
238 * Description : Initializes the logging module. Must call before
242 * 1 : prog_name = The program name.
243 * 2 : logfname = The logfile name, or NULL for stderr.
244 * 3 : debuglevel = The debugging level.
248 *********************************************************************/
249 void init_error_log(const char *prog_name, const char *logfname, int debuglevel)
253 /* FIXME RACE HAZARD: should start critical section error_log_use here */
255 /* set the logging detail level */
256 debug = debuglevel | LOG_LEVEL_MINIMUM;
258 if ((logfp != NULL) && (logfp != stderr))
264 /* set the designated log file */
267 if( !(fp = fopen(logfname, "a")) )
269 log_error(LOG_LEVEL_FATAL, "init_errlog(): can't open logfile: %s", logfname);
272 /* set logging to be completely unbuffered */
278 log_error(LOG_LEVEL_INFO, "Internet JunkBuster version " VERSION);
279 if (prog_name != NULL)
281 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
284 /* FIXME RACE HAZARD: should end critical section error_log_use here */
286 } /* init_error_log */
289 /*********************************************************************
291 * Function : log_error
293 * Description : This is the error-reporting and logging function.
296 * 1 : loglevel = the type of message to be logged
297 * 2 : fmt = the main string we want logged, printf-like
298 * 3 : ... = arguments to be inserted in fmt (printf-like).
302 *********************************************************************/
303 void log_error(int loglevel, char *fmt, ...)
309 long this_thread = 1; /* was: pthread_t this_thread;*/
315 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
317 * Irrespective of debug setting, a GET/POST/CONNECT makes
318 * the taskbar icon animate. (There is an option to disable
319 * this but checking that is handled inside LogShowActivity()).
321 if (loglevel == LOG_LEVEL_GPC)
325 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
327 /* verify if loglevel applies to current settings and bail out if negative */
328 if ((loglevel & debug) == 0)
333 /* FIXME get current thread id */
334 #ifdef FEATURE_PTHREAD
335 this_thread = (long)pthread_self();
338 ulrc = DosGetInfoBlocks(&ptib, NULL);
340 this_thread = ptib -> tib_ptib2 -> tib2_ultid;
341 #endif /* def FEATURE_PTHREAD */
343 outbuf = (char*)malloc(BUFFER_SIZE);
347 case LOG_LEVEL_ERROR:
348 outc = sprintf(outbuf, "IJB(%ld) Error: ", this_thread);
350 case LOG_LEVEL_FATAL:
351 outc = sprintf(outbuf, "IJB(%ld) Fatal error: ", this_thread);
354 outc = sprintf(outbuf, "IJB(%ld) Request: ", this_thread);
356 case LOG_LEVEL_CONNECT:
357 outc = sprintf(outbuf, "IJB(%ld) Connect: ", this_thread);
360 outc = sprintf(outbuf, "IJB(%ld) Writing: ", this_thread);
362 case LOG_LEVEL_HEADER:
363 outc = sprintf(outbuf, "IJB(%ld) Header: ", this_thread);
366 outc = sprintf(outbuf, "IJB(%ld) Info: ", this_thread);
368 case LOG_LEVEL_RE_FILTER:
369 outc = sprintf(outbuf, "IJB(%ld) Re-Filter: ", this_thread);
371 #ifdef FEATURE_FORCE_LOAD
372 case LOG_LEVEL_FORCE:
373 outc = sprintf(outbuf, "IJB(%ld) Force: ", this_thread);
375 #endif /* def FEATURE_FORCE_LOAD */
376 #ifdef FEATURE_FAST_REDIRECTS
377 case LOG_LEVEL_REDIRECTS:
378 outc = sprintf(outbuf, "IJB(%ld) Redirect: ", this_thread);
380 #endif /* def FEATURE_FAST_REDIRECTS */
381 case LOG_LEVEL_DEANIMATE:
382 outc = sprintf(outbuf, "IJB(%ld) Gif-Deanimate: ", this_thread);
388 #ifdef FEATURE_KILL_POPUPS
389 case LOG_LEVEL_POPUPS:
390 outc = sprintf(outbuf, "IJB(%ld) Kill-Popups: ", this_thread);
392 #endif /* def FEATURE_KILL_POPUPS */
394 outc = sprintf(outbuf, "IJB(%ld) CGI: ", this_thread);
397 outc = sprintf(outbuf, "IJB(%ld) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
401 /* get ready to scan var. args. */
404 /* build formatted message from fmt and var-args */
405 while ((*src) && (outc < BUFFER_SIZE-2))
407 char tempbuf[BUFFER_SIZE];
426 outbuf[outc++] = '%';
429 ival = va_arg( ap, int );
431 outc += sprintf(tempbuf, "%d", ival);
432 if (outc < BUFFER_SIZE-1)
434 strcpy(outbuf + oldoutc, tempbuf);
438 outbuf[oldoutc] = '\0';
442 uval = va_arg( ap, unsigned );
444 outc += sprintf(tempbuf, "%u", uval);
445 if (outc < BUFFER_SIZE-1)
447 strcpy(outbuf + oldoutc, tempbuf);
451 outbuf[oldoutc] = '\0';
455 /* this is a modifier that must be followed by u or d */
459 lval = va_arg( ap, long );
461 outc += sprintf(tempbuf, "%ld", lval);
465 ulval = va_arg( ap, unsigned long );
467 outc += sprintf(tempbuf, "%lu", ulval);
472 sprintf(outbuf, "IJB(%ld) Error: log_error(): Bad format string:\n"
474 "Exiting.", this_thread, fmt);
475 /* FIXME RACE HAZARD: should start critical section error_log_use here */
480 fputs(outbuf, logfp);
481 /* FIXME RACE HAZARD: should end critical section error_log_use here */
486 if (outc < BUFFER_SIZE-1)
488 strcpy(outbuf + oldoutc, tempbuf);
492 outbuf[oldoutc] = '\0';
497 * Note that char paramaters are converted to int, so we need to
498 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
500 outbuf[outc++] = (char) va_arg( ap, int );
503 sval = va_arg( ap, char * );
505 outc += strlen(sval);
506 if (outc < BUFFER_SIZE-1)
508 strcpy(outbuf + oldoutc, sval);
512 outbuf[oldoutc] = '\0';
516 /* Non-standard: Print a counted string. Takes 2 parameters:
517 * int length, const char * string
519 ival = va_arg( ap, int );
520 sval = va_arg( ap, char * );
527 if (outc < BUFFER_SIZE-1)
529 memcpy(outbuf + oldoutc, sval, ival);
533 outbuf[oldoutc] = '\0';
537 /* Non-standard: Print error code from errno */
539 ival = WSAGetLastError();
540 sval = w32_socket_strerr(ival, tempbuf);
541 #else /* ifndef _WIN32 */
544 sval = strerror(ival);
545 #else /* ifndef HAVE_STRERROR */
547 #endif /* ndef HAVE_STRERROR */
550 sprintf(tempbuf, "(errno = %d)", ival);
553 #endif /* ndef _WIN32 */
555 outc += strlen(sval);
556 if (outc < BUFFER_SIZE-1)
558 strcpy(outbuf + oldoutc, sval);
562 outbuf[oldoutc] = '\0';
566 /* Non-standard: Print a Common Log File timestamp */
569 * Write timestamp into tempbuf.
571 * Complex because not all OSs have tm_gmtoff or
572 * the %z field in strftime()
579 gmt = *gmtime (&now);
580 tm_now = localtime (&now);
581 days = tm_now->tm_yday - gmt.tm_yday;
582 hrs = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + tm_now->tm_hour - gmt.tm_hour);
583 mins = hrs * 60 + tm_now->tm_min - gmt.tm_min;
584 strftime (tempbuf, BUFFER_SIZE-6, "%d/%b/%Y:%H:%M:%S ", tm_now);
585 sprintf (tempbuf + strlen(tempbuf), "%+03d%02d", mins / 60, abs(mins) % 60);
588 outc += strlen(tempbuf);
589 if (outc < BUFFER_SIZE-1)
591 strcpy(outbuf + oldoutc, tempbuf);
595 outbuf[oldoutc] = '\0';
599 sprintf(outbuf, "IJB(%ld) Error: log_error(): Bad format string:\n"
601 "Exiting.", this_thread, fmt);
602 /* FIXME RACE HAZARD: should start critical section error_log_use here */
607 fputs(outbuf, logfp);
608 /* FIXME RACE HAZARD: should end critical section error_log_use here */
617 /* done with var. args */
620 if (outc >= BUFFER_SIZE-2)
622 /* insufficient room for newline and trailing null. */
624 static const char warning[] = "... [too long, truncated]\n";
626 if (outc < BUFFER_SIZE)
628 /* Need to add terminating null in this case. */
632 /* Truncate output */
633 outbuf[BUFFER_SIZE - sizeof(warning)] = '\0';
636 strcat(outbuf, warning);
640 /* Add terminating newline and null */
641 outbuf[outc++] = '\n';
645 /* FIXME RACE HAZARD: should start critical section error_log_use here */
647 /* deal with glibc stupidity - it won't let you initialize logfp */
653 fputs(outbuf, logfp);
655 if (loglevel == LOG_LEVEL_FATAL)
661 /* FIXME RACE HAZARD: should end critical section error_log_use here */
663 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
664 /* Write to display */
665 LogPutString(outbuf);
666 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
672 /*********************************************************************
674 * Function : w32_socket_strerr
676 * Description : Translate the return value from WSAGetLastError()
680 * 1 : errcode = The return value from WSAGetLastError().
681 * 2 : tmp_buf = A temporary buffer that might be used to
684 * Returns : String representing the error code. This may be
685 * a global string constant or a string stored in
688 *********************************************************************/
689 static char *w32_socket_strerr(int errcode, char *tmp_buf)
691 #define TEXT_FOR_ERROR(code,text) \
692 if (errcode == code) \
694 return #code " - " text; \
697 TEXT_FOR_ERROR(WSAEACCES, "Permission denied")
698 TEXT_FOR_ERROR(WSAEADDRINUSE, "Address already in use.")
699 TEXT_FOR_ERROR(WSAEADDRNOTAVAIL, "Cannot assign requested address.");
700 TEXT_FOR_ERROR(WSAEAFNOSUPPORT, "Address family not supported by protocol family.");
701 TEXT_FOR_ERROR(WSAEALREADY, "Operation already in progress.");
702 TEXT_FOR_ERROR(WSAECONNABORTED, "Software caused connection abort.");
703 TEXT_FOR_ERROR(WSAECONNREFUSED, "Connection refused.");
704 TEXT_FOR_ERROR(WSAECONNRESET, "Connection reset by peer.");
705 TEXT_FOR_ERROR(WSAEDESTADDRREQ, "Destination address required.");
706 TEXT_FOR_ERROR(WSAEFAULT, "Bad address.");
707 TEXT_FOR_ERROR(WSAEHOSTDOWN, "Host is down.");
708 TEXT_FOR_ERROR(WSAEHOSTUNREACH, "No route to host.");
709 TEXT_FOR_ERROR(WSAEINPROGRESS, "Operation now in progress.");
710 TEXT_FOR_ERROR(WSAEINTR, "Interrupted function call.");
711 TEXT_FOR_ERROR(WSAEINVAL, "Invalid argument.");
712 TEXT_FOR_ERROR(WSAEISCONN, "Socket is already connected.");
713 TEXT_FOR_ERROR(WSAEMFILE, "Too many open sockets.");
714 TEXT_FOR_ERROR(WSAEMSGSIZE, "Message too long.");
715 TEXT_FOR_ERROR(WSAENETDOWN, "Network is down.");
716 TEXT_FOR_ERROR(WSAENETRESET, "Network dropped connection on reset.");
717 TEXT_FOR_ERROR(WSAENETUNREACH, "Network is unreachable.");
718 TEXT_FOR_ERROR(WSAENOBUFS, "No buffer space available.");
719 TEXT_FOR_ERROR(WSAENOPROTOOPT, "Bad protocol option.");
720 TEXT_FOR_ERROR(WSAENOTCONN, "Socket is not connected.");
721 TEXT_FOR_ERROR(WSAENOTSOCK, "Socket operation on non-socket.");
722 TEXT_FOR_ERROR(WSAEOPNOTSUPP, "Operation not supported.");
723 TEXT_FOR_ERROR(WSAEPFNOSUPPORT, "Protocol family not supported.");
724 TEXT_FOR_ERROR(WSAEPROCLIM, "Too many processes.");
725 TEXT_FOR_ERROR(WSAEPROTONOSUPPORT, "Protocol not supported.");
726 TEXT_FOR_ERROR(WSAEPROTOTYPE, "Protocol wrong type for socket.");
727 TEXT_FOR_ERROR(WSAESHUTDOWN, "Cannot send after socket shutdown.");
728 TEXT_FOR_ERROR(WSAESOCKTNOSUPPORT, "Socket type not supported.");
729 TEXT_FOR_ERROR(WSAETIMEDOUT, "Connection timed out.");
730 TEXT_FOR_ERROR(WSAEWOULDBLOCK, "Resource temporarily unavailable.");
731 TEXT_FOR_ERROR(WSAHOST_NOT_FOUND, "Host not found.");
732 TEXT_FOR_ERROR(WSANOTINITIALISED, "Successful WSAStartup not yet performed.");
733 TEXT_FOR_ERROR(WSANO_DATA, "Valid name, no data record of requested type.");
734 TEXT_FOR_ERROR(WSANO_RECOVERY, "This is a non-recoverable error.");
735 TEXT_FOR_ERROR(WSASYSNOTREADY, "Network subsystem is unavailable.");
736 TEXT_FOR_ERROR(WSATRY_AGAIN, "Non-authoritative host not found.");
737 TEXT_FOR_ERROR(WSAVERNOTSUPPORTED, "WINSOCK.DLL version out of range.");
738 TEXT_FOR_ERROR(WSAEDISCON, "Graceful shutdown in progress.");
740 * The following error codes are documented in the Microsoft WinSock
741 * reference guide, but don't actually exist.
743 * TEXT_FOR_ERROR(WSA_INVALID_HANDLE, "Specified event object handle is invalid.");
744 * TEXT_FOR_ERROR(WSA_INVALID_PARAMETER, "One or more parameters are invalid.");
745 * TEXT_FOR_ERROR(WSAINVALIDPROCTABLE, "Invalid procedure table from service provider.");
746 * TEXT_FOR_ERROR(WSAINVALIDPROVIDER, "Invalid service provider version number.");
747 * TEXT_FOR_ERROR(WSA_IO_PENDING, "Overlapped operations will complete later.");
748 * TEXT_FOR_ERROR(WSA_IO_INCOMPLETE, "Overlapped I/O event object not in signaled state.");
749 * TEXT_FOR_ERROR(WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available.");
750 * TEXT_FOR_ERROR(WSAPROVIDERFAILEDINIT, "Unable to initialize a service provider.");
751 * TEXT_FOR_ERROR(WSASYSCALLFAILURE, "System call failure.");
752 * TEXT_FOR_ERROR(WSA_OPERATION_ABORTED, "Overlapped operation aborted.");
755 sprintf(tmp_buf, "(error number %d)", errcode);
758 #endif /* def _WIN32 */