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
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
7
	All rights reserved.
8

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

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

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

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

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

    
42
require("guiconfig.inc");
43
require_once("functions.inc");
44
require_once("filter.inc");
45
require_once("shaper.inc");
46

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

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

    
59
//More variables
60
$pgtitle = array(gettext("Firewall"),gettext("Traffic Shaper"), gettext("Layer7"));
61
$shortcut_section = "trafficshaper";
62

    
63
$output_form = "";
64

    
65
$default_layer7shaper_msg = "<tr><td colspan=\"4\">";
66
$default_layer7shaper_msg .= "<span class=\"vexpl\"><span class=\"red\"><strong>" . gettext("Note") . ":<br />";
67
$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></span><br />";
68
$default_layer7shaper_msg .= "</td></tr>";
69

    
70

    
71
read_layer7_config();
72

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

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

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

    
92
if ($name) {
93
	//Get the object from the 7rules list
94
	$container = $layer7_rules_list[$name];
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 .= $dn_default_shaper_msg . $default_layer7shaper_msg;
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
			if (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 lose 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 .= $dn_default_shaper_msg . $default_layer7shaper_msg;
196
		}
197
	} else if ($_POST['delete']) {
198
		$container->delete_l7c();
199
		if (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 .= $dn_default_shaper_msg . $default_layer7shaper_msg;
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 = "<table summary=\"output form\">";
225
$output .= $output_form;
226
$closehead = false;
227
include("head.inc");
228
?>
229
<link rel="stylesheet" type="text/css" media="all" href="./tree/tree.css" />
230
<script type="text/javascript" src="./tree/tree.js"></script>
231

    
232
<script type="text/javascript">
233
//<![CDATA[
234
var initial_count = new Array();
235
var rows_limit = 0; // Set to 0 to disable limitation
236

    
237

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

    
241
var js_behaviours_altq = new Array();
242
js_behaviours_altq = array_altq(js_behaviours_altq);
243

    
244
var js_behaviours_limiter = new Array();
245
js_behaviours_limiter = array_limiter(js_behaviours_limiter);
246

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

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

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

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

    
282
	return protocol;
283
}
284

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

    
302
	return structure;
303
}
304

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

    
315
	return behaviour;
316
}
317

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

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

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

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

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

    
360
  var remove = '<a onclick="removeRow(\''+table_id+'\',this.parentNode.parentNode)" href="#"><img border="0" src="/themes/<?=$g['theme'];?>/images/icons/icon_x.gif" alt="x" /><\/a>';
361

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

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

    
392
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
393

    
394
<?php include("fbegin.inc"); ?>
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 /></p>
403
<?php endif; ?>
404
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="traffic shaper layer7">
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" summary="main area">
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" align="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" alt="add" />  <?=gettext("Create new l7 rules group"); ?>
434
			</a><br />
435
			</td>
436
			<td width="75%" valign="top" align="center">
437
			<div id="shaperarea" style="position:relative">
438
			<?php
439
				echo $output;
440
			?>
441

    
442
			<!-- Layer 7 rules form -->
443
			<?php if($show_proto_form): ?>
444
			<tr><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" summary="main table">
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" style="font-size:8pt">
493
							<?php foreach($avail_protos as $proto): ?>
494
							<option value="<?=$proto;?>" <?php if ($proto == $l7rule->GetRProtocol()) echo "selected=\"selected\""; ?>><?=$proto;?></option>
495
							<?php endforeach; ?>
496
							</select>
497
						</td>
498
						<td>
499
							<select name="structure[]" class="formselect" 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=\"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=\"selected\""; ?>><?=$struct;?></option>
510
								<?php }
511
							    }
512
							    else {
513
							      if($struct == "action") { ?>
514
								  <option value="<?=$struct ?>" <?php if ($struct == $l7rule->GetRStructure()) echo "selected=\"selected\""; ?>><?=$struct;?></option>
515
							      <?php }
516
							    }
517
							  }
518
							} ?>
519
							</select>
520
						</td>
521
						<td>
522
							<select name="behaviour[]" class="formselect" 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=\"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=\"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=\"selected\""; ?>><?=$behaviour;?></option>
538
								<?php endforeach; ?>
539
								</select>
540
							<?php endif; ?>
541
						</td>
542
						<td>
543
							<a onclick="removeRow('maintable',this.parentNode.parentNode); return false;" href="#"><img border="0" src="/themes/<?=$g['theme'];?>/images/icons/icon_x.gif" alt="x" /></a>
544
						</td>
545
						</tr>
546

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

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

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

    
565
                        <td width="78%">
566
                                <input id="submit" name="submit" type="submit" class="formbtn" value="<?=gettext("Save"); ?>" />
567

    
568
				<a href="firewall_shaper_layer7.php">
569
                                <input id="cancelbutton" name="cancelbutton" type="button" class="formbtn" value="<?=gettext("Cancel"); ?>" />
570

    
571
				<?php if($container): ?>
572
						<input id="delete" type="submit" class="formbtn" name="delete" value="<?=gettext("Delete"); ?>" />
573
				<?php endif ?>
574
				</a>
575
                        </td>
576
                        </tr>
577
			<?php endif; ?>
578
			<!-- End of layer7 rules form -->
579
			</table>
580
			</div><!-- end of div:shape area -->
581

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

    
590
<?php include("fend.inc"); ?>
591
</body>
592
</html>
(76-76/252)