1
|
<?php
|
2
|
/*
|
3
|
* firewall_aliases.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
|
* originally based on m0n0wall (http://m0n0.ch/wall)
|
10
|
* Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
11
|
* All rights reserved.
|
12
|
*
|
13
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
14
|
* you may not use this file except in compliance with the License.
|
15
|
* You may obtain a copy of the License at
|
16
|
*
|
17
|
* http://www.apache.org/licenses/LICENSE-2.0
|
18
|
*
|
19
|
* Unless required by applicable law or agreed to in writing, software
|
20
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
21
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
22
|
* See the License for the specific language governing permissions and
|
23
|
* limitations under the License.
|
24
|
*/
|
25
|
|
26
|
##|+PRIV
|
27
|
##|*IDENT=page-firewall-aliases
|
28
|
##|*NAME=Firewall: Aliases
|
29
|
##|*DESCR=Allow access to the 'Firewall: Aliases' page.
|
30
|
##|*MATCH=firewall_aliases.php*
|
31
|
##|-PRIV
|
32
|
|
33
|
require_once("guiconfig.inc");
|
34
|
require_once("functions.inc");
|
35
|
require_once("filter.inc");
|
36
|
require_once("shaper.inc");
|
37
|
|
38
|
if (!is_array($config['aliases']['alias'])) {
|
39
|
$config['aliases']['alias'] = array();
|
40
|
}
|
41
|
$a_aliases = &$config['aliases']['alias'];
|
42
|
|
43
|
$tab = ($_REQUEST['tab'] == "" ? "ip" : preg_replace("/\W/", "", $_REQUEST['tab']));
|
44
|
|
45
|
if ($_POST) {
|
46
|
|
47
|
if ($_POST['apply']) {
|
48
|
$retval = 0;
|
49
|
|
50
|
/* reload all components that use aliases */
|
51
|
$retval = filter_configure();
|
52
|
|
53
|
if (stristr($retval, "error") <> true) {
|
54
|
$savemsg = get_std_save_message($retval);
|
55
|
$class = "success";
|
56
|
} else {
|
57
|
$savemsg = $retval;
|
58
|
$class = "danger";
|
59
|
}
|
60
|
if ($retval == 0) {
|
61
|
clear_subsystem_dirty('aliases');
|
62
|
}
|
63
|
}
|
64
|
}
|
65
|
|
66
|
if ($_GET['act'] == "del") {
|
67
|
if ($a_aliases[$_GET['id']]) {
|
68
|
/* make sure rule is not being referenced by any nat or filter rules */
|
69
|
$is_alias_referenced = false;
|
70
|
$referenced_by = false;
|
71
|
$alias_name = $a_aliases[$_GET['id']]['name'];
|
72
|
// Firewall rules
|
73
|
find_alias_reference(array('filter', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
|
74
|
find_alias_reference(array('filter', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
|
75
|
find_alias_reference(array('filter', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
|
76
|
find_alias_reference(array('filter', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
|
77
|
// NAT Rules
|
78
|
find_alias_reference(array('nat', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
|
79
|
find_alias_reference(array('nat', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
|
80
|
find_alias_reference(array('nat', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
|
81
|
find_alias_reference(array('nat', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
|
82
|
find_alias_reference(array('nat', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
|
83
|
find_alias_reference(array('nat', 'rule'), array('local-port'), $alias_name, $is_alias_referenced, $referenced_by);
|
84
|
// NAT 1:1 Rules
|
85
|
//find_alias_reference(array('nat', 'onetoone'), array('external'), $alias_name, $is_alias_referenced, $referenced_by);
|
86
|
//find_alias_reference(array('nat', 'onetoone'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
|
87
|
find_alias_reference(array('nat', 'onetoone'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
|
88
|
// NAT Outbound Rules
|
89
|
find_alias_reference(array('nat', 'outbound', 'rule'), array('source', 'network'), $alias_name, $is_alias_referenced, $referenced_by);
|
90
|
find_alias_reference(array('nat', 'outbound', 'rule'), array('sourceport'), $alias_name, $is_alias_referenced, $referenced_by);
|
91
|
find_alias_reference(array('nat', 'outbound', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
|
92
|
find_alias_reference(array('nat', 'outbound', 'rule'), array('dstport'), $alias_name, $is_alias_referenced, $referenced_by);
|
93
|
find_alias_reference(array('nat', 'outbound', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
|
94
|
// Alias in an alias
|
95
|
find_alias_reference(array('aliases', 'alias'), array('address'), $alias_name, $is_alias_referenced, $referenced_by);
|
96
|
// Load Balancer
|
97
|
find_alias_reference(array('load_balancer', 'lbpool'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
|
98
|
find_alias_reference(array('load_balancer', 'virtual_server'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
|
99
|
// Static routes
|
100
|
find_alias_reference(array('staticroutes', 'route'), array('network'), $alias_name, $is_alias_referenced, $referenced_by);
|
101
|
if ($is_alias_referenced == true) {
|
102
|
$savemsg = sprintf(gettext("Cannot delete alias. Currently in use by %s."), htmlspecialchars($referenced_by));
|
103
|
$class = "danger";
|
104
|
} else {
|
105
|
if (preg_match("/urltable/i", $a_aliases[$_GET['id']]['type'])) {
|
106
|
// this is a URL table type alias, delete its file as well
|
107
|
unlink_if_exists("/var/db/aliastables/" . $a_aliases[$_GET['id']]['name'] . ".txt");
|
108
|
}
|
109
|
unset($a_aliases[$_GET['id']]);
|
110
|
if (write_config()) {
|
111
|
filter_configure();
|
112
|
mark_subsystem_dirty('aliases');
|
113
|
}
|
114
|
header("Location: firewall_aliases.php?tab=" . $tab);
|
115
|
exit;
|
116
|
}
|
117
|
}
|
118
|
}
|
119
|
|
120
|
function find_alias_reference($section, $field, $origname, &$is_alias_referenced, &$referenced_by) {
|
121
|
global $config;
|
122
|
if (!$origname || $is_alias_referenced) {
|
123
|
return;
|
124
|
}
|
125
|
|
126
|
$sectionref = &$config;
|
127
|
foreach ($section as $sectionname) {
|
128
|
if (is_array($sectionref) && isset($sectionref[$sectionname])) {
|
129
|
$sectionref = &$sectionref[$sectionname];
|
130
|
} else {
|
131
|
return;
|
132
|
}
|
133
|
}
|
134
|
|
135
|
if (is_array($sectionref)) {
|
136
|
foreach ($sectionref as $itemkey => $item) {
|
137
|
$fieldfound = true;
|
138
|
$fieldref = &$sectionref[$itemkey];
|
139
|
foreach ($field as $fieldname) {
|
140
|
if (is_array($fieldref) && isset($fieldref[$fieldname])) {
|
141
|
$fieldref = &$fieldref[$fieldname];
|
142
|
} else {
|
143
|
$fieldfound = false;
|
144
|
break;
|
145
|
}
|
146
|
}
|
147
|
if ($fieldfound && $fieldref == $origname) {
|
148
|
$is_alias_referenced = true;
|
149
|
if (is_array($item)) {
|
150
|
$referenced_by = $item['descr'];
|
151
|
}
|
152
|
break;
|
153
|
}
|
154
|
}
|
155
|
}
|
156
|
}
|
157
|
|
158
|
$tab_array = array();
|
159
|
$tab_array[] = array(gettext("IP"), ($tab == "ip" ? true : ($tab == "host" ? true : ($tab == "network" ? true : false))), "/firewall_aliases.php?tab=ip");
|
160
|
$tab_array[] = array(gettext("Ports"), ($tab == "port"? true : false), "/firewall_aliases.php?tab=port");
|
161
|
$tab_array[] = array(gettext("URLs"), ($tab == "url"? true : false), "/firewall_aliases.php?tab=url");
|
162
|
$tab_array[] = array(gettext("All"), ($tab == "all"? true : false), "/firewall_aliases.php?tab=all");
|
163
|
|
164
|
foreach ($tab_array as $dtab) {
|
165
|
if ($dtab[1] == true) {
|
166
|
$bctab = $dtab[0];
|
167
|
break;
|
168
|
}
|
169
|
}
|
170
|
|
171
|
$pgtitle = array(gettext("Firewall"), gettext("Aliases"), $bctab);
|
172
|
$shortcut_section = "aliases";
|
173
|
|
174
|
include("head.inc");
|
175
|
|
176
|
if ($savemsg) {
|
177
|
print_info_box($savemsg, $class);
|
178
|
}
|
179
|
|
180
|
if (is_subsystem_dirty('aliases')) {
|
181
|
print_apply_box(gettext("The alias list has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
|
182
|
}
|
183
|
|
184
|
display_top_tabs($tab_array);
|
185
|
|
186
|
?>
|
187
|
|
188
|
<div class="panel panel-default">
|
189
|
<div class="panel-heading"><h2 class="panel-title"><?=sprintf(gettext('Firewall Aliases %s'), $bctab)?></h2></div>
|
190
|
<div class="panel-body">
|
191
|
|
192
|
<div class="table-responsive">
|
193
|
<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
|
194
|
<thead>
|
195
|
<tr>
|
196
|
<th><?=gettext("Name")?></th>
|
197
|
<th><?=gettext("Values")?></th>
|
198
|
<th><?=gettext("Description")?></th>
|
199
|
<th><?=gettext("Actions")?></th>
|
200
|
</tr>
|
201
|
</thead>
|
202
|
<tbody>
|
203
|
<?php
|
204
|
asort($a_aliases);
|
205
|
foreach ($a_aliases as $i => $alias):
|
206
|
unset ($show_alias);
|
207
|
switch ($tab) {
|
208
|
case "all":
|
209
|
$show_alias= true;
|
210
|
break;
|
211
|
case "ip":
|
212
|
case "host":
|
213
|
case "network":
|
214
|
if (preg_match("/(host|network)/", $alias["type"])) {
|
215
|
$show_alias= true;
|
216
|
}
|
217
|
break;
|
218
|
case "url":
|
219
|
if (preg_match("/(url)/i", $alias["type"])) {
|
220
|
$show_alias= true;
|
221
|
}
|
222
|
break;
|
223
|
case "port":
|
224
|
if ($alias["type"] == "port") {
|
225
|
$show_alias= true;
|
226
|
}
|
227
|
break;
|
228
|
}
|
229
|
if ($show_alias):
|
230
|
?>
|
231
|
<tr>
|
232
|
<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
|
233
|
<?=htmlspecialchars($alias['name'])?>
|
234
|
</td>
|
235
|
<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
|
236
|
<?php
|
237
|
if ($alias["url"]) {
|
238
|
echo $alias["url"] . "<br />";
|
239
|
} else {
|
240
|
if (is_array($alias["aliasurl"])) {
|
241
|
$aliasurls = implode(", ", array_slice($alias["aliasurl"], 0, 10));
|
242
|
echo $aliasurls;
|
243
|
if (count($aliasurls) > 10) {
|
244
|
echo "…<br />";
|
245
|
}
|
246
|
echo "<br />\n";
|
247
|
}
|
248
|
$tmpaddr = explode(" ", $alias['address']);
|
249
|
$addresses = implode(", ", array_slice($tmpaddr, 0, 10));
|
250
|
echo $addresses;
|
251
|
if (count($tmpaddr) > 10) {
|
252
|
echo '…';
|
253
|
}
|
254
|
}
|
255
|
?>
|
256
|
</td>
|
257
|
<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
|
258
|
<?=htmlspecialchars($alias['descr'])?>
|
259
|
</td>
|
260
|
<td>
|
261
|
<a class="fa fa-pencil" title="<?=gettext("Edit alias"); ?>" href="firewall_aliases_edit.php?id=<?=$i?>"></a>
|
262
|
<a class="fa fa-trash" title="<?=gettext("Delete alias")?>" href="?act=del&tab=<?=$tab?>&id=<?=$i?>"></a>
|
263
|
</td>
|
264
|
</tr>
|
265
|
<?php endif?>
|
266
|
<?php endforeach?>
|
267
|
</tbody>
|
268
|
</table>
|
269
|
</div>
|
270
|
|
271
|
</div>
|
272
|
</div>
|
273
|
|
274
|
<nav class="action-buttons">
|
275
|
<a href="firewall_aliases_edit.php?tab=<?=$tab?>" role="button" class="btn btn-success btn-sm">
|
276
|
<i class="fa fa-plus icon-embed-btn"></i>
|
277
|
<?=gettext("Add");?>
|
278
|
</a>
|
279
|
<?php
|
280
|
if (($tab == "ip") || ($tab == "port") || ($tab == "all")):
|
281
|
?>
|
282
|
<a href="firewall_aliases_import.php?tab=<?=$tab?>" role="button" class="btn btn-primary btn-sm">
|
283
|
<i class="fa fa-upload icon-embed-btn"></i>
|
284
|
<?=gettext("Import");?>
|
285
|
</a>
|
286
|
<?php
|
287
|
endif
|
288
|
?>
|
289
|
</nav>
|
290
|
|
291
|
<!-- Information section. Icon ID must be "showinfo" and the information <div> ID must be "infoblock".
|
292
|
That way jQuery (in pfenseHelpers.js) will automatically take care of the display. -->
|
293
|
<div>
|
294
|
<div class="infoblock">
|
295
|
<?php print_info_box(gettext('Aliases act as placeholders for real hosts, networks or ports. They can be used to minimize the number ' .
|
296
|
'of changes that have to be made if a host, network or port changes. <br />' .
|
297
|
'The name of an alias can be entered instead of the host, network or port where indicated. The alias will be resolved according to the list above.' . '<br />' .
|
298
|
'If an alias cannot be resolved (e.g. because it was deleted), the corresponding element (e.g. filter/NAT/shaper rule) will be considered invalid and skipped.'), 'info', false); ?>
|
299
|
</div>
|
300
|
</div>
|
301
|
|
302
|
<?php
|
303
|
include("foot.inc");
|