Project

General

Profile

Download (19 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
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($_GET['dup'])) {
45
        $id = $_GET['dup'];
46
        $after = $_GET['dup'];
47
}
48

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

    
64
if (isset($_GET['dup']))
65
	unset($id);
66

    
67
/*  run through $_POST items encoding HTML entties so that the user
68
 *  cannot think he is slick and perform a XSS attack on the unwilling 
69
 */
70
foreach ($_POST as $key => $value) {
71
	$temp = $value;
72
	$newpost = htmlentities($temp);
73
	if($newpost <> $temp) 
74
		$input_errors[] = "Invalid characters detected ($temp).  Please remove invalid characters and save again.";		
75
}
76

    
77
if ($_POST) {
78

    
79
	if ($_POST['beginport_cust'] && !$_POST['beginport'])
80
		$_POST['beginport'] = $_POST['beginport_cust'];
81
	if ($_POST['endport_cust'] && !$_POST['endport'])
82
		$_POST['endport'] = $_POST['endport_cust'];
83
	if ($_POST['localbeginport_cust'] && !$_POST['localbeginport'])
84
		$_POST['localbeginport'] = $_POST['localbeginport_cust'];
85

    
86
	if (!$_POST['endport'])
87
		$_POST['endport'] = $_POST['beginport'];
88
        /* Make beginning port end port if not defined and endport is */
89
        if (!$_POST['beginport'] && $_POST['endport'])
90
                $_POST['beginport'] = $_POST['endport'];
91

    
92
	unset($input_errors);
93
	$pconfig = $_POST;
94

    
95
	/* input validation */
96
	if(strtoupper($_POST['proto']) == "TCP" or strtoupper($_POST['proto']) == "UDP" or strtoupper($_POST['proto']) == "TCP/UDP") {
97
		$reqdfields = explode(" ", "interface proto beginport endport localip localbeginport");
98
		$reqdfieldsn = explode(",", "Interface,Protocol,External port from,External port to,NAT IP,Local port");
99
	} else {
100
		$reqdfields = explode(" ", "interface proto localip");
101
		$reqdfieldsn = explode(",", "Interface,Protocol,NAT IP");
102
	}
103

    
104
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
105

    
106
	if (($_POST['localip'] && !is_ipaddroralias($_POST['localip']))) {
107
		$input_errors[] = "\"{$_POST['localip']}\" is not valid NAT IP address or host alias.";
108
	}
109

    
110
	/* only validate the ports if the protocol is TCP, UDP or TCP/UDP */
111
	if(strtoupper($_POST['proto']) == "TCP" or strtoupper($_POST['proto']) == "UDP" or strtoupper($_POST['proto']) == "TCP/UDP") {
112

    
113
		if (($_POST['beginport'] && !is_ipaddroralias($_POST['beginport']) && !is_port($_POST['beginport']))) {
114
			$input_errors[] = "The start port must be an integer between 1 and 65535.";
115
		}
116

    
117
		if (($_POST['endport'] && !is_ipaddroralias($_POST['endport']) && !is_port($_POST['endport']))) {
118
			$input_errors[] = "The end port must be an integer between 1 and 65535.";
119
		}
120

    
121
		if (($_POST['localbeginport'] && !is_ipaddroralias($_POST['localbeginport']) && !is_port($_POST['localbeginport']))) {
122
			$input_errors[] = "The local port must be an integer between 1 and 65535.";
123
		}
124

    
125
		if ($_POST['beginport'] > $_POST['endport']) {
126
			/* swap */
127
			$tmp = $_POST['endport'];
128
			$_POST['endport'] = $_POST['beginport'];
129
			$_POST['beginport'] = $tmp;
130
		}
131

    
132
		if (!$input_errors) {
133
			if (($_POST['endport'] - $_POST['beginport'] + $_POST['localbeginport']) > 65535)
134
				$input_errors[] = "The target port range must be an integer between 1 and 65535.";
135
		}
136

    
137
	}
138

    
139
	/* check for overlaps */
140
	foreach ($a_nat as $natent) {
141
		if (isset($id) && ($a_nat[$id]) && ($a_nat[$id] === $natent))
142
			continue;
143
		if ($natent['interface'] != $_POST['interface'])
144
			continue;
145
		if ($natent['external-address'] != $_POST['extaddr'])
146
			continue;
147
		if (($natent['proto'] != $_POST['proto']) && ($natent['proto'] != "tcp/udp") && ($_POST['proto'] != "tcp/udp"))
148
			continue;
149

    
150
		list($begp,$endp) = explode("-", $natent['external-port']);
151
		if (!$endp)
152
			$endp = $begp;
153

    
154
		if (!(   (($_POST['beginport'] < $begp) && ($_POST['endport'] < $begp))
155
		      || (($_POST['beginport'] > $endp) && ($_POST['endport'] > $endp)))) {
156

    
157
			$input_errors[] = "The external port range overlaps with an existing entry.";
158
			break;
159
		}
160
	}
161

    
162
	if (!$input_errors) {
163
		$natent = array();
164
		if ($_POST['extaddr'])
165
			$natent['external-address'] = $_POST['extaddr'];
166
		$natent['protocol'] = $_POST['proto'];
167

    
168
		if ($_POST['beginport'] == $_POST['endport'])
169
			$natent['external-port'] = $_POST['beginport'];
170
		else
171
			$natent['external-port'] = $_POST['beginport'] . "-" . $_POST['endport'];
172

    
173
		$natent['target'] = $_POST['localip'];
174
		$natent['local-port'] = $_POST['localbeginport'];
175
		$natent['interface'] = $_POST['interface'];
176
		$natent['descr'] = $_POST['descr'];
177

    
178
		if($_POST['nosync'] == "yes")
179
			$natent['nosync'] = true;
180
		else
181
			unset($natent['nosync']);
182

    
183
		if (isset($id) && $a_nat[$id])
184
			$a_nat[$id] = $natent;
185
		else {
186
			if (is_numeric($after))
187
				array_splice($a_nat, $after+1, 0, array($natent));
188
			else
189
				$a_nat[] = $natent;
190
		}
191

    
192
		touch($d_natconfdirty_path);
193

    
194
		if ($_POST['autoadd']) {
195
			/* auto-generate a matching firewall rule */
196
			$filterent = array();
197
			$filterent['interface'] = $_POST['interface'];
198
			$filterent['protocol'] = $_POST['proto'];
199
			$filterent['source']['any'] = "";
200
			$filterent['destination']['address'] = $_POST['localip'];
201

    
202
			$dstpfrom = $_POST['localbeginport'];
203
			$dstpto = $dstpfrom + $_POST['endport'] - $_POST['beginport'];
204

    
205
			if ($dstpfrom == $dstpto)
206
				$filterent['destination']['port'] = $dstpfrom;
207
			else
208
				$filterent['destination']['port'] = $dstpfrom . "-" . $dstpto;
209

    
210
			$filterent['descr'] = "NAT " . $_POST['descr'];
211
			/*
212
			 * Our firewall filter description may be no longer than
213
			 * 63 characters, so don't let it be.
214
			 */
215
			$filterent['descr'] = substr("NAT " . $_POST['descr'], 0, 59);
216

    
217
			$config['filter']['rule'][] = $filterent;
218

    
219
			/*    auto add rule to external port 21 as well since we are using
220
			 *    pftpx to help open up ports automatically
221
			 */
222
			if($_POST['endport'] == "21") {
223
				$filterent = array();
224
				$filterent['interface'] = $_POST['interface'];
225
				$filterent['protocol'] = $_POST['proto'];
226
				$filterent['source']['any'] = "";
227

    
228
				if($_POST['extaddr'] == "") {
229
					$filterent['destination']['network'] = "wanip";
230
				} else {
231
					$filterent['destination']['address'] = $_POST['extaddr'];
232
				}
233

    
234
				$dstpfrom = $_POST['localbeginport'];
235
				$dstpto = $dstpfrom + $_POST['endport'] - $_POST['beginport'];
236

    
237
				if ($dstpfrom == $dstpto)
238
					$filterent['destination']['port'] = $dstpfrom;
239
				else
240
					$filterent['destination']['port'] = $dstpfrom . "-" . $dstpto;
241

    
242
				$filterent['descr'] = "NAT " . $_POST['descr'];
243
				/* See comment above */
244
				$filterent['descr'] = substr("NAT " . $_POST['descr'], 0, 63);
245

    
246
				$config['filter']['rule'][] = $filterent;
247

    
248
				touch($d_filterconfdirty_path);
249

    
250
				write_config();
251

    
252
				header("Location: firewall_nat.php?savemsg=The%20changes%20have%20been%20saved.%20%20Please%20note%20that%20we%20have%20added%20an%20additional%20rule%20for%20the%20FTP%20helper.");
253

    
254
				exit;
255

    
256
			}
257

    
258
			touch($d_filterconfdirty_path);
259
		}
260

    
261
		write_config();
262

    
263
		header("Location: firewall_nat.php");
264
		exit;
265
	}
266
}
267

    
268
$pgtitle = array("Firewall","NAT","Port Forward: Edit");
269
include("head.inc");
270

    
271
?>
272

    
273
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
274
<?php
275
include("fbegin.inc"); ?>
276
<?php if ($input_errors) print_input_errors($input_errors); ?>
277
            <form action="firewall_nat_edit.php" method="post" name="iform" id="iform">
