Skip to content

Commit 2010c02

Browse files
pprindevillebukka
authored andcommitted
Add syslog.filter INI for filtering syslog messages
Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
1 parent 4a528d4 commit 2010c02

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

main/main.c

+24
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "php_ini.h"
5454
#include "php_globals.h"
5555
#include "php_main.h"
56+
#include "php_syslog.h"
5657
#include "fopen_wrappers.h"
5758
#include "ext/standard/php_standard.h"
5859
#include "ext/standard/php_string.h"
@@ -329,6 +330,28 @@ static PHP_INI_MH(OnChangeMemoryLimit)
329330
}
330331
/* }}} */
331332

333+
/* {{{ PHP_INI_MH
334+
*/
335+
static PHP_INI_MH(OnSetLogFilter)
336+
{
337+
const char *filter = ZSTR_VAL(new_value);
338+
339+
if (!strcmp(filter, "none")) {
340+
PG(syslog_filter) = PHP_SYSLOG_FILTER_NONE;
341+
return SUCCESS;
342+
}
343+
if (!strcmp(filter, "no-ctrl")) {
344+
PG(syslog_filter) = PHP_SYSLOG_FILTER_NO_CTRL;
345+
return SUCCESS;
346+
}
347+
if (!strcmp(filter, "ascii")) {
348+
PG(syslog_filter) = PHP_SYSLOG_FILTER_ASCII;
349+
return SUCCESS;
350+
}
351+
352+
return FAILURE;
353+
}
354+
/* }}} */
332355

333356
/* {{{ php_disable_functions
334357
*/
@@ -775,6 +798,7 @@ PHP_INI_BEGIN()
775798
#endif
776799
STD_PHP_INI_ENTRY("syslog.facility", "LOG_USER", PHP_INI_SYSTEM, OnSetFacility, syslog_facility, php_core_globals, core_globals)
777800
STD_PHP_INI_ENTRY("syslog.ident", "php", PHP_INI_SYSTEM, OnUpdateString, syslog_ident, php_core_globals, core_globals)
801+
STD_PHP_INI_ENTRY("syslog.filter", "no-ctrl", PHP_INI_ALL, OnSetLogFilter, syslog_filter, php_core_globals, core_globals)
778802
PHP_INI_END()
779803
/* }}} */
780804

main/php_globals.h

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ struct _php_core_globals {
170170
zend_long syslog_facility;
171171
char *syslog_ident;
172172
zend_bool have_called_openlog;
173+
zend_long syslog_filter;
173174
};
174175

175176

main/php_syslog.c

+14-2
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,23 @@ PHPAPI void php_syslog(int priority, const char *format, ...) /* {{{ */
8686
break;
8787
}
8888

89-
if (c != '\n')
89+
/* check for NVT ASCII only unless test disabled */
90+
if (((0x20 <= c) && (c <= 0x7e)))
9091
smart_string_appendc(&sbuf, c);
91-
else {
92+
else if ((c >= 0x80) && (PG(syslog_filter) != PHP_SYSLOG_FILTER_ASCII))
93+
smart_string_appendc(&sbuf, c);
94+
else if (c == '\n') {
9295
syslog(priority, "%.*s", (int)sbuf.len, sbuf.c);
9396
smart_string_reset(&sbuf);
97+
} else if ((c < 0x20) && (PG(syslog_filter) == PHP_SYSLOG_FILTER_NONE))
98+
smart_string_appendc(&sbuf, c);
99+
else {
100+
const char xdigits[] = "0123456789abcdef";
101+
102+
smart_string_appendl(&sbuf, "\\x", 2);
103+
smart_string_appendc(&sbuf, xdigits[(c / 0x10)]);
104+
c &= 0x0f;
105+
smart_string_appendc(&sbuf, xdigits[c]);
94106
}
95107
}
96108

main/php_syslog.h

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
#endif
3333
#endif
3434

35+
/* Syslog filters */
36+
#define PHP_SYSLOG_FILTER_NONE 0
37+
#define PHP_SYSLOG_FILTER_NO_CTRL 1
38+
#define PHP_SYSLOG_FILTER_ASCII 2
39+
3540
BEGIN_EXTERN_C()
3641
PHPAPI void php_syslog(int, const char *format, ...);
3742
PHPAPI void php_openlog(const char *, int, int);

php.ini-development

+8
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,14 @@ report_memleaks = On
517517
; This setting is on by default.
518518
;report_zend_debug = 0
519519

520+
; Set this to disable filtering control characters (the default).
521+
; Some loggers only accept NVT-ASCII, others accept anything that's not
522+
; control characters. If your logger accepts everything, then no filtering
523+
; is needed at all.
524+
; Values are: ascii (space-tilde), no_ctrl (all characters space and above),
525+
; and none (all characters)
526+
;syslog.filter = ascii
527+
520528
; Store the last error/warning message in $php_errormsg (boolean).
521529
; This directive is DEPRECATED.
522530
; Default Value: Off

php.ini-production

+8
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,14 @@ report_memleaks = On
522522
; This setting is on by default.
523523
;report_zend_debug = 0
524524

525+
; Set this to disable filtering control characters (the default).
526+
; Some loggers only accept NVT-ASCII, others accept anything that's not
527+
; control characters. If your logger accepts everything, then no filtering
528+
; is needed at all.
529+
; Values are: ascii (space-tilde), no_ctrl (all characters space and above),
530+
; and none (all characters)
531+
;syslog.filter = ascii
532+
525533
; Store the last error/warning message in $php_errormsg (boolean). Setting this value
526534
; to On can assist in debugging and is appropriate for development servers. It should
527535
; however be disabled on production servers.

0 commit comments

Comments
 (0)