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