1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #include "common.h"
21 #include "comms.h"
22 #include "log.h"
23 #include "../../libs/zbxcrypto/tls_tcp_active.h"
24 
25 #include "checks_agent.h"
26 
27 #if !(defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL))
28 extern unsigned char	program_type;
29 #endif
30 
31 /******************************************************************************
32  *                                                                            *
33  * Function: get_value_agent                                                  *
34  *                                                                            *
35  * Purpose: retrieve data from Zabbix agent                                   *
36  *                                                                            *
37  * Parameters: item - item we are interested in                               *
38  *                                                                            *
39  * Return value: SUCCEED - data successfully retrieved and stored in result   *
40  *                         and result_str (as string)                         *
41  *               NETWORK_ERROR - network related error occurred               *
42  *               NOTSUPPORTED - item not supported by the agent               *
43  *               AGENT_ERROR - uncritical error on agent side occurred        *
44  *               FAIL - otherwise                                             *
45  *                                                                            *
46  * Author: Alexei Vladishev                                                   *
47  *                                                                            *
48  * Comments: error will contain error message                                 *
49  *                                                                            *
50  ******************************************************************************/
get_value_agent(DC_ITEM * item,AGENT_RESULT * result)51 int	get_value_agent(DC_ITEM *item, AGENT_RESULT *result)
52 {
53 	const char	*__function_name = "get_value_agent";
54 	zbx_socket_t	s;
55 	char		*tls_arg1, *tls_arg2;
56 	int		ret = SUCCEED;
57 	ssize_t		received_len;
58 
59 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() host:'%s' addr:'%s' key:'%s' conn:'%s'", __function_name, item->host.host,
60 			item->interface.addr, item->key, zbx_tcp_connection_type_name(item->host.tls_connect));
61 
62 	switch (item->host.tls_connect)
63 	{
64 		case ZBX_TCP_SEC_UNENCRYPTED:
65 			tls_arg1 = NULL;
66 			tls_arg2 = NULL;
67 			break;
68 #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
69 		case ZBX_TCP_SEC_TLS_CERT:
70 			tls_arg1 = item->host.tls_issuer;
71 			tls_arg2 = item->host.tls_subject;
72 			break;
73 		case ZBX_TCP_SEC_TLS_PSK:
74 			tls_arg1 = item->host.tls_psk_identity;
75 			tls_arg2 = item->host.tls_psk;
76 			break;
77 #else
78 		case ZBX_TCP_SEC_TLS_CERT:
79 		case ZBX_TCP_SEC_TLS_PSK:
80 			SET_MSG_RESULT(result, zbx_dsprintf(NULL, "A TLS connection is configured to be used with agent"
81 					" but support for TLS was not compiled into %s.",
82 					get_program_type_string(program_type)));
83 			ret = CONFIG_ERROR;
84 			goto out;
85 #endif
86 		default:
87 			THIS_SHOULD_NEVER_HAPPEN;
88 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid TLS connection parameters."));
89 			ret = CONFIG_ERROR;
90 			goto out;
91 	}
92 
93 	if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, item->interface.addr, item->interface.port, 0,
94 			item->host.tls_connect, tls_arg1, tls_arg2)))
95 	{
96 		zabbix_log(LOG_LEVEL_DEBUG, "Sending [%s]", item->key);
97 
98 		if (SUCCEED != zbx_tcp_send(&s, item->key))
99 			ret = NETWORK_ERROR;
100 		else if (FAIL != (received_len = zbx_tcp_recv_ext(&s, 0)))
101 			ret = SUCCEED;
102 		else if (SUCCEED == zbx_alarm_timed_out())
103 			ret = TIMEOUT_ERROR;
104 		else
105 			ret = NETWORK_ERROR;
106 	}
107 	else
108 		ret = NETWORK_ERROR;
109 
110 	if (SUCCEED == ret)
111 	{
112 		zbx_rtrim(s.buffer, " \r\n");
113 		zbx_ltrim(s.buffer, " ");
114 
115 		zabbix_log(LOG_LEVEL_DEBUG, "get value from agent result: '%s'", s.buffer);
116 
117 		if (0 == strcmp(s.buffer, ZBX_NOTSUPPORTED))
118 		{
119 			/* 'ZBX_NOTSUPPORTED\0<error message>' */
120 			if (sizeof(ZBX_NOTSUPPORTED) < s.read_bytes)
121 				SET_MSG_RESULT(result, zbx_dsprintf(NULL, "%s", s.buffer + sizeof(ZBX_NOTSUPPORTED)));
122 			else
123 				SET_MSG_RESULT(result, zbx_strdup(NULL, "Not supported by Zabbix Agent"));
124 
125 			ret = NOTSUPPORTED;
126 		}
127 		else if (0 == strcmp(s.buffer, ZBX_ERROR))
128 		{
129 			SET_MSG_RESULT(result, zbx_strdup(NULL, "Zabbix Agent non-critical error"));
130 			ret = AGENT_ERROR;
131 		}
132 		else if (0 == received_len)
133 		{
134 			SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Received empty response from Zabbix Agent at [%s]."
135 					" Assuming that agent dropped connection because of access permissions.",
136 					item->interface.addr));
137 			ret = NETWORK_ERROR;
138 		}
139 		else
140 			set_result_type(result, ITEM_VALUE_TYPE_TEXT, s.buffer);
141 	}
142 	else
143 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Get value from agent failed: %s", zbx_socket_strerror()));
144 
145 	zbx_tcp_close(&s);
146 out:
147 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));
148 
149 	return ret;
150 }
151