bss_log.c 9.2 KB

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