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 = {
90 /* TODO: Convert to new style write function */
93 NULL, /* slg_write_old, */
100 NULL, /* slg_callback_ctrl */
103 const BIO_METHOD *BIO_s_log(void)
108 static int slg_new(BIO *bi)
113 xopenlog(bi, "application", LOG_DAEMON);
117 static int slg_free(BIO *a)
125 static int slg_write(BIO *b, const char *in, int inl)
131 static const struct {
137 6, "PANIC ", LOG_EMERG
140 6, "EMERG ", LOG_EMERG
146 6, "ALERT ", LOG_ALERT
164 8, "WARNING ", LOG_WARNING
167 5, "WARN ", LOG_WARNING
170 4, "WAR ", LOG_WARNING
173 7, "NOTICE ", LOG_NOTICE
176 5, "NOTE ", LOG_NOTICE
179 4, "NOT ", LOG_NOTICE
188 6, "DEBUG ", LOG_DEBUG
199 if ((buf = OPENSSL_malloc(inl + 1)) == NULL) {
202 strncpy(buf, in, inl);
206 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
208 priority = mapping[i].log_level;
209 pp = buf + mapping[i].strl;
211 xsyslog(b, priority, pp);
217 static long slg_ctrl(BIO *b, int cmd, long num, void *ptr)
222 xopenlog(b, ptr, num);
230 static int slg_puts(BIO *bp, const char *str)
235 ret = slg_write(bp, str, n);
239 # if defined(OPENSSL_SYS_WIN32)
241 static void xopenlog(BIO *bp, char *name, int level)
244 bp->ptr = RegisterEventSourceA(NULL, name);
249 static void xsyslog(BIO *bp, int priority, const char *string)
251 LPCSTR lpszStrings[2];
252 WORD evtype = EVENTLOG_ERROR_TYPE;
253 char pidbuf[DECIMAL_SIZE(DWORD) + 4];
263 evtype = EVENTLOG_ERROR_TYPE;
266 evtype = EVENTLOG_WARNING_TYPE;
271 evtype = EVENTLOG_INFORMATION_TYPE;
275 * Should never happen, but set it
278 evtype = EVENTLOG_ERROR_TYPE;
282 sprintf(pidbuf, "[%lu] ", GetCurrentProcessId());
283 lpszStrings[0] = pidbuf;
284 lpszStrings[1] = string;
286 ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL);
289 static void xcloselog(BIO *bp)
292 DeregisterEventSource((HANDLE) (bp->ptr));
296 # elif defined(OPENSSL_SYS_VMS)
298 static int VMS_OPC_target = LOG_DAEMON;
300 static void xopenlog(BIO *bp, char *name, int level)
302 VMS_OPC_target = level;
305 static void xsyslog(BIO *bp, int priority, const char *string)
307 struct dsc$descriptor_s opc_dsc;
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] */
320 struct opcdef *opcdef_p;
322 # if __INITIAL_POINTER_SIZE == 64
323 # pragma pointer_size restore
324 # endif /* __INITIAL_POINTER_SIZE == 64 */
328 struct dsc$descriptor_s buf_dsc;
329 $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
334 priority_tag = "Emergency";
337 priority_tag = "Alert";
340 priority_tag = "Critical";
343 priority_tag = "Error";
346 priority_tag = "Warning";
349 priority_tag = "Notice";
352 priority_tag = "Info";
355 priority_tag = "DEBUG";
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;
364 lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
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);
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;
378 sys$sndopr(opc_dsc, 0);
380 OPENSSL_free(opcdef_p);
383 static void xcloselog(BIO *bp)
387 # else /* Unix/Watt32 */
389 static void xopenlog(BIO *bp, char *name, int level)
391 # ifdef WATT32 /* djgpp/DOS */
392 openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level);
394 openlog(name, LOG_PID | LOG_CONS, level);
398 static void xsyslog(BIO *bp, int priority, const char *string)
400 syslog(priority, "%s", string);
403 static void xcloselog(BIO *bp)
410 #endif /* NO_SYSLOG */