Project

General

Profile

Download (9.47 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_tables.php
4
*/
5
/* ====================================================================
6
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *	Portions borrowed from diag_dump_states.php
8
 *
9
 *  Redistribution and use in source and binary forms, with or without modification,
10
 *  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
 *
55
 */
56

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

    
64
$pgtitle = array(gettext("Diagnostics"), gettext("Tables"));
65
$shortcut_section = "aliases";
66

    
67
require_once("guiconfig.inc");
68

    
69
// Set default table
70
$tablename = "sshlockout";
71

    
72
if ($_REQUEST['type']) {
73
	$tablename = $_REQUEST['type'];
74
}
75

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

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

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

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

    
118
if ($_POST['Download'] && ($bogons || $urltable)) {
119

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

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

    
149
exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
150
exec("/sbin/pfctl -sT", $tables);
151

    
152
include("head.inc");
153

    
154
if ($savemsg) {
155
	print_info_box($savemsg, 'success');
156
}
157

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

    
164
$form = new Form(false);
165

    
166
$section = new Form_Section('Table to Display');
167
$group = new Form_Group("Table");
168

    
169
$group->add(new Form_Select(
170
	'type',
171
	null,
172
	$tablename,
173
	array_combine($tables, $tables)
174
));
175

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

    
194
$section->add($group);
195
$form->add($section);
196
print $form;
197

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

    
211
	$datestrregex = '(Mon|Tue|Wed|Thr|Fri|Sat|Sun).* GMT';
212
	$datelineregex = 'last.*' . $datestrregex;
213

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

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

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

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

    
228
	foreach ($comment_lines as $comment_line) {
229
		$table_comments .= "$comment_line" . "<br />";
230
	}
231

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

    
247
<script type="text/javascript">
248
//<![CDATA[
249
events.push(function() {
250

    
251
	$('#showtblcom').show();
252

    
253
	$('[id^="showinfo1"]').click(function() {
254
			$('#showtblcom').toggle();
255
	});
256

    
257
	$('a[data-entry]').on('click', function() {
258
		var el = $(this);
259

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

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

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

    
334
<?php
335
}
336

    
337
include("foot.inc");
(31-31/225)