Project

General

Profile

Download (17 KB) Statistics
| Branch: | Tag: | Revision:
1
<?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
function set_networking_interfaces_ports() {
42
	global $noreboot;
43
	global $config;
44
	global $g;
45
	global $fp;
46

    
47
	$fp = fopen('php://stdin', 'r');
48

    
49
	$memory = get_memory();
50
	$physmem = $memory[0];
51
	$realmem = $memory[1];
52

    
53
	if ($physmem < $g['minimum_ram_warning']) {
54
		echo "\n\n\n";
55
		echo gettext("DANGER!  WARNING!  ACHTUNG!") . "\n\n";
56
		printf(gettext("%s requires *AT LEAST* %s RAM to function correctly.%s"), $g['product_name'], $g['minimum_ram_warning_text'], "\n");
57
		printf(gettext("Only (%s) MB RAM has been detected, with (%s) available to %s.%s"), $realmem, $physmem, $g['product_name'], "\n");
58
		echo "\n" . gettext("Press ENTER to continue.") . " ";
59
		fgets($fp);
60
		echo "\n";
61
	}
62

    
63
	$iflist = get_interface_list();
64

    
65
	/* Function flow is based on $key and $auto_assign or the lack thereof */
66
	$key = null;
67

    
68
	/* Only present auto interface option if running from LiveCD and interface mismatch*/
69
	if ((preg_match("/cdrom/", $g['platform'])) && is_interface_mismatch()) {
70
		$auto_assign = false;
71
	}
72

    
73
	echo <<<EOD
74

    
75
Valid interfaces are:
76

    
77

    
78
EOD;
79

    
80
	if (!is_array($iflist)) {
81
		echo gettext("No interfaces found!") . "\n";
82
		$iflist = array();
83
	} else {
84
		foreach ($iflist as $iface => $ifa) {
85
			$ifsmallist = trim($ifsmallist . " " . $iface);
86
			echo sprintf("% -7s%s %s %s\n", $iface, $ifa['mac'],
87
				$ifa['up'] ? "  (up)" : "(down)", substr($ifa['dmesg'], 0, 48));
88
		}
89
	}
90

    
91
	if ($auto_assign) {
92
		echo <<<EOD
93

    
94
		!!! LiveCD Detected: Auto Interface Option !!!!
95
BEGIN MANUAL CONFIGURATION OR WE WILL PROCEED WITH AUTO CONFIGURATION.
96

    
97
EOD;
98
	}
99

    
100
	echo "\n" . gettext("Do you want to set up VLANs first?");
101
	echo "\n" .
102
		gettext(
103
			"If you are not going to use VLANs, or only for optional interfaces, you should\n" .
104
			"say no here and use the webConfigurator to configure VLANs later, if required.") .
105
		"\n";
106
	echo "\n" . gettext("Do you want to set up VLANs now [y|n]?") . " ";
107

    
108
	if ($auto_assign) {
109
		$key = timeout();
110
	} else {
111
		$key = chop(fgets($fp));
112
	}
113

    
114
	if (!isset($key) and $auto_assign) {	// Auto Assign Interfaces
115
		do {
116
			echo <<<EOD
117

    
118
   !!! Auto Assigning Interfaces !!!
119

    
120
For installation purposes, you must plug in at least one NIC
121
for the LAN connection. If you plug in a second NIC it will be
122
assigned to WAN. Otherwise, we'll temporarily assign WAN to the
123
next available NIC found regardless of activity. You should
124
assign and configure the WAN interface according to your requirements
125

    
126
If you haven't plugged in any network cables yet,
127
now is the time to do so.
128
We'll keep trying until you do.
129

    
130
Searching for active interfaces...
131

    
132
EOD;
133
			unset($wanif, $lanif);
134

    
135
			$media_iflist = $plugged_in = array();
136
			$media_iflist = get_interface_list("media");
137
			foreach ($media_iflist as $iface => $ifa) {
138
				if ($ifa['up']) {
139
					$plugged_in[] = $iface;
140
				}
141
			}
142

    
143
			$lanif = array_shift($plugged_in);
144
			$wanif = array_shift($plugged_in);
145

    
146
			if (isset($lanif) && !isset($wanif)) {
147
				foreach ($iflist as $iface => $ifa) {
148
					if ($iface != $lanif) {
149
						$wanif = $iface;
150
						break;
151
					}
152
				}
153
			}
154

    
155
			echo <<<EOD
156

    
157
Assigned WAN to : $wanif
158
Assigned LAN to : $lanif
159

    
160
If you don't like this assignment,
161
press any key to go back to manual configuration.
162

    
163
EOD;
164
			$key = timeout(20);
165
			if (isset($key)) {
166
				return;
167
			}
168
		} while (!isset($wanif));
169

    
170
		$config['system']['enablesshd'] = 'enabled';
171
		$key = 'y';
172

    
173
	} else {
174
		//Manually assign interfaces
175
		if (in_array($key, array('y', 'Y'))) {
176
			vlan_setup();
177
		}
178

    
179
		if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
180

    
181
			echo "\n\n" . gettext("VLAN interfaces:") . "\n\n";
182
			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
				$ifsmallist = trim($ifsmallist . " " . $vlan['if'] . '_vlan' . $vlan['tag']);
189
			}
190
		}
