]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/bss_log.c
Since return is inconsistent, I removed unnecessary parentheses and
[thirdparty/openssl.git] / crypto / bio / bss_log.c
CommitLineData
b1322259
RS
1/*
2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
0849d138 3 *
b1322259
RS
4 * Licensed under the OpenSSL license (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
0849d138
BL
8 */
9
10/*
0f113f3e
MC
11 * Why BIO_s_log?
12 *
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).
16 *
17 */
06c68491 18
0849d138
BL
19#include <stdio.h>
20#include <errno.h>
21
a146ae55 22#include "bio_lcl.h"
b39fc560 23#include "internal/cryptlib.h"
bc36ee62 24
0bf23d9b
RL
25#if defined(OPENSSL_SYS_WINCE)
26#elif defined(OPENSSL_SYS_WIN32)
bc36ee62 27#elif defined(OPENSSL_SYS_VMS)
0f113f3e
MC
28# include <opcdef.h>
29# include <descrip.h>
30# include <lib$routines.h>
31# include <starlet.h>
537c9823 32/* Some compiler options may mask the declaration of "_malloc32". */
0f113f3e
MC
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
37void *_malloc32(__size_t);
38# pragma pointer_size restore
39# endif /* __INITIAL_POINTER_SIZE == 64 */
40# endif /* __INITIAL_POINTER_SIZE && defined
41 * _ANSI_C_SOURCE */
4d8743f4 42#elif defined(OPENSSL_SYS_NETWARE)
0f113f3e 43# define NO_SYSLOG
f642ebc1 44#elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG)
0f113f3e 45# include <syslog.h>
0849d138
BL
46#endif
47
ec577822
BM
48#include <openssl/buffer.h>
49#include <openssl/err.h>
153a59f4 50
6e064240
DSH
51#ifndef NO_SYSLOG
52
0f113f3e
MC
53# if defined(OPENSSL_SYS_WIN32)
54# define LOG_EMERG 0
55# define LOG_ALERT 1
56# define LOG_CRIT 2
57# define LOG_ERR 3
58# define LOG_WARNING 4
59# define LOG_NOTICE 5
60# define LOG_INFO 6
61# define LOG_DEBUG 7
62
63# define LOG_DAEMON (3<<3)
64# elif defined(OPENSSL_SYS_VMS)
84339237 65/* On VMS, we don't really care about these, but we need them to compile */
0f113f3e
MC
66# define LOG_EMERG 0
67# define LOG_ALERT 1
68# define LOG_CRIT 2
69# define LOG_ERR 3
70# define LOG_WARNING 4
71# define LOG_NOTICE 5
72# define LOG_INFO 6
73# define LOG_DEBUG 7
74
75# define LOG_DAEMON OPC$M_NM_NTWORK
76# endif
0849d138 77
6d23cf97
RS
78static int slg_write(BIO *h, const char *buf, int num);
79static int slg_puts(BIO *h, const char *str);
80static long slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
81static int slg_new(BIO *h);
82static int slg_free(BIO *data);
0f113f3e
MC
83static void xopenlog(BIO *bp, char *name, int level);
84static void xsyslog(BIO *bp, int priority, const char *string);
85static void xcloselog(BIO *bp);
86
04f6b0fd 87static const BIO_METHOD methods_slg = {
0f113f3e 88 BIO_TYPE_MEM, "syslog",
3befffa3
MC
89 /* TODO: Convert to new style write function */
90 bwrite_conv,
0f113f3e
MC
91 slg_write,
92 NULL,
d07aee2c 93 NULL,
0f113f3e
MC
94 slg_puts,
95 NULL,
96 slg_ctrl,
97 slg_new,
98 slg_free,
99 NULL,
100};
0849d138 101
04f6b0fd 102const BIO_METHOD *BIO_s_log(void)
0f113f3e
MC
103{
104 return (&methods_slg);
105}
0849d138 106
6d23cf97 107static int slg_new(BIO *bi)
0f113f3e
MC
108{
109 bi->init = 1;
110 bi->num = 0;
111 bi->ptr = NULL;
112 xopenlog(bi, "application", LOG_DAEMON);
208fb891 113 return 1;
0f113f3e 114}
0849d138 115
6d23cf97 116static int slg_free(BIO *a)
0f113f3e
MC
117{
118 if (a == NULL)
119 return (0);
120 xcloselog(a);
208fb891 121 return 1;
0f113f3e
MC
122}
123
6d23cf97 124static int slg_write(BIO *b, const char *in, int inl)
0f113f3e
MC
125{
126 int ret = inl;
127 char *buf;
128 char *pp;
129 int priority, i;
130 static const struct {
131 int strl;
132 char str[10];
133 int log_level;
134 } mapping[] = {
135 {
136 6, "PANIC ", LOG_EMERG
137 },
138 {
139 6, "EMERG ", LOG_EMERG
140 },
141 {
142 4, "EMR ", LOG_EMERG
143 },
144 {
145 6, "ALERT ", LOG_ALERT
146 },
147 {
148 4, "ALR ", LOG_ALERT
149 },
150 {
151 5, "CRIT ", LOG_CRIT
152 },
153 {
154 4, "CRI ", LOG_CRIT
155 },
156 {
157 6, "ERROR ", LOG_ERR
158 },
159 {
160 4, "ERR ", LOG_ERR
161 },
162 {
163 8, "WARNING ", LOG_WARNING
164 },
165 {
166 5, "WARN ", LOG_WARNING
167 },
168 {
169 4, "WAR ", LOG_WARNING
170 },
171 {
172 7, "NOTICE ", LOG_NOTICE
173 },
174 {
175 5, "NOTE ", LOG_NOTICE
176 },
177 {
178 4, "NOT ", LOG_NOTICE
179 },
180 {
181 5, "INFO ", LOG_INFO
182 },
183 {
184 4, "INF ", LOG_INFO
185 },
186 {
187 6, "DEBUG ", LOG_DEBUG
188 },
189 {
190 4, "DBG ", LOG_DEBUG
191 },
192 {
193 0, "", LOG_ERR
194 }
195 /* The default */
196 };
197
b196e7d9 198 if ((buf = OPENSSL_malloc(inl + 1)) == NULL) {
0f113f3e
MC
199 return (0);
200 }
201 strncpy(buf, in, inl);
202 buf[inl] = '\0';
203
204 i = 0;
205 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
206 i++;
207 priority = mapping[i].log_level;
208 pp = buf + mapping[i].strl;
209
210 xsyslog(b, priority, pp);
211
212 OPENSSL_free(buf);
213 return (ret);
214}
0849d138 215
6d23cf97 216static long slg_ctrl(BIO *b, int cmd, long num, void *ptr)
0f113f3e
MC
217{
218 switch (cmd) {
219 case BIO_CTRL_SET:
220 xcloselog(b);
221 xopenlog(b, ptr, num);
222 break;
223 default:
224 break;
225 }
226 return (0);
227}
0849d138 228
6d23cf97 229static int slg_puts(BIO *bp, const char *str)
0f113f3e
MC
230{
231 int n, ret;
0849d138 232
0f113f3e
MC
233 n = strlen(str);
234 ret = slg_write(bp, str, n);
235 return (ret);
236}
0849d138 237
0f113f3e 238# if defined(OPENSSL_SYS_WIN32)
84339237 239
0f113f3e 240static void xopenlog(BIO *bp, char *name, int level)
84339237 241{
0f113f3e
MC
242 if (check_winnt())
243 bp->ptr = RegisterEventSourceA(NULL, name);
244 else
245 bp->ptr = NULL;
0849d138
BL
246}
247
84339237
RL
248static void xsyslog(BIO *bp, int priority, const char *string)
249{
0f113f3e
MC
250 LPCSTR lpszStrings[2];
251 WORD evtype = EVENTLOG_ERROR_TYPE;
252 char pidbuf[DECIMAL_SIZE(DWORD) + 4];
253
254 if (bp->ptr == NULL)
255 return;
256
257 switch (priority) {
258 case LOG_EMERG:
259 case LOG_ALERT:
260 case LOG_CRIT:
261 case LOG_ERR:
262 evtype = EVENTLOG_ERROR_TYPE;
263 break;
264 case LOG_WARNING:
265 evtype = EVENTLOG_WARNING_TYPE;
266 break;
267 case LOG_NOTICE:
268 case LOG_INFO:
269 case LOG_DEBUG:
270 evtype = EVENTLOG_INFORMATION_TYPE;
271 break;
272 default:
273 /*
274 * Should never happen, but set it
275 * as error anyway.
276 */
277 evtype = EVENTLOG_ERROR_TYPE;
278 break;
279 }
280
21ff9ac8 281 sprintf(pidbuf, "[%lu] ", GetCurrentProcessId());
0f113f3e
MC
282 lpszStrings[0] = pidbuf;
283 lpszStrings[1] = string;
284
285 ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL);
84339237 286}
0f113f3e
MC
287
288static void xcloselog(BIO *bp)
0849d138 289{
0f113f3e
MC
290 if (bp->ptr)
291 DeregisterEventSource((HANDLE) (bp->ptr));
292 bp->ptr = NULL;
84339237
RL
293}
294
0f113f3e 295# elif defined(OPENSSL_SYS_VMS)
84339237 296
158ef048 297static int VMS_OPC_target = LOG_DAEMON;
84339237 298
0f113f3e 299static void xopenlog(BIO *bp, char *name, int level)
84339237 300{
0f113f3e 301 VMS_OPC_target = level;
84339237
RL
302}
303
008fee38 304static void xsyslog(BIO *bp, int priority, const char *string)
84339237 305{
0f113f3e 306 struct dsc$descriptor_s opc_dsc;
537c9823
RL
307
308/* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
0f113f3e
MC
309# if __INITIAL_POINTER_SIZE == 64
310# pragma pointer_size save
311# pragma pointer_size 32
312# define OPCDEF_TYPE __char_ptr32
313# define OPCDEF_MALLOC _malloc32
314# else /* __INITIAL_POINTER_SIZE == 64 */
315# define OPCDEF_TYPE char *
316# define OPCDEF_MALLOC OPENSSL_malloc
317# endif /* __INITIAL_POINTER_SIZE == 64 [else] */
318
319 struct opcdef *opcdef_p;
320
321# if __INITIAL_POINTER_SIZE == 64
322# pragma pointer_size restore
323# endif /* __INITIAL_POINTER_SIZE == 64 */
324
325 char buf[10240];
326 unsigned int len;
327 struct dsc$descriptor_s buf_dsc;
328 $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
329 char *priority_tag;
330
331 switch (priority) {
332 case LOG_EMERG:
333 priority_tag = "Emergency";
334 break;
335 case LOG_ALERT:
336 priority_tag = "Alert";
337 break;
338 case LOG_CRIT:
339 priority_tag = "Critical";
340 break;
341 case LOG_ERR:
342 priority_tag = "Error";
343 break;
344 case LOG_WARNING:
345 priority_tag = "Warning";
346 break;
347 case LOG_NOTICE:
348 priority_tag = "Notice";
349 break;
350 case LOG_INFO:
351 priority_tag = "Info";
352 break;
353 case LOG_DEBUG:
354 priority_tag = "DEBUG";
355 break;
356 }
357
358 buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
359 buf_dsc.dsc$b_class = DSC$K_CLASS_S;
360 buf_dsc.dsc$a_pointer = buf;
361 buf_dsc.dsc$w_length = sizeof(buf) - 1;
362
363 lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
364
365 /* We know there's an 8-byte header. That's documented. */
366 opcdef_p = OPCDEF_MALLOC(8 + len);
367 opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
368 memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
369 opcdef_p->opc$l_ms_rqstid = 0;
370 memcpy(&opcdef_p->opc$l_ms_text, buf, len);
371
372 opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
373 opc_dsc.dsc$b_class = DSC$K_CLASS_S;
374 opc_dsc.dsc$a_pointer = (OPCDEF_TYPE) opcdef_p;
375 opc_dsc.dsc$w_length = len + 8;
376
377 sys$sndopr(opc_dsc, 0);
378
379 OPENSSL_free(opcdef_p);
84339237
RL
380}
381
0f113f3e 382static void xcloselog(BIO *bp)
84339237
RL
383{
384}
385
0f113f3e 386# else /* Unix/Watt32 */
84339237 387
0f113f3e 388static void xopenlog(BIO *bp, char *name, int level)
84339237 389{
0f113f3e
MC
390# ifdef WATT32 /* djgpp/DOS */
391 openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level);
392# else
393 openlog(name, LOG_PID | LOG_CONS, level);
394# endif
84339237
RL
395}
396
397static void xsyslog(BIO *bp, int priority, const char *string)
398{
0f113f3e 399 syslog(priority, "%s", string);
84339237
RL
400}
401
0f113f3e 402static void xcloselog(BIO *bp)
84339237 403{
0f113f3e 404 closelog();
0849d138
BL
405}
406
0f113f3e 407# endif /* Unix */
84339237 408
0f113f3e 409#endif /* NO_SYSLOG */