Project

General

Profile

Download (15.9 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/* $Id$ */
4
/*
5
	firewall_nat_edit.php
6
	part of m0n0wall (http://m0n0.ch/wall)
7

    
8
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10

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

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

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

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

    
33
require("guiconfig.inc");
34

    
35
if (!is_array($config['nat']['rule'])) {
36
	$config['nat']['rule'] = array();
37
}
38
//nat_rules_sort();
39
$a_nat = &$config['nat']['rule'];
40

    
41
$id = $_GET['id'];
42
if (isset($_POST['id']))
43
	$id = $_POST['id'];
44

    
45
if (isset($id) && $a_nat[$id]) {
46
	$pconfig['extaddr'] = $a_nat[$id]['external-address'];
47
	$pconfig['proto'] = $a_nat[$id]['protocol'];
48
	list($pconfig['beginport'],$pconfig['endport']) = explode("-", $a_nat[$id]['external-port']);
49
	$pconfig['localip'] = $a_nat[$id]['target'];
50
	$pconfig['localbeginport'] = $a_nat[$id]['local-port'];
51
	$pconfig['descr'] = $a_nat[$id]['descr'];
52
	$pconfig['interface'] = $a_nat[$id]['interface'];
53
	if (!$pconfig['interface'])
54
		$pconfig['interface'] = "wan";
55
} else {
56
	$pconfig['interface'] = "wan";
57
}
58

    
59
if ($_POST) {
60

    
61
	if ($_POST['beginport_cust'] && !$_POST['beginport'])
62
		$_POST['beginport'] = $_POST['beginport_cust'];
63
	if ($_POST['endport_cust'] && !$_POST['endport'])
64
		$_POST['endport'] = $_POST['endport_cust'];
65
	if ($_POST['localbeginport_cust'] && !$_POST['localbeginport'])
66
		$_POST['localbeginport'] = $_POST['localbeginport_cust'];
67

    
68
	if (!$_POST['endport'])
69
		$_POST['endport'] = $_POST['beginport'];
70

    
71
	unset($input_errors);
72
	$pconfig = $_POST;
73

    
74
	/* input validation */
75
	$reqdfields = explode(" ", "interface proto beginport localip localbeginport");
76
	$reqdfieldsn = explode(",", "Interface,Protocol,Start port,NAT IP,Local port");
77

    
78
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
79

    
80
	if (($_POST['beginport'] && !is_port($_POST['beginport']))) {
81
		$input_errors[] = "The start port must be an integer between 1 and 65535.";
82
	}
83
	if (($_POST['endport'] && !is_port($_POST['endport']))) {
84
		$input_errors[] = "The end port must be an integer between 1 and 65535.";
85
	}
86
	if (($_POST['localbeginport'] && !is_port($_POST['localbeginport']))) {
87
		$input_errors[] = "The local port must be an integer between 1 and 65535.";
88
	}
89
	if (($_POST['localip'] && !is_ipaddroralias($_POST['localip']))) {
90
		$input_errors[] = "A valid NAT IP address or host alias must be specified.";
91
	}
92

    
93
	if ($_POST['beginport'] > $_POST['endport']) {
94
		/* swap */
95
		$tmp = $_POST['endport'];
96
		$_POST['endport'] = $_POST['beginport'];
97
		$_POST['beginport'] = $tmp;
98
	}
99

    
100
	if (!$input_errors) {
101
		if (($_POST['endport'] - $_POST['beginport'] + $_POST['localbeginport']) > 65535)
102
			$input_errors[] = "The target port range must lie between 1 and 65535.";
103
	}
104

    
105
	/* check for overlaps */
106
	foreach ($a_nat as $natent) {
107
		if (isset($id) && ($a_nat[$id]) && ($a_nat[$id] === $natent))
108
			continue;
109
		if ($natent['interface'] != $_POST['interface'])
110
			continue;
111
		if ($natent['external-address'] != $_POST['extaddr'])
112
			continue;
113

    
114
		list($begp,$endp) = explode("-", $natent['external-port']);
115
		if (!$endp)
116
			$endp = $begp;
117

    
118
		if (!(   (($_POST['beginport'] < $begp) && ($_POST['endport'] < $begp))
119
		      || (($_POST['beginport'] > $endp) && ($_POST['endport'] > $endp)))) {
120

    
121
			$input_errors[] = "The external port range overlaps with an existing entry.";
122
			break;
123
		}
124
	}
125

    
126
	if (!$input_errors) {
127
		$natent = array();
128
		if ($_POST['extaddr'])
129
			$natent['external-address'] = $_POST['extaddr'];
130
		$natent['protocol'] = $_POST['proto'];
131

    
132
		if ($_POST['beginport'] == $_POST['endport'])
133
			$natent['external-port'] = $_POST['beginport'];
134
		else
135
			$natent['external-port'] = $_POST['beginport'] . "-" . $_POST['endport'];
136

    
137
		$natent['target'] = $_POST['localip'];
138
		$natent['local-port'] = $_POST['localbeginport'];
139
		$natent['interface'] = $_POST['interface'];
140
		$natent['descr'] = $_POST['descr'];
141

    
142
		if (isset($id) && $a_nat[$id])
143
			$a_nat[$id] = $natent;
144
		else
145
			$a_nat[] = $natent;
146

    
147
		touch($d_natconfdirty_path);
148

    
149
		if ($_POST['autoadd']) {
150
			/* auto-generate a matching firewall rule */
151
			$filterent = array();
152
			$filterent['interface'] = $_POST['interface'];
153
			$filterent['protocol'] = $_POST['proto'];
154
			$filterent['source']['any'] = "";
155
			$filterent['destination']['address'] = $_POST['localip'];
156

    
157
			$dstpfrom = $_POST['localbeginport'];
158
			$dstpto = $dstpfrom + $_POST['endport'] - $_POST['beginport'];
159

    
160
			if ($dstpfrom == $dstpto)
161
				$filterent['destination']['port'] = $dstpfrom;
162
			else
163
				$filterent['destination']['port'] = $dstpfrom . "-" . $dstpto;
164

    
165
			$filterent['descr'] = "NAT " . $_POST['descr'];
166

    
167
			$config['filter']['rule'][] = $filterent;
168

    
169
			touch($d_filterconfdirty_path);
170
		}
171

    
172
		write_config();
173

    
174
		header("Location: firewall_nat.php");
175
		exit;
176
	}
177
}
178
?>
179
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
180
<html>
181
<head>
182
<title><?=gentitle("Firewall: NAT: Edit inbound");?></title>
183
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
184
<link href="gui.css" rel="stylesheet" type="text/css">
185
<script language="JavaScript">
186
<!--
187
function ext_change() {
188
	if (document.iform.beginport.selectedIndex == 0) {
189
		document.iform.beginport_cust.disabled = 0;
190
	} else {
191
		document.iform.beginport_cust.value = "";
192
		document.iform.beginport_cust.disabled = 1;
193
	}
194
	if (document.iform.endport.selectedIndex == 0) {
195
		document.iform.endport_cust.disabled = 0;
196
	} else {
197
		document.iform.endport_cust.value = "";
198
		document.iform.endport_cust.disabled = 1;
199
	}
200
	if (document.iform.localbeginport.selectedIndex == 0) {
201
		document.iform.localbeginport_cust.disabled = 0;
202
	} else {
203
		document.iform.localbeginport_cust.value = "";
204
		document.iform.localbeginport_cust.disabled = 1;
205
	}
206
}
207
function ext_rep_change() {
208
	document.iform.endport.selectedIndex = document.iform.beginport.selectedIndex;
209
	document.iform.localbeginport.selectedIndex = document.iform.beginport.selectedIndex;
210
}
211
//-->
212
</script>
213
</head>
214

    
215
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
216
<?php include("fbegin.inc"); ?>
217
<p class="pgtitle">Firewall: NAT: Edit inbound</p>
218
<?php if ($input_errors) print_input_errors($input_errors); ?>
219
            <form action="firewall_nat_edit.php" method="post" name="iform" id="iform">
