2 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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(__DJGPP__) && defined(OPENSSL_NO_SOCK)
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
) {
200 BIOerr(BIO_F_SLG_WRITE
, ERR_R_MALLOC_FAILURE
);
203 memcpy(buf
, in
, inl
);
207 while (strncmp(buf
, mapping
[i
].str
, mapping
[i
].strl
) != 0)
209 priority
= mapping
[i
].log_level
;
210 pp
= buf
+ mapping
[i
].strl
;
212 xsyslog(b
, priority
, pp
);
218 static long slg_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
223 xopenlog(b
, ptr
, num
);
231 static int slg_puts(BIO
*bp
, const char *str
)
236 ret
= slg_write(bp
, str
, n
);
240 # if defined(OPENSSL_SYS_WIN32)
242 static void xopenlog(BIO
*bp
, char *name
, int level
)
245 bp
->ptr
= RegisterEventSourceA(NULL
, name
);
250 static void xsyslog(BIO
*bp
, int priority
, const char *string
)
252 LPCSTR lpszStrings
[2];
253 WORD evtype
= EVENTLOG_ERROR_TYPE
;
254 char pidbuf
[DECIMAL_SIZE(DWORD
) + 4];
264 evtype
= EVENTLOG_ERROR_TYPE
;
267 evtype
= EVENTLOG_WARNING_TYPE
;
272 evtype
= EVENTLOG_INFORMATION_TYPE
;
276 * Should never happen, but set it
279 evtype
= EVENTLOG_ERROR_TYPE
;
283 sprintf(pidbuf
, "[%lu] ", GetCurrentProcessId());
284 lpszStrings
[0] = pidbuf
;
285 lpszStrings
[1] = string
;
287 ReportEventA(bp
->ptr
, evtype
, 0, 1024, NULL
, 2, 0, lpszStrings
, NULL
);
290 static void xcloselog(BIO
*bp
)
293 DeregisterEventSource((HANDLE
) (bp
->ptr
));
297 # elif defined(OPENSSL_SYS_VMS)
299 static int VMS_OPC_target
= LOG_DAEMON
;
301 static void xopenlog(BIO
*bp
, char *name
, int level
)
303 VMS_OPC_target
= level
;
306 static void xsyslog(BIO
*bp
, int priority
, const char *string
)
308 struct dsc$descriptor_s opc_dsc
;
310 /* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
311 # if __INITIAL_POINTER_SIZE == 64
312 # pragma pointer_size save
313 # pragma pointer_size 32
314 # define OPCDEF_TYPE __char_ptr32
315 # define OPCDEF_MALLOC _malloc32
316 # else /* __INITIAL_POINTER_SIZE == 64 */
317 # define OPCDEF_TYPE char *
318 # define OPCDEF_MALLOC OPENSSL_malloc
319 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
321 struct opcdef
*opcdef_p
;
323 # if __INITIAL_POINTER_SIZE == 64
324 # pragma pointer_size restore
325 # endif /* __INITIAL_POINTER_SIZE == 64 */
329 struct dsc$descriptor_s buf_dsc
;
330 $
DESCRIPTOR(fao_cmd
, "!AZ: !AZ");
335 priority_tag
= "Emergency";
338 priority_tag
= "Alert";
341 priority_tag
= "Critical";
344 priority_tag
= "Error";
347 priority_tag
= "Warning";
350 priority_tag
= "Notice";
353 priority_tag
= "Info";
356 priority_tag
= "DEBUG";
360 buf_dsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
361 buf_dsc
.dsc$b_class
= DSC$K_CLASS_S
;
362 buf_dsc
.dsc$a_pointer
= buf
;
363 buf_dsc
.dsc$w_length
= sizeof(buf
) - 1;
365 lib$
sys_fao(&fao_cmd
, &len
, &buf_dsc
, priority_tag
, string
);
367 /* We know there's an 8-byte header. That's documented. */
368 opcdef_p
= OPCDEF_MALLOC(8 + len
);
369 opcdef_p
->opc$b_ms_type
= OPC$_RQ_RQST
;
370 memcpy(opcdef_p
->opc$z_ms_target_classes
, &VMS_OPC_target
, 3);
371 opcdef_p
->opc$l_ms_rqstid
= 0;
372 memcpy(&opcdef_p
->opc$l_ms_text
, buf
, len
);
374 opc_dsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
375 opc_dsc
.dsc$b_class
= DSC$K_CLASS_S
;
376 opc_dsc
.dsc$a_pointer
= (OPCDEF_TYPE
) opcdef_p
;
377 opc_dsc
.dsc$w_length
= len
+ 8;
379 sys$
sndopr(opc_dsc
, 0);
381 OPENSSL_free(opcdef_p
);
384 static void xcloselog(BIO
*bp
)
388 # else /* Unix/Watt32 */
390 static void xopenlog(BIO
*bp
, char *name
, int level
)
392 # ifdef WATT32 /* djgpp/DOS */
393 openlog(name
, LOG_PID
| LOG_CONS
| LOG_NDELAY
, level
);
395 openlog(name
, LOG_PID
| LOG_CONS
, level
);
399 static void xsyslog(BIO
*bp
, int priority
, const char *string
)
401 syslog(priority
, "%s", string
);
404 static void xcloselog(BIO
*bp
)
411 #else /* NO_SYSLOG */
412 const BIO_METHOD
*BIO_s_log(void)
416 #endif /* NO_SYSLOG */