bss_log.c 10 KB

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