Project

General

Profile

Download (9.42 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
		))->removeClass('btn-primary')->addClass('btn-success btn-sm');
182
	} elseif (!empty($entries)) {
183
		$group->add(new Form_Button(
184
			'clearall',
185
			'Clear Table'
186
		))->removeClass('btn-primary')->addClass('btn-danger btn-sm');
187
	}
188
}
189

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
328
<?php
329
}
330

    
331
include("foot.inc");
(31-31/229)