Project

General

Profile

Download (12.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php 
2
/* $Id$ */
3
/*
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
/*
32
	pfSense_MODULE:	routing
33
*/
34

    
35
##|+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
require("guiconfig.inc");
43
require("pkg-utils.inc");
44

    
45
$a_gateways = return_gateways_array();
46
$a_gateways_arr = array();
47
foreach($a_gateways as $gw) {
48
	$a_gateways_arr[] = $gw;
49
}
50
$a_gateways = $a_gateways_arr;
51

    
52
if (!is_array($config['gateways']['gateway_item']))
53
        $config['gateways']['gateway_item'] = array();
54
        
55
$a_gateway_item = &$config['gateways']['gateway_item'];
56

    
57
$id = $_GET['id'];
58
if (isset($_POST['id']))
59
	$id = $_POST['id'];
60

    
61
if (isset($_GET['dup'])) {
62
	$id = $_GET['dup'];
63
}
64

    
65
if (isset($id) && $a_gateways[$id]) {
66
	$pconfig['name'] = $a_gateways[$id]['name'];
67
	$pconfig['interface'] = $a_gateways[$id]['interface'];
68
	$pconfig['friendlyiface'] = $a_gateways[$id]['friendlyiface'];
69
	$pconfig['gateway'] = $a_gateways[$id]['gateway'];
70
	$pconfig['defaultgw'] = isset($a_gateways[$id]['defaultgw']);
71
	if (isset($a_gateways[$id]['dynamic']))
72
		$pconfig['dynamic'] = true;
73
	if($a_gateways[$id]['monitor'] <> "") {
74
		$pconfig['monitor'] = $a_gateways[$id]['monitor'];
75
	} else {
76
		$pconfig['monitor'] == "";
77
	}
78
	$pconfig['descr'] = $a_gateways[$id]['descr'];
79
	$pconfig['attribute'] = $a_gateways[$id]['attribute'];
80
}
81

    
82
if (isset($_GET['dup'])) {
83
	unset($id);
84
	unset($pconfig['attribute']);
85
}
86

    
87
if ($_POST) {
88

    
89
	unset($input_errors);
90

    
91
	/* input validation */
92
	$reqdfields = explode(" ", "name");
93
	$reqdfieldsn = explode(",", "Name");
94

    
95
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
96

    
97
	if (! isset($_POST['name'])) {
98
		$input_errors[] = "A valid gateway name must be specified.";
99
	}
100
	if (! is_validaliasname($_POST['name'])) {
101
		$input_errors[] = "The gateway name must not contain invalid characters.";
102
	}
103
	/* skip system gateways which have been automatically added */
104
	if (($_POST['gateway'] && (!is_ipaddr($_POST['gateway'])) && ($_POST['attribute'] != "system")) && ($_POST['gateway'] != "dynamic")) {
105
		$input_errors[] = "A valid gateway IP address must be specified.";
106
	}
107

    
108
	if ($_POST['gateway'] && (is_ipaddr($_POST['gateway'])) && ($pconfig['attribute'] != "system") && !$_REQUEST['isAjax']) {
109
		$parent_ip = get_interface_ip($_POST['interface']);
110
		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
		}
116
	}
117
	if (($_POST['monitor'] <> "") && !is_ipaddr($_POST['monitor']) && $_POST['monitor'] != "dynamic") {
118
		$input_errors[] = "A valid monitor IP address must be specified.";
119
	}
120

    
121
	if (isset($_POST['name'])) {
122
		/* check for overlaps */
123
		foreach ($a_gateways as $gateway) {
124
			if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
125
				continue;
126
			}
127
			if($_POST['name'] <> "") {
128
				if (($gateway['name'] <> "") && ($_POST['name'] == $gateway['name']) && ($gateway['attribute'] != "system")) {
129
					$input_errors[] = "The gateway name \"{$_POST['name']}\" already exists.";
130
					break;
131
				}
132
			}
133
			if(is_ipaddr($_POST['gateway'])) {
134
				if (($gateway['gateway'] <> "") && ($_POST['gateway'] == $gateway['gateway']) && ($gateway['attribute'] != "system")) {
135
					$input_errors[] = "The gateway IP address \"{$_POST['gateway']}\" already exists.";
136
					break;
137
				}
138
			}
139
			if(is_ipaddr($_POST['monitor'])) {
140
				if (($gateway['monitor'] <> "") && ($_POST['monitor'] == $gateway['monitor']) && ($gateway['attribute'] != "system")) {
141
					$input_errors[] = "The monitor IP address \"{$_POST['monitor']}\" is already in use. You must choose a different monitor IP.";
142
					break;
143
				}
144
			}
145
		}
146
	}
