Project

General

Profile

Download (8.11 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-2019 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
exec("/sbin/pfctl -sT", $tables);
35

    
36
// Set default table
37
$tablename = "sshguard";
38

    
39
if ($_REQUEST['type'] && in_array($_REQUEST['type'], $tables)) {
40
	$tablename = $_REQUEST['type'];
41
} else {
42
	/* Invalid 'type' passed, do not take any actions that use the 'type' field. */
43
	unset($_REQUEST['type']);
44
	$_REQUEST['delete'];
45
}
46

    
47
// Gather selected alias metadata.
48
if (isset($config['aliases']['alias'])) {
49
	foreach ($config['aliases']['alias'] as $alias) {
50
		if ( $alias['name'] == $tablename ) {
51
			$tmp = array();
52
			$tmp['type'] = $alias['type'];
53
			$tmp['name'] = $alias['name'];
54
			$tmp['url']  = $alias['url'];
55
			$tmp['freq'] = $alias['updatefreq'];
56
			break;
57
		}
58
	}
59
}
60

    
61
# Determine if selected alias is either a bogons or URL table.
62
if (($tablename == "bogons") || ($tablename == "bogonsv6")) {
63
	$bogons = true;
64
} else if (preg_match('/urltable/i', $tmp['type'])) {
65
	$urltable = true;
66
} else {
67
	$bogons = $urltable = false;
68
}
69

    
70
if ($_REQUEST['delete']) {
71
	if (is_ipaddr($_REQUEST['delete']) || is_subnet($_REQUEST['delete'])) {
72
		exec("/sbin/pfctl -t " . escapeshellarg($_REQUEST['type']) . " -T delete " . escapeshellarg($_REQUEST['delete']), $delete);
73
		echo htmlentities($_REQUEST['delete']);
74
	}
75
	exit;
76
}
77

    
78
if ($_POST['clearall']) {
79
	$entries = array();
80
	exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
81
	if (is_array($entries)) {
82
		foreach ($entries as $entryA) {
83
			$entry = trim($entryA);
84
			exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T delete " . escapeshellarg($entry), $delete);
85
		}
86
	}
87
	unset($entries);
88
}
89

    
90
if ($_POST['Download'] && ($bogons || $urltable)) {
91

    
92
	if ($bogons) {				// If selected table is either bogons or bogonsv6.
93
		$mwexec_bg_cmd = '/etc/rc.update_bogons.sh now';
94
		$table_type = 'bogons';
95
		$db_name = 'bogons';
96
	} else if ($urltable) {		//  If selected table is a URL table alias.
97
		$mwexec_bg_cmd = '/etc/rc.update_urltables now forceupdate ' . $tablename;
98
		$table_type = 'urltables';
99
		$db_name = $tablename;
100
	}
101

    
102
	mwexec_bg($mwexec_bg_cmd);
103
	$maxtimetowait = 0;
104
	$loading = true;
105
	while ($loading == true) {
106
		$isrunning = `/bin/ps awwwux | /usr/bin/grep -v grep | /usr/bin/grep $table_type`;
107
		if ($isrunning == "") {
108
			$loading = false;
109
		}
110
		$maxtimetowait++;
111
		if ($maxtimetowait > 89) {
112
			$loading = false;
113
		}
114
		sleep(1);
115
	}
116
	if ($maxtimetowait < 90) {
117
		$savemsg = sprintf(gettext("The %s file contents have been updated."), $db_name);
118
	}
119
}
120

    
121
$entries = array();
122
exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
123

    
124
include("head.inc");
125

    
126
if ($savemsg) {
127
	print_info_box($savemsg, 'success');
128
}
129

    
130
if ($tablename == "sshguard") {
131
	$displayname = gettext("SSH and GUI Lockout Table");
132
} else {
133
	$displayname = sprintf(gettext("%s Table"), ucfirst($tablename));
134
}
135

    
136
$form = new Form(false);
137

    
138
$section = new Form_Section('Table to Display');
139
$group = new Form_Group("Table");
140

    
141
$group->add(new Form_Select(
142
	'type',
143
	null,
144
	$tablename,
145
	array_combine($tables, $tables)
146
))->setHelp('Select a user-defined alias name or system table name to view its contents. %s' .
147
	'Aliases become Tables when loaded into the active firewall ruleset. ' .
148
	'The contents displayed on this page reflect the current addresses inside tables used by the firewall.', '<br/><br/>');
149

    
150
if ($bogons || $urltable || !empty($entries)) {
151
	if ($bogons || $urltable) {
152
		$group->add(new Form_Button(
153
			'Download',
154
			'Update',
155
			null,
156
			'fa-refresh'
157
		))->addClass('btn-success btn-sm');
158
	} elseif (!empty($entries)) {
159
		$group->add(new Form_Button(
160
			'clearall',
161
			'Empty Table',
162
			null,
163
			'fa-trash'
164
		))->addClass('btn-danger btn-sm');
165
	}
166
}
167

    
168
$section->add($group);
169
$form->add($section);
170
print $form;
171

    
172
if ($bogons || $urltable || !empty($entries)) {
173
?>
174
<div>
175
	<div class="infoblock blockopen">
176
<?php
177
	if ($bogons) {
178
		$table_file = '/etc/' . escapeshellarg($tablename);
179
	} else if ($urltable) {
180
		$table_file = '/var/db/aliastables/' . escapeshellarg($tablename) . '.txt';
181
	} else {
182
		$table_file = '';
183
	}
184

    
185
	$datestrregex = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun).* GMT';
186
	$datelineregex = 'last.*' . $datestrregex;
187

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

    
190
	if ($last_updated != "") {
191
		$last_update_msg = sprintf(gettext("Table last updated on %s."), $last_updated);
192
	} else {
193
		$last_update_msg = gettext("Date of last update of table is unknown.");
194
	}
195

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

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

    
202
	foreach ($comment_lines as $comment_line) {
203
		$table_comments .= "$comment_line" . "<br />";
204
	}
205

    
206
	if ($table_comments) {
207
		print_info_box($last_update_msg . " &nbsp; &nbsp; " . $records_count_msg . "<br />" .
208
		'<span style="display:none" class="infoblock">' . ' ' . gettext("Hide table comments.") . '<br />' . $table_comments . '</span>' .
209
		'<span style="display:none"   id="showtblcom">' . ' ' . gettext("Show table comments.") . '</span>' .
210
		'' , 'info', false);
211
	} else {
212
		print_info_box($last_update_msg . "&nbsp; &nbsp; " . $records_count_msg, 'info', false);
213
	}
214
?>
215
	</div>
216
</div>
217
<?php
218
}
219
?>
220

    
221
<script type="text/javascript">
222
//<![CDATA[
223
events.push(function() {
224

    
225
	$('#showtblcom').show();
226

    
227
	$('[id^="showinfo1"]').click(function() {
228
			$('#showtblcom').toggle();
229
	});
230

    
231
	$('a[data-entry]').on('click', function() {
232
		var el = $(this);
233

    
234
		$.ajax(
235
			'/diag_tables.php',
236
			{
237
				type: 'post',
238
				data: {
239
					type: '<?=htmlspecialchars(addslashes($tablename))?>',
240
					delete: $(this).data('entry')
241
				},
242
				success: function() {
243
					el.parents('tr').remove();
244
				},
245
		});
246
	});
247

    
248
	// Auto-submit the form on table selector change
249
	$('#type').on('change', function() {
250
        $('form').submit();
251
    });
252
});
253
//]]>
254
</script>
255

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

    
308
<?php
309
}
310

    
311
include("foot.inc");
(33-33/235)