Project

General

Profile

Download (15 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-2018 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
if (!is_array($config['gateways'])) {
39
	$config['gateways'] = array();
40
}
41

    
42
if (!is_array($config['gateways']['gateway_item'])) {
43
	$config['gateways']['gateway_item'] = array();
44
}
45

    
46
$a_gateway_item = &$config['gateways']['gateway_item'];
47

    
48
$pconfig = $_REQUEST;
49

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

    
75
$a_gateways = return_gateways_array(true, false, true, true);
76

    
77
if ($_POST['apply']) {
78

    
79
	$retval = 0;
80

    
81
	$retval |= system_routing_configure();
82
	$retval |= system_resolvconf_generate();
83
	$retval |= filter_configure();
84
	/* reconfigure our gateway monitor */
85
	setup_gateways_monitor();
86
	/* Dynamic DNS on gw groups may have changed */
87
	send_event("service reload dyndnsall");
88

    
89
	if ($retval == 0) {
90
		clear_subsystem_dirty('staticroutes');
91
	}
92
}
93

    
94

    
95
function can_delete_disable_gateway_item($id, $disable = false) {
96
	global $config, $input_errors, $a_gateways;
97

    
98
	if (!isset($a_gateways[$id])) {
99
		return false;
100
	}
101

    
102
	if (is_array($config['gateways']['gateway_group'])) {
103
		foreach ($config['gateways']['gateway_group'] as $group) {
104
			foreach ($group['item'] as $item) {
105
				$items = explode("|", $item);
106
				if ($items[0] == $a_gateways[$id]['name']) {
107
					if (!$disable) {
108
						$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']);
109
					} else {
110
						$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']);
111
					}
112
				}
113
			}
114
		}
115
	}
116

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

    
132
	if (isset($input_errors)) {
133
		return false;
134
	}
135

    
136
	return true;
137
}
138

    
139
function delete_gateway_item($id) {
140
	global $config, $a_gateways;
141

    
142
	if (!isset($a_gateways[$id])) {
143
		return;
144
	}
145

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

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

    
177
	if ($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway'] == $a_gateways[$id]['name']) {
178
		unset($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway']);
179
	}
180
	unset($config['gateways']['gateway_item'][$a_gateways[$id]['attribute']]);
181
}
182

    
183
unset($input_errors);
184
if ($_REQUEST['act'] == "del") {
185
	if (can_delete_disable_gateway_item($_REQUEST['id'])) {
186
		$realid = $a_gateways[$_REQUEST['id']]['attribute'];
187
		delete_gateway_item($_REQUEST['id']);
188
		write_config("Gateways: removed gateway {$realid}");
189
		mark_subsystem_dirty('staticroutes');
190
		header("Location: system_gateways.php");
191
		exit;
192
	}
193
}
194

    
195
if (isset($_REQUEST['del_x'])) {
196
	/* delete selected items */
197
	if (is_array($_REQUEST['rule']) && count($_REQUEST['rule'])) {
198
		foreach ($_REQUEST['rule'] as $rulei) {
199
			if (!can_delete_disable_gateway_item($rulei)) {
200
				break;
201
			}
202
		}
203

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

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

    
232
		if (write_config("Gateways: enable/disable")) {
233
			mark_subsystem_dirty('staticroutes');
234
		}
235

    
236
		header("Location: system_gateways.php");
237
		exit;
238
	}
239
}
240

    
241
foreach($simplefields as $field) {
242
	$pconfig[$field] = $config['gateways'][$field];
243
}
244

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

    
278
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Gateways"));
279
$pglinks = array("", "@self", "@self");
280
$shortcut_section = "gateways";
281

    
282
include("head.inc");
283

    
284
if ($input_errors) {
285
	print_input_errors($input_errors);
286
}
287

    
288
if ($_POST['apply']) {
289
	print_apply_result_box($retval);
290
}
291

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

    
296
$tab_array = array();
297
$tab_array[0] = array(gettext("Gateways"), true, "system_gateways.php");
298
$tab_array[1] = array(gettext("Static Routes"), false, "system_routes.php");
299
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
300
display_top_tabs($tab_array);
301

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

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

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

    
387
<?php endif; ?>
388
						</td>
389
					</tr>
390
<?php endforeach; ?>
391
				</tbody>
392
			</table>
393
		</div>
394
	</div>
395
</div>
396

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

    
410
$form = new Form;
411
$section = new Form_Section('Default gateway');
412

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

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

    
444
$section->addInput(new Form_Select(
445
	'defaultgw6',
446
	'Default gateway IPv6',
447
	$pconfig['defaultgw6'],
448
	$items6
449
))->setHelp('Select the gateway or gatewaygroup to use as the default gateway.');
450

    
451
$form->add($section);
452
print $form;
453

    
454
?>
455
<script type="text/javascript">
456
//<![CDATA[
457
events.push(function() {
458
	$('#order-store').click(function () {
459
		// Check all of the rule checkboxes so that their values are posted
460
	   $('[id^=frc]').prop('checked', true);
461
	});
462

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

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

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