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