ssl/libcrypto/src/crypto/bio/bss_log.c
changeset 31 ce057bb09d0b
parent 0 e4d67989cc36
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 /* crypto/bio/bss_log.c */
       
     2 /* ====================================================================
       
     3  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
       
     4  *
       
     5  * Redistribution and use in source and binary forms, with or without
       
     6  * modification, are permitted provided that the following conditions
       
     7  * are met:
       
     8  *
       
     9  * 1. Redistributions of source code must retain the above copyright
       
    10  *    notice, this list of conditions and the following disclaimer. 
       
    11  *
       
    12  * 2. Redistributions in binary form must reproduce the above copyright
       
    13  *    notice, this list of conditions and the following disclaimer in
       
    14  *    the documentation and/or other materials provided with the
       
    15  *    distribution.
       
    16  *
       
    17  * 3. All advertising materials mentioning features or use of this
       
    18  *    software must display the following acknowledgment:
       
    19  *    "This product includes software developed by the OpenSSL Project
       
    20  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
       
    21  *
       
    22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
       
    23  *    endorse or promote products derived from this software without
       
    24  *    prior written permission. For written permission, please contact
       
    25  *    licensing@OpenSSL.org.
       
    26  *
       
    27  * 5. Products derived from this software may not be called "OpenSSL"
       
    28  *    nor may "OpenSSL" appear in their names without prior written
       
    29  *    permission of the OpenSSL Project.
       
    30  *
       
    31  * 6. Redistributions of any form whatsoever must retain the following
       
    32  *    acknowledgment:
       
    33  *    "This product includes software developed by the OpenSSL Project
       
    34  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
       
    35  *
       
    36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
       
    37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
       
    40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
       
    42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
       
    43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
    44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
       
    45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
       
    47  * OF THE POSSIBILITY OF SUCH DAMAGE.
       
    48  * ====================================================================
       
    49  *
       
    50  * This product includes cryptographic software written by Eric Young
       
    51  * (eay@cryptsoft.com).  This product includes software written by Tim
       
    52  * Hudson (tjh@cryptsoft.com).
       
    53  *
       
    54  */
       
    55 /*
       
    56  © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
    57  */
       
    58 /*
       
    59 	Why BIO_s_log?
       
    60 
       
    61 	BIO_s_log is useful for system daemons (or services under NT).
       
    62 	It is one-way BIO, it sends all stuff to syslogd (on system that
       
    63 	commonly use that), or event log (on NT), or OPCOM (on OpenVMS).
       
    64 
       
    65 */
       
    66 
       
    67 
       
    68 #include <stdio.h>
       
    69 #include <errno.h>
       
    70 
       
    71 #include "cryptlib.h"
       
    72 
       
    73 #if defined(OPENSSL_SYS_WINCE)
       
    74 #elif defined(OPENSSL_SYS_WIN32)
       
    75 #  include <process.h>
       
    76 #elif defined(OPENSSL_SYS_VMS)
       
    77 #  include <opcdef.h>
       
    78 #  include <descrip.h>
       
    79 #  include <lib$routines.h>
       
    80 #  include <starlet.h>
       
    81 #elif defined(__ultrix)
       
    82 #  include <sys/syslog.h>
       
    83 #elif defined(OPENSSL_SYS_NETWARE)
       
    84 #  define NO_SYSLOG
       
    85 #elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG)
       
    86 #  include <syslog.h>
       
    87 #endif
       
    88 
       
    89 #include <openssl/buffer.h>
       
    90 #include <openssl/err.h>
       
    91 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
       
    92 #include "libcrypto_wsd_macros.h"
       
    93 #include "libcrypto_wsd.h"
       
    94 #endif
       
    95 
       
    96 
       
    97 #ifndef NO_SYSLOG
       
    98 
       
    99 #if defined(OPENSSL_SYS_WIN32)
       
   100 #define LOG_EMERG	0
       
   101 #define LOG_ALERT	1
       
   102 #define LOG_CRIT	2
       
   103 #define LOG_ERR		3
       
   104 #define LOG_WARNING	4
       
   105 #define LOG_NOTICE	5
       
   106 #define LOG_INFO	6
       
   107 #define LOG_DEBUG	7
       
   108 
       
   109 #define LOG_DAEMON	(3<<3)
       
   110 #elif defined(OPENSSL_SYS_VMS)
       
   111 /* On VMS, we don't really care about these, but we need them to compile */
       
   112 #define LOG_EMERG	0
       
   113 #define LOG_ALERT	1
       
   114 #define LOG_CRIT	2
       
   115 #define LOG_ERR		3
       
   116 #define LOG_WARNING	4
       
   117 #define LOG_NOTICE	5
       
   118 #define LOG_INFO	6
       
   119 #define LOG_DEBUG	7
       
   120 
       
   121 #define LOG_DAEMON	OPC$M_NM_NTWORK
       
   122 #endif
       
   123 
       
   124 static int MS_CALLBACK slg_write(BIO *h, const char *buf, int num);
       
   125 static int MS_CALLBACK slg_puts(BIO *h, const char *str);
       
   126 static long MS_CALLBACK slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
       
   127 static int MS_CALLBACK slg_new(BIO *h);
       
   128 static int MS_CALLBACK slg_free(BIO *data);
       
   129 static void xopenlog(BIO* bp, char* name, int level);
       
   130 static void xsyslog(BIO* bp, int priority, const char* string);
       
   131 static void xcloselog(BIO* bp);
       
   132 #ifdef OPENSSL_SYS_WIN32
       
   133 LONG	(WINAPI *go_for_advapi)()	= RegOpenKeyEx;
       
   134 HANDLE	(WINAPI *register_event_source)()	= NULL;
       
   135 BOOL	(WINAPI *deregister_event_source)()	= NULL;
       
   136 BOOL	(WINAPI *report_event)()	= NULL;
       
   137 #define DL_PROC(m,f)	(GetProcAddress( m, f ))
       
   138 #ifdef UNICODE
       
   139 #define DL_PROC_X(m,f) DL_PROC( m, f "W" )
       
   140 #else
       
   141 #define DL_PROC_X(m,f) DL_PROC( m, f "A" )
       
   142 #endif
       
   143 #endif
       
   144 
       
   145 #ifndef EMULATOR
       
   146 static BIO_METHOD methods_slg=
       
   147 	{
       
   148 	BIO_TYPE_MEM,"syslog",
       
   149 	slg_write,
       
   150 	NULL,
       
   151 	slg_puts,
       
   152 	NULL,
       
   153 	slg_ctrl,
       
   154 	slg_new,
       
   155 	slg_free,
       
   156 	NULL,
       
   157 	};
       
   158 #else
       
   159 
       
   160 GET_STATIC_VAR_FROM_TLS(methods_slg,bss_log,BIO_METHOD)
       
   161 #define methods_slg (*GET_WSD_VAR_NAME(methods_slg,bss_log,s)())
       
   162 const BIO_METHOD temp_s_methods_slg=
       
   163 	{
       
   164 	BIO_TYPE_MEM,"syslog",
       
   165 	slg_write,
       
   166 	NULL,
       
   167 	slg_puts,
       
   168 	NULL,
       
   169 	slg_ctrl,
       
   170 	slg_new,
       
   171 	slg_free,
       
   172 	NULL,
       
   173 	};
       
   174 
       
   175 #endif	
       
   176 
       
   177 EXPORT_C BIO_METHOD *BIO_s_log(void)
       
   178 	{
       
   179 	return(&methods_slg);
       
   180 	}
       
   181 
       
   182 static int MS_CALLBACK slg_new(BIO *bi)
       
   183 	{
       
   184 	bi->init=1;
       
   185 	bi->num=0;
       
   186 	bi->ptr=NULL;
       
   187 	xopenlog(bi, "application", LOG_DAEMON);
       
   188 	return(1);
       
   189 	}
       
   190 
       
   191 static int MS_CALLBACK slg_free(BIO *a)
       
   192 	{
       
   193 	if (a == NULL) return(0);
       
   194 	xcloselog(a);
       
   195 	return(1);
       
   196 	}
       
   197 	
       
   198 static int MS_CALLBACK slg_write(BIO *b, const char *in, int inl)
       
   199 	{
       
   200 	int ret= inl;
       
   201 	char* buf;
       
   202 	char* pp;
       
   203 	int priority, i;
       
   204 	static struct
       
   205 		{
       
   206 		int strl;
       
   207 		char str[10];
       
   208 		int log_level;
       
   209 		}
       
   210 	mapping[] =
       
   211 		{
       
   212 		{ 6, "PANIC ", LOG_EMERG },
       
   213 		{ 6, "EMERG ", LOG_EMERG },
       
   214 		{ 4, "EMR ", LOG_EMERG },
       
   215 		{ 6, "ALERT ", LOG_ALERT },
       
   216 		{ 4, "ALR ", LOG_ALERT },
       
   217 		{ 5, "CRIT ", LOG_CRIT },
       
   218 		{ 4, "CRI ", LOG_CRIT },
       
   219 		{ 6, "ERROR ", LOG_ERR },
       
   220 		{ 4, "ERR ", LOG_ERR },
       
   221 		{ 8, "WARNING ", LOG_WARNING },
       
   222 		{ 5, "WARN ", LOG_WARNING },
       
   223 		{ 4, "WAR ", LOG_WARNING },
       
   224 		{ 7, "NOTICE ", LOG_NOTICE },
       
   225 		{ 5, "NOTE ", LOG_NOTICE },
       
   226 		{ 4, "NOT ", LOG_NOTICE },
       
   227 		{ 5, "INFO ", LOG_INFO },
       
   228 		{ 4, "INF ", LOG_INFO },
       
   229 		{ 6, "DEBUG ", LOG_DEBUG },
       
   230 		{ 4, "DBG ", LOG_DEBUG },
       
   231 		{ 0, "", LOG_ERR } /* The default */
       
   232 		};
       
   233 
       
   234 	if((buf= (char *)OPENSSL_malloc(inl+ 1)) == NULL){
       
   235 		return(0);
       
   236 	}
       
   237 	strncpy(buf, in, inl);
       
   238 	buf[inl]= '\0';
       
   239 
       
   240 	i = 0;
       
   241 	while(strncmp(buf, mapping[i].str, mapping[i].strl) != 0) i++;
       
   242 	priority = mapping[i].log_level;
       
   243 	pp = buf + mapping[i].strl;
       
   244 
       
   245 	xsyslog(b, priority, pp);
       
   246 
       
   247 	OPENSSL_free(buf);
       
   248 	return(ret);
       
   249 	}
       
   250 
       
   251 static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, void *ptr)
       
   252 	{
       
   253 	switch (cmd)
       
   254 		{
       
   255 	case BIO_CTRL_SET:
       
   256 		xcloselog(b);
       
   257 		xopenlog(b, ptr, num);
       
   258 		break;
       
   259 	default:
       
   260 		break;
       
   261 		}
       
   262 	return(0);
       
   263 	}
       
   264 
       
   265 static int MS_CALLBACK slg_puts(BIO *bp, const char *str)
       
   266 	{
       
   267 	int n,ret;
       
   268 
       
   269 	n=strlen(str);
       
   270 	ret=slg_write(bp,str,n);
       
   271 	return(ret);
       
   272 	}
       
   273 
       
   274 #if defined(OPENSSL_SYS_WIN32)
       
   275 
       
   276 static void xopenlog(BIO* bp, char* name, int level)
       
   277 {
       
   278 	if ( !register_event_source )
       
   279 		{
       
   280 		HANDLE	advapi;
       
   281 		if ( !(advapi = GetModuleHandle("advapi32")) )
       
   282 			return;
       
   283 		register_event_source = (HANDLE (WINAPI *)())DL_PROC_X(advapi,
       
   284 			"RegisterEventSource" );
       
   285 		deregister_event_source = (BOOL (WINAPI *)())DL_PROC(advapi,
       
   286 			"DeregisterEventSource");
       
   287 		report_event = (BOOL (WINAPI *)())DL_PROC_X(advapi,
       
   288 			"ReportEvent" );
       
   289 		if ( !(register_event_source && deregister_event_source &&
       
   290 				report_event) )
       
   291 			{
       
   292 			register_event_source = NULL;
       
   293 			deregister_event_source = NULL;
       
   294 			report_event = NULL;
       
   295 			return;
       
   296 			}
       
   297 		}
       
   298 	bp->ptr= (char *)register_event_source(NULL, name);
       
   299 }
       
   300 
       
   301 static void xsyslog(BIO *bp, int priority, const char *string)
       
   302 {
       
   303 	LPCSTR lpszStrings[2];
       
   304 	WORD evtype= EVENTLOG_ERROR_TYPE;
       
   305 	int pid = _getpid();
       
   306 	char pidbuf[DECIMAL_SIZE(pid)+4];
       
   307 
       
   308 	switch (priority)
       
   309 		{
       
   310 	case LOG_EMERG:
       
   311 	case LOG_ALERT:
       
   312 	case LOG_CRIT:
       
   313 	case LOG_ERR:
       
   314 		evtype = EVENTLOG_ERROR_TYPE;
       
   315 		break;
       
   316 	case LOG_WARNING:
       
   317 		evtype = EVENTLOG_WARNING_TYPE;
       
   318 		break;
       
   319 	case LOG_NOTICE:
       
   320 	case LOG_INFO:
       
   321 	case LOG_DEBUG:
       
   322 		evtype = EVENTLOG_INFORMATION_TYPE;
       
   323 		break;
       
   324 	default:		/* Should never happen, but set it
       
   325 				   as error anyway. */
       
   326 		evtype = EVENTLOG_ERROR_TYPE;
       
   327 		break;
       
   328 		}
       
   329 
       
   330 	sprintf(pidbuf, "[%d] ", pid);
       
   331 	lpszStrings[0] = pidbuf;
       
   332 	lpszStrings[1] = string;
       
   333 
       
   334 	if(report_event && bp->ptr)
       
   335 		report_event(bp->ptr, evtype, 0, 1024, NULL, 2, 0,
       
   336 				lpszStrings, NULL);
       
   337 }
       
   338 	
       
   339 static void xcloselog(BIO* bp)
       
   340 {
       
   341 	if(deregister_event_source && bp->ptr)
       
   342 		deregister_event_source((HANDLE)(bp->ptr));
       
   343 	bp->ptr= NULL;
       
   344 }
       
   345 
       
   346 #elif defined(OPENSSL_SYS_VMS)
       
   347 
       
   348 static int VMS_OPC_target = LOG_DAEMON;
       
   349 
       
   350 static void xopenlog(BIO* bp, char* name, int level)
       
   351 {
       
   352 	VMS_OPC_target = level; 
       
   353 }
       
   354 
       
   355 static void xsyslog(BIO *bp, int priority, const char *string)
       
   356 {
       
   357 	struct dsc$descriptor_s opc_dsc;
       
   358 	struct opcdef *opcdef_p;
       
   359 #ifndef SYMBAIN	
       
   360 	char buf[10240];
       
   361 #else
       
   362   char buf[100];
       
   363 #endif	
       
   364 	unsigned int len;
       
   365         struct dsc$descriptor_s buf_dsc;
       
   366 	$DESCRIPTOR(fao_cmd, "!AZ: !AZ");
       
   367 	char *priority_tag;
       
   368 
       
   369 	switch (priority)
       
   370 	  {
       
   371 	  case LOG_EMERG: priority_tag = "Emergency"; break;
       
   372 	  case LOG_ALERT: priority_tag = "Alert"; break;
       
   373 	  case LOG_CRIT: priority_tag = "Critical"; break;
       
   374 	  case LOG_ERR: priority_tag = "Error"; break;
       
   375 	  case LOG_WARNING: priority_tag = "Warning"; break;
       
   376 	  case LOG_NOTICE: priority_tag = "Notice"; break;
       
   377 	  case LOG_INFO: priority_tag = "Info"; break;
       
   378 	  case LOG_DEBUG: priority_tag = "DEBUG"; break;
       
   379 	  }
       
   380 
       
   381 	buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
       
   382 	buf_dsc.dsc$b_class = DSC$K_CLASS_S;
       
   383 	buf_dsc.dsc$a_pointer = buf;
       
   384 	buf_dsc.dsc$w_length = sizeof(buf) - 1;
       
   385 
       
   386 	lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
       
   387 
       
   388 	/* we know there's an 8 byte header.  That's documented */
       
   389 	opcdef_p = (struct opcdef *) OPENSSL_malloc(8 + len);
       
   390 	opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
       
   391 	memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
       
   392 	opcdef_p->opc$l_ms_rqstid = 0;
       
   393 	memcpy(&opcdef_p->opc$l_ms_text, buf, len);
       
   394 
       
   395 	opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
       
   396 	opc_dsc.dsc$b_class = DSC$K_CLASS_S;
       
   397 	opc_dsc.dsc$a_pointer = (char *)opcdef_p;
       
   398 	opc_dsc.dsc$w_length = len + 8;
       
   399 
       
   400 	sys$sndopr(opc_dsc, 0);
       
   401 
       
   402 	OPENSSL_free(opcdef_p);
       
   403 }
       
   404 
       
   405 static void xcloselog(BIO* bp)
       
   406 {
       
   407 }
       
   408 
       
   409 #else /* Unix/Watt32 */
       
   410 
       
   411 static void xopenlog(BIO* bp, char* name, int level)
       
   412 {
       
   413 #ifdef WATT32   /* djgpp/DOS */
       
   414 	openlog(name, LOG_PID|LOG_CONS|LOG_NDELAY, level);
       
   415 #else
       
   416 	openlog(name, LOG_PID|LOG_CONS, level);
       
   417 #endif
       
   418 }
       
   419 
       
   420 static void xsyslog(BIO *bp, int priority, const char *string)
       
   421 {
       
   422 	syslog(priority, "%s", string);
       
   423 }
       
   424 
       
   425 static void xcloselog(BIO* bp)
       
   426 {
       
   427 	closelog();
       
   428 }
       
   429 
       
   430 #endif /* Unix */
       
   431 
       
   432 #endif /* NO_SYSLOG */