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).
68 #include "internal/cryptlib.h"
70 #if defined(OPENSSL_SYS_WINCE)
71 #elif defined(OPENSSL_SYS_WIN32)
72 #elif defined(OPENSSL_SYS_VMS)
75 # include <lib$routines.h>
77 /* Some compiler options may mask the declaration of "_malloc32". */
78 # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
79 # if __INITIAL_POINTER_SIZE == 64
80 # pragma pointer_size save
81 # pragma pointer_size 32
82 void *_malloc32(__size_t);
83 # pragma pointer_size restore
84 # endif /* __INITIAL_POINTER_SIZE == 64 */
85 # endif /* __INITIAL_POINTER_SIZE && defined
87 #elif defined(OPENSSL_SYS_NETWARE)
89 #elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG)
93 #include <openssl/buffer.h>
94 #include <openssl/err.h>
98 # if defined(OPENSSL_SYS_WIN32)
103 # define LOG_WARNING 4
104 # define LOG_NOTICE 5
108 # define LOG_DAEMON (3<<3)
109 # elif defined(OPENSSL_SYS_VMS)
110 /* On VMS, we don't really care about these, but we need them to compile */
115 # define LOG_WARNING 4
116 # define LOG_NOTICE 5
120 # define LOG_DAEMON OPC$M_NM_NTWORK
123 static int slg_write(BIO *h, const char *buf, int num);
124 static int slg_puts(BIO *h, const char *str);
125 static long slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
126 static int slg_new(BIO *h);
127 static int slg_free(BIO *data);
128 static void xopenlog(BIO *bp, char *name, int level);
129 static void xsyslog(BIO *bp, int priority, const char *string);
130 static void xcloselog(BIO *bp);
132 static const BIO_METHOD methods_slg = {
133 BIO_TYPE_MEM, "syslog",
144 const BIO_METHOD *BIO_s_log(void)
146 return (&methods_slg);
149 static int slg_new(BIO *bi)
154 xopenlog(bi, "application", LOG_DAEMON);
158 static int slg_free(BIO *a)
166 static int slg_write(BIO *b, const char *in, int inl)
172 static const struct {
178 6, "PANIC ", LOG_EMERG
181 6, "EMERG ", LOG_EMERG
187 6, "ALERT ", LOG_ALERT
205 8, "WARNING ", LOG_WARNING
208 5, "WARN ", LOG_WARNING
211 4, "WAR ", LOG_WARNING
214 7, "NOTICE ", LOG_NOTICE
217 5, "NOTE ", LOG_NOTICE
220 4, "NOT ", LOG_NOTICE
229 6, "DEBUG ", LOG_DEBUG
240 if ((buf = OPENSSL_malloc(inl + 1)) == NULL) {
243 strncpy(buf, in, inl);
247 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
249 priority = mapping[i].log_level;
250 pp = buf + mapping[i].strl;
252 xsyslog(b, priority, pp);
258 static long slg_ctrl(BIO *b, int cmd, long num, void *ptr)
263 xopenlog(b, ptr, num);
271 static int slg_puts(BIO *bp, const char *str)
276 ret = slg_write(bp, str, n);
280 # if defined(OPENSSL_SYS_WIN32)
282 static void xopenlog(BIO *bp, char *name, int level)
285 bp->ptr = RegisterEventSourceA(NULL, name);
290 static void xsyslog(BIO *bp, int priority, const char *string)
292 LPCSTR lpszStrings[2];
293 WORD evtype = EVENTLOG_ERROR_TYPE;
294 char pidbuf[DECIMAL_SIZE(DWORD) + 4];
304 evtype = EVENTLOG_ERROR_TYPE;
307 evtype = EVENTLOG_WARNING_TYPE;
312 evtype = EVENTLOG_INFORMATION_TYPE;
316 * Should never happen, but set it
319 evtype = EVENTLOG_ERROR_TYPE;
323 sprintf(pidbuf, "[%lu] ", GetCurrentProcessId());
324 lpszStrings[0] = pidbuf;
325 lpszStrings[1] = string;
327 ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL);
330 static void xcloselog(BIO *bp)
333 DeregisterEventSource((HANDLE) (bp->ptr));
337 # elif defined(OPENSSL_SYS_VMS)
339 static int VMS_OPC_target = LOG_DAEMON;
341 static void xopenlog(BIO *bp, char *name, int level)
343 VMS_OPC_target = level;
346 static void xsyslog(BIO *bp, int priority, const char *string)
348 struct dsc$descriptor_s opc_dsc;
350 /* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
351 # if __INITIAL_POINTER_SIZE == 64
352 # pragma pointer_size save
353 # pragma pointer_size 32
354 # define OPCDEF_TYPE __char_ptr32
355 # define OPCDEF_MALLOC _malloc32
356 # else /* __INITIAL_POINTER_SIZE == 64 */
357 # define OPCDEF_TYPE char *
358 # define OPCDEF_MALLOC OPENSSL_malloc
359 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
361 struct opcdef *opcdef_p;
363 # if __INITIAL_POINTER_SIZE == 64
364 # pragma pointer_size restore
365 # endif /* __INITIAL_POINTER_SIZE == 64 */
369 struct dsc$descriptor_s buf_dsc;
370 $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
375 priority_tag = "Emergency";
378 priority_tag = "Alert";
381 priority_tag = "Critical";
384 priority_tag = "Error";
387 priority_tag = "Warning";
390 priority_tag = "Notice";
393 priority_tag = "Info";
396 priority_tag = "DEBUG";
400 buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
401 buf_dsc.dsc$b_class = DSC$K_CLASS_S;
402 buf_dsc.dsc$a_pointer = buf;
403 buf_dsc.dsc$w_length = sizeof(buf) - 1;
405 lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
407 /* We know there's an 8-byte header. That's documented. */
408 opcdef_p = OPCDEF_MALLOC(8 + len);
409 opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
410 memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
411 opcdef_p->opc$l_ms_rqstid = 0;
412 memcpy(&opcdef_p->opc$l_ms_text, buf, len);
414 opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
415 opc_dsc.dsc$b_class = DSC$K_CLASS_S;
416 opc_dsc.dsc$a_pointer = (OPCDEF_TYPE) opcdef_p;
417 opc_dsc.dsc$w_length = len + 8;
419 sys$sndopr(opc_dsc, 0);
421 OPENSSL_free(opcdef_p);
424 static void xcloselog(BIO *bp)
428 # else /* Unix/Watt32 */
430 static void xopenlog(BIO *bp, char *name, int level)
432 # ifdef WATT32 /* djgpp/DOS */
433 openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level);
435 openlog(name, LOG_PID | LOG_CONS, level);
439 static void xsyslog(BIO *bp, int priority, const char *string)
441 syslog(priority, "%s", string);
444 static void xcloselog(BIO *bp)
451 #endif /* NO_SYSLOG */