1
|
#!/usr/local/bin/php
|
2
|
<?php
|
3
|
/*
|
4
|
firewall_nat_edit.php
|
5
|
part of m0n0wall (http://m0n0.ch/wall)
|
6
|
|
7
|
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
8
|
All rights reserved.
|
9
|
|
10
|
Redistribution and use in source and binary forms, with or without
|
11
|
modification, are permitted provided that the following conditions are met:
|
12
|
|
13
|
1. Redistributions of source code must retain the above copyright notice,
|
14
|
this list of conditions and the following disclaimer.
|
15
|
|
16
|
2. Redistributions in binary form must reproduce the above copyright
|
17
|
notice, this list of conditions and the following disclaimer in the
|
18
|
documentation and/or other materials provided with the distribution.
|
19
|
|
20
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
21
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
22
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
23
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
24
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
25
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
26
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
27
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
28
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29
|
POSSIBILITY OF SUCH DAMAGE.
|
30
|
*/
|
31
|
|
32
|
require("guiconfig.inc");
|
33
|
|
34
|
if (!is_array($config['nat']['rule'])) {
|
35
|
$config['nat']['rule'] = array();
|
36
|
}
|
37
|
nat_rules_sort();
|
38
|
$a_nat = &$config['nat']['rule'];
|
39
|
|
40
|
$id = $_GET['id'];
|
41
|
if (isset($_POST['id']))
|
42
|
$id = $_POST['id'];
|
43
|
|
44
|
if (isset($id) && $a_nat[$id]) {
|
45
|
$pconfig['extaddr'] = $a_nat[$id]['external-address'];
|
46
|
$pconfig['proto'] = $a_nat[$id]['protocol'];
|
47
|
list($pconfig['beginport'],$pconfig['endport']) = explode("-", $a_nat[$id]['external-port']);
|
48
|
$pconfig['localip'] = $a_nat[$id]['target'];
|
49
|
$pconfig['localbeginport'] = $a_nat[$id]['local-port'];
|
50
|
$pconfig['descr'] = $a_nat[$id]['descr'];
|
51
|
$pconfig['interface'] = $a_nat[$id]['interface'];
|
52
|
if (!$pconfig['interface'])
|
53
|
$pconfig['interface'] = "wan";
|
54
|
} else {
|
55
|
$pconfig['interface'] = "wan";
|
56
|
}
|
57
|
|
58
|
if ($_POST) {
|
59
|
|
60
|
if ($_POST['beginport_cust'] && !$_POST['beginport'])
|
61
|
$_POST['beginport'] = $_POST['beginport_cust'];
|
62
|
if ($_POST['endport_cust'] && !$_POST['endport'])
|
63
|
$_POST['endport'] = $_POST['endport_cust'];
|
64
|
if ($_POST['localbeginport_cust'] && !$_POST['localbeginport'])
|
65
|
$_POST['localbeginport'] = $_POST['localbeginport_cust'];
|
66
|
|
67
|
if (!$_POST['endport'])
|
68
|
$_POST['endport'] = $_POST['beginport'];
|
69
|
|
70
|
unset($input_errors);
|
71
|
$pconfig = $_POST;
|
72
|
|
73
|
/* input validation */
|
74
|
$reqdfields = explode(" ", "interface proto beginport localip localbeginport");
|
75
|
$reqdfieldsn = explode(",", "Interface,Protocol,Start port,NAT IP,Local port");
|
76
|
|
77
|
do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
|
78
|
|
79
|
if (($_POST['beginport'] && !is_port($_POST['beginport']))) {
|
80
|
$input_errors[] = "The start port must be an integer between 1 and 65535.";
|
81
|
}
|
82
|
if (($_POST['endport'] && !is_port($_POST['endport']))) {
|
83
|
$input_errors[] = "The end port must be an integer between 1 and 65535.";
|
84
|
}
|
85
|
if (($_POST['localbeginport'] && !is_port($_POST['localbeginport']))) {
|
86
|
$input_errors[] = "The local port must be an integer between 1 and 65535.";
|
87
|
}
|
88
|
if (($_POST['localip'] && !is_ipaddroralias($_POST['localip']))) {
|
89
|
$input_errors[] = "A valid NAT IP address or host alias must be specified.";
|
90
|
}
|
91
|
|
92
|
if ($_POST['beginport'] > $_POST['endport']) {
|
93
|
/* swap */
|
94
|
$tmp = $_POST['endport'];
|
95
|
$_POST['endport'] = $_POST['beginport'];
|
96
|
$_POST['beginport'] = $tmp;
|
97
|
}
|
98
|
|
99
|
if (!$input_errors) {
|
100
|
if (($_POST['endport'] - $_POST['beginport'] + $_POST['localbeginport']) > 65535)
|
101
|
$input_errors[] = "The target port range must lie between 1 and 65535.";
|
102
|
}
|
103
|
|
104
|
/* check for overlaps */
|
105
|
foreach ($a_nat as $natent) {
|
106
|
if (isset($id) && ($a_nat[$id]) && ($a_nat[$id] === $natent))
|
107
|
continue;
|
108
|
if ($natent['interface'] != $_POST['interface'])
|
109
|
continue;
|
110
|
if ($natent['external-address'] != $_POST['extaddr'])
|
111
|
continue;
|
112
|
|
113
|
list($begp,$endp) = explode("-", $natent['external-port']);
|
114
|
if (!$endp)
|
115
|
$endp = $begp;
|
116
|
|
117
|
if (!( (($_POST['beginport'] < $begp) && ($_POST['endport'] < $begp))
|
118
|
|| (($_POST['beginport'] > $endp) && ($_POST['endport'] > $endp)))) {
|
119
|
|
120
|
$input_errors[] = "The external port range overlaps with an existing entry.";
|
121
|
break;
|
122
|
}
|
123
|
}
|
124
|
|
125
|
if (!$input_errors) {
|
126
|
$natent = array();
|
127
|
if ($_POST['extaddr'])
|
128
|
$natent['external-address'] = $_POST['extaddr'];
|
129
|
$natent['protocol'] = $_POST['proto'];
|
130
|
|
131
|
if ($_POST['beginport'] == $_POST['endport'])
|
132
|
$natent['external-port'] = $_POST['beginport'];
|
133
|
else
|
134
|
$natent['external-port'] = $_POST['beginport'] . "-" . $_POST['endport'];
|
135
|
|
136
|
$natent['target'] = $_POST['localip'];
|
137
|
$natent['local-port'] = $_POST['localbeginport'];
|
138
|
$natent['interface'] = $_POST['interface'];
|
139
|
$natent['descr'] = $_POST['descr'];
|
140
|
|
141
|
if (isset($id) && $a_nat[$id])
|
142
|
$a_nat[$id] = $natent;
|
143
|
else
|
144
|
$a_nat[] = $natent;
|
145
|
|
146
|
touch($d_natconfdirty_path);
|
147
|
|
148
|
if ($_POST['autoadd']) {
|
149
|
/* auto-generate a matching firewall rule */
|
150
|
$filterent = array();
|
151
|
$filterent['interface'] = $_POST['interface'];
|
152
|
$filterent['protocol'] = $_POST['proto'];
|
153
|
$filterent['source']['any'] = "";
|
154
|
$filterent['destination']['address'] = $_POST['localip'];
|
155
|
|
156
|
$dstpfrom = $_POST['localbeginport'];
|
157
|
$dstpto = $dstpfrom + $_POST['endport'] - $_POST['beginport'];
|
158
|
|
159
|
if ($dstpfrom == $dstpto)
|
160
|
$filterent['destination']['port'] = $dstpfrom;
|
161
|
else
|
162
|
$filterent['destination']['port'] = $dstpfrom . "-" . $dstpto;
|
163
|
|
164
|
$filterent['descr'] = "NAT " . $_POST['descr'];
|
165
|
|
166
|
$config['filter']['rule'][] = $filterent;
|
167
|
|
168
|
touch($d_filterconfdirty_path);
|
169
|
}
|
170
|
|
171
|
write_config();
|
172
|
|
173
|
header("Location: firewall_nat.php");
|
174
|
exit;
|
175
|
}
|
176
|
}
|
177
|
?>
|
178
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
179
|
<html>
|
180
|
<head>
|
181
|
<title><?=gentitle("Firewall: NAT: Edit inbound");?></title>
|
182
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
183
|
<link href="gui.css" rel="stylesheet" type="text/css">
|
184
|
<script language="JavaScript">
|
185
|
<!--
|
186
|
function ext_change() {
|
187
|
if (document.iform.beginport.selectedIndex == 0) {
|
188
|
document.iform.beginport_cust.disabled = 0;
|
189
|
} else {
|
190
|
document.iform.beginport_cust.value = "";
|
191
|
document.iform.beginport_cust.disabled = 1;
|
192
|
}
|
193
|
if (document.iform.endport.selectedIndex == 0) {
|
194
|
document.iform.endport_cust.disabled = 0;
|
195
|
} else {
|
196
|
document.iform.endport_cust.value = "";
|
197
|
document.iform.endport_cust.disabled = 1;
|
198
|
}
|
199
|
if (document.iform.localbeginport.selectedIndex == 0) {
|
200
|
document.iform.localbeginport_cust.disabled = 0;
|
201
|
} else {
|
202
|
document.iform.localbeginport_cust.value = "";
|
203
|
document.iform.localbeginport_cust.disabled = 1;
|
204
|
}
|
205
|
}
|
206
|
function ext_rep_change() {
|
207
|
document.iform.endport.selectedIndex = document.iform.beginport.selectedIndex;
|
208
|
document.iform.localbeginport.selectedIndex = document.iform.beginport.selectedIndex;
|
209
|
}
|
210
|
//-->
|
211
|
</script>
|
212
|
</head>
|
213
|
|
214
|
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
|
215
|
<?php include("fbegin.inc"); ?>
|
216
|
<p class="pgtitle">Firewall: NAT: Edit inbound</p>
|
217
|
<?php if ($input_errors) print_input_errors($input_errors); ?>
|
218
|
<form action="firewall_nat_edit.php" method="post" name="iform" id="iform">
|
219
|
<table width="100%" border="0" cellpadding="6" cellspacing="0">
|
220
|
<tr>
|
221
|
<td width="22%" valign="top" class="vncellreq">Interface</td>
|
222
|
<td width="78%" class="vtable">
|
223
|
<select name="interface" class="formfld">
|
224
|
<?php
|
225
|
$interfaces = array('wan' => 'WAN', 'lan' => 'LAN');
|
226
|
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
|
227
|
$interfaces['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];
|
228
|
}
|
229
|
foreach ($interfaces as $iface => $ifacename): ?>
|
230
|
<option value="<?=$iface;?>" <?php if ($iface == $pconfig['interface']) echo "selected"; ?>>
|
231
|
<?=htmlspecialchars($ifacename);?>
|
232
|
</option>
|
233
|
<?php endforeach; ?>
|
234
|
</select><br>
|
235
|
<span class="vexpl">Choose which interface this rule applies to.<br>
|
236
|
Hint: in most cases, you'll want to use WAN here.</span></td>
|
237
|
</tr>
|
238
|
<tr>
|
239
|
<td width="22%" valign="top" class="vncellreq">External address</td>
|
240
|
<td width="78%" class="vtable">
|
241
|
<select name="extaddr" class="formfld">
|
242
|
<option value="" <?php if (!$pconfig['extaddr']) echo "selected"; ?>>Interface address</option>
|
243
|
<?php
|
244
|
if (is_array($config['nat']['servernat'])):
|
245
|
foreach ($config['nat']['servernat'] as $sn): ?>
|
246
|
<option value="<?=$sn['ipaddr'];?>" <?php if ($sn['ipaddr'] == $pconfig['extaddr']) echo "selected"; ?>><?=htmlspecialchars("{$sn['ipaddr']} ({$sn['descr']})");?></option>
|
247
|
<?php endforeach; endif; ?>
|
248
|
</select><br>
|
249
|
<span class="vexpl">
|
250
|
If you want this rule to apply to another IP address than the IP address of the interface chosen above,
|
251
|
select it here (you need to define IP addresses on the
|
252
|
<a href="firewall_nat_server.php">Server NAT</a> page first).</span></td>
|
253
|
</tr>
|
254
|
<tr>
|
255
|
<td width="22%" valign="top" class="vncellreq">Protocol</td>
|
256
|
<td width="78%" class="vtable">
|
257
|
<select name="proto" class="formfld">
|
258
|
<?php $protocols = explode(" ", "TCP UDP TCP/UDP"); foreach ($protocols as $proto): ?>
|
259
|
<option value="<?=strtolower($proto);?>" <?php if (strtolower($proto) == $pconfig['proto']) echo "selected"; ?>><?=htmlspecialchars($proto);?></option>
|
260
|
<?php endforeach; ?>
|
261
|
</select> <br> <span class="vexpl">Choose which IP protocol
|
262
|
this rule should match.<br>
|
263
|
Hint: in most cases, you should specify <em>TCP</em> here.</span></td>
|
264
|
</tr>
|
265
|
<tr>
|
266
|
<td width="22%" valign="top" class="vncellreq">External port
|
267
|
range </td>
|
268
|
<td width="78%" class="vtable">
|
269
|
<table border="0" cellspacing="0" cellpadding="0">
|
270
|
<tr>
|
271
|
<td>from: </td>
|
272
|
<td><select name="beginport" class="formfld" onChange="ext_rep_change();ext_change()">
|
273
|
<option value="">(other)</option>
|
274
|
<?php $bfound = 0; foreach ($wkports as $wkport => $wkportdesc): ?>
|
275
|
<option value="<?=$wkport;?>" <?php if ($wkport == $pconfig['beginport']) {
|
276
|
echo "selected";
|
277
|
$bfound = 1;
|
278
|
}?>>
|
279
|
<?=htmlspecialchars($wkportdesc);?>
|
280
|
</option>
|
281
|
<?php endforeach; ?>
|
282
|
</select> <input name="beginport_cust" type="text" size="5" value="<?php if (!$bfound) echo $pconfig['beginport']; ?>"></td>
|
283
|
</tr>
|
284
|
<tr>
|
285
|
<td>to:</td>
|
286
|
<td><select name="endport" class="formfld" onChange="ext_change()">
|
287
|
<option value="">(other)</option>
|
288
|
<?php $bfound = 0; foreach ($wkports as $wkport => $wkportdesc): ?>
|
289
|
<option value="<?=$wkport;?>" <?php if ($wkport == $pconfig['endport']) {
|
290
|
echo "selected";
|
291
|
$bfound = 1;
|
292
|
}?>>
|
293
|
<?=htmlspecialchars($wkportdesc);?>
|
294
|
</option>
|
295
|
<?php endforeach; ?>
|
296
|
</select> <input name="endport_cust" type="text" size="5" value="<?php if (!$bfound) echo $pconfig['endport']; ?>"></td>
|
297
|
</tr>
|
298
|
</table>
|
299
|
<br> <span class="vexpl">Specify the port or port range on
|
300
|
the firewall's external address for this mapping.<br>
|
301
|
Hint: you can leave the <em>'to'</em> field empty if you only
|
302
|
want to map a single port</span></td>
|
303
|
</tr>
|
304
|
<tr>
|
305
|
<td width="22%" valign="top" class="vncellreq">NAT IP</td>
|
306
|
<td width="78%" class="vtable">
|
307
|
<input autocomplete='off' onblur='actb_removedisp()' onkeypress='return (event.keyCode!=13);' onkeydown='actb_checkkey(event, this)' onkeyup='actb_tocomplete(this,event,addressarray);' name="localip" type="text" class="formfldalias" id="localip" size="20" value="<?=htmlspecialchars($pconfig['localip']);?>">
|
308
|
<br> <span class="vexpl">Enter the internal IP address of
|
309
|
the server on which you want to map the ports.<br>
|
310
|
e.g. <em>192.168.1.12</em></span></td>
|
311
|
</tr>
|
312
|
<tr>
|
313
|
<td width="22%" valign="top" class="vncellreq">Local port</td>
|
314
|
<td width="78%" class="vtable">
|
315
|
<select name="localbeginport" class="formfld" onChange="ext_change()">
|
316
|
<option value="">(other)</option>
|
317
|
<?php $bfound = 0; foreach ($wkports as $wkport => $wkportdesc): ?>
|
318
|
<option value="<?=$wkport;?>" <?php if ($wkport == $pconfig['localbeginport']) {
|
319
|
echo "selected";
|
320
|
$bfound = 1;
|
321
|
}?>>
|
322
|
<?=htmlspecialchars($wkportdesc);?>
|
323
|
</option>
|
324
|
<?php endforeach; ?>
|
325
|
</select> <input name="localbeginport_cust" type="text" size="5" value="<?php if (!$bfound) echo $pconfig['localbeginport']; ?>">
|
326
|
<br>
|
327
|
<span class="vexpl">Specify the port on the machine with the
|
328
|
IP address entered above. In case of a port range, specify
|
329
|
the beginning port of the range (the end port will be calculated
|
330
|
automatically).<br>
|
331
|
Hint: this is usually identical to the 'from' port above</span></td>
|
332
|
</tr>
|
333
|
<tr>
|
334
|
<td width="22%" valign="top" class="vncell">Description</td>
|
335
|
<td width="78%" class="vtable">
|
336
|
<input name="descr" type="text" class="formfld" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
|
337
|
<br> <span class="vexpl">You may enter a description here
|
338
|
for your reference (not parsed).</span></td>
|
339
|
</tr><?php if (!(isset($id) && $a_nat[$id])): ?>
|
340
|
<tr>
|
341
|
<td width="22%" valign="top"> </td>
|
342
|
<td width="78%">
|
343
|
<input name="autoadd" type="checkbox" id="autoadd" value="yes">
|
344
|
<strong>Auto-add a firewall rule to permit traffic through
|
345
|
this NAT rule</strong></td>
|
346
|
</tr><?php endif; ?>
|
347
|
<tr>
|
348
|
<td width="22%" valign="top"> </td>
|
349
|
<td width="78%">
|
350
|
<input name="Submit" type="submit" class="formbtn" value="Save">
|
351
|
<?php if (isset($id) && $a_nat[$id]): ?>
|
352
|
<input name="id" type="hidden" value="<?=$id;?>">
|
353
|
<?php endif; ?>
|
354
|
</td>
|
355
|
</tr>
|
356
|
</table>
|
357
|
</form>
|
358
|
<script language="JavaScript">
|
359
|
<!--
|
360
|
ext_change();
|
361
|
//-->
|
362
|
</script>
|
363
|
<?php
|
364
|
$isfirst = 0;
|
365
|
$aliases = "";
|
366
|
$addrisfirst = 0;
|
367
|
$aliasesaddr = "";
|
368
|
if($config['aliases']['alias'] <> "")
|
369
|
foreach($config['aliases']['alias'] as $alias_name) {
|
370
|
if(!stristr($alias_name['address'], ".")) {
|
371
|
if($isfirst == 1) $aliases .= ",";
|
372
|
$aliases .= "'" . $alias_name['name'] . "'";
|
373
|
$isfirst = 1;
|
374
|
} else {
|
375
|
if($addrisfirst == 1) $aliasesaddr .= ",";
|
376
|
$aliasesaddr .= "'" . $alias_name['name'] . "'";
|
377
|
$addrisfirst = 1;
|
378
|
}
|
379
|
}
|
380
|
?>
|
381
|
<script language="JavaScript">
|
382
|
<!--
|
383
|
var addressarray=new Array(<?php echo $aliasesaddr; ?>);
|
384
|
var customarray=new Array(<?php echo $aliases; ?>);
|
385
|
//-->
|
386
|
</script>
|
387
|
<script type="text/javascript" language="javascript" src="auto_complete_helper.js">
|
388
|
</script>
|
389
|
<?php include("fend.inc"); ?>
|
390
|
</body>
|
391
|
</html>
|