bss_log.c 9.9 KB

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