Project

General

Profile

Download (14.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-2024 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
config_init_path('gateways/gateway_item');
41
refresh_gateways(); // make sure we're working on a current gateway list
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[] = config_get_path("gateways/gateway_item/{$id}");
53
	}
54
	//print_r($a_gateway_item);
55
	//print_r($a_gateway_item_new);
56
	//print "</pre>";
57
	config_set_path('gateways/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_set_path("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
	/* reconfigure our gateway monitor */
75
	setup_gateways_monitor();
76
	$retval |= system_routing_configure();
77
	$retval |= system_resolvconf_generate();
78
	$retval |= filter_configure();
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 = get_gateways(GW_CACHE_INDEXED);
88

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

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

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

    
122
	/* prevent removing a gateway if it's still in use by DNS servers
123
	 * see https://redmine.pfsense.org/issues/8390 */
124
	$dnsgw_counter = 1;
125
	config_init_path('system/dnsserver');
126
	foreach (config_get_path('system/dnsserver', []) as $dnsserver) {
127
		if (config_path_enabled("system", "dns{$dnsgw_counter}gw") &&
128
		    ($a_gateways[$id]['name'] == config_get_path("system/dns{$dnsgw_counter}gw"))) {
129
				if (!$disable) {
130
					// The user wants to delete this gateway, but there is a static route to the DNS server that refers to the gateway.
131
					$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be deleted because it is in use by DNS Server "%2$s"'), $a_gateways[$id]['name'], $dnsserver);
132
				} else {
133
					// The user wants to disable this gateway, but there is a static route to the DNS server that refers to the gateway.
134
					$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be disabled because it is in use by DNS Server "%2$s"'), $a_gateways[$id]['name'], $dnsserver);
135
				}
136
		}
137
		$dnsgw_counter++;
138
	}
139

    
140
	if (isset($input_errors)) {
141
		return false;
142
	}
143

    
144
	return true;
145
}
146

    
147
function delete_gateway_item($id) {
148
	global $a_gateways;
149

    
150
	if (!isset($a_gateways[$id])) {
151
		return;
152
	}
153

    
154
	/* If the removed gateway was the default route, remove the default route */
155
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
156
	    !isset($a_gateways[$id]['disabled']) &&
157
	    isset($a_gateways[$id]['isdefaultgw'])) {
158
		$inet = (!is_ipaddrv4($a_gateways[$id]['gateway'])
159
		    ? 'inet6' : 'inet');
160
		route_del('default', $inet);
161
	}
162

    
163
	/* NOTE: Cleanup static routes for the interface route if any */
164
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
165
	    isset($a_gateways[$id]["nonlocalgateway"])) {
166
		route_del($a_gateways[$id]['gateway']);
167
	}
168
	/* NOTE: Cleanup static routes for the monitor ip if any */
169
	if (!empty($a_gateways[$id]['monitor']) &&
170
	    $a_gateways[$id]['monitor'] != "dynamic" &&
171
	    is_ipaddr($a_gateways[$id]['monitor']) &&
172
	    $a_gateways[$id]['gateway'] != $a_gateways[$id]['monitor']) {
173
		route_del($a_gateways[$id]['monitor']);
174
	}
175

    
176
	if (config_get_path("interfaces/{$a_gateways[$id]['friendlyiface']}/gateway") == $a_gateways[$id]['name']) {
177
		config_del_path("interfaces/{$a_gateways[$id]['friendlyiface']}/gateway");
178
	}
179
	config_del_path("gateways/gateway_item/{$a_gateways[$id]['attribute']}");
180
}
181

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

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

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

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

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

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

    
240
foreach($simplefields as $field) {
241
	$pconfig[$field] = config_get_path("gateways/{$field}");
242
}
243

    
244
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Gateways"));
245
$pglinks = array("", "@self", "@self");
246
$shortcut_section = "gateways";
247

    
248
include("head.inc");
249

    
250
if ($input_errors) {
251
	print_input_errors($input_errors);
252
}
253

    
254
if ($_POST['apply']) {
255
	print_apply_result_box($retval);
256
}
257

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

    
262
$tab_array = array();
263
$tab_array[0] = array(gettext("Gateways"), true, "system_gateways.php");
264
$tab_array[1] = array(gettext("Static Routes"), false, "system_routes.php");
265
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
266
display_top_tabs($tab_array);
267

    
268
?>
269
<form method="post">
270
<div class="panel panel-default">
271
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Gateways')?></h2></div>
272
	<div class="panel-body">
