Project

General

Profile

Download (17.3 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
	if (!$pconfig['interface'])
58
		$pconfig['interface'] = "wan";
59
} else {
60
	$pconfig['interface'] = "wan";
61
}
62

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

    
66
if ($_POST) {
67

    
68
	if ($_POST['beginport_cust'] && !$_POST['beginport'])
69
		$_POST['beginport'] = $_POST['beginport_cust'];
70
	if ($_POST['endport_cust'] && !$_POST['endport'])
71
		$_POST['endport'] = $_POST['endport_cust'];
72
	if ($_POST['localbeginport_cust'] && !$_POST['localbeginport'])
73
		$_POST['localbeginport'] = $_POST['localbeginport_cust'];
74

    
75
	if (!$_POST['endport'])
76
		$_POST['endport'] = $_POST['beginport'];
77
        /* Make beginning port end port if not defined and endport is */
78
        if (!$_POST['beginport'] && $_POST['endport'])
79
                $_POST['beginport'] = $_POST['endport'];
80

    
81
	unset($input_errors);
82
	$pconfig = $_POST;
83

    
84
	/* input validation */
85
	if(strtoupper($_POST['proto']) == "TCP" or strtoupper($_POST['proto']) == "UDP" or strtoupper($_POST['proto']) == "TCP/UDP") {
86
		$reqdfields = explode(" ", "interface proto beginport localip localbeginport");
87
		$reqdfieldsn = explode(",", "Interface,Protocol,Ext. start port,NAT IP,Local port");
88
	} else {
89
		$reqdfields = explode(" ", "interface proto localip");
90
		$reqdfieldsn = explode(",", "Interface,Protocol,NAT IP");		
91
	}
92

    
93
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
94

    
95
	if (($_POST['localip'] && !is_ipaddroralias($_POST['localip']))) {
96
		$input_errors[] = "\"{$_POST['localip']}\" is not valid NAT IP address or host alias.";
97
	}
98

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

    
102
		if (($_POST['beginport'] && !is_ipaddroralias($_POST['beginport']) && !is_port($_POST['beginport']))) {
103
			$input_errors[] = "The start port must be an integer between 1 and 65535.";
104
		}
105

    
106
		if (($_POST['endport'] && !is_ipaddroralias($_POST['endport']) && !is_port($_POST['endport']))) {
107
			$input_errors[] = "The end port must be an integer between 1 and 65535.";
108
		}
109

    
110
		if (($_POST['localbeginport'] && !is_ipaddroralias($_POST['localbeginport']) && !is_port($_POST['localbeginport']))) {
111
			$input_errors[] = "The local port must be an integer between 1 and 65535.";
112
		}
113

    
114
		if ($_POST['beginport'] > $_POST['endport']) {
115
			/* swap */
116
			$tmp = $_POST['endport'];
117
			$_POST['endport'] = $_POST['beginport'];
118
			$_POST['beginport'] = $tmp;
119
		}
120

    
121
		if (!$input_errors) {
122
			if (($_POST['endport'] - $_POST['beginport'] + $_POST['localbeginport']) > 65535)
123
				$input_errors[] = "The target port range must be an integer between 1 and 65535.";
124
		}
125
		
126
	}
127

    
128
	/* check for overlaps */
129
	foreach ($a_nat as $natent) {
130
		if (isset($id) && ($a_nat[$id]) && ($a_nat[$id] === $natent))
131
			continue;
132
		if ($natent['interface'] != $_POST['interface'])
133
			continue;
134
		if ($natent['external-address'] != $_POST['extaddr'])
135
			continue;
136
		if (($natent['proto'] != $_POST['proto']) && ($natent['proto'] != "tcp/udp") && ($_POST['proto'] != "tcp/udp"))
137
			continue;
138
		
139
		list($begp,$endp) = explode("-", $natent['external-port']);
140
		if (!$endp)
141
			$endp = $begp;
142
		
143
		if (!(   (($_POST['beginport'] < $begp) && ($_POST['endport'] < $begp))
144
		      || (($_POST['beginport'] > $endp) && ($_POST['endport'] > $endp)))) {
145
			
146
			$input_errors[] = "The external port range overlaps with an existing entry.";
147
			break;
148
		}
149
	}
