Project

General

Profile

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