Project

General

Profile

Download (14.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']))) {
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
	if(is_ipaddrv6($_POST['network'])) {
111
		$osn = Net_IPv6::compress(gen_subnetv6($_POST['network'], $_POST['network_subnet'])) . "/" . $_POST['network_subnet'];
112
	}
113
	if(is_ipaddrv4($_POST['network'])) {
114
		if($_POST['network_subnet'] > 32)
115
			$input_errors[] = gettext("A IPv4 subnet can not be over 32 bits.");
116
		else
117
			$osn = gen_subnet($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
118
	}
119
	foreach ($a_routes as $route) {
120
		if (isset($id) && ($a_routes[$id]) && ($a_routes[$id] === $route))
121
			continue;
122

    
123
		if ($route['network'] == $osn) {
124
			$input_errors[] = gettext("A route to this destination network already exists.");
125
			break;
126
		}
127
	}
128

    
129
	if (!$input_errors) {
130
		$route = array();
131
		$route['network'] = $osn;
132
		$route['gateway'] = $_POST['gateway'];
133
		$route['descr'] = $_POST['descr'];
134
		if ($_POST['disabled'])
135
			$route['disabled'] = true;
136
		else
137
			unset($route['disabled']);
138

    
139
		if (!isset($id))
140
                        $id = count($a_routes);
141
                if (file_exists("{$g['tmp_path']}/.system_routes.apply"))
142
                        $toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
143
                else
144
                        $toapplylist = array();
145
                $oroute = $a_routes[$id];
146

    
147
		$a_routes[$id] = $route;
148

    
149
		if (!empty($oroute)) {
150
			$osn = explode('/', $oroute['network']);
151
			$sn = explode('/', $route['network']);
152
			if ($oroute['network'] <> $route['network']) {
153
				if(is_ipaddrv6($oroute['network']))
154
					$family = "-inet6";
155
				$toapplylist[] = "/sbin/route delete {$family} {$oroute['network']}";
156
			}
157
		}
158
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
159
		staticroutes_sort();
160
		
161
		mark_subsystem_dirty('staticroutes');
162
		
163
		write_config();
164
		
165
		header("Location: system_routes.php");
166
		exit;
167
	}
168
}
169

    
170
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
171
include("head.inc");
172

    
173
?>
174

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