Project

General

Profile

Download (13.7 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/*
4
    firewall_shaper.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['shaper']['rule'])) {
37
	$config['shaper']['rule'] = array();
38
}
39
if (!is_array($config['shaper']['queue'])) {
40
	$config['shaper']['queue'] = array();
41
}
42
$a_shaper = &$config['shaper']['rule'];
43
$a_queue = &$config['shaper']['queue'];
44

    
45
$pconfig['enable'] = isset($config['shaper']['enable']);
46

    
47
if ($_POST) {
48

    
49
	if ($_POST['submit']) {
50
		$pconfig = $_POST;
51
		$config['shaper']['enable'] = $_POST['enable'] ? true : false;
52
		write_config();
53
	}
54

    
55
	if ($_POST['apply'] || $_POST['submit']) {
56
		$config['shaper']['enable'] = $_POST['enable'] ? true : false;
57
		write_config();		
58
		$retval = 0;
59
		$savemsg = get_std_save_message($retval);
60
		/* Setup pf rules since the user may have changed the optimization value */
61
		config_lock();
62
		$retval = filter_configure();
63
		config_unlock();
64
		if(stristr($retval, "error") <> true)
65
		    $savemsg = get_std_save_message($retval);
66
		else
67
		    $savemsg = $retval;
68

    
69
	        if(file_exists($d_shaperconfdirty_path))
70
	          unlink($d_shaperconfdirty_path);
71
	}
72
}
73

    
74
if ($_GET['act'] == "del") {
75
	if ($a_shaper[$_GET['id']]) {
76
		unset($a_shaper[$_GET['id']]);
77
		write_config();
78
		touch($d_shaperconfdirty_path);
79
		header("Location: firewall_shaper.php");
80
		exit;
81
	}
82
} else if ($_GET['act'] == "down") {
83
	if ($a_shaper[$_GET['id']] && $a_shaper[$_GET['id']+1]) {
84
		$tmp = $a_shaper[$_GET['id']+1];
85
		$a_shaper[$_GET['id']+1] = $a_shaper[$_GET['id']];
86
		$a_shaper[$_GET['id']] = $tmp;
87
		write_config();
88
		touch($d_shaperconfdirty_path);
89
		header("Location: firewall_shaper.php");
90
		exit;
91
	}
92
} else if ($_GET['act'] == "up") {
93
	if (($_GET['id'] > 0) && $a_shaper[$_GET['id']]) {
94
		$tmp = $a_shaper[$_GET['id']-1];
95
		$a_shaper[$_GET['id']-1] = $a_shaper[$_GET['id']];
96
		$a_shaper[$_GET['id']] = $tmp;
97
		write_config();
98
		touch($d_shaperconfdirty_path);
99
		header("Location: firewall_shaper.php");
100
		exit;
101
	}
102
} else if ($_GET['act'] == "toggle") {
103
	if ($a_shaper[$_GET['id']]) {
104
		$a_shaper[$_GET['id']]['disabled'] = !isset($a_shaper[$_GET['id']]['disabled']);
105
		write_config();
106
		touch($d_shaperconfdirty_path);
107
		header("Location: firewall_shaper.php");
108
		exit;
109
	}
110
} else {
111
	/* yuck - IE won't send value attributes for image buttons, while Mozilla does -
112
	   so we use .x/.y to fine move button clicks instead... */
113
	unset($movebtn);
114
	foreach ($_POST as $pn => $pd) {
115
		if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
116
			$movebtn = $matches[1];
117
			break;
118
		}
119
	}
120
	/* move selected rules before this rule */
121
	if (isset($movebtn) && is_array($_POST['rule']) && count($_POST['rule'])) {
122
		$a_shaper_new = array();
123

    
124
		/* copy all rules < $movebtn and not selected */
125
		for ($i = 0; $i < $movebtn; $i++) {
126
			if (!in_array($i, $_POST['rule']))
127
				$a_shaper_new[] = $a_shaper[$i];
128
		}
129

    
130
		/* copy all selected rules */
131
		for ($i = 0; $i < count($a_shaper); $i++) {
132
			if ($i == $movebtn)
133
				continue;
134
			if (in_array($i, $_POST['rule']))
135
				$a_shaper_new[] = $a_shaper[$i];
136
		}
137

    
138
		/* copy $movebtn rule */
139
		if ($movebtn < count($a_shaper))
140
			$a_shaper_new[] = $a_shaper[$movebtn];
141

    
142
		/* copy all rules > $movebtn and not selected */
143
		for ($i = $movebtn+1; $i < count($a_shaper); $i++) {
144
			if (!in_array($i, $_POST['rule']))
145
				$a_shaper_new[] = $a_shaper[$i];
146
		}
147

    
148
		$a_shaper = $a_shaper_new;
149
		write_config();
150
		touch($d_shaperconfdirty_path);
151
		header("Location: firewall_shaper.php");
152
		exit;
153
	}
