Project

General

Profile

Download (14.5 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
refresh_gateways(); // make sure we're working on a current gateway list
41

    
42
$pconfig = $_REQUEST;
43

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

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

    
71
	$retval = 0;
72

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

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

    
86
$a_gateways = get_gateways(GW_CACHE_INDEXED);
87

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

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

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

    
121
	/* prevent removing a gateway if it's still in use by DNS servers
122
	 * see https://redmine.pfsense.org/issues/8390 */
123
	$dnsgw_counter = 1;
124
	foreach (config_get_path('system/dnsserver', []) as $dnsserver) {
125
		if (config_path_enabled("system", "dns{$dnsgw_counter}gw") &&
126
		    ($a_gateways[$id]['name'] == config_get_path("system/dns{$dnsgw_counter}gw"))) {
127
				if (!$disable) {
128
					// The user wants to delete this gateway, but there is a static route to the DNS server that refers to the gateway.
129
					$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);
130
				} else {
131
					// The user wants to disable this gateway, but there is a static route to the DNS server that refers to the gateway.
132
					$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);
133
				}
134
		}
135
		$dnsgw_counter++;
136
	}
137

    
138
	if (isset($input_errors)) {
139
		return false;
140
	}
141

    
142
	return true;
143
}
144

    
145
function delete_gateway_item($id) {
146
	global $a_gateways;
147

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

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

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

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

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

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

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

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

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

    
233
		header("Location: system_gateways.php");
234
		exit;
235
	}
236
}
237

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

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

    
246
include("head.inc");
247

    
248
if ($input_errors) {
249
	print_input_errors($input_errors);
250
}
251

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

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

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

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

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

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

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

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

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

    
377
$form = new Form;
378
$section = new Form_Section('Default gateway');
379

    
380
$dflts = available_default_gateways();
381

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

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

    
396
$form->add($section);
397
print $form;
398

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

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

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

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