Project

General

Profile

Download (12.3 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-2004 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
require("guiconfig.inc");
33

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

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

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

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

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

    
53
if (!$if || !isset($iflist[$if]))
54
	$if = "lan";
55

    
56
if ($_POST) {
57

    
58
	$pconfig = $_POST;
59

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

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

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

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

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

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

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

    
142
?>
143
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
144
<html>
145
<head>
146
<title><?=gentitle("Firewall: Rules");?></title>
147
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
148
<link href="gui.css" rel="stylesheet" type="text/css">
149
</head>
150

    
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
<?php if ($savemsg) print_info_box($savemsg); ?>
156
<?php if (file_exists($d_filterconfdirty_path)): ?><p>
157
<?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>
158
<input name="apply" type="submit" class="formbtn" id="apply" value="Apply changes"></p>
159
<?php endif; ?>
160
<table width="100%" border="0" cellpadding="0" cellspacing="0">
161
  <tr><td>
162
  <ul id="tabnav">
163
<?php foreach ($iflist as $ifent => $ifname):
164
	if ($ifent == $if): ?>
165
    <li class="tabact"><?=htmlspecialchars($ifname);?></li>
166
<?php else: ?>
167
    <li class="tabinact"><a href="firewall_rules.php?if=<?=$ifent;?>"><?=htmlspecialchars($ifname);?></a></li>
168
<?php endif; ?>
169
<?php endforeach; ?>
170
  </ul>
171
  <ul id="tabnav">
172
    <li class="tabinact"><a href="firewall_shaper_queues.php">Queues</a></li>
173
  </ul>
174
  </td></tr>
175
  <tr>
176
    <td class="tabcont">
177
              <table width="100%" border="0" cellpadding="0" cellspacing="0">
178
                <tr>
179
                  <td width="5%" class="list">&nbsp;</td>
180
                  <td width="10%" class="listhdrr">Proto</td>
181
                  <td width="15%" class="listhdrr">Source</td>
182
                  <td width="10%" class="listhdrr">Port</td>
183
                  <td width="15%" class="listhdrr">Destination</td>
184
                  <td width="10%" class="listhdrr">Port</td>
185
                  <td width="25%" class="listhdr">Description</td>
186
                  <td width="10%" class="list"></td>
187
				</tr>
188
				<?php $nrules = 0; for ($i = 0; isset($a_filter[$i]); $i++):
189
					$filterent = $a_filter[$i];
190
					if ($filterent['interface'] != $if)
191
						continue;
192
				?>
193
                <tr valign="top">
194
                  <td class="listt">
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="<?=$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="<?=$iconfn;?>.gif" width="11" height="15" border="0">
219
				  <?php endif; ?>
220
				  </td>
221
                  <td class="listlr">
222
                    <?=$textss;?><?php if (isset($filterent['protocol'])) echo strtoupper($filterent['protocol']); else echo "*"; ?><?=$textse;?>
223
                  </td>
224
                  <td class="listr">
225
				    <?=$textss;?><?php echo htmlspecialchars(pprint_address($filterent['source'])); ?><?=$textse;?>
226
                  </td>
227
                  <td class="listr">
228
                    <?=$textss;?><?php echo htmlspecialchars(pprint_port($filterent['source']['port'])); ?><?=$textse;?>
229
                  </td>
230
                  <td class="listr">
231
				    <?=$textss;?><?php echo htmlspecialchars(pprint_address($filterent['destination'])); ?><?=$textse;?>
232
                  </td>
233
                  <td class="listr">
234
                    <?=$textss;?><?php echo htmlspecialchars(pprint_port($filterent['destination']['port'])); ?><?=$textse;?>
235
                  </td>
236
                  <td class="listbg"><font color="#FFFFFF">
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><a href="firewall_rules_edit.php?id=<?=$i;?>"><img src="e.gif" title="edit rule" width="17" height="17" border="0"></a></td>
243
					  <td align="center" valign="middle"><input type="checkbox" name="rule[]" value="<?=$i;?>" style="margin: 0; padding: 0; width: 15px; height: 15px;"></td>
244
					</tr>
245
					<tr>
246
					  <td><input name="move_<?=$i;?>" type="image" src="left.gif" width="17" height="17" title="move selected rules before this rule"></td>
247
					  <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>
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="listlr" colspan="6" align="center" valign="middle">
256
			  <span class="gray">
257
			  No rules are currently defined for this interface.<br>
258
			  All incoming connections on this interface will be blocked until you add pass rules.<br><br>
259
			  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>
260
			  </td>
261
			  <?php endif; ?>
262
                <tr>
263
                  <td class="list" colspan="7"></td>
264
                  <td class="list">
265
				    <table border="0" cellspacing="0" cellpadding="1">
266
					<tr>
267
				      <td>
268
					  <?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"><?php endif; ?></td>
269
					  <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>
270
				    </tr>
271
					<tr>
272
					  <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>
273
					  <td></td>
274
					</tr>
275
				    </table>
276
				  </td>
277
				</tr>
278
              </table>
279
			  <table border="0" cellspacing="0" cellpadding="0">
280
                <tr>
281
                  <td width="16"><img src="pass.gif" width="11" height="11"></td>
282
                  <td>pass</td>
283
                  <td width="14"></td>
284
                  <td width="16"><img src="block.gif" width="11" height="11"></td>
285
                  <td>block</td>
286
                  <td width="14"></td>
287
                  <td width="16"><img src="reject.gif" width="11" height="11"></td>
288
                  <td>reject</td>
289
                  <td width="14"></td>
290
                  <td width="16"><img src="log.gif" width="11" height="11"></td>
291
                  <td>log</td>
292
                </tr>
293
                <tr>
294
                  <td colspan="5" height="4"></td>
295
                </tr>
296
                <tr>
297
                  <td><img src="pass_d.gif" width="11" height="11"></td>
298
                  <td>pass (disabled)</td>
299
                  <td></td>
300
                  <td><img src="block_d.gif" width="11" height="11"></td>
301
                  <td>block (disabled)</td>
302
                  <td></td>
303
                  <td><img src="reject_d.gif" width="11" height="11"></td>
304
                  <td>reject (disabled)</td>
305
                  <td></td>
306
                  <td width="16"><img src="log_d.gif" width="11" height="11"></td>
307
                  <td>log (disabled)</td>
308
                </tr>
309
              </table>
310
    </td>
311
  </tr>
312
</table>
313
  <p>
314
  <strong><span class="red">Hint:<br>
315
  </span></strong>rules are evaluated on a first-match basis (i.e.
316
  the action of the first rule to match a packet will be executed).
317
  This means that if you use block rules, you'll have to pay attention
318
  to the rule order. Everything that isn't explicitly passed is blocked
319
  by default.</p>
320
<input type="hidden" name="if" value="<?=$if;?>">
321
</form>
322
<?php include("fend.inc"); ?>
323
</body>
324
</html>
(29-29/99)