Project

General

Profile

Download (15.2 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 "\n" . gettext("Do you want to set up VLANs first?");
105
	echo "\n" .
106
		gettext(
107
			"If you are not going to use VLANs, or only for optional interfaces, you should\n" .
108
			"say no here and use the webConfigurator to configure VLANs later, if required.") .
109
		"\n";
110
	echo "\n" . gettext("Do you want to set up VLANs now [y|n]?") . " ";
111

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

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

    
122
   !!! Auto Assigning Interfaces !!!
123

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

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

    
134
Searching for active interfaces...
135

    
136
EOD;
137
			unset($wanif, $lanif);
138

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

    
147
			$lanif = array_shift($plugged_in);
148
			$wanif = array_shift($plugged_in);
149

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

    
159
			echo <<<EOD
160

    
161
Assigned WAN to : $wanif
162
Assigned LAN to : $lanif
163

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

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

    
174
		$config['system']['enablesshd'] = 'enabled';
175
		$key = 'y';
176

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

    
183
		if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
184

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

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

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

    
196
		echo <<<EOD
197

    
198
If you do not know the names of your interfaces, you may choose to use
199
auto-detection. In that case, disconnect all interfaces now before
200
hitting 'a' to initiate auto detection.
201

    
202
EOD;
203

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

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

    
226
			$lanif = chop(fgets($fp));
227

    
228
			if ($lanif == "exit") {
229
				exit;
230
			}
231

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

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

    
247
		/* optional interfaces */
248
		$i = 0;
249
		$optif = array();
250

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

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

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

    
265
				$optif[$i] = chop(fgets($fp));
266

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

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

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

    
296
Error: you cannot assign the same interface name twice!
297

    
298
EOD;
299
					fclose($fp);
300
					return;
301
				}
302
			}
303
		}
304

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

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

    
315
		echo "\n" . gettext("Do you want to proceed [y|n]?") . " ";
316
		$key = chop(fgets($fp));
317
	}
318

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

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

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

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

    
410
		for ($i = 0; $i < count($optif); $i++) {
411
			if (!is_array($config['interfaces']['opt' . ($i+1)])) {
412
				$config['interfaces']['opt' . ($i+1)] = array();
413
			}
414

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

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

    
426
			if (empty($config['interfaces']['opt' . ($i+1)]['descr'])) {
427
				$config['interfaces']['opt' . ($i+1)]['descr'] = "OPT" . ($i+1);
428
				unset($config['interfaces']['opt' . ($i+1)]['enable']);
429
			}
430
		}
431

    
432
		/* remove all other (old) optional interfaces */
433
		for (; isset($config['interfaces']['opt' . ($i+1)]); $i++) {
434
			unset($config['interfaces']['opt' . ($i+1)]);
435
		}
436

    
437
		printf(gettext("%sWriting configuration..."), "\n");
438
		write_config("Console assignment of interfaces");
439
		printf(gettext("done.%s"), "\n");
440

    
441
		fclose($fp);
442

    
443
		if (platform_booting()) {
444
			return;
445
		}
446

    
447
		echo gettext("One moment while we reload the settings...");
448
		echo gettext(" done!") . "\n";
449

    
450
		touch("{$g['tmp_path']}/assign_complete");
451

    
452
	}
453
}
454

    
455
function autodetect_interface($ifname, $fp) {
456
	$iflist_prev = get_interface_list("media");
457
	echo <<<EOD
458

    
459
Connect the {$ifname} interface now and make sure that the link is up.
460
Then press ENTER to continue.
461

    
462
EOD;
463
	fgets($fp);
464
	$iflist = get_interface_list("media");
465

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

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

    
475
	return null;
476
}
477

    
478
function interfaces_setup() {
479
	global $iflist, $config, $g, $fp;
480

    
481
	$iflist = get_interface_list();
482
}
483

    
484
function vlan_setup() {
485
	global $iflist, $config, $g, $fp;
486

    
487
	$iflist = get_interface_list();
488

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

    
493
		if (strcasecmp(chop(fgets($fp)), "y") != 0) {
494
			return;
495
		}
496
	}
497

    
498
	$config['vlans']['vlan'] = array();
499
	echo "\n";
500

    
501
	$vlanif = 0;
502

    
503
	while (1) {
504
		$vlan = array();
505

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

    
520
		if ($vlan_capable == 0) {
521
			echo gettext("No VLAN capable interfaces detected.") . "\n";
522
			return;
523
		}
524

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

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

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

    
546
		$config['vlans']['vlan'][] = $vlan;
547
		$vlanif++;
548
	}
549
}
550

    
551
?>
(9-9/65)