Project

General

Profile

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

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

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

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

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

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

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

    
88
		$a_nat = $a_nat_new;
89

    
90

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

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

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

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

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

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

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

    
119
		$retval = 0;
120

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

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

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

    
131
	}
132
}
133

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

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

    
143
		// Update the separators
144
		$a_separators = &$config['nat']['separator'];
145

    
146
		for ($idx=0; isset($a_separators['sep' . $idx]); $idx++ ) {
147
			$seprow = substr($a_separators['sep' . $idx]['row']['0'], 2);
148
			if ($seprow >= $_GET['id']) {
149
				$a_separators['sep' . $idx]['row']['0'] = 'fr' . ($seprow - 1);
150
			}
151
		}
152

    
153
		if (write_config()) {
154
			mark_subsystem_dirty('natconf');
155
			if ($want_dirty_filter) {
156
				mark_subsystem_dirty('filter');
157
			}
158
		}
159

    
160
		header("Location: firewall_nat.php");
161
		exit;
162
	}
163
}
164

    
165
if (isset($_POST['del_x'])) {
166
	/* delete selected rules */
167
	if (is_array($_POST['rule']) && count($_POST['rule'])) {
168
		$a_separators = &$config['nat']['separator'];
169

    
170
		foreach ($_POST['rule'] as $rulei) {
171
			$target = $rule['target'];
172

    
173
			// Check for filter rule associations
174
			if (isset($a_nat[$rulei]['associated-rule-id'])) {
175
				delete_id($a_nat[$rulei]['associated-rule-id'], $config['filter']['rule']);
176

    
177
				mark_subsystem_dirty('filter');
178
			}
179

    
180
			unset($a_nat[$rulei]);
181

    
182
			// Update the separators
183
			for ($idx=0; isset($a_separators['sep' . $idx]); $idx++ ) {
184
				$seprow = substr($a_separators['sep' . $idx]['row']['0'], 2);
185
				if ($seprow >= $rulei) {
186
					$a_separators['sep' . $idx]['row']['0'] = 'fr' . ($seprow - 1);
187
				}
188
			}
189
		}
190

    
191
		if (write_config()) {
192
			mark_subsystem_dirty('natconf');
193
		}
194

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

    
213
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("Port Forward"));
214
include("head.inc");
215

    
216
if ($savemsg) {
217
	print_info_box($savemsg, 'success');
218
}
219

    
220
if (is_subsystem_dirty('natconf')) {
221
	print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
222
					gettext('You must apply the changes in order for them to take effect.'));
223
}
224

    
225
$tab_array = array();
226
$tab_array[] = array(gettext("Port Forward"), true, "firewall_nat.php");
227
$tab_array[] = array(gettext("1:1"), false, "firewall_nat_1to1.php");
228
$tab_array[] = array(gettext("Outbound"), false, "firewall_nat_out.php");
229
$tab_array[] = array(gettext("NPt"), false, "firewall_nat_npt.php");
230
display_top_tabs($tab_array);
231

    
232
$columns_in_table = 13;
233
?>
234

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

    
260
$nnats = $i = 0;
261

    
262
// There can be a separator before any rules are listed
263
if ($config['nat']['separator']['sep0']['row'][0] == "fr-1") {
264
	$cellcolor = $config['nat']['separator']['sep0']['color'];
265
	print('<tr class="ui-sortable-handle separator">' .
266
		'<td class="' . $cellcolor . '" colspan="' . ($columns_in_table -1) . '">' . '<span class="' . $cellcolor . '">' . $config['nat']['separator']['sep0']['text'] . '</span></td>' .
267
		'<td  class="' . $cellcolor . '"><a href="#"><i class="fa fa-trash no-confirm sepdel" title="delete this separator"></i></a></td>' .
268
		'</tr>' . "\n");
269
}
270

    
271
foreach ($a_nat as $natent):
272

    
273
	$alias = rule_columns_with_alias(
274
		$natent['source']['address'],
275
		pprint_port($natent['source']['port']),
276
		$natent['destination']['address'],
277
		pprint_port($natent['destination']['port'])
278
	);
