Project

General

Profile

Download (13.6 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-2020 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'])
145
		    ? 'inet6' : 'inet');
146
		route_del('default', $inet);
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
		route_del($a_gateways[$id]['gateway']);
154
	}
155
	/* NOTE: Cleanup static routes for the monitor ip if any */
156
	if (!empty($a_gateways[$id]['monitor']) &&
157
	    $a_gateways[$id]['monitor'] != "dynamic" &&
158
	    is_ipaddr($a_gateways[$id]['monitor']) &&
159
	    $a_gateways[$id]['gateway'] != $a_gateways[$id]['monitor']) {
160
		route_del($a_gateways[$id]['monitor']);
161
	}
162

    
163
	if ($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway'] == $a_gateways[$id]['name']) {
164
		unset($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway']);
165
	}
166
	unset($config['gateways']['gateway_item'][$a_gateways[$id]['attribute']]);
167
}
168

    
169
unset($input_errors);
170
if ($_REQUEST['act'] == "del") {
171
	if (can_delete_disable_gateway_item($_REQUEST['id'])) {
172
		$realid = $a_gateways[$_REQUEST['id']]['attribute'];
173
		delete_gateway_item($_REQUEST['id']);
174
		write_config("Gateways: removed gateway {$realid}");
175
		mark_subsystem_dirty('staticroutes');
176
		header("Location: system_gateways.php");
177
		exit;
178
	}
179
}
180

    
181
if (isset($_REQUEST['del_x'])) {
182
	/* delete selected items */
183
	if (is_array($_REQUEST['rule']) && count($_REQUEST['rule'])) {
184
		foreach ($_REQUEST['rule'] as $rulei) {
185
			if (!can_delete_disable_gateway_item($rulei)) {
186
				break;
187
			}
188
		}
189

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

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

    
218
		if (write_config("Gateways: enable/disable")) {
219
			mark_subsystem_dirty('staticroutes');
220
		}
221

    
222
		header("Location: system_gateways.php");
223
		exit;
224
	}
225
}
226

    
227
foreach($simplefields as $field) {
228
	$pconfig[$field] = $config['gateways'][$field];
229
}
230

    
231
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Gateways"));
232
$pglinks = array("", "@self", "@self");
233
$shortcut_section = "gateways";
234

    
235
include("head.inc");
236

    
237
if ($input_errors) {
238
	print_input_errors($input_errors);
239
}
240

    
241
if ($_POST['apply']) {
242
	print_apply_result_box($retval);
243
}
244

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

    
249
$tab_array = array();
250
$tab_array[0] = array(gettext("Gateways"), true, "system_gateways.php");
251
$tab_array[1] = array(gettext("Static Routes"), false, "system_routes.php");
252
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
253
display_top_tabs($tab_array);
254

    
255
?>
256
<form method="post">
257
<div class="panel panel-default">
258
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Gateways')?></h2></div>
259
	<div class="panel-body">
260
		<div class="table-responsive">
261
			<table id="gateways" class="table table-striped table-hover table-condensed table-rowdblclickedit">
262
				<thead>
263
					<tr>
264
						<th></th>
265
						<th></th>
266
						<th><?=gettext("Name")?></th>
267
						<th><?=gettext("Default")?></th>
268
						<th><?=gettext("Interface")?></th>
269
						<th><?=gettext("Gateway")?></th>
270
						<th><?=gettext("Monitor IP")?></th>
271
						<th><?=gettext("Description")?></th>
272
						<th><?=gettext("Actions")?></th>
273
					</tr>
274
				</thead>
275
				<tbody>
276
<?php
277
foreach ($a_gateways as $i => $gateway):
278
	if (isset($gateway['inactive'])) {
279
		$title = gettext("Gateway inactive, interface is missing");
280
		$icon = 'fa-times-circle-o';
281
	} elseif (isset($gateway['disabled'])) {
282
		$icon = 'fa-ban';
283
		$title = gettext("Gateway disabled");
284
	} else {
285
		$icon = 'fa-check-circle-o';
286
		$title = gettext("Gateway enabled");
287
	}
288

    
289
	$gtitle = "";
290
	if (isset($gateway['isdefaultgw'])) {
291
		$gtitle = gettext("Default gateway");
292
	}
293

    
294
	$id = $gateway['attribute'];
295
?>
296
					<tr<?=($icon != 'fa-check-circle-o')? ' class="disabled"' : ''?> onClick="fr_toggle(<?=$id;?>)" id="fr<?=$id;?>">
297
						<td style="white-space: nowrap;">
298
							<?php 
299
							if (is_numeric($id)) :?>
300
								<input type='checkbox' id='frc<?=$id?>' onClick='fr_toggle(<?=$id?>)' name='row[]' value='<?=$id?>'/>
301
								<a class='fa fa-anchor' id='Xmove_<?=$id?>' title='"<?=gettext("Move checked entries to here")?>"'></a>
302
							<?php endif; ?>
303
						</td>
304
						<td title="<?=$title?>"><i class="fa <?=$icon?>"></i></td>
305
						<td title="<?=$gtitle?>">
306
						<?=htmlspecialchars($gateway['name'])?>
