Project

General

Profile

Download (14.8 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
/* redirect to wizard if shaper isn't already configured */
47
if(isset($config['shaper']['enable'])) {
48
	$pconfig['enable'] = TRUE;
49
} else {
50
	Header("Location: wizard.php?xml=traffic_shaper_wizard.xml");
51
}
52

    
53
function wipe_magic () {
54
  global $config;
55

    
56
  /* wipe previous */
57
  unset($config['shaper']['queue']);
58
  unset($config['shaper']['rule']);
59
  $config['shaper']['enable'] = FALSE;
60
}
61

    
62
if ($_POST['remove']) {
63
  wipe_magic();
64
  $savemsg = '<p><span class="red"><strong>Note: The traffic shaper has been disabled.</strong></span><strong><br>';
65
  touch($d_shaperconfdirty_path);
66
  unset($config['shaper']['enable']);
67
  write_config();
68
  Header("Location: firewall_shaper.php");
69
  exit;
70
}
71

    
72
if ($_POST) {
73

    
74
	if ($_POST['submit']) {
75
		$pconfig = $_POST;
76
		$config['shaper']['enable'] = $_POST['enable'] ? true : false;
77
		write_config();
78
	}
79

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

    
94
	        if(file_exists($d_shaperconfdirty_path))
95
	          unlink($d_shaperconfdirty_path);
96
	}
97
}
98

    
99
if (isset($_POST['del_x'])) {
100
        /* delete selected rules */
101
        if (is_array($_POST['rule']) && count($_POST['rule'])) {
102
                foreach ($_POST['rule'] as $rulei) {
103
                        unset($a_shaper[$rulei]);
104
                }
105
                write_config();
106
                touch($d_natconfdirty_path);
107
                header("Location: firewall_shaper.php");
108
                exit;
109
        }
110
}
111

    
112

    
113
if ($_GET['act'] == "down") {
114
	if ($a_shaper[$_GET['id']] && $a_shaper[$_GET['id']+1]) {
115
		$tmp = $a_shaper[$_GET['id']+1];
116
		$a_shaper[$_GET['id']+1] = $a_shaper[$_GET['id']];
117
		$a_shaper[$_GET['id']] = $tmp;
118
		write_config();
119
		touch($d_shaperconfdirty_path);
120
		header("Location: firewall_shaper.php");
121
		exit;
122
	}
123
} else if ($_GET['act'] == "up") {
124
	if (($_GET['id'] > 0) && $a_shaper[$_GET['id']]) {
125
		$tmp = $a_shaper[$_GET['id']-1];
126
		$a_shaper[$_GET['id']-1] = $a_shaper[$_GET['id']];
127
		$a_shaper[$_GET['id']] = $tmp;
128
		write_config();
129
		touch($d_shaperconfdirty_path);
130
		header("Location: firewall_shaper.php");
131
		exit;
132
	}
133
} else if ($_GET['act'] == "toggle") {
134
	if ($a_shaper[$_GET['id']]) {
135
		$a_shaper[$_GET['id']]['disabled'] = !isset($a_shaper[$_GET['id']]['disabled']);
136
		write_config();
137
		touch($d_shaperconfdirty_path);
138
		header("Location: firewall_shaper.php");
139
		exit;
140
	}
141
} else {
142
	/* yuck - IE won't send value attributes for image buttons, while Mozilla does -
143
	   so we use .x/.y to fine move button clicks instead... */
144
	unset($movebtn);
145
	foreach ($_POST as $pn => $pd) {
146
		if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
147
			$movebtn = $matches[1];
148
			break;
149
		}
150
	}
151
	/* move selected rules before this rule */
152
	if (isset($movebtn) && is_array($_POST['rule']) && count($_POST['rule'])) {
153
		$a_shaper_new = array();
154

    
155
		/* copy all rules < $movebtn and not selected */
156
		for ($i = 0; $i < $movebtn; $i++) {
157
			if (!in_array($i, $_POST['rule']))
158
				$a_shaper_new[] = $a_shaper[$i];
159
		}
160

    
161
		/* copy all selected rules */
162
		for ($i = 0; $i < count($a_shaper); $i++) {
163
			if ($i == $movebtn)
164
				continue;
165
			if (in_array($i, $_POST['rule']))
166
				$a_shaper_new[] = $a_shaper[$i];
167
		}
168

    
169
		/* copy $movebtn rule */
170
		if ($movebtn < count($a_shaper))
171
			$a_shaper_new[] = $a_shaper[$movebtn];
172

    
173
		/* copy all rules > $movebtn and not selected */
174
		for ($i = $movebtn+1; $i < count($a_shaper); $i++) {
175
			if (!in_array($i, $_POST['rule']))
176
				$a_shaper_new[] = $a_shaper[$i];
177
		}
178

    
179
		$a_shaper = $a_shaper_new;
180
		write_config();
181
		touch($d_shaperconfdirty_path);
182
		header("Location: firewall_shaper.php");
183
		exit;
184
	}
