Project

General

Profile

Download (36.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * firewall_rules.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
##|+PRIV
29
##|*IDENT=page-firewall-rules
30
##|*NAME=Firewall: Rules
31
##|*DESCR=Allow access to the 'Firewall: Rules' page.
32
##|*MATCH=firewall_rules.php*
33
##|-PRIV
34

    
35
require_once("guiconfig.inc");
36
require_once("functions.inc");
37
require_once("filter.inc");
38
require_once("ipsec.inc");
39
require_once("shaper.inc");
40

    
41
$XmoveTitle = gettext("Move checked rules above this one. Shift+Click to move checked rules below.");
42
$ShXmoveTitle = gettext("Move checked rules below this one. Release shift to move checked rules above.");
43

    
44
$shortcut_section = "firewall";
45

    
46
function get_pf_rules($rules, $tracker_start, $tracker_end) {
47

    
48
	if ($rules == NULL || !is_array($rules)) {
49
		return (NULL);
50
	}
51

    
52
	$arr = array();
53
	foreach ($rules as $rule) {
54
		if ($rule['tracker'] >= $tracker_start &&
55
		    $rule['tracker'] <= $tracker_end) {
56
			$arr[] = $rule;
57
		}
58
	}
59

    
60
	if (count($arr) == 0)
61
		return (NULL);
62

    
63
	return ($arr);
64
}
65

    
66
function print_states($tracker_start, $tracker_end = -1) {
67
	global $rulescnt;
68

    
69
	if (empty($tracker_start)) {
70
		return;
71
	}
72

    
73
	if ($tracker_end === -1) {
74
		$tracker_end = $tracker_start;
75
	} elseif ($tracker_end < $tracker_start) {
76
		return;
77
	}
78

    
79
	$rulesid = "";
80
	$bytes = 0;
81
	$states = 0;
82
	$packets = 0;
83
	$evaluations = 0;
84
	$stcreations = 0;
85
	$rules = get_pf_rules($rulescnt, $tracker_start, $tracker_end);
86
	if (is_array($rules)) {
87
		foreach ($rules as $rule) {
88
			$bytes += $rule['bytes'];
89
			$states += $rule['states'];
90
			$packets += $rule['packets'];
91
			$evaluations += $rule['evaluations'];
92
			$stcreations += $rule['state creations'];
93
			if (strlen($rulesid) > 0) {
94
				$rulesid .= ",";
95
			}
96
			$rulesid .= "{$rule['id']}";
97
		}
98
	}
99

    
100
	$trackertext = "Tracking ID: {$tracker_start}";
101
	if ($tracker_end != $tracker_start) {
102
		$trackertext .= '-' . $tracker_end;
103
	}
104
	$trackertext .= "<br />";
105

    
106
	printf("<a href=\"diag_dump_states.php?ruleid=%s\" " .
107
	    "data-toggle=\"popover\" data-trigger=\"hover focus\" " .
108
	    "title=\"%s\" ", $rulesid, gettext("States details"));
109
	printf("data-content=\"{$trackertext}evaluations: %s<br />packets: " .
110
	    "%s<br />bytes: %s<br />states: %s<br />state creations: " .
111
	    "%s\" data-html=\"true\" usepost>",
112
	    format_number($evaluations), format_number($packets),
113
	    format_bytes($bytes), format_number($states),
114
	    format_number($stcreations));
115
	printf("%s/%s</a><br />", format_number($states), format_bytes($bytes));
116
}
117

    
118
function delete_nat_association($id) {
119
	global $config;
120

    
121
	if (!$id || !is_array($config['nat']['rule'])) {
122
		return;
123
	}
124

    
125
	$a_nat = &$config['nat']['rule'];
126

    
127
	foreach ($a_nat as &$natent) {
128
		if ($natent['associated-rule-id'] == $id) {
129
			$natent['associated-rule-id'] = '';
130
		}
131
	}
132
}
133

    
134
init_config_arr(array('filter', 'rule'));
135
filter_rules_sort();
136
$a_filter = &$config['filter']['rule'];
137

    
138
if ($_REQUEST['if']) {
139
	$if = $_REQUEST['if'];
140
}
141

    
142
$ifdescs = get_configured_interface_with_descr();
143

    
144
$iflist = filter_get_interface_list();
145

    
146
if (!$if || !isset($iflist[$if])) {
147
	if ($if != "any" && $if != "FloatingRules" && isset($iflist['wan'])) {
148
		$if = "wan";
149
	} else {
150
		$if = "FloatingRules";
151
	}
152
}
153

    
154
if ($_POST['apply']) {
155
	$retval = 0;
156
	$retval |= filter_configure();
157

    
158
	clear_subsystem_dirty('filter');
159
}
160

    
161
if ($_POST['act'] == "del") {
162
	if ($a_filter[$_POST['id']]) {
163
		if (!empty($a_filter[$_POST['id']]['associated-rule-id'])) {
164
			delete_nat_association($a_filter[$_POST['id']]['associated-rule-id']);
165
		}
166
		unset($a_filter[$_POST['id']]);
167

    
168
		// Update the separators
169
		init_config_arr(array('filter', 'separator', strtolower($if)));
170
		$a_separators = &$config['filter']['separator'][strtolower($if)];
171
		$ridx = ifridx($if, $_POST['id']);	// get rule index within interface
172
		$mvnrows = -1;
173
		move_separators($a_separators, $ridx, $mvnrows);
174

    
175
		if (write_config(gettext("Firewall: Rules - deleted a firewall rule."))) {
176
			mark_subsystem_dirty('filter');
177
		}
178

    
179
		header("Location: firewall_rules.php?if=" . htmlspecialchars($if));
180
		exit;
181
	}
182
}
183

    
184
// Handle save msg if defined
185
if ($_REQUEST['savemsg']) {
186
	$savemsg = htmlentities($_REQUEST['savemsg']);
187
}
188

    
189
if (isset($_POST['del_x'])) {
190
	/* delete selected rules */
191
	$deleted = false;
192

    
193
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
194
		init_config_arr(array('filter', 'separator', strtolower($if)));
195
		$a_separators = &$config['filter']['separator'][strtolower($if)];
196
		$num_deleted = 0;
197

    
198
		foreach ($_POST['rule'] as $rulei) {
199
			delete_nat_association($a_filter[$rulei]['associated-rule-id']);
200
			unset($a_filter[$rulei]);
201
			$deleted = true;
202

    
203
			// Update the separators
204
			// As rules are deleted, $ridx has to be decremented or separator position will break
205
			$ridx = ifridx($if, $rulei) - $num_deleted;	// get rule index within interface
206
			$mvnrows = -1;
207
			move_separators($a_separators, $ridx, $mvnrows);
208
			$num_deleted++;
209
		}
210

    
211
		if ($deleted) {
212
			if (write_config(gettext("Firewall: Rules - deleted selected firewall rules."))) {
213
				mark_subsystem_dirty('filter');
214
			}
215
		}
216

    
217
		header("Location: firewall_rules.php?if=" . htmlspecialchars($if));
218
		exit;
219
	}
220
} else if ($_POST['act'] == "toggle") {
221
	if ($a_filter[$_POST['id']]) {
222
		if (isset($a_filter[$_POST['id']]['disabled'])) {
223
			unset($a_filter[$_POST['id']]['disabled']);
224
			$wc_msg = gettext('Firewall: Rules - enabled a firewall rule.');
225
		} else {
226
			$a_filter[$_POST['id']]['disabled'] = true;
227
			$wc_msg = gettext('Firewall: Rules - disabled a firewall rule.');
228
		}
229
		if (write_config($wc_msg)) {
230
			mark_subsystem_dirty('filter');
231
		}
232

    
233
		header("Location: firewall_rules.php?if=" . htmlspecialchars($if));
234
		exit;
235
	}
236
} else if ($_POST['order-store']) {
237
	$updated = false;
238
	$dirty = false;
239

    
240
	/* update rule order, POST[rule] is an array of ordered IDs */
241
	if (is_array($_POST['rule']) && !empty($_POST['rule'])) {
242
		$a_filter_new = array();
243

    
244
		// Include the rules of other interfaces listed in config before this (the selected) interface.
245
		foreach ($a_filter as $filteri_before => $filterent) {
246
			if (($filterent['interface'] == $if && !isset($filterent['floating'])) || (isset($filterent['floating']) && "FloatingRules" == $if)) {
247
				break;
248
			} else {
249
				$a_filter_new[] = $filterent;
250
			}
251
		}
252

    
253
		// Include the rules of this (the selected) interface.
254
		// If a rule is not in POST[rule], it has been deleted by the user
255
		foreach ($_POST['rule'] as $id) {
256
			$a_filter_new[] = $a_filter[$id];
257
		}
258

    
259
		// Include the rules of other interfaces listed in config after this (the selected) interface.
260
		foreach ($a_filter as $filteri_after => $filterent) {
261
			if ($filteri_before > $filteri_after) {
262
				continue;
263
			}
264
			if (($filterent['interface'] == $if && !isset($filterent['floating'])) || (isset($filterent['floating']) && "FloatingRules" == $if)) {
265
				continue;
266
			} else {
267
				$a_filter_new[] = $filterent;
268
			}
269
		}
270

    
271
		if ($a_filter !== $a_filter_new) {
272
			$a_filter = $a_filter_new;
273
			$dirty = true;
274
		}
275
	}
276

    
277
	$a_separators = &$config['filter']['separator'][strtolower($if)];
278

    
279
	/* update separator order, POST[separator] is an array of ordered IDs */
280
	if (is_array($_POST['separator']) && !empty($_POST['separator'])) {
281
		$new_separator = array();
282
		$idx = 0;
283

    
284
		foreach ($_POST['separator'] as $separator) {
285
			$new_separator['sep' . $idx++] = $separator;
286
		}
287

    
288
		if ($a_separators !== $new_separator) {
289
			$a_separators = $new_separator;
290
			$updated = true;
291
		}
292
	} else if (!empty($a_separators)) {
293
		$a_separators = "";
294
		$updated = true;
295
	}
296

    
297
	if ($updated || $dirty) {
298
		if (write_config(gettext("Firewall: Rules - reordered firewall rules."))) {
299
			if ($dirty) {
300
				mark_subsystem_dirty('filter');
301
			}
302
		}
303
	}
304

    
305
	header("Location: firewall_rules.php?if=" . htmlspecialchars($if));
306
	exit;
307
}
308

    
309
$tab_array = array(array(gettext("Floating"), ("FloatingRules" == $if), "firewall_rules.php?if=FloatingRules"));
310

    
311
foreach ($iflist as $ifent => $ifname) {
312
	$tab_array[] = array($ifname, ($ifent == $if), "firewall_rules.php?if={$ifent}");
313
}
314

    
315
foreach ($tab_array as $dtab) {
316
	if ($dtab[1]) {
317
		$bctab = $dtab[0];
318
		break;
319
	}
320
}
321

    
322
$pgtitle = array(gettext("Firewall"), gettext("Rules"), $bctab);
323
$pglinks = array("", "firewall_rules.php", "@self");
324
$shortcut_section = "firewall";
325

    
326
include("head.inc");
327
$nrules = 0;
328

    
329
if ($savemsg) {
330
	print_info_box($savemsg, 'success');
331
}
332

    
333
if ($_POST['apply']) {
334
	print_apply_result_box($retval);
335
}
336

    
337
if (is_subsystem_dirty('filter')) {
338
	print_apply_box(gettext("The firewall rule configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
339
}
340

    
341
display_top_tabs($tab_array, false, 'pills');
342

    
343
$showantilockout = false;
344
$showprivate = false;
345
$showblockbogons = false;
346

    
347
if (!isset($config['system']['webgui']['noantilockout']) &&
348
    (((count($config['interfaces']) > 1) && ($if == 'lan')) ||
349
    ((count($config['interfaces']) == 1) && ($if == 'wan')))) {
350
	$showantilockout = true;
351
}
352

    
353
if (isset($config['interfaces'][$if]['blockpriv'])) {
354
	$showprivate = true;
355
}
356

    
357
if (isset($config['interfaces'][$if]['blockbogons'])) {
358
	$showblockbogons = true;
359
}
360

    
361
if (isset($config['system']['webgui']['roworderdragging'])) {
362
	$rules_header_text = gettext("Rules");
363
} else {
364
	$rules_header_text = gettext("Rules (Drag to Change Order)");
365
}
366

    
367
/* Load the counter data of each pf rule. */
368
$rulescnt = pfSense_get_pf_rules();
369

    
370
// Update this if you add or remove columns!
371
$columns_in_table = 13;
372

    
373
/* Floating rules tab has one extra column
374
 * https://redmine.pfsense.org/issues/10667 */
375
if ($if == "FloatingRules") {
376
	$columns_in_table++;
377
}
378

    
379
?>
380
<!-- Allow table to scroll when dragging outside of the display window -->
381
<style>
382
.table-responsive {
383
    clear: both;
384
    overflow-x: visible;
385
    margin-bottom: 0px;
386
}
387
</style>
388

    
389
<form method="post">
390
	<input name="if" id="if" type="hidden" value="<?=$if?>" />
391
	<div class="panel panel-default">
392
		<div class="panel-heading"><h2 class="panel-title"><?=$rules_header_text?></h2></div>
393
		<div id="mainarea" class="table-responsive panel-body">
394
			<table id="ruletable" class="table table-hover table-striped table-condensed" style="overflow-x: 'visible'">
395
				<thead>
396
					<tr>
397
						<th><input type="checkbox" id="selectAll" name="selectAll" /></th>
398
						<th><!-- status icons --></th>
399
						<th><?=gettext("States")?></th>
400
				<?php
401
					if ('FloatingRules' == $if) {
402
				?>
403
						<th><?=gettext("Interfaces")?></th>
404
				<?php
405
					}
406
				?>
407
						<th><?=gettext("Protocol")?></th>
408
						<th><?=gettext("Source")?></th>
409
						<th><?=gettext("Port")?></th>
410
						<th><?=gettext("Destination")?></th>
411
						<th><?=gettext("Port")?></th>
412
						<th><?=gettext("Gateway")?></th>
413
						<th><?=gettext("Queue")?></th>
414
						<th><?=gettext("Schedule")?></th>
415
						<th><?=gettext("Description")?></th>
416
						<th><?=gettext("Actions")?></th>
417
					</tr>
418
				</thead>
419

    
420
<?php if ($showblockbogons || $showantilockout || $showprivate) :
421
?>
422
				<tbody>
423
<?php
424
		// Show the anti-lockout rule if it's enabled, and we are on LAN with an if count > 1, or WAN with an if count of 1.
425
		if ($showantilockout):
426
			$alports = implode('<br />', filter_get_antilockout_ports(true));
427
?>
428
					<tr id="antilockout">
429
						<td></td>
430
						<td title="<?=gettext("traffic is passed")?>"><i class="fa fa-check text-success"></i></td>
431
						<td><?php print_states(intval(ANTILOCKOUT_TRACKER_START), intval(ANTILOCKOUT_TRACKER_END)); ?></td>
432
						<td>*</td>
433
						<td>*</td>
434
						<td>*</td>
435
						<td><?=$iflist[$if];?> Address</td>
436
						<td><?=$alports?></td>
437
						<td>*</td>
438
						<td>*</td>
439
						<td></td>
440
						<td><?=gettext("Anti-Lockout Rule");?></td>
441
						<td>
442
							<a href="system_advanced_admin.php" title="<?=gettext("Settings");?>"><i class="fa fa-cog"></i></a>
443
						</td>
444
					</tr>
445
<?php 	endif;?>
446
<?php 	if ($showprivate): ?>
447
					<tr id="private">
448
						<td></td>
449
						<td title="<?=gettext("traffic is blocked")?>"><i class="fa fa-times text-danger"></i></td>
450
						<td><?php print_states(intval(RFC1918_TRACKER_START), intval(RFC1918_TRACKER_END)); ?></td>
451
						<td>*</td>
452
						<td><?=gettext("RFC 1918 networks");?></td>
453
						<td>*</td>
454
						<td>*</td>
455
						<td>*</td>
456
						<td>*</td>
457
						<td>*</td>
458
						<td></td>
459
						<td><?=gettext("Block private networks");?></td>
460
						<td>
461
							<a href="interfaces.php?if=<?=htmlspecialchars($if)?>" title="<?=gettext("Settings");?>" usepost><i class="fa fa-cog"></i></a>
462
						</td>
463
					</tr>
464
<?php 	endif;?>
465
<?php 	if ($showblockbogons): ?>
466
					<tr id="bogons">
467
						<td></td>
468
						<td title="<?=gettext("traffic is blocked")?>"><i class="fa fa-times text-danger"></i></td>
469
						<td><?php print_states(intval(BOGONS_TRACKER_START), intval(BOGONS_TRACKER_END)); ?></td>
470
						<td>*</td>
471
						<td><?=sprintf(gettext("Reserved%sNot assigned by IANA"), "<br />");?></td>
472
						<td>*</td>
473
						<td>*</td>
474
						<td>*</td>
475
						<td>*</td>
476
						<td>*</td>
477
						<td></td>
478
						<td><?=gettext("Block bogon networks");?></td>
479
						<td>
480
							<a href="interfaces.php?if=<?=htmlspecialchars($if)?>" title="<?=gettext("Settings");?>" usepost><i class="fa fa-cog"></i></a>
481
						</td>
482
					</tr>
483
<?php 	endif;?>
484
			</tbody>
485
<?php endif;?>
486
			<tbody class="user-entries">
487
<?php
488
$nrules = 0;
489
$separators = $config['filter']['separator'][strtolower($if)];
490

    
491
// Get a list of separator rows and use it to call the display separator function only for rows which there are separator(s).
492
// More efficient than looping through the list of separators on every row.
493
$seprows = separator_rows($separators);
494

    
495
foreach ($a_filter as $filteri => $filterent):
496

    
497
	if (($filterent['interface'] == $if && !isset($filterent['floating'])) || (isset($filterent['floating']) && "FloatingRules" == $if)) {
498

    
499
		// Display separator(s) for section beginning at rule n
500
		if ($seprows[$nrules]) {
501
			display_separator($separators, $nrules, $columns_in_table);
502
		}
503
?>
504
					<tr id="fr<?=$nrules;?>" onClick="fr_toggle(<?=$nrules;?>)" ondblclick="document.location='firewall_rules_edit.php?id=<?=$filteri;?>';" <?=(isset($filterent['disabled']) ? ' class="disabled"' : '')?>>
505
						<td>
506
							<input type="checkbox" id="frc<?=$nrules;?>" onClick="fr_toggle(<?=$nrules;?>)" name="rule[]" value="<?=$filteri;?>"/>
507
						</td>
508

    
509
	<?php
510
		if ($filterent['type'] == "block") {
511
			$iconfn = "times text-danger";
512
			$title_text = gettext("traffic is blocked");
513
		} else if ($filterent['type'] == "reject") {
514
			$iconfn = "hand-stop-o text-warning";
515
			$title_text = gettext("traffic is rejected");
516
		} else if ($filterent['type'] == "match") {
517
			$iconfn = "filter";
518
			$title_text = gettext("traffic is matched");
519
		} else {
520
			$iconfn = "check text-success";
521
			$title_text = gettext("traffic is passed");
522
		}
523
	?>
524
						<td title="<?=$title_text?>">
525
							<a href="?if=<?=htmlspecialchars($if);?>&amp;act=toggle&amp;id=<?=$filteri;?>" usepost>
526
								<i class="fa fa-<?=$iconfn?>" title="<?=gettext("click to toggle enabled/disabled status");?>"></i>
527
							</a>
528
	<?php
529
		if ($filterent['quick'] == 'yes') {
530
			print '<i class="fa fa-forward text-success" title="'. gettext("&quot;Quick&quot; rule. Applied immediately on match.") .'" style="cursor: pointer;"></i>';
531
		}
532

    
533
		$isadvset = firewall_check_for_advanced_options($filterent);
534
		if ($isadvset) {
535
			print '<i class="fa fa-cog" title="'. gettext("advanced setting") .': '. $isadvset .'"></i>';
536
		}
537

    
538
		if (isset($filterent['log'])) {
539
			print '<i class="fa fa-tasks" title="'. gettext("traffic is logged") .'" style="cursor: pointer;"></i>';
540
		}
541
	?>
542
						</td>
543
	<?php
544
		$alias = rule_columns_with_alias(
545
			$filterent['source']['address'],
546
			pprint_port($filterent['source']['port']),
547
			$filterent['destination']['address'],
548
			pprint_port($filterent['destination']['port'])
549
		);
550

    
551
		//build Schedule popup box
552
		init_config_arr(array('schedules', 'schedule'));
553
		$a_schedules = &$config['schedules']['schedule'];
554
		$schedule_span_begin = "";
555
		$schedule_span_end = "";
556
		$sched_caption_escaped = "";
557
		$sched_content = "";
558
		$schedstatus = false;
559
		$dayArray = array (gettext('Mon'), gettext('Tues'), gettext('Wed'), gettext('Thur'), gettext('Fri'), gettext('Sat'), gettext('Sun'));
560
		$monthArray = array (gettext('January'), gettext('February'), gettext('March'), gettext('April'), gettext('May'), gettext('June'), gettext('July'), gettext('August'), gettext('September'), gettext('October'), gettext('November'), gettext('December'));
561
		if ($config['schedules']['schedule'] != "" && is_array($config['schedules']['schedule'])) {
562
			$idx = 0;
563
			foreach ($a_schedules as $schedule) {
564
				if (!empty($schedule['name']) &&
565
				    $schedule['name'] == $filterent['sched']) {
566
					$schedstatus = filter_get_time_based_rule_status($schedule);
567

    
568
					foreach ($schedule['timerange'] as $timerange) {
569
						$tempFriendlyTime = "";
570
						$tempID = "";
571
						$firstprint = false;
572
						if ($timerange) {
573
							$dayFriendly = "";
574
							$tempFriendlyTime = "";
575

    
576
							//get hours
577
							$temptimerange = $timerange['hour'];
578
							$temptimeseparator = strrpos($temptimerange, "-");
579

    
580
							$starttime = substr ($temptimerange, 0, $temptimeseparator);
581
							$stoptime = substr ($temptimerange, $temptimeseparator+1);
582

    
583
							if ($timerange['month']) {
584
								$tempmontharray = explode(",", $timerange['month']);
585
								$tempdayarray = explode(",", $timerange['day']);
586
								$arraycounter = 0;
587
								$firstDayFound = false;
588
								$firstPrint = false;
589
								foreach ($tempmontharray as $monthtmp) {
590
									$month = $tempmontharray[$arraycounter];
591
									$day = $tempdayarray[$arraycounter];
592

    
593
									if (!$firstDayFound) {
594
										$firstDay = $day;
595
										$firstmonth = $month;
596
										$firstDayFound = true;
597
									}
598

    
599
									$currentDay = $day;
600
									$nextDay = $tempdayarray[$arraycounter+1];
601
									$currentDay++;
602
									if (($currentDay != $nextDay) || ($tempmontharray[$arraycounter] != $tempmontharray[$arraycounter+1])) {
603
										if ($firstPrint) {
604
											$dayFriendly .= ", ";
605
										}
606
										$currentDay--;
607
										if ($currentDay != $firstDay) {
608
											$dayFriendly .= $monthArray[$firstmonth-1] . " " . $firstDay . " - " . $currentDay ;
609
										} else {
610
											$dayFriendly .=	 $monthArray[$month-1] . " " . $day;
611
										}
612
										$firstDayFound = false;
613
										$firstPrint = true;
614
									}
615
									$arraycounter++;
616
								}
617
							} else {
618
								$tempdayFriendly = $timerange['position'];
619
								$firstDayFound = false;
620
								$tempFriendlyDayArray = explode(",", $tempdayFriendly);
621
								$currentDay = "";
622
								$firstDay = "";
623
								$nextDay = "";
624
								$counter = 0;
625
								foreach ($tempFriendlyDayArray as $day) {
626
									if ($day != "") {
627
										if (!$firstDayFound) {
628
											$firstDay = $tempFriendlyDayArray[$counter];
629
											$firstDayFound = true;
630
										}
631
										$currentDay =$tempFriendlyDayArray[$counter];
632
										//get next day
633
										$nextDay = $tempFriendlyDayArray[$counter+1];
634
										$currentDay++;
635
										if ($currentDay != $nextDay) {
636
											if ($firstprint) {
637
												$dayFriendly .= ", ";
638
											}
639
											$currentDay--;
640
											if ($currentDay != $firstDay) {
641
												$dayFriendly .= $dayArray[$firstDay-1] . " - " . $dayArray[$currentDay-1];
642
											} else {
643
												$dayFriendly .= $dayArray[$firstDay-1];
644
											}
645
											$firstDayFound = false;
646
											$firstprint = true;
647
										}
648
										$counter++;
649
									}
650
								}
651
							}
652
							$timeFriendly = $starttime . " - " . $stoptime;
653
							$description = $timerange['rangedescr'];
654
							$sched_content .= $dayFriendly . "; " . $timeFriendly . "<br />";
655
						}
656
					}
657
					#FIXME
658
					$sched_caption_escaped = str_replace("'", "\'", $schedule['descr']);
659
					$schedule_span_begin = '<a href="/firewall_schedule_edit.php?id=' . $idx . '" data-toggle="popover" data-trigger="hover focus" title="' . $schedule['name'] . '" data-content="' .
660
						$sched_caption_escaped . '" data-html="true">';
661
					$schedule_span_end = "</a>";
662
				}
663
				$idx++;
664
			}
665
		}
666
		$printicon = false;
667
		$alttext = "";
668
		$image = "";
669
		if (!isset($filterent['disabled'])) {
670
			if ($schedstatus) {
671
				if ($filterent['type'] == "block" || $filterent['type'] == "reject") {
672
					$image = "times-circle";
673
					$dispcolor = "text-danger";
674
					$alttext = gettext("Traffic matching this rule is currently being denied");
675
				} else {
676
					$image = "play-circle";
677
					$dispcolor = "text-success";
678
					$alttext = gettext("Traffic matching this rule is currently being allowed");
679
				}
680
				$printicon = true;
681
			} else if ($filterent['sched']) {
682
				if ($filterent['type'] == "block" || $filterent['type'] == "reject") {
683
					$image = "times-circle";
684
				} else {
685
					$image = "play-circle";
686
				}
687
				$alttext = gettext("This rule is not currently active because its period has expired");
688
				$dispcolor = "text-warning";
689
				$printicon = true;
690
			}
691
		}
692
	?>
693
				<td><?php print_states(intval($filterent['tracker'])); ?></td>
694
	<?php
695
		if ($if == 'FloatingRules') {
696
	?>
697
			<td onclick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='firewall_rules_edit.php?id=<?=$i;?>';">
698
	<?php
699
			if (isset($filterent['interface'])) {
700
				$selected_interfaces = explode(',', $filterent['interface']);
701
				unset($selected_descs);
702
				foreach ($selected_interfaces as $interface) {
703
					if (isset($ifdescs[$interface])) {
704
						$selected_descs[] = $ifdescs[$interface];
705
					} else {
706
						switch ($interface) {
707
						case 'l2tp':
708
							if ($config['l2tp']['mode'] == 'server')
709
								$selected_descs[] = 'L2TP VPN';
710
							break;
711
						case 'pppoe':
712
							if (is_pppoe_server_enabled())
713
								$selected_descs[] = 'PPPoE Server';
714
							break;
715
						case 'enc0':
716
							if (ipsec_enabled())
717
								$selected_descs[] = 'IPsec';
718
							break;
719
						case 'openvpn':
720
							if  ($config['openvpn']['openvpn-server'] || $config['openvpn']['openvpn-client'])
721
								$selected_descs[] = 'OpenVPN';
722
							break;
723
						case 'wireguard':
724
							$selected_descs[] = 'WireGuard';
725
							break;
726
						default:
727
							$selected_descs[] = $interface;
728
							break;
729
						}
730
					}
731
				}
732
				if (!empty($selected_descs)) {
733
					$desclist = '';
734
					$desclength = 0;
735
					foreach ($selected_descs as $descid => $desc) {
736
						$desclength += strlen($desc);
737
						if ($desclength > 18) {
738
							$desclist .= ',<br/>';
739
							$desclength = 0;
740
						} elseif ($desclist) {
741
							$desclist .= ', ';
742
							$desclength += 2;
743
						}
744
						$desclist .= $desc;
745
					}
746
					echo $desclist;
747
				}
748
			}
749
	?>
750
			</td>
751
	<?php
752
		}
753
	?>
754
			<td>
755
	<?php
756
		if (isset($filterent['ipprotocol'])) {
757
			switch ($filterent['ipprotocol']) {
758
				case "inet":
759
					echo "IPv4 ";
760
					break;
761
				case "inet6":
762
					echo "IPv6 ";
763
					break;
764
				case "inet46":
765
					echo "IPv4+6 ";
766
					break;
767
			}
768
		} else {
769
			echo "IPv4 ";
770
		}
771

    
772
		if (isset($filterent['protocol'])) {
773
			echo strtoupper($filterent['protocol']);
774

    
775
			if (strtoupper($filterent['protocol']) == "ICMP" && !empty($filterent['icmptype'])) {
776
				// replace each comma-separated icmptype item by its (localised) full description
777
				$t = 	implode(', ',
778
						array_map(
779
						        function($type) {
780
								global $icmptypes;
781
								return $icmptypes[$type]['descrip'];
782
							},
783
							explode(',', $filterent['icmptype'])
784
						)
785
					);
786
				echo sprintf('<br /><div style="cursor:help;padding:1px;line-height:1.1em;max-height:2.5em;max-width:180px;overflow-y:auto;overflow-x:hidden" title="%s:%s%s"><small><u>%s</u></small></div>', gettext('ICMP subtypes'), chr(13), $t, str_replace(',', '</u>, <u>',$filterent['icmptype']));
787
			}
788
		} else {
789
			echo " *";
790
		}
791
	?>
792
						</td>
793
						<td>
794
							<?php if (isset($alias['src'])): ?>
795
								<a href="/firewall_aliases_edit.php?id=<?=$alias['src']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['src'])?>" data-html="true">
796
									<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($filterent['source'])))?>
797
								</a>
798
							<?php else: ?>
799
								<?=htmlspecialchars(pprint_address($filterent['source']))?>
800
							<?php endif; ?>
801
						</td>
802
						<td>
803
							<?php if (isset($alias['srcport'])): ?>
804
								<a href="/firewall_aliases_edit.php?id=<?=$alias['srcport']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['srcport'])?>" data-html="true">
805
									<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($filterent['source']['port'])))?>
