Project

General

Profile

Download (15 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
	pfSense_BUILDER_BINARIES:	/sbin/mount	/sbin/umount	/sbin/halt	/sbin/fsck
42
	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
	$physmem = $memory[0];
55
	$realmem = $memory[1];
56

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

    
67
	$iflist = get_interface_list();
68

    
69
	/* Function flow is based on $key and $auto_assign or the lack thereof */
70
	$key = null;
71

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

    
77
	echo <<<EOD
78

    
79
Valid interfaces are:
80

    
81

    
82
EOD;
83

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

    
95
	if ($auto_assign) {
96
		echo <<<EOD
97

    
98
		!!! LiveCD Detected: Auto Interface Option !!!!
99
BEGIN MANUAL CONFIGURATION OR WE WILL PROCEED WITH AUTO CONFIGURATION.
100

    
101
EOD;
102
	}
103

    
104
	echo <<<EOD
105

    
106
Do you want to set up VLANs first? 
107

    
108
If you are not going to use VLANs, or only for optional interfaces, you should
109
say no here and use the webConfigurator to configure VLANs later, if required.
110

    
111
Do you want to set up VLANs now [y|n]? 
112
EOD;
113

    
114
	if ($auto_assign) {
115
		$key = timeout();
116
	} else {
117
		$key = chop(fgets($fp));
118
	}
119

    
120
	if (!isset($key) and $auto_assign) {	// Auto Assign Interfaces
121
		do {
122
			echo <<<EOD
123

    
124
   !!! Auto Assigning Interfaces !!!
125

    
126
For installation purposes, you must plug in at least one NIC
127
for the LAN connection. If you plug in a second NIC it will be
128
assigned to WAN. Otherwise, we'll temporarily assign WAN to the
129
next available NIC found regardless of activity. You should
130
assign and configure the WAN interface according to your requirements
131

    
132
If you haven't plugged in any network cables yet,
133
now is the time to do so.
134
We'll keep trying until you do.
135

    
136
Searching for active interfaces...
137

    
138
EOD;
139
			unset($wanif, $lanif);
140

    
141
			$media_iflist = $plugged_in = array();
142
			$media_iflist = get_interface_list("media");
143
			foreach ($media_iflist as $iface => $ifa) {
144
				if ($ifa['up']) {
145
					$plugged_in[] = $iface;
146
				}
147
			}
148

    
149
			$lanif = array_shift($plugged_in);
150
			$wanif = array_shift($plugged_in);
151

    
152
			if (isset($lanif) && !isset($wanif)) {
153
				foreach ($iflist as $iface => $ifa) {
154
					if ($iface != $lanif) {
155
						$wanif = $iface;
156
						break;
157
					}
158
				}
159
			}
160

    
161
			echo <<<EOD
162

    
163
Assigned WAN to : $wanif
164
Assigned LAN to : $lanif
165

    
166
If you don't like this assignment,
167
press any key to go back to manual configuration.
168

    
169
EOD;
170
			$key = timeout(20);
171
			if (isset($key)) {
172
				return;
173
			}
174
		} while (!isset($wanif));
175

    
176
		$config['system']['enablesshd'] = 'enabled';
177
		$key = 'y';
178

    
179
	} else {
180
		//Manually assign interfaces
181
		if (in_array($key, array('y', 'Y'))) {
182
			vlan_setup();
183
		}
184

    
185
		if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
186

    
187
			echo "\n\n" . gettext("VLAN interfaces:") . "\n\n";
188
			foreach ($config['vlans']['vlan'] as $vlan) {
189

    
190
				echo sprintf("% -16s%s\n", "{$vlan['if']}_vlan{$vlan['tag']}",
191
					"VLAN tag {$vlan['tag']}, parent interface {$vlan['if']}");
192

    
193
				$iflist[$vlan['if'] . '_vlan' . $vlan['tag']] = array();
194
				$ifsmallist = trim($ifsmallist . " " . $vlan['if'] . '_vlan' . $vlan['tag']);
195
			}
196
		}
197

    
198
		echo <<<EOD
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
			echo "\n" . gettext("Enter the WAN interface name or 'a' for auto-detection") . " ";
208
			printf(gettext("%s(%s or a): "), "\n", $ifsmallist);
209
			$wanif = chop(fgets($fp));
210
			if ($wanif === "") {
211
				return;
212
			}
213
			if ($wanif === "a") {
214
				$wanif = autodetect_interface("WAN", $fp);
215
			} else if (!array_key_exists($wanif, $iflist)) {
216
				printf(gettext("%sInvalid interface name '%s'%s"), "\n", $wanif, "\n");
217
				unset($wanif);
218
				continue;
219
			}
220
			$ifsmallist = trim(str_replace("  ", " ", str_replace($wanif, "", $ifsmallist)));
221
		} while (!$wanif);
