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