806
								</a>
807
							<?php else: ?>
808
								<?=htmlspecialchars(pprint_port($filterent['source']['port']))?>
809
							<?php endif; ?>
810
						</td>
811
						<td>
812
							<?php if (isset($alias['dst'])): ?>
813
								<a href="/firewall_aliases_edit.php?id=<?=$alias['dst']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['dst'])?>" data-html="true">
814
									<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($filterent['destination'])))?>
815
								</a>
816
							<?php else: ?>
817
								<?=htmlspecialchars(pprint_address($filterent['destination']))?>
818
							<?php endif; ?>
819
						</td>
820
						<td>
821
							<?php if (isset($alias['dstport'])): ?>
822
								<a href="/firewall_aliases_edit.php?id=<?=$alias['dstport']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['dstport'])?>" data-html="true">
823
									<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($filterent['destination']['port'])))?>
824
								</a>
825
							<?php else: ?>
826
								<?=htmlspecialchars(pprint_port($filterent['destination']['port']))?>
827
							<?php endif; ?>
828
						</td>
829
						<td>
830
							<?php if (isset($filterent['gateway'])): ?>
831
								<span data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Gateways details')?>" data-content="<?=gateway_info_popup($filterent['gateway'])?>" data-html="true">
832
							<?php else: ?>
