Project

General

Profile

Download (14.4 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-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2022 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-nat-portforward
30
##|*NAME=Firewall: NAT: Port Forward
31
##|*DESCR=Allow access to the 'Firewall: NAT: Port Forward' page.
32
##|*MATCH=firewall_nat.php*
33
##|-PRIV
34

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

    
42
init_config_arr(array('nat', 'rule'));
43
$a_nat = &$config['nat']['rule'];
44

    
45
// Process $_POST/$_REQUEST =======================================================================
46
if ($_REQUEST['savemsg']) {
47
	$savemsg = $_REQUEST['savemsg'];
48
}
49

    
50
if (array_key_exists('order-store', $_REQUEST) && have_natpfruleint_access($natent['interface'])) {
51
	reorderNATrules($_POST);
52
} else if ($_POST['apply'] && have_natpfruleint_access($natent['interface'])) {
53
	$retval = applyNATrules();
54
} else if (($_POST['act'] == "del") && have_natpfruleint_access($natent['interface'])) {
55
	if ($a_nat[$_POST['id']]) {
56
		deleteNATrule($_POST);
57
	}
58
} else if (isset($_POST['del_x']) && have_natpfruleint_access($natent['interface'])) {
59
	/* delete selected rules */
60
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
61
		deleteMultipleNATrules($_POST);
62
	}
63
} else if (isset($_POST['toggle_x']) && have_natpfruleint_access($natent['interface'])) {
64
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
65
		toggleMultipleNATrules($_POST);
66
	}
67
} elseif (($_POST['act'] == "toggle") && have_natpfruleint_access($natent['interface'])) {
68
	if ($a_nat[$_POST['id']]) {
69
		toggleNATrule($_POST);
70
	}
71
}
72

    
73
// Construct the page =============================================================================
74
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("Port Forward"));
75
$pglinks = array("", "@self", "@self");
76
include("head.inc");
77

    
78
if ($_POST['apply']) {
79
	print_apply_result_box($retval);
80
}
81

    
82
if (is_subsystem_dirty('natconf') && have_natpfruleint_access($natent['interface'])) {
83
	print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
84
					gettext('The changes must be applied for them to take effect.'));
85
}
86

    
87
$tab_array = array();
88
$tab_array[] = array(gettext("Port Forward"), true, "firewall_nat.php");
89
$tab_array[] = array(gettext("1:1"), false, "firewall_nat_1to1.php");
90
$tab_array[] = array(gettext("Outbound"), false, "firewall_nat_out.php");
91
$tab_array[] = array(gettext("NPt"), false, "firewall_nat_npt.php");
92
display_top_tabs($tab_array);
93

    
94
$columns_in_table = 13;
95
?>
96
<!-- Allow table to scroll when dragging outside of the display window -->
97
<style>
98
.table-responsive {
99
    clear: both;
100
    overflow-x: visible;
101
    margin-bottom: 0px;
102
}
103
</style>
104

    
105
<form action="firewall_nat.php" method="post" name="iform">
106
	<div class="panel panel-default">
107
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Rules')?></h2></div>
108
		<div class="panel-body table-responsive">
109
			<table id="ruletable" class="table table-striped table-hover table-condensed">
110
				<thead>
111
					<tr>
112
						<th style="padding-left:10px;">  <input type="checkbox" id="selectAll" name="selectAll" /></th>
113
						<th><!-- Icon --></th>
114
						<th><!-- Rule type --></th>
115
						<th><?=gettext("Interface")?></th>
116
						<th><?=gettext("Protocol")?></th>
117
						<th><?=gettext("Source Address")?></th>
118
						<th><?=gettext("Source Ports")?></th>
119
						<th><?=gettext("Dest. Address")?></th>
120
						<th><?=gettext("Dest. Ports")?></th>
121
						<th><?=gettext("NAT IP")?></th>
122
						<th><?=gettext("NAT Ports")?></th>
123
						<th><?=gettext("Description")?></th>
124
						<th><?=gettext("Actions")?></th>
125
					</tr>
126
				</thead>
127
				<tbody class='user-entries'>
128
<?php
129

    
130
$nnats = $i = 0;
131
$separators = $config['nat']['separator'];
132

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

    
137
foreach ($a_nat as $natent):
138

    
139
	// Display separator(s) for section beginning at rule n
140
	if ($seprows[$nnats]) {
141
		display_separator($separators, $nnats, $columns_in_table);
142
	}
143

    
144
	$localport = $natent['local-port'];
145

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

    
148
	if ($dstendport && is_port($localport)) {
149
		$localendport = $natent['local-port'] + $dstendport - $dstbeginport;
150
		$localport	 .= '-' . $localendport;
151
	}
152

    
153
	$alias = rule_columns_with_alias(
154
		$natent['source']['address'],
155
		pprint_port($natent['source']['port']),
156
		$natent['destination']['address'],
157
		pprint_port($natent['destination']['port']),
158
		$natent['target'],
159
		$localport
160
	);
161

    
162
	if (isset($natent['disabled'])) {
163
		$iconfn = "pass_d";
164
		$trclass = 'class="disabled"';
165
	} else {
166
		$iconfn = "pass";
167
		$trclass = '';
168
	}
