1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
|
/*
** Zabbix
** Copyright (C) 2001-2021 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include "log.h"
#include "zbxjson.h"
#include "dbcache.h"
#include "audit.h"
#define AUDIT_USERID 0
#define AUDIT_USERNAME "System"
#define AUDIT_IP ""
static int audit_mode;
static zbx_hashset_t zbx_audit;
int zbx_get_audit_mode(void)
{
return audit_mode;
}
zbx_hashset_t *zbx_get_audit_hashset(void)
{
return &zbx_audit;
}
zbx_audit_entry_t *zbx_audit_entry_init(zbx_uint64_t id, const char *name, int audit_action, int resource_type)
{
zbx_audit_entry_t *audit_entry;
audit_entry = (zbx_audit_entry_t*)zbx_malloc(NULL, sizeof(zbx_audit_entry_t));
audit_entry->id = id;
audit_entry->cuid = NULL;
audit_entry->name = zbx_strdup(NULL, name);
audit_entry->audit_action = audit_action;
audit_entry->resource_type = resource_type;
zbx_json_init(&(audit_entry->details_json), ZBX_JSON_STAT_BUF_LEN);
return audit_entry;
}
zbx_audit_entry_t *zbx_audit_entry_init_cuid(const char *cuid, const char *name, int audit_action,
int resource_type)
{
zbx_audit_entry_t *audit_entry;
audit_entry = (zbx_audit_entry_t*)zbx_malloc(NULL, sizeof(zbx_audit_entry_t));
audit_entry->id = 0;
audit_entry->cuid = zbx_strdup(NULL, cuid);
audit_entry->name = zbx_strdup(NULL, name);
audit_entry->audit_action = audit_action;
audit_entry->resource_type = resource_type;
zbx_json_init(&(audit_entry->details_json), ZBX_JSON_STAT_BUF_LEN);
return audit_entry;
}
static void append_str_json(struct zbx_json *json, const char *audit_op, const char *key, const char *val)
{
zbx_json_addarray(json, key);
zbx_json_addstring(json, NULL, audit_op, ZBX_JSON_TYPE_STRING);
zbx_json_addstring(json, NULL, val, ZBX_JSON_TYPE_STRING);
zbx_json_close(json);
}
static void append_uint64_json(struct zbx_json *json, const char *audit_op, const char *key, const uint64_t val)
{
zbx_json_addarray(json, key);
zbx_json_addstring(json, NULL, audit_op, ZBX_JSON_TYPE_STRING);
zbx_json_adduint64(json, NULL, val);
zbx_json_close(json);
}
static void append_int_json(struct zbx_json *json, const char *audit_op, const char *key, int val)
{
zbx_json_addarray(json, key);
zbx_json_addstring(json, NULL, audit_op, ZBX_JSON_TYPE_STRING);
zbx_json_addint64(json, NULL, val);
zbx_json_close(json);
}
static void append_double_json(struct zbx_json *json, const char *audit_op, const char *key, double val)
{
zbx_json_addarray(json, key);
zbx_json_addstring(json, NULL, audit_op, ZBX_JSON_TYPE_STRING);
zbx_json_addfloat(json, NULL, val);
zbx_json_close(json);
}
static void append_json_no_value(struct zbx_json *json, const char *audit_op, const char *key)
{
zbx_json_addarray(json, key);
zbx_json_addstring(json, NULL, audit_op, ZBX_JSON_TYPE_STRING);
zbx_json_close(json);
}
static void update_str_json(struct zbx_json *json, const char *key, const char *val_old, const char *val_new)
{
zbx_json_addarray(json, key);
zbx_json_addstring(json, NULL, "update", ZBX_JSON_TYPE_STRING);
zbx_json_addstring(json, NULL, val_new, ZBX_JSON_TYPE_STRING);
zbx_json_addstring(json, NULL, val_old, ZBX_JSON_TYPE_STRING);
zbx_json_close(json);
}
static void update_uint64_json(struct zbx_json *json, const char *key, uint64_t val_old, uint64_t val_new)
{
zbx_json_addarray(json, key);
zbx_json_addstring(json, NULL, "update", ZBX_JSON_TYPE_STRING);
zbx_json_adduint64(json, NULL, val_new);
zbx_json_adduint64(json, NULL, val_old);
zbx_json_close(json);
}
static void update_int_json(struct zbx_json *json, const char *key, int val_old, int val_new)
{
zbx_json_addarray(json, key);
zbx_json_addstring(json, NULL, "update", ZBX_JSON_TYPE_STRING);
zbx_json_addint64(json, NULL, val_new);
zbx_json_addint64(json, NULL, val_old);
zbx_json_close(json);
}
static void update_double_json(struct zbx_json *json, const char *key, double val_old, double val_new)
{
zbx_json_addarray(json, key);
zbx_json_addstring(json, NULL, "update", ZBX_JSON_TYPE_STRING);
zbx_json_addfloat(json, NULL, val_new);
zbx_json_addfloat(json, NULL, val_old);
zbx_json_close(json);
}
static void delete_json(struct zbx_json *json, const char *audit_op, const char *key)
{
zbx_json_addarray(json, key);
zbx_json_addstring(json, NULL, audit_op, ZBX_JSON_TYPE_STRING);
zbx_json_close(json);
}
/******************************************************************************
* *
* Function: zbx_auditlog_global_script *
* *
* Purpose: record global script execution results into audit log *
* *
* Comments: 'hostid' should be always > 0. 'eventid' is > 0 in case of *
* "manual script on event" *
* *
******************************************************************************/
int zbx_auditlog_global_script(unsigned char script_type, unsigned char script_execute_on,
const char *script_command_orig, zbx_uint64_t hostid, const char *hostname, zbx_uint64_t eventid,
zbx_uint64_t proxy_hostid, zbx_uint64_t userid, const char *username, const char *clientip,
const char *output, const char *error)
{
int ret = SUCCEED;
char auditid_cuid[CUID_LEN], execute_on_s[MAX_ID_LEN + 1], hostid_s[MAX_ID_LEN + 1],
eventid_s[MAX_ID_LEN + 1], proxy_hostid_s[MAX_ID_LEN + 1];
struct zbx_json details_json;
zbx_config_t cfg;
zabbix_log(LOG_LEVEL_TRACE, "In %s()", __func__);
zbx_config_get(&cfg, ZBX_CONFIG_FLAGS_AUDITLOG_ENABLED);
if (ZBX_AUDITLOG_ENABLED != cfg.auditlog_enabled)
goto out;
zbx_new_cuid(auditid_cuid);
zbx_json_init(&details_json, ZBX_JSON_STAT_BUF_LEN);
zbx_snprintf(execute_on_s, sizeof(execute_on_s), "%hhu", script_execute_on);
append_str_json(&details_json, AUDIT_DETAILS_ACTION_ADD, "script.execute_on", execute_on_s);
if (0 != eventid)
{
zbx_snprintf(eventid_s, sizeof(eventid_s), ZBX_FS_UI64, eventid);
append_str_json(&details_json, AUDIT_DETAILS_ACTION_ADD, "script.eventid", eventid_s);
}
zbx_snprintf(hostid_s, sizeof(hostid_s), ZBX_FS_UI64, hostid);
append_str_json(&details_json, AUDIT_DETAILS_ACTION_ADD, "script.hostid", hostid_s);
if (0 != proxy_hostid)
{
zbx_snprintf(proxy_hostid_s, sizeof(proxy_hostid_s), ZBX_FS_UI64, proxy_hostid);
append_str_json(&details_json, AUDIT_DETAILS_ACTION_ADD, "script.proxy_hostid", proxy_hostid_s);
}
if (ZBX_SCRIPT_TYPE_WEBHOOK != script_type)
append_str_json(&details_json, AUDIT_DETAILS_ACTION_ADD, "script.command", script_command_orig);
if (NULL != output)
{
char *output_esc;
output_esc = zbx_strdup(NULL, output);
zbx_json_escape(&output_esc);
append_str_json(&details_json, AUDIT_DETAILS_ACTION_ADD, "script.output", output_esc);
zbx_free(output_esc);
}
if (NULL != error)
append_str_json(&details_json, AUDIT_DETAILS_ACTION_ADD, "script.error", error);
if (ZBX_DB_OK > DBexecute("insert into auditlog (auditid,userid,username,clock,action,ip,resourceid,"
"resourcename,resourcetype,recordsetid,details) values ('%s'," ZBX_FS_UI64 ",'%s',%d,'%d','%s',"
ZBX_FS_UI64 ",'%s',%d,'%s','%s')", auditid_cuid, userid, username, (int)time(NULL),
AUDIT_ACTION_EXECUTE, clientip, hostid, hostname, AUDIT_RESOURCE_SCRIPT, auditid_cuid,
details_json.buffer))
{
ret = FAIL;
}
zbx_json_free(&details_json);
out:
zabbix_log(LOG_LEVEL_TRACE, "End of %s():%s", __func__, zbx_result_string(ret));
return ret;
}
static unsigned zbx_audit_hash_func(const void *data)
{
const zbx_audit_entry_t * const *audit_entry = (const zbx_audit_entry_t * const *)data;
zbx_hash_t hash;
hash = ZBX_DEFAULT_UINT64_HASH_FUNC(&(*audit_entry)->id);
if (NULL != (*audit_entry)->cuid)
hash = ZBX_DEFAULT_STRING_HASH_ALGO((*audit_entry)->cuid, strlen((*audit_entry)->cuid), hash);
return hash;
}
static int zbx_audit_compare_func(const void *d1, const void *d2)
{
const zbx_audit_entry_t * const *audit_entry_1 = (const zbx_audit_entry_t * const *)d1;
const zbx_audit_entry_t * const *audit_entry_2 = (const zbx_audit_entry_t * const *)d2;
ZBX_RETURN_IF_NOT_EQUAL((*audit_entry_1)->id, (*audit_entry_2)->id);
return zbx_strcmp_null((*audit_entry_1)->cuid, (*audit_entry_2)->cuid);
}
void zbx_audit_clean(void)
{
zbx_hashset_iter_t iter;
zbx_audit_entry_t **audit_entry;
RETURN_IF_AUDIT_OFF();
zbx_hashset_iter_reset(&zbx_audit, &iter);
while (NULL != (audit_entry = (zbx_audit_entry_t **)zbx_hashset_iter_next(&iter)))
{
zbx_json_free(&((*audit_entry)->details_json));
zbx_free((*audit_entry)->name);
zbx_free((*audit_entry)->cuid);
zbx_free(*audit_entry);
}
zbx_hashset_destroy(&zbx_audit);
}
void zbx_audit_init(int audit_mode_set)
{
audit_mode = audit_mode_set;
RETURN_IF_AUDIT_OFF();
#define AUDIT_HASHSET_DEF_SIZE 100
zbx_hashset_create(&zbx_audit, AUDIT_HASHSET_DEF_SIZE, zbx_audit_hash_func, zbx_audit_compare_func);
#undef AUDIT_HASHSET_DEF_SIZE
}
int zbx_audit_initialized(void)
{
if (ZBX_AUDITLOG_ENABLED != zbx_get_audit_mode())
return SUCCEED;
return 0 == zbx_audit.num_slots ? SUCCEED : FAIL;
}
void zbx_audit_flush(void)
{
char audit_cuid[CUID_LEN], recsetid_cuid[CUID_LEN];
zbx_hashset_iter_t iter;
zbx_audit_entry_t **audit_entry;
zbx_db_insert_t db_insert_audit;
RETURN_IF_AUDIT_OFF();
zbx_new_cuid(recsetid_cuid);
zbx_hashset_iter_reset(&zbx_audit, &iter);
zbx_db_insert_prepare(&db_insert_audit, "auditlog", "auditid", "userid", "username", "clock", "action", "ip",
"resourceid", "resourcename", "resourcetype", "recordsetid", "details", NULL);
while (NULL != (audit_entry = (zbx_audit_entry_t **)zbx_hashset_iter_next(&iter)))
{
zbx_new_cuid(audit_cuid);
if (AUDIT_ACTION_DELETE == (*audit_entry)->audit_action ||
0 != strcmp((*audit_entry)->details_json.buffer, "{}"))
{
zbx_db_insert_add_values(&db_insert_audit, audit_cuid, AUDIT_USERID, AUDIT_USERNAME,
(int)time(NULL), (*audit_entry)->audit_action, AUDIT_IP, (*audit_entry)->id,
(*audit_entry)->name, (*audit_entry)->resource_type,
recsetid_cuid, 0 == strcmp((*audit_entry)->details_json.buffer, "{}") ? "" :
(*audit_entry)->details_json.buffer);
}
}
zbx_db_insert_execute(&db_insert_audit);
zbx_db_insert_clean(&db_insert_audit);
zbx_audit_clean();
}
int zbx_audit_flush_once(void)
{
char audit_cuid[CUID_LEN], recsetid_cuid[CUID_LEN];
int ret = ZBX_DB_OK;
zbx_hashset_iter_t iter;
zbx_audit_entry_t **audit_entry;
if (ZBX_AUDITLOG_ENABLED != zbx_get_audit_mode())
return ZBX_DB_OK;
zbx_new_cuid(recsetid_cuid);
zbx_hashset_iter_reset(&zbx_audit, &iter);
while (NULL != (audit_entry = (zbx_audit_entry_t **)zbx_hashset_iter_next(&iter)))
{
char id[ZBX_MAX_UINT64_LEN + 1], *pfield, *pvalue, *name_esc;
if (AUDIT_ACTION_DELETE != (*audit_entry)->audit_action &&
0 == strcmp((*audit_entry)->details_json.buffer, "{}"))
{
continue;
}
zbx_new_cuid(audit_cuid);
if (0 != (*audit_entry)->id)
{
zbx_snprintf(id, sizeof(id), ZBX_FS_UI64, (*audit_entry)->id);
pfield = "resourceid";
pvalue = id;
}
else
{
pfield = "resource_cuid";
pvalue = (*audit_entry)->cuid;
}
name_esc = DBdyn_escape_string((*audit_entry)->name);
ret = DBexecute_once("insert into auditlog (auditid,userid,username,"
"clock,action,ip,%s,resourcename,resourcetype,recordsetid,details) values"
" ('%s',%d,'%s','%d','%d','%s','%s','%s',%d,'%s','%s')",
pfield, audit_cuid, AUDIT_USERID, AUDIT_USERNAME, (int)time(NULL),
(*audit_entry)->audit_action, AUDIT_IP, pvalue, name_esc, (*audit_entry)->resource_type,
recsetid_cuid, 0 == strcmp((*audit_entry)->details_json.buffer, "{}") ? "" :
(*audit_entry)->details_json.buffer);
zbx_free(name_esc);
if (ZBX_DB_OK > ret)
break;
}
zbx_audit_clean();
return ret;
}
void zbx_audit_update_json_append_string(const zbx_uint64_t id, const char *audit_op, const char *key,
const char *value)
{
zbx_audit_entry_t local_audit_entry, **found_audit_entry;
zbx_audit_entry_t *local_audit_entry_x = &local_audit_entry;
local_audit_entry.id = id;
local_audit_entry.cuid = NULL;
found_audit_entry = (zbx_audit_entry_t**)zbx_hashset_search(&zbx_audit, &(local_audit_entry_x));
if (NULL == found_audit_entry)
{
THIS_SHOULD_NEVER_HAPPEN;
exit(EXIT_FAILURE);
}
append_str_json(&((*found_audit_entry)->details_json), audit_op, key, value);
}
void zbx_audit_update_json_append_uint64(const zbx_uint64_t id, const char *audit_op, const char *key,
uint64_t value)
{
zbx_audit_entry_t local_audit_entry, **found_audit_entry;
zbx_audit_entry_t *local_audit_entry_x = &local_audit_entry;
local_audit_entry.id = id;
local_audit_entry.cuid = NULL;
found_audit_entry = (zbx_audit_entry_t**)zbx_hashset_search(&zbx_audit, &(local_audit_entry_x));
if (NULL == found_audit_entry)
{
THIS_SHOULD_NEVER_HAPPEN;
exit(EXIT_FAILURE);
}
append_uint64_json(&((*found_audit_entry)->details_json), audit_op, key, value);
}
#define PREPARE_UPDATE_JSON_APPEND_OP(...) \
zbx_audit_entry_t local_audit_entry, **found_audit_entry; \
zbx_audit_entry_t *local_audit_entry_x = &local_audit_entry; \
\
local_audit_entry.id = id; \
local_audit_entry.cuid = NULL; \
\
found_audit_entry = (zbx_audit_entry_t**)zbx_hashset_search(&zbx_audit, \
&(local_audit_entry_x)); \
if (NULL == found_audit_entry) \
{ \
THIS_SHOULD_NEVER_HAPPEN; \
exit(EXIT_FAILURE); \
} \
void zbx_audit_update_json_append_no_value(const zbx_uint64_t id, const char *audit_op, const char *key)
{
PREPARE_UPDATE_JSON_APPEND_OP();
append_json_no_value(&((*found_audit_entry)->details_json), audit_op, key);
}
void zbx_audit_update_json_append_int(const zbx_uint64_t id, const char *audit_op, const char *key, int value)
{
PREPARE_UPDATE_JSON_APPEND_OP();
append_int_json(&((*found_audit_entry)->details_json), audit_op, key, value);
}
void zbx_audit_update_json_append_double(const zbx_uint64_t id, const char *audit_op, const char *key, double value)
{
PREPARE_UPDATE_JSON_APPEND_OP();
append_double_json(&((*found_audit_entry)->details_json), audit_op, key, value);
}
void zbx_audit_update_json_update_string(const zbx_uint64_t id, const char *key, const char *value_old,
const char *value_new)
{
PREPARE_UPDATE_JSON_APPEND_OP();
update_str_json(&((*found_audit_entry)->details_json), key, value_old, value_new);
}
void zbx_audit_update_json_update_uint64(const zbx_uint64_t id, const char *key, uint64_t value_old,
uint64_t value_new)
{
PREPARE_UPDATE_JSON_APPEND_OP();
update_uint64_json(&((*found_audit_entry)->details_json), key, value_old, value_new);
}
void zbx_audit_update_json_update_int(const zbx_uint64_t id, const char *key, int value_old,
int value_new)
{
PREPARE_UPDATE_JSON_APPEND_OP();
update_int_json(&((*found_audit_entry)->details_json), key, value_old, value_new);
}
void zbx_audit_update_json_update_double(const zbx_uint64_t id, const char *key, double value_old,
double value_new)
{
PREPARE_UPDATE_JSON_APPEND_OP();
update_double_json(&((*found_audit_entry)->details_json), key, value_old, value_new);
}
void zbx_audit_update_json_delete(const zbx_uint64_t id, const char *audit_op, const char *key)
{
PREPARE_UPDATE_JSON_APPEND_OP();
delete_json(&((*found_audit_entry)->details_json), audit_op, key);
}
zbx_audit_entry_t *zbx_audit_get_entry(zbx_uint64_t id, const char *cuid)
{
zbx_audit_entry_t local_audit_entry, *plocal_audit_entry = &local_audit_entry, **paudit_entry;
local_audit_entry.id = id;
local_audit_entry.cuid = (char *)cuid;
if (NULL == (paudit_entry = (zbx_audit_entry_t**)zbx_hashset_search(&zbx_audit, &plocal_audit_entry)))
{
THIS_SHOULD_NEVER_HAPPEN;
exit(EXIT_FAILURE);
}
return *paudit_entry;
}
void zbx_audit_entry_append_int(zbx_audit_entry_t *entry, int audit_op, const char *key, ...)
{
va_list args;
int value1, value2;
va_start(args, key);
value1 = va_arg(args, int);
switch (audit_op)
{
case AUDIT_ACTION_ADD:
append_int_json(&entry->details_json, AUDIT_DETAILS_ACTION_ADD, key, value1);
break;
case AUDIT_ACTION_UPDATE:
value2 = va_arg(args, int);
update_int_json(&entry->details_json, key, value1, value2);
break;
}
va_end(args);
}
void zbx_audit_entry_append_string(zbx_audit_entry_t *entry, int audit_op, const char *key, ...)
{
va_list args;
const char *value1, *value2;
va_start(args, key);
value1 = va_arg(args, const char *);
switch (audit_op)
{
case AUDIT_ACTION_ADD:
append_str_json(&entry->details_json, AUDIT_DETAILS_ACTION_ADD, key, value1);
break;
case AUDIT_ACTION_UPDATE:
value2 = va_arg(args, const char *);
update_str_json(&entry->details_json, key, value1, value2);
break;
}
va_end(args);
}
|