833
								<span>
834
							<?php endif; ?>
835
								<?php if (isset($config['interfaces'][$filterent['gateway']]['descr'])): ?>
836
									<?=str_replace('_', '_<wbr>', htmlspecialchars($config['interfaces'][$filterent['gateway']]['descr']))?>
837
								<?php else: ?>
838
									<?=htmlspecialchars(pprint_port($filterent['gateway']))?>
839
								<?php endif; ?>
840
							</span>
841
						</td>
842
						<td>
843
							<?php
844
								if (isset($filterent['ackqueue']) && isset($filterent['defaultqueue'])) {
845
									$desc = str_replace('_', ' ', $filterent['ackqueue']);
846
									echo "<a href=\"firewall_shaper_queues.php?queue={$filterent['ackqueue']}&amp;action=show\">{$desc}</a>";
847
									$desc = str_replace('_', '_<wbr>', $filterent['defaultqueue']);
848
									echo "/<a href=\"firewall_shaper_queues.php?queue={$filterent['defaultqueue']}&amp;action=show\">{$desc}</a>";
849
								} else if (isset($filterent['defaultqueue'])) {
850
									$desc = str_replace('_', '_<wbr>', $filterent['defaultqueue']);
851
									echo "<a href=\"firewall_shaper_queues.php?queue={$filterent['defaultqueue']}&amp;action=show\">{$desc}</a>";
852
								} else {
853
									echo gettext("none");
854
								}
