Repository navigation
Expand file tree
/
Copy pathagent_checkin.php
More file actions
1029 lines (920 loc) · 52.9 KB
/
Copy pathagent_checkin.php
File metadata and controls
1029 lines (920 loc) · 52.9 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
require_once __DIR__ . '/inc/bootstrap_agent.php';
require_once __DIR__ . '/inc/logging.php';
require_once __DIR__ . '/inc/agent_auth.php';
require_once __DIR__ . '/inc/command_results.php';
require_once __DIR__ . '/inc/agent_commands.php';
require_once __DIR__ . '/inc/reboot_state.php';
require_once __DIR__ . '/inc/firewall_health.php';
require_once __DIR__ . '/inc/backup_storage.php';
require_once __DIR__ . '/inc/platform/platform.php';
require_once __DIR__ . '/inc/speedtest.php';
// Endpoint for firewall agent check-ins
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['success' => false, 'message' => 'Method not allowed']);
exit;
}
// Get JSON input. agent_request_input() caches the raw body so that HMAC
// signature verification hashes exactly the bytes we received.
$input = agent_request_input();
if (!$input) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Invalid JSON input']);
exit;
}
// Authenticate once, up front, for every branch of this endpoint.
// Identity resolution (firewall_id or hardware_id lookup), hardware_id pinning,
// API key verification and HMAC signature checking all live in inc/agent_auth.php.
$authenticated_firewall = authenticateAgentRequest($input);
$firewall_id = (int)$authenticated_firewall['id'];
$hardware_id = (string)$authenticated_firewall['hardware_id'];
// Credentials to hand back if this agent has not yet adopted its API key.
$agent_credentials = agent_credentials_payload($authenticated_firewall);
// Check if this is a command result report (agent reporting back command execution status)
$command_id = (int)($input['command_id'] ?? 0);
$command_status = trim($input['status'] ?? '');
$command_result = trim($input['result'] ?? '');
if ($command_id > 0 && !empty($command_status)) {
// This is a command result report, not a regular check-in.
try {
// Ownership, terminal-state and status validation live in one place so
// that agent_checkin.php and api/command_result.php cannot drift apart.
$accepted = record_agent_command_result(
$firewall_id,
$command_id,
$command_status,
$command_result,
['result_is_base64' => true]
);
if (!$accepted['ok']) {
http_response_code($accepted['status']);
echo json_encode(['success' => false, 'message' => $accepted['message']]);
exit;
}
$command_result = $accepted['result'];
$command_status = $accepted['normalized_status'];
// Check if this was a speedtest command - parse results into bandwidth_tests
if ($command_status === 'completed' && !empty($command_result)) {
// Reuse the row record_agent_command_result() already verified as
// belonging to this firewall rather than re-querying by id alone.
$cmd_info = $accepted['command'];
if ($cmd_info && (speedtest_is_command($cmd_info['command'] ?? '') || ($cmd_info['command_type'] ?? '') === 'speedtest')) {
// A failed leg is stored as "not measured", never 0 Mbps, and
// a failed test is recorded as failed instead of dropped.
$speedtest_data = json_decode($command_result, true);
if (is_array($speedtest_data)) {
// Storing the measurement must never fail the command
// result: the raw JSON stays in firewall_commands.result.
try {
$bw_id = speedtest_ingest((int)$cmd_info['firewall_id'], $speedtest_data, $cmd_info);
error_log("Speedtest result stored for firewall {$cmd_info['firewall_id']} (bandwidth_tests #{$bw_id})");
} catch (Throwable $e) {
error_log("Speedtest result NOT stored for firewall {$cmd_info['firewall_id']} (command {$cmd_info['id']}): " . $e->getMessage());
}
}
}
}
// Interpret the real outcome of an OS update.
//
// The agent hardcodes "status":"completed" for every command it runs, so
// the reported status says only that the agent finished executing - not
// that the upgrade succeeded. install_updates echoes its own exit code
// for exactly this reason; read it rather than assuming success.
$cmd_row = $accepted['command'] ?? null;
if ($cmd_row && ($cmd_row['action'] ?? '') === 'install_updates') {
$outcome = interpret_update_result((string)$command_result);
if (!$outcome['known']) {
$verdict = 'unconfirmed';
} else {
$verdict = $outcome['ok'] ? 'success' : 'failed';
}
db()->prepare(
'UPDATE firewalls SET last_update_result = ?, last_update_attempt_at = NOW() WHERE id = ?'
)->execute([$verdict, $firewall_id]);
// Reflect a genuine failure in the command row too, so the history
// does not show a green "completed" for an upgrade that failed.
if ($outcome['known'] && !$outcome['ok']) {
db()->prepare("UPDATE firewall_commands SET status = 'failed' WHERE id = ?")
->execute([$command_id]);
}
// Force a fresh update check instead of asserting what is now
// installed. The agent reports the real version on its next
// check-in; guessing here is what previously left the server
// claiming "no updates available" for an upgrade that never ran.
db()->prepare('UPDATE firewalls SET last_update_check = NULL WHERE id = ?')
->execute([$firewall_id]);
// A successful install means the new base/kernel is on disk but not
// yet running, so re-derive the reboot state immediately instead of
// waiting for the next check-in.
try {
recompute_reboot_required($firewall_id);
} catch (Throwable $e) {
error_log("Reboot state recompute failed after update for firewall $firewall_id: " . $e->getMessage());
}
error_log("Update result for firewall $firewall_id (command $command_id): $verdict"
. ($outcome['known'] ? " (exit {$outcome['exit_code']})" : ' (no exit marker)'));
}
echo json_encode(['success' => true, 'message' => 'Command result recorded']);
exit;
} catch (Exception $e) {
error_log("Failed to update command $command_id: " . $e->getMessage());
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Failed to record command result']);
exit;
}
}
$agent_version = trim($input['agent_version'] ?? '');
$agent_type = trim($input['agent_type'] ?? 'primary'); // 'primary' or 'update'
$api_key = trim($input['api_key'] ?? '');
$wan_ip_raw = trim($input['wan_ip'] ?? '');
$lan_ip_raw = trim($input['lan_ip'] ?? '');
$ipv6_raw = trim($input['ipv6_address'] ?? '');
// WAN interface auto-detection fields (Agent v3.4.0+)
$wan_interfaces = trim($input['wan_interfaces'] ?? '');
$wan_groups = trim($input['wan_groups'] ?? '');
$wan_interface_stats = $input['wan_interface_stats'] ?? null;
if (is_array($wan_interface_stats)) {
$wan_interface_stats = json_encode($wan_interface_stats);
}
// New network configuration fields
// These are addresses, masks and CIDRs. They are rendered on the firewall
// list, so anything that is not address-shaped is dropped here rather than
// stored: a firewall (or its stolen credentials) could otherwise plant markup
// that runs in an administrator's browser.
$netField = static function ($v): string {
$v = is_scalar($v) ? trim((string)$v) : '';
return (strlen($v) <= 64 && preg_match('/^[0-9A-Fa-fx:.\/%]*$/', $v)) ? $v : '';
};
$wan_netmask = $netField($input['wan_netmask'] ?? '');
$wan_gateway = $netField($input['wan_gateway'] ?? '');
$wan_dns_primary = $netField($input['wan_dns_primary'] ?? '');
$wan_dns_secondary = $netField($input['wan_dns_secondary'] ?? '');
$lan_netmask = $netField($input['lan_netmask'] ?? '');
$lan_network = $netField($input['lan_network'] ?? '');
// Handle opnsense_version - can be string or object
$opnsense_version = $input['opnsense_version'] ?? '';
if (is_array($opnsense_version) || is_object($opnsense_version)) {
$opnsense_version = json_encode($opnsense_version);
} else {
$opnsense_version = trim($opnsense_version);
}
$uptime = trim($input['uptime'] ?? '');
$agent_pid = (int)($input['agent_pid'] ?? 0); // Process ID for duplicate detection
// Validate inputs
if (empty($agent_version)) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Missing required fields']);
exit;
}
// Identity, hardware_id pinning, API key and HMAC signature were all verified by
// authenticateAgentRequest() at the top of this file. $firewall is the row it
// returned, so no endpoint-local credential comparison happens here any more.
$firewall = $authenticated_firewall;
// Rate limiting removed - Agent v3.0 has built-in PID locking to prevent duplicates
try {
// Check if agent reports reboot is required
// Only update reboot_required if agent explicitly sends it (for newer agents)
// Otherwise, preserve existing value in database
// FIXED: Use $input (JSON body) instead of $_POST (which is empty for JSON requests)
$agent_sent_reboot_status = isset($input['reboot_required']);
$reboot_required = $agent_sent_reboot_status ? (int)$input['reboot_required'] : null;
// When reboot_required transitions from 1→0 (firewall rebooted), force update check
if ($agent_sent_reboot_status && $reboot_required === 0) {
$prev_reboot = db()->prepare('SELECT reboot_required FROM firewalls WHERE id = ?');
$prev_reboot->execute([$firewall_id]);
$prev_reboot_val = (int)$prev_reboot->fetchColumn();
if ($prev_reboot_val === 1) {
// Firewall just rebooted - force fresh update check
$force_check = db()->prepare('UPDATE firewalls SET last_update_check = NULL WHERE id = ?');
$force_check->execute([$firewall_id]);
error_log("Firewall $firewall_id rebooted (reboot_required 1→0), forcing update check");
}
}
// Update the main firewalls table with all the collected information
// If agent sends empty values, preserve existing data in DB
// This prevents good data from being overwritten when agent restarts or has collection issues
$preserve_stmt = db()->prepare('SELECT wan_ip, lan_ip, ipv6_address, version, uptime FROM firewalls WHERE id = ?');
$preserve_stmt->execute([$firewall_id]);
$existing = $preserve_stmt->fetch(PDO::FETCH_ASSOC);
$wan_ip = !empty($wan_ip_raw) ? $wan_ip_raw : ($existing['wan_ip'] ?? '');
$lan_ip = !empty($lan_ip_raw) ? $lan_ip_raw : ($existing['lan_ip'] ?? '');
$ipv6_address = !empty($ipv6_raw) ? $ipv6_raw : ($existing['ipv6_address'] ?? '');
if (empty($opnsense_version)) {
$opnsense_version = $existing['version'] ?? '';
}
if (empty($uptime) || $uptime === 'Unknown') {
$uptime = $existing['uptime'] ?? 'Unknown';
}
$uptime = $uptime ?: 'Unknown';
// Preserve 'updating'/'update_pending' status during check-in - let the update
// detection logic (below) handle the status transition properly
// Read the current status before writing it back. $firewall_status is not
// populated until the update-check block much further down, so this line
// was reading an undefined variable and always falling through to 'online',
// clobbering an in-progress 'updating'/'update_pending' state on every
// check-in.
$status_stmt = db()->prepare('SELECT status FROM firewalls WHERE id = ?');
$status_stmt->execute([$firewall_id]);
$current_status = (string)($status_stmt->fetchColumn() ?: '');
$checkin_status = in_array($current_status, ['updating', 'update_pending'], true)
? $current_status
: 'online';
if ($agent_sent_reboot_status) {
// Agent supports reboot detection - update the flag
// Only update network config if provided by agent, otherwise preserve existing values
if (!empty($wan_netmask) || !empty($wan_gateway)) {
$stmt = db()->prepare('UPDATE firewalls SET last_checkin = NOW(), agent_version = ?, status = ?, wan_ip = ?, lan_ip = ?, ipv6_address = ?, version = ?, uptime = ?, reboot_required = ?, wan_netmask = ?, wan_gateway = ?, wan_dns_primary = ?, wan_dns_secondary = ?, lan_netmask = ?, lan_network = ?, wan_interfaces = ?, wan_groups = ?, wan_interface_stats = ?, network_config_updated = NOW(), tunnel_active = 1 WHERE id = ?');
$result = $stmt->execute([$agent_version, $checkin_status, $wan_ip, $lan_ip, $ipv6_address, $opnsense_version, $uptime, $reboot_required, $wan_netmask, $wan_gateway, $wan_dns_primary, $wan_dns_secondary, $lan_netmask, $lan_network, $wan_interfaces, $wan_groups, $wan_interface_stats, $firewall_id]);
} else {
// Preserve existing network config
$stmt = db()->prepare('UPDATE firewalls SET last_checkin = NOW(), agent_version = ?, status = ?, wan_ip = ?, lan_ip = ?, ipv6_address = ?, version = ?, uptime = ?, reboot_required = ?, wan_interfaces = ?, wan_groups = ?, wan_interface_stats = ?, tunnel_active = 1 WHERE id = ?');
$result = $stmt->execute([$agent_version, $checkin_status, $wan_ip, $lan_ip, $ipv6_address, $opnsense_version, $uptime, $reboot_required, $wan_interfaces, $wan_groups, $wan_interface_stats, $firewall_id]);
}
} else {
// Agent doesn't support reboot detection - preserve existing reboot_required value
// Only update network config if provided by agent, otherwise preserve existing values
if (!empty($wan_netmask) || !empty($wan_gateway)) {
$stmt = db()->prepare('UPDATE firewalls SET last_checkin = NOW(), agent_version = ?, status = ?, wan_ip = ?, lan_ip = ?, ipv6_address = ?, version = ?, uptime = ?, wan_netmask = ?, wan_gateway = ?, wan_dns_primary = ?, wan_dns_secondary = ?, lan_netmask = ?, lan_network = ?, wan_interfaces = ?, wan_groups = ?, wan_interface_stats = ?, network_config_updated = NOW(), tunnel_active = 1 WHERE id = ?');
$result = $stmt->execute([$agent_version, $checkin_status, $wan_ip, $lan_ip, $ipv6_address, $opnsense_version, $uptime, $wan_netmask, $wan_gateway, $wan_dns_primary, $wan_dns_secondary, $lan_netmask, $lan_network, $wan_interfaces, $wan_groups, $wan_interface_stats, $firewall_id]);
} else {
// Preserve existing network config
$stmt = db()->prepare('UPDATE firewalls SET last_checkin = NOW(), agent_version = ?, status = ?, wan_ip = ?, lan_ip = ?, ipv6_address = ?, version = ?, uptime = ?, wan_interfaces = ?, wan_groups = ?, wan_interface_stats = ?, tunnel_active = 1 WHERE id = ?');
$result = $stmt->execute([$agent_version, $checkin_status, $wan_ip, $lan_ip, $ipv6_address, $opnsense_version, $uptime, $wan_interfaces, $wan_groups, $wan_interface_stats, $firewall_id]);
}
}
if (!$result) {
error_log("Failed to update firewalls table for firewall $firewall_id: " . print_r($stmt->errorInfo(), true));
}
// Derive reboot_required from the uptime just reported rather than leaving
// it frozen. The agent has never sent a reboot flag, so this column was
// previously writable only by code that guessed - which left one firewall
// asserting "reboot required" continuously from March through many actual
// reboots, and another reporting no reboot needed while running a kernel it
// had not booted into. Never allowed to break a check-in.
try {
$reboot_verdict = recompute_reboot_required($firewall_id);
if ($reboot_verdict['changed']) {
error_log("Firewall $firewall_id reboot_required -> {$reboot_verdict['state']} ({$reboot_verdict['reason']})");
}
} catch (Throwable $e) {
error_log("Reboot state recompute failed for firewall $firewall_id: " . $e->getMessage());
}
// Also update or insert agent record for historical tracking
// Support both 'primary' and 'update' agent types
$stmt = db()->prepare('INSERT INTO firewall_agents (firewall_id, agent_version, agent_type, last_checkin, status, wan_ip, lan_ip, ipv6_address, opnsense_version) VALUES (?, ?, ?, NOW(), ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE agent_version = VALUES(agent_version), last_checkin = NOW(), status = VALUES(status), wan_ip = VALUES(wan_ip), lan_ip = VALUES(lan_ip), ipv6_address = VALUES(ipv6_address), opnsense_version = VALUES(opnsense_version)');
$result2 = $stmt->execute([$firewall_id, $agent_version, $agent_type, 'online', $wan_ip, $lan_ip, $ipv6_address, $opnsense_version]);
if (!$result2) {
error_log("Failed to update firewall_agents table for firewall $firewall_id: " . print_r($stmt->errorInfo(), true));
} else {
// Log successful checkin
log_info('agent', "Agent checkin: firewall_id=$firewall_id, type=$agent_type, version=$agent_version, wan_ip=$wan_ip", null, $firewall_id);
}
// Platform detection (3.79.0). Runs on every check-in so the platform is
// re-verified before any queued command below is released, and so a
// firewall that stops reporting goes stale instead of staying trusted.
// Never allowed to fail a check-in; on error the previous values stand.
try {
$agent_platform = is_array($input['platform'] ?? null) ? $input['platform'] : [];
$detected = platform_detect([
'agent_platform' => $agent_platform,
'agent_version' => $agent_version,
'version_string' => is_string($opnsense_version) ? $opnsense_version : '',
// Only consulted when the agent sent no explicit platform block:
// reading the latest backup's root costs a file read per check-in.
'config_root' => $agent_platform ? null : platform_config_root_of_latest_backup($firewall_id),
]);
platform_record_detection($firewall_id, $detected, $agent_platform);
} catch (Throwable $e) {
error_log('Platform detection failed for firewall ' . $firewall_id . ': ' . $e->getMessage());
}
// OPNsense health telemetry (Agent v1.6.0+): gateways, VPN tunnels, CARP,
// services and certificates. Absent for older agents, which simply do not
// populate those views rather than being treated as "everything is down".
if (isset($input['health']) && is_array($input['health'])) {
try {
$health_stored = health_ingest($firewall_id, $input['health']);
if ($health_stored) {
error_log('Health ingested for firewall ' . $firewall_id . ': '
. json_encode($health_stored));
}
} catch (Throwable $e) {
// Health is supplementary; never fail a check-in over it.
error_log('Health ingest failed for firewall ' . $firewall_id . ': ' . $e->getMessage());
}
}
// Process WAN interface statistics if provided (Agent v3.4.0+)
if (!empty($wan_interface_stats)) {
processWANInterfaceStats($firewall_id, $wan_interface_stats);
}
// Store traffic statistics for charts
if (isset($input['traffic_stats'])) {
$traffic = $input['traffic_stats'];
if (isset($traffic['bytes_in']) && $traffic['bytes_in'] > 0) {
$stmt = db()->prepare("
INSERT INTO firewall_traffic_stats
(firewall_id, wan_interface, bytes_in, bytes_out, packets_in, packets_out, recorded_at)
VALUES (?, ?, ?, ?, ?, ?, NOW())
");
$stmt->execute([
$firewall_id,
$traffic['interface'] ?? 'unknown',
(int)($traffic['bytes_in'] ?? 0),
(int)($traffic['bytes_out'] ?? 0),
(int)($traffic['packets_in'] ?? 0),
(int)($traffic['packets_out'] ?? 0)
]);
}
}
// Store system statistics for charts (CPU, memory, disk)
if (isset($input['system_stats'])) {
$system = $input['system_stats'];
if (isset($system['memory_percent']) || isset($system['cpu_load_1min'])) {
$stmt = db()->prepare("INSERT INTO firewall_system_stats
(firewall_id, cpu_load_1min, cpu_load_5min, cpu_load_15min, memory_percent, memory_total_mb, memory_used_mb, disk_percent, disk_total_gb, disk_used_gb, recorded_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())");
$stmt->execute([
$firewall_id,
(float)($system['cpu_load_1min'] ?? 0),
(float)($system['cpu_load_5min'] ?? 0),
(float)($system['cpu_load_15min'] ?? 0),
(float)($system['memory_percent'] ?? 0),
(int)($system['memory_total_mb'] ?? 0),
(int)($system['memory_used_mb'] ?? 0),
(float)($system['disk_percent'] ?? 0),
(float)($system['disk_total_gb'] ?? 0),
(float)($system['disk_used_gb'] ?? 0)
]);
}
}
// Note: speedtest results are stored via command result handler (top of file)
// when agent reports back run_speedtest command completion
// Store latency statistics for charts
if (isset($input['latency_stats'])) {
$latency = $input['latency_stats'];
$avg_latency = (float)($latency['average_latency'] ?? 0);
if ($avg_latency > 0) {
$stmt = db()->prepare("INSERT INTO firewall_latency (firewall_id, latency_ms, measured_at) VALUES (?, ?, NOW())");
$stmt->execute([$firewall_id, $avg_latency]);
}
}
// Check if we need to perform update check (every 5 hours)
$stmt = db()->prepare('SELECT last_update_check, status, current_version FROM firewalls WHERE id = ?');
$stmt->execute([$firewall_id]);
$firewall_status = $stmt->fetch(PDO::FETCH_ASSOC);
// Use reported version, or preserve existing current_version from DB
$current_version = $opnsense_version;
if (empty($current_version) || $current_version === 'Unknown') {
$current_version = $firewall_status['current_version'] ?? '';
}
if (empty($current_version)) {
$current_version = 'Unknown';
}
$check_updates = false;
$is_updating = in_array($firewall_status['status'], ['updating', 'update_pending']);
if ($is_updating || !$firewall_status['last_update_check'] || strtotime($firewall_status['last_update_check']) < (time() - 18000)) { // 5 hours = 18000 seconds
$check_updates = true;
// Accept update status from agent instead of hardcoded version comparison
// Agent sends updates in opnsense_updates nested object or top-level fields
$updates_available = 0;
$latest_stable_version = $current_version;
// Handle nested opnsense_updates format (agent v1.3+)
if (isset($input['opnsense_updates']) && is_array($input['opnsense_updates'])) {
$upd = $input['opnsense_updates'];
$updates_available = ($upd['updates_available'] === true || $upd['updates_available'] === 'true' || $upd['updates_available'] == 1) ? 1 : 0;
if (!empty($upd['new_version'])) {
$latest_stable_version = trim($upd['new_version']);
}
}
// Also check top-level fields (agent v1.5+)
if (isset($input['updates_available'])) {
$updates_available = ($input['updates_available'] === true || $input['updates_available'] === 'true' || intval($input['updates_available']) == 1) ? 1 : 0;
}
if (!empty($input['available_version'])) {
$latest_stable_version = trim($input['available_version']);
}
// Sanity check: if current and available versions match, no updates needed
if ($updates_available == 1 && !empty($current_version) && $current_version !== 'Unknown'
&& !empty($latest_stable_version) && $current_version === $latest_stable_version) {
$updates_available = 0;
error_log("Firewall $firewall_id: Cleared stale updates_available (current=$current_version matches available=$latest_stable_version)");
}
// Update the database with check results from agent
$stmt = db()->prepare('UPDATE firewalls SET last_update_check = NOW(), current_version = ?, available_version = ?, updates_available = ? WHERE id = ?');
$stmt->execute([$current_version, $latest_stable_version, $updates_available, $firewall_id]);
// If firewall was marked as 'updating' but reported back with current version,
// check if update actually completed
if (in_array($firewall_status['status'], ['updating', 'update_pending'])) {
// Check for stuck update timeout (15 minutes)
$update_requested_at_stmt = db()->prepare('SELECT update_requested_at FROM firewalls WHERE id = ?');
$update_requested_at_stmt->execute([$firewall_id]);
$update_requested_at = $update_requested_at_stmt->fetchColumn();
$update_age_minutes = $update_requested_at ? (time() - strtotime($update_requested_at)) / 60 : 999;
if ($updates_available == 0) {
// Update completed (or was already up to date) - return to online
// reboot_required is deliberately not touched here. This branch fires when
// a firewall reappears after an update, which says nothing about whether
// it has actually booted into the new base/kernel - clearing the flag
// here reported "no reboot needed" for a box still running the old one.
// recompute_reboot_required() decides it from uptime instead.
$stmt = db()->prepare('UPDATE firewalls SET status = ? WHERE id = ?');
$stmt->execute(['online', $firewall_id]);
log_info('firewall', "Update completed for firewall - now running version $current_version",
null, $firewall_id, [
'action' => 'update_completed',
'new_version' => $current_version
]);
} elseif (version_compare($current_version, $firewall_status['current_version'] ?: '', '>')) {
// Version increased but still not latest - partial update
$stmt = db()->prepare('UPDATE firewalls SET status = ? WHERE id = ?');
$stmt->execute(['online', $firewall_id]);
log_info('firewall', "Partial update completed for firewall - version upgraded to $current_version",
null, $firewall_id, [
'action' => 'partial_update_completed',
'new_version' => $current_version
]);
} elseif ($update_age_minutes > 5) {
// Agent is checking in while status is 'updating' and 5+ minutes have passed.
// The update either completed (after reboot) or failed - recover to online.
// opnsense-update -bkf may leave minor patches still available, so
// updates_available can still be 1 even after a successful base update.
// reboot_required is deliberately not touched here. This branch fires when
// a firewall reappears after an update, which says nothing about whether
// it has actually booted into the new base/kernel - clearing the flag
// here reported "no reboot needed" for a box still running the old one.
// recompute_reboot_required() decides it from uptime instead.
$stmt = db()->prepare('UPDATE firewalls SET status = ? WHERE id = ?');
$stmt->execute(['online', $firewall_id]);
log_info('firewall', "Update completed for firewall after {$update_age_minutes}min - version $current_version (minor patches may still be available)",
null, $firewall_id, [
'action' => 'update_timeout_recovery',
'version' => $current_version,
'updates_still_available' => $updates_available
]);
}
}
} else {
// Even if not doing full update check, update current_version
$stmt = db()->prepare('UPDATE firewalls SET current_version = ? WHERE id = ?');
$stmt->execute([$current_version, $firewall_id]);
}
// Get check-in interval based on agent type
// Primary agent: 120 seconds (2 minutes)
// Update agent: 300 seconds (5 minutes)
if ($agent_type === 'update') {
$checkin_interval = 300; // 5 minutes for update agent
} else {
// Check for firewall-specific setting, default to 120 seconds (2 min) for primary agent
$stmt = db()->prepare('SELECT checkin_interval FROM firewalls WHERE id = ?');
$stmt->execute([$firewall_id]);
$firewall_data = $stmt->fetch();
$checkin_interval = (int)($firewall_data['checkin_interval'] ?? 120);
}
// Check for agent updates
$agent_update_check = checkAgentUpdate($agent_version, $firewall_id);
// Check for OPNsense update requests
$stmt = db()->prepare('SELECT update_requested, update_requested_at FROM firewalls WHERE id = ?');
$stmt->execute([$firewall_id]);
$update_info = $stmt->fetch();
$opnsense_update_requested = false;
$platform_row = platform_firewall($firewall_id) ?? $firewall;
$firmware_allowed = platform_capability_allows($platform_row, 'firmware.install');
if ($update_info && $update_info['update_requested'] && !$firmware_allowed['allowed']) {
// The request flag predates platform awareness: a firmware request on a
// platform where installation is not implemented is closed out with
// the reason instead of sending `opnsense-update` to a pfSense box.
db()->prepare(
"UPDATE firewalls SET update_requested = 0, last_update_result = 'unsupported',
last_update_error = ?, last_update_attempt_at = NOW() WHERE id = ?"
)->execute([substr($firmware_allowed['reason'], 0, 255), $firewall_id]);
audit_log_agent('update.refused', $firewall_id, [
'success' => false,
'message' => 'Firmware update not delivered: ' . substr($firmware_allowed['reason'], 0, 400),
]);
} elseif ($update_info && $update_info['update_requested']) {
$opnsense_update_requested = true;
// Clear the request flag so it is delivered once, and mark the firewall
// as updating.
//
// Deliberately no longer sets updates_available = 0 or
// reboot_required = 1 here. Those asserted an outcome at the moment the
// request was handed to the agent, before anything had run - so a
// request that never executed still left the server reporting "no
// updates available, reboot required", which is worse than saying
// nothing. Both are now set from what the agent actually reports.
$stmt = db()->prepare(
'UPDATE firewalls
SET update_requested = 0, status = \'updating\',
last_update_check = NULL, last_update_attempt_at = NOW()
WHERE id = ?'
);
$stmt->execute([$firewall_id]);
}
$response = [
'success' => true,
'message' => 'Check-in successful',
'checkin_interval' => $checkin_interval,
'server_time' => date('c')
];
// Include update check results if performed
if ($check_updates) {
$response['update_check_performed'] = true;
$response['updates_available'] = $updates_available ?? false;
}
// Include agent update information
if (!empty($agent_update_check['update_held'])) {
// Told, but not instructed: the agent must not act on this.
$response['agent_update_held'] = true;
$response['latest_version'] = $agent_update_check['latest_version'];
$response['agent_rollout_stage'] = $agent_update_check['rollout_stage'];
}
if ($agent_update_check['update_available']) {
$response['agent_update_available'] = true;
$response['latest_version'] = $agent_update_check['latest_version'];
if (isset($agent_update_check['download_url'])) {
$response['agent_download_url'] = $agent_update_check['download_url'];
}
if (isset($agent_update_check['update_command'])) {
$response['update_command'] = $agent_update_check['update_command'];
}
if (isset($agent_update_check['manual_reinstall_command'])) {
$response['agent_manual_reinstall_command'] = $agent_update_check['manual_reinstall_command'];
}
}
// Include OPNsense update command if requested
if ($opnsense_update_requested) {
$response['opnsense_update_requested'] = true;
$response['opnsense_update_command'] = '/usr/local/sbin/opnsense-update -bkp';
}
// Check for agent cleanup request
$stmt = db()->prepare('SELECT agent_cleanup_requested FROM firewalls WHERE id = ?');
$stmt->execute([$firewall_id]);
$cleanup_info = $stmt->fetch();
$agent_cleanup_requested = false;
if ($cleanup_info && $cleanup_info['agent_cleanup_requested']) {
$agent_cleanup_requested = true;
// Clear the cleanup request flag
$stmt = db()->prepare('UPDATE firewalls SET agent_cleanup_requested = 0 WHERE id = ?');
$stmt->execute([$firewall_id]);
// Provide the cleanup script URL, from this installation's configured
// address rather than the maintainer's host, which used to be typed in
// here and would have sent every self-hosted fleet's firewalls to fetch
// and execute a script from somebody else's server.
$cleanup_base = opnmgr_server_url();
$response['agent_cleanup_requested'] = true;
if ($cleanup_base === '') {
error_log('agent_checkin.php: server URL not configured; cannot send an agent cleanup URL');
} else {
$cleanup_url = $cleanup_base . '/downloads/cleanup_and_fix_agent.sh';
$response['agent_cleanup_url'] = $cleanup_url;
$response['agent_cleanup_command'] =
'fetch -o /tmp/cleanup_fix.sh ' . escapeshellarg($cleanup_url)
. ' && chmod +x /tmp/cleanup_fix.sh && /tmp/cleanup_fix.sh';
}
}
// Auto-setup of the reverse SSH tunnel used to happen here. It fetched
// setup_reverse_proxy.sh from a hardcoded host and piped it into sh on the
// firewall, once for every box whose tunnel had never been established.
// That script has never existed in this codebase or on the server, so the
// command downloaded an error page and executed it, and the queue filled
// with work that could only ever fail. Tunnels are established on demand
// through manage_ssh_tunnel.php, which is the path the UI actually uses.
// Check for queued custom commands
// Update agent only processes commands marked for update agent
if ($agent_type === 'update') {
$queued_commands = checkQueuedCommandsForUpdateAgent($firewall_id);
} else {
$queued_commands = checkQueuedCommands($firewall_id);
}
if (!empty($queued_commands)) {
$response['queued_commands'] = $queued_commands;
error_log("Agent checkin for firewall $firewall_id: Sending " . count($queued_commands) . " queued command(s)");
}
// Check for pending proxy requests
$stmt = db()->prepare('SELECT id, tunnel_port, client_id, method, path, headers, body FROM request_queue WHERE firewall_id = ? AND status = "pending" ORDER BY created_at ASC LIMIT 10');
$stmt->execute([$firewall_id]);
$pending_requests = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!empty($pending_requests)) {
$response['pending_requests'] = $pending_requests;
$response['pending_requests_count'] = count($pending_requests);
// Log that we're sending requests to agent
error_log("Agent checkin for firewall $firewall_id: Sending " . count($pending_requests) . " pending proxy requests");
}
// Hand the agent its API key and signing secret if it has not adopted them
// yet. This is what lets an already-deployed fleet upgrade from
// hardware_id-only authentication without a flag day. Once the agent sends
// the key back, authentication for this firewall fails closed without it.
if (!empty($agent_credentials)) {
$response = array_merge($response, $agent_credentials);
}
// Tell the agent what the server expects, so it can self-configure.
$response['auth_policy'] = [
'mode' => agent_auth_mode(),
'signature_window' => (int)agent_auth_setting('agent_signature_window', '300'),
'api_key_required' => (bool)($firewall['_auth']['confirmed'] ?? false),
'signature_required' => agent_auth_mode() === 'require_signed'
|| (agent_auth_mode() === 'prefer_signed' && (int)($firewall['agent_signing_supported'] ?? 0) === 1),
];
echo json_encode($response);
} catch (Exception $e) {
error_log("agent_checkin.php error for firewall_id=$firewall_id: " . $e->getMessage());
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Internal server error']);
}
/**
* Check if agent update is available
*/
function checkAgentUpdate($current_agent_version, $firewall_id) {
// Use centralized version constant from inc/agent_version.php
require_once __DIR__ . '/inc/agent_version.php';
$latest_agent_version = LATEST_AGENT_VERSION;
// pfSense runs a separate agent with its own version line and installer;
// offering it the OPNsense plugin would install an MVC plugin on a product
// that has no MVC framework.
$fw_row = platform_firewall((int)$firewall_id);
if ($fw_row && ($fw_row['agent_flavor'] ?? '') === 'pfsense-agent') {
return checkPfsenseAgentUpdate($current_agent_version, $fw_row);
}
// Clean version strings for comparison
$current_clean = preg_replace('/[^0-9.]/', '', $current_agent_version);
$latest_clean = preg_replace('/[^0-9.]/', '', $latest_agent_version);
// Get firewall hostname for self-healing, and its rollout pilot flag
$stmt = db()->prepare('SELECT hostname, agent_rollout_pilot FROM firewalls WHERE id = ?');
$stmt->execute([$firewall_id]);
$firewall = $stmt->fetch(PDO::FETCH_ASSOC);
$hostname = $firewall['hostname'] ?? 'unknown';
$is_pilot = (bool) ($firewall['agent_rollout_pilot'] ?? 0);
// Log version comparison for debugging
error_log("Agent version check: current='$current_clean' latest='$latest_clean' fw_id=$firewall_id");
if (version_compare($current_clean, $latest_clean, '<')) {
// A newer version exists. Whether this firewall is told to install it is
// a separate question, and used to not be asked at all: syncing
// AGENT_VERSION to production deployed it to the whole fleet within one
// check-in. The rollout stage is bound to a specific version, so a newly
// published one is held until somebody promotes it by name.
require_once __DIR__ . '/inc/agent_rollout.php';
if (!agent_rollout_allows($is_pilot)) {
$rollout = agent_rollout_state();
error_log(sprintf(
'agent_checkin.php: agent %s held from fw_id=%d (stage=%s%s)',
$latest_agent_version, $firewall_id, $rollout['stage'],
$rollout['superseded'] ? ', not yet promoted' : ''
));
// The manager still knows, and still shows it. Suppressing the offer
// must not mean hiding that an update exists.
return [
'update_available' => false,
'update_held' => true,
'rollout_stage' => $rollout['stage'],
'latest_version' => $latest_agent_version,
];
}
// Agent update is available. The host below was the maintainer's own,
// typed in here, so every install told its firewalls to fetch agent
// updates from a third party. It comes from configuration now; if that
// is not set we report no update rather than hand out a wrong URL.
$server_name = opnmgr_server_host();
if ($server_name === '') {
error_log('agent_checkin.php: server URL not configured; cannot offer an agent update');
return ['update_available' => false];
}
// Check if this is a plugin-based agent (v1.x)
if (strpos($current_clean, '1.') === 0) {
// Plugin-based agent - use plugin installer. The package's SHA-256
// goes with the command, as it does for a manual install, so the
// installer refuses a package that is not the verified, staged one.
// Auto-update used to omit it, and the installer then only warned.
require_once __DIR__ . '/inc/agent_package.php';
$ready = agent_package_readiness();
if (empty($ready['ok']) || !preg_match('/^[0-9a-f]{64}$/', (string)($ready['sha256'] ?? ''))) {
error_log('agent_checkin.php: agent package not verified/staged; not offering an update: ' . ($ready['error'] ?? 'unknown'));
return ['update_available' => false, 'latest_version' => $latest_agent_version];
}
$env = "env OPNMGR_BASE_URL=https://{$server_name} OPNMGR_AGENT_SHA256={$ready['sha256']}";
return [
'update_available' => true,
'latest_version' => $latest_agent_version,
'update_command' => 'fetch -o - ' . "https://{$server_name}/downloads/plugins/install_opnmanager_agent.sh"
. " | {$env} sh > /var/log/opnmanager_agent_update.log 2>&1 &",
'manual_reinstall_command' => 'fetch -o - ' . "https://{$server_name}/downloads/plugins/install_opnmanager_agent.sh"
. " | {$env} sh"
];
} elseif ($current_clean === '2.1.2') {
// Special self-healing for v2.1.2 agents
return [
'update_available' => true,
'latest_version' => $latest_agent_version,
'selfheal_required' => true,
'selfheal_url' => "https://{$server_name}/download_agent.php?selfheal=true&hostname={$hostname}&version={$current_agent_version}",
'download_url' => "https://{$server_name}/download_tunnel_agent.php?firewall_id={$firewall_id}",
'update_command' => 'fetch -o /tmp/selfheal_agent.sh ' . "\"https://{$server_name}/download_agent.php?selfheal=true&hostname={$hostname}&version={$current_agent_version}\"" . ' && chmod +x /tmp/selfheal_agent.sh && nohup /tmp/selfheal_agent.sh > /tmp/selfheal.log 2>&1 &',
'manual_reinstall_command' => buildAgentReinstallCommand($server_name, $firewall_id)
];
} else {
// Normal update for other versions (v2.x standalone agents)
return [
'update_available' => true,
'latest_version' => $latest_agent_version,
'download_url' => "https://{$server_name}/download_tunnel_agent.php?firewall_id={$firewall_id}",
'update_command' => 'fetch -o /tmp/update_agent.sh ' . "https://{$server_name}/download/update_agent.sh" . ' && chmod +x /tmp/update_agent.sh && nohup /tmp/update_agent.sh ' . "https://{$server_name}/download_tunnel_agent.php?firewall_id={$firewall_id}" . ' > /dev/null 2>&1 &',
'manual_reinstall_command' => buildAgentReinstallCommand($server_name, $firewall_id)
];
}
}
return [
'update_available' => false,
'latest_version' => $latest_agent_version
];
}
/**
* Check for queued commands for this firewall
*/
function checkQueuedCommands($firewall_id) {
try {
// A reboot cannot report back - the box stops executing partway through -
// so settle those first. Without this the timeout below hands the reboot
// straight back to the firewall that just performed it, on every check-in.
settle_unacknowledgeable_commands($firewall_id, 10);
// Reset commands stuck in 'sent' status for more than 10 minutes back to 'pending'
$timeout_stmt = db()->prepare("UPDATE firewall_commands SET status = 'pending', sent_at = NULL WHERE firewall_id = ? AND status = 'sent' AND sent_at < DATE_SUB(NOW(), INTERVAL 10 MINUTE) AND NOT " . agent_unacknowledgeable_command_sql());
$timeout_stmt->execute([$firewall_id]);
$reset_count = $timeout_stmt->rowCount();
if ($reset_count > 0) {
error_log("Reset $reset_count stuck command(s) for firewall $firewall_id back to pending");
}
// Get pending commands
$stmt = db()->prepare('SELECT id, command, description, action, is_raw FROM firewall_commands WHERE firewall_id = ? AND status = "pending" ORDER BY created_at ASC LIMIT 5');
$stmt->execute([$firewall_id]);
$commands = platform_filter_deliverable($firewall_id, $stmt->fetchAll(PDO::FETCH_ASSOC));
if (!empty($commands)) {
// Mark commands as sent
$command_ids = array_column($commands, 'id');
$placeholders = str_repeat('?,', count($command_ids) - 1) . '?';
$update_stmt = db()->prepare("UPDATE firewall_commands SET status = 'sent', sent_at = NOW() WHERE id IN ($placeholders)");
$update_stmt->execute($command_ids);
}
return $commands;
} catch (Exception $e) {
error_log("Failed to check queued commands: " . $e->getMessage());
return [];
}
}
/**
* Check for queued commands specifically for update agent
* Update agent only processes commands marked with is_update_command=1
*/
function checkQueuedCommandsForUpdateAgent($firewall_id) {
try {
// Same reasoning as checkQueuedCommands(): an update command that ends in
// a reboot never gets to answer, so settle it rather than reissuing it.
settle_unacknowledgeable_commands($firewall_id, 10);
// Reset commands stuck in 'sent' status for more than 10 minutes back to 'pending'
$timeout_stmt = db()->prepare("UPDATE firewall_commands SET status = 'pending', sent_at = NULL WHERE firewall_id = ? AND status = 'sent' AND is_update_command = 1 AND sent_at < DATE_SUB(NOW(), INTERVAL 10 MINUTE) AND NOT " . agent_unacknowledgeable_command_sql());
$timeout_stmt->execute([$firewall_id]);
$reset_count = $timeout_stmt->rowCount();
if ($reset_count > 0) {
error_log("Reset $reset_count stuck update agent command(s) for firewall $firewall_id back to pending");
}
// Get pending commands marked for update agent
$stmt = db()->prepare('SELECT id, command, description, action, is_raw FROM firewall_commands WHERE firewall_id = ? AND status = "pending" AND is_update_command = 1 ORDER BY created_at ASC LIMIT 5');
$stmt->execute([$firewall_id]);
$commands = platform_filter_deliverable($firewall_id, $stmt->fetchAll(PDO::FETCH_ASSOC));
if (!empty($commands)) {
// Mark commands as sent
$command_ids = array_column($commands, 'id');
$placeholders = str_repeat('?,', count($command_ids) - 1) . '?';
$update_stmt = db()->prepare("UPDATE firewall_commands SET status = 'sent', sent_at = NOW() WHERE id IN ($placeholders)");
$update_stmt->execute($command_ids);
}
return $commands;
} catch (Exception $e) {
error_log("Failed to check queued commands for update agent: " . $e->getMessage());
return [];
}
}
/**
* Process WAN interface statistics from agent v3.4.0+
* Updates the firewall_wan_interfaces table with detailed interface stats
*/
function processWANInterfaceStats($firewall_id, $wan_interface_stats_json) {
if (empty($wan_interface_stats_json)) {
return;
}
// Parse JSON stats
$stats = json_decode($wan_interface_stats_json, true);
if (!is_array($stats) || empty($stats)) {
return;
}
try {
foreach ($stats as $iface_data) {
$interface = $iface_data['interface'] ?? '';
if (empty($interface)) {
continue;
}
// Prepare data for insertion/update
$status = $iface_data['status'] ?? 'unknown';
$ip_address = $iface_data['ip_address'] ?? '';
$netmask = $iface_data['netmask'] ?? '';
$gateway = $iface_data['gateway'] ?? '';
$media = $iface_data['media'] ?? '';
$rx_packets = (int)($iface_data['rx_packets'] ?? 0);
$rx_bytes = (int)($iface_data['rx_bytes'] ?? 0);
$rx_errors = (int)($iface_data['rx_errors'] ?? 0);
$tx_packets = (int)($iface_data['tx_packets'] ?? 0);
$tx_bytes = (int)($iface_data['tx_bytes'] ?? 0);
$tx_errors = (int)($iface_data['tx_errors'] ?? 0);
// Insert or update interface stats
$stmt = db()->prepare('
INSERT INTO firewall_wan_interfaces
(firewall_id, interface_name, status, ip_address, netmask, gateway, media,
rx_packets, rx_bytes, rx_errors, tx_packets, tx_bytes, tx_errors, last_updated)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())
ON DUPLICATE KEY UPDATE
status = VALUES(status),
ip_address = VALUES(ip_address),
netmask = VALUES(netmask),
gateway = VALUES(gateway),
media = VALUES(media),
rx_packets = VALUES(rx_packets),
rx_bytes = VALUES(rx_bytes),
rx_errors = VALUES(rx_errors),
tx_packets = VALUES(tx_packets),
tx_bytes = VALUES(tx_bytes),
tx_errors = VALUES(tx_errors),
last_updated = NOW()
');
$stmt->execute([
$firewall_id, $interface, $status, $ip_address, $netmask, $gateway, $media,
$rx_packets, $rx_bytes, $rx_errors, $tx_packets, $tx_bytes, $tx_errors
]);
}
} catch (Exception $e) {
error_log("Failed to process WAN interface stats for firewall $firewall_id: " . $e->getMessage());
}
}
/**
* Drop, and close out, queued commands this firewall's platform must not run.
*
* This is the gate every command passes, including the ones legacy endpoints
* insert into firewall_commands directly with OPNsense shell text in them. A
* refused command is marked failed with the reason, not left pending, so the
* operator sees why and it is never retried onto the wrong product.
*
* @return array The deliverable commands, as id/command/description rows
*/
function platform_filter_deliverable(int $firewall_id, array $commands): array {
if (!$commands) {
return [];
}
$fw = platform_firewall($firewall_id);
$out = [];
foreach ($commands as $cmd) {
$verdict = $fw ? platform_delivery_verdict($fw, $cmd) : ['deliver' => false, 'reason' => 'Firewall not found'];
if ($verdict['deliver']) {
$out[] = ['id' => $cmd['id'], 'command' => $cmd['command'], 'description' => $cmd['description']];
continue;
}
db()->prepare(
"UPDATE firewall_commands SET status = 'failed', completed_at = NOW(), result = ?
WHERE id = ? AND status = 'pending'"
)->execute([$verdict['reason'], $cmd['id']]);
audit_log_agent('command.refused', $firewall_id, [
'success' => false,
'object_type' => 'command',
'object_id' => (string)$cmd['id'],
'message' => substr($verdict['reason'], 0, 500),
'metadata' => ['action' => $cmd['action'] ?? null, 'platform' => $fw['platform_family'] ?? null],
]);
}
return $out;
}
/**