Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

    
57
if ($_POST) {
58

    
59
	$pconfig = $_POST;
60

    
61
	if ($_POST['apply']) {
62
		$retval = 0;
63
		if (!file_exists($d_sysrebootreqd_path)) {
64
			config_lock();
65
			$retval = filter_configure();
66
			config_unlock();
67
		}
68
		$savemsg = get_std_save_message($retval);
69
		if ($retval == 0) {
70
			if (file_exists($d_natconfdirty_path))
71
				unlink($d_natconfdirty_path);
72
			if (file_exists($d_filterconfdirty_path))
73
				unlink($d_filterconfdirty_path);
74
		}
75
	}
76
}
77

    
78
if (isset($_POST['del_x'])) {
79
	/* delete selected rules */
80
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
81
		foreach ($_POST['rule'] as $rulei) {
82
			unset($a_filter[$rulei]);
83
		}
84
		write_config();
85
		touch($d_filterconfdirty_path);
86
		header("Location: firewall_rules.php?if={$if}");
87
		exit;
88
	}
89
} else if ($_GET['act'] == "toggle") {
90
	if ($a_filter[$_GET['id']]) {
91
		$a_filter[$_GET['id']]['disabled'] = !isset($a_filter[$_GET['id']]['disabled']);
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
?>
144
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
145
<html>
146
<head>
147
<title><?=gentitle("Firewall: Rules");?></title>
148
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
149
<link href="gui.css" rel="stylesheet" type="text/css">
150
</head>
151
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
152
<?php include("fbegin.inc"); ?>
153
<p class="pgtitle">Firewall: Rules</p>
154
<form action="firewall_rules.php" method="post">
155
<script type="text/javascript" language="javascript" src="row_toggle.js">
156
</script>
157
<form action="firewall_rules.php" method="post">
158
<?php if ($savemsg) print_info_box($savemsg); ?>
159
<?php if (file_exists($d_filterconfdirty_path)): ?><p>
160
<?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>
161
<input name="apply" type="submit" class="formbtn" id="apply" value="Apply changes"></p>
162
<?php endif; ?>
163
<table width="100%" border="0" cellpadding="0" cellspacing="0">
164
  <tr><td class="tabnavtbl">
165
  <ul id="tabnav">
166
<?php $i = 0; foreach ($iflist as $ifent => $ifname):
167
	if ($ifent == $if): ?>
168
    <li class="tabact"><?=htmlspecialchars($ifname);?></li>
169
<?php else: ?>
170
    <li class="<?php if ($i == 0) echo "tabinact1"; else echo "tabinact";?>"><a href="firewall_rules.php?if=<?=$ifent;?>"><?=htmlspecialchars($ifname);?></a></li>
171
<?php endif; ?>
172
<?php $i++; endforeach; ?>
173
  </ul>
174
  </td></tr>
175
  <tr>
176
    <td class="tabcont">
177
              <table width="100%" border="0" cellpadding="0" cellspacing="0">
178
                <tr id="frheader">
179
                  <td width="3%" class="list">&nbsp;</td>
180
                  <td width="5%" class="list">&nbsp;</td>
181
                  <td width="10%" class="listhdrr">Proto</td>
182
                  <td width="15%" class="listhdrr">Source</td>
183
                  <td width="10%" class="listhdrr">Port</td>
184
                  <td width="15%" class="listhdrr">Destination</td>
185
                  <td width="10%" class="listhdrr">Port</td>
186
                  <td width="22%" class="listhdr">Description</td>
187
                  <td width="10%" class="list"></td>
188
				</tr>
189
				<?php $nrules = 0; for ($i = 0; isset($a_filter[$i]); $i++):
190
					$filterent = $a_filter[$i];
191
					if ($filterent['interface'] != $if)
192
						continue;
193
				?>
194
                <tr valign="top" id="fr<?=$nrules;?>">
195
                  <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>
196
                  <td class="listt" align="center">
197
				  <?php if ($filterent['type'] == "block")
198
				  			$iconfn = "block";
199
						else if ($filterent['type'] == "reject") {
200
							if ($filterent['protocol'] == "tcp" || $filterent['protocol'] == "udp")
201
								$iconfn = "reject";
202
							else
203
								$iconfn = "block";
204
						} else
205
							$iconfn = "pass";
206
						if (isset($filterent['disabled'])) {
207
							$textss = "<span class=\"gray\">";
208
							$textse = "</span>";
209
							$iconfn .= "_d";
210
						} else {
211
							$textss = $textse = "";
212
						}
213
				  ?>
214
				  <a href="?if=<?=$if;?>&act=toggle&id=<?=$i;?>"><img src="<?=$iconfn;?>.gif" width="11" height="11" border="0" title="click to toggle enabled/disabled status"></a>
215
				  <?php if (isset($filterent['log'])):
216
							$iconfn = "log_s";
217
						if (isset($filterent['disabled']))
218
							$iconfn .= "_d";
219
				  	?>
220
				  <br><img src="<?=$iconfn;?>.gif" width="11" height="15" border="0">
221
				  <?php endif; ?>
222
				  </td>
223
                  <td class="listlr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>">
224
                    <?=$textss;?><?php if (isset($filterent['protocol'])) echo strtoupper($filterent['protocol']); else echo "*"; ?><?=$textse;?>
225
                  </td>
226
                  <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>">
227
				    <?=$textss;?><?php echo htmlspecialchars(pprint_address($filterent['source'])); ?><?=$textse;?>
228
                  </td>
229
                  <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>">
230
                    <?=$textss;?><?php echo htmlspecialchars(pprint_port($filterent['source']['port'])); ?><?=$textse;?>
231
                  </td>
232
                  <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>">
233
				    <?=$textss;?><?php echo htmlspecialchars(pprint_address($filterent['destination'])); ?><?=$textse;?>
234
                  </td>
235
                  <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>">
236
                    <?=$textss;?><?php echo htmlspecialchars(pprint_port($filterent['destination']['port'])); ?><?=$textse;?>
237
                  </td>
238
                  <td class="listbg" onClick="fr_toggle(<?=$nrules;?>)" bcolor="#990000"><font color="white">
239
                    <?=$textss;?><?=htmlspecialchars($filterent['descr']);?>&nbsp;<?=$textse;?>
240
                  </td>
241
                  <td valign="middle" nowrap class="list">
242
				    <table border="0" cellspacing="0" cellpadding="1">
243
					<tr>
244
					  <td><input name="move_<?=$i;?>" type="image" src="left.gif" width="17" height="17" title="move selected rules before this rule" onMouseOver="fr_insline(<?=$nrules;?>, true)" onMouseOut="fr_insline(<?=$nrules;?>, false)"></td>
245
					  <td><a href="firewall_rules_edit.php?id=<?=$i;?>"><img src="e.gif" title="edit rule" width="17" height="17" border="0"></a></td>
246
					</tr>
247
					<tr>
248
					  <td align="center" valign="middle"></td>
249
					  <td><a href="firewall_rules_edit.php?dup=<?=$i;?>"><img src="plus.gif" title="add a new rule based on this one" width="17" height="17" border="0"></a></td>
250
					</tr>
251
					</table>
252
				  </td>
253
				</tr>
254
			  <?php $nrules++; endfor; ?>
255
			  <?php if ($nrules == 0): ?>
256
              <td class="listt"></td>
257
			  <td class="listt"></td>
258
			  <td class="listlr" colspan="6" align="center" valign="middle">
259
			  <span class="gray">
260
			  No rules are currently defined for this interface.<br>
261
			  All incoming connections on this interface will be blocked until you add pass rules.<br><br>
262
			  Click the <a href="firewall_rules_edit.php?if=<?=$if;?>"><img src="plus.gif" title="add new rule" border="0" width="17" height="17" align="absmiddle"></a> button to add a new rule.</span>
263
			  </td>
264
			  <?php endif; ?>
265
                <tr id="fr<?=$nrules;?>">
266
                  <td class="list"></td>
267
                  <td class="list"></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">&nbsp;</td>
273
                  <td class="list">&nbsp;</td>
274
                  <td class="list">
275
				    <table border="0" cellspacing="0" cellpadding="1">
276
					<tr>
277
				      <td>
278
					  <?php if ($nrules == 0): ?><img src="left_d.gif" width="17" height="17" title="move selected rules to end" border="0"><?php else: ?><input name="move_<?=$i;?>" type="image" src="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>
279
					  <td></td>
280
				    </tr>
281
					<tr>
282
					  <td><?php if ($nrules == 0): ?><img src="x_d.gif" width="17" height="17" title="delete selected rules" border="0"><?php else: ?><input name="del" type="image" src="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>
283
					  <td><a href="firewall_rules_edit.php?if=<?=$if;?>"><img src="plus.gif" title="add new rule" width="17" height="17" border="0"></a></td>
284
					</tr>
285
				    </table>
286
				  </td>
287
				</tr>
288
              </table>
289
			  <table border="0" cellspacing="0" cellpadding="0">
290
                <tr>
291
                  <td width="16"><img src="pass.gif" width="11" height="11"></td>
292
                  <td>pass</td>
293
                  <td width="14"></td>
294
                  <td width="16"><img src="block.gif" width="11" height="11"></td>
295
                  <td>block</td>
296
                  <td width="14"></td>
297
                  <td width="16"><img src="reject.gif" width="11" height="11"></td>
298
                  <td>reject</td>
299
                  <td width="14"></td>
300
                  <td width="16"><img src="log.gif" width="11" height="11"></td>
301
                  <td>log</td>
302
                </tr>
303
                <tr>
304
                  <td colspan="5" height="4"></td>
305
                </tr>
306
                <tr>
307
                  <td><img src="pass_d.gif" width="11" height="11"></td>
308
                  <td>pass (disabled)</td>
309
                  <td></td>
310
                  <td><img src="block_d.gif" width="11" height="11"></td>
311
                  <td>block (disabled)</td>
312
                  <td></td>
313
                  <td><img src="reject_d.gif" width="11" height="11"></td>
314
                  <td>reject (disabled)</td>
315
                  <td></td>
316
                  <td width="16"><img src="log_d.gif" width="11" height="11"></td>
317
                  <td>log (disabled)</td>
318
                </tr>
319
              </table>
320
    </td>
321
  </tr>
322
</table>
323
  <p>
324
  <strong><span class="red">Hint:<br>
325
  </span></strong>Rules are evaluated on a first-match basis (i.e.
326
  the action of the first rule to match a packet will be executed).
327
  This means that if you use block rules, you'll have to pay attention
328
  to the rule order. Everything that isn't explicitly passed is blocked
329
  by default.</p>
330
  <input type="hidden" name="if" value="<?=$if;?>">
331
</form>
332
<?php include("fend.inc"); ?>
(30-30/101)