2 * Copyright 1999-2018 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(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG)
46 #include <openssl/buffer.h>
47 #include <openssl/err.h>
51 # if defined(OPENSSL_SYS_WIN32)
56 # define LOG_WARNING 4
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 */
68 # define LOG_WARNING 4
73 # define LOG_DAEMON OPC$M_NM_NTWORK
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);
85 static const BIO_METHOD methods_slg = {
88 /* TODO: Convert to new style write function */
91 NULL, /* slg_write_old, */
98 NULL, /* slg_callback_ctrl */
101 const BIO_METHOD *BIO_s_log(void)
106 static int slg_new(BIO *bi)
111 xopenlog(bi, "application", LOG_DAEMON);
115 static int slg_free(BIO *a)
123 static int slg_write(BIO *b, const char *in, int inl)
129 static const struct {
135 6, "PANIC ", LOG_EMERG
138 6, "EMERG ", LOG_EMERG
144 6, "ALERT ", LOG_ALERT
162 8, "WARNING ", LOG_WARNING
165 5, "WARN ", LOG_WARNING
168 4, "WAR ", LOG_WARNING
171 7, "NOTICE ", LOG_NOTICE
174 5, "NOTE ", LOG_NOTICE
177 4, "NOT ", LOG_NOTICE
186 6, "DEBUG ", LOG_DEBUG
197 if ((buf = OPENSSL_malloc(inl + 1)) == NULL) {
200 strncpy(buf, in, inl);
204 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
206 priority = mapping[i].log_level;
207 pp = buf + mapping[i].strl;
209 xsyslog(b, priority, pp);
215 static long slg_ctrl(BIO *b, int cmd, long num, void *ptr)
220 xopenlog(b, ptr, num);
228 static int slg_puts(BIO *bp, const char *str)
233 ret = slg_write(bp, str, n);
237 # if defined(OPENSSL_SYS_WIN32)
239 static void xopenlog(BIO *bp, char *name, int level)
242 bp->ptr = RegisterEventSourceA(NULL, name);
247 static void xsyslog(BIO *bp, int priority, const char *string)
249 LPCSTR lpszStrings[2];
250 WORD evtype = EVENTLOG_ERROR_TYPE;
251 char pidbuf[DECIMAL_SIZE(DWORD) + 4];
261 evtype = EVENTLOG_ERROR_TYPE;
264 evtype = EVENTLOG_WARNING_TYPE;
269 evtype = EVENTLOG_INFORMATION_TYPE;
273 * Should never happen, but set it
276 evtype = EVENTLOG_ERROR_TYPE;
280 sprintf(pidbuf, "[%lu] ", GetCurrentProcessId());
281 lpszStrings[0] = pidbuf;
282 lpszStrings[1] = string;
284 ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL);
287 static void xcloselog(BIO *bp)
290 DeregisterEventSource((HANDLE) (bp->ptr));
294 # elif defined(OPENSSL_SYS_VMS)
296 static int VMS_OPC_target = LOG_DAEMON;
298 static void xopenlog(BIO *bp, char *name, int level)
300 VMS_OPC_target = level;
303 static void xsyslog(BIO *bp, int priority, const char *string)
305 struct dsc$descriptor_s opc_dsc;
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] */
318 struct opcdef *opcdef_p;
320 # if __INITIAL_POINTER_SIZE == 64
321 # pragma pointer_size restore
322 # endif /* __INITIAL_POINTER_SIZE == 64 */
326 struct dsc$descriptor_s buf_dsc;
327 $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
332 priority_tag = "Emergency";
335 priority_tag = "Alert";
338 priority_tag = "Critical";
341 priority_tag = "Error";
344 priority_tag = "Warning";
347 priority_tag = "Notice";
350 priority_tag = "Info";
353 priority_tag = "DEBUG";
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;
362 lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
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);
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;
376 sys$sndopr(opc_dsc, 0);
378 OPENSSL_free(opcdef_p);
381 static void xcloselog(BIO *bp)
385 # else /* Unix/Watt32 */
387 static void xopenlog(BIO *bp, char *name, int level)
389 # ifdef WATT32 /* djgpp/DOS */
390 openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level);
392 openlog(name, LOG_PID | LOG_CONS, level);
396 static void xsyslog(BIO *bp, int priority, const char *string)
398 syslog(priority, "%s", string);
401 static void xcloselog(BIO *bp)
408 #endif /* NO_SYSLOG */