Project

General

Profile

Download (16 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
			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
                        $natent = array();
105
                        $osn = convert_ip_to_network_format($config['interfaces'][$if]['ipaddr'],
106
                                $config['interfaces'][$if]['subnet']);
107
                        $natent['source']['network'] = $osn;
108
                        $natent['sourceport'] = "";
109
                        $natent['descr'] = "Auto created rule for {$config['interfaces'][$if]['descr']}";
110
                        $natent['target'] = "";
111
                        $natent['interface'] = "wan";
112
                        $natent['destination']['any'] = true;
113
                        $natent['natport'] = "";
114
                        $a_out[] = $natent;
115
                }
116
                $savemsg = "Default rules for each interface have been created.";
117
        }
118
        write_config();
119
        touch($d_natconfdirty_path);
120
        header("Location: firewall_nat_out.php");
121
        exit;
122
}
123

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

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

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

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

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

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

    
180

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

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