Project

General

Profile

Download (16.1 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/* $Id$ */
4
/*
5
	firewall_rules.php
6
	part of pfSense (http://www.pfsense.com)
7
        Copyright (C) 2005 Scott Ullrich (sullrich@gmail.com)
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
$pgtitle = array("Firewall", "Rules");
36
require("guiconfig.inc");
37

    
38
if (!is_array($config['filter']['rule'])) {
39
	$config['filter']['rule'] = array();
40
}
41
filter_rules_sort();
42
$a_filter = &$config['filter']['rule'];
43

    
44
$if = $_GET['if'];
45
if ($_POST['if'])
46
	$if = $_POST['if'];
47

    
48
$iflist = array("lan" => "LAN", "wan" => "WAN");
49

    
50
if ($config['pptpd']['mode'] == "server")
51
	$iflist['pptp'] = "PPTP VPN";
52

    
53
if ($config['pppoe']['mode'] == "server")
54
	$iflist['pppoe'] = "PPPoE VPN";
55

    
56
/* add ipsec filter gif interfaces */
57
if (is_array($config['ipsec']['tunnel']) && isset($config['ipsec']['enable'])) {
58
	$a_ipsec = &$config['ipsec']['tunnel'];
59
	if(is_array($a_ipsec)) {
60
		$i = 0; foreach ($a_ipsec as $ipsecent) {
61
			if(isset($ipsecent['creategif'])) {
62
				$iflist["gif{$i}"] = "{$ipsecent['descr']}";
63
				$i++;
64
			}
65
		}
66
	}
67
}
68

    
69
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
70
	$iflist['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];
71
}
72

    
73
if (!$if || !isset($iflist[$if]))
74
	$if = "wan";
75

    
76
if ($_POST) {
77

    
78
	$pconfig = $_POST;
79

    
80
	if ($_POST['apply']) {
81
		$retval = 0;
82
		config_lock();
83
		$retval = filter_configure();
84
		config_unlock();
85

    
86
		if (file_exists($d_filterconfdirty_path))
87
			unlink($d_filterconfdirty_path);
88

    
89
		$savemsg = "The settings have been applied";
90
	}
91
}
92

    
93
if ($_GET['act'] == "del") {
94
        if ($a_filter[$_GET['id']]) {
95
                unset($a_filter[$_GET['id']]);
96
                write_config();
97
                touch($d_filterconfdirty_path);
98
                header("Location: firewall_rules.php?if={$if}");
99
                exit;
100
        }
101
}
102

    
103
if (isset($_POST['del_x'])) {
104
	/* delete selected rules */
105
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
106
		foreach ($_POST['rule'] as $rulei) {
107
			unset($a_filter[$rulei]);
108
		}
109
		write_config();
110
		touch($d_filterconfdirty_path);
111
		header("Location: firewall_rules.php?if={$if}");
112
		exit;
113
	}
114
} else if ($_GET['act'] == "toggle") {
115
	if ($a_filter[$_GET['id']]) {
116
                if(isset($a_filter[$_GET['id']]['disabled']))
117
                        unset($a_filter[$_GET['id']]['disabled']);
118
                else
119
                        $a_filter[$_GET['id']]['disabled'] = true;
120
		write_config();
121
		touch($d_filterconfdirty_path);
122
		header("Location: firewall_rules.php?if={$if}");
123
		exit;
124
	}
125
} else {
126
	/* yuck - IE won't send value attributes for image buttons, while Mozilla does -
127
	   so we use .x/.y to fine move button clicks instead... */
128
	unset($movebtn);
129
	foreach ($_POST as $pn => $pd) {
130
		if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
131
			$movebtn = $matches[1];
132
			break;
133
		}
134
	}
135
	/* move selected rules before this rule */