855
							?>
856
						</td>
857
						<td>
858
							<?php if ($printicon) { ?>
859
								<i class="fa fa-<?=$image?> <?=$dispcolor?>" title="<?=$alttext;?>"></i>
860
							<?php } ?>
861
							<?=$schedule_span_begin;?><?=str_replace('_', '_<wbr>', htmlspecialchars($filterent['sched']));?>&nbsp;<?=$schedule_span_end;?>
862
						</td>
863
						<td>
864
							<?=htmlspecialchars($filterent['descr']);?>
865
						</td>
866
						<td class="action-icons">
867
						<!-- <?=(isset($filterent['disabled']) ? 'enable' : 'disable')?> -->
868
							<a	class="fa fa-anchor icon-pointer" id="Xmove_<?=$filteri?>" title="<?=$XmoveTitle?>"></a>
869
							<a href="firewall_rules_edit.php?id=<?=$filteri;?>" class="fa fa-pencil" title="<?=gettext('Edit')?>"></a>
870
							<a href="firewall_rules_edit.php?dup=<?=$filteri;?>" class="fa fa-clone" title="<?=gettext('Copy')?>"></a>
871
<?php if (isset($filterent['disabled'])) {
872
?>
873
							<a href="?act=toggle&amp;if=<?=htmlspecialchars($if);?>&amp;id=<?=$filteri;?>" class="fa fa-check-square-o" title="<?=gettext('Enable')?>" usepost></a>
874
<?php } else {
875
?>
876
							<a href="?act=toggle&amp;if=<?=htmlspecialchars($if);?>&amp;id=<?=$filteri;?>" class="fa fa-ban" title="<?=gettext('Disable')?>" usepost></a>
877
<?php }
878
?>
879
							<a href="?act=del&amp;if=<?=htmlspecialchars($if);?>&amp;id=<?=$filteri;?>" class="fa fa-trash" title="<?=gettext('Delete this rule')?>" usepost></a>
