Project

General

Profile

Download (14.7 KB) Statistics
| Branch: | Tag: | Revision:
1 5b74202b Scott Ullrich
<?php
2
/****h* pfSense/config
3
 * NAME
4
 *   config.inc - Functions to manipulate config.xml
5
 * DESCRIPTION
6
 *   This include contains various config.xml specific functions.
7
 * HISTORY
8
 * $Id$
9
 ******
10
11
	config.console.inc
12
	Copyright (C) 2004-2010 Scott Ullrich
13
	All rights reserved.
14
15
	originally part of m0n0wall (http://m0n0.ch/wall)
16
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
17
	All rights reserved.
18
19
	Redistribution and use in source and binary forms, with or without
20
	modification, are permitted provided that the following conditions are met:
21
22
	1. Redistributions of source code must retain the above copyright notice,
23
	   this list of conditions and the following disclaimer.
24
25
	2. Redistributions in binary form must reproduce the above copyright
26
	   notice, this list of conditions and the following disclaimer in the
27
	   documentation and/or other materials provided with the distribution.
28
29
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
30
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
31
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
33
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
	POSSIBILITY OF SUCH DAMAGE.
39
40
41 5ba5a8de Scott Ullrich
	pfSense_BUILDER_BINARIES:	/sbin/mount	/sbin/sysctl	/sbin/umount	/sbin/halt	/sbin/fsck
42 5b74202b Scott Ullrich
	pfSense_MODULE:	config
43
*/
44
45
function set_networking_interfaces_ports() {
46
	global $noreboot;
47
	global $config;
48
	global $g;
49
	global $fp;
50
51
	$fp = fopen('php://stdin', 'r');
52
53
	$memory = get_memory();
54 76a7d8e4 Ermal
	$avail = $memory[1];
55 5b74202b Scott Ullrich
56
	if($avail < $g['minimum_ram_warning']) {
57
		echo "\n\n\n";
58 814f9e64 Carlos Eduardo Ramos
		echo gettext("DANGER!  WARNING!  ACHTUNG!") . "\n\n";
59
		printf(gettext("%s requires *AT LEAST* %s RAM to function correctly.%s"), $g['product_name'], $g['minimum_ram_warning_text'], "\n");
60
		printf(gettext("Only (%s) MB RAM has been detected.%s"), $avail, "\n");
61
		echo "\n" . gettext("Press ENTER to continue.") . " ";
62 5b74202b Scott Ullrich
		fgets($fp);
63
		echo "\n";
64
	}
65
66
	$iflist = get_interface_list();
67
68
/* Function flow is based on $key and $auto_assign or the lack thereof */	
69
	$key = null;
70
71
/* Only present auto interface option if running from LiveCD and interface mismatch*/
72 f1bdc4e3 ccesario
	if ((preg_match("/cdrom/", $g['platform'])) && is_interface_mismatch())
73 5b74202b Scott Ullrich
		$auto_assign = false;
74
75
	echo <<<EOD
76
77
Valid interfaces are:
78
79
80
EOD;
81
82
	if(!is_array($iflist)) {
83 814f9e64 Carlos Eduardo Ramos
		echo gettext("No interfaces found!") . "\n";
84 5b74202b Scott Ullrich
		$iflist = array();
85
	} else {
86
		foreach ($iflist as $iface => $ifa) {
87 ca90133b jim-p
			echo sprintf("% -6s%s %s %s\n", $iface, $ifa['mac'],
88
				$ifa['up'] ? "  (up)" : "(down)", $ifa['dmesg']);
89 5b74202b Scott Ullrich
		}
90
	}
91
92
	if ($auto_assign) {
93
		echo <<<EOD
94
		
95
		!!! LiveCD Detected: Auto Interface Option !!!!
96
BEGIN MANUAL CONFIGURATION OR WE WILL PROCEED WITH AUTO CONFIGURATION.
97
98
EOD;
99
	}	
100
	
101
	echo <<<EOD
102
103
Do you want to set up VLANs first? 
104
105
If you are not going to use VLANs, or only for optional interfaces, you should
106
say no here and use the webConfigurator to configure VLANs later, if required.
107
108
Do you want to set up VLANs now [y|n]? 
109
EOD;
110
111
	if ($auto_assign) {
112
		$key = timeout();
113
114
	} else
115
		$key = chop(fgets($fp));
116
117
	if (!isset($key) and $auto_assign) {	// Auto Assign Interfaces
118
		do {
119
			echo <<<EOD
120
121
   !!! Auto Assigning Interfaces !!!
122
123
For installation purposes, you must plug in at least one NIC
124
for the LAN connection. If you plug in a second NIC it will be
125
assigned to WAN. Otherwise, we'll temporarily assign WAN to the
126
next available NIC found regardless of activity. You should
127
assign and configure the WAN interface according to your requirements
128
129
If you haven't plugged in any network cables yet,
130
now is the time to do so.
131
We'll keep trying until you do.
132
133
Searching for active interfaces...
134
 
135
EOD;
136
			unset($wanif, $lanif);
137
138
			$media_iflist = $plugged_in = array();
139
			$media_iflist = get_interface_list("media");
140
			foreach ($media_iflist as $iface => $ifa) {
141
				if ($ifa['up']) 
142
					$plugged_in[] = $iface;
143
				
144
			}
145
146
			$lanif = array_shift($plugged_in);
147
			$wanif = array_shift($plugged_in);
148
149
			if(isset($lanif) && !isset($wanif)) {
150
				foreach ($iflist as $iface => $ifa) {
151
					if ($iface != $lanif) {
152
						$wanif = $iface;
153
						break;
154
					}
155
				}
156
			}
157
158
			echo <<<EOD
159
160
Assigned WAN to : $wanif 
161
Assigned LAN to : $lanif
162
163
If you don't like this assignment,
164
press any key to go back to manual configuration. 
165
166
EOD;
167
			$key = timeout(20);
168
			if(isset($key))
169
				return;
170
		} while (!isset($wanif));
171
172
		$config['system']['enablesshd'] = 'enabled';	
173
		$key = 'y';
174
175
	} else {		//Manually assign interfaces	
176
		if (in_array($key, array('y', 'Y')))
177
			vlan_setup();
178
	
179
		if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
180
	
181 814f9e64 Carlos Eduardo Ramos
			echo "\n\n" . gettext("VLAN interfaces:") . "\n\n";
182 5b74202b Scott Ullrich
			foreach ($config['vlans']['vlan'] as $vlan) {
183
	
184
				echo sprintf("% -16s%s\n", "{$vlan['if']}_vlan{$vlan['tag']}",
185
					"VLAN tag {$vlan['tag']}, parent interface {$vlan['if']}");
186
	
187
				$iflist[$vlan['if'] . '_vlan' . $vlan['tag']] = array();
188
			}
189
		}
190
	
191
		echo <<<EOD
192
	
193 7916acc3 smos
*NOTE*  {$g['product_name']} requires {$g['minimum_nic_count_text']} assigned interface(s) to function.
194 5b74202b Scott Ullrich
        If you do not have {$g['minimum_nic_count_text']} interfaces you CANNOT continue. 
195
196 7916acc3 smos
        If you do not have at least {$g['minimum_nic_count']} *REAL* network interface card(s)
197 5b74202b Scott Ullrich
        or one interface with multiple VLANs then {$g['product_name']}
198
        *WILL NOT* function correctly.
199
200
If you do not know the names of your interfaces, you may choose to use
201
auto-detection. In that case, disconnect all interfaces now before
202
hitting 'a' to initiate auto detection.
203
	
204
EOD;
205
	
206
		do {
207 814f9e64 Carlos Eduardo Ramos
			echo "\n" . gettext("Enter the WAN interface name or 'a' for auto-detection:") . " ";
208 5b74202b Scott Ullrich
			$wanif = chop(fgets($fp));
209
			if ($wanif === "") {
210
				return;
211
			}
212
			if ($wanif === "a")
213
				$wanif = autodetect_interface("WAN", $fp);
214
			else if (!array_key_exists($wanif, $iflist)) {
215 814f9e64 Carlos Eduardo Ramos
				printf(gettext("%sInvalid interface name '%s'%s"), "\n", $wanif, "\n");
216 5b74202b Scott Ullrich
				unset($wanif);
217
				continue;
218
			}
219
		} while (!$wanif);
220
	
221
		do {
222 814f9e64 Carlos Eduardo Ramos
			printf(gettext("%sEnter the LAN interface name or 'a' for auto-detection %s" .
223
			    "NOTE: this enables full Firewalling/NAT mode.%s" .
224
				"(or nothing if finished):%s"), "\n", "\n", "\n", " ");
225 5b74202b Scott Ullrich
	
226
			$lanif = chop(fgets($fp));
227
			
228
			if($lanif == "exit") {
229
				exit;
230
			}
231
			
232
			if($lanif == "") {
233
				if($g['minimum_nic_count'] < 2) {
234
					break;	
235
				} else {
236
					fclose($fp);
237
					return;
238
				}
239
			}
240
	
241
			if ($lanif === "a")
242
				$lanif = autodetect_interface("LAN", $fp);
243
			else if (!array_key_exists($lanif, $iflist)) {
244 589e9633 jim-p
				printf(gettext("%sInvalid interface name '%s'%s"), "\n", $lanif, "\n");
245 5b74202b Scott Ullrich
				unset($lanif);
246
				continue;
247
			}
248
		} while (!$lanif);
249
	
250
		/* optional interfaces */
251
		$i = 0;
252
		$optif = array();
253
	
254
		if($lanif <> "") {
255
			while (1) {
256
				if ($optif[$i])
257
					$i++;
258 870952cf Ermal
				$io = $i + 1;
259 db0d446f Darren Embry
260 870952cf Ermal
				if($config['interfaces']['opt' . $io]['descr'])
261
					printf(gettext("%sOptional interface %s description found: %s"), "\n", $io, $config['interfaces']['opt' . $io]['descr']);
262 5b74202b Scott Ullrich
	
263 db0d446f Darren Embry
				printf(gettext("%sEnter the Optional %s interface name or 'a' for auto-detection%s" .
264 df0e1f2d Ermal
					"(or nothing if finished):%s"), "\n", $io, "\n", " ");
265 5b74202b Scott Ullrich
		
266
				$optif[$i] = chop(fgets($fp));
267
		
268
				if ($optif[$i]) {
269
					if ($optif[$i] === "a") {
270 870952cf Ermal
						$ad = autodetect_interface(gettext("Optional") . " " . $io, $fp);
271 5b74202b Scott Ullrich
						if ($ad)
272
							$optif[$i] = $ad;
273
						else
274
							unset($optif[$i]);
275
					} else if (!array_key_exists($optif[$i], $iflist)) {
276 814f9e64 Carlos Eduardo Ramos
						printf(gettext("%sInvalid interface name '%s'%s"), "\n", $optif[$i], "\n");
277 5b74202b Scott Ullrich
						unset($optif[$i]);
278
						continue;
279
					}
280
				} else {
281
					unset($optif[$i]);
282
					break;
283
				}
284
			}
285
		}
286
		
287
		/* check for double assignments */
288
		$ifarr = array_merge(array($lanif, $wanif), $optif);
289
		
290
		for ($i = 0; $i < (count($ifarr)-1); $i++) {
291
			for ($j = ($i+1); $j < count($ifarr); $j++) {
292
				if ($ifarr[$i] == $ifarr[$j]) {
293
					echo <<<EOD
294
	
295
Error: you cannot assign the same interface name twice!
296
	
297
EOD;
298
					fclose($fp);
299
					return;
300
				}
301
			}
302
		}
303
	
304 8e1fd4fe Renato Botelho
		echo "\n" . gettext("The interfaces will be assigned as follows:") . "\n\n";
305 5b74202b Scott Ullrich
	
306 95524508 Ermal
		echo "WAN  -> " . $wanif . "\n";
307 5b74202b Scott Ullrich
		if ($lanif != "")
308
			echo "LAN  -> " . $lanif . "\n";
309
		for ($i = 0; $i < count($optif); $i++) {
310
			echo "OPT" . ($i+1) . " -> " . $optif[$i] . "\n";
311
		}
312
	
313
		echo <<<EOD
314
	
315
Do you want to proceed [y|n]?
316
EOD;
317
			$key = chop(fgets($fp));		
318
	}
