static int web_set_login(AGENT_RESULT *result, struct web *opt, const char *params, int param_id) { char login_tmp[MAX_STRING_LEN] = {0}; char *passwd_ptr = NULL; size_t user_lenght; if (get_param(params, param_id, login_tmp, MAX_STRING_LEN)) goto failed; zbx_remove_whitespace(login_tmp); if (!(passwd_ptr = strchr(login_tmp, ':'))) goto failed; if (!(user_lenght = strlen(login_tmp) - strlen(passwd_ptr))) goto failed; opt->username = strndup(login_tmp, user_lenght); opt->passwd = zbx_strdup(opt->passwd, ++passwd_ptr); return SUCCEED; failed: SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Invalid LOGIN parameter", NULL)); return FAIL; }
static int web_set_timeout(AGENT_RESULT *result, struct web *opt, const char *params, int param_id) { char timeout_tmp[WEB_MAX_TIMEOUT_STRLEN] = {0}; zbx_uint64_t timeout_test; /* maximum is one digit timeout */ if (get_param(params, param_id, timeout_tmp, WEB_MAX_TIMEOUT_STRLEN)) goto failed; zbx_remove_whitespace(timeout_tmp); if (strlen(timeout_tmp)) { /* check timeout range 1 to WEB_MAX_TIMEOUT */ /* and convert string to uint64 */ if (is_uint_range(timeout_tmp, &timeout_test, 1, WEB_MAX_TIMEOUT)) goto failed; opt->timeout = timeout_test; return SUCCEED; } else { opt->timeout = WEB_DEF_TIMEOUT; return SUCCEED; } failed: SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Invalid TIMEOUT parameter", NULL)); return FAIL; }
static int web_set_host(AGENT_RESULT *result, struct web *opt, const char *params, int param_id) { char host_tmp[WEB_MAX_DNS_STRLEN] = {0}; if (get_param(params, param_id, host_tmp, WEB_MAX_DNS_STRLEN)) goto failed; zbx_remove_whitespace(host_tmp); if (strlen(host_tmp)) { if (!strncmp(host_tmp, "none", strlen(host_tmp))) { if (opt->host) zbx_free(opt->host); } else { /* RFC2616 14.23 non compliant. We dont accept port number in Host field */ if (zbx_check_hostname(host_tmp)) goto failed; /* if hostname is previously set in IP / DNS then replace it with user param */ if (opt->host) zbx_free(opt->host); opt->host = zbx_strdup(NULL, host_tmp); } } return SUCCEED; failed: SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Invalid HOST parameter", NULL)); return FAIL; }
static int web_set_port(AGENT_RESULT *result, struct web *opt, const char *params, int param_id) { char port_tmp[WEB_MAX_PORT_STRLEN] = {0}; if (get_param(params, param_id, port_tmp, WEB_MAX_PORT_STRLEN)) goto failed; zbx_remove_whitespace(port_tmp); if (strlen(port_tmp)) { /* RFC6335 */ if (is_uint_range(port_tmp, NULL, 0, WEB_MAX_PORT)) goto failed; if (strncmp(port_tmp, "443", strlen(port_tmp))) opt->is_https = (zbx_uint64_t) 0; else opt->is_https = (zbx_uint64_t) 1; opt->port = zbx_strdup(NULL, port_tmp); } else { opt->port = zbx_strdup(NULL, WEB_DEF_PORT); } return SUCCEED; failed: SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Invalid PORT parameter", NULL)); return FAIL; }
static int web_set_uri(AGENT_RESULT *result, struct web *opt, const char *params, int param_id) { char req_uri_tmp[MAX_STRING_LEN] = {0}; if (get_param(params, param_id, req_uri_tmp, MAX_STRING_LEN)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Invalid URI parameter", NULL)); return FAIL; } zbx_remove_whitespace(req_uri_tmp); if (strlen(req_uri_tmp)) opt->uri = zbx_strdup(NULL, req_uri_tmp); else opt->uri = zbx_strdup(NULL, WEB_DEF_URI); return SUCCEED; }
static int web_set_ip(AGENT_RESULT *result, struct web *opt, const char *params, int param_id) { char ip_tmp[WEB_MAX_DNS_STRLEN] = {0}; /* RFC1035 */ if (get_param(params, param_id, ip_tmp, WEB_MAX_DNS_STRLEN)) goto failed; zbx_remove_whitespace(ip_tmp); if (strlen(ip_tmp)) { if (!is_ip4(ip_tmp)) { opt->is_ipv6 = (zbx_uint64_t) 0; opt->ip = zbx_strdup(NULL, ip_tmp); return SUCCEED; } #if defined(HAVE_IPV6) else if (!is_ip6(ip_tmp)) { opt->is_ipv6 = (zbx_uint64_t) 1; opt->ip = zbx_strdup(NULL, ip_tmp); return SUCCEED; } #endif /*HAVE_IPV6*/ else if (!(zbx_check_hostname(ip_tmp))) { opt->is_ip_hostname = (zbx_uint64_t) 1; opt->host = zbx_strdup(NULL, ip_tmp); return SUCCEED; } else { goto failed; } } else { if (opt->item->interface.useip) { opt->ip = zbx_strdup(NULL, opt->item->interface.ip_orig); return SUCCEED; } else { opt->host = zbx_strdup(NULL, opt->item->interface.dns_orig); return web_resolv_dns(opt, opt->item->interface.dns_orig); } } failed: SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Invalid IP / DNS parameter", NULL)); return FAIL; }
static int web_set_time_expression(AGENT_RESULT *result, struct web *opt, const char *params, int param_id) { char time_tmp[WEB_MAX_TIME_STRLEN] = {0}; int i; if (get_param(params, param_id, time_tmp, WEB_MAX_TIME_STRLEN)) goto failed; zbx_remove_whitespace(time_tmp); for (i = 0; WEB_CURL_TIME[i].time_name != NULL; i++) { if (!strncmp(WEB_CURL_TIME[i].time_name, time_tmp, strlen(time_tmp))) { opt->curl_time_flag = WEB_CURL_TIME[i].curl_time_flag; return SUCCEED; } } failed: SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Invalid TIME parameter", NULL)); return FAIL; }
int set_result_type(AGENT_RESULT *result, int value_type, int data_type, char *c) { int ret = FAIL; zbx_uint64_t value_uint64; double value_double; assert(result); switch (value_type) { case ITEM_VALUE_TYPE_UINT64: zbx_rtrim(c, " \""); zbx_ltrim(c, " \"+"); del_zeroes(c); switch (data_type) { case ITEM_DATA_TYPE_BOOLEAN: if (SUCCEED == is_boolean(c, &value_uint64)) { SET_UI64_RESULT(result, value_uint64); ret = SUCCEED; } break; case ITEM_DATA_TYPE_OCTAL: if (SUCCEED == is_uoct(c)) { ZBX_OCT2UINT64(value_uint64, c); SET_UI64_RESULT(result, value_uint64); ret = SUCCEED; } break; case ITEM_DATA_TYPE_DECIMAL: if (SUCCEED == is_uint64(c, &value_uint64)) { SET_UI64_RESULT(result, value_uint64); ret = SUCCEED; } break; case ITEM_DATA_TYPE_HEXADECIMAL: if (SUCCEED == is_uhex(c)) { ZBX_HEX2UINT64(value_uint64, c); SET_UI64_RESULT(result, value_uint64); ret = SUCCEED; } else if (SUCCEED == is_hex_string(c)) { zbx_remove_whitespace(c); ZBX_HEX2UINT64(value_uint64, c); SET_UI64_RESULT(result, value_uint64); ret = SUCCEED; } break; default: THIS_SHOULD_NEVER_HAPPEN; break; } break; case ITEM_VALUE_TYPE_FLOAT: zbx_rtrim(c, " \""); zbx_ltrim(c, " \"+"); if (SUCCEED != is_double(c)) break; value_double = atof(c); SET_DBL_RESULT(result, value_double); ret = SUCCEED; break; case ITEM_VALUE_TYPE_STR: zbx_replace_invalid_utf8(c); SET_STR_RESULT(result, zbx_strdup(NULL, c)); ret = SUCCEED; break; case ITEM_VALUE_TYPE_TEXT: zbx_replace_invalid_utf8(c); SET_TEXT_RESULT(result, zbx_strdup(NULL, c)); ret = SUCCEED; break; case ITEM_VALUE_TYPE_LOG: zbx_replace_invalid_utf8(c); add_log_result(result, c); ret = SUCCEED; break; } if (SUCCEED != ret) { char *error = NULL; zbx_remove_chars(c, "\r\n"); zbx_replace_invalid_utf8(c); if (ITEM_VALUE_TYPE_UINT64 == value_type) error = zbx_dsprintf(error, "Received value [%s] is not suitable for value type [%s] and data type [%s]", c, zbx_item_value_type_string(value_type), zbx_item_data_type_string(data_type)); else error = zbx_dsprintf(error, "Received value [%s] is not suitable for value type [%s]", c, zbx_item_value_type_string(value_type)); SET_MSG_RESULT(result, error); } return ret; }