191

    
192
		echo <<<EOD
193

    
194
If you do not know the names of your interfaces, you may choose to use
195
auto-detection. In that case, disconnect all interfaces now before
196
hitting 'a' to initiate auto detection.
197

    
198
EOD;
199

    
200
		do {
201
			echo "\n" . gettext("Enter the WAN interface name or 'a' for auto-detection") . " ";
202
			printf(gettext("%s(%s or a): "), "\n", $ifsmallist);
203
			$wanif = chop(fgets($fp));
204
			if ($wanif === "") {
205
				return;
206
			}
207
			if ($wanif === "a") {
208
				$wanif = autodetect_interface("WAN", $fp);
209
			} else if (!array_key_exists($wanif, $iflist)) {
210
				printf(gettext("%sInvalid interface name '%s'%s"), "\n", $wanif, "\n");
211
				unset($wanif);
212
				continue;
213
			}
214
			$ifsmallist = trim(str_replace("  ", " ", str_replace($wanif, "", $ifsmallist)));
215
		} while (!$wanif);
216

    
217
		do {
218
			printf(gettext("%sEnter the LAN interface name or 'a' for auto-detection %s" .
219
				"NOTE: this enables full Firewalling/NAT mode.%s" .
220
				"(%s a or nothing if finished):%s"), "\n", "\n", "\n", $ifsmallist, " ");
221

    
222
			$lanif = chop(fgets($fp));
223

    
224
			if ($lanif == "exit") {
225
				exit;
226
			}
227

    
228
			if ($lanif == "") {
229
				/* It is OK to have just a WAN, without a LAN so break if the user does not want LAN. */
230
				break;
231
			}
232

    
233
			if ($lanif === "a") {
234
				$lanif = autodetect_interface("LAN", $fp);
235
			} else if (!array_key_exists($lanif, $iflist)) {
236
				printf(gettext("%sInvalid interface name '%s'%s"), "\n", $lanif, "\n");
237
				unset($lanif);
238
				continue;
239
			}
240
			$ifsmallist = trim(str_replace("  ", " ", str_replace($lanif, "", $ifsmallist)));
241
		} while (!$lanif);
242

    
243
		/* optional interfaces */
244
		$i = 0;
245
		$optif = array();
246

    
247
		if ($lanif <> "") {
248
			while (1) {
249
				if ($optif[$i]) {
250
					$i++;
251
				}
252
				$io = $i + 1;
253

    
254
				if ($config['interfaces']['opt' . $io]['descr']) {
255
					printf(gettext("%sOptional interface %s description found: %s"), "\n", $io, $config['interfaces']['opt' . $io]['descr']);
256
				}
257

    
258
				printf(gettext("%sEnter the Optional %s interface name or 'a' for auto-detection%s" .
259
					"(%s a or nothing if finished):%s"), "\n", $io, "\n", $ifsmallist, " ");
260

    
261
				$optif[$i] = chop(fgets($fp));
262

    
263
				if ($optif[$i]) {
264
					if ($optif[$i] === "a") {
265
						$ad = autodetect_interface(gettext("Optional") . " " . $io, $fp);
266
						if ($ad) {
267
							$optif[$i] = $ad;
268
						} else {
269
							unset($optif[$i]);
270
						}
271
					} else if (!array_key_exists($optif[$i], $iflist)) {
272
						printf(gettext("%sInvalid interface name '%s'%s"), "\n", $optif[$i], "\n");
273
						unset($optif[$i]);
274
						continue;
275
					}
276
					$ifsmallist = trim(str_replace("  ", " ", str_replace($optif[$i], "", $ifsmallist)));
277
				} else {
278
					unset($optif[$i]);
279
					break;
280
				}
281
			}
282
		}
