1*acf64401Sbeck /* $OpenBSD: bss_log.c,v 1.24 2023/07/05 21:23:37 beck Exp $ */
2913ec974Sbeck /* ====================================================================
3913ec974Sbeck * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4913ec974Sbeck *
5913ec974Sbeck * Redistribution and use in source and binary forms, with or without
6913ec974Sbeck * modification, are permitted provided that the following conditions
7913ec974Sbeck * are met:
8913ec974Sbeck *
9913ec974Sbeck * 1. Redistributions of source code must retain the above copyright
10913ec974Sbeck * notice, this list of conditions and the following disclaimer.
11913ec974Sbeck *
12913ec974Sbeck * 2. Redistributions in binary form must reproduce the above copyright
13913ec974Sbeck * notice, this list of conditions and the following disclaimer in
14913ec974Sbeck * the documentation and/or other materials provided with the
15913ec974Sbeck * distribution.
16913ec974Sbeck *
17913ec974Sbeck * 3. All advertising materials mentioning features or use of this
18913ec974Sbeck * software must display the following acknowledgment:
19913ec974Sbeck * "This product includes software developed by the OpenSSL Project
20913ec974Sbeck * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21913ec974Sbeck *
22913ec974Sbeck * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23913ec974Sbeck * endorse or promote products derived from this software without
24913ec974Sbeck * prior written permission. For written permission, please contact
25913ec974Sbeck * licensing@OpenSSL.org.
26913ec974Sbeck *
27913ec974Sbeck * 5. Products derived from this software may not be called "OpenSSL"
28913ec974Sbeck * nor may "OpenSSL" appear in their names without prior written
29913ec974Sbeck * permission of the OpenSSL Project.
30913ec974Sbeck *
31913ec974Sbeck * 6. Redistributions of any form whatsoever must retain the following
32913ec974Sbeck * acknowledgment:
33913ec974Sbeck * "This product includes software developed by the OpenSSL Project
34913ec974Sbeck * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35913ec974Sbeck *
36913ec974Sbeck * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37913ec974Sbeck * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38913ec974Sbeck * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39913ec974Sbeck * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40913ec974Sbeck * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41913ec974Sbeck * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42913ec974Sbeck * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43913ec974Sbeck * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44913ec974Sbeck * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45913ec974Sbeck * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46913ec974Sbeck * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47913ec974Sbeck * OF THE POSSIBILITY OF SUCH DAMAGE.
48913ec974Sbeck * ====================================================================
49913ec974Sbeck *
50913ec974Sbeck * This product includes cryptographic software written by Eric Young
51913ec974Sbeck * (eay@cryptsoft.com). This product includes software written by Tim
52913ec974Sbeck * Hudson (tjh@cryptsoft.com).
53913ec974Sbeck *
54913ec974Sbeck */
55913ec974Sbeck
56913ec974Sbeck /*
57913ec974Sbeck Why BIO_s_log?
58913ec974Sbeck
59913ec974Sbeck BIO_s_log is useful for system daemons (or services under NT).
60f6e3f262Sbeck It is one-way BIO, it sends all stuff to syslogd (on system that
61f6e3f262Sbeck commonly use that), or event log (on NT), or OPCOM (on OpenVMS).
62913ec974Sbeck
63913ec974Sbeck */
64913ec974Sbeck
65913ec974Sbeck #include <errno.h>
66a8913c44Sjsing #include <stdio.h>
67a8913c44Sjsing #include <string.h>
68a8913c44Sjsing #include <syslog.h>
69913ec974Sbeck
70913ec974Sbeck #include <openssl/buffer.h>
71913ec974Sbeck #include <openssl/err.h>
72f6e3f262Sbeck
7394b1984eStb #include "bio_local.h"
7494b1984eStb
75913ec974Sbeck #ifndef NO_SYSLOG
76913ec974Sbeck
778b5c64d9Sbeck static int slg_write(BIO *h, const char *buf, int num);
788b5c64d9Sbeck static int slg_puts(BIO *h, const char *str);
798b5c64d9Sbeck static long slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
808b5c64d9Sbeck static int slg_new(BIO *h);
818b5c64d9Sbeck static int slg_free(BIO *data);
82c109e398Sbeck static void xopenlog(BIO* bp, char* name, int level);
83f6e3f262Sbeck static void xsyslog(BIO* bp, int priority, const char* string);
84f6e3f262Sbeck static void xcloselog(BIO* bp);
85913ec974Sbeck
866dc76777Stb static const BIO_METHOD methods_slg = {
87e402ce74Smiod .type = BIO_TYPE_MEM,
88e402ce74Smiod .name = "syslog",
89e402ce74Smiod .bwrite = slg_write,
90e402ce74Smiod .bputs = slg_puts,
91e402ce74Smiod .ctrl = slg_ctrl,
92e402ce74Smiod .create = slg_new,
93e402ce74Smiod .destroy = slg_free
94913ec974Sbeck };
95913ec974Sbeck
966dc76777Stb const BIO_METHOD *
BIO_s_log(void)97ae7f143bSderaadt BIO_s_log(void)
98913ec974Sbeck {
99913ec974Sbeck return (&methods_slg);
100913ec974Sbeck }
101*acf64401Sbeck LCRYPTO_ALIAS(BIO_s_log);
102913ec974Sbeck
103c3d505beSjsing static int
slg_new(BIO * bi)104c3d505beSjsing slg_new(BIO *bi)
105913ec974Sbeck {
106913ec974Sbeck bi->init = 1;
107913ec974Sbeck bi->num = 0;
108913ec974Sbeck bi->ptr = NULL;
109913ec974Sbeck xopenlog(bi, "application", LOG_DAEMON);
110913ec974Sbeck return (1);
111913ec974Sbeck }
112913ec974Sbeck
113c3d505beSjsing static int
slg_free(BIO * a)114c3d505beSjsing slg_free(BIO *a)
115913ec974Sbeck {
116c3d505beSjsing if (a == NULL)
117c3d505beSjsing return (0);
118913ec974Sbeck xcloselog(a);
119913ec974Sbeck return (1);
120913ec974Sbeck }
121913ec974Sbeck
122c3d505beSjsing static int
slg_write(BIO * b,const char * in,int inl)123c3d505beSjsing slg_write(BIO *b, const char *in, int inl)
124913ec974Sbeck {
125913ec974Sbeck int ret = inl;
126c109e398Sbeck char* buf;
127913ec974Sbeck char* pp;
128c109e398Sbeck int priority, i;
129c3d505beSjsing static const struct {
130c109e398Sbeck int strl;
131c109e398Sbeck char str[10];
132c109e398Sbeck int log_level;
133c109e398Sbeck }
134c3d505beSjsing mapping[] = {
135c109e398Sbeck { 6, "PANIC ", LOG_EMERG },
136c109e398Sbeck { 6, "EMERG ", LOG_EMERG },
137c109e398Sbeck { 4, "EMR ", LOG_EMERG },
138c109e398Sbeck { 6, "ALERT ", LOG_ALERT },
139c109e398Sbeck { 4, "ALR ", LOG_ALERT },
140c109e398Sbeck { 5, "CRIT ", LOG_CRIT },
141c109e398Sbeck { 4, "CRI ", LOG_CRIT },
142c109e398Sbeck { 6, "ERROR ", LOG_ERR },
143c109e398Sbeck { 4, "ERR ", LOG_ERR },
144c109e398Sbeck { 8, "WARNING ", LOG_WARNING },
145c109e398Sbeck { 5, "WARN ", LOG_WARNING },
146c109e398Sbeck { 4, "WAR ", LOG_WARNING },
147c109e398Sbeck { 7, "NOTICE ", LOG_NOTICE },
148c109e398Sbeck { 5, "NOTE ", LOG_NOTICE },
149c109e398Sbeck { 4, "NOT ", LOG_NOTICE },
150c109e398Sbeck { 5, "INFO ", LOG_INFO },
151c109e398Sbeck { 4, "INF ", LOG_INFO },
152c109e398Sbeck { 6, "DEBUG ", LOG_DEBUG },
153c109e398Sbeck { 4, "DBG ", LOG_DEBUG },
154c109e398Sbeck { 0, "", LOG_ERR } /* The default */
155c109e398Sbeck };
156913ec974Sbeck
1573c6fe066Sderaadt if ((buf = malloc(inl + 1)) == NULL) {
158913ec974Sbeck return (0);
159913ec974Sbeck }
16078e6ae94Sbeck strlcpy(buf, in, inl + 1);
161c109e398Sbeck i = 0;
162c3d505beSjsing while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
163c3d505beSjsing i++;
164c109e398Sbeck priority = mapping[i].log_level;
165c109e398Sbeck pp = buf + mapping[i].strl;
166913ec974Sbeck
167f6e3f262Sbeck xsyslog(b, priority, pp);
168f6e3f262Sbeck
1696f3a6cb1Sbeck free(buf);
170913ec974Sbeck return (ret);
171913ec974Sbeck }
172913ec974Sbeck
173c3d505beSjsing static long
slg_ctrl(BIO * b,int cmd,long num,void * ptr)174c3d505beSjsing slg_ctrl(BIO *b, int cmd, long num, void *ptr)
175913ec974Sbeck {
176c3d505beSjsing switch (cmd) {
177913ec974Sbeck case BIO_CTRL_SET:
178913ec974Sbeck xcloselog(b);
179913ec974Sbeck xopenlog(b, ptr, num);
180913ec974Sbeck break;
181913ec974Sbeck default:
182913ec974Sbeck break;
183913ec974Sbeck }
184913ec974Sbeck return (0);
185913ec974Sbeck }
186913ec974Sbeck
187c3d505beSjsing static int
slg_puts(BIO * bp,const char * str)188c3d505beSjsing slg_puts(BIO *bp, const char *str)
189913ec974Sbeck {
190913ec974Sbeck int n, ret;
191913ec974Sbeck
192913ec974Sbeck n = strlen(str);
193913ec974Sbeck ret = slg_write(bp, str, n);
194913ec974Sbeck return (ret);
195913ec974Sbeck }
196913ec974Sbeck
197f6e3f262Sbeck
198c3d505beSjsing static void
xopenlog(BIO * bp,char * name,int level)199c3d505beSjsing xopenlog(BIO* bp, char* name, int level)
200f6e3f262Sbeck {
201f6e3f262Sbeck openlog(name, LOG_PID|LOG_CONS, level);
202f6e3f262Sbeck }
203f6e3f262Sbeck
204c3d505beSjsing static void
xsyslog(BIO * bp,int priority,const char * string)205c3d505beSjsing xsyslog(BIO *bp, int priority, const char *string)
206f6e3f262Sbeck {
207f6e3f262Sbeck syslog(priority, "%s", string);
208f6e3f262Sbeck }
209f6e3f262Sbeck
210c3d505beSjsing static void
xcloselog(BIO * bp)211c3d505beSjsing xcloselog(BIO* bp)
212f6e3f262Sbeck {
213f6e3f262Sbeck closelog();
214f6e3f262Sbeck }
215f6e3f262Sbeck
216f6e3f262Sbeck #endif /* NO_SYSLOG */
217