Project

General

Profile

Download (20.1 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
require_once("functions.inc");
43
require_once("filter.inc");
44
require_once("shaper.inc");
45

    
46
// Variables protocols (dynamic) and structures (static)
47
$avail_protos =& generate_protocols_array();
48
$avail_structures = array("action","queue","limiter");
49

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

    
58
//More variables
59
$pgtitle = array(gettext("Firewall"),gettext("Traffic Shaper"), gettext("Layer7"));
60
$statusurl = "status_queues.php";
61

    
62
$output_form = "";
63

    
64
$default_layer7shaper_msg = "<tr><td colspan=\"4\">";
65
$default_layer7shaper_msg .= "<p><span class=\"vexpl\"><span class=\"red\"><strong>" . gettext("Note") . ":<br>";
66
$default_layer7shaper_msg .= "</strong></span>" . gettext("You can add new layer7 protocol patterns by simply uploading the file") . " <a href=\"diag_patterns.php\">" . gettext("here") . ".</a><br>";
67
$default_layer7shaper_msg .= "</td></tr>";
68

    
69

    
70
read_layer7_config();
71

    
72
if($_GET['reset'] <> "") {
73
	// kill all ipfw-classifyd processes
74
	mwexec("killall -9 ipfw-classifyd");
75
	exit;
76
}
77

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

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

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

    
96

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

    
123
//add a new l7rules container
124
else if ($_POST) {
125
	$show_proto_form = true;
126
	unset($input_errors);
127

    
128
	if($_POST['submit']) {
129
		if (isset($layer7_rules_list[$name])) {
130
			$l7r = $layer7_rules_list[$name];
131
			$_POST['divert_port'] = $l7r->GetRPort();
132
		} else {
133
			$l7r =& new layer7();
134
			$_POST['divert_port'] = $l7r->gen_divert_port();
135
		}
136
		for($i=0; $_POST['protocol'][$i] <> ""; $i++) {
137
			$_POST['l7rules'][$i]['protocol'] = $_POST['protocol'][$i];
138
			$_POST['l7rules'][$i]['structure'] = $_POST['structure'][$i];
139
			$_POST['l7rules'][$i]['behaviour'] = $_POST['behaviour'][$i];
140
		}
141
		$l7r->validate_input($_POST,&$input_errors);
142
		$l7r->ReadConfig($_POST['container'], $_POST);
143
		//Before writing the results, we need to test for repeated protocols
144
		$non_dupes = array();
145
		$dupes = array();
146
		for($j=0; $j<$i; $j++) {
147
			if(!$non_dupes[$_POST['protocol'][$j]])
148
				$non_dupes[$_POST['protocol'][$j]] = true;
149
			else
150
				$dupes[] = $_POST['protocol'][$j];
151
		}
152
		unset($non_dupes);
153
		if(sizeof($dupes) == 0 && !$input_errors) {
154
			$l7r->wconfig();
155
			write_config();
156
			mark_subsystem_dirty('shaper');
157

    
158
			read_layer7_config();
159
		}
160
		else {
161
			if(sizeof($dupes) > 0) {
162
				$dupe_error = gettext("Found the following repeated protocol definitions") . ": ";
163
				foreach($dupes as $dupe)
164
					$dupe_error .= "$dupe ";
165
				$input_errors[] .= $dupe_error;
166
			}
167
		}
168
		unset($dupes);
169
		unset($dupe_error);
170
		//Even if there are repeated protocols, we won't loose any previous values
171
		//The user will be able to solve the situation
172
		$output_form .= $l7r->build_form();
173
		//Necessary to correctly build the proto form
174
		$container = $layer7_rules_list[$name];
175
		if($input_errors)
176
			$container =& $l7r;
177
	} else if($_POST['apply']) {
178
		write_config();
179

    
180
		$retval = 0;
181
		$retval = filter_configure();
182
		$savemsg = get_std_save_message($retval);
183

    
184
		if(stristr($retval, "error") <> true)
185
			$savemsg = get_std_save_message($retval);
186
		else
187
			$savemsg = $retval;
188

    
189
		clear_subsystem_dirty('shaper');
190

    
191
		if($container) {
192
			$output_form .= $container->build_form();
193
		} else {
194
			$show_proto_form = false;
195
			$output_form .= "<p class=\"pgtitle\">" . $dn_default_shaper_msg . $default_layer7shaper_msg . "</p>";
196
		}
197
	} else if ($_POST['delete']) {
198
		$container->delete_l7c();
199
		write_config();
200
		mark_subsystem_dirty('shaper');
201
		unset($container);
202

    
203
		header("Location: firewall_shaper_layer7.php");
204
		exit;
205
	}
206
	else {
207
		$show_proto_form = false;
208
	}
209
}
210
else {
211
	$show_proto_form = false;
212
	$output_form .= "<p class=\"pgtitle\">" . $dn_default_shaper_msg . $default_layer7shaper_msg . "</p>";
213
}
214

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

    
224
$output = "<div id=\"shaperarea\" style=\"position:relative\">";
225
$output .= $output_form;
226

    
227
include("head.inc");
228
?>
229

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

    
234
<script language="javascript">
235

    
236
var initial_count = new Array();
237
var rows_limit = 0; // Set to 0 to disable limitation
238

    
239

    
240
/* Build the behaviours arrays in javascript */
241
var js_behaviours_action = ['block']; //static
242

    
243
var js_behaviours_altq = new Array();
244
js_behaviours_altq = array_altq(js_behaviours_altq);
245

    
246
var js_behaviours_limiter = new Array();
247
js_behaviours_limiter = array_limiter(js_behaviours_limiter);
248

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

    
261
function array_limiter(a_behav) {
262
	var index;
263
	<?php if (!empty($avail_behaviours_limiter)) {
264
	  foreach ($avail_behaviours_limiter as $key => $limiter) { ?>
265
		name = "<?= $limiter; ?>";
266
		index = <?= $key; ?>;
267
		a_behav[index] = name;
268
	<?php }
269
	} ?>
270
	return a_behav;
271
}
272

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

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

    
284
	return protocol;
285
}
286

    
287
function fillStructure() {
288
	var structure = '<select name="structure[]" id="structure" style="font-size:8pt" onchange="changeBehaviourValues(this.parentNode.parentNode);">';
289
	var name;
290
	<?php foreach ($avail_structures as $key => $struct) { ?>
291
		name = "<?= $struct; ?>";
292
		if(name == "queue") {
293
		  if(js_behaviours_altq != "") { structure += "<option value=" + name + ">" + name + "</option>";}
294
		}
295
		else {
296
		  if(name == "limiter") {
297
		    if(js_behaviours_limiter != "") { structure += "<option value=" + name + ">" + name + "</option>";}
298
		  }
299
		  else structure += "<option value=" + name + ">" + name + "</option>"; //action
300
		}
301
	<?php } ?>
302
	structure += "</select>";
303

    
304
	return structure;
305
}
306

    
307
//Used by default to fill the values when inserting a new row.
308
function fillBehaviour() {
309
	var behaviour = '<select name="behaviour[]" id="behaviour" style="width:80px; font-size:8pt">';
310
	var name;
311
	<?php foreach ($avail_behaviours_action as $key => $behav) { ?>
312
		name = "<?= $behav; ?>";
313
		behaviour += "<option value=" + name + ">" + name + "</option>";
314
	<?php } ?>
315
	behaviour += "</select>";
316

    
317
	return behaviour;
318
}
319

    
320
/* Change the values on behaviours select when changing the structure row */
321
function changeBehaviourValues(row) {
322
	var selectedRow = row.rowIndex - 2; //because row.rowIndex returns 2, not 0
323
	var structureSelected = document.getElementsByName("structure[]")[selectedRow].value;
324

    
325
	//Select the behaviours values to array a_behav
326
	var a_behav = new Array();
327
	if (structureSelected == "action") {
328
		a_behav = js_behaviours_action; //static
329
	}
330
	else {
331
		if (structureSelected == "queue") {
332
			a_behav = js_behaviours_altq;
333
		}
334
		else {
335
			a_behav = js_behaviours_limiter;
336
		}
337
	}
338

    
339
	//Build the html statement with the array values previously selected
340
	var new_behav;
341
	var name;
342
	for(i=0; i<a_behav.length; i++) {
343
		new_behav += "<option value=" + a_behav[i] + ">" + a_behav[i] + "</option>";
344
	}
345

    
346
	document.getElementsByName("behaviour[]")[selectedRow].innerHTML = new_behav;
347
}
348

    
349
/* Add row to the table */
350
function addRow(table_id) {
351
  var tbl = document.getElementById(table_id);
352
  // counting rows in table
353
  var rows_count = tbl.rows.length;
354
  if (initial_count[table_id] == undefined) {
355
    // if it is first adding in this table setting initial rows count
356
    initial_count[table_id] = rows_count;
357
  }
358
  // determining real count of added fields
359
  var tFielsNum =  rows_count - initial_count[table_id];
360
  if (rows_limit!=0 && tFielsNum >= rows_limit) return false;
361

    
362
  var remove = '<input type = "image" src = "/themes/<?=$g['theme'];?>/images/icons/icon_x.gif" onclick="removeRow(\''+table_id+'\',this.parentNode.parentNode)" value = "Delete" />';
363

    
364
  try {
365
    var newRow = tbl.insertRow(rows_count);
366
    var newCell = newRow.insertCell(0);
367
    newCell.innerHTML = fillProtocol();
368
    var newCell = newRow.insertCell(1);
369
    newCell.innerHTML = fillStructure();
370
    var newCell = newRow.insertCell(2);
371
    newCell.innerHTML = fillBehaviour();
372
    var newCell = newRow.insertCell(3);
373
    newCell.innerHTML = remove;
374
  }
375
  catch (ex) {
376
    //if exception occurs
377
    alert(ex);
378
  }
379
}
380

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

    
392
<?php
393
include("fbegin.inc");
394
?>
395
<div id="inputerrors"></div>
396
<?php if ($input_errors) print_input_errors($input_errors); ?>
397

    
398
<form action="firewall_shaper_layer7.php" method="post" id="iform" name="iform">
399

    
400
<?php if ($savemsg) print_info_box($savemsg); ?>
401
<?php if (is_subsystem_dirty('shaper')): ?><p>
402
<?php print_info_box_np(gettext("The traffic shaper configuration has been changed")  .  ".<br>" . gettext("You must apply the changes in order for them to take effect."));?><br>
403
<?php endif; ?>
404
<table width="100%" border="0" cellpadding="0" cellspacing="0">
405
  <tr><td>