136
	if (isset($movebtn) && is_array($_POST['rule']) && count($_POST['rule'])) {
137
		$a_filter_new = array();
138

    
139
		/* copy all rules < $movebtn and not selected */
140
		for ($i = 0; $i < $movebtn; $i++) {
141
			if (!in_array($i, $_POST['rule']))
142
				$a_filter_new[] = $a_filter[$i];
143
		}
144

    
145
		/* copy all selected rules */
146
		for ($i = 0; $i < count($a_filter); $i++) {
147
			if ($i == $movebtn)
148
				continue;
149
			if (in_array($i, $_POST['rule']))
150
				$a_filter_new[] = $a_filter[$i];
151
		}
152

    
153
		/* copy $movebtn rule */
154
		if ($movebtn < count($a_filter))
155
			$a_filter_new[] = $a_filter[$movebtn];
156

    
157
		/* copy all rules > $movebtn and not selected */
158
		for ($i = $movebtn+1; $i < count($a_filter); $i++) {
159
			if (!in_array($i, $_POST['rule']))
160
				$a_filter_new[] = $a_filter[$i];
161
		}
162

    
163
		$a_filter = $a_filter_new;
164
		write_config();
165
		touch($d_filterconfdirty_path);
166
		header("Location: firewall_rules.php?if={$if}");
167
		exit;
168
	}
169
}
170

    
171
$pgtitle = "Firewall: Rules";
172
include("head.inc");
173

    
174
?>
175
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
176
<?php include("fbegin.inc"); ?>
177
<p class="pgtitle"><?=$pgtitle?></p>
178
<form action="firewall_rules.php" method="post">
179
<script type="text/javascript" language="javascript" src="row_toggle.js">
180
</script>
181
<?php if ($savemsg) print_info_box($savemsg); ?>
182
<?php if (file_exists($d_filterconfdirty_path)): ?><p>
183
<?php print_info_box_np("The firewall rule configuration has been changed.<br>You must apply the changes in order for them to take effect.");?><br>
184
<?php endif; ?>
185
<table width="100%" border="0" cellpadding="0" cellspacing="0">
186
  <tr><td class="tabnavtbl">
187
  <?php
188
	/* active tabs */
189
	$tab_array = array();
190
	$tabscounter = 0; $i = 0; foreach ($iflist as $ifent => $ifname) {
191
		if ($ifent == $if)
192
			$active = true;
193
		else
194
			$active = false;
195
		$tab_array[] = array($ifname, $active, "firewall_rules.php?if={$ifent}");
196
	}
197
	display_top_tabs($tab_array);
198
  ?>
199
  </td></tr>
200
  <tr>
201
    <td>
202
	<div id="mainarea">
203
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
204
                <tr id="frheader">
205
                  <td width="3%" class="list">&nbsp;</td>
206
                  <td width="5%" class="list">&nbsp;</td>
207
                  <td width="10%" class="listhdrr">Proto</td>
208
                  <td width="15%" class="listhdrr">Source</td>
209
                  <td width="10%" class="listhdrr">Port</td>
210
                  <td width="15%" class="listhdrr">Destination</td>
211
                  <td width="10%" class="listhdrr">Port</td>
212
		  <td width="10%" class="listhdrr">Gateway</td>
213
                  <td width="22%" class="listhdr">Description</td>
214
                  <td width="10%" class="list"></td>
215
				</tr>
216
				<?php $nrules = 0; for ($i = 0; isset($a_filter[$i]); $i++):
217
					$filterent = $a_filter[$i];
218
					if ($filterent['interface'] != $if)
219
						continue;
220
				?>
221
                <tr valign="top" id="fr<?=$nrules;?>">
222
                  <td class="listt"><input type="checkbox" id="frc<?=$nrules;?>" name="rule[]" value="<?=$i;?>" onClick="fr_bgcolor('<?=$nrules;?>')" style="margin: 0; padding: 0; width: 15px; height: 15px;"></td>
223
                  <td class="listt" align="center">
224
				  <?php if ($filterent['type'] == "block")
225
				  			$iconfn = "block";
226
						else if ($filterent['type'] == "reject") {
227
							if ($filterent['protocol'] == "tcp" || $filterent['protocol'] == "udp" || $filterent['protocol'] == "tcp/udp")
228
								$iconfn = "reject";
229
							else
230
								$iconfn = "block";
231
						} else
