bss_log.c revision 337982
1/* crypto/bio/bss_log.c */
2/* ====================================================================
3 * Copyright (c) 1999-2018 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). It is
60 * one-way BIO, it sends all stuff to syslogd (on system that commonly use
61 * that), or event log (on NT), or OPCOM (on OpenVMS).
62 *
63 */
64
65#include <stdio.h>
66#include <errno.h>
67
68#include "cryptlib.h"
69
70#if defined(OPENSSL_SYS_WINCE)
71#elif defined(OPENSSL_SYS_WIN32)
72#elif defined(OPENSSL_SYS_VMS)
73# include <opcdef.h>
74# include <descrip.h>
75# include <lib$routines.h>
76# include <starlet.h>
77/* Some compiler options may mask the declaration of "_malloc32". */
78# if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
79#  if __INITIAL_POINTER_SIZE == 64
80#   pragma pointer_size save
81#   pragma pointer_size 32
82void *_malloc32(__size_t);
83#   pragma pointer_size restore
84#  endif                        /* __INITIAL_POINTER_SIZE == 64 */
85# endif                         /* __INITIAL_POINTER_SIZE && defined
86                                 * _ANSI_C_SOURCE */
87#elif defined(__ultrix)
88# include <sys/syslog.h>
89#elif defined(OPENSSL_SYS_NETWARE)
90# define NO_SYSLOG
91#elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG)
92# include <syslog.h>
93#endif
94
95#include <openssl/buffer.h>
96#include <openssl/err.h>
97
98#ifndef NO_SYSLOG
99
100# if defined(OPENSSL_SYS_WIN32)
101#  define LOG_EMERG       0
102#  define LOG_ALERT       1
103#  define LOG_CRIT        2
104#  define LOG_ERR         3
105#  define LOG_WARNING     4
106#  define LOG_NOTICE      5
107#  define LOG_INFO        6
108#  define LOG_DEBUG       7
109
110#  define LOG_DAEMON      (3<<3)
111# elif defined(OPENSSL_SYS_VMS)
112/* On VMS, we don't really care about these, but we need them to compile */
113#  define LOG_EMERG       0
114#  define LOG_ALERT       1
115#  define LOG_CRIT        2
116#  define LOG_ERR         3
117#  define LOG_WARNING     4
118#  define LOG_NOTICE      5
119#  define LOG_INFO        6
120#  define LOG_DEBUG       7
121
122#  define LOG_DAEMON      OPC$M_NM_NTWORK
123# endif
124
125static int MS_CALLBACK slg_write(BIO *h, const char *buf, int num);
126static int MS_CALLBACK slg_puts(BIO *h, const char *str);
127static long MS_CALLBACK slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
128static int MS_CALLBACK slg_new(BIO *h);
129static int MS_CALLBACK slg_free(BIO *data);
130static void xopenlog(BIO *bp, char *name, int level);
131static void xsyslog(BIO *bp, int priority, const char *string);
132static void xcloselog(BIO *bp);
133
134static BIO_METHOD methods_slg = {
135    BIO_TYPE_MEM, "syslog",
136    slg_write,
137    NULL,
138    slg_puts,
139    NULL,
140    slg_ctrl,
141    slg_new,
142    slg_free,
143    NULL,
144};
145
146BIO_METHOD *BIO_s_log(void)
147{
148    return (&methods_slg);
149}
150
151static int MS_CALLBACK slg_new(BIO *bi)
152{
153    bi->init = 1;
154    bi->num = 0;
155    bi->ptr = NULL;
156    xopenlog(bi, "application", LOG_DAEMON);
157    return (1);
158}
159
160static int MS_CALLBACK slg_free(BIO *a)
161{
162    if (a == NULL)
163        return (0);
164    xcloselog(a);
165    return (1);
166}
167
168static int MS_CALLBACK slg_write(BIO *b, const char *in, int inl)
169{
170    int ret = inl;
171    char *buf;
172    char *pp;
173    int priority, i;
174    static const struct {
175        int strl;
176        char str[10];
177        int log_level;
178    } mapping[] = {
179        {
180            6, "PANIC ", LOG_EMERG
181        },
182        {
183            6, "EMERG ", LOG_EMERG
184        },
185        {
186            4, "EMR ", LOG_EMERG
187        },
188        {
189            6, "ALERT ", LOG_ALERT
190        },
191        {
192            4, "ALR ", LOG_ALERT
193        },
194        {
195            5, "CRIT ", LOG_CRIT
196        },
197        {
198            4, "CRI ", LOG_CRIT
199        },
200        {
201            6, "ERROR ", LOG_ERR
202        },
203        {
204            4, "ERR ", LOG_ERR
205        },
206        {
207            8, "WARNING ", LOG_WARNING
208        },
209        {
210            5, "WARN ", LOG_WARNING
211        },
212        {
213            4, "WAR ", LOG_WARNING
214        },
215        {
216            7, "NOTICE ", LOG_NOTICE
217        },
218        {
219            5, "NOTE ", LOG_NOTICE
220        },
221        {
222            4, "NOT ", LOG_NOTICE
223        },
224        {
225            5, "INFO ", LOG_INFO
226        },
227        {
228            4, "INF ", LOG_INFO
229        },
230        {
231            6, "DEBUG ", LOG_DEBUG
232        },
233        {
234            4, "DBG ", LOG_DEBUG
235        },
236        {
237            0, "", LOG_ERR
238        }
239        /* The default */
240    };
241
242    if ((buf = (char *)OPENSSL_malloc(inl + 1)) == NULL) {
243        return (0);
244    }
245    memcpy(buf, in, inl);
246    buf[inl] = '\0';
247
248    i = 0;
249    while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
250        i++;
251    priority = mapping[i].log_level;
252    pp = buf + mapping[i].strl;
253
254    xsyslog(b, priority, pp);
255
256    OPENSSL_free(buf);
257    return (ret);
258}
259
260static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, void *ptr)
261{
262    switch (cmd) {
263    case BIO_CTRL_SET:
264        xcloselog(b);
265        xopenlog(b, ptr, num);
266        break;
267    default:
268        break;
269    }
270    return (0);
271}
272
273static int MS_CALLBACK slg_puts(BIO *bp, const char *str)
274{
275    int n, ret;
276
277    n = strlen(str);
278    ret = slg_write(bp, str, n);
279    return (ret);
280}
281
282# if defined(OPENSSL_SYS_WIN32)
283
284static void xopenlog(BIO *bp, char *name, int level)
285{
286    if (check_winnt())
287        bp->ptr = RegisterEventSourceA(NULL, name);
288    else
289        bp->ptr = NULL;
290}
291
292static void xsyslog(BIO *bp, int priority, const char *string)
293{
294    LPCSTR lpszStrings[2];
295    WORD evtype = EVENTLOG_ERROR_TYPE;
296    char pidbuf[DECIMAL_SIZE(DWORD) + 4];
297
298    if (bp->ptr == NULL)
299        return;
300
301    switch (priority) {
302    case LOG_EMERG:
303    case LOG_ALERT:
304    case LOG_CRIT:
305    case LOG_ERR:
306        evtype = EVENTLOG_ERROR_TYPE;
307        break;
308    case LOG_WARNING:
309        evtype = EVENTLOG_WARNING_TYPE;
310        break;
311    case LOG_NOTICE:
312    case LOG_INFO:
313    case LOG_DEBUG:
314        evtype = EVENTLOG_INFORMATION_TYPE;
315        break;
316    default:
317        /*
318         * Should never happen, but set it
319         * as error anyway.
320         */
321        evtype = EVENTLOG_ERROR_TYPE;
322        break;
323    }
324
325    sprintf(pidbuf, "[%u] ", GetCurrentProcessId());
326    lpszStrings[0] = pidbuf;
327    lpszStrings[1] = string;
328
329    ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL);
330}
331
332static void xcloselog(BIO *bp)
333{
334    if (bp->ptr)
335        DeregisterEventSource((HANDLE) (bp->ptr));
336    bp->ptr = NULL;
337}
338
339# elif defined(OPENSSL_SYS_VMS)
340
341static int VMS_OPC_target = LOG_DAEMON;
342
343static void xopenlog(BIO *bp, char *name, int level)
344{
345    VMS_OPC_target = level;
346}
347
348static void xsyslog(BIO *bp, int priority, const char *string)
349{
350    struct dsc$descriptor_s opc_dsc;
351
352/* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
353#  if __INITIAL_POINTER_SIZE == 64
354#   pragma pointer_size save
355#   pragma pointer_size 32
356#   define OPCDEF_TYPE __char_ptr32
357#   define OPCDEF_MALLOC _malloc32
358#  else                         /* __INITIAL_POINTER_SIZE == 64 */
359#   define OPCDEF_TYPE char *
360#   define OPCDEF_MALLOC OPENSSL_malloc
361#  endif                        /* __INITIAL_POINTER_SIZE == 64 [else] */
362
363    struct opcdef *opcdef_p;
364
365#  if __INITIAL_POINTER_SIZE == 64
366#   pragma pointer_size restore
367#  endif                        /* __INITIAL_POINTER_SIZE == 64 */
368
369    char buf[10240];
370    unsigned int len;
371    struct dsc$descriptor_s buf_dsc;
372    $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
373    char *priority_tag;
374
375    switch (priority) {
376    case LOG_EMERG:
377        priority_tag = "Emergency";
378        break;
379    case LOG_ALERT:
380        priority_tag = "Alert";
381        break;
382    case LOG_CRIT:
383        priority_tag = "Critical";
384        break;
385    case LOG_ERR:
386        priority_tag = "Error";
387        break;
388    case LOG_WARNING:
389        priority_tag = "Warning";
390        break;
391    case LOG_NOTICE:
392        priority_tag = "Notice";
393        break;
394    case LOG_INFO:
395        priority_tag = "Info";
396        break;
397    case LOG_DEBUG:
398        priority_tag = "DEBUG";
399        break;
400    }
401
402    buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
403    buf_dsc.dsc$b_class = DSC$K_CLASS_S;
404    buf_dsc.dsc$a_pointer = buf;
405    buf_dsc.dsc$w_length = sizeof(buf) - 1;
406
407    lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
408
409    /* We know there's an 8-byte header.  That's documented. */
410    opcdef_p = OPCDEF_MALLOC(8 + len);
411    opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
412    memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
413    opcdef_p->opc$l_ms_rqstid = 0;
414    memcpy(&opcdef_p->opc$l_ms_text, buf, len);
415
416    opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
417    opc_dsc.dsc$b_class = DSC$K_CLASS_S;
418    opc_dsc.dsc$a_pointer = (OPCDEF_TYPE) opcdef_p;
419    opc_dsc.dsc$w_length = len + 8;
420
421    sys$sndopr(opc_dsc, 0);
422
423    OPENSSL_free(opcdef_p);
424}
425
426static void xcloselog(BIO *bp)
427{
428}
429
430# else                          /* Unix/Watt32 */
431
432static void xopenlog(BIO *bp, char *name, int level)
433{
434#  ifdef WATT32                 /* djgpp/DOS */
435    openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level);
436#  else
437    openlog(name, LOG_PID | LOG_CONS, level);
438#  endif
439}
440
441static void xsyslog(BIO *bp, int priority, const char *string)
442{
443    syslog(priority, "%s", string);
444}
445
446static void xcloselog(BIO *bp)
447{
448    closelog();
449}
450
451# endif                         /* Unix */
452
453#endif                          /* NO_SYSLOG */
454