283

    
284
		/* check for double assignments */
285
		$ifarr = array_merge(array($lanif, $wanif), $optif);
286

    
287
		for ($i = 0; $i < (count($ifarr)-1); $i++) {
288
			for ($j = ($i+1); $j < count($ifarr); $j++) {
289
				if ($ifarr[$i] == $ifarr[$j]) {
290
					echo <<<EOD
291

    
292
Error: you cannot assign the same interface name twice!
293

    
294
EOD;
295
					fclose($fp);
296
					return;
297
				}
298
			}
299
		}
300

    
301
		echo "\n" . gettext("The interfaces will be assigned as follows:") . "\n\n";
302

    
303
		echo "WAN  -> " . $wanif . "\n";
304
		if ($lanif != "") {
305
			echo "LAN  -> " . $lanif . "\n";
306
		}
307
		for ($i = 0; $i < count($optif); $i++) {
308
			echo "OPT" . ($i+1) . " -> " . $optif[$i] . "\n";
309
		}
310

    
311
		echo "\n" . gettext("Do you want to proceed [y|n]?") . " ";
312
		$key = chop(fgets($fp));
313
	}
314

    
315
	if (in_array($key, array('y', 'Y'))) {
316
		if ($lanif) {
317
			if (!is_array($config['interfaces']['lan'])) {
318
				$config['interfaces']['lan'] = array();
319
			}
320
			$config['interfaces']['lan']['if'] = $lanif;
321
			$config['interfaces']['lan']['enable'] = true;
322
		} elseif (!platform_booting() && !$auto_assign) {
323

    
324
			echo "\n" . gettext("You have chosen to remove the LAN interface.") . "\n";
325
			echo "\n" . gettext("Would you like to remove the LAN IP address and \nunload the interface now [y|n]?") . " ";
326

    
327
			if (strcasecmp(chop(fgets($fp)), "y") == 0) {
328
				if (isset($config['interfaces']['lan']) && $config['interfaces']['lan']['if']) {
329
					mwexec("/sbin/ifconfig " . $config['interfaces']['lan']['if'] . " delete");
330
				}
331
			}
332
			if (isset($config['interfaces']['lan'])) {
333
				unset($config['interfaces']['lan']);
334
			}
335
			if (isset($config['dhcpd']['lan'])) {
336
				unset($config['dhcpd']['lan']);
337
			}
338
			if (isset($config['interfaces']['lan']['if'])) {
339
				unset($config['interfaces']['lan']['if']);
340
			}
341
			if (isset($config['interfaces']['wan']['blockpriv'])) {
342
				unset($config['interfaces']['wan']['blockpriv']);
343
			}
344
			if (isset($config['shaper'])) {
345
				unset($config['shaper']);
346
			}
347
			if (isset($config['ezshaper'])) {
348
				unset($config['ezshaper']);
349
			}
350
			if (isset($config['nat'])) {
351
				unset($config['nat']);
352
			}
353
		} else {
354
			if (isset($config['interfaces']['lan']['if'])) {
355
				mwexec("/sbin/ifconfig " . $config['interfaces']['lan']['if'] . " delete");
356
			}
357
			if (isset($config['interfaces']['lan'])) {
358
				unset($config['interfaces']['lan']);
359
			}
360
			if (isset($config['dhcpd']['lan'])) {
361
				unset($config['dhcpd']['lan']);
362
			}
363
			if (isset($config['interfaces']['lan']['if'])) {
364
				unset($config['interfaces']['lan']['if']);
365
			}
366
			if (isset($config['interfaces']['wan']['blockpriv'])) {
367
				unset($config['interfaces']['wan']['blockpriv']);
368
			}
369
			if (isset($config['shaper'])) {
370
				unset($config['shaper']);
371
			}
372
			if (isset($config['ezshaper'])) {
373
				unset($config['ezshaper']);
374
			}
375
			if (isset($config['nat'])) {
376
				unset($config['nat']);
377
			}
378
		}
379
		if (preg_match($g['wireless_regex'], $lanif)) {
380
			if (is_array($config['interfaces']['lan']) &&
381
			    !is_array($config['interfaces']['lan']['wireless'])) {
382
				$config['interfaces']['lan']['wireless'] = array();
383
			}
384
		} else {
385
			if (isset($config['interfaces']['lan'])) {
386
				unset($config['interfaces']['lan']['wireless']);
387
			}
388
		}
389

    
390
		if (!is_array($config['interfaces']['wan'])) {
391
			$config['interfaces']['wan'] = array();
392
		}
393
		$config['interfaces']['wan']['if'] = $wanif;
394
		$config['interfaces']['wan']['enable'] = true;
395
		if (preg_match($g['wireless_regex'], $wanif)) {
396
			if (is_array($config['interfaces']['wan']) &&
397
			    !is_array($config['interfaces']['wan']['wireless'])) {
398
				$config['interfaces']['wan']['wireless'] = array();
399
			}
400
		} else {
401
			if (isset($config['interfaces']['wan'])) {
402
				unset($config['interfaces']['wan']['wireless']);
403
			}
404
		}
405

    
406
		for ($i = 0; $i < count($optif); $i++) {
407
			if (!is_array($config['interfaces']['opt' . ($i+1)])) {
408
				$config['interfaces']['opt' . ($i+1)] = array();
409
			}
410

    
411
			$config['interfaces']['opt' . ($i+1)]['if'] = $optif[$i];
412

    
413
			/* wireless interface? */
414
			if (preg_match($g['wireless_regex'], $optif[$i])) {
415
				if (!is_array($config['interfaces']['opt' . ($i+1)]['wireless'])) {
416
					$config['interfaces']['opt' . ($i+1)]['wireless'] = array();
417
				}
418
			} else {
419
				unset($config['interfaces']['opt' . ($i+1)]['wireless']);
420
			}
421

    
422
			if (empty($config['interfaces']['opt' . ($i+1)]['descr'])) {
423
				$config['interfaces']['opt' . ($i+1)]['descr'] = "OPT" . ($i+1);
424
				unset($config['interfaces']['opt' . ($i+1)]['enable']);
425
			}
426
		}
427

    
428
		/* remove all other (old) optional interfaces */
429
		for (; isset($config['interfaces']['opt' . ($i+1)]); $i++) {
430
			unset($config['interfaces']['opt' . ($i+1)]);
431
		}
432

    
433
		printf(gettext("%sWriting configuration..."), "\n");
434
		write_config("Console assignment of interfaces");
435
		printf(gettext("done.%s"), "\n");
436

    
437
		fclose($fp);
438

    
439
		if (platform_booting()) {
440
			return;
441
		}
442

    
443
		echo gettext("One moment while we reload the settings...");
444
		echo gettext(" done!") . "\n";
445

    
446
		touch("{$g['tmp_path']}/assign_complete");
447

    
448
	}
