Project

General

Profile

Download (12.2 KB) Statistics
| Branch: | Tag: | Revision:
1 d173230c Seth Mos
<?php 
2 124aee67 Chris Buechler
/* $Id$ */
3 d173230c Seth Mos
/*
4
	system_gateways_edit.php
5
	part of pfSense (http://pfsense.com)
6
	
7
	Copyright (C) 2007 Seth Mos <seth.mos@xs4all.nl>.
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 1d333258 Scott Ullrich
/*
32
	pfSense_MODULE:	routing
33
*/
34 d173230c Seth Mos
35 6b07c15a Matthew Grooms
##|+PRIV
36
##|*IDENT=page-system-gateways-editgateway
37
##|*NAME=System: Gateways: Edit Gateway page
38
##|*DESCR=Allow access to the 'System: Gateways: Edit Gateway' page.
39
##|*MATCH=system_gateways_edit.php*
40
##|-PRIV
41
42 124aee67 Chris Buechler
require("guiconfig.inc");
43 4666b787 Ermal Lu?i
require("pkg-utils.inc");
44 d173230c Seth Mos
45 124aee67 Chris Buechler
$a_gateways = return_gateways_array();
46 87f0be87 Chris Buechler
$a_gateways_arr = array();
47
foreach($a_gateways as $gw) {
48
	$a_gateways_arr[] = $gw;
49
}
50
$a_gateways = $a_gateways_arr;
51 d173230c Seth Mos
52 616e1956 Seth Mos
if (!is_array($config['gateways']['gateway_item']))
53
        $config['gateways']['gateway_item'] = array();
54
        
55
$a_gateway_item = &$config['gateways']['gateway_item'];
56
57 87f0be87 Chris Buechler
$id = $_GET['id'];
58
if (isset($_POST['id']))
59 d173230c Seth Mos
	$id = $_POST['id'];
