Project

General

Profile

Download (14.6 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
  Header("Location: firewall_shaper.php");
64
  exit;
65
}
66

    
67
if ($_POST) {
68

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

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

    
89
	        if(file_exists($d_shaperconfdirty_path))
90
	          unlink($d_shaperconfdirty_path);
91
	}
92
}
93

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

    
107

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

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

    
156
		/* copy all selected rules */
157
		for ($i = 0; $i < count($a_shaper); $i++) {
158
			if ($i == $movebtn)
159
				continue;
160
			if (in_array($i, $_POST['rule']))
161
				$a_shaper_new[] = $a_shaper[$i];
162
		}
163

    
164
		/* copy $movebtn rule */
165
		if ($movebtn < count($a_shaper))
166
			$a_shaper_new[] = $a_shaper[$movebtn];
167

    
168
		/* copy all rules > $movebtn and not selected */
169
		for ($i = $movebtn+1; $i < count($a_shaper); $i++) {
170
			if (!in_array($i, $_POST['rule']))
171
				$a_shaper_new[] = $a_shaper[$i];
172
		}
173

    
174
		$a_shaper = $a_shaper_new;
175
		write_config();
176
		touch($d_shaperconfdirty_path);
177
		header("Location: firewall_shaper.php");
178
		exit;
179
	}
180
}
181

    
182
$pgtitle = "Firewall: Shaper: Rules";
183
include("head.inc");
184

    
185
?>
186

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

    
262
				  echo "</a>" . $textse;;
263
				  ?>
264
                        </td>
265
                        <td class="listr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';">
266
                          <?=$textss;?><?php if (isset($shaperent['protocol'])) echo strtoupper($shaperent['protocol']); else echo "*"; ?><?=$textse;?>
267
                        </td>
268
                        <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'])); ?>
269
						<?php if ($shaperent['source']['port']): ?><br>
270
						Port: <?=htmlspecialchars(pprint_port($shaperent['source']['port'])); ?>
271
						<?php endif; ?><?=$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['destination'])); ?>
274
						<?php if ($shaperent['destination']['port']): ?><br>
275
						Port: <?=htmlspecialchars(pprint_port($shaperent['destination']['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;?>
279
                          <?php
280
							if (isset($shaperent['outqueue']) && isset($shaperent['inqueue'])) {
281
								$desc = htmlspecialchars($shaperent['outqueue']);
282
							    echo "<a href=\"firewall_shaper_queues_edit.php?id={$shaperent['outqueue']}\">{$desc}</a>";
283
								$desc = htmlspecialchars($shaperent['inqueue']);
284
							    echo "/<a href=\"firewall_shaper_queues_edit.php?id={$shaperent['inqueue']}\">{$desc}</a>";
285
							}
286
						  ?><?=$textse;?>
287
                        </td>
288
                        <td class="listbg" onClick="fr_toggle(<?=$nrules;?>)" ondblclick="document.location='firewall_shaper_edit.php?id=<?=$i;?>';"><font color="white">
289
                          <?=$textss;?><?=htmlspecialchars($shaperent['descr']);?><?=$textse;?>
290
                          &nbsp; </td>
291
                        <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>
292
                          <?php if ($i > 0): ?>
293
                          <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>
294
                          <?php else: ?>
295
                          <img src="./themes/<?= $g['theme']; ?>/images/icons/icon_up_d.gif" width="17" height="17" border="0">
296
                          <?php endif; ?>
297
			  <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>
298
			  
299
			  <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?')">
300
			  
301
                          <?php if (isset($a_shaper[$i+1])): ?>
302
                          <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>
303
                          <?php else: ?>
304
                          <img src="./themes/<?= $g['theme']; ?>/images/icons/icon_down_d.gif" width="17" height="17" border="0">
305
                          <?php endif; ?>
306
                          <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>
307
                        </td>
308
                      </tr>
309
                      <?php $nrules++; $i++; endforeach; ?>
310
                      <tr>
311
                        <td class="list" colspan="8"></td>
312
                        <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>
313
                      </tr>
314
		      <tr>
315
		    <td colspan="8"><p><span class="red"><strong>Note:</strong></span><strong><br>
316
                    </strong>the first rule that matches a packet will be executed.<br>
317
                    The following match patterns are not shown in the list above:
318
                    IP packet length, TCP flags.<br>
319
                    You can check the results of your queues at <a href="status_queues.php">Status:Queues</a>.</td>
320
		    </tr>
321
                    </table>
322
		</div>
323
	  </td>
324
	</tr>
325
</table>
326
            </form>
327
<?php include("fend.inc"); ?>
328
</body>
329
</html>
(42-42/137)