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-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2024 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 <[email protected]>.
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("util.inc");
37
require_once("functions.inc");
38
require_once("filter.inc");
39
require_once("shaper.inc");
40
require_once("itemid.inc");
41
require_once("firewall_nat.inc");
42

    
43
config_init_path('nat/rule');
44
$rdr_lcltype_flags = [SPECIALNET_IFADDR];
45
$rdr_srctype_flags = [SPECIALNET_ANY, SPECIALNET_CLIENTS, SPECIALNET_IFADDR, SPECIALNET_IFNET];
46
$rdr_dsttype_flags = [SPECIALNET_ANY, SPECIALNET_SELF, SPECIALNET_CLIENTS, SPECIALNET_IFADDR, SPECIALNET_IFNET, SPECIALNET_VIPS];
47

    
48
// Process $_POST/$_REQUEST =======================================================================
49
if ($_REQUEST['savemsg']) {
50
	$savemsg = $_REQUEST['savemsg'];
51
}
52

    
53
if (array_key_exists('order-store', $_REQUEST) && have_natpfruleint_access($natent['interface'])) {
54
	reorderNATrules($_POST);
55
} else if ($_POST['apply'] && have_natpfruleint_access($natent['interface'])) {
56
	$retval = applyNATrules();
57
} else if (($_POST['act'] == "del" || isset($_POST['del_x'])) && have_natpfruleint_access($natent['interface'])) {
58
	if (config_get_path("nat/rule/{$_POST['id']}") || (is_array($_POST['rule']) && count($_POST['rule']))) {
59
		deleteNATrule($_POST);
60
	}
61
} elseif (($_POST['act'] == "toggle" || isset($_POST['toggle_x'])) && have_natpfruleint_access($natent['interface'])) {
62
	if (config_get_path("nat/rule/{$_POST['id']}") || (is_array($_POST['rule']) && count($_POST['rule']))) {
63
		toggleNATrule($_POST);
64
	}
65
}
66

    
67
// Construct the page =============================================================================
68
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("Port Forward"));
69
$pglinks = array("", "@self", "@self");
70
include("head.inc");
71

    
72
if ($_POST['apply']) {
73
	print_apply_result_box($retval);
74
}
75

    
76
if (is_subsystem_dirty('natconf') && have_natpfruleint_access($natent['interface'])) {
77
	print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
78
					gettext('The changes must be applied for them to take effect.'));
79
}
80

    
81
$tab_array = array();
82
$tab_array[] = array(gettext("Port Forward"), true, "firewall_nat.php");
83
$tab_array[] = array(gettext("1:1"), false, "firewall_nat_1to1.php");
84
$tab_array[] = array(gettext("Outbound"), false, "firewall_nat_out.php");
85
$tab_array[] = array(gettext("NPt"), false, "firewall_nat_npt.php");
86
display_top_tabs($tab_array);
87

    
88
$columns_in_table = 13;
89
?>
90
<!-- Allow table to scroll when dragging outside of the display window -->
91
<style>
92
.table-responsive {
93
    clear: both;
94
    overflow-x: visible;
95
    margin-bottom: 0px;
96
}
97
</style>
98

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

    
124
$nnats = $i = 0;
125
$separators = config_get_path('nat/separator');
126

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

    
131
global $user_settings;
132
$show_system_alias_popup = (array_key_exists('webgui', $user_settings) && !$user_settings['webgui']['disablealiaspopupdetail']);
133
$system_alias_specialnet = get_specialnet('', [SPECIALNET_IFNET, SPECIALNET_GROUP]);
134
foreach (config_get_path('nat/rule', []) as $natent):
135

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

    
141
	$localport = $natent['local-port'];
142

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

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

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

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

    
167
?>
168

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

    
210
						<td>
211
							<?=$textss?><?=strtoupper($natent['protocol'])?><?=$textse?>
212
						</td>
213

    
214
						<td>
215
							<?php if (isset($alias['src'])): ?>
216
								<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">
217
									<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['source'], $rdr_srctype_flags)))?>
218
								</a>
219
							<?php elseif ($show_system_alias_popup && array_key_exists($natent['source']['network'], $system_alias_specialnet)): ?>
