Project

General

Profile

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