bss_log.c revision 59191
186222Swollman/* crypto/bio/bss_log.c */
22742Swollman/* ====================================================================
352770Sgrog * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
42742Swollman *
52742Swollman * Redistribution and use in source and binary forms, with or without
62742Swollman * modification, are permitted provided that the following conditions
72742Swollman * are met:
82742Swollman *
92742Swollman * 1. Redistributions of source code must retain the above copyright
1086222Swollman *    notice, this list of conditions and the following disclaimer.
1186222Swollman *
122742Swollman * 2. Redistributions in binary form must reproduce the above copyright
1358787Sru *    notice, this list of conditions and the following disclaimer in
142742Swollman *    the documentation and/or other materials provided with the
152742Swollman *    distribution.
162742Swollman *
172742Swollman * 3. All advertising materials mentioning features or use of this
182742Swollman *    software must display the following acknowledgment:
192742Swollman *    "This product includes software developed by the OpenSSL Project
2058787Sru *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2158787Sru *
2258787Sru * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
232742Swollman *    endorse or promote products derived from this software without
242742Swollman *    prior written permission. For written permission, please contact
259908Swollman *    licensing@OpenSSL.org.
262742Swollman *
2730711Swollman * 5. Products derived from this software may not be called "OpenSSL"
282742Swollman *    nor may "OpenSSL" appear in their names without prior written
299908Swollman *    permission of the OpenSSL Project.
302742Swollman *
3158787Sru * 6. Redistributions of any form whatsoever must retain the following
3258787Sru *    acknowledgment:
3314343Swollman *    "This product includes software developed by the OpenSSL Project
3414343Swollman *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3514343Swollman *
3614343Swollman * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
3714343Swollman * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
382742Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
399908Swollman * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4020094Swollman * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4120094Swollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4220094Swollman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4320094Swollman * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4420094Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4520094Swollman * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4620094Swollman * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4720094Swollman * OF THE POSSIBILITY OF SUCH DAMAGE.
4820094Swollman * ====================================================================
4920094Swollman *
5020094Swollman * This product includes cryptographic software written by Eric Young
5120094Swollman * (eay@cryptsoft.com).  This product includes software written by Tim
5220094Swollman * Hudson (tjh@cryptsoft.com).
5358787Sru *
5458787Sru */
5521217Swollman
5621217Swollman/*
5758787Sru	Why BIO_s_log?
5858787Sru
592742Swollman	BIO_s_log is useful for system daemons (or services under NT).
6058787Sru	It is one-way BIO, it sends all stuff to syslogd (on system that
6121217Swollman	commonly use that), or event log (on NT), or OPCOM (on OpenVMS).
6220094Swollman
6358787Sru*/
6458787Sru
6520094Swollman
662742Swollman#include <stdio.h>
679908Swollman#include <errno.h>
682742Swollman
6914343Swollman#if defined(WIN32)
7014343Swollman#  include <process.h>
7114343Swollman#elif defined(VMS) || defined(__VMS)
7214343Swollman#  include <opcdef.h>
7314343Swollman#  include <descrip.h>
7414343Swollman#  include <lib$routines.h>
7564499Swollman#  include <starlet.h>
7664499Swollman#elif defined(__ultrix)
7764499Swollman#  include <sys/syslog.h>
7864499Swollman#elif !defined(MSDOS) /* Unix */
7964499Swollman#  include <syslog.h>
8014343Swollman#endif
812742Swollman
822742Swollman#include "cryptlib.h"
832742Swollman#include <openssl/buffer.h>
8458787Sru#include <openssl/err.h>
852742Swollman
862742Swollman#ifndef NO_SYSLOG
879908Swollman
882742Swollman#if defined(WIN32)
8958787Sru#define LOG_EMERG	0
9058787Sru#define LOG_ALERT	1
9114343Swollman#define LOG_CRIT	2
9214343Swollman#define LOG_ERR		3
9358787Sru#define LOG_WARNING	4
9414343Swollman#define LOG_NOTICE	5
9514343Swollman#define LOG_INFO	6
9614343Swollman#define LOG_DEBUG	7
9758787Sru
9814343Swollman#define LOG_DAEMON	(3<<3)
9958787Sru#elif defined(VMS)
10058787Sru/* On VMS, we don't really care about these, but we need them to compile */
10158787Sru#define LOG_EMERG	0
10214343Swollman#define LOG_ALERT	1
10358787Sru#define LOG_CRIT	2
10458787Sru#define LOG_ERR		3
1052742Swollman#define LOG_WARNING	4
1062742Swollman#define LOG_NOTICE	5
10758787Sru#define LOG_INFO	6
10858787Sru#define LOG_DEBUG	7
10958787Sru
1102742Swollman#define LOG_DAEMON	OPC$M_NM_NTWORK
1112742Swollman#endif
1129908Swollman
1132742Swollmanstatic int MS_CALLBACK slg_write(BIO *h,char *buf,int num);
11414343Swollmanstatic int MS_CALLBACK slg_puts(BIO *h,char *str);
11558787Srustatic long MS_CALLBACK slg_ctrl(BIO *h,int cmd,long arg1,char *arg2);
11614343Swollmanstatic int MS_CALLBACK slg_new(BIO *h);
11714343Swollmanstatic int MS_CALLBACK slg_free(BIO *data);
11858787Srustatic void xopenlog(BIO* bp, const char* name, int level);
11958787Srustatic void xsyslog(BIO* bp, int priority, const char* string);
12014343Swollmanstatic void xcloselog(BIO* bp);
12114343Swollman
12258787Srustatic BIO_METHOD methods_slg=
12343543Swollman	{
1242742Swollman	BIO_TYPE_MEM,"syslog",
1252742Swollman	slg_write,
12658787Sru	NULL,
1272742Swollman	slg_puts,
1282742Swollman	NULL,
1299908Swollman	slg_ctrl,
1302742Swollman	slg_new,
13114343Swollman	slg_free,
13214343Swollman	NULL,
13314343Swollman	};
13414343Swollman
13514343SwollmanBIO_METHOD *BIO_s_log(void)
13614343Swollman	{
13714343Swollman	return(&methods_slg);
13843543Swollman	}
13914343Swollman
14014343Swollmanstatic int MS_CALLBACK slg_new(BIO *bi)
14158787Sru	{
14243543Swollman	bi->init=1;
1432742Swollman	bi->num=0;
1442742Swollman	bi->ptr=NULL;
14558787Sru	xopenlog(bi, "application", LOG_DAEMON);
1462742Swollman	return(1);
1472742Swollman	}
1482742Swollman
1492742Swollmanstatic int MS_CALLBACK slg_free(BIO *a)
15058787Sru	{
15158787Sru	if (a == NULL) return(0);
15258787Sru	xcloselog(a);
1538029Swollman	return(1);
15414343Swollman	}
15514343Swollman
15675267Swollmanstatic int MS_CALLBACK slg_write(BIO *b, char *in, int inl)
15775267Swollman	{
15875267Swollman	int ret= inl;
15975267Swollman	char* buf= in;
16075267Swollman	char* pp;
16175267Swollman	int priority;
16275267Swollman
16375267Swollman	if((buf= (char *)Malloc(inl+ 1)) == NULL){
16475267Swollman		return(0);
16575267Swollman	}
1662742Swollman	strncpy(buf, in, inl);
1672742Swollman	buf[inl]= '\0';
16814343Swollman
1698029Swollman	if(strncmp(buf, "ERR ", 4) == 0){
17014343Swollman		priority= LOG_ERR;
1712742Swollman		pp= buf+ 4;
1722742Swollman	}else if(strncmp(buf, "WAR ", 4) == 0){
17314343Swollman		priority= LOG_WARNING;
17458787Sru		pp= buf+ 4;
1752742Swollman	}else if(strncmp(buf, "INF ", 4) == 0){
17614343Swollman		priority= LOG_INFO;
17714343Swollman		pp= buf+ 4;
17814343Swollman	}else{
17914343Swollman		priority= LOG_ERR;
18030711Swollman		pp= buf;
18130711Swollman	}
18258787Sru
18358787Sru	xsyslog(b, priority, pp);
1842742Swollman
18543014Swollman	Free(buf);
18643014Swollman	return(ret);
18743014Swollman	}
18843014Swollman
1892742Swollmanstatic long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, char *ptr)
1902742Swollman	{
19158787Sru	switch (cmd)
1922742Swollman		{
19319878Swollman	case BIO_CTRL_SET:
19443014Swollman		xcloselog(b);
19543014Swollman		xopenlog(b, ptr, num);
1962742Swollman		break;
1972742Swollman	default:
19819878Swollman		break;
19919878Swollman		}
2002742Swollman	return(0);
2012742Swollman	}
2022742Swollman
2032742Swollmanstatic int MS_CALLBACK slg_puts(BIO *bp, char *str)
20419878Swollman	{
2052742Swollman	int n,ret;
2062742Swollman
20743543Swollman	n=strlen(str);
20875267Swollman	ret=slg_write(bp,str,n);
20975267Swollman	return(ret);
2102742Swollman	}
2112742Swollman
21243543Swollman#if defined(WIN32)
2132742Swollman
2142742Swollmanstatic void xopenlog(BIO* bp, const char* name, int level)
2152742Swollman{
2162742Swollman	bp->ptr= (char *)RegisterEventSource(NULL, name);
21719878Swollman}
2182742Swollman
21919878Swollmanstatic void xsyslog(BIO *bp, int priority, const char *string)
2202742Swollman{
22119878Swollman	LPCSTR lpszStrings[2];
22243014Swollman	WORD evtype= EVENTLOG_ERROR_TYPE;
22343014Swollman	int pid = _getpid();
2242742Swollman	char pidbuf[20];
2252742Swollman
2262742Swollman	switch (priority)
22775267Swollman		{
22875267Swollman	case LOG_ERR:
22975267Swollman		evtype = EVENTLOG_ERROR_TYPE;
23075267Swollman		break;
2312742Swollman	case LOG_WARNING:
2322742Swollman		evtype = EVENTLOG_WARNING_TYPE;
2332742Swollman		break;
2342742Swollman	case LOG_INFO:
23519878Swollman		evtype = EVENTLOG_INFORMATION_TYPE;
2362742Swollman		break;
23719878Swollman	default:
23819878Swollman		evtype = EVENTLOG_ERROR_TYPE;
23919878Swollman		break;
2402742Swollman		}
24119878Swollman
24219878Swollman	sprintf(pidbuf, "[%d] ", pid);
24319878Swollman	lpszStrings[0] = pidbuf;
2442742Swollman	lpszStrings[1] = string;
24514343Swollman
24614343Swollman	if(bp->ptr)
24775267Swollman		ReportEvent(bp->ptr, evtype, 0, 1024, NULL, 2, 0,
24875267Swollman				lpszStrings, NULL);
24919878Swollman}
25075267Swollman
25175267Swollmanstatic void xcloselog(BIO* bp)
25214343Swollman{
25314343Swollman	if(bp->ptr)
25414343Swollman		DeregisterEventSource((HANDLE)(bp->ptr));
25514343Swollman	bp->ptr= NULL;
25619878Swollman}
25719878Swollman
25814343Swollman#elif defined(VMS)
25919878Swollman
26019878Swollmanstatic int VMS_OPC_target = LOG_DAEMON;
26119878Swollman
26214343Swollmanstatic void xopenlog(BIO* bp, const char* name, int level)
26314343Swollman{
26414343Swollman	VMS_OPC_target = level;
26514343Swollman}
26619878Swollman
26719878Swollmanstatic void xsyslog(BIO *bp, int priority, const char *string)
26814343Swollman{
26919878Swollman	struct dsc$descriptor_s opc_dsc;
27014343Swollman	struct opcdef *opcdef_p;
27119878Swollman	char buf[10240];
27214343Swollman	unsigned int len;
27358787Sru        struct dsc$descriptor_s buf_dsc;
27458787Sru	$DESCRIPTOR(fao_cmd, "!AZ: !AZ");
27558787Sru	char *priority_tag;
27614343Swollman
2772742Swollman	switch (priority)
2782742Swollman	  {
2792742Swollman	  case LOG_EMERG: priority_tag = "Emergency"; break;
28019878Swollman	  case LOG_ALERT: priority_tag = "Alert"; break;
2812742Swollman	  case LOG_CRIT: priority_tag = "Critical"; break;
28219878Swollman	  case LOG_ERR: priority_tag = "Error"; break;
28319878Swollman	  case LOG_WARNING: priority_tag = "Warning"; break;
2842742Swollman	  case LOG_NOTICE: priority_tag = "Notice"; break;
2852742Swollman	  case LOG_INFO: priority_tag = "Info"; break;
2862742Swollman	  case LOG_DEBUG: priority_tag = "DEBUG"; break;
28719878Swollman	  }
28819878Swollman
28943014Swollman	buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
29058787Sru	buf_dsc.dsc$b_class = DSC$K_CLASS_S;
29143014Swollman	buf_dsc.dsc$a_pointer = buf;
2922742Swollman	buf_dsc.dsc$w_length = sizeof(buf) - 1;
2932742Swollman
2942742Swollman	lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
2952742Swollman
2962742Swollman	/* we know there's an 8 byte header.  That's documented */
2972742Swollman	opcdef_p = (struct opcdef *) Malloc(8 + len);
2982742Swollman	opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
2992742Swollman	memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
3002742Swollman	opcdef_p->opc$l_ms_rqstid = 0;
3012742Swollman	memcpy(&opcdef_p->opc$l_ms_text, buf, len);
3022742Swollman
3032742Swollman	opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
30414343Swollman	opc_dsc.dsc$b_class = DSC$K_CLASS_S;
3052742Swollman	opc_dsc.dsc$a_pointer = (char *)opcdef_p;
30614343Swollman	opc_dsc.dsc$w_length = len + 8;
30714343Swollman
3082742Swollman	sys$sndopr(opc_dsc, 0);
30914343Swollman
31043014Swollman	Free(opcdef_p);
31114343Swollman}
31214343Swollman
31314343Swollmanstatic void xcloselog(BIO* bp)
3149908Swollman{
3159908Swollman}
3169908Swollman
3179908Swollman#else /* Unix */
3189908Swollman
3199908Swollmanstatic void xopenlog(BIO* bp, const char* name, int level)
3209908Swollman{
32120094Swollman	openlog(name, LOG_PID|LOG_CONS, level);
32220094Swollman}
3232742Swollman
3242742Swollmanstatic void xsyslog(BIO *bp, int priority, const char *string)
32514343Swollman{
3262742Swollman	syslog(priority, "%s", string);
32720094Swollman}
3282742Swollman
3298029Swollmanstatic void xcloselog(BIO* bp)
33030711Swollman{
33158787Sru	closelog();
33258787Sru}
3332742Swollman
33430711Swollman#endif /* Unix */
33558787Sru
33658787Sru#endif /* NO_SYSLOG */
33758787Sru