Project

General

Profile

Download (16.5 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

    
42
if ($_POST['apply']) {
43
	write_config();
44

    
45
	$retval = 0;
46

    
47
	config_lock();
48
	$retval |= filter_configure();
49
	config_unlock();
50

    
51
	if(stristr($retval, "error") <> true)
52
	        $savemsg = get_std_save_message($retval);
53
	else
54
		$savemsg = $retval;
55

    
56
	if ($retval == 0) {
57
		unlink_if_exists($d_natconfdirty_path);
58
		unlink_if_exists($d_filterconfdirty_path);
59
        }
60
}
61

    
62

    
63

    
64
if (isset($_POST['save']) && $_POST['save'] == "Save") {
65
	/* mutually exclusive settings - if user wants advanced NAT, we don't generate automatic rules */
66
	switch ($_POST['advancedoripsec']) {
67
	case "ipsecpassthru":
68
               	$config['nat']['ipsecpassthru']['enable'] = true;
69
               	unset($config['nat']['advancedoutbound']['enable']);
70
               	if(count($config['nat']['advancedoutbound']['rule']) == 0)
71
			unset($config['nat']['advancedoutbound']['rule']);
72
		break;
73
	case "advancedoutboundnat":
74
        	$was_enabled = isset($config['nat']['advancedoutbound']['enable']);
75
		$config['nat']['advancedoutbound']['enable'] = true;
76
		unset($config['nat']['ipsecpassthru']['enable']);
77
		if($was_enabled == false) {
78
			/*
79
			 *    user has enabled advanced outbound nat -- lets automatically create entries
80
			 *    for all of the interfaces to make life easier on the pip-o-chap
81
			 */
82
			$ifdescrs = get_configured_interface_with_descr();
83
				
84
			foreach($ifdescrs as $if => $ifdesc) {
85
				$natent = array();
86
				$osn = gen_subnet($config['interfaces'][$if]['ipaddr'],
87
					$config['interfaces'][$if]['subnet']);
88
				$natent['source']['network'] = $osn . "/" . $config['interfaces'][$if]['subnet'];
89
				$natent['sourceport'] = "";
90
				$natent['descr'] = "Auto created rule for {$ifdesc}";
91
				$natent['target'] = "";
92
				$natent['interface'] = "wan";
93
				$natent['destination']['any'] = true;
94
				$natent['natport'] = "";
95
				$a_out[] = $natent;
96
			}
97
			$savemsg = "Default rules for each interface have been created.";
98
		}
99
		break;
100
	}
101
        write_config();
102
        touch($d_natconfdirty_path);
103
        header("Location: firewall_nat_out.php");
104
        exit;
105
}
106

    
107
if (isset($_POST['del_x'])) {
108
        /* delete selected rules */
109
        if (is_array($_POST['rule']) && count($_POST['rule'])) {
110
                foreach ($_POST['rule'] as $rulei) {
111
                        unset($a_out[$rulei]);
112
                }
113
                write_config();
114
                touch($d_natconfdirty_path);
115
                header("Location: firewall_nat_out.php");
116
                exit;
117
        }
118

    
119
} else {
120
        /* yuck - IE won't send value attributes for image buttons, while Mozilla does - so we use .x/.y to find move button clicks instead... */
121
        unset($movebtn);
122
        foreach ($_POST as $pn => $pd) {
123
                if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
124
                        $movebtn = $matches[1];
125
                        break;
126
                }
127
        }
128
        /* move selected rules before this rule */
129
        if (isset($movebtn) && is_array($_POST['rule']) && count($_POST['rule'])) {
130
                $a_out_new = array();
131

    
132
                /* copy all rules < $movebtn and not selected */
133
                for ($i = 0; $i < $movebtn; $i++) {
134
                        if (!in_array($i, $_POST['rule']))
135
                                $a_out_new[] = $a_out[$i];
136
                }
137

    
138
                /* copy all selected rules */
139
                for ($i = 0; $i < count($a_out); $i++) {
140
                        if ($i == $movebtn)
141
                                continue;
142
                        if (in_array($i, $_POST['rule']))
143
                                $a_out_new[] = $a_out[$i];
144
                }
145

    
146
                /* copy $movebtn rule */
147
                if ($movebtn < count($a_out))
148
                        $a_out_new[] = $a_out[$movebtn];
149

    
150
                /* copy all rules > $movebtn and not selected */
151
                for ($i = $movebtn+1; $i < count($a_out); $i++) {
152
                        if (!in_array($i, $_POST['rule']))
153
                                $a_out_new[] = $a_out[$i];
154
                }
155
                if (count($a_out_new) > 0)
156
			$a_out = $a_out_new;
157
		else
158
			unset($config['nat']['advancedoutbound']);
159

    
160
                write_config();
161
                touch($d_natconfdirty_path);
162
                header("Location: firewall_nat_out.php");
163
                exit;
164
        }
165
}
166

    
167

    
168
$pgtitle = array("Firewall","NAT","Outbound");
169
include("head.inc");
170

    
171
?>
172
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
173
<?php include("fbegin.inc"); ?>
174
<form action="firewall_nat_out.php" method="post" name="iform">
175
<script type="text/javascript" language="javascript" src="row_toggle.js">
176
</script>
177
<?php if ($savemsg) print_info_box($savemsg); ?>
178
<?php if (file_exists($d_natconfdirty_path)): ?><p>
179
<?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>
180
<?php endif; ?>
181
<table width="100%" border="0" cellpadding="0" cellspacing="0">  <tr><td>
182
<?php
183
	$tab_array = array();
