Project

General

Profile

Download (11.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-2016 Electric Sheep Fencing, LLC
7
 * Copyright (c) 2010 Seth Mos <seth.mos@dds.nl>
8
 * All rights reserved.
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions are met:
12
 *
13
 * 1. Redistributions of source code must retain the above copyright notice,
14
 *    this list of conditions and the following disclaimer.
15
 *
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in
18
 *    the documentation and/or other materials provided with the
19
 *    distribution.
20
 *
21
 * 3. All advertising materials mentioning features or use of this software
22
 *    must display the following acknowledgment:
23
 *    "This product includes software developed by the pfSense Project
24
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
25
 *
26
 * 4. The names "pfSense" and "pfSense Project" must not be used to
27
 *    endorse or promote products derived from this software without
28
 *    prior written permission. For written permission, please contact
29
 *    coreteam@pfsense.org.
30
 *
31
 * 5. Products derived from this software may not be called "pfSense"
32
 *    nor may "pfSense" appear in their names without prior written
33
 *    permission of the Electric Sheep Fencing, LLC.
34
 *
35
 * 6. Redistributions of any form whatsoever must retain the following
36
 *    acknowledgment:
37
 *
38
 * "This product includes software developed by the pfSense Project
39
 * for use in the pfSense software distribution (http://www.pfsense.org/).
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
42
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
45
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52
 * OF THE POSSIBILITY OF SUCH DAMAGE.
53
 */
54

    
55
##|+PRIV
56
##|*IDENT=page-system-gateways
57
##|*NAME=System: Gateways
58
##|*DESCR=Allow access to the 'System: Gateways' page.
59
##|*MATCH=system_gateways.php*
60
##|-PRIV
61

    
62
require_once("guiconfig.inc");
63
require_once("functions.inc");
64
require_once("filter.inc");
65
require_once("shaper.inc");
66

    
67
$a_gateways = return_gateways_array(true, false, true);
68
$a_gateways_arr = array();
69
foreach ($a_gateways as $gw) {
70
	$a_gateways_arr[] = $gw;
71
}
72
$a_gateways = $a_gateways_arr;
73

    
74
if (!is_array($config['gateways']['gateway_item'])) {
75
	$config['gateways']['gateway_item'] = array();
76
}
77

    
78
$a_gateway_item = &$config['gateways']['gateway_item'];
79

    
80
if ($_POST) {
81

    
82
	$pconfig = $_POST;
83

    
84
	if ($_POST['apply']) {
85

    
86
		$retval = 0;
87

    
88
		$retval = system_routing_configure();
89
		$retval |= system_resolvconf_generate();
90
		$retval |= filter_configure();
91
		/* reconfigure our gateway monitor */
92
		setup_gateways_monitor();
93
		/* Dynamic DNS on gw groups may have changed */
94
		send_event("service reload dyndnsall");
95

    
96
		$savemsg = get_std_save_message($retval);
97
		if ($retval == 0) {
98
			clear_subsystem_dirty('staticroutes');
99
		}
100
	}
101
}
102

    
103
function can_delete_disable_gateway_item($id, $disable = false) {
104
	global $config, $input_errors, $a_gateways;
105

    
106
	if (!isset($a_gateways[$id])) {
107
		return false;
108
	}
109

    
110
	if (is_array($config['gateways']['gateway_group'])) {
111
		foreach ($config['gateways']['gateway_group'] as $group) {
112
			foreach ($group['item'] as $item) {
113
				$items = explode("|", $item);
114
				if ($items[0] == $a_gateways[$id]['name']) {
115
					if (!$disable) {
116
						$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']);
117
					} else {
118
						$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']);
119
					}
120
				}
121
			}
122
		}
123
	}
124

    
125
	if (is_array($config['staticroutes']['route'])) {
126
		foreach ($config['staticroutes']['route'] as $route) {
127
			if ($route['gateway'] == $a_gateways[$id]['name']) {
128
				if (!$disable) {
129
					// The user wants to delete this gateway, but there is a static route (enabled or disabled) that refers to the gateway.
130
					$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']);
131
				} else if (!isset($route['disabled'])) {
132
					// The user wants to disable this gateway.
133
					// But there is a static route that uses this gateway and is enabled (not disabled).
134
					$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']);
135
				}
136
			}
137
		}
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 $config, $a_gateways;
149

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

    
154
	/* NOTE: Cleanup static routes for the interface route if any */
155
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
156
	    $gateway['gateway'] != $a_gateways[$id]['gateway'] &&
157
	    isset($a_gateways[$id]["nonlocalgateway"])) {
158
		$realif = get_real_interface($a_gateways[$id]['interface']);
159
		$inet = (!is_ipaddrv4($a_gateways[$id]['gateway']) ? "-inet6" : "-inet");
160
		$cmd = "/sbin/route delete $inet " . escapeshellarg($a_gateways[$id]['gateway']) . " -iface " . escapeshellarg($realif);
161
		mwexec($cmd);
162
	}
163
	/* NOTE: Cleanup static routes for the monitor ip if any */
164
	if (!empty($a_gateways[$id]['monitor']) &&
165
	    $a_gateways[$id]['monitor'] != "dynamic" &&
166
	    is_ipaddr($a_gateways[$id]['monitor']) &&
167
	    $a_gateways[$id]['gateway'] != $a_gateways[$id]['monitor']) {
168
		if (is_ipaddrv4($a_gateways[$id]['monitor'])) {
169
			mwexec("/sbin/route delete " . escapeshellarg($a_gateways[$id]['monitor']));
170
		} else {
171
			mwexec("/sbin/route delete -inet6 " . escapeshellarg($a_gateways[$id]['monitor']));
172
		}
173
	}
174

    
175
	if ($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway'] == $a_gateways[$id]['name']) {
176
		unset($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway']);
177
	}
178
	unset($config['gateways']['gateway_item'][$a_gateways[$id]['attribute']]);
179
}
180

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

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

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

    
217
} else if ($_GET['act'] == "toggle" && $a_gateways[$_GET['id']]) {
218
	$realid = $a_gateways[$_GET['id']]['attribute'];
219
	$disable_gw = !isset($a_gateway_item[$realid]['disabled']);
220
	if ($disable_gw) {
221
		// The user wants to disable the gateway, so check if that is OK.
222
		$ok_to_toggle = can_delete_disable_gateway_item($_GET['id'], $disable_gw);
223
	} else {
224
		// The user wants to enable the gateway. That is always OK.
225
		$ok_to_toggle = true;
226
	}
227
	if ($ok_to_toggle) {
228
		if ($disable_gw) {
229
			$a_gateway_item[$realid]['disabled'] = true;
230
		} else {
231
			unset($a_gateway_item[$realid]['disabled']);
232
		}
233

    
234
		if (write_config("Gateways: enable/disable")) {
235
			mark_subsystem_dirty('staticroutes');
236
		}
237

    
238
		header("Location: system_gateways.php");
239
		exit;
240
	}
241
}
242

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

    
246
include("head.inc");
247

    
248
if ($input_errors) {
249
	print_input_errors($input_errors);
250
}
251
if ($savemsg) {
252
	print_info_box($savemsg, 'success');
253
}
254

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

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

    
265
?>
266
<div class="panel panel-default">
267
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Gateways')?></h2></div>
268
	<div class="panel-body">
269
		<div class="table-responsive">
270
			<table class="table table-striped tabel-hover table-condensed table-rowdblclickedit">
271
				<thead>
272
					<tr>
273
						<th></th>
274
						<th><?=gettext("Name")?></th>
275
						<th><?=gettext("Interface")?></th>
276
						<th><?=gettext("Gateway")?></th>
277
						<th><?=gettext("Monitor IP")?></th>
278
						<th><?=gettext("Description")?></th>
279
						<th><?=gettext("Actions")?></th>
280
					</tr>
281
				</thead>
282
				<tbody>
283
<?php
284
foreach ($a_gateways as $i => $gateway):
285
	if (isset($gateway['inactive'])) {
286
		$icon = 'fa-times-circle-o';
287
	} elseif (isset($gateway['disabled'])) {
288
		$icon = 'fa-ban';
289
	} else {
290
		$icon = 'fa-check-circle-o';
291
	}
292

    
293
	if (isset($gateway['inactive'])) {
294
		$title = gettext("This gateway is inactive because interface is missing");
295
	} else {
296
		$title = '';
297
	}
298
?>
299
				<tr<?=($icon != 'fa-check-circle-o')? ' class="disabled"' : ''?>>
300
					<td title="<?=$title?>"><i class="fa <?=$icon?>"></i></td>
301
					<td>
302
						<?=htmlspecialchars($gateway['name'])?>
303
<?php
304
			if (isset($gateway['defaultgw'])) {
305
				echo " <strong>(default)</strong>";
306
			}
307
?>
308
						</td>
309
						<td>
310
							<?=htmlspecialchars(convert_friendly_interface_to_friendly_descr($gateway['friendlyiface']))?>
311
						</td>
312
						<td>
313
							<?=htmlspecialchars($gateway['gateway'])?>
314
						</td>
315
						<td>
316
							<?=htmlspecialchars($gateway['monitor'])?>
317
						</td>
318
						<td>
319
							<?=htmlspecialchars($gateway['descr'])?>
320
						</td>
321
						<td>
322
							<a href="system_gateways_edit.php?id=<?=$i?>" class="fa fa-pencil" title="<?=gettext('Edit gateway');?>"></a>
323
							<a href="system_gateways_edit.php?dup=<?=$i?>" class="fa fa-clone" title="<?=gettext('Copy gateway')?>"></a>
324

    
325
<?php if (is_numeric($gateway['attribute'])): ?>
326
	<?php if (isset($gateway['disabled'])) {
327
	?>
328
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-check-square-o" title="<?=gettext('Enable gateway')?>"></a>
329
	<?php } else {
330
	?>
331
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-ban" title="<?=gettext('Disable gateway')?>"></a>
332
	<?php }
333
	?>
334
							<a href="system_gateways.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext('Delete gateway')?>"></a>
335

    
336
<?php endif; ?>
337
						</td>
338
					</tr>
339
<?php endforeach; ?>
340
				</tbody>
341
			</table>
342
		</div>
343
	</div>
344
</div>
345

    
346
<nav class="action-buttons">
347
	<a href="system_gateways_edit.php" role="button" class="btn btn-success">
348
		<i class="fa fa-plus icon-embed-btn"></i>
349
		<?=gettext("Add");?>
350
	</a>
351
</nav>
352
<?php
353

    
354
include("foot.inc");
(197-197/225)