Project

General

Profile

Download (13 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	firewall_aliases.php
5
	Copyright (C) 2004 Scott Ullrich
6
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
7
	All rights reserved.
8

    
9
	originally part of m0n0wall (http://m0n0.ch/wall)
10
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
	All rights reserved.
12

    
13
	Redistribution and use in source and binary forms, with or without
14
	modification, are permitted provided that the following conditions are met:
15

    
16
	1. Redistributions of source code must retain the above copyright notice,
17
	   this list of conditions and the following disclaimer.
18

    
19
	2. Redistributions in binary form must reproduce the above copyright
20
	   notice, this list of conditions and the following disclaimer in the
21
	   documentation and/or other materials provided with the distribution.
22

    
23
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
	POSSIBILITY OF SUCH DAMAGE.
33
*/
34
/*
35
	pfSense_MODULE:	aliases
36
*/
37

    
38
##|+PRIV
39
##|*IDENT=page-firewall-aliases
40
##|*NAME=Firewall: Aliases page
41
##|*DESCR=Allow access to the 'Firewall: Aliases' page.
42
##|*MATCH=firewall_aliases.php*
43
##|-PRIV
44

    
45
require("guiconfig.inc");
46
require_once("functions.inc");
47
require_once("filter.inc");
48
require_once("shaper.inc");
49

    
50
if (!is_array($config['aliases']['alias'])) {
51
	$config['aliases']['alias'] = array();
52
}
53
$a_aliases = &$config['aliases']['alias'];
54

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

    
57
if ($_POST) {
58

    
59
	if ($_POST['apply']) {
60
		$retval = 0;
61

    
62
		/* reload all components that use aliases */
63
		$retval = filter_configure();
64

    
65
		if (stristr($retval, "error") <> true) {
66
			$savemsg = get_std_save_message($retval);
67
		} else {
68
			$savemsg = $retval;
69
		}
70
		if ($retval == 0) {
71
			clear_subsystem_dirty('aliases');
72
		}
73
	}
74
}
75

    
76
if ($_GET['act'] == "del") {
77
	if ($a_aliases[$_GET['id']]) {
78
		/* make sure rule is not being referenced by any nat or filter rules */
79
		$is_alias_referenced = false;
80
		$referenced_by = false;
81
		$alias_name = $a_aliases[$_GET['id']]['name'];
82
		// Firewall rules
83
		find_alias_reference(array('filter', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
84
		find_alias_reference(array('filter', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
85
		find_alias_reference(array('filter', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
86
		find_alias_reference(array('filter', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
87
		// NAT Rules
88
		find_alias_reference(array('nat', 'rule'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
89
		find_alias_reference(array('nat', 'rule'), array('source', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
90
		find_alias_reference(array('nat', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
91
		find_alias_reference(array('nat', 'rule'), array('destination', 'port'), $alias_name, $is_alias_referenced, $referenced_by);
92
		find_alias_reference(array('nat', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
93
		find_alias_reference(array('nat', 'rule'), array('local-port'), $alias_name, $is_alias_referenced, $referenced_by);
94
		// NAT 1:1 Rules
95
		//find_alias_reference(array('nat', 'onetoone'), array('external'), $alias_name, $is_alias_referenced, $referenced_by);
96
		//find_alias_reference(array('nat', 'onetoone'), array('source', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
97
		find_alias_reference(array('nat', 'onetoone'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
98
		// NAT Outbound Rules
99
		find_alias_reference(array('nat', 'advancedoutbound', 'rule'), array('source', 'network'), $alias_name, $is_alias_referenced, $referenced_by);
100
		find_alias_reference(array('nat', 'advancedoutbound', 'rule'), array('sourceport'), $alias_name, $is_alias_referenced, $referenced_by);
101
		find_alias_reference(array('nat', 'advancedoutbound', 'rule'), array('destination', 'address'), $alias_name, $is_alias_referenced, $referenced_by);
102
		find_alias_reference(array('nat', 'advancedoutbound', 'rule'), array('dstport'), $alias_name, $is_alias_referenced, $referenced_by);
103
		find_alias_reference(array('nat', 'advancedoutbound', 'rule'), array('target'), $alias_name, $is_alias_referenced, $referenced_by);
104
		// Alias in an alias
105
		find_alias_reference(array('aliases', 'alias'), array('address'), $alias_name, $is_alias_referenced, $referenced_by);
106
		// Load Balancer
107
		find_alias_reference(array('load_balancer', 'lbpool'),         array('port'), $alias_name, $is_alias_referenced, $referenced_by);
108
		find_alias_reference(array('load_balancer', 'virtual_server'), array('port'), $alias_name, $is_alias_referenced, $referenced_by);
109
		// Static routes
110
		find_alias_reference(array('staticroutes', 'route'), array('network'), $alias_name, $is_alias_referenced, $referenced_by);
111
		if ($is_alias_referenced == true) {
112
			$savemsg = sprintf(gettext("Cannot delete alias. Currently in use by %s"), $referenced_by);
113
		} else {
114
			unset($a_aliases[$_GET['id']]);
115
			if (write_config()) {
116
				filter_configure();
117
				mark_subsystem_dirty('aliases');
118
			}
119
			header("Location: firewall_aliases.php?tab=" . $tab);
120
			exit;
121
		}
122
	}
123
}
124

    
125
function find_alias_reference($section, $field, $origname, &$is_alias_referenced, &$referenced_by) {
126
	global $config;
127
	if (!$origname || $is_alias_referenced) {
128
		return;
129
	}
130

    
131
	$sectionref = &$config;
132
	foreach ($section as $sectionname) {
133
		if (is_array($sectionref) && isset($sectionref[$sectionname])) {
134
			$sectionref = &$sectionref[$sectionname];
135
		} else {
136
			return;
137
		}
138
	}
139

    
140
	if (is_array($sectionref)) {
141
		foreach ($sectionref as $itemkey => $item) {
142
			$fieldfound = true;
143
			$fieldref = &$sectionref[$itemkey];
144
			foreach ($field as $fieldname) {
145
				if (is_array($fieldref) && isset($fieldref[$fieldname])) {
146
					$fieldref = &$fieldref[$fieldname];
147
				} else {
148
					$fieldfound = false;
149
					break;
150
				}
151
			}
152
			if ($fieldfound && $fieldref == $origname) {
153
				$is_alias_referenced = true;
154
				if (is_array($item)) {
155
					$referenced_by = $item['descr'];
156
				}
157
				break;
158
			}
159
		}
160
	}
161
}
162

    
163
$pgtitle = array(gettext("Firewall"),gettext("Aliases"));
164
$shortcut_section = "aliases";
165

    
166
include("head.inc");
167

    
168
?>
169

    
170
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
171
<?php include("fbegin.inc"); ?>
172
<form action="firewall_aliases.php" method="post">
173
<?php if ($savemsg) print_info_box($savemsg); ?>
174
<?php if (is_subsystem_dirty('aliases')): ?><p>
175
<?php print_info_box_np(gettext("The alias list has been changed.") . "<br />" . gettext("You must apply the changes in order for them to take effect."));?>
176
<?php endif; ?>
177
<?php pfSense_handle_custom_code("/usr/local/pkg/firewall_aliases/pre_table"); ?>
178
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="firewall aliases">
179
	<tr>
180
		<td class="tabnavtbl">
181
			<?php
182
				$tab_array = array();
183
				$tab_array[] = array(gettext("IP"),($tab=="ip" ? true : ($tab=="host" ? true : ($tab == "network" ? true : false))), "/firewall_aliases.php?tab=ip");
184
				$tab_array[] = array(gettext("Ports"), ($tab=="port"? true : false), "/firewall_aliases.php?tab=port");
185
				$tab_array[] = array(gettext("URLs"), ($tab=="url"? true : false), "/firewall_aliases.php?tab=url");
186
				$tab_array[] = array(gettext("All"), ($tab=="all"? true : false), "/firewall_aliases.php?tab=all");
187
				display_top_tabs($tab_array);
188
			?>
189
			<input type="hidden" name="tab" value="<?=htmlspecialchars($tab);?>" />
190
		</td>
191
	</tr>
192
	<tr>
193
		<td>
194
			<div id="mainarea">
195
				<table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0" summary="main area">
196
					<tr>
197
						<td width="20%" class="listhdrr"><?=gettext("Name"); ?></td>
198
						<td width="43%" class="listhdrr"><?=gettext("Values"); ?></td>
199
						<td width="30%" class="listhdr"><?=gettext("Description"); ?></td>
200
						<td width="7%" class="list">
201
							<table  border="0" cellspacing="0" cellpadding="1" summary="add">
202
								<tr>
203
									<td valign="middle" width="17">&nbsp;</td>
204
									<td valign="middle"><a href="firewall_aliases_edit.php?tab=<?=$tab?>"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0" <?=dom_title(gettext("Add a new alias"));?> alt="add" /></a></td>
205
								</tr>
206
							</table>
207
						</td>
208
					</tr>
209
					<?php
210
					asort($a_aliases);
211
					foreach ($a_aliases as $i=> $alias){
212
						unset ($show_alias);
213
						switch ($tab){
214
							case "all":
215
								$show_alias= true;
216
								break;
217
							case "ip":
218
							case "host":
219
							case "network":
220
								if (preg_match("/(host|network)/",$alias["type"])) {
221
									$show_alias= true;
222
								}
223
								break;
224
							case "url":
225
								if (preg_match("/(url)/i",$alias["type"])) {
226
									$show_alias= true;
227
								}
228
								break;
229
							case "port":
230
								if ($alias["type"] == "port") {
231
									$show_alias= true;
232
								}
233
								break;
234
						}
235
						if ($show_alias) {
236
					?>
237
					<tr>
238
						<td class="listlr" ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
239
							<?=htmlspecialchars($alias['name']);?>
240
						</td>
241
						<td class="listr" ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
242
						<?php
243
						if ($alias["url"]) {
244
							echo $alias["url"] . "<br />";
245
						} else {
246
							if (is_array($alias["aliasurl"])) {
247
								$aliasurls = implode(", ", array_slice($alias["aliasurl"], 0, 10));
248
								echo $aliasurls;
249
								if (count($aliasurls) > 10) {
250
									echo "...<br />";
251
								}
252
								echo "<br />\n";
253
							}
254
							$tmpaddr = explode(" ", $alias['address']);
255
							$addresses = implode(", ", array_slice($tmpaddr, 0, 10));
256
							echo $addresses;
257
							if (count($tmpaddr) > 10) {
258
								echo "...";
259
							}
260
						}
261
						?>
262
						</td>
263
						<td class="listbg" ondblclick="document.location='firewall_aliases_edit.php?id=<?=$i;?>';">
264
							<?=htmlspecialchars($alias['descr']);?>&nbsp;
265
						</td>
266
						<td valign="middle" class="list nowrap">
267
							<table border="0" cellspacing="0" cellpadding="1" summary="icons">
268
								<tr>
269
									<td valign="middle"><a href="firewall_aliases_edit.php?id=<?=$i;?>"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" width="17" height="17" border="0" <?=dom_title(gettext("Edit alias")." {$alias['name']}");?> alt="edit" /></a></td>
270
									<td><a href="firewall_aliases.php?act=del&amp;tab=<?=$tab;?>&amp;id=<?=$i;?>" onclick="return confirm('<?=gettext("Do you really want to delete this alias? All elements that still use it will become invalid (e.g. filter rules)!");?>')"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" border="0" <?=dom_title(gettext("Delete alias")." {$alias['name']}");?> alt="delete" /></a></td>
271
								</tr>
272
							</table>
273
						</td>
274
					</tr>
275
					<?php
276
						} // if ($show_alias)
277
					} // foreach
278
					?>
279

    
280
					<tr>
281
						<td colspan="3">&nbsp;</td>
282
						<td valign="middle" class="list nowrap">
283
							<table border="0" cellspacing="0" cellpadding="1" summary="edit">
284
								<tbody>
285
									<tr>
286
										<td valign="middle">
287
											<a href="firewall_aliases_edit.php?tab=<?=$tab?>"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0" <?=dom_title(gettext("Add a new alias")); ?> alt="add" /></a>
288
										</td>
289
										<td valign="middle">
290
											<a href="firewall_aliases_import.php"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_import_alias.gif" width="17" height="17" border="0" <?=dom_title(gettext("Bulk import aliases from list"));?> alt="import" /></a>
291
										</td>
292
									</tr>
293
								</tbody>
294
							</table>
295
						</td>
296
					</tr>
297

    
298
					<tr>
299
						<td class="tabcont" colspan="3">
300
							<p><span class="vexpl"><span class="red"><strong><?=gettext("Note:"); ?><br /></strong></span></span></p><div style="overflow:hidden; text-align:justify;"><p><span class="vexpl"><?=gettext("Aliases act as placeholders for real hosts, networks or ports. They can be used to minimize the number of changes that have to be made if a host, network or port changes. You can enter the name of an alias instead of the host, network or port in all fields that have a red background. The alias will be resolved according to the list above. If an alias cannot be resolved (e.g. because you deleted it), the corresponding element (e.g. filter/NAT/shaper rule) will be considered invalid and skipped."); ?></span></p></div>
301
						</td>
302
					</tr>
303
				</table>
304
			</div>
305
		</td>
306
	</tr>
307
</table>
308
</form>
309
<?php include("fend.inc"); ?>
310
</body>
311
</html>
(60-60/256)