Project

General

Profile

Download (10.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-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

    
35
$a_gateways = return_gateways_array(true, false, true, true);
36

    
37
if (!is_array($config['gateways']['gateway_item'])) {
38
	$config['gateways']['gateway_item'] = array();
39
}
40

    
41
$a_gateway_item = &$config['gateways']['gateway_item'];
42

    
43
$pconfig = $_REQUEST;
44

    
45
if ($_POST['apply']) {
46

    
47
	$retval = 0;
48

    
49
	$retval |= system_routing_configure();
50
	$retval |= system_resolvconf_generate();
51
	$retval |= filter_configure();
52
	/* reconfigure our gateway monitor */
53
	setup_gateways_monitor();
54
	/* Dynamic DNS on gw groups may have changed */
55
	send_event("service reload dyndnsall");
56

    
57
	if ($retval == 0) {
58
		clear_subsystem_dirty('staticroutes');
59
	}
60
}
61

    
62

    
63
function can_delete_disable_gateway_item($id, $disable = false) {
64
	global $config, $input_errors, $a_gateways;
65

    
66
	if (!isset($a_gateways[$id])) {
67
		return false;
68
	}
69

    
70
	if (is_array($config['gateways']['gateway_group'])) {
71
		foreach ($config['gateways']['gateway_group'] as $group) {
72
			foreach ($group['item'] as $item) {
73
				$items = explode("|", $item);
74
				if ($items[0] == $a_gateways[$id]['name']) {
75
					if (!$disable) {
76
						$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']);
77
					} else {
78
						$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']);
79
					}
80
				}
81
			}
82
		}
83
	}
84

    
85
	if (is_array($config['staticroutes']['route'])) {
86
		foreach ($config['staticroutes']['route'] as $route) {
87
			if ($route['gateway'] == $a_gateways[$id]['name']) {
88
				if (!$disable) {
89
					// The user wants to delete this gateway, but there is a static route (enabled or disabled) that refers to the gateway.
90
					$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']);
91
				} else if (!isset($route['disabled'])) {
92
					// The user wants to disable this gateway.
93
					// But there is a static route that uses this gateway and is enabled (not disabled).
94
					$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']);
95
				}
96
			}
97
		}
98
	}
99

    
100
	if (isset($input_errors)) {
101
		return false;
102
	}
103

    
104
	return true;
105
}
106

    
107
function delete_gateway_item($id) {
108
	global $config, $a_gateways;
109

    
110
	if (!isset($a_gateways[$id])) {
111
		return;
112
	}
113

    
114
	/* If the removed gateway was the default route, remove the default route */
115
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
116
	    !isset($a_gateways[$id]['disabled']) &&
117
	    isset($a_gateways[$id]['defaultgw'])) {
118
		$inet = (!is_ipaddrv4($a_gateways[$id]['gateway']) ? '-inet6' : '-inet');
119
		mwexec("/sbin/route delete {$inet} default");
120
	}
121

    
122
	/* NOTE: Cleanup static routes for the interface route if any */
123
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
124
	    $gateway['gateway'] != $a_gateways[$id]['gateway'] &&
125
	    isset($a_gateways[$id]["nonlocalgateway"])) {
126
		$realif = get_real_interface($a_gateways[$id]['interface']);
127
		$inet = (!is_ipaddrv4($a_gateways[$id]['gateway']) ? "-inet6" : "-inet");
128
		$cmd = "/sbin/route delete $inet " . escapeshellarg($a_gateways[$id]['gateway']) . " -iface " . escapeshellarg($realif);
129
		mwexec($cmd);
130
	}
131
	/* NOTE: Cleanup static routes for the monitor ip if any */
