Project

General

Profile

Download (15 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
$a_ipsec = &$config['ipsec']['tunnel'];
58
if(is_array($a_ipsec)) {
59
	$i = 0; foreach ($a_ipsec as $ipsecent) {
60
		if(isset($ipsecent['creategif'])) {
61
			$iflist["gif{$i}"] = "{$ipsecent['descr']}";
62
			$i++;
63
		}
64
	}
65
}
66

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

    
71
if (!$if || !isset($iflist[$if]))
72
	$if = "wan";
73

    
74
if ($_POST) {
75

    
76
	$pconfig = $_POST;
77

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

    
84
		if (file_exists($d_filterconfdirty_path))
85
			unlink($d_filterconfdirty_path);
86

    
87
		$savemsg = "The settings have been applied";
88
	}
89
}
90

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

    
127
		/* copy all rules < $movebtn and not selected */
128
		for ($i = 0; $i < $movebtn; $i++) {
129
			if (!in_array($i, $_POST['rule']))
130
				$a_filter_new[] = $a_filter[$i];
131
		}
132

    
133
		/* copy all selected rules */
134
		for ($i = 0; $i < count($a_filter); $i++) {
135
			if ($i == $movebtn)
136
				continue;
137
			if (in_array($i, $_POST['rule']))
138
				$a_filter_new[] = $a_filter[$i];
139
		}
140

    
141
		/* copy $movebtn rule */
142
		if ($movebtn < count($a_filter))
143
			$a_filter_new[] = $a_filter[$movebtn];
144

    
145
		/* copy all rules > $movebtn and not selected */
146
		for ($i = $movebtn+1; $i < count($a_filter); $i++) {
147
			if (!in_array($i, $_POST['rule']))
148
				$a_filter_new[] = $a_filter[$i];
149
		}
150

    
151
		$a_filter = $a_filter_new;
152
		write_config();
153
		touch($d_filterconfdirty_path);
154
		header("Location: firewall_rules.php?if={$if}");
155
		exit;
156
	}
157
}
158

    
159
$pgtitle = "Firewall: Rules";
160
include("head.inc");
161

    
162
?>
163
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
164
<?php include("fbegin.inc"); ?>
165
<p class="pgtitle"><?=$pgtitle?></p>
166
<form action="firewall_rules.php" method="post">
167
<script type="text/javascript" language="javascript" src="row_toggle.js">
168
</script>
169
<?php if ($savemsg) print_info_box($savemsg); ?>
170
<?php if (file_exists($d_filterconfdirty_path)): ?><p>
171
<?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>
172
<?php endif; ?>
173
<table width="100%" border="0" cellpadding="0" cellspacing="0">
174
  <tr><td class="tabnavtbl">
175
  <?php
176
	/* active tabs */
177
	$tab_array = array();
178
	$tabscounter = 0; $i = 0; foreach ($iflist as $ifent => $ifname) {
179
		if ($ifent == $if)
180
			$active = true;
181
		else
182
			$active = false;
183
		$tab_array[] = array($ifname, $active, "firewall_rules.php?if={$ifent}");
184
	}
185
	display_top_tabs($tab_array);
186
  ?>
187
  </td></tr>
188
  <tr>
189
    <td>
190
	<div id="mainarea">
191
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
192
                <tr id="frheader">
193
                  <td width="3%" class="list">&nbsp;</td>
194
                  <td width="5%" class="list">&nbsp;</td>
195
                  <td width="10%" class="listhdrr">Proto</td>
196
                  <td width="15%" class="listhdrr">Source</td>
197
                  <td width="10%" class="listhdrr">Port</td>
198
                  <td width="15%" class="listhdrr">Destination</td>
199
                  <td width="10%" class="listhdrr">Port</td>
200
                  <td width="22%" class="listhdr">Description</td>
201
                  <td width="10%" class="list"></td>
202
				</tr>
203
				<?php $nrules = 0; for ($i = 0; isset($a_filter[$i]); $i++):
204
					$filterent = $a_filter[$i];
205
					if ($filterent['interface'] != $if)
206
						continue;
207
				?>
208
                <tr valign="top" id="fr<?=$nrules;?>">
209
                  <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>
210
                  <td class="listt" align="center">
211
				  <?php if ($filterent['type'] == "block")
212
				  			$iconfn = "block";
213
						else if ($filterent['type'] == "reject") {
214
							if ($filterent['protocol'] == "tcp" || $filterent['protocol'] == "udp" || $filterent['protocol'] == "tcp/udp")
215
								$iconfn = "reject";
216
							else
217
								$iconfn = "block";
218
						} else
219
							$iconfn = "pass";
220
						if (isset($filterent['disabled'])) {
221
							$textss = "<span class=\"gray\">";
222
							$textse = "</span>";
223
							$iconfn .= "_d";
224
						} else {
225
							$textss = $textse = "";
226
						}
227
				  ?>
228
				  <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>
229
				  <?php if (isset($filterent['log'])):
230
							$iconfn = "log_s";
231
						if (isset($filterent['disabled']))
232
							$iconfn .= "_d";
233
				  	?>
234
				  <br><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_<?=$iconfn;?>.gif" width="11" height="15" border="0">