220
								<a data-toggle="popover" data-trigger="hover focus" title="<?=gettext('System alias details')?>" data-content="<?=system_alias_info_popup(strtoupper($natent['source']['network']) . '__NETWORK')?>" data-html="true">
221
									<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['source'], $rdr_srctype_flags)))?>
222
								</a>
223
							<?php else: ?>
224
								<?=htmlspecialchars(pprint_address($natent['source'], $rdr_srctype_flags))?>
225
							<?php endif; ?>
226
						</td>
227
						<td>
228
<?php
229
	if (isset($alias['srcport'])):
230
?>
231
							<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">
232
<?php
233
	endif;
234
?>
235
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['source']['port'])))?>
236
<?php
237
	if (isset($alias['srcport'])):
238
?>
239
							</a>
240
<?php
241
	endif;
242
?>
243
						</td>
244

    
245
						<td>
246
							<?php if (isset($alias['dst'])): ?>
247
								<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">
248
									<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['destination'], $rdr_dsttype_flags)))?>
249
								</a>
250
							<?php elseif ($show_system_alias_popup && array_key_exists($natent['destination']['network'], $system_alias_specialnet)): ?>
251
								<a data-toggle="popover" data-trigger="hover focus" title="<?=gettext('System alias details')?>" data-content="<?=system_alias_info_popup(strtoupper($natent['destination']['network']) . '__NETWORK')?>" data-html="true">
252
									<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['destination'], $rdr_dsttype_flags)))?>
253
								</a>
254
							<?php else: ?>
255
								<?=htmlspecialchars(pprint_address($natent['destination'], $rdr_dsttype_flags))?>
256
							<?php endif; ?>
257
						</td>
258
						<td>
259
<?php
260
	if (isset($alias['dstport'])):
261
?>
262
							<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">
263
<?php
264
	endif;
265
?>
266
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['destination']['port'])))?>
267
<?php
268
	if (isset($alias['dstport'])):
269
?>
270
							</a>
271
<?php
272
	endif;
273
?>
274
						</td>
275
						<td>
276
							<?php if (isset($alias['target'])): ?>
277
								<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">
278
									<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address(['network' => $natent['target']], $rdr_lcltype_flags)))?>
279
								</a>
280
							<?php else: ?>
281
								<?=htmlspecialchars(pprint_address(['network' => $natent['target']], $rdr_lcltype_flags))?>
282
							<?php endif; ?>
283
						</td>
284
						<td>
285
<?php
286
	if (isset($alias['targetport'])):
287
?>
288
							<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">
289
<?php
290
	endif;
291
?>
292
							<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($localport)))?>
293
<?php
294
	if (isset($alias['targetport'])):
295
?>
296
							</a>
297
<?php
298
	endif;
299
?>
300
						</td>
301

    
302
						<td>
303
							<?=htmlspecialchars($natent['descr'])?>
304
						</td>
305
						<td>
306
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
307
							<a class="fa-solid fa-pencil" title="<?=gettext("Edit rule"); ?>" href="firewall_nat_edit.php?id=<?=$i?>"></a>
308
							<a class="fa-regular fa-clone"	  title="<?=gettext("Add a new NAT based on this one")?>" href="firewall_nat_edit.php?dup=<?=$i?>"></a>
309
							<a class="fa-solid fa-trash-can"	title="<?=gettext("Delete rule")?>" href="firewall_nat.php?act=del&amp;id=<?=$i?>" usepost></a>
310
<?php	else: ?>
311
							-
312
<?php	endif; ?>
313
						</td>
314
					</tr>
315
<?php
316
	$i++;
317
	$nnats++;
318

    
319
endforeach;
320

    
321
// There can be separator(s) after the last rule listed.
322
if ($seprows[$nnats]) {
323
	display_separator($separators, $nnats, $columns_in_table);
324
}
325
?>
326
				</tbody>
327
			</table>
328
		</div>
329
	</div>
330

    
331
<?php	if (have_natpfruleint_access($natent['interface'])): ?>
332
	<nav class="action-buttons">
333
		<a href="firewall_nat_edit.php?after=-1" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the top of the list')?>">
334
			<i class="fa-solid fa-turn-up icon-embed-btn"></i>
335
			<?=gettext('Add')?>
