Project

General

Profile

Download (17.1 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($_GET['dup'])) {
46
        $id = $_GET['dup'];
47
        $after = $_GET['dup'];
48
}
49

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

    
64
if ($_POST) {
65

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

    
73
	if (!$_POST['endport'])
74
		$_POST['endport'] = $_POST['beginport'];
75

    
76
	unset($input_errors);
77
	$pconfig = $_POST;
78

    
79
	/* input validation */
80
	$reqdfields = explode(" ", "interface proto beginport localip localbeginport");
81
	$reqdfieldsn = explode(",", "Interface,Protocol,Start port,NAT IP,Local port");
82

    
83
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
84

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

    
98
	if ($_POST['beginport'] > $_POST['endport']) {
99
		/* swap */
100
		$tmp = $_POST['endport'];
101
		$_POST['endport'] = $_POST['beginport'];
102
		$_POST['beginport'] = $tmp;
103
	}
104

    
105
	if (!$input_errors) {
106
		if (($_POST['endport'] - $_POST['beginport'] + $_POST['localbeginport']) > 65535)
107
			$input_errors[] = "The target port range must be an integer between 1 and 65535.";
108
	}
109

    
110
	/* check for overlaps */
111
	foreach ($a_nat as $natent) {
112
		if (isset($id) && ($a_nat[$id]) && ($a_nat[$id] === $natent))
113
			continue;
114
		if ($natent['interface'] != $_POST['interface'])
115
			continue;
116
		if ($natent['external-address'] != $_POST['extaddr'])
117
			continue;
118

    
119
		list($begp,$endp) = explode("-", $natent['external-port']);
120
		if (!$endp)
121
			$endp = $begp;
122

    
123
		if (!(   (($_POST['beginport'] < $begp) && ($_POST['endport'] < $begp))
124
		      || (($_POST['beginport'] > $endp) && ($_POST['endport'] > $endp)))) {
125

    
126
			$input_errors[] = "The external port range overlaps with an existing entry.";
127
			break;
128
		}
129
	}
130

    
131
	if (!$input_errors) {
132
		$natent = array();
133
		if ($_POST['extaddr'])
134
			$natent['external-address'] = $_POST['extaddr'];
135
		$natent['protocol'] = $_POST['proto'];
136

    
137
		if ($_POST['beginport'] == $_POST['endport'])
138
			$natent['external-port'] = $_POST['beginport'];
139
		else
140
			$natent['external-port'] = $_POST['beginport'] . "-" . $_POST['endport'];
141

    
142
		$natent['target'] = $_POST['localip'];
143
		$natent['local-port'] = $_POST['localbeginport'];
144
		$natent['interface'] = $_POST['interface'];
145
		$natent['descr'] = $_POST['descr'];
146

    
147
		if (isset($id) && $a_nat[$id])
148
			$a_nat[$id] = $natent;
149
		else {
150
			if (is_numeric($after))
151
				array_splice($a_nat, $after+1, 0, array($natent));
152
			else
153
				$a_nat[] = $natent;
154
		}
155

    
156
		touch($d_natconfdirty_path);
157

    
158
		if ($_POST['autoadd']) {
159
			/* auto-generate a matching firewall rule */
160
			$filterent = array();
161
			$filterent['interface'] = $_POST['interface'];
162
			$filterent['protocol'] = $_POST['proto'];
163
			$filterent['source']['any'] = "";
164
			$filterent['destination']['address'] = $_POST['localip'];
165

    
166
			$dstpfrom = $_POST['localbeginport'];
167
			$dstpto = $dstpfrom + $_POST['endport'] - $_POST['beginport'];
168

    
169
			if ($dstpfrom == $dstpto)
170
				$filterent['destination']['port'] = $dstpfrom;
171
			else
172
				$filterent['destination']['port'] = $dstpfrom . "-" . $dstpto;
173

    
174
			$filterent['descr'] = "NAT " . $_POST['descr'];
175

    
176
			$config['filter']['rule'][] = $filterent;
177

    
178
			touch($d_filterconfdirty_path);
179
		}
180

    
181
		write_config();
182

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

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