880
						</td>
881
					</tr>
882
<?php
883
		$nrules++;
884
	}
885
endforeach;
886

    
887
// There can be separator(s) after the last rule listed.
888
if ($seprows[$nrules]) {
889
	display_separator($separators, $nrules, $columns_in_table);
890
}
891
?>
892
				</tbody>
893
			</table>
894
		</div>
895
	</div>
896

    
897
<?php if ($nrules == 0): ?>
898
	<div class="alert alert-warning" role="alert">
899
		<p>
900
		<?php if ($_REQUEST['if'] == "FloatingRules"): ?>
901
			<?=gettext("No floating rules are currently defined.");?>
902
		<?php else: ?>
903
			<?=gettext("No rules are currently defined for this interface");?><br />
904
			<?=gettext("All incoming connections on this interface will be blocked until pass rules are added.");?>
905
		<?php endif;?>
906
			<?=gettext("Click the button to add a new rule.");?>
907
		</p>
908
	</div>
909
<?php endif;?>
910

    
911
	<nav class="action-buttons">
912
		<a href="firewall_rules_edit.php?if=<?=htmlspecialchars($if);?>&amp;after=-1" role="button" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the top of the list')?>">
913
			<i class="fa fa-level-up icon-embed-btn"></i>
914
			<?=gettext("Add");?>
