Project

General

Profile

Download (19.9 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['weight'] = $a_gateways[$id]['weight'];
68
	$pconfig['interface'] = $a_gateways[$id]['interface'];
69
	$pconfig['friendlyiface'] = $a_gateways[$id]['friendlyiface'];
70
	$pconfig['gateway'] = $a_gateways[$id]['gateway'];
71
	$pconfig['defaultgw'] = isset($a_gateways[$id]['defaultgw']);
72
	$pconfig['latencylow'] = $a_gateway_item[$id]['latencylow'];
73
        $pconfig['latencyhigh'] = $a_gateway_item[$id]['latencyhigh'];
74
        $pconfig['losslow'] = $a_gateway_item[$id]['losslow'];
75
        $pconfig['losshigh'] = $a_gateway_item[$id]['losshigh'];
76
        $pconfig['down'] = $a_gateway_item[$id]['down'];
77
	if (isset($a_gateways[$id]['dynamic']))
78
		$pconfig['dynamic'] = true;
79
	if(($a_gateways[$id]['monitor'] <> "") && ($a_gateways[$id]['attribute'] != "system") && ($a_gateways[$id]['gateway'] != "dynamic")) {
80
		$pconfig['monitor'] = $a_gateways[$id]['monitor'];
81
	} else {
82
		$pconfig['monitor'] == "";
83
	}
84
	$pconfig['descr'] = $a_gateways[$id]['descr'];
85
	$pconfig['attribute'] = $a_gateways[$id]['attribute'];
86
}
87

    
88
if (isset($_GET['dup'])) {
89
	unset($id);
90
	unset($pconfig['attribute']);
91
}
92

    
93
if ($_POST) {
94

    
95
	unset($input_errors);
96

    
97
	/* input validation */
98
	$reqdfields = explode(" ", "name");
99
	$reqdfieldsn = array(gettext("Name"));
100

    
101
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
102

    
103
	if (! isset($_POST['name'])) {
104
		$input_errors[] = "A valid gateway name must be specified.";
105
	}
106
	if (! is_validaliasname($_POST['name'])) {
107
		$input_errors[] = gettext("The gateway name must not contain invalid characters.");
108
	}
109
	/* skip system gateways which have been automatically added */
110
	if (($_POST['gateway'] && (!is_ipaddr($_POST['gateway'])) && ($_POST['attribute'] != "system")) && ($_POST['gateway'] != "dynamic")) {
111
		$input_errors[] = gettext("A valid gateway IP address must be specified.");
112
	}
113

    
114
	if ($_POST['gateway'] && (is_ipaddr($_POST['gateway'])) && ($pconfig['attribute'] != "system") && !$_REQUEST['isAjax']) {
115
		$parent_ip = get_interface_ip($_POST['interface']);
116
		if (is_ipaddr($parent_ip)) {
117
			$parent_sn = get_interface_subnet($_POST['interface']);
118
			if(!ip_in_subnet($_POST['gateway'], gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn)) {
119
				$input_errors[] = sprintf(gettext("The gateway address %s does not lie within the chosen interface's subnet."), $_POST['gateway']);
120
			}
121
		}
122
	}
123
	if (($_POST['monitor'] <> "") && !is_ipaddr($_POST['monitor']) && $_POST['monitor'] != "dynamic") {
124
		$input_errors[] = gettext("A valid monitor IP address must be specified.");
125
	}
126

    
127
	if (isset($_POST['name'])) {
128
		/* check for overlaps */
129
		foreach ($a_gateways as $gateway) {
130
			if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
131
				continue;
132
			}
133
			if($_POST['name'] <> "") {
134
				if (($gateway['name'] <> "") && ($_POST['name'] == $gateway['name']) && ($gateway['attribute'] != "system")) {
135
					$input_errors[] = sprintf(gettext('The gateway name "%s" already exists.'), $_POST['name']);
136
					break;
137
				}
138
			}
139
			if(is_ipaddr($_POST['gateway'])) {
140
				if (($gateway['gateway'] <> "") && ($_POST['gateway'] == $gateway['gateway']) && ($gateway['attribute'] != "system")) {
141
					$input_errors[] = sprintf(gettext('The gateway IP address "%s" already exists.'), $_POST['gateway']);
142
					break;
143
				}
144
			}
145
			if(is_ipaddr($_POST['monitor'])) {
146
				if (($gateway['monitor'] <> "") && ($_POST['monitor'] == $gateway['monitor']) && ($gateway['attribute'] != "system")) {
147
					$input_errors[] = sprintf(gettext('The monitor IP address "%s" is already in use. You must choose a different monitor IP.'), $_POST['monitor']);
148
					break;
149
				}
150
			}
151
		}
152
	}
