Project

General

Profile

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

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

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

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

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

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

    
35
require("guiconfig.inc");
36

    
37
if (!is_array($config['shaper']['rule'])) {
38
	$config['shaper']['rule'] = array();
39
}
40
if (!is_array($config['shaper']['queue'])) {
41
	$config['shaper']['queue'] = array();
42
}
43
$a_shaper = &$config['shaper']['rule'];
44
$a_queue = &$config['shaper']['queue'];
45

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

    
48
function wipe_magic () {
49
  global $config;
50

    
51
  /* wipe previous */
52
  unset($config['shaper']['queue']);
53
  unset($config['shaper']['rule']);
54
  $config['shaper']['enable'] = FALSE;
55
}
56

    
57
if ($_POST['remove']) {
58
  wipe_magic();
59
  $savemsg = '<p><span class="red"><strong>Note: The traffic shaper has been disabled.</strong></span><strong><br>';
60
  touch($d_shaperconfdirty_path);
61
  unset($config['shaper']['enable']);
62
  write_config();
63
}
64

    
65
if ($_POST) {
66

    
67
	if ($_POST['submit']) {
68
		$pconfig = $_POST;
69
		$config['shaper']['enable'] = $_POST['enable'] ? true : false;
70
		write_config();
71
	}
72

    
73
	if ($_POST['apply'] || $_POST['submit']) {
74
		$config['shaper']['enable'] = $_POST['enable'] ? true : false;
75
		write_config();		
76
		$retval = 0;
77
		$savemsg = get_std_save_message($retval);
78
		/* Setup pf rules since the user may have changed the optimization value */
79
		config_lock();
80
		$retval = filter_configure();
81
		config_unlock();
82
		if(stristr($retval, "error") <> true)
83
		    $savemsg = get_std_save_message($retval);
84
		else
85
		    $savemsg = $retval;
86

    
87
	        if(file_exists($d_shaperconfdirty_path))
88
	          unlink($d_shaperconfdirty_path);
89
	}
90
}
91

    
92
if ($_GET['act'] == "del") {
93
	if ($a_shaper[$_GET['id']]) {
94
		unset($a_shaper[$_GET['id']]);
95
		write_config();
96
		touch($d_shaperconfdirty_path);
97
		header("Location: firewall_shaper.php");
98
		exit;
99
	}
100
} else if ($_GET['act'] == "down") {
101
	if ($a_shaper[$_GET['id']] && $a_shaper[$_GET['id']+1]) {
102
		$tmp = $a_shaper[$_GET['id']+1];
103
		$a_shaper[$_GET['id']+1] = $a_shaper[$_GET['id']];
104
		$a_shaper[$_GET['id']] = $tmp;
105
		write_config();
106
		touch($d_shaperconfdirty_path);
107
		header("Location: firewall_shaper.php");
108
		exit;
109
	}
110
} else if ($_GET['act'] == "up") {
111
	if (($_GET['id'] > 0) && $a_shaper[$_GET['id']]) {
112
		$tmp = $a_shaper[$_GET['id']-1];
113
		$a_shaper[$_GET['id']-1] = $a_shaper[$_GET['id']];
114
		$a_shaper[$_GET['id']] = $tmp;
115
		write_config();
116
		touch($d_shaperconfdirty_path);
117
		header("Location: firewall_shaper.php");
118
		exit;
119
	}
120
} else if ($_GET['act'] == "toggle") {
121
	if ($a_shaper[$_GET['id']]) {
122
		$a_shaper[$_GET['id']]['disabled'] = !isset($a_shaper[$_GET['id']]['disabled']);
123
		write_config();
124
		touch($d_shaperconfdirty_path);
125
		header("Location: firewall_shaper.php");
126
		exit;
127
	}
128
} else {
129
	/* yuck - IE won't send value attributes for image buttons, while Mozilla does -
130
	   so we use .x/.y to fine move button clicks instead... */
131
	unset($movebtn);
132
	foreach ($_POST as $pn => $pd) {
133
		if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
134
			$movebtn = $matches[1];
135
			break;
136
		}
137
	}
138
	/* move selected rules before this rule */