185
}
186

    
187
$pgtitle = "Firewall: Shaper: Rules";
188
include("head.inc");
189

    
190
?>
191

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

    
267
				  echo "</a>" . $textse;;
268
				  ?>
269
                        </td>
270
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';">
271
                          <?=$textss;?><?php if (isset($shaperent['protocol'])) echo strtoupper($shaperent['protocol']); else echo "*"; ?><?=$textse;?>
272
                        </td>
273
                        <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'])); ?>
274
						<?php if ($shaperent['source']['port']): ?><br>
275
						Port: <?=htmlspecialchars(pprint_port($shaperent['source']['port'])); ?>
276
						<?php endif; ?><?=$textse;?>
277
                        </td>
278
                        <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'])); ?>
279
						<?php if ($shaperent['destination']['port']): ?><br>
280
						Port: <?=htmlspecialchars(pprint_port($shaperent['destination']['port'])); ?>
281
						<?php endif; ?><?=$textse;?>
282
                        </td>
283
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';"><?=$textss;?>
284
                          <?php
285
							if (isset($shaperent['outqueue']) && isset($shaperent['inqueue'])) {
286
								$desc = htmlspecialchars($shaperent['outqueue']);
287
							    echo "<a href=\"firewall_shaper_queues_edit.php?id={$shaperent['outqueue']}\">{$desc}</a>";
288
								$desc = htmlspecialchars($shaperent['inqueue']);
289
							    echo "/<a href=\"firewall_shaper_queues_edit.php?id={$shaperent['inqueue']}\">{$desc}</a>";
290
							}
291
						  ?><?=$textse;?>
292
                        </td>
293
                        <td class="listbg" onClick="fr_toggle(<?=$nrules;?>)" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';"><font color="white">
294
                          <?=$textss;?><?=htmlspecialchars($shaperent['descr']);?><?=$textse;?>
295
                          &nbsp; </td>
296
                        <td valign="middle" nowrap class="list"> <a href="firewall_shaper_edit.php?id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" title="edit rule" width="17" height="17" border="0"></a>
297
                          <?php if ($i > 0): ?>
298
                          <a href="firewall_shaper.php?act=up&id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_up.gif" title="move up" width="17" height="17" border="0"></a>
299
                          <?php else: ?>
300
                          <img src="./themes/<?= $g['theme']; ?>/images/icons/icon_up_d.gif" width="17" height="17" border="0">
301
                          <?php endif; ?>
302
			  <input name="move_<?=$i;?>" type="image" src="./themes/<?= $g['theme']; ?>/images/icons/icon_left.gif" width="17" height="17" title="move selected rules before this rule" onMouseOver="fr_insline(<?=$nrules;?>, true)" onMouseOut="fr_insline(<?=$nrules;?>, false)"><br>
303
			  
304
			  <input name="del" type="image" src="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" title="delete selected mappings" onclick="return confirm('Do you really want to delete the selected mappings?')">
305
			  
306
                          <?php if (isset($a_shaper[$i+1])): ?>
307
                          <a href="firewall_shaper.php?act=down&id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_down.gif" title="move down" width="17" height="17" border="0"></a>
308
                          <?php else: ?>
309
                          <img src="./themes/<?= $g['theme']; ?>/images/icons/icon_down_d.gif" width="17" height="17" border="0">
310
                          <?php endif; ?>
311
                          <a href="firewall_shaper_edit.php?dup=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="add a new rule based on this one" width="17" height="17" border="0"></a>
312
                        </td>
313
                      </tr>
314
                      <?php $nrules++; $i++; endforeach; ?>
315
                      <tr>
316
                        <td class="list" colspan="8"></td>
317
                        <td class="list"> <a href="firewall_shaper_edit.php"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0"></a></td>
318
                      </tr>
319
		      <tr>
320
		    <td colspan="8"><p><span class="red"><strong>Note:</strong></span><strong><br>
321
                    </strong>The last rule that matches a packet will be executed.<br>
322
                    The following match patterns are not shown in the list above:
323
                    IP packet length, TCP flags.<br>
324
                    You can check the results of your queues at <a href="status_queues.php">Status:Queues</a>.</td>
325
		    </tr>
326
                    </table>
327
		</div>
328
	  </td>
329
	</tr>
330
</table>
331
            </form>
332
<?php include("fend.inc"); ?>
333
</body>
334
</html>
(45-45/153)