169

    
170
	if (is_specialnet($natent['target'])) {
171
		foreach ($ifdisp as $kif => $kdescr) {
172
			if ($natent['target'] == "{$kif}ip") {
173
				$natent['target'] = $kdescr . ' address';
174
				break;
175
			}
176
		}
177
	}
178
?>
179

    
180
					<tr id="fr<?=$nnats;?>" <?=$trclass?> onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_edit.php?id=<?=$i;?>';">
181
						<td >
182
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
183
							<input type="checkbox" id="frc<?=$nnats;?>" onClick="fr_toggle(<?=$nnats;?>)" name="rule[]" value="<?=$i;?>"/>
184
<?php	endif; ?>
185
						</td>
186
						<td>
187
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
188
							<a href="?act=toggle&amp;id=<?=$i?>" usepost>
189
								<i class="fa fa-check" title="<?=gettext("click to toggle enabled/disabled status")?>"></i>
190
							</a>
191
<?php	endif; ?>
192
<?php 	if (isset($natent['nordr'])) { ?>
193
								&nbsp;<i class="fa fa-hand-stop-o text-danger" title="<?=gettext("Negated: This rule excludes NAT from a later rule")?>"></i>
194
<?php 	} ?>
195
						</td>
196
						<td>
197
<?php
198
	if ($natent['associated-rule-id'] == "pass"):
199
?>
200
							<i class="fa fa-play" title="<?=gettext("All traffic matching this NAT entry is passed")?>"></i>
201
<?php
202
	elseif (!empty($natent['associated-rule-id'])):
203
?>
204
							<i class="fa fa-random" title="<?=sprintf(gettext("Firewall rule ID %s is managed by this rule"), htmlspecialchars($natent['associated-rule-id']))?>"></i>
205
<?php
206
	endif;
207
?>
208
						</td>
209
						<td>
210
							<?=$textss?>
211
<?php
212
	if (!$natent['interface']) {
213
		echo htmlspecialchars(convert_friendly_interface_to_friendly_descr("wan"));
214
	} else {
215
		echo htmlspecialchars(convert_friendly_interface_to_friendly_descr($natent['interface']));
216
	}
217
?>
218
							<?=$textse?>
219
						</td>
220

    
221
						<td>
222
							<?=$textss?><?=strtoupper($natent['protocol'])?><?=$textse?>
223
						</td>
224

    
225
						<td>
226

    
227

    
228
<?php
229
	if (isset($alias['src'])):
230
?>
231
							<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">
232
<?php
233
	endif;
234
?>
235
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['source'])))?>
236
<?php
237
	if (isset($alias['src'])):
238
?>
239
							</a>
240
<?php
241
	endif;
242
?>
243
						</td>
244
						<td>
245
<?php
246
	if (isset($alias['srcport'])):
247
?>
248
							<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">
249
<?php
250
	endif;
251
?>
252
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['source']['port'])))?>
253
<?php
254
	if (isset($alias['srcport'])):
255
?>
256
							</a>
257
<?php
258
	endif;
259
?>
260
						</td>
261

    
262
						<td>
263
<?php
264
	if (isset($alias['dst'])):
265
?>
266
							<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">
267
<?php
268
	endif;
269
?>
270
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['destination'])))?>
271
<?php
272
	if (isset($alias['dst'])):
273
?>
274
							</a>
275
<?php
276
	endif;
277
?>
278
						</td>
279
						<td>
280
<?php
281
	if (isset($alias['dstport'])):
282
?>
283
							<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">
284
<?php
285
	endif;
286
?>
287
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['destination']['port'])))?>
288
<?php
289
	if (isset($alias['dstport'])):
290
?>
291
							</a>
292
<?php
293
	endif;
294
?>
295
						</td>
296
						<td>
297
<?php
298
	if (isset($alias['target'])):
299
?>
300
							<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" >
301
<?php
302
	endif;
303
?>
304

    
305
							<?=str_replace('_', '_<wbr>', htmlspecialchars($natent['target']))?>
306
<?php
307
	if (isset($alias['target'])):
308
?>
309
							</a>
310
<?php
311
	endif;
312
?>
313
						</td>
314
						<td>
315
<?php
316
	if (isset($alias['targetport'])):
317
?>
318
							<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">
319
<?php
320
	endif;
321
?>
322
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($localport)))?>
323
<?php
324
	if (isset($alias['targetport'])):
325
?>
326
							</a>
327
<?php
328
	endif;
329
?>
330
						</td>
331

    
332
						<td>
333
							<?=htmlspecialchars($natent['descr'])?>
334
						</td>
335
						<td>
336
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
337
							<a class="fa fa-pencil" title="<?=gettext("Edit rule"); ?>" href="firewall_nat_edit.php?id=<?=$i?>"></a>
338
							<a class="fa fa-clone"	  title="<?=gettext("Add a new NAT based on this one")?>" href="firewall_nat_edit.php?dup=<?=$i?>"></a>