273
		<div class="table-responsive">
274
			<table id="gateways" class="table table-striped table-hover table-condensed table-rowdblclickedit">
275
				<thead>
276
					<tr>
277
						<th></th>
278
						<th></th>
279
						<th><?=gettext("Name")?></th>
280
						<th><?=gettext("Default")?></th>
281
						<th><?=gettext("Interface")?></th>
282
						<th><?=gettext("Gateway")?></th>
283
						<th><?=gettext("Monitor IP")?></th>
284
						<th><?=gettext("Description")?></th>
285
						<th><?=gettext("Actions")?></th>
286
					</tr>
287
				</thead>
288
				<tbody>
289
<?php
290
foreach ($a_gateways as $i => $gateway):
291
	if (isset($gateway['inactive'])) {
292
		$title = gettext("Gateway inactive, interface is missing");
293
		$icon = 'fa-regular fa-circle-xmark';
294
	} elseif (isset($gateway['disabled'])) {
295
		$icon = 'fa-solid fa-ban';
296
		$title = gettext("Gateway disabled");
297
	} else {
298
		$icon = 'fa-regular fa-circle-check';
299
		$title = gettext("Gateway enabled");
300
	}
301

    
302
	$gtitle = "";
303
	if (isset($gateway['isdefaultgw'])) {
304
		$gtitle = gettext("Default gateway");
305
	}
306

    
307
	$id = $gateway['attribute'];
308
?>
309
					<tr<?=($icon != 'fa-regular fa-circle-check')? ' class="disabled"' : ''?> onClick="fr_toggle(<?=$id;?>)" id="fr<?=$id;?>">
310
						<td style="white-space: nowrap;">
311
							<?php 
312
							if (is_numeric($id)) :?>
313
								<input type='checkbox' id='frc<?=$id?>' onClick='fr_toggle(<?=$id?>)' name='row[]' value='<?=$id?>'/>
314
								<a class='fa-solid fa-anchor' id='Xmove_<?=$id?>' title='"<?=gettext("Move checked entries to here")?>"'></a>
315
							<?php endif; ?>
316
						</td>
317
						<td title="<?=$title?>"><i class="<?=$icon?>"></i></td>
318
						<td title="<?=$gtitle?>">
319
						<?=htmlspecialchars($gateway['name'])?>
320
<?php
321
							if (isset($gateway['isdefaultgw'])) {
322
								echo ' <i class="fa-solid fa-globe"></i>';
323
							}
324
?>
325
						</td>
326
						<td>
327
							<?=htmlspecialchars($gateway['tiername'])?>
328
						</td>
329
						<td>
330
							<?=htmlspecialchars($gateway['friendlyifdescr'])?>
331
						</td>
332
						<td>
333
							<?=htmlspecialchars($gateway['gateway'])?>
334
						</td>
335
						<td>
336
							<?=htmlspecialchars($gateway['monitor'])?>
337
						</td>
338
						<td>
339
							<?=htmlspecialchars($gateway['descr'])?>
340
						</td>
341
						<td style="white-space: nowrap;">
342
							<a href="system_gateways_edit.php?id=<?=$i?>" class="fa-solid fa-pencil" title="<?=gettext('Edit gateway');?>"></a>
343
							<a href="system_gateways_edit.php?dup=<?=$i?>" class="fa-regular fa-clone" title="<?=gettext('Copy gateway')?>"></a>
