Project

General

Profile

Download (17.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
    firewall_nat_out.php
5
    Copyright (C) 2004 Scott Ullrich
6
    All rights reserved.
7

    
8
    originally part of m0n0wall (http://m0n0.ch/wall)
9
    Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
10
    All rights reserved.
11

    
12
    Redistribution and use in source and binary forms, with or without
13
    modification, are permitted provided that the following conditions are met:
14

    
15
    1. Redistributions of source code must retain the above copyright notice,
16
       this list of conditions and the following disclaimer.
17

    
18
    2. Redistributions in binary form must reproduce the above copyright
19
       notice, this list of conditions and the following disclaimer in the
20
       documentation and/or other materials provided with the distribution.
21

    
22
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
    POSSIBILITY OF SUCH DAMAGE.
32
*/
33

    
34
require("guiconfig.inc");
35

    
36
if (!is_array($config['nat']['advancedoutbound']['rule']))
37
    $config['nat']['advancedoutbound']['rule'] = array();
38

    
39
$a_out = &$config['nat']['advancedoutbound']['rule'];
40

    
41
if ($_POST) {
42

    
43
    $pconfig = $_POST;
44

    
45
    if ($_POST['apply']) {
46

    
47
        write_config();
48

    
49
        $retval = 0;
50

    
51
	config_lock();
52
	$retval |= filter_configure();
53
	config_unlock();
54

    
55
	if(stristr($retval, "error") <> true)
56
	        $savemsg = get_std_save_message($retval);
57
	else
58
		$savemsg = $retval;
59

    
60
        if ($retval == 0) {
61
            if (file_exists($d_natconfdirty_path))
62
                unlink($d_natconfdirty_path);
63
            if (file_exists($d_filterconfdirty_path))
64
                unlink($d_filterconfdirty_path);
65
        }
66
    }
67
}
68

    
69

    
70

    
71
if (isset($_POST['save'])) {
72
        $was_enabled = $config['nat']['advancedoutbound']['enable'];
73
        /* mutually exclusive settings - if user wants advanced NAT, we don't help with IPSec */
74
        if ($_POST['ipsecpassthru'] == true) {
75
                $config['nat']['ipsecpassthru']['enable'] = true;
76
                $config['nat']['advancedoutbound']['enable'] = false;
77
        }
78
        if ($_POST['advancedoutbound'] == true) {
79
                $config['nat']['advancedoutbound']['enable'] = true;
80
                $config['nat']['ipsecpassthru']['enable'] = false;
81
        }
82
        if ($_POST['ipsecpassthru'] == false)
83
                $config['nat']['ipsecpassthru']['enable'] = false;
84
        if ($_POST['advancedoutbound'] == false)
85
                $config['nat']['advancedoutbound']['enable'] = false;
86
        if(!$was_enabled and $_POST['advancedoutbound'] <> "") {
87
                /*
88
                 *    user has enabled advanced outbound nat -- lets automatically create entries
89
                 *    for all of the interfaces to make life easier on the pip-o-chap
90
                 */
91
                $a_out = &$config['nat']['advancedoutbound']['rule'];
92
                $ifdescrs = array('lan');
93
                for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) 
94
                        $ifdescrs[] = "opt" . $j;
95
                foreach($ifdescrs as $if) {
96
			if($if <> "lan" and $if <> "wan") {
97
				/* interface is an optional.  is it enabled? */
98
				if(!isset($config['interfaces'][$if]['enabled'])) {
99
					continue;
100
				}
101
			}
102
                        $natent = array();
103
                        $osn = gen_subnet($config['interfaces'][$if]['ipaddr'],
104
                                $config['interfaces'][$if]['subnet']);
105
                        $natent['source']['network'] = $osn . "/" . $config['interfaces'][$if]['subnet'];
106
                        $natent['sourceport'] = "";
107
                        $int_description = $config['interfaces'][$if]['descr'];
108
                        if($if == "lan")
109
                                $int_description = "LAN";
110
                        $natent['descr'] = "Auto created rule for {$int_description}";
111
                        $natent['target'] = "";
112
                        $natent['interface'] = "wan";
113
                        $natent['destination']['any'] = true;
114
                        $natent['natport'] = "";
115
                        $a_out[] = $natent;
116
                }
117
                $savemsg = "Default rules for each interface have been created.";
118
        }
119
        write_config();
120
        touch($d_natconfdirty_path);
121
        header("Location: firewall_nat_out.php");
122
        exit;
123
}
124

    
125
if (isset($_POST['del_x'])) {
126
        /* delete selected rules */
127
        if (is_array($_POST['rule']) && count($_POST['rule'])) {
128
                foreach ($_POST['rule'] as $rulei) {
129
                        unset($a_out[$rulei]);
130
                }
131
                write_config();
132
                touch($d_natconfdirty_path);
133
                header("Location: firewall_nat_out.php");
134
                exit;
135
        }
136

    
137
} else {
138
        /* yuck - IE won't send value attributes for image buttons, while Mozilla does - so we use .x/.y to find move button clicks instead... */
139
        unset($movebtn);
140
        foreach ($_POST as $pn => $pd) {
141
                if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
142
                        $movebtn = $matches[1];
143
                        break;
144
                }
145
        }
146
        /* move selected rules before this rule */
147
        if (isset($movebtn) && is_array($_POST['rule']) && count($_POST['rule'])) {
148
                $a_out_new = array();
149

    
150
                /* copy all rules < $movebtn and not selected */
151
                for ($i = 0; $i < $movebtn; $i++) {
152
                        if (!in_array($i, $_POST['rule']))
153
                                $a_out_new[] = $a_out[$i];
154
                }
155

    
156
                /* copy all selected rules */
157
                for ($i = 0; $i < count($a_out); $i++) {
158
                        if ($i == $movebtn)
159
                                continue;
160
                        if (in_array($i, $_POST['rule']))
161
                                $a_out_new[] = $a_out[$i];
162
                }
163

    
164
                /* copy $movebtn rule */
165
                if ($movebtn < count($a_out))
166
                        $a_out_new[] = $a_out[$movebtn];
167

    
168
                /* copy all rules > $movebtn and not selected */
169
                for ($i = $movebtn+1; $i < count($a_out); $i++) {
170
                        if (!in_array($i, $_POST['rule']))
171
                                $a_out_new[] = $a_out[$i];
172
                }
173
                $a_out = $a_out_new;
174
                write_config();
175
                touch($d_natconfdirty_path);
176
                header("Location: firewall_nat_out.php");
177
                exit;
178
        }
179
}
180

    
181

    
182
$pgtitle = "Firewall: NAT: Outbound";
183
include("head.inc");
184

    
185
?>
186
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
187
<?php include("fbegin.inc"); ?>
188
<p class="pgtitle">Firewall: NAT: Outbound</p>
189
<form action="firewall_nat_out.php" method="post" name="iform">
190
<script type="text/javascript" language="javascript" src="row_toggle.js">
191
</script>
192
<?php if ($savemsg) print_info_box($savemsg); ?>
193
<?php if (file_exists($d_natconfdirty_path)): ?><p>
194
<?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>
195
<?php endif; ?>
196
<table width="100%" border="0" cellpadding="0" cellspacing="0">  <tr><td>
197
<?php
198
	$tab_array = array();
