bss_log.c revision 55714
155714Skris/* crypto/bio/bss_log.c */
255714Skris/* ====================================================================
355714Skris * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
455714Skris *
555714Skris * Redistribution and use in source and binary forms, with or without
655714Skris * modification, are permitted provided that the following conditions
755714Skris * are met:
855714Skris *
955714Skris * 1. Redistributions of source code must retain the above copyright
1055714Skris *    notice, this list of conditions and the following disclaimer.
1155714Skris *
1255714Skris * 2. Redistributions in binary form must reproduce the above copyright
1355714Skris *    notice, this list of conditions and the following disclaimer in
1455714Skris *    the documentation and/or other materials provided with the
1555714Skris *    distribution.
1655714Skris *
1755714Skris * 3. All advertising materials mentioning features or use of this
1855714Skris *    software must display the following acknowledgment:
1955714Skris *    "This product includes software developed by the OpenSSL Project
2055714Skris *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2155714Skris *
2255714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2355714Skris *    endorse or promote products derived from this software without
2455714Skris *    prior written permission. For written permission, please contact
2555714Skris *    licensing@OpenSSL.org.
2655714Skris *
2755714Skris * 5. Products derived from this software may not be called "OpenSSL"
2855714Skris *    nor may "OpenSSL" appear in their names without prior written
2955714Skris *    permission of the OpenSSL Project.
3055714Skris *
3155714Skris * 6. Redistributions of any form whatsoever must retain the following
3255714Skris *    acknowledgment:
3355714Skris *    "This product includes software developed by the OpenSSL Project
3455714Skris *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3555714Skris *
3655714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
3755714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3855714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3955714Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4055714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4155714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4255714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4355714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4455714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4555714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4655714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4755714Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
4855714Skris * ====================================================================
4955714Skris *
5055714Skris * This product includes cryptographic software written by Eric Young
5155714Skris * (eay@cryptsoft.com).  This product includes software written by Tim
5255714Skris * Hudson (tjh@cryptsoft.com).
5355714Skris *
5455714Skris */
5555714Skris
5655714Skris/*
5755714Skris	Why BIO_s_log?
5855714Skris
5955714Skris	BIO_s_log is useful for system daemons (or services under NT).
6055714Skris	It is one-way BIO, it sends all stuff to syslogd (or event log
6155714Skris	under NT).
6255714Skris
6355714Skris*/
6455714Skris
6555714Skris
6655714Skris#include <stdio.h>
6755714Skris#include <errno.h>
6855714Skris
6955714Skris#ifndef WIN32
7055714Skris#ifdef __ultrix
7155714Skris#include <sys/syslog.h>
7255714Skris#else
7355714Skris#include <syslog.h>
7455714Skris#endif
7555714Skris#endif
7655714Skris
7755714Skris#include "cryptlib.h"
7855714Skris#include <openssl/buffer.h>
7955714Skris#include <openssl/err.h>
8055714Skris#ifndef NO_SYSLOG
8155714Skris
8255714Skris
8355714Skrisstatic int MS_CALLBACK slg_write(BIO *h,char *buf,int num);
8455714Skrisstatic int MS_CALLBACK slg_puts(BIO *h,char *str);
8555714Skrisstatic long MS_CALLBACK slg_ctrl(BIO *h,int cmd,long arg1,char *arg2);
8655714Skrisstatic int MS_CALLBACK slg_new(BIO *h);
8755714Skrisstatic int MS_CALLBACK slg_free(BIO *data);
8855714Skrisstatic int xopenlog(BIO* bp, const char* name, int level);
8955714Skrisstatic int xcloselog(BIO* bp);
9055714Skris
9155714Skrisstatic BIO_METHOD methods_slg=
9255714Skris	{
9355714Skris	BIO_TYPE_MEM,"syslog",
9455714Skris	slg_write,
9555714Skris	NULL,
9655714Skris	slg_puts,
9755714Skris	NULL,
9855714Skris	slg_ctrl,
9955714Skris	slg_new,
10055714Skris	slg_free,
10155714Skris	};
10255714Skris
10355714SkrisBIO_METHOD *BIO_s_log(void)
10455714Skris	{
10555714Skris	return(&methods_slg);
10655714Skris	}
10755714Skris
10855714Skrisstatic int MS_CALLBACK slg_new(BIO *bi)
10955714Skris	{
11055714Skris	bi->init=1;
11155714Skris	bi->num=0;
11255714Skris	bi->ptr=NULL;
11355714Skris#ifndef WIN32
11455714Skris	xopenlog(bi, "application", LOG_DAEMON);
11555714Skris#else
11655714Skris	xopenlog(bi, "application", 0);
11755714Skris#endif
11855714Skris	return(1);
11955714Skris	}
12055714Skris
12155714Skrisstatic int MS_CALLBACK slg_free(BIO *a)
12255714Skris	{
12355714Skris	if (a == NULL) return(0);
12455714Skris	xcloselog(a);
12555714Skris	return(1);
12655714Skris	}
12755714Skris
12855714Skrisstatic int MS_CALLBACK slg_write(BIO *b, char *in, int inl)
12955714Skris	{
13055714Skris	int ret= inl;
13155714Skris	char* buf= in;
13255714Skris	char* pp;
13355714Skris#if defined(WIN32)
13455714Skris	LPTSTR lpszStrings[1];
13555714Skris	WORD evtype= EVENTLOG_ERROR_TYPE;
13655714Skris#else
13755714Skris	int priority;
13855714Skris#endif
13955714Skris
14055714Skris	if((buf= (char *)Malloc(inl+ 1)) == NULL){
14155714Skris		return(0);
14255714Skris	}
14355714Skris	strncpy(buf, in, inl);
14455714Skris	buf[inl]= '\0';
14555714Skris#if defined(WIN32)
14655714Skris	if(strncmp(buf, "ERR ", 4) == 0){
14755714Skris		evtype= EVENTLOG_ERROR_TYPE;
14855714Skris		pp= buf+ 4;
14955714Skris	}else if(strncmp(buf, "WAR ", 4) == 0){
15055714Skris		evtype= EVENTLOG_WARNING_TYPE;
15155714Skris		pp= buf+ 4;
15255714Skris	}else if(strncmp(buf, "INF ", 4) == 0){
15355714Skris		evtype= EVENTLOG_INFORMATION_TYPE;
15455714Skris		pp= buf+ 4;
15555714Skris	}else{
15655714Skris		evtype= EVENTLOG_ERROR_TYPE;
15755714Skris		pp= buf;
15855714Skris	}
15955714Skris	lpszStrings[0]= pp;
16055714Skris
16155714Skris	if(b->ptr)
16255714Skris		ReportEvent(b->ptr, evtype, 0, 1024, NULL, 1, 0,
16355714Skris				lpszStrings, NULL);
16455714Skris#else
16555714Skris	if(strncmp(buf, "ERR ", 4) == 0){
16655714Skris		priority= LOG_ERR;
16755714Skris		pp= buf+ 4;
16855714Skris	}else if(strncmp(buf, "WAR ", 4) == 0){
16955714Skris		priority= LOG_WARNING;
17055714Skris		pp= buf+ 4;
17155714Skris	}else if(strncmp(buf, "INF ", 4) == 0){
17255714Skris		priority= LOG_INFO;
17355714Skris		pp= buf+ 4;
17455714Skris	}else{
17555714Skris		priority= LOG_ERR;
17655714Skris		pp= buf;
17755714Skris	}
17855714Skris
17955714Skris	syslog(priority, "%s", pp);
18055714Skris#endif
18155714Skris	Free(buf);
18255714Skris	return(ret);
18355714Skris	}
18455714Skris
18555714Skrisstatic long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, char *ptr)
18655714Skris	{
18755714Skris	switch (cmd)
18855714Skris		{
18955714Skris	case BIO_CTRL_SET:
19055714Skris		xcloselog(b);
19155714Skris		xopenlog(b, ptr, num);
19255714Skris		break;
19355714Skris	default:
19455714Skris		break;
19555714Skris		}
19655714Skris	return(0);
19755714Skris	}
19855714Skris
19955714Skrisstatic int MS_CALLBACK slg_puts(BIO *bp, char *str)
20055714Skris	{
20155714Skris	int n,ret;
20255714Skris
20355714Skris	n=strlen(str);
20455714Skris	ret=slg_write(bp,str,n);
20555714Skris	return(ret);
20655714Skris	}
20755714Skris
20855714Skrisstatic int xopenlog(BIO* bp, const char* name, int level)
20955714Skris{
21055714Skris#if defined(WIN32)
21155714Skris	if((bp->ptr= (char *)RegisterEventSource(NULL, name)) == NULL){
21255714Skris		return(0);
21355714Skris	}
21455714Skris#else
21555714Skris	openlog(name, LOG_PID|LOG_CONS, level);
21655714Skris#endif
21755714Skris	return(1);
21855714Skris}
21955714Skris
22055714Skrisstatic int xcloselog(BIO* bp)
22155714Skris{
22255714Skris#if defined(WIN32)
22355714Skris	if(bp->ptr)
22455714Skris		DeregisterEventSource((HANDLE)(bp->ptr));
22555714Skris	bp->ptr= NULL;
22655714Skris#else
22755714Skris	closelog();
22855714Skris#endif
22955714Skris	return(1);
23055714Skris}
23155714Skris
23255714Skris#endif
233