Project

General

Profile

Download (16 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	system_routes_edit.php
4
	part of m0n0wall (http://m0n0.ch/wall)
5
	part of pfSense
6
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
7
	Copyright (C) 2010 Scott Ullrich
8
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
9
	All rights reserved.
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

    
17
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in the
19
	   documentation and/or other materials provided with the distribution.
20

    
21
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32
/*
33
	pfSense_MODULE:	routing
34
*/
35

    
36
##|+PRIV
37
##|*IDENT=page-system-staticroutes-editroute
38
##|*NAME=System: Static Routes: Edit route page
39
##|*DESCR=Allow access to the 'System: Static Routes: Edit route' page.
40
##|*MATCH=system_routes_edit.php*
41
##|-PRIV
42

    
43
require_once("guiconfig.inc");
44
require_once("filter.inc");
45
require_once("util.inc");
46
require_once("gwlb.inc");
47

    
48
$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/system_routes.php');
49

    
50
if (!is_array($config['staticroutes']['route']))
51
	$config['staticroutes']['route'] = array();
52

    
53
$a_routes = &$config['staticroutes']['route'];
54
$a_gateways = return_gateways_array(true, true);
55

    
56
if (is_numericint($_GET['id']))
57
	$id = $_GET['id'];
58
if (isset($_POST['id']) && is_numericint($_POST['id']))
59
	$id = $_POST['id'];
60

    
61
if (isset($_GET['dup']) && is_numericint($_GET['dup']))
62
	$id = $_GET['dup'];
63

    
64
if (isset($id) && $a_routes[$id]) {
65
	list($pconfig['network'],$pconfig['network_subnet']) =
66
		explode('/', $a_routes[$id]['network']);
67
	$pconfig['gateway'] = $a_routes[$id]['gateway'];
68
	$pconfig['descr'] = $a_routes[$id]['descr'];
69
	$pconfig['disabled'] = isset($a_routes[$id]['disabled']);
70
}
71

    
72
if (isset($_GET['dup']) && is_numericint($_GET['dup']))
73
	unset($id);
74

    
75
if ($_POST) {
76

    
77
	global $aliastable;
78

    
79
	unset($input_errors);
80
	$pconfig = $_POST;
81

    
82
	/* input validation */
83
	$reqdfields = explode(" ", "network network_subnet gateway");
84
	$reqdfieldsn = explode(",",
85
			gettext("Destination network") . "," .
86
			gettext("Destination network bit count") . "," .
87
			gettext("Gateway"));
88

    
89
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
90

    
91
	if (($_POST['network'] && !is_ipaddr($_POST['network']) && !is_alias($_POST['network']))) {
92
		$input_errors[] = gettext("A valid IPv4 or IPv6 destination network must be specified.");
93
	}
94
	if (($_POST['network_subnet'] && !is_numeric($_POST['network_subnet']))) {
95
		$input_errors[] = gettext("A valid destination network bit count must be specified.");
96
	}
97
	if (($_POST['gateway']) && is_ipaddr($_POST['network'])) {
98
		if (!isset($a_gateways[$_POST['gateway']]))
99
			$input_errors[] = gettext("A valid gateway must be specified.");
100
		if(!validate_address_family($_POST['network'], $_POST['gateway']))
101
			$input_errors[] = gettext("The gateway '{$a_gateways[$_POST['gateway']]['gateway']}' is a different Address Family as network '{$_POST['network']}'.");
102
	}
103

    
104
	/* check for overlaps */
105
	$current_targets = get_staticroutes(true);
106
	$new_targets = array();
107
	if(is_ipaddrv6($_POST['network'])) {
108
		$osn = gen_subnetv6($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
109
		$new_targets[] = $osn;
110
	}
111
	if (is_ipaddrv4($_POST['network'])) {
112
		if($_POST['network_subnet'] > 32)
113
			$input_errors[] = gettext("A IPv4 subnet can not be over 32 bits.");
114
		else {
115
			$osn = gen_subnet($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
116
			$new_targets[] = $osn;
117
		}
118
	} elseif (is_alias($_POST['network'])) {
119
		$osn = $_POST['network'];
120
		foreach (preg_split('/\s+/', $aliastable[$osn]) as $tgt) {
121
			if (is_ipaddrv4($tgt))
122
				$tgt .= "/32";
123
			if (is_ipaddrv6($tgt))
124
				$tgt .= "/128";
125
			if (!is_subnet($tgt))
126
				continue;
127
			if (!is_subnetv6($tgt))
128
				continue;
129
			$new_targets[] = $tgt;
130
		}
131
	}
132
	if (!isset($id))
133
		$id = count($a_routes);
134
	$oroute = $a_routes[$id];
135
	$old_targets = array();
136
	if (!empty($oroute)) {
137
		if (is_alias($oroute['network'])) {
138
			foreach (filter_expand_alias_array($oroute['network']) as $tgt) {
139
				if (is_ipaddrv4($tgt))
140
					$tgt .= "/32";
141
				else if (is_ipaddrv6($tgt))
142
					$tgt .= "/128";
143
				if (!is_subnet($tgt))
144
					continue;
145
				$old_targets[] = $tgt;
146
			}
147
		} else {
148
			$old_targets[] = $oroute['network'];
149
		}
150
	}
151

    
152
	$overlaps = array_intersect($current_targets, $new_targets);
153
	$overlaps = array_diff($overlaps, $old_targets);
154
	if (count($overlaps)) {
155
		$input_errors[] = gettext("A route to these destination networks already exists") . ": " . implode(", ", $overlaps);
156
	}
157

    
158
	if (is_array($config['interfaces'])) {
159
		foreach ($config['interfaces'] as $if) {
160
			if (is_ipaddrv4($_POST['network'])
161
				&& isset($if['ipaddr']) && isset($if['subnet'])
162
				&& is_ipaddrv4($if['ipaddr']) && is_numeric($if['subnet'])
163
				&& ($_POST['network_subnet'] == $if['subnet'])
164
				&& (gen_subnet($_POST['network'], $_POST['network_subnet']) == gen_subnet($if['ipaddr'], $if['subnet'])))
165
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
166

    
167
			else if (is_ipaddrv6($_POST['network'])
168
				&& isset($if['ipaddrv6']) && isset($if['subnetv6'])
169
				&& is_ipaddrv6($if['ipaddrv6']) && is_numeric($if['subnetv6'])
170
				&& ($_POST['network_subnet'] == $if['subnetv6'])
171
				&& (gen_subnetv6($_POST['network'], $_POST['network_subnet']) == gen_subnetv6($if['ipaddrv6'], $if['subnetv6'])))
172
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
173
		}
174
	}
175

    
176
	if (!$input_errors) {
177
		$route = array();
178
		$route['network'] = $osn;
179
		$route['gateway'] = $_POST['gateway'];
180
		$route['descr'] = $_POST['descr'];
181
		if ($_POST['disabled'])
182
			$route['disabled'] = true;
183
		else
184
			unset($route['disabled']);
185

    
186
		if (file_exists("{$g['tmp_path']}/.system_routes.apply"))
187
			$toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
188
		else
189
			$toapplylist = array();
190
		$a_routes[$id] = $route;
191

    
192
		if (!empty($oroute)) {
193
			$delete_targets = array_diff($old_targets, $new_targets);
194
			if (count($delete_targets))
195
				foreach ($delete_targets as $dts) {
196
					if(is_ipaddrv6($dts))
197
						$family = "-inet6";
198
					$toapplylist[] = "/sbin/route delete {$family} {$dts}";
199
				}
200
		}
201
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
202

    
203
		mark_subsystem_dirty('staticroutes');
204

    
205
		write_config();
206

    
207
		header("Location: system_routes.php");
208
		exit;
209
	}
210
}
211

    
212
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
213
$shortcut_section = "routing";
214
include("head.inc");
215
?>
216

    
217
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
218
<script type="text/javascript" src="/javascript/jquery.ipv4v6ify.js"></script>
219
<script type="text/javascript" src="/javascript/autosuggest.js?rev=1"></script>
220
<script type="text/javascript" src="/javascript/suggestions.js"></script>
221
<?php include("fbegin.inc");?>
222
<?php if ($input_errors) print_input_errors($input_errors); ?>
223
	<form action="system_routes_edit.php" method="post" name="iform" id="iform">
224
		<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="system routes edit">
225
			<tr>
226
				<td colspan="2" valign="top" class="listtopic"><?=gettext("Edit route entry"); ?></td>
227
			</tr>
228
			<tr>
229
				<td width="22%" valign="top" class="vncellreq"><?=gettext("Destination network"); ?></td>
230
				<td width="78%" class="vtable">
231
					<input name="network" type="text" class="formfldalias ipv4v6" id="network" size="20" value="<?=htmlspecialchars($pconfig['network']);?>" />
232
					/
233
					<select name="network_subnet" class="formselect ipv4v6" id="network_subnet">
234
					<?php for ($i = 128; $i >= 1; $i--): ?>
235
						<option value="<?=$i;?>" <?php if ($i == $pconfig['network_subnet']) echo "selected=\"selected\""; ?>>
236
							<?=$i;?>
237
						</option>
238
					<?php endfor; ?>
239
					</select>
240
					<br /><span class="vexpl"><?=gettext("Destination network for this static route"); ?></span>
241
				</td>
242
			</tr>
243
			<tr>
244
				<td width="22%" valign="top" class="vncellreq"><?=gettext("Gateway"); ?></td>
245
				<td width="78%" class="vtable">
246
					<select name="gateway" id="gateway" class="formselect">
247
					<?php
248
						foreach ($a_gateways as $gateway) {
249
							echo "<option value='{$gateway['name']}' ";
250
							if ($gateway['name'] == $pconfig['gateway'])
251
								echo "selected=\"selected\"";
252
							echo ">" . htmlspecialchars($gateway['name']) . " - " . htmlspecialchars($gateway['gateway']) . "</option>\n";
253
						}
254
					?>
255
					</select> <br />
256
					<div id='addgwbox'>
257
						<?=gettext("Choose which gateway this route applies to or"); ?> <a onclick="show_add_gateway();" href="#"><?=gettext("add a new one.");?></a>
258
					</div>
259
					<div id='notebox'>
260
					</div>
261
					<div style="display:none" id="status">
262
					</div>
263
					<div style="display:none" id="addgateway">
264
						<table border="1" style="background:#990000; border-style: none none none none; width:225px;" summary="add gateway">
265
							<tr>
266
								<td>
267
									<table bgcolor="#990000" cellpadding="1" cellspacing="1" summary="add">
268
										<tr><td>&nbsp;</td></tr>
269
										<tr>
270
											<td colspan="2" align="center"><b><font color="white"><?=gettext("Add new gateway:"); ?></font></b></td>
271
										</tr>
272
										<tr><td>&nbsp;</td></tr>
273
										<tr>
274
											<td width="45%" align="right"><font color="white"><?=gettext("Default gateway:"); ?></font></td><td><input type="checkbox" id="defaultgw" name="defaultgw" /></td>
275
										</tr>
276
										<tr>
277
											<td width="45%" align="right"><font color="white"><?=gettext("Interface:"); ?></font></td>
278
											<td>
279
												<select name="addinterfacegw" id="addinterfacegw">
280
												<?php $gwifs = get_configured_interface_with_descr();
281
													foreach($gwifs as $fif => $dif)
282
														echo "<option value=\"{$fif}\">{$dif}</option>\n";
283
												?>
284
												</select>
285
											</td>
286
										</tr>
287
										<tr>
288
											<td align="right"><font color="white"><?=gettext("Gateway Name:"); ?></font></td><td><input id="name" name="name" value="GW" /></td>
289
										</tr>
290
										<tr>
291
											<td align="right"><font color="white"><?=gettext("Gateway IP:"); ?></font></td><td><input id="gatewayip" name="gatewayip" /></td>
292
										</tr>
293
										<tr>
294
											<td align="right"><font color="white"><?=gettext("Description:"); ?></font></td><td><input id="gatewaydescr" name="gatewaydescr" /></td>
295
										</tr>
296
										<tr><td>&nbsp;</td></tr>
297
										<tr>
298
											<td colspan="2" align="center">
299
												<div id='savebuttondiv'>
300
													<input type="hidden" name="addrtype" id="addrtype" value="IPv4" />
301
													<input id="gwsave" type="button" value="<?=gettext("Save Gateway"); ?>" onclick='hide_add_gatewaysave();' />
302
													<input id="gwcancel" type="button" value="<?=gettext("Cancel"); ?>" onclick='hide_add_gateway();' />
303
												</div>
304
											</td>
305
										</tr>
306
										<tr><td>&nbsp;</td></tr>
307
									</table>
308
								</td>
309
							</tr>
310
						</table>
311
					</div>
312
				</td>
313
			</tr>
314
			<tr>
315
				<td width="22%" valign="top" class="vncell"><?=gettext("Disabled");?></td>
316
				<td width="78%" class="vtable">
317
					<input name="disabled" type="checkbox" id="disabled" value="yes" <?php if ($pconfig['disabled']) echo "checked=\"checked\""; ?> />
318
					<strong><?=gettext("Disable this static route");?></strong><br />
319
					<span class="vexpl"><?=gettext("Set this option to disable this static route without removing it from the list.");?></span>
320
				</td>
321
			</tr>
322
			<tr>
323
				<td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
324
				<td width="78%" class="vtable">
325
					<input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>" />
326
					<br /><span class="vexpl"><?=gettext("You may enter a description here for your reference (not parsed)."); ?></span>
327
				</td>
328
			</tr>
329
			<tr>
330
				<td width="22%" valign="top">&nbsp;</td>
331
				<td width="78%">
332
					<input id="save" name="Submit" type="submit" class="formbtn" value="<?=gettext("Save");?>" />
333
					<input type="button" class="formbtn" value="<?=gettext("Cancel");?>" onclick="window.location.href='<?=$referer;?>'" />
334
					<?php if (isset($id) && $a_routes[$id]): ?>
335
						<input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
336
					<?php endif; ?>
337
				</td>
338
			</tr>
339
		</table>
340
	</form>
341
<script type="text/javascript">
342
//<![CDATA[
343
	var gatewayip;
344
	var name;
345
	function show_add_gateway() {
346
		document.getElementById("addgateway").style.display = '';
347
		document.getElementById("addgwbox").style.display = 'none';
348
		document.getElementById("gateway").style.display = 'none';
349
		document.getElementById("save").style.display = 'none';
350
		document.getElementById("cancel").style.display = 'none';
351
		document.getElementById("gwsave").style.display = '';
352
		document.getElementById("gwcancel").style.display = '';
353
		jQuery('#notebox').html("");
354
	}
355
	function hide_add_gateway() {
356
		document.getElementById("addgateway").style.display = 'none';
357
		document.getElementById("addgwbox").style.display = '';
358
		document.getElementById("gateway").style.display = '';
359
		document.getElementById("save").style.display = '';
360
		document.getElementById("cancel").style.display = '';
361
		document.getElementById("gwsave").style.display = '';
362
		document.getElementById("gwcancel").style.display = '';
363
	}
364
	function hide_add_gatewaysave() {
365
		document.getElementById("addgateway").style.display = 'none';
366
		jQuery('#status').html('<img src="/themes/<?=$g['theme'];?>/images/misc/loader.gif"> One moment please...');
367
		var iface = jQuery('#addinterfacegw').val();
368
		name = jQuery('#name').val();
369
		var descr = jQuery('#gatewaydescr').val();
370
		gatewayip = jQuery('#gatewayip').val();
371
		addrtype = jQuery('#addrtype').val();
372
		var defaultgw = '';
373
		if (jQuery('#defaultgw').checked)
374
			defaultgw = 'yes';
375
		var url = "system_gateways_edit.php";
376
		var pars = 'isAjax=true&defaultgw=' + escape(defaultgw) + '&interface=' + escape(iface) + '&name=' + escape(name) + '&descr=' + escape(descr) + '&gateway=' + escape(gatewayip) + '&type=' + escape(addrtype);
377
		jQuery.ajax(
378
			url,
379
		{
380
			type: 'post',
381
				data: pars,
382
				error: report_failure,
383
				complete: save_callback
384
		});
385
	}
386
	function addOption(selectbox,text,value)
387
	{
388
		var optn = document.createElement("OPTION");
389
		optn.text = text;
390
		optn.value = value;
391
		selectbox.append(optn);
392
		selectbox.prop('selectedIndex',selectbox.children('option').length-1);
393
		jQuery('#notebox').html("<p><strong><?=gettext("NOTE:");?><\/strong> <?php printf(gettext("You can manage Gateways %shere%s."), "<a target='_blank' href='system_gateways.php'>", "<\/a>");?> <\/strong><\/p>");
394
	}
395
	function report_failure() {
396
		alert("<?=gettext("Sorry, we could not create your gateway at this time."); ?>");
397
		hide_add_gateway();
398
	}
399
	function save_callback(transport) {
400
		var response = transport.responseText;
401
		if (response) {
402
			document.getElementById("addgateway").style.display = 'none';
403
			hide_add_gateway();
404
			jQuery('#status').html('');
405
			addOption(jQuery('#gateway'), name, name);
406
		} else {
407
			report_failure();
408
		}
409
	}
410
	var addressarray = <?= json_encode(get_alias_list(array("host", "network"))) ?>;
411
	var oTextbox1 = new AutoSuggestControl(document.getElementById("network"), new StateSuggestions(addressarray));
412
//]]>
413
</script>
414
<?php include("fend.inc"); ?>
415
</body>
416
</html>
(228-228/256)