Project

General

Profile

Download (15.5 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("guiconfig.inc");
56

    
57
if (!is_array($config['staticroutes']['route']))
58
	$config['staticroutes']['route'] = array();
59

    
60
$a_routes = &$config['staticroutes']['route'];
61
$a_gateways = return_gateways_array(true);
62

    
63
$id = $_GET['id'];
64
if (isset($_POST['id']))
65
	$id = $_POST['id'];
66

    
67
if (isset($_GET['dup'])) {
68
	$id = $_GET['dup'];
69
}
70

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

    
79
if (isset($_GET['dup']))
80
	unset($id);
81

    
82
if ($_POST) {
83

    
84
	unset($input_errors);
85
	$pconfig = $_POST;
86

    
87
	/* input validation */
88
	$reqdfields = explode(" ", "network network_subnet gateway");
89
	$reqdfieldsn = explode(",",
90
			gettext("Destination network") . "," .
91
			gettext("Destination network bit count") . "," .
92
			gettext("Gateway"));		
93
	
94
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
95
	
96
	if (($_POST['network'] && !is_ipaddr($_POST['network']) && !is_alias($_POST['network']))) {
97
		$input_errors[] = gettext("A valid IPv4 or IPv6 destination network must be specified.");
98
	}
99
	if (($_POST['network_subnet'] && !is_numeric($_POST['network_subnet']))) {
100
		$input_errors[] = gettext("A valid destination network bit count must be specified.");
101
	}
102
	if (($_POST['gateway']) && is_ipaddr($_POST['network'])) {
103
		if (!isset($a_gateways[$_POST['gateway']]))
104
			$input_errors[] = gettext("A valid gateway must be specified.");
105
		if(!validate_address_family($_POST['network'], lookup_gateway_ip_by_name($_POST['gateway'])))
106
			$input_errors[] = gettext("The gateway '{$a_gateways[$_POST['gateway']]['gateway']}' is a different Address Family as network '{$_POST['network']}'.");
107
	}
108

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

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

    
161
	if (!$input_errors) {
162
		$route = array();
163
		$route['network'] = $osn;
164
		$route['gateway'] = $_POST['gateway'];
165
		$route['descr'] = $_POST['descr'];
166
		if ($_POST['disabled'])
167
			$route['disabled'] = true;
168
		else
169
			unset($route['disabled']);
170

    
171
		if (file_exists("{$g['tmp_path']}/.system_routes.apply"))
172
			$toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
173
		else
174
			$toapplylist = array();
175
		$a_routes[$id] = $route;
176

    
177
		if (!empty($oroute)) {
178
			$delete_targets = array_diff($old_targets, $new_targets);
179
			if (count($delete_targets))
180
				foreach ($delete_targets as $dts) {
181
					if(is_ipaddrv6($dts))
182
						$family = "-inet6";
183
					$toapplylist[] = "/sbin/route delete {$family} {$dts}"; 
184
				}
185
		}
186
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
187
		staticroutes_sort();
188
		
189
		mark_subsystem_dirty('staticroutes');
190
		
191
		write_config();
192
		
193
		header("Location: system_routes.php");
194
		exit;
195
	}
196
}
197

    
198
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
199
include("head.inc");
200
?>
201

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

    
394
				</script>
395
<?php include("fend.inc"); ?>
396
</body>
397
</html>
(219-219/247)