Project

General

Profile

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

    
26
##|+PRIV
27
##|*IDENT=page-firewall-nat-portforward
28
##|*NAME=Firewall: NAT: Port Forward
29
##|*DESCR=Allow access to the 'Firewall: NAT: Port Forward' page.
30
##|*MATCH=firewall_nat.php*
31
##|-PRIV
32

    
33
require_once("guiconfig.inc");
34
require_once("functions.inc");
35
require_once("filter.inc");
36
require_once("shaper.inc");
37
require_once("itemid.inc");
38

    
39
init_config_arr(array('nat', 'separator'));
40
init_config_arr(array('nat', 'rule'));
41
$a_nat = &$config['nat']['rule'];
42
$a_separators = &$config['nat']['separator'];
43

    
44
/* update rule order, POST[rule] is an array of ordered IDs */
45
if (array_key_exists('order-store', $_REQUEST) && have_natpfruleint_access($natent['interface'])) {
46
	if (is_array($_REQUEST['rule']) && !empty($_REQUEST['rule'])) {
47
		$a_nat_new = array();
48

    
49
		// if a rule is not in POST[rule], it has been deleted by the user
50
		foreach ($_POST['rule'] as $id) {
51
			$a_nat_new[] = $a_nat[$id];
52
		}
53

    
54
		$a_nat = $a_nat_new;
55

    
56

    
57
		$config['nat']['separator'] = "";
58

    
59
		if ($_POST['separator']) {
60
			$idx = 0;
61

    
62
			if (!is_array($config['nat']['separator'])) {
63
				$config['nat']['separator'] = array();
64
			}
65

    
66
			foreach ($_POST['separator'] as $separator) {
67
				$config['nat']['separator']['sep' . $idx++] = $separator;
68
			}
69
		}
70

    
71
		if (write_config()) {
72
			mark_subsystem_dirty('filter');
73
		}
74

    
75
		header("Location: firewall_nat.php");
76
		exit;
77
	}
78
}
79

    
80
/* if a custom message has been passed along, lets process it */
81
if ($_REQUEST['savemsg']) {
82
	$savemsg = $_REQUEST['savemsg'];
83
}
84

    
85
if ($_POST['apply'] && have_natpfruleint_access($natent['interface'])) {
86

    
87
	$retval = 0;
88

    
89
	$retval |= filter_configure();
90

    
91
	pfSense_handle_custom_code("/usr/local/pkg/firewall_nat/apply");
92

    
93
	if ($retval == 0) {
94
		clear_subsystem_dirty('natconf');
95
		clear_subsystem_dirty('filter');
96
	}
97

    
98
}
99

    
100
if (($_POST['act'] == "del") && have_natpfruleint_access($natent['interface'])) {
101
	if ($a_nat[$_POST['id']]) {
102

    
103
		if (isset($a_nat[$_POST['id']]['associated-rule-id'])) {
104
			delete_id($a_nat[$_POST['id']]['associated-rule-id'], $config['filter']['rule']);
105
			$want_dirty_filter = true;
106
		}
107

    
108
		unset($a_nat[$_POST['id']]);
109

    
110
		// Update the separators
111
		$ridx = $_POST['id'];
112
		$mvnrows = -1;
113
		move_separators($a_separators, $ridx, $mvnrows);
114

    
115
		if (write_config()) {
116
			mark_subsystem_dirty('natconf');
117
			if ($want_dirty_filter) {
118
				mark_subsystem_dirty('filter');
119
			}
120
		}
121

    
122
		header("Location: firewall_nat.php");
123
		exit;
124
	}
125
}
126

    
127
if (isset($_POST['del_x']) && have_natpfruleint_access($natent['interface'])) {
128

    
129
	/* delete selected rules */
130
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
131
		$num_deleted = 0;
132

    
133
		foreach ($_POST['rule'] as $rulei) {
134
			$target = $rule['target'];
135

    
136
			// Check for filter rule associations
137
			if (isset($a_nat[$rulei]['associated-rule-id'])) {
138
				delete_id($a_nat[$rulei]['associated-rule-id'], $config['filter']['rule']);
139
				mark_subsystem_dirty('filter');
140
			}
141

    
142
			unset($a_nat[$rulei]);
143

    
144
			// Update the separators
145
			// As rules are deleted, $ridx has to be decremented or separator position will break
146
			$ridx = $rulei - $num_deleted;
147
			$mvnrows = -1;
148
			move_separators($a_separators, $ridx, $mvnrows);
149
			$num_deleted++;
150
		}
151

    
152
		if (write_config()) {
153
			mark_subsystem_dirty('natconf');
154
		}
155

    
156
		header("Location: firewall_nat.php");
157
		exit;
158
	}
