Project

General

Profile

Download (20.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	firewall_shaper_layer7.php
5
	Copyright (C) 2008 Helder Pereira, Andr? Ribeiro
6
	All rights reserved.
7

    
8
	Redistribution and use in source and binary forms, with or without
9
	modification, are permitted provided that the following conditions are met:
10

    
11
	1. Redistributions of source code must retain the above copyright notice,
12
	   this list of conditions and the following disclaimer.
13

    
14
	2. Redistributions in binary form must reproduce the above copyright
15
	   notice, this list of conditions and the following disclaimer in the
16
	   documentation and/or other materials provided with the distribution.
17

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

    
30
##|+PRIV
31
##|*IDENT=page-firewall-trafficshaper-layer7
32
##|*NAME=Firewall: Traffic Shaper: Layer7 page
33
##|*DESCR=Allow access to the 'Firewall: Traffic Shaper: Layer7' page.
34
##|*MATCH=firewall_shaper_layer7.php*
35
##|-PRIV
36

    
37
require("guiconfig.inc");
38

    
39
// Variables protocols (dynamic) and structures (static)
40
$avail_protos =& generate_protocols_array();
41
$avail_structures = array("action","queue","limiter");
42

    
43
// Available behaviours
44
$avail_behaviours_action = array("block");
45
read_altq_config();
46
$avail_behaviours_altq = get_altq_name_list();
47
read_dummynet_config();
48
$avail_behaviours_limiter = get_dummynet_name_list();
49
$show_proto_form = false;
50

    
51

    
52
//More variables
53
$pgtitle = array("Firewall","Traffic Shaper", "Layer7");
54

    
55
$output_form = "";
56

    
57
$default_layer7shaper_msg = "<tr><td colspan=\"4\">";
58
$default_layer7shaper_msg .= "<p><span class=\"vexpl\"><span class=\"red\"><strong>Note:<br>";
59
$default_layer7shaper_msg .= "</strong></span>You can add new layer7 protocol patterns by simply uploading the file <a href=\"diag_patterns.php\">here.</a><br>";
60
$default_layer7shaper_msg .= "</td></tr>";
61

    
62

    
63
read_layer7_config();
64

    
65
if($_GET['reset'] <> "") {
66
        mwexec("killall -9 pfctl php");
67
	mwexec("killall -9 ipfw-classifyd"); //kill all ipfw-classifyd processes
68
	exit;
69
}
70

    
71
if ($_GET) {
72
	if ($_GET['container'])
73
        	$name = trim($_GET['container']);        
74
        if ($_GET['action'])
75
                $action = $_GET['action'];
76
}
77

    
78
if($_POST) {
79
	if ($_POST['container']) {
80
		$name = trim($_POST['container']);
81
	}
82
}
83

    
84
if ($name) {
85
	//Get the object from the 7rules list
86
	$container = $layer7_rules_list[$name];
87
}
88

    
89

    
90
if ($_GET) {
91
	switch ($action) {
92
		case "add":
93
			$show_proto_form = true;
94
			$container = new layer7();
95
			$output_form .= $container->build_form(); //constructs the graphical interface on the right side
96
			unset($container);
97
			break;
98
		case "show":
99
			$show_proto_form = true;
100
			if($container) {
101
				$output_form .= $container->build_form();
102
			}
103
			else {
104
				$show_proto_form = false;
105
				$input_errors[] = "Layer7 Rules Container not found!";
106
			}
107
			break;
108
		default:
109
			echo log_error("Get default");
110
			$show_proto_form = false;
111
			$output_form .= "<p class=\"pgtitle\">" . $dn_default_shaper_msg . $default_layer7shaper_msg . "</p>";
112
			break;
113
	}
114
}
115

    
116
//add a new l7rules container
117
else if ($_POST) {
118
	$show_proto_form = true;
119
	unset($input_errors);
120
	
121
	if($_POST['submit']) {
122
		$l7r =& new layer7();
123
		$_POST['divert_port'] = $l7r->gen_divert_port();
124
		for($i=0; $_POST['protocol'][$i] <> ""; $i++) {
125
			$_POST['l7rules'][$i]['protocol'] = $_POST['protocol'][$i];
126
			$_POST['l7rules'][$i]['structure'] = $_POST['structure'][$i];
127
			$_POST['l7rules'][$i]['behaviour'] = $_POST['behaviour'][$i];
128
		}
129
		$l7r->validate_input($_POST,&$input_errors);
130
		$l7r->ReadConfig($_POST['container'], $_POST);
131
		//Before writing the results, we need to test for repeated protocols
132
		$non_dupes = array();
133
		$dupes = array();
134
		for($j=0; $j<$i; $j++) {
135
			if(!$non_dupes[$_POST['protocol'][$j]])
136
				$non_dupes[$_POST['protocol'][$j]] = true;
137
			else
138
				$dupes[] = $_POST['protocol'][$j];
139
		}
140
		unset($non_dupes);
141
		if(sizeof($dupes) == 0 && !$input_errors) {
142
			$l7r->wconfig();
143
			write_config();
144
			touch($d_shaperconfdirty_path);
145
		
146
			read_layer7_config();
147
		}
148
		else {
149
			if(sizeof($dupes) > 0) {
150
				$dupe_error = "Found the following repeated protocol definitions: ";
151
				foreach($dupes as $dupe)
152
					$dupe_error .= "$dupe ";
153
				$input_errors[] .= $dupe_error;
154
			}
155
		}
156
		unset($dupes);
157
		unset($dupe_error);
158
		//Even if there are repeated protocols, we won't loose any previous values
159
		//The user will be able to solve the situation
160
		$output_form .= $l7r->build_form();
161
		//Necessary to correctly build the proto form
162
		$container = $layer7_rules_list[$name];
163
		if($input_errors)
164
			$container =& $l7r;
165
	} else if($_POST['apply']) {
166
		write_config();
167

    
168
		$retval = 0;
169
		$savemsg = get_std_save_message($retval);
170
			
171
		config_lock();
172
		$retval = filter_configure();
173
		config_unlock();
174
			
175
		if(stristr($retval, "error") <> true)
176
			$savemsg = get_std_save_message($retval);
177
		else
178
			$savemsg = $retval;
179

    
180
		unlink($d_shaperconfdirty_path);
181
		
182
		if($container) {
183
			$output_form .= $container->build_form();
184
		} else {
185
			$show_proto_form = false;
186
			$output_form .= "<p class=\"pgtitle\">" . $dn_default_shaper_msg . $default_layer7shaper_msg . "</p>";
187
		}
188
	} else if ($_POST['delete']) {
189
		$container->delete_l7c();
190
		write_config();
191
		touch($d_shaperconfdirty_path);
192
		unset($container);
193
		
194
		header("Location: firewall_shaper_layer7.php");
195
		exit;
196
	}
197
	else {
198
		$show_proto_form = false;
199
	}
200
}
201
else {
202
	$show_proto_form = false;
203
	$output_form .= "<p class=\"pgtitle\">" . $dn_default_shaper_msg . $default_layer7shaper_msg . "</p>";
204
}
205

    
206
// Builds the left tree
207
$tree = "<ul class=\"tree\" >";
208
if (is_array($layer7_rules_list)) {
209
        foreach ($layer7_rules_list as $tmpl7) {
210
                $tree .= $tmpl7->build_tree();
211
        }
212
}
213
$tree .= "</ul>";
214

    
215
$output = "<div id=\"shaperarea\" style=\"position:relative\">";
216
$output .= $output_form;
217

    
218
include("head.inc");
219
?>
220

    
221
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" >
222
<link rel="stylesheet" type="text/css" media="all" href="./tree/tree.css" />
223
<script type="text/javascript" src="./tree/tree.js"></script>
224
                                        
225
<script language="javascript">
226

    
227
var initial_count = new Array();
228
var rows_limit = 0; // Set to 0 to disable limitation
229

    
230

    
231
/* Build the behaviours arrays in javascript */
232
var js_behaviours_action = ['block']; //static
233

    
234
var js_behaviours_altq = new Array();
235
js_behaviours_altq = array_altq(js_behaviours_altq);
236

    
237
var js_behaviours_limiter = new Array();
238
js_behaviours_limiter = array_limiter(js_behaviours_limiter);
239

    
240
function array_altq(a_behav) {
241
	var index;
242
	<? if (!empty($avail_behaviours_altq)) {
243
	  foreach ($avail_behaviours_altq as $key => $queue) { ?>
244
	    name = "<?= $queue; ?>";
245
	    index = <? echo $key; ?>;
246
	    a_behav[index] = name;
247
	<? }
248
	} ?>
249
	return a_behav;
250
}
251

    
252
function array_limiter(a_behav) {
253
	var index;
254
	<? if (!empty($avail_behaviours_limiter)) {
255
	  foreach ($avail_behaviours_limiter as $key => $limiter) { ?>
256
		name = "<?= $limiter; ?>";
257
		index = <? echo $key; ?>;
258
		a_behav[index] = name;
259
	<? }
260
	} ?>
261
	return a_behav;
262
}
263

    
264
/* Fill the variables with available protocols, structures and behaviours */
265
function fillProtocol() {
266
	var protocol = '<select name="protocol[]" id="protocol" style="font-size:8pt">';
267
	var name;
268

    
269
	<? foreach ($avail_protos as $key => $proto) { ?>
270
		name = "<?= $proto; ?>";
271
		protocol += "<option value=" + name + ">" + name + "</option>";
272
	<? } ?>
273
	protocol += "</select>";
274
	
275
	return protocol;
276
}
277

    
278
function fillStructure() {
279
	var structure = '<select name="structure[]" id="structure" style="font-size:8pt" onchange="changeBehaviourValues(this.parentNode.parentNode);">';
280
	var name;
281
	<? foreach ($avail_structures as $key => $struct) { ?>
282
		name = "<?= $struct; ?>";
283
		if(name == "queue") {
284
		  if(js_behaviours_altq != "") { structure += "<option value=" + name + ">" + name + "</option>";}
285
		}
286
		else {
287
		  if(name == "limiter") {
288
		    if(js_behaviours_limiter != "") { structure += "<option value=" + name + ">" + name + "</option>";}
289
		  }
290
		  else structure += "<option value=" + name + ">" + name + "</option>"; //action		  		
291
		}		
292
	<? } ?>
293
	structure += "</select>";
294
	
295
	return structure;
296
}
297
		
298
//Used by default to fill the values when inserting a new row.
299
function fillBehaviour() {
300
	var behaviour = '<select name="behaviour[]" id="behaviour" style="width:80px; font-size:8pt">';
301
	var name;
302
	<? foreach ($avail_behaviours_action as $key => $behav) { ?>
303
		name = "<?= $behav; ?>";
304
		behaviour += "<option value=" + name + ">" + name + "</option>";
305
	<? } ?>
306
	behaviour += "</select>";
307
	
308
	return behaviour;
309
}
310

    
311
/* Change the values on behaviours select when changing the structure row */
312
function changeBehaviourValues(row) {
313
	var selectedRow = row.rowIndex - 2; //because row.rowIndex returns 2, not 0
314
	var structureSelected = document.getElementsByName("structure[]")[selectedRow].value;		
315
	
316
	//Select the behaviours values to array a_behav
317
	var a_behav = new Array();	
318
	if (structureSelected == "action") {
319
		a_behav = js_behaviours_action; //static
320
	}
321
	else {
322
		if (structureSelected == "queue") {			
323
			a_behav = js_behaviours_altq;
324
		}
325
		else {							
326
			a_behav = js_behaviours_limiter;
327
		}					
328
	}			
329
	
330
	//Build the html statement with the array values previously selected
331
	var new_behav;
332
	var name;
333
	for(i=0; i<a_behav.length; i++) {
334
		new_behav += "<option value=" + a_behav[i] + ">" + a_behav[i] + "</option>";	
335
	}
336
		
337
	document.getElementsByName("behaviour[]")[selectedRow].innerHTML = new_behav;		
338
}
339

    
340
/* Add row to the table */
341
function addRow(table_id) {
342
  var tbl = document.getElementById(table_id);
343
  // counting rows in table
344
  var rows_count = tbl.rows.length;
345
  if (initial_count[table_id] == undefined) {
346
    // if it is first adding in this table setting initial rows count
347
    initial_count[table_id] = rows_count;
348
  }
349
  // determining real count of added fields
350
  var tFielsNum =  rows_count - initial_count[table_id];
351
  if (rows_limit!=0 && tFielsNum >= rows_limit) return false;
352
  
353
  var remove = '<input type = "image" src = "/themes/<?echo $g['theme'];?>/images/icons/icon_x.gif" onclick="removeRow(\''+table_id+'\',this.parentNode.parentNode)" value = "Delete" />';
354
  
355
  try {
356
    var newRow = tbl.insertRow(rows_count);
357
    var newCell = newRow.insertCell(0);
358
    newCell.innerHTML = fillProtocol();
359
    var newCell = newRow.insertCell(1);
360
    newCell.innerHTML = fillStructure();
361
    var newCell = newRow.insertCell(2);
362
    newCell.innerHTML = fillBehaviour();
363
    var newCell = newRow.insertCell(3);
364
    newCell.innerHTML = remove;
365
  }   
366
  catch (ex) {
367
    //if exception occurs
368
    alert(ex);
369
  }   
370
}
371

    
372
/* Remove row from the table */
373
function removeRow(tbl,row) {
374
  var table = document.getElementById(tbl);
375
  try {
376
    table.deleteRow(row.rowIndex);
377
  } catch (ex) {
378
    alert(ex);
379
  }
380
}
381
</script>
382

    
383
<?php
384
include("fbegin.inc"); 
385
?>
386
<div id="inputerrors"></div>
387
<?php if ($input_errors) print_input_errors($input_errors); ?>
388

    
389
<form action="firewall_shaper_layer7.php" method="post" id="iform" name="iform">
390

    
391
<?php if ($savemsg) print_info_box($savemsg); ?>
392
<?php if (file_exists($d_shaperconfdirty_path)): ?><p>
393
<?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>
394
<?php endif; ?>
395
<table width="100%" border="0" cellpadding="0" cellspacing="0">
396
  <tr><td>
397
<?php
398
	$tab_array = array();
399
	$tab_array[0] = array("By Interface", false, "firewall_shaper.php");
400
	$tab_array[1] = array("By Queue", false, "firewall_shaper_queues.php");
401
	$tab_array[2] = array("Limiter", false, "firewall_shaper_vinterface.php");
402
	$tab_array[3] = array("Layer7", true, "firewall_shaper_layer7.php");
403
	$tab_array[4] = array("Wizards", false, "firewall_shaper_wizards.php");
404
	display_top_tabs($tab_array);
405
?>
406
  </td></tr>
407
  <tr>
408
    <td>
409
	<div id="mainarea">
410
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
411
		
412
		<?php if (count($layer7_rules_list) > 0): ?>
413
                        <tr class="tabcont"><td width="25%" align="left">
414
                        </td><td width="75%"> </td></tr>
415
		
416
		<? endif; ?>
417
			<tr>
418
			<td width="25%" valign="top" algin="left">
419
			<?php
420
				echo $tree; 
421
			?>
422
			<br/><br/>
423
			<a href="firewall_shaper_layer7.php?action=add">
424
			<img src="./themes/<?=$g['theme']; ?>/images/icons/icon_plus.gif" title="Create new l7 rules group" width="17" height="17" border="0">  Create new l7 rules group
425
			</a><br/>
426
			</td>
427
			<td width="75%" valign="top" align="center">
428
			<table>
429
			<?
430
				echo $output;
431
			?>
432
			
433
			<!-- Layer 7 rules form -->
434
			<?php if($show_proto_form): ?>
435
			<td width = "22%" valign = "top" class = "vncellreq">
436
                                <div id = "addressnetworkport">
437
                                        Rule(s)
438
                                </div>
439
                        </td>
440

    
441
                        <td width = "78%" class = "vtable">
442
                                <table width="236" id = "maintable">
443
					<tbody>
444
                                                    
445
						<tr>
446
                                                        <td colspan = "4">
447
                                                            <div style = "font-size: 8pt; padding:5px; margin-top: 16px; margin-bottom: 16px; border:1px dashed #000066;"
448
                                                                id = "itemhelp">
449
                                                                Add one or more rules
450
                                                            </div>
451
                                                        </td>
452
                                                </tr>
453

    
454
                                                <tr>
455
                                                        <td>
456
                                                            <div style = "font-size: 8pt; padding:5px;"
457
                                                                id = "onecolumn">
458
                                                                Protocol
459
                                                            </div>
460
                                                        </td>
461

    
462
                                                        <td>
463
                                                            <div style = "font-size: 8pt; padding:5px;"
464
                                                                id = "twocolumn">
465
                                                                Structure
466
                                                            </div>
467
                                                        </td>
468

    
469
                                                        <td>
470
                                                            <div style = "font-size: 8pt; padding:5px;"
471
                                                                id = "threecolumn">
472
                                                                Behaviour
473
                                                            </div>
474
                                                        </td>
475
                                                </tr>                                                                                                        
476
                                                <!-- PHP Code to generate the existing rules -->
477
						<?php
478
						if($container) {
479
							foreach($container->rsets as $l7rule) {
480
						?>
481
						<tr>
482
							<td>
483
							<select name="protocol[]" class="formselect" id="protocol" style="font-size:8pt">
484
							<?php foreach($avail_protos as $proto): ?>
485
							<option value="<?=$proto;?>" <?php if ($proto == $l7rule->GetRProtocol()) echo "selected"; ?>><?=$proto;?></option>
486
							<? endforeach; ?>
487
							</select>
488
						</td>
489
						<td>
490
							<select name="structure[]" class="formselect" id="structure" style="font-size:8pt" onchange="changeBehaviourValues(this.parentNode.parentNode);">
491
							<?php foreach($avail_structures as $struct) {
492
							  if($struct == "queue") {
493
							    if(!empty($avail_behaviours_altq)) { ?>
494
							      <option value="<?=$struct ?>" <?php if ($struct == $l7rule->GetRStructure()) echo "selected"; ?>><?=$struct;?></option>
495
							    <?php }
496
							  }
497
							  else {
498
							    if($struct == "limiter") {
499
								if(!empty($avail_behaviours_limiter)) { ?>
500
								  <option value="<?=$struct ?>" <?php if ($struct == $l7rule->GetRStructure()) echo "selected"; ?>><?=$struct;?></option>
501
								<?php }
502
							    }
503
							    else {
504
							      if($struct == "action") { ?>
505
								  <option value="<?=$struct ?>" <?php if ($struct == $l7rule->GetRStructure()) echo "selected"; ?>><?=$struct;?></option>
506
							      <?php }							      							      
507
							    }
508
							  }
509
							} ?>
510
							</select>
511
						</td>
512
						<td>
513
							<select name="behaviour[]" class="formselect" id="behaviour" style="width:80px; font-size:8pt">
514
							<?php if($l7rule->GetRStructure() == "action"): ?>
515
								<?php foreach($avail_behaviours_action as $behaviour): ?>
516
								<option value="<?=$behaviour ?>" <?php if ($behaviour == $l7rule->GetRBehaviour()) echo "selected"; ?>><?=$behaviour;?></option>
517
								<? endforeach; ?>
518
								</select>
519
							<? endif; ?>
520
							<?php if($l7rule->GetRStructure() == "queue"): ?>
521
								<?php foreach($avail_behaviours_altq as $behaviour): ?>
522
								<option value="<?=$behaviour ?>" <?php if ($behaviour == $l7rule->GetRBehaviour()) echo "selected"; ?>><?=$behaviour;?></option>
523
								<? endforeach; ?>
524
								</select>
525
							<? endif; ?>
526
							<?php if($l7rule->GetRStructure() == "limiter"): ?>
527
								<?php foreach($avail_behaviours_limiter as $behaviour): ?>
528
								<option value="<?=$behaviour ?>" <?php if ($behaviour == $l7rule->GetRBehaviour()) echo "selected"; ?>><?=$behaviour;?></option>
529
								<? endforeach; ?>
530
								</select>
531
							<? endif; ?>							
532
						</td>
533
						<td>
534
							<input type="image" src="/themes/<? echo $g['theme'];?>/images/icons/icon_x.gif" onclick="removeRow('maintable',this.parentNode.parentNode); return false;" value="Delete" />
535
						</td>
536
						</tr>
537
						
538
						<?php
539
							} //end foreach
540
						} //end if
541
						?>  
542
                                        </tbody>
543

    
544
                                        <tfoot>
545
                                        </tfoot>
546
                                </table>
547

    
548
                                        <a onclick = "javascript:addRow('maintable'); return false;" href="#"> <img border = "0"
549
                                                src = "/themes/<?=$g['theme']; ?>/images/icons/icon_plus.gif"
550
                                                alt = "" title = "add another entry" /> </a>
551
                        </td>
552
			</tr>
553

    
554
                        <tr>
555
                        <td width = "22%" valign = "top">
556
                                &nbsp;
557
                        </td>
558

    
559
                        <td width = "78%">
560
                                <input id = "submit"
561
                                name = "submit" type = "submit" class = "formbtn" value = "Save" />
562

    
563
				<a href= "firewall_shaper_layer7.php">
564
                                <input id = "cancelbutton"
565
                                name = "cancelbutton" type = "button" class = "formbtn" value = "Cancel" /></a>
566
				
567
				<?php if($container): ?>
568
						<input id = "delete" type="submit" class="formbtn" name="delete" value="Delete"></a>
569
				<? endif ?>
570
                        </td>
571
                        </tr>
572
			<? endif; ?>
573
			<!-- End of layer7 rules form -->
574
			</table>
575

    
576
		      </td></tr>
577
                    </table>
578
		</div>
579
	  </td>
580
	</tr>
581
</table>
582
</form>
583

    
584
<?php include("fend.inc"); 
585
?>
586
</body>
587
</html>
(56-56/206)