199
	$tab_array[] = array("Port Forward", false, "firewall_nat.php");
200
	$tab_array[] = array("1:1", false, "firewall_nat_1to1.php");
201
	$tab_array[] = array("Outbound", true, "firewall_nat_out.php");
202
	display_top_tabs($tab_array);
203
?>
204
  </td></tr>
205
  <tr>
206
    <td>
207
	<div id="mainarea">
208
              <table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
209
              <tr>
210
                  <td class="vtable"><p>
211
                      <input name="ipsecpassthru" type="checkbox" id="ipsecpassthru" value="yes" onClick="document.iform.advancedoutbound.checked=false" <?php if (isset($config['nat']['ipsecpassthru']['enable'])) echo "checked";?>>
212
                      <strong>Enable IPSec passthru</strong></p>
213
                  </td>
214
                </tr>
215
                <tr>
216
                  <td class="vtable"><p>
217
                      <input name="advancedoutbound" type="checkbox" id="advancedoutbound" value="yes" onClick="document.iform.ipsecpassthru.checked=false" <?php if (isset($config['nat']['advancedoutbound']['enable'])) echo "checked";?>>
218
                      <strong>Enable advanced outbound NAT</strong></p></td>
219
                </tr>
220
                <tr>
221
                  <td> <input name="save" type="submit" class="formbtn" value="Save">
