Project

General

Profile

Download (9.75 KB) Statistics
| Branch: | Tag: | Revision:
1 7afae53f Scott Ullrich
<?php
2
/*
3 aaec5634 Renato Botelho
 * diag_tables.php
4 fd9ebcd5 Stephen Beaver
 *
5 aaec5634 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 2a2396a6 Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 aaec5634 Renato Botelho
 * All rights reserved.
8 fd9ebcd5 Stephen Beaver
 *
9 aaec5634 Renato Botelho
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11 fd9ebcd5 Stephen Beaver
 *
12 aaec5634 Renato Botelho
 * 1. Redistributions of source code must retain the above copyright notice,
13
 *    this list of conditions and the following disclaimer.
14 fd9ebcd5 Stephen Beaver
 *
15 aaec5634 Renato Botelho
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *    the documentation and/or other materials provided with the
18
 *    distribution.
19 fd9ebcd5 Stephen Beaver
 *
20 aaec5634 Renato Botelho
 * 3. All advertising materials mentioning features or use of this software
21
 *    must display the following acknowledgment:
22
 *    "This product includes software developed by the pfSense Project
23
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
24 fd9ebcd5 Stephen Beaver
 *
25 aaec5634 Renato Botelho
 * 4. The names "pfSense" and "pfSense Project" must not be used to
26
 *    endorse or promote products derived from this software without
27
 *    prior written permission. For written permission, please contact
28
 *    coreteam@pfsense.org.
29 fd9ebcd5 Stephen Beaver
 *
30 aaec5634 Renato Botelho
 * 5. Products derived from this software may not be called "pfSense"
31
 *    nor may "pfSense" appear in their names without prior written
32
 *    permission of the Electric Sheep Fencing, LLC.
33 fd9ebcd5 Stephen Beaver
 *
34 aaec5634 Renato Botelho
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36 0da0d43e Phil Davis
 *
37 aaec5634 Renato Botelho
 * "This product includes software developed by the pfSense Project
38
 * for use in the pfSense software distribution (http://www.pfsense.org/).
39 fd9ebcd5 Stephen Beaver
 *
40 aaec5634 Renato Botelho
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 fd9ebcd5 Stephen Beaver
 */
