Project

General

Profile

Download (11.6 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
	pfSense_MODULE:	aliases
60
*/
61

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

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

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

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

    
81
if ($_POST) {
82

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

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

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

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

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

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

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

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

    
193
foreach ($tab_array as $dtab) {
194
	if($dtab[1] == true) {
195
		$bctab = $dtab[0];
196
		break;
197
	}
198
}
199

    
200
$pgtitle = array(gettext("Firewall"), gettext("Aliases"), $bctab);
201
$shortcut_section = "aliases";
202

    
203
include("head.inc");
204

    
205
if ($savemsg)
206
	print_info_box($savemsg, 'success');
207

    
208
if (is_subsystem_dirty('aliases'))
209
	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."));
210

    
211

    
212
display_top_tabs($tab_array);
213

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

    
291
<nav class="action-buttons">
292
	<a href="firewall_aliases_edit.php?tab=<?=$tab?>" role="button" class="btn btn-success btn-sm">
293
		<i class="fa fa-plus icon-embed-btn"></i>
294
		<?=gettext("Add");?>
295
	</a>
296
	<a href="firewall_aliases_import.php" role="button" class="btn btn-default btn-sm">
297
		<i class="fa fa-download icon-embed-btn"></i>
298
		<?=gettext("Import");?>
299
	</a>
300
</nav>
301

    
302
<!-- Information section. Icon ID must be "showinfo" and the information <div> ID must be "infoblock".
303
	 That way jQuery (in pfenseHelpers.js) will automatically take care of the display. -->
304
<div>
305
	<div id="infoblock">
306
		<?=print_info_box(gettext( 'Aliases act as placeholders for real hosts, networks or ports. They can be used to minimize the number ' .
307
			'of changes that have to be made if a host, network or port changes. <br />' .
308
			'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 />' .
309
			'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)?>
310
	</div>
311
</div>
312

    
313
<?php
314
include("foot.inc");
(48-48/228)