222
                  </td>
223
                </tr>
224
                <tr>
225
                  <td colspan="2"><p><span class="vexpl"><span class="red"><strong>Note:<br>
226
                      </strong></span>If advanced outbound NAT is enabled, no outbound NAT
227
                      rules will be automatically generated any longer. Instead, only the mappings
228
                      you specify below will be used. With advanced outbound NAT disabled,
229
                      a mapping is automatically created for each interface's subnet
230
                      (except WAN).  If you use target addresses other than the WAN interface's
231
		      IP address, then depending on the way your WAN connection is setup, you
232
	              may also need a <a href="firewall_virtual_ip.php">Virtual IP</a>.</span><br>
233
                      <br>
234
                      You may enter your own mappings below.</p>
235
                    </td>
236
                </tr>
237
              </table>
238
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
239
                <tr id="frheader">
240
                  <td width="3%" class="list">&nbsp;</td>
241
                  <td width="3%" class="list">&nbsp;</td>
242
                  <td width="10%" class="listhdrr">Interface</td>
243
                  <td width="15%" class="listhdrr">Source</td>
244
                  <td width="10%" class="listhdrr">Source Port</td>
245
                  <td width="15%" class="listhdrr">Destination</td>
246
                  <td width="10%" class="listhdrr">Destination Port</td>
247
                  <td width="15%" class="listhdrr">NAT Address</td>
248
                  <td width="10%" class="listhdrr">NAT Port</td>
249
		  <td width="10%" class="listhdrr">Static Port</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 if (!$natent['interface'] || ($natent['interface'] == "lan"))
262
                                                 echo "LAN";                                                
263
					else
264
						echo htmlspecialchars($config['interfaces'][$natent['interface']]['descr']);
265
					?>
266
                                        &nbsp;
267
                  </td>
268
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
269
                    <?=$natent['source']['network'];?>
270
                  </td>
271
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
272
                    <?php
273
                      if (!$natent['sourceport'])
274
                          echo "*";
275
                      else
276
                          echo $natent['sourceport'];
277
                    ?>
278
                  </td>
279
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
280
                    <?php
281
                      if (isset($natent['destination']['any']))
282
                          echo "*";
283
                      else {
284
                          if (isset($natent['destination']['not']))
285
                              echo "!&nbsp;";
286
                          echo $natent['destination']['address'];
287
                      }
288
                    ?>
289
                  </td>
290
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
291
                    <?php
292
                      if (!$natent['dstport'])
293
                          echo "*";
294
                      else
295
                          echo $natent['dstport'];
296
                    ?>
297
                  </td>
298
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
299
                    <?php
300
                      if (!$natent['target'])
301
                          echo "*";
302
                      else
303
                          echo $natent['target'];
304
                    ?>
305
                  </td>
306
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
307
                    <?php
308
                      if (!$natent['natport'])
309
                          echo "*";
310
                      else
311
                          echo $natent['natport'];
312
                    ?>
313
                  </td>
314
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
315
                    <?php
316
			if(isset($natent['staticnatport']))
317
			    echo "<CENTER>YES</CENTER>";
318
			else
319
			    echo "<CENTER>NO</CENTER>";
320
                    ?>		    
321
                  </td>
322
                  <td class="listbg"  onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
323
                    <font color="#FFFFFF"><?=htmlspecialchars($natent['descr']);?>&nbsp;
324
                  </td>
325
                  <td class="list" valign="middle" nowrap>
326
                    <table border="0" cellspacing="0" cellpadding="1">
327
                      <tr>
328
                        <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>
329
                      </tr>
330
                      <tr>
331
                        <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>
332
                        <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>
333
                      </tr>
334
                    </table>
335
              <?php $i++; $nnats++; endforeach; ?>
336
                <tr>
337
                  <td class="list" colspan="11"></td>
338
                  <td class="list" valign="middle" nowrap>
339
                    <table border="0" cellspacing="0" cellpadding="1">
340
                      <tr>
341
                        <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>
342
                        <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>
343
                      </tr>
344
                      <tr>
345
                        <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>
346
                      </tr>
347
                    </table></td>
348
                </tr>
349
              </table>
350
	    </div>
351
</td>
352
  </tr>
353
</table>
354
            </form>
355
<?php include("fend.inc"); ?>
356
</body>
357
</html>
(42-42/160)