184
	$tab_array[] = array("Port Forward", false, "firewall_nat.php");
185
	$tab_array[] = array("1:1", false, "firewall_nat_1to1.php");
186
	$tab_array[] = array("Outbound", true, "firewall_nat_out.php");
187
	display_top_tabs($tab_array);
188
?>
189
  </td></tr>
190
  <tr>
191
    <td>
192
	<div id="mainarea">
193
              <table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
194
              <tr>
195
                  <td class="vtable"><p>
196
                      <input name="advancedoripsec" type="radio" id="ipsecpassthru" value="ipsecpassthru" <?php if (isset($config['nat']['ipsecpassthru']['enable'])) echo "checked";?>>
197
                      <strong><?=gettext("Automatic outbound NAT rule generation (IPsec passthrough)");?></strong></p>
198
                  </td>
199
                </tr>
200
                <tr>
201
                  <td class="vtable"><p>
202
                      <input name="advancedoripsec" type="radio" id="advancedoutbound" value="advancedoutboundnat" <?php if (isset($config['nat']['advancedoutbound']['enable'])) echo "checked";?>>
203
                      <strong><?=gettext("Manual Outbound NAT rule generation (Advanced Outbound NAT (AON))");?></strong></p></td>
204
                </tr>
205
                <tr>
206
                  <td> <input name="save" type="submit" class="formbtn" value="Save">
207
                  </td>
208
                </tr>
209
                <tr>
210
                  <td colspan="2"><p><span class="vexpl"><span class="red"><strong>Note:<br>
211
                      </strong></span>If advanced outbound NAT is enabled, no outbound NAT
212
                      rules will be automatically generated any longer. Instead, only the mappings
213
                      you specify below will be used. With advanced outbound NAT disabled,
214
                      a mapping is automatically created for each interface's subnet
215
                      (except WAN).  If you use target addresses other than the WAN interface's
216
		      IP address, then depending on the way your WAN connection is setup, you
217
	              may also need a <a href="firewall_virtual_ip.php">Virtual IP</a>.</span><br>
218
                      <br>
219
                      You may enter your own mappings below.</p>
220
                    </td>
221
                </tr>
222
              </table>
223
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
224
                <tr id="frheader">