153

    
154
	/* input validation */
155
        if($_POST['latencylow']) {
156
                if (! is_numeric($_POST['latencylow'])) {
157
                        $input_errors[] = gettext("The low latency watermark needs to be a numeric value.");
158
                }
159
        }
160

    
161
        if($_POST['latencyhigh']) {
162
                if (! is_numeric($_POST['latencyhigh'])) {
163
                        $input_errors[] = gettext("The high latency watermark needs to be a numeric value.");
164
                }
165
        }
166
        if($_POST['losslow']) {
167
                if (! is_numeric($_POST['losslow'])) {
168
                        $input_errors[] = gettext("The low loss watermark needs to be a numeric value.");
169
                }
170
        }
171
        if($_POST['losshigh']) {
172
                if (! is_numeric($_POST['losshigh'])) {
173
                        $input_errors[] = gettext("The high loss watermark needs to be a numeric value.");
174
                }
175
        }
176

    
177
        if(($_POST['latencylow']) && ($_POST['latencyhigh'])){
178
                if(($_POST['latencylow'] > $_POST['latencyhigh'])) {
179
                        $input_errors[] = gettext("The High latency watermark needs to be higher then the low latency watermark");
180
                }
181
        }
182

    
183
        if(($_POST['losslow']) && ($_POST['losshigh'])){
184
                if($_POST['losslow'] > $_POST['losshigh']) {
185
                        $input_errors[] = gettext("The High packet loss watermark needs to be higher then the low packet loss watermark");
186
                }
187
        }
188
	if($_POST['down']) {
189
                if (! is_numeric($_POST['down']) || $_POST['down'] < 1) {
190
                        $input_errors[] = gettext("The low latency watermark needs to be a numeric value.");
191
                }
192
        }