53 7afae53f Scott Ullrich
54
##|+PRIV
55 c07b05e0 Scott Ullrich
##|*IDENT=page-diagnostics-tables
56 9599211d jim-p
##|*NAME=Diagnostics: pf Table IP addresses
57 c07b05e0 Scott Ullrich
##|*DESCR=Allow access to the 'Diagnostics: Tables' page.
58
##|*MATCH=diag_tables.php*
59 7afae53f Scott Ullrich
##|-PRIV
60
61 c07b05e0 Scott Ullrich
$pgtitle = array(gettext("Diagnostics"), gettext("Tables"));
62 d71fc5d3 jim-p
$shortcut_section = "aliases";
63 7afae53f Scott Ullrich
64
require_once("guiconfig.inc");
65
66 6c989d4a jim-p
exec("/sbin/pfctl -sT", $tables);
67
68 7afae53f Scott Ullrich
// Set default table
69
$tablename = "sshlockout";
70 e166769c Renato Botelho
71 6c989d4a jim-p
if ($_REQUEST['type'] && in_array($_REQUEST['type'], $tables)) {
72 34525fef Ermal
	$tablename = $_REQUEST['type'];
73 6c989d4a jim-p
} else {
74
	/* Invalid 'type' passed, do not take any actions that use the 'type' field. */
75
	unset($_REQUEST['type']);
76
	$_REQUEST['delete'];
77 5f601060 Phil Davis
}
78 e166769c Renato Botelho
79 f6622167 NOYB
// Gather selected alias metadata.
80
if (isset($config['aliases']['alias'])) {
81
	foreach ($config['aliases']['alias'] as $alias) {
82
		if ( $alias['name'] == $tablename ) {
83
			$tmp = array();
84
			$tmp['type'] = $alias['type'];
85
			$tmp['name'] = $alias['name'];
86
			$tmp['url']  = $alias['url'];
87
			$tmp['freq'] = $alias['updatefreq'];
88
			break;
89
		}
90
	}
91
}
92
93
# Determine if selected alias is either a bogons or URL table.
94
if (($tablename == "bogons") || ($tablename == "bogonsv6")) {
95
	$bogons = true;
96
} else if (preg_match('/urltable/i', $tmp['type'])) {
97
	$urltable = true;
98
} else {
99
	$bogons = $urltable = false;
100
}
101
102 5f601060 Phil Davis
if ($_REQUEST['delete']) {
103
	if (is_ipaddr($_REQUEST['delete']) || is_subnet($_REQUEST['delete'])) {
104 7afae53f Scott Ullrich
		exec("/sbin/pfctl -t " . escapeshellarg($_REQUEST['type']) . " -T delete " . escapeshellarg($_REQUEST['delete']), $delete);
105
		echo htmlentities($_REQUEST['delete']);
106
	}
107 e166769c Renato Botelho
	exit;
108 7afae53f Scott Ullrich
}
109
110 6f80b61e Phil Davis
if ($_POST['clearall']) {
111 e26e0eac jim-p
	exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
112 5f601060 Phil Davis
	if (is_array($entries)) {
113
		foreach ($entries as $entryA) {
114 7afae53f Scott Ullrich
			$entry = trim($entryA);
115
			exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T delete " . escapeshellarg($entry), $delete);
116
		}
117
	}
118 16424666 Phil Davis
	unset($entries);
119 7afae53f Scott Ullrich
}
120
121 f6622167 NOYB
if ($_POST['Download'] && ($bogons || $urltable)) {
122 3a652703 sbeaver
123 f6622167 NOYB
	if ($bogons) {				// If selected table is either bogons or bogonsv6.
124
		$mwexec_bg_cmd = '/etc/rc.update_bogons.sh now';
125
		$table_type = 'bogons';
126
		$db_name = 'bogons';
127
	} else if ($urltable) {		//  If selected table is a URL table alias.
128
		$mwexec_bg_cmd = '/etc/rc.update_urltables now forceupdate ' . $tablename;
129
		$table_type = 'urltables';
130
		$db_name = $tablename;
131
	}
132
133
	mwexec_bg($mwexec_bg_cmd);
134
	$maxtimetowait = 0;
135
	$loading = true;
136
	while ($loading == true) {
137
		$isrunning = `/bin/ps awwwux | /usr/bin/grep -v grep | /usr/bin/grep $table_type`;
138
		if ($isrunning == "") {
139
			$loading = false;
140 3a652703 sbeaver
		}
141 f6622167 NOYB
		$maxtimetowait++;
142
		if ($maxtimetowait > 89) {
143
			$loading = false;
144 947141fd Phil Davis
		}
145 f6622167 NOYB
		sleep(1);
146
	}
147
	if ($maxtimetowait < 90) {
148 d1855944 jim-p
		$savemsg = sprintf(gettext("The %s file contents have been updated."), $db_name);
149 6c474eb8 Warren Baker
	}
150
}
151
152 e26e0eac jim-p
exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
153 7afae53f Scott Ullrich
154
include("head.inc");
155 c054d8bc sbeaver
156 947141fd Phil Davis
if ($savemsg) {
157 e6f5c464 Stephen Beaver
	print_info_box($savemsg, 'success');
158 947141fd Phil Davis
}
159 ad9e2a90 sbeaver
160 060ed238 Stephen Beaver
if ($tablename == "webConfiguratorlockout") {
161 1176360c k-paulius
	$displayname = gettext("webConfigurator Lockout Table");
162 060ed238 Stephen Beaver
} else {
163 3d7a8696 k-paulius
	$displayname = sprintf(gettext("%s Table"), ucfirst($tablename));
164 060ed238 Stephen Beaver
}
165
166 e6f5c464 Stephen Beaver
$form = new Form(false);
167 3a652703 sbeaver
168 5f88f964 k-paulius
$section = new Form_Section('Table to Display');
169 e6f5c464 Stephen Beaver
$group = new Form_Group("Table");
170 ad9e2a90 sbeaver
171 e6f5c464 Stephen Beaver
$group->add(new Form_Select(
172 ad9e2a90 sbeaver
	'type',
173 e6f5c464 Stephen Beaver
	null,
174 ad9e2a90 sbeaver
	$tablename,
175
	array_combine($tables, $tables)
176 d1855944 jim-p
))->setHelp('Select a user-defined alias name or system table name to view its contents. <br/><br/>' .
177
	'Aliases become Tables when loaded into the active firewall ruleset. ' .
178
	'The contents displayed on this page reflect the current addresses inside tables used by the firewall.');