449
}
450

    
451
function autodetect_interface($ifname, $fp) {
452
	$iflist_prev = get_interface_list("media");
453
	echo <<<EOD
454

    
455
Connect the {$ifname} interface now and make sure that the link is up.
456
Then press ENTER to continue.
457

    
458
EOD;
459
	fgets($fp);
460
	$iflist = get_interface_list("media");
461

    
462
	foreach ($iflist_prev as $ifn => $ifa) {
463
		if (!$ifa['up'] && $iflist[$ifn]['up']) {
464
			printf(gettext("Detected link-up on interface %s.%s"), $ifn, "\n");
465
			return $ifn;
466
		}
467
	}
468

    
469
	printf(gettext("No link-up detected.%s"), "\n");
470

    
471
	return null;
472
}
473

    
474
function interfaces_setup() {
475
	global $iflist, $config, $g, $fp;
476

    
477
	$iflist = get_interface_list();
478
}
479

    
480
function vlan_setup() {
481
	global $iflist, $config, $g, $fp;
482

    
483
	$iflist = get_interface_list();
484

    
485
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
486
		echo "\n" . gettext("WARNING: all existing VLANs will be cleared if you proceed!") . "\n";
487
		echo "\n" . gettext("Do you want to proceed [y|n]?") . " ";
488

    
489
		if (strcasecmp(chop(fgets($fp)), "y") != 0) {
490
			return;
491
		}
492
	}
493

    
494
	$config['vlans']['vlan'] = array();
495
	echo "\n";
