Project

General

Profile

Download (9.56 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
// 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 file contents have 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
))->setHelp('Select a user-defined alias name or system table name to view its contents. <br/><br/>' .
172
	'Aliases become Tables when loaded into the active firewall ruleset. ' .
173
	'The contents displayed on this page reflect the current addresses inside tables used by the firewall.');
174

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
333
<?php
334
}
335

    
336
include("foot.inc");
(30-30/227)