Project

General

Profile

Download (16.2 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-2016 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
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *
16
 * 1. Redistributions of source code must retain the above copyright notice,
17
 *    this list of conditions and the following disclaimer.
18
 *
19
 * 2. Redistributions in binary form must reproduce the above copyright
20
 *    notice, this list of conditions and the following disclaimer in
21
 *    the documentation and/or other materials provided with the
22
 *    distribution.
23
 *
24
 * 3. All advertising materials mentioning features or use of this software
25
 *    must display the following acknowledgment:
26
 *    "This product includes software developed by the pfSense Project
27
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
28
 *
29
 * 4. The names "pfSense" and "pfSense Project" must not be used to
30
 *    endorse or promote products derived from this software without
31
 *    prior written permission. For written permission, please contact
32
 *    coreteam@pfsense.org.
33
 *
34
 * 5. Products derived from this software may not be called "pfSense"
35
 *    nor may "pfSense" appear in their names without prior written
36
 *    permission of the Electric Sheep Fencing, LLC.
37
 *
38
 * 6. Redistributions of any form whatsoever must retain the following
39
 *    acknowledgment:
40
 *
41
 * "This product includes software developed by the pfSense Project
42
 * for use in the pfSense software distribution (http://www.pfsense.org/).
43
 *
44
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
45
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
48
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55
 * OF THE POSSIBILITY OF SUCH DAMAGE.
56
 */
57

    
58
##|+PRIV
59
##|*IDENT=page-firewall-nat-portforward
60
##|*NAME=Firewall: NAT: Port Forward
61
##|*DESCR=Allow access to the 'Firewall: NAT: Port Forward' page.
62
##|*MATCH=firewall_nat.php*
63
##|-PRIV
64

    
65
require_once("guiconfig.inc");
66
require_once("functions.inc");
67
require_once("filter.inc");
68
require_once("shaper.inc");
69
require_once("itemid.inc");
70

    
71
if (!is_array($config['nat']['rule'])) {
72
	$config['nat']['rule'] = array();
73
}
74

    
75
$a_nat = &$config['nat']['rule'];
76

    
77
/* update rule order, POST[rule] is an array of ordered IDs */
78
if (array_key_exists('order-store', $_POST)) {
79
	if (is_array($_POST['rule']) && !empty($_POST['rule'])) {
80
		$a_nat_new = array();
81

    
82
		// if a rule is not in POST[rule], it has been deleted by the user
83
		foreach ($_POST['rule'] as $id) {
84
			$a_nat_new[] = $a_nat[$id];
85
		}
86

    
87
		$a_nat = $a_nat_new;
88

    
89

    
90
		$config['nat']['separator'] = "";
91

    
92
		if ($_POST['separator']) {
93
			$idx = 0;
94
			foreach ($_POST['separator'] as $separator) {
95
				$config['nat']['separator']['sep' . $idx++] = $separator;
96
			}
97
		}
98

    
99
		if (write_config()) {
100
			mark_subsystem_dirty('filter');
101
		}
102

    
103
		header("Location: firewall_nat.php");
104
		exit;
105
	}
106
}
107

    
108
/* if a custom message has been passed along, lets process it */
109
if ($_GET['savemsg']) {
110
	$savemsg = $_GET['savemsg'];
111
}
112

    
113
if ($_POST) {
114
	$pconfig = $_POST;
115

    
116
	if ($_POST['apply']) {
117

    
118
		$retval = 0;
119

    
120
		$retval |= filter_configure();
121
		$savemsg = get_std_save_message($retval);
122

    
123
		pfSense_handle_custom_code("/usr/local/pkg/firewall_nat/apply");
124

    
125
		if ($retval == 0) {
126
			clear_subsystem_dirty('natconf');
127
			clear_subsystem_dirty('filter');
128
		}
129

    
130
	}
131
}
132

    
133
if ($_GET['act'] == "del") {
134
	if ($a_nat[$_GET['id']]) {
135

    
136
		if (isset($a_nat[$_GET['id']]['associated-rule-id'])) {
137
			delete_id($a_nat[$_GET['id']]['associated-rule-id'], $config['filter']['rule']);
138
			$want_dirty_filter = true;
139
		}
140
		unset($a_nat[$_GET['id']]);
141

    
142
		// Update the separators
143
		$a_separators = &$config['nat']['separator'];
144
		$ridx = $_GET['id'];
145
		$mvnrows = -1;
146
		move_separators($a_separators, $ridx, $mvnrows);
147

    
148
		if (write_config()) {
149
			mark_subsystem_dirty('natconf');
150
			if ($want_dirty_filter) {
151
				mark_subsystem_dirty('filter');
152
			}
153
		}
154

    
155
		header("Location: firewall_nat.php");
156
		exit;
157
	}
158
}
159

    
160
if (isset($_POST['del_x'])) {
161
	/* delete selected rules */
162
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
163
		$a_separators = &$config['nat']['separator'];
164
		$num_deleted = 0;
165

    
166
		foreach ($_POST['rule'] as $rulei) {
167
			$target = $rule['target'];
168

    
169
			// Check for filter rule associations
170
			if (isset($a_nat[$rulei]['associated-rule-id'])) {
171
				delete_id($a_nat[$rulei]['associated-rule-id'], $config['filter']['rule']);
172
				mark_subsystem_dirty('filter');
173
			}
174

    
175
			unset($a_nat[$rulei]);
176

    
177
			// Update the separators
178
			// As rules are deleted, $ridx has to be decremented or separator position will break
179
			$ridx = $rulei - $num_deleted;
180
			$mvnrows = -1;
181
			move_separators($a_separators, $ridx, $mvnrows);
182
			$num_deleted++;
183
		}
184

    
185
		if (write_config()) {
186
			mark_subsystem_dirty('natconf');
187
		}
188

    
189
		header("Location: firewall_nat.php");
190
		exit;
191
	}
192
} else if ($_GET['act'] == "toggle") {
193
	if ($a_nat[$_GET['id']]) {
194
		if (isset($a_nat[$_GET['id']]['disabled'])) {
195
			unset($a_nat[$_GET['id']]['disabled']);
196
		} else {
197
			$a_nat[$_GET['id']]['disabled'] = true;
198
		}
199
		if (write_config(gettext("Firewall: NAT: Port forward, enable/disable NAT rule"))) {
200
			mark_subsystem_dirty('natconf');
201
		}
202
		header("Location: firewall_nat.php");
203
		exit;
204
	}
205
}
206

    
207
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("Port Forward"));
208
include("head.inc");
209

    
210
if ($savemsg) {
211
	print_info_box($savemsg, 'success');
212
}
213

    
214
if (is_subsystem_dirty('natconf')) {
215
	print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
216
					gettext('The changes must be applied for them to take effect.'));
