summaryrefslogtreecommitdiff
path: root/src/sp_utils.c
diff options
context:
space:
mode:
authorjvoisin2017-12-05 17:25:29 +0100
committerGitHub2017-12-05 17:25:29 +0100
commit529a13648fd9e0c6fa9cfdff877e62ed19185b6c (patch)
tree991034201397c2d5c7c65adab9ed94a51825477a /src/sp_utils.c
parent9537313b88047d40a4b6f297605d1e0db972b1c0 (diff)
Dump environnement variables (#83)
Apparently, PHP thinks that it's a great idea to type environnement variables, because why not.
Diffstat (limited to 'src/sp_utils.c')
-rw-r--r--src/sp_utils.c38
1 files changed, 21 insertions, 17 deletions
diff --git a/src/sp_utils.c b/src/sp_utils.c
index 74fbff7..62cb1a1 100644
--- a/src/sp_utils.c
+++ b/src/sp_utils.c
@@ -81,12 +81,12 @@ int compute_hash(const char* const filename, char* file_hash) {
81} 81}
82 82
83static int construct_filename(char* filename, const char* folder, 83static int construct_filename(char* filename, const char* folder,
84 const char* textual) { 84 const char* textual) {
85 time_t t = time(NULL); 85 PHP_SHA256_CTX context;
86 struct tm* tm = localtime(&t); // FIXME use `localtime_r` instead 86 unsigned char digest[SHA256_SIZE] = {0};
87 struct timeval tval; 87 char strhash[65] = {0};
88 88
89 if (0 > mkdir(folder, 0700) && errno != EEXIST) { 89 if (-1 == mkdir(folder, 0700) && errno != EEXIST) {
90 sp_log_err("request_logging", "Unable to create the folder '%s'.", 90 sp_log_err("request_logging", "Unable to create the folder '%s'.",
91 folder); 91 folder);
92 return -1; 92 return -1;
@@ -95,9 +95,6 @@ static int construct_filename(char* filename, const char* folder,
95 /* We're using the sha256 sum of the rule's textual representation 95 /* We're using the sha256 sum of the rule's textual representation
96 * as filename, in order to only have one dump per rule, to migitate 96 * as filename, in order to only have one dump per rule, to migitate
97 * DoS attacks. */ 97 * DoS attacks. */
98 PHP_SHA256_CTX context;
99 unsigned char digest[SHA256_SIZE] = {0};
100 char strhash[65] = {0};
101 PHP_SHA256Init(&context); 98 PHP_SHA256Init(&context);
102 PHP_SHA256Update(&context, (const unsigned char *) textual, strlen(textual)); 99 PHP_SHA256Update(&context, (const unsigned char *) textual, strlen(textual));
103 PHP_SHA256Final(digest, &context); 100 PHP_SHA256Final(digest, &context);
@@ -116,18 +113,15 @@ int sp_log_request(const char* folder, const char* text_repr) {
116 const char* str; 113 const char* str;
117 const int key; 114 const int key;
118 } zones[] = {{"GET", TRACK_VARS_GET}, {"POST", TRACK_VARS_POST}, 115 } zones[] = {{"GET", TRACK_VARS_GET}, {"POST", TRACK_VARS_POST},
119 {"COOKIE", TRACK_VARS_COOKIE}, /*{"SERVER", TRACK_VARS_SERVER}, */ 116 {"COOKIE", TRACK_VARS_COOKIE}, {"SERVER", TRACK_VARS_SERVER},
120 {"ENV", TRACK_VARS_ENV}, /*{"REQUEST", TRACK_VARS_REQUEST},*/ 117 {"ENV", TRACK_VARS_ENV}, {NULL, 0}};
121 {NULL, 0}};
122 // Apparently, PHP has trouble always giving SERVER,
123 // and REQUEST is never used in its source code.
124 118
125 if (0 != construct_filename(filename, folder, text_repr)) { 119 if (0 != construct_filename(filename, folder, text_repr)) {
126 return -1; 120 return -1;
127 } 121 }
128 if (NULL == (file = fopen(filename, "w+"))) { 122 if (NULL == (file = fopen(filename, "w+"))) {
129 sp_log_err("request_logging", "Unable to open %s: %s", filename, 123 sp_log_err("request_logging", "Unable to open %s: %s", filename,
130 strerror(errno)); 124 strerror(errno));
131 return -1; 125 return -1;
132 } 126 }
133 127
@@ -145,7 +139,17 @@ int sp_log_request(const char* folder, const char* text_repr) {
145 HashTable* ht = Z_ARRVAL(PG(http_globals)[zones[i].key]); 139 HashTable* ht = Z_ARRVAL(PG(http_globals)[zones[i].key]);
146 fprintf(file, "%s:", zones[i].str); 140 fprintf(file, "%s:", zones[i].str);
147 ZEND_HASH_FOREACH_STR_KEY_VAL(ht, variable_key, variable_value) { 141 ZEND_HASH_FOREACH_STR_KEY_VAL(ht, variable_key, variable_value) {
148 fprintf(file, "%s=%s&", ZSTR_VAL(variable_key), Z_STRVAL_P(variable_value)); 142 const char* key = ZSTR_VAL(variable_key);
143
144 if (Z_TYPE_INFO_P(variable_value) == IS_LONG) {
145 fprintf(file, "%s=%ld\n", key, Z_DVAL_P(variable_value));
146 } else if (Z_TYPE_INFO_P(variable_value) == IS_DOUBLE) {
147 fprintf(file, "%s=%lf\n", key, Z_DVAL_P(variable_value));
148 } else if (Z_TYPE_INFO_P(variable_value) == IS_ARRAY) {
149 fprintf(file, "%s=array", key);
150 } else {
151 fprintf(file, "%s=%s\n", key, Z_STRVAL_P(variable_value));
152 }
149 } 153 }
150 ZEND_HASH_FOREACH_END(); 154 ZEND_HASH_FOREACH_END();
151 fputs("\n", file); 155 fputs("\n", file);
@@ -245,7 +249,7 @@ void sp_log_disable(const char* restrict path, const char* restrict arg_name,
245 } else { 249 } else {
246 sp_log_msg("disabled_function", sim?SP_LOG_SIMULATION:SP_LOG_DROP, 250 sp_log_msg("disabled_function", sim?SP_LOG_SIMULATION:SP_LOG_DROP,
247 "The call to the function '%s' in %s:%d has been disabled.", 251 "The call to the function '%s' in %s:%d has been disabled.",
248 path, zend_get_executed_filename(TSRMLS_C), 252 path, zend_get_executed_filename(TSRMLS_C),
249 zend_get_executed_lineno(TSRMLS_C)); 253 zend_get_executed_lineno(TSRMLS_C));
250 } 254 }
251 } 255 }
@@ -362,7 +366,7 @@ int hook_function(const char* original_name, HashTable* hook_table,
362 VAR_AND_LEN(original_name), 366 VAR_AND_LEN(original_name),
363 func->handler) == NULL) { 367 func->handler) == NULL) {
364 sp_log_err("function_pointer_saving", 368 sp_log_err("function_pointer_saving",
365 "Could not save function pointer for %s", original_name); 369 "Could not save function pointer for %s", original_name);
366 return FAILURE; 370 return FAILURE;
367 } else { 371 } else {
368 func->handler = new_function; 372 func->handler = new_function;