bss_log.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. *
  58. * BIO_s_log is useful for system daemons (or services under NT). It is
  59. * one-way BIO, it sends all stuff to syslogd (on system that commonly use
  60. * that), or event log (on NT), or OPCOM (on OpenVMS).
  61. *
  62. */
  63. #include <stdio.h>
  64. #include <errno.h>
  65. #include "cryptlib.h"
  66. #if defined(OPENSSL_SYS_WINCE)
  67. #elif defined(OPENSSL_SYS_WIN32)
  68. #elif defined(OPENSSL_SYS_VMS)
  69. # include <opcdef.h>
  70. # include <descrip.h>
  71. # include <lib$routines.h>
  72. # include <starlet.h>
  73. /* Some compiler options may mask the declaration of "_malloc32". */
  74. # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
  75. # if __INITIAL_POINTER_SIZE == 64
  76. # pragma pointer_size save
  77. # pragma pointer_size 32
  78. void *_malloc32(__size_t);
  79. # pragma pointer_size restore
  80. # endif /* __INITIAL_POINTER_SIZE == 64 */
  81. # endif /* __INITIAL_POINTER_SIZE && defined
  82. * _ANSI_C_SOURCE */
  83. #elif defined(__ultrix)
  84. # include <sys/syslog.h>
  85. #elif defined(OPENSSL_SYS_NETWARE)
  86. # define NO_SYSLOG
  87. #elif (!defined(MSDOS) || defined(WATT32)) && !defined(OPENSSL_SYS_VXWORKS) && !defined(NO_SYSLOG)
  88. # include <syslog.h>
  89. #endif
  90. #include <openssl/buffer.h>
  91. #include <openssl/err.h>
  92. #ifndef NO_SYSLOG
  93. # if defined(OPENSSL_SYS_WIN32)
  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 (3<<3)
  103. # elif defined(OPENSSL_SYS_VMS)
  104. /* On VMS, we don't really care about these, but we need them to compile */
  105. # define LOG_EMERG 0
  106. # define LOG_ALERT 1
  107. # define LOG_CRIT 2
  108. # define LOG_ERR 3
  109. # define LOG_WARNING 4
  110. # define LOG_NOTICE 5
  111. # define LOG_INFO 6
  112. # define LOG_DEBUG 7
  113. # define LOG_DAEMON OPC$M_NM_NTWORK
  114. # endif
  115. static int MS_CALLBACK slg_write(BIO *h, const char *buf, int num);
  116. static int MS_CALLBACK slg_puts(BIO *h, const char *str);
  117. static long MS_CALLBACK slg_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  118. static int MS_CALLBACK slg_new(BIO *h);
  119. static int MS_CALLBACK slg_free(BIO *data);
  120. static void xopenlog(BIO *bp, char *name, int level);
  121. static void xsyslog(BIO *bp, int priority, const char *string);
  122. static void xcloselog(BIO *bp);
  123. static BIO_METHOD methods_slg = {
  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)
  149. return (0);
  150. xcloselog(a);
  151. return (1);
  152. }
  153. static int MS_CALLBACK slg_write(BIO *b, const char *in, int inl)
  154. {
  155. int ret = inl;
  156. char *buf;
  157. char *pp;
  158. int priority, i;
  159. static const struct {
  160. int strl;
  161. char str[10];
  162. int log_level;
  163. } mapping[] = {
  164. {
  165. 6, "PANIC ", LOG_EMERG
  166. },
  167. {
  168. 6, "EMERG ", LOG_EMERG
  169. },
  170. {
  171. 4, "EMR ", LOG_EMERG
  172. },
  173. {
  174. 6, "ALERT ", LOG_ALERT
  175. },
  176. {
  177. 4, "ALR ", LOG_ALERT
  178. },
  179. {
  180. 5, "CRIT ", LOG_CRIT
  181. },
  182. {
  183. 4, "CRI ", LOG_CRIT
  184. },
  185. {
  186. 6, "ERROR ", LOG_ERR
  187. },
  188. {
  189. 4, "ERR ", LOG_ERR
  190. },
  191. {
  192. 8, "WARNING ", LOG_WARNING
  193. },
  194. {
  195. 5, "WARN ", LOG_WARNING
  196. },
  197. {
  198. 4, "WAR ", LOG_WARNING
  199. },
  200. {
  201. 7, "NOTICE ", LOG_NOTICE
  202. },
  203. {
  204. 5, "NOTE ", LOG_NOTICE
  205. },
  206. {
  207. 4, "NOT ", LOG_NOTICE
  208. },
  209. {
  210. 5, "INFO ", LOG_INFO
  211. },
  212. {
  213. 4, "INF ", LOG_INFO
  214. },
  215. {
  216. 6, "DEBUG ", LOG_DEBUG
  217. },
  218. {
  219. 4, "DBG ", LOG_DEBUG
  220. },
  221. {
  222. 0, "", LOG_ERR
  223. }
  224. /* The default */
  225. };
  226. if ((buf = (char *)OPENSSL_malloc(inl + 1)) == NULL) {
  227. return (0);
  228. }
  229. strncpy(buf, in, inl);
  230. buf[inl] = '\0';
  231. i = 0;
  232. while (strncmp(buf, mapping[i].str, mapping[i].strl) != 0)
  233. i++;
  234. priority = mapping[i].log_level;
  235. pp = buf + mapping[i].strl;
  236. xsyslog(b, priority, pp);
  237. OPENSSL_free(buf);
  238. return (ret);
  239. }
  240. static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, void *ptr)
  241. {
  242. switch (cmd) {
  243. case BIO_CTRL_SET:
  244. xcloselog(b);
  245. xopenlog(b, ptr, num);
  246. break;
  247. default:
  248. break;
  249. }
  250. return (0);
  251. }
  252. static int MS_CALLBACK slg_puts(BIO *bp, const char *str)
  253. {
  254. int n, ret;
  255. n = strlen(str);
  256. ret = slg_write(bp, str, n);
  257. return (ret);
  258. }
  259. # if defined(OPENSSL_SYS_WIN32)
  260. static void xopenlog(BIO *bp, char *name, int level)
  261. {
  262. if (check_winnt())
  263. bp->ptr = RegisterEventSourceA(NULL, name);
  264. else
  265. bp->ptr = NULL;
  266. }
  267. static void xsyslog(BIO *bp, int priority, const char *string)
  268. {
  269. LPCSTR lpszStrings[2];
  270. WORD evtype = EVENTLOG_ERROR_TYPE;
  271. char pidbuf[DECIMAL_SIZE(DWORD) + 4];
  272. if (bp->ptr == NULL)
  273. return;
  274. switch (priority) {
  275. case LOG_EMERG:
  276. case LOG_ALERT:
  277. case LOG_CRIT:
  278. case LOG_ERR:
  279. evtype = EVENTLOG_ERROR_TYPE;
  280. break;
  281. case LOG_WARNING:
  282. evtype = EVENTLOG_WARNING_TYPE;
  283. break;
  284. case LOG_NOTICE:
  285. case LOG_INFO:
  286. case LOG_DEBUG:
  287. evtype = EVENTLOG_INFORMATION_TYPE;
  288. break;
  289. default:
  290. /*
  291. * Should never happen, but set it
  292. * as error anyway.
  293. */
  294. evtype = EVENTLOG_ERROR_TYPE;
  295. break;
  296. }
  297. sprintf(pidbuf, "[%u] ", GetCurrentProcessId());
  298. lpszStrings[0] = pidbuf;
  299. lpszStrings[1] = string;
  300. ReportEventA(bp->ptr, evtype, 0, 1024, NULL, 2, 0, lpszStrings, NULL);
  301. }
  302. static void xcloselog(BIO *bp)
  303. {
  304. if (bp->ptr)
  305. DeregisterEventSource((HANDLE) (bp->ptr));
  306. bp->ptr = NULL;
  307. }
  308. # elif defined(OPENSSL_SYS_VMS)
  309. static int VMS_OPC_target = LOG_DAEMON;
  310. static void xopenlog(BIO *bp, char *name, int level)
  311. {
  312. VMS_OPC_target = level;
  313. }
  314. static void xsyslog(BIO *bp, int priority, const char *string)
  315. {
  316. struct dsc$descriptor_s opc_dsc;
  317. /* Arrange 32-bit pointer to opcdef buffer and malloc(), if needed. */
  318. # if __INITIAL_POINTER_SIZE == 64
  319. # pragma pointer_size save
  320. # pragma pointer_size 32
  321. # define OPCDEF_TYPE __char_ptr32
  322. # define OPCDEF_MALLOC _malloc32
  323. # else /* __INITIAL_POINTER_SIZE == 64 */
  324. # define OPCDEF_TYPE char *
  325. # define OPCDEF_MALLOC OPENSSL_malloc
  326. # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
  327. struct opcdef *opcdef_p;
  328. # if __INITIAL_POINTER_SIZE == 64
  329. # pragma pointer_size restore
  330. # endif /* __INITIAL_POINTER_SIZE == 64 */
  331. char buf[10240];
  332. unsigned int len;
  333. struct dsc$descriptor_s buf_dsc;
  334. $DESCRIPTOR(fao_cmd, "!AZ: !AZ");
  335. char *priority_tag;
  336. switch (priority) {
  337. case LOG_EMERG:
  338. priority_tag = "Emergency";
  339. break;
  340. case LOG_ALERT:
  341. priority_tag = "Alert";
  342. break;
  343. case LOG_CRIT:
  344. priority_tag = "Critical";
  345. break;
  346. case LOG_ERR:
  347. priority_tag = "Error";
  348. break;
  349. case LOG_WARNING:
  350. priority_tag = "Warning";
  351. break;
  352. case LOG_NOTICE:
  353. priority_tag = "Notice";
  354. break;
  355. case LOG_INFO:
  356. priority_tag = "Info";
  357. break;
  358. case LOG_DEBUG:
  359. priority_tag = "DEBUG";
  360. break;
  361. }
  362. buf_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  363. buf_dsc.dsc$b_class = DSC$K_CLASS_S;
  364. buf_dsc.dsc$a_pointer = buf;
  365. buf_dsc.dsc$w_length = sizeof(buf) - 1;
  366. lib$sys_fao(&fao_cmd, &len, &buf_dsc, priority_tag, string);
  367. /* We know there's an 8-byte header. That's documented. */
  368. opcdef_p = OPCDEF_MALLOC(8 + len);
  369. opcdef_p->opc$b_ms_type = OPC$_RQ_RQST;
  370. memcpy(opcdef_p->opc$z_ms_target_classes, &VMS_OPC_target, 3);
  371. opcdef_p->opc$l_ms_rqstid = 0;
  372. memcpy(&opcdef_p->opc$l_ms_text, buf, len);
  373. opc_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  374. opc_dsc.dsc$b_class = DSC$K_CLASS_S;
  375. opc_dsc.dsc$a_pointer = (OPCDEF_TYPE) opcdef_p;
  376. opc_dsc.dsc$w_length = len + 8;
  377. sys$sndopr(opc_dsc, 0);
  378. OPENSSL_free(opcdef_p);
  379. }
  380. static void xcloselog(BIO *bp)
  381. {
  382. }
  383. # else /* Unix/Watt32 */
  384. static void xopenlog(BIO *bp, char *name, int level)
  385. {
  386. # ifdef WATT32 /* djgpp/DOS */
  387. openlog(name, LOG_PID | LOG_CONS | LOG_NDELAY, level);
  388. # else
  389. openlog(name, LOG_PID | LOG_CONS, level);
  390. # endif
  391. }
  392. static void xsyslog(BIO *bp, int priority, const char *string)
  393. {
  394. syslog(priority, "%s", string);
  395. }
  396. static void xcloselog(BIO *bp)
  397. {
  398. closelog();
  399. }
  400. # endif /* Unix */
  401. #endif /* NO_SYSLOG */