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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
<?php declare(strict_types = 1);
/*
** 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.
**/
/**
* Class to log audit records.
*/
class CAudit {
/**
* Audit actions.
*
* @var int
*/
public const ACTION_ADD = 0;
public const ACTION_UPDATE = 1;
public const ACTION_DELETE = 2;
public const ACTION_LOGOUT = 4;
public const ACTION_EXECUTE = 7;
public const ACTION_LOGIN_SUCCESS = 8;
public const ACTION_LOGIN_FAILED = 9;
/**
* Audit resources.
*
* @var int
*/
public const RESOURCE_USER = 0;
public const RESOURCE_MEDIA_TYPE = 3;
public const RESOURCE_HOST = 4;
public const RESOURCE_ACTION = 5;
public const RESOURCE_GRAPH = 6;
public const RESOURCE_USER_GROUP = 11;
public const RESOURCE_TRIGGER = 13;
public const RESOURCE_HOST_GROUP = 14;
public const RESOURCE_ITEM = 15;
public const RESOURCE_IMAGE = 16;
public const RESOURCE_VALUE_MAP = 17;
public const RESOURCE_IT_SERVICE = 18;
public const RESOURCE_MAP = 19;
public const RESOURCE_SCENARIO = 22;
public const RESOURCE_DISCOVERY_RULE = 23;
public const RESOURCE_SCRIPT = 25;
public const RESOURCE_PROXY = 26;
public const RESOURCE_MAINTENANCE = 27;
public const RESOURCE_REGEXP = 28;
public const RESOURCE_MACRO = 29;
public const RESOURCE_TEMPLATE = 30;
public const RESOURCE_TRIGGER_PROTOTYPE = 31;
public const RESOURCE_ICON_MAP = 32;
public const RESOURCE_DASHBOARD = 33;
public const RESOURCE_CORRELATION = 34;
public const RESOURCE_GRAPH_PROTOTYPE = 35;
public const RESOURCE_ITEM_PROTOTYPE = 36;
public const RESOURCE_HOST_PROTOTYPE = 37;
public const RESOURCE_AUTOREGISTRATION = 38;
public const RESOURCE_MODULE = 39;
public const RESOURCE_SETTINGS = 40;
public const RESOURCE_HOUSEKEEPING = 41;
public const RESOURCE_AUTHENTICATION = 42;
public const RESOURCE_TEMPLATE_DASHBOARD = 43;
public const RESOURCE_USER_ROLE = 44;
public const RESOURCE_AUTH_TOKEN = 45;
public const RESOURCE_SCHEDULED_REPORT = 46;
public const RESOURCE_HA_NODE = 47;
/**
* Audit details actions.
*
* @var string
*/
public const DETAILS_ACTION_ADD = 'add';
public const DETAILS_ACTION_UPDATE = 'update';
public const DETAILS_ACTION_DELETE = 'delete';
/**
* Auditlog enabled value.
*
* @var int
*/
private const AUDITLOG_ENABLE = 1;
/**
* Table names of audit resources.
* resource => table name
*
* @var array
*/
private const TABLE_NAMES = [
self::RESOURCE_AUTHENTICATION => 'config',
self::RESOURCE_AUTH_TOKEN => 'token',
self::RESOURCE_AUTOREGISTRATION => 'config',
self::RESOURCE_HOUSEKEEPING => 'config',
self::RESOURCE_MODULE => 'module',
self::RESOURCE_PROXY => 'hosts',
self::RESOURCE_REGEXP => 'regexps',
self::RESOURCE_SCHEDULED_REPORT => 'report',
self::RESOURCE_SETTINGS => 'config',
self::RESOURCE_USER => 'users',
self::RESOURCE_USER_GROUP => 'usrgrp'
];
/**
* Table primary keys of audit resources.
* resource => table key
*
* @var array
*/
private const TABLE_PKS = [
self::RESOURCE_PROXY => 'proxyid'
];
/**
* Name field names of audit resources.
* resource => name field
*
* @var array
*/
private const FIELD_NAMES = [
self::RESOURCE_AUTHENTICATION => null,
self::RESOURCE_AUTH_TOKEN => 'name',
self::RESOURCE_AUTOREGISTRATION => null,
self::RESOURCE_HOUSEKEEPING => null,
self::RESOURCE_MODULE => 'id',
self::RESOURCE_PROXY => 'host',
self::RESOURCE_REGEXP => 'name',
self::RESOURCE_SCHEDULED_REPORT => 'name',
self::RESOURCE_SETTINGS => null,
self::RESOURCE_USER => 'username',
self::RESOURCE_USER_GROUP => 'name'
];
/**
* API names of audit resources.
* resource => API name
*
* @var array
*/
private const API_NAMES = [
self::RESOURCE_AUTHENTICATION => 'authentication',
self::RESOURCE_AUTH_TOKEN => 'token',
self::RESOURCE_AUTOREGISTRATION => 'autoregistration',
self::RESOURCE_HOUSEKEEPING => 'housekeeping',
self::RESOURCE_MODULE => 'module',
self::RESOURCE_PROXY => 'proxy',
self::RESOURCE_REGEXP => 'regexp',
self::RESOURCE_SETTINGS => 'settings',
self::RESOURCE_SCHEDULED_REPORT => 'report',
self::RESOURCE_USER => 'user',
self::RESOURCE_USER_GROUP => 'usergroup'
];
/**
* Array of paths that should be masked in audit details.
*
* @var array
*/
private const MASKED_PATHS = [
self::RESOURCE_AUTHENTICATION => ['paths' => ['authentication.ldap_bind_password']],
self::RESOURCE_AUTH_TOKEN => ['paths' => ['token.token']],
self::RESOURCE_AUTOREGISTRATION => ['paths' => ['autoregistration.tls_psk_identity', 'autoregistration.tls_psk']],
// self::RESOURCE_MACRO => [
// 'paths' => ['usermacro.value'],
// 'conditions' => ['usermacro.type' => ZBX_MACRO_TYPE_SECRET]
// ],
self::RESOURCE_PROXY => ['paths' => ['proxy.tls_psk_identity', 'proxy.tls_psk']],
self::RESOURCE_USER => ['paths' => ['user.passwd']]
];
/**
* Table names of nested objects to check default values.
* path => table name
*
* @var array
*/
private const NESTED_OBJECTS_TABLE_NAMES = [
'proxy.hosts' => 'hosts',
'proxy.interface' => 'interface',
'regexp.expressions' => 'expressions',
'report.users' => 'report_user',
'report.user_groups' => 'report_usrgrp',
'user.medias' => 'media',
'user.usrgrps' => 'users_groups',
'usergroup.rights' => 'rights',
'usergroup.tag_filters' => 'tag_filter',
'usergroup.users' => 'users_groups'
];
/**
* ID field names for arrays of nested objects.
* path => id field
*
* @var array
*/
private const NESTED_OBJECTS_IDS = [
'proxy.hosts' => 'hostid',
'regexp.expressions' => 'expressionid',
'report.users' => 'reportuserid',
'report.user_groups' => 'reportusrgrpid',
'user.medias' => 'mediaid',
'user.usrgrps' => 'id',
'usergroup.rights' => 'rightid',
'usergroup.tag_filters' => 'tag_filterid',
'usergroup.users' => 'id'
];
/**
* ID field names for single nested objects.
* path => id field
*
* @var array
*/
private const NESTED_SINGLE_OBJECTS_IDS = [
'proxy.interface' => 'interfaceid'
];
/**
* Array of paths that should be skipped in audit details.
*
* @var array
*/
private const SKIP_FIELDS = ['token.creator_userid', 'token.created_at'];
/**
* Add audit records.
*
* @param string|null $userid
* @param string $ip
* @param string $username
* @param int $action CAudit::ACTION_*
* @param int $resource CAudit::RESOURCE_*
* @param array $objects
* @param array $db_objects
*/
public static function log(?string $userid, string $ip, string $username, int $action, int $resource,
array $objects, array $db_objects): void {
if (!self::isAuditEnabled() && ($resource != self::RESOURCE_SETTINGS
|| !array_key_exists(CSettingsHelper::AUDITLOG_ENABLED, current($objects)))) {
return;
}
$auditlog = [];
$clock = time();
$ip = substr($ip, 0, DB::getFieldLength('auditlog', 'ip'));
$recordsetid = self::getRecordSetId();
switch ($action) {
case self::ACTION_LOGOUT:
case self::ACTION_LOGIN_SUCCESS:
case self::ACTION_LOGIN_FAILED:
$auditlog[] = [
'userid' => $userid,
'username' => $username,
'clock' => $clock,
'ip' => $ip,
'action' => $action,
'resourcetype' => $resource,
'resourceid' => $userid,
'resourcename' => '',
'recordsetid' => $recordsetid,
'details' => ''
];
break;
default:
$table_key = array_key_exists($resource, self::TABLE_PKS)
? self::TABLE_PKS[$resource]
: DB::getPk(self::TABLE_NAMES[$resource]);
foreach ($objects as $object) {
$resourceid = $object[$table_key];
$db_object = ($action == self::ACTION_UPDATE) ? $db_objects[$resourceid] : [];
$resource_name = self::getResourceName($resource, $action, $object, $db_object);
$diff = self::handleObjectDiff($resource, $action, $object, $db_object);
if ($action == self::ACTION_UPDATE && count($diff) === 0) {
continue;
}
$auditlog[] = [
'userid' => $userid,
'username' => $username,
'clock' => $clock,
'ip' => $ip,
'action' => $action,
'resourcetype' => $resource,
'resourceid' => $resourceid,
'resourcename' => $resource_name,
'recordsetid' => $recordsetid,
'details' => (count($diff) == 0) ? '' : json_encode($diff)
];
}
}
DB::insertBatch('auditlog', $auditlog);
}
/**
* Return recordsetid. Generate recordsetid if its not been generated yet.
*
* @return string
*/
private static function getRecordSetId(): string {
static $recordsetid = null;
if ($recordsetid === null) {
$recordsetid = CCuid::generate();
}
return $recordsetid;
}
/**
* Check audit logging is enabled.
*
* @return bool
*/
private static function isAuditEnabled(): bool {
return CSettingsHelper::get(CSettingsHelper::AUDITLOG_ENABLED) == self::AUDITLOG_ENABLE;
}
/**
* Return resource name of logging object.
*
* @param int $resource
* @param int $action
* @param array $object
* @param array $db_object
*
* @return string
*/
private static function getResourceName(int $resource, int $action, array $object, array $db_object): string {
$field_name = self::FIELD_NAMES[$resource];
$resource_name = ($field_name !== null)
? (($action == self::ACTION_UPDATE)
? $db_object[$field_name]
: $object[$field_name])
: '';
if (mb_strlen($resource_name) > 255) {
$resource_name = mb_substr($resource_name, 0, 252).'...';
}
return $resource_name;
}
/**
* Prepares the details for audit log.
*
* @param int $resource
* @param int $action
* @param array $object
* @param array $db_object
*
* @return array
*/
private static function handleObjectDiff(int $resource, int $action, array $object, array $db_object): array {
if (!in_array($action, [self::ACTION_ADD, self::ACTION_UPDATE])) {
return [];
}
$api_name = self::API_NAMES[$resource];
$details = self::convertKeysToPaths($api_name, $object);
switch ($action) {
case self::ACTION_ADD:
return self::handleAdd($resource, $details);
case self::ACTION_UPDATE:
$db_details = self::convertKeysToPaths($api_name, array_intersect_key($db_object, $object));
return self::handleUpdate($resource, $details, $db_details);
}
}
/**
* Checks by path, whether the value of the object should be masked.
*
* @param int $resource
* @param string $path
* @param array $object
*
* @return bool
*/
private static function isValueToMask(int $resource, string $path, array $object): bool {
if (!array_key_exists($resource, self::MASKED_PATHS)) {
return false;
}
if (strpos($path, '[') !== false) {
$path = preg_replace('/\[[0-9]+\]/', '', $path);
}
if (!array_key_exists('conditions', self::MASKED_PATHS[$resource])) {
return in_array($path, self::MASKED_PATHS[$resource]['paths']);
}
if (in_array($path, self::MASKED_PATHS[$resource])) {
$all_counditions = count(self::MASKED_PATHS[$resource]['conditions']);
$true_conditions = 0;
foreach (self::MASKED_PATHS[$resource]['conditions'] as $condition_path => $value) {
if (array_key_exists($condition_path, $object) && $object[$condition_path] == $value) {
$true_conditions++;
}
}
return ($true_conditions == $all_counditions);
}
return false;
}
/**
* Converts the object properties to the one-dimensional array where the key is a path.
*
* @param string $prefix
* @param array $object
*
* @return array
*/
private static function convertKeysToPaths(string $prefix, array $object): array {
$result = [];
foreach ($object as $key => $value) {
if (array_key_exists($prefix, self::NESTED_SINGLE_OBJECTS_IDS)) {
$pk = self::NESTED_SINGLE_OBJECTS_IDS[$prefix];
$index = '['.$object[$pk].'].'.$key;
}
elseif (array_key_exists($prefix, self::NESTED_OBJECTS_IDS)) {
$pk = self::NESTED_OBJECTS_IDS[$prefix];
$index = '['.$value[$pk].']';
}
else {
$index = '.'.$key;
}
$new_prefix = $prefix.$index;
if (in_array($new_prefix, self::SKIP_FIELDS)) {
continue;
}
if (is_array($value)) {
$result += self::convertKeysToPaths($new_prefix, $value);
}
else {
$result[$new_prefix] = (string) $value;
}
}
return $result;
}
/**
* Checks by path, whether the value is equal to default value from the database schema.
*
* @param int $resource
* @param string $path
* @param string $value
*
* @return bool
*/
private static function isDefaultValue(int $resource, string $path, string $value): bool {
$object_path = self::getLastObjectPath($path);
$table_name = self::TABLE_NAMES[$resource];
if ($object_path !== self::API_NAMES[$resource]) {
if (strpos($object_path, '[') !== false) {
$object_path = preg_replace('/\[[0-9]+\]/', '', $object_path);
}
$table_name = self::NESTED_OBJECTS_TABLE_NAMES[$object_path];
}
$schema_fields = DB::getSchema($table_name)['fields'];
$field_name = substr($path, strrpos($path, '.') + 1);
if (!array_key_exists($field_name, $schema_fields)) {
return false;
}
if (!array_key_exists('default', $schema_fields[$field_name])) {
return false;
}
return $value == $schema_fields[$field_name]['default'];
}
/**
* Checks whether a path is path to nested object property.
*
* @param string $path
*
* @return bool
*/
private static function isNestedObjectProperty(string $path): bool {
return (count(explode('.', $path)) > 2);
}
/**
* Return the path to the parent property object from the passed path.
*
* @param string $path
*
* @return string
*/
private static function getLastObjectPath(string $path): string {
return substr($path, 0, strrpos($path, '.'));
}
/**
* Return the paths to nested object properties from the paths of passing object.
*
* @param array $object
*
* @return array
*/
private static function getNestedObjectsPaths(array $object): array {
$paths = [];
foreach ($object as $path => $foo) {
if (!self::isNestedObjectProperty($path)) {
continue;
}
$object_path = self::getLastObjectPath($path);
if (!in_array($object_path, $paths)) {
$paths[] = $object_path;
}
}
return $paths;
}
/**
* Prepares the audit details for add action.
*
* @param int $resource
* @param array $object
*
* @return array
*/
private static function handleAdd(int $resource, array $object): array {
$result = [];
foreach ($object as $path => $value) {
if (self::isNestedObjectProperty($path)) {
$result[self::getLastObjectPath($path)] = [self::DETAILS_ACTION_ADD];
}
if (self::isDefaultValue($resource, $path, $value)) {
continue;
}
if (self::isValueToMask($resource, $path, $object)) {
$result[$path] = [self::DETAILS_ACTION_ADD, ZBX_SECRET_MASK];
}
else {
$result[$path] = [self::DETAILS_ACTION_ADD, $value];
}
}
return $result;
}
/**
* Prepares the audit details for update action.
*
* @param int $resource
* @param array $object
* @param array $db_object
*
* @return array
*/
private static function handleUpdate(int $resource, array $object, array $db_object): array {
$result = [];
$full_object = $object + $db_object;
$nested_objects_paths = self::getNestedObjectsPaths($object);
$db_nested_objects_paths = self::getNestedObjectsPaths($db_object);
foreach ($db_nested_objects_paths as $path) {
if (!in_array($path, $nested_objects_paths)) {
$result[$path] = [self::DETAILS_ACTION_DELETE];
}
}
foreach ($nested_objects_paths as $path) {
if (!in_array($path, $db_nested_objects_paths)) {
$result[$path] = [self::DETAILS_ACTION_ADD];
}
}
foreach ($object as $path => $foo) {
$value = array_key_exists($path, $object) ? $object[$path] : null;
$db_value = array_key_exists($path, $db_object) ? $db_object[$path] : null;
if ($db_value === null) {
if (self::isDefaultValue($resource, $path, $value)) {
continue;
}
if (self::isValueToMask($resource, $path, $object)) {
$result[$path] = [self::DETAILS_ACTION_ADD, ZBX_SECRET_MASK];
}
else {
$result[$path] = [self::DETAILS_ACTION_ADD, $value];
}
}
elseif ($value != $db_value) {
if (self::isNestedObjectProperty($path)) {
$result[self::getLastObjectPath($path)] = [self::DETAILS_ACTION_UPDATE];
}
if (self::isValueToMask($resource, $path, $full_object)) {
$result[$path] = [self::DETAILS_ACTION_UPDATE, ZBX_SECRET_MASK, ZBX_SECRET_MASK];
}
else {
$result[$path] = [self::DETAILS_ACTION_UPDATE, $value, $db_value];
}
}
}
return $result;
}
}
|