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
	pfSense_BUILDER_BINARIES:	/usr/bin/killall
31
	pfSense_MODULE:	shaper
32
*/
33

    
34
##|+PRIV
35
##|*IDENT=page-firewall-trafficshaper-layer7
36
##|*NAME=Firewall: Traffic Shaper: Layer7 page
37
##|*DESCR=Allow access to the 'Firewall: Traffic Shaper: Layer7' page.
38
##|*MATCH=firewall_shaper_layer7.php*
39
##|-PRIV
40

    
41
require("guiconfig.inc");
42

    
43
// Variables protocols (dynamic) and structures (static)
44
$avail_protos =& generate_protocols_array();
45
$avail_structures = array("action","queue","limiter");
46

    
47
// Available behaviours
48
$avail_behaviours_action = array("block");
49
read_altq_config();
50
$avail_behaviours_altq = get_altq_name_list();
51
read_dummynet_config();
52
$avail_behaviours_limiter = get_dummynet_name_list();
53
$show_proto_form = false;
54

    
55
//More variables
56
$pgtitle = array("Firewall","Traffic Shaper", "Layer7");
57

    
58
$output_form = "";
59

    
60
$default_layer7shaper_msg = "<tr><td colspan=\"4\">";
61
$default_layer7shaper_msg .= "<p><span class=\"vexpl\"><span class=\"red\"><strong>Note:<br>";
62
$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>";
63
$default_layer7shaper_msg .= "</td></tr>";
64

    
65

    
66
read_layer7_config();
67

    
68
if($_GET['reset'] <> "") {
69
	/* XXX: Huh!? Why are we killing php here? */
70
	mwexec("killall -9 pfctl php");
71
	// kill all ipfw-classifyd processes
72
	mwexec("killall -9 ipfw-classifyd"); 
73
	exit;
74
}
75

    
76
if ($_GET) {
77
	if ($_GET['container'])
78
        	$name = trim($_GET['container']);        
79
        if ($_GET['action'])
80
                $action = $_GET['action'];
81
}
82

    
83
if($_POST) {
84
	if ($_POST['container']) {
85
		$name = trim($_POST['container']);
86
	}
87
}
88

    
89
if ($name) {
90
	//Get the object from the 7rules list
91
	$container = $layer7_rules_list[$name];
92
}
93

    
94

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

    
121
//add a new l7rules container
122
else if ($_POST) {
123
	$show_proto_form = true;
124
	unset($input_errors);
125
	
126
	if($_POST['submit']) {
127
		$l7r =& new layer7();
128
		$_POST['divert_port'] = $l7r->gen_divert_port();
129
		for($i=0; $_POST['protocol'][$i] <> ""; $i++) {
130
			$_POST['l7rules'][$i]['protocol'] = $_POST['protocol'][$i];
131
			$_POST['l7rules'][$i]['structure'] = $_POST['structure'][$i];
132
			$_POST['l7rules'][$i]['behaviour'] = $_POST['behaviour'][$i];
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
			mark_subsystem_dirty('shaper');
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
		$retval = filter_configure();
175
		$savemsg = get_std_save_message($retval);
176

    
177
		if(stristr($retval, "error") <> true)
178
			$savemsg = get_std_save_message($retval);
179
		else
180
			$savemsg = $retval;
181

    
182
		clear_subsystem_dirty('shaper');
183

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

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

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

    
220
include("head.inc");
221
?>
222

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

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

    
232

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

    
236
var js_behaviours_altq = new Array();
237
js_behaviours_altq = array_altq(js_behaviours_altq);
238

    
239
var js_behaviours_limiter = new Array();
240
js_behaviours_limiter = array_limiter(js_behaviours_limiter);
241

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

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

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

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

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

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

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

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

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

    
391
<form action="firewall_shaper_layer7.php" method="post" id="iform" name="iform">
392

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

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

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

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

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

    
546
                                        <tfoot>
547
                                        </tfoot>
548
                                </table>
549

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

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

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

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

    
578
		      </td></tr>
579
                    </table>
580
		</div>
581
	  </td>
582
	</tr>
583
</table>
584
</form>
585

    
586
<?php include("fend.inc"); 
587
?>
588
</body>
589
</html>
(61-61/215)