Project

General

Profile

Download (8.18 KB) Statistics
| Branch: | Tag: | Revision:
1 7afae53f Scott Ullrich
<?php
2
/*
3 c5d81585 Renato Botelho
 * diag_tables.php
4 fd9ebcd5 Stephen Beaver
 *
5 c5d81585 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 38809d47 Renato Botelho do Couto
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8 402c98a2 Reid Linnemann
 * Copyright (c) 2014-2023 Rubicon Communications, LLC (Netgate)
9 c5d81585 Renato Botelho
 * All rights reserved.
10 fd9ebcd5 Stephen Beaver
 *
11 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14 fd9ebcd5 Stephen Beaver
 *
15 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
16 fd9ebcd5 Stephen Beaver
 *
17 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22 fd9ebcd5 Stephen Beaver
 */
23 7afae53f Scott Ullrich
24
##|+PRIV
25 c07b05e0 Scott Ullrich
##|*IDENT=page-diagnostics-tables
26 9599211d jim-p
##|*NAME=Diagnostics: pf Table IP addresses
27 c07b05e0 Scott Ullrich
##|*DESCR=Allow access to the 'Diagnostics: Tables' page.
28
##|*MATCH=diag_tables.php*
29 7afae53f Scott Ullrich
##|-PRIV
30
31 c07b05e0 Scott Ullrich
$pgtitle = array(gettext("Diagnostics"), gettext("Tables"));
32 d71fc5d3 jim-p
$shortcut_section = "aliases";
33 7afae53f Scott Ullrich
34
require_once("guiconfig.inc");
35
36 e90eaf31 jim-p
exec("/sbin/pfctl -sT", $tables);
37
38 7afae53f Scott Ullrich
// Set default table
39 b89270b7 Renato Botelho
$tablename = "sshguard";
40 e166769c Renato Botelho
41 e90eaf31 jim-p
if ($_REQUEST['type'] && in_array($_REQUEST['type'], $tables)) {
42 34525fef Ermal
	$tablename = $_REQUEST['type'];
43 e90eaf31 jim-p
} else {
44
	/* Invalid 'type' passed, do not take any actions that use the 'type' field. */
45
	unset($_REQUEST['type']);
46 5f601060 Phil Davis
}
47 e166769c Renato Botelho
48 f6622167 NOYB
// Gather selected alias metadata.
49
if (isset($config['aliases']['alias'])) {
50
	foreach ($config['aliases']['alias'] as $alias) {
51
		if ( $alias['name'] == $tablename ) {
52
			$tmp = array();
53
			$tmp['type'] = $alias['type'];
54
			$tmp['name'] = $alias['name'];
55
			$tmp['url']  = $alias['url'];
56
			$tmp['freq'] = $alias['updatefreq'];
57
			break;
58
		}
59
	}
60
}
61
62
# Determine if selected alias is either a bogons or URL table.
63
if (($tablename == "bogons") || ($tablename == "bogonsv6")) {
64
	$bogons = true;
65
} else if (preg_match('/urltable/i', $tmp['type'])) {
66
	$urltable = true;
67
} else {
68
	$bogons = $urltable = false;
69
}
70
71 5f601060 Phil Davis
if ($_REQUEST['delete']) {
72
	if (is_ipaddr($_REQUEST['delete']) || is_subnet($_REQUEST['delete'])) {
73 7afae53f Scott Ullrich
		exec("/sbin/pfctl -t " . escapeshellarg($_REQUEST['type']) . " -T delete " . escapeshellarg($_REQUEST['delete']), $delete);
74
		echo htmlentities($_REQUEST['delete']);
75
	}
76 e166769c Renato Botelho
	exit;
77 7afae53f Scott Ullrich
}
78
79 6f80b61e Phil Davis
if ($_POST['clearall']) {
80 9146639e jim-p
	$entries = array();
81 e26e0eac jim-p
	exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
82 5f601060 Phil Davis
	if (is_array($entries)) {
83
		foreach ($entries as $entryA) {
84 7afae53f Scott Ullrich
			$entry = trim($entryA);
85
			exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T delete " . escapeshellarg($entry), $delete);
86
		}
87
	}
88 16424666 Phil Davis
	unset($entries);
89 7afae53f Scott Ullrich
}
90
91 f6622167 NOYB
if ($_POST['Download'] && ($bogons || $urltable)) {
92 3a652703 sbeaver
93 f6622167 NOYB
	if ($bogons) {				// If selected table is either bogons or bogonsv6.
94
		$mwexec_bg_cmd = '/etc/rc.update_bogons.sh now';
95
		$table_type = 'bogons';
96
		$db_name = 'bogons';
97
	} else if ($urltable) {		//  If selected table is a URL table alias.
98
		$mwexec_bg_cmd = '/etc/rc.update_urltables now forceupdate ' . $tablename;
99
		$table_type = 'urltables';
100
		$db_name = $tablename;
101
	}
102
103
	mwexec_bg($mwexec_bg_cmd);
104
	$maxtimetowait = 0;
105
	$loading = true;
106
	while ($loading == true) {
107
		$isrunning = `/bin/ps awwwux | /usr/bin/grep -v grep | /usr/bin/grep $table_type`;
108
		if ($isrunning == "") {
109
			$loading = false;
110 3a652703 sbeaver
		}
111 f6622167 NOYB
		$maxtimetowait++;
112
		if ($maxtimetowait > 89) {
113
			$loading = false;
114 947141fd Phil Davis
		}
115 f6622167 NOYB
		sleep(1);
116
	}
117
	if ($maxtimetowait < 90) {
118 2b7902fe jim-p
		$savemsg = sprintf(gettext("The %s file contents have been updated."), $db_name);
119 6c474eb8 Warren Baker
	}
120
}
121
122 9146639e jim-p
$entries = array();
123 e26e0eac jim-p
exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
124 7afae53f Scott Ullrich
125
include("head.inc");
126 c054d8bc sbeaver
127 947141fd Phil Davis
if ($savemsg) {
128 e6f5c464 Stephen Beaver
	print_info_box($savemsg, 'success');
129 947141fd Phil Davis
}
130 ad9e2a90 sbeaver
131 555a9ab5 jim-p
if ($tablename == "sshguard") {
132
	$displayname = gettext("SSH and GUI Lockout Table");
133 060ed238 Stephen Beaver
} else {
134 3d7a8696 k-paulius
	$displayname = sprintf(gettext("%s Table"), ucfirst($tablename));
135 060ed238 Stephen Beaver
}
136
137 e6f5c464 Stephen Beaver
$form = new Form(false);
138 3a652703 sbeaver
139 5f88f964 k-paulius
$section = new Form_Section('Table to Display');
140 e6f5c464 Stephen Beaver
$group = new Form_Group("Table");
141 ad9e2a90 sbeaver
142 e6f5c464 Stephen Beaver
$group->add(new Form_Select(
143 ad9e2a90 sbeaver
	'type',
144 e6f5c464 Stephen Beaver
	null,
145 ad9e2a90 sbeaver
	$tablename,
146
	array_combine($tables, $tables)
147 5db70796 Phil Davis
))->setHelp('Select a user-defined alias name or system table name to view its contents. %s' .
148 2b7902fe jim-p
	'Aliases become Tables when loaded into the active firewall ruleset. ' .
149 5db70796 Phil Davis
	'The contents displayed on this page reflect the current addresses inside tables used by the firewall.', '<br/><br/>');