154
}
155
?>
156
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
157
<html>
158
<head>
159
<title><?=gentitle("Firewall: Traffic shaper: Rules");?></title>
160
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
161
<link href="gui.css" rel="stylesheet" type="text/css">
162
</head>
163

    
164
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
165
<?php include("fbegin.inc"); ?>
166
<p class="pgtitle">Firewall: Traffic shaper: Rules</p>
167
<form action="firewall_shaper.php" method="post" name="iform">
168
<script type="text/javascript" language="javascript" src="row_toggle.js">
169
</script>
170
<?php if ($savemsg) print_info_box($savemsg); ?>
171
<?php if (file_exists($d_shaperconfdirty_path)): ?><p>
172
<?php print_info_box_np("The traffic shaper configuration has been changed.<br>You must apply the changes in order for them to take effect.");?><br>
173
<input name="apply" type="submit" class="formbtn" id="apply" value="Apply changes"></p>
174
<?php endif; ?>
175
<table width="100%" border="0" cellpadding="0" cellspacing="0">
176
  <tr><td>
177
  <ul id="tabnav">
178
    <li class="tabact">Rules</li>
179
    <li class="tabinact"><a href="firewall_shaper_queues.php">Queues</a></li>
180
    <li class="tabinact"><a href="firewall_shaper_magic.php">Magic shaper wizard</a></li>
181
  </ul>
182
  </td></tr>
183
  <tr>
184
    <td class="tabcont">
185
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
186
                <tr>
187
                  <td class="vtable"><p>
188
                      <input name="enable" type="checkbox" id="enable" value="yes" <?php if ($pconfig['enable'] == "yes") echo "checked";?>>
189
                      <strong>Enable traffic shaper<br>
190
                      </strong></p></td>
191
                </tr>
192
                <tr>
193
                  <td> <input name="submit" type="submit" class="formbtn" value="Save">
194
                  </td>
195
                </tr>
196
              </table>
197
              &nbsp;<br>
198
              <table width="100%" border="0" cellpadding="0" cellspacing="0">
199
                      <tr id="frheader">
200
		        <td width="3%" class="list">&nbsp;</td>
201
                        <td width="3%" class="list">&nbsp;</td>
202
                        <td width="5%" class="listhdrrns">If</td>
203
                        <td width="5%" class="listhdrrns">Proto</td>
204
                        <td width="20%" class="listhdrr">Source</td>
205
                        <td width="20%" class="listhdrr">Destination</td>
206
                        <td width="15%" class="listhdrrns">Target</td>
207
                        <td width="25%" class="listhdr">Description</td>
208
                        <td width="10%" class="list"></td>
209
                      </tr>
210
                      <?php $nrules = $i = 0; foreach ($a_shaper as $shaperent): ?>
211
                      <tr valign="top" id="fr<?=$nrules;?>">
212
                        <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>
213
                        <td class="listt" align="center"></td>
214
                        <td class="listlr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>">
215
                          <?php
216
				  $dis = "";
217
				  if (isset($shaperent['disabled'])) {
218
				  	$dis = "_d";
219
					$textss = "<span class=\"gray\">";
220
					$textse = "</span>";
221
				  } else {
222
				  	$textss = $textse = "";
223
				  }
224
				  $iflabels = array('lan' => 'LAN', 'wan' => 'WAN', 'pptp' => 'PPTP');
225
				  for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
