Project

General

Profile

Download (17 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
<<<<<<< firewall_nat_out.php
3
/* $Id$ */
4
=======
5
/* $Id$ */
6
>>>>>>> 1.47.2.16.2.1
7
/*
8
    firewall_nat_out.php
9
    Copyright (C) 2004 Scott Ullrich
10
    All rights reserved.
11

    
12
    originally part of m0n0wall (http://m0n0.ch/wall)
13
    Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
14
    All rights reserved.
15

    
16
    Redistribution and use in source and binary forms, with or without
17
    modification, are permitted provided that the following conditions are met:
18

    
19
    1. Redistributions of source code must retain the above copyright notice,
20
       this list of conditions and the following disclaimer.
21

    
22
    2. Redistributions in binary form must reproduce the above copyright
23
       notice, this list of conditions and the following disclaimer in the
24
       documentation and/or other materials provided with the distribution.
25

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

    
38
require("guiconfig.inc");
39

    
40
if (!is_array($config['nat']['advancedoutbound']['rule']))
41
	$config['nat']['advancedoutbound']['rule'] = array();
42

    
43
$a_out = &$config['nat']['advancedoutbound']['rule'];
44

    
45

    
46
if ($_POST['apply']) {
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
		unlink_if_exists($d_natconfdirty_path);
62
		unlink_if_exists($d_filterconfdirty_path);
63
        }
64
}
65

    
66

    
67

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

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

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

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

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

    
160
                /* copy $movebtn rule */
161
                if ($movebtn < count($a_out))
162
                        $a_out_new[] = $a_out[$movebtn];
163

    
164
                /* copy all rules > $movebtn and not selected */
165
                for ($i = $movebtn+1; $i < count($a_out); $i++) {
166
                        if (!in_array($i, $_POST['rule']))
167
                                $a_out_new[] = $a_out[$i];
168
                }
169
                if (count($a_out_new) > 0)
170
			$a_out = $a_out_new;
171
		else
172
			unset($config['nat']['advancedoutbound']);
173

    
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="advancedoripsec" type="radio" id="ipsecpassthru" value="ipsecpassthru" <?php if (isset($config['nat']['ipsecpassthru']['enable'])) echo "checked";?>>
212
                      <strong><?=gettext("Automatic outbound NAT rule generation (IPSEC passthrough)");?></strong></p>
213
                  </td>
214
                </tr>
215
                <tr>
216
                  <td class="vtable"><p>
217
                      <input name="advancedoripsec" type="radio" id="advancedoutbound" value="advancedoutboundnat" <?php if (isset($config['nat']['advancedoutbound']['enable'])) echo "checked";?>>
218
                      <strong><?=gettext("Manual Outbound NAT rule generation (Advanced Outbound NAT (AON))");?></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">
252
                    <table border="0" cellspacing="0" cellpadding="1">
253
                      <tr>
254
			<td width="17"></td>
255
                        <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>
256
                      </tr>
257
                    </table>
258
		  </td>
259
                </tr>
260
              <?php $nnats = $i = 0; foreach ($a_out as $natent): ?>
261
                <tr valign="top" id="fr<?=$nnats;?>">
262
                  <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>
263
                  <td class="listt" align="center"></td>
264
                  <td class="listlr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
265
                    <?php
266
					if (!$natent['interface'] || ($natent['interface'] == "wan"))
267
					  	echo "WAN";
268
                                        else if (!$natent['interface'] || ($natent['interface'] == "lan"))
269
                                                 echo "LAN";                                                
270
					else
271
						echo htmlspecialchars($config['interfaces'][$natent['interface']]['descr']);
272
					?>
273
                                        &nbsp;
274
                  </td>
275
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
276
                    <?=$natent['source']['network'];?>
277
                  </td>
278
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
279
                    <?php
280
                      if (!$natent['sourceport'])
281
                          echo "*";
282
                      else
283
                          echo $natent['sourceport'];
284
                    ?>
285
                  </td>
286
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
287
                    <?php
288
                      if (isset($natent['destination']['any']))
289
                          echo "*";
290
                      else {
291
                          if (isset($natent['destination']['not']))
292
                              echo "!&nbsp;";
293
                          echo $natent['destination']['address'];
294
                      }
295
                    ?>
296
                  </td>
297
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
298
                    <?php
299
                      if (!$natent['dstport'])
300
                          echo "*";
301
                      else
302
                          echo $natent['dstport'];
303
                    ?>
304
                  </td>
305
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
306
                    <?php
307
                      if (!$natent['target'])
308
                          echo "*";
309
                      else
310
                          echo $natent['target'];
311
                    ?>
312
                  </td>
313
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
314
                    <?php
315
                      if (!$natent['natport'])
316
                          echo "*";
317
                      else
318
                          echo $natent['natport'];
319
                    ?>
320
                  </td>
321
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
322
                    <?php
323
			if(isset($natent['staticnatport']))
324
			    echo "<CENTER>YES</CENTER>";
325
			else
326
			    echo "<CENTER>NO</CENTER>";
327
                    ?>		    
328
                  </td>
329
                  <td class="listbg"  onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
330
                    <font color="#FFFFFF"><?=htmlspecialchars($natent['descr']);?>&nbsp;
331
                  </td>
332
                  <td class="list" valign="middle" nowrap>
333
                    <table border="0" cellspacing="0" cellpadding="1">
334
                      <tr>
335
                        <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>
336
                      </tr>
337
                      <tr>
338
                        <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>
339
                        <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>
340
                      </tr>
341
                    </table>
342
              <?php $i++; $nnats++; endforeach; ?>
343
                <tr>
344
                  <td class="list" colspan="11"></td>
345
                  <td class="list" valign="middle" nowrap>
346
                    <table border="0" cellspacing="0" cellpadding="1">
347
                      <tr>
348
                        <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>
349
                        <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>
350
                      </tr>
351
                      <tr>
352
                        <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>
353
                      </tr>
354
                    </table></td>
355
                </tr>
356
              </table>
357
	    </div>
358
</td>
359
  </tr>
360
</table>
361
            </form>
362
<?php include("fend.inc"); ?>
363
</body>
364
</html>
(44-44/171)