xref: /PHP-8.0/main/php_syslog.c (revision e330f443)
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    | http://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 
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include "php.h"
21 #include "php_syslog.h"
22 
23 #include "zend.h"
24 #include "zend_smart_string.h"
25 
26 /*
27  * The SCO OpenServer 5 Development System (not the UDK)
28  * defines syslog to std_syslog.
29  */
30 
31 #ifdef HAVE_STD_SYSLOG
32 #define syslog std_syslog
33 #endif
34 
php_openlog(const char * ident,int option,int facility)35 void php_openlog(const char *ident, int option, int facility)
36 {
37 	openlog(ident, option, facility);
38 	PG(have_called_openlog) = 1;
39 }
40 
php_closelog()41 void php_closelog()
42 {
43 	closelog();
44 	PG(have_called_openlog) = 0;
45 }
46 
47 #ifdef PHP_WIN32
php_syslog(int priority,const char * format,...)48 PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
49 {
50 	va_list args;
51 
52 	/*
53 	 * don't rely on openlog() being called by syslog() if it's
54 	 * not already been done; call it ourselves and pass the
55 	 * correct parameters!
56 	 */
57 	if (!PG(have_called_openlog)) {
58 		php_openlog(PG(syslog_ident), 0, PG(syslog_facility));
59 	}
60 
61 	va_start(args, format);
62 	vsyslog(priority, format, args);
63 	va_end(args);
64 }
65 /* }}} */
66 #else
php_syslog(int priority,const char * format,...)67 PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
68 {
69 	const char *ptr;
70 	unsigned char c;
71 	smart_string fbuf = {0};
72 	smart_string sbuf = {0};
73 	va_list args;
74 
75 	/*
76 	 * don't rely on openlog() being called by syslog() if it's
77 	 * not already been done; call it ourselves and pass the
78 	 * correct parameters!
79 	 */
80 	if (!PG(have_called_openlog)) {
81 		php_openlog(PG(syslog_ident), 0, PG(syslog_facility));
82 	}
83 
84 	va_start(args, format);
85 	zend_printf_to_smart_string(&fbuf, format, args);
86 	smart_string_0(&fbuf);
87 	va_end(args);
88 
89 	if (PG(syslog_filter) == PHP_SYSLOG_FILTER_RAW) {
90 		/* Just send it directly to the syslog */
91 		syslog(priority, "%.*s", (int)fbuf.len, fbuf.c);
92 		smart_string_free(&fbuf);
93 		return;
94 	}
95 
96 	for (ptr = fbuf.c; ; ++ptr) {
97 		c = *ptr;
98 		if (c == '\0') {
99 			syslog(priority, "%.*s", (int)sbuf.len, sbuf.c);
100 			break;
101 		}
102 
103 		/* check for NVT ASCII only unless test disabled */
104 		if (((0x20 <= c) && (c <= 0x7e)))
105 			smart_string_appendc(&sbuf, c);
106 		else if ((c >= 0x80) && (PG(syslog_filter) != PHP_SYSLOG_FILTER_ASCII))
107 			smart_string_appendc(&sbuf, c);
108 		else if (c == '\n') {
109 			syslog(priority, "%.*s", (int)sbuf.len, sbuf.c);
110 			smart_string_reset(&sbuf);
111 		} else if ((c < 0x20) && (PG(syslog_filter) == PHP_SYSLOG_FILTER_ALL))
112 			smart_string_appendc(&sbuf, c);
113 		else {
114 			const char xdigits[] = "0123456789abcdef";
115 
116 			smart_string_appendl(&sbuf, "\\x", 2);
117 			smart_string_appendc(&sbuf, xdigits[(c / 0x10)]);
118 			c &= 0x0f;
119 			smart_string_appendc(&sbuf, xdigits[c]);
120 		}
121 	}
122 
123 	smart_string_free(&fbuf);
124 	smart_string_free(&sbuf);
125 }
126 /* }}} */
127 #endif
128