496

    
497
	$vlanif = 0;
498

    
499
	while (1) {
500
		$vlan = array();
501

    
502
		echo "\n\n" . gettext("VLAN Capable interfaces:") . "\n\n";
503
		if (!is_array($iflist)) {
504
			echo gettext("No interfaces found!") . "\n";
505
		} else {
506
			$vlan_capable = 0;
507
			foreach ($iflist as $iface => $ifa) {
508
				if (is_jumbo_capable($iface)) {
509
					echo sprintf("% -8s%s%s\n", $iface, $ifa['mac'],
510
						$ifa['up'] ? "   (up)" : "");
511
					$vlan_capable++;
512
				}
513
			}
514
		}
515

    
516
		if ($vlan_capable == 0) {
517
			echo gettext("No VLAN capable interfaces detected.") . "\n";
518
			return;
519
		}
520

    
521
		echo "\n" . gettext("Enter the parent interface name for the new VLAN (or nothing if finished):") . " ";
522
		$vlan['if'] = chop(fgets($fp));
523

    
524
		if ($vlan['if']) {
525
			if (!array_key_exists($vlan['if'], $iflist) or
526
			    !is_jumbo_capable($vlan['if'])) {
527
				printf(gettext("%sInvalid interface name '%s'%s"), "\n", $vlan['if'], "\n");
528
				continue;
529
			}
530
		} else {
531
			break;
532
		}
533

    
534
		echo gettext("Enter the VLAN tag (1-4094):") . " ";
535
		$vlan['tag'] = chop(fgets($fp));
536
		$vlan['vlanif'] = "{$vlan['if']}_vlan{$vlan['tag']}";
537
		if (!is_numericint($vlan['tag']) || ($vlan['tag'] < 1) || ($vlan['tag'] > 4094)) {
538
			printf(gettext("%sInvalid VLAN tag '%s'%s"), "\n", $vlan['tag'], "\n");
539
			continue;
540
		}
541

    
542
		$config['vlans']['vlan'][] = $vlan;
543
		$vlanif++;
544
	}
545
}
546

    
547
function check_for_alternate_interfaces() {
548
	global $config;
549

    
550
	// If the WAN and/or LAN devices in the factory default config do not exist,
551
	// then look for alternate devices.
552
	// This lets many systems boot a factory default config without being
553
	// forced to do interface assignment on the console.
554

    
555
	$specplatform = system_identify_specific_platform();
556
	$default_device = array();
557

    
558
	// If we recognise the platform, then specify the devices directly.
559
	switch ($specplatform['name']) {
560
		case 'alix':
561
			$default_device['wan'] = "vr1";
562
			$default_device['lan'] = "vr0";
563
			break;
564
		case 'APU':
565
			$default_device['wan'] = "re1";
566
			$default_device['lan'] = "re2";
567
			break;
568
		case 'RCC-VE':
569
			$default_device['wan'] = "igb0";
570
			$default_device['lan'] = "igb1";
571
			break;
572
		default:
573
			$default_device['wan'] = "";
574
			$default_device['lan'] = "";
575
			break;
576
	}
577

    
578
	// Other common device names can be put here and will be looked for
579
	// if the system was not one of the known platforms.
580
	$other_devices_arr['wan'] = array("vr1", "re1", "igb0", "em0");
581
	$other_devices_arr['lan'] = array("vr0", "re2", "igb1", "em1");
582
	$interface_assignment_changed = false;
583

    
584
	foreach ($other_devices_arr as $ifname => $other_devices) {
585
		if (!does_interface_exist($config['interfaces'][$ifname]['if'])) {
586
			if (does_interface_exist($default_device[$ifname])) {
587
				$config['interfaces'][$ifname]['if'] = $default_device[$ifname];
588
				$interface_assignment_changed = true;
589
			} else {
590
				foreach ($other_devices as $other_device) {
591
					if (does_interface_exist($other_device)) {
592
						$config['interfaces'][$ifname]['if'] = $other_device;
593
						$interface_assignment_changed = true;
594
						break;
595
					}
596
				}
597
			}
598
		}
599
	}
600

    
601
	if ($interface_assignment_changed) {
602
		write_config("Factory default boot detected WAN " . $config['interfaces']['wan']['if'] . " and LAN " . $config['interfaces']['lan']['if']);
603
	}
604
}
605

    
606
?>
(9-9/65)