217
}
218

    
219
$tab_array = array();
220
$tab_array[] = array(gettext("Port Forward"), true, "firewall_nat.php");
221
$tab_array[] = array(gettext("1:1"), false, "firewall_nat_1to1.php");
222
$tab_array[] = array(gettext("Outbound"), false, "firewall_nat_out.php");
223
$tab_array[] = array(gettext("NPt"), false, "firewall_nat_npt.php");
224
display_top_tabs($tab_array);
225

    
226
$columns_in_table = 13;
227
?>
228

    
229
<form action="firewall_nat.php" method="post" name="iform">
230
	<div class="panel panel-default">
231
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Rules')?></h2></div>
232
		<div class="panel-body table-responsive">
233
			<table id="ruletable" class="table table-striped table-hover table-condensed">
234
				<thead>
235
					<tr>
236
						<th><!-- Checkbox --></th>
237
						<th><!-- Icon --></th>
238
						<th><!-- Rule type --></th>
239
						<th><?=gettext("Interface")?></th>
240
						<th><?=gettext("Protocol")?></th>
241
						<th><?=gettext("Source Address")?></th>
242
						<th><?=gettext("Source Ports")?></th>
243
						<th><?=gettext("Dest. Address")?></th>
244
						<th><?=gettext("Dest. Ports")?></th>
245
						<th><?=gettext("NAT IP")?></th>
246
						<th><?=gettext("NAT Ports")?></th>
247
						<th><?=gettext("Description")?></th>
248
						<th><?=gettext("Actions")?></th>
249
					</tr>
250
				</thead>
251
				<tbody class='user-entries'>
252
<?php
253

    
254
$nnats = $i = 0;
255
$separators = $config['nat']['separator'];
256

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

    
261
foreach ($a_nat as $natent):
262

    
263
	// Display separator(s) for section beginning at rule n
264
	if ($seprows[$nnats]) {
265
		display_separator($separators, $nnats, $columns_in_table);
266
	}
267

    
268
	$localport = $natent['local-port'];
269

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

    
272
	if ($dstendport) {
273
		$localendport = $natent['local-port'] + $dstendport - $dstbeginport;
274
		$localport	 .= '-' . $localendport;
275
	}
