Project

General

Profile

Download (14.5 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/* $Id$ */
4
/*
5
	firewall_rules.php
6
	part of m0n0wall (http://m0n0.ch/wall)
7

    
8
	Copyright (C) 2003-2005 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

    
17
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in the
19
	   documentation and/or other materials provided with the distribution.
20

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

    
33
$pgtitle = array("Firewall", "Rules");
34
require("guiconfig.inc");
35

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

    
42
$if = $_GET['if'];
43
if ($_POST['if'])
44
	$if = $_POST['if'];
45

    
46
$iflist = array("lan" => "LAN", "wan" => "WAN");
47

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

    
51
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
52
	$iflist['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];
53
}
54

    
55
if (!$if || !isset($iflist[$if]))
56
	$if = "wan";
57

    
58
if ($_POST) {
59

    
60
	$pconfig = $_POST;
61

    
62
	if ($_POST['apply']) {
63
		$retval = 0;
64
		config_lock();
65
		$retval = filter_configure();
66
		config_unlock();
67

    
68
		if (file_exists($d_filterconfdirty_path))
69
			unlink($d_filterconfdirty_path);
70

    
71
		$savemsg = "The settings have been applied";
72
	}
73
}
74

    
75
if (isset($_POST['del_x'])) {
76
	/* delete selected rules */
77
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
78
		foreach ($_POST['rule'] as $rulei) {
79
			unset($a_filter[$rulei]);
80
		}
81
		write_config();
82
		touch($d_filterconfdirty_path);
83
		header("Location: firewall_rules.php?if={$if}");
84
		exit;
85
	}
86
} else if ($_GET['act'] == "toggle") {
87
	if ($a_filter[$_GET['id']]) {
88
                if(isset($a_filter[$_GET['id']]['disabled']))
89
                        unset($a_filter[$_GET['id']]['disabled']);
90
                else
91
                        $a_filter[$_GET['id']]['disabled'] = true;
92
		write_config();
93
		touch($d_filterconfdirty_path);
94
		header("Location: firewall_rules.php?if={$if}");
95
		exit;
96
	}
97
} else {
98
	/* yuck - IE won't send value attributes for image buttons, while Mozilla does -
99
	   so we use .x/.y to fine move button clicks instead... */
100
	unset($movebtn);
101
	foreach ($_POST as $pn => $pd) {
102
		if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
103
			$movebtn = $matches[1];
104
			break;
105
		}
106
	}
107
	/* move selected rules before this rule */
108
	if (isset($movebtn) && is_array($_POST['rule']) && count($_POST['rule'])) {
109
		$a_filter_new = array();
110

    
111
		/* copy all rules < $movebtn and not selected */
112
		for ($i = 0; $i < $movebtn; $i++) {
113
			if (!in_array($i, $_POST['rule']))
114
				$a_filter_new[] = $a_filter[$i];
115
		}
116

    
117
		/* copy all selected rules */
118
		for ($i = 0; $i < count($a_filter); $i++) {
119
			if ($i == $movebtn)
120
				continue;
121
			if (in_array($i, $_POST['rule']))
122
				$a_filter_new[] = $a_filter[$i];
123
		}
124

    
125
		/* copy $movebtn rule */
126
		if ($movebtn < count($a_filter))
127
			$a_filter_new[] = $a_filter[$movebtn];
128

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

    
135
		$a_filter = $a_filter_new;
136
		write_config();
137
		touch($d_filterconfdirty_path);
138
		header("Location: firewall_rules.php?if={$if}");
139
		exit;
140
	}
141
}
142

    
143
$pgtitle = "Firewall: Rules";
144
include("head.inc");
145

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