319
320
	if (in_array($key, array('y', 'Y'))) {
321
		if($lanif) {
322 e503c44a Ermal
			if (!is_array($config['interfaces']['lan']))
323
				$config['interfaces']['lan'] = array();
324 5b74202b Scott Ullrich
			$config['interfaces']['lan']['if'] = $lanif;
325
			$config['interfaces']['lan']['enable'] = true;
326
		} elseif (!$g['booting'] && !$auto_assign) {
327
328
echo <<<EODD
329
330
You have chosen to remove the LAN interface.
331
332
Would you like to remove the LAN IP address and
333
unload the interface now? [y|n]? 
334
EODD;
335
336
				if (strcasecmp(chop(fgets($fp)), "y") == 0) {
337 e503c44a Ermal
					if(isset($config['interfaces']['lan']) && $config['interfaces']['lan']['if'])
338 5b74202b Scott Ullrich
						mwexec("/sbin/ifconfig " . $config['interfaces']['lan']['if'] . " delete");
339
				}
340
				if(isset($config['interfaces']['lan']))
341
					unset($config['interfaces']['lan']);
342
				if(isset($config['dhcpd']['lan']))
343
					unset($config['dhcpd']['lan']);
344
				if(isset($config['interfaces']['lan']['if']))
345
					unset($config['interfaces']['lan']['if']);
346
				if(isset($config['interfaces']['wan']['blockpriv']))
347
					unset($config['interfaces']['wan']['blockpriv']);
348
				if(isset($config['shaper']))
349
					unset($config['shaper']);
350
				if(isset($config['ezshaper']))
351
					unset($config['ezshaper']);
352
				if(isset($config['nat']))
353
					unset($config['nat']);				
354
		} else {
355
			if(isset($config['interfaces']['lan']['if']))
356
				mwexec("/sbin/ifconfig " . $config['interfaces']['lan']['if'] . " delete");
357
			if(isset($config['interfaces']['lan']))
358
				unset($config['interfaces']['lan']);
359
			if(isset($config['dhcpd']['lan']))
360
				unset($config['dhcpd']['lan']);
361
			if(isset($config['interfaces']['lan']['if']))
362
				unset($config['interfaces']['lan']['if']);
363
			if(isset($config['interfaces']['wan']['blockpriv']))
364
				unset($config['interfaces']['wan']['blockpriv']);
365
			if(isset($config['shaper']))
366
				unset($config['shaper']);
367
			if(isset($config['ezshaper']))
368
				unset($config['ezshaper']);
369
			if(isset($config['nat']))
370
				unset($config['nat']);				
371
		}
372
		if (preg_match($g['wireless_regex'], $lanif)) {
373
			if (is_array($config['interfaces']['lan']) &&
374
				(!is_array($config['interfaces']['lan']['wireless'])))
375
				$config['interfaces']['lan']['wireless'] = array();
376
		} else {
377 e503c44a Ermal
			if (isset($config['interfaces']['lan']))
378
				unset($config['interfaces']['lan']['wireless']);
379 5b74202b Scott Ullrich
		}
380
381 e503c44a Ermal
		if (!is_array($config['interfaces']['wan']))
382
			$config['interfaces']['wan'] = array();
383 5b74202b Scott Ullrich
		$config['interfaces']['wan']['if'] = $wanif;
384
		$config['interfaces']['wan']['enable'] = true;
385
		if (preg_match($g['wireless_regex'], $wanif)) {
386
			if (is_array($config['interfaces']['wan']) &&
387
				(!is_array($config['interfaces']['wan']['wireless'])))
388
				$config['interfaces']['wan']['wireless'] = array();
389
		} else {
390 e503c44a Ermal
			if (isset($config['interfaces']['wan']))
391
				unset($config['interfaces']['wan']['wireless']);
392 5b74202b Scott Ullrich
		}
393
394
		for ($i = 0; $i < count($optif); $i++) {
395
			if (!is_array($config['interfaces']['opt' . ($i+1)]))
396
				$config['interfaces']['opt' . ($i+1)] = array();
397
398
			$config['interfaces']['opt' . ($i+1)]['if'] = $optif[$i];
399
400
			/* wireless interface? */
401
			if (preg_match($g['wireless_regex'], $optif[$i])) {
402
				if (!is_array($config['interfaces']['opt' . ($i+1)]['wireless']))
403
					$config['interfaces']['opt' . ($i+1)]['wireless'] = array();
404
			} else {
405
				unset($config['interfaces']['opt' . ($i+1)]['wireless']);
406
			}
407
408 62784b05 Ermal
			if (empty($config['interfaces']['opt' . ($i+1)]['descr'])) {
409
				$config['interfaces']['opt' . ($i+1)]['descr'] = "OPT" . ($i+1);
410
				unset($config['interfaces']['opt' . ($i+1)]['enable']);
411
			}
412 5b74202b Scott Ullrich
		}
413
414
		/* remove all other (old) optional interfaces */
415
		for (; isset($config['interfaces']['opt' . ($i+1)]); $i++)
416
			unset($config['interfaces']['opt' . ($i+1)]);
417
418 814f9e64 Carlos Eduardo Ramos
		printf(gettext("%sWriting configuration..."), "\n");
419 62784b05 Ermal
		write_config("Console assignment of interfaces");
420 814f9e64 Carlos Eduardo Ramos
		printf(gettext("done.%s"), "\n");
421 5b74202b Scott Ullrich
422
		fclose($fp);
423 c9fa8254 Scott Ullrich
424 5b74202b Scott Ullrich
		if($g['booting'])
425
			return;
426
427 814f9e64 Carlos Eduardo Ramos
		echo gettext("One moment while we reload the settings...");
428 5b74202b Scott Ullrich
429
		$g['booting'] = false;
430
431 9d3d8d00 Vinicius Coque
		echo gettext(" done!") . "\n";
432 5b74202b Scott Ullrich
433
		touch("{$g['tmp_path']}/assign_complete");
434
435
	}