276

    
277
	$alias = rule_columns_with_alias(
278
		$natent['source']['address'],
279
		pprint_port($natent['source']['port']),
280
		$natent['destination']['address'],
281
		pprint_port($natent['destination']['port']),
282
		$natent['target'],
283
		$localport
284
	);
285

    
286
	/* if user does not have access to edit an interface skip on to the next record */
287
	if (!have_natpfruleint_access($natent['interface'])) {
288
		continue;
289
	}
290

    
291
	if (isset($natent['disabled'])) {
292
		$iconfn = "pass_d";
293
		$trclass = 'class="disabled"';
294
	} else {
295
		$iconfn = "pass";
296
		$trclass = '';
297
	}
298
?>
299

    
300
					<tr id="fr<?=$nnats;?>" <?=$trclass?> onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_edit.php?id=<?=$i;?>';">
301
						<td >
302
							<input type="checkbox" id="frc<?=$nnats;?>" onClick="fr_toggle(<?=$nnats;?>)" name="rule[]" value="<?=$i;?>"/>
303
						</td>
304
						<td>
305
							<a href="?act=toggle&amp;id=<?=$i?>">
306
								<i class="fa <?= ($iconfn == "pass") ? "fa-check":"fa-times"?>" title="<?=gettext("click to toggle enabled/disabled status")?>"></i>
307
<?php 	if (isset($natent['nordr'])) { ?>
308
								&nbsp;<i class="fa fa-hand-stop-o text-danger" title="<?=gettext("Negated: This rule excludes NAT from a later rule")?>"></i>
309
<?php 	} ?>
310
							</a>
311
						</td>
312
						<td>
313
<?php
314
	if ($natent['associated-rule-id'] == "pass"):
315
?>
316
							<i class="fa fa-play" title="<?=gettext("All traffic matching this NAT entry is passed")?>"></i>
317
<?php
318
	elseif (!empty($natent['associated-rule-id'])):
319
?>
320
							<i class="fa fa-random" title="<?=sprintf(gettext("Firewall rule ID %s is managed by this rule"), htmlspecialchars($natent['associated-rule-id']))?>"></i>
321
<?php
322
	endif;
323
?>
324
						</td>
325
						<td>
326
							<?=$textss?>
327
<?php
328
	if (!$natent['interface']) {
329
		echo htmlspecialchars(convert_friendly_interface_to_friendly_descr("wan"));
330
	} else {
331
		echo htmlspecialchars(convert_friendly_interface_to_friendly_descr($natent['interface']));
332
	}
333
?>
334
							<?=$textse?>
335
						</td>
336

    
337
						<td>
338
							<?=$textss?><?=strtoupper($natent['protocol'])?><?=$textse?>
339
						</td>
340

    
341
						<td>
342

    
343

    
344
<?php
345
	if (isset($alias['src'])):
346
?>
347
							<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">
348
<?php
349
	endif;
350
?>
351
							<?=str_replace('_', ' ', htmlspecialchars(pprint_address($natent['source'])))?>
352
<?php
353
	if (isset($alias['src'])):
354
?>
355
							</a>
356
<?php
357
	endif;
358
?>
359
						</td>
360
						<td>
361
<?php
362
	if (isset($alias['srcport'])):
363
?>
364
							<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">
365
<?php
366
	endif;
367
?>
368
							<?=str_replace('_', ' ', htmlspecialchars(pprint_port($natent['source']['port'])))?>
369
<?php
370
	if (isset($alias['srcport'])):
371
?>
372
							</a>
373
<?php
374
	endif;
375
?>
376
						</td>
377

    
378
						<td>
379
<?php
380
	if (isset($alias['dst'])):
381
?>
382
							<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">
383
<?php
384
	endif;
385
?>
386
							<?=str_replace('_', ' ', htmlspecialchars(pprint_address($natent['destination'])))?>
387
<?php
388
	if (isset($alias['dst'])):
389
?>
390
							</a>
391
<?php
392
	endif;
393
?>
394
						</td>
395
						<td>
396
<?php
397
	if (isset($alias['dstport'])):
398
?>
399
							<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">
400
<?php
401
	endif;
402
?>
403
							<?=str_replace('_', ' ', htmlspecialchars(pprint_port($natent['destination']['port'])))?>
404
<?php
405
	if (isset($alias['dstport'])):
406
?>
407
							</a>
408
<?php
409
	endif;
410
?>
411
						</td>
412
						<td>
413
<?php
414
	if (isset($alias['target'])):
415
?>
416
							<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">
417
<?php
418
	endif;
