Project

General

Profile

Download (10.7 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2
/*
3 c5d81585 Renato Botelho
 * firewall_aliases.php
4 9da2cf1c Stephen Beaver
 *
5 c5d81585 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 b8f91b7c Luiz Souza
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
7 c5d81585 Renato Botelho
 * All rights reserved.
8 fd9ebcd5 Stephen Beaver
 *
9 c5d81585 Renato Botelho
 * originally based on m0n0wall (http://m0n0.ch/wall)
10
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
 * All rights reserved.
12 fd9ebcd5 Stephen Beaver
 *
13 b12ea3fb Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 *
17 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
18 fd9ebcd5 Stephen Beaver
 *
19 b12ea3fb Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 */
25 5b237745 Scott Ullrich
26 6b07c15a Matthew Grooms
##|+PRIV
27
##|*IDENT=page-firewall-aliases
28 5230f468 jim-p
##|*NAME=Firewall: Aliases
29 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Firewall: Aliases' page.
30
##|*MATCH=firewall_aliases.php*
31
##|-PRIV
32
33 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
34 7a927e67 Scott Ullrich
require_once("functions.inc");
35
require_once("filter.inc");
36
require_once("shaper.inc");
37 5b237745 Scott Ullrich
38 3987186c Steve Beaver
if (!is_array($config['aliases'])) {
39
	$config['aliases'] = array();
40
}
41
42 95a40ac0 Phil Davis
if (!is_array($config['aliases']['alias'])) {
43 5b237745 Scott Ullrich
	$config['aliases']['alias'] = array();
44 95a40ac0 Phil Davis
}
45 3987186c Steve Beaver
46 5b237745 Scott Ullrich
$a_aliases = &$config['aliases']['alias'];
47
48 6c07db48 Phil Davis
$tab = ($_REQUEST['tab'] == "" ? "ip" : preg_replace("/\W/", "", $_REQUEST['tab']));
49 5b237745 Scott Ullrich
50 d1fd8c3b Steve Beaver
if ($_POST['apply']) {
51
	$retval = 0;
52 920b3bb0 Scott Ullrich
53 d1fd8c3b Steve Beaver
	/* reload all components that use aliases */
54
	$retval |= filter_configure();
55 920b3bb0 Scott Ullrich
56 d1fd8c3b Steve Beaver
	if ($retval == 0) {
57
		clear_subsystem_dirty('aliases');
58 5b237745 Scott Ullrich
	}
59
}
60
61 d1fd8c3b Steve Beaver
62
if ($_POST['act'] == "del") {
63
	if ($a_aliases[$_POST['id']]) {
64 58c32a28 Scott Ullrich
		/* make sure rule is not being referenced by any nat or filter rules */
65
		$is_alias_referenced = false;
66
		$referenced_by = false;
67 d1fd8c3b Steve Beaver
		$alias_name = $a_aliases[$_POST['id']]['name'];
68 c769f983 Erik Fonnesbeck
		// Firewall rules
69
		find_alias_reference(array('filter', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
70
		find_alias_reference(array('filter', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
71
		find_alias_reference(array('filter', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
72
		find_alias_reference(array('filter', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
73
		// NAT Rules
74
		find_alias_reference(array('nat', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
75
		find_alias_reference(array('nat', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
76
		find_alias_reference(array('nat', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
77
		find_alias_reference(array('nat', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
78
		find_alias_reference(array('nat', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
79
		find_alias_reference(array('nat', 'rule'), array('local-port'), $alias_name, $is_alias_referenced, $referenced_by);
80 b43b7613 Erik Fonnesbeck
		// NAT 1:1 Rules
81
		//find_alias_reference(array('nat', 'onetoone'), array('external'), $alias_name, $is_alias_referenced, $referenced_by);
82
		//find_alias_reference(array('nat', 'onetoone'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
83
		find_alias_reference(array('nat', 'onetoone'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
84
		// NAT Outbound Rules
85 682b8f12 Phil Davis
		find_alias_reference(array('nat', 'outbound', 'rule'), array('source', 'network'), $alias_name, $is_alias_referenced, $referenced_by);
86
		find_alias_reference(array('nat', 'outbound', 'rule'), array('sourceport'), $alias_name, $is_alias_referenced, $referenced_by);
87
		find_alias_reference(array('nat', 'outbound', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
88
		find_alias_reference(array('nat', 'outbound', 'rule'), array('dstport'), $alias_name, $is_alias_referenced, $referenced_by);
89
		find_alias_reference(array('nat', 'outbound', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
90 c769f983 Erik Fonnesbeck
		// Alias in an alias
91
		find_alias_reference(array('aliases', 'alias'), array('address'), $alias_name, $is_alias_referenced, $referenced_by);
92 3df59bab Darren Embry
		// Load Balancer
93 e6f34d22 Phil Davis
		find_alias_reference(array('load_balancer', 'lbpool'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
94 3df59bab Darren Embry
		find_alias_reference(array('load_balancer', 'virtual_server'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
95 a161bfb8 Renato Botelho
		// Static routes
96
		find_alias_reference(array('staticroutes', 'route'), array('network'), $alias_name, $is_alias_referenced, $referenced_by);
97 95a40ac0 Phil Davis
		if ($is_alias_referenced == true) {
98 44c42356 Phil Davis
			$delete_error = sprintf(gettext("Cannot delete alias. Currently in use by %s."), htmlspecialchars($referenced_by));
99 58c32a28 Scott Ullrich
		} else {
100 d1fd8c3b Steve Beaver
			if (preg_match("/urltable/i", $a_aliases[$_POST['id']]['type'])) {
101 99a05a0a Chris Buechler
				// this is a URL table type alias, delete its file as well
102 d1fd8c3b Steve Beaver
				unlink_if_exists("/var/db/aliastables/" . $a_aliases[$_POST['id']]['name'] . ".txt");
103 99a05a0a Chris Buechler
			}
104 d1fd8c3b Steve Beaver
			unset($a_aliases[$_POST['id']]);
105 d3f7f5d5 doktornotor
			if (write_config(gettext("Deleted a firewall alias."))) {
106 3a343d73 jim-p
				filter_configure();
107
				mark_subsystem_dirty('aliases');
108
			}
109 ac7f1763 Renato Botelho
			header("Location: firewall_aliases.php?tab=" . $tab);
110 58c32a28 Scott Ullrich
			exit;
111
		}
112 5b237745 Scott Ullrich
	}
113
}
114 b63695db Scott Ullrich
115 c769f983 Erik Fonnesbeck
function find_alias_reference($section, $field, $origname, &$is_alias_referenced, &$referenced_by) {
116
	global $config;
117 95a40ac0 Phil Davis
	if (!$origname || $is_alias_referenced) {
118 c769f983 Erik Fonnesbeck
		return;
119 95a40ac0 Phil Davis
	}
120 c769f983 Erik Fonnesbeck
121
	$sectionref = &$config;
122 95a40ac0 Phil Davis
	foreach ($section as $sectionname) {
123
		if (is_array($sectionref) && isset($sectionref[$sectionname])) {
124 c769f983 Erik Fonnesbeck
			$sectionref = &$sectionref[$sectionname];
125 95a40ac0 Phil Davis
		} else {
126 c769f983 Erik Fonnesbeck
			return;
127 95a40ac0 Phil Davis
		}
128 c769f983 Erik Fonnesbeck
	}
129
130 95a40ac0 Phil Davis
	if (is_array($sectionref)) {
131
		foreach ($sectionref as $itemkey => $item) {
132 c769f983 Erik Fonnesbeck
			$fieldfound = true;
133
			$fieldref = &$sectionref[$itemkey];
134 95a40ac0 Phil Davis
			foreach ($field as $fieldname) {
135
				if (is_array($fieldref) && isset($fieldref[$fieldname])) {
136 c769f983 Erik Fonnesbeck
					$fieldref = &$fieldref[$fieldname];
137 95a40ac0 Phil Davis
				} else {
138 c769f983 Erik Fonnesbeck
					$fieldfound = false;
139
					break;
140
				}
141
			}
142 95a40ac0 Phil Davis
			if ($fieldfound && $fieldref == $origname) {
143 c769f983 Erik Fonnesbeck
				$is_alias_referenced = true;
144 95a40ac0 Phil Davis
				if (is_array($item)) {
145 c769f983 Erik Fonnesbeck
					$referenced_by = $item['descr'];
146 95a40ac0 Phil Davis
				}
147 c769f983 Erik Fonnesbeck
				break;
148
			}
149
		}
150
	}
151
}
152
153 e1515be9 Stephen Beaver
$tab_array = array();
154
$tab_array[] = array(gettext("IP"),    ($tab == "ip" ? true : ($tab == "host" ? true : ($tab == "network" ? true : false))), "/firewall_aliases.php?tab=ip");
155
$tab_array[] = array(gettext("Ports"), ($tab == "port"? true : false), "/firewall_aliases.php?tab=port");
156
$tab_array[] = array(gettext("URLs"),  ($tab == "url"? true : false), "/firewall_aliases.php?tab=url");
157
$tab_array[] = array(gettext("All"),   ($tab == "all"? true : false), "/firewall_aliases.php?tab=all");
158
159
foreach ($tab_array as $dtab) {
160 67c2baf1 Phil Davis
	if ($dtab[1] == true) {
161 e1515be9 Stephen Beaver
		$bctab = $dtab[0];
162
		break;
163
	}
164
}
165
166
$pgtitle = array(gettext("Firewall"), gettext("Aliases"), $bctab);
167 edcd7535 Phil Davis
$pglinks = array("", "firewall_aliases.php", "@self");
168 d71fc5d3 jim-p
$shortcut_section = "aliases";
169
170 b63695db Scott Ullrich
include("head.inc");
171
172 44c42356 Phil Davis
if ($delete_error) {
173
	print_info_box($delete_error, 'danger');
174
}
175
if ($_POST['apply']) {
176
	print_apply_result_box($retval);
177 67c2baf1 Phil Davis
}
178 6cb366de Stephen Beaver
179 67c2baf1 Phil Davis
if (is_subsystem_dirty('aliases')) {
180 381ad9f3 NOYB
	print_apply_box(gettext("The alias list has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
181 67c2baf1 Phil Davis
}
182 14726356 Sjon Hortensius
183
display_top_tabs($tab_array);
184
185 5b237745 Scott Ullrich
?>
186 b40fb789 Colin Fleming
187
<div class="panel panel-default">
188 7d95365e Phil Davis
	<div class="panel-heading"><h2 class="panel-title"><?=sprintf(gettext('Firewall Aliases %s'), $bctab)?></h2></div>
189 b40fb789 Colin Fleming
	<div class="panel-body">
190
191 0ce45fdf Sander van Leeuwen
<div class="table-responsive">
192 250061b0 Stephen Beaver
<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
193 14726356 Sjon Hortensius
	<thead>
194
		<tr>
195 0ce45fdf Sander van Leeuwen
			<th><?=gettext("Name")?></th>
196
			<th><?=gettext("Values")?></th>
197
			<th><?=gettext("Description")?></th>
198 af5e730b Stephen Beaver
			<th><?=gettext("Actions")?></th>
199 14726356 Sjon Hortensius
		</tr>
200
	</thead>
201
	<tbody>
202
<?php
203
	asort($a_aliases);
204 e6f34d22 Phil Davis
	foreach ($a_aliases as $i => $alias):
205 14726356 Sjon Hortensius
		unset ($show_alias);
206 e6f34d22 Phil Davis
		switch ($tab) {
207 14726356 Sjon Hortensius
		case "all":
208
			$show_alias= true;
209
			break;
210
		case "ip":
211
		case "host":
212
		case "network":
213 67c2baf1 Phil Davis
			if (preg_match("/(host|network)/", $alias["type"])) {
214 14726356 Sjon Hortensius
				$show_alias= true;
215 67c2baf1 Phil Davis
			}
216 14726356 Sjon Hortensius
			break;
217
		case "url":
218 67c2baf1 Phil Davis
			if (preg_match("/(url)/i", $alias["type"])) {
219 14726356 Sjon Hortensius
				$show_alias= true;
220 67c2baf1 Phil Davis
			}
221 14726356 Sjon Hortensius
			break;
222
		case "port":
223 67c2baf1 Phil Davis
			if ($alias["type"] == "port") {
224 14726356 Sjon Hortensius
				$show_alias= true;
225 67c2baf1 Phil Davis
			}
226 14726356 Sjon Hortensius
			break;
227
		}
228
		if ($show_alias):
229
?>
230
		<tr>
231 f9a2b76d Stephen Beaver
			<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
232 14726356 Sjon Hortensius
				<?=htmlspecialchars($alias['name'])?>
233
			</td>
234 f9a2b76d Stephen Beaver
			<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
235 14726356 Sjon Hortensius
<?php
236
	if ($alias["url"]) {
237
		echo $alias["url"] . "<br />";
238
	} else {
239 e6f34d22 Phil Davis
		if (is_array($alias["aliasurl"])) {
240 14726356 Sjon Hortensius
			$aliasurls = implode(", ", array_slice($alias["aliasurl"], 0, 10));
241
			echo $aliasurls;
242 e6f34d22 Phil Davis
			if (count($aliasurls) > 10) {
243 14726356 Sjon Hortensius
				echo "&hellip;<br />";
244
			}
245
			echo "<br />\n";
246
		}
247
		$tmpaddr = explode(" ", $alias['address']);
248
		$addresses = implode(", ", array_slice($tmpaddr, 0, 10));
249
		echo $addresses;
250 e6f34d22 Phil Davis
		if (count($tmpaddr) > 10) {
251 14726356 Sjon Hortensius
			echo '&hellip;';
252
		}
253
	}
254
?>
255
			</td>
256 f9a2b76d Stephen Beaver
			<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
257 14726356 Sjon Hortensius
				<?=htmlspecialchars($alias['descr'])?>&nbsp;
258
			</td>
259
			<td>
260 84147b7b Steve Beaver
				<a class="fa fa-pencil" title="<?=gettext("Edit alias"); ?>" href="firewall_aliases_edit.php?id=<?=$i?>"></a>
261 d1fd8c3b Steve Beaver
				<a class="fa fa-trash"	title="<?=gettext("Delete alias")?>" href="?act=del&amp;tab=<?=$tab?>&amp;id=<?=$i?>" usepost></a>
262 df4957e4 Stephen Beaver
			</td>
263
		</tr>
264 14726356 Sjon Hortensius
<?php endif?>
265
<?php endforeach?>
266
	</tbody>
267
</table>
268 0ce45fdf Sander van Leeuwen
</div>
269 5b237745 Scott Ullrich
270 ac950976 Colin Fleming
	</div>
271
</div>
272 b40fb789 Colin Fleming
273 c10cb196 Stephen Beaver
<nav class="action-buttons">
274 84147b7b Steve Beaver
	<a href="firewall_aliases_edit.php?tab=<?=$tab?>" role="button" class="btn btn-success btn-sm">
275 9d5a20cf heper
		<i class="fa fa-plus icon-embed-btn"></i>
276 df4957e4 Stephen Beaver
		<?=gettext("Add");?>
277 11486640 Sjon Hortensius
	</a>
278 e4c784bf Phil Davis
<?php
279
if (($tab == "ip") || ($tab == "port") || ($tab == "all")):
280
?>
281 84147b7b Steve Beaver
	<a href="firewall_aliases_import.php?tab=<?=$tab?>" role="button" class="btn btn-primary btn-sm">
282 c933c798 jim-p
		<i class="fa fa-upload icon-embed-btn"></i>
283 df4957e4 Stephen Beaver
		<?=gettext("Import");?>
284 11486640 Sjon Hortensius
	</a>
285 e4c784bf Phil Davis
<?php
286
endif
287
?>
288 11486640 Sjon Hortensius
</nav>
289 0f89c125 marcelloc
290 df4957e4 Stephen Beaver
<!-- Information section. Icon ID must be "showinfo" and the information <div> ID must be "infoblock".
291
	 That way jQuery (in pfenseHelpers.js) will automatically take care of the display. -->
292
<div>
293 35681930 Stephen Beaver
	<div class="infoblock">
294 f6aebbcc NewEraCracker
		<?php print_info_box(gettext('Aliases act as placeholders for real hosts, networks or ports. They can be used to minimize the number ' .
295 1a147fcd Phil Davis
			'of changes that have to be made if a host, network or port changes.') . '<br />' .
296
			gettext('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 />' .
297
			gettext('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); ?>
298 df4957e4 Stephen Beaver
	</div>
299
</div>
300 0f89c125 marcelloc
301 df4957e4 Stephen Beaver
<?php
302 5932fc6a heper
include("foot.inc");