436
}
437
438
function autodetect_interface($ifname, $fp) {
439
	$iflist_prev = get_interface_list("media");
440
	echo <<<EOD
441
442
Connect the {$ifname} interface now and make sure that the link is up.
443
Then press ENTER to continue.
444
445
EOD;
446
	fgets($fp);
447
	$iflist = get_interface_list("media");
448
449
	foreach ($iflist_prev as $ifn => $ifa) {
450
		if (!$ifa['up'] && $iflist[$ifn]['up']) {
451 814f9e64 Carlos Eduardo Ramos
			printf(gettext("Detected link-up on interface %s.%s"), $ifn, "\n");
452 5b74202b Scott Ullrich
			return $ifn;
453
		}
454
	}
455
456 814f9e64 Carlos Eduardo Ramos
	printf(gettext("No link-up detected.%s"), "\n");
457 5b74202b Scott Ullrich
458
	return null;
459
}
460
461 c9fa8254 Scott Ullrich
function interfaces_setup() {
462
	global $iflist, $config, $g, $fp;
463
464
	$iflist = get_interface_list();
465
466
	
467
}
468
469 5b74202b Scott Ullrich
function vlan_setup() {
470
	global $iflist, $config, $g, $fp;
471
472
	$iflist = get_interface_list();
473
474
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
475
476
	echo <<<EOD
477
478
WARNING: all existing VLANs will be cleared if you proceed!
479
480
Do you want to proceed [y|n]?
481
EOD;
482
483
	if (strcasecmp(chop(fgets($fp)), "y") != 0)
484
		return;
485
	}
