Project

General

Profile

Download (10.6 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
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16
 *
17
 * http://www.apache.org/licenses/LICENSE-2.0
18
 *
19
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24
 */
25

    
26
##|+PRIV
27
##|*IDENT=page-firewall-aliases
28
##|*NAME=Firewall: Aliases
29
##|*DESCR=Allow access to the 'Firewall: Aliases' page.
30
##|*MATCH=firewall_aliases.php*
31
##|-PRIV
32

    
33
require_once("guiconfig.inc");
34
require_once("functions.inc");
35
require_once("filter.inc");
36
require_once("shaper.inc");
37

    
38
if (!is_array($config['aliases']['alias'])) {
39
	$config['aliases']['alias'] = array();
40
}
41
$a_aliases = &$config['aliases']['alias'];
42

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

    
45
if ($_POST['apply']) {
46
	$retval = 0;
47

    
48
	/* reload all components that use aliases */
49
	$retval |= filter_configure();
50

    
51
	if ($retval == 0) {
52
		clear_subsystem_dirty('aliases');
53
	}
54
}
55

    
56

    
57
if ($_POST['act'] == "del") {
58
	if ($a_aliases[$_POST['id']]) {
59
		/* make sure rule is not being referenced by any nat or filter rules */
60
		$is_alias_referenced = false;
61
		$referenced_by = false;
62
		$alias_name = $a_aliases[$_POST['id']]['name'];
63
		// Firewall rules
64
		find_alias_reference(array('filter', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
65
		find_alias_reference(array('filter', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
66
		find_alias_reference(array('filter', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
67
		find_alias_reference(array('filter', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
68
		// NAT Rules
69
		find_alias_reference(array('nat', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
70
		find_alias_reference(array('nat', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
71
		find_alias_reference(array('nat', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
72
		find_alias_reference(array('nat', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
73
		find_alias_reference(array('nat', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
74
		find_alias_reference(array('nat', 'rule'), array('local-port'), $alias_name, $is_alias_referenced, $referenced_by);
75
		// NAT 1:1 Rules
76
		//find_alias_reference(array('nat', 'onetoone'), array('external'), $alias_name, $is_alias_referenced, $referenced_by);
77
		//find_alias_reference(array('nat', 'onetoone'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
78
		find_alias_reference(array('nat', 'onetoone'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
79
		// NAT Outbound Rules
80
		find_alias_reference(array('nat', 'outbound', 'rule'), array('source', 'network'), $alias_name, $is_alias_referenced, $referenced_by);
81
		find_alias_reference(array('nat', 'outbound', 'rule'), array('sourceport'), $alias_name, $is_alias_referenced, $referenced_by);
82
		find_alias_reference(array('nat', 'outbound', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
83
		find_alias_reference(array('nat', 'outbound', 'rule'), array('dstport'), $alias_name, $is_alias_referenced, $referenced_by);
84
		find_alias_reference(array('nat', 'outbound', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
85
		// Alias in an alias
86
		find_alias_reference(array('aliases', 'alias'), array('address'), $alias_name, $is_alias_referenced, $referenced_by);
87
		// Load Balancer
88
		find_alias_reference(array('load_balancer', 'lbpool'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
89
		find_alias_reference(array('load_balancer', 'virtual_server'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
90
		// Static routes
91
		find_alias_reference(array('staticroutes', 'route'), array('network'), $alias_name, $is_alias_referenced, $referenced_by);
92
		if ($is_alias_referenced == true) {
93
			$delete_error = sprintf(gettext("Cannot delete alias. Currently in use by %s."), htmlspecialchars($referenced_by));
94
		} else {
95
			if (preg_match("/urltable/i", $a_aliases[$_POST['id']]['type'])) {
96
				// this is a URL table type alias, delete its file as well
97
				unlink_if_exists("/var/db/aliastables/" . $a_aliases[$_POST['id']]['name'] . ".txt");
98
			}
99
			unset($a_aliases[$_POST['id']]);
100
			if (write_config(gettext("Deleted a firewall alias."))) {
101
				filter_configure();
102
				mark_subsystem_dirty('aliases');
103
			}
104
			header("Location: firewall_aliases.php?tab=" . $tab);
105
			exit;
106
		}
107
	}
108
}
109

    
110
function find_alias_reference($section, $field, $origname, &$is_alias_referenced, &$referenced_by) {
111
	global $config;
112
	if (!$origname || $is_alias_referenced) {
113
		return;
114
	}
115

    
116
	$sectionref = &$config;
117
	foreach ($section as $sectionname) {
118
		if (is_array($sectionref) && isset($sectionref[$sectionname])) {
119
			$sectionref = &$sectionref[$sectionname];
120
		} else {
121
			return;
122
		}
123
	}
124

    
125
	if (is_array($sectionref)) {
126
		foreach ($sectionref as $itemkey => $item) {
127
			$fieldfound = true;
128
			$fieldref = &$sectionref[$itemkey];
129
			foreach ($field as $fieldname) {
130
				if (is_array($fieldref) && isset($fieldref[$fieldname])) {
131
					$fieldref = &$fieldref[$fieldname];
132
				} else {
133
					$fieldfound = false;
134
					break;
135
				}
136
			}
137
			if ($fieldfound && $fieldref == $origname) {
138
				$is_alias_referenced = true;
139
				if (is_array($item)) {
140
					$referenced_by = $item['descr'];
141
				}
142
				break;
143
			}
144
		}
145
	}
146
}
147

    
148
$tab_array = array();
149
$tab_array[] = array(gettext("IP"),    ($tab == "ip" ? true : ($tab == "host" ? true : ($tab == "network" ? true : false))), "/firewall_aliases.php?tab=ip");
150
$tab_array[] = array(gettext("Ports"), ($tab == "port"? true : false), "/firewall_aliases.php?tab=port");
151
$tab_array[] = array(gettext("URLs"),  ($tab == "url"? true : false), "/firewall_aliases.php?tab=url");
152
$tab_array[] = array(gettext("All"),   ($tab == "all"? true : false), "/firewall_aliases.php?tab=all");
153

    
154
foreach ($tab_array as $dtab) {
155
	if ($dtab[1] == true) {
156
		$bctab = $dtab[0];
157
		break;
158
	}
159
}
160

    
161
$pgtitle = array(gettext("Firewall"), gettext("Aliases"), $bctab);
162
$pglinks = array("", "firewall_aliases.php", "@self");
163
$shortcut_section = "aliases";
164

    
165
include("head.inc");
166

    
167
if ($delete_error) {
168
	print_info_box($delete_error, 'danger');
169
}
170
if ($_POST['apply']) {
171
	print_apply_result_box($retval);
172
}
173

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

    
178
display_top_tabs($tab_array);
179

    
180
?>
181

    
182
<div class="panel panel-default">
183
	<div class="panel-heading"><h2 class="panel-title"><?=sprintf(gettext('Firewall Aliases %s'), $bctab)?></h2></div>
184
	<div class="panel-body">
185

    
186
<div class="table-responsive">
187
<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable>
188
	<thead>
189
		<tr>
190
			<th><?=gettext("Name")?></th>
191
			<th><?=gettext("Values")?></th>
192
			<th><?=gettext("Description")?></th>
193
			<th><?=gettext("Actions")?></th>
194
		</tr>
195
	</thead>
196
	<tbody>
197
<?php
198
	asort($a_aliases);
199
	foreach ($a_aliases as $i => $alias):
200
		unset ($show_alias);
201
		switch ($tab) {
202
		case "all":
203
			$show_alias= true;
204
			break;
205
		case "ip":
206
		case "host":
207
		case "network":
208
			if (preg_match("/(host|network)/", $alias["type"])) {
209
				$show_alias= true;
210
			}
211
			break;
212
		case "url":
213
			if (preg_match("/(url)/i", $alias["type"])) {
214
				$show_alias= true;
215
			}
216
			break;
217
		case "port":
218
			if ($alias["type"] == "port") {
219
				$show_alias= true;
220
			}
221
			break;
222
		}
223
		if ($show_alias):
224
?>
225
		<tr>
226
			<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
227
				<?=htmlspecialchars($alias['name'])?>
228
			</td>
229
			<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
230
<?php
231
	if ($alias["url"]) {
232
		echo $alias["url"] . "<br />";
233
	} else {
234
		if (is_array($alias["aliasurl"])) {
235
			$aliasurls = implode(", ", array_slice($alias["aliasurl"], 0, 10));
236
			echo $aliasurls;
237
			if (count($aliasurls) > 10) {
238
				echo "&hellip;<br />";
239
			}
240
			echo "<br />\n";
241
		}
242
		$tmpaddr = explode(" ", $alias['address']);
243
		$addresses = implode(", ", array_slice($tmpaddr, 0, 10));
244
		echo $addresses;
245
		if (count($tmpaddr) > 10) {
246
			echo '&hellip;';
247
		}
248
	}
249
?>
250
			</td>
251
			<td ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
252
				<?=htmlspecialchars($alias['descr'])?>&nbsp;
253
			</td>
254
			<td>
255
				<a class="fa fa-pencil" title="<?=gettext("Edit alias"); ?>" href="firewall_aliases_edit.php?id=<?=$i?>"></a>
256
				<a class="fa fa-trash"	title="<?=gettext("Delete alias")?>" href="?act=del&amp;tab=<?=$tab?>&amp;id=<?=$i?>" usepost></a>
257
			</td>
258
		</tr>
259
<?php endif?>
260
<?php endforeach?>
261
	</tbody>
262
</table>
263
</div>
264

    
265
	</div>
266
</div>
267

    
268
<nav class="action-buttons">
269
	<a href="firewall_aliases_edit.php?tab=<?=$tab?>" role="button" class="btn btn-success btn-sm">
270
		<i class="fa fa-plus icon-embed-btn"></i>
271
		<?=gettext("Add");?>
272
	</a>
273
<?php
274
if (($tab == "ip") || ($tab == "port") || ($tab == "all")):
275
?>
276
	<a href="firewall_aliases_import.php?tab=<?=$tab?>" role="button" class="btn btn-primary btn-sm">
277
		<i class="fa fa-upload icon-embed-btn"></i>
278
		<?=gettext("Import");?>
279
	</a>
280
<?php
281
endif
282
?>
283
</nav>
284

    
285
<!-- Information section. Icon ID must be "showinfo" and the information <div> ID must be "infoblock".
286
	 That way jQuery (in pfenseHelpers.js) will automatically take care of the display. -->
287
<div>
288
	<div class="infoblock">
289
		<?php print_info_box(gettext('Aliases act as placeholders for real hosts, networks or ports. They can be used to minimize the number ' .
290
			'of changes that have to be made if a host, network or port changes.') . '<br />' .
291
			gettext('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 />' .
292
			gettext('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); ?>
293
	</div>
294
</div>
295

    
296
<?php
297
include("foot.inc");
(40-40/231)