220
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
221
			  	<tr>
222
                  <td width="22%" valign="top" class="vncellreq">Interface</td>
223
                  <td width="78%" class="vtable">
224
					<select name="interface" class="formfld">
225
						<?php
226
						$interfaces = array('wan' => 'WAN', 'lan' => 'LAN');
227
						for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
228
							$interfaces['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];
229
						}
230
						foreach ($interfaces as $iface => $ifacename): ?>
231
						<option value="<?=$iface;?>" <?php if ($iface == $pconfig['interface']) echo "selected"; ?>>
232
						<?=htmlspecialchars($ifacename);?>
233
						</option>
234
						<?php endforeach; ?>
235
					</select><br>
236
                     <span class="vexpl">Choose which interface this rule applies to.<br>
237
                     Hint: in most cases, you'll want to use WAN here.</span></td>
238
                </tr>
239
			    <tr>
240
                  <td width="22%" valign="top" class="vncellreq">External address</td>
241
                  <td width="78%" class="vtable">
242
                    <select name="extaddr" class="formfld">
243
					  <option value="" <?php if (!$pconfig['extaddr']) echo "selected"; ?>>Interface address</option>
244
                      <?php
245
					  if (is_array($config['nat']['servernat'])):
246
						  foreach ($config['nat']['servernat'] as $sn): ?>