139
	if (isset($movebtn) && is_array($_POST['rule']) && count($_POST['rule'])) {
140
		$a_shaper_new = array();
141

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

    
148
		/* copy all selected rules */
149
		for ($i = 0; $i < count($a_shaper); $i++) {
150
			if ($i == $movebtn)
151
				continue;
152
			if (in_array($i, $_POST['rule']))
153
				$a_shaper_new[] = $a_shaper[$i];
154
		}
155

    
156
		/* copy $movebtn rule */
157
		if ($movebtn < count($a_shaper))
158
			$a_shaper_new[] = $a_shaper[$movebtn];
159

    
160
		/* copy all rules > $movebtn and not selected */
161
		for ($i = $movebtn+1; $i < count($a_shaper); $i++) {
162
			if (!in_array($i, $_POST['rule']))
163
				$a_shaper_new[] = $a_shaper[$i];
164
		}
165

    
166
		$a_shaper = $a_shaper_new;
167
		write_config();
168
		touch($d_shaperconfdirty_path);
169
		header("Location: firewall_shaper.php");
170
		exit;
171
	}
172
}
173
?>
174
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
175
<html>
176
<head>
177
<title><?=gentitle("Firewall: Traffic shaper: Rules");?></title>
178
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
179
<link href="gui.css" rel="stylesheet" type="text/css">
180
</head>
181

    
182
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
183
<?php include("fbegin.inc"); ?>
184
<p class="pgtitle">Firewall: Traffic shaper: Rules</p>
185
<form action="firewall_shaper.php" method="post" name="iform">
186
<script type="text/javascript" language="javascript" src="row_toggle.js">
187
</script>
188
<?php if ($savemsg) print_info_box($savemsg); ?>
189
<?php if (file_exists($d_shaperconfdirty_path)): ?><p>
190
<?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>
191
<input name="apply" type="submit" class="formbtn" id="apply" value="Apply changes"></p>
192
<?php endif; ?>
193
<table width="100%" border="0" cellpadding="0" cellspacing="0">
194
  <tr><td>
195
  <ul id="tabnav">
196
    <li class="tabact">Rules</li>
197
    <li class="tabinact"><a href="firewall_shaper_queues.php">Queues</a></li>
198
    <li class="tabinact"><a href="wizard.php?xml=traffic_shaper_wizard.xml">EZ Shaper wizard</a></li>
199
  </ul>
200
  </td></tr>
201
  <tr>
202
    <td class="tabcont">
203
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
204
                <tr>
205
                  <td class="vtable"><p>
206
                      <input name="enable" type="checkbox" id="enable" value="yes" <?php if ($pconfig['enable'] == "yes") echo "checked";?>>
207
                      <strong>Enable traffic shaper<br>
208
                      </strong></p></td>
209
                </tr>
210
                <tr>
211
                  <td> 
212
		  <input name="submit" type="submit" class="formbtn" value="Save"> 
213
		  <input name="remove" type="submit" class="formbtn" id="remove" value="Remove Wizard"> 
214
                  </td>
215
		  
216
                </tr>
217
              </table>
218
              &nbsp;<br>
219
              <table width="100%" border="0" cellpadding="0" cellspacing="0">
220
                      <tr id="frheader">
221
		        <td width="3%" class="list">&nbsp;</td>
222
                        <td width="3%" class="list">&nbsp;</td>
223
                        <td width="5%" class="listhdrrns">If</td>
224
                        <td width="5%" class="listhdrrns">Proto</td>
225
                        <td width="20%" class="listhdrr">Source</td>
226
                        <td width="20%" class="listhdrr">Destination</td>
227
                        <td width="15%" class="listhdrrns">Target</td>
228
                        <td width="25%" class="listhdr">Description</td>
229
                        <td width="10%" class="list"></td>
230
                      </tr>
231
                      <?php $nrules = $i = 0; foreach ($a_shaper as $shaperent): ?>
232
                      <tr valign="top" id="fr<?=$nrules;?>">
233
                        <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>
234
                        <td class="listt" align="center"></td>
235
                        <td class="listlr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';">
236
                          <?php
237
				  $dis = "";
238
				  if (isset($shaperent['disabled'])) {
239
				  	$dis = "_d";
240
					$textss = "<span class=\"gray\">";
241
					$textse = "</span>";
242
				  } else {
243
				  	$textss = $textse = "";
244
				  }
245
				  $iflabels = array('lan' => 'LAN', 'wan' => 'WAN', 'pptp' => 'PPTP');
