summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bio/bss_log.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bio/bss_log.c')
-rw-r--r--src/lib/libcrypto/bio/bss_log.c325
1 files changed, 244 insertions, 81 deletions
diff --git a/src/lib/libcrypto/bio/bss_log.c b/src/lib/libcrypto/bio/bss_log.c
index db82e757e7..a39d95297c 100644
--- a/src/lib/libcrypto/bio/bss_log.c
+++ b/src/lib/libcrypto/bio/bss_log.c
@@ -57,8 +57,8 @@
57 Why BIO_s_log? 57 Why BIO_s_log?
58 58
59 BIO_s_log is useful for system daemons (or services under NT). 59 BIO_s_log is useful for system daemons (or services under NT).
60 It is one-way BIO, it sends all stuff to syslogd (or event log 60 It is one-way BIO, it sends all stuff to syslogd (on system that
61 under NT). 61 commonly use that), or event log (on NT), or OPCOM (on OpenVMS).
62 62
63*/ 63*/
64 64
@@ -66,27 +66,71 @@
66#include <stdio.h> 66#include <stdio.h>
67#include <errno.h> 67#include <errno.h>
68 68
69#ifndef WIN32 69#include "cryptlib.h"
70#ifdef __ultrix 70
71#include <sys/syslog.h> 71#if defined(OPENSSL_SYS_WIN32)
72#else 72# include <process.h>
73#include <syslog.h> 73#elif defined(OPENSSL_SYS_VMS)
74#endif 74# include <opcdef.h>
75# include <descrip.h>
76# include <lib$routines.h>
77# include <starlet.h>
78#elif defined(__ultrix)
79# include <sys/syslog.h>
80#elif !defined(MSDOS) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG) /* Unix */
81# include <syslog.h>
75#endif 82#endif
76 83
77#include "cryptlib.h"
78#include <openssl/buffer.h> 84#include <openssl/buffer.h>
79#include <openssl/err.h> 85#include <openssl/err.h>
86
80#ifndef NO_SYSLOG 87#ifndef NO_SYSLOG
81 88
89#if defined(OPENSSL_SYS_WIN32)
90#define LOG_EMERG 0
91#define LOG_ALERT 1
92#define LOG_CRIT 2
93#define LOG_ERR 3
94#define LOG_WARNING 4
95#define LOG_NOTICE 5
96#define LOG_INFO 6
97#define LOG_DEBUG 7
82 98
83static int MS_CALLBACK slg_write(BIO *h,char *buf,int num); 99#define LOG_DAEMON (3<<3)
84static int MS_CALLBACK slg_puts(BIO *h,char *str); 100#elif defined(OPENSSL_SYS_VMS)
85static long MS_CALLBACK slg_ctrl(BIO *h,int cmd,long arg1,char *arg2); 101/* On VMS, we don't really care about these, but we need them to compile */
102#define LOG_EMERG 0
103#define LOG_ALERT 1
104#define LOG_CRIT 2
105#define LOG_ERR 3
106#define LOG_WARNING 4
107#define LOG_NOTICE 5
108#define LOG_INFO 6
109#define LOG_DEBUG 7
110
111#define LOG_DAEMON OPC$M_NM_NTWORK
112#endif
113
114static int MS_CALLBACK slg_write(BIO *h, const char *buf, int num);
115static int MS_CALLBACK slg_puts(BIO *h, const char *str);
116static long MS_CALLBACK slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
86static int MS_CALLBACK slg_new(BIO *h); 117static int MS_CALLBACK slg_new(BIO *h);
87static int MS_CALLBACK slg_free(BIO *data); 118static int MS_CALLBACK slg_free(BIO *data);
88static int xopenlog(BIO* bp, const char* name, int level); 119static void xopenlog(BIO* bp, char* name, int level);
89static int xcloselog(BIO* bp); 120static void xsyslog(BIO* bp, int priority, const char* string);
121static void xcloselog(BIO* bp);
122#ifdef OPENSSL_SYS_WIN32
123LONG (WINAPI *go_for_advapi)() = RegOpenKeyEx;
124HANDLE (WINAPI *register_event_source)() = NULL;
125BOOL (WINAPI *deregister_event_source)() = NULL;
126BOOL (WINAPI *report_event)() = NULL;
127#define DL_PROC(m,f) (GetProcAddress( m, f ))
128#ifdef UNICODE
129#define DL_PROC_X(m,f) DL_PROC( m, f "W" )
130#else
131#define DL_PROC_X(m,f) DL_PROC( m, f "A" )
132#endif
133#endif
90 134
91static BIO_METHOD methods_slg= 135static BIO_METHOD methods_slg=
92 { 136 {
@@ -98,6 +142,7 @@ static BIO_METHOD methods_slg=
98 slg_ctrl, 142 slg_ctrl,
99 slg_new, 143 slg_new,
100 slg_free, 144 slg_free,
145 NULL,
101 }; 146 };
102 147
103BIO_METHOD *BIO_s_log(void) 148BIO_METHOD *BIO_s_log(void)
@@ -110,11 +155,7 @@ static int MS_CALLBACK slg_new(BIO *bi)
110 bi->init=1; 155 bi->init=1;
111 bi->num=0; 156 bi->num=0;
112 bi->ptr=NULL; 157 bi->ptr=NULL;
113#ifndef WIN32
114 xopenlog(bi, "application", LOG_DAEMON); 158 xopenlog(bi, "application", LOG_DAEMON);
115#else
116 xopenlog(bi, "application", 0);
117#endif
118 return(1); 159 return(1);
119 } 160 }
120 161
@@ -125,64 +166,60 @@ static int MS_CALLBACK slg_free(BIO *a)
125 return(1); 166 return(1);
126 } 167 }
127 168
128static int MS_CALLBACK slg_write(BIO *b, char *in, int inl) 169static int MS_CALLBACK slg_write(BIO *b, const char *in, int inl)
129 { 170 {
130 int ret= inl; 171 int ret= inl;
131 char* buf= in; 172 char* buf;
132 char* pp; 173 char* pp;
133#if defined(WIN32) 174 int priority, i;
134 LPTSTR lpszStrings[1]; 175 static struct
135 WORD evtype= EVENTLOG_ERROR_TYPE; 176 {
136#else 177 int strl;
137 int priority; 178 char str[10];
138#endif 179 int log_level;
180 }
181 mapping[] =
182 {
183 { 6, "PANIC ", LOG_EMERG },
184 { 6, "EMERG ", LOG_EMERG },
185 { 4, "EMR ", LOG_EMERG },
186 { 6, "ALERT ", LOG_ALERT },
187 { 4, "ALR ", LOG_ALERT },
188 { 5, "CRIT ", LOG_CRIT },
189 { 4, "CRI ", LOG_CRIT },
190 { 6, "ERROR ", LOG_ERR },
191 { 4, "ERR ", LOG_ERR },
192 { 8, "WARNING ", LOG_WARNING },
193 { 5, "WARN ", LOG_WARNING },
194 { 4, "WAR ", LOG_WARNING },
195 { 7, "NOTICE ", LOG_NOTICE },
196 { 5, "NOTE ", LOG_NOTICE },
197 { 4, "NOT ", LOG_NOTICE },
198 { 5, "INFO ", LOG_INFO },
199 { 4, "INF ", LOG_INFO },
200 { 6, "DEBUG ", LOG_DEBUG },
201 { 4, "DBG ", LOG_DEBUG },
202 { 0, "", LOG_ERR } /* The default */
203 };
139 204
140 if((buf= (char *)Malloc(inl+ 1)) == NULL){ 205 if((buf= (char *)OPENSSL_malloc(inl+ 1)) == NULL){
141 return(0); 206 return(0);
142 } 207 }
143 strncpy(buf, in, inl); 208 strncpy(buf, in, inl);
144 buf[inl]= '\0'; 209 buf[inl]= '\0';
145#if defined(WIN32)
146 if(strncmp(buf, "ERR ", 4) == 0){
147 evtype= EVENTLOG_ERROR_TYPE;
148 pp= buf+ 4;
149 }else if(strncmp(buf, "WAR ", 4) == 0){
150 evtype= EVENTLOG_WARNING_TYPE;
151 pp= buf+ 4;
152 }else if(strncmp(buf, "INF ", 4) == 0){
153 evtype= EVENTLOG_INFORMATION_TYPE;
154 pp= buf+ 4;
155 }else{
156 evtype= EVENTLOG_ERROR_TYPE;
157 pp= buf;
158 }
159 lpszStrings[0]= pp;
160 210
161 if(b->ptr) 211 i = 0;
162 ReportEvent(b->ptr, evtype, 0, 1024, NULL, 1, 0, 212 while(strncmp(buf, mapping[i].str, mapping[i].strl) != 0) i++;
163 lpszStrings, NULL); 213 priority = mapping[i].log_level;
164#else 214 pp = buf + mapping[i].strl;
165 if(strncmp(buf, "ERR ", 4) == 0){
166 priority= LOG_ERR;
167 pp= buf+ 4;
168 }else if(strncmp(buf, "WAR ", 4) == 0){
169 priority= LOG_WARNING;
170 pp= buf+ 4;
171 }else if(strncmp(buf, "INF ", 4) == 0){
172 priority= LOG_INFO;
173 pp= buf+ 4;
174 }else{
175 priority= LOG_ERR;
176 pp= buf;
177 }
178 215
179 syslog(priority, "%s", pp); 216 xsyslog(b, priority, pp);
180#endif 217
181 Free(buf); 218 OPENSSL_free(buf);
182 return(ret); 219 return(ret);
183 } 220 }
184 221
185static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, char *ptr) 222static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, void *ptr)
186 { 223 {
187 switch (cmd) 224 switch (cmd)
188 { 225 {
@@ -196,7 +233,7 @@ static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, char *ptr)
196 return(0); 233 return(0);
197 } 234 }
198 235
199static int MS_CALLBACK slg_puts(BIO *bp, char *str) 236static int MS_CALLBACK slg_puts(BIO *bp, const char *str)
200 { 237 {
201 int n,ret; 238 int n,ret;
202 239
@@ -205,28 +242,154 @@ static int MS_CALLBACK slg_puts(BIO *bp, char *str)
205 return(ret); 242 return(ret);
206 } 243 }
207 244
208static int xopenlog(BIO* bp, const char* name, int level) 245#if defined(OPENSSL_SYS_WIN32)
246
247static void xopenlog(BIO* bp, char* name, int level)
209{ 248{
210#if defined(WIN32) 249 if ( !register_event_source )
211 if((bp->ptr= (char *)RegisterEventSource(NULL, name)) == NULL){ 250 {
212 return(0); 251 HANDLE advapi;
213 } 252 if ( !(advapi = GetModuleHandle("advapi32")) )
214#else 253 return;
215 openlog(name, LOG_PID|LOG_CONS, level); 254 register_event_source = (HANDLE (WINAPI *)())DL_PROC_X(advapi,
216#endif 255 "RegisterEventSource" );
217 return(1); 256 deregister_event_source = (BOOL (WINAPI *)())DL_PROC(advapi,
257 "DeregisterEventSource");
258 report_event = (BOOL (WINAPI *)())DL_PROC_X(advapi,
259 "ReportEvent" );
260 if ( !(register_event_source && deregister_event_source &&
261 report_event) )
262 {
263 register_event_source = NULL;
264 deregister_event_source = NULL;
265 report_event = NULL;
266 return;
267 }
268 }
269 bp->ptr= (char *)register_event_source(NULL, name);
218} 270}
219 271
220static int xcloselog(BIO* bp) 272static void xsyslog(BIO *bp, int priority, const char *string)
273{
274 LPCSTR lpszStrings[2];
275 WORD evtype= EVENTLOG_ERROR_TYPE;
276 int pid = _getpid();
277 char pidbuf[20];
278
279 switch (priority)
280 {
281 case LOG_EMERG:
282 case LOG_ALERT:
283 case LOG_CRIT:
284 case LOG_ERR:
285 evtype = EVENTLOG_ERROR_TYPE;
286 break;
287 case LOG_WARNING:
288 evtype = EVENTLOG_WARNING_TYPE;
289 break;
290 case LOG_NOTICE:
291 case LOG_INFO:
292 case LOG_DEBUG:
293 evtype = EVENTLOG_INFORMATION_TYPE;
294 break;
295 default: /* Should never happen, but set it
296 as error anyway. */
297 evtype = EVENTLOG_ERROR_TYPE;
298 break;
299 }
300
301 sprintf(pidbuf, "[%d] ", pid);
302 lpszStrings[0] = pidbuf;
303 lpszStrings[1] = string;
304
305 if(report_event && bp->ptr)
306 report_event(bp->ptr, evtype, 0, 1024, NULL, 2, 0,
307 lpszStrings, NULL);
308}
309
310static void xcloselog(BIO* bp)
221{ 311{
222#if defined(WIN32) 312 if(deregister_event_source && bp->ptr)
223 if(bp->ptr) 313 deregister_event_source((HANDLE)(bp->ptr));
224 DeregisterEventSource((HANDLE)(bp->ptr));
225 bp->ptr= NULL; 314 bp->ptr= NULL;
226#else 315}
316
317#elif defined(OPENSSL_SYS_VMS)
318
319static int VMS_OPC_target = LOG_DAEMON;
320
321static void xopenlog(BIO* bp, char* name, int level)
322{
323 VMS_OPC_target = level;
324}
325
326static void xsyslog(BIO *bp, int priority, const char *string)
327{
328 struct dsc$descriptor_s opc_dsc;
329 struct opcdef *opcdef_p;
330 char buf[10240];
331 unsigned int len;
332 struct dsc$descriptor_s buf_dsc;
333 $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
334 char *priority_tag;
335
336 switch (priority)
337 {
338 case LOG_EMERG: priority_tag = "Emergency"; break;
339 case LOG_ALERT: priority_tag = "Alert"; break;
340 case LOG_CRIT: priority_tag = "Critical"; break;
341 case LOG_ERR: priority_tag = "Error"; break;
342 case LOG_WARNING: priority_tag = "Warning"; break;
343 case LOG_NOTICE: priority_tag = "Notice"; break;
344 case LOG_INFO: priority_tag = "Info"; break;
345 case LOG_DEBUG: priority_tag = "DEBUG"; break;
346 }
347
348 buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
349 buf_dsc.dsc$b_class = DSC$K_CLASS_S;
350 buf_dsc.dsc$a_pointer = buf;
351 buf_dsc.dsc$w_length = sizeof(buf) - 1;
352
353 lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
354
355 /* we know there's an 8 byte header. That's documented */
356 opcdef_p = (struct opcdef *) OPENSSL_malloc(8 + len);
357 opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
358 memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
359 opcdef_p->opc$l_ms_rqstid = 0;
360 memcpy(&opcdef_p->opc$l_ms_text, buf, len);
361
362 opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
363 opc_dsc.dsc$b_class = DSC$K_CLASS_S;
364 opc_dsc.dsc$a_pointer = (char *)opcdef_p;
365 opc_dsc.dsc$w_length = len + 8;
366
367 sys$sndopr(opc_dsc, 0);
368
369 OPENSSL_free(opcdef_p);
370}
371
372static void xcloselog(BIO* bp)
373{
374}
375
376#else /* Unix */
377
378static void xopenlog(BIO* bp, char* name, int level)
379{
380 openlog(name, LOG_PID|LOG_CONS, level);
381}
382
383static void xsyslog(BIO *bp, int priority, const char *string)
384{
385 syslog(priority, "%s", string);
386}
387
388static void xcloselog(BIO* bp)
389{
227 closelog(); 390 closelog();
228#endif
229 return(1);
230} 391}
231 392
232#endif 393#endif /* Unix */
394
395#endif /* NO_SYSLOG */