Project

General

Profile

Download (12.4 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_once("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
			$class = "success";
89
		} else {
90
			$savemsg = $retval;
91
			$class = "danger";
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
			$class = "danger";
137
		} else {
138
			if (preg_match("/urltable/i", $a_aliases[$_GET['id']]['type'])) {
139
				// this is a URL table type alias, delete its file as well
140
				unlink_if_exists("/var/db/aliastables/" . $a_aliases[$_GET['id']]['name'] . ".txt");
141
			}
142
			unset($a_aliases[$_GET['id']]);
143
			if (write_config()) {
144
				filter_configure();
145
				mark_subsystem_dirty('aliases');
146
			}
147
			header("Location: firewall_aliases.php?tab=" . $tab);
148
			exit;
149
		}
150
	}
151
}
152

    
153
function find_alias_reference($section, $field, $origname, &$is_alias_referenced, &$referenced_by) {
154
	global $config;
155
	if (!$origname || $is_alias_referenced) {
156
		return;
157
	}
158

    
159
	$sectionref = &$config;
160
	foreach ($section as $sectionname) {
161
		if (is_array($sectionref) && isset($sectionref[$sectionname])) {
162
			$sectionref = &$sectionref[$sectionname];
163
		} else {
164
			return;
165
		}
166
	}
167

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

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

    
197
foreach ($tab_array as $dtab) {
198
	if ($dtab[1] == true) {
199
		$bctab = $dtab[0];
200
		break;
201
	}
202
}
203

    
204
$pgtitle = array(gettext("Firewall"), gettext("Aliases"), $bctab);
205
$shortcut_section = "aliases";
206

    
207
include("head.inc");
208

    
209
if ($savemsg) {
210
	print_info_box($savemsg, $class);
211
}
212

    
213
if (is_subsystem_dirty('aliases')) {
214
	print_apply_box(gettext("The alias list has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
215
}
216

    
217
display_top_tabs($tab_array);
218

    
219
?>
220

    
221
<div class="panel panel-default">
222
	<div class="panel-heading"><h2 class="panel-title"><?=sprintf(gettext('Firewall Aliases %s'), $bctab)?></h2></div>
223
	<div class="panel-body">
224

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

    
304
	</div>
305
</div>
306

    
307
<nav class="action-buttons">
308
	<a href="firewall_aliases_edit.php?tab=<?=$tab?>" role="button" class="btn btn-success btn-sm">
309
		<i class="fa fa-plus icon-embed-btn"></i>
310
		<?=gettext("Add");?>
311
	</a>
312
<?php
313
if (($tab == "ip") || ($tab == "port") || ($tab == "all")):
314
?>
315
	<a href="firewall_aliases_import.php?tab=<?=$tab?>" role="button" class="btn btn-primary btn-sm">
316
		<i class="fa fa-upload icon-embed-btn"></i>
317
		<?=gettext("Import");?>
318
	</a>
319
<?php
320
endif
321
?>
322
</nav>
323

    
324
<!-- Information section. Icon ID must be "showinfo" and the information <div> ID must be "infoblock".
325
	 That way jQuery (in pfenseHelpers.js) will automatically take care of the display. -->
326
<div>
327
	<div class="infoblock">
328
		<?php print_info_box(gettext('Aliases act as placeholders for real hosts, networks or ports. They can be used to minimize the number ' .
329
			'of changes that have to be made if a host, network or port changes. <br />' .
330
			'The name of an alias can be entered instead of the host, network or port where indicated. The alias will be resolved according to the list above.' . '<br />' .
331
			'If an alias cannot be resolved (e.g. because it was deleted), the corresponding element (e.g. filter/NAT/shaper rule) will be considered invalid and skipped.'), 'info', false); ?>
332
	</div>
333
</div>
334

    
335
<?php
336
include("foot.inc");
(38-38/226)