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