Project

General

Profile

Download (16.7 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
		foreach (filter_expand_alias_array($_POST['network']) as $tgt) {
129
			if (is_ipaddr($tgt))
130
				$tgt .= "/32";
131
			if (is_ipaddr($tgt))
132
				$tgt .= "/128";
133
			if (!is_subnet($tgt))
134
				continue;
135
			if (!is_subnetv6($tgt))
136
				continue;
137
			$new_targets[] = $tgt;
138
		}
139
	}
140
	if (!isset($id))
141
		$id = count($a_routes);
142
	$oroute = $a_routes[$id];
143
	$old_targets = array();
144
	if (!empty($oroute)) {
145
		if (is_alias($oroute['network'])) {
146
			foreach (filter_expand_alias_array($oroute['network']) as $tgt) {
147
				if (is_ipaddr($tgt))
148
					$tgt .= "/32";
149
				if (!is_subnet($tgt))
150
					continue;
151
				$old_targets[] = $tgt;
152
			}
153
		} else {
154
			$old_targets[] = $oroute['network'];
155
		}
156
	}
157

    
158
	$overlaps = array_intersect($current_targets, $new_targets);
159
	$overlaps = array_diff($overlaps, $old_targets);
160
	if (count($overlaps)) {
161
		$input_errors[] = gettext("A route to these destination networks already exists") . ": " . implode(", ", $overlaps);
162
	}
163

    
164
	if (is_array($config['interfaces'])) {
165
		foreach ($config['interfaces'] as $if) {
166
			if (is_ipaddrv4($_POST['network'])
167
				&& isset($if['ipaddr']) && isset($if['subnet'])
168
				&& is_ipaddrv4($if['ipaddr']) && is_numeric($if['subnet'])
169
				&& ($_POST['network_subnet'] == $if['subnet'])
170
				&& (gen_subnet($_POST['network'], $_POST['network_subnet']) == gen_subnet($if['ipaddr'], $if['subnet'])))
171
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
172

    
173
			else if (is_ipaddrv6($_POST['network'])
174
				&& isset($if['ipaddrv6']) && isset($if['subnetv6'])
175
				&& is_ipaddrv6($if['ipaddrv6']) && is_numeric($if['subnetv6'])
176
				&& ($_POST['network_subnet'] == $if['subnetv6'])
177
				&& (gen_subnetv6($_POST['network'], $_POST['network_subnet']) == gen_subnetv6($if['ipaddrv6'], $if['subnetv6'])))
178
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
179
		}
180
	}
181

    
182
	if (!$input_errors) {
183
		$route = array();
184
		$route['network'] = $osn;
185
		$route['gateway'] = $_POST['gateway'];
186
		$route['descr'] = $_POST['descr'];
187
		if ($_POST['disabled'])
188
			$route['disabled'] = true;
189
		else
190
			unset($route['disabled']);
191

    
192
		if (file_exists("{$g['tmp_path']}/.system_routes.apply"))
193
			$toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
194
		else
195
			$toapplylist = array();
196
		$a_routes[$id] = $route;
197

    
198
		if (!empty($oroute)) {
199
			$delete_targets = array_diff($old_targets, $new_targets);
200
			if (count($delete_targets))
201
				foreach ($delete_targets as $dts) {
202
					if(is_ipaddrv6($dts))
203
						$family = "-inet6";
204
					$toapplylist[] = "/sbin/route delete {$family} {$dts}"; 
205
				}
206
		}
207
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
208
		staticroutes_sort();
209
		
210
		mark_subsystem_dirty('staticroutes');
211
		
212
		write_config();
213
		
214
		header("Location: system_routes.php");
215
		exit;
216
	}
217
}
218

    
219
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
220
$shortcut_section = "routing";
221
include("head.inc");
222
?>
223

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