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",
100 const BIO_METHOD *BIO_s_log(void)
102 return (&methods_slg);
105 static int slg_new(BIO *bi)
110 xopenlog(bi, "application", LOG_DAEMON);
114 static int slg_free(BIO *a)
122 static int slg_write(BIO *b, const char *in, int inl)
128 static const struct {
134 6, "PANIC ", LOG_EMERG
137 6, "EMERG ", LOG_EMERG
143 6, "ALERT ", LOG_ALERT
161 8, "WARNING ", LOG_WARNING
164 5, "WARN ", LOG_WARNING
167 4, "WAR ", LOG_WARNING
170 7, "NOTICE ", LOG_NOTICE
173 5, "NOTE ", LOG_NOTICE
176 4, "NOT ", LOG_NOTICE
185 6, "DEBUG ", LOG_DEBUG
196 if ((buf = OPENSSL_malloc(inl + 1)) == NULL) {
199 strncpy(buf, in, inl);
203 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
205 priority = mapping[i].log_level;
206 pp = buf + mapping[i].strl;
208 xsyslog(b, priority, pp);
214 static long slg_ctrl(BIO *b, int cmd, long num, void *ptr)
219 xopenlog(b, ptr, num);
227 static int slg_puts(BIO *bp, const char *str)
232 ret = slg_write(bp, str, n);
236 # if defined(OPENSSL_SYS_WIN32)
238 static void xopenlog(BIO *bp, char *name, int level)
241 bp->ptr = RegisterEventSourceA(NULL, name);
246 static void xsyslog(BIO *bp, int priority, const char *string)
248 LPCSTR lpszStrings[2];
249 WORD evtype = EVENTLOG_ERROR_TYPE;
250 char pidbuf[DECIMAL_SIZE(DWORD) + 4];
260 evtype = EVENTLOG_ERROR_TYPE;
263 evtype = EVENTLOG_WARNING_TYPE;
268 evtype = EVENTLOG_INFORMATION_TYPE;
272 * Should never happen, but set it
275 evtype = EVENTLOG_ERROR_TYPE;
279 sprintf(pidbuf, "[%lu] ", GetCurrentProcessId());
280 lpszStrings[0] = pidbuf;
281 lpszStrings[1] = string;
283 ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL);
286 static void xcloselog(BIO *bp)
289 DeregisterEventSource((HANDLE) (bp->ptr));
293 # elif defined(OPENSSL_SYS_VMS)
295 static int VMS_OPC_target = LOG_DAEMON;
297 static void xopenlog(BIO *bp, char *name, int level)
299 VMS_OPC_target = level;
302 static void xsyslog(BIO *bp, int priority, const char *string)
304 struct dsc$descriptor_s opc_dsc;
306 /* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
307 # if __INITIAL_POINTER_SIZE == 64
308 # pragma pointer_size save
309 # pragma pointer_size 32
310 # define OPCDEF_TYPE __char_ptr32
311 # define OPCDEF_MALLOC _malloc32
312 # else /* __INITIAL_POINTER_SIZE == 64 */
313 # define OPCDEF_TYPE char *
314 # define OPCDEF_MALLOC OPENSSL_malloc
315 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
317 struct opcdef *opcdef_p;
319 # if __INITIAL_POINTER_SIZE == 64
320 # pragma pointer_size restore
321 # endif /* __INITIAL_POINTER_SIZE == 64 */
325 struct dsc$descriptor_s buf_dsc;
326 $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
331 priority_tag = "Emergency";
334 priority_tag = "Alert";
337 priority_tag = "Critical";
340 priority_tag = "Error";
343 priority_tag = "Warning";
346 priority_tag = "Notice";
349 priority_tag = "Info";
352 priority_tag = "DEBUG";
356 buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
357 buf_dsc.dsc$b_class = DSC$K_CLASS_S;
358 buf_dsc.dsc$a_pointer = buf;
359 buf_dsc.dsc$w_length = sizeof(buf) - 1;
361 lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
363 /* We know there's an 8-byte header. That's documented. */
364 opcdef_p = OPCDEF_MALLOC(8 + len);
365 opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
366 memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
367 opcdef_p->opc$l_ms_rqstid = 0;
368 memcpy(&opcdef_p->opc$l_ms_text, buf, len);
370 opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
371 opc_dsc.dsc$b_class = DSC$K_CLASS_S;
372 opc_dsc.dsc$a_pointer = (OPCDEF_TYPE) opcdef_p;
373 opc_dsc.dsc$w_length = len + 8;
375 sys$sndopr(opc_dsc, 0);
377 OPENSSL_free(opcdef_p);
380 static void xcloselog(BIO *bp)
384 # else /* Unix/Watt32 */
386 static void xopenlog(BIO *bp, char *name, int level)
388 # ifdef WATT32 /* djgpp/DOS */
389 openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level);
391 openlog(name, LOG_PID | LOG_CONS, level);
395 static void xsyslog(BIO *bp, int priority, const char *string)
397 syslog(priority, "%s", string);
400 static void xcloselog(BIO *bp)
407 #endif /* NO_SYSLOG */