225
                  <td width="3%" class="list">&nbsp;</td>
226
                  <td width="3%" class="list">&nbsp;</td>
227
                  <td width="10%" class="listhdrr">Interface</td>
228
                  <td width="15%" class="listhdrr">Source</td>
229
                  <td width="10%" class="listhdrr">Source Port</td>
230
                  <td width="15%" class="listhdrr">Destination</td>
231
                  <td width="10%" class="listhdrr">Destination Port</td>
232
                  <td width="15%" class="listhdrr">NAT Address</td>
233
                  <td width="10%" class="listhdrr">NAT Port</td>
234
		  <td width="10%" class="listhdrr">Static Port</td>
235
                  <td width="25%" class="listhdr">Description</td>
236
                  <td width="5%" class="list">
237
                    <table border="0" cellspacing="0" cellpadding="1">
238
                      <tr>
239
			<td width="17"></td>
240
                        <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>
241
                      </tr>
242
                    </table>
243
		  </td>
244
                </tr>
245
              <?php $nnats = $i = 0; foreach ($a_out as $natent): ?>
246
                <tr valign="top" id="fr<?=$nnats;?>">
247
                  <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>
248
                  <td class="listt" align="center"></td>
249
                  <td class="listlr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
250
                    <?php
251
					if (!$natent['interface'] || ($natent['interface'] == "wan"))
252
					  	echo "WAN";
253
                                        else if (!$natent['interface'] || ($natent['interface'] == "lan"))
254
                                                 echo "LAN";                                                
255
					else
256
						echo htmlspecialchars($config['interfaces'][$natent['interface']]['descr']);
257
					?>
258
                                        &nbsp;
259
                  </td>
260
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
261
                    <?=$natent['source']['network'];?>
262
                  </td>
263
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
264
                    <?php
265
                      if (!$natent['sourceport'])
266
                          echo "*";
267
                      else
268
                          echo $natent['sourceport'];
269
                    ?>
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 (isset($natent['destination']['any']))
274
                          echo "*";
275
                      else {
276
                          if (isset($natent['destination']['not']))
277
                              echo "!&nbsp;";
278
                          echo $natent['destination']['address'];
279
                      }
280
                    ?>
281
                  </td>
282
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
283
                    <?php
284
                      if (!$natent['dstport'])
285
                          echo "*";
286
                      else
287
                          echo $natent['dstport'];
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['target'])
293
                          echo "*";
294
                      else
295
                          echo $natent['target'];
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['natport'])
301
                          echo "*";
302
                      else
303
                          echo $natent['natport'];
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(isset($natent['staticnatport']))
309
			    echo "<CENTER>YES</CENTER>";
310
			else
311
			    echo "<CENTER>NO</CENTER>";
312
                    ?>		    
313
                  </td>
314
                  <td class="listbg"  onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
315
                    <font color="#FFFFFF"><?=htmlspecialchars($natent['descr']);?>&nbsp;
316
                  </td>
317
                  <td class="list" valign="middle" nowrap>
318
                    <table border="0" cellspacing="0" cellpadding="1">
319
                      <tr>
320
                        <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>
321
                      </tr>
322
                      <tr>
323
                        <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>
324
                        <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>
325
                      </tr>
326
                    </table>
327
              <?php $i++; $nnats++; endforeach; ?>
328
                <tr>
329
                  <td class="list" colspan="11"></td>
330
                  <td class="list" valign="middle" nowrap>
331
                    <table border="0" cellspacing="0" cellpadding="1">
332
                      <tr>
333
                        <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>
334
                        <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>
335
                      </tr>
336
                      <tr>
337
                        <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>
338
                      </tr>
339
                    </table></td>
340
                </tr>
341
              </table>
342
	    </div>
343
</td>
344
  </tr>
345
</table>
346
            </form>
347
<?php include("fend.inc"); ?>
348
</body>
349
</html>
(46-46/197)