Project

General

Profile

Download (12 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	firewall_aliases.php
4
*/
5
/* ====================================================================
6
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *  Some or all of this file is based on the m0n0wall project which is
9
 *  Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
10
 *
11
 *  Redistribution and use in source and binary forms, with or without modification,
12
 *  are permitted provided that the following conditions are met:
13
 *
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
 *      distribution.
21
 *
22
 *  3. All advertising materials mentioning features or use of this software
23
 *      must display the following acknowledgment:
24
 *      "This product includes software developed by the pfSense Project
25
 *       for use in the pfSense software distribution. (http://www.pfsense.org/).
26
 *
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
 *
42
 *  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

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

    
66
require("guiconfig.inc");
67
require_once("functions.inc");
68
require_once("filter.inc");
69
require_once("shaper.inc");
70

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

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

    
78
if ($_POST) {
79

    
80
	if ($_POST['apply']) {
81
		$retval = 0;
82

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

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

    
97
if ($_GET['act'] == "del") {
98
	if ($a_aliases[$_GET['id']]) {
99
		/* 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
		// 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
		// 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
		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
		// Alias in an alias
126
		find_alias_reference(array('aliases', 'alias'), array('address'), $alias_name, $is_alias_referenced, $referenced_by);
127
		// Load Balancer
128
		find_alias_reference(array('load_balancer', 'lbpool'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
129
		find_alias_reference(array('load_balancer', 'virtual_server'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
130
		// Static routes
131
		find_alias_reference(array('staticroutes', 'route'), array('network'), $alias_name, $is_alias_referenced, $referenced_by);
132
		if ($is_alias_referenced == true) {
133
			$savemsg = sprintf(gettext("Cannot delete alias. Currently in use by %s"), htmlspecialchars($referenced_by));
134
		} else {
135
			unset($a_aliases[$_GET['id']]);
136
			if (write_config()) {
137
				filter_configure();
138
				mark_subsystem_dirty('aliases');
139
			}
140
			header("Location: firewall_aliases.php?tab=" . $tab);
141
			exit;
142
		}
143
	}
144
}
145

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

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

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

    
184
$tab_array = array();
185
$tab_array[] = array(gettext("IP"),    ($tab == "ip" ? true : ($tab == "host" ? true : ($tab == "network" ? true : false))), "/firewall_aliases.php?tab=ip");
186
$tab_array[] = array(gettext("Ports"), ($tab == "port"? true : false), "/firewall_aliases.php?tab=port");
187
$tab_array[] = array(gettext("URLs"),  ($tab == "url"? true : false), "/firewall_aliases.php?tab=url");
188
$tab_array[] = array(gettext("All"),   ($tab == "all"? true : false), "/firewall_aliases.php?tab=all");
189

    
190
foreach ($tab_array as $dtab) {
191
	if ($dtab[1] == true) {
192
		$bctab = $dtab[0];
193
		break;
194
	}
195
}
196

    
197
$pgtitle = array(gettext("Firewall"), gettext("Aliases"), $bctab);
198
$shortcut_section = "aliases";
199

    
200
include("head.inc");
201

    
202
if ($savemsg) {
203
	print_info_box($savemsg, 'success');
204
}
205

    
206
if (is_subsystem_dirty('aliases')) {
207
	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."));
208
}
209

    
210
display_top_tabs($tab_array);
211

    
212
?>
213

    
214
<div class="panel panel-default">
215
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Firewall Aliases') . " " . $bctab?></h2></div>
216
	<div class="panel-body">
217

    
218
<div class="table-responsive">
219
<table class="table table-striped table-hover">
220
	<thead>
221
		<tr>
222
			<th><?=gettext("Name")?></th>
223
			<th><?=gettext("Values")?></th>
224
			<th><?=gettext("Description")?></th>
225
			<th><?=gettext("Actions")?></th>
226
		</tr>
227
	</thead>
228
	<tbody>
229
<?php
230
	asort($a_aliases);
231
	foreach ($a_aliases as $i => $alias):
232
		unset ($show_alias);
233
		switch ($tab) {
234
		case "all":
235
			$show_alias= true;
236
			break;
237
		case "ip":
238
		case "host":
239
		case "network":
240
			if (preg_match("/(host|network)/", $alias["type"])) {
241
				$show_alias= true;
242
			}
243
			break;
244
		case "url":
245
			if (preg_match("/(url)/i", $alias["type"])) {
246
				$show_alias= true;
247
			}
248
			break;
249
		case "port":
250
			if ($alias["type"] == "port") {
251
				$show_alias= true;
252
			}
253
			break;
254
		}
255
		if ($show_alias):
256
?>
257
		<tr>
258
			<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
259
				<?=htmlspecialchars($alias['name'])?>
260
			</td>
261
			<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
262
<?php
263
	if ($alias["url"]) {
264
		echo $alias["url"] . "<br />";
265
	} else {
266
		if (is_array($alias["aliasurl"])) {
267
			$aliasurls = implode(", ", array_slice($alias["aliasurl"], 0, 10));
268
			echo $aliasurls;
269
			if (count($aliasurls) > 10) {
270
				echo "&hellip;<br />";
271
			}
272
			echo "<br />\n";
273
		}
274
		$tmpaddr = explode(" ", $alias['address']);
275
		$addresses = implode(", ", array_slice($tmpaddr, 0, 10));
276
		echo $addresses;
277
		if (count($tmpaddr) > 10) {
278
			echo '&hellip;';
279
		}
280
	}
281
?>
282
			</td>
283
			<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
284
				<?=htmlspecialchars($alias['descr'])?>&nbsp;
285
			</td>
286
			<td>
287
				<a class="fa fa-pencil" title="<?=gettext("Edit alias"); ?>" href="firewall_aliases_edit.php?id=<?=$i?>"></a>
288
				<a class="fa fa-trash"	title="<?=gettext("Delete alias")?>" href="?act=del&amp;tab=<?=$tab?>&amp;id=<?=$i?>"></a>
289
			</td>
290
		</tr>
291
<?php endif?>
292
<?php endforeach?>
293
	</tbody>
294
</table>
295
</div>
296

    
297
	</div>
298
</div>
299

    
300
<nav class="action-buttons">
301
	<a href="firewall_aliases_edit.php?tab=<?=$tab?>" role="button" class="btn btn-success btn-sm">
302
		<i class="fa fa-plus icon-embed-btn"></i>
303
		<?=gettext("Add");?>
304
	</a>
305
	<a href="firewall_aliases_import.php" role="button" class="btn btn-default btn-sm">
306
		<i class="fa fa-download icon-embed-btn"></i>
307
		<?=gettext("Import");?>
308
	</a>
309
</nav>
310

    
311
<!-- Information section. Icon ID must be "showinfo" and the information <div> ID must be "infoblock".
312
	 That way jQuery (in pfenseHelpers.js) will automatically take care of the display. -->
313
<div>
314
	<div class="infoblock">
315
		<?=print_info_box(gettext('Aliases act as placeholders for real hosts, networks or ports. They can be used to minimize the number ' .
316
			'of changes that have to be made if a host, network or port changes. <br />' .
317
			'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 />' .
318
			'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')?>
319
	</div>
320
</div>
321

    
322
<?php
323
include("foot.inc");
(38-38/229)