diff options
Diffstat (limited to 'src/libs/zbxsysinfo/common/file.c')
-rw-r--r-- | src/libs/zbxsysinfo/common/file.c | 172 |
1 files changed, 167 insertions, 5 deletions
diff --git a/src/libs/zbxsysinfo/common/file.c b/src/libs/zbxsysinfo/common/file.c index a6ac7aa4c23..83a3c13098a 100644 --- a/src/libs/zbxsysinfo/common/file.c +++ b/src/libs/zbxsysinfo/common/file.c @@ -22,6 +22,7 @@ #include "md5.h" #include "file.h" #include "zbxregexp.h" +#include "log.h" #define ZBX_MAX_DB_FILE_SIZE 64 * ZBX_KIBIBYTE /* files larger than 64 KB cannot be stored in the database */ @@ -34,12 +35,24 @@ int VFS_FILE_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result) int ret = SYSINFO_RET_FAIL; if (1 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); goto err; + } filename = get_rparam(request, 0); - if (NULL == filename || '\0' == *filename || 0 != zbx_stat(filename, &buf)) + if (NULL == filename || '\0' == *filename) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); + goto err; + } + + if (0 != zbx_stat(filename, &buf)) + { + SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain file information: %s", zbx_strerror(errno))); goto err; + } SET_UI64_RESULT(result, buf.st_size); @@ -55,13 +68,25 @@ int VFS_FILE_TIME(AGENT_REQUEST *request, AGENT_RESULT *result) int ret = SYSINFO_RET_FAIL; if (2 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); goto err; + } filename = get_rparam(request, 0); type = get_rparam(request, 1); - if (NULL == filename || '\0' == *filename || 0 != zbx_stat(filename, &buf)) + if (NULL == filename || '\0' == *filename) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); + goto err; + } + + if (0 != zbx_stat(filename, &buf)) + { + SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain file information: %s", zbx_strerror(errno))); goto err; + } if (NULL == type || '\0' == *type || 0 == strcmp(type, "modify")) /* default parameter */ SET_UI64_RESULT(result, buf.st_mtime); @@ -70,7 +95,10 @@ int VFS_FILE_TIME(AGENT_REQUEST *request, AGENT_RESULT *result) else if (0 == strcmp(type, "change")) SET_UI64_RESULT(result, buf.st_ctime); else + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); goto err; + } ret = SYSINFO_RET_OK; err: @@ -84,12 +112,18 @@ int VFS_FILE_EXISTS(AGENT_REQUEST *request, AGENT_RESULT *result) int ret = SYSINFO_RET_FAIL, file_exists = -1; if (1 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); goto err; + } filename = get_rparam(request, 0); if (NULL == filename || '\0' == *filename) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); goto err; + } if (0 == zbx_stat(filename, &buf)) file_exists = S_ISREG(buf.st_mode) ? 1 : 0; @@ -117,7 +151,10 @@ int VFS_FILE_CONTENTS(AGENT_REQUEST *request, AGENT_RESULT *result) ts = zbx_time(); if (2 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); goto err; + } filename = get_rparam(request, 0); tmp = get_rparam(request, 1); @@ -127,27 +164,54 @@ int VFS_FILE_CONTENTS(AGENT_REQUEST *request, AGENT_RESULT *result) else strscpy(encoding, tmp); - if (NULL == filename || '\0' == *filename || 0 != zbx_stat(filename, &stat_buf)) + if (NULL == filename || '\0' == *filename) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); goto err; + } + + if (0 != zbx_stat(filename, &stat_buf)) + { + SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain file information: %s", zbx_strerror(errno))); + goto err; + } if (CONFIG_TIMEOUT < zbx_time() - ts) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Timeout while processing item.")); goto err; + } if (ZBX_MAX_DB_FILE_SIZE < stat_buf.st_size) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "File is too large for this check.")); goto err; + } if (-1 == (f = zbx_open(filename, O_RDONLY))) goto err; if (CONFIG_TIMEOUT < zbx_time() - ts) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Timeout while processing item.")); goto err; + } flen = 0; while (0 < (nbytes = zbx_read(f, read_buf, sizeof(read_buf), encoding))) { - if (CONFIG_TIMEOUT < zbx_time() - ts || ZBX_MAX_DB_FILE_SIZE < (flen += nbytes)) + if (CONFIG_TIMEOUT < zbx_time() - ts) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Timeout while processing item.")); goto err; + } + + if (ZBX_MAX_DB_FILE_SIZE < (flen += nbytes)) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "File is too large for this check.")); + goto err; + } utf8 = convert_to_utf8(read_buf, nbytes, encoding); zbx_strcpy_alloc(&contents, &contents_alloc, &contents_offset, utf8); @@ -156,6 +220,7 @@ int VFS_FILE_CONTENTS(AGENT_REQUEST *request, AGENT_RESULT *result) if (-1 == nbytes) /* error occurred */ { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot read from file.")); zbx_free(contents); goto err; } @@ -190,7 +255,10 @@ int VFS_FILE_REGEXP(AGENT_REQUEST *request, AGENT_RESULT *result) ts = zbx_time(); if (6 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); goto err; + } filename = get_rparam(request, 0); regexp = get_rparam(request, 1); @@ -200,10 +268,16 @@ int VFS_FILE_REGEXP(AGENT_REQUEST *request, AGENT_RESULT *result) output = get_rparam(request, 5); if (NULL == filename || '\0' == *filename) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); goto err; + } if (NULL == regexp || '\0' == *regexp) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); goto err; + } if (NULL == tmp) *encoding = '\0'; @@ -213,37 +287,55 @@ int VFS_FILE_REGEXP(AGENT_REQUEST *request, AGENT_RESULT *result) if (NULL == start_line_str || '\0' == *start_line_str) start_line = 0; else if (FAIL == is_uint32(start_line_str, &start_line)) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter.")); goto err; + } if (NULL == end_line_str || '\0' == *end_line_str) end_line = 0xffffffff; else if (FAIL == is_uint32(end_line_str, &end_line)) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fifth parameter.")); goto err; + } if (start_line > end_line) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Start line parameter must not exceed end line.")); goto err; + } if (-1 == (f = zbx_open(filename, O_RDONLY))) + { + SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open file: %s", zbx_strerror(errno))); goto err; + } if (CONFIG_TIMEOUT < zbx_time() - ts) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Timeout while processing item.")); goto err; + } while (0 < (nbytes = zbx_read(f, buf, sizeof(buf), encoding))) { if (CONFIG_TIMEOUT < zbx_time() - ts) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Timeout while processing item.")); goto err; + } if (++current_line < start_line) continue; utf8 = convert_to_utf8(buf, nbytes, encoding); + zbx_rtrim(utf8, "\r\n"); ptr = zbx_regexp_sub(utf8, regexp, output); zbx_free(utf8); if (NULL != ptr) { - zbx_rtrim(ptr, "\r\n "); SET_STR_RESULT(result, ptr); break; } @@ -257,7 +349,10 @@ int VFS_FILE_REGEXP(AGENT_REQUEST *request, AGENT_RESULT *result) } if (-1 == nbytes) /* error occurred */ + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot read from file.")); goto err; + } if (0 == nbytes) /* EOF */ SET_STR_RESULT(result, zbx_strdup(NULL, "")); @@ -281,7 +376,10 @@ int VFS_FILE_REGMATCH(AGENT_REQUEST *request, AGENT_RESULT *result) ts = zbx_time(); if (5 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); goto err; + } filename = get_rparam(request, 0); regexp = get_rparam(request, 1); @@ -290,10 +388,16 @@ int VFS_FILE_REGMATCH(AGENT_REQUEST *request, AGENT_RESULT *result) end_line_str = get_rparam(request, 4); if (NULL == filename || '\0' == *filename) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); goto err; + } if (NULL == regexp || '\0' == *regexp) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); goto err; + } if (NULL == tmp) *encoding = '\0'; @@ -303,33 +407,52 @@ int VFS_FILE_REGMATCH(AGENT_REQUEST *request, AGENT_RESULT *result) if (NULL == start_line_str || '\0' == *start_line_str) start_line = 0; else if (FAIL == is_uint32(start_line_str, &start_line)) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fourth parameter.")); goto err; + } if (NULL == end_line_str || '\0' == *end_line_str) end_line = 0xffffffff; else if (FAIL == is_uint32(end_line_str, &end_line)) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fifth parameter.")); goto err; + } if (start_line > end_line) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Start line must not exceed end line.")); goto err; + } if (-1 == (f = zbx_open(filename, O_RDONLY))) + { + SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open file: %s", zbx_strerror(errno))); goto err; + } if (CONFIG_TIMEOUT < zbx_time() - ts) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Timeout while processing item.")); goto err; + } res = 0; while (0 == res && 0 < (nbytes = zbx_read(f, buf, sizeof(buf), encoding))) { if (CONFIG_TIMEOUT < zbx_time() - ts) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Timeout while processing item.")); goto err; + } if (++current_line < start_line) continue; utf8 = convert_to_utf8(buf, nbytes, encoding); + zbx_rtrim(utf8, "\r\n"); if (NULL != zbx_regexp_match(utf8, regexp, &len)) res = 1; zbx_free(utf8); @@ -339,7 +462,10 @@ int VFS_FILE_REGMATCH(AGENT_REQUEST *request, AGENT_RESULT *result) } if (-1 == nbytes) /* error occurred */ + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot read from file.")); goto err; + } SET_UI64_RESULT(result, res); @@ -365,25 +491,40 @@ int VFS_FILE_MD5SUM(AGENT_REQUEST *request, AGENT_RESULT *result) ts = zbx_time(); if (1 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); goto err; + } filename = get_rparam(request, 0); if (NULL == filename || '\0' == *filename) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); goto err; + } if (-1 == (f = zbx_open(filename, O_RDONLY))) + { + SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open file: %s", zbx_strerror(errno))); goto err; + } if (CONFIG_TIMEOUT < zbx_time() - ts) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Timeout while processing item.")); goto err; + } md5_init(&state); while (0 < (nbytes = (int)read(f, buf, sizeof(buf)))) { if (CONFIG_TIMEOUT < zbx_time() - ts) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Timeout while processing item.")); goto err; + } md5_append(&state, (const md5_byte_t *)buf, nbytes); } @@ -391,7 +532,10 @@ int VFS_FILE_MD5SUM(AGENT_REQUEST *request, AGENT_RESULT *result) md5_finish(&state, hash); if (0 > nbytes) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot read from file.")); goto err; + } /* convert MD5 hash to text form */ @@ -486,25 +630,40 @@ int VFS_FILE_CKSUM(AGENT_REQUEST *request, AGENT_RESULT *result) ts = zbx_time(); if (1 < request->nparam) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); goto err; + } filename = get_rparam(request, 0); if (NULL == filename || '\0' == *filename) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); goto err; + } if (-1 == (f = zbx_open(filename, O_RDONLY))) + { + SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open file: %s", zbx_strerror(errno))); goto err; + } if (CONFIG_TIMEOUT < zbx_time() - ts) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Timeout while processing item.")); goto err; + } crc = flen = 0; while (0 < (nr = (int)read(f, buf, sizeof(buf)))) { if (CONFIG_TIMEOUT < zbx_time() - ts) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Timeout while processing item.")); goto err; + } flen += nr; @@ -513,7 +672,10 @@ int VFS_FILE_CKSUM(AGENT_REQUEST *request, AGENT_RESULT *result) } if (0 > nr) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot read from file.")); goto err; + } /* include the length of the file */ for (; 0 != flen; flen >>= 8) |