Project

General

Profile

Download (20.4 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_unique_queue_list();
47
read_dummynet_config();
48
$avail_behaviours_limiter = get_unique_dnqueue_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; $i<100; $i++) {
125
			if($_POST['protocol'][$i] <> "") {
126
				$_POST['l7rules'][$i]['protocol'] = $_POST['protocol'][$i];
127
				$_POST['l7rules'][$i]['structure'] = $_POST['structure'][$i];
128
				$_POST['l7rules'][$i]['behaviour'] = $_POST['behaviour'][$i];
129
			}
130
			else {
131
				break;
132
			}
133
		}
134
		$l7r->validate_input($_POST,&$input_errors);
135
		$l7r->ReadConfig($_POST['container'], $_POST);
136
		//Before writing the results, we need to test for repeated protocols
137
		$non_dupes = array();
138
		$dupes = array();
139
		for($j=0; $j<$i; $j++) {
140
			if(!$non_dupes[$_POST['protocol'][$j]])
141
				$non_dupes[$_POST['protocol'][$j]] = true;
142
			else
143
				$dupes[] = $_POST['protocol'][$j];
144
		}
145
		unset($non_dupes);
146
		if(sizeof($dupes) == 0 && !$input_errors) {
147
			$l7r->wconfig();
148
			write_config();
149
			touch($d_shaperconfdirty_path);
150
		
151
			read_layer7_config();
152
		}
153
		else {
154
			if(sizeof($dupes) > 0) {
155
				$dupe_error = "Found the following repeated protocol definitions: ";
156
				foreach($dupes as $dupe)
157
					$dupe_error .= "$dupe ";
158
				$input_errors[] .= $dupe_error;
159
			}
160
		}
161
		unset($dupes);
162
		unset($dupe_error);
163
		//Even if there are repeated protocols, we won't loose any previous values
164
		//The user will be able to solve the situation
165
		$output_form .= $l7r->build_form();
166
		//Necessary to correctly build the proto form
167
		$container = $layer7_rules_list[$name];
168
		if($input_errors)
169
			$container =& $l7r;
170
	} else if($_POST['apply']) {
171
		write_config();
172

    
173
		$retval = 0;
174
		$savemsg = get_std_save_message($retval);
175
			
176
		config_lock();
177
		$retval = filter_configure();
178
		config_unlock();
179
			
180
		if(stristr($retval, "error") <> true)
181
			$savemsg = get_std_save_message($retval);
182
		else
183
			$savemsg = $retval;
184

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

    
211
// Builds the left tree
212
$tree = "<ul class=\"tree\" >";
213
if (is_array($layer7_rules_list)) {
214
        foreach ($layer7_rules_list as $tmpl7) {
215
                $tree .= $tmpl7->build_tree();
216
        }
217
}
218
$tree .= "</ul>";
219

    
220
$output = "<div id=\"shaperarea\" style=\"position:relative\">";
221
$output .= $output_form;
222

    
223
include("head.inc");
224
?>
225

    
226
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" >
227
<link rel="stylesheet" type="text/css" media="all" href="./tree/tree.css" />
228
<script type="text/javascript" src="./tree/tree.js"></script>
229
                                        
230
<script language="javascript">
231

    
232
var initial_count = new Array();
233
var rows_limit = 0; // Set to 0 to disable limitation
234

    
235

    
236
/* Build the behaviours arrays in javascript */
237
var js_behaviours_action = ['block']; //static
238

    
239
var js_behaviours_altq = new Array();
240
js_behaviours_altq = array_altq(js_behaviours_altq);
241

    
242
var js_behaviours_limiter = new Array();
243
js_behaviours_limiter = array_limiter(js_behaviours_limiter);
244

    
245
function array_altq(a_behav) {
246
	var index;
247
	<? if (!empty($avail_behaviours_altq)) {
248
	  foreach ($avail_behaviours_altq as $key => $queue) { ?>
249
	    name = "<?= $queue; ?>";
250
	    index = <? echo $key; ?>;
251
	    a_behav[index] = name;
252
	<? }
253
	} ?>
254
	return a_behav;
255
}
256

    
257
function array_limiter(a_behav) {
258
	var index;
259
	<? if (!empty($avail_behaviours_limiter)) {
260
	  foreach ($avail_behaviours_limiter as $key => $limiter) { ?>
261
		name = "<?= $limiter; ?>";
262
		index = <? echo $key; ?>;
263
		a_behav[index] = name;
264
	<? }
265
	} ?>
266
	return a_behav;
267
}
268

    
269
/* Fill the variables with available protocols, structures and behaviours */
270
function fillProtocol() {
271
	var protocol = '<select name="protocol[]" id="protocol" style="font-size:8pt">';
272
	var name;
273

    
274
	<? foreach ($avail_protos as $key => $proto) { ?>
275
		name = "<?= $proto; ?>";
276
		protocol += "<option value=" + name + ">" + name + "</option>";
277
	<? } ?>
278
	protocol += "</select>";
279
	
280
	return protocol;
281
}
282

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

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

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

    
377
/* Remove row from the table */
378
function removeRow(tbl,row) {
379
  var table = document.getElementById(tbl);
380
  try {
381
    table.deleteRow(row.rowIndex);
382
  } catch (ex) {
383
    alert(ex);
384
  }
385
}
386
</script>
387

    
388
<?php
389
include("fbegin.inc"); 
390
?>
391
<div id="inputerrors"></div>
392
<?php if ($input_errors) print_input_errors($input_errors); ?>
393

    
394
<form action="firewall_shaper_layer7.php" method="post" id="iform" name="iform">
395

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

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

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

    
467
                                                        <td>
468
                                                            <div style = "font-size: 8pt; padding:5px;"
469
                                                                id = "twocolumn">
470
                                                                Structure
471
                                                            </div>
472
                                                        </td>
473

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

    
549
                                        <tfoot>
550
                                        </tfoot>
551
                                </table>
552

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

    
559
                        <tr>
560
                        <td width = "22%" valign = "top">
561
                                &nbsp;
562
                        </td>
563

    
564
                        <td width = "78%">
565
                                <input id = "submit"
566
                                name = "submit" type = "submit" class = "formbtn" value = "Save" />
567

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

    
581
		      </td></tr>
582
                    </table>
583
		</div>
584
	  </td>
585
	</tr>
586
</table>
587
</form>
588

    
589
<?php include("fend.inc"); 
590
?>
591
</body>
592
</html>
(60-60/214)