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