419
?>
420

    
421
							<?=str_replace('_', ' ', htmlspecialchars($natent['target']))?>
422
<?php
423
	if (isset($alias['target'])):
424
?>
425
							</a>
426
<?php
427
	endif;
428
?>
429
						</td>
430
						<td>
431
<?php
432
	if (isset($alias['targetport'])):
433
?>
434
							<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">
435
<?php
436
	endif;
437
?>
438
							<?=str_replace('_', ' ', htmlspecialchars(pprint_port($localport)))?>
439
<?php
440
	if (isset($alias['targetport'])):
441
?>
442
							</a>
443
<?php
444
	endif;
445
?>
446
						</td>
447

    
448
						<td>
449
							<?=htmlspecialchars($natent['descr'])?>
450
						</td>
451
						<td>
452
							<a class="fa fa-pencil" title="<?=gettext("Edit rule"); ?>" href="firewall_nat_edit.php?id=<?=$i?>"></a>
453
							<a class="fa fa-clone"	  title="<?=gettext("Add a new NAT based on this one")?>" href="firewall_nat_edit.php?dup=<?=$i?>"></a>
454
							<a class="fa fa-trash"	title="<?=gettext("Delete rule")?>" href="firewall_nat.php?act=del&amp;id=<?=$i?>"></a>
455
						</td>
456
					</tr>
457
<?php
458
	$i++;
459
	$nnats++;
460

    
461
endforeach;
462

    
463
// There can be separator(s) after the last rule listed.
464
if ($seprows[$nnats]) {
465
	display_separator($separators, $nnats, $columns_in_table);
466
}
467
?>
468
				</tbody>
469
			</table>
470
		</div>
471
	</div>
472

    
473
	<nav class="action-buttons">
474
		<a href="firewall_nat_edit.php?after=-1" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the top of the list')?>">
475
			<i class="fa fa-level-up icon-embed-btn"></i>
476
			<?=gettext('Add')?>
477
		</a>
478
		<a href="firewall_nat_edit.php" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the end of the list')?>">
479
			<i class="fa fa-level-down icon-embed-btn"></i>
480
			<?=gettext('Add')?>
481
		</a>
482
		<button name="del_x" type="submit" class="btn btn-danger btn-sm" title="<?=gettext('Delete selected rules')?>">
483
			<i class="fa fa-trash icon-embed-btn"></i>
484
			<?=gettext("Delete"); ?>
485
		</button>
486
		<button type="submit" id="order-store" name="order-store" class="btn btn-primary btn-sm" disabled title="<?=gettext('Save rule order')?>">
487
			<i class="fa fa-save icon-embed-btn"></i>
488
			<?=gettext("Save")?>
489
		</button>
490
		<button type="submit" id="addsep" name="addsep" class="btn btn-sm btn-warning" title="<?=gettext('Add separator')?>">
491
			<i class="fa fa-plus icon-embed-btn"></i>
492
			<?=gettext("Separator")?>
493
		</button>
494
	</nav>
495
</form>
496

    
497
<script type="text/javascript">
498
//<![CDATA[
499
//Need to create some variables here so that jquery/pfSenseHelpers.js can read them
500
iface = "<?=strtolower($if)?>";
501
cncltxt = '<?=gettext("Cancel")?>';
502
svtxt = '<?=gettext("Save")?>';
503
svbtnplaceholder = '<?=gettext("Enter a description, Save, then drag to final location.")?>';
504
configsection = "nat";
505
dirty = false;
506

    
507
events.push(function() {
508

    
509
	// Make rules sortable
510
	$('table tbody.user-entries').sortable({
511
		cursor: 'grabbing',
512
		update: function(event, ui) {
513
			$('#order-store').removeAttr('disabled');
514
			dirty = true;
515
			reindex_rules(ui.item.parent('tbody'));
516
			dirty = true;
517
		}
518
	});
519

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

    
524
		// Save the separator bar configuration
525
		save_separators();
526

    
527
		// Suppress the "Do you really want to leave the page" message
528
		saving = true;
529

    
530
	});
531

    
532
	// Globals
533
	saving = false;
534
	dirty = false;
535

    
536
	// provide a warning message if the user tries to change page before saving
537
	$(window).bind('beforeunload', function(){
538
		if (!saving && dirty) {
539
			return ("<?=gettext('One or more Port Forward rules have been moved but have not yet been saved')?>");
540
		} else {
541
			return undefined;
542
		}
543
	});
544
});
545
//]]>
546
</script>
547
<?php
548

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

    
560
<?php
561
}
562

    
563
include("foot.inc");
(40-40/227)