Projet

Général

Profil

Télécharger (8,15 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / usr / local / www / firewall_aliases_import.php @ 62424bdb

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
					if (is_iprange($impip)) {
105
						list($startip, $endip) = explode('-', $impip);
106
						$rangesubnets = ip_range_to_subnet_array($startip, $endip);
107
						$imported_ips = array_merge($imported_ips, $rangesubnets);
108
						$rangedescs = array_fill(0, count($rangesubnets), $impdesc);
109
						$imported_descs = array_merge($imported_descs, $rangedescs);
110
					} else if (!is_ipaddr($impip) && !is_subnet($impip) && !is_hostname($impip) && !empty($impip)) {
111
						$input_errors[] = sprintf(gettext("%s is not an IP address. Please correct the error to continue"), $impip);
112
					} elseif (!empty($impip)) {
113
						$imported_ips[] = $impip;
114
						$imported_descs[] = $impdesc;
115
					}
116
				}
117
				else {
118
					if (!$desc_fmt_err_found) {
119
						$input_errors[] = gettext("Descriptions may not start or end with vertical bar (|) or contain double vertical bar ||.");
120
						$desc_fmt_err_found = true;
121
					}
122
				}
123
			}
124
			else {
125
				if (!$desc_len_err_found) {
126
					/* Note: The 200 character limit is just a practical check to avoid accidents */
127
					/* if the user pastes a large number of IP addresses without line breaks.     */
128
					$input_errors[] = gettext("Descriptions must be less than 200 characters long.");
129
					$desc_len_err_found = true;
130
				}
131
			}
132
		}
133
		unset($desc_len_err_found, $desc_fmt_err_found);
134
	}
135

    
136
	if (!$input_errors && is_array($imported_ips)) {
137
		$alias = array();
138
		$alias['address'] = implode(" ", $imported_ips);
139
		$alias['detail'] = implode("||", $imported_descs);
140
		$alias['name'] = $_POST['name'];
141
		$alias['type'] = "network";
142
		$alias['descr'] = $_POST['descr'];
143
		unset($imported_ips, $imported_descs);
144
		$a_aliases[] = $alias;
145

    
146
		// Sort list
147
		$a_aliases = msort($a_aliases, "name");
148

    
149
		if (write_config())
150
			mark_subsystem_dirty('aliases');
151
		pfSenseHeader("firewall_aliases.php");
152

    
153
		exit;
154
	}
155
}
156

    
157
include("head.inc");
158

    
159
?>
160
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
161
<?php include("fbegin.inc"); ?>
162
<?php if ($input_errors) print_input_errors($input_errors); ?>
163
<div id="niftyOutter">
164
<form action="firewall_aliases_import.php" method="post" name="iform" id="iform">
165
<div id="inputerrors"></div>
166
<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="firewall alias import">
167
	<tr>
168
		<td colspan="2" valign="top" class="listtopic"><?=gettext("Alias Import"); ?></td>
169
	</tr>
170
	<tr>
171
		<td valign="top" class="vncellreq"><?=gettext("Alias Name"); ?></td>
172
		<td class="vtable">
173
			<input name="name" type="text" class="formfld unknown" id="name" size="40" maxlength="31" value="<?=htmlspecialchars($_POST['name']);?>" />
174
			<br />
175
			<span class="vexpl">
176
				<?=gettext("The name of the alias may only consist of the characters \"a-z, A-Z and 0-9\"."); ?>
177
			</span>
178
		</td>
179
	</tr>
180
	<tr>
181
		<td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
182
		<td width="78%" class="vtable">
183
			<input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($_POST['descr']);?>" />
184
			<br />
185
			<span class="vexpl">
186
				<?=gettext("You may enter a description here for your reference (not parsed)"); ?>.
187
			</span>
188
		</td>
189
	</tr>
190
	<tr>
191
		<td valign="top" class="vncellreq"><?=gettext("Aliases to import"); ?></td>
192
		<td class="vtable">
193
			<textarea name="aliasimport" rows="15" cols="40"><?php echo $_POST['aliasimport']; ?></textarea>
194
			<br />
195
			<span class="vexpl">
196
				<?=gettext("Paste in the aliases to import separated by a carriage return.  Common examples are lists of IPs, networks, blacklists, etc."); ?>
197
				<br />
198
				<?=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.:"); ?>
199
				<br />172.16.1.2
200
				<br />172.16.0.0/24
201
				<br />10.11.12.100-10.11.12.200
202
				<br />192.168.1.254 Home router
203
				<br />10.20.0.0/16 Office network
204
				<br />10.40.1.10-10.40.1.19 Managed switches
205
			</span>
206
		</td>
207
	</tr>
208
	<tr>
209
		<td width="22%" valign="top">&nbsp;</td>
210
		<td width="78%">
211
			<input id="submit" name="Submit" type="submit" class="formbtn" value="<?=gettext("Save"); ?>" />
212
			<input type="button" class="formbtn" value="<?=gettext("Cancel");?>" onclick="window.location.href='<?=$referer;?>'" />
213
		</td>
214
	</tr>
215
</table>
216

    
217

    
218
</form>
219
</div>
220

    
221
<?php include("fend.inc"); ?>
222

    
223
<script type="text/javascript">
224
//<![CDATA[
225
	NiftyCheck();
226
	Rounded("div#nifty","top","#FFF","#EEEEEE","smooth");
227
//]]>
228
</script>
229

    
230
</body>
231
</html>
(62-62/256)