Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/bio/bss_log.c

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