235
				  <?php endif; ?>
236
				  </td>
237
                  <td class="listlr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';">
238
                    <?=$textss;?><?php if (isset($filterent['protocol'])) echo strtoupper($filterent['protocol']); else echo "*"; ?><?=$textse;?>
239
                  </td>
240
                  <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';">
241
				    <?=$textss;?><?php echo htmlspecialchars(pprint_address($filterent['source'])); ?><?=$textse;?>
242
                  </td>
243
                  <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';">
244
                    <?=$textss;?><?php echo htmlspecialchars(pprint_port($filterent['source']['port'])); ?><?=$textse;?>
245
                  </td>
246
                  <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';">
247
				    <?=$textss;?><?php echo htmlspecialchars(pprint_address($filterent['destination'])); ?><?=$textse;?>
248
                  </td>
249
                  <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';">
250
                    <?=$textss;?><?php echo htmlspecialchars(pprint_port($filterent['destination']['port'])); ?><?=$textse;?>
251
                  </td>
252
                  <td class="listbg" onClick="fr_toggle(<?=$nrules;?>)" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';" bcolor="#990000"><font color="white">
253
                    <?=$textss;?><?=htmlspecialchars($filterent['descr']);?>&nbsp;<?=$textse;?>
254
                  </td>
255
                  <td valign="middle" nowrap class="list">
256
				    <table border="0" cellspacing="0" cellpadding="1">
257
					<tr>
258
					  <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>
259
					  <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>
260
					</tr>
261
					<tr>
262
					  <td align="center" valign="middle"></td>
263
					  <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>
264
					</tr>
265
					</table>
266
				  </td>
267
				</tr>
268
			  <?php $nrules++; endfor; ?>
269
			  <?php if ($nrules == 0): ?>
270
              <td class="listt"></td>
271
			  <td class="listt"></td>
272
			  <td class="listlr" colspan="6" align="center" valign="middle">
273
			  <span class="gray">
274
			  No rules are currently defined for this interface.<br>
275
			  All incoming connections on this interface will be blocked until you add pass rules.<br><br>
276
			  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>
277
			  </td>
278
			  <?php endif; ?>
279
                <tr id="fr<?=$nrules;?>">
280
                  <td class="list"></td>
281
                  <td class="list"></td>
282
                  <td class="list">&nbsp;</td>
283
                  <td class="list">&nbsp;</td>
284
                  <td class="list">&nbsp;</td>
285
                  <td class="list">&nbsp;</td>
286
                  <td class="list">&nbsp;</td>
287
                  <td class="list">&nbsp;</td>
288
                  <td class="list">
289
				    <table border="0" cellspacing="0" cellpadding="1">
290
					<tr>
291
				      <td>
292
					  <?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>
293
					  <td></td>
294
				    </tr>
295
					<tr>
296
					  <td><?php if ($nrules == 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 rules" onclick="return confirm('Do you really want to delete the selected rules?')"><?php endif; ?></td>
297
					  <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>
298
					</tr>
299
				    </table>
300
				  </td>
301
				</tr>
302
              </table>
303
	      <table class="tabcont" width="100%" border="0" cellspacing="0" cellpadding="0">
304
                <tr>
305
                  <td width="16"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_pass.gif" width="11" height="11"></td>
306
                  <td>pass</td>
307
                  <td width="14"></td>
308
                  <td width="16"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_block.gif" width="11" height="11"></td>
309
                  <td>block</td>
310
                  <td width="14"></td>
311
                  <td width="16"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_reject.gif" width="11" height="11"></td>
312
                  <td>reject</td>
313
                  <td width="14"></td>
314
                  <td width="16"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_log.gif" width="11" height="11"></td>
315
                  <td>log</td>
316
                </tr>
317
                <tr>
318
                  <td><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_pass_d.gif" width="11" height="11"></td>
319
                  <td nowrap>pass (disabled)</td>
320
                  <td>&nbsp;</td>
321
                  <td><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_block_d.gif" width="11" height="11"></td>
322
                  <td nowrap>block (disabled)</td>
323
                  <td>&nbsp;</td>
324
                  <td><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_reject_d.gif" width="11" height="11"></td>
325
                  <td nowrap>reject (disabled)</td>
326
                  <td>&nbsp;</td>
327
                  <td width="16"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_log_d.gif" width="11" height="11"></td>
328
                  <td nowrap>log (disabled)</td>
329
                </tr>
330
		<tr>
331
		  <td colspan="9">
332
  <p>
333
  <strong><span class="red">Hint:<br>
334
  </span></strong>Rules are evaluated on a first-match basis (i.e.
335
  the action of the first rule to match a packet will be executed).
336
  This means that if you use block rules, you'll have to pay attention
337
  to the rule order. Everything that isn't explicitly passed is blocked
338
  by default.</p>
339
		 </td>
340
	        </tr>
341
              </table>
342
	</div>
343
    </td>
344
  </tr>
345
</table>
346
  <input type="hidden" name="if" value="<?=$if;?>">
347
</form>
348
<?php include("fend.inc"); ?>
349
</body>
350
</html>
(41-41/144)