307
<?php
308
							if (isset($gateway['isdefaultgw'])) {
309
								echo ' <i class="fa fa-globe"></i>';
310
							}
311
?>
312
						</td>
313
						<td>
314
							<?=htmlspecialchars($gateway['tiername'])?>
315
						</td>
316
						<td>
317
							<?=htmlspecialchars($gateway['friendlyifdescr'])?>
318
						</td>
319
						<td>
320
							<?=htmlspecialchars($gateway['gateway'])?>
321
						</td>
322
						<td>
323
							<?=htmlspecialchars($gateway['monitor'])?>
324
						</td>
325
						<td>
326
							<?=htmlspecialchars($gateway['descr'])?>
327
						</td>
328
						<td style="white-space: nowrap;">
329
							<a href="system_gateways_edit.php?id=<?=$i?>" class="fa fa-pencil" title="<?=gettext('Edit gateway');?>"></a>
330
							<a href="system_gateways_edit.php?dup=<?=$i?>" class="fa fa-clone" title="<?=gettext('Copy gateway')?>"></a>
331

    
332
<?php if (is_numeric($gateway['attribute'])): ?>
333
	<?php if (isset($gateway['disabled'])) {
334
	?>
335
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-check-square-o" title="<?=gettext('Enable gateway')?>" usepost></a>
336
	<?php } else {
337
	?>
338
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-ban" title="<?=gettext('Disable gateway')?>" usepost></a>
339
	<?php }
340
	?>
341
							<a href="system_gateways.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext('Delete gateway')?>" usepost></a>
342

    
343
<?php endif; ?>
344
						</td>
345
					</tr>
346
<?php endforeach; ?>
347
				</tbody>
348
			</table>
349
		</div>
350
	</div>
351
</div>
352

    
353
<nav class="action-buttons">
354
	<button type="submit" id="order-store" name="order-store" class="btn btn-sm btn-primary" value="store changes" disabled title="<?=gettext('Save rule order')?>">
355
		<i class="fa fa-save icon-embed-btn"></i>
356
		<?=gettext("Save")?>
357
	</button>
358
	<a href="system_gateways_edit.php" role="button" class="btn btn-success">
359
		<i class="fa fa-plus icon-embed-btn"></i>
360
		<?=gettext("Add");?>
361
	</a>
362
</nav>
363
</form>
364
<?php
365

    
366
$form = new Form;
367
$section = new Form_Section('Default gateway');
368

    
369
$dflts = available_default_gateways();
370

    
371
$section->addInput(new Form_Select(
372
	'defaultgw4',
373
	'Default gateway IPv4',
374
	$pconfig['defaultgw4'],
375
	$dflts['v4']
376
))->setHelp('Select the gateway or gatewaygroup to use as the default gateway.');
377

    
378
$section->addInput(new Form_Select(
379
	'defaultgw6',
380
	'Default gateway IPv6',
381
	$pconfig['defaultgw6'],
382
	$dflts['v6']
383
))->setHelp('Select the gateway or gatewaygroup to use as the default gateway.');
384

    
385
$form->add($section);
386
print $form;
387

    
388
?>
389
<div class="infoblock">
390
<?php
391
print_info_box(
392
	sprintf(gettext('%1$s The current default route as present in the current routing table of the operating system'), '<strong><i class="fa fa-globe"></i></strong>') .
393
	sprintf(gettext('%1$s Gateway is inactive, interface is missing'), '<br /><strong><i class="fa fa-times-circle-o"></i></strong>') .
394
	sprintf(gettext('%1$s Gateway disabled'), '<br /><strong><i class="fa fa-ban"></i></strong>') .
395
	sprintf(gettext('%1$s Gateway enabled'), '<br /><strong><i class="fa fa-check-circle-o"></i></strong>')
396
	);
397
?>
398
</div>
399
<script type="text/javascript">
400
//<![CDATA[
401
events.push(function() {
402
	$('#order-store').click(function () {
403
		// Check all of the rule checkboxes so that their values are posted
404
	   $('[id^=frc]').prop('checked', true);
405
	});
406

    
407
	$('[id^=Xmove_]').click(function (event) {
408
		// anchor click to move gateways around..
409
		moveRowUpAboveAnchor(event.target.id.slice(6),"gateways");
410
		return false;
411
	});
412
	$('[id^=Xmove_]').css('cursor', 'pointer');
413
});
414
	function moveRowUpAboveAnchor(rowId, tableId) {
415
		var table = $('#'+tableId);
416
		var viewcheckboxes = $('[id^=frc]input:checked', table);
417
		var rowview = $("#fr" + rowId, table);
418
		var moveabove = rowview;
419
		//var parent = moveabove[0].parentNode;
420
		
421
		viewcheckboxes.each(function( index ) {
422
			var moveid = this.value;
423
			console.log( index + ": " + this.id );
424

    
425
			var prevrowview = $("#fr" + moveid, table);
426
			prevrowview.insertBefore(moveabove);
427
			$('#order-store').removeAttr('disabled');
428
		});
429
	}
430
//]]>
431
</script>
432

    
433
<?php include("foot.inc");
(201-201/230)