]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/bss_log.c
Adjust unixly mk1mf after introduction of tkey
[thirdparty/openssl.git] / crypto / bio / bss_log.c
CommitLineData
0849d138
BL
1/* crypto/bio/bss_log.c */
2/* ====================================================================
3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
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
0f113f3e 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/*
0f113f3e
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)
0f113f3e
MC
73# include <opcdef.h>
74# include <descrip.h>
75# include <lib$routines.h>
76# include <starlet.h>
537c9823 77/* Some compiler options may mask the declaration of "_malloc32". */
0f113f3e
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
82void *_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)
0f113f3e 88# include <sys/syslog.h>
4d8743f4 89#elif defined(OPENSSL_SYS_NETWARE)
0f113f3e 90# define NO_SYSLOG
f642ebc1 91#elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG)
0f113f3e 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
0f113f3e
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 */
0f113f3e
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
6d23cf97
RS
125static int slg_write(BIO *h, const char *buf, int num);
126static int slg_puts(BIO *h, const char *str);
127static long slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
128static int slg_new(BIO *h);
129static int slg_free(BIO *data);
0f113f3e
MC
130static void xopenlog(BIO *bp, char *name, int level);
131static void xsyslog(BIO *bp, int priority, const char *string);
132static void xcloselog(BIO *bp);
133
134static 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 146BIO_METHOD *BIO_s_log(void)
0f113f3e
MC
147{
148 return (&methods_slg);
149}
0849d138 150
6d23cf97 151static int slg_new(BIO *bi)
0f113f3e
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
6d23cf97 160static int slg_free(BIO *a)
0f113f3e
MC
161{
162 if (a == NULL)
163 return (0);
164 xcloselog(a);
165 return (1);
166}
167
6d23cf97 168static int slg_write(BIO *b, const char *in, int inl)
0f113f3e
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
b196e7d9 242 if ((buf = OPENSSL_malloc(inl + 1)) == NULL) {
0f113f3e
MC
243 return (0);
244 }
245 strncpy(buf, in, inl);
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
6d23cf97 260static long slg_ctrl(BIO *b, int cmd, long num, void *ptr)
0f113f3e
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
6d23cf97 273static int slg_puts(BIO *bp, const char *str)
0f113f3e
MC
274{
275 int n, ret;
0849d138 276
0f113f3e
MC
277 n = strlen(str);
278 ret = slg_write(bp, str, n);
279 return (ret);
280}
0849d138 281
0f113f3e 282# if defined(OPENSSL_SYS_WIN32)
84339237 283
0f113f3e 284static void xopenlog(BIO *bp, char *name, int level)
84339237 285{
0f113f3e
MC
286 if (check_winnt())
287 bp->ptr = RegisterEventSourceA(NULL, name);
288 else
289 bp->ptr = NULL;
0849d138
BL
290}
291
84339237
RL
292static void xsyslog(BIO *bp, int priority, const char *string)
293{
0f113f3e
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}
0f113f3e
MC
331
332static void xcloselog(BIO *bp)
0849d138 333{
0f113f3e
MC
334 if (bp->ptr)
335 DeregisterEventSource((HANDLE) (bp->ptr));
336 bp->ptr = NULL;
84339237
RL
337}
338
0f113f3e 339# elif defined(OPENSSL_SYS_VMS)
84339237 340
158ef048 341static int VMS_OPC_target = LOG_DAEMON;
84339237 342
0f113f3e 343static void xopenlog(BIO *bp, char *name, int level)
84339237 344{
0f113f3e 345 VMS_OPC_target = level;
84339237
RL
346}
347
008fee38 348static void xsyslog(BIO *bp, int priority, const char *string)
84339237 349{
0f113f3e 350 struct dsc$descriptor_s opc_dsc;
537c9823
RL
351
352/* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
0f113f3e
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
0f113f3e 426static void xcloselog(BIO *bp)
84339237
RL
427{
428}
429
0f113f3e 430# else /* Unix/Watt32 */
84339237 431
0f113f3e 432static void xopenlog(BIO *bp, char *name, int level)
84339237 433{
0f113f3e
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
441static void xsyslog(BIO *bp, int priority, const char *string)
442{
0f113f3e 443 syslog(priority, "%s", string);
84339237
RL
444}
445
0f113f3e 446static void xcloselog(BIO *bp)
84339237 447{
0f113f3e 448 closelog();
0849d138
BL
449}
450
0f113f3e 451# endif /* Unix */
84339237 452
0f113f3e 453#endif /* NO_SYSLOG */