Project

General

Profile

Download (12.4 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/*
4
    firewall_rules.php
5
    Copyright (C) 2004, 2005 Scott Ullrich
6
    All rights reserved.
7

    
8
    Originally part of m0n0wall (http://m0n0.ch/wall)
9
    Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
10
    All rights reserved.
11

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

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

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

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

    
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 = "lan";
57

    
58
if ($_POST) {
59

    
60
	$pconfig = $_POST;
61

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

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

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

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

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

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

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

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

    
153
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
154
<?php include("fbegin.inc"); ?>
155
<p class="pgtitle">Firewall: Rules</p>
156
<form action="firewall_rules.php" method="post">
157
<?php if ($savemsg) print_info_box($savemsg); ?>
158
<?php if (file_exists($d_filterconfdirty_path)): ?><p>
159
<?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>
160
<input name="apply" type="submit" class="formbtn" id="apply" value="Apply changes"></p>
161
<?php endif; ?>
162
<table width="100%" border="0" cellpadding="0" cellspacing="0">
163
  <tr><td>
164
  <ul id="tabnav">
165
<?php foreach ($iflist as $ifent => $ifname):
166
	if ($ifent == $if): ?>
167
    <li class="tabact"><?=htmlspecialchars($ifname);?></li>
168
<?php else: ?>
169
    <li class="tabinact"><a href="firewall_rules.php?if=<?=$ifent;?>"><?=htmlspecialchars($ifname);?></a></li>
170
<?php endif; ?>
171
<?php endforeach; ?>
172
  </ul>
173
  </td></tr>
174
  <tr>
175
    <td class="tabcont">
176
              <table width="100%" border="0" cellpadding="0" cellspacing="0">
177
                <tr>
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="25%" 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">
193
                  <td class="listt">
194
				  <?php if ($filterent['type'] == "block")
195
				  			$iconfn = "block";
196
						else if ($filterent['type'] == "reject") {
197
							if ($filterent['protocol'] == "tcp" || $filterent['protocol'] == "udp")
198
								$iconfn = "reject";
199
							else
200
								$iconfn = "block";
201
						} else
202
							$iconfn = "pass";
203
						if (isset($filterent['disabled'])) {
204
							$textss = "<span class=\"gray\">";
205
							$textse = "</span>";
206
							$iconfn .= "_d";
207
						} else {
208
							$textss = $textse = "";
209
						}
210
				  ?>
211
				  <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>
212
				  <?php if (isset($filterent['log'])):
213
							$iconfn = "log_s";
214
						if (isset($filterent['disabled']))
215
							$iconfn .= "_d";
216
				  	?>
217
				  <br><img src="<?=$iconfn;?>.gif" width="11" height="15" border="0">
218
				  <?php endif; ?>
219
				  </td>
220
                  <td class="listlr">
221
                    <?=$textss;?><?php if (isset($filterent['protocol'])) echo strtoupper($filterent['protocol']); else echo "*"; ?><?=$textse;?>
222
                  </td>
223
                  <td class="listr">
224
				    <?=$textss;?><?php echo htmlspecialchars(pprint_address($filterent['source'])); ?><?=$textse;?>
225
                  </td>
226
                  <td class="listr">
227
                    <?=$textss;?><?php echo htmlspecialchars(pprint_port($filterent['source']['port'])); ?><?=$textse;?>
228
                  </td>
229
                  <td class="listr">
230
				    <?=$textss;?><?php echo htmlspecialchars(pprint_address($filterent['destination'])); ?><?=$textse;?>
231
                  </td>
232
                  <td class="listr">
233
                    <?=$textss;?><?php echo htmlspecialchars(pprint_port($filterent['destination']['port'])); ?><?=$textse;?>
234
                  </td>
235
                  <td class="listbg"><font color="#FFFFFF">
236
                    <?=$textss;?><?=htmlspecialchars($filterent['descr']);?>&nbsp;<?=$textse;?>
237
                  </td>
238
                  <td valign="middle" nowrap class="list">
239
				    <table border="0" cellspacing="0" cellpadding="1">
240
					<tr>
241
					  <td><a href="firewall_rules_edit.php?id=<?=$i;?>"><img src="e.gif" title="edit rule" width="17" height="17" border="0"></a></td>
242
					  <td align="center" valign="middle"><input type="checkbox" name="rule[]" value="<?=$i;?>" style="margin: 0; padding: 0; width: 15px; height: 15px;"></td>
243
					</tr>
244
					<tr>
245
					  <td><input name="move_<?=$i;?>" type="image" src="left.gif" width="17" height="17" title="move selected rules before this rule"></td>
246
					  <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>
247
					</tr>
248
					</table>
249
				  </td>
250
				</tr>
251
			  <?php $nrules++; endfor; ?>
252
			  <?php if ($nrules == 0): ?>
253
			  <td class="listt"></td>
254
			  <td class="listlr" colspan="6" align="center" valign="middle">
255
			  <span class="gray">
256
			  No rules are currently defined for this interface.<br>
257
			  All incoming connections on this interface will be blocked until you add pass rules.<br><br>
258
			  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>
259
			  </td>
260
			  <?php endif; ?>
261
                <tr>
262
                  <td class="list" colspan="7"></td>
263
                  <td class="list">
264
				    <table border="0" cellspacing="0" cellpadding="1">
265
					<tr>
266
				      <td>
267
					  <?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>
268
					  <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>
269
				    </tr>
270
					<tr>
271
					  <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>
272
					  <td></td>
273
					</tr>
274
				    </table>
275
				  </td>
276
				</tr>
277
              </table>
278
			  <table border="0" cellspacing="0" cellpadding="0">
279
                <tr>
280
                  <td width="16"><img src="pass.gif" width="11" height="11"></td>
281
                  <td>pass</td>
282
                  <td width="14"></td>
283
                  <td width="16"><img src="block.gif" width="11" height="11"></td>
284
                  <td>block</td>
285
                  <td width="14"></td>
286
                  <td width="16"><img src="reject.gif" width="11" height="11"></td>
287
                  <td>reject</td>
288
                  <td width="14"></td>
289
                  <td width="16"><img src="log.gif" width="11" height="11"></td>
290
                  <td>log</td>
291
                </tr>
292
                <tr>
293
                  <td colspan="5" height="4"></td>
294
                </tr>
295
                <tr>
296
                  <td><img src="pass_d.gif" width="11" height="11"></td>
297
                  <td>pass (disabled)</td>
298
                  <td></td>
299
                  <td><img src="block_d.gif" width="11" height="11"></td>
300
                  <td>block (disabled)</td>
301
                  <td></td>
302
                  <td><img src="reject_d.gif" width="11" height="11"></td>
303
                  <td>reject (disabled)</td>
304
                  <td></td>
305
                  <td width="16"><img src="log_d.gif" width="11" height="11"></td>
306
                  <td>log (disabled)</td>
307
                </tr>
308
              </table>
309
    </td>
310
  </tr>
311
</table>
312
  <p>
313
  <strong><span class="red">Hint:<br>
314
  </span></strong>rules are evaluated on a first-match basis (i.e.
315
  the action of the first rule to match a packet will be executed).
316
  This means that if you use block rules, you'll have to pay attention
317
  to the rule order. Everything that isn't explicitly passed is blocked
318
  by default.</p>
319
<input type="hidden" name="if" value="<?=$if;?>">
320
</form>
321
<?php include("fend.inc"); ?>
322
</body>
323
</html>
(30-30/100)