Project

General

Profile

Download (18.7 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
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 = htmlspecialchars(trim($_GET['container']));
81
	}
82
	if ($_GET['action']) {
83
		$action = htmlspecialchars($_GET['action']);
84
	}
85
}
86

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

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

    
98
if ($_GET) {
99
	switch ($action) {
100
		case "add":
101
			$show_proto_form = true;
102
			$container = new layer7();
103
			$output_form .= $container->build_form(); //constructs the graphical interface on the right side
104
			unset($container);
105
			break;
106
		case "show":
107
			$show_proto_form = true;
108
			if ($container) {
109
				$output_form .= $container->build_form();
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
} else if ($_POST) {
122
	//add a new l7rules container
123
	$show_proto_form = true;
124
	unset($input_errors);
125

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

    
158
			read_layer7_config();
159
		} else {
160
			if (sizeof($dupes) > 0) {
161
				$dupe_error = gettext("Found the following repeated protocol definitions") . ": ";
162
				foreach ($dupes as $dupe) {
163
					$dupe_error .= "$dupe ";
164
				}
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
		}
178
	} else if ($_POST['apply']) {
179
		write_config();
180

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

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

    
191
		clear_subsystem_dirty('shaper');
192

    
193
		if ($container) {
194
			$output_form .= $container->build_form();
195
		} else {
196
			$show_proto_form = false;
197
			$output_form .= $dn_default_shaper_msg . $default_layer7shaper_msg;
198
		}
199
	} else if ($_POST['delete']) {
200
		$container->delete_l7c();
201
		if (write_config()) {
202
			mark_subsystem_dirty('shaper');
203
		}
204
		unset($container);
205

    
206
		header("Location: firewall_shaper_layer7.php");
207
		exit;
208
	} else {
209
		$show_proto_form = false;
210
	}
211
} else {
212
	$show_proto_form = false;
213
	$output_form .= $dn_default_shaper_msg . $default_layer7shaper_msg;
214
}
215

    
216
// Builds the left tree
217
$tree = "<ul class=\"tree\" >";
218
if (is_array($layer7_rules_list)) {
219
	foreach ($layer7_rules_list as $tmpl7) {
220
		$tree .= $tmpl7->build_tree();
221
	}
222
}
223
$tree .= "</ul>";
224
$closehead = false;
225
include("head.inc");
226
?>
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 type="text/javascript">
231
//<![CDATA[
232
var initial_count = new Array();
233
var rows_limit = 0; // Set to 0 to disable limitation
234

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

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

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

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

    
258
function array_limiter(a_behav) {
259
	var index;
260
	<?php
261
	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
	} ?>
269
	return a_behav;
270
}
271

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

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

    
283
	return protocol;
284
}
285

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

    
307
	return structure;
308
}
309

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

    
320
	return behaviour;
321
}
322

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

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

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

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

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

    
363
	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>';
364

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

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

    
395
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
396

    
397
<?php include("fbegin.inc"); ?>
398
<div id="inputerrors"></div>
399
<?php if ($input_errors) print_input_errors($input_errors); ?>
400

    
401
<form action="firewall_shaper_layer7.php" method="post" id="iform" name="iform">
402

    
403
<?php if ($savemsg) print_info_box($savemsg); ?>
404
<?php if (is_subsystem_dirty('shaper')): ?><p>
405
<?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>
406
<?php endif; ?>
407
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="traffic shaper layer7">
408
	<tr><td>
409
<?php
410
	$tab_array = array();
411
	$tab_array[0] = array(gettext("By Interface"), false, "firewall_shaper.php");
412
	$tab_array[1] = array(gettext("By Queue"), false, "firewall_shaper_queues.php");
413
	$tab_array[2] = array(gettext("Limiter"), false, "firewall_shaper_vinterface.php");
414
	$tab_array[3] = array(gettext("Layer7"), true, "firewall_shaper_layer7.php");
415
	$tab_array[4] = array(gettext("Wizards"), false, "firewall_shaper_wizards.php");
416
	display_top_tabs($tab_array);
417
?>
418
	</td></tr>
419
	<tr>
420
		<td>
421
			<div id="mainarea">
422
				<table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0" summary="main area">
423

    
424
				<?php if (count($layer7_rules_list) > 0): ?>
425
					<tr class="tabcont">
426
						<td width="25%" align="left"></td>
427
						<td width="75%"> </td>
428
					</tr>
429
				<?php endif; ?>
430
					<tr>
431
						<td width="25%" valign="top" align="left">
432
						<?php
433
							echo $tree;
434
						?>
435
							<br /><br />
436
							<a href="firewall_shaper_layer7.php?action=add">
437
								<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"); ?>
438
							</a>
439
							<br />
440
						</td>
441
						<td width="75%" valign="top" align="center">
442
							<div id="shaperarea" style="position:relative">
443
							<table summary="output form">
444
							<?php
445
								echo $output_form;
446
							?>
447

    
448
						<!-- Layer 7 rules form -->
449
						<?php if ($show_proto_form): ?>
