Project

General

Profile

Download (16.2 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
#!/usr/local/bin/php
2 19ae0929 Scott Ullrich
<?php
3 b46bfcf5 Bill Marquette
/* $Id$ */
4 5b237745 Scott Ullrich
/*
5
    firewall_nat_out.php
6 c55b323d Scott Ullrich
    Copyright (C) 2004 Scott Ullrich
7
    All rights reserved.
8 19ae0929 Scott Ullrich
9 c55b323d Scott Ullrich
    originally part of m0n0wall (http://m0n0.ch/wall)
10 5b237745 Scott Ullrich
    Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
    All rights reserved.
12 19ae0929 Scott Ullrich
13 5b237745 Scott Ullrich
    Redistribution and use in source and binary forms, with or without
14
    modification, are permitted provided that the following conditions are met:
15 19ae0929 Scott Ullrich
16 5b237745 Scott Ullrich
    1. Redistributions of source code must retain the above copyright notice,
17
       this list of conditions and the following disclaimer.
18 19ae0929 Scott Ullrich
19 5b237745 Scott Ullrich
    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 19ae0929 Scott Ullrich
23 5b237745 Scott Ullrich
    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 19ae0929 Scott Ullrich
40 5b237745 Scott Ullrich
$a_out = &$config['nat']['advancedoutbound']['rule'];
41
42
if ($_POST) {
43
44
    $pconfig = $_POST;
45
46 9c96aff5 Bill Marquette
    if ($_POST['apply']) {
47 19ae0929 Scott Ullrich
48 9c96aff5 Bill Marquette
        write_config();
49
50
        $retval = 0;
51 19ae0929 Scott Ullrich
52 9c96aff5 Bill Marquette
        if (!file_exists($d_sysrebootreqd_path)) {
53 5b237745 Scott Ullrich
		config_lock();
54 e8c2c890 Bill Marquette
		$retval |= filter_configure();
55
		config_unlock();
56 9c96aff5 Bill Marquette
        }
57 e8c2c890 Bill Marquette
	if(stristr($retval, "error") <> true)
58
	        $savemsg = get_std_save_message($retval);
59
	else
60
		$savemsg = $retval;
61 19ae0929 Scott Ullrich
62 9c96aff5 Bill Marquette
        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 5b237745 Scott Ullrich
    }
69
}
70
71 fe693b89 Bill Marquette
72
73
if (isset($_POST['save'])) {
74 63868cb8 Scott Ullrich
        
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 fa56ab75 Scott Ullrich
			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 63868cb8 Scott Ullrich
                        $natent = array();
105 3decfc11 Scott Ullrich
                        $osn = convert_ip_to_network_format($config['interfaces'][$if]['ipaddr'],
106
                                $config['interfaces'][$if]['subnet']);
107 63868cb8 Scott Ullrich
                        $natent['source']['network'] = $osn;
108
                        $natent['sourceport'] = "";
109 04452b37 Scott Ullrich
                        $int_description = $config['interfaces'][$if]['descr'];
110
                        if($if == "lan")
111
                                $int_description = "LAN";
112
                        $natent['descr'] = "Auto created rule for {$int_description}";
113 63868cb8 Scott Ullrich
                        $natent['target'] = "";
114
                        $natent['interface'] = "wan";
115
                        $natent['destination']['any'] = true;
116
                        $natent['natport'] = "";
117
                        $a_out[] = $natent;
118
                }
119 e034e424 Scott Ullrich
                $savemsg = "Default rules for each interface have been created.";
120 63868cb8 Scott Ullrich
        }
121
        write_config();
122
        touch($d_natconfdirty_path);
123
        header("Location: firewall_nat_out.php");
124
        exit;
125 fe693b89 Bill Marquette
}
126
127 9c96aff5 Bill Marquette
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 87b10bed Bill Marquette
        /* 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 9c96aff5 Bill Marquette
        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 5b237745 Scott Ullrich
}
182 9c96aff5 Bill Marquette
183
184 183a4aae Bill Marquette
$pgtitle = "Firewall: NAT: Outbound";
185 6eb17647 Scott Ullrich
include("head.inc");
186
187 24f600b0 Scott Ullrich
?>
188 5b237745 Scott Ullrich
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
189
<?php include("fbegin.inc"); ?>
190 e8074dcd Bill Marquette
<p class="pgtitle">Firewall: NAT: Outbound</p>
191 fe693b89 Bill Marquette
<form action="firewall_nat_out.php" method="post" name="iform">
192 3f57ceee Bill Marquette
<script type="text/javascript" language="javascript" src="row_toggle.js">
193
</script>
194 5b237745 Scott Ullrich
<?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 a8726a3d Scott Ullrich
<?php
200
	$tab_array = array();
201 1425e067 Bill Marquette
	$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 a8726a3d Scott Ullrich
	display_top_tabs($tab_array);
205
?>
206 5b237745 Scott Ullrich
  </td></tr>
207 19ae0929 Scott Ullrich
  <tr>
208 d732f186 Bill Marquette
    <td>
209
	<div id="mainarea">
210
              <table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
211 fe693b89 Bill Marquette
              <tr>
212 5b237745 Scott Ullrich
                  <td class="vtable"><p>
213 fe693b89 Bill Marquette
                      <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 6b3a7398 Scott Ullrich
                  </td>
216 5b237745 Scott Ullrich
                </tr>
217 19ae0929 Scott Ullrich
                <tr>
218 fe693b89 Bill Marquette
                  <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 5b237745 Scott Ullrich
                  </td>
225
                </tr>
226
                <tr>
227 6b3a7398 Scott Ullrich
                  <td colspan="2"><p><span class="vexpl"><span class="red"><strong>Note:<br>
228 5b237745 Scott Ullrich
                      </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 1425e067 Bill Marquette
                      (except WAN).</span> If you use target addresses other than the WAN interface's IP address, then depending on<span class="vexpl"> the way your WAN connection is setup, you may also need a <a href="firewall_virtual_ip.php">Virtual IP</a>.</span><br>
233 5b237745 Scott Ullrich
                      <br>
234
                      You may enter your own mappings below.</p>
235
                    </td>
236
                </tr>
237
              </table>
238 d732f186 Bill Marquette
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
239 3f57ceee Bill Marquette
                <tr id="frheader">
240
                  <td width="3%" class="list">&nbsp;</td>
241
                  <td width="3%" class="list">&nbsp;</td>
242 5b237745 Scott Ullrich
                  <td width="10%" class="listhdrr">Interface</td>
243
                  <td width="20%" class="listhdrr">Source</td>
244 a539f08b Bill Marquette
                  <td width="20%" class="listhdrr">Source Port</td>
245 5b237745 Scott Ullrich
                  <td width="20%" class="listhdrr">Destination</td>
246 a539f08b Bill Marquette
                  <td width="20%" class="listhdrr">NAT Port</td>
247 5b237745 Scott Ullrich
                  <td width="20%" class="listhdrr">Target</td>
248
                  <td width="25%" class="listhdr">Description</td>
249
                  <td width="5%" class="list"></td>
250
                </tr>
251 9c96aff5 Bill Marquette
              <?php $nnats = $i = 0; foreach ($a_out as $natent): ?>
252 3f57ceee Bill Marquette
                <tr valign="top" id="fr<?=$nnats;?>">
253
                  <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>
254 3364bed4 Bill Marquette
                  <td class="listt" align="center"></td>
255 f8b8c2fd Bill Marquette
                  <td class="listlr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
256 5b237745 Scott Ullrich
                    <?php
257
					if (!$natent['interface'] || ($natent['interface'] == "wan"))
258
					  	echo "WAN";
259
					else
260 bb43786e Scott Ullrich
						echo htmlspecialchars($config['interfaces'][$natent['interface']]['descr']);
261 5b237745 Scott Ullrich
					?>
262 19f09ae1 Scott Ullrich
                                        &nbsp;
263 5b237745 Scott Ullrich
                  </td>
264 f8b8c2fd Bill Marquette
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
265 5b237745 Scott Ullrich
                    <?=$natent['source']['network'];?>
266
                  </td>
267 f8b8c2fd Bill Marquette
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
268 a539f08b Bill Marquette
                    <?php
269
                      if (!$natent['sourceport'])
270
                          echo "*";
271
                      else
272
                          echo $natent['sourceport'];
273
                    ?>
274
                  </td>
275 f8b8c2fd Bill Marquette
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
276 5b237745 Scott Ullrich
                    <?php
277
                      if (isset($natent['destination']['any']))
278
                          echo "*";
279
                      else {
280
                          if (isset($natent['destination']['not']))
281
                              echo "!&nbsp;";
282 2e56710c Scott Ullrich
                          echo $natent['destination']['address'];
283 5b237745 Scott Ullrich
                      }
284
                    ?>
285
                  </td>
286 f8b8c2fd Bill Marquette
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
287 a539f08b Bill Marquette
                    <?php
288
                      if (!$natent['natport'])
289
                          echo "*";
290
                      else
291
                          echo $natent['natport'];
292
                    ?>
293
                  </td>
294 f8b8c2fd Bill Marquette
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
295 5b237745 Scott Ullrich
                    <?php
296
                      if (!$natent['target'])
297
                          echo "*";
298
                      else
299
                          echo $natent['target'];
300
                    ?>
301
                  </td>
302 f8b8c2fd Bill Marquette
                  <td class="listbg"  onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
303 19ae0929 Scott Ullrich
                    <font color="#FFFFFF"><?=htmlspecialchars($natent['descr']);?>&nbsp;
304 5b237745 Scott Ullrich
                  </td>
305 9c96aff5 Bill Marquette
                  <td class="list" valign="middle" nowrap>
306
                    <table border="0" cellspacing="0" cellpadding="1">
307
                      <tr>
308 90e4939a Bill Marquette
                        <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>
309 9c96aff5 Bill Marquette
                      </tr>
310
                      <tr>
311 677c0869 Erik Kristensen
                        <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>
312
                        <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>
313 9c96aff5 Bill Marquette
                      </tr>
314
                    </table>
315
              <?php $i++; $nnats++; endforeach; ?>
316 19ae0929 Scott Ullrich
                <tr>
317 3f57ceee Bill Marquette
                  <td class="list" colspan="9"></td>
318 9c96aff5 Bill Marquette
                  <td class="list" valign="middle" nowrap>
319
                    <table border="0" cellspacing="0" cellpadding="1">
320
                      <tr>
321 677c0869 Erik Kristensen
                        <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>
322 90e4939a Bill Marquette
                        <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>
323 9c96aff5 Bill Marquette
                      </tr>
324
                      <tr>
325 677c0869 Erik Kristensen
                        <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>
326 9c96aff5 Bill Marquette
                      </tr>
327
                    </table></td>
328 5b237745 Scott Ullrich
                </tr>
329
              </table>
330 d732f186 Bill Marquette
	    </div>
331 5b237745 Scott Ullrich
</td>
332
  </tr>
333
</table>
334
            </form>
335
<?php include("fend.inc"); ?>
336
</body>
337
</html>