222

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

    
228
			$lanif = chop(fgets($fp));
229

    
230
			if ($lanif == "exit") {
231
				exit;
232
			}
233

    
234
			if ($lanif == "") {
235
				/* It is OK to have just a WAN, without a LAN so break if the user does not want LAN. */
236
				break;
237
			}
238

    
239
			if ($lanif === "a") {
240
				$lanif = autodetect_interface("LAN", $fp);
241
			} else if (!array_key_exists($lanif, $iflist)) {
242
				printf(gettext("%sInvalid interface name '%s'%s"), "\n", $lanif, "\n");
243
				unset($lanif);
244
				continue;
245
			}
246
			$ifsmallist = trim(str_replace("  ", " ", str_replace($lanif, "", $ifsmallist)));
247
		} while (!$lanif);
248

    
249
		/* optional interfaces */
250
		$i = 0;
251
		$optif = array();
252

    
253
		if ($lanif <> "") {
254
			while (1) {
255
				if ($optif[$i]) {
256
					$i++;
257
				}
258
				$io = $i + 1;
259

    
260
				if ($config['interfaces']['opt' . $io]['descr']) {
261
					printf(gettext("%sOptional interface %s description found: %s"), "\n", $io, $config['interfaces']['opt' . $io]['descr']);
262
				}
263

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

    
267
				$optif[$i] = chop(fgets($fp));
268

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

    
290
		/* check for double assignments */
291
		$ifarr = array_merge(array($lanif, $wanif), $optif);
292

    
293
		for ($i = 0; $i < (count($ifarr)-1); $i++) {
294
			for ($j = ($i+1); $j < count($ifarr); $j++) {
295
				if ($ifarr[$i] == $ifarr[$j]) {
296
					echo <<<EOD
297

    
298
Error: you cannot assign the same interface name twice!
299

    
300
EOD;
301
					fclose($fp);
302
					return;
303
				}
304
			}
305
		}
306

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

    
309
		echo "WAN  -> " . $wanif . "\n";
310
		if ($lanif != "") {
311
			echo "LAN  -> " . $lanif . "\n";
312
		}
313
		for ($i = 0; $i < count($optif); $i++) {
314
			echo "OPT" . ($i+1) . " -> " . $optif[$i] . "\n";
315
		}
316

    
317
		echo <<<EOD
318

    
319
Do you want to proceed [y|n]?
320
EOD;
321
		$key = chop(fgets($fp));
322
	}
323

    
324
	if (in_array($key, array('y', 'Y'))) {
325
		if ($lanif) {
326
			if (!is_array($config['interfaces']['lan'])) {
327
				$config['interfaces']['lan'] = array();
328
			}
329
			$config['interfaces']['lan']['if'] = $lanif;
330
			$config['interfaces']['lan']['enable'] = true;
331
		} elseif (!platform_booting() && !$auto_assign) {
332

    
333
echo <<<EODD
334

    
335
You have chosen to remove the LAN interface.
336

    
337
Would you like to remove the LAN IP address and
338
unload the interface now? [y|n]? 
339
EODD;
340

    
341
			if (strcasecmp(chop(fgets($fp)), "y") == 0) {
342
				if (isset($config['interfaces']['lan']) && $config['interfaces']['lan']['if']) {
343
					mwexec("/sbin/ifconfig " . $config['interfaces']['lan']['if'] . " delete");
344
				}
345
			}
346
			if (isset($config['interfaces']['lan'])) {
347
				unset($config['interfaces']['lan']);
348
			}
349
			if (isset($config['dhcpd']['lan'])) {
350
				unset($config['dhcpd']['lan']);
351
			}
352
			if (isset($config['interfaces']['lan']['if'])) {
353
				unset($config['interfaces']['lan']['if']);
354
			}
355
			if (isset($config['interfaces']['wan']['blockpriv'])) {
356
				unset($config['interfaces']['wan']['blockpriv']);
357
			}
358
			if (isset($config['shaper'])) {
359
				unset($config['shaper']);
360
			}
361
			if (isset($config['ezshaper'])) {
362
				unset($config['ezshaper']);
363
			}
364
			if (isset($config['nat'])) {
365
				unset($config['nat']);
366
			}
367
		} else {
368
			if (isset($config['interfaces']['lan']['if'])) {
369
				mwexec("/sbin/ifconfig " . $config['interfaces']['lan']['if'] . " delete");
370
			}
371
			if (isset($config['interfaces']['lan'])) {
372
				unset($config['interfaces']['lan']);
373
			}
374
			if (isset($config['dhcpd']['lan'])) {
375
				unset($config['dhcpd']['lan']);
376
			}
377
			if (isset($config['interfaces']['lan']['if'])) {
378
				unset($config['interfaces']['lan']['if']);
379
			}
380
			if (isset($config['interfaces']['wan']['blockpriv'])) {
381
				unset($config['interfaces']['wan']['blockpriv']);
382
			}
383
			if (isset($config['shaper'])) {
384
				unset($config['shaper']);
385
			}
386
			if (isset($config['ezshaper'])) {
387
				unset($config['ezshaper']);
388
			}
389
			if (isset($config['nat'])) {
390
				unset($config['nat']);
391
			}
392
		}
393
		if (preg_match($g['wireless_regex'], $lanif)) {
394
			if (is_array($config['interfaces']['lan']) &&
395
			    !is_array($config['interfaces']['lan']['wireless'])) {
396
				$config['interfaces']['lan']['wireless'] = array();
397
			}
398
		} else {
399
			if (isset($config['interfaces']['lan'])) {
400
				unset($config['interfaces']['lan']['wireless']);
401
			}
402
		}
403

    
404
		if (!is_array($config['interfaces']['wan'])) {
405
			$config['interfaces']['wan'] = array();
406
		}
407
		$config['interfaces']['wan']['if'] = $wanif;
408
		$config['interfaces']['wan']['enable'] = true;
409
		if (preg_match($g['wireless_regex'], $wanif)) {
410
			if (is_array($config['interfaces']['wan']) &&
411
			    !is_array($config['interfaces']['wan']['wireless'])) {
412
				$config['interfaces']['wan']['wireless'] = array();
413
			}
414
		} else {
415
			if (isset($config['interfaces']['wan'])) {
416
				unset($config['interfaces']['wan']['wireless']);
417
			}
418
		}
419

    
420
		for ($i = 0; $i < count($optif); $i++) {
421
			if (!is_array($config['interfaces']['opt' . ($i+1)])) {
422
				$config['interfaces']['opt' . ($i+1)] = array();
423
			}
424

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

    
427
			/* wireless interface? */
428
			if (preg_match($g['wireless_regex'], $optif[$i])) {
429
				if (!is_array($config['interfaces']['opt' . ($i+1)]['wireless'])) {
430
					$config['interfaces']['opt' . ($i+1)]['wireless'] = array();
431
				}
432
			} else {
433
				unset($config['interfaces']['opt' . ($i+1)]['wireless']);
434
			}
435

    
436
			if (empty($config['interfaces']['opt' . ($i+1)]['descr'])) {
437
				$config['interfaces']['opt' . ($i+1)]['descr'] = "OPT" . ($i+1);
438
				unset($config['interfaces']['opt' . ($i+1)]['enable']);
439
			}
440
		}
441

    
442
		/* remove all other (old) optional interfaces */
443
		for (; isset($config['interfaces']['opt' . ($i+1)]); $i++) {
444
			unset($config['interfaces']['opt' . ($i+1)]);
445
		}
446

    
447
		printf(gettext("%sWriting configuration..."), "\n");
448
		write_config("Console assignment of interfaces");
449
		printf(gettext("done.%s"), "\n");
450

    
451
		fclose($fp);
452

    
453
		if (platform_booting()) {
454
			return;
455
		}
456

    
457
		echo gettext("One moment while we reload the settings...");
458
		echo gettext(" done!") . "\n";
459

    
460
		touch("{$g['tmp_path']}/assign_complete");
461

    
462
	}
463
}
464

    
465
function autodetect_interface($ifname, $fp) {
466
	$iflist_prev = get_interface_list("media");
467
	echo <<<EOD
468

    
469
Connect the {$ifname} interface now and make sure that the link is up.
470
Then press ENTER to continue.
471

    
472
EOD;
473
	fgets($fp);
474
	$iflist = get_interface_list("media");
475

    
476
	foreach ($iflist_prev as $ifn => $ifa) {
477
		if (!$ifa['up'] && $iflist[$ifn]['up']) {
478
			printf(gettext("Detected link-up on interface %s.%s"), $ifn, "\n");
479
			return $ifn;
480
		}
481
	}
482

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

    
485
	return null;
486
}
487

    
488
function interfaces_setup() {
489
	global $iflist, $config, $g, $fp;
490

    
491
	$iflist = get_interface_list();
492
}
493

    
494
function vlan_setup() {
495
	global $iflist, $config, $g, $fp;
496

    
497
	$iflist = get_interface_list();
498

    
499
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
500

    
501
		echo <<<EOD
502

    
503
WARNING: all existing VLANs will be cleared if you proceed!
504

    
505
Do you want to proceed [y|n]?
506
EOD;
507

    
508
		if (strcasecmp(chop(fgets($fp)), "y") != 0) {
509
			return;
510
		}
511
	}