193

    
194
	if (!$input_errors) {
195
		$reloadif = false;
196
		$save = false;
197
		if ($_POST['weight'] > 1 || $_POST['latencylow'] || $_POST['latencyhigh'] || $_POST['losslow'] || $_POST['losshigh'] || $_POST['down'] ||
198
		    $_POST['defaultgw'])
199
			$save = true;
200
		/* if we are processing a system gateway only save the monitorip */
201
		if (!$save && empty($_POST['interface']) && empty($_POST['gateway'])) {
202
			if (is_ipaddr($_POST['monitor'])) {
203
				if (empty($_POST['interface']))
204
					$interface = $pconfig['friendlyiface'];
205
				else
206
					$interface = $_POST['interface'];
207
				$config['interfaces'][$interface]['monitorip'] = $_POST['monitor'];
208
			}
209
			/* when dynamic gateway is not anymore a default the entry is no more needed. */
210
                        if (isset($id) && $a_gateway_item[$id]) {
211
                                unset($a_gateway_item[$id]);
212
                        }
213
		} else {
214

    
215
			/* Manual gateways are handled differently */
216
			/* rebuild the array with the manual entries only */
217

    
218
			$gateway = array();
219
			if ($_POST['attribute'] == "system") {
220
				$gateway['interface'] = $pconfig['friendlyiface'];
221
				$gateway['gateway'] = "dynamic";
222
			} else {
223
				$gateway['interface'] = $_POST['interface'];
224
				$gateway['gateway'] = $_POST['gateway'];
225
			}
226
			$gateway['name'] = $_POST['name'];
227
			$gateway['weight'] = $_POST['weight'];
228
			$gateway['descr'] = $_POST['descr'];
229
			if(is_ipaddr($_POST['monitor'])) {
230
				$gateway['monitor'] = $_POST['monitor'];
231
			} else {
232
				unset($gateway['monitor']);
233
			}			
234
			if ($_POST['defaultgw'] == "yes" or $_POST['defaultgw'] == "on") {
235
				$i = 0;
236
				foreach($a_gateway_item as $gw) {
237
					unset($config['gateways']['gateway_item'][$i]['defaultgw']);
238
					$i++;
239
				}
240
				$gateway['defaultgw'] = true;
241
				$reloadif = true;
242
			} else {
243
				unset($gateway['defaultgw']);
244
			}
245

    
246
			if ($_POST['latencylow'])
247
				$gateway['latencylow'] = $_POST['latencylow'];
248
			if ($_POST['latencyhigh'])
249
                		$gateway['latencyhigh'] = $_POST['latencyhigh'];
250
			if ($_POST['losslow'])
251
               			$gateway['losslow'] = $_POST['losslow'];
252
			if ($_POST['losshigh'])
253
                		$gateway['losshigh'] = $_POST['losshigh'];
254
			if ($_POST['down'])
255
                		$gateway['down'] = $_POST['down'];
256

    
257
			/* when saving the manual gateway we use the attribute which has the corresponding id */
258
			if (isset($id) && $a_gateway_item[$id]) {
259
				$a_gateway_item[$id] = $gateway;
260
			} else {
261
				$a_gateway_item[] = $gateway;
262
			}
263
		}
264
		system_resolvconf_generate();
265
		mark_subsystem_dirty('staticroutes');
266
		
267
		write_config();
268
		
269
		if($_REQUEST['isAjax']) {
270
			echo $_POST['name'];
271
			exit;
272
		} else if ($reloadif == true)
273
			interface_configure($_POST['interface']);
274
		
275
		header("Location: system_gateways.php");
276
		exit;
277
	}  else {
278
		$pconfig = $_POST;
279
		if (empty($_POST['friendlyiface']))
280
			$pconfig['friendlyiface'] = $_POST['interface'];
281
	}
282
}
283

    
284

    
285
$pgtitle = array(gettext("System"),gettext("Gateways"),gettext("Edit gateway"));
286
$statusurl = "status_gateways.php";
287

    
288
include("head.inc");
289

    
290
?>
291

    
292
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
293
<?php include("fbegin.inc"); ?>
294
<script language="JavaScript">
295
function enable_change(obj) {
296
	if (document.iform.gateway.disabled) {
297
		if (obj.checked)
298
			document.iform.interface.disabled=false;
299
		else
300
			document.iform.interface.disabled=true;
301
	}	
302
	
303
}
304
function show_advanced_gateway() {
305
        document.getElementById("showadvgatewaybox").innerHTML='';
306
        aodiv = document.getElementById('showgatewayadv');
307
        aodiv.style.display = "block";
308
}
309
</script>
310
<?php if ($input_errors) print_input_errors($input_errors); ?>
311
            <form action="system_gateways_edit.php" method="post" name="iform" id="iform">
312
	<?php
313

    
314
	/* If this is a system gateway we need this var */
315
	if(($pconfig['attribute'] == "system") || is_numeric($pconfig['attribute'])) {
316
		echo "<input type='hidden' name='attribute' id='attribute' value='{$pconfig['attribute']}' >\n";
317
	}
318
	echo "<input type='hidden' name='friendlyiface' id='friendlyiface' value='{$pconfig['friendlyiface']}' >\n";
319
	?>
320
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
321
				<tr>
322
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Edit gateway"); ?></td>
323
				</tr>	
324
                <tr> 
325
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("Interface"); ?></td>
326
                  <td width="78%" class="vtable">
327
				  <select name="interface" class="formselect" <?php if ($pconfig['dynamic'] == true && $pconfig['attribute'] == "system") echo "disabled"; ?>>
328
		<?php 
329
                      	$interfaces = get_configured_interface_with_descr(false, true);