150 ad9e2a90 sbeaver
151 f6622167 NOYB
if ($bogons || $urltable || !empty($entries)) {
152
	if ($bogons || $urltable) {
153 e6f5c464 Stephen Beaver
		$group->add(new Form_Button(
154
			'Download',
155 faab522f Renato Botelho
			'Update',
156 37676f4e jim-p
			null,
157
			'fa-refresh'
158
		))->addClass('btn-success btn-sm');
159 e6f5c464 Stephen Beaver
	} elseif (!empty($entries)) {
160
		$group->add(new Form_Button(
161
			'clearall',
162 faab522f Renato Botelho
			'Empty Table',
163 37676f4e jim-p
			null,
164 8a3c6f0c jim-p
			'fa-trash'
165 37676f4e jim-p
		))->addClass('btn-danger btn-sm');
166 e6f5c464 Stephen Beaver
	}
167
}
168
169
$section->add($group);
170 ad9e2a90 sbeaver
$form->add($section);
171
print $form;
172 e6f5c464 Stephen Beaver
173 f6622167 NOYB
if ($bogons || $urltable || !empty($entries)) {
174 c57b2aad Phil Davis
?>
175
<div>
176 c95dabdd Stephen Beaver
	<div class="infoblock blockopen">
177 c57b2aad Phil Davis
<?php
178 f6622167 NOYB
	if ($bogons) {
179
		$table_file = '/etc/' . escapeshellarg($tablename);
180
	} else if ($urltable) {
181
		$table_file = '/var/db/aliastables/' . escapeshellarg($tablename) . '.txt';
182
	} else {
183
		$table_file = '';
184
	}
185
186 5ba0caa7 NOYB
	$datestrregex = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun).* GMT';
187 f6622167 NOYB
	$datelineregex = 'last.*' . $datestrregex;
188
189
	$last_updated = exec('/usr/bin/grep -i -m 1 -E "^# ' . $datelineregex . '" ' . $table_file . '|/usr/bin/grep -i -m 1 -E -o "' . $datestrregex . '"');
190
191 c57b2aad Phil Davis
	if ($last_updated != "") {
192 8031655d NOYB
		$last_update_msg = sprintf(gettext("Table last updated on %s."), $last_updated);
193 c57b2aad Phil Davis
	} else {
194 8031655d NOYB
		$last_update_msg = gettext("Date of last update of table is unknown.");
195 c57b2aad Phil Davis
	}
196 8031655d NOYB
197
	$records_count_msg = sprintf(gettext("%s records."), number_format(count($entries), 0, gettext("."), gettext(",")));
