Project

General

Profile

Download (9.27 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-2016 Electric Sheep Fencing, LLC
7
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright notice,
13
 *    this list of conditions and the following disclaimer.
14
 *
15
 * 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
 *
20
 * 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
 *
25
 * 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
 *
30
 * 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
 *
34
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36
 *
37
 * "This product includes software developed by the pfSense Project
38
 * for use in the pfSense software distribution (http://www.pfsense.org/).
39
 *
40
 * 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
 */
53

    
54
##|+PRIV
55
##|*IDENT=page-diagnostics-tables
56
##|*NAME=Diagnostics: pf Table IP addresses
57
##|*DESCR=Allow access to the 'Diagnostics: Tables' page.
58
##|*MATCH=diag_tables.php*
59
##|-PRIV
60

    
61
$pgtitle = array(gettext("Diagnostics"), gettext("Tables"));
62
$shortcut_section = "aliases";
63

    
64
require_once("guiconfig.inc");
65

    
66
// Set default table
67
$tablename = "sshlockout";
68

    
69
if ($_REQUEST['type']) {
70
	$tablename = $_REQUEST['type'];
71
}
72

    
73
// Gather selected alias metadata.
74
if (isset($config['aliases']['alias'])) {
75
	foreach ($config['aliases']['alias'] as $alias) {
76
		if ( $alias['name'] == $tablename ) {
77
			$tmp = array();
78
			$tmp['type'] = $alias['type'];
79
			$tmp['name'] = $alias['name'];
80
			$tmp['url']  = $alias['url'];
81
			$tmp['freq'] = $alias['updatefreq'];
82
			break;
83
		}
84
	}
85
}
86

    
87
# Determine if selected alias is either a bogons or URL table.
88
if (($tablename == "bogons") || ($tablename == "bogonsv6")) {
89
	$bogons = true;
90
} else if (preg_match('/urltable/i', $tmp['type'])) {
91
	$urltable = true;
92
} else {
93
	$bogons = $urltable = false;
94
}
95

    
96
if ($_REQUEST['delete']) {
97
	if (is_ipaddr($_REQUEST['delete']) || is_subnet($_REQUEST['delete'])) {
98
		exec("/sbin/pfctl -t " . escapeshellarg($_REQUEST['type']) . " -T delete " . escapeshellarg($_REQUEST['delete']), $delete);
99
		echo htmlentities($_REQUEST['delete']);
100
	}
101
	exit;
102
}
103

    
104
if ($_POST['clearall']) {
105
	exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
106
	if (is_array($entries)) {
107
		foreach ($entries as $entryA) {
108
			$entry = trim($entryA);
109
			exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T delete " . escapeshellarg($entry), $delete);
110
		}
111
	}
112
	unset($entries);
113
}
114

    
115
if ($_POST['Download'] && ($bogons || $urltable)) {
116

    
117
	if ($bogons) {				// If selected table is either bogons or bogonsv6.
118
		$mwexec_bg_cmd = '/etc/rc.update_bogons.sh now';
119
		$table_type = 'bogons';
120
		$db_name = 'bogons';
121
	} else if ($urltable) {		//  If selected table is a URL table alias.
122
		$mwexec_bg_cmd = '/etc/rc.update_urltables now forceupdate ' . $tablename;
123
		$table_type = 'urltables';
124
		$db_name = $tablename;
125
	}
126

    
127
	mwexec_bg($mwexec_bg_cmd);
128
	$maxtimetowait = 0;
129
	$loading = true;
130
	while ($loading == true) {
131
		$isrunning = `/bin/ps awwwux | /usr/bin/grep -v grep | /usr/bin/grep $table_type`;
132
		if ($isrunning == "") {
133
			$loading = false;
134
		}
135
		$maxtimetowait++;
136
		if ($maxtimetowait > 89) {
137
			$loading = false;
138
		}
139
		sleep(1);
140
	}
141
	if ($maxtimetowait < 90) {
142
		$savemsg = sprintf(gettext("The %s database has been updated."), $db_name);
143
	}
144
}
145

    
146
exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
147
exec("/sbin/pfctl -sT", $tables);
148

    
149
include("head.inc");
150

    
151
if ($savemsg) {
152
	print_info_box($savemsg, 'success');
153
}
154

    
155
if ($tablename == "webConfiguratorlockout") {
156
	$displayname = gettext("webConfigurator Lockout Table");
157
} else {
158
	$displayname = sprintf(gettext("%s Table"), ucfirst($tablename));
159
}
160

    
161
$form = new Form(false);
162

    
163
$section = new Form_Section('Table to Display');
164
$group = new Form_Group("Table");
165

    
166
$group->add(new Form_Select(
167
	'type',
168
	null,
169
	$tablename,
170
	array_combine($tables, $tables)
171
));
172

    
173
if ($bogons || $urltable || !empty($entries)) {
174
	if ($bogons || $urltable) {
175
		$group->add(new Form_Button(
176
			'Download',
177
			'Update',
178
			null,
179
			'fa-refresh'
180
		))->addClass('btn-success btn-sm');
181
	} elseif (!empty($entries)) {
182
		$group->add(new Form_Button(
183
			'clearall',
184
			'Empty Table',
185
			null,
186
			'fa-trash'
187
		))->addClass('btn-danger btn-sm');
188
	}
189
}
190

    
191
$section->add($group);
192
$form->add($section);
193
print $form;
194

    
195
if ($bogons || $urltable || !empty($entries)) {
196
?>
197
<div>
198
	<div class="infoblock blockopen">
199
<?php
200
	if ($bogons) {
201
		$table_file = '/etc/' . escapeshellarg($tablename);
202
	} else if ($urltable) {
203
		$table_file = '/var/db/aliastables/' . escapeshellarg($tablename) . '.txt';
204
	} else {
205
		$table_file = '';
206
	}
207

    
208
	$datestrregex = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun).* GMT';
209
	$datelineregex = 'last.*' . $datestrregex;
210

    
211
	$last_updated = exec('/usr/bin/grep -i -m 1 -E "^# ' . $datelineregex . '" ' . $table_file . '|/usr/bin/grep -i -m 1 -E -o "' . $datestrregex . '"');
212

    
213
	if ($last_updated != "") {
214
		$last_update_msg = sprintf(gettext("Table last updated on %s."), $last_updated);
215
	} else {
216
		$last_update_msg = gettext("Date of last update of table is unknown.");
217
	}
218

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

    
221
	# Display up to 10 comment lines (lines that begin with '#').
222
	unset($comment_lines);
223
	$res = exec('/usr/bin/grep -i -m 10 -E "^#" ' . $table_file, $comment_lines);
224

    
225
	foreach ($comment_lines as $comment_line) {
226
		$table_comments .= "$comment_line" . "<br />";
227
	}
228

    
229
	if ($table_comments) {
230
		print_info_box($last_update_msg . " &nbsp; &nbsp; " . $records_count_msg . "<br />" .
231
		'<span style="display:none" class="infoblock">' . ' ' . gettext("Hide table comments.") . '<br />' . $table_comments . '</span>' .
232
		'<span style="display:none"   id="showtblcom">' . ' ' . gettext("Show table comments.") . '</span>' .
233
		'' , 'info', false);
234
	} else {
235
		print_info_box($last_update_msg . "&nbsp; &nbsp; " . $records_count_msg, 'info', false);
236
	}
237
?>
238
	</div>
239
</div>
240
<?php
241
}
242
?>
243

    
244
<script type="text/javascript">
245
//<![CDATA[
246
events.push(function() {
247

    
248
	$('#showtblcom').show();
249

    
250
	$('[id^="showinfo1"]').click(function() {
251
			$('#showtblcom').toggle();
252
	});
253

    
254
	$('a[data-entry]').on('click', function() {
255
		var el = $(this);
256

    
257
		$.ajax(
258
			'/diag_tables.php',
259
			{
260
				type: 'post',
261
				data: {
262
					type: '<?=htmlspecialchars($tablename)?>',
263
					delete: $(this).data('entry')
264
				},
265
				success: function() {
266
					el.parents('tr').remove();
267
				},
268
		});
269
	});
270

    
271
	// Auto-submit the form on table selector change
272
	$('#type').on('change', function() {
273
        $('form').submit();
274
    });
275
});
276
//]]>
277
</script>
278

    
279
<?php
280
if (empty($entries)) {
281
	print_info_box(gettext("No entries exist in this table."), 'warning', false);
282
} else {
283
?>
284
<div class="panel panel-default">
285
	<div class="panel-heading"><h2 class="panel-title"><?=$displayname?></h2></div>
286
	<div class="panel-body">
287
		<div class="table-responsive">
288
			<table class="table table-striped table-hover table-condensed">
289
				<thead>
290
					<tr>
291
						<th><?=gettext("IP Address")?></th>
292
						<th></th>
293
					</tr>
294
				</thead>
295
				<tbody>
296
<?php
297
		// This is a band-aid for a yet to be root caused performance issue with large tables.  Suspected is css and/or sorting.
298
 		if (count($entries) > 3000) {
299
			print "<tr><td colspan='2'><pre>";
300
			foreach ($entries as $entry) {
301
				$entry = trim($entry);
302
					print $entry . "\n";
303
			}
304
			print "</pre></td></tr>";
305
		} else {
306
?>
307
<?php
308
		foreach ($entries as $entry):
309
			$entry = trim($entry);
310
?>
311
					<tr>
312
						<td>
313
							<?=$entry?>
314
						</td>
315
						<td>
316
							<?php if (!$bogons && !$urltable): ?>
317
								<a style="cursor: pointer;" data-entry="<?=htmlspecialchars($entry)?>">
318
									<i class="fa fa-trash" title="<?= gettext("Remove this entry") ?>"></i>
319
								</a>
320
							<?php endif ?>
321
						</td>
322
					</tr>
323
<?php endforeach ?>
324
<?php } ?>
325
				</tbody>
326
			</table>
327
		</div>
328
	</div>
329
</div>
330

    
331
<?php
332
}
333

    
334
include("foot.inc");
(30-30/225)