1 /* ====================================================================
2 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * licensing@OpenSSL.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This product includes cryptographic software written by Eric Young
50 * (eay@cryptsoft.com). This product includes software written by Tim
51 * Hudson (tjh@cryptsoft.com).
58 * BIO_s_log is useful for system daemons (or services under NT). It is
59 * one-way BIO, it sends all stuff to syslogd (on system that commonly use
60 * that), or event log (on NT), or OPCOM (on OpenVMS).
67 #include "internal/cryptlib.h"
69 #if defined(OPENSSL_SYS_WINCE)
70 #elif defined(OPENSSL_SYS_WIN32)
71 #elif defined(OPENSSL_SYS_VMS)
74 # include <lib$routines.h>
76 /* Some compiler options may mask the declaration of "_malloc32". */
77 # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
78 # if __INITIAL_POINTER_SIZE == 64
79 # pragma pointer_size save
80 # pragma pointer_size 32
81 void *_malloc32(__size_t);
82 # pragma pointer_size restore
83 # endif /* __INITIAL_POINTER_SIZE == 64 */
84 # endif /* __INITIAL_POINTER_SIZE && defined
86 #elif defined(OPENSSL_SYS_NETWARE)
88 #elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG)
92 #include <openssl/buffer.h>
93 #include <openssl/err.h>
97 # if defined(OPENSSL_SYS_WIN32)
102 # define LOG_WARNING 4
103 # define LOG_NOTICE 5
107 # define LOG_DAEMON (3<<3)
108 # elif defined(OPENSSL_SYS_VMS)
109 /* On VMS, we don't really care about these, but we need them to compile */
114 # define LOG_WARNING 4
115 # define LOG_NOTICE 5
119 # define LOG_DAEMON OPC$M_NM_NTWORK
122 static int slg_write(BIO *h, const char *buf, int num);
123 static int slg_puts(BIO *h, const char *str);
124 static long slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
125 static int slg_new(BIO *h);
126 static int slg_free(BIO *data);
127 static void xopenlog(BIO *bp, char *name, int level);
128 static void xsyslog(BIO *bp, int priority, const char *string);
129 static void xcloselog(BIO *bp);
131 static BIO_METHOD methods_slg = {
132 BIO_TYPE_MEM, "syslog",
143 BIO_METHOD *BIO_s_log(void)
145 return (&methods_slg);
148 static int slg_new(BIO *bi)
153 xopenlog(bi, "application", LOG_DAEMON);
157 static int slg_free(BIO *a)
165 static int slg_write(BIO *b, const char *in, int inl)
171 static const struct {
177 6, "PANIC ", LOG_EMERG
180 6, "EMERG ", LOG_EMERG
186 6, "ALERT ", LOG_ALERT
204 8, "WARNING ", LOG_WARNING
207 5, "WARN ", LOG_WARNING
210 4, "WAR ", LOG_WARNING
213 7, "NOTICE ", LOG_NOTICE
216 5, "NOTE ", LOG_NOTICE
219 4, "NOT ", LOG_NOTICE
228 6, "DEBUG ", LOG_DEBUG
239 if ((buf = OPENSSL_malloc(inl + 1)) == NULL) {
242 strncpy(buf, in, inl);
246 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
248 priority = mapping[i].log_level;
249 pp = buf + mapping[i].strl;
251 xsyslog(b, priority, pp);
257 static long slg_ctrl(BIO *b, int cmd, long num, void *ptr)
262 xopenlog(b, ptr, num);
270 static int slg_puts(BIO *bp, const char *str)
275 ret = slg_write(bp, str, n);
279 # if defined(OPENSSL_SYS_WIN32)
281 static void xopenlog(BIO *bp, char *name, int level)
284 bp->ptr = RegisterEventSourceA(NULL, name);
289 static void xsyslog(BIO *bp, int priority, const char *string)
291 LPCSTR lpszStrings[2];
292 WORD evtype = EVENTLOG_ERROR_TYPE;
293 char pidbuf[DECIMAL_SIZE(DWORD) + 4];
303 evtype = EVENTLOG_ERROR_TYPE;
306 evtype = EVENTLOG_WARNING_TYPE;
311 evtype = EVENTLOG_INFORMATION_TYPE;
315 * Should never happen, but set it
318 evtype = EVENTLOG_ERROR_TYPE;
322 sprintf(pidbuf, "[%lu] ", GetCurrentProcessId());
323 lpszStrings[0] = pidbuf;
324 lpszStrings[1] = string;
326 ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL);
329 static void xcloselog(BIO *bp)
332 DeregisterEventSource((HANDLE) (bp->ptr));
336 # elif defined(OPENSSL_SYS_VMS)
338 static int VMS_OPC_target = LOG_DAEMON;
340 static void xopenlog(BIO *bp, char *name, int level)
342 VMS_OPC_target = level;
345 static void xsyslog(BIO *bp, int priority, const char *string)
347 struct dsc$descriptor_s opc_dsc;
349 /* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
350 # if __INITIAL_POINTER_SIZE == 64
351 # pragma pointer_size save
352 # pragma pointer_size 32
353 # define OPCDEF_TYPE __char_ptr32
354 # define OPCDEF_MALLOC _malloc32
355 # else /* __INITIAL_POINTER_SIZE == 64 */
356 # define OPCDEF_TYPE char *
357 # define OPCDEF_MALLOC OPENSSL_malloc
358 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
360 struct opcdef *opcdef_p;
362 # if __INITIAL_POINTER_SIZE == 64
363 # pragma pointer_size restore
364 # endif /* __INITIAL_POINTER_SIZE == 64 */
368 struct dsc$descriptor_s buf_dsc;
369 $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
374 priority_tag = "Emergency";
377 priority_tag = "Alert";
380 priority_tag = "Critical";
383 priority_tag = "Error";
386 priority_tag = "Warning";
389 priority_tag = "Notice";
392 priority_tag = "Info";
395 priority_tag = "DEBUG";
399 buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
400 buf_dsc.dsc$b_class = DSC$K_CLASS_S;
401 buf_dsc.dsc$a_pointer = buf;
402 buf_dsc.dsc$w_length = sizeof(buf) - 1;
404 lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
406 /* We know there's an 8-byte header. That's documented. */
407 opcdef_p = OPCDEF_MALLOC(8 + len);
408 opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
409 memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
410 opcdef_p->opc$l_ms_rqstid = 0;
411 memcpy(&opcdef_p->opc$l_ms_text, buf, len);
413 opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
414 opc_dsc.dsc$b_class = DSC$K_CLASS_S;
415 opc_dsc.dsc$a_pointer = (OPCDEF_TYPE) opcdef_p;
416 opc_dsc.dsc$w_length = len + 8;
418 sys$sndopr(opc_dsc, 0);
420 OPENSSL_free(opcdef_p);
423 static void xcloselog(BIO *bp)
427 # else /* Unix/Watt32 */
429 static void xopenlog(BIO *bp, char *name, int level)
431 # ifdef WATT32 /* djgpp/DOS */
432 openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level);
434 openlog(name, LOG_PID | LOG_CONS, level);
438 static void xsyslog(BIO *bp, int priority, const char *string)
440 syslog(priority, "%s", string);
443 static void xcloselog(BIO *bp)
450 #endif /* NO_SYSLOG */