1
|
<?php
|
2
|
/* $Id$ */
|
3
|
/*
|
4
|
firewall_aliases_import.php
|
5
|
Copyright (C) 2005 Scott Ullrich
|
6
|
All rights reserved.
|
7
|
|
8
|
Redistribution and use in source and binary forms, with or without
|
9
|
modification, are permitted provided that the following conditions are met:
|
10
|
|
11
|
1. Redistributions of source code must retain the above copyright notice,
|
12
|
this list of conditions and the following disclaimer.
|
13
|
|
14
|
2. Redistributions in binary form must reproduce the above copyright
|
15
|
notice, this list of conditions and the following disclaimer in the
|
16
|
documentation and/or other materials provided with the distribution.
|
17
|
|
18
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
19
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
20
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
21
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
22
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
23
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
25
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
26
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27
|
POSSIBILITY OF SUCH DAMAGE.
|
28
|
*/
|
29
|
/*
|
30
|
pfSense_MODULE: filter
|
31
|
*/
|
32
|
|
33
|
##|+PRIV
|
34
|
##|*IDENT=page-firewall-alias-import
|
35
|
##|*NAME=Firewall: Alias: Import page
|
36
|
##|*DESCR=Allow access to the 'Firewall: Alias: Import' page.
|
37
|
##|*MATCH=firewall_aliases_import.php*
|
38
|
##|-PRIV
|
39
|
|
40
|
|
41
|
// Keywords not allowed in names
|
42
|
$reserved_keywords = array("all", "pass", "block", "out", "queue", "max", "min", "pptp", "pppoe", "L2TP", "OpenVPN", "IPsec");
|
43
|
|
44
|
require("guiconfig.inc");
|
45
|
require_once("util.inc");
|
46
|
require_once("filter.inc");
|
47
|
require("shaper.inc");
|
48
|
|
49
|
$pgtitle = array(gettext("Firewall"),gettext("Aliases"),gettext("Bulk import"));
|
50
|
|
51
|
$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/firewall_aliases.php');
|
52
|
|
53
|
// Add all Load balance names to reserved_keywords
|
54
|
if (is_array($config['load_balancer']['lbpool']))
|
55
|
foreach ($config['load_balancer']['lbpool'] as $lbpool)
|
56
|
$reserved_keywords[] = $lbpool['name'];
|
57
|
|
58
|
$reserved_ifs = get_configured_interface_list(false, true);
|
59
|
$reserved_keywords = array_merge($reserved_keywords, $reserved_ifs, $reserved_table_names);
|
60
|
|
61
|
if (!is_array($config['aliases']['alias']))
|
62
|
$config['aliases']['alias'] = array();
|
63
|
$a_aliases = &$config['aliases']['alias'];
|
64
|
|
65
|
if($_POST['aliasimport'] <> "") {
|
66
|
$reqdfields = explode(" ", "name aliasimport");
|
67
|
$reqdfieldsn = array(gettext("Name"),gettext("Aliases"));
|
68
|
|
69
|
do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
|
70
|
|
71
|
if (is_validaliasname($_POST['name']) == false)
|
72
|
$input_errors[] = gettext("The alias name may only consist of the characters") . " a-z, A-Z, 0-9, _.";
|
73
|
|
74
|
/* check for name duplicates */
|
75
|
if (is_alias($_POST['name']))
|
76
|
$input_errors[] = gettext("An alias with this name already exists.");
|
77
|
|
78
|
|
79
|
/* Check for reserved keyword names */
|
80
|
foreach($reserved_keywords as $rk)
|
81
|
if ($rk == $_POST['name'])
|
82
|
$input_errors[] = sprintf(gettext("Cannot use a reserved keyword as alias name %s"), $rk);
|
83
|
|
84
|
/* check for name interface description conflicts */
|
85
|
foreach($config['interfaces'] as $interface) {
|
86
|
if($interface['descr'] == $_POST['name']) {
|
87
|
$input_errors[] = gettext("An interface description with this name already exists.");
|
88
|
break;
|
89
|
}
|
90
|
}
|
91
|
|
92
|
if ($_POST['aliasimport']) {
|
93
|
$tocheck = explode("\n", $_POST['aliasimport']);
|
94
|
$imported_ips = array();
|
95
|
$imported_descs = array();
|
96
|
$desc_len_err_found = false;
|
97
|
$desc_fmt_err_found = false;
|
98
|
foreach ($tocheck as $impline) {
|
99
|
$implinea = explode(" ",trim($impline),2);
|
100
|
$impip = $implinea[0];
|
101
|
$impdesc = trim($implinea[1]);
|
102
|
if (strlen($impdesc) < 200) {
|
103
|
if ((strpos($impdesc, "||") === false) && (substr($impdesc, 0, 1) != "|") && (substr($impdesc, -1, 1) != "|")) {
|
104
|
$iprange_type = is_iprange($impip);
|
105
|
if ($iprange_type == 4) {
|
106
|
list($startip, $endip) = explode('-', $impip);
|
107
|
$rangesubnets = ip_range_to_subnet_array($startip, $endip);
|
108
|
$imported_ips = array_merge($imported_ips, $rangesubnets);
|
109
|
$rangedescs = array_fill(0, count($rangesubnets), $impdesc);
|
110
|
$imported_descs = array_merge($imported_descs, $rangedescs);
|
111
|
} else if ($iprange_type == 6) {
|
112
|
$input_errors[] = sprintf(gettext('IPv6 address ranges are not supported (%s)'), $impip);
|
113
|
} else if (!is_ipaddr($impip) && !is_subnet($impip) && !is_hostname($impip) && !empty($impip)) {
|
114
|
$input_errors[] = sprintf(gettext("%s is not an IP address. Please correct the error to continue"), $impip);
|
115
|
} elseif (!empty($impip)) {
|
116
|
$imported_ips[] = $impip;
|
117
|
$imported_descs[] = $impdesc;
|
118
|
}
|
119
|
}
|
120
|
else {
|
121
|
if (!$desc_fmt_err_found) {
|
122
|
$input_errors[] = gettext("Descriptions may not start or end with vertical bar (|) or contain double vertical bar ||.");
|
123
|
$desc_fmt_err_found = true;
|
124
|
}
|
125
|
}
|
126
|
}
|
127
|
else {
|
128
|
if (!$desc_len_err_found) {
|
129
|
/* Note: The 200 character limit is just a practical check to avoid accidents */
|
130
|
/* if the user pastes a large number of IP addresses without line breaks. */
|
131
|
$input_errors[] = gettext("Descriptions must be less than 200 characters long.");
|
132
|
$desc_len_err_found = true;
|
133
|
}
|
134
|
}
|
135
|
}
|
136
|
unset($desc_len_err_found, $desc_fmt_err_found);
|
137
|
}
|
138
|
|
139
|
if (!$input_errors && is_array($imported_ips)) {
|
140
|
$alias = array();
|
141
|
$alias['address'] = implode(" ", $imported_ips);
|
142
|
$alias['detail'] = implode("||", $imported_descs);
|
143
|
$alias['name'] = $_POST['name'];
|
144
|
$alias['type'] = "network";
|
145
|
$alias['descr'] = $_POST['descr'];
|
146
|
unset($imported_ips, $imported_descs);
|
147
|
$a_aliases[] = $alias;
|
148
|
|
149
|
// Sort list
|
150
|
$a_aliases = msort($a_aliases, "name");
|
151
|
|
152
|
if (write_config())
|
153
|
mark_subsystem_dirty('aliases');
|
154
|
pfSenseHeader("firewall_aliases.php");
|
155
|
|
156
|
exit;
|
157
|
}
|
158
|
}
|
159
|
|
160
|
include("head.inc");
|
161
|
|
162
|
?>
|
163
|
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
|
164
|
<?php include("fbegin.inc"); ?>
|
165
|
<?php if ($input_errors) print_input_errors($input_errors); ?>
|
166
|
<div id="niftyOutter">
|
167
|
<form action="firewall_aliases_import.php" method="post" name="iform" id="iform">
|
168
|
<div id="inputerrors"></div>
|
169
|
<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="firewall alias import">
|
170
|
<tr>
|
171
|
<td colspan="2" valign="top" class="listtopic"><?=gettext("Alias Import"); ?></td>
|
172
|
</tr>
|
173
|
<tr>
|
174
|
<td valign="top" class="vncellreq"><?=gettext("Alias Name"); ?></td>
|
175
|
<td class="vtable">
|
176
|
<input name="name" type="text" class="formfld unknown" id="name" size="40" maxlength="31" value="<?=htmlspecialchars($_POST['name']);?>" />
|
177
|
<br />
|
178
|
<span class="vexpl">
|
179
|
<?=gettext("The name of the alias may only consist of the characters \"a-z, A-Z and 0-9\"."); ?>
|
180
|
</span>
|
181
|
</td>
|
182
|
</tr>
|
183
|
<tr>
|
184
|
<td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
|
185
|
<td width="78%" class="vtable">
|
186
|
<input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($_POST['descr']);?>" />
|
187
|
<br />
|
188
|
<span class="vexpl">
|
189
|
<?=gettext("You may enter a description here for your reference (not parsed)"); ?>.
|
190
|
</span>
|
191
|
</td>
|
192
|
</tr>
|
193
|
<tr>
|
194
|
<td valign="top" class="vncellreq"><?=gettext("Aliases to import"); ?></td>
|
195
|
<td class="vtable">
|
196
|
<textarea name="aliasimport" rows="15" cols="40"><?php echo $_POST['aliasimport']; ?></textarea>
|
197
|
<br />
|
198
|
<span class="vexpl">
|
199
|
<?=gettext("Paste in the aliases to import separated by a carriage return. Common examples are lists of IPs, networks, blacklists, etc."); ?>
|
200
|
<br />
|
201
|
<?=gettext("The list may contain IP addresses, with or without CIDR prefix, IP ranges, blank lines (ignored) and an optional description after each IP. e.g.:"); ?>
|
202
|
<br />172.16.1.2
|
203
|
<br />172.16.0.0/24
|
204
|
<br />10.11.12.100-10.11.12.200
|
205
|
<br />192.168.1.254 Home router
|
206
|
<br />10.20.0.0/16 Office network
|
207
|
<br />10.40.1.10-10.40.1.19 Managed switches
|
208
|
</span>
|
209
|
</td>
|
210
|
</tr>
|
211
|
<tr>
|
212
|
<td width="22%" valign="top"> </td>
|
213
|
<td width="78%">
|
214
|
<input id="submit" name="Submit" type="submit" class="formbtn" value="<?=gettext("Save"); ?>" />
|
215
|
<input type="button" class="formbtn" value="<?=gettext("Cancel");?>" onclick="window.location.href='<?=$referer;?>'" />
|
216
|
</td>
|
217
|
</tr>
|
218
|
</table>
|
219
|
|
220
|
|
221
|
</form>
|
222
|
</div>
|
223
|
|
224
|
<?php include("fend.inc"); ?>
|
225
|
|
226
|
<script type="text/javascript">
|
227
|
//<![CDATA[
|
228
|
NiftyCheck();
|
229
|
Rounded("div#nifty","top","#FFF","#EEEEEE","smooth");
|
230
|
//]]>
|
231
|
</script>
|
232
|
|
233
|
</body>
|
234
|
</html>
|