Project

General

Profile

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

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

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

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

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

    
77
if ($_POST) {
78

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

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

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

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

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

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

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

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

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

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

    
206
include("head.inc");
207

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

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

    
216
display_top_tabs($tab_array);
217

    
218
?>
219

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

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

    
303
	</div>
304
</div>
305

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

    
323
<!-- Information section. Icon ID must be "showinfo" and the information <div> ID must be "infoblock".
324
	 That way jQuery (in pfenseHelpers.js) will automatically take care of the display. -->
325
<div>
326
	<div class="infoblock">
327
		<?php print_info_box(gettext('Aliases act as placeholders for real hosts, networks or ports. They can be used to minimize the number ' .
328
			'of changes that have to be made if a host, network or port changes. <br />' .
329
			'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 />' .
330
			'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); ?>
331
	</div>
332
</div>
333

    
334
<?php
335
include("foot.inc");
(37-37/227)