Project

General

Profile

Download (15.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * system_gateways.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-2019 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2010 Seth Mos <seth.mos@dds.nl>
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
##|+PRIV
26
##|*IDENT=page-system-gateways
27
##|*NAME=System: Gateways
28
##|*DESCR=Allow access to the 'System: Gateways' page.
29
##|*MATCH=system_gateways.php*
30
##|-PRIV
31

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

    
38
$simplefields = array('defaultgw4', 'defaultgw6');
39

    
40
init_config_arr(array('gateways', 'gateway_item'));
41
$a_gateway_item = &$config['gateways']['gateway_item'];
42

    
43
$pconfig = $_REQUEST;
44

    
45
if ($_POST['order-store']) {
46
	// Include the rules of this (the selected) interface.
47
	// If a rule is not in POST[rule], it has been deleted by the user
48
	$a_gateway_item_new = array();
49
	//print "<pre>";
50
	foreach ($_POST['row'] as $id) {
51
		//print " $id";
52
		$a_gateway_item_new[] = $a_gateway_item[$id];
53
	}
54
	//print_r($a_gateway_item);
55
	//print_r($a_gateway_item_new);
56
	//print "</pre>";
57
	$a_gateway_item = $a_gateway_item_new;
58
	//mark_subsystem_dirty('staticroutes');
59
	write_config("System - Gateways: save default gateway");
60
} else if ($_POST['save']) {
61
	unset($input_errors);
62
	$pconfig = $_POST;
63
	foreach($simplefields as $field) {
64
		$config['gateways'][$field] = $pconfig[$field];
65
	}
66
	mark_subsystem_dirty('staticroutes');
67
	write_config("System - Gateways: save default gateway");
68
}
69

    
70
if ($_POST['apply']) {
71

    
72
	$retval = 0;
73

    
74
	$retval |= system_routing_configure();
75
	$retval |= system_resolvconf_generate();
76
	$retval |= filter_configure();
77
	/* reconfigure our gateway monitor */
78
	setup_gateways_monitor();
79
	/* Dynamic DNS on gw groups may have changed */
80
	send_event("service reload dyndnsall");
81

    
82
	if ($retval == 0) {
83
		clear_subsystem_dirty('staticroutes');
84
	}
85
}
86

    
87
$a_gateways = return_gateways_array(true, false, true, true);
88

    
89
function can_delete_disable_gateway_item($id, $disable = false) {
90
	global $config, $input_errors, $a_gateways;
91

    
92
	if (!isset($a_gateways[$id])) {
93
		return false;
94
	}
95

    
96
	if (is_array($config['gateways']['gateway_group'])) {
97
		foreach ($config['gateways']['gateway_group'] as $group) {
98
			foreach ($group['item'] as $item) {
99
				$items = explode("|", $item);
100
				if ($items[0] == $a_gateways[$id]['name']) {
101
					if (!$disable) {
102
						$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be deleted because it is in use on Gateway Group "%2$s"'), $a_gateways[$id]['name'], $group['name']);
103
					} else {
104
						$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be disabled because it is in use on Gateway Group "%2$s"'), $a_gateways[$id]['name'], $group['name']);
105
					}
106
				}
107
			}
108
		}
109
	}
110

    
111
	if (is_array($config['staticroutes']['route'])) {
112
		foreach ($config['staticroutes']['route'] as $route) {
113
			if ($route['gateway'] == $a_gateways[$id]['name']) {
114
				if (!$disable) {
115
					// The user wants to delete this gateway, but there is a static route (enabled or disabled) that refers to the gateway.
116
					$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be deleted because it is in use on Static Route "%2$s"'), $a_gateways[$id]['name'], $route['network']);
117
				} else if (!isset($route['disabled'])) {
118
					// The user wants to disable this gateway.
119
					// But there is a static route that uses this gateway and is enabled (not disabled).
120
					$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be disabled because it is in use on Static Route "%2$s"'), $a_gateways[$id]['name'], $route['network']);
121
				}
122
			}
123
		}
124
	}
125

    
126
	if (isset($input_errors)) {
127
		return false;
128
	}
129

    
130
	return true;
131
}
132

    
133
function delete_gateway_item($id) {
134
	global $config, $a_gateways;
135

    
136
	if (!isset($a_gateways[$id])) {
137
		return;
138
	}
139

    
140
	/* If the removed gateway was the default route, remove the default route */
141
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
142
	    !isset($a_gateways[$id]['disabled']) &&
143
	    isset($a_gateways[$id]['isdefaultgw'])) {
144
		$inet = (!is_ipaddrv4($a_gateways[$id]['gateway']) ? '-inet6' : '-inet');
145
		file_put_contents("/dev/console", "\n[".getmypid()."] DEL_GW, route= delete {$inet} default");
146
		mwexec("/sbin/route delete {$inet} default");
147
	}
148

    
149
	/* NOTE: Cleanup static routes for the interface route if any */
150
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
151
	    $gateway['gateway'] != $a_gateways[$id]['gateway'] &&
152
	    isset($a_gateways[$id]["nonlocalgateway"])) {
153
		$realif = get_real_interface($a_gateways[$id]['interface']);
154
		$inet = (!is_ipaddrv4($a_gateways[$id]['gateway']) ? "-inet6" : "-inet");
155
		file_put_contents("/dev/console", "\n[".getmypid()."] DEL_GW, route= $inet " . escapeshellarg($a_gateways[$id]['gateway']) . " -iface " . escapeshellarg($realif));
156
		$cmd = "/sbin/route delete $inet " . escapeshellarg($a_gateways[$id]['gateway']) . " -iface " . escapeshellarg($realif);
157
		mwexec($cmd);
158
	}