450
								<tr>
451
									<td width="22%" valign="top" class="vncellreq">
452
										<div id="addressnetworkport">
453
												<?=gettext("Rule(s)"); ?>
454
										</div>
455
									</td>
456

    
457
									<td width="78%" class="vtable">
458
										<table width="236" id="maintable" summary="main table">
459
											<tbody>
460
												<tr>
461
													<td colspan="4">
462
														<div style="font-size: 8pt; padding:5px; margin-top: 16px; margin-bottom: 16px; border:1px dashed #000066;" id="itemhelp">
463
															<?=gettext("Add one or more rules"); ?>
464
														</div>
465
													</td>
466
												</tr>
467
												<tr>
468
													<td>
469
														<div style="font-size: 8pt; padding:5px;" id="onecolumn">
470
															<?=gettext("Protocol"); ?>
471
														</div>
472
													</td>
473
													<td>
474
														<div style="font-size: 8pt; padding:5px;" id="twocolumn">
475
															<?=gettext("Structure"); ?>
476
														</div>
477
													</td>
478
													<td>
479
														<div style="font-size: 8pt; padding:5px;" id="threecolumn">
480
															<?=gettext("Behaviour"); ?>
481
														</div>
482
													</td>
483
												</tr>
484
												<!-- PHP Code to generate the existing rules -->
485
										<?php
486
										if ($container) {
487
											foreach ($container->rsets as $l7rule) {
488
										?>
489
												<tr>
490
													<td>
491
														<select name="protocol[]" class="formselect" style="font-size:8pt">
492
										<?php 	foreach ($avail_protos as $proto): ?>
493
															<option value="<?=$proto;?>" <?php if ($proto == $l7rule->GetRProtocol()) echo "selected=\"selected\""; ?>><?=$proto;?></option>
494
										<?php 	endforeach; ?>
495
														</select>
496
													</td>
497
													<td>
498
														<select name="structure[]" class="formselect" style="font-size:8pt" onchange="changeBehaviourValues(this.parentNode.parentNode);">
499
										<?php 	foreach ($avail_structures as $struct) {
500
													if ($struct == "queue") {
501
														if (!empty($avail_behaviours_altq)) { ?>
502
															<option value="<?=$struct ?>" <?php if ($struct == $l7rule->GetRStructure()) echo "selected=\"selected\""; ?>><?=$struct;?></option>
503
										<?php 			}
504
													} else {
505
														if ($struct == "limiter") {
506
															if (!empty($avail_behaviours_limiter)) { ?>
507
															<option value="<?=$struct ?>" <?php if ($struct == $l7rule->GetRStructure()) echo "selected=\"selected\""; ?>><?=$struct;?></option>
508
										<?php 				}
509
														} else {
510
															if ($struct == "action") { ?>
511
															<option value="<?=$struct ?>" <?php if ($struct == $l7rule->GetRStructure()) echo "selected=\"selected\""; ?>><?=$struct;?></option>
512
										<?php 				}
513
														}
514
													}
515
												} ?>
516
														</select>
517
													</td>
518
													<td>
519
														<select name="behaviour[]" class="formselect" style="width:80px; font-size:8pt">
520
										<?php if ($l7rule->GetRStructure() == "action"): ?>
521
											<?php foreach ($avail_behaviours_action as $behaviour): ?>
522
															<option value="<?=$behaviour ?>" <?php if ($behaviour == $l7rule->GetRBehaviour()) echo "selected=\"selected\""; ?>><?=$behaviour;?></option>
523
											<?php endforeach; ?>
524
										<?php 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=\"selected\""; ?>><?=$behaviour;?></option>
528
											<?php endforeach; ?>
529
										<?php endif; ?>
530
										<?php if ($l7rule->GetRStructure() == "limiter"): ?>
531
											<?php foreach ($avail_behaviours_limiter as $behaviour): ?>
532
															<option value="<?=$behaviour ?>" <?php if ($behaviour == $l7rule->GetRBehaviour()) echo "selected=\"selected\""; ?>><?=$behaviour;?></option>
533
											<?php endforeach; ?>
534
										<?php endif; ?>
535
														</select>
536
													</td>
537
													<td>
538
														<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>
539
													</td>
540
												</tr>
541
										<?php
542
											} //end foreach
543
										} //end if
544
										?>
545
											</tbody>
546
										</table>
547

    
548
										<a onclick="javascript:addRow('maintable'); return false;" href="#"> <img border="0"
549
											src="/themes/<?=$g['theme']; ?>/images/icons/icon_plus.gif"
550
											alt="" title="<?=gettext("add another entry"); ?>" /> </a>
551
									</td>
552
								</tr>
553
								<tr>
554
									<td width="22%" valign="top">
555
										&nbsp;
556
									</td>
557
									<td width="78%">
558
										<input id="submit" name="submit" type="submit" class="formbtn" value="<?=gettext("Save"); ?>" />
559

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

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

    
582
<?php include("fend.inc"); ?>
583
</body>
584
</html>
(76-76/256)