159
} elseif (($_POST['act'] == "toggle") && have_natpfruleint_access($natent['interface'])) {
160
	if ($a_nat[$_POST['id']]) {
161
		if (isset($a_nat[$_POST['id']]['disabled'])) {
162
			unset($a_nat[$_POST['id']]['disabled']);
163
			$rule_status = true;
164
		} else {
165
			$a_nat[$_POST['id']]['disabled'] = true;
166
			$rule_status = false;
167
		}
168

    
169
		// Check for filter rule associations
170
		if (isset($a_nat[$_POST['id']]['associated-rule-id'])) {
171
			toggle_id($a_nat[$_POST['id']]['associated-rule-id'],
172
			    $config['filter']['rule'], $rule_status);
173
			unset($rule_status);
174
			mark_subsystem_dirty('filter');
175
		}
176

    
177
		if (write_config(gettext("Firewall: NAT: Port forward, enable/disable NAT rule"))) {
178
			mark_subsystem_dirty('natconf');
179
		}
180
		header("Location: firewall_nat.php");
181
		exit;
182
	}
183
}
184

    
185
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("Port Forward"));
186
$pglinks = array("", "@self", "@self");
187
include("head.inc");
188

    
189
if ($_POST['apply']) {
190
	print_apply_result_box($retval);
191
}
192

    
193
if (is_subsystem_dirty('natconf') && have_natpfruleint_access($natent['interface'])) {
194
	print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
195
					gettext('The changes must be applied for them to take effect.'));
196
}
197

    
198
$tab_array = array();
199
$tab_array[] = array(gettext("Port Forward"), true, "firewall_nat.php");
200
$tab_array[] = array(gettext("1:1"), false, "firewall_nat_1to1.php");
201
$tab_array[] = array(gettext("Outbound"), false, "firewall_nat_out.php");
202
$tab_array[] = array(gettext("NPt"), false, "firewall_nat_npt.php");
203
display_top_tabs($tab_array);
204

    
205
$columns_in_table = 13;
206
?>
207
<!-- Allow table to scroll when dragging outside of the display window -->
208
<style>
209
.table-responsive {
210
    clear: both;
211
    overflow-x: visible;
212
    margin-bottom: 0px;
213
}
214
</style>
215

    
216
<form action="firewall_nat.php" method="post" name="iform">
217
	<div class="panel panel-default">
218
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Rules')?></h2></div>
219
		<div class="panel-body table-responsive">
220
			<table id="ruletable" class="table table-striped table-hover table-condensed">
221
				<thead>
222
					<tr>
223
						<th style="padding-left:10px;">  <input type="checkbox" id="selectAll" name="selectAll" /></th>
224
						<th><!-- Icon --></th>
225
						<th><!-- Rule type --></th>
226
						<th><?=gettext("Interface")?></th>
227
						<th><?=gettext("Protocol")?></th>
228
						<th><?=gettext("Source Address")?></th>
229
						<th><?=gettext("Source Ports")?></th>
230
						<th><?=gettext("Dest. Address")?></th>
231
						<th><?=gettext("Dest. Ports")?></th>
232
						<th><?=gettext("NAT IP")?></th>
233
						<th><?=gettext("NAT Ports")?></th>
234
						<th><?=gettext("Description")?></th>
235
						<th><?=gettext("Actions")?></th>
236
					</tr>
237
				</thead>
238
				<tbody class='user-entries'>
239
<?php
240

    
241
$nnats = $i = 0;
242
$separators = $config['nat']['separator'];
243

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

    
248
foreach ($a_nat as $natent):
249

    
250
	// Display separator(s) for section beginning at rule n
251
	if ($seprows[$nnats]) {
252
		display_separator($separators, $nnats, $columns_in_table);
253
	}
254

    
255
	$localport = $natent['local-port'];
256

    
257
	list($dstbeginport, $dstendport) = explode("-", $natent['destination']['port']);
258

    
259
	if ($dstendport) {
260
		$localendport = $natent['local-port'] + $dstendport - $dstbeginport;
261
		$localport	 .= '-' . $localendport;
262
	}
263

    
264
	$alias = rule_columns_with_alias(
265
		$natent['source']['address'],
266
		pprint_port($natent['source']['port']),
267
		$natent['destination']['address'],
268
		pprint_port($natent['destination']['port']),
269
		$natent['target'],
270
		$localport
271
	);
272

    
273
	if (isset($natent['disabled'])) {
274
		$iconfn = "pass_d";
275
		$trclass = 'class="disabled"';
276
	} else {
277
		$iconfn = "pass";
278
		$trclass = '';
279
	}