512

    
513
	$config['vlans']['vlan'] = array();
514
	echo "\n";
515

    
516
	$vlanif = 0;
517

    
518
	while (1) {
519
		$vlan = array();
520

    
521
		echo "\n\n" . gettext("VLAN Capable interfaces:") . "\n\n";
522
		if (!is_array($iflist)) {
523
			echo gettext("No interfaces found!") . "\n";
524
		} else {
525
			$vlan_capable = 0;
526
			foreach ($iflist as $iface => $ifa) {
527
				if (is_jumbo_capable($iface)) {
528
					echo sprintf("% -8s%s%s\n", $iface, $ifa['mac'],
529
						$ifa['up'] ? "   (up)" : "");
530
					$vlan_capable++;
531
				}
532
			}
533
		}
534

    
535
		if ($vlan_capable == 0) {
536
			echo gettext("No VLAN capable interfaces detected.") . "\n";
537
			return;
538
		}
539

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

    
543
		if ($vlan['if']) {
544
			if (!array_key_exists($vlan['if'], $iflist) or
545
			    !is_jumbo_capable($vlan['if'])) {
546
				printf(gettext("%sInvalid interface name '%s'%s"), "\n", $vlan['if'], "\n");
547
				continue;
548
			}
549
		} else {
550
			break;
551
		}
552

    
553
		echo gettext("Enter the VLAN tag (1-4094):") . " ";
554
		$vlan['tag'] = chop(fgets($fp));
555
		$vlan['vlanif'] = "{$vlan['if']}_vlan{$vlan['tag']}";
556
		if (!is_numericint($vlan['tag']) || ($vlan['tag'] < 1) || ($vlan['tag'] > 4094)) {
557
			printf(gettext("%sInvalid VLAN tag '%s'%s"), "\n", $vlan['tag'], "\n");
558
			continue;
559
		}
560

    
561
		$config['vlans']['vlan'][] = $vlan;
562
		$vlanif++;
563
	}
564
}
565

    
566
?>
(9-9/68)