60
61
if (isset($_GET['dup'])) {
62
	$id = $_GET['dup'];
63
}
64
65
if (isset($id) && $a_gateways[$id]) {
66 e90db2b4 Ermal Lu?i
	$pconfig['name'] = $a_gateways[$id]['name'];
67
	$pconfig['interface'] = $a_gateways[$id]['interface'];
68 883c53c9 Ermal Lu?i
	$pconfig['friendlyiface'] = $a_gateways[$id]['friendlyiface'];
69 e90db2b4 Ermal Lu?i
	$pconfig['gateway'] = $a_gateways[$id]['gateway'];
70 883c53c9 Ermal Lu?i
	$pconfig['defaultgw'] = isset($a_gateways[$id]['defaultgw']);
71 a2532739 Ermal Lu?i
	if (isset($a_gateways[$id]['dynamic']))
72
		$pconfig['dynamic'] = true;
73 883c53c9 Ermal Lu?i
	if($a_gateways[$id]['monitor'] <> "") {
74
		$pconfig['monitor'] = $a_gateways[$id]['monitor'];
75 616e1956 Seth Mos
	} else {
76
		$pconfig['monitor'] == "";
77
	}
78 e90db2b4 Ermal Lu?i
	$pconfig['descr'] = $a_gateways[$id]['descr'];
79
	$pconfig['attribute'] = $a_gateways[$id]['attribute'];
80 d173230c Seth Mos
}
81
82 124aee67 Chris Buechler
if (isset($_GET['dup'])) {
83 d173230c Seth Mos
	unset($id);
84 124aee67 Chris Buechler
	unset($pconfig['attribute']);
85
}
86 d173230c Seth Mos
87
if ($_POST) {
88
89
	unset($input_errors);
90
91
	/* input validation */
92 883c53c9 Ermal Lu?i
	$reqdfields = explode(" ", "name");
93
	$reqdfieldsn = explode(",", "Name");
94
95 d173230c Seth Mos
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
96 883c53c9 Ermal Lu?i
97 d173230c Seth Mos
	if (! isset($_POST['name'])) {
98
		$input_errors[] = "A valid gateway name must be specified.";
99
	}
100 31ace93c Seth Mos
	if (! is_validaliasname($_POST['name'])) {
101
		$input_errors[] = "The gateway name must not contain invalid characters.";
102
	}
103 87f0be87 Chris Buechler
	/* skip system gateways which have been automatically added */
104 d52b71c7 Ermal Lu?i
	if (($_POST['gateway'] && (!is_ipaddr($_POST['gateway'])) && ($_POST['attribute'] != "system")) && ($_POST['gateway'] != "dynamic")) {
105 87f0be87 Chris Buechler
		$input_errors[] = "A valid gateway IP address must be specified.";
106
	}
107 15a13dab Scott Ullrich
108
	if ($_POST['gateway'] && (is_ipaddr($_POST['gateway'])) && ($pconfig['attribute'] != "system") && !$_REQUEST['isAjax']) {
109 b29b1a33 Chris Buechler
		$parent_ip = get_interface_ip($_POST['interface']);
110 e227df57 Ermal Lu?i
		if (is_ipaddr($parent_ip)) {
111
			$parent_sn = get_interface_subnet($_POST['interface']);
112
			if(!ip_in_subnet($_POST['gateway'], gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn)) {
113
				$input_errors[] = "The gateway address {$_POST['gateway']} does not lie within the chosen interface's subnet.";
114
			}
115 e7d3b8f4 Seth Mos
		}
116
	}
117 883c53c9 Ermal Lu?i
	if (($_POST['monitor'] <> "") && !is_ipaddr($_POST['monitor']) && $_POST['monitor'] != "dynamic") {
118 87f0be87 Chris Buechler
		$input_errors[] = "A valid monitor IP address must be specified.";
119 d173230c Seth Mos
	}
120
121 8b060357 Seth Mos
	if (isset($_POST['name'])) {
122 31ace93c Seth Mos
		/* check for overlaps */
123
		foreach ($a_gateways as $gateway) {
124 2328dcc5 Seth Mos
			if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
125 31ace93c Seth Mos
				continue;
126
			}
127 2328dcc5 Seth Mos
			if($_POST['name'] <> "") {
128 883c53c9 Ermal Lu?i
				if (($gateway['name'] <> "") && ($_POST['name'] == $gateway['name']) && ($gateway['attribute'] != "system")) {
129 2328dcc5 Seth Mos
					$input_errors[] = "The gateway name \"{$_POST['name']}\" already exists.";
130
					break;
131
				}
132
			}
133
			if(is_ipaddr($_POST['gateway'])) {
134 883c53c9 Ermal Lu?i
				if (($gateway['gateway'] <> "") && ($_POST['gateway'] == $gateway['gateway']) && ($gateway['attribute'] != "system")) {
135 2328dcc5 Seth Mos
					$input_errors[] = "The gateway IP address \"{$_POST['gateway']}\" already exists.";
136
					break;
137
				}
138 31ace93c Seth Mos
			}
139 2328dcc5 Seth Mos
			if(is_ipaddr($_POST['monitor'])) {
140 883c53c9 Ermal Lu?i
				if (($gateway['monitor'] <> "") && ($_POST['monitor'] == $gateway['monitor']) && ($gateway['attribute'] != "system")) {
141 2328dcc5 Seth Mos
					$input_errors[] = "The monitor IP address \"{$_POST['monitor']}\" is already in use. You must choose a different monitor IP.";
142
					break;
143
				}
144 31ace93c Seth Mos
			}
145 d173230c Seth Mos
		}
146
	}