280
?>
281

    
282
					<tr id="fr<?=$nnats;?>" <?=$trclass?> onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_edit.php?id=<?=$i;?>';">
283
						<td >
284
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
285
							<input type="checkbox" id="frc<?=$nnats;?>" onClick="fr_toggle(<?=$nnats;?>)" name="rule[]" value="<?=$i;?>"/>
286
<?php	endif; ?>
287
						</td>
288
						<td>
289
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
290
							<a href="?act=toggle&amp;id=<?=$i?>" usepost>
291
								<i class="fa fa-check" title="<?=gettext("click to toggle enabled/disabled status")?>"></i>
292
							</a>
293
<?php	endif; ?>
294
<?php 	if (isset($natent['nordr'])) { ?>
295
								&nbsp;<i class="fa fa-hand-stop-o text-danger" title="<?=gettext("Negated: This rule excludes NAT from a later rule")?>"></i>
296
<?php 	} ?>
297
						</td>
298
						<td>
299
<?php
300
	if ($natent['associated-rule-id'] == "pass"):
301
?>
302
							<i class="fa fa-play" title="<?=gettext("All traffic matching this NAT entry is passed")?>"></i>
303
<?php
304
	elseif (!empty($natent['associated-rule-id'])):
305
?>
306
							<i class="fa fa-random" title="<?=sprintf(gettext("Firewall rule ID %s is managed by this rule"), htmlspecialchars($natent['associated-rule-id']))?>"></i>
307
<?php
308
	endif;
309
?>
310
						</td>
311
						<td>
312
							<?=$textss?>
313
<?php
314
	if (!$natent['interface']) {
315
		echo htmlspecialchars(convert_friendly_interface_to_friendly_descr("wan"));
316
	} else {
317
		echo htmlspecialchars(convert_friendly_interface_to_friendly_descr($natent['interface']));
318
	}
319
?>
320
							<?=$textse?>
321
						</td>
322

    
323
						<td>
324
							<?=$textss?><?=strtoupper($natent['protocol'])?><?=$textse?>
325
						</td>
326

    
327
						<td>
328

    
329

    
330
<?php
331
	if (isset($alias['src'])):
332
?>
333
							<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">
334
<?php
335
	endif;
336
?>
337
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['source'])))?>
338
<?php
339
	if (isset($alias['src'])):
340
?>
341
							</a>
342
<?php
343
	endif;
344
?>
345
						</td>
346
						<td>
347
<?php
348
	if (isset($alias['srcport'])):
349
?>
350
							<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">
351
<?php
352
	endif;
353
?>
354
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['source']['port'])))?>
355
<?php
356
	if (isset($alias['srcport'])):
357
?>
358
							</a>
359
<?php
360
	endif;
361
?>
362
						</td>
363

    
364
						<td>
365
<?php
366
	if (isset($alias['dst'])):
367
?>
368
							<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">
369
<?php
370
	endif;
371
?>
372
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['destination'])))?>
373
<?php
374
	if (isset($alias['dst'])):
375
?>
376
							</a>
377
<?php
378
	endif;
379
?>
380
						</td>
381
						<td>
382
<?php
383
	if (isset($alias['dstport'])):
384
?>
385
							<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">
386
<?php
387
	endif;
388
?>
389
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['destination']['port'])))?>
390
<?php
391
	if (isset($alias['dstport'])):
392
?>
393
							</a>
394
<?php
395
	endif;
396
?>
397
						</td>
398
						<td>
399
<?php
400
	if (isset($alias['target'])):
401
?>
402
							<a href="/firewall_aliases_edit.php?id=<?=$alias['target']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['target'])?>" data-html="true" >
403
<?php
404
	endif;
405
?>
406

    
407
							<?=str_replace('_', '_<wbr>', htmlspecialchars($natent['target']))?>
408
<?php
409
	if (isset($alias['target'])):
410
?>
411
							</a>
412
<?php
413
	endif;
414
?>
415
						</td>
416
						<td>
417
<?php
418
	if (isset($alias['targetport'])):
419
?>
420
							<a href="/firewall_aliases_edit.php?id=<?=$alias['targetport']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['targetport'])?>" data-html="true">
421
<?php
422
	endif;
423
?>
424
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($localport)))?>
425
<?php
426
	if (isset($alias['targetport'])):
427
?>
428
							</a>
429
<?php
430
	endif;
431
?>
432
						</td>
433

    
434
						<td>
435
							<?=htmlspecialchars($natent['descr'])?>
436
						</td>
437
						<td>
438
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
439
							<a class="fa fa-pencil" title="<?=gettext("Edit rule"); ?>" href="firewall_nat_edit.php?id=<?=$i?>"></a>
