diff options
| author | jvoisin | 2020-07-04 17:56:03 +0200 |
|---|---|---|
| committer | jvoisin | 2020-07-04 17:56:03 +0200 |
| commit | 08f21d8b878b6c1490e6d6bb3d44aa640a88e9ca (patch) | |
| tree | 0b4b55a5c8d24d05408bf02bd244e78c130ba87d | |
| parent | 6c7ad0c1dfcf324828be4153b86a808b35995820 (diff) | |
Factorize how snuffleupagus gets client's ip addr
| -rw-r--r-- | src/sp_disabled_functions.c | 2 | ||||
| -rw-r--r-- | src/sp_utils.c | 20 | ||||
| -rw-r--r-- | src/sp_utils.h | 1 |
3 files changed, 18 insertions, 5 deletions
diff --git a/src/sp_disabled_functions.c b/src/sp_disabled_functions.c index 4807955..a6fc194 100644 --- a/src/sp_disabled_functions.c +++ b/src/sp_disabled_functions.c | |||
| @@ -327,7 +327,7 @@ static void should_disable(zend_execute_data* execute_data, | |||
| 327 | } | 327 | } |
| 328 | 328 | ||
| 329 | if (config_node->cidr) { | 329 | if (config_node->cidr) { |
| 330 | char* client_ip = getenv("REMOTE_ADDR"); | 330 | const char* client_ip = get_ipaddr(); |
| 331 | if (client_ip && false == cidr_match(client_ip, config_node->cidr)) { | 331 | if (client_ip && false == cidr_match(client_ip, config_node->cidr)) { |
| 332 | goto next; | 332 | goto next; |
| 333 | } | 333 | } |
diff --git a/src/sp_utils.c b/src/sp_utils.c index 40cf44e..1bd37fe 100644 --- a/src/sp_utils.c +++ b/src/sp_utils.c | |||
| @@ -7,6 +7,21 @@ bool sp_zend_string_equals(const zend_string* s1, const zend_string* s2) { | |||
| 7 | !memcmp(ZSTR_VAL(s1), ZSTR_VAL(s2), ZSTR_LEN(s1)); | 7 | !memcmp(ZSTR_VAL(s1), ZSTR_VAL(s2), ZSTR_LEN(s1)); |
| 8 | } | 8 | } |
| 9 | 9 | ||
| 10 | static const char* default_ipaddr = "0.0.0.0"; | ||
| 11 | const char* get_ipaddr() { | ||
| 12 | const char* client_ip = getenv("REMOTE_ADDR"); | ||
| 13 | if (client_ip) { | ||
| 14 | return client_ip; | ||
| 15 | } | ||
| 16 | |||
| 17 | const char* fwd_ip = getenv("HTTP_X_FORWARDED_FOR"); | ||
| 18 | if (fwd_ip) { | ||
| 19 | return fwd_ip; | ||
| 20 | } | ||
| 21 | |||
| 22 | return default_ipaddr; | ||
| 23 | } | ||
| 24 | |||
| 10 | void sp_log_msg(char const* feature, int type, const char* fmt, ...) { | 25 | void sp_log_msg(char const* feature, int type, const char* fmt, ...) { |
| 11 | char* msg; | 26 | char* msg; |
| 12 | va_list args; | 27 | va_list args; |
| @@ -15,10 +30,7 @@ void sp_log_msg(char const* feature, int type, const char* fmt, ...) { | |||
| 15 | vspprintf(&msg, 0, fmt, args); | 30 | vspprintf(&msg, 0, fmt, args); |
| 16 | va_end(args); | 31 | va_end(args); |
| 17 | 32 | ||
| 18 | const char *client_ip = getenv("REMOTE_ADDR"); | 33 | const char* client_ip = get_ipaddr(); |
| 19 | if (!client_ip) { | ||
| 20 | client_ip = "0.0.0.0"; | ||
| 21 | } | ||
| 22 | switch (SNUFFLEUPAGUS_G(config).log_media) { | 34 | switch (SNUFFLEUPAGUS_G(config).log_media) { |
| 23 | case SP_SYSLOG: | 35 | case SP_SYSLOG: |
| 24 | openlog(PHP_SNUFFLEUPAGUS_EXTNAME, LOG_PID, LOG_AUTH); | 36 | openlog(PHP_SNUFFLEUPAGUS_EXTNAME, LOG_PID, LOG_AUTH); |
diff --git a/src/sp_utils.h b/src/sp_utils.h index 200e82c..6fc59e5 100644 --- a/src/sp_utils.h +++ b/src/sp_utils.h | |||
| @@ -44,6 +44,7 @@ | |||
| 44 | 44 | ||
| 45 | #define GET_SUFFIX(x) (x == 1) ? "st" : ((x == 2) ? "nd" : "th") | 45 | #define GET_SUFFIX(x) (x == 1) ? "st" : ((x == 2) ? "nd" : "th") |
| 46 | 46 | ||
| 47 | const char* get_ipaddr(); | ||
| 47 | void sp_log_msg(char const *feature, int type, const char *fmt, ...); | 48 | void sp_log_msg(char const *feature, int type, const char *fmt, ...); |
| 48 | int compute_hash(const char *const filename, char *file_hash); | 49 | int compute_hash(const char *const filename, char *file_hash); |
| 49 | const zend_string *sp_zval_to_zend_string(const zval *); | 50 | const zend_string *sp_zval_to_zend_string(const zval *); |
