Project

General

Profile

Download (10.5 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
$shortcut_section = "aliases";
165

    
166
include("head.inc");
167

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

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

    
179
display_top_tabs($tab_array);
180

    
181
?>
182

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

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

    
266
	</div>
267
</div>
268

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

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

    
297
<?php
298
include("foot.inc");
(36-36/225)