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",
89 /* TODO: Convert to new style write function */
102 const BIO_METHOD *BIO_s_log(void)
104 return (&methods_slg);
107 static int slg_new(BIO *bi)
112 xopenlog(bi, "application", LOG_DAEMON);
116 static int slg_free(BIO *a)
124 static int slg_write(BIO *b, const char *in, int inl)
130 static const struct {
136 6, "PANIC ", LOG_EMERG
139 6, "EMERG ", LOG_EMERG
145 6, "ALERT ", LOG_ALERT
163 8, "WARNING ", LOG_WARNING
166 5, "WARN ", LOG_WARNING
169 4, "WAR ", LOG_WARNING
172 7, "NOTICE ", LOG_NOTICE
175 5, "NOTE ", LOG_NOTICE
178 4, "NOT ", LOG_NOTICE
187 6, "DEBUG ", LOG_DEBUG
198 if ((buf = OPENSSL_malloc(inl + 1)) == NULL) {
201 strncpy(buf, in, inl);
205 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
207 priority = mapping[i].log_level;
208 pp = buf + mapping[i].strl;
210 xsyslog(b, priority, pp);
216 static long slg_ctrl(BIO *b, int cmd, long num, void *ptr)
221 xopenlog(b, ptr, num);
229 static int slg_puts(BIO *bp, const char *str)
234 ret = slg_write(bp, str, n);
238 # if defined(OPENSSL_SYS_WIN32)
240 static void xopenlog(BIO *bp, char *name, int level)
243 bp->ptr = RegisterEventSourceA(NULL, name);
248 static void xsyslog(BIO *bp, int priority, const char *string)
250 LPCSTR lpszStrings[2];
251 WORD evtype = EVENTLOG_ERROR_TYPE;
252 char pidbuf[DECIMAL_SIZE(DWORD) + 4];
262 evtype = EVENTLOG_ERROR_TYPE;
265 evtype = EVENTLOG_WARNING_TYPE;
270 evtype = EVENTLOG_INFORMATION_TYPE;
274 * Should never happen, but set it
277 evtype = EVENTLOG_ERROR_TYPE;
281 sprintf(pidbuf, "[%lu] ", GetCurrentProcessId());
282 lpszStrings[0] = pidbuf;
283 lpszStrings[1] = string;
285 ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL);
288 static void xcloselog(BIO *bp)
291 DeregisterEventSource((HANDLE) (bp->ptr));
295 # elif defined(OPENSSL_SYS_VMS)
297 static int VMS_OPC_target = LOG_DAEMON;
299 static void xopenlog(BIO *bp, char *name, int level)
301 VMS_OPC_target = level;
304 static void xsyslog(BIO *bp, int priority, const char *string)
306 struct dsc$descriptor_s opc_dsc;
308 /* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
309 # if __INITIAL_POINTER_SIZE == 64
310 # pragma pointer_size save
311 # pragma pointer_size 32
312 # define OPCDEF_TYPE __char_ptr32
313 # define OPCDEF_MALLOC _malloc32
314 # else /* __INITIAL_POINTER_SIZE == 64 */
315 # define OPCDEF_TYPE char *
316 # define OPCDEF_MALLOC OPENSSL_malloc
317 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
319 struct opcdef *opcdef_p;
321 # if __INITIAL_POINTER_SIZE == 64
322 # pragma pointer_size restore
323 # endif /* __INITIAL_POINTER_SIZE == 64 */
327 struct dsc$descriptor_s buf_dsc;
328 $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
333 priority_tag = "Emergency";
336 priority_tag = "Alert";
339 priority_tag = "Critical";
342 priority_tag = "Error";
345 priority_tag = "Warning";
348 priority_tag = "Notice";
351 priority_tag = "Info";
354 priority_tag = "DEBUG";
358 buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
359 buf_dsc.dsc$b_class = DSC$K_CLASS_S;
360 buf_dsc.dsc$a_pointer = buf;
361 buf_dsc.dsc$w_length = sizeof(buf) - 1;
363 lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
365 /* We know there's an 8-byte header. That's documented. */
366 opcdef_p = OPCDEF_MALLOC(8 + len);
367 opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
368 memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
369 opcdef_p->opc$l_ms_rqstid = 0;
370 memcpy(&opcdef_p->opc$l_ms_text, buf, len);
372 opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
373 opc_dsc.dsc$b_class = DSC$K_CLASS_S;
374 opc_dsc.dsc$a_pointer = (OPCDEF_TYPE) opcdef_p;
375 opc_dsc.dsc$w_length = len + 8;
377 sys$sndopr(opc_dsc, 0);
379 OPENSSL_free(opcdef_p);
382 static void xcloselog(BIO *bp)
386 # else /* Unix/Watt32 */
388 static void xopenlog(BIO *bp, char *name, int level)
390 # ifdef WATT32 /* djgpp/DOS */
391 openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level);
393 openlog(name, LOG_PID | LOG_CONS, level);
397 static void xsyslog(BIO *bp, int priority, const char *string)
399 syslog(priority, "%s", string);
402 static void xcloselog(BIO *bp)
409 #endif /* NO_SYSLOG */