915
		</a>
916
		<a href="firewall_rules_edit.php?if=<?=htmlspecialchars($if);?>" role="button" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the end of the list')?>">
917
			<i class="fa fa-level-down icon-embed-btn"></i>
918
			<?=gettext("Add");?>
919
		</a>
920
		<button name="del_x" type="submit" class="btn btn-danger btn-sm" value="<?=gettext("Delete selected rules"); ?>" title="<?=gettext('Delete selected rules')?>">
921
			<i class="fa fa-trash icon-embed-btn"></i>
922
			<?=gettext("Delete"); ?>
923
		</button>
924
		<button type="submit" id="order-store" name="order-store" class="btn btn-sm btn-primary" value="store changes" disabled title="<?=gettext('Save rule order')?>">
925
			<i class="fa fa-save icon-embed-btn"></i>
926
			<?=gettext("Save")?>
927
		</button>
928
		<button type="submit" id="addsep" name="addsep" class="btn btn-sm btn-warning" title="<?=gettext('Add separator')?>">
929
			<i class="fa fa-plus icon-embed-btn"></i>
930
			<?=gettext("Separator")?>
931
		</button>
932
	</nav>
933
</form>
934

    
935
<div class="infoblock">
936
	<div class="alert alert-info clearfix" role="alert"><div class="pull-left">