336
		</a>
337
		<a href="firewall_nat_edit.php" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the end of the list')?>">
338
			<i class="fa-solid fa-turn-down icon-embed-btn"></i>
339
			<?=gettext('Add')?>
340
		</a>
341
		<button id="del_x" name="del_x" type="submit" class="btn btn-danger btn-sm" disabled title="<?=gettext('Delete selected rules')?>">
342
			<i class="fa-solid fa-trash-can icon-embed-btn"></i>
343
			<?=gettext("Delete"); ?>
344
		</button>
345
		<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')?>">
346
			<i class="fa-solid fa-ban icon-embed-btn"></i>
347
			<?=gettext("Toggle"); ?>
348
		</button>
349
		<button type="submit" id="order-store" name="order-store" class="btn btn-primary btn-sm" disabled title="<?=gettext('Save rule order')?>">
350
			<i class="fa-solid fa-save icon-embed-btn"></i>
351
			<?=gettext("Save")?>
352
		</button>
353
		<button type="submit" id="addsep" name="addsep" class="btn btn-sm btn-warning" title="<?=gettext('Add separator')?>">
354
			<i class="fa-solid fa-plus icon-embed-btn"></i>
355
			<?=gettext("Separator")?>
356
		</button>
357
	</nav>
358
<?php	endif; ?>
359
</form>
360

    
361
<script type="text/javascript">
362
//<![CDATA[
363
//Need to create some variables here so that jquery/pfSenseHelpers.js can read them
364
iface = "<?=strtolower($if)?>";
365
cncltxt = '<?=gettext("Cancel")?>';
366
svtxt = '<?=gettext("Save")?>';
367
svbtnplaceholder = '<?=gettext("Enter a description, Save, then drag to final location.")?>';
368
configsection = "nat";
369
dirty = false;
370

    
371
events.push(function() {
372

    
373
<?php if(!config_path_enabled('system/webgui', 'roworderdragging')): ?>
374
	// Make rules sortable
375
	$('table tbody.user-entries').sortable({
376
		cursor: 'grabbing',
377
		update: function(event, ui) {
378
			$('#order-store').removeAttr('disabled');
379
			dirty = true;
380
			reindex_rules(ui.item.parent('tbody'));
381
			dirty = true;
382
		}
383
	});
384
<?php endif; ?>
385

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

    
390
		// Save the separator bar configuration
391
		save_separators();
392

    
393
		// Suppress the "Do you really want to leave the page" message
394
		saving = true;
395

    
396
	});
397

    
398
	$('[id^=fr]').click(function () {
399
		buttonsmode('frc', ['del_x', 'toggle_x']);
400
	});
401

    
402
	// Globals
403
	saving = false;
404
	dirty = false;
405

    
406
	// provide a warning message if the user tries to change page before saving
407
	// Unfortunately the custom message is not supported in modern browsers, but he user wil lat
408
	// least see a generic warning message
409
	$(window).bind('beforeunload', function(){
410
		if (!saving && dirty) {
411
			return ("<?=gettext('One or more Port Forward rules have been moved but have not yet been saved')?>");
412
		} else {
413
			return undefined;
414
		}
415
	});
416

    
417
	$('#selectAll').click(function() {
418
		var checkedStatus = this.checked;
419
		$('#ruletable tbody tr').find('td:first :checkbox').each(function() {
420
		$(this).prop('checked', checkedStatus);
421
		});
422
		buttonsmode('frc', ['del_x', 'toggle_x']);
423
	});
424
});
425
//]]>
426
</script>
427
<?php
428

    
429
if (count(config_get_path('nat/rule', [])) > 0) {
430
?>
431
<!-- Legend -->
432
<div>
433
	<dl class="dl-horizontal responsive">
434
		<dt><?=gettext('Legend')?></dt>					<dd></dd>
435
		<dt><i class="fa-solid fa-play"></i></dt>			<dd><?=gettext('Pass')?></dd>
436
		<dt><i class="fa-solid fa-random"></i></dt>		<dd><?=gettext('Linked rule')?></dd>
437
	</dl>
438
</div>
439

    
440
<?php
441
}
442

    
443
include("foot.inc");
(44-44/232)
OSZAR »