406
<?php
407
	$tab_array = array();
408
	$tab_array[0] = array(gettext("By Interface"), false, "firewall_shaper.php");
409
	$tab_array[1] = array(gettext("By Queue"), false, "firewall_shaper_queues.php");
410
	$tab_array[2] = array(gettext("Limiter"), false, "firewall_shaper_vinterface.php");
411
	$tab_array[3] = array(gettext("Layer7"), true, "firewall_shaper_layer7.php");
412
	$tab_array[4] = array(gettext("Wizards"), false, "firewall_shaper_wizards.php");
413
	display_top_tabs($tab_array);
414
?>
415
  </td></tr>
416
  <tr>
417
    <td>
418
	<div id="mainarea">
419
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
420

    
421
		<?php if (count($layer7_rules_list) > 0): ?>
422
                        <tr class="tabcont"><td width="25%" align="left">
423
                        </td><td width="75%"> </td></tr>
424

    
425
		<?php endif; ?>
426
			<tr>
427
			<td width="25%" valign="top" algin="left">
428
			<?php
429
				echo $tree;
430
			?>
431
			<br/><br/>
432
			<a href="firewall_shaper_layer7.php?action=add">
433
			<img src="./themes/<?=$g['theme']; ?>/images/icons/icon_plus.gif" title="<?=gettext("Create new l7 rules group"); ?>" width="17" height="17" border="0">  <?=gettext("Create new l7 rules group"); ?>
