1
|
<?php
|
2
|
/*
|
3
|
* diag_tables.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2004-2018 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 = "sshlockout";
|
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
|
exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
|
80
|
if (is_array($entries)) {
|
81
|
foreach ($entries as $entryA) {
|
82
|
$entry = trim($entryA);
|
83
|
exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T delete " . escapeshellarg($entry), $delete);
|
84
|
}
|
85
|
}
|
86
|
unset($entries);
|
87
|
}
|
88
|
|
89
|
if ($_POST['Download'] && ($bogons || $urltable)) {
|
90
|
|
91
|
if ($bogons) { // If selected table is either bogons or bogonsv6.
|
92
|
$mwexec_bg_cmd = '/etc/rc.update_bogons.sh now';
|
93
|
$table_type = 'bogons';
|
94
|
$db_name = 'bogons';
|
95
|
} else if ($urltable) { // If selected table is a URL table alias.
|
96
|
$mwexec_bg_cmd = '/etc/rc.update_urltables now forceupdate ' . $tablename;
|
97
|
$table_type = 'urltables';
|
98
|
$db_name = $tablename;
|
99
|
}
|
100
|
|
101
|
mwexec_bg($mwexec_bg_cmd);
|
102
|
$maxtimetowait = 0;
|
103
|
$loading = true;
|
104
|
while ($loading == true) {
|
105
|
$isrunning = `/bin/ps awwwux | /usr/bin/grep -v grep | /usr/bin/grep $table_type`;
|
106
|
if ($isrunning == "") {
|
107
|
$loading = false;
|
108
|
}
|
109
|
$maxtimetowait++;
|
110
|
if ($maxtimetowait > 89) {
|
111
|
$loading = false;
|
112
|
}
|
113
|
sleep(1);
|
114
|
}
|
115
|
if ($maxtimetowait < 90) {
|
116
|
$savemsg = sprintf(gettext("The %s file contents have been updated."), $db_name);
|
117
|
}
|
118
|
}
|
119
|
|
120
|
exec("/sbin/pfctl -t " . escapeshellarg($tablename) . " -T show", $entries);
|
121
|
|
122
|
include("head.inc");
|
123
|
|
124
|
if ($savemsg) {
|
125
|
print_info_box($savemsg, 'success');
|
126
|
}
|
127
|
|
128
|
if ($tablename == "webConfiguratorlockout") {
|
129
|
$displayname = gettext("webConfigurator Lockout Table");
|
130
|
} else {
|
131
|
$displayname = sprintf(gettext("%s Table"), ucfirst($tablename));
|
132
|
}
|
133
|
|
134
|
$form = new Form(false);
|
135
|
|
136
|
$section = new Form_Section('Table to Display');
|
137
|
$group = new Form_Group("Table");
|
138
|
|
139
|
$group->add(new Form_Select(
|
140
|
'type',
|
141
|
null,
|
142
|
$tablename,
|
143
|
array_combine($tables, $tables)
|
144
|
))->setHelp('Select a user-defined alias name or system table name to view its contents. %s' .
|
145
|
'Aliases become Tables when loaded into the active firewall ruleset. ' .
|
146
|
'The contents displayed on this page reflect the current addresses inside tables used by the firewall.', '<br/><br/>');
|
147
|
|
148
|
if ($bogons || $urltable || !empty($entries)) {
|
149
|
if ($bogons || $urltable) {
|
150
|
$group->add(new Form_Button(
|
151
|
'Download',
|
152
|
'Update',
|
153
|
null,
|
154
|
'fa-refresh'
|
155
|
))->addClass('btn-success btn-sm');
|
156
|
} elseif (!empty($entries)) {
|
157
|
$group->add(new Form_Button(
|
158
|
'clearall',
|
159
|
'Empty Table',
|
160
|
null,
|
161
|
'fa-trash'
|
162
|
))->addClass('btn-danger btn-sm');
|
163
|
}
|
164
|
}
|
165
|
|
166
|
$section->add($group);
|
167
|
$form->add($section);
|
168
|
print $form;
|
169
|
|
170
|
if ($bogons || $urltable || !empty($entries)) {
|
171
|
?>
|
172
|
<div>
|
173
|
<div class="infoblock blockopen">
|
174
|
<?php
|
175
|
if ($bogons) {
|
176
|
$table_file = '/etc/' . escapeshellarg($tablename);
|
177
|
} else if ($urltable) {
|
178
|
$table_file = '/var/db/aliastables/' . escapeshellarg($tablename) . '.txt';
|
179
|
} else {
|
180
|
$table_file = '';
|
181
|
}
|
182
|
|
183
|
$datestrregex = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun).* GMT';
|
184
|
$datelineregex = 'last.*' . $datestrregex;
|
185
|
|
186
|
$last_updated = exec('/usr/bin/grep -i -m 1 -E "^# ' . $datelineregex . '" ' . $table_file . '|/usr/bin/grep -i -m 1 -E -o "' . $datestrregex . '"');
|
187
|
|
188
|
if ($last_updated != "") {
|
189
|
$last_update_msg = sprintf(gettext("Table last updated on %s."), $last_updated);
|
190
|
} else {
|
191
|
$last_update_msg = gettext("Date of last update of table is unknown.");
|
192
|
}
|
193
|
|
194
|
$records_count_msg = sprintf(gettext("%s records."), number_format(count($entries), 0, gettext("."), gettext(",")));
|
195
|
|
196
|
# Display up to 10 comment lines (lines that begin with '#').
|
197
|
unset($comment_lines);
|
198
|
$res = exec('/usr/bin/grep -i -m 10 -E "^#" ' . $table_file, $comment_lines);
|
199
|
|
200
|
foreach ($comment_lines as $comment_line) {
|
201
|
$table_comments .= "$comment_line" . "<br />";
|
202
|
}
|
203
|
|
204
|
if ($table_comments) {
|
205
|
print_info_box($last_update_msg . " " . $records_count_msg . "<br />" .
|
206
|
'<span style="display:none" class="infoblock">' . ' ' . gettext("Hide table comments.") . '<br />' . $table_comments . '</span>' .
|
207
|
'<span style="display:none" id="showtblcom">' . ' ' . gettext("Show table comments.") . '</span>' .
|
208
|
'' , 'info', false);
|
209
|
} else {
|
210
|
print_info_box($last_update_msg . " " . $records_count_msg, 'info', false);
|
211
|
}
|
212
|
?>
|
213
|
</div>
|
214
|
</div>
|
215
|
<?php
|
216
|
}
|
217
|
?>
|
218
|
|
219
|
<script type="text/javascript">
|
220
|
//<![CDATA[
|
221
|
events.push(function() {
|
222
|
|
223
|
$('#showtblcom').show();
|
224
|
|
225
|
$('[id^="showinfo1"]').click(function() {
|
226
|
$('#showtblcom').toggle();
|
227
|
});
|
228
|
|
229
|
$('a[data-entry]').on('click', function() {
|
230
|
var el = $(this);
|
231
|
|
232
|
$.ajax(
|
233
|
'/diag_tables.php',
|
234
|
{
|
235
|
type: 'post',
|
236
|
data: {
|
237
|
type: '<?=htmlspecialchars(addslashes($tablename))?>',
|
238
|
delete: $(this).data('entry')
|
239
|
},
|
240
|
success: function() {
|
241
|
el.parents('tr').remove();
|
242
|
},
|
243
|
});
|
244
|
});
|
245
|
|
246
|
// Auto-submit the form on table selector change
|
247
|
$('#type').on('change', function() {
|
248
|
$('form').submit();
|
249
|
});
|
250
|
});
|
251
|
//]]>
|
252
|
</script>
|
253
|
|
254
|
<?php
|
255
|
if (empty($entries)) {
|
256
|
print_info_box(gettext("No entries exist in this table."), 'warning', false);
|
257
|
} else {
|
258
|
?>
|
259
|
<div class="panel panel-default">
|
260
|
<div class="panel-heading"><h2 class="panel-title"><?=$displayname?></h2></div>
|
261
|
<div class="panel-body">
|
262
|
<div class="table-responsive">
|
263
|
<table class="table table-striped table-hover table-condensed">
|
264
|
<thead>
|
265
|
<tr>
|
266
|
<th><?=gettext("IP Address")?></th>
|
267
|
<th></th>
|
268
|
</tr>
|
269
|
</thead>
|
270
|
<tbody>
|
271
|
<?php
|
272
|
// This is a band-aid for a yet to be root caused performance issue with large tables. Suspected is css and/or sorting.
|
273
|
if (count($entries) > 3000) {
|
274
|
print "<tr><td colspan='2'><pre>";
|
275
|
foreach ($entries as $entry) {
|
276
|
$entry = trim($entry);
|
277
|
print $entry . "\n";
|
278
|
}
|
279
|
print "</pre></td></tr>";
|
280
|
} else {
|
281
|
?>
|
282
|
<?php
|
283
|
foreach ($entries as $entry):
|
284
|
$entry = trim($entry);
|
285
|
?>
|
286
|
<tr>
|
287
|
<td>
|
288
|
<?=$entry?>
|
289
|
</td>
|
290
|
<td>
|
291
|
<?php if (!$bogons && !$urltable): ?>
|
292
|
<a style="cursor: pointer;" data-entry="<?=htmlspecialchars($entry)?>">
|
293
|
<i class="fa fa-trash" title="<?= gettext("Remove this entry") ?>"></i>
|
294
|
</a>
|
295
|
<?php endif ?>
|
296
|
</td>
|
297
|
</tr>
|
298
|
<?php endforeach ?>
|
299
|
<?php } ?>
|
300
|
</tbody>
|
301
|
</table>
|
302
|
</div>
|
303
|
</div>
|
304
|
</div>
|
305
|
|
306
|
<?php
|
307
|
}
|
308
|
|
309
|
include("foot.inc");
|