330
			foreach ($interfaces as $iface => $ifacename) {
331
				echo "<option value=\"{$iface}\"";
332
				if ($iface == $pconfig['friendlyiface'])
333
					echo " selected";
334
				echo ">" . htmlspecialchars($ifacename) . "</option>";
335
			}
336
			if (is_package_installed("openbgpd") == 1) {
337
				echo "<option value=\"bgpd\"";
338
				if ($pconfig['interface'] == "bgpd") 
339
					echo " selected";
340
				echo ">" . gettext("Use BGPD") . "</option>";
341
			}
342
 		  ?>
343
                    </select> <br>
344
                    <span class="vexpl"><?=gettext("Choose which interface this gateway applies to."); ?></span></td>
345
                </tr>
346
                <tr>
347
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("Name"); ?></td>
348
                  <td width="78%" class="vtable"> 
349
                    <input name="name" type="text" class="formfld unknown" id="name" size="20" value="<?=htmlspecialchars($pconfig['name']);?>"> 
350
                    <br> <span class="vexpl"><?=gettext("Gateway name"); ?></span></td>
351
                </tr>
352
		<tr>
353
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("Gateway"); ?></td>
354
                  <td width="78%" class="vtable"> 
355
                    <input name="gateway" type="text" class="formfld host" id="gateway" size="40" value="<?php if ($pconfig['dynamic']) echo "dynamic"; else echo $pconfig['gateway']; ?>" <?php if ($pconfig['dynamic'] && $pconfig['attribute'] == "system") echo "disabled"; ?>>
356
                    <br> <span class="vexpl"><?=gettext("Gateway IP address"); ?></span></td>
357
                </tr>
358
		<tr>
359
		  <td width="22%" valign="top" class="vncell"><?=gettext("Default Gateway"); ?></td>
360
		  <td width="78%" class="vtable">
361
			<input name="defaultgw" type="checkbox" id="defaultgw" value="yes" <?php if ($pconfig['defaultgw'] == true) echo "checked"; ?> onclick="enable_change(this)" />
362
			<strong><?=gettext("Default Gateway"); ?></strong><br />
363
			<?=gettext("This will select the above gateway as the default gateway"); ?>
364
		  </td>
365
		</tr>
366
		<tr>
367
		  <td width="22%" valign="top" class="vncell"><?=gettext("Monitor IP"); ?></td>
368
		  <td width="78%" class="vtable">
369
			<?php
370
				if(($pconfig['attribute'] == "system") && ($pconfig['gateway'] == "dynamic") && ($pconfig['monitor'] == "")) {
371
					$monitor = "";
372
				} else {
373
					$monitor = htmlspecialchars($pconfig['monitor']);
374
				}
375
			?>
376
			<input name="monitor" type="text" id="monitor" value="<?php echo $monitor; ?>" />
377
			<strong><?=gettext("Alternative monitor IP"); ?></strong> <br />
378
			<?gettext("Enter an alternative address here to be used to monitor the link. This is used for the " .
379
			"quality RRD graphs as well as the load balancer entries. Use this if the gateway does not respond " .
380
			"to ICMP echo requests (pings)"); ?>.</strong>
381
			<br />
382
		  </td>
383
		</tr>
384
		<tr>
385
		  <td width="22%" valign="top" class="vncell"><?=gettext("Advanced");?></td>
386
		  <td width="78%" class="vtable">
387
			<div id="showadvgatewaybox" <? if (!empty($pconfig['latencylow']) || !empty($pconfig['latencyhigh']) || !empty($pconfig['losslow']) || !empty($pconfig['losshigh']) || (isset($pconfig['weight']) && $pconfig['weight'] > 1)) echo "style='display:none'"; ?>>
388
				<input type="button" onClick="show_advanced_gateway()" value="Advanced"></input> - Show advanced option</a>
389
			</div>
390
			<div id="showgatewayadv" <? if (empty($pconfig['latencylow']) && empty($pconfig['latencyhigh']) && empty($pconfig['losslow']) && empty($pconfig['losshigh']) && (empty($pconfig['weight']) || $pconfig['weight'] == 1)) echo "style='display:none'"; ?>>
