Project

General

Profile

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

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

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

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

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

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

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

    
160
include("head.inc");
161

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

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

    
173
display_top_tabs($tab_array);
174

    
175
?>
176

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

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

    
260
	</div>
261
</div>
262

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

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

    
291
<?php
292
include("foot.inc");
(39-39/225)