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) {
46

    
47
	if ($_POST['apply']) {
48
		$retval = 0;
49

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

    
53
		if ($retval == 0) {
54
			clear_subsystem_dirty('aliases');
55
		}
56
	}
57
}
58

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

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

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

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

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

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

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

    
167
include("head.inc");
168

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

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

    
180
display_top_tabs($tab_array);
181

    
182
?>
183

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

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

    
267
	</div>
268
</div>
269

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

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

    
298
<?php
299
include("foot.inc");
(34-34/223)