344

    
345
<?php if (is_numeric($gateway['attribute'])): ?>
346
	<?php if (isset($gateway['disabled'])) {
347
	?>
348
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa-regular fa-square-check" title="<?=gettext('Enable gateway')?>" usepost></a>
349
	<?php } else {
350
	?>
351
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa-solid fa-ban" title="<?=gettext('Disable gateway')?>" usepost></a>
352
	<?php }
353
	?>
354
							<a href="system_gateways.php?act=del&amp;id=<?=$i?>" class="fa-solid fa-trash-can" title="<?=gettext('Delete gateway')?>" usepost></a>
355

    
356
<?php endif; ?>
357
						</td>
358
					</tr>
359
<?php endforeach; ?>
360
				</tbody>
361
			</table>
362
		</div>
363
	</div>
364
</div>
365

    
366
<nav class="action-buttons">
367
	<button type="submit" id="order-store" name="order-store" class="btn btn-sm btn-primary" value="store changes" disabled title="<?=gettext('Save rule order')?>">
368
		<i class="fa-solid fa-save icon-embed-btn"></i>
369
		<?=gettext("Save")?>
370
	</button>
371
	<a href="system_gateways_edit.php" role="button" class="btn btn-success">
372
		<i class="fa-solid fa-plus icon-embed-btn"></i>
373
		<?=gettext("Add");?>
374
	</a>
375
</nav>
376
</form>
377
<?php
378

    
379
$form = new Form;
380
$section = new Form_Section('Default gateway');
381

    
382
$dflts = available_default_gateways();
383

    
384
$section->addInput(new Form_Select(
385
	'defaultgw4',
386
	'Default gateway IPv4',
387
	$pconfig['defaultgw4'],
388
	$dflts['v4']
389
))->setHelp('Select a gateway or failover gateway group to use as the default gateway.');
390

    
391
$section->addInput(new Form_Select(
392
	'defaultgw6',
393
	'Default gateway IPv6',
394
	$pconfig['defaultgw6'],
395
	$dflts['v6']
396
))->setHelp('Select a gateway or failover gateway group to use as the default gateway.');
397

    
398
$form->add($section);
399
print $form;
400

    
401
?>
402
<div class="infoblock">
403
<?php
404
print_info_box(
405
	sprintf(gettext('%1$s The current default route as present in the current routing table of the operating system'), '<strong><i class="fa-solid fa-globe"></i></strong>') .
406
	sprintf(gettext('%1$s Gateway is inactive, interface is missing'), '<br /><strong><i class="fa-regular fa-circle-xmark"></i></strong>') .
407
	sprintf(gettext('%1$s Gateway disabled'), '<br /><strong><i class="fa-solid fa-ban"></i></strong>') .
408
	sprintf(gettext('%1$s Gateway enabled'), '<br /><strong><i class="fa-regular fa-circle-check"></i></strong>')
409
	);
410
?>
411
</div>
412
<script type="text/javascript">
413
//<![CDATA[
414
events.push(function() {
415
	$('#order-store').click(function () {
416
		// Check all of the rule checkboxes so that their values are posted
417
	   $('[id^=frc]').prop('checked', true);
418
	});
419

    
420
	$('[id^=Xmove_]').click(function (event) {
421
		// anchor click to move gateways around..
422
		moveRowUpAboveAnchor(event.target.id.slice(6),"gateways");
423
		return false;
424
	});
425
	$('[id^=Xmove_]').css('cursor', 'pointer');
426
});
427
	function moveRowUpAboveAnchor(rowId, tableId) {
428
		var table = $('#'+tableId);
429
		var viewcheckboxes = $('[id^=frc]input:checked', table);
430
		var rowview = $("#fr" + rowId, table);
431
		var moveabove = rowview;
432
		//var parent = moveabove[0].parentNode;
433
		
434
		viewcheckboxes.each(function( index ) {
435
			var moveid = this.value;
436
			console.log( index + ": " + this.id );
437

    
438
			var prevrowview = $("#fr" + moveid, table);
439
			prevrowview.insertBefore(moveabove);
440
			$('#order-store').removeAttr('disabled');
441
		});
442
	}
443
//]]>
444
</script>
445

    
446
<?php include("foot.inc");
(202-202/232)