339
							<a class="fa fa-trash"	title="<?=gettext("Delete rule")?>" href="firewall_nat.php?act=del&amp;id=<?=$i?>" usepost></a>
340
<?php	else: ?>
341
							-
342
<?php	endif; ?>
343
						</td>
344
					</tr>
345
<?php
346
	$i++;
347
	$nnats++;
348

    
349
endforeach;
350

    
351
// There can be separator(s) after the last rule listed.
352
if ($seprows[$nnats]) {
353
	display_separator($separators, $nnats, $columns_in_table);
354
}
355
?>
356
				</tbody>
357
			</table>
358
		</div>
359
	</div>
360

    
361
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
362
	<nav class="action-buttons">
363
		<a href="firewall_nat_edit.php?after=-1" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the top of the list')?>">
364
			<i class="fa fa-level-up icon-embed-btn"></i>
365
			<?=gettext('Add')?>
366
		</a>
367
		<a href="firewall_nat_edit.php" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the end of the list')?>">
368
			<i class="fa fa-level-down icon-embed-btn"></i>
369
			<?=gettext('Add')?>
370
		</a>
371
		<button id="del_x" name="del_x" type="submit" class="btn btn-danger btn-sm" disabled title="<?=gettext('Delete selected rules')?>">
372
			<i class="fa fa-trash icon-embed-btn"></i>
373
			<?=gettext("Delete"); ?>
374
		</button>
375
		<button id="toggle_x" name="toggle_x" type="submit" class="btn btn-primary btn-sm" disabled value="<?=gettext("Toggle selected rules"); ?>" title="<?=gettext('Toggle selected rules')?>">
376
			<i class="fa fa-ban icon-embed-btn"></i>
377
			<?=gettext("Toggle"); ?>
378
		</button>
379
		<button type="submit" id="order-store" name="order-store" class="btn btn-primary btn-sm" disabled title="<?=gettext('Save rule order')?>">
380
			<i class="fa fa-save icon-embed-btn"></i>
381
			<?=gettext("Save")?>
382
		</button>
383
		<button type="submit" id="addsep" name="addsep" class="btn btn-sm btn-warning" title="<?=gettext('Add separator')?>">
384
			<i class="fa fa-plus icon-embed-btn"></i>
385
			<?=gettext("Separator")?>
386
		</button>
387
	</nav>
388
<?php	endif; ?>
389
</form>
390

    
391
<script type="text/javascript">
392
//<![CDATA[
393
//Need to create some variables here so that jquery/pfSenseHelpers.js can read them
394
iface = "<?=strtolower($if)?>";
395
cncltxt = '<?=gettext("Cancel")?>';
396
svtxt = '<?=gettext("Save")?>';
397
svbtnplaceholder = '<?=gettext("Enter a description, Save, then drag to final location.")?>';
398
configsection = "nat";
399
dirty = false;
400

    
401
events.push(function() {
402

    
403
<?php if(!isset($config['system']['webgui']['roworderdragging'])): ?>
404
	// Make rules sortable
405
	$('table tbody.user-entries').sortable({
406
		cursor: 'grabbing',
407
		update: function(event, ui) {
408
			$('#order-store').removeAttr('disabled');
409
			dirty = true;
410
			reindex_rules(ui.item.parent('tbody'));
411
			dirty = true;
412
		}
413
	});
414
<?php endif; ?>
415

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

    
420
		// Save the separator bar configuration
421
		save_separators();
422

    
423
		// Suppress the "Do you really want to leave the page" message
424
		saving = true;
425

    
426
	});
427

    
428
	$('[id^=fr]').click(function () {
429
		buttonsmode('frc', ['del_x', 'toggle_x']);
430
	});
431

    
432
	// Globals
433
	saving = false;
434
	dirty = false;
435

    
436
	// provide a warning message if the user tries to change page before saving
437
	// Unfortunately the custom message is not supported in modern browsers, but he user wil lat
438
	// least see a generic warning message
439
	$(window).bind('beforeunload', function(){
440
		if (!saving && dirty) {
441
			return ("<?=gettext('One or more Port Forward rules have been moved but have not yet been saved')?>");
442
		} else {
443
			return undefined;
444
		}
445
	});
446

    
447
	$('#selectAll').click(function() {
448
		var checkedStatus = this.checked;
449
		$('#ruletable tbody tr').find('td:first :checkbox').each(function() {
450
		$(this).prop('checked', checkedStatus);
451
		});
452
		buttonsmode('frc', ['del_x', 'toggle_x']);
453
	});
454
});
455
//]]>
456
</script>
457
<?php
458

    
459
if (count($a_nat) > 0) {
460
?>
461
<!-- Legend -->
462
<div>
463
	<dl class="dl-horizontal responsive">
464
		<dt><?=gettext('Legend')?></dt>					<dd></dd>
465
		<dt><i class="fa fa-play"></i></dt>			<dd><?=gettext('Pass')?></dd>
466
		<dt><i class="fa fa-random"></i></dt>		<dd><?=gettext('Linked rule')?></dd>
467
	</dl>
468
</div>
469

    
470
<?php
471
}
472

    
473
include("foot.inc");
(42-42/228)