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