132
	if (!empty($a_gateways[$id]['monitor']) &&
133
	    $a_gateways[$id]['monitor'] != "dynamic" &&
134
	    is_ipaddr($a_gateways[$id]['monitor']) &&
135
	    $a_gateways[$id]['gateway'] != $a_gateways[$id]['monitor']) {
136
		if (is_ipaddrv4($a_gateways[$id]['monitor'])) {
137
			mwexec("/sbin/route delete " . escapeshellarg($a_gateways[$id]['monitor']));
138
		} else {
139
			mwexec("/sbin/route delete -inet6 " . escapeshellarg($a_gateways[$id]['monitor']));
140
		}
141
	}
142

    
143
	if ($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway'] == $a_gateways[$id]['name']) {
144
		unset($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway']);
145
	}
146
	unset($config['gateways']['gateway_item'][$a_gateways[$id]['attribute']]);
147
}
148

    
149
unset($input_errors);
150
if ($_REQUEST['act'] == "del") {
151
	if (can_delete_disable_gateway_item($_REQUEST['id'])) {
152
		$realid = $a_gateways[$_REQUEST['id']]['attribute'];
153
		delete_gateway_item($_REQUEST['id']);
154
		write_config("Gateways: removed gateway {$realid}");
155
		mark_subsystem_dirty('staticroutes');
156
		header("Location: system_gateways.php");
157
		exit;
158
	}
159
}
160

    
161
if (isset($_REQUEST['del_x'])) {
162
	/* delete selected items */
163
	if (is_array($_REQUEST['rule']) && count($_REQUEST['rule'])) {
164
		foreach ($_REQUEST['rule'] as $rulei) {
165
			if (!can_delete_disable_gateway_item($rulei)) {
166
				break;
167
			}
168
		}
169

    
170
		if (!isset($input_errors)) {
171
			$items_deleted = "";
172
			foreach ($_REQUEST['rule'] as $rulei) {
173
				delete_gateway_item($rulei);
174
				$items_deleted .= "{$rulei} ";
175
			}
176
			if (!empty($items_deleted)) {
177
				write_config(sprintf(gettext("Gateways: removed gateways %s", $items_deleted)));
178
				mark_subsystem_dirty('staticroutes');
179
			}
180
			header("Location: system_gateways.php");
181
			exit;
182
		}
183
	}
184

    
185
} else if ($_REQUEST['act'] == "toggle" && $a_gateways[$_REQUEST['id']]) {
186
	$realid = $a_gateways[$_REQUEST['id']]['attribute'];
187
	$disable_gw = !isset($a_gateway_item[$realid]['disabled']);
188
	if ($disable_gw) {
189
		// The user wants to disable the gateway, so check if that is OK.
190
		$ok_to_toggle = can_delete_disable_gateway_item($_REQUEST['id'], $disable_gw);
191
	} else {
192
		// The user wants to enable the gateway. That is always OK.
193
		$ok_to_toggle = true;
194
	}
195
	if ($ok_to_toggle) {
196
		if ($disable_gw) {
197
			$a_gateway_item[$realid]['disabled'] = true;
198
			/* If the disabled gateway was the default route, remove the default route */
199
			if (!empty($a_gateway_item[$realid]) && is_ipaddr($a_gateway_item[$realid]['gateway']) &&
200
			    isset($a_gateway_item[$realid]['defaultgw'])) {
201
				$inet = (!is_ipaddrv4($a_gateway_item[$realid]['gateway']) ? '-inet6' : '-inet');
202
				mwexec("/sbin/route delete {$inet} default");
203
			}
204
		} else {
205
			unset($a_gateway_item[$realid]['disabled']);
206
		}
207

    
208
		if (write_config("Gateways: enable/disable")) {
209
			mark_subsystem_dirty('staticroutes');
210
		}
211

    
212
		header("Location: system_gateways.php");
213
		exit;
214
	}
215
}
216

    
217
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Gateways"));
218
$pglinks = array("", "@self", "@self");
219
$shortcut_section = "gateways";
220

    
221
include("head.inc");
222

    
223
if ($input_errors) {
224
	print_input_errors($input_errors);
225
}
226

    
227
if ($_POST['apply']) {
228
	print_apply_result_box($retval);
229
}
230

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

    
235
$tab_array = array();
236
$tab_array[0] = array(gettext("Gateways"), true, "system_gateways.php");
237
$tab_array[1] = array(gettext("Static Routes"), false, "system_routes.php");
238
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
239
display_top_tabs($tab_array);
240

    
241
?>
242
<div class="panel panel-default">
243
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Gateways')?></h2></div>
244
	<div class="panel-body">