198
199 f6622167 NOYB
	# Display up to 10 comment lines (lines that begin with '#').
200
	unset($comment_lines);
201
	$res = exec('/usr/bin/grep -i -m 10 -E "^#" ' . $table_file, $comment_lines);
202
203
	foreach ($comment_lines as $comment_line) {
204
		$table_comments .= "$comment_line" . "<br />";
205
	}
206
207
	if ($table_comments) {
208 f72e804a NOYB
		print_info_box($last_update_msg . " &nbsp; &nbsp; " . $records_count_msg . "<br />" .
209 f6622167 NOYB
		'<span style="display:none" class="infoblock">' . ' ' . gettext("Hide table comments.") . '<br />' . $table_comments . '</span>' .
210
		'<span style="display:none"   id="showtblcom">' . ' ' . gettext("Show table comments.") . '</span>' .
211
		'' , 'info', false);
212
	} else {
213
		print_info_box($last_update_msg . "&nbsp; &nbsp; " . $records_count_msg, 'info', false);
214
	}
215 c57b2aad Phil Davis
?>
216
	</div>
217
</div>
218
<?php
219 e6f5c464 Stephen Beaver
}
220 7afae53f Scott Ullrich
?>
221
222 8fd9052f Colin Fleming
<script type="text/javascript">
223
//<![CDATA[
224 947141fd Phil Davis
events.push(function() {
225 f6622167 NOYB
226
	$('#showtblcom').show();
227
228
	$('[id^="showinfo1"]').click(function() {
229
			$('#showtblcom').toggle();
230
	});
231
232 947141fd Phil Davis
	$('a[data-entry]').on('click', function() {
233 eb500b85 Sjon Hortensius
		var el = $(this);
234
235
		$.ajax(
236
			'/diag_tables.php',
237
			{
238
				type: 'post',
239
				data: {
240 e90eaf31 jim-p
					type: '<?=htmlspecialchars(addslashes($tablename))?>',
241 eb500b85 Sjon Hortensius
					delete: $(this).data('entry')
242
				},
243 947141fd Phil Davis
				success: function() {
244 eb500b85 Sjon Hortensius
					el.parents('tr').remove();
245
				},
246 7afae53f Scott Ullrich
		});
247 eb500b85 Sjon Hortensius
	});
248 e6f5c464 Stephen Beaver
249
	// Auto-submit the form on table selector change
250
	$('#type').on('change', function() {
251
        $('form').submit();
252
    });
253 eb500b85 Sjon Hortensius
});
254 8fd9052f Colin Fleming
//]]>
255 7afae53f Scott Ullrich
</script>
256 e166769c Renato Botelho
257 4027d64e k-paulius
<?php
258
if (empty($entries)) {
259
	print_info_box(gettext("No entries exist in this table."), 'warning', false);
260
} else {
261
?>
262 060ed238 Stephen Beaver
<div class="panel panel-default">
263
	<div class="panel-heading"><h2 class="panel-title"><?=$displayname?></h2></div>
264
	<div class="panel-body">
265
		<div class="table-responsive">
266
			<table class="table table-striped table-hover table-condensed">
267
				<thead>
268
					<tr>
269
						<th><?=gettext("IP Address")?></th>
270
						<th></th>
271
					</tr>
272
				</thead>
273
				<tbody>
274 37f73a7c NOYB
<?php
275
		// This is a band-aid for a yet to be root caused performance issue with large tables.  Suspected is css and/or sorting.
276
 		if (count($entries) > 3000) {
277
			print "<tr><td colspan='2'><pre>";
278
			foreach ($entries as $entry) {
279
				$entry = trim($entry);
280
					print $entry . "\n";
281
			}
282
			print "</pre></td></tr>";
283
		} else {
284
?>
285 7afae53f Scott Ullrich
<?php
286 eb500b85 Sjon Hortensius
		foreach ($entries as $entry):
287
			$entry = trim($entry);
288 7afae53f Scott Ullrich
?>
289 060ed238 Stephen Beaver
					<tr>
290
						<td>
291
							<?=$entry?>
292
						</td>
293
						<td>
294 f6622167 NOYB
							<?php if (!$bogons && !$urltable): ?>
295 37676f4e jim-p
								<a style="cursor: pointer;" data-entry="<?=htmlspecialchars($entry)?>">
296
									<i class="fa fa-trash" title="<?= gettext("Remove this entry") ?>"></i>
297
								</a>
298 060ed238 Stephen Beaver
							<?php endif ?>
299
						</td>
300
					</tr>
301 eb500b85 Sjon Hortensius
<?php endforeach ?>
302 37f73a7c NOYB
<?php } ?>
303 060ed238 Stephen Beaver
				</tbody>
304
			</table>
305
		</div>
306
	</div>
307 c054d8bc sbeaver
</div>
308 060ed238 Stephen Beaver
309 3a652703 sbeaver
<?php
310 7c945f74 k-paulius
}
311 4027d64e k-paulius
312 7ac86a5f Colin Fleming
include("foot.inc");