279

    
280
	/* if user does not have access to edit an interface skip on to the next record */
281
	if (!have_natpfruleint_access($natent['interface'])) {
282
		continue;
283
	}
284

    
285
	if (isset($natent['disabled'])) {
286
		$iconfn = "pass_d";
287
		$trclass = 'class="disabled"';
288
	} else {
289
		$iconfn = "pass";
290
		$trclass = '';
291
	}
292
?>
293

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

    
328
						<td>
329
							<?=$textss?><?=strtoupper($natent['protocol'])?><?=$textse?>
330
						</td>
331

    
332
						<td>
333

    
334

    
335
<?php
336
	if (isset($alias['src'])):
337
?>
338
							<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">
339
<?php
340
	endif;
341
?>
342
							<?=htmlspecialchars(pprint_address($natent['source']))?>
343
<?php
344
	if (isset($alias['src'])):
345
?>
346
							<i class='fa fa-pencil'></i></a>
347
<?php
348
	endif;
349
?>
350
						</td>
351
						<td>
352
<?php
353
	if (isset($alias['srcport'])):
354
?>
355
							<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">
356
<?php
357
	endif;
358
?>
359
							<?=htmlspecialchars(pprint_port($natent['source']['port']))?>
360
<?php
361
	if (isset($alias['srcport'])):
362
?>
363
							<i class='fa fa-pencil'></i></a>
364
<?php
365
	endif;
366
?>
367
						</td>
368

    
369
						<td>
370
<?php
371
	if (isset($alias['dst'])):
372
?>
373
							<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">
374
<?php
375
	endif;
376
?>
377
							<?=htmlspecialchars(pprint_address($natent['destination']))?>
378
<?php
379
	if (isset($alias['dst'])):
380
?>
381
							<i class='fa fa-pencil'></i></a>
382
<?php
383
	endif;
384
?>
385
						</td>
386
						<td>
387
<?php
388
	if (isset($alias['dstport'])):
389
?>
390
							<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">
391
<?php
392
	endif;
393
?>
394
							<?=htmlspecialchars(pprint_port($natent['destination']['port']))?>
395
<?php
396
	if (isset($alias['dstport'])):
397
?>
398
							<i class='fa fa-pencil'></i></a>
399
<?php
400
	endif;
401
?>
402
						</td>
403

    
404
						<td >
405
							<?=htmlspecialchars($natent['target'])?>
406
						</td>
407
						<td>
408
<?php
409
	$localport = $natent['local-port'];
410

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

    
413
	if ($dstendport) {
414
		$localendport = $natent['local-port'] + $dstendport - $dstbeginport;
415
		$localport	 .= '-' . $localendport;
416
	}
417
?>
418
							<?=htmlspecialchars(pprint_port($localport))?>
419
						</td>
420

    
421
						<td>
422
							<?=htmlspecialchars($natent['descr'])?>
423
						</td>
424
						<td>
425
							<a class="fa fa-pencil" title="<?=gettext("Edit rule"); ?>" href="firewall_nat_edit.php?id=<?=$i?>"></a>
426
							<a class="fa fa-clone"	  title="<?=gettext("Add a new NAT based on this one")?>" href="firewall_nat_edit.php?dup=<?=$i?>"></a>
427
							<a class="fa fa-trash"	title="<?=gettext("Delete rule")?>" href="firewall_nat.php?act=del&amp;id=<?=$i?>"></a>
428
						</td>
429
					</tr>
