1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/bio/bss_log.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,432 @@
1.4 +/* crypto/bio/bss_log.c */
1.5 +/* ====================================================================
1.6 + * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
1.7 + *
1.8 + * Redistribution and use in source and binary forms, with or without
1.9 + * modification, are permitted provided that the following conditions
1.10 + * are met:
1.11 + *
1.12 + * 1. Redistributions of source code must retain the above copyright
1.13 + * notice, this list of conditions and the following disclaimer.
1.14 + *
1.15 + * 2. Redistributions in binary form must reproduce the above copyright
1.16 + * notice, this list of conditions and the following disclaimer in
1.17 + * the documentation and/or other materials provided with the
1.18 + * distribution.
1.19 + *
1.20 + * 3. All advertising materials mentioning features or use of this
1.21 + * software must display the following acknowledgment:
1.22 + * "This product includes software developed by the OpenSSL Project
1.23 + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
1.24 + *
1.25 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1.26 + * endorse or promote products derived from this software without
1.27 + * prior written permission. For written permission, please contact
1.28 + * licensing@OpenSSL.org.
1.29 + *
1.30 + * 5. Products derived from this software may not be called "OpenSSL"
1.31 + * nor may "OpenSSL" appear in their names without prior written
1.32 + * permission of the OpenSSL Project.
1.33 + *
1.34 + * 6. Redistributions of any form whatsoever must retain the following
1.35 + * acknowledgment:
1.36 + * "This product includes software developed by the OpenSSL Project
1.37 + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
1.38 + *
1.39 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1.40 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.41 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.42 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1.43 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1.44 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1.45 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1.46 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.47 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.48 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.49 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1.50 + * OF THE POSSIBILITY OF SUCH DAMAGE.
1.51 + * ====================================================================
1.52 + *
1.53 + * This product includes cryptographic software written by Eric Young
1.54 + * (eay@cryptsoft.com). This product includes software written by Tim
1.55 + * Hudson (tjh@cryptsoft.com).
1.56 + *
1.57 + */
1.58 +/*
1.59 + © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
1.60 + */
1.61 +/*
1.62 + Why BIO_s_log?
1.63 +
1.64 + BIO_s_log is useful for system daemons (or services under NT).
1.65 + It is one-way BIO, it sends all stuff to syslogd (on system that
1.66 + commonly use that), or event log (on NT), or OPCOM (on OpenVMS).
1.67 +
1.68 +*/
1.69 +
1.70 +
1.71 +#include <stdio.h>
1.72 +#include <errno.h>
1.73 +
1.74 +#include "cryptlib.h"
1.75 +
1.76 +#if defined(OPENSSL_SYS_WINCE)
1.77 +#elif defined(OPENSSL_SYS_WIN32)
1.78 +# include <process.h>
1.79 +#elif defined(OPENSSL_SYS_VMS)
1.80 +# include <opcdef.h>
1.81 +# include <descrip.h>
1.82 +# include <lib$routines.h>
1.83 +# include <starlet.h>
1.84 +#elif defined(__ultrix)
1.85 +# include <sys/syslog.h>
1.86 +#elif defined(OPENSSL_SYS_NETWARE)
1.87 +# define NO_SYSLOG
1.88 +#elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG)
1.89 +# include <syslog.h>
1.90 +#endif
1.91 +
1.92 +#include <openssl/buffer.h>
1.93 +#include <openssl/err.h>
1.94 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
1.95 +#include "libcrypto_wsd_macros.h"
1.96 +#include "libcrypto_wsd.h"
1.97 +#endif
1.98 +
1.99 +
1.100 +#ifndef NO_SYSLOG
1.101 +
1.102 +#if defined(OPENSSL_SYS_WIN32)
1.103 +#define LOG_EMERG 0
1.104 +#define LOG_ALERT 1
1.105 +#define LOG_CRIT 2
1.106 +#define LOG_ERR 3
1.107 +#define LOG_WARNING 4
1.108 +#define LOG_NOTICE 5
1.109 +#define LOG_INFO 6
1.110 +#define LOG_DEBUG 7
1.111 +
1.112 +#define LOG_DAEMON (3<<3)
1.113 +#elif defined(OPENSSL_SYS_VMS)
1.114 +/* On VMS, we don't really care about these, but we need them to compile */
1.115 +#define LOG_EMERG 0
1.116 +#define LOG_ALERT 1
1.117 +#define LOG_CRIT 2
1.118 +#define LOG_ERR 3
1.119 +#define LOG_WARNING 4
1.120 +#define LOG_NOTICE 5
1.121 +#define LOG_INFO 6
1.122 +#define LOG_DEBUG 7
1.123 +
1.124 +#define LOG_DAEMON OPC$M_NM_NTWORK
1.125 +#endif
1.126 +
1.127 +static int MS_CALLBACK slg_write(BIO *h, const char *buf, int num);
1.128 +static int MS_CALLBACK slg_puts(BIO *h, const char *str);
1.129 +static long MS_CALLBACK slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
1.130 +static int MS_CALLBACK slg_new(BIO *h);
1.131 +static int MS_CALLBACK slg_free(BIO *data);
1.132 +static void xopenlog(BIO* bp, char* name, int level);
1.133 +static void xsyslog(BIO* bp, int priority, const char* string);
1.134 +static void xcloselog(BIO* bp);
1.135 +#ifdef OPENSSL_SYS_WIN32
1.136 +LONG (WINAPI *go_for_advapi)() = RegOpenKeyEx;
1.137 +HANDLE (WINAPI *register_event_source)() = NULL;
1.138 +BOOL (WINAPI *deregister_event_source)() = NULL;
1.139 +BOOL (WINAPI *report_event)() = NULL;
1.140 +#define DL_PROC(m,f) (GetProcAddress( m, f ))
1.141 +#ifdef UNICODE
1.142 +#define DL_PROC_X(m,f) DL_PROC( m, f "W" )
1.143 +#else
1.144 +#define DL_PROC_X(m,f) DL_PROC( m, f "A" )
1.145 +#endif
1.146 +#endif
1.147 +
1.148 +#ifndef EMULATOR
1.149 +static BIO_METHOD methods_slg=
1.150 + {
1.151 + BIO_TYPE_MEM,"syslog",
1.152 + slg_write,
1.153 + NULL,
1.154 + slg_puts,
1.155 + NULL,
1.156 + slg_ctrl,
1.157 + slg_new,
1.158 + slg_free,
1.159 + NULL,
1.160 + };
1.161 +#else
1.162 +
1.163 +GET_STATIC_VAR_FROM_TLS(methods_slg,bss_log,BIO_METHOD)
1.164 +#define methods_slg (*GET_WSD_VAR_NAME(methods_slg,bss_log,s)())
1.165 +const BIO_METHOD temp_s_methods_slg=
1.166 + {
1.167 + BIO_TYPE_MEM,"syslog",
1.168 + slg_write,
1.169 + NULL,
1.170 + slg_puts,
1.171 + NULL,
1.172 + slg_ctrl,
1.173 + slg_new,
1.174 + slg_free,
1.175 + NULL,
1.176 + };
1.177 +
1.178 +#endif
1.179 +
1.180 +EXPORT_C BIO_METHOD *BIO_s_log(void)
1.181 + {
1.182 + return(&methods_slg);
1.183 + }
1.184 +
1.185 +static int MS_CALLBACK slg_new(BIO *bi)
1.186 + {
1.187 + bi->init=1;
1.188 + bi->num=0;
1.189 + bi->ptr=NULL;
1.190 + xopenlog(bi, "application", LOG_DAEMON);
1.191 + return(1);
1.192 + }
1.193 +
1.194 +static int MS_CALLBACK slg_free(BIO *a)
1.195 + {
1.196 + if (a == NULL) return(0);
1.197 + xcloselog(a);
1.198 + return(1);
1.199 + }
1.200 +
1.201 +static int MS_CALLBACK slg_write(BIO *b, const char *in, int inl)
1.202 + {
1.203 + int ret= inl;
1.204 + char* buf;
1.205 + char* pp;
1.206 + int priority, i;
1.207 + static struct
1.208 + {
1.209 + int strl;
1.210 + char str[10];
1.211 + int log_level;
1.212 + }
1.213 + mapping[] =
1.214 + {
1.215 + { 6, "PANIC ", LOG_EMERG },
1.216 + { 6, "EMERG ", LOG_EMERG },
1.217 + { 4, "EMR ", LOG_EMERG },
1.218 + { 6, "ALERT ", LOG_ALERT },
1.219 + { 4, "ALR ", LOG_ALERT },
1.220 + { 5, "CRIT ", LOG_CRIT },
1.221 + { 4, "CRI ", LOG_CRIT },
1.222 + { 6, "ERROR ", LOG_ERR },
1.223 + { 4, "ERR ", LOG_ERR },
1.224 + { 8, "WARNING ", LOG_WARNING },
1.225 + { 5, "WARN ", LOG_WARNING },
1.226 + { 4, "WAR ", LOG_WARNING },
1.227 + { 7, "NOTICE ", LOG_NOTICE },
1.228 + { 5, "NOTE ", LOG_NOTICE },
1.229 + { 4, "NOT ", LOG_NOTICE },
1.230 + { 5, "INFO ", LOG_INFO },
1.231 + { 4, "INF ", LOG_INFO },
1.232 + { 6, "DEBUG ", LOG_DEBUG },
1.233 + { 4, "DBG ", LOG_DEBUG },
1.234 + { 0, "", LOG_ERR } /* The default */
1.235 + };
1.236 +
1.237 + if((buf= (char *)OPENSSL_malloc(inl+ 1)) == NULL){
1.238 + return(0);
1.239 + }
1.240 + strncpy(buf, in, inl);
1.241 + buf[inl]= '\0';
1.242 +
1.243 + i = 0;
1.244 + while(strncmp(buf, mapping[i].str, mapping[i].strl) != 0) i++;
1.245 + priority = mapping[i].log_level;
1.246 + pp = buf + mapping[i].strl;
1.247 +
1.248 + xsyslog(b, priority, pp);
1.249 +
1.250 + OPENSSL_free(buf);
1.251 + return(ret);
1.252 + }
1.253 +
1.254 +static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, void *ptr)
1.255 + {
1.256 + switch (cmd)
1.257 + {
1.258 + case BIO_CTRL_SET:
1.259 + xcloselog(b);
1.260 + xopenlog(b, ptr, num);
1.261 + break;
1.262 + default:
1.263 + break;
1.264 + }
1.265 + return(0);
1.266 + }
1.267 +
1.268 +static int MS_CALLBACK slg_puts(BIO *bp, const char *str)
1.269 + {
1.270 + int n,ret;
1.271 +
1.272 + n=strlen(str);
1.273 + ret=slg_write(bp,str,n);
1.274 + return(ret);
1.275 + }
1.276 +
1.277 +#if defined(OPENSSL_SYS_WIN32)
1.278 +
1.279 +static void xopenlog(BIO* bp, char* name, int level)
1.280 +{
1.281 + if ( !register_event_source )
1.282 + {
1.283 + HANDLE advapi;
1.284 + if ( !(advapi = GetModuleHandle("advapi32")) )
1.285 + return;
1.286 + register_event_source = (HANDLE (WINAPI *)())DL_PROC_X(advapi,
1.287 + "RegisterEventSource" );
1.288 + deregister_event_source = (BOOL (WINAPI *)())DL_PROC(advapi,
1.289 + "DeregisterEventSource");
1.290 + report_event = (BOOL (WINAPI *)())DL_PROC_X(advapi,
1.291 + "ReportEvent" );
1.292 + if ( !(register_event_source && deregister_event_source &&
1.293 + report_event) )
1.294 + {
1.295 + register_event_source = NULL;
1.296 + deregister_event_source = NULL;
1.297 + report_event = NULL;
1.298 + return;
1.299 + }
1.300 + }
1.301 + bp->ptr= (char *)register_event_source(NULL, name);
1.302 +}
1.303 +
1.304 +static void xsyslog(BIO *bp, int priority, const char *string)
1.305 +{
1.306 + LPCSTR lpszStrings[2];
1.307 + WORD evtype= EVENTLOG_ERROR_TYPE;
1.308 + int pid = _getpid();
1.309 + char pidbuf[DECIMAL_SIZE(pid)+4];
1.310 +
1.311 + switch (priority)
1.312 + {
1.313 + case LOG_EMERG:
1.314 + case LOG_ALERT:
1.315 + case LOG_CRIT:
1.316 + case LOG_ERR:
1.317 + evtype = EVENTLOG_ERROR_TYPE;
1.318 + break;
1.319 + case LOG_WARNING:
1.320 + evtype = EVENTLOG_WARNING_TYPE;
1.321 + break;
1.322 + case LOG_NOTICE:
1.323 + case LOG_INFO:
1.324 + case LOG_DEBUG:
1.325 + evtype = EVENTLOG_INFORMATION_TYPE;
1.326 + break;
1.327 + default: /* Should never happen, but set it
1.328 + as error anyway. */
1.329 + evtype = EVENTLOG_ERROR_TYPE;
1.330 + break;
1.331 + }
1.332 +
1.333 + sprintf(pidbuf, "[%d] ", pid);
1.334 + lpszStrings[0] = pidbuf;
1.335 + lpszStrings[1] = string;
1.336 +
1.337 + if(report_event && bp->ptr)
1.338 + report_event(bp->ptr, evtype, 0, 1024, NULL, 2, 0,
1.339 + lpszStrings, NULL);
1.340 +}
1.341 +
1.342 +static void xcloselog(BIO* bp)
1.343 +{
1.344 + if(deregister_event_source && bp->ptr)
1.345 + deregister_event_source((HANDLE)(bp->ptr));
1.346 + bp->ptr= NULL;
1.347 +}
1.348 +
1.349 +#elif defined(OPENSSL_SYS_VMS)
1.350 +
1.351 +static int VMS_OPC_target = LOG_DAEMON;
1.352 +
1.353 +static void xopenlog(BIO* bp, char* name, int level)
1.354 +{
1.355 + VMS_OPC_target = level;
1.356 +}
1.357 +
1.358 +static void xsyslog(BIO *bp, int priority, const char *string)
1.359 +{
1.360 + struct dsc$descriptor_s opc_dsc;
1.361 + struct opcdef *opcdef_p;
1.362 +#ifndef SYMBAIN
1.363 + char buf[10240];
1.364 +#else
1.365 + char buf[100];
1.366 +#endif
1.367 + unsigned int len;
1.368 + struct dsc$descriptor_s buf_dsc;
1.369 + $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
1.370 + char *priority_tag;
1.371 +
1.372 + switch (priority)
1.373 + {
1.374 + case LOG_EMERG: priority_tag = "Emergency"; break;
1.375 + case LOG_ALERT: priority_tag = "Alert"; break;
1.376 + case LOG_CRIT: priority_tag = "Critical"; break;
1.377 + case LOG_ERR: priority_tag = "Error"; break;
1.378 + case LOG_WARNING: priority_tag = "Warning"; break;
1.379 + case LOG_NOTICE: priority_tag = "Notice"; break;
1.380 + case LOG_INFO: priority_tag = "Info"; break;
1.381 + case LOG_DEBUG: priority_tag = "DEBUG"; break;
1.382 + }
1.383 +
1.384 + buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
1.385 + buf_dsc.dsc$b_class = DSC$K_CLASS_S;
1.386 + buf_dsc.dsc$a_pointer = buf;
1.387 + buf_dsc.dsc$w_length = sizeof(buf) - 1;
1.388 +
1.389 + lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
1.390 +
1.391 + /* we know there's an 8 byte header. That's documented */
1.392 + opcdef_p = (struct opcdef *) OPENSSL_malloc(8 + len);
1.393 + opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
1.394 + memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
1.395 + opcdef_p->opc$l_ms_rqstid = 0;
1.396 + memcpy(&opcdef_p->opc$l_ms_text, buf, len);
1.397 +
1.398 + opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
1.399 + opc_dsc.dsc$b_class = DSC$K_CLASS_S;
1.400 + opc_dsc.dsc$a_pointer = (char *)opcdef_p;
1.401 + opc_dsc.dsc$w_length = len + 8;
1.402 +
1.403 + sys$sndopr(opc_dsc, 0);
1.404 +
1.405 + OPENSSL_free(opcdef_p);
1.406 +}
1.407 +
1.408 +static void xcloselog(BIO* bp)
1.409 +{
1.410 +}
1.411 +
1.412 +#else /* Unix/Watt32 */
1.413 +
1.414 +static void xopenlog(BIO* bp, char* name, int level)
1.415 +{
1.416 +#ifdef WATT32 /* djgpp/DOS */
1.417 + openlog(name, LOG_PID|LOG_CONS|LOG_NDELAY, level);
1.418 +#else
1.419 + openlog(name, LOG_PID|LOG_CONS, level);
1.420 +#endif
1.421 +}
1.422 +
1.423 +static void xsyslog(BIO *bp, int priority, const char *string)
1.424 +{
1.425 + syslog(priority, "%s", string);
1.426 +}
1.427 +
1.428 +static void xcloselog(BIO* bp)
1.429 +{
1.430 + closelog();
1.431 +}
1.432 +
1.433 +#endif /* Unix */
1.434 +
1.435 +#endif /* NO_SYSLOG */