Project

General

Profile

Download (15.8 KB) Statistics
| Branch: | Tag: | Revision:
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
                        $natent = array();
99
                        $osn = convert_ip_to_network_format($config['interfaces'][$if]['ipaddr'],
100
                                $config['interfaces'][$if]['subnet']);
101
                        $natent['source']['network'] = $osn;
102
                        $natent['sourceport'] = "";
103
                        $natent['descr'] = "Auto created rule for {$if}";
104
                        $natent['target'] = "";
105
                        $natent['interface'] = "wan";
106
                        $natent['destination']['any'] = true;
107
                        $natent['natport'] = "";
108
                        $a_out[] = $natent;
109
                }
110
                $savemsg = "Default rules for each interface have been created.";
111
        }
112
        write_config();
113
        touch($d_natconfdirty_path);
114
        header("Location: firewall_nat_out.php");
115
        exit;
116
}
117

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

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

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

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

    
157
                /* copy $movebtn rule */
158
                if ($movebtn < count($a_out))
159
                        $a_out_new[] = $a_out[$movebtn];
160

    
161
                /* copy all rules > $movebtn and not selected */
162
                for ($i = $movebtn+1; $i < count($a_out); $i++) {
163
                        if (!in_array($i, $_POST['rule']))
164
                                $a_out_new[] = $a_out[$i];
165
                }
166
                $a_out = $a_out_new;
167
                write_config();
168
                touch($d_natconfdirty_path);
169
                header("Location: firewall_nat_out.php");
170
                exit;
171
        }
172
}
173

    
174

    
175
$pgtitle = "Firewall: NAT: Outbound";
176
include("head.inc");
177

    
178
?>
179
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
180
<?php include("fbegin.inc"); ?>
181
<p class="pgtitle">Firewall: NAT: Outbound</p>
182
<form action="firewall_nat_out.php" method="post" name="iform">
183
<script type="text/javascript" language="javascript" src="row_toggle.js">
184
</script>
185
<?php if ($savemsg) print_info_box($savemsg); ?>
186
<?php if (file_exists($d_natconfdirty_path)): ?><p>
187
<?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>
188
<?php endif; ?>
189
<table width="100%" border="0" cellpadding="0" cellspacing="0">  <tr><td>
190
<?php
191
	$tab_array = array();
192
	$tab_array[] = array("Port Forward", false, "firewall_nat.php");
193
	$tab_array[] = array("1:1", false, "firewall_nat_1to1.php");
194
	$tab_array[] = array("Outbound", true, "firewall_nat_out.php");
195
	display_top_tabs($tab_array);
196
?>
197
  </td></tr>
198
  <tr>
199
    <td>
200
	<div id="mainarea">
201
              <table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
202
              <tr>
203
                  <td class="vtable"><p>
204
                      <input name="ipsecpassthru" type="checkbox" id="ipsecpassthru" value="yes" onClick="document.iform.advancedoutbound.checked=false" <?php if (isset($config['nat']['ipsecpassthru']['enable'])) echo "checked";?>>
205
                      <strong>Enable IPSec passthru</strong></p>
206
                  </td>
207
                </tr>
208
                <tr>
209
                  <td class="vtable"><p>
210
                      <input name="advancedoutbound" type="checkbox" id="advancedoutbound" value="yes" onClick="document.iform.ipsecpassthru.checked=false" <?php if (isset($config['nat']['advancedoutbound']['enable'])) echo "checked";?>>
211
                      <strong>Enable advanced outbound NAT</strong></p></td>
212
                </tr>
213
                <tr>
214
                  <td> <input name="save" type="submit" class="formbtn" value="Save">
215
                  </td>
216
                </tr>
217
                <tr>
218
                  <td colspan="2"><p><span class="vexpl"><span class="red"><strong>Note:<br>
219
                      </strong></span>If advanced outbound NAT is enabled, no outbound NAT
220
                      rules will be automatically generated anymore. Instead, only the mappings
221
                      you specify below will be used. With advanced outbound NAT disabled,
222
                      a mapping is automatically created for each interface's subnet
223
                      (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>
224
                      <br>
225
                      You may enter your own mappings below.</p>
226
                    </td>
227
                </tr>
228
              </table>
229
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
230
                <tr id="frheader">
231
                  <td width="3%" class="list">&nbsp;</td>
232
                  <td width="3%" class="list">&nbsp;</td>
233
                  <td width="10%" class="listhdrr">Interface</td>
234
                  <td width="20%" class="listhdrr">Source</td>
235
                  <td width="20%" class="listhdrr">Source Port</td>
236
                  <td width="20%" class="listhdrr">Destination</td>
237
                  <td width="20%" class="listhdrr">NAT Port</td>
238
                  <td width="20%" class="listhdrr">Target</td>
239
                  <td width="25%" class="listhdr">Description</td>
240
                  <td width="5%" class="list"></td>
241
                </tr>
242
              <?php $nnats = $i = 0; foreach ($a_out as $natent): ?>
243
                <tr valign="top" id="fr<?=$nnats;?>">
244
                  <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>
245
                  <td class="listt" align="center"></td>
246
                  <td class="listlr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
247
                    <?php
248
					if (!$natent['interface'] || ($natent['interface'] == "wan"))
249
					  	echo "WAN";
250
					else
251
						echo htmlspecialchars($config['interfaces'][$natent['interface']]['descr']);
252
					?>
253
                                        &nbsp;
254
                  </td>
255
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
256
                    <?=$natent['source']['network'];?>
257
                  </td>
258
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
259
                    <?php
260
                      if (!$natent['sourceport'])
261
                          echo "*";
262
                      else
263
                          echo $natent['sourceport'];
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
                    <?php
268
                      if (isset($natent['destination']['any']))
269
                          echo "*";
270
                      else {
271
                          if (isset($natent['destination']['not']))
272
                              echo "!&nbsp;";
273
                          echo $natent['destination']['network'];
274
                      }
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 (!$natent['natport'])
280
                          echo "*";
281
                      else
282
                          echo $natent['natport'];
283
                    ?>
284
                  </td>
285
                  <td class="listr" onClick="fr_toggle(<?=$nnats;?>)" id="frd<?=$nnats;?>" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
286
                    <?php
287
                      if (!$natent['target'])
288
                          echo "*";
289
                      else
290
                          echo $natent['target'];
291
                    ?>
292
                  </td>
293
                  <td class="listbg"  onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_out_edit.php?id=<?=$nnats;?>';">
294
                    <font color="#FFFFFF"><?=htmlspecialchars($natent['descr']);?>&nbsp;
295
                  </td>
296
                  <td class="list" valign="middle" nowrap>
297
                    <table border="0" cellspacing="0" cellpadding="1">
298
                      <tr>
299
                        <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"></a></td>
300
                      </tr>
301
                      <tr>
302
                        <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>
303
                        <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>
304
                      </tr>
305
                    </table>
306
              <?php $i++; $nnats++; endforeach; ?>
307
                <tr>
308
                  <td class="list" colspan="9"></td>
309
                  <td class="list" valign="middle" nowrap>
310
                    <table border="0" cellspacing="0" cellpadding="1">
311
                      <tr>
312
                        <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>
313
                        <td><a href="firewall_nat_out_edit.php"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0"></a></td>
314
                      </tr>
315
                      <tr>
316
                        <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>
317
                      </tr>
318
                    </table></td>
319
                </tr>
320
              </table>
321
	    </div>
322
</td>
323
  </tr>
324
</table>
325
            </form>
326
<?php include("fend.inc"); ?>
327
</body>
328
</html>
(37-37/144)