Project

General

Profile

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

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

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

    
38
global $console;
39
global $show_output;
40

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

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

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

    
62
$filtered_tags = array(
63
	'accountkey', 'authorizedkeys', 'auth_pass', 'auth_user', 'bcrypt-hash',
64
	'crypto_password', 'crypto_password2', 'dns_nsupdatensupdate_key',
65
	'gold_encryption_password', 'gold_password', 'ipsecpsk', 'ldap_bindpw',
66
	'lighttpd_ls_password', 'lighttpd_ls_password', 'md5-hash',
67
	'md5password', 'md5sigkey', 'md5sigpass', 'nt-hash', 'passphrase',
68
	'password', 'passwordagain', 'pre-shared-key', 'proxypass',
69
	'proxy_passwd', 'proxyuser', 'proxy_user', 'prv', 'radius_secret',
70
	'redis_password', 'redis_passwordagain', 'rocommunity', 'secret',
71
	'shared_key', 'tls', 'varclientpasswordinput', 'varclientsharedsecret',
72
	'varsyncpassword', 'varusersmotpinitsecret', 'varusersmotppin'
73
);
74

    
75
if ($_POST['submit'] == "DOWNLOAD" && file_exists($output_file)) {
76
	session_cache_limiter('public');
77
	$fd = fopen($output_file, "rb");
78
	header("Content-Type: application/octet-stream");
79
	header("Content-Length: " . filesize($output_file));
80
	header("Content-Disposition: attachment; filename=\"" .
81
		trim(htmlentities(basename($output_file))) . "\"");
82
	if (isset($_SERVER['HTTPS'])) {
83
		header('Pragma: ');
84
		header('Cache-Control: ');
85
	} else {
86
		header("Pragma: private");
87
		header("Cache-Control: private, must-revalidate");
88
	}
89

    
90
	fpassthru($fd);
91
	exit;
92
}
93

    
94
if (is_dir($output_path)) {
95
	unlink_if_exists("{$output_path}/*");
96
	@rmdir($output_path);
97
}
98
unlink_if_exists($output_file);
99
mkdir($output_path);
100

    
101
function doCmdT($title, $command, $method) {
102
	global $output_path, $output_file, $filtered_tags, $show_output;
103
	/* Fixup output directory */
104

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

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

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

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

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

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

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

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

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

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

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

    
217
	if (function_exists("system_get_thothid")) {
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
	$thothid = system_get_thothid();
271
	if (!empty($thothid)) {
272
		defCmdT("Product-Public Key", "/usr/bin/openssl ec -in /etc/thoth/key.pem -noout -text | /usr/bin/sed -n '/pub:/,\$p'");
273
	}
274
}
275

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

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

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

    
321
if (is_array($config['load_balancer']['lbpool']) && is_array($config['load_balancer']['virtual_server'])) {
322
	defCmdT("Load Balancer-Redirects", "/usr/local/sbin/relayctl show redirects");
323
	defCmdT("Load Balancer-Relays", "/usr/local/sbin/relayctl show relays");
324
	defCmdT("Load Balancer-Summary", "/usr/local/sbin/relayctl show summary");
325
}
326

    
327
/* Configuration Files */
328
defCmdT("Disk-Contents of var run", "/bin/ls /var/run");
329
defCmdT("Disk-Contents of conf", "/bin/ls /conf");
330
defCmdT("config.xml", "dumpconfigxml");
331
defCmdT("DNS-Resolution Configuration", "/bin/cat /etc/resolv.conf");
332
defCmdT("DHCP-IPv4 Configuration", "/bin/cat /var/dhcpd/etc/dhcpd.conf");
333
defCmdT("DHCP-IPv6-Configuration", "/bin/cat /var/dhcpd/etc/dhcpdv6.conf");
334
defCmdT("IPsec-strongSwan Configuration", "/bin/cat /var/etc/ipsec/strongswan.conf | /usr/bin/sed 's/[[:blank:]]secret = .*//'");
335
defCmdT("IPsec-Configuration", "/bin/cat /var/etc/ipsec/ipsec.conf");
336
defCmdT("IPsec-Status", "/usr/local/sbin/ipsec statusall");
337
defCmdT("IPsec-SPD", "/sbin/setkey -DP");
338
defCmdT("IPsec-SAD", "/sbin/setkey -D");
339
if (file_exists("/cf/conf/upgrade_log.txt")) {
340
	defCmdT("OS-Upgrade Log", "/bin/cat /cf/conf/upgrade_log.txt");
341
}
342
if (file_exists("/boot/loader.conf")) {
343
	defCmdT("OS-Boot Loader Configuration", "/bin/cat /boot/loader.conf");
344
}
345
if (file_exists("/boot/loader.conf.local")) {
346
	defCmdT("OS-Boot Loader Configuration (Local)", "/bin/cat /boot/loader.conf.local");
347
}
348
if (file_exists("/var/etc/filterdns.conf")) {
349
	defCmdT("DNS-filterdns Daemon Configuration", "/bin/cat /var/etc/filterdns.conf");
350
}
351

    
352
/* Logs */
353
defCmdT("Log-System-Last 1000 entries", "/usr/local/sbin/clog /var/log/system.log 2>&1 | tail -n 1000");
354
defCmdT("Log-DHCP-Last 1000 entries", "/usr/local/sbin/clog /var/log/dhcpd.log 2>&1 | tail -n 1000");
355
defCmdT("Log-Filter-Last 500 entries", "/usr/local/sbin/clog /var/log/filter.log 2>&1 | tail -n 500");
356
defCmdT("Log-Gateways-Last 1000 entries", "/usr/local/sbin/clog /var/log/gateways.log 2>&1 | tail -n 1000");
357
defCmdT("Log-IPsec-Last 1000 entries", "/usr/local/sbin/clog /var/log/ipsec.log 2>&1 | tail -n 1000");
358
defCmdT("Log-L2TP-Last 1000 entries", "/usr/local/sbin/clog /var/log/l2tps.log 2>&1 | tail -n 1000");
359
defCmdT("Log-NTP-Last 1000 entries", "/usr/local/sbin/clog /var/log/ntpd.log 2>&1 | tail -n 1000");
360
defCmdT("Log-OpenVPN-Last 1000 entries", "/usr/local/sbin/clog /var/log/openvpn.log 2>&1 | tail -n 1000");
361
defCmdT("Log-Captive Portal Authentication-Last 1000 entries", "/usr/local/sbin/clog /var/log/portalauth.log 2>&1 | tail -n 1000");
362
defCmdT("Log-PPP-Last 1000 entries", "/usr/local/sbin/clog /var/log/ppp.log 2>&1 | tail -n 1000");
363
defCmdT("Log-PPPoE Server-Last 1000 entries", "/usr/local/sbin/clog /var/log/poes.log 2>&1 | tail -n 1000");
364
defCmdT("Log-relayd-Last 1000 entries", "/usr/local/sbin/clog /var/log/relayd.log 2>&1 | tail -n 1000");
365
defCmdT("Log-DNS-Last 1000 entries", "/usr/local/sbin/clog /var/log/resolver.log 2>&1 | tail -n 1000");
366
defCmdT("Log-Routing-Last 1000 entries", "/usr/local/sbin/clog /var/log/routing.log 2>&1 | tail -n 1000");
367
defCmdT("Log-Wireless-Last 1000 entries", "/usr/local/sbin/clog /var/log/wireless.log 2>&1 | tail -n 1000");
368
if (file_exists("/tmp/PHP_errors.log")) {
369
	defCmdT("Log-PHP Errors", "/bin/cat /tmp/PHP_errors.log");
370
}
371
defCmdT("OS-Message Buffer", "/sbin/dmesg -a");
372
defCmdT("OS-Message Buffer (Boot)", "/bin/cat /var/log/dmesg.boot");
373

    
374
/* OS/Hardware Status */
375
defCmdT("OS-sysctl values", "/sbin/sysctl -aq");
376
defCmdT("OS-Kernel Environment", "/bin/kenv");
377
defCmdT("OS-Installed Packages", "/usr/sbin/pkg info");
378
defCmdT("OS-Package Manager Configuration", "/usr/sbin/pkg -vv");
379
defCmdT("Hardware-PCI Devices", "/usr/sbin/pciconf -lvb");
380
defCmdT("Hardware-USB Devices", "/usr/sbin/usbconfig dump_device_desc");
381

    
382
if (is_module_loaded("zfs.ko")) {
383
	defCmdT("Disk-ZFS List", "/sbin/zfs list");
384
	defCmdT("Disk-ZFS Properties", "/sbin/zfs get all");
385
	defCmdT("Disk-ZFS Pool List", "/sbin/zpool list");
386
	defCmdT("Disk-ZFS Pool Status", "/sbin/zpool status");
387
}
388
defCmdT("Disk-GEOM Mirror Status", "/sbin/gmirror status");
389

    
390
exec("/bin/date", $dateOutput, $dateStatus);
391
$currentDate = $dateOutput[0];
392

    
393
$pgtitle = array($g['product_name'], "Status");
394

    
395
if (!$console):
396
include("head.inc"); ?>
397

    
398
<form action="status.php" method="post">
399

    
400
<?php print_info_box(
401
	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.") .
402
	'<br />' .
403
	gettext("Common password and other private fields in config.xml have been automatically redacted.") .
404
	'<br />' .
405
	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) .
406
	' <button name="submit" type="submit" class="btn btn-primary btn-sm" id="download" value="DOWNLOAD">' .
407
	'<i class="fa fa-download icon-embed-btn"></i>' .
408
	gettext("Download") .
409
	'</button>'); ?>
410

    
411
</form>
412

    
413
<?php print_info_box(get_firewall_info(), 'info', false);
414

    
415
if ($show_output) {
416
	listCmds();
417
} else {
418
	print_info_box(gettext("Status output suppressed. Download archive to view."), 'info', false);
419
}
420

    
421
endif;
422

    
423
if ($console) {
424
	print(gettext("Gathering status data...") . "\n");
425
	get_firewall_info();
426
}
427
execCmds();
428

    
429
print(gettext("Saving output to archive..."));
430

    
431
if (is_dir($output_path)) {
432
	mwexec("/usr/bin/tar czpf " . escapeshellarg($output_file) . " -C " . escapeshellarg(dirname($output_path)) . " " . escapeshellarg(basename($output_path)));
433

    
434
	if (!isset($_GET["nocleanup"])) {
435
		unlink_if_exists("{$output_path}/*");
436
		@rmdir($output_path);
437
	}
438
}
439

    
440
print(gettext("Done.") . "\n");
441

    
442
if (!$console) {
443
	include("foot.inc");
444
}
(156-156/234)