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 "db.h"
22 #include "log.h"
23 #include "proxy.h"
24 
25 #include "proxyautoreg.h"
26 
27 /******************************************************************************
28  *                                                                            *
29  * Function: recv_areg_data                                                   *
30  *                                                                            *
31  * Purpose: receive auto registration data from proxy                         *
32  *                                                                            *
33  * Author: Alexander Vladishev                                                *
34  *                                                                            *
35  ******************************************************************************/
recv_areg_data(zbx_socket_t * sock,struct zbx_json_parse * jp,zbx_timespec_t * ts)36 void	recv_areg_data(zbx_socket_t *sock, struct zbx_json_parse *jp, zbx_timespec_t *ts)
37 {
38 	const char	*__function_name = "recv_areg_data";
39 
40 	int		ret;
41 	char		*error = NULL;
42 	DC_PROXY	proxy;
43 
44 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
45 
46 	if (SUCCEED != (ret = get_active_proxy_from_request(jp, &proxy, &error)))
47 	{
48 		zabbix_log(LOG_LEVEL_WARNING, "cannot parse autoregistration data from active proxy at \"%s\": %s",
49 				sock->peer, error);
50 		goto out;
51 	}
52 
53 	if (SUCCEED != (ret = zbx_proxy_check_permissions(&proxy, sock, &error)))
54 	{
55 		zabbix_log(LOG_LEVEL_WARNING, "cannot accept connection from proxy \"%s\" at \"%s\", allowed address:"
56 				" \"%s\": %s", proxy.host, sock->peer, proxy.proxy_address, error);
57 		goto out;
58 	}
59 
60 	zbx_update_proxy_data(&proxy, zbx_get_protocol_version(jp), time(NULL),
61 			(0 != (sock->protocol & ZBX_TCP_COMPRESS) ? 1 : 0));
62 
63 	if (SUCCEED != zbx_check_protocol_version(&proxy))
64 	{
65 		goto out;
66 	}
67 
68 	if (SUCCEED != (ret = process_auto_registration(jp, proxy.hostid, ts, &error)))
69 	{
70 		zabbix_log(LOG_LEVEL_WARNING, "received invalid autoregistration data from proxy \"%s\" at \"%s\": %s",
71 				proxy.host, sock->peer, error);
72 	}
73 out:
74 	zbx_send_response(sock, ret, error, CONFIG_TIMEOUT);
75 
76 	zbx_free(error);
77 
78 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));
79 }
80 
81 /******************************************************************************
82  *                                                                            *
83  * Function: send_areg_data                                                   *
84  *                                                                            *
85  * Purpose: send auto registration data from proxy to a server                *
86  *                                                                            *
87  ******************************************************************************/
send_areg_data(zbx_socket_t * sock)88 void	send_areg_data(zbx_socket_t *sock)
89 {
90 	const char	*__function_name = "send_areg_data";
91 
92 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
93 
94 	/* do not send any reply to server in this case as the server expects auto registration data */
95 	if (SUCCEED == check_access_passive_proxy(sock, ZBX_DO_NOT_SEND_RESPONSE, "auto registration data request"))
96 		zbx_send_proxy_response(sock, FAIL, "Deprecated request", CONFIG_TIMEOUT);
97 
98 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name);
99 }
100