278
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
279
	  	<tr>
280
                  <td width="22%" valign="top" class="vncellreq">Interface</td>
281
                  <td width="78%" class="vtable">
282
					<select name="interface" class="formselect">
283
						<?php
284
						
285
						$iflist = get_configured_interface_with_descr(false, true);
286
						foreach ($iflist as $if => $ifdesc) 
287
							if(have_ruleint_access($if)) 
288
								$interfaces[$if] = $ifdesc;
289
						
290
						if ($config['pptpd']['mode'] == "server")
291
							if(have_ruleint_access("pptp")) 
292
								$interfaces['pptp'] = "PPTP VPN";
293
						
294
						if ($config['pppoe']['mode'] == "server")
295
							if(have_ruleint_access("pppoe")) 
296
								$interfaces['pppoe'] = "PPPoE VPN";
297
						
298
						/* add ipsec interfaces */
299
						if (isset($config['ipsec']['enable']) || isset($config['ipsec']['mobileclients']['enable']))
300
							if(have_ruleint_access("enc0")) 
301
								$interfaces["enc0"] = "IPsec";						
302

    
303
						foreach ($interfaces as $iface => $ifacename): ?>
304
						<option value="<?=$iface;?>" <?php if ($iface == $pconfig['interface']) echo "selected"; ?>>
