Project

General

Profile

Download (9.94 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	firewall_aliases.php
5
	Copyright (C) 2004 Scott Ullrich
6
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
7
	All rights reserved.
8

    
9
	originally part of m0n0wall (http://m0n0.ch/wall)
10
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
	All rights reserved.
12

    
13
	Redistribution and use in source and binary forms, with or without
14
	modification, are permitted provided that the following conditions are met:
15

    
16
	1. Redistributions of source code must retain the above copyright notice,
17
	   this list of conditions and the following disclaimer.
18

    
19
	2. Redistributions in binary form must reproduce the above copyright
20
	   notice, this list of conditions and the following disclaimer in the
21
	   documentation and/or other materials provided with the distribution.
22

    
23
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
	POSSIBILITY OF SUCH DAMAGE.
33
*/
34
/*
35
	pfSense_MODULE:	aliases
36
*/
37

    
38
##|+PRIV
39
##|*IDENT=page-firewall-aliases
40
##|*NAME=Firewall: Aliases page
41
##|*DESCR=Allow access to the 'Firewall: Aliases' page.
42
##|*MATCH=firewall_aliases.php*
43
##|-PRIV
44

    
45
require("guiconfig.inc");
46
require_once("functions.inc");
47
require_once("filter.inc");
48
require_once("shaper.inc");
49

    
50
if (!is_array($config['aliases']['alias']))
51
	$config['aliases']['alias'] = array();
52
$a_aliases = &$config['aliases']['alias'];
53

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

    
56
if ($_POST) {
57

    
58
	if ($_POST['apply']) {
59
		$retval = 0;
60

    
61
		/* reload all components that use aliases */
62
		$retval = filter_configure();
63

    
64
		if(stristr($retval, "error") != true)
65
			$savemsg = get_std_save_message($retval);
66
		else
67
			$savemsg = $retval;
68
		if ($retval == 0)
69
			clear_subsystem_dirty('aliases');
70
	}
71
}
72

    
73
if ($_GET['act'] == "del") {
74
	if ($a_aliases[$_GET['id']]) {
75
		/* make sure rule is not being referenced by any nat or filter rules */
76
		$is_alias_referenced = false;
77
		$referenced_by = false;
78
		$alias_name = $a_aliases[$_GET['id']]['name'];
79
		// Firewall rules
80
		find_alias_reference(array('filter', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
81
		find_alias_reference(array('filter', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
82
		find_alias_reference(array('filter', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
83
		find_alias_reference(array('filter', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
84
		// NAT Rules
85
		find_alias_reference(array('nat', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
86
		find_alias_reference(array('nat', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
87
		find_alias_reference(array('nat', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
88
		find_alias_reference(array('nat', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
89
		find_alias_reference(array('nat', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
90
		find_alias_reference(array('nat', 'rule'), array('local-port'), $alias_name, $is_alias_referenced, $referenced_by);
91
		// NAT 1:1 Rules
92
		//find_alias_reference(array('nat', 'onetoone'), array('external'), $alias_name, $is_alias_referenced, $referenced_by);
93
		//find_alias_reference(array('nat', 'onetoone'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
94
		find_alias_reference(array('nat', 'onetoone'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
95
		// NAT Outbound Rules
96
		find_alias_reference(array('nat', 'advancedoutbound', 'rule'), array('source', 'network'), $alias_name, $is_alias_referenced, $referenced_by);
97
		find_alias_reference(array('nat', 'advancedoutbound', 'rule'), array('sourceport'), $alias_name, $is_alias_referenced, $referenced_by);
98
		find_alias_reference(array('nat', 'advancedoutbound', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
99
		find_alias_reference(array('nat', 'advancedoutbound', 'rule'), array('dstport'), $alias_name, $is_alias_referenced, $referenced_by);
100
		find_alias_reference(array('nat', 'advancedoutbound', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
101
		// Alias in an alias
102
		find_alias_reference(array('aliases', 'alias'), array('address'), $alias_name, $is_alias_referenced, $referenced_by);
103
		// Load Balancer
104
		find_alias_reference(array('load_balancer', 'lbpool'),		 array('port'), $alias_name, $is_alias_referenced, $referenced_by);
105
		find_alias_reference(array('load_balancer', 'virtual_server'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
106
		// Static routes
107
		find_alias_reference(array('staticroutes', 'route'), array('network'), $alias_name, $is_alias_referenced, $referenced_by);
108
		if($is_alias_referenced == true) {
109
			$savemsg = sprintf(gettext("Cannot delete alias. Currently in use by %s"), $referenced_by);
110
		} else {
111
			unset($a_aliases[$_GET['id']]);
112
			if (write_config()) {
113
				filter_configure();
114
				mark_subsystem_dirty('aliases');
115
			}
116
			header("Location: firewall_aliases.php?tab=" . $tab);
117
			exit;
118
		}
119
	}
120
}
121

    
122
function find_alias_reference($section, $field, $origname, &$is_alias_referenced, &$referenced_by) {
123
	global $config;
124
	if(!$origname || $is_alias_referenced)
125
		return;
126

    
127
	$sectionref = &$config;
128
	foreach($section as $sectionname) {
129
		if(is_array($sectionref) && isset($sectionref[$sectionname]))
130
			$sectionref = &$sectionref[$sectionname];
131
		else
132
			return;
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
				break;
152
			}
153
		}
154
	}
155
}
156

    
157
$pgtitle = array(gettext("Firewall"),gettext("Aliases"));
158
$shortcut_section = "aliases";
159

    
160
include("head.inc");
161

    
162
if ($savemsg)
163
	print_info_box($savemsg);
164
if (is_subsystem_dirty('aliases'))
165
	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."));
166

    
167
$tab_array = array();
168
$tab_array[] = array(gettext("IP"),($tab=="ip" ? true : ($tab=="host" ? true : ($tab == "network" ? true : false))), "/firewall_aliases.php?tab=ip");
169
$tab_array[] = array(gettext("Ports"), ($tab=="port"? true : false), "/firewall_aliases.php?tab=port");
170
$tab_array[] = array(gettext("URLs"), ($tab=="url"? true : false), "/firewall_aliases.php?tab=url");
171
$tab_array[] = array(gettext("All"), ($tab=="all"? true : false), "/firewall_aliases.php?tab=all");
172
display_top_tabs($tab_array);
173

    
174
?>
175
<div class="table-responsive">
176
<table class="table table-striped table-hover">
177
	<thead>
178
		<tr>
179
			<th><?=gettext("Name")?></th>
180
			<th><?=gettext("Values")?></th>
181
			<th><?=gettext("Description")?></th>
182
		</tr>
183
	</thead>
184
	<tbody>
185
<?php
186
	asort($a_aliases);
187
	foreach ($a_aliases as $i=> $alias):
188
		unset ($show_alias);
189
		switch ($tab){
190
		case "all":
191
			$show_alias= true;
192
			break;
193
		case "ip":
194
		case "host":
195
		case "network":
196
			if (preg_match("/(host|network)/",$alias["type"]))
197
				$show_alias= true;
198
			break;
199
		case "url":
200
			if (preg_match("/(url)/i",$alias["type"]))
201
				$show_alias= true;
202
			break;
203
		case "port":
204
			if($alias["type"] == "port")
205
				$show_alias= true;
206
			break;
207
		}
208
		if ($show_alias):
209
?>
210
		<tr>
211
			<td>
212
				<?=htmlspecialchars($alias['name'])?>
213
			</td>
214
			<td>
215
<?php
216
	if ($alias["url"]) {
217
		echo $alias["url"] . "<br />";
218
	} else {
219
		if(is_array($alias["aliasurl"])) {
220
			$aliasurls = implode(", ", array_slice($alias["aliasurl"], 0, 10));
221
			echo $aliasurls;
222
			if(count($aliasurls) > 10) {
223
				echo "&hellip;<br />";
224
			}
225
			echo "<br />\n";
226
		}
227
		$tmpaddr = explode(" ", $alias['address']);
228
		$addresses = implode(", ", array_slice($tmpaddr, 0, 10));
229
		echo $addresses;
230
		if(count($tmpaddr) > 10) {
231
			echo '&hellip;';
232
		}
233
	}
234
?>
235
			</td>
236
			<td>
237
				<?=htmlspecialchars($alias['descr'])?>&nbsp;
238
			</td>
239
			<td>
240
				<a href="firewall_aliases_edit.php?id=<?=$i?>" class="btn btn-xs btn-primary">edit</a>
241
				<a href="?act=del&amp;tab=<?=$tab?>&amp;id=<?=$i?>" class="btn btn-xs btn-danger">delete</a>
242
				</td>
243
			</tr>
244
<?php endif?>
245
<?php endforeach?>
246
	</tbody>
247
</table>
248
</div>
249

    
250
<nav class="action-buttons">
251
	<a href="firewall_aliases_edit.php?tab=<?=$tab?>" role="button" class="btn btn-success">
252
		<?=gettext("add new alias");?>
253
	</a>
254
	<a href="firewall_aliases_import.php" role="button" class="btn btn-default">
255
		<?=gettext("bulk import");?>
256
	</a>
257
</nav>
258

    
259
<br/><br/>
260
<div class="panel panel-default">
261
	<div class="panel-heading"><?=gettext("Note:")?></div>
262
	<div class="panel-body">
263
		<?=gettext("Aliases act as placeholders for real hosts, networks or ports. They can be used to minimize the number of changes that have to be made if a host, network or port changes. You can enter the name of an alias instead of the host, network or port in all fields that have a red background. The alias will be resolved according to the list above. 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.")?>
264
	</div>
265
</div>
266

    
267
<?php include("foot.inc");
(49-49/241)