Project

General

Profile

Download (9.75 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 Rubicon Communications, LLC (Netgate)
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
exec("/sbin/pfctl -sT", $tables);
67

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

    
71
if ($_REQUEST['type'] && in_array($_REQUEST['type'], $tables)) {
72
	$tablename = $_REQUEST['type'];
73
} else {
74
	/* Invalid 'type' passed, do not take any actions that use the 'type' field. */
75
	unset($_REQUEST['type']);
76
	$_REQUEST['delete'];
77
}
78

    
79
// 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
if ($_REQUEST['delete']) {
103
	if (is_ipaddr($_REQUEST['delete']) || is_subnet($_REQUEST['delete'])) {
104
		exec("/sbin/pfctl -t " . escapeshellarg($_REQUEST['type']) . " -T delete " . escapeshellarg($_REQUEST['delete']), $delete);
105
		echo htmlentities($_REQUEST['delete']);
106
	}
107
	exit;
108
}
109

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

    
121
if ($_POST['Download'] && ($bogons || $urltable)) {
122

    
123
	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
		}
141
		$maxtimetowait++;
142
		if ($maxtimetowait > 89) {
143
			$loading = false;
144
		}
145
		sleep(1);
146
	}
147
	if ($maxtimetowait < 90) {
148
		$savemsg = sprintf(gettext("The %s file contents have been updated."), $db_name);
149
	}
150
}
151

    
152
exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
153

    
154
include("head.inc");
155

    
156
if ($savemsg) {
157
	print_info_box($savemsg, 'success');
158
}
159

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

    
166
$form = new Form(false);
167

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

    
171
$group->add(new Form_Select(
172
	'type',
173
	null,
174
	$tablename,
175
	array_combine($tables, $tables)
176
))->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

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

    
198
$section->add($group);
199
$form->add($section);
200
print $form;
201

    
202
if ($bogons || $urltable || !empty($entries)) {
203
?>
204
<div>
205
	<div class="infoblock blockopen">
206
<?php
207
	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
	$datestrregex = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun).* GMT';
216
	$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
	if ($last_updated != "") {
221
		$last_update_msg = sprintf(gettext("Table last updated on %s."), $last_updated);
222
	} else {
223
		$last_update_msg = gettext("Date of last update of table is unknown.");
224
	}
225

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

    
228
	# 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
		print_info_box($last_update_msg . " &nbsp; &nbsp; " . $records_count_msg . "<br />" .
238
		'<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
?>
245
	</div>
246
</div>
247
<?php
248
}
249
?>
250

    
251
<script type="text/javascript">
252
//<![CDATA[
253
events.push(function() {
254

    
255
	$('#showtblcom').show();
256

    
257
	$('[id^="showinfo1"]').click(function() {
258
			$('#showtblcom').toggle();
259
	});
260

    
261
	$('a[data-entry]').on('click', function() {
262
		var el = $(this);
263

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

    
278
	// Auto-submit the form on table selector change
279
	$('#type').on('change', function() {
280
        $('form').submit();
281
    });
282
});
283
//]]>
284
</script>
285

    
286
<?php
287
if (empty($entries)) {
288
	print_info_box(gettext("No entries exist in this table."), 'warning', false);
289
} else {
290
?>
291
<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
<?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
<?php
315
		foreach ($entries as $entry):
316
			$entry = trim($entry);
317
?>
318
					<tr>
319
						<td>
320
							<?=$entry?>
321
						</td>
322
						<td>
323
							<?php if (!$bogons && !$urltable): ?>
324
								<a style="cursor: pointer;" data-entry="<?=htmlspecialchars($entry)?>">
325
									<i class="fa fa-trash" title="<?= gettext("Remove this entry") ?>"></i>
326
								</a>
327
							<?php endif ?>
328
						</td>
329
					</tr>
330
<?php endforeach ?>
331
<?php } ?>
332
				</tbody>
333
			</table>
334
		</div>
335
	</div>
336
</div>
337

    
338
<?php
339
}
340

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