305
						<?=htmlspecialchars($ifacename);?>
306
						</option>
307
						<?php endforeach; ?>
308
					</select><br>
309
                     <span class="vexpl">Choose which interface this rule applies to.<br>
310
                     Hint: in most cases, you'll want to use WAN here.</span></td>
311
                </tr>
312
			    <tr>
313
                  <td width="22%" valign="top" class="vncellreq">External address</td>
314
                  <td width="78%" class="vtable">
315
					<select name="extaddr" class="formselect">
316
						<option value="" <?php if (!$pconfig['extaddr']) echo "selected"; ?>>Interface address</option>
317
<?php					if (is_array($config['virtualip']['vip'])):
318
						foreach ($config['virtualip']['vip'] as $sn): ?>
319
						<option value="<?=$sn['subnet'];?>" <?php if ($sn['subnet'] == $pconfig['extaddr']) echo "selected"; ?>><?=htmlspecialchars("{$sn['subnet']} ({$sn['descr']})");?></option>
320
<?php					endforeach;
321
						endif; ?>
322
						<option value="any" <?php if($pconfig['extaddr'] == "any") echo "selected"; ?>>any</option>
323
					</select>
324
					<br />
325
                    <span class="vexpl">
326
					If you want this rule to apply to another IP address than the IP address of the interface chosen above,
