Project

General

Profile

Download (8.18 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_tables.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-2023 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * 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
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * 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
 */
23

    
24
##|+PRIV
25
##|*IDENT=page-diagnostics-tables
26
##|*NAME=Diagnostics: pf Table IP addresses
27
##|*DESCR=Allow access to the 'Diagnostics: Tables' page.
28
##|*MATCH=diag_tables.php*
29
##|-PRIV
30

    
31
$pgtitle = array(gettext("Diagnostics"), gettext("Tables"));
32
$shortcut_section = "aliases";
33

    
34
require_once("guiconfig.inc");
35

    
36
exec("/sbin/pfctl -sT", $tables);
37

    
38
// Set default table
39
$tablename = "sshguard";
40

    
41
if ($_REQUEST['type'] && in_array($_REQUEST['type'], $tables)) {
42
	$tablename = $_REQUEST['type'];
43
} else {
44
	/* Invalid 'type' passed, do not take any actions that use the 'type' field. */
45
	unset($_REQUEST['type']);
46
}
47

    
48
// 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
if ($_REQUEST['delete']) {
72
	if (is_ipaddr($_REQUEST['delete']) || is_subnet($_REQUEST['delete'])) {
73
		exec("/sbin/pfctl -t " . escapeshellarg($_REQUEST['type']) . " -T delete " . escapeshellarg($_REQUEST['delete']), $delete);
74
		echo htmlentities($_REQUEST['delete']);
75
	}
76
	exit;
77
}
78

    
79
if ($_POST['clearall']) {
80
	$entries = array();
81
	exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
82
	if (is_array($entries)) {
83
		foreach ($entries as $entryA) {
84
			$entry = trim($entryA);
85
			exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T delete " . escapeshellarg($entry), $delete);
86
		}
87
	}
88
	unset($entries);
89
}
90

    
91
if ($_POST['Download'] && ($bogons || $urltable)) {
92

    
93
	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
		}
111
		$maxtimetowait++;
112
		if ($maxtimetowait > 89) {
113
			$loading = false;
114
		}
115
		sleep(1);
116
	}
117
	if ($maxtimetowait < 90) {
118
		$savemsg = sprintf(gettext("The %s file contents have been updated."), $db_name);
119
	}
120
}
121

    
122
$entries = array();
123
exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
124

    
125
include("head.inc");
126

    
127
if ($savemsg) {
128
	print_info_box($savemsg, 'success');
129
}
130

    
131
if ($tablename == "sshguard") {
132
	$displayname = gettext("SSH and GUI Lockout Table");
133
} else {
134
	$displayname = sprintf(gettext("%s Table"), ucfirst($tablename));
135
}
136

    
137
$form = new Form(false);
138

    
139
$section = new Form_Section('Table to Display');
140
$group = new Form_Group("Table");
141

    
142
$group->add(new Form_Select(
143
	'type',
144
	null,
145
	$tablename,
146
	array_combine($tables, $tables)
147
))->setHelp('Select a user-defined alias name or system table name to view its contents. %s' .
148
	'Aliases become Tables when loaded into the active firewall ruleset. ' .
149
	'The contents displayed on this page reflect the current addresses inside tables used by the firewall.', '<br/><br/>');
150

    
151
if ($bogons || $urltable || !empty($entries)) {
152
	if ($bogons || $urltable) {
153
		$group->add(new Form_Button(
154
			'Download',
155
			'Update',
156
			null,
157
			'fa-refresh'
158
		))->addClass('btn-success btn-sm');
159
	} elseif (!empty($entries)) {
160
		$group->add(new Form_Button(
161
			'clearall',
162
			'Empty Table',
163
			null,
164
			'fa-trash'
165
		))->addClass('btn-danger btn-sm');
166
	}
167
}
168

    
169
$section->add($group);
170
$form->add($section);
171
print $form;
172

    
173
if ($bogons || $urltable || !empty($entries)) {
174
?>
175
<div>
176
	<div class="infoblock blockopen">
177
<?php
178
	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
	$datestrregex = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun).* GMT';
187
	$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
	if ($last_updated != "") {
192
		$last_update_msg = sprintf(gettext("Table last updated on %s."), $last_updated);
193
	} else {
194
		$last_update_msg = gettext("Date of last update of table is unknown.");
195
	}
196

    
197
	$records_count_msg = sprintf(gettext("%s records."), number_format(count($entries), 0, gettext("."), gettext(",")));
198

    
199
	# 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
		print_info_box($last_update_msg . " &nbsp; &nbsp; " . $records_count_msg . "<br />" .
209
		'<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
?>
216
	</div>
217
</div>
218
<?php
219
}
220
?>
221

    
222
<script type="text/javascript">
223
//<![CDATA[
224
events.push(function() {
225

    
226
	$('#showtblcom').show();
227

    
228
	$('[id^="showinfo1"]').click(function() {
229
			$('#showtblcom').toggle();
230
	});
231

    
232
	$('a[data-entry]').on('click', function() {
233
		var el = $(this);
234

    
235
		$.ajax(
236
			'/diag_tables.php',
237
			{
238
				type: 'post',
239
				data: {
240
					type: '<?=htmlspecialchars(addslashes($tablename))?>',
241
					delete: $(this).data('entry')
242
				},
243
				success: function() {
244
					el.parents('tr').remove();
245
				},
246
		});
247
	});
248

    
249
	// Auto-submit the form on table selector change
250
	$('#type').on('change', function() {
251
        $('form').submit();
252
    });
253
});
254
//]]>
255
</script>
256

    
257
<?php
258
if (empty($entries)) {
259
	print_info_box(gettext("No entries exist in this table."), 'warning', false);
260
} else {
261
?>
262
<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
<?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
<?php
286
		foreach ($entries as $entry):
287
			$entry = trim($entry);
288
?>
289
					<tr>
290
						<td>
291
							<?=$entry?>
292
						</td>
293
						<td>
294
							<?php if (!$bogons && !$urltable): ?>
295
								<a style="cursor: pointer;" data-entry="<?=htmlspecialchars($entry)?>">
296
									<i class="fa fa-trash" title="<?= gettext("Remove this entry") ?>"></i>
297
								</a>
298
							<?php endif ?>
299
						</td>
300
					</tr>
301
<?php endforeach ?>
302
<?php } ?>
303
				</tbody>
304
			</table>
305
		</div>
306
	</div>
307
</div>
308

    
309
<?php
310
}
311

    
312
include("foot.inc");
(32-32/228)