Project

General

Profile

Download (16.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * status.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://neon1.net/m0n0wall)
12
 * Copyright (c) 2003 Jim McBeath <jimmc@macrovision.com>
13
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
14
 * All rights reserved.
15
 *
16
 * Licensed under the Apache License, Version 2.0 (the "License");
17
 * you may not use this file except in compliance with the License.
18
 * You may obtain a copy of the License at
19
 *
20
 * http://www.apache.org/licenses/LICENSE-2.0
21
 *
22
 * Unless required by applicable law or agreed to in writing, software
23
 * distributed under the License is distributed on an "AS IS" BASIS,
24
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25
 * See the License for the specific language governing permissions and
26
 * limitations under the License.
27
 */
28

    
29
##|+PRIV
30
##|*IDENT=page-hidden-detailedstatus
31
##|*NAME=Hidden: Detailed Status
32
##|*DESCR=Allow access to the 'Hidden: Detailed Status' page.
33
##|*MATCH=status.php*
34
##|-PRIV
35

    
36
/* Execute a command, with a title, and generate an HTML table
37
 * showing the results.
38
 */
39

    
40
global $console;
41
global $show_output;
42

    
43
$console = false;
44
$show_output = !isset($_GET['archiveonly']);
45

    
46
if ((php_sapi_name() == 'cli') || (defined('STDIN'))) {
47
	/* Running from console/shell, not web */
48
	$console = true;
49
	$show_output = false;
50
	parse_str($argv[1], $_GET);
51
}
52

    
53
/* include all configuration functions */
54
if ($console) {
55
	require_once("config.inc");
56
} else {
57
	require_once("guiconfig.inc");
58
}
59
require_once("functions.inc");
60
require_once("gwlb.inc");
61
$output_path = "/tmp/status_output/";
62
$output_file = "/tmp/status_output.tgz";
63

    
64
$filtered_tags = array(
65
	'accountkey', 'authorizedkeys', 'auth_pass', 'auth_user',
66
	'barnyard_dbpwd', 'bcrypt-hash', 'cert_key', 'crypto_password',
67
	'crypto_password2', 'dns_nsupdatensupdate_key', 'encryption_password',
68
	'etpro_code', 'etprocode', 'gold_encryption_password', 'gold_password',
69
	'influx_pass', 'ipsecpsk', 'ldap_bindpw', 'ldapbindpass', 'ldap_pass',
70
	'lighttpd_ls_password',	'md5-hash', 'md5password', 'md5sigkey',	'md5sigpass', 
71
	'nt-hash', 'oinkcode', 'oinkmastercode', 'passphrase', 'password', 
72
	'passwordagain', 'pkcs11pin', 'postgresqlpasswordenc', 'pre-shared-key',
73
	'proxypass', 'proxy_passwd', 'proxyuser', 'proxy_user', 'prv',
74
	'radius_secret', 'redis_password', 'redis_passwordagain', 'rocommunity',
75
	'secret', 'shared_key', 'tls', 'tlspskidentity', 'tlspskfile',
76
	'varclientpasswordinput', 'varclientsharedsecret', 'varsyncpassword',
77
	'varusersmotpinitsecret', 'varusersmotppin'
78
);
79

    
80
if ($_POST['submit'] == "DOWNLOAD" && file_exists($output_file)) {
81
	session_cache_limiter('public');
82
	send_user_download('file', $output_file);
83
}
84

    
85
if (is_dir($output_path)) {
86
	unlink_if_exists("{$output_path}/*");
87
	@rmdir($output_path);
88
}
89
unlink_if_exists($output_file);
90
mkdir($output_path);
91

    
92
function doCmdT($title, $command, $method) {
93
	global $output_path, $output_file, $filtered_tags, $show_output;
94
	/* Fixup output directory */
95

    
96
	if ($show_output) {
97
		$rubbish = array('|', '-', '/', '.', ' ');  /* fixes the <a> tag to be W3C compliant */
98
		echo "\n<a name=\"" . str_replace($rubbish, '', $title) . "\" id=\"" . str_replace($rubbish, '', $title) . "\"></a>\n";
99
		print('<div class="panel panel-default">');
100
		print('<div class="panel-heading"><h2 class="panel-title">' . $title . '</h2></div>');
101
		print('<div class="panel-body">');
102
		print('<pre>');
103
	}
104

    
105
	if ($command == "dumpconfigxml") {
106
		$ofd = @fopen("{$output_path}/config-sanitized.xml", "w");
107
		$fd = @fopen("/conf/config.xml", "r");
108
		if ($fd) {
109
			while (!feof($fd)) {
110
				$line = fgets($fd);
111
				/* remove sensitive contents */
112
				foreach ($filtered_tags as $tag) {
113
					$line = preg_replace("/<{$tag}>.*?<\\/{$tag}>/", "<{$tag}>xxxxx</{$tag}>", $line);
114
				}
115
				if ($show_output) {
116
					echo htmlspecialchars(str_replace("\t", "    ", $line), ENT_NOQUOTES);
117
				}
118
				fwrite($ofd, $line);
119
			}
120
		}
121
		fclose($fd);
122
		fclose($ofd);
123
	} else {
124
		$execOutput = "";
125
		$execStatus = "";
126
		$fn = "{$output_path}/{$title}.txt";
127
		if ($method == "exec") {
128
			exec($command . " > " . escapeshellarg($fn) . " 2>&1", $execOutput, $execStatus);
129
			if ($show_output) {
130
				$ofd = @fopen($fn, "r");
131
				if ($ofd) {
132
					while (!feof($ofd)) {
133
						echo htmlspecialchars(fgets($ofd), ENT_NOQUOTES);
134
					}
135
				}
136
				fclose($ofd);
137
			}
138
		} elseif ($method == "php_func") {
139
			$execOutput = $command();
140
			if ($show_output) {
141
				echo htmlspecialchars($execOutput, ENT_NOQUOTES);
142
			}
143
			file_put_contents($fn, $execOutput);
144
		}
145
	}
146

    
147
	if ($show_output) {
148
		print('</pre>');
149
		print('</div>');
150
		print('</div>');
151
	}
152
}
153

    
154
/* Define a command, with a title, to be executed later. */
155
function defCmdT($title, $command, $method = "exec") {
156
	global $commands;
157
	$title = htmlspecialchars($title, ENT_NOQUOTES);
158
	$commands[] = array($title, $command, $method);
159
}
160

    
161
/* List all of the commands as an index. */
162
function listCmds() {
163
	global $currentDate;
164
	global $commands;
165

    
166
	$rubbish = array('|', '-', '/', '.', ' ');	/* fixes the <a> tag to be W3C compliant */
167

    
168
	print('<div class="panel panel-default">');
169
	print('<div class="panel-heading"><h2 class="panel-title">' . sprintf(gettext("Firewall Status on %s"), $currentDate) . '</h2></div>');
170
	print('<div class="panel-body">');
171
	print('    <div class="content">');
172
	print("\n<p>" . gettext("This status page includes the following information") . ":\n");
173
	print("<ul>\n");
174
	for ($i = 0; isset($commands[$i]); $i++) {
175
		print("\t<li><strong><a href=\"#" . str_replace($rubbish, '', $commands[$i][0]) . "\">" . $commands[$i][0] . "</a></strong></li>\n");
176
	}
177

    
178
	print("</ul>\n");
179
	print('	       </div>');
180
	print('	   </div>');
181
	print('</div>');
182
}
183

    
184
/* Execute all of the commands which were defined by a call to defCmd. */
185
function execCmds() {
186
	global $commands;
187
	for ($i = 0; isset($commands[$i]); $i++) {
188
		doCmdT($commands[$i][0], $commands[$i][1], $commands[$i][2]);
189
	}
190
}
191

    
192
function get_firewall_info() {
193
	global $g, $output_path;
194
	/* Firewall Platform/Serial */
195
	$firewall_info = "Product Name: " . htmlspecialchars($g['product_name']);
196
	$platform = system_identify_specific_platform();
197
	if (!empty($platform['descr'])) {
198
		$firewall_info .= "<br/>Platform: " . htmlspecialchars($platform['descr']);
199
	}
200

    
201
	if (file_exists('/var/db/uniqueid')) {
202
		$ngid = file_get_contents('/var/db/uniqueid');
203
		if (!empty($ngid)) {
204
			$firewall_info .= "<br/>Netgate Device ID: " . htmlspecialchars($ngid);
205
		}
206
	}
207

    
208
	if (function_exists("system_get_thothid") &&
209
	    (php_uname("m") == "arm64")) {
210
		$thothid = system_get_thothid();
211
		if (!empty($thothid)) {
212
			$firewall_info .= "<br/>Netgate Crypto ID: " . htmlspecialchars(chop($thothid));
213
		}
214
	}
215

    
216
	$serial = system_get_serial();
217
	if (!empty($serial)) {
218
		$firewall_info .= "<br/>Serial: " . htmlspecialchars($serial);
219
	}
220

    
221
	if (!empty($g['product_version_string'])) {
222
		$firewall_info .= "<br/>" . htmlspecialchars($g['product_name']) .
223
		    " version: " . htmlspecialchars($g['product_version_string']);
224
	}
225

    
226
	if (file_exists('/etc/version.buildtime')) {
227
		$build_time = file_get_contents('/etc/version.buildtime');
228
		if (!empty($build_time)) {
229
			$firewall_info .= "<br/>Built On: " . htmlspecialchars($build_time);
230
		}
231
	}
232
	if (file_exists('/etc/version.lastcommit')) {
233
		$build_commit = file_get_contents('/etc/version.lastcommit');
234
		if (!empty($build_commit)) {
235
			$firewall_info .= "<br/>Last Commit: " . htmlspecialchars($build_commit);
236
		}
237
	}
238

    
239
	if (file_exists('/etc/version.gitsync')) {
240
		$gitsync = file_get_contents('/etc/version.gitsync');
241
		if (!empty($gitsync)) {
242
			$firewall_info .= "<br/>A gitsync was performed at " .
243
			    date("D M j G:i:s T Y", filemtime('/etc/version.gitsync')) .
244
			    " to commit " . htmlspecialchars($gitsync);
245
		}
246
	}
247

    
248
	file_put_contents("{$output_path}/Product-Info.txt", str_replace("<br/>", "\n", $firewall_info) . "\n");
249
	return $firewall_info;
250
}
251

    
252
function get_gateway_status() {
253
	return return_gateways_status_text(true, false);
254
}
255

    
256
global $g, $config;
257

    
258
/* Set up all of the commands we want to execute. */
259

    
260
/* OS stats/info */
261
if (function_exists("system_get_thothid") &&
262
    (php_uname("m") == "arm64")) {
263
	$thothid = system_get_thothid();
264
	if (!empty($thothid)) {
265
		defCmdT("Product-Public Key", "/usr/local/sbin/ping-auth -p");
266
	}
267
}
268

    
269
defCmdT("OS-Uptime", "/usr/bin/uptime");
270
defCmdT("Network-Interfaces", "/sbin/ifconfig -vvvvvam");
271
defCmdT("Network-Interface Statistics", "/usr/bin/netstat -nWi");
272
defCmdT("Process-Top Usage", "/usr/bin/top | /usr/bin/head -n5");
273
defCmdT("Process-List", "/bin/ps xauwwd");
274
defCmdT("Disk-Mounted Filesystems", "/sbin/mount");
275
defCmdT("Disk-Free Space", "/bin/df -hi");
276
defCmdT("Network-Routing tables", "/usr/bin/netstat -nWr");
277
defCmdT("Network-Gateway Status", 'get_gateway_status', "php_func");
278
defCmdT("Network-Mbuf Usage", "/usr/bin/netstat -mb");
279
defCmdT("Network-Protocol Statistics", "/usr/bin/netstat -s");
280
defCmdT("Network-Buffer and Timer Statistics", "/usr/bin/netstat -nWx");
281
defCmdT("Network-Listen Queues", "/usr/bin/netstat -LaAn");
282
defCmdT("Network-Sockets", "/usr/bin/sockstat");
283
defCmdT("Network-ARP Table", "/usr/sbin/arp -an");
284
defCmdT("Network-NDP Table", "/usr/sbin/ndp -na");
285
defCmdT("OS-Kernel Modules", "/sbin/kldstat -v");
286
defCmdT("OS-Kernel VMStat", "/usr/bin/vmstat -afimsz");
287

    
288
/* If a device has a switch, put the switch configuration in the status output */
289
if (file_exists("/dev/etherswitch0")) {
290
	defCmdT("Network-Switch Configuration", "/sbin/etherswitchcfg -f /dev/etherswitch0 info");
291
}
292

    
293
/* Firewall rules and info */
294
defCmdT("Firewall-Generated Ruleset", "/bin/cat {$g['tmp_path']}/rules.debug");
295
defCmdT("Firewall-Generated Ruleset Limiters", "/bin/cat {$g['tmp_path']}/rules.limiter");
296
defCmdT("Firewall-Generated Ruleset Limits", "/bin/cat {$g['tmp_path']}/rules.limits");
297
defCmdT("Firewall-pf NAT Rules", "/sbin/pfctl -vvsn");
298
defCmdT("Firewall-pf Firewall Rules", "/sbin/pfctl -vvsr");
299
defCmdT("Firewall-pf Tables", "/sbin/pfctl -vs Tables");
300
defCmdT("Firewall-pf State Table Contents", "/sbin/pfctl -vvss");
301
defCmdT("Firewall-pf Info", "/sbin/pfctl -si");
302
defCmdT("Firewall-pf Show All", "/sbin/pfctl -sa");
303
defCmdT("Firewall-pf Queues", "/sbin/pfctl -s queue -v");
304
defCmdT("Firewall-pf OSFP", "/sbin/pfctl -s osfp");
305
defCmdT("Firewall-pftop Default", "/usr/local/sbin/pftop -a -b");
306
defCmdT("Firewall-pftop Long", "/usr/local/sbin/pftop -w 150 -a -b -v long");
307
defCmdT("Firewall-pftop Queue", "/usr/local/sbin/pftop -w 150 -a -b -v queue");
308
defCmdT("Firewall-pftop Rules", "/usr/local/sbin/pftop -w 150 -a -b -v rules");
309
defCmdT("Firewall-pftop Size", "/usr/local/sbin/pftop -w 150 -a -b -v size");
310
defCmdT("Firewall-pftop Speed", "/usr/local/sbin/pftop -w 150 -a -b -v speed");
311
defCmdT("Firewall-IPFW Rules for Captive Portal", "/sbin/ipfw show");
312
defCmdT("Firewall-IPFW Limiter Info", "/sbin/ipfw pipe show");
313
defCmdT("Firewall-IPFW Queue Info", "/sbin/ipfw queue show");
314
defCmdT("Firewall-IPFW Tables", "/sbin/ipfw table all list");
315

    
316
/* Configuration Files */
317
defCmdT("Disk-Contents of var run", "/bin/ls /var/run");
318
defCmdT("Disk-Contents of conf", "/bin/ls /conf");
319
defCmdT("config.xml", "dumpconfigxml");
320
defCmdT("DNS-Resolution Configuration", "/bin/cat /etc/resolv.conf");
321
defCmdT("DHCP-IPv4 Configuration", "/bin/cat /var/dhcpd/etc/dhcpd.conf");
322
defCmdT("DHCP-IPv6-Configuration", "/bin/cat /var/dhcpd/etc/dhcpdv6.conf");
323
defCmdT("IPsec-strongSwan Configuration", '/usr/bin/sed "s/\([[:blank:]]secret = \).*/\1<redacted>/" /var/etc/ipsec/strongswan.conf');
324
defCmdT("IPsec-Configuration", '/usr/bin/sed "s/\([[:blank:]]secret = \).*/\1<redacted>/" /var/etc/ipsec/swanctl.conf');
325
defCmdT("IPsec-Status-Statistics", "/usr/local/sbin/swanctl --stats --pretty");
326
defCmdT("IPsec-Status-Connections", "/usr/local/sbin/swanctl --list-conns");
327
defCmdT("IPsec-Status-Active SAs", "/usr/local/sbin/swanctl --list-sas");
328
defCmdT("IPsec-Status-Policies", "/usr/local/sbin/swanctl --list-pols");
329
defCmdT("IPsec-Status-Certificates", "/usr/local/sbin/swanctl --list-certs --utc");
330
defCmdT("IPsec-Status-Pools", "/usr/local/sbin/swanctl --list-pools --leases");
331
defCmdT("IPsec-SPD", "/sbin/setkey -DP");
332
defCmdT("IPsec-SAD", "/sbin/setkey -D");
333
if (file_exists("/cf/conf/upgrade_log.txt")) {
334
	defCmdT("OS-Upgrade Log", "/bin/cat /cf/conf/upgrade_log.txt");
335
}
336
if (file_exists("/cf/conf/upgrade_log.latest.txt")) {
337
	defCmdT("OS-Upgrade Log Latest", "/bin/cat /cf/conf/upgrade_log.latest.txt");
338
}
339
if (file_exists("/boot/loader.conf")) {
340
	defCmdT("OS-Boot Loader Configuration", "/bin/cat /boot/loader.conf");
341
}
342
if (file_exists("/boot/loader.conf.local")) {
343
	defCmdT("OS-Boot Loader Configuration (Local)", "/bin/cat /boot/loader.conf.local");
344
}
345
if (file_exists("/var/etc/filterdns.conf")) {
346
	defCmdT("DNS-filterdns Daemon Configuration", "/bin/cat /var/etc/filterdns.conf");
347
}
348

    
349
if (is_dir("/var/etc/openvpn")) {
350
	foreach(glob('/var/etc/openvpn/*/config.ovpn') as $file) {
351
		$ovpnfile = explode('/', $file);
352
		if (!count($ovpnfile) || (count($ovpnfile) < 6)) {
353
			continue;
354
		}
355
		defCmdT("OpenVPN-Configuration {$ovpnfile[4]}", "/bin/cat " . escapeshellarg($file));
356
	}
357
}
358

    
359
/* Logs */
360
function status_add_log($name, $logfile, $number = 1000) {
361
	if (!file_exists($logfile)) {
362
		return;
363
	}
364
	$descr = "Log-{$name}";
365
	$tail = '';
366
	if ($number != "all") {
367
		$descr .= "-Last {$number} entries";
368
		$tail = ' | tail -n ' . escapeshellarg($number);
369
	}
370
	defCmdT($descr, system_log_get_cat() . ' ' . sort_related_log_files($logfile, true, true) . $tail);
371
}
372

    
373
status_add_log("System", '/var/log/system.log');
374
status_add_log("DHCP", '/var/log/dhcpd.log');
375
status_add_log("Filter", '/var/log/filter.log');
376
status_add_log("Gateways", '/var/log/gateways.log');
377
status_add_log("IPsec", '/var/log/ipsec.log');
378
status_add_log("L2TP", '/var/log/l2tps.log');
379
status_add_log("NTP", '/var/log/ntpd.log');
380
status_add_log("OpenVPN", '/var/log/openvpn.log');
381
status_add_log("Captive Portal Authentication", '/var/log/portalauth.log');
382
status_add_log("PPP", '/var/log/ppp.log');
383
status_add_log("PPPoE Server", '/var/log/poes.log');
384
status_add_log("DNS", '/var/log/resolver.log');
385
status_add_log("Routing", '/var/log/routing.log');
386
status_add_log("Wireless", '/var/log/wireless.log');
387
status_add_log("PHP Errors", '/tmp/PHP_errors.log', 'all');
388

    
389
defCmdT("OS-Message Buffer", "/sbin/dmesg -a");
390
defCmdT("OS-Message Buffer (Boot)", "/bin/cat /var/log/dmesg.boot");
391

    
392
/* OS/Hardware Status */
393
defCmdT("OS-sysctl values", "/sbin/sysctl -aq");
394
defCmdT("OS-Kernel Environment", "/bin/kenv");
395
defCmdT("OS-Kernel Memory Usage", "/usr/local/sbin/kmemusage.sh");
396
defCmdT("OS-Installed Packages", "/usr/local/sbin/pkg-static info");
397
defCmdT("OS-Package Manager Configuration", "/usr/local/sbin/pkg-static -vv");
398
defCmdT("Hardware-PCI Devices", "/usr/sbin/pciconf -lvb");
399
defCmdT("Hardware-USB Devices", "/usr/sbin/usbconfig dump_device_desc");
400

    
401
if (is_module_loaded("zfs.ko")) {
402
	defCmdT("Disk-ZFS List", "/sbin/zfs list");
403
	defCmdT("Disk-ZFS Properties", "/sbin/zfs get all");
404
	defCmdT("Disk-ZFS Pool List", "/sbin/zpool list");
405
	defCmdT("Disk-ZFS Pool Status", "/sbin/zpool status");
406
}
407
defCmdT("Disk-GEOM Mirror Status", "/sbin/gmirror status");
408

    
409
exec("/bin/date", $dateOutput, $dateStatus);
410
$currentDate = $dateOutput[0];
411

    
412
$pgtitle = array($g['product_name'], "Status");
413

    
414
if (!$console):
415
include("head.inc"); ?>
416

    
417
<form action="status.php" method="post">
418

    
419
<?php print_info_box(
420
	gettext("Make sure all sensitive information is removed! (Passwords, etc.) before posting information from this page in public places such as forum or social media sites.") .
421
	'<br />' .
422
	gettext("Common password and other private fields in config.xml have been automatically redacted.") .
423
	'<br />' .
424
	sprintf(gettext('When the page has finished loading, the output is stored in %1$s. It may be downloaded via scp or using this button: '), $output_file) .
425
	' <button name="submit" type="submit" class="btn btn-primary btn-sm" id="download" value="DOWNLOAD">' .
426
	'<i class="fa fa-download icon-embed-btn"></i>' .
427
	gettext("Download") .
428
	'</button>'); ?>
429

    
430
</form>
431

    
432
<?php print_info_box(get_firewall_info(), 'info', false);
433

    
434
if ($show_output) {
435
	listCmds();
436
} else {
437
	print_info_box(gettext("Status output suppressed. Download archive to view."), 'info', false);
438
}
439

    
440
endif;
441

    
442
if ($console) {
443
	print(gettext("Gathering status data...") . "\n");
444
	get_firewall_info();
445
}
446
execCmds();
447

    
448
print(gettext("Saving output to archive..."));
449

    
450
if (is_dir($output_path)) {
451
	mwexec("/usr/bin/tar czpf " . escapeshellarg($output_file) . " -C " . escapeshellarg(dirname($output_path)) . " " . escapeshellarg(basename($output_path)));
452

    
453
	if (!isset($_GET["nocleanup"])) {
454
		unlink_if_exists("{$output_path}/*");
455
		@rmdir($output_path);
456
	}
457
}
458

    
459
print(gettext("Done.") . "\n");
460

    
461
if (!$console) {
462
	include("foot.inc");
463
}
(150-150/227)