937
		<dl class="dl-horizontal responsive">
938
		<!-- Legend -->
939
			<dt><?=gettext('Legend')?></dt>				<dd></dd>
940
			<dt><i class="fa fa-check text-success"></i></dt>		<dd><?=gettext("Pass");?></dd>
941
			<dt><i class="fa fa-filter"></i></dt>	<dd><?=gettext("Match");?></dd>
942
			<dt><i class="fa fa-times text-danger"></i></dt>	<dd><?=gettext("Block");?></dd>
943
			<dt><i class="fa fa-hand-stop-o text-warning"></i></dt>		<dd><?=gettext("Reject");?></dd>
944
			<dt><i class="fa fa-tasks"></i></dt>	<dd> <?=gettext("Log");?></dd>
945
			<dt><i class="fa fa-cog"></i></dt>		<dd> <?=gettext("Advanced filter");?></dd>
946
			<dt><i class="fa fa-forward text-success"></i></dt><dd> <?=gettext("&quot;Quick&quot; rule. Applied immediately on match.")?></dd>
947
		</dl>
948

    
949
<?php
950
	if ("FloatingRules" != $if) {
951
		print(gettext("Rules are evaluated on a first-match basis (i.e. " .
952
			"the action of the first rule to match a packet will be executed). ") . '<br />' .
953
			gettext("This means that if block rules are used, it is important to pay attention " .
954
			"to the rule order. Everything that isn't explicitly passed is blocked " .
955
			"by default. "));
956
	} else {
957
		print(gettext("Floating rules are evaluated on a first-match basis (i.e. " .
958
			"the action of the first rule to match a packet will be executed) only " .
959
			"if the 'quick' option is checked on a rule. Otherwise they will only match if no " .
960
			"other rules match. Pay close attention to the rule order and options " .
961
			"chosen. If no rule here matches, the per-interface or default rules are used. "));
962
	}