430
<?php
431

    
432
		if (isset($config['nat']['separator']['sep0'])) {
433
			foreach ($config['nat']['separator'] as $rulesep) {
434
				if ($rulesep['row']['0'] == "fr" . $nnats) {
435
					$cellcolor = $rulesep['color'];
436
					print('<tr class="ui-sortable-handle separator">' .
437
						'<td class="' . $cellcolor . '" colspan="' . ($columns_in_table -1) . '">' . '<span class="' . $cellcolor . '">' . $rulesep['text'] . '</span></td>' .
438
						'<td  class="' . $cellcolor . '"><a href="#"><i class="fa fa-trash no-confirm sepdel" title="delete this separator"></i></a></td>' .
439
						'</tr>' . "\n");
440
				}
441
			}
442
		}
443
	$i++;
444
	$nnats++;
445
endforeach;
446
?>
447
				</tbody>
448
			</table>
449
		</div>
450
	</div>
451

    
452
	<nav class="action-buttons">
453
		<a href="firewall_nat_edit.php?after=-1" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the top of the list')?>">
454
			<i class="fa fa-level-up icon-embed-btn"></i>
455
			<?=gettext('Add')?>
456
		</a>
457
		<a href="firewall_nat_edit.php" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the end of the list')?>">
458
			<i class="fa fa-level-down icon-embed-btn"></i>
459
			<?=gettext('Add')?>
460
		</a>
461
		<button name="del_x" type="submit" class="btn btn-danger btn-sm" title="<?=gettext('Delete selected rules')?>">
462
			<i class="fa fa-trash icon-embed-btn"></i>
463
			<?=gettext("Delete"); ?>
464
		</button>
465
		<button type="submit" id="order-store" name="order-store" class="btn btn-primary btn-sm" disabled title="<?=gettext('Save rule order')?>">
466
			<i class="fa fa-save icon-embed-btn"></i>
467
			<?=gettext("Save")?>
468
		</button>
469
		<button type="submit" id="addsep" name="addsep" class="btn btn-sm btn-warning" title="<?=gettext('Add separator')?>">
470
			<i class="fa fa-plus icon-embed-btn"></i>
471
			<?=gettext("Separator")?>
472
		</button>
473
	</nav>
474
</form>
475

    
476
<script type="text/javascript">
477
//<![CDATA[
478
//Need to create some variables here so that jquery/pfSenseHelpers.js can read them
479
iface = "<?=strtolower($if)?>";
480
cncltxt = '<?=gettext("Cancel")?>';
481
svtxt = '<?=gettext("Save")?>';
482
svbtnplaceholder = '<?=gettext("Enter a description, Save, then drag to final location.")?>';
483
configsection = "nat";
484
dirty = false;
485

    
486
events.push(function() {
487

    
488
	// Make rules sortable
489
	$('table tbody.user-entries').sortable({
490
		cursor: 'grabbing',
491
		update: function(event, ui) {
492
			$('#order-store').removeAttr('disabled');
493
			dirty = true;
494
			reindex_rules(ui.item.parent('tbody'));
495
			dirty = true;
496
		}
497
	});
498

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

    
503
		// Save the separator bar configuration
504
		save_separators();
505

    
506
		// Suppress the "Do you really want to leave the page" message
507
		saving = true;
508

    
509
	});
510

    
511
	// Globals
512
	saving = false;
513
	dirty = false;
514

    
515
	// provide a warning message if the user tries to change page before saving
516
	$(window).bind('beforeunload', function(){
517
		if (!saving && dirty) {
518
			return ("<?=gettext('You have moved one or more Port Forward rules but have not yet saved')?>");
519
		} else {
520
			return undefined;
521
		}
522
	});
523
});
524
//]]>
525
</script>
526
<?php
527

    
528
if (count($a_nat) > 0) {
529
?>
530
<!-- Legend -->
531
<div>
532
	<dl class="dl-horizontal responsive">
533
		<dt><?=gettext('Legend')?></dt>					<dd></dd>
534
		<dt><i class="fa fa-play"></i></dt>			<dd><?=gettext('Pass')?></dd>
535
		<dt><i class="fa fa-random"></i></dt>		<dd><?=gettext('Linked rule')?></dd>
536
	</dl>
537
</div>
538

    
539
<?php
540
}
541

    
542
include("foot.inc");
(41-41/229)