Project

General

Profile

Download (8.17 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 a68f7a3d Luiz Otavio O Souza
 * Copyright (c) 2014-2024 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 ac0a027f Christian McDonald
foreach (config_get_path('aliases/alias', []) as $alias) {
50
	if ( $alias['name'] == $tablename ) {
51
		$tmp = array();
52
		$tmp['type'] = $alias['type'];
53
		$tmp['name'] = $alias['name'];
54
		$tmp['url']  = $alias['url'];
55
		$tmp['freq'] = $alias['updatefreq'];
56
		break;
57 f6622167 NOYB
	}
58
}
59
60
# Determine if selected alias is either a bogons or URL table.
61
if (($tablename == "bogons") || ($tablename == "bogonsv6")) {
62
	$bogons = true;
63
} else if (preg_match('/urltable/i', $tmp['type'])) {
64
	$urltable = true;
65
} else {
66
	$bogons = $urltable = false;
67
}
68
69 5f601060 Phil Davis
if ($_REQUEST['delete']) {
70
	if (is_ipaddr($_REQUEST['delete']) || is_subnet($_REQUEST['delete'])) {
71 7afae53f Scott Ullrich
		exec("/sbin/pfctl -t " . escapeshellarg($_REQUEST['type']) . " -T delete " . escapeshellarg($_REQUEST['delete']), $delete);
72
		echo htmlentities($_REQUEST['delete']);
73
	}
74 e166769c Renato Botelho
	exit;
75 7afae53f Scott Ullrich
}
76
77 6f80b61e Phil Davis
if ($_POST['clearall']) {
78 9146639e jim-p
	$entries = array();
79 e26e0eac jim-p
	exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
80 5f601060 Phil Davis
	if (is_array($entries)) {
81
		foreach ($entries as $entryA) {
82 7afae53f Scott Ullrich
			$entry = trim($entryA);
83
			exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T delete " . escapeshellarg($entry), $delete);
84
		}
85
	}
86 16424666 Phil Davis
	unset($entries);
87 7afae53f Scott Ullrich
}
88
89 f6622167 NOYB
if ($_POST['Download'] && ($bogons || $urltable)) {
90 3a652703 sbeaver
91 f6622167 NOYB
	if ($bogons) {				// If selected table is either bogons or bogonsv6.
92
		$mwexec_bg_cmd = '/etc/rc.update_bogons.sh now';
93
		$table_type = 'bogons';
94
		$db_name = 'bogons';
95
	} else if ($urltable) {		//  If selected table is a URL table alias.
96
		$mwexec_bg_cmd = '/etc/rc.update_urltables now forceupdate ' . $tablename;
97
		$table_type = 'urltables';
98
		$db_name = $tablename;
99
	}
100
101
	mwexec_bg($mwexec_bg_cmd);
102
	$maxtimetowait = 0;
103
	$loading = true;
104
	while ($loading == true) {
105
		$isrunning = `/bin/ps awwwux | /usr/bin/grep -v grep | /usr/bin/grep $table_type`;
106
		if ($isrunning == "") {
107
			$loading = false;
108 3a652703 sbeaver
		}
109 f6622167 NOYB
		$maxtimetowait++;
110
		if ($maxtimetowait > 89) {
111
			$loading = false;
112 947141fd Phil Davis
		}
113 f6622167 NOYB
		sleep(1);
114
	}
115
	if ($maxtimetowait < 90) {
116 2b7902fe jim-p
		$savemsg = sprintf(gettext("The %s file contents have been updated."), $db_name);
117 6c474eb8 Warren Baker
	}
118
}
119
120 9146639e jim-p
$entries = array();
121 e26e0eac jim-p
exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
122 7afae53f Scott Ullrich
123
include("head.inc");
124 c054d8bc sbeaver
125 947141fd Phil Davis
if ($savemsg) {
126 e6f5c464 Stephen Beaver
	print_info_box($savemsg, 'success');
127 947141fd Phil Davis
}
128 ad9e2a90 sbeaver
129 555a9ab5 jim-p
if ($tablename == "sshguard") {
130
	$displayname = gettext("SSH and GUI Lockout Table");
131 060ed238 Stephen Beaver
} else {
132 3d7a8696 k-paulius
	$displayname = sprintf(gettext("%s Table"), ucfirst($tablename));
133 060ed238 Stephen Beaver
}
134
135 e6f5c464 Stephen Beaver
$form = new Form(false);
136 3a652703 sbeaver
137 5f88f964 k-paulius
$section = new Form_Section('Table to Display');
138 e6f5c464 Stephen Beaver
$group = new Form_Group("Table");
139 ad9e2a90 sbeaver
140 e6f5c464 Stephen Beaver
$group->add(new Form_Select(
141 ad9e2a90 sbeaver
	'type',
142 e6f5c464 Stephen Beaver
	null,
143 ad9e2a90 sbeaver
	$tablename,
144
	array_combine($tables, $tables)
145 5db70796 Phil Davis
))->setHelp('Select a user-defined alias name or system table name to view its contents. %s' .
146 2b7902fe jim-p
	'Aliases become Tables when loaded into the active firewall ruleset. ' .
147 5db70796 Phil Davis
	'The contents displayed on this page reflect the current addresses inside tables used by the firewall.', '<br/><br/>');
148 ad9e2a90 sbeaver
149 f6622167 NOYB
if ($bogons || $urltable || !empty($entries)) {
150
	if ($bogons || $urltable) {
151 e6f5c464 Stephen Beaver
		$group->add(new Form_Button(
152
			'Download',
153 faab522f Renato Botelho
			'Update',
154 37676f4e jim-p
			null,
155 e6f78714 Marcos Mendoza
			'fa-solid fa-arrows-rotate'
156 37676f4e jim-p
		))->addClass('btn-success btn-sm');
157 e6f5c464 Stephen Beaver
	} elseif (!empty($entries)) {
158
		$group->add(new Form_Button(
159
			'clearall',
160 faab522f Renato Botelho
			'Empty Table',
161 37676f4e jim-p
			null,
162 e6f78714 Marcos Mendoza
			'fa-solid fa-trash-can'
163 37676f4e jim-p
		))->addClass('btn-danger btn-sm');
164 e6f5c464 Stephen Beaver
	}
165
}
166
167
$section->add($group);
168 ad9e2a90 sbeaver
$form->add($section);
169
print $form;
170 e6f5c464 Stephen Beaver
171 f6622167 NOYB
if ($bogons || $urltable || !empty($entries)) {
172 c57b2aad Phil Davis
?>
173
<div>
174 c95dabdd Stephen Beaver
	<div class="infoblock blockopen">
175 c57b2aad Phil Davis
<?php
176 f6622167 NOYB
	if ($bogons) {
177
		$table_file = '/etc/' . escapeshellarg($tablename);
178
	} else if ($urltable) {
179
		$table_file = '/var/db/aliastables/' . escapeshellarg($tablename) . '.txt';
180
	} else {
181
		$table_file = '';
182
	}
183
184 5ba0caa7 NOYB
	$datestrregex = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun).* GMT';
185 f6622167 NOYB
	$datelineregex = 'last.*' . $datestrregex;
186
187
	$last_updated = exec('/usr/bin/grep -i -m 1 -E "^# ' . $datelineregex . '" ' . $table_file . '|/usr/bin/grep -i -m 1 -E -o "' . $datestrregex . '"');
188
189 c57b2aad Phil Davis
	if ($last_updated != "") {
190 8031655d NOYB
		$last_update_msg = sprintf(gettext("Table last updated on %s."), $last_updated);
191 c57b2aad Phil Davis
	} else {
192 8031655d NOYB
		$last_update_msg = gettext("Date of last update of table is unknown.");
193 c57b2aad Phil Davis
	}
194 8031655d NOYB
195
	$records_count_msg = sprintf(gettext("%s records."), number_format(count($entries), 0, gettext("."), gettext(",")));
196
197 f6622167 NOYB
	# Display up to 10 comment lines (lines that begin with '#').
198
	unset($comment_lines);
199
	$res = exec('/usr/bin/grep -i -m 10 -E "^#" ' . $table_file, $comment_lines);
200
201
	foreach ($comment_lines as $comment_line) {
202
		$table_comments .= "$comment_line" . "<br />";
203
	}
204
205
	if ($table_comments) {
206 f72e804a NOYB
		print_info_box($last_update_msg . " &nbsp; &nbsp; " . $records_count_msg . "<br />" .
207 f6622167 NOYB
		'<span style="display:none" class="infoblock">' . ' ' . gettext("Hide table comments.") . '<br />' . $table_comments . '</span>' .
208
		'<span style="display:none"   id="showtblcom">' . ' ' . gettext("Show table comments.") . '</span>' .
209
		'' , 'info', false);
210
	} else {
211
		print_info_box($last_update_msg . "&nbsp; &nbsp; " . $records_count_msg, 'info', false);
212
	}
213 c57b2aad Phil Davis
?>
214
	</div>
215
</div>
216
<?php
217 e6f5c464 Stephen Beaver
}
218 7afae53f Scott Ullrich
?>
219
220 8fd9052f Colin Fleming
<script type="text/javascript">
221
//<![CDATA[
222 947141fd Phil Davis
events.push(function() {
223 f6622167 NOYB
224
	$('#showtblcom').show();
225
226
	$('[id^="showinfo1"]').click(function() {
227
			$('#showtblcom').toggle();
228
	});
229
230 947141fd Phil Davis
	$('a[data-entry]').on('click', function() {
231 eb500b85 Sjon Hortensius
		var el = $(this);
232
233
		$.ajax(
234
			'/diag_tables.php',
235
			{
236
				type: 'post',
237
				data: {
238 e90eaf31 jim-p
					type: '<?=htmlspecialchars(addslashes($tablename))?>',
239 eb500b85 Sjon Hortensius
					delete: $(this).data('entry')
240
				},
241 947141fd Phil Davis
				success: function() {
242 eb500b85 Sjon Hortensius
					el.parents('tr').remove();
243
				},
244 7afae53f Scott Ullrich
		});
245 eb500b85 Sjon Hortensius
	});
246 e6f5c464 Stephen Beaver
247
	// Auto-submit the form on table selector change
248
	$('#type').on('change', function() {
249
        $('form').submit();
250
    });
251 eb500b85 Sjon Hortensius
});
252 8fd9052f Colin Fleming
//]]>
253 7afae53f Scott Ullrich
</script>
254 e166769c Renato Botelho
255 4027d64e k-paulius
<?php
256
if (empty($entries)) {
257
	print_info_box(gettext("No entries exist in this table."), 'warning', false);
258
} else {
259
?>
260 060ed238 Stephen Beaver
<div class="panel panel-default">
261
	<div class="panel-heading"><h2 class="panel-title"><?=$displayname?></h2></div>
262
	<div class="panel-body">
263
		<div class="table-responsive">
264
			<table class="table table-striped table-hover table-condensed">
265
				<thead>
266
					<tr>
267
						<th><?=gettext("IP Address")?></th>
268
						<th></th>
269
					</tr>
270
				</thead>
271
				<tbody>
272 37f73a7c NOYB
<?php
273
		// This is a band-aid for a yet to be root caused performance issue with large tables.  Suspected is css and/or sorting.
274
 		if (count($entries) > 3000) {
275
			print "<tr><td colspan='2'><pre>";
276
			foreach ($entries as $entry) {
277
				$entry = trim($entry);
278
					print $entry . "\n";
279
			}
280
			print "</pre></td></tr>";
281
		} else {
282
?>
283 7afae53f Scott Ullrich
<?php
284 eb500b85 Sjon Hortensius
		foreach ($entries as $entry):
285
			$entry = trim($entry);
286 7afae53f Scott Ullrich
?>
287 060ed238 Stephen Beaver
					<tr>
288
						<td>
289
							<?=$entry?>
290
						</td>
291
						<td>
292 f6622167 NOYB
							<?php if (!$bogons && !$urltable): ?>
293 37676f4e jim-p
								<a style="cursor: pointer;" data-entry="<?=htmlspecialchars($entry)?>">
294 c1d304b3 Marcos Mendoza
									<i class="fa-solid fa-trash-can" title="<?= gettext("Remove this entry") ?>"></i>
295 37676f4e jim-p
								</a>
296 060ed238 Stephen Beaver
							<?php endif ?>
297
						</td>
298
					</tr>
299 eb500b85 Sjon Hortensius
<?php endforeach ?>
300 37f73a7c NOYB
<?php } ?>
301 060ed238 Stephen Beaver
				</tbody>
302
			</table>
303
		</div>
304
	</div>
305 c054d8bc sbeaver
</div>
306 060ed238 Stephen Beaver
307 3a652703 sbeaver
<?php
308 7c945f74 k-paulius
}
309 4027d64e k-paulius
310 7ac86a5f Colin Fleming
include("foot.inc");