963

    
964
	printf(gettext('%1$sClick the anchor icon %2$s to move checked rules before the clicked row. Hold down ' .
965
			'the shift key and click to move the rules after the clicked row.'), '<br /><br />', '<i class="fa fa-anchor"></i>')
966
?>
967
	</div>
968
	</div>
969
</div>
970

    
971
<script type="text/javascript">
972
//<![CDATA[
973

    
974
//Need to create some variables here so that jquery/pfSenseHelpers.js can read them
975
iface = "<?=strtolower($if)?>";
976
cncltxt = '<?=gettext("Cancel")?>';
977
svtxt = '<?=gettext("Save")?>';
978
svbtnplaceholder = '<?=gettext("Enter a description, Save, then drag to final location.")?>';
979
configsection = "filter";
980

    
981
events.push(function() {
982

    
983
	// "Move to here" (anchor) action
984
	$('[id^=Xmove_]').click(function (event) {
985

    
986
		// Prevent click from toggling row
987
		event.stopImmediatePropagation();
988

    
989
		// Save the target rule position
990
		var anchor_row = $(this).parents("tr:first");
991

    
992
		if (event.shiftKey) {
993
			$($('#ruletable > tbody  > tr').get().reverse()).each(function() {
994
				ruleid = this.id.slice(2);
995

    
996
				if (ruleid && !isNaN(ruleid)) {
997
					if ($('#frc' + ruleid).prop('checked')) {
998
						// Move the selected rows, un-select them and add highlight class
999
						$(this).insertAfter(anchor_row);
1000
						fr_toggle(ruleid, "fr");
1001
						$('#fr' + ruleid).addClass("highlight");
1002
					}
1003
				}
1004
			});
1005
		} else {
1006
			$('#ruletable > tbody  > tr').each(function() {
1007
				ruleid = this.id.slice(2);
1008

    
1009
				if (ruleid && !isNaN(ruleid)) {
1010
					if ($('#frc' + ruleid).prop('checked')) {
1011
						// Move the selected rows, un-select them and add highlight class
1012
						$(this).insertBefore(anchor_row);
1013
						fr_toggle(ruleid, "fr");
1014
						$('#fr' + ruleid).addClass("highlight");
1015
					}
1016
				}
1017
			});
1018
		}
1019

    
1020
		// Temporarily set background color so user can more easily see the moved rules, then fade
1021
		$('.highlight').effect("highlight", {color: "#739b4b;"}, 4000);
1022
		$('#ruletable tr').removeClass("highlight");
1023
		$('#order-store').removeAttr('disabled');
1024
		reindex_rules($(anchor_row).parent('tbody'));
1025
		dirty = true;
1026
	}).mouseover(function(e) {
1027
		var ruleselected = false;
1028

    
1029
		$(this).css("cursor", "default");
1030

    
1031
		// Are any rules currently selected?
1032
		$('[id^=frc]').each(function () {
1033
			if ($(this).prop("checked")) {
1034
				ruleselected = true;
1035
			}
1036
		});
1037

    
1038
		// If so, change the icon to show the insertion point
1039
		if (ruleselected) {
1040
			if (e.shiftKey) {
1041
				$(this).removeClass().addClass("fa fa-lg fa-arrow-down text-danger");
1042
			} else {
1043
				$(this).removeClass().addClass("fa fa-lg fa-arrow-up text-danger");
1044
			}
1045
		}
1046
	}).mouseout(function(e) {
1047
		$(this).removeClass().addClass("fa fa-anchor");
1048
	});
1049

    
1050
<?php if(!isset($config['system']['webgui']['roworderdragging'])): ?>
1051
	// Make rules sortable. Hiding the table before applying sortable, then showing it again is
1052
	// a work-around for very slow sorting on FireFox
1053
	$('table tbody.user-entries').hide();
1054

    
1055
	$('table tbody.user-entries').sortable({
1056
		cursor: 'grabbing',
1057
		scroll: true,
1058
		overflow: 'scroll',
1059
		scrollSensitivity: 100,
1060
		update: function(event, ui) {
1061
			$('#order-store').removeAttr('disabled');
1062
			reindex_rules(ui.item.parent('tbody'));
1063
			dirty = true;
1064
		}
1065
	});
1066

    
1067
	$('table tbody.user-entries').show();
1068
<?php endif; ?>
1069

    
1070
	// Check all of the rule checkboxes so that their values are posted
1071
	$('#order-store').click(function () {
1072
		$('[id^=frc]').prop('checked', true);
1073

    
1074
		// Save the separator bar configuration
1075
		save_separators();
1076

    
1077
		// Suppress the "Do you really want to leave the page" message
1078
		saving = true;
1079
	});
1080

    
1081
	// Provide a warning message if the user tries to change page before saving
1082
	$(window).bind('beforeunload', function(){
1083
		if ((!saving && dirty) || newSeperator) {
1084
			return ("<?=gettext('One or more rules have been moved but have not yet been saved')?>");
1085
		} else {
1086
			return undefined;
1087
		}
1088
	});
1089

    
1090
	$(document).on('keyup keydown', function(e){
1091
		if (e.shiftKey) {
1092
			$('[id^=Xmove_]').attr("title", "<?=$ShXmoveTitle?>");
1093
		} else {
1094
			$('[id^=Xmove_]').attr("title", "<?=$XmoveTitle?>");
1095
		}
1096
	});
1097

    
1098
	$('#selectAll').click(function() {
1099
		var checkedStatus = this.checked;
1100
		$('#ruletable tbody tr').find('td:first :checkbox').each(function() {
1101
		$(this).prop('checked', checkedStatus);
1102
		});
1103
	});
1104
});
1105
//]]>
1106
</script>
1107

    
1108
<?php include("foot.inc");?>
(50-50/229)