179 ad9e2a90 sbeaver
180 f6622167 NOYB
if ($bogons || $urltable || !empty($entries)) {
181
	if ($bogons || $urltable) {
182 e6f5c464 Stephen Beaver
		$group->add(new Form_Button(
183
			'Download',
184 faab522f Renato Botelho
			'Update',
185 37676f4e jim-p
			null,
186
			'fa-refresh'
187
		))->addClass('btn-success btn-sm');
188 e6f5c464 Stephen Beaver
	} elseif (!empty($entries)) {
189
		$group->add(new Form_Button(
190
			'clearall',
191 faab522f Renato Botelho
			'Empty Table',
192 37676f4e jim-p
			null,
193 8a3c6f0c jim-p
			'fa-trash'
194 37676f4e jim-p
		))->addClass('btn-danger btn-sm');
195 e6f5c464 Stephen Beaver
	}
196
}
197
198
$section->add($group);
199 ad9e2a90 sbeaver
$form->add($section);
200
print $form;
201 e6f5c464 Stephen Beaver
202 f6622167 NOYB
if ($bogons || $urltable || !empty($entries)) {
203 c57b2aad Phil Davis
?>
204
<div>
205 c95dabdd Stephen Beaver
	<div class="infoblock blockopen">
206 c57b2aad Phil Davis
<?php
207 f6622167 NOYB
	if ($bogons) {
208
		$table_file = '/etc/' . escapeshellarg($tablename);
209
	} else if ($urltable) {
210
		$table_file = '/var/db/aliastables/' . escapeshellarg($tablename) . '.txt';
211
	} else {
212
		$table_file = '';
213
	}
214
215 327052d0 NOYB
	$datestrregex = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun).* GMT';
216 f6622167 NOYB
	$datelineregex = 'last.*' . $datestrregex;
217
218
	$last_updated = exec('/usr/bin/grep -i -m 1 -E "^# ' . $datelineregex . '" ' . $table_file . '|/usr/bin/grep -i -m 1 -E -o "' . $datestrregex . '"');
219
220 c57b2aad Phil Davis
	if ($last_updated != "") {
221 8031655d NOYB
		$last_update_msg = sprintf(gettext("Table last updated on %s."), $last_updated);
222 c57b2aad Phil Davis
	} else {
223 8031655d NOYB
		$last_update_msg = gettext("Date of last update of table is unknown.");
224 c57b2aad Phil Davis
	}
225 8031655d NOYB
226
	$records_count_msg = sprintf(gettext("%s records."), number_format(count($entries), 0, gettext("."), gettext(",")));
227
228 f6622167 NOYB
	# Display up to 10 comment lines (lines that begin with '#').
229
	unset($comment_lines);
230
	$res = exec('/usr/bin/grep -i -m 10 -E "^#" ' . $table_file, $comment_lines);
231
232
	foreach ($comment_lines as $comment_line) {
233
		$table_comments .= "$comment_line" . "<br />";
234
	}
235
236
	if ($table_comments) {
237 f72e804a NOYB
		print_info_box($last_update_msg . " &nbsp; &nbsp; " . $records_count_msg . "<br />" .
238 f6622167 NOYB
		'<span style="display:none" class="infoblock">' . ' ' . gettext("Hide table comments.") . '<br />' . $table_comments . '</span>' .
239
		'<span style="display:none"   id="showtblcom">' . ' ' . gettext("Show table comments.") . '</span>' .
240
		'' , 'info', false);
241
	} else {
242
		print_info_box($last_update_msg . "&nbsp; &nbsp; " . $records_count_msg, 'info', false);
243
	}
