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(__ultrix)
87 # include <sys/syslog.h>
88 #elif defined(OPENSSL_SYS_NETWARE)
90 #elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG)
94 #include <openssl/buffer.h>
95 #include <openssl/err.h>
99 # if defined(OPENSSL_SYS_WIN32)
104 # define LOG_WARNING 4
105 # define LOG_NOTICE 5
109 # define LOG_DAEMON (3<<3)
110 # elif defined(OPENSSL_SYS_VMS)
111 /* On VMS, we don't really care about these, but we need them to compile */
116 # define LOG_WARNING 4
117 # define LOG_NOTICE 5
121 # define LOG_DAEMON OPC$M_NM_NTWORK
124 static int slg_write(BIO *h, const char *buf, int num);
125 static int slg_puts(BIO *h, const char *str);
126 static long slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
127 static int slg_new(BIO *h);
128 static int slg_free(BIO *data);
129 static void xopenlog(BIO *bp, char *name, int level);
130 static void xsyslog(BIO *bp, int priority, const char *string);
131 static void xcloselog(BIO *bp);
133 static BIO_METHOD methods_slg = {
134 BIO_TYPE_MEM, "syslog",
145 BIO_METHOD *BIO_s_log(void)
147 return (&methods_slg);
150 static int slg_new(BIO *bi)
155 xopenlog(bi, "application", LOG_DAEMON);
159 static int slg_free(BIO *a)
167 static int slg_write(BIO *b, const char *in, int inl)
173 static const struct {
179 6, "PANIC ", LOG_EMERG
182 6, "EMERG ", LOG_EMERG
188 6, "ALERT ", LOG_ALERT
206 8, "WARNING ", LOG_WARNING
209 5, "WARN ", LOG_WARNING
212 4, "WAR ", LOG_WARNING
215 7, "NOTICE ", LOG_NOTICE
218 5, "NOTE ", LOG_NOTICE
221 4, "NOT ", LOG_NOTICE
230 6, "DEBUG ", LOG_DEBUG
241 if ((buf = OPENSSL_malloc(inl + 1)) == NULL) {
244 strncpy(buf, in, inl);
248 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
250 priority = mapping[i].log_level;
251 pp = buf + mapping[i].strl;
253 xsyslog(b, priority, pp);
259 static long slg_ctrl(BIO *b, int cmd, long num, void *ptr)
264 xopenlog(b, ptr, num);
272 static int slg_puts(BIO *bp, const char *str)
277 ret = slg_write(bp, str, n);
281 # if defined(OPENSSL_SYS_WIN32)
283 static void xopenlog(BIO *bp, char *name, int level)
286 bp->ptr = RegisterEventSourceA(NULL, name);
291 static void xsyslog(BIO *bp, int priority, const char *string)
293 LPCSTR lpszStrings[2];
294 WORD evtype = EVENTLOG_ERROR_TYPE;
295 char pidbuf[DECIMAL_SIZE(DWORD) + 4];
305 evtype = EVENTLOG_ERROR_TYPE;
308 evtype = EVENTLOG_WARNING_TYPE;
313 evtype = EVENTLOG_INFORMATION_TYPE;
317 * Should never happen, but set it
320 evtype = EVENTLOG_ERROR_TYPE;
324 sprintf(pidbuf, "[%lu] ", GetCurrentProcessId());
325 lpszStrings[0] = pidbuf;
326 lpszStrings[1] = string;
328 ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL);
331 static void xcloselog(BIO *bp)
334 DeregisterEventSource((HANDLE) (bp->ptr));
338 # elif defined(OPENSSL_SYS_VMS)
340 static int VMS_OPC_target = LOG_DAEMON;
342 static void xopenlog(BIO *bp, char *name, int level)
344 VMS_OPC_target = level;
347 static void xsyslog(BIO *bp, int priority, const char *string)
349 struct dsc$descriptor_s opc_dsc;
351 /* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
352 # if __INITIAL_POINTER_SIZE == 64
353 # pragma pointer_size save
354 # pragma pointer_size 32
355 # define OPCDEF_TYPE __char_ptr32
356 # define OPCDEF_MALLOC _malloc32
357 # else /* __INITIAL_POINTER_SIZE == 64 */
358 # define OPCDEF_TYPE char *
359 # define OPCDEF_MALLOC OPENSSL_malloc
360 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
362 struct opcdef *opcdef_p;
364 # if __INITIAL_POINTER_SIZE == 64
365 # pragma pointer_size restore
366 # endif /* __INITIAL_POINTER_SIZE == 64 */
370 struct dsc$descriptor_s buf_dsc;
371 $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
376 priority_tag = "Emergency";
379 priority_tag = "Alert";
382 priority_tag = "Critical";
385 priority_tag = "Error";
388 priority_tag = "Warning";
391 priority_tag = "Notice";
394 priority_tag = "Info";
397 priority_tag = "DEBUG";
401 buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
402 buf_dsc.dsc$b_class = DSC$K_CLASS_S;
403 buf_dsc.dsc$a_pointer = buf;
404 buf_dsc.dsc$w_length = sizeof(buf) - 1;
406 lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
408 /* We know there's an 8-byte header. That's documented. */
409 opcdef_p = OPCDEF_MALLOC(8 + len);
410 opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
411 memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
412 opcdef_p->opc$l_ms_rqstid = 0;
413 memcpy(&opcdef_p->opc$l_ms_text, buf, len);
415 opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
416 opc_dsc.dsc$b_class = DSC$K_CLASS_S;
417 opc_dsc.dsc$a_pointer = (OPCDEF_TYPE) opcdef_p;
418 opc_dsc.dsc$w_length = len + 8;
420 sys$sndopr(opc_dsc, 0);
422 OPENSSL_free(opcdef_p);
425 static void xcloselog(BIO *bp)
429 # else /* Unix/Watt32 */
431 static void xopenlog(BIO *bp, char *name, int level)
433 # ifdef WATT32 /* djgpp/DOS */
434 openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level);
436 openlog(name, LOG_PID | LOG_CONS, level);
440 static void xsyslog(BIO *bp, int priority, const char *string)
442 syslog(priority, "%s", string);
445 static void xcloselog(BIO *bp)
452 #endif /* NO_SYSLOG */