Project

General

Profile

Download (13.8 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
}
77

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

    
81
if ($_POST) {
82

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

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

    
108
	/* check for overlaps */
109
	if(is_ipaddrv6($_POST['network'])) {
110
		$osn = Net_IPv6::compress(gen_subnetv6($_POST['network'], $_POST['network_subnet'])) . "/" . $_POST['network_subnet'];
111
	}
112
	if(is_ipaddrv4($_POST['network'])) {
113
		if($_POST['network_subnet'] > 32)
114
			$input_errors[] = gettext("A IPv4 subnet can not be over 32 bits.");
115
		else
116
			$osn = gen_subnet($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
117
	}
118
	foreach ($a_routes as $route) {
119
		if (isset($id) && ($a_routes[$id]) && ($a_routes[$id] === $route))
120
			continue;
121

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

    
128
	if (!$input_errors) {
129
		$route = array();
130
		$route['network'] = $osn;
131
		$route['gateway'] = $_POST['gateway'];
132
		$route['descr'] = $_POST['descr'];
133

    
134
		if (!isset($id))
135
                        $id = count($a_routes);
136
                if (file_exists("{$g['tmp_path']}/.system_routes.apply"))
137
                        $toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
138
                else
139
                        $toapplylist = array();
140
                $oroute = $a_routes[$id];
141

    
142
		$a_routes[$id] = $route;
143

    
144
		if (!empty($oroute)) {
145
			$osn = explode('/', $oroute['network']);
146
			$sn = explode('/', $route['network']);
147
			if ($oroute['network'] <> $route['network'])
148
				$toapplylist[] = "/sbin/route delete {$oroute['network']}"; 
149
		}
150
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
151
		staticroutes_sort();
152
		
153
		mark_subsystem_dirty('staticroutes');
154
		
155
		write_config();
156
		
157
		header("Location: system_routes.php");
158
		exit;
159
	}
160
}
161

    
162
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
163
include("head.inc");
164

    
165
?>
166

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