1 const char errlog_rcs[] = "$Id: errlog.c,v 1.13 2001/07/13 13:58:58 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.13 2001/07/13 13:58:58 oes
37 * - Added case for LOG_LEVEL_DEANIMATE
38 * - Removed all #ifdef PCRS
40 * Revision 1.12 2001/06/09 10:55:28 jongfoster
41 * Changing BUFSIZ ==> BUFFER_SIZE
43 * Revision 1.11 2001/06/01 18:14:49 jongfoster
44 * Changing the calls to strerr() to check HAVE_STRERR (which is defined
45 * in config.h if appropriate) rather than the NO_STRERR macro.
47 * Revision 1.10 2001/05/29 11:52:21 oes
48 * Conditional compilation of w32_socket_error
50 * Revision 1.9 2001/05/28 16:15:17 jongfoster
51 * Improved reporting of errors under Win32.
53 * Revision 1.8 2001/05/26 17:25:14 jongfoster
54 * Added support for CLF (Common Log Format) and fixed LOG_LEVEL_LOG
56 * Revision 1.7 2001/05/26 15:21:28 jongfoster
57 * Activity animation in Win32 GUI now works even if debug==0
59 * Revision 1.6 2001/05/25 21:55:08 jongfoster
60 * Now cleans up properly on FATAL (removes taskbar icon etc)
62 * Revision 1.5 2001/05/22 18:46:04 oes
64 * - Enabled filtering banners by size rather than URL
65 * by adding patterns that replace all standard banner
66 * sizes with the "Junkbuster" gif to the re_filterfile
68 * - Enabled filtering WebBugs by providing a pattern
69 * which kills all 1x1 images
71 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
72 * which is selected by the (nonstandard and therefore
73 * capital) letter 'U' in the option string.
74 * It causes the quantifiers to be ungreedy by default.
75 * Appending a ? turns back to greedy (!).
77 * - Added a new interceptor ijb-send-banner, which
78 * sends back the "Junkbuster" gif. Without imagelist or
79 * MSIE detection support, or if tinygif = 1, or the
80 * URL isn't recognized as an imageurl, a lame HTML
81 * explanation is sent instead.
83 * - Added new feature, which permits blocking remote
84 * script redirects and firing back a local redirect
86 * The feature is conditionally compiled, i.e. it
87 * can be disabled with --disable-fast-redirects,
88 * plus it must be activated by a "fast-redirects"
89 * line in the config file, has its own log level
90 * and of course wants to be displayed by show-proxy-args
91 * Note: Boy, all the #ifdefs in 1001 locations and
92 * all the fumbling with configure.in and acconfig.h
93 * were *way* more work than the feature itself :-(
95 * - Because a generic redirect template was needed for
96 * this, tinygif = 3 now uses the same.
98 * - Moved GIFs, and other static HTTP response templates
103 * - Removed some >400 CRs again (Jon, you really worked
106 * Revision 1.4 2001/05/21 19:32:54 jongfoster
107 * Added another #ifdef _WIN_CONSOLE
109 * Revision 1.3 2001/05/20 01:11:40 jongfoster
110 * Added support for LOG_LEVEL_FATAL
111 * Renamed LOG_LEVEL_FRC to LOG_LEVEL_FORCE,
112 * and LOG_LEVEL_REF to LOG_LEVEL_RE_FILTER
114 * Revision 1.2 2001/05/17 22:42:01 oes
115 * - Cleaned CRLF's from the sources and related files
116 * - Repaired logging for REF and FRC
118 * Revision 1.1.1.1 2001/05/15 13:58:51 oes
119 * Initial import of version 2.9.3 source tree
122 *********************************************************************/
134 #endif /* ndef _WIN32 */
137 /* #include <pthread.h> */
143 #endif /* ndef _WIN_CONSOLE */
144 #endif /* def _WIN32 */
149 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
153 * LOG_LEVEL_FATAL cannot be turned off. (There are
154 * some exceptional situations where we need to get a
155 * message to the user).
157 #define LOG_LEVEL_MINIMUM LOG_LEVEL_FATAL
159 /* where to log (default: stderr) */
160 static FILE *logfp = NULL;
162 /* where to log (NULL == stderr) */
163 static char * logfilename = NULL;
165 /* logging detail level. */
166 static int debug = (LOG_LEVEL_FATAL | LOG_LEVEL_ERROR | LOG_LEVEL_INFO);
168 /* static functions */
169 static void fatal_error(const char * error_message);
170 static char * w32_socket_strerr(int errcode, char * tmp_buf);
173 /*********************************************************************
175 * Function : fatal_error
177 * Description : Displays a fatal error to standard error (or, on
178 * a WIN32 GUI, to a dialog box), and exits
179 * JunkBuster with status code 1.
182 * 1 : error_message = The error message to display.
184 * Returns : Does not return.
186 *********************************************************************/
187 static void fatal_error(const char * error_message)
189 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
190 MessageBox(g_hwndLogFrame, error_message, "Internet JunkBuster Error",
191 MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
193 /* Cleanup - remove taskbar icon etc. */
196 #else /* if !defined(_WIN32) || defined(_WIN_CONSOLE) */
197 fputs(error_message, stderr);
198 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
204 /*********************************************************************
206 * Function : init_errlog
208 * Description : Initializes the logging module. Must call before
212 * 1 : prog_name = The program name.
213 * 2 : logfname = The logfile name, or NULL for stderr.
214 * 3 : debuglevel = The debugging level.
218 *********************************************************************/
219 void init_error_log(const char *prog_name, const char *logfname, int debuglevel)
223 /* FIXME RACE HAZARD: should start critical section error_log_use here */
225 /* set the logging detail level */
226 debug = debuglevel | LOG_LEVEL_MINIMUM;
228 if ((logfp != NULL) && (logfp != stderr))
234 /* set the designated log file */
237 if( !(fp = fopen(logfname, "a")) )
239 log_error(LOG_LEVEL_FATAL, "init_errlog(): can't open logfile: %s", logfname);
242 /* set logging to be completely unbuffered */
248 log_error(LOG_LEVEL_INFO, "Internet JunkBuster version " VERSION);
249 if (prog_name != NULL)
251 log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
254 /* FIXME RACE HAZARD: should end critical section error_log_use here */
256 } /* init_error_log */
259 /*********************************************************************
261 * Function : log_error
263 * Description : This is the error-reporting and logging function.
266 * 1 : loglevel = the type of message to be logged
267 * 2 : fmt = the main string we want logged, printf-like
268 * 3 : ... = arguments to be inserted in fmt (printf-like).
272 *********************************************************************/
273 void log_error(int loglevel, char *fmt, ...)
276 char outbuf[BUFFER_SIZE];
279 long this_thread = 1; /* was: pthread_t this_thread;*/
281 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
283 * Irrespective of debug setting, a GET/POST/CONNECT makes
284 * the taskbar icon animate. (There is an option to disable
285 * this but checking that is handled inside LogShowActivity()).
287 if (loglevel == LOG_LEVEL_GPC)
291 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
293 /* verify if loglevel applies to current settings and bail out if negative */
294 if ((loglevel & debug) == 0)
299 /* FIXME get current thread id */
300 /* this_thread = (long)pthread_self(); */
304 case LOG_LEVEL_ERROR:
305 outc = sprintf(outbuf, "IJB(%d) Error: ", this_thread);
307 case LOG_LEVEL_FATAL:
308 outc = sprintf(outbuf, "IJB(%d) Fatal error: ", this_thread);
311 outc = sprintf(outbuf, "IJB(%d) Request: ", this_thread);
313 case LOG_LEVEL_CONNECT:
314 outc = sprintf(outbuf, "IJB(%d) Connect: ", this_thread);
317 outc = sprintf(outbuf, "IJB(%d) Writing: ", this_thread);
319 case LOG_LEVEL_HEADER:
320 outc = sprintf(outbuf, "IJB(%d) Header: ", this_thread);
323 outc = sprintf(outbuf, "IJB(%d) Info: ", this_thread);
325 case LOG_LEVEL_RE_FILTER:
326 outc = sprintf(outbuf, "IJB(%d) Re-Filter: ", this_thread);
329 case LOG_LEVEL_FORCE:
330 outc = sprintf(outbuf, "IJB(%d) Force: ", this_thread);
332 #endif /* def FORCE_LOAD */
333 #ifdef FAST_REDIRECTS
334 case LOG_LEVEL_REDIRECTS:
335 outc = sprintf(outbuf, "IJB(%d) Redirect: ", this_thread);
337 #endif /* def FAST_REDIRECTS */
338 case LOG_LEVEL_DEANIMATE:
339 outc = sprintf(outbuf, "IJB(%d) Gif-Deanimate: ", this_thread);
346 case LOG_LEVEL_POPUPS:
347 outc = sprintf(outbuf, "IJB(%d) Kill-Popups: ", this_thread);
349 #endif /* def KILLPOPUPS */
351 outc = sprintf(outbuf, "IJB(%d) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
355 /* get ready to scan var. args. */
358 /* build formatted message from fmt and var-args */
359 while ((*src) && (outc < BUFFER_SIZE-2))
361 char tempbuf[BUFFER_SIZE];
380 outbuf[outc++] = '%';
383 ival = va_arg( ap, int );
385 outc += sprintf(tempbuf, "%d", ival);
386 if (outc < BUFFER_SIZE-1)
388 strcpy(outbuf + oldoutc, tempbuf);
392 outbuf[oldoutc] = '\0';
396 uval = va_arg( ap, unsigned );
398 outc += sprintf(tempbuf, "%u", uval);
399 if (outc < BUFFER_SIZE-1)
401 strcpy(outbuf + oldoutc, tempbuf);
405 outbuf[oldoutc] = '\0';
409 /* this is a modifier that must be followed by u or d */
413 lval = va_arg( ap, long );
415 outc += sprintf(tempbuf, "%ld", lval);
419 ulval = va_arg( ap, unsigned long );
421 outc += sprintf(tempbuf, "%lu", ulval);
426 sprintf(outbuf, "IJB(%d) Error: log_error(): Bad format string:\n"
428 "Exiting.", this_thread, fmt);
429 /* FIXME RACE HAZARD: should start critical section error_log_use here */
434 fputs(outbuf, logfp);
435 /* FIXME RACE HAZARD: should end critical section error_log_use here */
440 if (outc < BUFFER_SIZE-1)
442 strcpy(outbuf + oldoutc, tempbuf);
446 outbuf[oldoutc] = '\0';
451 * Note that char paramaters are converted to int, so we need to
452 * pass "int" to va_arg. (See K&R, 2nd ed, section A7.3.2, page 202)
454 outbuf[outc++] = (char) va_arg( ap, int );
457 sval = va_arg( ap, char * );
459 outc += strlen(sval);
460 if (outc < BUFFER_SIZE-1)
462 strcpy(outbuf + oldoutc, sval);
466 outbuf[oldoutc] = '\0';
470 /* Non-standard: Print a counted string. Takes 2 parameters:
471 * int length, const char * string
473 ival = va_arg( ap, int );
474 sval = va_arg( ap, char * );
481 if (outc < BUFFER_SIZE-1)
483 memcpy(outbuf + oldoutc, sval, ival);
487 outbuf[oldoutc] = '\0';
491 /* Non-standard: Print error code from errno */
493 ival = WSAGetLastError();
494 sval = w32_socket_strerr(ival, tempbuf);
495 #else /* ifndef _WIN32 */
498 sval = strerror(ival);
499 #else /* ifndef HAVE_STRERROR */
501 #endif /* ndef HAVE_STRERROR */
504 sprintf(tempbuf, "(errno = %d)", ival);
507 #endif /* ndef _WIN32 */
509 outc += strlen(sval);
510 if (outc < BUFFER_SIZE-1)
512 strcpy(outbuf + oldoutc, sval);
516 outbuf[oldoutc] = '\0';
520 /* Non-standard: Print a Common Log File timestamp */
523 * Write timestamp into tempbuf.
525 * Complex because not all OSs have tm_gmtoff or
526 * the %z field in strftime()
533 gmt = *gmtime (&now);
534 tm_now = localtime (&now);
535 days = tm_now->tm_yday - gmt.tm_yday;
536 hrs = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + tm_now->tm_hour - gmt.tm_hour);
537 mins = hrs * 60 + tm_now->tm_min - gmt.tm_min;
538 strftime (tempbuf, BUFFER_SIZE-6, "%d/%b/%Y:%H:%M:%S ", tm_now);
539 sprintf (tempbuf + strlen(tempbuf), "%+03d%02d", mins / 60, abs(mins) % 60);
542 outc += strlen(tempbuf);
543 if (outc < BUFFER_SIZE-1)
545 strcpy(outbuf + oldoutc, tempbuf);
549 outbuf[oldoutc] = '\0';
553 sprintf(outbuf, "IJB(%d) Error: log_error(): Bad format string:\n"
555 "Exiting.", this_thread, fmt);
556 /* FIXME RACE HAZARD: should start critical section error_log_use here */
561 fputs(outbuf, logfp);
562 /* FIXME RACE HAZARD: should end critical section error_log_use here */
571 /* done with var. args */
574 if (outc >= BUFFER_SIZE-2)
576 /* insufficient room for newline and trailing null. */
578 static const char warning[] = "... [too long, truncated]\n";
580 if (outc < BUFFER_SIZE)
582 /* Need to add terminating null in this case. */
586 /* Truncate output */
587 outbuf[BUFFER_SIZE - sizeof(warning)] = '\0';
590 strcat(outbuf, warning);
594 /* Add terminating newline and null */
595 outbuf[outc++] = '\n';
599 /* FIXME RACE HAZARD: should start critical section error_log_use here */
601 /* deal with glibc stupidity - it won't let you initialize logfp */
607 fputs(outbuf, logfp);
609 if (loglevel == LOG_LEVEL_FATAL)
615 /* FIXME RACE HAZARD: should end critical section error_log_use here */
617 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
618 /* Write to display */
619 LogPutString(outbuf);
620 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
626 /*********************************************************************
628 * Function : w32_socket_strerr
630 * Description : Translate the return value from WSAGetLastError()
634 * 1 : errcode = The return value from WSAGetLastError().
635 * 2 : tmp_buf = A temporary buffer that might be used to
638 * Returns : String representing the error code. This may be
639 * a global string constant or a string stored in
642 *********************************************************************/
643 static char * w32_socket_strerr(int errcode, char * tmp_buf)
645 #define TEXT_FOR_ERROR(code,text) \
646 if (errcode == code) \
648 return #code " - " text; \
651 TEXT_FOR_ERROR(WSAEACCES, "Permission denied")
652 TEXT_FOR_ERROR(WSAEADDRINUSE, "Address already in use.")
653 TEXT_FOR_ERROR(WSAEADDRNOTAVAIL, "Cannot assign requested address.");
654 TEXT_FOR_ERROR(WSAEAFNOSUPPORT, "Address family not supported by protocol family.");
655 TEXT_FOR_ERROR(WSAEALREADY, "Operation already in progress.");
656 TEXT_FOR_ERROR(WSAECONNABORTED, "Software caused connection abort.");
657 TEXT_FOR_ERROR(WSAECONNREFUSED, "Connection refused.");
658 TEXT_FOR_ERROR(WSAECONNRESET, "Connection reset by peer.");
659 TEXT_FOR_ERROR(WSAEDESTADDRREQ, "Destination address required.");
660 TEXT_FOR_ERROR(WSAEFAULT, "Bad address.");
661 TEXT_FOR_ERROR(WSAEHOSTDOWN, "Host is down.");
662 TEXT_FOR_ERROR(WSAEHOSTUNREACH, "No route to host.");
663 TEXT_FOR_ERROR(WSAEINPROGRESS, "Operation now in progress.");
664 TEXT_FOR_ERROR(WSAEINTR, "Interrupted function call.");
665 TEXT_FOR_ERROR(WSAEINVAL, "Invalid argument.");
666 TEXT_FOR_ERROR(WSAEISCONN, "Socket is already connected.");
667 TEXT_FOR_ERROR(WSAEMFILE, "Too many open sockets.");
668 TEXT_FOR_ERROR(WSAEMSGSIZE, "Message too long.");
669 TEXT_FOR_ERROR(WSAENETDOWN, "Network is down.");
670 TEXT_FOR_ERROR(WSAENETRESET, "Network dropped connection on reset.");
671 TEXT_FOR_ERROR(WSAENETUNREACH, "Network is unreachable.");
672 TEXT_FOR_ERROR(WSAENOBUFS, "No buffer space available.");
673 TEXT_FOR_ERROR(WSAENOPROTOOPT, "Bad protocol option.");
674 TEXT_FOR_ERROR(WSAENOTCONN, "Socket is not connected.");
675 TEXT_FOR_ERROR(WSAENOTSOCK, "Socket operation on non-socket.");
676 TEXT_FOR_ERROR(WSAEOPNOTSUPP, "Operation not supported.");
677 TEXT_FOR_ERROR(WSAEPFNOSUPPORT, "Protocol family not supported.");
678 TEXT_FOR_ERROR(WSAEPROCLIM, "Too many processes.");
679 TEXT_FOR_ERROR(WSAEPROTONOSUPPORT, "Protocol not supported.");
680 TEXT_FOR_ERROR(WSAEPROTOTYPE, "Protocol wrong type for socket.");
681 TEXT_FOR_ERROR(WSAESHUTDOWN, "Cannot send after socket shutdown.");
682 TEXT_FOR_ERROR(WSAESOCKTNOSUPPORT, "Socket type not supported.");
683 TEXT_FOR_ERROR(WSAETIMEDOUT, "Connection timed out.");
684 TEXT_FOR_ERROR(WSAEWOULDBLOCK, "Resource temporarily unavailable.");
685 TEXT_FOR_ERROR(WSAHOST_NOT_FOUND, "Host not found.");
686 TEXT_FOR_ERROR(WSANOTINITIALISED, "Successful WSAStartup not yet performed.");
687 TEXT_FOR_ERROR(WSANO_DATA, "Valid name, no data record of requested type.");
688 TEXT_FOR_ERROR(WSANO_RECOVERY, "This is a non-recoverable error.");
689 TEXT_FOR_ERROR(WSASYSNOTREADY, "Network subsystem is unavailable.");
690 TEXT_FOR_ERROR(WSATRY_AGAIN, "Non-authoritative host not found.");
691 TEXT_FOR_ERROR(WSAVERNOTSUPPORTED, "WINSOCK.DLL version out of range.");
692 TEXT_FOR_ERROR(WSAEDISCON, "Graceful shutdown in progress.");
694 * The following error codes are documented in the Microsoft WinSock
695 * reference guide, but don't actually exist.
697 * TEXT_FOR_ERROR(WSA_INVALID_HANDLE, "Specified event object handle is invalid.");
698 * TEXT_FOR_ERROR(WSA_INVALID_PARAMETER, "One or more parameters are invalid.");
699 * TEXT_FOR_ERROR(WSAINVALIDPROCTABLE, "Invalid procedure table from service provider.");
700 * TEXT_FOR_ERROR(WSAINVALIDPROVIDER, "Invalid service provider version number.");
701 * TEXT_FOR_ERROR(WSA_IO_PENDING, "Overlapped operations will complete later.");
702 * TEXT_FOR_ERROR(WSA_IO_INCOMPLETE, "Overlapped I/O event object not in signaled state.");
703 * TEXT_FOR_ERROR(WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available.");
704 * TEXT_FOR_ERROR(WSAPROVIDERFAILEDINIT, "Unable to initialize a service provider.");
705 * TEXT_FOR_ERROR(WSASYSCALLFAILURE, "System call failure.");
706 * TEXT_FOR_ERROR(WSA_OPERATION_ABORTED, "Overlapped operation aborted.");
709 sprintf(tmp_buf, "(error number %d)", errcode);
712 #endif /* def _WIN32 */