246
				  for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
247
				  	$iflabels['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
248
				  echo $textss . htmlspecialchars($iflabels[$shaperent['interface']]);
249
				  echo "<br>";
250
				  echo "<a href=\"?act=toggle&id={$i}\">";
251
				  if ($shaperent['direction'] == "in")
252
				  	echo "<img src=\"in{$dis}.gif\" width=\"11\" height=\"11\" border=\"0\" style=\"margin-top: 5px\" title=\"click to toggle enabled/disabled status\">";
253
				  if ($shaperent['direction'] == "out")
254
				  	echo "<img src=\"out{$dis}.gif\" width=\"11\" height=\"11\" border=\"0\" style=\"margin-top: 5px\" title=\"click to toggle enabled/disabled status\">";
255

    
256
				  echo "</a>" . $textse;;
257
				  ?>
258
                        </td>
259
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';">
260
                          <?=$textss;?><?php if (isset($shaperent['protocol'])) echo strtoupper($shaperent['protocol']); else echo "*"; ?><?=$textse;?>
261
                        </td>
262
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';"><?=$textss;?><?php echo htmlspecialchars(pprint_address($shaperent['source'])); ?>
263
						<?php if ($shaperent['source']['port']): ?><br>
264
						Port: <?=htmlspecialchars(pprint_port($shaperent['source']['port'])); ?>
265
						<?php endif; ?><?=$textse;?>
266
                        </td>
267
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';"><?=$textss;?><?php echo htmlspecialchars(pprint_address($shaperent['destination'])); ?>
268
						<?php if ($shaperent['destination']['port']): ?><br>
269
						Port: <?=htmlspecialchars(pprint_port($shaperent['destination']['port'])); ?>
270
						<?php endif; ?><?=$textse;?>
271
                        </td>
272
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';"><?=$textss;?>
273
                          <?php
274
							if (isset($shaperent['outqueue']) && isset($shaperent['inqueue'])) {
275
								$desc = htmlspecialchars($shaperent['outqueue']);
276
							    echo "<a href=\"firewall_shaper_queues_edit.php?id={$shaperent['outqueue']}\">{$desc}</a>";
277
								$desc = htmlspecialchars($shaperent['inqueue']);
278
							    echo "/<a href=\"firewall_shaper_queues_edit.php?id={$shaperent['inqueue']}\">{$desc}</a>";
279
							}
280
						  ?><?=$textse;?>
281
                        </td>
282
                        <td class="listbg" onClick="fr_toggle(<?=$nrules;?>)" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';"><font color="white">
283
                          <?=$textss;?><?=htmlspecialchars($shaperent['descr']);?><?=$textse;?>
284
                          &nbsp; </td>
285
                        <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>
286
                          <?php if ($i > 0): ?>
287
                          <a href="firewall_shaper.php?act=up&id=<?=$i;?>"><img src="up.gif" title="move up" width="17" height="17" border="0"></a>
288
                          <?php else: ?>
289
                          <img src="up_d.gif" width="17" height="17" border="0">
290
                          <?php endif; ?>
291
			  <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>
292
			  <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>
293
                          <?php if (isset($a_shaper[$i+1])): ?>
294
                          <a href="firewall_shaper.php?act=down&id=<?=$i;?>"><img src="down.gif" title="move down" width="17" height="17" border="0"></a>
295
                          <?php else: ?>
296
                          <img src="down_d.gif" width="17" height="17" border="0">
297
                          <?php endif; ?>
298
                          <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>
299
                        </td>
300
                      </tr>
301
                      <?php $nrules++; $i++; endforeach; ?>
302
                      <tr>
303
                        <td class="list" colspan="8"></td>
304
                        <td class="list"> <a href="firewall_shaper_edit.php"><img src="plus.gif" width="17" height="17" border="0"></a></td>
305
                      </tr>
306
                    </table>
307

    
308
			        <p><span class="red"><strong>Note:</strong></span><strong><br>
309
                    </strong>the first rule that matches a packet will be executed.<br>
310
                    The following match patterns are not shown in the list above:
311
                    IP packet length, TCP flags.</td>
312
	</tr>
313
</table>
314
            </form>
315
<?php include("fend.inc"); ?>
316
</body>
317
</html>
(38-38/117)