1
|
#!/usr/local/bin/php
|
2
|
<?php
|
3
|
/* $Id$ */
|
4
|
/*
|
5
|
firewall_nat_out.php
|
6
|
Copyright (C) 2004 Scott Ullrich
|
7
|
All rights reserved.
|
8
|
|
9
|
originally part of m0n0wall (http://m0n0.ch/wall)
|
10
|
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
11
|
All rights reserved.
|
12
|
|
13
|
Redistribution and use in source and binary forms, with or without
|
14
|
modification, are permitted provided that the following conditions are met:
|
15
|
|
16
|
1. Redistributions of source code must retain the above copyright notice,
|
17
|
this list of conditions and the following disclaimer.
|
18
|
|
19
|
2. Redistributions in binary form must reproduce the above copyright
|
20
|
notice, this list of conditions and the following disclaimer in the
|
21
|
documentation and/or other materials provided with the distribution.
|
22
|
|
23
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
24
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
25
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
26
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
27
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
POSSIBILITY OF SUCH DAMAGE.
|
33
|
*/
|
34
|
|
35
|
require("guiconfig.inc");
|
36
|
|
37
|
if (!is_array($config['nat']['advancedoutbound']['rule']))
|
38
|
$config['nat']['advancedoutbound']['rule'] = array();
|
39
|
|
40
|
$a_out = &$config['nat']['advancedoutbound']['rule'];
|
41
|
|
42
|
if ($_POST) {
|
43
|
|
44
|
$pconfig = $_POST;
|
45
|
|
46
|
if ($_POST['apply']) {
|
47
|
|
48
|
write_config();
|
49
|
|
50
|
$retval = 0;
|
51
|
|
52
|
if (!file_exists($d_sysrebootreqd_path)) {
|
53
|
config_lock();
|
54
|
$retval |= filter_configure();
|
55
|
config_unlock();
|
56
|
}
|
57
|
if(stristr($retval, "error") <> true)
|
58
|
$savemsg = get_std_save_message($retval);
|
59
|
else
|
60
|
$savemsg = $retval;
|
61
|
|
62
|
if ($retval == 0) {
|
63
|
if (file_exists($d_natconfdirty_path))
|
64
|
unlink($d_natconfdirty_path);
|
65
|
if (file_exists($d_filterconfdirty_path))
|
66
|
unlink($d_filterconfdirty_path);
|
67
|
}
|
68
|
}
|
69
|
}
|
70
|
|
71
|
|
72
|
|
73
|
if (isset($_POST['save'])) {
|
74
|
|
75
|
/* mutually exclusive settings - if user wants advanced NAT, we don't help with IPSec */
|
76
|
if ($_POST['ipsecpassthru'] == true) {
|
77
|
$config['nat']['ipsecpassthru']['enable'] = true;
|
78
|
$config['nat']['advancedoutbound']['enable'] = false;
|
79
|
}
|
80
|
if ($_POST['advancedoutbound'] == true) {
|
81
|
$config['nat']['advancedoutbound']['enable'] = true;
|
82
|
$config['nat']['ipsecpassthru']['enable'] = false;
|
83
|
}
|
84
|
if ($_POST['ipsecpassthru'] == false)
|
85
|
$config['nat']['ipsecpassthru']['enable'] = false;
|
86
|
if ($_POST['advancedoutbound'] == false)
|
87
|
$config['nat']['advancedoutbound']['enable'] = false;
|
88
|
if($config['nat']['advancedoutbound']['enable'] and $_POST['advancedoutbound'] <> "") {
|
89
|
/*
|
90
|
* user has enabled advanced outbound nat -- lets automatically create entries
|
91
|
* for all of the interfaces to make life easier on the pip-o-chap
|
92
|
*/
|
93
|
$a_out = &$config['nat']['advancedoutbound']['rule'];
|
94
|
$ifdescrs = array('lan');
|
95
|
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
|
96
|
$ifdescrs[] = "opt" . $j;
|
97
|
foreach($ifdescrs as $if) {
|
98
|
if($if <> "lan" and $if <> "wan") {
|
99
|
/* interface is an optional. is it enabled? */
|
100
|
if(!isset($config['interfaces'][$if]['enabled'])) {
|
101
|
continue;
|
102
|
}
|
103
|
}
|
104
|
$natent = array();
|
105
|
$osn = convert_ip_to_network_format($config['interfaces'][$if]['ipaddr'],
|
106
|
$config['interfaces'][$if]['subnet']);
|
107
|
$natent['source']['network'] = $osn;
|
108
|
$natent['sourceport'] = "";
|
109
|
$int_description = $config['interfaces'][$if]['descr'];
|
110
|
if($if == "lan")
|
111
|
$int_description = "LAN";
|
112
|
$natent['descr'] = "Auto created rule for {$int_description}";
|
113
|
$natent['target'] = "";
|
114
|
$natent['interface'] = "wan";
|
115
|
$natent['destination']['any'] = true;
|
116
|
$natent['natport'] = "";
|
117
|
$a_out[] = $natent;
|
118
|
}
|
119
|
$savemsg = "Default rules for each interface have been created.";
|
120
|
}
|
121
|
write_config();
|
122
|
touch($d_natconfdirty_path);
|
123
|
header("Location: firewall_nat_out.php");
|
124
|
exit;
|
125
|
}
|
126
|
|
127
|
if (isset($_POST['del_x'])) {
|
128
|
/* delete selected rules */
|
129
|
if (is_array($_POST['rule']) && count($_POST['rule'])) {
|
130
|
foreach ($_POST['rule'] as $rulei) {
|
131
|
unset($a_out[$rulei]);
|
132
|
}
|
133
|
write_config();
|
134
|
touch($d_natconfdirty_path);
|
135
|
header("Location: firewall_nat_out.php");
|
136
|
exit;
|
137
|
}
|
138
|
|
139
|
} else {
|
140
|
/* yuck - IE won't send value attributes for image buttons, while Mozilla does - so we use .x/.y to find move button clicks instead... */
|
141
|
unset($movebtn);
|
142
|
foreach ($_POST as $pn => $pd) {
|
143
|
if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
|
144
|
$movebtn = $matches[1];
|
145
|
break;
|
146
|
}
|
147
|
}
|
148
|
/* move selected rules before this rule */
|
149
|
if (isset($movebtn) && is_array($_POST['rule']) && count($_POST['rule'])) {
|
150
|
$a_out_new = array();
|
151
|
|
152
|
/* copy all rules < $movebtn and not selected */
|
153
|
for ($i = 0; $i < $movebtn; $i++) {
|
154
|
if (!in_array($i, $_POST['rule']))
|
155
|
$a_out_new[] = $a_out[$i];
|
156
|
}
|
157
|
|
158
|
/* copy all selected rules */
|
159
|
for ($i = 0; $i < count($a_out); $i++) {
|
160
|
if ($i == $movebtn)
|
161
|
continue;
|
162
|
if (in_array($i, $_POST['rule']))
|
163
|
$a_out_new[] = $a_out[$i];
|
164
|
}
|
165
|
|
166
|
/* copy $movebtn rule */
|
167
|
if ($movebtn < count($a_out))
|
168
|
$a_out_new[] = $a_out[$movebtn];
|
169
|
|
170
|
/* copy all rules > $movebtn and not selected */
|
171
|
for ($i = $movebtn+1; $i < count($a_out); $i++) {
|
172
|
if (!in_array($i, $_POST['rule']))
|
173
|
$a_out_new[] = $a_out[$i];
|
174
|
}
|
175
|
$a_out = $a_out_new;
|
176
|
write_config();
|
177
|
touch($d_natconfdirty_path);
|
178
|
header("Location: firewall_nat_out.php");
|
179
|
exit;
|
180
|
}
|
181
|
}
|
182
|
|
183
|
|
184
|
$pgtitle = "Firewall: NAT: Outbound";
|
185
|
include("head.inc");
|
186
|
|
187
|
?>
|
188
|
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
|
189
|
<?php include("fbegin.inc"); ?>
|
190
|
<p class="pgtitle">Firewall: NAT: Outbound</p>
|
191
|
<form action="firewall_nat_out.php" method="post" name="iform">
|
192
|
<script type="text/javascript" language="javascript" src="row_toggle.js">
|
193
|
</script>
|
194
|
<?php if ($savemsg) print_info_box($savemsg); ?>
|
195
|
<?php if (file_exists($d_natconfdirty_path)): ?><p>
|
196
|
<?php print_info_box_np("The NAT configuration has been changed.<br>You must apply the changes in order for them to take effect.");?><br>
|
197
|
<?php endif; ?>
|
198
|
<table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr><td>
|
199
|
<?php
|
200
|
$tab_array = array();
|
201
|
$tab_array[] = array("Port Forward", false, "firewall_nat.php");
|
202
|
$tab_array[] = array("1:1", false, "firewall_nat_1to1.php");
|
203
|
$tab_array[] = array("Outbound", true, "firewall_nat_out.php");
|
204
|
display_top_tabs($tab_array);
|
205
|
?>
|
206
|
</td></tr>
|
207
|
<tr>
|
208
|
<td>
|
209
|
<div id="mainarea">
|
210
|
<table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
|
211
|
<tr>
|
212
|
<td class="vtable"><p>
|
213
|
<input name="ipsecpassthru" type="checkbox" id="ipsecpassthru" value="yes" onClick="document.iform.advancedoutbound.checked=false" <?php if (isset($config['nat']['ipsecpassthru']['enable'])) echo "checked";?>>
|
214
|
<strong>Enable IPSec passthru</strong></p>
|
215
|
</td>
|
216
|
</tr>
|
217
|
<tr>
|
218
|
<td class="vtable"><p>
|
219
|
<input name="advancedoutbound" type="checkbox" id="advancedoutbound" value="yes" onClick="document.iform.ipsecpassthru.checked=false" <?php if (isset($config['nat']['advancedoutbound']['enable'])) echo "checked";?>>
|
220
|
<strong>Enable advanced outbound NAT</strong></p></td>
|
221
|
</tr>
|
222
|
<tr>
|
223
|
<td> <input name="save" type="submit" class="formbtn" value="Save">
|
224
|
</td>
|
225
|
</tr>
|
226
|
<tr>
|
227
|
<td colspan="2"><p><span class="vexpl"><span class="red"><strong>Note:<br>
|
228
|
</strong></span>If advanced outbound NAT is enabled, no outbound NAT
|
229
|
rules will be automatically generated anymore. Instead, only the mappings
|
230
|
you specify below will be used. With advanced outbound NAT disabled,
|
231
|
a mapping is automatically created for each interface's subnet
|
232
|
(except WAN). If you use target addresses other than the WAN interface's
|
233
|
IP address, then depending on the way your WAN connection is setup, you
|
234
|
may also need a <a href="firewall_virtual_ip.php">Virtual IP</a>.</span><br>
|
235
|
<br>
|
236
|
You may enter your own mappings below.</p>
|
237
|
</td>
|
238
|
</tr>
|
239
|
</table>
|
240
|
<table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
|
241
|
<tr id="frheader">
|
242
|
<td width="3%" class="list"> </td>
|
243
|
<td width="3%" class="list"> </td>
|
244
|
<td width="10%" class="listhdrr">Interface</td>
|
245
|
<td width="20%" class="listhdrr">Source</td>
|
246
|
<td width="20%" class="listhdrr">Source Port</td>
|
247
|
<td width="20%" class="listhdrr">Destination</td>
|
248
|
<td width="20%" class="listhdrr">NAT Port</td>
|
249
|
<td width="20%" class="listhdrr">Target</td>
|
250
|
<td width="25%" class="listhdr">Description</td>
|
251
|
<td width="5%" class="list"></td>
|
252
|
</tr>
|
253
|
<?php $nnats = $i = 0; foreach ($a_out as $natent): ?>
|
254
|
<tr valign="top" id="fr<?=$nnats;?>">
|
255
|
<td class="listt"><input type="checkbox" id="frc<?=$nnats;?>" name="rule[]" value="<?=$i;?>" onClick="fr_bgcolor('<?=$nnats;?>')" style="margin: 0; padding: 0; width: 15px; height: 15px;"></td>
|
256
|
<td class="listt" align="center"></td>
|
257
|
<td class="listlr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
|
258
|
<?php
|
259
|
if (!$natent['interface'] || ($natent['interface'] == "wan"))
|
260
|
echo "WAN";
|
261
|
else
|
262
|
echo htmlspecialchars($config['interfaces'][$natent['interface']]['descr']);
|
263
|
?>
|
264
|
|
265
|
</td>
|
266
|
<td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
|
267
|
<?=$natent['source']['network'];?>
|
268
|
</td>
|
269
|
<td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
|
270
|
<?php
|
271
|
if (!$natent['sourceport'])
|
272
|
echo "*";
|
273
|
else
|
274
|
echo $natent['sourceport'];
|
275
|
?>
|
276
|
</td>
|
277
|
<td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
|
278
|
<?php
|
279
|
if (isset($natent['destination']['any']))
|
280
|
echo "*";
|
281
|
else {
|
282
|
if (isset($natent['destination']['not']))
|
283
|
echo "! ";
|
284
|
echo $natent['destination']['address'];
|
285
|
}
|
286
|
?>
|
287
|
</td>
|
288
|
<td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
|
289
|
<?php
|
290
|
if (!$natent['natport'])
|
291
|
echo "*";
|
292
|
else
|
293
|
echo $natent['natport'];
|
294
|
?>
|
295
|
</td>
|
296
|
<td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
|
297
|
<?php
|
298
|
if (!$natent['target'])
|
299
|
echo "*";
|
300
|
else
|
301
|
echo $natent['target'];
|
302
|
?>
|
303
|
</td>
|
304
|
<td class="listbg" onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
|
305
|
<font color="#FFFFFF"><?=htmlspecialchars($natent['descr']);?>
|
306
|
</td>
|
307
|
<td class="list" valign="middle" nowrap>
|
308
|
<table border="0" cellspacing="0" cellpadding="1">
|
309
|
<tr>
|
310
|
<td><a href="firewall_nat_out_edit.php?id=<?=$i;?>"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" width="17" height="17" border="0" title="edit mapping"></a></td>
|
311
|
</tr>
|
312
|
<tr>
|
313
|
<td><input onmouseover="fr_insline(<?=$nnats;?>, true)" onmouseout="fr_insline(<?=$nnats;?>, false)" name="move_<?=$i;?>" src="/themes/<?= $g['theme']; ?>/images/icons/icon_left.gif" title="move selected rules before this rule" height="17" type="image" width="17" border="0"></td>
|
314
|
<td><a href="firewall_nat_out_edit.php?dup=<?=$i;?>"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="add a new nat based on this one" width="17" height="17" border="0"></a></td>
|
315
|
</tr>
|
316
|
</table>
|
317
|
<?php $i++; $nnats++; endforeach; ?>
|
318
|
<tr>
|
319
|
<td class="list" colspan="9"></td>
|
320
|
<td class="list" valign="middle" nowrap>
|
321
|
<table border="0" cellspacing="0" cellpadding="1">
|
322
|
<tr>
|
323
|
<td><?php if ($nnats == 0): ?><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_left_d.gif" width="17" height="17" title="move selected mappings to end" border="0"><?php else: ?><input name="move_<?=$i;?>" type="image" src="/themes/<?= $g['theme']; ?>/images/icons/icon_left.gif" width="17" height="17" title="move selected mappings to end" border="0"><?php endif; ?></td>
|
324
|
<td><a href="firewall_nat_out_edit.php"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0" title="add new mapping"></a></td>
|
325
|
</tr>
|
326
|
<tr>
|
327
|
<td><?php if ($nnats == 0): ?><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_x_d.gif" width="17" height="17" title="delete selected rules" border="0"><?php else: ?><input name="del" type="image" src="/themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" title="delete selected mappings" onclick="return confirm('Do you really want to delete the selected mappings?')"><?php endif; ?></td>
|
328
|
</tr>
|
329
|
</table></td>
|
330
|
</tr>
|
331
|
</table>
|
332
|
</div>
|
333
|
</td>
|
334
|
</tr>
|
335
|
</table>
|
336
|
</form>
|
337
|
<?php include("fend.inc"); ?>
|
338
|
</body>
|
339
|
</html>
|