Project

General

Profile

Download (10.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-2016 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);
36
$a_gateways_arr = array();
37
foreach ($a_gateways as $gw) {
38
	$a_gateways_arr[] = $gw;
39
}
40
$a_gateways = $a_gateways_arr;
41

    
42
if (!is_array($config['gateways']['gateway_item'])) {
43
	$config['gateways']['gateway_item'] = array();
44
}
45

    
46
$a_gateway_item = &$config['gateways']['gateway_item'];
47

    
48
$pconfig = $_REQUEST;
49

    
50
if ($_POST['apply']) {
51

    
52
	$retval = 0;
53

    
54
	$retval |= system_routing_configure();
55
	$retval |= system_resolvconf_generate();
56
	$retval |= filter_configure();
57
	/* reconfigure our gateway monitor */
58
	setup_gateways_monitor();
59
	/* Dynamic DNS on gw groups may have changed */
60
	send_event("service reload dyndnsall");
61

    
62
	if ($retval == 0) {
63
		clear_subsystem_dirty('staticroutes');
64
	}
65
}
66

    
67

    
68
function can_delete_disable_gateway_item($id, $disable = false) {
69
	global $config, $input_errors, $a_gateways;
70

    
71
	if (!isset($a_gateways[$id])) {
72
		return false;
73
	}
74

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

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

    
105
	if (isset($input_errors)) {
106
		return false;
107
	}
108

    
109
	return true;
110
}
111

    
112
function delete_gateway_item($id) {
113
	global $config, $a_gateways;
114

    
115
	if (!isset($a_gateways[$id])) {
116
		return;
117
	}
118

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

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

    
148
	if ($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway'] == $a_gateways[$id]['name']) {
149
		unset($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway']);
150
	}
151
	unset($config['gateways']['gateway_item'][$a_gateways[$id]['attribute']]);
152
}
153

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

    
166
if (isset($_REQUEST['del_x'])) {
167
	/* delete selected items */
168
	if (is_array($_REQUEST['rule']) && count($_REQUEST['rule'])) {
169
		foreach ($_REQUEST['rule'] as $rulei) {
170
			if (!can_delete_disable_gateway_item($rulei)) {
171
				break;
172
			}
173
		}
174

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

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

    
213
		if (write_config("Gateways: enable/disable")) {
214
			mark_subsystem_dirty('staticroutes');
215
		}
216

    
217
		header("Location: system_gateways.php");
218
		exit;
219
	}
220
}
221

    
222
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Gateways"));
223
$pglinks = array("", "@self", "@self");
224
$shortcut_section = "gateways";
225

    
226
include("head.inc");
227

    
228
if ($input_errors) {
229
	print_input_errors($input_errors);
230
}
231

    
232
if ($_POST['apply']) {
233
	print_apply_result_box($retval);
234
}
235

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

    
240
$tab_array = array();
241
$tab_array[0] = array(gettext("Gateways"), true, "system_gateways.php");
242
$tab_array[1] = array(gettext("Static Routes"), false, "system_routes.php");
243
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
244
display_top_tabs($tab_array);
245

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

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

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

    
317
<?php endif; ?>
318
						</td>
319
					</tr>
320
<?php endforeach; ?>
321
				</tbody>
322
			</table>
323
		</div>
324
	</div>
325
</div>
326

    
327
<nav class="action-buttons">
328
	<a href="system_gateways_edit.php" role="button" class="btn btn-success">
329
		<i class="fa fa-plus icon-embed-btn"></i>
330
		<?=gettext("Add");?>
331
	</a>
332
</nav>
333
<?php
334

    
335
include("foot.inc");
(195-195/223)