440
							<a class="fa fa-clone"	  title="<?=gettext("Add a new NAT based on this one")?>" href="firewall_nat_edit.php?dup=<?=$i?>"></a>
441
							<a class="fa fa-trash"	title="<?=gettext("Delete rule")?>" href="firewall_nat.php?act=del&amp;id=<?=$i?>" usepost></a>
442
<?php	else: ?>
443
							-
444
<?php	endif; ?>
445
						</td>
446
					</tr>
447
<?php
448
	$i++;
449
	$nnats++;
450

    
451
endforeach;
452

    
453
// There can be separator(s) after the last rule listed.
454
if ($seprows[$nnats]) {
455
	display_separator($separators, $nnats, $columns_in_table);
456
}
457
?>
458
				</tbody>
459
			</table>
460
		</div>
461
	</div>
462

    
463
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
464
	<nav class="action-buttons">
465
		<a href="firewall_nat_edit.php?after=-1" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the top of the list')?>">
466
			<i class="fa fa-level-up icon-embed-btn"></i>
467
			<?=gettext('Add')?>
468
		</a>
469
		<a href="firewall_nat_edit.php" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the end of the list')?>">
470
			<i class="fa fa-level-down icon-embed-btn"></i>
471
			<?=gettext('Add')?>
472
		</a>
473
		<button name="del_x" type="submit" class="btn btn-danger btn-sm" title="<?=gettext('Delete selected rules')?>">
474
			<i class="fa fa-trash icon-embed-btn"></i>
475
			<?=gettext("Delete"); ?>
476
		</button>
477
		<button type="submit" id="order-store" name="order-store" class="btn btn-primary btn-sm" disabled title="<?=gettext('Save rule order')?>">
478
			<i class="fa fa-save icon-embed-btn"></i>
479
			<?=gettext("Save")?>
480
		</button>
481
		<button type="submit" id="addsep" name="addsep" class="btn btn-sm btn-warning" title="<?=gettext('Add separator')?>">
482
			<i class="fa fa-plus icon-embed-btn"></i>
483
			<?=gettext("Separator")?>
484
		</button>
485
	</nav>
486
<?php	endif; ?>
487
</form>
488

    
489
<script type="text/javascript">
490
//<![CDATA[
491
//Need to create some variables here so that jquery/pfSenseHelpers.js can read them
492
iface = "<?=strtolower($if)?>";
493
cncltxt = '<?=gettext("Cancel")?>';
494
svtxt = '<?=gettext("Save")?>';
495
svbtnplaceholder = '<?=gettext("Enter a description, Save, then drag to final location.")?>';
496
configsection = "nat";
497
dirty = false;
498

    
499
events.push(function() {
500

    
501
<?php if(!isset($config['system']['webgui']['roworderdragging'])): ?>
502
	// Make rules sortable
503
	$('table tbody.user-entries').sortable({
504
		cursor: 'grabbing',
505
		update: function(event, ui) {
506
			$('#order-store').removeAttr('disabled');
507
			dirty = true;
508
			reindex_rules(ui.item.parent('tbody'));
509
			dirty = true;
510
		}
511
	});
512
<?php endif; ?>
513

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

    
518
		// Save the separator bar configuration
519
		save_separators();
520

    
521
		// Suppress the "Do you really want to leave the page" message
522
		saving = true;
523

    
524
	});
525

    
526
	// Globals
527
	saving = false;
528
	dirty = false;
529

    
530
	// provide a warning message if the user tries to change page before saving
531
	$(window).bind('beforeunload', function(){
532
		if (!saving && dirty) {
533
			return ("<?=gettext('One or more Port Forward rules have been moved but have not yet been saved')?>");
534
		} else {
535
			return undefined;
536
		}
537
	});
538

    
539
	$('#selectAll').click(function() {
540
		var checkedStatus = this.checked;
541
		$('#ruletable tbody tr').find('td:first :checkbox').each(function() {
542
		$(this).prop('checked', checkedStatus);
543
		});
544
	});
545
});
546
//]]>
547
</script>
548
<?php
549

    
550
if (count($a_nat) > 0) {
551
?>
552
<!-- Legend -->
553
<div>
554
	<dl class="dl-horizontal responsive">
555
		<dt><?=gettext('Legend')?></dt>					<dd></dd>
556
		<dt><i class="fa fa-play"></i></dt>			<dd><?=gettext('Pass')?></dd>
557
		<dt><i class="fa fa-random"></i></dt>		<dd><?=gettext('Linked rule')?></dd>
558
	</dl>
559
</div>
560

    
561
<?php
562
}
563

    
564
include("foot.inc");
(42-42/234)