327
					select it here (you need to define <a href="firewall_virtual_ip.php">Virtual IP</a> addresses on the first).  Also note that if you are trying to redirect connections on the LAN select the "any" option.</span></td>
328
                </tr>
329
                <tr>
330
                  <td width="22%" valign="top" class="vncellreq">Protocol</td>
331
                  <td width="78%" class="vtable">
332
                    <select name="proto" class="formselect" onChange="proto_change(); check_for_aliases();">
333
                      <?php $protocols = explode(" ", "TCP UDP TCP/UDP GRE ESP"); foreach ($protocols as $proto): ?>
334
                      <option value="<?=strtolower($proto);?>" <?php if (strtolower($proto) == $pconfig['proto']) echo "selected"; ?>><?=htmlspecialchars($proto);?></option>
335
                      <?php endforeach; ?>
336
                    </select> <br> <span class="vexpl">Choose which IP protocol
337
                    this rule should match.<br>
338
                    Hint: in most cases, you should specify <em>TCP</em> &nbsp;here.</span></td>
339
                </tr>
340
                <tr>
341
                  <td width="22%" valign="top" class="vncellreq">External port
342
                    range </td>
343
                  <td width="78%" class="vtable">
344
                    <table border="0" cellspacing="0" cellpadding="0">
345
                      <tr>
346
                        <td>from:&nbsp;&nbsp;</td>
347
                        <td><select name="beginport" class="formselect" onChange="ext_rep_change(); ext_change(); check_for_aliases();">
348
                            <option value="">(other)</option>
349
                            <?php $bfound = 0; foreach ($wkports as $wkport => $wkportdesc): ?>
350
                            <option value="<?=$wkport;?>" <?php if ($wkport == $pconfig['beginport']) {
351
								echo "selected";
352
								$bfound = 1;
353
							}?>>
354
							<?=htmlspecialchars($wkportdesc);?>
355
							</option>
356
                            <?php endforeach; ?>
357
                          </select> <input onChange="check_for_aliases();" autocomplete='off' class="formfldalias" name="beginport_cust" id="beginport_cust" type="text" size="5" value="<?php if (!$bfound) echo $pconfig['beginport']; ?>"></td>
358
                      </tr>
359
                      <tr>
360
                        <td>to:</td>
361
                        <td><select name="endport" class="formselect" onChange="ext_change(); check_for_aliases();">
362
                            <option value="">(other)</option>
363
                            <?php $bfound = 0; foreach ($wkports as $wkport => $wkportdesc): ?>
364
                            <option value="<?=$wkport;?>" <?php if ($wkport == $pconfig['endport']) {
365
								echo "selected";
366
								$bfound = 1;
367
							}?>>
368
							<?=htmlspecialchars($wkportdesc);?>
369
							</option>
370
							<?php endforeach; ?>
371
                          </select> <input onChange="check_for_aliases();" class="formfldalias" autocomplete='off' name="endport_cust" id="endport_cust" type="text" size="5" value="<?php if (!$bfound) echo $pconfig['endport']; ?>"></td>
372
                      </tr>
373
                    </table>
374
                    <br> <span class="vexpl">Specify the port or port range on
375
                    the firewall's external address for this mapping.<br>
376
                    Hint: you can leave the <em>'to'</em> field empty if you only
377
                    want to map a single port</span></td>
378
                </tr>
379
                <tr>
380
                  <td width="22%" valign="top" class="vncellreq">NAT IP</td>
381
                  <td width="78%" class="vtable">
382
                    <input autocomplete='off' name="localip" type="text" class="formfldalias" id="localip" size="20" value="<?=htmlspecialchars($pconfig['localip']);?>">
383
                    <br> <span class="vexpl">Enter the internal IP address of
384
                    the server on which you want to map the ports.<br>
385
                    e.g. <em>192.168.1.12</em></span></td>
386
                </tr>
387
                <tr>
388
                  <td width="22%" valign="top" class="vncellreq">Local port</td>
