Project

General

Profile

Download (17.1 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', 'varsqlconfpassword',
77
	'varsqlconf2password', 'varsyncpassword', 'varmodulesldappassword', 
78
	'varmodulesldap2password', 'varusersmotpinitsecret', 'varusersmotppin', 
79
	'varuserspassword', 'webrootftppassword'
80
);
81

    
82
$acme_filtered_tags = array('key', 'password', 'secret', 'token', 'pwd', 'pw');
83

    
84
if ($_POST['submit'] == "DOWNLOAD" && file_exists($output_file)) {
85
	session_cache_limiter('public');
86
	send_user_download('file', $output_file);
87
}
88

    
89
if (is_dir($output_path)) {
90
	unlink_if_exists("{$output_path}/*");
91
	@rmdir($output_path);
92
}
93
unlink_if_exists($output_file);
94
mkdir($output_path);
95

    
96
function doCmdT($title, $command, $method) {
97
	global $output_path, $output_file, $filtered_tags, $acme_filtered_tags, $show_output;
98
	/* Fixup output directory */
99

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

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

    
155
	if ($show_output) {
156
		print('</pre>');
157
		print('</div>');
158
		print('</div>');
159
	}
160
}
161

    
162
/* Define a command, with a title, to be executed later. */
163
function defCmdT($title, $command, $method = "exec") {
164
	global $commands;
165
	$title = htmlspecialchars($title, ENT_NOQUOTES);
166
	$commands[] = array($title, $command, $method);
167
}
168

    
169
/* List all of the commands as an index. */
170
function listCmds() {
171
	global $currentDate;
172
	global $commands;
173

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

    
176
	print('<div class="panel panel-default">');
177
	print('<div class="panel-heading"><h2 class="panel-title">' . sprintf(gettext("Firewall Status on %s"), $currentDate) . '</h2></div>');
178
	print('<div class="panel-body">');
179
	print('    <div class="content">');
180
	print("\n<p>" . gettext("This status page includes the following information") . ":\n");
181
	print("<ul>\n");
182
	for ($i = 0; isset($commands[$i]); $i++) {
183
		print("\t<li><strong><a href=\"#" . str_replace($rubbish, '', $commands[$i][0]) . "\">" . $commands[$i][0] . "</a></strong></li>\n");
184
	}
185

    
186
	print("</ul>\n");
187
	print('	       </div>');
188
	print('	   </div>');
189
	print('</div>');
190
}
191

    
192
/* Execute all of the commands which were defined by a call to defCmd. */
193
function execCmds() {
194
	global $commands;
195
	for ($i = 0; isset($commands[$i]); $i++) {
196
		doCmdT($commands[$i][0], $commands[$i][1], $commands[$i][2]);
197
	}
198
}
199

    
200
function get_firewall_info() {
201
	global $g, $output_path;
202
	/* Firewall Platform/Serial */
203
	$firewall_info = "Product Name: " . htmlspecialchars($g['product_name']);
204
	$platform = system_identify_specific_platform();
205
	if (!empty($platform['descr'])) {
206
		$firewall_info .= "<br/>Platform: " . htmlspecialchars($platform['descr']);
207
	}
208

    
209
	if (file_exists('/var/db/uniqueid')) {
210
		$ngid = file_get_contents('/var/db/uniqueid');
211
		if (!empty($ngid)) {
212
			$firewall_info .= "<br/>Netgate Device ID: " . htmlspecialchars($ngid);
213
		}
214
	}
215

    
216
	if (function_exists("system_get_thothid") &&
217
	    (php_uname("m") == "arm64")) {
218
		$thothid = system_get_thothid();
219
		if (!empty($thothid)) {
220
			$firewall_info .= "<br/>Netgate Crypto ID: " . htmlspecialchars(chop($thothid));
221
		}
222
	}
223

    
224
	$serial = system_get_serial();
225
	if (!empty($serial)) {
226
		$firewall_info .= "<br/>Serial: " . htmlspecialchars($serial);
227
	}
228

    
229
	if (!empty($g['product_version_string'])) {
230
		$firewall_info .= "<br/>" . htmlspecialchars($g['product_name']) .
231
		    " version: " . htmlspecialchars($g['product_version_string']);
232
	}
233

    
234
	if (file_exists('/etc/version.buildtime')) {
235
		$build_time = file_get_contents('/etc/version.buildtime');
236
		if (!empty($build_time)) {
237
			$firewall_info .= "<br/>Built On: " . htmlspecialchars($build_time);
238
		}
239
	}
240
	if (file_exists('/etc/version.lastcommit')) {
241
		$build_commit = file_get_contents('/etc/version.lastcommit');
242
		if (!empty($build_commit)) {
243
			$firewall_info .= "<br/>Last Commit: " . htmlspecialchars($build_commit);
244
		}
245
	}
246

    
247
	if (file_exists('/etc/version.gitsync')) {
248
		$gitsync = file_get_contents('/etc/version.gitsync');
249
		if (!empty($gitsync)) {
250
			$firewall_info .= "<br/>A gitsync was performed at " .
251
			    date("D M j G:i:s T Y", filemtime('/etc/version.gitsync')) .
252
			    " to commit " . htmlspecialchars($gitsync);
253
		}
254
	}
255

    
256
	file_put_contents("{$output_path}/Product-Info.txt", str_replace("<br/>", "\n", $firewall_info) . "\n");
257
	return $firewall_info;
258
}
259

    
260
function get_gateway_status() {
261
	return return_gateways_status_text(true, false);
262
}
263

    
264
global $g, $config;
265

    
266
/* Set up all of the commands we want to execute. */
267

    
268
/* OS stats/info */
269
if (function_exists("system_get_thothid") &&
270
    (php_uname("m") == "arm64")) {
271
	$thothid = system_get_thothid();
272
	if (!empty($thothid)) {
273
		defCmdT("Product-Public Key", "/usr/local/sbin/ping-auth -p");
274
	}
275
}
276

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

    
296
/* If a device has a switch, put the switch configuration in the status output */
297
if (file_exists("/dev/etherswitch0")) {
298
	defCmdT("Network-Switch Configuration", "/sbin/etherswitchcfg -f /dev/etherswitch0 info");
299
}
300

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

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

    
357
if (is_dir("/var/etc/openvpn")) {
358
	foreach(glob('/var/etc/openvpn/*/config.ovpn') as $file) {
359
		$ovpnfile = explode('/', $file);
360
		if (!count($ovpnfile) || (count($ovpnfile) < 6)) {
361
			continue;
362
		}
363
		defCmdT("OpenVPN-Configuration {$ovpnfile[4]}", "/bin/cat " . escapeshellarg($file));
364
	}
365
}
366

    
367
if (file_exists("/var/etc/l2tp-vpn/mpd.conf")) {
368
	defCmdT("L2TP-Configuration", '/usr/bin/sed -E "s/([[:blank:]](secret|radius server .*) ).*/\1<redacted>/" /var/etc/l2tp-vpn/mpd.conf');
369
}
370

    
371
/* Logs */
372
function status_add_log($name, $logfile, $number = 1000) {
373
	if (!file_exists($logfile)) {
374
		return;
375
	}
376
	$descr = "Log-{$name}";
377
	$tail = '';
378
	if ($number != "all") {
379
		$descr .= "-Last {$number} entries";
380
		$tail = ' | tail -n ' . escapeshellarg($number);
381
	}
382
	defCmdT($descr, system_log_get_cat() . ' ' . sort_related_log_files($logfile, true, true) . $tail);
383
}
384

    
385
status_add_log("System", '/var/log/system.log');
386
status_add_log("DHCP", '/var/log/dhcpd.log');
387
status_add_log("Filter", '/var/log/filter.log');
388
status_add_log("Gateways", '/var/log/gateways.log');
389
status_add_log("IPsec", '/var/log/ipsec.log');
390
status_add_log("L2TP", '/var/log/l2tps.log');
391
status_add_log("NTP", '/var/log/ntpd.log');
392
status_add_log("OpenVPN", '/var/log/openvpn.log');
393
status_add_log("Captive Portal Authentication", '/var/log/portalauth.log');
394
status_add_log("PPP", '/var/log/ppp.log');
395
status_add_log("PPPoE Server", '/var/log/poes.log');
396
status_add_log("DNS", '/var/log/resolver.log');
397
status_add_log("Routing", '/var/log/routing.log');
398
status_add_log("Wireless", '/var/log/wireless.log');
399
status_add_log("PHP Errors", '/tmp/PHP_errors.log', 'all');
400

    
401
defCmdT("OS-Message Buffer", "/sbin/dmesg -a");
402
defCmdT("OS-Message Buffer (Boot)", "/bin/cat /var/log/dmesg.boot");
403

    
404
/* OS/Hardware Status */
405
defCmdT("OS-sysctl values", "/sbin/sysctl -aq");
406
defCmdT("OS-Kernel Environment", "/bin/kenv");
407
defCmdT("OS-Kernel Memory Usage", "/usr/local/sbin/kmemusage.sh");
408
defCmdT("OS-Installed Packages", "/usr/local/sbin/pkg-static info");
409
defCmdT("OS-Package Manager Configuration", "/usr/local/sbin/pkg-static -vv");
410
defCmdT("Hardware-PCI Devices", "/usr/sbin/pciconf -lvb");
411
defCmdT("Hardware-USB Devices", "/usr/sbin/usbconfig dump_device_desc");
412

    
413
if (is_module_loaded("zfs.ko")) {
414
	defCmdT("Disk-ZFS List", "/sbin/zfs list");
415
	defCmdT("Disk-ZFS Properties", "/sbin/zfs get all");
416
	defCmdT("Disk-ZFS Pool List", "/sbin/zpool list");
417
	defCmdT("Disk-ZFS Pool Status", "/sbin/zpool status");
418
}
419
defCmdT("Disk-GEOM Mirror Status", "/sbin/gmirror status");
420

    
421
exec("/bin/date", $dateOutput, $dateStatus);
422
$currentDate = $dateOutput[0];
423

    
424
$pgtitle = array($g['product_name'], "Status");
425

    
426
if (!$console):
427
include("head.inc"); ?>
428

    
429
<form action="status.php" method="post">
430

    
431
<?php print_info_box(
432
	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.") .
433
	'<br />' .
434
	gettext("Common password and other private fields in config.xml have been automatically redacted.") .
435
	'<br />' .
436
	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) .