391
                        <table class="tabcont" width="100%" border="0" cellspacing="0" cellpadding="6">
392
			<tr>
393
                                <td width="22%" valign="top" class="vncellreq"><?=gettext("Weight");?></td>
394
                                <td width="78%" class="vtable">
395
					<select name='weight' class='formfldselect' id='weight'>
396
				<?php
397
					for ($i = 1; $i < 6; $i++) {
398
                                        	$selected = "";
399
                                        	if ($pconfig['weight'] == $i)
400
                                                	$selected = "selected";
401
                                        	echo "<option value='{$i}' {$selected} >{$i}</option>";
402
                                	}
403
				?>
404
					</select>
405
					<br /><?=gettext("Weight for this gateway when used in a Gateway Group.");?> <br />
406
		   		</td>
407
			</tr>
408
                        <tr>
409
                                <td width="22%" valign="top" class="vncellreq"><?=gettext("Latency thresholds");?></td>
410
                                <td width="78%" class="vtable">
411
                                <?=gettext("From");?>
412
                                    <input name="latencylow" type="text" class="formfld unknown" id="latencylow" size="2"
413
                                        value="<?=htmlspecialchars($pconfig['latencylow']);?>">
414
                                <?=gettext("To");?>
415
                                    <input name="latencyhigh" type="text" class="formfld unknown" id="latencyhigh" size="2"
416
                                        value="<?=htmlspecialchars($pconfig['latencyhigh']);?>">
417
                                    <br> <span class="vexpl"><?=gettext("These define the low and high water marks for latency in milliseconds.");?></span></td>
418
                                </td>
419
                        </tr>
420
                        <tr>
421
                                <td width="22%" valign="top" class="vncellreq"><?=gettext("Packet Loss thresholds");?></td>
422
                                <td width="78%" class="vtable">
423
                                <?=gettext("From");?>
424
                                    <input name="losslow" type="text" class="formfld unknown" id="losslow" size="2"
425
                                        value="<?=htmlspecialchars($pconfig['losslow']);?>">
426
                                <?=gettext("To");?>
427
                                    <input name="losshigh" type="text" class="formfld unknown" id="losshigh" size="2"
428
                                        value="<?=htmlspecialchars($pconfig['losshigh']);?>">
429
                                    <br> <span class="vexpl"><?=gettext("These define the low and high water marks for packet loss in %.");?></span></td>
430
                                </td>
431
                        </tr>
432
			<tr>
433
                                <td width="22%" valign="top" class="vncellreq"><?=gettext("Down");?></td>
434
                                <td width="78%" class="vtable">
435
                                    <input name="down" type="text" class="formfld unknown" id="down" size="2"
436
                                        value="<?=htmlspecialchars($pconfig['down']);?>">
437
                                    <br> <span class="vexpl"><?=gettext("This defines the down time for the alarm to fire, in seconds.");?></span></td>
438
                                </td>
439
                        </tr>
440
                        </table>
441
			</div>
442
		   </td>
443
		</tr>
444
		<tr>
445
                  <td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
446
                  <td width="78%" class="vtable"> 
447
                    <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
448
                    <br> <span class="vexpl"><?=gettext("You may enter a description here for your reference (not parsed)"); ?>.</span></td>
449
                </tr>
450
                <tr>
451
                  <td width="22%" valign="top">&nbsp;</td>
452
                  <td width="78%"> 
453
                    <input name="Submit" type="submit" class="formbtn" value="<?=gettext("Save");?>"> <input type="button" value="<?=gettext("Cancel");?>" class="formbtn"  onclick="history.back()">
454
                    <?php if (isset($id) && $a_gateways[$id]): ?>
455
                    <input name="id" type="hidden" value="<?=$id;?>">
456
                    <?php endif; ?>
457
                  </td>
458
                </tr>
459
              </table>
460
</form>
461
<?php include("fend.inc"); ?>
462
<script language="JavaScript">
463
enable_change(document.iform.defaultgw);
464
</script>
465
</body>
466
</html>
(188-188/221)