2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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).
23 #include "internal/cryptlib.h"
25 #if defined(OPENSSL_SYS_WINCE)
26 #elif defined(OPENSSL_SYS_WIN32)
27 #elif defined(OPENSSL_SYS_VMS)
30 # include <lib$routines.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
42 #elif defined(OPENSSL_SYS_NETWARE)
44 #elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG)
48 #include <openssl/buffer.h>
49 #include <openssl/err.h>
53 # if defined(OPENSSL_SYS_WIN32)
58 # define LOG_WARNING 4
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 */
70 # define LOG_WARNING 4
75 # define LOG_DAEMON OPC$M_NM_NTWORK
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);
87 static const BIO_METHOD methods_slg = {
88 BIO_TYPE_MEM, "syslog",
99 const BIO_METHOD *BIO_s_log(void)
101 return (&methods_slg);
104 static int slg_new(BIO *bi)
109 xopenlog(bi, "application", LOG_DAEMON);
113 static int slg_free(BIO *a)
121 static int slg_write(BIO *b, const char *in, int inl)
127 static const struct {
133 6, "PANIC ", LOG_EMERG
136 6, "EMERG ", LOG_EMERG
142 6, "ALERT ", LOG_ALERT
160 8, "WARNING ", LOG_WARNING
163 5, "WARN ", LOG_WARNING
166 4, "WAR ", LOG_WARNING
169 7, "NOTICE ", LOG_NOTICE
172 5, "NOTE ", LOG_NOTICE
175 4, "NOT ", LOG_NOTICE
184 6, "DEBUG ", LOG_DEBUG
195 if ((buf = OPENSSL_malloc(inl + 1)) == NULL) {
198 strncpy(buf, in, inl);
202 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
204 priority = mapping[i].log_level;
205 pp = buf + mapping[i].strl;
207 xsyslog(b, priority, pp);
213 static long slg_ctrl(BIO *b, int cmd, long num, void *ptr)
218 xopenlog(b, ptr, num);
226 static int slg_puts(BIO *bp, const char *str)
231 ret = slg_write(bp, str, n);
235 # if defined(OPENSSL_SYS_WIN32)
237 static void xopenlog(BIO *bp, char *name, int level)
240 bp->ptr = RegisterEventSourceA(NULL, name);
245 static void xsyslog(BIO *bp, int priority, const char *string)
247 LPCSTR lpszStrings[2];
248 WORD evtype = EVENTLOG_ERROR_TYPE;
249 char pidbuf[DECIMAL_SIZE(DWORD) + 4];
259 evtype = EVENTLOG_ERROR_TYPE;
262 evtype = EVENTLOG_WARNING_TYPE;
267 evtype = EVENTLOG_INFORMATION_TYPE;
271 * Should never happen, but set it
274 evtype = EVENTLOG_ERROR_TYPE;
278 sprintf(pidbuf, "[%lu] ", GetCurrentProcessId());
279 lpszStrings[0] = pidbuf;
280 lpszStrings[1] = string;
282 ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL);
285 static void xcloselog(BIO *bp)
288 DeregisterEventSource((HANDLE) (bp->ptr));
292 # elif defined(OPENSSL_SYS_VMS)
294 static int VMS_OPC_target = LOG_DAEMON;
296 static void xopenlog(BIO *bp, char *name, int level)
298 VMS_OPC_target = level;
301 static void xsyslog(BIO *bp, int priority, const char *string)
303 struct dsc$descriptor_s opc_dsc;
305 /* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
306 # if __INITIAL_POINTER_SIZE == 64
307 # pragma pointer_size save
308 # pragma pointer_size 32
309 # define OPCDEF_TYPE __char_ptr32
310 # define OPCDEF_MALLOC _malloc32
311 # else /* __INITIAL_POINTER_SIZE == 64 */
312 # define OPCDEF_TYPE char *
313 # define OPCDEF_MALLOC OPENSSL_malloc
314 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
316 struct opcdef *opcdef_p;
318 # if __INITIAL_POINTER_SIZE == 64
319 # pragma pointer_size restore
320 # endif /* __INITIAL_POINTER_SIZE == 64 */
324 struct dsc$descriptor_s buf_dsc;
325 $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
330 priority_tag = "Emergency";
333 priority_tag = "Alert";
336 priority_tag = "Critical";
339 priority_tag = "Error";
342 priority_tag = "Warning";
345 priority_tag = "Notice";
348 priority_tag = "Info";
351 priority_tag = "DEBUG";
355 buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
356 buf_dsc.dsc$b_class = DSC$K_CLASS_S;
357 buf_dsc.dsc$a_pointer = buf;
358 buf_dsc.dsc$w_length = sizeof(buf) - 1;
360 lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
362 /* We know there's an 8-byte header. That's documented. */
363 opcdef_p = OPCDEF_MALLOC(8 + len);
364 opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
365 memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
366 opcdef_p->opc$l_ms_rqstid = 0;
367 memcpy(&opcdef_p->opc$l_ms_text, buf, len);
369 opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
370 opc_dsc.dsc$b_class = DSC$K_CLASS_S;
371 opc_dsc.dsc$a_pointer = (OPCDEF_TYPE) opcdef_p;
372 opc_dsc.dsc$w_length = len + 8;
374 sys$sndopr(opc_dsc, 0);
376 OPENSSL_free(opcdef_p);
379 static void xcloselog(BIO *bp)
383 # else /* Unix/Watt32 */
385 static void xopenlog(BIO *bp, char *name, int level)
387 # ifdef WATT32 /* djgpp/DOS */
388 openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level);
390 openlog(name, LOG_PID | LOG_CONS, level);
394 static void xsyslog(BIO *bp, int priority, const char *string)
396 syslog(priority, "%s", string);
399 static void xcloselog(BIO *bp)
406 #endif /* NO_SYSLOG */