247
                      <option value="<?=$sn['ipaddr'];?>" <?php if ($sn['ipaddr'] == $pconfig['extaddr']) echo "selected"; ?>><?=htmlspecialchars("{$sn['ipaddr']} ({$sn['descr']})");?></option>
248
                      <?php endforeach; endif; ?>
249
                    </select><br>
250
                    <span class="vexpl">
251
					If you want this rule to apply to another IP address than the IP address of the interface chosen above,
252
					select it here (you need to define IP addresses on the
253
					<a href="firewall_nat_server.php">Server NAT</a> page first).</span></td>
254
                </tr>
255
                <tr>
256
                  <td width="22%" valign="top" class="vncellreq">Protocol</td>
257
                  <td width="78%" class="vtable">
258
                    <select name="proto" class="formfld">
259
                      <?php $protocols = explode(" ", "TCP UDP TCP/UDP"); foreach ($protocols as $proto): ?>
260
                      <option value="<?=strtolower($proto);?>" <?php if (strtolower($proto) == $pconfig['proto']) echo "selected"; ?>><?=htmlspecialchars($proto);?></option>
261
                      <?php endforeach; ?>
262
                    </select> <br> <span class="vexpl">Choose which IP protocol
263
                    this rule should match.<br>
264
                    Hint: in most cases, you should specify <em>TCP</em> &nbsp;here.</span></td>
265
                </tr>
266
                <tr>
267
                  <td width="22%" valign="top" class="vncellreq">External port
268
                    range </td>
269
                  <td width="78%" class="vtable">
270
                    <table border="0" cellspacing="0" cellpadding="0">
271
                      <tr>
272
                        <td>from:&nbsp;&nbsp;</td>
273
                        <td><select name="beginport" class="formfld" onChange="ext_rep_change();ext_change()">
274
                            <option value="">(other)</option>
275
                            <?php $bfound = 0; foreach ($wkports as $wkport => $wkportdesc): ?>
276
                            <option value="<?=$wkport;?>" <?php if ($wkport == $pconfig['beginport']) {
277
																echo "selected";
278
																$bfound = 1;
279
															}?>>
280
							<?=htmlspecialchars($wkportdesc);?>
281
							</option>
282
                            <?php endforeach; ?>
283
                          </select> <input name="beginport_cust" type="text" size="5" value="<?php if (!$bfound) echo $pconfig['beginport']; ?>"></td>
284
                      </tr>
285
                      <tr>
286
                        <td>to:</td>
287
                        <td><select name="endport" class="formfld" onChange="ext_change()">
288
                            <option value="">(other)</option>
289
                            <?php $bfound = 0; foreach ($wkports as $wkport => $wkportdesc): ?>
290
                            <option value="<?=$wkport;?>" <?php if ($wkport == $pconfig['endport']) {
291
																echo "selected";
292
																$bfound = 1;
293
															}?>>
294
							<?=htmlspecialchars($wkportdesc);?>
295
							</option>
296
							<?php endforeach; ?>
297
                          </select> <input name="endport_cust" type="text" size="5" value="<?php if (!$bfound) echo $pconfig['endport']; ?>"></td>
298
                      </tr>