226
				  	$iflabels['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
227
				  echo $textss . htmlspecialchars($iflabels[$shaperent['interface']]);
228
				  echo "<br>";
229
				  echo "<a href=\"?act=toggle&id={$i}\">";
230
				  if ($shaperent['direction'] != "in")
231
				  	echo "<img src=\"out{$dis}.gif\" width=\"11\" height=\"11\" border=\"0\" style=\"margin-top: 5px\" title=\"click to toggle enabled/disabled status\">";
232
				  if ($shaperent['direction'] != "out")
233
				  	echo "<img src=\"in{$dis}.gif\" width=\"11\" height=\"11\" border=\"0\" style=\"margin-top: 5px\" title=\"click to toggle enabled/disabled status\">";
234
				  echo "</a>" . $textse;;
235
				  ?>
236
                        </td>
237
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>">
238
                          <?=$textss;?><?php if (isset($shaperent['protocol'])) echo strtoupper($shaperent['protocol']); else echo "*"; ?><?=$textse;?>
239
                        </td>
240
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>"><?=$textss;?><?php echo htmlspecialchars(pprint_address($shaperent['source'])); ?>
241
						<?php if ($shaperent['source']['port']): ?><br>
242
						Port: <?=htmlspecialchars(pprint_port($shaperent['source']['port'])); ?>
243
						<?php endif; ?><?=$textse;?>
244
                        </td>
245
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>"><?=$textss;?><?php echo htmlspecialchars(pprint_address($shaperent['destination'])); ?>
246
						<?php if ($shaperent['destination']['port']): ?><br>
247
						Port: <?=htmlspecialchars(pprint_port($shaperent['destination']['port'])); ?>
248
						<?php endif; ?><?=$textse;?>
249
                        </td>
250
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>"><?=$textss;?>
251
                          <?php
252
							if (isset($shaperent['targetqueue'])) {
253
								$desc = htmlspecialchars($shaperent['targetqueue']);
254
							    echo "<a href=\"firewall_shaper_queues_edit.php?id={$shaperent['targetqueue']}\">{$desc}</a>";
255
							}
256
						  ?><?=$textse;?>
257
                        </td>
258
                        <td class="listbg"><font color="white">
259
                          <?=$textss;?><?=htmlspecialchars($shaperent['descr']);?><?=$textse;?>
260
                          &nbsp; </td>
261
                        <td valign="middle" nowrap class="list"> <a href="firewall_shaper_edit.php?id=<?=$i;?>"><img src="e.gif" title="edit rule" width="17" height="17" border="0"></a>
262
                          <?php if ($i > 0): ?>
263
                          <a href="firewall_shaper.php?act=up&id=<?=$i;?>"><img src="up.gif" title="move up" width="17" height="17" border="0"></a>
264
                          <?php else: ?>
265
                          <img src="up_d.gif" width="17" height="17" border="0">
266
                          <?php endif; ?>
267
			  <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)"><br>
268
			  <a href="firewall_shaper.php?act=del&id=<?=$i;?>" onclick="return confirm('Do you really want to delete this rule?')"><img src="x.gif" title="delete rule" width="17" height="17" border="0"></a>
269
                          <?php if (isset($a_shaper[$i+1])): ?>
270
                          <a href="firewall_shaper.php?act=down&id=<?=$i;?>"><img src="down.gif" title="move down" width="17" height="17" border="0"></a>
271
                          <?php else: ?>
272
                          <img src="down_d.gif" width="17" height="17" border="0">
273
                          <?php endif; ?>
274
                          <a href="firewall_shaper_edit.php?dup=<?=$i;?>"><img src="plus.gif" title="add a new rule based on this one" width="17" height="17" border="0"></a>
275
                        </td>
276
                      </tr>
277
                      <?php $nrules++; $i++; endforeach; ?>
278
                      <tr>
279
                        <td class="list" colspan="8"></td>
280
                        <td class="list"> <a href="firewall_shaper_edit.php"><img src="plus.gif" width="17" height="17" border="0"></a></td>
281
                      </tr>
282
                    </table>
283

    
284
                    <table border="0" cellspacing="0" cellpadding="0">
285
                      <tr>
286
                        <td width="16"><img src="in.gif" width="11" height="11"></td>
287
                        <td>incoming (as seen by firewall)</td>
288
                        <td width="14"></td>
289
                        <td width="16"><img src="out.gif" width="11" height="11"></td>
290
                        <td>outgoing (as seen by firewall)</td>
291
                      </tr>
292
                      <tr>
293
                        <td colspan="5" height="4"></td>
294
                      </tr>
295
                      <tr>
296
                        <td><img src="in_d.gif" width="11" height="11"></td>
297
                        <td>incoming (disabled)</td>
298
                        <td width="14"></td>
299
                        <td><img src="out_d.gif" width="11" height="11"></td>
300
                        <td>outgoing (disabled)</td>
301
                      </tr>
302
                    </table>
303
			        <p><span class="red"><strong>Note:</strong></span><strong><br>
304
                    </strong>the first rule that matches a packet will be executed.<br>
305
                    The following match patterns are not shown in the list above:
306
                    IP packet length, TCP flags.</td>
307
	</tr>
308
</table>
309
            </form>
310
<?php include("fend.inc"); ?>
311
</body>
312
</html>
(34-34/106)