Project

General

Profile

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

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

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

    
36
$simplefields = array('defaultgw4', 'defaultgw6');
37

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

    
41
$pconfig = $_REQUEST;
42

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

    
68
if ($_POST['apply']) {
69

    
70
	$retval = 0;
71

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

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

    
85
$a_gateways = return_gateways_array(true, false, true, true);
86

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

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

    
94
	if (is_array($config['gateways']['gateway_group'])) {
95
		foreach ($config['gateways']['gateway_group'] as $group) {
96
			foreach ($group['item'] as $item) {
97
				$items = explode("|", $item);
98
				if ($items[0] == $a_gateways[$id]['name']) {
99
					if (!$disable) {
100
						$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']);
101
					} else {
102
						$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']);
103
					}
104
				}
105
			}
106
		}
107
	}
108

    
109
	if (is_array($config['staticroutes']['route'])) {
110
		foreach ($config['staticroutes']['route'] as $route) {
111
			if ($route['gateway'] == $a_gateways[$id]['name']) {
112
				if (!$disable) {
113
					// The user wants to delete this gateway, but there is a static route (enabled or disabled) that refers to the gateway.
114
					$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']);
115
				} else if (!isset($route['disabled'])) {
116
					// The user wants to disable this gateway.
117
					// But there is a static route that uses this gateway and is enabled (not disabled).
118
					$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']);
119
				}
120
			}
121
		}
122
	}
123

    
124
	if (isset($input_errors)) {
125
		return false;
126
	}
127

    
128
	return true;
129
}
130

    
131
function delete_gateway_item($id) {
132
	global $config, $a_gateways;
133

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

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

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

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

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

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

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

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

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

    
228
		header("Location: system_gateways.php");
229
		exit;
230
	}
231
}
232

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

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

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

    
274
include("head.inc");
275

    
276
if ($input_errors) {
277
	print_input_errors($input_errors);
278
}
279

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

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

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

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

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

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

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

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

    
402
$form = new Form;
403
$section = new Form_Section('Default gateway');
404

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

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

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

    
443
$form->add($section);
444
print $form;
445

    
446
?>
447
<div class="infoblock">
448
<?php
449
print_info_box(
450
	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>')
451
	);
452
?>
453
</div>
454
<script type="text/javascript">
455
//<![CDATA[
456
events.push(function() {
457
	$('#order-store').click(function () {
458
		// Check all of the rule checkboxes so that their values are posted
459
	   $('[id^=frc]').prop('checked', true);
460
	});
461

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

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

    
488
<?php include("foot.inc");
(206-206/235)