150

    
151
	if (!$input_errors) {
152
		$natent = array();
153
		if ($_POST['extaddr'])
154
			$natent['external-address'] = $_POST['extaddr'];
155
		$natent['protocol'] = $_POST['proto'];
156

    
157
		if ($_POST['beginport'] == $_POST['endport'])
158
			$natent['external-port'] = $_POST['beginport'];
159
		else
160
			$natent['external-port'] = $_POST['beginport'] . "-" . $_POST['endport'];
161

    
162
		$natent['target'] = $_POST['localip'];
163
		$natent['local-port'] = $_POST['localbeginport'];
164
		$natent['interface'] = $_POST['interface'];
165
		$natent['descr'] = $_POST['descr'];
166

    
167
		if (isset($id) && $a_nat[$id])
168
			$a_nat[$id] = $natent;
169
		else {
170
			if (is_numeric($after))
171
				array_splice($a_nat, $after+1, 0, array($natent));
172
			else
173
				$a_nat[] = $natent;
174
		}
175

    
176
		touch($d_natconfdirty_path);
177

    
178
		if ($_POST['autoadd']) {
179
			/* auto-generate a matching firewall rule */
180
			$filterent = array();
181
			$filterent['interface'] = $_POST['interface'];
182
			$filterent['protocol'] = $_POST['proto'];
183
			$filterent['source']['any'] = "";
184
			$filterent['destination']['address'] = $_POST['localip'];
185

    
186
			$dstpfrom = $_POST['localbeginport'];
187
			$dstpto = $dstpfrom + $_POST['endport'] - $_POST['beginport'];
188

    
189
			if ($dstpfrom == $dstpto)
190
				$filterent['destination']['port'] = $dstpfrom;
191
			else
192
				$filterent['destination']['port'] = $dstpfrom . "-" . $dstpto;
193

    
194
			$filterent['descr'] = "NAT " . $_POST['descr'];
195

    
196
			$config['filter']['rule'][] = $filterent;
197

    
198
			/*    auto add rule to external port 21 as well since we are using
199
			 *    pftpx to help open up ports automatically
200
             */
201
			if($_POST['endport'] == "21") {
202
				$filterent = array();
203
				$filterent['interface'] = $_POST['interface'];
204
				$filterent['protocol'] = $_POST['proto'];
205
				$filterent['source']['any'] = "";
206
				
207
				if($_POST['extaddr'] == "wanip") {
208
					$filterent['destination']['network'] = "wanip";
209
				} else {
210
					$filterent['destination']['address'] = $_POST['extaddr'];
211
				}
212
	
213
				$dstpfrom = $_POST['localbeginport'];
214
				$dstpto = $dstpfrom + $_POST['endport'] - $_POST['beginport'];
215
	
216
				if ($dstpfrom == $dstpto)
217
					$filterent['destination']['port'] = $dstpfrom;
218
				else
219
					$filterent['destination']['port'] = $dstpfrom . "-" . $dstpto;
220
	
221
				$filterent['descr'] = "NAT " . $_POST['descr'];
222
	
223
				$config['filter']['rule'][] = $filterent;
224
				
225
				touch($d_filterconfdirty_path);
226
				
227
				write_config();
228

    
229
				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.");
230
				
231
				exit;
232
				
233
			}
234

    
235
			touch($d_filterconfdirty_path);
236
		}
237

    
238
		write_config();
239

    
240
		header("Location: firewall_nat.php");
241
		exit;
242
	}
243
}
244

    
245
$pgtitle = "Firewall: NAT: Port Forward: Edit";
246
include("head.inc");
247

    
248
?>
249

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