bss_log.c 10 KB

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