299
                    </table>
300
                    <br> <span class="vexpl">Specify the port or port range on
301
                    the firewall's external address for this mapping.<br>
302
                    Hint: you can leave the <em>'to'</em> field empty if you only
303
                    want to map a single port</span></td>
304
                </tr>
305
                <tr>
306
                  <td width="22%" valign="top" class="vncellreq">NAT IP</td>
307
                  <td width="78%" class="vtable">
308
                    <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']);?>">
309
                    <br> <span class="vexpl">Enter the internal IP address of
310
                    the server on which you want to map the ports.<br>
311
                    e.g. <em>192.168.1.12</em></span></td>
312
                </tr>
313
                <tr>
314
                  <td width="22%" valign="top" class="vncellreq">Local port</td>
315
                  <td width="78%" class="vtable">
316
                    <select name="localbeginport" class="formfld" onChange="ext_change()">
317
                      <option value="">(other)</option>
318
                      <?php $bfound = 0; foreach ($wkports as $wkport => $wkportdesc): ?>
319
                      <option value="<?=$wkport;?>" <?php if ($wkport == $pconfig['localbeginport']) {
320
																echo "selected";
321
																$bfound = 1;
322
															}?>>
323
					  <?=htmlspecialchars($wkportdesc);?>
324
					  </option>
325
                      <?php endforeach; ?>
326
                    </select> <input name="localbeginport_cust" type="text" size="5" value="<?php if (!$bfound) echo $pconfig['localbeginport']; ?>">
327
                    <br>
328
                    <span class="vexpl">Specify the port on the machine with the
329
                    IP address entered above. In case of a port range, specify
330
                    the beginning port of the range (the end port will be calculated
331
                    automatically).<br>
332
                    Hint: this is usually identical to the 'from' port above</span></td>
333
                </tr>
334
                <tr>
335
                  <td width="22%" valign="top" class="vncell">Description</td>
336
                  <td width="78%" class="vtable">
337
                    <input name="descr" type="text" class="formfld" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
338
                    <br> <span class="vexpl">You may enter a description here
339
                    for your reference (not parsed).</span></td>
340
                </tr><?php if (!(isset($id) && $a_nat[$id])): ?>
341
                <tr>
342
                  <td width="22%" valign="top">&nbsp;</td>
343
                  <td width="78%">
344
                    <input name="autoadd" type="checkbox" id="autoadd" value="yes">
345
                    <strong>Auto-add a firewall rule to permit traffic through
346
                    this NAT rule</strong></td>
347
                </tr><?php endif; ?>
348
                <tr>
349
                  <td width="22%" valign="top">&nbsp;</td>
350
                  <td width="78%">
351
                    <input name="Submit" type="submit" class="formbtn" value="Save">
352
                    <?php if (isset($id) && $a_nat[$id]): ?>
353
                    <input name="id" type="hidden" value="<?=$id;?>">
354
                    <?php endif; ?>
355
                  </td>
356
                </tr>
357
              </table>
358
</form>
359
<script language="JavaScript">
360
<!--
361
ext_change();
362
//-->
363
</script>
364
<?php
365
$isfirst = 0;
366
$aliases = "";
367
$addrisfirst = 0;
368
$aliasesaddr = "";
369
if($config['aliases']['alias'] <> "")
370
	foreach($config['aliases']['alias'] as $alias_name) {
371
		if(!stristr($alias_name['address'], ".")) {
372
			if($isfirst == 1) $aliases .= ",";
373
			$aliases .= "'" . $alias_name['name'] . "'";
374
			$isfirst = 1;
375
		} else {
376
			if($addrisfirst == 1) $aliasesaddr .= ",";
377
			$aliasesaddr .= "'" . $alias_name['name'] . "'";
378
			$addrisfirst = 1;
379
		}
380
	}
381
?>
382
<script language="JavaScript">
383
<!--
384
var addressarray=new Array(<?php echo $aliasesaddr; ?>);
385
var customarray=new Array(<?php echo $aliases; ?>);
386
//-->
387
</script>
388
<script type="text/javascript" language="javascript" src="auto_complete_helper.js">
389
</script>
390
<?php include("fend.inc"); ?>
391
</body>
392
</html>
(27-27/109)