Projet

Général

Profil

Télécharger (15,8 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / usr / local / www / system_routes_edit.php @ cac103c5

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
require_once("guiconfig.inc");
43
require_once("filter.inc");
44
require_once("util.inc");
45
require_once("gwlb.inc");
46

    
47
if (!is_array($config['staticroutes']['route']))
48
	$config['staticroutes']['route'] = array();
49

    
50
$a_routes = &$config['staticroutes']['route'];
51
$a_gateways = return_gateways_array(true, true);
52

    
53
if (is_numericint($_GET['id']))
54
	$id = $_GET['id'];
55
if (isset($_POST['id']) && is_numericint($_POST['id']))
56
	$id = $_POST['id'];
57

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

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

    
69
if (isset($_GET['dup']) && is_numericint($_GET['dup']))
70
	unset($id);
71

    
72
if ($_POST) {
73

    
74
	global $aliastable;
75

    
76
	unset($input_errors);
77
	$pconfig = $_POST;
78

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

    
86
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
87

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

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

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

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

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

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

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

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

    
200
		mark_subsystem_dirty('staticroutes');
201

    
202
		write_config();
203

    
204
		header("Location: system_routes.php");
205
		exit;
206
	}
207
}
208

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

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