Project

General

Profile

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

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

    
161
$section->add($group);
162
$form->add($section);
163
print $form;
164

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

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

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

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

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

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

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

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

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

    
218
	$('#showtblcom').show();
219

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

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

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

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

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

    
301
<?php
302
}
303

    
304
include("foot.inc");
(29-29/225)