232
							$iconfn = "pass";
233
						if (isset($filterent['disabled'])) {
234
							$textss = "<span class=\"gray\">";
235
							$textse = "</span>";
236
							$iconfn .= "_d";
237
						} else {
238
							$textss = $textse = "";
239
						}
240
				  ?>
241
				  <a href="?if=<?=$if;?>&act=toggle&id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_<?=$iconfn;?>.gif" width="11" height="11" border="0" title="click to toggle enabled/disabled status"></a>
242
				  <?php if (isset($filterent['log'])):
243
							$iconfn = "log_s";
244
						if (isset($filterent['disabled']))
245
							$iconfn .= "_d";
246
				  	?>
247
				  <br><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_<?=$iconfn;?>.gif" width="11" height="15" border="0">
248
				  <?php endif; ?>
249
				  </td>
250
                  <td class="listlr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';">
251
                    <?=$textss;?><?php if (isset($filterent['protocol'])) echo strtoupper($filterent['protocol']); else echo "*"; ?><?=$textse;?>
252
                  </td>
253
                  <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';">
254
				    <?=$textss;?><?php echo htmlspecialchars(pprint_address($filterent['source'])); ?><?=$textse;?>
255
                  </td>
256
                  <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';">
257
                    <?=$textss;?><?php echo htmlspecialchars(pprint_port($filterent['source']['port'])); ?><?=$textse;?>
258
                  </td>
259
                  <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';">
260
				    <?=$textss;?><?php echo htmlspecialchars(pprint_address($filterent['destination'])); ?><?=$textse;?>
261
                  </td>
262
                  <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';">
263
                    <?=$textss;?><?php echo htmlspecialchars(pprint_port($filterent['destination']['port'])); ?><?=$textse;?>
264
                  </td>
265

    
266
                  <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';">
267
                    <?=$textss;?><?php if (isset($config['interfaces'][$filterent['gateway']]['descr'])) echo htmlspecialchars($config['interfaces'][$filterent['gateway']]['descr']); else  echo htmlspecialchars(pprint_port($filterent['gateway'])); ?><?=$textse;?>
268
                  </td>
269

    
270
                  <td class="listbg" onClick="fr_toggle(<?=$nrules;?>)" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';" bcolor="#990000"><font color="white">
271
                    <?=$textss;?><?=htmlspecialchars($filterent['descr']);?>&nbsp;<?=$textse;?>
272
                  </td>
273
                  <td valign="middle" nowrap class="list">
274
				    <table border="0" cellspacing="0" cellpadding="1">
275
					<tr>
276
					  <td><input name="move_<?=$i;?>" type="image" src="./themes/<?= $g['theme']; ?>/images/icons/icon_left.gif" width="17" height="17" title="move selected rules before this rule" onMouseOver="fr_insline(<?=$nrules;?>, true)" onMouseOut="fr_insline(<?=$nrules;?>, false)"></td>
277
					  <td><a href="firewall_rules_edit.php?id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" title="edit rule" width="17" height="17" border="0"></a></td>
278
					</tr>
279
					<tr>
280
					  <td align="center" valign="middle"><a href="firewall_rules.php?act=del&id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" border="0" title="delete rule" onclick="return confirm('Do you really want to delete this rule?')"></a></td>
281
					  <td><a href="firewall_rules_edit.php?dup=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="add a new rule based on this one" width="17" height="17" border="0"></a></td>
282
					</tr>
283
					</table>
284
				  </td>
285
				</tr>
286
			  <?php $nrules++; endfor; ?>
287
			  <?php if ($nrules == 0): ?>
288
              <td class="listt"></td>
289
			  <td class="listt"></td>
290
			  <td class="listlr" colspan="7" align="center" valign="middle">
291
			  <span class="gray">
292
			  No rules are currently defined for this interface.<br>
