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 #ifndef ZABBIX_ZJSON_H
21 #define ZABBIX_ZJSON_H
22 
23 #define ZBX_PROTO_TAG_CLOCK			"clock"
24 #define ZBX_PROTO_TAG_NS			"ns"
25 #define ZBX_PROTO_TAG_DATA			"data"
26 #define ZBX_PROTO_TAG_REGEXP			"regexp"
27 #define ZBX_PROTO_TAG_DELAY			"delay"
28 #define ZBX_PROTO_TAG_REFRESH_UNSUPPORTED	"refresh_unsupported"
29 #define ZBX_PROTO_TAG_DRULE			"drule"
30 #define ZBX_PROTO_TAG_DCHECK			"dcheck"
31 #define ZBX_PROTO_TAG_HOST			"host"
32 #define ZBX_PROTO_TAG_HOST_METADATA		"host_metadata"
33 #define ZBX_PROTO_TAG_INFO			"info"
34 #define ZBX_PROTO_TAG_IP			"ip"
35 #define ZBX_PROTO_TAG_DNS			"dns"
36 #define ZBX_PROTO_TAG_CONN			"conn"
37 #define ZBX_PROTO_TAG_KEY			"key"
38 #define ZBX_PROTO_TAG_KEY_ORIG			"key_orig"
39 #define ZBX_PROTO_TAG_KEYS			"keys"
40 #define ZBX_PROTO_TAG_LASTLOGSIZE		"lastlogsize"
41 #define ZBX_PROTO_TAG_MTIME			"mtime"
42 #define ZBX_PROTO_TAG_LOGTIMESTAMP		"timestamp"
43 #define ZBX_PROTO_TAG_LOGSOURCE			"source"
44 #define ZBX_PROTO_TAG_LOGSEVERITY		"severity"
45 #define ZBX_PROTO_TAG_LOGEVENTID		"eventid"
46 #define ZBX_PROTO_TAG_PORT			"port"
47 #define ZBX_PROTO_TAG_TLS_ACCEPTED		"tls_accepted"
48 #define ZBX_PROTO_TAG_PROXY			"proxy"
49 #define ZBX_PROTO_TAG_REQUEST			"request"
50 #define ZBX_PROTO_TAG_RESPONSE			"response"
51 #define ZBX_PROTO_TAG_STATUS			"status"
52 #define ZBX_PROTO_TAG_STATE			"state"
53 #define ZBX_PROTO_TAG_TYPE			"type"
54 #define ZBX_PROTO_TAG_LIMIT			"limit"
55 #define ZBX_PROTO_TAG_VALUE			"value"
56 #define ZBX_PROTO_TAG_SCRIPTID			"scriptid"
57 #define ZBX_PROTO_TAG_HOSTID			"hostid"
58 #define ZBX_PROTO_TAG_AVAILABLE			"available"
59 #define ZBX_PROTO_TAG_ERROR			"error"
60 #define ZBX_PROTO_TAG_USERNAME			"username"
61 #define ZBX_PROTO_TAG_PASSWORD			"password"
62 #define ZBX_PROTO_TAG_SID			"sid"
63 #define ZBX_PROTO_TAG_VERSION			"version"
64 #define ZBX_PROTO_TAG_INTERFACE_AVAILABILITY	"interface availability"
65 #define ZBX_PROTO_TAG_HISTORY_DATA		"history data"
66 #define ZBX_PROTO_TAG_DISCOVERY_DATA		"discovery data"
67 #define ZBX_PROTO_TAG_AUTOREGISTRATION		"auto registration"
68 #define ZBX_PROTO_TAG_MORE			"more"
69 #define ZBX_PROTO_TAG_ITEMID			"itemid"
70 #define ZBX_PROTO_TAG_TTL			"ttl"
71 #define ZBX_PROTO_TAG_COMMANDTYPE		"commandtype"
72 #define ZBX_PROTO_TAG_COMMAND			"command"
73 #define ZBX_PROTO_TAG_EXECUTE_ON		"execute_on"
74 #define ZBX_PROTO_TAG_AUTHTYPE			"authtype"
75 #define ZBX_PROTO_TAG_PUBLICKEY			"publickey"
76 #define ZBX_PROTO_TAG_PRIVATEKEY		"privatekey"
77 #define ZBX_PROTO_TAG_PARENT_TASKID		"parent_taskid"
78 #define ZBX_PROTO_TAG_TASKS			"tasks"
79 #define ZBX_PROTO_TAG_ALERTID			"alertid"
80 #define ZBX_PROTO_TAG_JMX_ENDPOINT		"jmx_endpoint"
81 #define ZBX_PROTO_TAG_EVENTID			"eventid"
82 #define ZBX_PROTO_TAG_NAME			"name"
83 #define ZBX_PROTO_TAG_SEVERITY			"severity"
84 #define ZBX_PROTO_TAG_HOSTS			"hosts"
85 #define ZBX_PROTO_TAG_GROUPS			"groups"
86 #define ZBX_PROTO_TAG_TAGS			"tags"
87 #define ZBX_PROTO_TAG_TAG			"tag"
88 #define ZBX_PROTO_TAG_PROBLEM_EVENTID		"p_eventid"
89 #define ZBX_PROTO_TAG_ITEMID			"itemid"
90 #define ZBX_PROTO_TAG_COUNT			"count"
91 #define ZBX_PROTO_TAG_MIN			"min"
92 #define ZBX_PROTO_TAG_AVG			"avg"
93 #define ZBX_PROTO_TAG_MAX			"max"
94 #define ZBX_PROTO_TAG_SESSION			"session"
95 #define ZBX_PROTO_TAG_ID			"id"
96 #define ZBX_PROTO_TAG_PARAMS			"params"
97 #define ZBX_PROTO_TAG_FROM			"from"
98 #define ZBX_PROTO_TAG_TO			"to"
99 #define ZBX_PROTO_TAG_HISTORY			"history"
100 #define ZBX_PROTO_TAG_TIMESTAMP			"timestamp"
101 #define ZBX_PROTO_TAG_ERROR_HANDLER		"error_handler"
102 #define ZBX_PROTO_TAG_ERROR_HANDLER_PARAMS	"error_handler_params"
103 #define ZBX_PROTO_TAG_VALUE_TYPE		"value_type"
104 #define ZBX_PROTO_TAG_STEPS			"steps"
105 #define ZBX_PROTO_TAG_ACTION			"action"
106 #define ZBX_PROTO_TAG_FAILED			"failed"
107 #define ZBX_PROTO_TAG_RESULT			"result"
108 #define ZBX_PROTO_TAG_LINE_RAW			"line_raw"
109 #define ZBX_PROTO_TAG_LABELS			"labels"
110 #define ZBX_PROTO_TAG_HELP			"help"
111 #define ZBX_PROTO_TAG_MEDIATYPEID		"mediatypeid"
112 #define ZBX_PROTO_TAG_SENDTO			"sendto"
113 #define ZBX_PROTO_TAG_SUBJECT			"subject"
114 #define ZBX_PROTO_TAG_MESSAGE			"message"
115 #define ZBX_PROTO_TAG_PREVIOUS			"previous"
116 #define ZBX_PROTO_TAG_SINGLE			"single"
117 #define ZBX_PROTO_TAG_INTERFACE			"interface"
118 #define ZBX_PROTO_TAG_FLAGS			"flags"
119 #define ZBX_PROTO_TAG_PARAMETERS		"parameters"
120 #define ZBX_PROTO_TAG_PROXY_HOSTID		"proxy_hostid"
121 #define ZBX_PROTO_TAG_INTERFACE_ID		"interfaceid"
122 #define ZBX_PROTO_TAG_USEIP			"useip"
123 #define ZBX_PROTO_TAG_ADDRESS			"address"
124 #define ZBX_PROTO_TAG_TLS_CONNECT		"tls_connect"
125 #define ZBX_PROTO_TAG_TLS_ISSUER		"tls_issuer"
126 #define ZBX_PROTO_TAG_TLS_SUBJECT		"tls_subject"
127 #define ZBX_PROTO_TAG_TLS_PSK_IDENTITY		"tls_psk_identity"
128 #define ZBX_PROTO_TAG_TLS_PSK			"tls_psk"
129 #define ZBX_PROTO_TAG_FOLLOW_REDIRECTS		"follow_redirects"
130 #define ZBX_PROTO_TAG_POST_TYPE			"post_type"
131 #define ZBX_PROTO_TAG_RETRIEVE_MODE		"retrieve_mode"
132 #define ZBX_PROTO_TAG_REQUEST_METHOD		"request_method"
133 #define ZBX_PROTO_TAG_OUTPUT_FORMAT		"output_format"
134 #define ZBX_PROTO_TAG_VERIFY_PEER		"verify_peer"
135 #define ZBX_PROTO_TAG_VERIFY_HOST		"verify_host"
136 #define ZBX_PROTO_TAG_SNMP_OID			"snmp_oid"
137 #define ZBX_PROTO_TAG_DETAILS			"details"
138 #define ZBX_PROTO_TAG_VERSION			"version"
139 #define ZBX_PROTO_TAG_COMMUNITY			"community"
140 #define	ZBX_PROTO_TAG_SECURITYNAME		"securityname"
141 #define ZBX_PROTO_TAG_SECURITYLEVEL		"securitylevel"
142 #define ZBX_PROTO_TAG_AUTHPASSPHRASE		"authpassphrase"
143 #define ZBX_PROTO_TAG_PRIVPASSPHRASE		"privpassphrase"
144 #define ZBX_PROTO_TAG_AUTHPROTOCOL		"authprotocol"
145 #define ZBX_PROTO_TAG_PRIVPROTOCOL		"privprotocol"
146 #define ZBX_PROTO_TAG_CONTEXTNAME		"contextname"
147 #define ZBX_PROTO_TAG_IPMI_SENSOR		"ipmi_sensor"
148 #define ZBX_PROTO_TAG_TIMEOUT			"timeout"
149 #define ZBX_PROTO_TAG_URL			"url"
150 #define ZBX_PROTO_TAG_QUERY_FIELDS		"query_fields"
151 #define ZBX_PROTO_TAG_POSTS			"posts"
152 #define ZBX_PROTO_TAG_STATUS_CODES		"status_codes"
153 #define ZBX_PROTO_TAG_HTTP_PROXY		"http_proxy"
154 #define ZBX_PROTO_TAG_HTTP_HEADERS		"headers"
155 #define ZBX_PROTO_TAG_SSL_CERT_FILE		"ssl_cert_file"
156 #define ZBX_PROTO_TAG_SSL_KEY_FILE		"ssl_key_file"
157 #define ZBX_PROTO_TAG_SSL_KEY_PASSWORD		"ssl_key_password"
158 #define ZBX_PROTO_TAG_MAINTENANCE_STATUS	"maintenance_status"
159 #define ZBX_PROTO_TAG_MAINTENANCE_TYPE		"maintenance_type"
160 #define ZBX_PROTO_TAG_IPMI_AUTHTYPE		"ipmi_authtype"
161 #define ZBX_PROTO_TAG_IPMI_PRIVILEGE		"ipmi_privilege"
162 #define ZBX_PROTO_TAG_IPMI_USERNAME		"ipmi_username"
163 #define ZBX_PROTO_TAG_IPMI_PASSWORD		"ipmi_password"
164 #define ZBX_PROTO_TAG_DATA_TYPE			"datatype"
165 #define ZBX_PROTO_TAG_PROXY_DELAY		"proxy_delay"
166 #define ZBX_PROTO_TAG_EXPRESSIONS		"expressions"
167 #define ZBX_PROTO_TAG_EXPRESSION		"expression"
168 #define ZBX_PROTO_TAG_CLIENTIP			"clientip"
169 #define ZBX_PROTO_TAG_ITEM_TAGS			"item_tags"
170 #define ZBX_PROTO_TAG_PROXY_UPLOAD		"upload"
171 #define ZBX_PROTO_TAG_DASHBOARDID		"dashboardid"
172 #define ZBX_PROTO_TAG_USERID			"userid"
173 #define ZBX_PROTO_TAG_PERIOD			"period"
174 #define ZBX_PROTO_TAG_NOW			"now"
175 #define ZBX_PROTO_TAG_SESSIONID			"sessionid"
176 #define ZBX_PROTO_TAG_SIGN			"sign"
177 #define ZBX_PROTO_TAG_DETAIL			"detail"
178 #define ZBX_PROTO_TAG_RECIPIENT			"recipient"
179 #define ZBX_PROTO_TAG_RECIPIENTS		"recipients"
180 
181 #define ZBX_PROTO_VALUE_FAILED		"failed"
182 #define ZBX_PROTO_VALUE_SUCCESS		"success"
183 
184 #define ZBX_PROTO_VALUE_GET_ACTIVE_CHECKS	"active checks"
185 #define ZBX_PROTO_VALUE_PROXY_CONFIG		"proxy config"
186 #define ZBX_PROTO_VALUE_PROXY_HEARTBEAT		"proxy heartbeat"
187 #define ZBX_PROTO_VALUE_SENDER_DATA		"sender data"
188 #define ZBX_PROTO_VALUE_AGENT_DATA		"agent data"
189 #define ZBX_PROTO_VALUE_COMMAND			"command"
190 #define ZBX_PROTO_VALUE_JAVA_GATEWAY_INTERNAL	"java gateway internal"
191 #define ZBX_PROTO_VALUE_JAVA_GATEWAY_JMX	"java gateway jmx"
192 #define ZBX_PROTO_VALUE_GET_QUEUE		"queue.get"
193 #define ZBX_PROTO_VALUE_GET_STATUS		"status.get"
194 #define ZBX_PROTO_VALUE_PROXY_DATA		"proxy data"
195 #define ZBX_PROTO_VALUE_PROXY_TASKS		"proxy tasks"
196 
197 #define ZBX_PROTO_VALUE_GET_QUEUE_OVERVIEW	"overview"
198 #define ZBX_PROTO_VALUE_GET_QUEUE_PROXY		"overview by proxy"
199 #define ZBX_PROTO_VALUE_GET_QUEUE_DETAILS	"details"
200 
201 #define ZBX_PROTO_VALUE_GET_STATUS_PING		"ping"
202 #define ZBX_PROTO_VALUE_GET_STATUS_FULL		"full"
203 
204 #define ZBX_PROTO_VALUE_ZABBIX_STATS		"zabbix.stats"
205 #define ZBX_PROTO_VALUE_ZABBIX_STATS_QUEUE	"queue"
206 
207 #define ZBX_PROTO_VALUE_ZABBIX_ALERT_SEND	"alert.send"
208 #define ZBX_PROTO_VALUE_ZABBIX_ITEM_TEST	"item.test"
209 #define ZBX_PROTO_VALUE_PREPROCESSING_TEST	"preprocessing.test"
210 #define ZBX_PROTO_VALUE_EXPRESSIONS_EVALUATE	"expressions.evaluate"
211 
212 #define ZBX_PROTO_VALUE_PROXY_UPLOAD_ENABLED	"enabled"
213 #define ZBX_PROTO_VALUE_PROXY_UPLOAD_DISABLED	"disabled"
214 
215 #define ZBX_PROTO_VALUE_REPORT_TEST		"report.test"
216 
217 typedef enum
218 {
219 	ZBX_JSON_TYPE_UNKNOWN = 0,
220 	ZBX_JSON_TYPE_STRING,
221 	ZBX_JSON_TYPE_INT,
222 	ZBX_JSON_TYPE_ARRAY,
223 	ZBX_JSON_TYPE_OBJECT,
224 	ZBX_JSON_TYPE_NULL,
225 	ZBX_JSON_TYPE_TRUE,
226 	ZBX_JSON_TYPE_FALSE
227 }
228 zbx_json_type_t;
229 
230 typedef enum
231 {
232 	ZBX_JSON_EMPTY = 0,
233 	ZBX_JSON_COMMA
234 }
235 zbx_json_status_t;
236 
237 #define ZBX_JSON_STAT_BUF_LEN 4096
238 
239 struct zbx_json
240 {
241 	char			*buffer;
242 	char			buf_stat[ZBX_JSON_STAT_BUF_LEN];
243 	size_t			buffer_allocated;
244 	size_t			buffer_offset;
245 	size_t			buffer_size;
246 	zbx_json_status_t	status;
247 	int			level;
248 };
249 
250 struct zbx_json_parse
251 {
252 	const char		*start;
253 	const char		*end;
254 };
255 
256 const char	*zbx_json_strerror(void);
257 
258 void	zbx_json_init(struct zbx_json *j, size_t allocate);
259 void	zbx_json_initarray(struct zbx_json *j, size_t allocate);
260 void	zbx_json_clean(struct zbx_json *j);
261 void	zbx_json_cleanarray(struct zbx_json *j);
262 void	zbx_json_free(struct zbx_json *j);
263 void	zbx_json_addobject(struct zbx_json *j, const char *name);
264 void	zbx_json_addarray(struct zbx_json *j, const char *name);
265 void	zbx_json_addstring(struct zbx_json *j, const char *name, const char *string, zbx_json_type_t type);
266 void	zbx_json_adduint64(struct zbx_json *j, const char *name, zbx_uint64_t value);
267 void	zbx_json_addint64(struct zbx_json *j, const char *name, zbx_int64_t value);
268 void	zbx_json_addraw(struct zbx_json *j, const char *name, const char *data);
269 void	zbx_json_addfloat(struct zbx_json *j, const char *name, double value);
270 int	zbx_json_close(struct zbx_json *j);
271 
272 int		zbx_json_open(const char *buffer, struct zbx_json_parse *jp);
273 const char	*zbx_json_next(const struct zbx_json_parse *jp, const char *p);
274 const char	*zbx_json_next_value(const struct zbx_json_parse *jp, const char *p, char *string, size_t len,
275 		zbx_json_type_t *type);
276 const char	*zbx_json_next_value_dyn(const struct zbx_json_parse *jp, const char *p, char **string,
277 		size_t *string_alloc, zbx_json_type_t *type);
278 const char	*zbx_json_pair_next(const struct zbx_json_parse *jp, const char *p, char *name, size_t len);
279 const char	*zbx_json_pair_by_name(const struct zbx_json_parse *jp, const char *name);
280 int		zbx_json_value_by_name(const struct zbx_json_parse *jp, const char *name, char *string, size_t len,
281 		zbx_json_type_t *type);
282 int		zbx_json_value_by_name_dyn(const struct zbx_json_parse *jp, const char *name, char **string,
283 		size_t *string_alloc, zbx_json_type_t *type);
284 int		zbx_json_brackets_open(const char *p, struct zbx_json_parse *jp);
285 int		zbx_json_brackets_by_name(const struct zbx_json_parse *jp, const char *name, struct zbx_json_parse *out);
286 int		zbx_json_object_is_empty(const struct zbx_json_parse *jp);
287 int		zbx_json_count(const struct zbx_json_parse *jp);
288 const char	*zbx_json_decodevalue(const char *p, char *string, size_t size, zbx_json_type_t *type);
289 const char	*zbx_json_decodevalue_dyn(const char *p, char **string, size_t *string_alloc, zbx_json_type_t *type);
290 void		zbx_json_escape(char **string);
291 int		zbx_json_open_path(const struct zbx_json_parse *jp, const char *path, struct zbx_json_parse *out);
292 zbx_json_type_t	zbx_json_valuetype(const char *p);
293 
294 /* jsonpath support */
295 
296 typedef struct zbx_jsonpath_segment zbx_jsonpath_segment_t;
297 
298 typedef struct
299 {
300 	zbx_jsonpath_segment_t	*segments;
301 	int			segments_num;
302 	int			segments_alloc;
303 
304 	/* set to 1 when jsonpath points at single location */
305 	unsigned char		definite;
306 }
307 zbx_jsonpath_t;
308 
309 void	zbx_jsonpath_clear(zbx_jsonpath_t *jsonpath);
310 int	zbx_jsonpath_compile(const char *path, zbx_jsonpath_t *jsonpath);
311 int	zbx_jsonpath_query(const struct zbx_json_parse *jp, const char *path, char **output);
312 
313 #endif /* ZABBIX_ZJSON_H */
314