]>
Commit | Line | Data |
---|---|---|
0849d138 BL |
1 | /* crypto/bio/bss_log.c */ |
2 | /* ==================================================================== | |
c24e2f18 | 3 | * Copyright (c) 1999-2018 The OpenSSL Project. All rights reserved. |
0849d138 BL |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
ae5c8664 | 10 | * notice, this list of conditions and the following disclaimer. |
0849d138 BL |
11 | * |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in | |
14 | * the documentation and/or other materials provided with the | |
15 | * distribution. | |
16 | * | |
17 | * 3. All advertising materials mentioning features or use of this | |
18 | * software must display the following acknowledgment: | |
19 | * "This product includes software developed by the OpenSSL Project | |
20 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
21 | * | |
22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
23 | * endorse or promote products derived from this software without | |
24 | * prior written permission. For written permission, please contact | |
25 | * licensing@OpenSSL.org. | |
26 | * | |
27 | * 5. Products derived from this software may not be called "OpenSSL" | |
28 | * nor may "OpenSSL" appear in their names without prior written | |
29 | * permission of the OpenSSL Project. | |
30 | * | |
31 | * 6. Redistributions of any form whatsoever must retain the following | |
32 | * acknowledgment: | |
33 | * "This product includes software developed by the OpenSSL Project | |
34 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
35 | * | |
36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
48 | * ==================================================================== | |
49 | * | |
50 | * This product includes cryptographic software written by Eric Young | |
51 | * (eay@cryptsoft.com). This product includes software written by Tim | |
52 | * Hudson (tjh@cryptsoft.com). | |
53 | * | |
54 | */ | |
55 | ||
56 | /* | |
ae5c8664 MC |
57 | * Why BIO_s_log? |
58 | * | |
59 | * BIO_s_log is useful for system daemons (or services under NT). It is | |
60 | * one-way BIO, it sends all stuff to syslogd (on system that commonly use | |
61 | * that), or event log (on NT), or OPCOM (on OpenVMS). | |
62 | * | |
63 | */ | |
06c68491 | 64 | |
0849d138 BL |
65 | #include <stdio.h> |
66 | #include <errno.h> | |
67 | ||
72b1072f | 68 | #include "cryptlib.h" |
bc36ee62 | 69 | |
0bf23d9b RL |
70 | #if defined(OPENSSL_SYS_WINCE) |
71 | #elif defined(OPENSSL_SYS_WIN32) | |
bc36ee62 | 72 | #elif defined(OPENSSL_SYS_VMS) |
ae5c8664 MC |
73 | # include <opcdef.h> |
74 | # include <descrip.h> | |
75 | # include <lib$routines.h> | |
76 | # include <starlet.h> | |
01d2e27a | 77 | /* Some compiler options may mask the declaration of "_malloc32". */ |
ae5c8664 MC |
78 | # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE |
79 | # if __INITIAL_POINTER_SIZE == 64 | |
80 | # pragma pointer_size save | |
81 | # pragma pointer_size 32 | |
82 | void *_malloc32(__size_t); | |
83 | # pragma pointer_size restore | |
84 | # endif /* __INITIAL_POINTER_SIZE == 64 */ | |
85 | # endif /* __INITIAL_POINTER_SIZE && defined | |
86 | * _ANSI_C_SOURCE */ | |
84339237 | 87 | #elif defined(__ultrix) |
ae5c8664 | 88 | # include <sys/syslog.h> |
4d8743f4 | 89 | #elif defined(OPENSSL_SYS_NETWARE) |
ae5c8664 | 90 | # define NO_SYSLOG |
8d6e6048 | 91 | #elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG) |
ae5c8664 | 92 | # include <syslog.h> |
0849d138 BL |
93 | #endif |
94 | ||
ec577822 BM |
95 | #include <openssl/buffer.h> |
96 | #include <openssl/err.h> | |
153a59f4 | 97 | |
6e064240 DSH |
98 | #ifndef NO_SYSLOG |
99 | ||
ae5c8664 MC |
100 | # if defined(OPENSSL_SYS_WIN32) |
101 | # define LOG_EMERG 0 | |
102 | # define LOG_ALERT 1 | |
103 | # define LOG_CRIT 2 | |
104 | # define LOG_ERR 3 | |
105 | # define LOG_WARNING 4 | |
106 | # define LOG_NOTICE 5 | |
107 | # define LOG_INFO 6 | |
108 | # define LOG_DEBUG 7 | |
109 | ||
110 | # define LOG_DAEMON (3<<3) | |
111 | # elif defined(OPENSSL_SYS_VMS) | |
84339237 | 112 | /* On VMS, we don't really care about these, but we need them to compile */ |
ae5c8664 MC |
113 | # define LOG_EMERG 0 |
114 | # define LOG_ALERT 1 | |
115 | # define LOG_CRIT 2 | |
116 | # define LOG_ERR 3 | |
117 | # define LOG_WARNING 4 | |
118 | # define LOG_NOTICE 5 | |
119 | # define LOG_INFO 6 | |
120 | # define LOG_DEBUG 7 | |
121 | ||
122 | # define LOG_DAEMON OPC$M_NM_NTWORK | |
123 | # endif | |
0849d138 | 124 | |
0e1c0612 UM |
125 | static int MS_CALLBACK slg_write(BIO *h, const char *buf, int num); |
126 | static int MS_CALLBACK slg_puts(BIO *h, const char *str); | |
127 | static long MS_CALLBACK slg_ctrl(BIO *h, int cmd, long arg1, void *arg2); | |
0849d138 BL |
128 | static int MS_CALLBACK slg_new(BIO *h); |
129 | static int MS_CALLBACK slg_free(BIO *data); | |
ae5c8664 MC |
130 | static void xopenlog(BIO *bp, char *name, int level); |
131 | static void xsyslog(BIO *bp, int priority, const char *string); | |
132 | static void xcloselog(BIO *bp); | |
133 | ||
134 | static BIO_METHOD methods_slg = { | |
135 | BIO_TYPE_MEM, "syslog", | |
136 | slg_write, | |
137 | NULL, | |
138 | slg_puts, | |
139 | NULL, | |
140 | slg_ctrl, | |
141 | slg_new, | |
142 | slg_free, | |
143 | NULL, | |
144 | }; | |
0849d138 | 145 | |
6b691a5c | 146 | BIO_METHOD *BIO_s_log(void) |
ae5c8664 MC |
147 | { |
148 | return (&methods_slg); | |
149 | } | |
0849d138 | 150 | |
6b691a5c | 151 | static int MS_CALLBACK slg_new(BIO *bi) |
ae5c8664 MC |
152 | { |
153 | bi->init = 1; | |
154 | bi->num = 0; | |
155 | bi->ptr = NULL; | |
156 | xopenlog(bi, "application", LOG_DAEMON); | |
157 | return (1); | |
158 | } | |
0849d138 | 159 | |
6b691a5c | 160 | static int MS_CALLBACK slg_free(BIO *a) |
ae5c8664 MC |
161 | { |
162 | if (a == NULL) | |
163 | return (0); | |
164 | xcloselog(a); | |
165 | return (1); | |
166 | } | |
167 | ||
0e1c0612 | 168 | static int MS_CALLBACK slg_write(BIO *b, const char *in, int inl) |
ae5c8664 MC |
169 | { |
170 | int ret = inl; | |
171 | char *buf; | |
172 | char *pp; | |
173 | int priority, i; | |
174 | static const struct { | |
175 | int strl; | |
176 | char str[10]; | |
177 | int log_level; | |
178 | } mapping[] = { | |
179 | { | |
180 | 6, "PANIC ", LOG_EMERG | |
181 | }, | |
182 | { | |
183 | 6, "EMERG ", LOG_EMERG | |
184 | }, | |
185 | { | |
186 | 4, "EMR ", LOG_EMERG | |
187 | }, | |
188 | { | |
189 | 6, "ALERT ", LOG_ALERT | |
190 | }, | |
191 | { | |
192 | 4, "ALR ", LOG_ALERT | |
193 | }, | |
194 | { | |
195 | 5, "CRIT ", LOG_CRIT | |
196 | }, | |
197 | { | |
198 | 4, "CRI ", LOG_CRIT | |
199 | }, | |
200 | { | |
201 | 6, "ERROR ", LOG_ERR | |
202 | }, | |
203 | { | |
204 | 4, "ERR ", LOG_ERR | |
205 | }, | |
206 | { | |
207 | 8, "WARNING ", LOG_WARNING | |
208 | }, | |
209 | { | |
210 | 5, "WARN ", LOG_WARNING | |
211 | }, | |
212 | { | |
213 | 4, "WAR ", LOG_WARNING | |
214 | }, | |
215 | { | |
216 | 7, "NOTICE ", LOG_NOTICE | |
217 | }, | |
218 | { | |
219 | 5, "NOTE ", LOG_NOTICE | |
220 | }, | |
221 | { | |
222 | 4, "NOT ", LOG_NOTICE | |
223 | }, | |
224 | { | |
225 | 5, "INFO ", LOG_INFO | |
226 | }, | |
227 | { | |
228 | 4, "INF ", LOG_INFO | |
229 | }, | |
230 | { | |
231 | 6, "DEBUG ", LOG_DEBUG | |
232 | }, | |
233 | { | |
234 | 4, "DBG ", LOG_DEBUG | |
235 | }, | |
236 | { | |
237 | 0, "", LOG_ERR | |
238 | } | |
239 | /* The default */ | |
240 | }; | |
241 | ||
242 | if ((buf = (char *)OPENSSL_malloc(inl + 1)) == NULL) { | |
243 | return (0); | |
244 | } | |
e78c4f53 | 245 | memcpy(buf, in, inl); |
ae5c8664 MC |
246 | buf[inl] = '\0'; |
247 | ||
248 | i = 0; | |
249 | while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0) | |
250 | i++; | |
251 | priority = mapping[i].log_level; | |
252 | pp = buf + mapping[i].strl; | |
253 | ||
254 | xsyslog(b, priority, pp); | |
255 | ||
256 | OPENSSL_free(buf); | |
257 | return (ret); | |
258 | } | |
0849d138 | 259 | |
0e1c0612 | 260 | static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, void *ptr) |
ae5c8664 MC |
261 | { |
262 | switch (cmd) { | |
263 | case BIO_CTRL_SET: | |
264 | xcloselog(b); | |
265 | xopenlog(b, ptr, num); | |
266 | break; | |
267 | default: | |
268 | break; | |
269 | } | |
270 | return (0); | |
271 | } | |
0849d138 | 272 | |
0e1c0612 | 273 | static int MS_CALLBACK slg_puts(BIO *bp, const char *str) |
ae5c8664 MC |
274 | { |
275 | int n, ret; | |
0849d138 | 276 | |
ae5c8664 MC |
277 | n = strlen(str); |
278 | ret = slg_write(bp, str, n); | |
279 | return (ret); | |
280 | } | |
0849d138 | 281 | |
ae5c8664 | 282 | # if defined(OPENSSL_SYS_WIN32) |
84339237 | 283 | |
ae5c8664 | 284 | static void xopenlog(BIO *bp, char *name, int level) |
84339237 | 285 | { |
ae5c8664 MC |
286 | if (check_winnt()) |
287 | bp->ptr = RegisterEventSourceA(NULL, name); | |
288 | else | |
289 | bp->ptr = NULL; | |
0849d138 BL |
290 | } |
291 | ||
84339237 RL |
292 | static void xsyslog(BIO *bp, int priority, const char *string) |
293 | { | |
ae5c8664 MC |
294 | LPCSTR lpszStrings[2]; |
295 | WORD evtype = EVENTLOG_ERROR_TYPE; | |
296 | char pidbuf[DECIMAL_SIZE(DWORD) + 4]; | |
297 | ||
298 | if (bp->ptr == NULL) | |
299 | return; | |
300 | ||
301 | switch (priority) { | |
302 | case LOG_EMERG: | |
303 | case LOG_ALERT: | |
304 | case LOG_CRIT: | |
305 | case LOG_ERR: | |
306 | evtype = EVENTLOG_ERROR_TYPE; | |
307 | break; | |
308 | case LOG_WARNING: | |
309 | evtype = EVENTLOG_WARNING_TYPE; | |
310 | break; | |
311 | case LOG_NOTICE: | |
312 | case LOG_INFO: | |
313 | case LOG_DEBUG: | |
314 | evtype = EVENTLOG_INFORMATION_TYPE; | |
315 | break; | |
316 | default: | |
317 | /* | |
318 | * Should never happen, but set it | |
319 | * as error anyway. | |
320 | */ | |
321 | evtype = EVENTLOG_ERROR_TYPE; | |
322 | break; | |
323 | } | |
324 | ||
325 | sprintf(pidbuf, "[%u] ", GetCurrentProcessId()); | |
326 | lpszStrings[0] = pidbuf; | |
327 | lpszStrings[1] = string; | |
328 | ||
329 | ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL); | |
84339237 | 330 | } |
ae5c8664 MC |
331 | |
332 | static void xcloselog(BIO *bp) | |
0849d138 | 333 | { |
ae5c8664 MC |
334 | if (bp->ptr) |
335 | DeregisterEventSource((HANDLE) (bp->ptr)); | |
336 | bp->ptr = NULL; | |
84339237 RL |
337 | } |
338 | ||
ae5c8664 | 339 | # elif defined(OPENSSL_SYS_VMS) |
84339237 | 340 | |
158ef048 | 341 | static int VMS_OPC_target = LOG_DAEMON; |
84339237 | 342 | |
ae5c8664 | 343 | static void xopenlog(BIO *bp, char *name, int level) |
84339237 | 344 | { |
ae5c8664 | 345 | VMS_OPC_target = level; |
84339237 RL |
346 | } |
347 | ||
008fee38 | 348 | static void xsyslog(BIO *bp, int priority, const char *string) |
84339237 | 349 | { |
ae5c8664 | 350 | struct dsc$descriptor_s opc_dsc; |
01d2e27a RL |
351 | |
352 | /* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */ | |
ae5c8664 MC |
353 | # if __INITIAL_POINTER_SIZE == 64 |
354 | # pragma pointer_size save | |
355 | # pragma pointer_size 32 | |
356 | # define OPCDEF_TYPE __char_ptr32 | |
357 | # define OPCDEF_MALLOC _malloc32 | |
358 | # else /* __INITIAL_POINTER_SIZE == 64 */ | |
359 | # define OPCDEF_TYPE char * | |
360 | # define OPCDEF_MALLOC OPENSSL_malloc | |
361 | # endif /* __INITIAL_POINTER_SIZE == 64 [else] */ | |
362 | ||
363 | struct opcdef *opcdef_p; | |
364 | ||
365 | # if __INITIAL_POINTER_SIZE == 64 | |
366 | # pragma pointer_size restore | |
367 | # endif /* __INITIAL_POINTER_SIZE == 64 */ | |
368 | ||
369 | char buf[10240]; | |
370 | unsigned int len; | |
371 | struct dsc$descriptor_s buf_dsc; | |
372 | $DESCRIPTOR(fao_cmd, "!AZ: !AZ"); | |
373 | char *priority_tag; | |
374 | ||
375 | switch (priority) { | |
376 | case LOG_EMERG: | |
377 | priority_tag = "Emergency"; | |
378 | break; | |
379 | case LOG_ALERT: | |
380 | priority_tag = "Alert"; | |
381 | break; | |
382 | case LOG_CRIT: | |
383 | priority_tag = "Critical"; | |
384 | break; | |
385 | case LOG_ERR: | |
386 | priority_tag = "Error"; | |
387 | break; | |
388 | case LOG_WARNING: | |
389 | priority_tag = "Warning"; | |
390 | break; | |
391 | case LOG_NOTICE: | |
392 | priority_tag = "Notice"; | |
393 | break; | |
394 | case LOG_INFO: | |
395 | priority_tag = "Info"; | |
396 | break; | |
397 | case LOG_DEBUG: | |
398 | priority_tag = "DEBUG"; | |
399 | break; | |
400 | } | |
401 | ||
402 | buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T; | |
403 | buf_dsc.dsc$b_class = DSC$K_CLASS_S; | |
404 | buf_dsc.dsc$a_pointer = buf; | |
405 | buf_dsc.dsc$w_length = sizeof(buf) - 1; | |
406 | ||
407 | lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string); | |
408 | ||
409 | /* We know there's an 8-byte header. That's documented. */ | |
410 | opcdef_p = OPCDEF_MALLOC(8 + len); | |
411 | opcdef_p->opc$b_ms_type = OPC$_RQ_RQST; | |
412 | memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3); | |
413 | opcdef_p->opc$l_ms_rqstid = 0; | |
414 | memcpy(&opcdef_p->opc$l_ms_text, buf, len); | |
415 | ||
416 | opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T; | |
417 | opc_dsc.dsc$b_class = DSC$K_CLASS_S; | |
418 | opc_dsc.dsc$a_pointer = (OPCDEF_TYPE) opcdef_p; | |
419 | opc_dsc.dsc$w_length = len + 8; | |
420 | ||
421 | sys$sndopr(opc_dsc, 0); | |
422 | ||
423 | OPENSSL_free(opcdef_p); | |
84339237 RL |
424 | } |
425 | ||
ae5c8664 | 426 | static void xcloselog(BIO *bp) |
84339237 RL |
427 | { |
428 | } | |
429 | ||
ae5c8664 | 430 | # else /* Unix/Watt32 */ |
84339237 | 431 | |
ae5c8664 | 432 | static void xopenlog(BIO *bp, char *name, int level) |
84339237 | 433 | { |
ae5c8664 MC |
434 | # ifdef WATT32 /* djgpp/DOS */ |
435 | openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level); | |
436 | # else | |
437 | openlog(name, LOG_PID | LOG_CONS, level); | |
438 | # endif | |
84339237 RL |
439 | } |
440 | ||
441 | static void xsyslog(BIO *bp, int priority, const char *string) | |
442 | { | |
ae5c8664 | 443 | syslog(priority, "%s", string); |
84339237 RL |
444 | } |
445 | ||
ae5c8664 | 446 | static void xcloselog(BIO *bp) |
84339237 | 447 | { |
ae5c8664 | 448 | closelog(); |
0849d138 BL |
449 | } |
450 | ||
ae5c8664 | 451 | # endif /* Unix */ |
84339237 | 452 | |
ae5c8664 | 453 | #endif /* NO_SYSLOG */ |