437
	' <button name="submit" type="submit" class="btn btn-primary btn-sm" id="download" value="DOWNLOAD">' .
438
	'<i class="fa fa-download icon-embed-btn"></i>' .
439
	gettext("Download") .
440
	'</button>'); ?>
441

    
442
</form>
443

    
444
<?php print_info_box(get_firewall_info(), 'info', false);
445

    
446
if ($show_output) {
447
	listCmds();
448
} else {
449
	print_info_box(gettext("Status output suppressed. Download archive to view."), 'info', false);
450
}
451

    
452
endif;
453

    
454
if ($console) {
455
	print(gettext("Gathering status data...") . "\n");
456
	get_firewall_info();
457
}
458
execCmds();
459

    
460
print(gettext("Saving output to archive..."));
461

    
462
if (is_dir($output_path)) {
463
	mwexec("/usr/bin/tar czpf " . escapeshellarg($output_file) . " -C " . escapeshellarg(dirname($output_path)) . " " . escapeshellarg(basename($output_path)));
464

    
465
	if (!isset($_GET["nocleanup"])) {
466
		unlink_if_exists("{$output_path}/*");
467
		@rmdir($output_path);
468
	}
469
}
470

    
471
print(gettext("Done.") . "\n");
472

    
473
if (!$console) {
474
	include("foot.inc");
475
}
(151-151/228)