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