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 "proxyconfig.h"
26 #include "../../libs/zbxcrypto/tls_tcp_active.h"
27 #include "zbxcompress.h"
28 #include "zbxipcservice.h"
29
30 /******************************************************************************
31 * *
32 * Function: send_proxyconfig *
33 * *
34 * Purpose: send configuration tables to the proxy from server *
35 * (for active proxies) *
36 * *
37 * Author: Alexander Vladishev *
38 * *
39 ******************************************************************************/
send_proxyconfig(zbx_socket_t * sock,struct zbx_json_parse * jp)40 void send_proxyconfig(zbx_socket_t *sock, struct zbx_json_parse *jp)
41 {
42 char *error = NULL, *buffer = NULL;
43 struct zbx_json j;
44 DC_PROXY proxy;
45 int ret, flags = ZBX_TCP_PROTOCOL;
46 size_t buffer_size, reserved = 0;
47
48 zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
49
50 if (SUCCEED != get_active_proxy_from_request(jp, &proxy, &error))
51 {
52 zabbix_log(LOG_LEVEL_WARNING, "cannot parse proxy configuration data request from active proxy at"
53 " \"%s\": %s", sock->peer, error);
54 goto out;
55 }
56
57 if (SUCCEED != zbx_proxy_check_permissions(&proxy, sock, &error))
58 {
59 zabbix_log(LOG_LEVEL_WARNING, "cannot accept connection from proxy \"%s\" at \"%s\", allowed address:"
60 " \"%s\": %s", proxy.host, sock->peer, proxy.proxy_address, error);
61 goto out;
62 }
63
64 zbx_update_proxy_data(&proxy, zbx_get_proxy_protocol_version(jp), time(NULL),
65 (0 != (sock->protocol & ZBX_TCP_COMPRESS) ? 1 : 0), ZBX_FLAGS_PROXY_DIFF_UPDATE_CONFIG);
66
67 if (0 != proxy.auto_compress)
68 flags |= ZBX_TCP_COMPRESS;
69
70 zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN);
71
72 if (SUCCEED != get_proxyconfig_data(proxy.hostid, &j, &error))
73 {
74 zbx_send_response_ext(sock, FAIL, error, NULL, flags, CONFIG_TIMEOUT);
75 zabbix_log(LOG_LEVEL_WARNING, "cannot collect configuration data for proxy \"%s\" at \"%s\": %s",
76 proxy.host, sock->peer, error);
77 goto clean;
78 }
79
80 zabbix_log(LOG_LEVEL_DEBUG, "%s", j.buffer);
81
82 if (0 != proxy.auto_compress)
83 {
84 if (SUCCEED != zbx_compress(j.buffer, j.buffer_size, &buffer, &buffer_size))
85 {
86 zabbix_log(LOG_LEVEL_ERR,"cannot compress data: %s", zbx_compress_strerror());
87 goto clean;
88 }
89
90 reserved = j.buffer_size;
91
92 zbx_json_free(&j); /* json buffer can be large, free as fast as possible */
93
94 zabbix_log(LOG_LEVEL_WARNING, "sending configuration data to proxy \"%s\" at \"%s\", datalen "
95 ZBX_FS_SIZE_T ", bytes " ZBX_FS_SIZE_T " with compression ratio %.1f", proxy.host,
96 sock->peer, (zbx_fs_size_t)reserved, (zbx_fs_size_t)buffer_size,
97 (double)reserved / buffer_size);
98
99 ret = zbx_tcp_send_ext(sock, buffer, buffer_size, reserved, flags, CONFIG_TRAPPER_TIMEOUT);
100 }
101 else
102 {
103 zabbix_log(LOG_LEVEL_WARNING, "sending configuration data to proxy \"%s\" at \"%s\", datalen "
104 ZBX_FS_SIZE_T, proxy.host, sock->peer, (zbx_fs_size_t)j.buffer_size);
105
106 ret = zbx_tcp_send_ext(sock, j.buffer, strlen(j.buffer), 0, flags, CONFIG_TRAPPER_TIMEOUT);
107 }
108
109 if (SUCCEED != ret)
110 {
111 zabbix_log(LOG_LEVEL_WARNING, "cannot send configuration data to proxy \"%s\" at \"%s\": %s",
112 proxy.host, sock->peer, zbx_socket_strerror());
113 }
114 clean:
115 zbx_json_free(&j);
116 out:
117 zbx_free(error);
118 zbx_free(buffer);
119
120 zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __func__);
121 }
122
123 /******************************************************************************
124 * *
125 * Function: recv_proxyconfig *
126 * *
127 * Purpose: receive configuration tables from server (passive proxies) *
128 * *
129 * Author: Alexander Vladishev *
130 * *
131 ******************************************************************************/
recv_proxyconfig(zbx_socket_t * sock,struct zbx_json_parse * jp)132 void recv_proxyconfig(zbx_socket_t *sock, struct zbx_json_parse *jp)
133 {
134 struct zbx_json_parse jp_data;
135 int ret;
136
137 zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
138
139 if (SUCCEED != (ret = zbx_json_brackets_by_name(jp, ZBX_PROTO_TAG_DATA, &jp_data)))
140 {
141 zabbix_log(LOG_LEVEL_WARNING, "cannot parse proxy configuration data received from server at"
142 " \"%s\": %s", sock->peer, zbx_json_strerror());
143 zbx_send_proxy_response(sock, ret, zbx_json_strerror(), CONFIG_TIMEOUT);
144 goto out;
145 }
146
147 if (SUCCEED != check_access_passive_proxy(sock, ZBX_SEND_RESPONSE, "configuration update"))
148 goto out;
149
150 if (SUCCEED == process_proxyconfig(&jp_data))
151 {
152 unsigned char *result;
153 char *error = NULL;
154
155 if (SUCCEED == zbx_ipc_async_exchange(ZBX_IPC_SERVICE_CONFIG, ZBX_IPC_CONFIG_RELOAD_REQUEST,
156 ZBX_IPC_WAIT_FOREVER, NULL, 0, &result, &error))
157 {
158 zbx_free(result);
159 }
160 else
161 {
162 THIS_SHOULD_NEVER_HAPPEN;
163 zabbix_log(LOG_LEVEL_WARNING, "cannot send message to configuration syncer: %s", error);
164 zbx_free(error);
165 }
166 }
167 zbx_send_proxy_response(sock, ret, NULL, CONFIG_TIMEOUT);
168 out:
169 zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __func__);
170 }
171