Project

General

Profile

Download (10.7 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-2018 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'])) {
39
	$config['aliases'] = array();
40
}
41

    
42
if (!is_array($config['aliases']['alias'])) {
43
	$config['aliases']['alias'] = array();
44
}
45

    
46
$a_aliases = &$config['aliases']['alias'];
47

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

    
50
if ($_POST['apply']) {
51
	$retval = 0;
52

    
53
	/* reload all components that use aliases */
54
	$retval |= filter_configure();
55

    
56
	if ($retval == 0) {
57
		clear_subsystem_dirty('aliases');
58
	}
59
}
60

    
61

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

    
115
function find_alias_reference($section, $field, $origname, &$is_alias_referenced, &$referenced_by) {
116
	global $config;
117
	if (!$origname || $is_alias_referenced) {
118
		return;
119
	}
120

    
121
	$sectionref = &$config;
122
	foreach ($section as $sectionname) {
123
		if (is_array($sectionref) && isset($sectionref[$sectionname])) {
124
			$sectionref = &$sectionref[$sectionname];
125
		} else {
126
			return;
127
		}
128
	}
129

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

    
153
$tab_array = array();
154
$tab_array[] = array(gettext("IP"),    ($tab == "ip" ? true : ($tab == "host" ? true : ($tab == "network" ? true : false))), "/firewall_aliases.php?tab=ip");
155
$tab_array[] = array(gettext("Ports"), ($tab == "port"? true : false), "/firewall_aliases.php?tab=port");
156
$tab_array[] = array(gettext("URLs"),  ($tab == "url"? true : false), "/firewall_aliases.php?tab=url");
157
$tab_array[] = array(gettext("All"),   ($tab == "all"? true : false), "/firewall_aliases.php?tab=all");
158

    
159
foreach ($tab_array as $dtab) {
160
	if ($dtab[1] == true) {
161
		$bctab = $dtab[0];
162
		break;
163
	}
164
}
165

    
166
$pgtitle = array(gettext("Firewall"), gettext("Aliases"), $bctab);
167
$pglinks = array("", "firewall_aliases.php", "@self");
168
$shortcut_section = "aliases";
169

    
170
include("head.inc");
171

    
172
if ($delete_error) {
173
	print_info_box($delete_error, 'danger');
174
}
175
if ($_POST['apply']) {
176
	print_apply_result_box($retval);
177
}
178

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

    
183
display_top_tabs($tab_array);
184

    
185
?>
186

    
187
<div class="panel panel-default">
188
	<div class="panel-heading"><h2 class="panel-title"><?=sprintf(gettext('Firewall Aliases %s'), $bctab)?></h2></div>
189
	<div class="panel-body">
190

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

    
270
	</div>
271
</div>
272

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

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

    
301
<?php
302
include("foot.inc");
(39-39/234)