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
		$retval = filter_configure();
170
		$savemsg = get_std_save_message($retval);
171

    
172
		if(stristr($retval, "error") <> true)
173
			$savemsg = get_std_save_message($retval);
174
		else
175
			$savemsg = $retval;
176

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

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

    
212
$output = "<div id=\"shaperarea\" style=\"position:relative\">";
213
$output .= $output_form;
214

    
215
include("head.inc");
216
?>
217

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

    
224
var initial_count = new Array();
225
var rows_limit = 0; // Set to 0 to disable limitation
226

    
227

    
228
/* Build the behaviours arrays in javascript */
229
var js_behaviours_action = ['block']; //static
230

    
231
var js_behaviours_altq = new Array();
232
js_behaviours_altq = array_altq(js_behaviours_altq);
233

    
234
var js_behaviours_limiter = new Array();
235
js_behaviours_limiter = array_limiter(js_behaviours_limiter);
236

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

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

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

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

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

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

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

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

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

    
386
<form action="firewall_shaper_layer7.php" method="post" id="iform" name="iform">
387

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

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

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

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

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

    
541
                                        <tfoot>
542
                                        </tfoot>
543
                                </table>
544

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

    
551
                        <tr>
552
                        <td width = "22%" valign = "top">
553
                                &nbsp;
554
                        </td>
555

    
556
                        <td width = "78%">
557
                                <input id = "submit"
558
                                name = "submit" type = "submit" class = "formbtn" value = "Save" />
559

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

    
573
		      </td></tr>
574
                    </table>
575
		</div>
576
	  </td>
577
	</tr>
578
</table>
579
</form>
580

    
581
<?php include("fend.inc"); 
582
?>
583
</body>
584
</html>
(56-56/206)