Project

General

Profile

Download (10.4 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-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

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

    
35
require_once("guiconfig.inc");
36
require_once("functions.inc");
37
require_once("filter.inc");
38
require_once("shaper.inc");
39

    
40
init_config_arr(array('aliases', 'alias'));
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
		// Static routes
88
		find_alias_reference(array('staticroutes', 'route'), array('network'), $alias_name, $is_alias_referenced, $referenced_by);
89
		if ($is_alias_referenced == true) {
90
			$delete_error = sprintf(gettext("Cannot delete alias. Currently in use by %s."), htmlspecialchars($referenced_by));
91
		} else {
92
			if (preg_match("/urltable/i", $a_aliases[$_POST['id']]['type'])) {
93
				// this is a URL table type alias, delete its file as well
94
				unlink_if_exists("/var/db/aliastables/" . $a_aliases[$_POST['id']]['name'] . ".txt");
95
			}
96
			unset($a_aliases[$_POST['id']]);
97
			if (write_config(gettext("Deleted a firewall alias."))) {
98
				filter_configure();
99
				mark_subsystem_dirty('aliases');
100
			}
101
			header("Location: firewall_aliases.php?tab=" . $tab);
102
			exit;
103
		}
104
	}
105
}
106

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

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

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

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

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

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

    
162
include("head.inc");
163

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

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

    
175
display_top_tabs($tab_array);
176

    
177
?>
178

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

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

    
262
	</div>
263
</div>
264

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

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

    
293
<?php
294
include("foot.inc");
(40-40/227)