244 c57b2aad Phil Davis
?>
245
	</div>
246
</div>
247
<?php
248 e6f5c464 Stephen Beaver
}
249 7afae53f Scott Ullrich
?>
250
251 8fd9052f Colin Fleming
<script type="text/javascript">
252
//<![CDATA[
253 947141fd Phil Davis
events.push(function() {
254 f6622167 NOYB
255
	$('#showtblcom').show();
256
257
	$('[id^="showinfo1"]').click(function() {
258
			$('#showtblcom').toggle();
259
	});
260
261 947141fd Phil Davis
	$('a[data-entry]').on('click', function() {
262 eb500b85 Sjon Hortensius
		var el = $(this);
263
264
		$.ajax(
265
			'/diag_tables.php',
266
			{
267
				type: 'post',
268
				data: {
269 6c989d4a jim-p
					type: '<?=htmlspecialchars(addslashes($tablename))?>',
270 eb500b85 Sjon Hortensius
					delete: $(this).data('entry')
271
				},
272 947141fd Phil Davis
				success: function() {
273 eb500b85 Sjon Hortensius
					el.parents('tr').remove();
274
				},
275 7afae53f Scott Ullrich
		});
276 eb500b85 Sjon Hortensius
	});
277 e6f5c464 Stephen Beaver
278
	// Auto-submit the form on table selector change
279
	$('#type').on('change', function() {
280
        $('form').submit();
281
    });
282 eb500b85 Sjon Hortensius
});
283 8fd9052f Colin Fleming
//]]>
284 7afae53f Scott Ullrich
</script>
285 e166769c Renato Botelho
286 4027d64e k-paulius
<?php
287
if (empty($entries)) {
288
	print_info_box(gettext("No entries exist in this table."), 'warning', false);
289
} else {
290
?>
291 060ed238 Stephen Beaver
<div class="panel panel-default">
292
	<div class="panel-heading"><h2 class="panel-title"><?=$displayname?></h2></div>
293
	<div class="panel-body">
294
		<div class="table-responsive">
295
			<table class="table table-striped table-hover table-condensed">
296
				<thead>
297
					<tr>
298
						<th><?=gettext("IP Address")?></th>
299
						<th></th>
300
					</tr>
301
				</thead>
302
				<tbody>
303 37f73a7c NOYB
<?php
304
		// This is a band-aid for a yet to be root caused performance issue with large tables.  Suspected is css and/or sorting.
305
 		if (count($entries) > 3000) {
306
			print "<tr><td colspan='2'><pre>";
307
			foreach ($entries as $entry) {
308
				$entry = trim($entry);
309
					print $entry . "\n";
310
			}
311
			print "</pre></td></tr>";
312
		} else {
313
?>
314 7afae53f Scott Ullrich
<?php
315 eb500b85 Sjon Hortensius
		foreach ($entries as $entry):
316
			$entry = trim($entry);
317 7afae53f Scott Ullrich
?>
318 060ed238 Stephen Beaver
					<tr>
319
						<td>
320
							<?=$entry?>
321
						</td>
322
						<td>
323 f6622167 NOYB
							<?php if (!$bogons && !$urltable): ?>
324 37676f4e jim-p
								<a style="cursor: pointer;" data-entry="<?=htmlspecialchars($entry)?>">
325
									<i class="fa fa-trash" title="<?= gettext("Remove this entry") ?>"></i>
326
								</a>
327 060ed238 Stephen Beaver
							<?php endif ?>
328
						</td>
329
					</tr>
330 eb500b85 Sjon Hortensius
<?php endforeach ?>
331 37f73a7c NOYB
<?php } ?>
332 060ed238 Stephen Beaver
				</tbody>
333
			</table>
334
		</div>
335
	</div>
336 c054d8bc sbeaver
</div>
337 060ed238 Stephen Beaver
338 3a652703 sbeaver
<?php
339 7c945f74 k-paulius
}
340 4027d64e k-paulius
341 7ac86a5f Colin Fleming
include("foot.inc");