Project

General

Profile

Download (11 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	firewall_aliases.php
5
*/
6
/* ====================================================================
7
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved. 
8
 *  Copyright (c)  2004 Scott Ullrich
9
 *
10
 *  Redistribution and use in source and binary forms, with or without modification, 
11
 *  are permitted provided that the following conditions are met: 
12
 *
13
 *  1. Redistributions of source code must retain the above copyright notice,
14
 *      this list of conditions and the following disclaimer.
15
 *
16
 *  2. Redistributions in binary form must reproduce the above copyright
17
 *      notice, this list of conditions and the following disclaimer in
18
 *      the documentation and/or other materials provided with the
19
 *      distribution. 
20
 *
21
 *  3. All advertising materials mentioning features or use of this software 
22
 *      must display the following acknowledgment:
23
 *      "This product includes software developed by the pfSense Project
24
 *       for use in the pfSense software distribution. (http://www.pfsense.org/). 
25
 *
26
 *  4. The names "pfSense" and "pfSense Project" must not be used to
27
 *       endorse or promote products derived from this software without
28
 *       prior written permission. For written permission, please contact
29
 *       coreteam@pfsense.org.
30
 *
31
 *  5. Products derived from this software may not be called "pfSense"
32
 *      nor may "pfSense" appear in their names without prior written
33
 *      permission of the Electric Sheep Fencing, LLC.
34
 *
35
 *  6. Redistributions of any form whatsoever must retain the following
36
 *      acknowledgment:
37
 *
38
 *  "This product includes software developed by the pfSense Project
39
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
40
  *
41
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
42
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
45
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
53
 *
54
 *  ====================================================================
55
 *
56
 */
57
/*
58
	pfSense_MODULE:	aliases
59
*/
60

    
61
##|+PRIV
62
##|*IDENT=page-firewall-aliases
63
##|*NAME=Firewall: Aliases page
64
##|*DESCR=Allow access to the 'Firewall: Aliases' page.
65
##|*MATCH=firewall_aliases.php*
66
##|-PRIV
67

    
68
require("guiconfig.inc");
69
require_once("functions.inc");
70
require_once("filter.inc");
71
require_once("shaper.inc");
72

    
73
if (!is_array($config['aliases']['alias'])) {
74
	$config['aliases']['alias'] = array();
75
}
76
$a_aliases = &$config['aliases']['alias'];
77

    
78
$tab = ($_REQUEST['tab'] == "" ? "ip" : preg_replace("/\W/", "", $_REQUEST['tab']));
79

    
80
if ($_POST) {
81

    
82
	if ($_POST['apply']) {
83
		$retval = 0;
84

    
85
		/* reload all components that use aliases */
86
		$retval = filter_configure();
87

    
88
		if (stristr($retval, "error") <> true) {
89
			$savemsg = get_std_save_message($retval);
90
		} else {
91
			$savemsg = $retval;
92
		}
93
		if ($retval == 0) {
94
			clear_subsystem_dirty('aliases');
95
		}
96
	}
97
}
98

    
99
if ($_GET['act'] == "del") {
100
	if ($a_aliases[$_GET['id']]) {
101
		/* make sure rule is not being referenced by any nat or filter rules */
102
		$is_alias_referenced = false;
103
		$referenced_by = false;
104
		$alias_name = $a_aliases[$_GET['id']]['name'];
105
		// Firewall rules
106
		find_alias_reference(array('filter', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
107
		find_alias_reference(array('filter', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
108
		find_alias_reference(array('filter', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
109
		find_alias_reference(array('filter', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
110
		// NAT Rules
111
		find_alias_reference(array('nat', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
112
		find_alias_reference(array('nat', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
113
		find_alias_reference(array('nat', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
114
		find_alias_reference(array('nat', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
115
		find_alias_reference(array('nat', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
116
		find_alias_reference(array('nat', 'rule'), array('local-port'), $alias_name, $is_alias_referenced, $referenced_by);
117
		// NAT 1:1 Rules
118
		//find_alias_reference(array('nat', 'onetoone'), array('external'), $alias_name, $is_alias_referenced, $referenced_by);
119
		//find_alias_reference(array('nat', 'onetoone'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
120
		find_alias_reference(array('nat', 'onetoone'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
121
		// NAT Outbound Rules
122
		find_alias_reference(array('nat', 'outbound', 'rule'), array('source', 'network'), $alias_name, $is_alias_referenced, $referenced_by);
123
		find_alias_reference(array('nat', 'outbound', 'rule'), array('sourceport'), $alias_name, $is_alias_referenced, $referenced_by);
124
		find_alias_reference(array('nat', 'outbound', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
125
		find_alias_reference(array('nat', 'outbound', 'rule'), array('dstport'), $alias_name, $is_alias_referenced, $referenced_by);
126
		find_alias_reference(array('nat', 'outbound', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
127
		// Alias in an alias
128
		find_alias_reference(array('aliases', 'alias'), array('address'), $alias_name, $is_alias_referenced, $referenced_by);
129
		// Load Balancer
130
		find_alias_reference(array('load_balancer', 'lbpool'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
131
		find_alias_reference(array('load_balancer', 'virtual_server'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
132
		// Static routes
133
		find_alias_reference(array('staticroutes', 'route'), array('network'), $alias_name, $is_alias_referenced, $referenced_by);
134
		if ($is_alias_referenced == true) {
135
			$savemsg = sprintf(gettext("Cannot delete alias. Currently in use by %s"), htmlspecialchars($referenced_by));
136
		} else {
137
			unset($a_aliases[$_GET['id']]);
138
			if (write_config()) {
139
				filter_configure();
140
				mark_subsystem_dirty('aliases');
141
			}
142
			header("Location: firewall_aliases.php?tab=" . $tab);
143
			exit;
144
		}
145
	}
146
}
147

    
148
function find_alias_reference($section, $field, $origname, &$is_alias_referenced, &$referenced_by) {
149
	global $config;
150
	if (!$origname || $is_alias_referenced) {
151
		return;
152
	}
153

    
154
	$sectionref = &$config;
155
	foreach ($section as $sectionname) {
156
		if (is_array($sectionref) && isset($sectionref[$sectionname])) {
157
			$sectionref = &$sectionref[$sectionname];
158
		} else {
159
			return;
160
		}
161
	}
162

    
163
	if (is_array($sectionref)) {
164
		foreach ($sectionref as $itemkey => $item) {
165
			$fieldfound = true;
166
			$fieldref = &$sectionref[$itemkey];
167
			foreach ($field as $fieldname) {
168
				if (is_array($fieldref) && isset($fieldref[$fieldname])) {
169
					$fieldref = &$fieldref[$fieldname];
170
				} else {
171
					$fieldfound = false;
172
					break;
173
				}
174
			}
175
			if ($fieldfound && $fieldref == $origname) {
176
				$is_alias_referenced = true;
177
				if (is_array($item)) {
178
					$referenced_by = $item['descr'];
179
				}
180
				break;
181
			}
182
		}
183
	}
184
}
185

    
186
$pgtitle = array(gettext("Firewall"), gettext("Aliases"));
187
$shortcut_section = "aliases";
188

    
189
include("head.inc");
190

    
191
if ($savemsg)
192
	print_info_box($savemsg, 'success');
193

    
194
if (is_subsystem_dirty('aliases'))
195
	print_info_box_np(gettext("The alias list has been changed.") . "<br />" . gettext("You must apply the changes in order for them to take effect."));
196

    
197
$tab_array = array();
198
$tab_array[] = array(gettext("IP"), ($tab == "ip" ? true : ($tab == "host" ? true : ($tab == "network" ? true : false))), "/firewall_aliases.php?tab=ip");
199
$tab_array[] = array(gettext("Ports"), ($tab == "port"? true : false), "/firewall_aliases.php?tab=port");
200
$tab_array[] = array(gettext("URLs"), ($tab == "url"? true : false), "/firewall_aliases.php?tab=url");
201
$tab_array[] = array(gettext("All"), ($tab == "all"? true : false), "/firewall_aliases.php?tab=all");
202
display_top_tabs($tab_array);
203

    
204
?>
205
<div class="table-responsive">
206
<table class="table table-striped table-hover">
207
	<thead>
208
		<tr>
209
			<th><?=gettext("Name")?></th>
210
			<th><?=gettext("Values")?></th>
211
			<th><?=gettext("Description")?></th>
212
		</tr>
213
	</thead>
214
	<tbody>
215
<?php
216
	asort($a_aliases);
217
	foreach ($a_aliases as $i => $alias):
218
		unset ($show_alias);
219
		switch ($tab) {
220
		case "all":
221
			$show_alias= true;
222
			break;
223
		case "ip":
224
		case "host":
225
		case "network":
226
			if (preg_match("/(host|network)/", $alias["type"]))
227
				$show_alias= true;
228
			break;
229
		case "url":
230
			if (preg_match("/(url)/i", $alias["type"]))
231
				$show_alias= true;
232
			break;
233
		case "port":
234
			if ($alias["type"] == "port")
235
				$show_alias= true;
236
			break;
237
		}
238
		if ($show_alias):
239
?>
240
		<tr>
241
			<td>
242
				<?=htmlspecialchars($alias['name'])?>
243
			</td>
244
			<td>
245
<?php
246
	if ($alias["url"]) {
247
		echo $alias["url"] . "<br />";
248
	} else {
249
		if (is_array($alias["aliasurl"])) {
250
			$aliasurls = implode(", ", array_slice($alias["aliasurl"], 0, 10));
251
			echo $aliasurls;
252
			if (count($aliasurls) > 10) {
253
				echo "&hellip;<br />";
254
			}
255
			echo "<br />\n";
256
		}
257
		$tmpaddr = explode(" ", $alias['address']);
258
		$addresses = implode(", ", array_slice($tmpaddr, 0, 10));
259
		echo $addresses;
260
		if (count($tmpaddr) > 10) {
261
			echo '&hellip;';
262
		}
263
	}
264
?>
265
			</td>
266
			<td>
267
				<?=htmlspecialchars($alias['descr'])?>&nbsp;
268
			</td>
269
			<td>
270
				<a href="firewall_aliases_edit.php?id=<?=$i?>" class="btn btn-xs btn-primary">edit</a>
271
				<a href="?act=del&amp;tab=<?=$tab?>&amp;id=<?=$i?>" class="btn btn-xs btn-danger">delete</a>
272
				</td>
273
			</tr>
274
<?php endif?>
275
<?php endforeach?>
276
	</tbody>
277
</table>
278
</div>
279

    
280
<nav class="action-buttons">
281
	<a href="firewall_aliases_edit.php?tab=<?=$tab?>" role="button" class="btn btn-success">
282
		<?=gettext("add new alias");?>
283
	</a>
284
	<a href="firewall_aliases_import.php" role="button" class="btn btn-default">
285
		<?=gettext("bulk import");?>
286
	</a>
287
</nav>
288

    
289
<?php
290

    
291
print_info_box(gettext('Aliases act as placeholders for real hosts, networks or ports. They can be used to minimize the number ' .
292
			   'of changes that have to be made if a host, network or port changes.' . '<br />' .
293
			   '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 />' .
294
			   '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.'));
295

    
296
include("foot.inc");
(50-50/235)