Project

General

Profile

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