diff options
| author | xXx-caillou-xXx | 2018-08-28 14:24:29 +0200 |
|---|---|---|
| committer | jvoisin | 2018-08-28 12:24:29 +0000 |
| commit | aa5d6895d0ac8c6ea050a8ea55e2f8a39ddfe365 (patch) | |
| tree | c640e50ab007bae3a2fd55ca05bc13c73e293c69 /src/sp_utils.c | |
| parent | eff2c658d37108020215f838d4c47c176ec3e050 (diff) | |
Use php's logging functions
This commit replace our usage of `php_log_err` with `zend_error`. This should
allow administrators to display errors in the webpage, should they want to;
and to properly manipulate the verbosity's level.
This should close #217
Diffstat (limited to 'src/sp_utils.c')
| -rw-r--r-- | src/sp_utils.c | 86 |
1 files changed, 34 insertions, 52 deletions
diff --git a/src/sp_utils.c b/src/sp_utils.c index cdcf205..cb6b4a7 100644 --- a/src/sp_utils.c +++ b/src/sp_utils.c | |||
| @@ -9,16 +9,6 @@ | |||
| 9 | 9 | ||
| 10 | ZEND_DECLARE_MODULE_GLOBALS(snuffleupagus) | 10 | ZEND_DECLARE_MODULE_GLOBALS(snuffleupagus) |
| 11 | 11 | ||
| 12 | static inline void _sp_log_err(const char* fmt, ...) { | ||
| 13 | char* msg; | ||
| 14 | va_list args; | ||
| 15 | |||
| 16 | va_start(args, fmt); | ||
| 17 | vspprintf(&msg, 0, fmt, args); | ||
| 18 | va_end(args); | ||
| 19 | php_log_err(msg); | ||
| 20 | } | ||
| 21 | |||
| 22 | static bool sp_zend_string_equals(const zend_string* s1, | 12 | static bool sp_zend_string_equals(const zend_string* s1, |
| 23 | const zend_string* s2) { | 13 | const zend_string* s2) { |
| 24 | // We can't use `zend_string_equals` here because it doesn't work on | 14 | // We can't use `zend_string_equals` here because it doesn't work on |
| @@ -27,7 +17,7 @@ static bool sp_zend_string_equals(const zend_string* s1, | |||
| 27 | !memcmp(ZSTR_VAL(s1), ZSTR_VAL(s2), ZSTR_LEN(s1)); | 17 | !memcmp(ZSTR_VAL(s1), ZSTR_VAL(s2), ZSTR_LEN(s1)); |
| 28 | } | 18 | } |
| 29 | 19 | ||
| 30 | void sp_log_msg(char const* feature, char const* level, const char* fmt, ...) { | 20 | void sp_log_msg(char const* feature, int type, const char* fmt, ...) { |
| 31 | char* msg; | 21 | char* msg; |
| 32 | va_list args; | 22 | va_list args; |
| 33 | 23 | ||
| @@ -35,9 +25,7 @@ void sp_log_msg(char const* feature, char const* level, const char* fmt, ...) { | |||
| 35 | vspprintf(&msg, 0, fmt, args); | 25 | vspprintf(&msg, 0, fmt, args); |
| 36 | va_end(args); | 26 | va_end(args); |
| 37 | 27 | ||
| 38 | char const* const client_ip = getenv("REMOTE_ADDR"); | 28 | zend_error(type, "[snuffleupagus][%s] %s", feature, msg); |
| 39 | _sp_log_err("[snuffleupagus][%s][%s][%s] %s", | ||
| 40 | client_ip ? client_ip : "0.0.0.0", feature, level, msg); | ||
| 41 | } | 29 | } |
| 42 | 30 | ||
| 43 | int compute_hash(const char* const filename, char* file_hash) { | 31 | int compute_hash(const char* const filename, char* file_hash) { |
| @@ -50,7 +38,7 @@ int compute_hash(const char* const filename, char* file_hash) { | |||
| 50 | php_stream_open_wrapper(filename, "rb", REPORT_ERRORS, NULL); | 38 | php_stream_open_wrapper(filename, "rb", REPORT_ERRORS, NULL); |
| 51 | if (!stream) { | 39 | if (!stream) { |
| 52 | sp_log_err("hash_computation", | 40 | sp_log_err("hash_computation", |
| 53 | "Can not open the file %s to compute its hash.\n", filename); | 41 | "Can not open the file %s to compute its hash", filename); |
| 54 | return FAILURE; | 42 | return FAILURE; |
| 55 | } | 43 | } |
| 56 | 44 | ||
| @@ -71,8 +59,8 @@ static int construct_filename(char* filename, const zend_string* folder, | |||
| 71 | char strhash[65] = {0}; | 59 | char strhash[65] = {0}; |
| 72 | 60 | ||
| 73 | if (-1 == mkdir(ZSTR_VAL(folder), 0700) && errno != EEXIST) { | 61 | if (-1 == mkdir(ZSTR_VAL(folder), 0700) && errno != EEXIST) { |
| 74 | sp_log_err("request_logging", "Unable to create the folder '%s'.", | 62 | sp_log_warn("request_logging", "Unable to create the folder '%s'", |
| 75 | ZSTR_VAL(folder)); | 63 | ZSTR_VAL(folder)); |
| 76 | return -1; | 64 | return -1; |
| 77 | } | 65 | } |
| 78 | 66 | ||
| @@ -106,8 +94,8 @@ int sp_log_request(const zend_string* folder, const zend_string* text_repr, | |||
| 106 | return -1; | 94 | return -1; |
| 107 | } | 95 | } |
| 108 | if (NULL == (file = fopen(filename, "w+"))) { | 96 | if (NULL == (file = fopen(filename, "w+"))) { |
| 109 | sp_log_err("request_logging", "Unable to open %s: %s", filename, | 97 | sp_log_warn("request_logging", "Unable to open %s: %s", filename, |
| 110 | strerror(errno)); | 98 | strerror(errno)); |
| 111 | return -1; | 99 | return -1; |
| 112 | } | 100 | } |
| 113 | 101 | ||
| @@ -146,7 +134,7 @@ static char* zend_string_to_char(const zend_string* zs) { | |||
| 146 | 134 | ||
| 147 | if (ZSTR_LEN(zs) + 1 < ZSTR_LEN(zs)) { | 135 | if (ZSTR_LEN(zs) + 1 < ZSTR_LEN(zs)) { |
| 148 | sp_log_err("overflow_error", | 136 | sp_log_err("overflow_error", |
| 149 | "Overflow tentative detected in zend_string_to_char."); | 137 | "Overflow tentative detected in zend_string_to_char"); |
| 150 | sp_terminate(); | 138 | sp_terminate(); |
| 151 | } | 139 | } |
| 152 | char* copy = emalloc(ZSTR_LEN(zs) + 1); | 140 | char* copy = emalloc(ZSTR_LEN(zs) + 1); |
| @@ -214,15 +202,15 @@ bool sp_match_value(const zend_string* value, const zend_string* to_match, | |||
| 214 | 202 | ||
| 215 | void sp_log_disable(const char* restrict path, const char* restrict arg_name, | 203 | void sp_log_disable(const char* restrict path, const char* restrict arg_name, |
| 216 | const zend_string* restrict arg_value, | 204 | const zend_string* restrict arg_value, |
| 217 | const sp_disabled_function* config_node, unsigned int line, | 205 | const sp_disabled_function* config_node) { |
| 218 | const char* restrict filename) { | ||
| 219 | const zend_string* dump = config_node->dump; | 206 | const zend_string* dump = config_node->dump; |
| 220 | const zend_string* alias = config_node->alias; | 207 | const zend_string* alias = config_node->alias; |
| 221 | const int sim = config_node->simulation; | 208 | const int sim = config_node->simulation; |
| 222 | 209 | ||
| 223 | filename = filename ? filename : zend_get_executed_filename(TSRMLS_C); | 210 | if (dump) { |
| 224 | line = line ? line : zend_get_executed_lineno(TSRMLS_C); | 211 | sp_log_request(config_node->dump, config_node->textual_representation, |
| 225 | 212 | SP_TOKEN_DISABLE_FUNC); | |
| 213 | } | ||
| 226 | if (arg_name) { | 214 | if (arg_name) { |
| 227 | char* char_repr = NULL; | 215 | char* char_repr = NULL; |
| 228 | if (arg_value) { | 216 | if (arg_value) { |
| @@ -231,33 +219,29 @@ void sp_log_disable(const char* restrict path, const char* restrict arg_name, | |||
| 231 | if (alias) { | 219 | if (alias) { |
| 232 | sp_log_msg( | 220 | sp_log_msg( |
| 233 | "disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, | 221 | "disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, |
| 234 | "Aborted execution on call of the function '%s' in %s:%d, " | 222 | "Aborted execution on call of the function '%s', " |
| 235 | "because its argument '%s' content (%s) matched the rule '%s'.", | 223 | "because its argument '%s' content (%s) matched the rule '%s'", |
| 236 | path, filename, line, arg_name, char_repr ? char_repr : "?", | 224 | path, arg_name, char_repr ? char_repr : "?", |
| 237 | ZSTR_VAL(alias)); | 225 | ZSTR_VAL(alias)); |
| 238 | } else { | 226 | } else { |
| 239 | sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, | 227 | sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, |
| 240 | "Aborted execution on call of the function '%s' in %s:%d, " | 228 | "Aborted execution on call of the function '%s', " |
| 241 | "because its argument '%s' content (%s) matched a rule.", | 229 | "because its argument '%s' content (%s) matched a rule", |
| 242 | path, filename, line, arg_name, char_repr ? char_repr : "?"); | 230 | path, arg_name, char_repr ? char_repr : "?"); |
| 243 | } | 231 | } |
| 244 | efree(char_repr); | 232 | efree(char_repr); |
| 245 | } else { | 233 | } else { |
| 246 | if (alias) { | 234 | if (alias) { |
| 247 | sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, | 235 | sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, |
| 248 | "Aborted execution on call of the function '%s' in %s:%d, " | 236 | "Aborted execution on call of the function '%s', " |
| 249 | "because of the the rule '%s'.", | 237 | "because of the the rule '%s'", |
| 250 | path, filename, line, ZSTR_VAL(alias)); | 238 | path, ZSTR_VAL(alias)); |
| 251 | } else { | 239 | } else { |
| 252 | sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, | 240 | sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, |
| 253 | "Aborted execution on call of the function '%s' in %s:%d.", | 241 | "Aborted execution on call of the function '%s'", |
| 254 | path, filename, line); | 242 | path); |
| 255 | } | 243 | } |
| 256 | } | 244 | } |
| 257 | if (dump) { | ||
| 258 | sp_log_request(config_node->dump, config_node->textual_representation, | ||
| 259 | SP_TOKEN_DISABLE_FUNC); | ||
| 260 | } | ||
| 261 | } | 245 | } |
| 262 | 246 | ||
| 263 | void sp_log_disable_ret(const char* restrict path, | 247 | void sp_log_disable_ret(const char* restrict path, |
| @@ -268,29 +252,27 @@ void sp_log_disable_ret(const char* restrict path, | |||
| 268 | const int sim = config_node->simulation; | 252 | const int sim = config_node->simulation; |
| 269 | char* char_repr = NULL; | 253 | char* char_repr = NULL; |
| 270 | 254 | ||
| 255 | if (dump) { | ||
| 256 | sp_log_request(dump, config_node->textual_representation, | ||
| 257 | SP_TOKEN_DISABLE_FUNC); | ||
| 258 | } | ||
| 271 | if (ret_value) { | 259 | if (ret_value) { |
| 272 | char_repr = zend_string_to_char(ret_value); | 260 | char_repr = zend_string_to_char(ret_value); |
| 273 | } | 261 | } |
| 274 | if (alias) { | 262 | if (alias) { |
| 275 | sp_log_msg( | 263 | sp_log_msg( |
| 276 | "disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, | 264 | "disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, |
| 277 | "Aborted execution on return of the function '%s' in %s:%d, " | 265 | "Aborted execution on return of the function '%s', " |
| 278 | "because the function returned '%s', which matched the rule '%s'.", | 266 | "because the function returned '%s', which matched the rule '%s'", |
| 279 | path, zend_get_executed_filename(TSRMLS_C), | 267 | path, char_repr ? char_repr : "?", |
| 280 | zend_get_executed_lineno(TSRMLS_C), char_repr ? char_repr : "?", | ||
| 281 | ZSTR_VAL(alias)); | 268 | ZSTR_VAL(alias)); |
| 282 | } else { | 269 | } else { |
| 283 | sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, | 270 | sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, |
| 284 | "Aborted execution on return of the function '%s' in %s:%d, " | 271 | "Aborted execution on return of the function '%s', " |
| 285 | "because the function returned '%s', which matched a rule.", | 272 | "because the function returned '%s', which matched a rule", |
| 286 | path, zend_get_executed_filename(TSRMLS_C), | 273 | path, char_repr ? char_repr : "?"); |
| 287 | zend_get_executed_lineno(TSRMLS_C), char_repr ? char_repr : "?"); | ||
| 288 | } | 274 | } |
| 289 | efree(char_repr); | 275 | efree(char_repr); |
| 290 | if (dump) { | ||
| 291 | sp_log_request(dump, config_node->textual_representation, | ||
| 292 | SP_TOKEN_DISABLE_FUNC); | ||
| 293 | } | ||
| 294 | } | 276 | } |
| 295 | 277 | ||
| 296 | bool sp_match_array_key(const zval* zv, const zend_string* to_match, | 278 | bool sp_match_array_key(const zval* zv, const zend_string* to_match, |
