Project

General

Profile

Download (16.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	system_routes_edit.php
4
	part of m0n0wall (http://m0n0.ch/wall)
5

    
6
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
7
	Copyright (C) 2010 Scott Ullrich
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 the
18
	   documentation and/or other materials provided with the distribution.
19

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

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

    
42
function staticroutecmp($a, $b) {
43
	return strcmp($a['network'], $b['network']);
44
}
45

    
46
function staticroutes_sort() {
47
	global $g, $config;
48

    
49
	if (!is_array($config['staticroutes']['route']))
50
		return;
51

    
52
	usort($config['staticroutes']['route'], "staticroutecmp");
53
}
54

    
55
require_once("guiconfig.inc");
56
require_once("filter.inc");
57
require_once("util.inc");
58
require_once("gwlb.inc");
59

    
60
if (!is_array($config['staticroutes']['route']))
61
	$config['staticroutes']['route'] = array();
62

    
63
$a_routes = &$config['staticroutes']['route'];
64
$a_gateways = return_gateways_array(true, true);
65

    
66
$id = $_GET['id'];
67
if (isset($_POST['id']))
68
	$id = $_POST['id'];
69

    
70
if (isset($_GET['dup'])) {
71
	$id = $_GET['dup'];
72
}
73

    
74
if (isset($id) && $a_routes[$id]) {
75
	list($pconfig['network'],$pconfig['network_subnet']) =
76
		explode('/', $a_routes[$id]['network']);
77
	$pconfig['gateway'] = $a_routes[$id]['gateway'];
78
	$pconfig['descr'] = $a_routes[$id]['descr'];
79
	$pconfig['disabled'] = isset($a_routes[$id]['disabled']);
80
}
81

    
82
if (isset($_GET['dup']))
83
	unset($id);
84

    
85
if ($_POST) {
86

    
87
	unset($input_errors);
88
	$pconfig = $_POST;
89

    
90
	/* input validation */
91
	$reqdfields = explode(" ", "network network_subnet gateway");
92
	$reqdfieldsn = explode(",",
93
			gettext("Destination network") . "," .
94
			gettext("Destination network bit count") . "," .
95
			gettext("Gateway"));
96

    
97
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
98

    
99
	if (($_POST['network'] && !is_ipaddr($_POST['network']) && !is_alias($_POST['network']))) {
100
		$input_errors[] = gettext("A valid IPv4 or IPv6 destination network must be specified.");
101
	}
102
	if (($_POST['network_subnet'] && !is_numeric($_POST['network_subnet']))) {
103
		$input_errors[] = gettext("A valid destination network bit count must be specified.");
104
	}
105
	if (($_POST['gateway']) && is_ipaddr($_POST['network'])) {
106
		if (!isset($a_gateways[$_POST['gateway']]))
107
			$input_errors[] = gettext("A valid gateway must be specified.");
108
		if(!validate_address_family($_POST['network'], lookup_gateway_ip_by_name($_POST['gateway'])))
109
			$input_errors[] = gettext("The gateway '{$a_gateways[$_POST['gateway']]['gateway']}' is a different Address Family as network '{$_POST['network']}'.");
110
	}
111

    
112
	/* check for overlaps */
113
	$current_targets = get_staticroutes(true);
114
	$new_targets = array();
115
	if(is_ipaddrv6($_POST['network'])) {
116
		$osn = gen_subnetv6($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
117
		$new_targets[] = $osn;
118
	}
119
	if (is_ipaddrv4($_POST['network'])) {
120
		if($_POST['network_subnet'] > 32)
121
			$input_errors[] = gettext("A IPv4 subnet can not be over 32 bits.");
122
		else {
123
			$osn = gen_subnet($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
124
			$new_targets[] = $osn;
125
		}
126
	} elseif (is_alias($_POST['network'])) {
127
		$osn = $_POST['network'];
128
		$fqdn_found = 0;
129
		foreach (filter_expand_alias_array($_POST['network']) as $tgt) {
130
			if (!is_ipaddr($tgt) && is_hostname($tgt)) {
131
				if ($fqdn_found === 0) {
132
					$input_errors[] = sprintf(gettext("The alias (%s) has one or more FQDNs configured and cannot be used to configure a static route."), $_POST['network']);
133
					$fqdn_found = 1;
134
				}
135
				continue;
136
			}
137
			if (is_ipaddrv4($tgt))
138
				$tgt .= "/32";
139
			if (is_ipaddrv6($tgt))
140
				$tgt .= "/128";
141
			if (!is_subnet($tgt))
142
				continue;
143
			if (!is_subnetv6($tgt))
144
				continue;
145
			$new_targets[] = $tgt;
146
		}
147
	}
148
	if (!isset($id))
149
		$id = count($a_routes);
150
	$oroute = $a_routes[$id];
151
	$old_targets = array();
152
	if (!empty($oroute)) {
153
		if (is_alias($oroute['network'])) {
154
			foreach (filter_expand_alias_array($oroute['network']) as $tgt) {
155
				if (is_ipaddr($tgt))
156
					$tgt .= "/32";
157
				if (!is_subnet($tgt))
158
					continue;
159
				$old_targets[] = $tgt;
160
			}
161
		} else {
162
			$old_targets[] = $oroute['network'];
163
		}
164
	}
165

    
166
	$overlaps = array_intersect($current_targets, $new_targets);
167
	$overlaps = array_diff($overlaps, $old_targets);
168
	if (count($overlaps)) {
169
		$input_errors[] = gettext("A route to these destination networks already exists") . ": " . implode(", ", $overlaps);
170
	}
171

    
172
	if (is_array($config['interfaces'])) {
173
		foreach ($config['interfaces'] as $if) {
174
			if (is_ipaddrv4($_POST['network'])
175
				&& isset($if['ipaddr']) && isset($if['subnet'])
176
				&& is_ipaddrv4($if['ipaddr']) && is_numeric($if['subnet'])
177
				&& ($_POST['network_subnet'] == $if['subnet'])
178
				&& (gen_subnet($_POST['network'], $_POST['network_subnet']) == gen_subnet($if['ipaddr'], $if['subnet'])))
179
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
180

    
181
			else if (is_ipaddrv6($_POST['network'])
182
				&& isset($if['ipaddrv6']) && isset($if['subnetv6'])
183
				&& is_ipaddrv6($if['ipaddrv6']) && is_numeric($if['subnetv6'])
184
				&& ($_POST['network_subnet'] == $if['subnetv6'])
185
				&& (gen_subnetv6($_POST['network'], $_POST['network_subnet']) == gen_subnetv6($if['ipaddrv6'], $if['subnetv6'])))
186
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
187
		}
188
	}
189

    
190
	if (!$input_errors) {
191
		$route = array();
192
		$route['network'] = $osn;
193
		$route['gateway'] = $_POST['gateway'];
194
		$route['descr'] = $_POST['descr'];
195
		if ($_POST['disabled'])
196
			$route['disabled'] = true;
197
		else
198
			unset($route['disabled']);
199

    
200
		if (file_exists("{$g['tmp_path']}/.system_routes.apply"))
201
			$toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
202
		else
203
			$toapplylist = array();
204
		$a_routes[$id] = $route;
205

    
206
		if (!empty($oroute)) {
207
			$delete_targets = array_diff($old_targets, $new_targets);
208
			if (count($delete_targets))
209
				foreach ($delete_targets as $dts) {
210
					if(is_ipaddrv6($dts))
211
						$family = "-inet6";
212
					$toapplylist[] = "/sbin/route delete {$family} {$dts}";
213
				}
214
		}
215
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
216
		staticroutes_sort();
217

    
218
		mark_subsystem_dirty('staticroutes');
219

    
220
		write_config();
221

    
222
		header("Location: system_routes.php");
223
		exit;
224
	}
225
}
226

    
227
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
228
$shortcut_section = "routing";
229
include("head.inc");
230
?>
231

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