fcb0a132296a0ba13b28de51182ecc237390aba0
[openssl.git] / crypto / bio / bss_log.c
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 /*
57         Why BIO_s_log?
58
59         BIO_s_log is useful for system daemons (or services under NT).
60         It is one-way BIO, it sends all stuff to syslogd (on system that
61         commonly use that), or event log (on NT), or OPCOM (on OpenVMS).
62
63 */
64
65
66 #include <stdio.h>
67 #include <errno.h>
68
69 #if defined(WIN32)
70 #  include <process.h>
71 #elif defined(VMS) || defined(__VMS)
72 #  include <opcdef.h>
73 #  include <descrip.h>
74 #elif defined(__ultrix)
75 #  include <sys/syslog.h>
76 #elif !defined(MSDOS) /* Unix */
77 #  include <syslog.h>
78 #endif
79
80 #include "cryptlib.h"
81 #include <openssl/buffer.h>
82 #include <openssl/err.h>
83
84 #ifndef NO_SYSLOG
85
86 #if defined(WIN32)
87 #define LOG_EMERG       0
88 #define LOG_ALERT       1
89 #define LOG_CRIT        2
90 #define LOG_ERR         3
91 #define LOG_WARNING     4
92 #define LOG_NOTICE      5
93 #define LOG_INFO        6
94 #define LOG_DEBUG       7
95
96 #define LOG_DAEMON      (3<<3)
97 #elif defined(VMS)
98 /* On VMS, we don't really care about these, but we need them to compile */
99 #define LOG_EMERG       0
100 #define LOG_ALERT       1
101 #define LOG_CRIT        2
102 #define LOG_ERR         3
103 #define LOG_WARNING     4
104 #define LOG_NOTICE      5
105 #define LOG_INFO        6
106 #define LOG_DEBUG       7
107
108 #define LOG_DAEMON      OPC$M_NM_NTWORK
109 #endif
110
111 static int MS_CALLBACK slg_write(BIO *h,char *buf,int num);
112 static int MS_CALLBACK slg_puts(BIO *h,char *str);
113 static long MS_CALLBACK slg_ctrl(BIO *h,int cmd,long arg1,char *arg2);
114 static int MS_CALLBACK slg_new(BIO *h);
115 static int MS_CALLBACK slg_free(BIO *data);
116 static void xopenlog(BIO* bp, const char* name, int level);
117 static void xsyslog(BIO* bp, int priority, const char* string);
118 static void xcloselog(BIO* bp);
119
120 static BIO_METHOD methods_slg=
121         {
122         BIO_TYPE_MEM,"syslog",
123         slg_write,
124         NULL,
125         slg_puts,
126         NULL,
127         slg_ctrl,
128         slg_new,
129         slg_free,
130         NULL,
131         };
132
133 BIO_METHOD *BIO_s_log(void)
134         {
135         return(&methods_slg);
136         }
137
138 static int MS_CALLBACK slg_new(BIO *bi)
139         {
140         bi->init=1;
141         bi->num=0;
142         bi->ptr=NULL;
143         xopenlog(bi, "application", LOG_DAEMON);
144         return(1);
145         }
146
147 static int MS_CALLBACK slg_free(BIO *a)
148         {
149         if (a == NULL) return(0);
150         xcloselog(a);
151         return(1);
152         }
153         
154 static int MS_CALLBACK slg_write(BIO *b, char *in, int inl)
155         {
156         int ret= inl;
157         char* buf= in;
158         char* pp;
159         int priority;
160
161         if((buf= (char *)Malloc(inl+ 1)) == NULL){
162                 return(0);
163         }
164         strncpy(buf, in, inl);
165         buf[inl]= '\0';
166
167         if(strncmp(buf, "ERR ", 4) == 0){
168                 priority= LOG_ERR;
169                 pp= buf+ 4;
170         }else if(strncmp(buf, "WAR ", 4) == 0){
171                 priority= LOG_WARNING;
172                 pp= buf+ 4;
173         }else if(strncmp(buf, "INF ", 4) == 0){
174                 priority= LOG_INFO;
175                 pp= buf+ 4;
176         }else{
177                 priority= LOG_ERR;
178                 pp= buf;
179         }
180
181         xsyslog(b, priority, pp);
182
183         Free(buf);
184         return(ret);
185         }
186
187 static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, char *ptr)
188         {
189         switch (cmd)
190                 {
191         case BIO_CTRL_SET:
192                 xcloselog(b);
193                 xopenlog(b, ptr, num);
194                 break;
195         default:
196                 break;
197                 }
198         return(0);
199         }
200
201 static int MS_CALLBACK slg_puts(BIO *bp, char *str)
202         {
203         int n,ret;
204
205         n=strlen(str);
206         ret=slg_write(bp,str,n);
207         return(ret);
208         }
209
210 #if defined(WIN32)
211
212 static void xopenlog(BIO* bp, const char* name, int level)
213 {
214         bp->ptr= (char *)RegisterEventSource(NULL, name);
215 }
216
217 static void xsyslog(BIO *bp, int priority, const char *string)
218 {
219         LPCSTR lpszStrings[2];
220         WORD evtype= EVENTLOG_ERROR_TYPE;
221         int pid = _getpid();
222         char pidbuf[20];
223
224         switch (priority)
225                 {
226         case LOG_ERR:
227                 evtype = EVENTLOG_ERROR_TYPE;
228                 break;
229         case LOG_WARNING:
230                 evtype = EVENTLOG_WARNING_TYPE;
231                 break;
232         case LOG_INFO:
233                 evtype = EVENTLOG_INFORMATION_TYPE;
234                 break;
235         default:
236                 evtype = EVENTLOG_ERROR_TYPE;
237                 break;
238                 }
239
240         sprintf(pidbuf, "[%d] ", pid);
241         lpszStrings[0] = pidbuf;
242         lpszStrings[1] = string;
243
244         if(bp->ptr)
245                 ReportEvent(bp->ptr, evtype, 0, 1024, NULL, 2, 0,
246                                 lpszStrings, NULL);
247 }
248         
249 static void xcloselog(BIO* bp)
250 {
251         if(bp->ptr)
252                 DeregisterEventSource((HANDLE)(bp->ptr));
253         bp->ptr= NULL;
254 }
255
256 #elif defined(VMS)
257
258 static int VMS_OPC_target = OPC$M_NM_NTWORK;
259
260 static void xopenlog(BIO* bp, const char* name, int level)
261 {
262         VMS_OPC_target = level; 
263 }
264
265 static void xsyslog(BIO *bp, int priority, const char *string)
266 {
267         struct descriptor_s opc_dsc;
268         struct opcdef *opcdef_p;
269         char buf[10240];
270         unsigned int len;
271         $DESCRIPTOR(buf_dsc, buf);
272         $DESCRIPTOR(fao_cmd, "!AZ");
273
274         lib$sys_fao(&fao_cmd, &len, &buf_dsc, s);
275
276         /* we knoe there's an 8 byte header.  That's documented */
277         opcdef_p = (struct opcdef *) Malloc(8 + strlen(s));
278         opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
279         memcpy(opcdef_p->opc$b_ms_target, &priority, 3);
280         opcdef_p->opc$l_ms_rqstid = 0;
281         memcpy(&opcdef_p->opc$l_ms_text, buf, len);
282
283         opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
284         opc_dsc.dsc$b_class = DSC$K_CLASS_S;
285         opc_dsc.dsc$a_pointer = opcdef_p;
286         opc_dsc.dsc$w_length = len + 8;
287
288         sys$sndopc(opc_dsc, 0);
289
290         Free(opcdef_p);
291 }
292
293 static void xcloselog(BIO* bp)
294 {
295 }
296
297 #else /* Unix */
298
299 static void xopenlog(BIO* bp, const char* name, int level)
300 {
301         openlog(name, LOG_PID|LOG_CONS, level);
302 }
303
304 static void xsyslog(BIO *bp, int priority, const char *string)
305 {
306         syslog(priority, "%s", string);
307 }
308
309 static void xcloselog(BIO* bp)
310 {
311         closelog();
312 }
313
314 #endif /* Unix */
315
316 #endif /* NO_SYSLOG */