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-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
init_config_arr(array('aliases', 'alias'));
39
$a_aliases = &$config['aliases']['alias'];
40

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

    
43
if ($_POST['apply']) {
44
	$retval = 0;
45

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

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

    
54

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

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

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

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

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

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

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

    
163
include("head.inc");
164

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

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

    
176
display_top_tabs($tab_array);
177

    
178
?>
179

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

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

    
263
	</div>
264
</div>
265

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

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

    
294
<?php
295
include("foot.inc");
(39-39/234)