147
148
	if (!$input_errors) {
149 124aee67 Chris Buechler
		/* if we are processing a system gateway only save the monitorip */
150 883c53c9 Ermal Lu?i
		if(($_POST['attribute'] == "system" && empty($_POST['defaultgw'])) || (empty($_POST['interface']) && empty($_POST['gateway']) && empty($_POST['defaultgw']))) {
151
			if (is_ipaddr($_POST['monitor'])) {
152
				if (empty($_POST['interface']))
153
					$interface = $pconfig['interface'];
154
				else
155
					$interface = $_POST['interface'];
156
				$config['interfaces'][$interface]['monitorip'] = $_POST['monitor'];
157
			}
158
			/* when dynamic gateway is not anymore a default the entry is no more needed. */
159
                        if (isset($id) && $a_gateway_item[$id]) {
160
                                unset($a_gateway_item[$id]);
161
                        }
162
		} else {
163 124aee67 Chris Buechler
164 883c53c9 Ermal Lu?i
			/* Manual gateways are handled differently */
165
			/* rebuild the array with the manual entries only */
166 124aee67 Chris Buechler
167 c0f5182c Ermal Lu?i
			$reloadif = false;
168 124aee67 Chris Buechler
			$gateway = array();
169
			$gateway['interface'] = $_POST['interface'];
170
			$gateway['name'] = $_POST['name'];
171
			$gateway['gateway'] = $_POST['gateway'];
172
			$gateway['descr'] = $_POST['descr'];
173 616e1956 Seth Mos
			if(is_ipaddr($_POST['monitor'])) {
174
				$gateway['monitor'] = $_POST['monitor'];
175
			} else {
176
				unset($gateway['monitor']);
177
			}			
178 b547deb6 Scott Ullrich
			if ($_POST['defaultgw'] == "yes" or $_POST['defaultgw'] == "on") {
179 87f0be87 Chris Buechler
				$i = 0;
180 883c53c9 Ermal Lu?i
				foreach($a_gateway_item as $gw) {
181
					unset($config['gateways']['gateway_item'][$i]['defaultgw']);
182 124aee67 Chris Buechler
					$i++;
183
				}
184 87f0be87 Chris Buechler
				$gateway['defaultgw'] = true;
185 c0f5182c Ermal Lu?i
				$reloadif = true;
186 87f0be87 Chris Buechler
			} else {
187
				unset($gateway['defaultgw']);
188 124aee67 Chris Buechler
			}
189
190
			/* when saving the manual gateway we use the attribute which has the corresponding id */
191 883c53c9 Ermal Lu?i
			if (isset($id) && $a_gateway_item[$id]) {
192
				$a_gateway_item[$id] = $gateway;
193 124aee67 Chris Buechler
			} else {
194 883c53c9 Ermal Lu?i
				$a_gateway_item[] = $gateway;
195 124aee67 Chris Buechler
			}
196
		}
197 e4749e66 Seth Mos
		system_resolvconf_generate();
198 a368a026 Ermal Lu?i
		mark_subsystem_dirty('staticroutes');
199 d173230c Seth Mos
		
200
		write_config();
201
		
202 ad862220 Charlie
		if($_REQUEST['isAjax']) {
203
			echo $_POST['name'];
204
			exit;
205 c0f5182c Ermal Lu?i
		} else if ($reloadif == true)
206
			interface_configure($_POST['interface']);
207 ad862220 Charlie
		
208 d173230c Seth Mos
		header("Location: system_gateways.php");
209
		exit;
210 5313aea5 Ermal Lu?i
	}  else {
211 883c53c9 Ermal Lu?i
		$pconfig = $_POST;
212 5313aea5 Ermal Lu?i
		if (empty($_POST['friendlyiface']))
213
			$pconfig['friendlyiface'] = $_POST['interface'];
214
	}
215 d173230c Seth Mos
}
216
217 2328dcc5 Seth Mos
218 d88c6a9f Scott Ullrich
$pgtitle = array("System","Gateways","Edit gateway");
219 d173230c Seth Mos
include("head.inc");
220
221
?>
222
223
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
224
<?php include("fbegin.inc"); ?>
225 883c53c9 Ermal Lu?i
<script language="JavaScript">
226
function enable_change(obj) {
227
	if (document.iform.gateway.disabled) {
228
		if (obj.checked)
229
			document.iform.interface.disabled=false;
230
		else
231
			document.iform.interface.disabled=true;
232
	}	
233
	
234
}
235
</script>
236 d173230c Seth Mos
<?php if ($input_errors) print_input_errors($input_errors); ?>
237
            <form action="system_gateways_edit.php" method="post" name="iform" id="iform">
238 124aee67 Chris Buechler
	<?php
239 e9df5769 Seth Mos
240
	/* If this is a system gateway we need this var */
241 124aee67 Chris Buechler
	if(($pconfig['attribute'] == "system") || is_numeric($pconfig['attribute'])) {
242
		echo "<input type='hidden' name='attribute' id='attribute' value='{$pconfig['attribute']}' >\n";
243
	}
244 883c53c9 Ermal Lu?i
	echo "<input type='hidden' name='friendlyiface' id='friendlyiface' value='{$pconfig['friendlyiface']}' >\n";
245 124aee67 Chris Buechler
	?>
246 d173230c Seth Mos
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
247 56d4e7fb Scott Ullrich
				<tr>
248
					<td colspan="2" valign="top" class="listtopic">Edit gateway</td>
249
				</tr>	
250 d173230c Seth Mos
                <tr> 
251
                  <td width="22%" valign="top" class="vncellreq">Interface</td>
252
                  <td width="78%" class="vtable">
253 d9a2ff01 Ermal Lu?i
				  <select name="interface" class="formselect" <?php if ($pconfig['dynamic'] == true && $pconfig['attribute'] == "system") echo "disabled"; ?>>
254 883c53c9 Ermal Lu?i
		<?php 