245
		<div class="table-responsive">
246
			<table class="table table-striped table-hover table-condensed table-rowdblclickedit">
247
				<thead>
248
					<tr>
249
						<th></th>
250
						<th><?=gettext("Name")?></th>
251
						<th><?=gettext("Interface")?></th>
252
						<th><?=gettext("Gateway")?></th>
253
						<th><?=gettext("Monitor IP")?></th>
254
						<th><?=gettext("Description")?></th>
255
						<th><?=gettext("Actions")?></th>
256
					</tr>
257
				</thead>
258
				<tbody>
259
<?php
260
foreach ($a_gateways as $i => $gateway):
261
	if (isset($gateway['inactive'])) {
262
		$icon = 'fa-times-circle-o';
263
	} elseif (isset($gateway['disabled'])) {
264
		$icon = 'fa-ban';
265
	} else {
266
		$icon = 'fa-check-circle-o';
267
	}
268

    
269
	if (isset($gateway['inactive'])) {
270
		$title = gettext("This gateway is inactive because interface is missing");
271
	} else {
272
		$title = '';
273
	}
274
?>
275
				<tr<?=($icon != 'fa-check-circle-o')? ' class="disabled"' : ''?>>
276
					<td title="<?=$title?>"><i class="fa <?=$icon?>"></i></td>
277
					<td>
278
						<?=htmlspecialchars($gateway['name'])?>
279
<?php
280
			if (isset($gateway['defaultgw'])) {
281
				echo " <strong>(default)</strong>";
282
			}
283
?>
284
						</td>
285
						<td>
286
							<?=htmlspecialchars(convert_friendly_interface_to_friendly_descr($gateway['friendlyiface']))?>
287
						</td>
288
						<td>
289
							<?=htmlspecialchars($gateway['gateway'])?>
290
						</td>
291
						<td>
292
							<?=htmlspecialchars($gateway['monitor'])?>
293
						</td>
294
						<td>
295
							<?=htmlspecialchars($gateway['descr'])?>
296
						</td>
297
						<td>
298
							<a href="system_gateways_edit.php?id=<?=$i?>" class="fa fa-pencil" title="<?=gettext('Edit gateway');?>"></a>
299
							<a href="system_gateways_edit.php?dup=<?=$i?>" class="fa fa-clone" title="<?=gettext('Copy gateway')?>"></a>
300

    
301
<?php if (is_numeric($gateway['attribute'])): ?>
302
	<?php if (isset($gateway['disabled'])) {
303
	?>
304
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-check-square-o" title="<?=gettext('Enable gateway')?>" usepost></a>
305
	<?php } else {
306
	?>
307
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-ban" title="<?=gettext('Disable gateway')?>" usepost></a>
308
	<?php }
309
	?>
310
							<a href="system_gateways.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext('Delete gateway')?>" usepost></a>
311

    
312
<?php endif; ?>
313
						</td>
314
					</tr>
315
<?php endforeach; ?>
316
				</tbody>
317
			</table>
318
		</div>
319
	</div>
320
</div>
321

    
322
<nav class="action-buttons">
323
	<a href="system_gateways_edit.php" role="button" class="btn btn-success">
324
		<i class="fa fa-plus icon-embed-btn"></i>
325
		<?=gettext("Add");?>
326
	</a>
327
</nav>
328
<?php
329

    
330
include("foot.inc");
(203-203/232)