159
	/* NOTE: Cleanup static routes for the monitor ip if any */
160
	if (!empty($a_gateways[$id]['monitor']) &&
161
	    $a_gateways[$id]['monitor'] != "dynamic" &&
162
	    is_ipaddr($a_gateways[$id]['monitor']) &&
163
	    $a_gateways[$id]['gateway'] != $a_gateways[$id]['monitor']) {
164
		if (is_ipaddrv4($a_gateways[$id]['monitor'])) {
165
			mwexec("/sbin/route delete " . escapeshellarg($a_gateways[$id]['monitor']));
166
		} else {
167
			mwexec("/sbin/route delete -inet6 " . escapeshellarg($a_gateways[$id]['monitor']));
168
		}
169
	}
170

    
171
	if ($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway'] == $a_gateways[$id]['name']) {
172
		unset($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway']);
173
	}
174
	unset($config['gateways']['gateway_item'][$a_gateways[$id]['attribute']]);
175
}
176

    
177
unset($input_errors);
178
if ($_REQUEST['act'] == "del") {
179
	if (can_delete_disable_gateway_item($_REQUEST['id'])) {
180
		$realid = $a_gateways[$_REQUEST['id']]['attribute'];
181
		delete_gateway_item($_REQUEST['id']);
182
		write_config("Gateways: removed gateway {$realid}");
183
		mark_subsystem_dirty('staticroutes');
184
		header("Location: system_gateways.php");
185
		exit;
186
	}
187
}
188

    
189
if (isset($_REQUEST['del_x'])) {
190
	/* delete selected items */
191
	if (is_array($_REQUEST['rule']) && count($_REQUEST['rule'])) {
192
		foreach ($_REQUEST['rule'] as $rulei) {
193
			if (!can_delete_disable_gateway_item($rulei)) {
194
				break;
195
			}
196
		}
197

    
198
		if (!isset($input_errors)) {
199
			$items_deleted = "";
200
			foreach ($_REQUEST['rule'] as $rulei) {
201
				delete_gateway_item($rulei);
202
				$items_deleted .= "{$rulei} ";
203
			}
204
			if (!empty($items_deleted)) {
205
				write_config(sprintf(gettext("Gateways: removed gateways %s", $items_deleted)));
206
				mark_subsystem_dirty('staticroutes');
207
			}
208
			header("Location: system_gateways.php");
209
			exit;
210
		}
211
	}
212

    
213
} else if ($_REQUEST['act'] == "toggle" && $a_gateways[$_REQUEST['id']]) {
214
	$realid = $a_gateways[$_REQUEST['id']]['attribute'];
215
	$disable_gw = !isset($a_gateway_item[$realid]['disabled']);
216
	if ($disable_gw) {
217
		// The user wants to disable the gateway, so check if that is OK.
218
		$ok_to_toggle = can_delete_disable_gateway_item($_REQUEST['id'], $disable_gw);
219
	} else {
220
		// The user wants to enable the gateway. That is always OK.
221
		$ok_to_toggle = true;
222
	}
223
	if ($ok_to_toggle) {
224
		gateway_set_enabled($a_gateway_item[$realid]['name'], !$disable_gw);
225

    
226
		if (write_config("Gateways: enable/disable")) {
227
			mark_subsystem_dirty('staticroutes');
228
		}
229

    
230
		header("Location: system_gateways.php");
231
		exit;
232
	}
233
}
234

    
235
foreach($simplefields as $field) {
236
	$pconfig[$field] = $config['gateways'][$field];
237
}
238

    
239
function gateway_displaygwtiername($gwname) {
240
	global $config;
241
	$gw = lookup_gateway_or_group_by_name($gwname);
242
	if ($config['gateways']['defaultgw4'] == $gwname || $config['gateways']['defaultgw6'] == $gwname) {
243
		$result = "Default";
244
	} else {
245
		if ($gw['ipprotocol'] == 'inet') {
246
			$defgw = lookup_gateway_or_group_by_name($config['gateways']['defaultgw4']);
247
		} else {
248
			$defgw = lookup_gateway_or_group_by_name($config['gateways']['defaultgw6']);
249
		}
250
		if ($defgw['type'] == "gatewaygroup") {
251
			$detail = gateway_is_gwgroup_member($gwname, true);
252
			foreach($detail as $gwitem) {
253
				if ($gwitem['name'] == $defgw['name']) {
254
					if (isset($gwitem['tier'])) {
255
						$result = "Tier " . $gwitem['tier'];
256
						break;
257
					}
258
				}
259
			}
260
		}
261
	}
262
	if (!empty($result)) {
263
		if ($gw['ipprotocol'] == "inet") {
264
			$result .= " (IPv4)";
265
		} elseif ($gw['ipprotocol'] == "inet6") {
266
			$result .= " (IPv6)";
267
		}
268
	}
269
	return $result;
270
}
271

    
272
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Gateways"));
273
$pglinks = array("", "@self", "@self");
274
$shortcut_section = "gateways";
275

    
276
include("head.inc");
277

    
278
if ($input_errors) {
279
	print_input_errors($input_errors);
280
}
281

    
282
if ($_POST['apply']) {
283
	print_apply_result_box($retval);
284
}
285

    
286
if (is_subsystem_dirty('staticroutes')) {
287
	print_apply_box(gettext("The gateway configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
288
}
289

    
290
$tab_array = array();
291
$tab_array[0] = array(gettext("Gateways"), true, "system_gateways.php");
292
$tab_array[1] = array(gettext("Static Routes"), false, "system_routes.php");
293
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
294
display_top_tabs($tab_array);
295

    
296
?>
297
<form method="post">
298
<div class="panel panel-default">
299
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Gateways')?></h2></div>
300
	<div class="panel-body">
301
		<div class="table-responsive">
302
			<table id="gateways" class="table table-striped table-hover table-condensed table-rowdblclickedit">
303
				<thead>
304
					<tr>
305
						<th></th>
306
						<th></th>
307
						<th><?=gettext("Name")?></th>
308
						<th><?=gettext("Default")?></th>
309
						<th><?=gettext("Interface")?></th>
310
						<th><?=gettext("Gateway")?></th>
311
						<th><?=gettext("Monitor IP")?></th>
312
						<th><?=gettext("Description")?></th>
313
						<th><?=gettext("Actions")?></th>
314
					</tr>
315
				</thead>
316
				<tbody>
317
<?php
318
foreach ($a_gateways as $i => $gateway):
319
	if (isset($gateway['inactive'])) {
320
		$icon = 'fa-times-circle-o';
321
	} elseif (isset($gateway['disabled'])) {
322
		$icon = 'fa-ban';
323
	} else {
324
		$icon = 'fa-check-circle-o';
325
	}
326

    
327
	if (isset($gateway['inactive'])) {
328
		$title = gettext("This gateway is inactive because interface is missing");
329
	} else {
330
		$title = '';
331
	}
332
	$id = $gateway['attribute'];
333
?>
334
				<tr<?=($icon != 'fa-check-circle-o')? ' class="disabled"' : ''?> onClick="fr_toggle(<?=$id;?>)" id="fr<?=$id;?>">
335
					<td style="white-space: nowrap;">
336
						<?php 
337
						if (is_numeric($id)) :?>
338
							<input type='checkbox' id='frc<?=$id?>' onClick='fr_toggle(<?=$id?>)' name='row[]' value='<?=$id?>'/>
339
							<a class='fa fa-anchor' id='Xmove_<?=$id?>' title='"<?=gettext("Move checked entries to here")?>"'></a>
340
						<?php endif; ?>
341
					</td>
342
					<td title="<?=$title?>"><i class="fa <?=$icon?>"></i></td>
343
					<td>
344
						<?=htmlspecialchars($gateway['name'])?>
345
<?php
346
						if (isset($gateway['isdefaultgw'])) {
347
							echo ' <i class="fa fa-globe"></i>';
348
						}
349
?>
350
						</td>
351
						<td>
352
							<?=gateway_displaygwtiername($gateway['name'])?>
353
						</td>
354
						<td>
355
							<?=htmlspecialchars(convert_friendly_interface_to_friendly_descr($gateway['friendlyiface']))?>
356
						</td>
357
						<td>
358
							<?=htmlspecialchars($gateway['gateway'])?>
359
						</td>
360
						<td>
361
							<?=htmlspecialchars($gateway['monitor'])?>
362
						</td>
363
						<td>
364
							<?=htmlspecialchars($gateway['descr'])?>
365
						</td>
366
						<td style="white-space: nowrap;">
367
							<a href="system_gateways_edit.php?id=<?=$i?>" class="fa fa-pencil" title="<?=gettext('Edit gateway');?>"></a>
368
							<a href="system_gateways_edit.php?dup=<?=$i?>" class="fa fa-clone" title="<?=gettext('Copy gateway')?>"></a>
369

    
370
<?php if (is_numeric($gateway['attribute'])): ?>
371
	<?php if (isset($gateway['disabled'])) {
372
	?>
373
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-check-square-o" title="<?=gettext('Enable gateway')?>" usepost></a>
374
	<?php } else {
375
	?>
376
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-ban" title="<?=gettext('Disable gateway')?>" usepost></a>
377
	<?php }
378
	?>
379
							<a href="system_gateways.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext('Delete gateway')?>" usepost></a>
380

    
381
<?php endif; ?>
382
						</td>
383
					</tr>
384
<?php endforeach; ?>
385
				</tbody>
386
			</table>
387
		</div>
388
	</div>
389
</div>
390

    
391
<nav class="action-buttons">
392
	<button type="submit" id="order-store" name="order-store" class="btn btn-sm btn-primary" value="store changes" disabled title="<?=gettext('Save rule order')?>">
393
		<i class="fa fa-save icon-embed-btn"></i>
394
		<?=gettext("Save")?>
395
	</button>
396
	<a href="system_gateways_edit.php" role="button" class="btn btn-success">
397
		<i class="fa fa-plus icon-embed-btn"></i>
398
		<?=gettext("Add");?>
399
	</a>
400
</nav>
401
</form>
402
<?php
403

    
404
$form = new Form;
405
$section = new Form_Section('Default gateway');
406

    
407
$items4 = array();
408
$items6 = array();
409
$items4[''] = "Automatic";
410
$items6[''] = "Automatic";
411
foreach($a_gateways as $gw) {
412
	$gwn = $gw['name'];
413
	if ($gw['ipprotocol'] == "inet6") {
414
		$items6[$gwn] = $gwn;
415
	} else {
416
		$items4[$gwn] = $gwn;
417
	}
418
}
419
$groups = return_gateway_groups_array();
420
foreach ($groups as $key => $group) {
421
	$gwn = $group['descr'];
422
	if ($group['ipprotocol'] == "inet6") {
423
		$items6[$key] = "$key ($gwn)";
424
	} else {
425
		$items4[$key] = "$key ($gwn)";
426
	}
427
}
428
$items4['-'] = "None";
429
$items6['-'] = "None";
430

    
431
$section->addInput(new Form_Select(
432
	'defaultgw4',
433
	'Default gateway IPv4',
434
	$pconfig['defaultgw4'],
435
	$items4
436
))->setHelp('Select the gateway or gatewaygroup to use as the default gateway.');
437

    
438
$section->addInput(new Form_Select(
439
	'defaultgw6',
440
	'Default gateway IPv6',
441
	$pconfig['defaultgw6'],
442
	$items6
443
))->setHelp('Select the gateway or gatewaygroup to use as the default gateway.');
444

    
445
$form->add($section);
446
print $form;
447

    
448
?>
449
<div class="infoblock">
450
<?php
451
print_info_box(
452
	sprintf(gettext('%1$s%2$s%3$s is the current default route as present in the current routing table of the operating system'), '<strong>', '<i class="fa fa-globe"></i>', '</strong>')
453
	);
454
?>
455
</div>
456
<script type="text/javascript">
457
//<![CDATA[
458
events.push(function() {
459
	$('#order-store').click(function () {
460
		// Check all of the rule checkboxes so that their values are posted
461
	   $('[id^=frc]').prop('checked', true);
462
	});
463

    
464
	$('[id^=Xmove_]').click(function (event) {
465
		// anchor click to move gateways around..
466
		moveRowUpAboveAnchor(event.target.id.slice(6),"gateways");
467
		return false;
468
	});
469
	$('[id^=Xmove_]').css('cursor', 'pointer');
470
});
471
	function moveRowUpAboveAnchor(rowId, tableId) {
472
		var table = $('#'+tableId);
473
		var viewcheckboxes = $('[id^=frc]input:checked', table);
474
		var rowview = $("#fr" + rowId, table);
475
		var moveabove = rowview;
476
		//var parent = moveabove[0].parentNode;
477
		
478
		viewcheckboxes.each(function( index ) {
479
			var moveid = this.value;
480
			console.log( index + ": " + this.id );
481

    
482
			var prevrowview = $("#fr" + moveid, table);
483
			prevrowview.insertBefore(moveabove);
484
			$('#order-store').removeAttr('disabled');
485
		});
486
	}
487
//]]>
488
</script>
489

    
490
<?php include("foot.inc");
(198-198/227)