Repository navigation
Expand file tree
/
Copy pathpfs_admin.php
More file actions
303 lines (252 loc) · 11.3 KB
/
Copy pathpfs_admin.php
File metadata and controls
303 lines (252 loc) · 11.3 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
<?php
/*
* SPDX-FileCopyrightText: 2026 Thomas Vincent <thomasvincent@gmail.com>
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of plugin_pfs, a plugin for Cacti.
* https://github.com/somethingwithproof/plugin_pfs
*
* 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.
*/
/*
* Parallel Filesystem Registry (Parallel Filesystem Admin realm). This file
* must parse on PHP 8.0: Cacti may include it on an unsupported runtime, and
* pfs_page_start() refuses there before any plugin class loads. Request
* values are read through Cacti's request helpers and validated by the
* input classes before anything is stored (REQ-UI-002). Nothing here
* contacts a node.
*/
chdir('../../');
include('./include/auth.php');
include_once('./plugins/pfs/setup.php');
if (!pfs_page_start()) {
exit;
}
pfs_require_post('pfs_admin.php', array('save_fs', 'delete_fs', 'save_node', 'delete_node', 'add_network', 'delete_network', 'accept_target', 'ignore_target', 'save_device', 'unmap_device'));
$pfs_clock = new \Somethingwithproof\Pfs\SystemClock();
$pfs_backends = \Somethingwithproof\Pfs\BackendRegistry::production();
$pfs_filesystems = new \Somethingwithproof\Pfs\FilesystemRepository(false, $pfs_clock);
$pfs_nodes = new \Somethingwithproof\Pfs\NodeRepository(false, $pfs_clock);
$pfs_networks = new \Somethingwithproof\Pfs\NetworkRepository(false, $pfs_clock);
$pfs_targets = new \Somethingwithproof\Pfs\TargetRepository(false);
$pfs_pollers = new \Somethingwithproof\Pfs\PollerRepository(false);
$pfs_objects = new \Somethingwithproof\Pfs\CactiObjectRepository(false);
$pfs_provisioner = pfs_provisioner();
$pfs_access = new \Somethingwithproof\Pfs\DeviceAccess(function ($host_id) {
return is_device_allowed($host_id);
});
$pfs_page = new \Somethingwithproof\Pfs\AdminPage($pfs_filesystems, $pfs_nodes, $pfs_networks, $pfs_targets, $pfs_backends, $pfs_pollers, $pfs_provisioner, $pfs_objects, $pfs_access);
$pfs_action = (string) get_nfilter_request_var('action');
/**
* Refuse an action on a record whose Cacti device this user may not see.
* Every action that loads an existing record calls this before it changes
* anything (DeviceAccess).
*
* @param mixed $allowed
*/
function pfs_admin_require($allowed) {
if ($allowed !== true) {
raise_message('pfs_denied', __('That record belongs to a Cacti device you do not have permission for. Nothing was changed.', 'pfs'), MESSAGE_LEVEL_ERROR);
header('Location: pfs_admin.php');
exit;
}
}
/**
* The filesystem of a node or target, for the device check.
*
* @param mixed $record
* @return mixed
*/
function pfs_admin_parent($record) {
global $pfs_filesystems;
return is_object($record) && isset($record->filesystemId) ? $pfs_filesystems->find((int) $record->filesystemId) : null;
}
/**
* @param list<string> $fields
* @return array<string, string>
*/
function pfs_request_fields(array $fields): array {
$input = array();
foreach ($fields as $field) {
$input[$field] = (string) get_nfilter_request_var($field);
}
return $input;
}
switch ($pfs_action) {
case 'save_fs':
$pfs_id = (int) get_filter_request_var('id');
$pfs_input = pfs_request_fields(array('name', 'backend', 'poller_id', 'expected_interval', 'enabled'));
$pfs_existing = $pfs_id > 0 ? $pfs_filesystems->find($pfs_id) : null;
if ($pfs_id > 0 && $pfs_existing === null) {
header('Location: pfs_admin.php');
exit;
}
if ($pfs_existing !== null) {
pfs_admin_require($pfs_access->filesystem($pfs_existing));
}
$pfs_parsed = \Somethingwithproof\Pfs\FilesystemInput::parse($pfs_input, $pfs_id, $pfs_backends, $pfs_pollers->names());
$pfs_errors = $pfs_parsed->errors;
if ($pfs_parsed->filesystem !== null && $pfs_filesystems->nameTaken($pfs_parsed->filesystem->backend, $pfs_parsed->filesystem->name, $pfs_id)) {
$pfs_errors['name'] = __('Name: another filesystem of this backend already uses this name.', 'pfs');
}
if ($pfs_errors !== array() || $pfs_parsed->filesystem === null) {
top_header();
$pfs_page->renderFilesystemEdit($pfs_existing, $pfs_input, $pfs_errors);
bottom_footer();
exit;
}
$pfs_saved = $pfs_filesystems->save($pfs_parsed->filesystem) > 0;
raise_message('pfs_saved', $pfs_saved ? __('The filesystem was saved.', 'pfs') : __('The filesystem could not be saved.', 'pfs'), $pfs_saved ? MESSAGE_LEVEL_INFO : MESSAGE_LEVEL_ERROR);
header('Location: pfs_admin.php');
exit;
case 'delete_fs':
$pfs_fs = $pfs_filesystems->find((int) get_filter_request_var('id'));
if ($pfs_fs === null) {
header('Location: pfs_admin.php');
exit;
}
pfs_admin_require($pfs_access->filesystem($pfs_fs));
if ($pfs_filesystems->delete($pfs_fs->id)) {
raise_message('pfs_deleted', __('The filesystem, its nodes, and its inventory were deleted. Its Cacti device, data sources, and graphs were kept. No node was contacted.', 'pfs'), MESSAGE_LEVEL_INFO);
}
header('Location: pfs_admin.php');
exit;
case 'save_node':
$pfs_id = (int) get_filter_request_var('id');
$pfs_input = pfs_request_fields(array('filesystem_id', 'name', 'role', 'poller_id', 'endpoint', 'port', 'ssh_user', 'enabled'));
$pfs_existing = $pfs_id > 0 ? $pfs_nodes->find($pfs_id) : null;
if ($pfs_id > 0 && $pfs_existing === null) {
header('Location: pfs_admin.php');
exit;
}
if ($pfs_existing !== null) {
pfs_admin_require($pfs_access->filesystem(pfs_admin_parent($pfs_existing)));
}
// A node can be saved only into a filesystem this user may see.
$pfs_fs_map = array();
foreach ($pfs_access->filesystems($pfs_filesystems->all()) as $pfs_fs) {
$pfs_fs_map[$pfs_fs->id] = $pfs_fs;
}
$pfs_parsed = \Somethingwithproof\Pfs\NodeInput::parse($pfs_input, $pfs_id, $pfs_fs_map, $pfs_pollers->names(), new \Somethingwithproof\Pfs\SystemResolver(), $pfs_networks->policy());
$pfs_errors = $pfs_parsed->errors;
if ($pfs_parsed->node !== null && $pfs_nodes->nameTaken($pfs_parsed->node->name, $pfs_id)) {
$pfs_errors['name'] = __('Name: another node already uses this name.', 'pfs');
}
if ($pfs_errors !== array() || $pfs_parsed->node === null) {
top_header();
$pfs_page->renderNodeEdit($pfs_existing, $pfs_input, $pfs_errors);
bottom_footer();
exit;
}
$pfs_saved = $pfs_nodes->save($pfs_parsed->node) > 0;
raise_message('pfs_saved', $pfs_saved ? __('The node was saved.', 'pfs') : __('The node could not be saved.', 'pfs'), $pfs_saved ? MESSAGE_LEVEL_INFO : MESSAGE_LEVEL_ERROR);
header('Location: pfs_admin.php');
exit;
case 'delete_node':
$pfs_node = $pfs_nodes->find((int) get_filter_request_var('id'));
if ($pfs_node === null) {
header('Location: pfs_admin.php');
exit;
}
pfs_admin_require($pfs_access->filesystem(pfs_admin_parent($pfs_node)));
if ($pfs_nodes->delete($pfs_node->id)) {
raise_message('pfs_deleted', __('The node was deleted. Targets it reported keep their history.', 'pfs'), MESSAGE_LEVEL_INFO);
}
header('Location: pfs_admin.php');
exit;
case 'add_network':
if (!$pfs_networks->add((int) get_filter_request_var('poller_id'), (string) get_nfilter_request_var('cidr'))) {
raise_message('pfs_network', __('Enter a network as an address and prefix length, such as 10.20.0.0/16.', 'pfs'), MESSAGE_LEVEL_ERROR);
}
header('Location: pfs_admin.php');
exit;
case 'delete_network':
$pfs_networks->delete((int) get_filter_request_var('id'));
header('Location: pfs_admin.php');
exit;
case 'accept_target':
case 'ignore_target':
$pfs_target = $pfs_targets->find((int) get_filter_request_var('id'));
if ($pfs_target === null) {
header('Location: pfs_admin.php');
exit;
}
pfs_admin_require($pfs_access->filesystem(pfs_admin_parent($pfs_target)));
$pfs_targets->review($pfs_target->id, $pfs_action === 'accept_target' ? \Somethingwithproof\Pfs\Review::Accepted : \Somethingwithproof\Pfs\Review::Ignored);
header('Location: pfs_admin.php');
exit;
case 'device':
$pfs_fs = $pfs_filesystems->find((int) get_filter_request_var('id'));
if ($pfs_fs === null || $pfs_fs->hostId > 0) {
header('Location: pfs_admin.php');
exit;
}
pfs_admin_require($pfs_access->filesystem($pfs_fs));
top_header();
$pfs_page->renderDeviceConfirm($pfs_fs, (int) get_filter_request_var('host_id'));
bottom_footer();
exit;
case 'save_device':
$pfs_fs = $pfs_filesystems->find((int) get_filter_request_var('id'));
$pfs_host = (int) get_filter_request_var('host_id');
pfs_admin_require($pfs_fs !== null && $pfs_access->filesystem($pfs_fs) && ($pfs_host === 0 || $pfs_access->device($pfs_host)));
if ($pfs_fs === null || $pfs_fs->hostId > 0 || $pfs_provisioner->refusal() !== null) {
raise_message('pfs_device', __('No device was added: the filesystem already has one, or the templates are not installed.', 'pfs'), MESSAGE_LEVEL_ERROR);
} elseif ($pfs_host > 0) {
$pfs_ok = $pfs_provisioner->map($pfs_fs, $pfs_host);
raise_message('pfs_device', $pfs_ok ? __('The device was mapped and the data queries and overview graphs were added.', 'pfs') : __('The device could not be mapped. It may be mapped to another filesystem.', 'pfs'), $pfs_ok ? MESSAGE_LEVEL_INFO : MESSAGE_LEVEL_ERROR);
} else {
$pfs_ok = $pfs_provisioner->create($pfs_fs) > 0;
raise_message('pfs_device', $pfs_ok ? __('The device, its data queries, and its overview graphs were created.', 'pfs') : __('Cacti did not create the device. See the Cacti log.', 'pfs'), $pfs_ok ? MESSAGE_LEVEL_INFO : MESSAGE_LEVEL_ERROR);
}
header('Location: pfs_admin.php');
exit;
case 'unmap_device':
$pfs_fs = $pfs_filesystems->find((int) get_filter_request_var('id'));
if ($pfs_fs !== null && $pfs_fs->hostId > 0) {
pfs_admin_require($pfs_access->filesystem($pfs_fs));
$pfs_filesystems->unmapHosts(array($pfs_fs->hostId));
raise_message('pfs_device', __('The device is no longer mapped. It and its graphs were kept; its data queries list no targets until it is mapped again.', 'pfs'), MESSAGE_LEVEL_INFO);
}
header('Location: pfs_admin.php');
exit;
case 'edit_fs':
$pfs_id = (int) get_filter_request_var('id');
$pfs_fs = $pfs_id > 0 ? $pfs_filesystems->find($pfs_id) : null;
if ($pfs_fs !== null) {
pfs_admin_require($pfs_access->filesystem($pfs_fs));
}
top_header();
$pfs_page->renderFilesystemEdit($pfs_fs);
bottom_footer();
exit;
case 'edit_node':
$pfs_id = (int) get_filter_request_var('id');
$pfs_node = $pfs_id > 0 ? $pfs_nodes->find($pfs_id) : null;
if ($pfs_node !== null) {
pfs_admin_require($pfs_access->filesystem(pfs_admin_parent($pfs_node)));
}
top_header();
$pfs_page->renderNodeEdit($pfs_node);
bottom_footer();
exit;
case 'export':
$pfs_info = plugin_pfs_version();
header('Content-Type: application/json; charset=utf-8');
header('Content-Disposition: attachment; filename="pfs-registry.json"');
header('X-Content-Type-Options: nosniff');
print json_encode($pfs_page->export($pfs_info['version'] ?? '', $pfs_clock->now()), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
exit;
default:
top_header();
$pfs_page->renderList();
bottom_footer();
}