Project

General

Profile

Download (14.5 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
			echo sprintf("% -7s%s %s %s\n", $iface, $ifa['mac'],
90
				$ifa['up'] ? "  (up)" : "(down)", $ifa['dmesg']);
91
		}
92
	}
93

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

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

    
100
EOD;
101
	}
102

    
103
	echo <<<EOD
104

    
105
Do you want to set up VLANs first? 
106

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

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

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

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

    
123
   !!! Auto Assigning Interfaces !!!
124

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

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

    
135
Searching for active interfaces...
136

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

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

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

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

    
160
			echo <<<EOD
161

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

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

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

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

    
178
	} else {		//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
			}
193
		}
194

    
195
		echo <<<EOD
196

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

    
201
EOD;
202

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

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

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

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

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

    
234
			if ($lanif === "a") {
235
				$lanif = autodetect_interface("LAN", $fp);
236
			} else if (!array_key_exists($lanif, $iflist)) {
237
				printf(gettext("%sInvalid interface name '%s'%s"), "\n", $lanif, "\n");
238
				unset($lanif);
239
				continue;
240
			}
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
					"(or nothing if finished):%s"), "\n", $io, "\n", " ");
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
				} else {
277
					unset($optif[$i]);
278
					break;
279
				}
280
			}
281
		}
282

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

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

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

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

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

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

    
310
		echo <<<EOD
311

    
312
Do you want to proceed [y|n]?
313
EOD;
314
		$key = chop(fgets($fp));
315
	}
316

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

    
326
echo <<<EODD
327

    
328
You have chosen to remove the LAN interface.
329

    
330
Would you like to remove the LAN IP address and
331
unload the interface now? [y|n]? 
332
EODD;
333

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

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

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

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

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

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

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

    
440
		printf(gettext("%sWriting configuration..."), "\n");
441
		write_config("Console assignment of interfaces");
442
		printf(gettext("done.%s"), "\n");
443

    
444
		fclose($fp);
445

    
446
		if (platform_booting()) {
447
			return;
448
		}
449

    
450
		echo gettext("One moment while we reload the settings...");
451
		echo gettext(" done!") . "\n";
452

    
453
		touch("{$g['tmp_path']}/assign_complete");
454

    
455
	}
456
}
457

    
458
function autodetect_interface($ifname, $fp) {
459
	$iflist_prev = get_interface_list("media");
460
	echo <<<EOD
461

    
462
Connect the {$ifname} interface now and make sure that the link is up.
463
Then press ENTER to continue.
464

    
465
EOD;
466
	fgets($fp);
467
	$iflist = get_interface_list("media");
468

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

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

    
478
	return null;
479
}
480

    
481
function interfaces_setup() {
482
	global $iflist, $config, $g, $fp;
483

    
484
	$iflist = get_interface_list();
485
}
486

    
487
function vlan_setup() {
488
	global $iflist, $config, $g, $fp;
489

    
490
	$iflist = get_interface_list();
491

    
492
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
493

    
494
		echo <<<EOD
495

    
496
WARNING: all existing VLANs will be cleared if you proceed!
497

    
498
Do you want to proceed [y|n]?
499
EOD;
500

    
501
		if (strcasecmp(chop(fgets($fp)), "y") != 0) {
502
			return;
503
		}
504
	}
505

    
506
	$config['vlans']['vlan'] = array();
507
	echo "\n";
508

    
509
	$vlanif = 0;
510

    
511
	while (1) {
512
		$vlan = array();
513

    
514
		echo "\n\n" . gettext("VLAN Capable interfaces:") . "\n\n";
515
		if (!is_array($iflist)) {
516
			echo gettext("No interfaces found!") . "\n";
517
		} else {
518
			$vlan_capable=0;
519
			foreach ($iflist as $iface => $ifa) {
520
				if (is_jumbo_capable($iface)) {
521
					echo sprintf("% -8s%s%s\n", $iface, $ifa['mac'],
522
						$ifa['up'] ? "   (up)" : "");
523
					$vlan_capable++;
524
				}
525
			}
526
		}
527

    
528
		if ($vlan_capable == 0) {
529
			echo gettext("No VLAN capable interfaces detected.") . "\n";
530
			return;
531
		}
532

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

    
536
		if ($vlan['if']) {
537
			if (!array_key_exists($vlan['if'], $iflist) or
538
			    !is_jumbo_capable($vlan['if'])) {
539
				printf(gettext("%sInvalid interface name '%s'%s"), "\n", $vlan['if'], "\n");
540
				continue;
541
			}
542
		} else {
543
			break;
544
		}
545

    
546
		echo gettext("Enter the VLAN tag (1-4094):") . " ";
547
		$vlan['tag'] = chop(fgets($fp));
548
		$vlan['vlanif'] = "{$vlan['if']}_vlan{$vlan['tag']}";
549
		if (!is_numericint($vlan['tag']) || ($vlan['tag'] < 1) || ($vlan['tag'] > 4094)) {
550
			printf(gettext("%sInvalid VLAN tag '%s'%s"), "\n", $vlan['tag'], "\n");
551
			continue;
552
		}
553

    
554
		$config['vlans']['vlan'][] = $vlan;
555
		$vlanif++;
556
	}
557
}
558

    
559
?>
(10-10/68)