293
			  All incoming connections on this interface will be blocked until you add pass rules.<br><br>
294
			  Click the <a href="firewall_rules_edit.php?if=<?=$if;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="add new rule" border="0" width="17" height="17" align="absmiddle"></a> button to add a new rule.</span>
295
			  </td>
296
			  <?php endif; ?>
297
                <tr id="fr<?=$nrules;?>">
298
                  <td class="list"></td>
299
                  <td class="list"></td>
300
                  <td class="list">&nbsp;</td>
301
                  <td class="list">&nbsp;</td>
302
                  <td class="list">&nbsp;</td>
303
		  <td class="list">&nbsp;</td>
304
                  <td class="list">&nbsp;</td>
305
                  <td class="list">&nbsp;</td>
306
                  <td class="list">&nbsp;</td>
307
                  <td class="list">
308
				    <table border="0" cellspacing="0" cellpadding="1">
309
					<tr>
310
				      <td>
311
					  <?php if ($nrules == 0): ?><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_left_d.gif" width="17" height="17" title="move selected rules 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 rules to end" onMouseOver="fr_insline(<?=$nrules;?>, true)" onMouseOut="fr_insline(<?=$nrules;?>, false)"><?php endif; ?></td>
312
					  <td></td>
313
				    </tr>
314
					<tr>
315
					  <td>
316
					  <?php if ($nrules == 0): ?>
317
					  <img src="./themes/<?= $g['theme']; ?>/images/icons/icon_x_d.gif" width="17" height="17" title="delete selected rules" border="0"><?php else: ?>
318
					  <input name="del" type="image" src="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" title="delete selected rules" onclick="return confirm('Do you really want to delete the selected rules?')"><?php endif; ?>
319
					  </td>
320
					  <td><a href="firewall_rules_edit.php?if=<?=$if;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="add new rule" width="17" height="17" border="0"></a></td>
321
					</tr>
322
				    </table>
323
				  </td>
324
				</tr>
325
              </table>
326
	      <table class="tabcont" width="100%" border="0" cellspacing="0" cellpadding="0">
327
                <tr>
328
                  <td width="16"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_pass.gif" width="11" height="11"></td>
329
                  <td>pass</td>
330
                  <td width="14"></td>
331
                  <td width="16"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_block.gif" width="11" height="11"></td>
332
                  <td>block</td>
333
                  <td width="14"></td>
334
                  <td width="16"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_reject.gif" width="11" height="11"></td>
335
                  <td>reject</td>
336
                  <td width="14"></td>
337
                  <td width="16"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_log.gif" width="11" height="11"></td>
338
                  <td>log</td>
339
                </tr>
340
                <tr>
341
                  <td><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_pass_d.gif" width="11" height="11"></td>
342
                  <td nowrap>pass (disabled)</td>
343
                  <td>&nbsp;</td>
344
                  <td><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_block_d.gif" width="11" height="11"></td>
345
                  <td nowrap>block (disabled)</td>
346
                  <td>&nbsp;</td>
347
                  <td><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_reject_d.gif" width="11" height="11"></td>
348
                  <td nowrap>reject (disabled)</td>
349
                  <td>&nbsp;</td>
350
                  <td width="16"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_log_d.gif" width="11" height="11"></td>
351
                  <td nowrap>log (disabled)</td>
352
                </tr>
353
		<tr>
354
		  <td colspan="10">
355
  <p>
356
  <strong><span class="red">Hint:<br>
357
  </span></strong>Rules are evaluated on a first-match basis (i.e.
358
  the action of the first rule to match a packet will be executed).
359
  This means that if you use block rules, you'll have to pay attention
360
  to the rule order. Everything that isn't explicitly passed is blocked
361
  by default.</p>
362
		 </td>
363
	        </tr>
364
              </table>
365
	</div>
366
    </td>
367
  </tr>
368
</table>
369
  <input type="hidden" name="if" value="<?=$if;?>">
370
</form>
371
<?php include("fend.inc"); ?>
372
</body>
373
</html>
(43-43/146)