Project

General

Profile

Download (7.61 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
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

    
22
##|+PRIV
23
##|*IDENT=page-diagnostics-tables
24
##|*NAME=Diagnostics: pf Table IP addresses
25
##|*DESCR=Allow access to the 'Diagnostics: Tables' page.
26
##|*MATCH=diag_tables.php*
27
##|-PRIV
28

    
29
$pgtitle = array(gettext("Diagnostics"), gettext("Tables"));
30
$shortcut_section = "aliases";
31

    
32
require_once("guiconfig.inc");
33

    
34
// Set default table
35
$tablename = "sshlockout";
36

    
37
if ($_REQUEST['type']) {
38
	$tablename = $_REQUEST['type'];
39
}
40

    
41
// Gather selected alias metadata.
42
if (isset($config['aliases']['alias'])) {
43
	foreach ($config['aliases']['alias'] as $alias) {
44
		if ( $alias['name'] == $tablename ) {
45
			$tmp = array();
46
			$tmp['type'] = $alias['type'];
47
			$tmp['name'] = $alias['name'];
48
			$tmp['url']  = $alias['url'];
49
			$tmp['freq'] = $alias['updatefreq'];
50
			break;
51
		}
52
	}
53
}
54

    
55
# Determine if selected alias is either a bogons or URL table.
56
if (($tablename == "bogons") || ($tablename == "bogonsv6")) {
57
	$bogons = true;
58
} else if (preg_match('/urltable/i', $tmp['type'])) {
59
	$urltable = true;
60
} else {
61
	$bogons = $urltable = false;
62
}
63

    
64
if ($_REQUEST['delete']) {
65
	if (is_ipaddr($_REQUEST['delete']) || is_subnet($_REQUEST['delete'])) {
66
		exec("/sbin/pfctl -t " . escapeshellarg($_REQUEST['type']) . " -T delete " . escapeshellarg($_REQUEST['delete']), $delete);
67
		echo htmlentities($_REQUEST['delete']);
68
	}
69
	exit;
70
}
71

    
72
if ($_POST['clearall']) {
73
	exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
74
	if (is_array($entries)) {
75
		foreach ($entries as $entryA) {
76
			$entry = trim($entryA);
77
			exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T delete " . escapeshellarg($entry), $delete);
78
		}
79
	}
80
	unset($entries);
81
}
82

    
83
if ($_POST['Download'] && ($bogons || $urltable)) {
84

    
85
	if ($bogons) {				// If selected table is either bogons or bogonsv6.
86
		$mwexec_bg_cmd = '/etc/rc.update_bogons.sh now';
87
		$table_type = 'bogons';
88
		$db_name = 'bogons';
89
	} else if ($urltable) {		//  If selected table is a URL table alias.
90
		$mwexec_bg_cmd = '/etc/rc.update_urltables now forceupdate ' . $tablename;
91
		$table_type = 'urltables';
92
		$db_name = $tablename;
93
	}
94

    
95
	mwexec_bg($mwexec_bg_cmd);
96
	$maxtimetowait = 0;
97
	$loading = true;
98
	while ($loading == true) {
99
		$isrunning = `/bin/ps awwwux | /usr/bin/grep -v grep | /usr/bin/grep $table_type`;
100
		if ($isrunning == "") {
101
			$loading = false;
102
		}
103
		$maxtimetowait++;
104
		if ($maxtimetowait > 89) {
105
			$loading = false;
106
		}
107
		sleep(1);
108
	}
109
	if ($maxtimetowait < 90) {
110
		$savemsg = sprintf(gettext("The %s database has been updated."), $db_name);
111
	}
112
}
113

    
114
exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
115
exec("/sbin/pfctl -sT", $tables);
116

    
117
include("head.inc");
118

    
119
if ($savemsg) {
120
	print_info_box($savemsg, 'success');
121
}
122

    
123
if ($tablename == "webConfiguratorlockout") {
124
	$displayname = gettext("webConfigurator Lockout Table");
125
} else {
126
	$displayname = sprintf(gettext("%s Table"), ucfirst($tablename));
127
}
128

    
129
$form = new Form(false);
130

    
131
$section = new Form_Section('Table to Display');
132
$group = new Form_Group("Table");
133

    
134
$group->add(new Form_Select(
135
	'type',
136
	null,
137
	$tablename,
138
	array_combine($tables, $tables)
139
));
140

    
141
if ($bogons || $urltable || !empty($entries)) {
142
	if ($bogons || $urltable) {
143
		$group->add(new Form_Button(
144
			'Download',
145
			'Update',
146
			null,
147
			'fa-refresh'
148
		))->addClass('btn-success btn-sm');
149
	} elseif (!empty($entries)) {
150
		$group->add(new Form_Button(
151
			'clearall',
152
			'Empty Table',
153
			null,
154
			'fa-trash'
155
		))->addClass('btn-danger btn-sm');
156
	}
157
}
158

    
159
$section->add($group);
160
$form->add($section);
161
print $form;
162

    
163
if ($bogons || $urltable || !empty($entries)) {
164
?>
165
<div>
166
	<div class="infoblock blockopen">
167
<?php
168
	if ($bogons) {
169
		$table_file = '/etc/' . escapeshellarg($tablename);
170
	} else if ($urltable) {
171
		$table_file = '/var/db/aliastables/' . escapeshellarg($tablename) . '.txt';
172
	} else {
173
		$table_file = '';
174
	}
175

    
176
	$datestrregex = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun).* GMT';
177
	$datelineregex = 'last.*' . $datestrregex;
178

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

    
181
	if ($last_updated != "") {
182
		$last_update_msg = sprintf(gettext("Table last updated on %s."), $last_updated);
183
	} else {
184
		$last_update_msg = gettext("Date of last update of table is unknown.");
185
	}
186

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

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

    
193
	foreach ($comment_lines as $comment_line) {
194
		$table_comments .= "$comment_line" . "<br />";
195
	}
196

    
197
	if ($table_comments) {
198
		print_info_box($last_update_msg . " &nbsp; &nbsp; " . $records_count_msg . "<br />" .
199
		'<span style="display:none" class="infoblock">' . ' ' . gettext("Hide table comments.") . '<br />' . $table_comments . '</span>' .
200
		'<span style="display:none"   id="showtblcom">' . ' ' . gettext("Show table comments.") . '</span>' .
201
		'' , 'info', false);
202
	} else {
203
		print_info_box($last_update_msg . "&nbsp; &nbsp; " . $records_count_msg, 'info', false);
204
	}
205
?>
206
	</div>
207
</div>
208
<?php
209
}
210
?>
211

    
212
<script type="text/javascript">
213
//<![CDATA[
214
events.push(function() {
215

    
216
	$('#showtblcom').show();
217

    
218
	$('[id^="showinfo1"]').click(function() {
219
			$('#showtblcom').toggle();
220
	});
221

    
222
	$('a[data-entry]').on('click', function() {
223
		var el = $(this);
224

    
225
		$.ajax(
226
			'/diag_tables.php',
227
			{
228
				type: 'post',
229
				data: {
230
					type: '<?=htmlspecialchars($tablename)?>',
231
					delete: $(this).data('entry')
232
				},
233
				success: function() {
234
					el.parents('tr').remove();
235
				},
236
		});
237
	});
238

    
239
	// Auto-submit the form on table selector change
240
	$('#type').on('change', function() {
241
        $('form').submit();
242
    });
243
});
244
//]]>
245
</script>
246

    
247
<?php
248
if (empty($entries)) {
249
	print_info_box(gettext("No entries exist in this table."), 'warning', false);
250
} else {
251
?>
252
<div class="panel panel-default">
253
	<div class="panel-heading"><h2 class="panel-title"><?=$displayname?></h2></div>
254
	<div class="panel-body">
255
		<div class="table-responsive">
256
			<table class="table table-striped table-hover table-condensed">
257
				<thead>
258
					<tr>
259
						<th><?=gettext("IP Address")?></th>
260
						<th></th>
261
					</tr>
262
				</thead>
263
				<tbody>
264
<?php
265
		// This is a band-aid for a yet to be root caused performance issue with large tables.  Suspected is css and/or sorting.
266
 		if (count($entries) > 3000) {
267
			print "<tr><td colspan='2'><pre>";
268
			foreach ($entries as $entry) {
269
				$entry = trim($entry);
270
					print $entry . "\n";
271
			}
272
			print "</pre></td></tr>";
273
		} else {
274
?>
275
<?php
276
		foreach ($entries as $entry):
277
			$entry = trim($entry);
278
?>
279
					<tr>
280
						<td>
281
							<?=$entry?>
282
						</td>
283
						<td>
284
							<?php if (!$bogons && !$urltable): ?>
285
								<a style="cursor: pointer;" data-entry="<?=htmlspecialchars($entry)?>">
286
									<i class="fa fa-trash" title="<?= gettext("Remove this entry") ?>"></i>
287
								</a>
288
							<?php endif ?>
289
						</td>
290
					</tr>
291
<?php endforeach ?>
292
<?php } ?>
293
				</tbody>
294
			</table>
295
		</div>
296
	</div>
297
</div>
298

    
299
<?php
300
}
301

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