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