Project

General

Profile

Download (18.1 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
##|+PRIV
33
##|*IDENT=page-firewall-nat-portforward-edit
34
##|*NAME=Firewall: NAT: Port Forward: Edit page
35
##|*DESCR=Allow access to the 'Firewall: NAT: Port Forward: Edit' page.
36
##|*MATCH=firewall_nat_edit.php*
37
##|-PRIV
38

    
39

    
40
require("guiconfig.inc");
41

    
42
if (!is_array($config['nat']['rule'])) {
43
	$config['nat']['rule'] = array();
44
}
45
$a_nat = &$config['nat']['rule'];
46

    
47
$id = $_GET['id'];
48
if (isset($_POST['id']))
49
	$id = $_POST['id'];
50

    
51
if (isset($_GET['dup'])) {
52
        $id = $_GET['dup'];
53
        $after = $_GET['dup'];
54
}
55

    
56
if (isset($id) && $a_nat[$id]) {
57
	$pconfig['extaddr'] = $a_nat[$id]['external-address'];
58
	$pconfig['proto'] = $a_nat[$id]['protocol'];
59
	list($pconfig['beginport'],$pconfig['endport']) = explode("-", $a_nat[$id]['external-port']);
60
	$pconfig['localip'] = $a_nat[$id]['target'];
61
	$pconfig['localbeginport'] = $a_nat[$id]['local-port'];
62
	$pconfig['descr'] = $a_nat[$id]['descr'];
63
	$pconfig['interface'] = $a_nat[$id]['interface'];
64
	$pconfig['nosync'] = isset($a_nat[$id]['nosync']);
65
	if (!$pconfig['interface'])
66
		$pconfig['interface'] = "wan";
67
} else {
68
	$pconfig['interface'] = "wan";
69
}
70

    
71
if (isset($_GET['dup']))
72
	unset($id);
73

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

    
84
if ($_POST) {
85

    
86
	if ($_POST['beginport_cust'] && !$_POST['beginport'])
87
		$_POST['beginport'] = $_POST['beginport_cust'];
88
	if ($_POST['endport_cust'] && !$_POST['endport'])
89
		$_POST['endport'] = $_POST['endport_cust'];
90
	if ($_POST['localbeginport_cust'] && !$_POST['localbeginport'])
91
		$_POST['localbeginport'] = $_POST['localbeginport_cust'];
92

    
93
	if (!$_POST['endport'])
94
		$_POST['endport'] = $_POST['beginport'];
95
        /* Make beginning port end port if not defined and endport is */
96
        if (!$_POST['beginport'] && $_POST['endport'])
97
                $_POST['beginport'] = $_POST['endport'];
98

    
99
	unset($input_errors);
100
	$pconfig = $_POST;
101

    
102
	/* input validation */
103
	if(strtoupper($_POST['proto']) == "TCP" or strtoupper($_POST['proto']) == "UDP" or strtoupper($_POST['proto']) == "TCP/UDP") {
104
		$reqdfields = explode(" ", "interface proto beginport endport localip localbeginport");
105
		$reqdfieldsn = explode(",", "Interface,Protocol,External port from,External port to,NAT IP,Local port");
106
	} else {
107
		$reqdfields = explode(" ", "interface proto localip");
108
		$reqdfieldsn = explode(",", "Interface,Protocol,NAT IP");
109
	}
110

    
111
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
112

    
113
	if (($_POST['localip'] && !is_ipaddroralias($_POST['localip']))) {
114
		$input_errors[] = "\"{$_POST['localip']}\" is not valid NAT IP address or host alias.";
115
	}
116

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

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

    
124
		if (($_POST['endport'] && !is_ipaddroralias($_POST['endport']) && !is_port($_POST['endport']))) {
125
			$input_errors[] = "The end port must be an integer between 1 and 65535.";
126
		}
127

    
128
		if (($_POST['localbeginport'] && !is_ipaddroralias($_POST['localbeginport']) && !is_port($_POST['localbeginport']))) {
129
			$input_errors[] = "The local port must be an integer between 1 and 65535.";
130
		}
131

    
132
		if ($_POST['beginport'] > $_POST['endport']) {
133
			/* swap */
134
			$tmp = $_POST['endport'];
135
			$_POST['endport'] = $_POST['beginport'];
136
			$_POST['beginport'] = $tmp;
137
		}
138

    
139
		if (!$input_errors) {
140
			if (($_POST['endport'] - $_POST['beginport'] + $_POST['localbeginport']) > 65535)
141
				$input_errors[] = "The target port range must be an integer between 1 and 65535.";
142
		}
143

    
144
	}
145

    
146
	/* check for overlaps */
147
	foreach ($a_nat as $natent) {
148
		if (isset($id) && ($a_nat[$id]) && ($a_nat[$id] === $natent))
149
			continue;
150
		if ($natent['interface'] != $_POST['interface'])
151
			continue;
152
		if ($natent['external-address'] != $_POST['extaddr'])
153
			continue;
154
		if (($natent['proto'] != $_POST['proto']) && ($natent['proto'] != "tcp/udp") && ($_POST['proto'] != "tcp/udp"))
155
			continue;
156

    
157
		list($begp,$endp) = explode("-", $natent['external-port']);
158
		if (!$endp)
159
			$endp = $begp;
160

    
161
		if (!(   (($_POST['beginport'] < $begp) && ($_POST['endport'] < $begp))
162
		      || (($_POST['beginport'] > $endp) && ($_POST['endport'] > $endp)))) {
163

    
164
			$input_errors[] = "The external port range overlaps with an existing entry.";
165
			break;
166
		}
167
	}
168

    
169
	if (!$input_errors) {
170
		$natent = array();
171
		if ($_POST['extaddr'])
172
			$natent['external-address'] = $_POST['extaddr'];
173
		$natent['protocol'] = $_POST['proto'];
174

    
175
		if ($_POST['beginport'] == $_POST['endport'])
176
			$natent['external-port'] = $_POST['beginport'];
177
		else
178
			$natent['external-port'] = $_POST['beginport'] . "-" . $_POST['endport'];
179

    
180
		$natent['target'] = $_POST['localip'];
181
		$natent['local-port'] = $_POST['localbeginport'];
182
		$natent['interface'] = $_POST['interface'];
183
		$natent['descr'] = $_POST['descr'];
184

    
185
		if($_POST['nosync'] == "yes")
186
			$natent['nosync'] = true;
187
		else
188
			unset($natent['nosync']);
189

    
190
		if (isset($id) && $a_nat[$id])
191
			$a_nat[$id] = $natent;
192
		else {
193
			if (is_numeric($after))
194
				array_splice($a_nat, $after+1, 0, array($natent));
195
			else
196
				$a_nat[] = $natent;
197
		}
198

    
199
		mark_subsystem_dirty('natconf');
200

    
201
		if ($_POST['autoadd']) {
202
			/* auto-generate a matching firewall rule */
203
			$filterent = array();
204
			$filterent['interface'] = $_POST['interface'];
205
			$filterent['protocol'] = $_POST['proto'];
206
			$filterent['source']['any'] = "";
207
			$filterent['destination']['address'] = $_POST['localip'];
208

    
209
			$dstpfrom = $_POST['localbeginport'];
210
			$dstpto = $dstpfrom + $_POST['endport'] - $_POST['beginport'];
211

    
212
			if ($dstpfrom == $dstpto)
213
				$filterent['destination']['port'] = $dstpfrom;
214
			else
215
				$filterent['destination']['port'] = $dstpfrom . "-" . $dstpto;
216

    
217
			$filterent['descr'] = "NAT " . $_POST['descr'];
218
			/*
219
			 * Our firewall filter description may be no longer than
220
			 * 63 characters, so don't let it be.
221
			 */
222
			$filterent['descr'] = substr("NAT " . $_POST['descr'], 0, 59);
223

    
224
			$config['filter']['rule'][] = $filterent;
225

    
226
			mark_subsystem_dirty('filter');
227
		}
228

    
229
		write_config();
230

    
231
		header("Location: firewall_nat.php");
232
		exit;
233
	}
234
}
235

    
236
$pgtitle = array("Firewall","NAT","Port Forward: Edit");
237
include("head.inc");
238

    
239
?>
240

    
241
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
242
<?php
243
include("fbegin.inc"); ?>
244
<?php if ($input_errors) print_input_errors($input_errors); ?>
245
            <form action="firewall_nat_edit.php" method="post" name="iform" id="iform">
246
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
247
				<tr>
248
					<td colspan="2" valign="top" class="listtopic">Edit NAT entry</td>
249
				</tr>	
250
				<tr>
251
                  <td width="22%" valign="top" class="vncellreq">Interface</td>
252
                  <td width="78%" class="vtable">
253
					<select name="interface" class="formselect">
254
						<?php
255
						
256
						$iflist = get_configured_interface_with_descr(false, true);
257
						foreach ($iflist as $if => $ifdesc) 
258
							if(have_ruleint_access($if)) 
259
								$interfaces[$if] = $ifdesc;
260
						
261
						if ($config['pptpd']['mode'] == "server")
262
							if(have_ruleint_access("pptp")) 
263
								$interfaces['pptp'] = "PPTP VPN";
264
						
265
						if ($config['pppoe']['mode'] == "server")
266
							if(have_ruleint_access("pppoe")) 
267
								$interfaces['pppoe'] = "PPPoE VPN";
268
						
269
						/* add ipsec interfaces */
270
						if (isset($config['ipsec']['enable']) || isset($config['ipsec']['mobileclients']['enable']))
271
							if(have_ruleint_access("enc0")) 
272
								$interfaces["enc0"] = "IPsec";						
273

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