-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsetup.php
More file actions
2664 lines (2327 loc) · 95.6 KB
/
Copy pathsetup.php
File metadata and controls
2664 lines (2327 loc) · 95.6 KB
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| 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. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
/**
* Install the Syslog plugin, registering its hooks, realms and database tables.
*
* @return bool|void True when the install action was queued or completed, false
* when the Syslog config file has not been created yet, and
* nothing when the install is already running in the background.
*/
function plugin_syslog_install() {
global $config, $syslog_upgrade;
static $bg_inprocess = false;
syslog_determine_config();
if (defined('SYSLOG_CONFIG')) {
include(SYSLOG_CONFIG);
} else {
raise_message('syslog_info', __('Please rename either your config.php.dist or config_local.php.dist files in the syslog directory, and change setup your database before installing.', 'syslog'), MESSAGE_LEVEL_ERROR);
return false;
}
syslog_connect();
/** @var string $syslogdb_default The name of the Syslog database, defined in the Syslog config file. */
$syslog_exists = sizeof(syslog_db_fetch_row("SHOW TABLES FROM `$syslogdb_default` LIKE 'syslog'"));
// ================= input validation =================
get_filter_request_var('days');
// ====================================================
api_plugin_register_hook('syslog', 'config_arrays', 'syslog_config_arrays', 'setup.php');
api_plugin_register_hook('syslog', 'draw_navigation_text', 'syslog_draw_navigation_text', 'setup.php');
api_plugin_register_hook('syslog', 'config_settings', 'syslog_config_settings', 'setup.php');
api_plugin_register_hook('syslog', 'settings_bottom', 'syslog_settings_bottom', 'setup.php', 1);
api_plugin_register_hook('syslog', 'top_header_tabs', 'syslog_show_tab', 'setup.php');
api_plugin_register_hook('syslog', 'top_graph_header_tabs', 'syslog_show_tab', 'setup.php');
api_plugin_register_hook('syslog', 'top_graph_refresh', 'syslog_top_graph_refresh', 'setup.php');
api_plugin_register_hook('syslog', 'poller_bottom', 'syslog_poller_bottom', 'setup.php');
api_plugin_register_hook('syslog', 'graph_buttons', 'syslog_graph_buttons', 'setup.php');
api_plugin_register_hook('syslog', 'config_insert', 'syslog_config_insert', 'setup.php');
api_plugin_register_hook('syslog', 'utilities_list', 'syslog_utilities_list', 'setup.php');
api_plugin_register_hook('syslog', 'utilities_action', 'syslog_utilities_action', 'setup.php');
// hook for table replication
api_plugin_register_hook('syslog', 'replicate_out', 'syslog_replicate_out', 'setup.php');
api_plugin_register_realm('syslog', 'syslog.php', 'Syslog User', 1);
api_plugin_register_realm('syslog', 'syslog_alerts.php,syslog_removal.php,syslog_reports.php', 'Rule Viewer', 1);
api_plugin_register_realm('syslog', 'syslog_rule_administrator.php,syslog_device_rules.php', 'Rule Administrator', 1);
api_plugin_register_realm('syslog', 'syslog_saved_searches.php,syslog_dashboards.php', 'Syslog Administration', 1);
api_plugin_register_realm('syslog', 'syslog_saved_searches_share.php', 'Share Saved Templates', 1);
api_plugin_register_realm('syslog', 'syslog_dashboards_share.php', 'Share Dashboards', 1);
api_plugin_register_realm('syslog', 'syslog_administrator.php', 'Syslog Administrator', 1);
if (isset_request_var('install')) {
if (!$bg_inprocess) {
syslog_execute_update($syslog_exists, $_REQUEST);
$bg_inprocess = true;
return true;
}
} elseif (isset($syslog_install_options) && cacti_sizeof($syslog_install_options)) {
// hack for syslog so IBM Spectrum LSF RTM can install syslog without user interaction with preset defaults
if (!$bg_inprocess) {
/** @var array<string, mixed> $syslog_install_options Optional pre-configured install options, defined in the Syslog config file. */
syslog_execute_update($syslog_exists, $syslog_install_options);
$bg_inprocess = true;
}
} elseif (isset_request_var('cancel')) {
header('Location:' . $config['url_path'] . 'plugins.php?mode=uninstall&id=syslog&uninstall&uninstall_method=all');
exit;
} else {
syslog_install_advisor($syslog_exists);
exit;
}
}
/**
* Perform the requested Syslog install or upgrade action.
*
* @param int $syslog_exists Whether the Syslog table already exists.
* @param array<string, mixed> $options The install options, either from the
* request or the Syslog config file.
*
* @return void
*/
function syslog_execute_update(int $syslog_exists, array $options): void {
global $config;
if (isset($options['cancel'])) {
header('Location:' . $config['url_path'] . 'plugins.php?mode=uninstall&id=syslog&uninstall&uninstall_method=all');
exit;
}
if (isset($options['return'])) {
db_execute('DELETE FROM plugin_config WHERE directory="syslog"');
db_execute('DELETE FROM plugin_realms WHERE plugin="syslog"');
db_execute('DELETE FROM plugin_db_changes WHERE plugin="syslog"');
db_execute('DELETE FROM plugin_hooks WHERE name="syslog"');
} elseif (isset($options['upgrade_type'])) {
if ($options['upgrade_type'] == 'truncate') {
syslog_setup_table_new($options);
}
} else {
syslog_setup_table_new($options);
}
set_config_option('syslog_retention', $options['days']);
}
/**
* Uninstall the Syslog plugin, optionally removing its database tables.
*
* @return void
*/
function plugin_syslog_uninstall(): void {
global $config, $syslogdb_default;
syslog_determine_config();
syslog_connect();
if (isset_request_var('cancel') || isset_request_var('return')) {
header('Location:' . $config['url_path'] . 'plugins.php?header=false');
exit;
}
if (isset_request_var('uninstall_method')) {
if (get_nfilter_request_var('uninstall_method') == 'all') {
// do the big tables first
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_removed`");
// do the settings tables last
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_incoming`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_alert`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_remove`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_reports`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_facilities`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_statistics`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_host_facilities`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_priorities`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_logs`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_hosts`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_saved_searches`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_dashboard_panels`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_dashboards`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_dashboards_perm`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_saved_searches_perm`");
} else {
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog`");
syslog_db_execute("DROP TABLE IF EXISTS `$syslogdb_default`.`syslog_removed`");
}
} elseif (function_exists('syslog_uninstall_advisor')) {
syslog_uninstall_advisor();
exit;
}
}
/**
* Check that the Syslog config file has been created before installing.
*
* @return bool True when either config file is present, false otherwise.
*/
function plugin_syslog_check_config(): bool {
// Here we will check to ensure everything is configured
if (!file_exists(__DIR__ . '/config_local.php') && !file_exists(__DIR__ . '/config.php')) {
raise_message('syslog_info', __('Please rename either your config.php.dist or config_local.php.dist files in the syslog directory, and change setup your database before installing.', 'syslog'), MESSAGE_LEVEL_ERROR);
return false;
}
if (api_plugin_installed('flowview')) {
syslog_check_upgrade();
}
return true;
}
/**
* Upgrade the Syslog plugin to the newest version.
*
* @return bool Always false, the upgrade is handled by syslog_check_upgrade().
*/
function plugin_syslog_upgrade(): bool {
// Here we will upgrade to the newest version
api_plugin_register_hook('syslog', 'settings_bottom', 'syslog_settings_bottom', 'setup.php', 1);
syslog_check_upgrade();
return false;
}
/**
* Connect to the Syslog database, either the local Cacti database or a remote one.
*
* @return bool True when a Syslog database connection is available, false otherwise.
*/
function syslog_connect(): bool {
global $config, $syslog_cnn, $syslogdb_default, $local_db_cnn_id, $remote_db_cnn_id, $syslog_incoming_config;
syslog_determine_config();
// Handle remote syslog processing
if (defined('SYSLOG_CONFIG')) {
include(SYSLOG_CONFIG);
}
include_once(__DIR__ . '/functions.php');
include_once(__DIR__ . '/database.php');
$connect_remote = false;
$connected = true;
// Connect to the Syslog Database
if (empty($syslog_cnn)) {
/** @var bool $use_cacti_db Whether the Syslog tables live in the local Cacti database, defined in the Syslog config file. */
if ($config['poller_id'] == 1) {
if ($use_cacti_db == true) {
$syslog_cnn = $local_db_cnn_id;
$connected = true;
} else {
$connect_remote = true;
}
} elseif (isset($config['syslog_remote_db'])) {
if ($use_cacti_db == true) {
$syslog_cnn = $local_db_cnn_id;
$connected = true;
} else {
$connect_remote = true;
}
} else {
if ($use_cacti_db == true) {
$syslog_cnn = $remote_db_cnn_id;
$connected = true;
} else {
$connect_remote = true;
}
}
if ($connect_remote) {
if (!isset($syslogdb_port)) {
$syslogdb_port = '3306';
}
if (!isset($syslogdb_retries)) {
$syslogdb_retries = '5';
}
if (!isset($syslogdb_ssl)) {
$syslogdb_ssl = false;
}
if (!isset($syslogdb_ssl_key)) {
$syslogdb_ssl_key = '';
}
if (!isset($syslogdb_ssl_cert)) {
$syslogdb_ssl_cert = '';
}
if (!isset($syslogdb_ssl_ca)) {
$syslogdb_ssl_ca = '';
}
/**
* @var string $syslogdb_hostname The hostname of the Syslog database server.
* @var string $syslogdb_username The username for the Syslog database server.
* @var string $syslogdb_password The password for the Syslog database server.
* @var string $syslogdb_type The database type of the Syslog database server.
*/
$syslog_cnn = syslog_db_connect_real($syslogdb_hostname, $syslogdb_username, $syslogdb_password, $syslogdb_default, $syslogdb_type, $syslogdb_port, $syslogdb_retries, $syslogdb_ssl, $syslogdb_ssl_key, $syslogdb_ssl_cert, $syslogdb_ssl_ca);
if ($syslog_cnn == false) {
print "FATAL Can not connect\n";
$connected = false;
}
}
if ($connected && !syslog_db_table_exists('syslog') && api_plugin_is_enabled('syslog')) {
cacti_log('Setting Up Database Tables Since they do not exist', false, 'SYSLOG');
if (!isset($syslog_install_options)) {
$syslog_install_options = [];
}
syslog_setup_table_new($syslog_install_options);
}
}
return $connected;
}
/**
* Repair old installs before Cacti's auth.php checks the requested page.
*
* @return void
*/
function syslog_upgrade_saved_search_realm(): void {
global $user_auth_realm_filenames;
$admin = null;
$template = null;
$realms = db_fetch_assoc_prepared('SELECT id, file, display FROM plugin_realms WHERE plugin = ?', ['syslog']);
if (is_array($realms)) {
foreach ($realms as $realm) {
$files = explode(',', $realm['file']);
if (in_array('syslog_alerts.php', $files, true) && ($realm['display'] ?? '') !== 'Rule Viewer') {
$admin = $realm;
}
if (in_array('syslog_saved_searches.php', $files, true)) {
$template = $realm;
}
}
}
if ($admin === null) {
return;
}
if ($template === null) {
// Keep the realm ID: existing user and group grants must not change.
if (!db_execute_prepared('UPDATE plugin_realms SET file = ? WHERE id = ? AND plugin = ?',
[$admin['file'] . ',syslog_saved_searches.php', $admin['id'], 'syslog'])) {
return;
}
$template = $admin;
api_plugin_replicate_config();
}
// A legacy standalone realm retains its grants. Administrators can also
// access Templates, without granting legacy template users other admin pages.
// Update the already-loaded map so the repair works on this request too.
if ($template['id'] == $admin['id'] || api_plugin_user_realm_auth('syslog_alerts.php')) {
$user_auth_realm_filenames['syslog_saved_searches.php'] = (int) $admin['id'] + 100;
}
}
/**
* Give pre-existing installs the Dashboards console page inside the admin realm.
*
* @return void
*/
function syslog_upgrade_dashboard_realm(): void {
global $user_auth_realm_filenames;
$realms = db_fetch_assoc_prepared('SELECT id, file, display FROM plugin_realms WHERE plugin = ?', ['syslog']);
if (is_array($realms)) {
foreach ($realms as $realm) {
$files = explode(',', $realm['file']);
if (!in_array('syslog_alerts.php', $files, true) || ($realm['display'] ?? '') === 'Rule Viewer') {
continue;
}
if (in_array('syslog_dashboards.php', $files, true)) {
return;
}
// Keep the realm ID: existing user and group grants must not change.
if (!db_execute_prepared('UPDATE plugin_realms SET file = ? WHERE id = ? AND plugin = ?',
[$realm['file'] . ',syslog_dashboards.php', $realm['id'], 'syslog'])) {
return;
}
api_plugin_replicate_config();
// Update the already-loaded map so the repair works on this request too.
$user_auth_realm_filenames['syslog_dashboards.php'] = (int) $realm['id'] + 100;
return;
}
}
}
/** Give Rule Administrators access to device alert rules. */
function syslog_upgrade_device_rule_realm(): void {
global $user_auth_realm_filenames;
$realms = db_fetch_assoc_prepared('SELECT id, file, display FROM plugin_realms WHERE plugin = ?', ['syslog']);
if (!is_array($realms)) {
return;
}
foreach ($realms as $realm) {
if (($realm['display'] ?? '') !== 'Rule Administrator') {
continue;
}
$files = explode(',', $realm['file']);
if (!in_array('syslog_device_rules.php', $files, true)) {
$files[] = 'syslog_device_rules.php';
if (!db_execute_prepared('UPDATE plugin_realms SET file = ? WHERE id = ? AND plugin = ?', [implode(',', $files), $realm['id'], 'syslog'])) {
return;
}
api_plugin_replicate_config();
}
$user_auth_realm_filenames['syslog_device_rules.php'] = (int) $realm['id'] + 100;
return;
}
}
/**
* Preserve existing Syslog Administration grants as Rule Administrator grants.
*
* Rule pages must be registered to the Rule Viewer realm so Cacti can admit
* read-only users before page code runs. The administrator realm is a
* permission-only entry that the rule pages check before every mutation.
* Legacy combined realms must not retain Saved Search Templates or Dashboards:
* those pages belong exclusively to the separate Syslog Administration realm.
*
* @return void
*/
function syslog_upgrade_rule_permissions(): void {
global $user_auth_realm_filenames;
$realms = db_fetch_assoc_prepared('SELECT id, file, display FROM plugin_realms WHERE plugin = ?', ['syslog']);
if (!is_array($realms)) {
return;
}
foreach ($realms as $realm) {
$files = explode(',', $realm['file']);
if (($realm['display'] ?? '') !== 'Syslog Administration' || !in_array('syslog_alerts.php', $files, true) || in_array('syslog_rule_administrator.php', $files, true)) {
continue;
}
$files = array_values(array_diff($files, [
'syslog_alerts.php',
'syslog_removal.php',
'syslog_reports.php',
'syslog_saved_searches.php',
'syslog_dashboards.php'
]));
$files[] = 'syslog_rule_administrator.php';
if (!db_execute_prepared('UPDATE plugin_realms SET file = ? WHERE id = ? AND plugin = ?',
[implode(',', $files), $realm['id'], 'syslog'])) {
return;
}
api_plugin_replicate_config();
$user_auth_realm_filenames['syslog_rule_administrator.php'] = (int) $realm['id'] + 100;
return;
}
}
/**
* Merge duplicate granular-rule realms created by earlier registrations.
*
* Cacti renders one permission checkbox per realm record. If an install has
* several records with the same Rule Viewer or Rule Administrator label, the
* permissions screen repeats that label. Preserve their grants in the first
* record and restore canonical filenames so the roles remain separate.
*
* @return bool Whether duplicate repair succeeded.
*/
function syslog_upgrade_consolidate_rule_realms(): bool {
$changed = false;
foreach (['Rule Viewer', 'Rule Administrator'] as $display) {
$realms = db_fetch_assoc_prepared('SELECT id, file FROM plugin_realms WHERE plugin = ? AND display = ? ORDER BY id', ['syslog', $display]);
$files = $display === 'Rule Viewer' ? 'syslog_alerts.php,syslog_removal.php,syslog_reports.php' : 'syslog_rule_administrator.php';
if (!is_array($realms) || !$realms || (count($realms) === 1 && $realms[0]['file'] === $files)) {
continue;
}
$keeper = array_shift($realms);
if (!db_begin_transaction()) {
return false;
}
foreach ($realms as $realm) {
foreach (['user_auth_realm' => 'user_id', 'user_auth_group_realm' => 'group_id'] as $table => $owner) {
if (!db_execute_prepared("INSERT IGNORE INTO $table ($owner, realm_id) SELECT $owner, ? FROM $table WHERE realm_id = ?", [(int) $keeper['id'] + 100, (int) $realm['id'] + 100]) ||
!db_execute_prepared("DELETE FROM $table WHERE realm_id = ?", [(int) $realm['id'] + 100])) {
db_rollback_transaction();
return false;
}
}
if (!db_execute_prepared('DELETE FROM plugin_realms WHERE id = ? AND plugin = ?', [(int) $realm['id'], 'syslog'])) {
db_rollback_transaction();
return false;
}
}
// Broken migrations put the administrator filename in Viewer records.
// Never union those files: doing so merges the two permission levels.
if (!db_execute_prepared('UPDATE plugin_realms SET file = ? WHERE id = ?', [$files, (int) $keeper['id']]) || !db_commit_transaction()) {
db_rollback_transaction();
return false;
}
$changed = true;
}
if ($changed) {
api_plugin_replicate_config();
}
return true;
}
/** Refresh permission labels, filenames and grouping from current realm IDs. */
function syslog_refresh_permission_roles(): void {
global $user_auth_realms, $user_auth_realm_filenames, $user_auth_roles;
$realms = db_fetch_assoc_prepared('SELECT id, file, display FROM plugin_realms WHERE plugin = ? ORDER BY id', ['syslog']);
if (!is_array($realms)) {
return;
}
$ids = [];
foreach ($realms as $realm) {
$id = (int) $realm['id'] + 100;
$ids[] = $id;
$user_auth_realms[$id] = $realm['display'];
foreach (explode(',', $realm['file']) as $file) {
$user_auth_realm_filenames[$file] = $id;
unset($_SESSION['sess_auth_names'][$file]);
}
}
// Assign actual IDs rather than cached filename lookups. This also removes
// deleted duplicate IDs from the Syslog group on the current request.
$user_auth_roles[__('Syslog', 'syslog')] = array_values(array_unique($ids));
// Rule Administrator is the write permission paired with Rule Viewer. Map
// the rule page filenames to the administrator realm for an administrator
// who does not also have the viewer realm, so Cacti can render the Syslog
// Settings menu and admit the rule pages. Do not include the separate
// saved-search or dashboard administration pages here.
$rule_administrator = $user_auth_realm_filenames['syslog_rule_administrator.php'] ?? 0;
if ($rule_administrator && is_realm_allowed($rule_administrator)) {
foreach (['syslog_alerts.php', 'syslog_removal.php', 'syslog_reports.php'] as $file) {
$user_auth_realm_filenames[$file] = $rule_administrator;
}
}
// Cacti filters console menus and authorizes pages from this map before the
// page controllers run. Keep the stored permissions independent, but make
// the aggregate administrator permission imply each Syslog capability for
// the current user.
$administrator = $user_auth_realm_filenames['syslog_administrator.php'] ?? 0;
if ($administrator && is_realm_allowed($administrator)) {
foreach ([
'syslog.php',
'syslog_alerts.php',
'syslog_removal.php',
'syslog_reports.php',
'syslog_rule_administrator.php',
'syslog_saved_searches.php',
'syslog_saved_searches_share.php',
'syslog_dashboards.php',
'syslog_dashboards_share.php'
] as $file) {
$user_auth_realm_filenames[$file] = $administrator;
}
}
}
/**
* Upgrade the Syslog database schema for legacy installs.
*
* @return void
*/
function syslog_check_upgrade(): void {
global $config, $syslogdb_default, $syslog_levels, $syslog_upgrade;
syslog_connect();
syslog_upgrade_saved_search_realm();
syslog_upgrade_dashboard_realm();
syslog_upgrade_rule_permissions();
if (!syslog_upgrade_consolidate_rule_realms()) {
return;
}
// Keep newly introduced permission realms available for existing installs.
api_plugin_register_realm('syslog', 'syslog_alerts.php,syslog_removal.php,syslog_reports.php', 'Rule Viewer', 0);
api_plugin_register_realm('syslog', 'syslog_rule_administrator.php,syslog_device_rules.php', 'Rule Administrator', 0);
api_plugin_register_realm('syslog', 'syslog_saved_searches.php,syslog_dashboards.php', 'Syslog Administration', 0);
api_plugin_register_realm('syslog', 'syslog_saved_searches_share.php', 'Share Saved Templates', 0);
api_plugin_register_realm('syslog', 'syslog_dashboards_share.php', 'Share Dashboards', 0);
api_plugin_register_realm('syslog', 'syslog_administrator.php', 'Syslog Administrator', 0);
syslog_upgrade_device_rule_realm();
syslog_refresh_permission_roles();
// Let's only run this check if we are on a page that actually needs the data
$files = ['plugins.php', 'syslog.php', 'syslog_removal.php', 'syslog_alerts.php', 'syslog_device_rules.php', 'syslog_reports.php', 'syslog_saved_searches.php', 'syslog_dashboards.php'];
if (substr($_SERVER['SCRIPT_FILENAME'], -18) != 'syslog_process.php' && !in_array(get_current_page(), $files, true)) {
return;
}
// don't let this script timeout
ini_set('max_execution_time', 0);
if (function_exists('api_plugin_upgrade_register')) {
if (!api_plugin_upgrade_register('syslog')) {
// No upgrade required, but still warn about deprecated table layouts
syslog_notice_traditional_tables(true);
return;
}
} else {
$version = plugin_syslog_version();
$current = $version['version'];
$old = db_fetch_cell("SELECT version FROM plugin_config WHERE directory='syslog'");
if ($current != $old) {
api_plugin_register_hook('syslog', 'replicate_out', 'syslog_replicate_out', 'setup.php', 1);
db_execute_prepared('UPDATE plugin_config SET
version = ?, name = ?, author = ?, webpage = ?
WHERE directory = ?',
[
$version['version'],
$version['longname'],
$version['author'],
$version['homepage'],
$version['name']
]
);
} else {
// No upgrade required, but still warn about deprecated table layouts
syslog_notice_traditional_tables(true);
return;
}
}
if (!syslog_db_column_exists('syslog_alert', 'hash')) {
syslog_db_add_column('syslog_alert', [
'name' => 'hash',
'type' => 'varchar(32)',
'NULL' => false,
'default' => '',
'after' => 'id']
);
syslog_db_add_column('syslog_remove', [
'name' => 'hash',
'type' => 'varchar(32)',
'NULL' => false,
'default' => '',
'after' => 'id']
);
syslog_db_add_column('syslog_reports', [
'name' => 'hash',
'type' => 'varchar(32)',
'NULL' => false,
'default' => '',
'after' => 'id']
);
}
if (syslog_db_column_exists('syslog_incoming', 'date')) {
syslog_db_execute("ALTER TABLE syslog_incoming
DROP COLUMN date,
CHANGE COLUMN `time` logtime timestamp default '0000-00-00';");
}
if (syslog_db_column_exists('syslog_alert', 'hash')) {
$alerts = syslog_db_fetch_assoc('SELECT *
FROM syslog_alert
WHERE hash IS NULL OR hash = ""');
if (cacti_sizeof($alerts)) {
foreach ($alerts as $a) {
$hash = get_hash_syslog($a['id'], 'syslog_alert');
syslog_db_execute_prepared('UPDATE syslog_alert
SET hash = ?
WHERE id = ?',
[$hash, $a['id']]);
}
}
}
if (syslog_db_column_exists('syslog_remove', 'hash')) {
$removes = syslog_db_fetch_assoc('SELECT *
FROM syslog_remove
WHERE hash IS NULL OR hash = ""');
if (cacti_sizeof($removes)) {
foreach ($removes as $r) {
$hash = get_hash_syslog($r['id'], 'syslog_remove');
syslog_db_execute_prepared('UPDATE syslog_remove
SET hash = ?
WHERE id = ?',
[$hash, $r['id']]);
}
}
}
if (syslog_db_column_exists('syslog_reports', 'hash')) {
$reports = syslog_db_fetch_assoc('SELECT *
FROM syslog_reports
WHERE hash IS NULL OR hash = ""');
if (cacti_sizeof($reports)) {
foreach ($reports as $r) {
$hash = get_hash_syslog($r['id'], 'syslog_reports');
syslog_db_execute_prepared('UPDATE syslog_reports
SET hash = ?
WHERE id = ?',
[$hash, $r['id']]);
}
}
}
if (syslog_db_column_exists('syslog_saved_searches', 'hash')) {
$searches = syslog_db_fetch_assoc('SELECT *
FROM syslog_saved_searches
WHERE hash IS NULL OR hash = ""');
if (cacti_sizeof($searches)) {
foreach ($searches as $s) {
$hash = get_hash_syslog($s['id'], 'syslog_saved_searches');
syslog_db_execute_prepared('UPDATE syslog_saved_searches
SET hash = ?
WHERE id = ?',
[$hash, $s['id']]);
}
}
}
if (syslog_db_column_exists('syslog_dashboards', 'hash')) {
$dashboards = syslog_db_fetch_assoc('SELECT *
FROM syslog_dashboards
WHERE hash IS NULL OR hash = ""');
if (cacti_sizeof($dashboards)) {
foreach ($dashboards as $d) {
$hash = get_hash_syslog($d['id'], 'syslog_dashboards');
syslog_db_execute_prepared('UPDATE syslog_dashboards
SET hash = ?
WHERE id = ?',
[$hash, $d['id']]);
}
}
}
if (!syslog_db_column_exists('syslog_alert', 'level')) {
syslog_db_add_column('syslog_alert', [
'name' => 'level',
'type' => 'int(10)',
'unsigned' => true,
'NULL' => false,
'default' => '0',
'after' => 'method']
);
}
if (!syslog_db_column_exists('syslog_alert', 'notify')) {
syslog_db_add_column('syslog_alert', [
'name' => 'notify',
'type' => 'int(10)',
'unsigned' => true,
'NULL' => false,
'default' => '0',
'after' => 'email']
);
}
if (!syslog_db_column_exists('syslog_alert', 'body')) {
syslog_db_add_column('syslog_alert', [
'name' => 'body',
'type' => 'varchar(8192)',
'NULL' => false,
'default' => '',
'after' => 'message']
);
}
foreach ([
['cooldown_minutes', 'int(10)', '-1', 'repeat_alert'],
['deduplication_minutes', 'int(10)', '-1', 'cooldown_minutes'],
['suppression_schedule', 'text', '', 'deduplication_minutes'],
['maintenance_mode', 'varchar(16)', 'inherit', 'suppression_schedule'],
['maintenance_days', 'varchar(32)', '1,2,3,4,5', 'maintenance_mode'],
['maintenance_start', 'char(5)', '00:00', 'maintenance_days'],
['maintenance_end', 'char(5)', '00:00', 'maintenance_start'],
['maintenance_datetime_start', 'varchar(16)', '', 'maintenance_end'],
['maintenance_datetime_end', 'varchar(16)', '', 'maintenance_datetime_start']
] as [$name, $type, $default, $after]) {
if (!syslog_db_column_exists('syslog_alert', $name)) {
syslog_db_add_column('syslog_alert', [
'name' => $name,
'type' => $type,
'NULL' => false,
'default' => $default,
'after' => $after]
);
}
}
syslog_db_execute("CREATE TABLE IF NOT EXISTS `$syslogdb_default`.`syslog_alert_suppression` (
`alert_id` int(10) unsigned NOT NULL,
`scope_key` varchar(255) NOT NULL,
`dedup_hash` char(40) NOT NULL,
`last_sent` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`alert_id`, `scope_key`, `dedup_hash`),
INDEX `last_sent` (`last_sent`))
ENGINE=InnoDB");
// Structured filter JSON can exceed the old VARCHAR limit. TEXT also keeps
// the alert table below the InnoDB/MariaDB inline row-size ceiling.
syslog_db_execute('ALTER TABLE syslog_alert MODIFY column message TEXT NOT NULL');
// Removal rules now store the same structured filter JSON as alert rules.
syslog_db_execute('ALTER TABLE syslog_remove MODIFY column message TEXT NOT NULL');
if (!syslog_db_column_exists('syslog_reports', 'notify')) {
syslog_db_add_column('syslog_reports', [
'name' => 'notify',
'type' => 'int(10)',
'unsigned' => true,
'NULL' => false,
'default' => '0',
'after' => 'email']
);
}
syslog_db_execute('ALTER TABLE syslog_reports MODIFY column body VARCHAR(8192) NOT NULL default ""');
if (!syslog_db_table_exists('syslog_saved_searches', false)) {
syslog_db_execute("CREATE TABLE IF NOT EXISTS `$syslogdb_default`.`syslog_saved_searches` (
`id` int(10) NOT NULL auto_increment,
`hash` varchar(32) NOT NULL default '',
`name` varchar(128) NOT NULL default '',
`search` text NOT NULL,
`removal` int(10) NOT NULL default '1',
`grouping` int(10) NOT NULL default '0',
`user` varchar(32) NOT NULL default '',
`is_global` char(2) NOT NULL default '',
`date` int(16) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY owner (`user`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic");
}
if (!syslog_db_column_exists('syslog_saved_searches', 'hash')) {
syslog_db_add_column('syslog_saved_searches', [
'name' => 'hash',
'type' => 'varchar(32)',
'NULL' => false,
'default' => '',
'after' => 'id']
);
}
if (!syslog_db_table_exists('syslog_dashboards', false)) {
syslog_db_execute("CREATE TABLE IF NOT EXISTS `$syslogdb_default`.`syslog_dashboards` (
`id` int(10) NOT NULL auto_increment,
`hash` varchar(32) NOT NULL default '',
`name` varchar(128) NOT NULL default '',
`user` varchar(32) NOT NULL default '',
`is_global` char(2) NOT NULL default '',
`date` int(16) NOT NULL default '0',
`updated` int(16) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY owner (`user`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic");
}
if (!syslog_db_column_exists('syslog_dashboards', 'hash')) {
syslog_db_add_column('syslog_dashboards', [
'name' => 'hash',
'type' => 'varchar(32)',
'NULL' => false,
'default' => '',
'after' => 'id']
);
}
if (!syslog_db_table_exists('syslog_dashboard_panels', false)) {
syslog_db_execute("CREATE TABLE IF NOT EXISTS `$syslogdb_default`.`syslog_dashboard_panels` (
`id` int(10) NOT NULL auto_increment,
`dashboard_id` int(10) NOT NULL default '0',
`title` varchar(128) NOT NULL default '',
`expression` text NOT NULL,
`source` varchar(16) NOT NULL default 'syslog',
`removal` int(10) NOT NULL default '1',
`kind` varchar(16) NOT NULL default 'timeseries',
`chart` varchar(16) NOT NULL default 'line',
`field` varchar(16) NOT NULL default 'host',
`interval` varchar(16) NOT NULL default 'dashboard',
`timespan` varchar(16) NOT NULL default 'dashboard',
`top_n` int(10) NOT NULL default '10',
`width` smallint(5) unsigned NOT NULL default '1',
`height` smallint(5) unsigned NOT NULL default '0',
`position` int(10) NOT NULL default '0',
`date` int(16) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY dashboard (`dashboard_id`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic");
}
// Panel resizing persists a grid span and chart height per panel.
if (!syslog_db_column_exists('syslog_dashboard_panels', 'width')) {
syslog_db_add_column('syslog_dashboard_panels', [
'name' => 'width',
'type' => 'smallint(5) unsigned',
'NULL' => false,
'default' => '1',
'after' => 'top_n']
);
}
if (!syslog_db_column_exists('syslog_dashboard_panels', 'height')) {
syslog_db_add_column('syslog_dashboard_panels', [
'name' => 'height',
'type' => 'smallint(5) unsigned',
'NULL' => false,
'default' => '0',
'after' => 'width']
);
}
// Sharing persists a global flag on the dashboard itself.
if (!syslog_db_column_exists('syslog_dashboards', 'is_global')) {
syslog_db_add_column('syslog_dashboards', [
'name' => 'is_global',
'type' => 'char(2)',
'NULL' => false,
'default' => '',
'after' => 'user']
);
}
// Per-user and per-group grants from the admin Dashboards page, beyond
// the owner and the global share flag.
if (!syslog_db_table_exists('syslog_dashboards_perm', false)) {
syslog_db_execute("CREATE TABLE IF NOT EXISTS `$syslogdb_default`.`syslog_dashboards_perm` (
`dashboard_id` int(10) NOT NULL default '0',
`type` varchar(5) NOT NULL default '',
`item_id` int(10) NOT NULL default '0',
PRIMARY KEY (`dashboard_id`, `type`, `item_id`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic");
}
// The same grants for saved searches.
if (!syslog_db_table_exists('syslog_saved_searches_perm', false)) {
syslog_db_execute("CREATE TABLE IF NOT EXISTS `$syslogdb_default`.`syslog_saved_searches_perm` (
`search_id` int(10) NOT NULL default '0',
`type` varchar(5) NOT NULL default '',
`item_id` int(10) NOT NULL default '0',
PRIMARY KEY (`search_id`, `type`, `item_id`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic");
}
if (!syslog_db_table_exists('syslog_status', false)) {
syslog_db_execute("CREATE TABLE IF NOT EXISTS `$syslogdb_default`.`syslog_status` (
`name` varchar(64) NOT NULL default '',
`value` text NOT NULL,
`updated` int(16) NOT NULL default '0',
PRIMARY KEY (`name`))
ENGINE=InnoDB
ROW_FORMAT=Dynamic");
} else {
syslog_db_execute("ALTER TABLE `$syslogdb_default`.`syslog_status` MODIFY column `value` TEXT NOT NULL");
}
}
/**
* Create the partitioned Syslog table, including past and future partitions.
*
* @param string $engine The storage engine to create the table with.
* @param float|int|string $days The number of days of retention to provision.
* @param int|string|false $ahead_days The number of days of partitions to create
* ahead of today, or false when unconfigured.
*
* @return void
*/
function syslog_create_partitioned_syslog_table($engine = 'InnoDB', $days = 30, $ahead_days = 3) {
global $config, $syslogdb_default, $syslog_levels;