Project

General

Profile

Download (12.3 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2
/*
3
	firewall_aliases.php
4
*/
5 fd9ebcd5 Stephen Beaver
/* ====================================================================
6 919d91f9 Phil Davis
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7 9da2cf1c Stephen Beaver
 *
8 c9df279d Stephen Beaver
 *  Some or all of this file is based on the m0n0wall project which is
9 9da2cf1c Stephen Beaver
 *  Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
10 fd9ebcd5 Stephen Beaver
 *
11 919d91f9 Phil Davis
 *  Redistribution and use in source and binary forms, with or without modification,
12
 *  are permitted provided that the following conditions are met:
13 fd9ebcd5 Stephen Beaver
 *
14
 *  1. Redistributions of source code must retain the above copyright notice,
15
 *      this list of conditions and the following disclaimer.
16
 *
17
 *  2. Redistributions in binary form must reproduce the above copyright
18
 *      notice, this list of conditions and the following disclaimer in
19
 *      the documentation and/or other materials provided with the
20 919d91f9 Phil Davis
 *      distribution.
21 fd9ebcd5 Stephen Beaver
 *
22 919d91f9 Phil Davis
 *  3. All advertising materials mentioning features or use of this software
23 fd9ebcd5 Stephen Beaver
 *      must display the following acknowledgment:
24
 *      "This product includes software developed by the pfSense Project
25 919d91f9 Phil Davis
 *       for use in the pfSense software distribution. (http://www.pfsense.org/).
26 fd9ebcd5 Stephen Beaver
 *
27
 *  4. The names "pfSense" and "pfSense Project" must not be used to
28
 *       endorse or promote products derived from this software without
29
 *       prior written permission. For written permission, please contact
30
 *       coreteam@pfsense.org.
31
 *
32
 *  5. Products derived from this software may not be called "pfSense"
33
 *      nor may "pfSense" appear in their names without prior written
34
 *      permission of the Electric Sheep Fencing, LLC.
35
 *
36
 *  6. Redistributions of any form whatsoever must retain the following
37
 *      acknowledgment:
38
 *
39
 *  "This product includes software developed by the pfSense Project
40
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
41 919d91f9 Phil Davis
 *
42 fd9ebcd5 Stephen Beaver
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
43
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
46
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
54
 *
55
 *  ====================================================================
56
 *
57
 */
58 5b237745 Scott Ullrich
59 6b07c15a Matthew Grooms
##|+PRIV
60
##|*IDENT=page-firewall-aliases
61 5230f468 jim-p
##|*NAME=Firewall: Aliases
62 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Firewall: Aliases' page.
63
##|*MATCH=firewall_aliases.php*
64
##|-PRIV
65
66 5b237745 Scott Ullrich
require("guiconfig.inc");
67 7a927e67 Scott Ullrich
require_once("functions.inc");
68
require_once("filter.inc");
69
require_once("shaper.inc");
70 5b237745 Scott Ullrich
71 95a40ac0 Phil Davis
if (!is_array($config['aliases']['alias'])) {
72 5b237745 Scott Ullrich
	$config['aliases']['alias'] = array();
73 95a40ac0 Phil Davis
}
74 5b237745 Scott Ullrich
$a_aliases = &$config['aliases']['alias'];
75
76 6c07db48 Phil Davis
$tab = ($_REQUEST['tab'] == "" ? "ip" : preg_replace("/\W/", "", $_REQUEST['tab']));
77 5b237745 Scott Ullrich
78 ac7f1763 Renato Botelho
if ($_POST) {
79 5b237745 Scott Ullrich
80
	if ($_POST['apply']) {
81
		$retval = 0;
82 920b3bb0 Scott Ullrich
83
		/* reload all components that use aliases */
84
		$retval = filter_configure();
85
86 95a40ac0 Phil Davis
		if (stristr($retval, "error") <> true) {
87 14726356 Sjon Hortensius
			$savemsg = get_std_save_message($retval);
88 95a40ac0 Phil Davis
		} else {
89 14726356 Sjon Hortensius
			$savemsg = $retval;
90 95a40ac0 Phil Davis
		}
91
		if ($retval == 0) {
92 a368a026 Ermal Lu?i
			clear_subsystem_dirty('aliases');
93 95a40ac0 Phil Davis
		}
94 5b237745 Scott Ullrich
	}
95
}
96
97
if ($_GET['act'] == "del") {
98
	if ($a_aliases[$_GET['id']]) {
99 58c32a28 Scott Ullrich
		/* make sure rule is not being referenced by any nat or filter rules */
100
		$is_alias_referenced = false;
101
		$referenced_by = false;
102
		$alias_name = $a_aliases[$_GET['id']]['name'];
103 c769f983 Erik Fonnesbeck
		// Firewall rules
104
		find_alias_reference(array('filter', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
105
		find_alias_reference(array('filter', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
106
		find_alias_reference(array('filter', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
107
		find_alias_reference(array('filter', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
108
		// NAT Rules
109
		find_alias_reference(array('nat', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
110
		find_alias_reference(array('nat', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
111
		find_alias_reference(array('nat', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
112
		find_alias_reference(array('nat', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
113
		find_alias_reference(array('nat', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
114
		find_alias_reference(array('nat', 'rule'), array('local-port'), $alias_name, $is_alias_referenced, $referenced_by);
115 b43b7613 Erik Fonnesbeck
		// NAT 1:1 Rules
116
		//find_alias_reference(array('nat', 'onetoone'), array('external'), $alias_name, $is_alias_referenced, $referenced_by);
117
		//find_alias_reference(array('nat', 'onetoone'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
118
		find_alias_reference(array('nat', 'onetoone'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
119
		// NAT Outbound Rules
120 682b8f12 Phil Davis
		find_alias_reference(array('nat', 'outbound', 'rule'), array('source', 'network'), $alias_name, $is_alias_referenced, $referenced_by);
121
		find_alias_reference(array('nat', 'outbound', 'rule'), array('sourceport'), $alias_name, $is_alias_referenced, $referenced_by);
122
		find_alias_reference(array('nat', 'outbound', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
123
		find_alias_reference(array('nat', 'outbound', 'rule'), array('dstport'), $alias_name, $is_alias_referenced, $referenced_by);
124
		find_alias_reference(array('nat', 'outbound', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
125 c769f983 Erik Fonnesbeck
		// Alias in an alias
126
		find_alias_reference(array('aliases', 'alias'), array('address'), $alias_name, $is_alias_referenced, $referenced_by);
127 3df59bab Darren Embry
		// Load Balancer
128 e6f34d22 Phil Davis
		find_alias_reference(array('load_balancer', 'lbpool'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
129 3df59bab Darren Embry
		find_alias_reference(array('load_balancer', 'virtual_server'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
130 a161bfb8 Renato Botelho
		// Static routes
131
		find_alias_reference(array('staticroutes', 'route'), array('network'), $alias_name, $is_alias_referenced, $referenced_by);
132 95a40ac0 Phil Davis
		if ($is_alias_referenced == true) {
133 8545adde k-paulius
			$savemsg = sprintf(gettext("Cannot delete alias. Currently in use by %s."), htmlspecialchars($referenced_by));
134 58c32a28 Scott Ullrich
		} else {
135 99a05a0a Chris Buechler
			if (preg_match("/urltable/i", $a_aliases[$_GET['id']]['type'])) {
136
				// this is a URL table type alias, delete its file as well
137
				unlink_if_exists("/var/db/aliastables/" . $a_aliases[$_GET['id']]['name'] . ".txt");
138
			}
139 58c32a28 Scott Ullrich
			unset($a_aliases[$_GET['id']]);
140 3a343d73 jim-p
			if (write_config()) {
141
				filter_configure();
142
				mark_subsystem_dirty('aliases');
143
			}
144 ac7f1763 Renato Botelho
			header("Location: firewall_aliases.php?tab=" . $tab);
145 58c32a28 Scott Ullrich
			exit;
146
		}
147 5b237745 Scott Ullrich
	}
148
}
149 b63695db Scott Ullrich
150 c769f983 Erik Fonnesbeck
function find_alias_reference($section, $field, $origname, &$is_alias_referenced, &$referenced_by) {
151
	global $config;
152 95a40ac0 Phil Davis
	if (!$origname || $is_alias_referenced) {
153 c769f983 Erik Fonnesbeck
		return;
154 95a40ac0 Phil Davis
	}
155 c769f983 Erik Fonnesbeck
156
	$sectionref = &$config;
157 95a40ac0 Phil Davis
	foreach ($section as $sectionname) {
158
		if (is_array($sectionref) && isset($sectionref[$sectionname])) {
159 c769f983 Erik Fonnesbeck
			$sectionref = &$sectionref[$sectionname];
160 95a40ac0 Phil Davis
		} else {
161 c769f983 Erik Fonnesbeck
			return;
162 95a40ac0 Phil Davis
		}
163 c769f983 Erik Fonnesbeck
	}
164
165 95a40ac0 Phil Davis
	if (is_array($sectionref)) {
166
		foreach ($sectionref as $itemkey => $item) {
167 c769f983 Erik Fonnesbeck
			$fieldfound = true;
168
			$fieldref = &$sectionref[$itemkey];
169 95a40ac0 Phil Davis
			foreach ($field as $fieldname) {
170
				if (is_array($fieldref) && isset($fieldref[$fieldname])) {
171 c769f983 Erik Fonnesbeck
					$fieldref = &$fieldref[$fieldname];
172 95a40ac0 Phil Davis
				} else {
173 c769f983 Erik Fonnesbeck
					$fieldfound = false;
174
					break;
175
				}
176
			}
177 95a40ac0 Phil Davis
			if ($fieldfound && $fieldref == $origname) {
178 c769f983 Erik Fonnesbeck
				$is_alias_referenced = true;
179 95a40ac0 Phil Davis
				if (is_array($item)) {
180 c769f983 Erik Fonnesbeck
					$referenced_by = $item['descr'];
181 95a40ac0 Phil Davis
				}
182 c769f983 Erik Fonnesbeck
				break;
183
			}
184
		}
185
	}
186
}
187
188 e1515be9 Stephen Beaver
$tab_array = array();
189
$tab_array[] = array(gettext("IP"),    ($tab == "ip" ? true : ($tab == "host" ? true : ($tab == "network" ? true : false))), "/firewall_aliases.php?tab=ip");
190
$tab_array[] = array(gettext("Ports"), ($tab == "port"? true : false), "/firewall_aliases.php?tab=port");
191
$tab_array[] = array(gettext("URLs"),  ($tab == "url"? true : false), "/firewall_aliases.php?tab=url");
192
$tab_array[] = array(gettext("All"),   ($tab == "all"? true : false), "/firewall_aliases.php?tab=all");
193
194
foreach ($tab_array as $dtab) {
195 67c2baf1 Phil Davis
	if ($dtab[1] == true) {
196 e1515be9 Stephen Beaver
		$bctab = $dtab[0];
197
		break;
198
	}
199
}
200
201
$pgtitle = array(gettext("Firewall"), gettext("Aliases"), $bctab);
202 d71fc5d3 jim-p
$shortcut_section = "aliases";
203
204 b63695db Scott Ullrich
include("head.inc");
205
206 67c2baf1 Phil Davis
if ($savemsg) {
207 6cb366de Stephen Beaver
	print_info_box($savemsg, 'success');
208 67c2baf1 Phil Davis
}
209 6cb366de Stephen Beaver
210 67c2baf1 Phil Davis
if (is_subsystem_dirty('aliases')) {
211 16e3a3e2 Phil Davis
	print_apply_box(gettext("The alias list has been changed.") . "<br />" . gettext("You must apply the changes in order for them to take effect."));
212 67c2baf1 Phil Davis
}
213 14726356 Sjon Hortensius
214
display_top_tabs($tab_array);
215
216 5b237745 Scott Ullrich
?>
217 b40fb789 Colin Fleming
218
<div class="panel panel-default">
219 7d95365e Phil Davis
	<div class="panel-heading"><h2 class="panel-title"><?=sprintf(gettext('Firewall Aliases %s'), $bctab)?></h2></div>
220 b40fb789 Colin Fleming
	<div class="panel-body">
221
222 0ce45fdf Sander van Leeuwen
<div class="table-responsive">
223 250061b0 Stephen Beaver
<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
224 14726356 Sjon Hortensius
	<thead>
225
		<tr>
226 0ce45fdf Sander van Leeuwen
			<th><?=gettext("Name")?></th>
227
			<th><?=gettext("Values")?></th>
228
			<th><?=gettext("Description")?></th>
229 af5e730b Stephen Beaver
			<th><?=gettext("Actions")?></th>
230 14726356 Sjon Hortensius
		</tr>
231
	</thead>
232
	<tbody>
233
<?php
234
	asort($a_aliases);
235 e6f34d22 Phil Davis
	foreach ($a_aliases as $i => $alias):
236 14726356 Sjon Hortensius
		unset ($show_alias);
237 e6f34d22 Phil Davis
		switch ($tab) {
238 14726356 Sjon Hortensius
		case "all":
239
			$show_alias= true;
240
			break;
241
		case "ip":
242
		case "host":
243
		case "network":
244 67c2baf1 Phil Davis
			if (preg_match("/(host|network)/", $alias["type"])) {
245 14726356 Sjon Hortensius
				$show_alias= true;
246 67c2baf1 Phil Davis
			}
247 14726356 Sjon Hortensius
			break;
248
		case "url":
249 67c2baf1 Phil Davis
			if (preg_match("/(url)/i", $alias["type"])) {
250 14726356 Sjon Hortensius
				$show_alias= true;
251 67c2baf1 Phil Davis
			}
252 14726356 Sjon Hortensius
			break;
253
		case "port":
254 67c2baf1 Phil Davis
			if ($alias["type"] == "port") {
255 14726356 Sjon Hortensius
				$show_alias= true;
256 67c2baf1 Phil Davis
			}
257 14726356 Sjon Hortensius
			break;
258
		}
259
		if ($show_alias):
260
?>
261
		<tr>
262 f9a2b76d Stephen Beaver
			<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
263 14726356 Sjon Hortensius
				<?=htmlspecialchars($alias['name'])?>
264
			</td>
265 f9a2b76d Stephen Beaver
			<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
266 14726356 Sjon Hortensius
<?php
267
	if ($alias["url"]) {
268
		echo $alias["url"] . "<br />";
269
	} else {
270 e6f34d22 Phil Davis
		if (is_array($alias["aliasurl"])) {
271 14726356 Sjon Hortensius
			$aliasurls = implode(", ", array_slice($alias["aliasurl"], 0, 10));
272
			echo $aliasurls;
273 e6f34d22 Phil Davis
			if (count($aliasurls) > 10) {
274 14726356 Sjon Hortensius
				echo "&hellip;<br />";
275
			}
276
			echo "<br />\n";
277
		}
278
		$tmpaddr = explode(" ", $alias['address']);
279
		$addresses = implode(", ", array_slice($tmpaddr, 0, 10));
280
		echo $addresses;
281 e6f34d22 Phil Davis
		if (count($tmpaddr) > 10) {
282 14726356 Sjon Hortensius
			echo '&hellip;';
283
		}
284
	}
285
?>
286
			</td>
287 f9a2b76d Stephen Beaver
			<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
288 14726356 Sjon Hortensius
				<?=htmlspecialchars($alias['descr'])?>&nbsp;
289
			</td>
290
			<td>
291 5932fc6a heper
				<a class="fa fa-pencil" title="<?=gettext("Edit alias"); ?>" href="firewall_aliases_edit.php?id=<?=$i?>"></a>
292 0619c9db Stephen Beaver
				<a class="fa fa-trash"	title="<?=gettext("Delete alias")?>" href="?act=del&amp;tab=<?=$tab?>&amp;id=<?=$i?>"></a>
293 df4957e4 Stephen Beaver
			</td>
294
		</tr>
295 14726356 Sjon Hortensius
<?php endif?>
296
<?php endforeach?>
297
	</tbody>
298
</table>
299 0ce45fdf Sander van Leeuwen
</div>
300 5b237745 Scott Ullrich
301 ac950976 Colin Fleming
	</div>
302
</div>
303 b40fb789 Colin Fleming
304 c10cb196 Stephen Beaver
<nav class="action-buttons">
305 df4957e4 Stephen Beaver
	<a href="firewall_aliases_edit.php?tab=<?=$tab?>" role="button" class="btn btn-success btn-sm">
306 9d5a20cf heper
		<i class="fa fa-plus icon-embed-btn"></i>
307 df4957e4 Stephen Beaver
		<?=gettext("Add");?>
308 11486640 Sjon Hortensius
	</a>
309 df4957e4 Stephen Beaver
	<a href="firewall_aliases_import.php" role="button" class="btn btn-default btn-sm">
310 9d5a20cf heper
		<i class="fa fa-download icon-embed-btn"></i>
311 df4957e4 Stephen Beaver
		<?=gettext("Import");?>
312 11486640 Sjon Hortensius
	</a>
313
</nav>
314 0f89c125 marcelloc
315 df4957e4 Stephen Beaver
<!-- Information section. Icon ID must be "showinfo" and the information <div> ID must be "infoblock".
316
	 That way jQuery (in pfenseHelpers.js) will automatically take care of the display. -->
317
<div>
318 35681930 Stephen Beaver
	<div class="infoblock">
319 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 ' .
320 df4957e4 Stephen Beaver
			'of changes that have to be made if a host, network or port changes. <br />' .
321
			'You can enter the name of an alias instead of the host, network or port where indicated. The alias will be resolved according to the list above.' . '<br />' .
322 f6aebbcc NewEraCracker
			'If an alias cannot be resolved (e.g. because you deleted it), the corresponding element (e.g. filter/NAT/shaper rule) will be considered invalid and skipped.'), 'info', false); ?>
323 df4957e4 Stephen Beaver
	</div>
324
</div>
325 0f89c125 marcelloc
326 df4957e4 Stephen Beaver
<?php
327 5932fc6a heper
include("foot.inc");