Project

General

Profile

Download (14.9 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
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
$a_gateways = return_gateways_array(true, false, true, true);
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

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

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

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

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

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

    
129
	return true;
130
}
131

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
275
include("head.inc");
276

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
447
?>
448
<script type="text/javascript">
449
//<![CDATA[
450
events.push(function() {
451
	$('#order-store').click(function () {
452
		// Check all of the rule checkboxes so that their values are posted
453
	   $('[id^=frc]').prop('checked', true);
454
	});
455

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

    
474
			var prevrowview = $("#fr" + moveid, table);
475
			prevrowview.insertBefore(moveabove);
476
			$('#order-store').removeAttr('disabled');
477
		});
478
	}
479
//]]>
480
</script>
481

    
482
<?php include("foot.inc");
(205-205/234)