486
487
	$config['vlans']['vlan'] = array();
488
	echo "\n";
489
490
	$vlanif = 0;
491
492
	while (1) {
493
		$vlan = array();
494
495 814f9e64 Carlos Eduardo Ramos
		echo "\n\n" . gettext("VLAN Capable interfaces:") . "\n\n";
496 5b74202b Scott Ullrich
		if(!is_array($iflist)) {
497 814f9e64 Carlos Eduardo Ramos
			echo gettext("No interfaces found!") . "\n";
498 5b74202b Scott Ullrich
		} else {
499
			$vlan_capable=0;
500
			foreach ($iflist as $iface => $ifa) {
501
				if (is_jumbo_capable($iface)) {
502
					echo sprintf("% -8s%s%s\n", $iface, $ifa['mac'],
503
						$ifa['up'] ? "   (up)" : "");
504
					$vlan_capable++;
505
				}
506
			}
507
		}
508
509
		if($vlan_capable == 0) {
510 814f9e64 Carlos Eduardo Ramos
			echo gettext("No VLAN capable interfaces detected.") . "\n";
511 5b74202b Scott Ullrich
			return;
512
		}
513
514 814f9e64 Carlos Eduardo Ramos
		echo "\n" . gettext("Enter the parent interface name for the new VLAN (or nothing if finished):") . " ";
515 5b74202b Scott Ullrich
		$vlan['if'] = chop(fgets($fp));
516
517
		if ($vlan['if']) {
518
			if (!array_key_exists($vlan['if'], $iflist) or
519
			    !is_jumbo_capable($vlan['if'])) {
520 814f9e64 Carlos Eduardo Ramos
				printf(gettext("%sInvalid interface name '%s'%s"), "\n", $vlan['if'], "\n");
521 5b74202b Scott Ullrich
				continue;
522
			}
523
		} else {
524
			break;
525
		}
526
527 814f9e64 Carlos Eduardo Ramos
		echo gettext("Enter the VLAN tag (1-4094):") . " ";
528 5b74202b Scott Ullrich
		$vlan['tag'] = chop(fgets($fp));
529
		$vlan['vlanif'] = "{$vlan['if']}_vlan{$vlan['tag']}";
530
		if (!is_numericint($vlan['tag']) || ($vlan['tag'] < 1) || ($vlan['tag'] > 4094)) {
531 814f9e64 Carlos Eduardo Ramos
			printf(gettext("%sInvalid VLAN tag '%s'%s"), "\n", $vlan['tag'], "\n");
532 5b74202b Scott Ullrich
			continue;
533
		}
534
		
535
		$config['vlans']['vlan'][] = $vlan;
536
		$vlanif++;
537
	}
538
}
539
540 814f9e64 Carlos Eduardo Ramos
?>