]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/bss_log.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / bio / bss_log.c
CommitLineData
b1322259 1/*
48e5119a 2 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
0849d138 3 *
09abbca1 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
706457b7 22#include "bio_local.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 */
d10ca7ae
AP
42#elif defined(__DJGPP__) && defined(OPENSSL_NO_SOCK)
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 = {
27ab9195
DB
88 BIO_TYPE_MEM,
89 "syslog",
3befffa3
MC
90 /* TODO: Convert to new style write function */
91 bwrite_conv,
0f113f3e 92 slg_write,
b4ff6622
DB
93 NULL, /* slg_write_old, */
94 NULL, /* slg_read, */
0f113f3e
MC
95 slg_puts,
96 NULL,
97 slg_ctrl,
98 slg_new,
99 slg_free,
b4ff6622 100 NULL, /* slg_callback_ctrl */
0f113f3e 101};
0849d138 102
04f6b0fd 103const BIO_METHOD *BIO_s_log(void)
0f113f3e 104{
26a7d938 105 return &methods_slg;
0f113f3e 106}
0849d138 107
6d23cf97 108static int slg_new(BIO *bi)
0f113f3e
MC
109{
110 bi->init = 1;
111 bi->num = 0;
112 bi->ptr = NULL;
113 xopenlog(bi, "application", LOG_DAEMON);
208fb891 114 return 1;
0f113f3e 115}
0849d138 116
6d23cf97 117static int slg_free(BIO *a)
0f113f3e
MC
118{
119 if (a == NULL)
26a7d938 120 return 0;
0f113f3e 121 xcloselog(a);
208fb891 122 return 1;
0f113f3e
MC
123}
124
6d23cf97 125static int slg_write(BIO *b, const char *in, int inl)
0f113f3e
MC
126{
127 int ret = inl;
128 char *buf;
129 char *pp;
130 int priority, i;
131 static const struct {
132 int strl;
133 char str[10];
134 int log_level;
135 } mapping[] = {
136 {
137 6, "PANIC ", LOG_EMERG
138 },
139 {
140 6, "EMERG ", LOG_EMERG
141 },
142 {
143 4, "EMR ", LOG_EMERG
144 },
145 {
146 6, "ALERT ", LOG_ALERT
147 },
148 {
149 4, "ALR ", LOG_ALERT
150 },
151 {
152 5, "CRIT ", LOG_CRIT
153 },
154 {
155 4, "CRI ", LOG_CRIT
156 },
157 {
158 6, "ERROR ", LOG_ERR
159 },
160 {
161 4, "ERR ", LOG_ERR
162 },
163 {
164 8, "WARNING ", LOG_WARNING
165 },
166 {
167 5, "WARN ", LOG_WARNING
168 },
169 {
170 4, "WAR ", LOG_WARNING
171 },
172 {
173 7, "NOTICE ", LOG_NOTICE
174 },
175 {
176 5, "NOTE ", LOG_NOTICE
177 },
178 {
179 4, "NOT ", LOG_NOTICE
180 },
181 {
182 5, "INFO ", LOG_INFO
183 },
184 {
185 4, "INF ", LOG_INFO
186 },
187 {
188 6, "DEBUG ", LOG_DEBUG
189 },
190 {
191 4, "DBG ", LOG_DEBUG
192 },
193 {
194 0, "", LOG_ERR
195 }
196 /* The default */
197 };
198
b196e7d9 199 if ((buf = OPENSSL_malloc(inl + 1)) == NULL) {
f06080cb 200 BIOerr(BIO_F_SLG_WRITE, ERR_R_MALLOC_FAILURE);
26a7d938 201 return 0;
0f113f3e 202 }
dc6c374b 203 memcpy(buf, in, inl);
0f113f3e
MC
204 buf[inl] = '\0';
205
206 i = 0;
207 while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
208 i++;
209 priority = mapping[i].log_level;
210 pp = buf + mapping[i].strl;
211
212 xsyslog(b, priority, pp);
213
214 OPENSSL_free(buf);
26a7d938 215 return ret;
0f113f3e 216}
0849d138 217
6d23cf97 218static long slg_ctrl(BIO *b, int cmd, long num, void *ptr)
0f113f3e
MC
219{
220 switch (cmd) {
221 case BIO_CTRL_SET:
222 xcloselog(b);
223 xopenlog(b, ptr, num);
224 break;
225 default:
226 break;
227 }
26a7d938 228 return 0;
0f113f3e 229}
0849d138 230
6d23cf97 231static int slg_puts(BIO *bp, const char *str)
0f113f3e
MC
232{
233 int n, ret;
0849d138 234
0f113f3e
MC
235 n = strlen(str);
236 ret = slg_write(bp, str, n);
26a7d938 237 return ret;
0f113f3e 238}
0849d138 239
0f113f3e 240# if defined(OPENSSL_SYS_WIN32)
84339237 241
0f113f3e 242static void xopenlog(BIO *bp, char *name, int level)
84339237 243{
0f113f3e
MC
244 if (check_winnt())
245 bp->ptr = RegisterEventSourceA(NULL, name);
246 else
247 bp->ptr = NULL;
0849d138
BL
248}
249
84339237
RL
250static void xsyslog(BIO *bp, int priority, const char *string)
251{
0f113f3e
MC
252 LPCSTR lpszStrings[2];
253 WORD evtype = EVENTLOG_ERROR_TYPE;
254 char pidbuf[DECIMAL_SIZE(DWORD) + 4];
255
256 if (bp->ptr == NULL)
257 return;
258
259 switch (priority) {
260 case LOG_EMERG:
261 case LOG_ALERT:
262 case LOG_CRIT:
263 case LOG_ERR:
264 evtype = EVENTLOG_ERROR_TYPE;
265 break;
266 case LOG_WARNING:
267 evtype = EVENTLOG_WARNING_TYPE;
268 break;
269 case LOG_NOTICE:
270 case LOG_INFO:
271 case LOG_DEBUG:
272 evtype = EVENTLOG_INFORMATION_TYPE;
273 break;
274 default:
275 /*
276 * Should never happen, but set it
277 * as error anyway.
278 */
279 evtype = EVENTLOG_ERROR_TYPE;
280 break;
281 }
282
21ff9ac8 283 sprintf(pidbuf, "[%lu] ", GetCurrentProcessId());
0f113f3e
MC
284 lpszStrings[0] = pidbuf;
285 lpszStrings[1] = string;
286
287 ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL);
84339237 288}
0f113f3e
MC
289
290static void xcloselog(BIO *bp)
0849d138 291{
0f113f3e
MC
292 if (bp->ptr)
293 DeregisterEventSource((HANDLE) (bp->ptr));
294 bp->ptr = NULL;
84339237
RL
295}
296
0f113f3e 297# elif defined(OPENSSL_SYS_VMS)
84339237 298
158ef048 299static int VMS_OPC_target = LOG_DAEMON;
84339237 300
0f113f3e 301static void xopenlog(BIO *bp, char *name, int level)
84339237 302{
0f113f3e 303 VMS_OPC_target = level;
84339237
RL
304}
305
008fee38 306static void xsyslog(BIO *bp, int priority, const char *string)
84339237 307{
0f113f3e 308 struct dsc$descriptor_s opc_dsc;
537c9823
RL
309
310/* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
0f113f3e
MC
311# if __INITIAL_POINTER_SIZE == 64
312# pragma pointer_size save
313# pragma pointer_size 32
314# define OPCDEF_TYPE __char_ptr32
315# define OPCDEF_MALLOC _malloc32
316# else /* __INITIAL_POINTER_SIZE == 64 */
317# define OPCDEF_TYPE char *
318# define OPCDEF_MALLOC OPENSSL_malloc
319# endif /* __INITIAL_POINTER_SIZE == 64 [else] */
320
321 struct opcdef *opcdef_p;
322
323# if __INITIAL_POINTER_SIZE == 64
324# pragma pointer_size restore
325# endif /* __INITIAL_POINTER_SIZE == 64 */
326
327 char buf[10240];
328 unsigned int len;
329 struct dsc$descriptor_s buf_dsc;
330 $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
331 char *priority_tag;
332
333 switch (priority) {
334 case LOG_EMERG:
335 priority_tag = "Emergency";
336 break;
337 case LOG_ALERT:
338 priority_tag = "Alert";
339 break;
340 case LOG_CRIT:
341 priority_tag = "Critical";
342 break;
343 case LOG_ERR:
344 priority_tag = "Error";
345 break;
346 case LOG_WARNING:
347 priority_tag = "Warning";
348 break;
349 case LOG_NOTICE:
350 priority_tag = "Notice";
351 break;
352 case LOG_INFO:
353 priority_tag = "Info";
354 break;
355 case LOG_DEBUG:
356 priority_tag = "DEBUG";
357 break;
358 }
359
360 buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
361 buf_dsc.dsc$b_class = DSC$K_CLASS_S;
362 buf_dsc.dsc$a_pointer = buf;
363 buf_dsc.dsc$w_length = sizeof(buf) - 1;
364
365 lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
366
367 /* We know there's an 8-byte header. That's documented. */
368 opcdef_p = OPCDEF_MALLOC(8 + len);
369 opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
370 memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
371 opcdef_p->opc$l_ms_rqstid = 0;
372 memcpy(&opcdef_p->opc$l_ms_text, buf, len);
373
374 opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
375 opc_dsc.dsc$b_class = DSC$K_CLASS_S;
376 opc_dsc.dsc$a_pointer = (OPCDEF_TYPE) opcdef_p;
377 opc_dsc.dsc$w_length = len + 8;
378
379 sys$sndopr(opc_dsc, 0);
380
381 OPENSSL_free(opcdef_p);
84339237
RL
382}
383
0f113f3e 384static void xcloselog(BIO *bp)
84339237
RL
385{
386}
387
0f113f3e 388# else /* Unix/Watt32 */
84339237 389
0f113f3e 390static void xopenlog(BIO *bp, char *name, int level)
84339237 391{
0f113f3e
MC
392# ifdef WATT32 /* djgpp/DOS */
393 openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level);
394# else
395 openlog(name, LOG_PID | LOG_CONS, level);
396# endif
84339237
RL
397}
398
399static void xsyslog(BIO *bp, int priority, const char *string)
400{
0f113f3e 401 syslog(priority, "%s", string);
84339237
RL
402}
403
0f113f3e 404static void xcloselog(BIO *bp)
84339237 405{
0f113f3e 406 closelog();
0849d138
BL
407}
408
0f113f3e 409# endif /* Unix */
84339237 410
7e09c5ea
RL
411#else /* NO_SYSLOG */
412const BIO_METHOD *BIO_s_log(void)
413{
414 return NULL;
415}
0f113f3e 416#endif /* NO_SYSLOG */