147

    
148
	if (!$input_errors) {
149
		$reloadif = false;
150
		/* if we are processing a system gateway only save the monitorip */
151
		if(($_POST['attribute'] == "system" && empty($_POST['defaultgw'])) || (empty($_POST['interface']) && empty($_POST['gateway']) && empty($_POST['defaultgw']))) {
152
			if (is_ipaddr($_POST['monitor'])) {
153
				if (empty($_POST['interface']))
154
					$interface = $pconfig['friendlyiface'];
155
				else
156
					$interface = $_POST['interface'];
157
				$config['interfaces'][$interface]['monitorip'] = $_POST['monitor'];
158
			}
159
			/* when dynamic gateway is not anymore a default the entry is no more needed. */
160
                        if (isset($id) && $a_gateway_item[$id]) {
161
                                unset($a_gateway_item[$id]);
162
                        }
163
		} else {
164

    
165
			/* Manual gateways are handled differently */
166
			/* rebuild the array with the manual entries only */
167

    
168
			$gateway = array();
169
			$gateway['interface'] = $_POST['interface'];
170
			$gateway['name'] = $_POST['name'];
171
			$gateway['gateway'] = $_POST['gateway'];
172
			$gateway['descr'] = $_POST['descr'];
173
			if(is_ipaddr($_POST['monitor'])) {
174
				$gateway['monitor'] = $_POST['monitor'];
175
			} else {
176
				unset($gateway['monitor']);
177
			}			
178
			if ($_POST['defaultgw'] == "yes" or $_POST['defaultgw'] == "on") {
179
				$i = 0;
180
				foreach($a_gateway_item as $gw) {
181
					unset($config['gateways']['gateway_item'][$i]['defaultgw']);
182
					$i++;
183
				}
184
				$gateway['defaultgw'] = true;
185
				$reloadif = true;
186
			} else {
187
				unset($gateway['defaultgw']);
188
			}
189

    
190
			/* when saving the manual gateway we use the attribute which has the corresponding id */
191
			if (isset($id) && $a_gateway_item[$id]) {
192
				$a_gateway_item[$id] = $gateway;
193
			} else {
194
				$a_gateway_item[] = $gateway;
195
			}
196
		}
197
		system_resolvconf_generate();
198
		mark_subsystem_dirty('staticroutes');
199
		
200
		write_config();
201
		
202
		if($_REQUEST['isAjax']) {
203
			echo $_POST['name'];
204
			exit;
205
		} else if ($reloadif == true)
206
			interface_configure($_POST['interface']);
207
		
208
		header("Location: system_gateways.php");
209
		exit;
210
	}  else {
211
		$pconfig = $_POST;
212
		if (empty($_POST['friendlyiface']))
213
			$pconfig['friendlyiface'] = $_POST['interface'];
214
	}
215
}
216

    
217

    
218
$pgtitle = array("System","Gateways","Edit gateway");
219
include("head.inc");
220

    
221
?>
222

    
223
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
224
<?php include("fbegin.inc"); ?>
225
<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
<?php if ($input_errors) print_input_errors($input_errors); ?>
237
            <form action="system_gateways_edit.php" method="post" name="iform" id="iform">
238
	<?php
239

    
240
	/* If this is a system gateway we need this var */
241
	if(($pconfig['attribute'] == "system") || is_numeric($pconfig['attribute'])) {
242
		echo "<input type='hidden' name='attribute' id='attribute' value='{$pconfig['attribute']}' >\n";
243
	}
244
	echo "<input type='hidden' name='friendlyiface' id='friendlyiface' value='{$pconfig['friendlyiface']}' >\n";
245
	?>
246
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
247
				<tr>
248
					<td colspan="2" valign="top" class="listtopic">Edit gateway</td>
249
				</tr>	
250
                <tr> 
251
                  <td width="22%" valign="top" class="vncellreq">Interface</td>
252
                  <td width="78%" class="vtable">
253
				  <select name="interface" class="formselect" <?php if ($pconfig['dynamic'] == true && $pconfig['attribute'] == "system") echo "disabled"; ?>>
254
		<?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
                    </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
                    <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
                    <br> <span class="vexpl">Gateway IP address</span></td>
283
                </tr>
284
		<tr>
285
		  <td width="22%" valign="top" class="vncell">Default Gateway</td>
286
		  <td width="78%" class="vtable">
287
			<input name="defaultgw" type="checkbox" id="defaultgw" value="yes" <?php if ($pconfig['defaultgw'] == true) echo "checked"; ?> onclick="enable_change(this)" />
288
			<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
			<?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
			<strong>Alternative monitor IP</strong> <br />
304
			Enter an alternative address here to be used to monitor the link. This is used for the
305
			quality RRD graphs as well as the load balancer entries. Use this if the gateway does not respond
306
			to ICMP echo requests (pings).</strong>
307
			<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
enable_change(document.iform.defaultgw);
331
</script>
332
</body>
333
</html>
(184-184/216)