389
                  <td width="78%" class="vtable">
390
                    <select name="localbeginport" class="formselect" onChange="ext_change();check_for_aliases();">
391
                      <option value="">(other)</option>
392
                      <?php $bfound = 0; foreach ($wkports as $wkport => $wkportdesc): ?>
393
                      <option value="<?=$wkport;?>" <?php if ($wkport == $pconfig['localbeginport']) {
394
							echo "selected";
395
							$bfound = 1;
396
						}?>>
397
					  <?=htmlspecialchars($wkportdesc);?>
398
					  </option>
399
                      <?php endforeach; ?>
400
                    </select> <input onChange="check_for_aliases();" autocomplete='off' class="formfldalias" name="localbeginport_cust" id="localbeginport_cust" type="text" size="5" value="<?php if (!$bfound) echo $pconfig['localbeginport']; ?>">
401
                    <br>
402
                    <span class="vexpl">Specify the port on the machine with the
403
                    IP address entered above. In case of a port range, specify
404
                    the beginning port of the range (the end port will be calculated
405
                    automatically).<br>
406
                    Hint: this is usually identical to the 'from' port above</span></td>
407
                </tr>
408
                <tr>
409
                  <td width="22%" valign="top" class="vncell">Description</td>
410
                  <td width="78%" class="vtable">
411
                    <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
412
                    <br> <span class="vexpl">You may enter a description here
413
                    for your reference (not parsed).</span></td>
414
                </tr>
415
				<tr>
416
					<td width="22%" valign="top" class="vncell">No XMLRPC Sync</td>
417
					<td width="78%" class="vtable">
418
						<input type="checkbox" value="yes" name="nosync"<?php if($pconfig['nosync']) echo " CHECKED"; ?>><br>
419
						HINT: This prevents the rule from automatically syncing to other CARP members.
420
					</td>
421
				</tr>
422
                <?php if ((!(isset($id) && $a_nat[$id])) || (isset($_GET['dup']))): ?>
423
                <tr>
424
                  <td width="22%" valign="top">&nbsp;</td>
425
                  <td width="78%">
426
                    <input name="autoadd" type="checkbox" id="autoadd" value="yes" CHECKED>
427
                    <strong>Auto-add a firewall rule to permit traffic through
428
                    this NAT rule</strong></td>
429
                </tr><?php endif; ?>
430
                <tr>
431
                  <td width="22%" valign="top">&nbsp;</td>
432
                  <td width="78%">
433
                    <input name="Submit" type="submit" class="formbtn" value="Save"> <input type="button" class="formbtn" value="Cancel" onclick="history.back()">
434
                    <?php if (isset($id) && $a_nat[$id]): ?>
435
                    <input name="id" type="hidden" value="<?=$id;?>">
436
                    <?php endif; ?>
437
                  </td>
438
                </tr>
439
              </table>
440
</form>
441
<script language="JavaScript">
442
<!--
443
	ext_change();
444
//-->
445
</script>
446
<?php
447
$isfirst = 0;
448
$aliases = "";
449
$addrisfirst = 0;
450
$aliasesaddr = "";
451
if($config['aliases']['alias'] <> "")
452
	foreach($config['aliases']['alias'] as $alias_name) {
453
		if(!stristr($alias_name['address'], ".")) {
454
			if($isfirst == 1) $aliases .= ",";
455
			$aliases .= "'" . $alias_name['name'] . "'";
456
			$isfirst = 1;
457
		} else {
458
			if($addrisfirst == 1) $aliasesaddr .= ",";
459
			$aliasesaddr .= "'" . $alias_name['name'] . "'";
460
			$addrisfirst = 1;
461
		}
462
	}
463
?>
464
<script language="JavaScript">
465
<!--
466
	var addressarray=new Array(<?php echo $aliasesaddr; ?>);
467
	var customarray=new Array(<?php echo $aliases; ?>);
468
//-->
469
</script>
470
<?php include("fend.inc"); ?>
471
</body>
472
</html>
(46-46/204)