255
                      	$interfaces = get_configured_interface_with_descr(false, true);
256
			foreach ($interfaces as $iface => $ifacename) {
257
				echo "<option value=\"{$iface}\"";
258
				if ($iface == $pconfig['friendlyiface'])
259
					echo " selected";
260
				echo ">" . htmlspecialchars($ifacename) . "</option>";
261
			}
262
			if (is_package_installed("openbgpd") == 1) {
263
				echo "<option value=\"bgpd\"";
264
				if ($pconfig['interface'] == "bgpd") 
265
					echo " selected";
266
				echo ">Use BGPD</option>";
267
			}
268
 		  ?>
269 d173230c Seth Mos
                    </select> <br>
270
                    <span class="vexpl">Choose which interface this gateway applies to.</span></td>
271
                </tr>
272
                <tr>
273
                  <td width="22%" valign="top" class="vncellreq">Name</td>
274
                  <td width="78%" class="vtable"> 
275
                    <input name="name" type="text" class="formfld unknown" id="name" size="20" value="<?=htmlspecialchars($pconfig['name']);?>"> 
276
                    <br> <span class="vexpl">Gateway name</span></td>
277
                </tr>
278
		<tr>
279
                  <td width="22%" valign="top" class="vncellreq">Gateway</td>
280
                  <td width="78%" class="vtable"> 
281 d9a2ff01 Ermal Lu?i
                    <input name="gateway" type="text" class="formfld host" id="gateway" size="40" value="<?php echo $pconfig['gateway']; ?>" <?php if ($pconfig['dynamic'] == true && $pconfig['attribute'] == "system") echo "disabled"; ?>>
282 87f0be87 Chris Buechler
                    <br> <span class="vexpl">Gateway IP address</span></td>
283 d173230c Seth Mos
                </tr>
284
		<tr>
285
		  <td width="22%" valign="top" class="vncell">Default Gateway</td>
286
		  <td width="78%" class="vtable">
287 883c53c9 Ermal Lu?i
			<input name="defaultgw" type="checkbox" id="defaultgw" value="yes" <?php if ($pconfig['defaultgw'] == true) echo "checked"; ?> onclick="enable_change(this)" />
288 d173230c Seth Mos
			<strong>Default Gateway</strong><br />
289
			This will select the above gateway as the default gateway
290
		  </td>
291
		</tr>
292
		<tr>
293
		  <td width="22%" valign="top" class="vncell">Monitor IP</td>
294
		  <td width="78%" class="vtable">
295 616e1956 Seth Mos
			<?php
296
				if(is_numeric($pconfig['attribute']) && ($pconfig['gateway'] == dynamic) && ($pconfig['monitor'] == "")) {
297
					$monitor = "";
298
				} else {
299
					$monitor = htmlspecialchars($pconfig['monitor']);
300
				}
301
			?>
302
			<input name="monitor" type="text" id="monitor" value="<?php echo $monitor; ?>" />
303 d173230c Seth Mos
			<strong>Alternative monitor IP</strong> <br />
304 9c6ea412 Chris Buechler
			Enter an alternative address here to be used to monitor the link. This is used for the
305 d173230c Seth Mos
			quality RRD graphs as well as the load balancer entries. Use this if the gateway does not respond
306 9c6ea412 Chris Buechler
			to ICMP echo requests (pings).</strong>
307 d173230c Seth Mos
			<br />
308
		  </td>
309
		</tr>
310
		<tr>
311
                  <td width="22%" valign="top" class="vncell">Description</td>
312
                  <td width="78%" class="vtable"> 
313
                    <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
314
                    <br> <span class="vexpl">You may enter a description here
315
                    for your reference (not parsed).</span></td>
316
                </tr>
317
                <tr>
318
                  <td width="22%" valign="top">&nbsp;</td>
319
                  <td width="78%"> 
320
                    <input name="Submit" type="submit" class="formbtn" value="Save"> <input type="button" value="Cancel" class="formbtn"  onclick="history.back()">
321
                    <?php if (isset($id) && $a_gateways[$id]): ?>
322
                    <input name="id" type="hidden" value="<?=$id;?>">
323
                    <?php endif; ?>
324
                  </td>
325
                </tr>
326
              </table>
327
</form>
328
<?php include("fend.inc"); ?>
329
<script language="JavaScript">
330 883c53c9 Ermal Lu?i
enable_change(document.iform.defaultgw);
331 d173230c Seth Mos
</script>
332
</body>
333
</html>