php_syslog.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Philip Prindeville <philipp@redfish-solutions.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include "php.h"
  20. #include "php_syslog.h"
  21. #include "zend.h"
  22. #include "zend_smart_string.h"
  23. /*
  24. * The SCO OpenServer 5 Development System (not the UDK)
  25. * defines syslog to std_syslog.
  26. */
  27. #ifdef HAVE_STD_SYSLOG
  28. #define syslog std_syslog
  29. #endif
  30. PHPAPI void php_syslog_str(int priority, const zend_string* message)
  31. {
  32. smart_string sbuf = {0};
  33. if (PG(syslog_filter) == PHP_SYSLOG_FILTER_RAW) {
  34. /* Just send it directly to the syslog */
  35. syslog(priority, "%s", ZSTR_VAL(message));
  36. return;
  37. }
  38. /* We use < because we don't want the final NUL byte to be converted to '\x00' */
  39. for (size_t i = 0; i < ZSTR_LEN(message); ++i) {
  40. unsigned char c = ZSTR_VAL(message)[i];
  41. /* check for NVT ASCII only unless test disabled */
  42. if (((0x20 <= c) && (c <= 0x7e))) {
  43. smart_string_appendc(&sbuf, c);
  44. } else if ((c >= 0x80) && (PG(syslog_filter) != PHP_SYSLOG_FILTER_ASCII)) {
  45. smart_string_appendc(&sbuf, c);
  46. } else if (c == '\n') {
  47. /* Smart string is not NUL terminated */
  48. syslog(priority, "%.*s", (int)sbuf.len, sbuf.c);
  49. smart_string_reset(&sbuf);
  50. } else if ((c < 0x20) && (PG(syslog_filter) == PHP_SYSLOG_FILTER_ALL)) {
  51. smart_string_appendc(&sbuf, c);
  52. } else {
  53. const char xdigits[] = "0123456789abcdef";
  54. smart_string_appendl(&sbuf, "\\x", 2);
  55. smart_string_appendc(&sbuf, xdigits[c >> 4]);
  56. smart_string_appendc(&sbuf, xdigits[c & 0xf]);
  57. }
  58. }
  59. /* Smart string is not NUL terminated */
  60. syslog(priority, "%.*s", (int)sbuf.len, sbuf.c);
  61. smart_string_free(&sbuf);
  62. }
  63. void php_openlog(const char *ident, int option, int facility)
  64. {
  65. openlog(ident, option, facility);
  66. PG(have_called_openlog) = 1;
  67. }
  68. void php_closelog(void)
  69. {
  70. closelog();
  71. PG(have_called_openlog) = 0;
  72. }
  73. #ifdef PHP_WIN32
  74. PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
  75. {
  76. va_list args;
  77. /*
  78. * don't rely on openlog() being called by syslog() if it's
  79. * not already been done; call it ourselves and pass the
  80. * correct parameters!
  81. */
  82. if (!PG(have_called_openlog)) {
  83. php_openlog(PG(syslog_ident), 0, PG(syslog_facility));
  84. }
  85. va_start(args, format);
  86. vsyslog(priority, format, args);
  87. va_end(args);
  88. }
  89. /* }}} */
  90. #else
  91. PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
  92. {
  93. zend_string *fbuf = NULL;
  94. va_list args;
  95. /*
  96. * don't rely on openlog() being called by syslog() if it's
  97. * not already been done; call it ourselves and pass the
  98. * correct parameters!
  99. */
  100. if (!PG(have_called_openlog)) {
  101. php_openlog(PG(syslog_ident), 0, PG(syslog_facility));
  102. }
  103. va_start(args, format);
  104. fbuf = zend_vstrpprintf(0, format, args);
  105. va_end(args);
  106. php_syslog_str(priority, fbuf);
  107. zend_string_release(fbuf);
  108. }
  109. /* }}} */
  110. #endif