434
			</a><br/>
435
			</td>
436
			<td width="75%" valign="top" align="center">
437
			<table>
438
			<?
439
				echo $output;
440
			?>
441

    
442
			<!-- Layer 7 rules form -->
443
			<?php if($show_proto_form): ?>
444
			<td width = "22%" valign = "top" class = "vncellreq">
445
                                <div id = "addressnetworkport">
446
                                        <?=gettext("Rule(s)"); ?>
447
                                </div>
448
                        </td>
449

    
450
                        <td width = "78%" class = "vtable">
451
                                <table width="236" id = "maintable">
452
					<tbody>
453

    
454
						<tr>
455
                                                        <td colspan = "4">
456
                                                            <div style = "font-size: 8pt; padding:5px; margin-top: 16px; margin-bottom: 16px; border:1px dashed #000066;"
457
                                                                id = "itemhelp">
458
                                                                <?=gettext("Add one or more rules"); ?>
459
                                                            </div>
460
                                                        </td>
461
                                                </tr>
462

    
463
                                                <tr>
464
                                                        <td>
465
                                                            <div style = "font-size: 8pt; padding:5px;"
466
                                                                id = "onecolumn">
467
                                                                <?=gettext("Protocol"); ?>
468
                                                            </div>
469
                                                        </td>
470

    
471
                                                        <td>
472
                                                            <div style = "font-size: 8pt; padding:5px;"
473
                                                                id = "twocolumn">
474
                                                                <?=gettext("Structure"); ?>
475
                                                            </div>
476
                                                        </td>
477

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

    
547
						<?php
548
							} //end foreach
549
						} //end if
550
						?>
551
                                        </tbody>
552

    
553
                                        <tfoot>
554
                                        </tfoot>
555
                                </table>
556

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

    
563
                        <tr>
564
                        <td width = "22%" valign = "top">
565
                                &nbsp;
566
                        </td>
567

    
568
                        <td width = "78%">
569
                                <input id = "submit"
570
                                name = "submit" type = "submit" class = "formbtn" value = "<?=gettext("Save"); ?>" />
571

    
572
				<a href= "firewall_shaper_layer7.php">
573
                                <input id = "cancelbutton"
574
                                name = "cancelbutton" type = "button" class = "formbtn" value = "<?=gettext("Cancel"); ?>" /></a>
575

    
576
				<?php if($container): ?>
577
						<input id = "delete" type="submit" class="formbtn" name="delete" value="<?=gettext("Delete"); ?>"></a>
578
				<?php endif ?>
579
                        </td>
580
                        </tr>
581
			<?php endif; ?>
582
			<!-- End of layer7 rules form -->
583
			</table>
584

    
585
		      </td></tr>
586
                    </table>
587
		</div>
588
	  </td>
589
	</tr>
590
</table>
591
</form>
592

    
593
<?php include("fend.inc");
594
?>
595
</body>
596
</html>
(62-62/222)