Repository navigation
Expand file tree
/
Copy pathaudit_log.php
More file actions
300 lines (279 loc) · 13.2 KB
/
Copy pathaudit_log.php
File metadata and controls
300 lines (279 loc) · 13.2 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
<?php
/**
* Audit Log
*
* Searchable view of who did what, to which firewall, from where.
*
* Reading the audit trail is available to every staff role, including Read Only:
* oversight is the point of an audit log, and it never contains credential
* material (audit_scrub_metadata() strips it before the row is written).
*/
require_once __DIR__ . '/inc/bootstrap.php';
require_permission('audit.view');
// --- filters ---------------------------------------------------------------
$page = max(1, (int)($_GET['page'] ?? 1));
$per_page = 50;
$offset = ($page - 1) * $per_page;
$f_action = trim($_GET['action_filter'] ?? '');
$f_user = trim($_GET['user'] ?? '');
$f_firewall = trim($_GET['firewall'] ?? '');
$f_customer = trim($_GET['customer'] ?? '');
$f_result = trim($_GET['result'] ?? '');
$f_from = trim($_GET['from'] ?? '');
$f_to = trim($_GET['to'] ?? '');
$f_search = trim($_GET['q'] ?? '');
$where = [];
$params = [];
if ($f_action !== '') {
$where[] = 'a.action = ?';
$params[] = $f_action;
}
if ($f_user !== '' && ctype_digit($f_user)) {
$where[] = 'a.user_id = ?';
$params[] = (int)$f_user;
}
if ($f_firewall !== '' && ctype_digit($f_firewall)) {
$where[] = 'a.firewall_id = ?';
$params[] = (int)$f_firewall;
}
if ($f_customer !== '' && ctype_digit($f_customer)) {
$where[] = 'a.customer_id = ?';
$params[] = (int)$f_customer;
}
if ($f_result === 'success') {
$where[] = 'a.success = 1';
} elseif ($f_result === 'failure') {
$where[] = 'a.success = 0';
}
// Dates are validated rather than interpolated, so a malformed value is ignored
// instead of becoming part of the query.
if ($f_from !== '' && preg_match('/^\d{4}-\d{2}-\d{2}$/', $f_from)) {
$where[] = 'a.occurred_at >= ?';
$params[] = $f_from . ' 00:00:00';
}
if ($f_to !== '' && preg_match('/^\d{4}-\d{2}-\d{2}$/', $f_to)) {
$where[] = 'a.occurred_at <= ?';
$params[] = $f_to . ' 23:59:59';
}
if ($f_search !== '') {
$where[] = '(a.message LIKE ? OR a.username LIKE ? OR a.source_ip LIKE ? OR a.object_id LIKE ?)';
$like = '%' . $f_search . '%';
array_push($params, $like, $like, $like, $like);
}
$where_sql = $where ? 'WHERE ' . implode(' AND ', $where) : '';
// --- data ------------------------------------------------------------------
// Note: inc/header.php declares its own $rows (the settings map) and is
// included further down, so page-level result sets must not use that name.
$total = 0;
$audit_rows = [];
$actions = [];
$users = [];
$firewalls = [];
$error = '';
try {
$count = db()->prepare("SELECT COUNT(*) FROM audit_log a {$where_sql}");
$count->execute($params);
$total = (int)$count->fetchColumn();
$sql = "SELECT a.*, f.hostname AS firewall_hostname
FROM audit_log a
LEFT JOIN firewalls f ON a.firewall_id = f.id
{$where_sql}
ORDER BY a.occurred_at DESC, a.id DESC
LIMIT {$per_page} OFFSET {$offset}";
$stmt = db()->prepare($sql);
$stmt->execute($params);
$audit_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Filter dropdowns
$actions = db()->query('SELECT DISTINCT action FROM audit_log ORDER BY action')->fetchAll(PDO::FETCH_COLUMN);
$users = db()->query('SELECT id, username FROM users ORDER BY username')->fetchAll(PDO::FETCH_ASSOC);
$firewalls = db()->query('SELECT id, hostname FROM firewalls ORDER BY hostname')->fetchAll(PDO::FETCH_ASSOC);
} catch (Throwable $e) {
error_log('audit_log.php: ' . $e->getMessage());
$error = 'Could not load the audit log.';
}
$total_pages = max(1, (int)ceil($total / $per_page));
/** Bootstrap colour for an action, so the list scans quickly. */
function audit_action_class(string $action, int $success): string {
if (!$success) {
return 'danger';
}
return match (true) {
str_starts_with($action, 'auth.') => 'info',
str_starts_with($action, 'command.') => 'warning',
str_starts_with($action, 'agent.') => 'primary',
str_starts_with($action, 'backup.') => 'success',
str_starts_with($action, 'authz.') => 'danger',
default => 'secondary',
};
}
/** Preserve the current filters when building a page link. */
function audit_page_url(int $page): string {
$params = $_GET;
$params['page'] = $page;
return '?' . http_build_query($params);
}
include __DIR__ . '/inc/header.php';
?>
<div class="container-fluid">
<div class="d-flex justify-content-between align-items-center mb-3">
<div>
<h4 class="mb-0"><i class="fas fa-clipboard-list me-2"></i>Audit Log</h4>
<small class="text-muted">
Who did what, to which firewall, from where.
Credentials are never recorded.
</small>
</div>
<span class="badge bg-secondary"><?php echo number_format($total); ?> entries</span>
</div>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<div class="card mb-3">
<div class="card-body">
<form method="get" class="row g-2 align-items-end">
<div class="col-md-2">
<label class="form-label small mb-1">Action</label>
<select name="action_filter" class="form-select form-select-sm">
<option value="">All actions</option>
<?php foreach ($actions as $a): ?>
<option value="<?php echo htmlspecialchars($a); ?>" <?php echo $f_action === $a ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($a); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label class="form-label small mb-1">User</label>
<select name="user" class="form-select form-select-sm">
<option value="">All users</option>
<?php foreach ($users as $u): ?>
<option value="<?php echo (int)$u['id']; ?>" <?php echo $f_user === (string)$u['id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($u['username']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label class="form-label small mb-1">Firewall</label>
<select name="firewall" class="form-select form-select-sm">
<option value="">All firewalls</option>
<?php foreach ($firewalls as $fw): ?>
<option value="<?php echo (int)$fw['id']; ?>" <?php echo $f_firewall === (string)$fw['id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($fw['hostname']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-1">
<label class="form-label small mb-1">Result</label>
<select name="result" class="form-select form-select-sm">
<option value="">Any</option>
<option value="success" <?php echo $f_result === 'success' ? 'selected' : ''; ?>>Success</option>
<option value="failure" <?php echo $f_result === 'failure' ? 'selected' : ''; ?>>Failure</option>
</select>
</div>
<div class="col-md-1">
<label class="form-label small mb-1">From</label>
<input type="date" name="from" class="form-control form-control-sm" value="<?php echo htmlspecialchars($f_from); ?>">
</div>
<div class="col-md-1">
<label class="form-label small mb-1">To</label>
<input type="date" name="to" class="form-control form-control-sm" value="<?php echo htmlspecialchars($f_to); ?>">
</div>
<div class="col-md-2">
<label class="form-label small mb-1">Search</label>
<input type="text" name="q" class="form-control form-control-sm"
placeholder="message, user, IP" value="<?php echo htmlspecialchars($f_search); ?>">
</div>
<div class="col-md-1 d-grid gap-1">
<button class="btn btn-primary btn-sm" type="submit"><i class="fas fa-filter me-1"></i>Filter</button>
<a class="btn btn-outline-secondary btn-sm" href="audit_log.php">Reset</a>
</div>
</form>
</div>
</div>
<div class="card">
<div class="table-responsive">
<table class="table table-sm table-hover align-middle mb-0">
<thead>
<tr>
<th style="white-space:nowrap">When</th>
<th>Action</th>
<th>Actor</th>
<th>Source IP</th>
<th>Firewall</th>
<th>Detail</th>
</tr>
</thead>
<tbody>
<?php if (!$audit_rows): ?>
<tr><td colspan="6" class="text-center text-muted py-4">No audit entries match these filters.</td></tr>
<?php else: foreach ($audit_rows as $r): ?>
<tr>
<td class="text-nowrap small text-muted">
<?php echo htmlspecialchars(date('Y-m-d H:i:s', strtotime($r['occurred_at']))); ?>
</td>
<td>
<span class="badge bg-<?php echo audit_action_class($r['action'], (int)$r['success']); ?>">
<?php echo htmlspecialchars($r['action']); ?>
</span>
<?php if (!$r['success']): ?>
<i class="fas fa-times-circle text-danger ms-1" title="Failed"></i>
<?php endif; ?>
</td>
<td class="small">
<?php
$actor = $r['username'] ?: ucfirst($r['actor_type']);
echo htmlspecialchars($actor);
?>
<span class="text-muted">(<?php echo htmlspecialchars($r['actor_type']); ?>)</span>
</td>
<td class="small text-muted"><?php echo htmlspecialchars($r['source_ip'] ?? '—'); ?></td>
<td class="small">
<?php if ($r['firewall_hostname']): ?>
<a href="firewall_details.php?id=<?php echo (int)$r['firewall_id']; ?>">
<?php echo htmlspecialchars($r['firewall_hostname']); ?>
</a>
<?php else: ?>
<span class="text-muted">—</span>
<?php endif; ?>
</td>
<td class="small">
<?php echo htmlspecialchars($r['message'] ?? ''); ?>
<?php if (!empty($r['metadata'])): ?>
<details class="d-inline">
<summary class="text-muted" style="cursor:pointer;display:inline">details</summary>
<pre class="small mb-0 mt-1" style="white-space:pre-wrap"><?php
$meta = json_decode($r['metadata'], true);
echo htmlspecialchars(
is_array($meta)
? json_encode($meta, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
: (string)$r['metadata']
);
?></pre>
</details>
<?php endif; ?>
</td>
</tr>
<?php endforeach; endif; ?>
</tbody>
</table>
</div>
</div>
<?php if ($total_pages > 1): ?>
<nav class="mt-3">
<ul class="pagination pagination-sm justify-content-center">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="<?php echo htmlspecialchars(audit_page_url($page - 1)); ?>">Previous</a>
</li>
<li class="page-item disabled">
<span class="page-link">Page <?php echo $page; ?> of <?php echo $total_pages; ?></span>
</li>
<li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
<a class="page-link" href="<?php echo htmlspecialchars(audit_page_url($page + 1)); ?>">Next</a>
</li>
</ul>
</nav>
<?php endif; ?>
</div>
<?php include __DIR__ . '/inc/footer.php'; ?>