Repository navigation
Expand file tree
/
Copy pathfabric_admin.php
More file actions
286 lines (234 loc) · 11.5 KB
/
Copy pathfabric_admin.php
File metadata and controls
286 lines (234 loc) · 11.5 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
<?php
/*
* SPDX-FileCopyrightText: 2026 Thomas Vincent <thomasvincent@gmail.com>
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of plugin_fabric, a plugin for Cacti.
* https://github.com/somethingwithproof/plugin_fabric
*
* 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.
*/
/*
* Fabric Admin (Fabric Admin realm). This file must parse on PHP 8.0: Cacti
* may include it on an unsupported runtime, and fabric_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 runs a tool.
*
* The realm alone is not enough to change a record tied to a Cacti
* device: every action on an existing node, proposal, or fabric checks
* the user's device permissions on that record before any write, and a
* new mapping needs the target device too (DeviceAccess).
*/
chdir('../../');
include('./include/auth.php');
include_once('./plugins/fabric/setup.php');
if (!fabric_page_start()) {
exit;
}
fabric_require_post('fabric_admin.php', array('save_fabric', 'delete_fabric_confirm', 'accept_conflict', 'confirm_relink', 'reject_relink', 'save_node', 'create_device_confirm'));
$fabric_clock = new \Somethingwithproof\Fabric\SystemClock();
$fabric_fabrics = new \Somethingwithproof\Fabric\FabricRepository(false, $fabric_clock);
$fabric_nodes = new \Somethingwithproof\Fabric\NodeRepository(false);
$fabric_relinks = new \Somethingwithproof\Fabric\RelinkRepository(false, $fabric_clock);
$fabric_pollers = new \Somethingwithproof\Fabric\PollerRepository(false);
$fabric_hosts = new \Somethingwithproof\Fabric\HostRepository(false);
// A mapping to a device that was deleted protects no device data, and
// is_device_allowed() refuses an id the host table no longer has, which
// would leave the node uneditable for everyone.
$fabric_access = new \Somethingwithproof\Fabric\DeviceAccess(static function (int $host) use ($fabric_hosts): bool {
return fabric_device_allowed($host) || $fabric_hosts->gone($host);
});
$fabric_objects = new \Somethingwithproof\Fabric\CactiObjectRepository(false);
$fabric_devices = new \Somethingwithproof\Fabric\DeviceProvisioner($fabric_nodes, $fabric_fabrics, $fabric_objects, $fabric_access, $config['base_path']);
// Creating a device needs Cacti's own device management permission, not
// only Fabric Admin: the realm grants plugin records, not core objects.
$fabric_create = (bool) api_user_realm_auth('host.php');
$fabric_page = new \Somethingwithproof\Fabric\AdminPage($fabric_fabrics, $fabric_nodes, $fabric_relinks, $fabric_pollers, $fabric_hosts, $fabric_access, $fabric_devices, $fabric_objects, $fabric_create);
$fabric_action = (string) get_nfilter_request_var('action');
/**
* @param list<string> $fields
* @return array<string, string>
*/
function fabric_request_fields(array $fields): array {
$input = array();
foreach ($fields as $field) {
$input[$field] = (string) get_nfilter_request_var($field);
}
return $input;
}
/**
* The node named by the request, only if it is unchanged since the form
* that posted it was drawn.
*/
function fabric_posted_node(\Somethingwithproof\Fabric\NodeRepository $nodes): ?\Somethingwithproof\Fabric\Node {
$node = $nodes->find((int) get_filter_request_var('id'));
if ($node === null || $node->revision !== (int) get_filter_request_var('revision')) {
raise_message('fabric_stale', __('The node changed after the page was drawn, so nothing was done. Review it and try again.', 'fabric'), MESSAGE_LEVEL_ERROR);
return null;
}
return $node;
}
function fabric_denied(): void {
raise_message('fabric_denied', __('Your device permissions do not allow changing this record.', 'fabric'), MESSAGE_LEVEL_ERROR);
header('Location: fabric_admin.php');
exit;
}
/**
* Stop unless the user may see every device mapped in the fabric.
*/
function fabric_require_fabric_access(\Somethingwithproof\Fabric\DeviceAccess $access, \Somethingwithproof\Fabric\NodeRepository $nodes, int $fabricId): void {
$all = $nodes->forFabric($fabricId);
if ($all === null || !$access->fabric($fabricId, $all)) {
fabric_denied();
}
}
function fabric_done(bool $ok, string $done, string $failed): void {
raise_message('fabric_result', $ok ? $done : $failed, $ok ? MESSAGE_LEVEL_INFO : MESSAGE_LEVEL_ERROR);
header('Location: fabric_admin.php');
exit;
}
switch ($fabric_action) {
case 'save_fabric':
$fabric_id = (int) get_filter_request_var('id');
$fabric_input = fabric_request_fields(array('name', 'backend', 'poller_id', 'sweep_interval', 'adapter', 'adapter_port', 'enabled'));
$fabric_existing = $fabric_id > 0 ? $fabric_fabrics->find($fabric_id) : null;
if ($fabric_id > 0 && $fabric_existing === null) {
header('Location: fabric_admin.php');
exit;
}
if ($fabric_existing !== null) {
fabric_require_fabric_access($fabric_access, $fabric_nodes, $fabric_existing->id);
}
$fabric_parsed = \Somethingwithproof\Fabric\FabricInput::parse($fabric_input, $fabric_id, $fabric_pollers->names());
$fabric_errors = $fabric_parsed->errors;
if ($fabric_parsed->fabric !== null && $fabric_fabrics->nameTaken($fabric_parsed->fabric->name, $fabric_id)) {
$fabric_errors['name'] = __('Name: another fabric already uses this name.', 'fabric');
}
if ($fabric_parsed->fabric !== null && $fabric_fabrics->portTaken($fabric_parsed->fabric)) {
$fabric_errors['adapter'] = __('Adapter: another fabric already sweeps this adapter port on this poller. One fabric per adapter port.', 'fabric');
}
if ($fabric_errors !== array() || $fabric_parsed->fabric === null) {
top_header();
$fabric_page->renderFabricEdit($fabric_existing, $fabric_input, $fabric_errors);
bottom_footer();
exit;
}
fabric_done($fabric_fabrics->save($fabric_parsed->fabric) > 0, __('The fabric was saved.', 'fabric'), __('The fabric could not be saved.', 'fabric'));
case 'delete_fabric':
$fabric_existing = $fabric_fabrics->find((int) get_filter_request_var('id'));
if ($fabric_existing === null) {
header('Location: fabric_admin.php');
exit;
}
fabric_require_fabric_access($fabric_access, $fabric_nodes, $fabric_existing->id);
top_header();
$fabric_page->renderDeleteConfirm($fabric_existing);
bottom_footer();
exit;
case 'delete_fabric_confirm':
fabric_require_fabric_access($fabric_access, $fabric_nodes, (int) get_filter_request_var('id'));
if ($fabric_fabrics->sweeping((int) get_filter_request_var('id'))) {
fabric_done(false, '', __('A sweep of this fabric is running, so nothing was deleted. Try again when it finishes.', 'fabric'));
}
fabric_done($fabric_fabrics->delete((int) get_filter_request_var('id')), __('The fabric and its inventory were deleted. No Cacti device was changed and the fabric was not contacted.', 'fabric'), __('The fabric could not be deleted.', 'fabric'));
case 'accept_conflict':
$fabric_node = fabric_posted_node($fabric_nodes);
if ($fabric_node !== null && !$fabric_access->node($fabric_node)) {
fabric_denied();
}
fabric_done($fabric_node !== null && $fabric_nodes->acceptConflict($fabric_node), __('The node identity was updated. It is swept again from the next cycle.', 'fabric'), __('The node identity was not changed.', 'fabric'));
case 'confirm_relink':
$fabric_moved = $fabric_relinks->confirm((int) get_filter_request_var('id'), $fabric_access);
// The device that moved now lists the new node's ports; the old
// node's data sources stay with their history (REQ-MAP-005).
if ($fabric_moved !== null) {
$fabric_devices->reindex($fabric_moved);
}
fabric_done($fabric_moved !== null, __('The device mapping moved to the new node.', 'fabric'), __('The proposal could not be confirmed. The nodes may have changed since it was made.', 'fabric'));
case 'reject_relink':
$fabric_relink = $fabric_relinks->find((int) get_filter_request_var('id'));
if ($fabric_relink === null || !$fabric_access->host((int) $fabric_relink['old_host_id']) || !$fabric_access->host((int) $fabric_relink['new_host_id'])) {
fabric_denied();
}
fabric_done($fabric_relinks->reject((int) get_filter_request_var('id')), __('The proposal was rejected.', 'fabric'), __('The proposal could not be rejected.', 'fabric'));
case 'save_node':
$fabric_node = fabric_posted_node($fabric_nodes);
$fabric_host = (int) get_filter_request_var('host_id');
// The node's current device and the one it moves to.
if ($fabric_node !== null && (!$fabric_access->node($fabric_node) || !$fabric_access->host($fabric_host))) {
fabric_denied();
}
$fabric_ok = $fabric_node !== null && ($fabric_host === 0 || $fabric_host === $fabric_node->hostId || $fabric_hosts->exists($fabric_host));
if ($fabric_ok && $fabric_node !== null && $fabric_host !== 0) {
$fabric_refusal = $fabric_devices->mapRefusal($fabric_node, $fabric_host);
if ($fabric_refusal !== null) {
fabric_done(false, '', __('The node mapping was not changed. %s', $fabric_refusal, 'fabric'));
}
}
fabric_done($fabric_ok && $fabric_node !== null && $fabric_devices->map($fabric_node, $fabric_host), __('The node mapping was saved.', 'fabric'), __('The node mapping was not changed.', 'fabric'));
case 'create_device':
$fabric_node = $fabric_nodes->find((int) get_filter_request_var('id'));
if ($fabric_node === null) {
header('Location: fabric_admin.php');
exit;
}
if (!$fabric_create || !$fabric_access->node($fabric_node)) {
fabric_denied();
}
top_header();
$fabric_page->renderCreateConfirm($fabric_node);
bottom_footer();
exit;
case 'create_device_confirm':
$fabric_node = fabric_posted_node($fabric_nodes);
if ($fabric_node !== null && (!$fabric_create || !$fabric_access->node($fabric_node))) {
fabric_denied();
}
fabric_done($fabric_node !== null && $fabric_devices->create($fabric_node) > 0, __('The device was created and the node mapped to it. Choose the ports to graph on the New Graphs page.', 'fabric'), __('No device was created. See the Cacti log.', 'fabric'));
case 'edit_fabric':
$fabric_id = (int) get_filter_request_var('id');
if ($fabric_id > 0) {
fabric_require_fabric_access($fabric_access, $fabric_nodes, $fabric_id);
}
top_header();
$fabric_page->renderFabricEdit($fabric_id > 0 ? $fabric_fabrics->find($fabric_id) : null);
bottom_footer();
exit;
case 'edit_node':
$fabric_node = $fabric_nodes->find((int) get_filter_request_var('id'));
if ($fabric_node === null) {
header('Location: fabric_admin.php');
exit;
}
if (!$fabric_access->node($fabric_node)) {
fabric_denied();
}
top_header();
$fabric_page->renderNodeEdit($fabric_node);
bottom_footer();
exit;
case 'export':
$fabric_info = plugin_fabric_version();
$fabric_json = $fabric_page->export(isset($fabric_info['version']) ? $fabric_info['version'] : '', $fabric_clock->now());
if ($fabric_json === null) {
fabric_done(false, '', __('The inventory could not be read for export.', 'fabric'));
}
header('Content-Type: application/json; charset=utf-8');
header('Content-Disposition: attachment; filename="fabric-inventory.json"');
header('X-Content-Type-Options: nosniff');
print $fabric_json;
exit;
default:
top_header();
$fabric_page->renderList();
bottom_footer();
}