Project

General

Profile

Download (14 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/sysctl	/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
	$avail = $memory[0];
55

    
56
	if($avail < $g['minimum_ram_warning']) {
57
		echo "\n\n\n";
58
		echo "DANGER!  WARNING!  ACHTUNG!\n\n";
59
		echo "{$g['product_name']} requires *AT LEAST* {$g['minimum_ram_warning_text']} RAM to function correctly.\n";
60
		echo "Only ({$avail}) MB RAM has been detected.\n";
61
		echo "\nPress ENTER to continue. ";
62
		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
	if ((ereg("cdrom", $g['platform'])) && is_interface_mismatch())
73
		$auto_assign = false;
74

    
75
	echo <<<EOD
76

    
77
Valid interfaces are:
78

    
79

    
80
EOD;
81

    
82
	if(!is_array($iflist)) {
83
		echo "No interfaces found!\n";
84
		$iflist = array();
85
	} else {
86
		foreach ($iflist as $iface => $ifa) {
87
			echo sprintf("% -6s%s%s\t%s\n", $iface, $ifa['mac'],
88
				$ifa['up'] ? "   (up)" : "   (down)", $ifa['dmesg']);
89
		}
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
			echo "\n\nVLAN 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
			}
189
		}
190
	
191
		echo <<<EOD
192
	
193
*NOTE*  {$g['product_name']} requires {$g['minimum_nic_count_text']} assigned interfaces to function.
194
        If you do not have {$g['minimum_nic_count_text']} interfaces you CANNOT continue. 
195

    
196
        If you do not have at least {$g['minimum_nic_count']} *REAL* network interface cards
197
        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
			echo "\nEnter the WAN interface name or 'a' for auto-detection: ";
208
			$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
				echo "\nInvalid interface name '{$wanif}'\n";
216
				unset($wanif);
217
				continue;
218
			}
219
		} while (!$wanif);
220
	
221
		do {
222
			echo "\nEnter the LAN interface name or 'a' for auto-detection \n" .
223
			    "NOTE: this enables full Firewalling/NAT mode.\n" .
224
				"(or nothing if finished): ";
225
	
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
				echo "\nInvalid interface name '{$lanif}'\n";
245
				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
				$i1 = $i + 1;
259
		
260
				if($config['interfaces']['opt' . $i1]['descr'])
261
					echo "\nOptional interface {$i1} description found: {$config['interfaces']['opt' . $i1]['descr']}";
262
	
263
				echo "\nEnter the Optional {$i1} interface name or 'a' for auto-detection\n" .
264
					"(or nothing if finished): ";
265
		
266
				$optif[$i] = chop(fgets($fp));
267
		
268
				if ($optif[$i]) {
269
					if ($optif[$i] === "a") {
270
						$ad = autodetect_interface("Optional " . $i1, $fp);
271
						if ($ad)
272
							$optif[$i] = $ad;
273
						else
274
							unset($optif[$i]);
275
					} else if (!array_key_exists($optif[$i], $iflist)) {
276
						echo "\nInvalid interface name '{$optif[$i]}'\n";
277
						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
		echo "\nThe interfaces will be assigned as follows: \n\n";
305
	
306
		if ($lanif != "")
307
			echo "LAN  -> " . $lanif . "\n";
308
		echo "WAN  -> " . $wanif . "\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
			if (!is_array($config['interfaces']['lan']))
323
				$config['interfaces']['lan'] = array();
324
			$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
					if(isset($config['interfaces']['lan']) && $config['interfaces']['lan']['if'])
338
						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
			if (isset($config['interfaces']['lan']))
378
				unset($config['interfaces']['lan']['wireless']);
379
		}
380

    
381
		if (!is_array($config['interfaces']['wan']))
382
			$config['interfaces']['wan'] = array();
383
		$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
			if (isset($config['interfaces']['wan']))
391
				unset($config['interfaces']['wan']['wireless']);
392
		}
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
			unset($config['interfaces']['opt' . ($i+1)]['enable']);
409
			$config['interfaces']['opt' . ($i+1)]['descr'] = "OPT" . ($i+1);
410
		}
411

    
412
		/* remove all other (old) optional interfaces */
413
		for (; isset($config['interfaces']['opt' . ($i+1)]); $i++)
414
			unset($config['interfaces']['opt' . ($i+1)]);
415

    
416
		echo "\nWriting configuration...";
417
		write_config();
418
		echo "done.\n";
419

    
420
		fclose($fp);
421

    
422
		if($g['booting'])
423
			return;
424

    
425
		echo "One moment while we reload the settings...";
426

    
427
		$g['booting'] = false;
428

    
429
		echo " done!\n";
430

    
431
		touch("{$g['tmp_path']}/assign_complete");
432

    
433
	}
434
}
435

    
436
function autodetect_interface($ifname, $fp) {
437
	$iflist_prev = get_interface_list("media");
438
	echo <<<EOD
439

    
440
Connect the {$ifname} interface now and make sure that the link is up.
441
Then press ENTER to continue.
442

    
443
EOD;
444
	fgets($fp);
445
	$iflist = get_interface_list("media");
446

    
447
	foreach ($iflist_prev as $ifn => $ifa) {
448
		if (!$ifa['up'] && $iflist[$ifn]['up']) {
449
			echo "Detected link-up on interface {$ifn}.\n";
450
			return $ifn;
451
		}
452
	}
453

    
454
	echo "No link-up detected.\n";
455

    
456
	return null;
457
}
458

    
459
function interfaces_setup() {
460
	global $iflist, $config, $g, $fp;
461

    
462
	$iflist = get_interface_list();
463

    
464
	
465
}
466

    
467
function vlan_setup() {
468
	global $iflist, $config, $g, $fp;
469

    
470
	$iflist = get_interface_list();
471

    
472
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
473

    
474
	echo <<<EOD
475

    
476
WARNING: all existing VLANs will be cleared if you proceed!
477

    
478
Do you want to proceed [y|n]?
479
EOD;
480

    
481
	if (strcasecmp(chop(fgets($fp)), "y") != 0)
482
		return;
483
	}
484

    
485
	$config['vlans']['vlan'] = array();
486
	echo "\n";
487

    
488
	$vlanif = 0;
489

    
490
	while (1) {
491
		$vlan = array();
492

    
493
		echo "\n\nVLAN Capable interfaces:\n\n";
494
		if(!is_array($iflist)) {
495
			echo "No interfaces found!\n";
496
		} else {
497
			$vlan_capable=0;
498
			foreach ($iflist as $iface => $ifa) {
499
				if (is_jumbo_capable($iface)) {
500
					echo sprintf("% -8s%s%s\n", $iface, $ifa['mac'],
501
						$ifa['up'] ? "   (up)" : "");
502
					$vlan_capable++;
503
				}
504
			}
505
		}
506

    
507
		if($vlan_capable == 0) {
508
			echo "No VLAN capable interfaces detected.\n";
509
			return;
510
		}
511

    
512
		echo "\nEnter the parent interface name for the new VLAN (or nothing if finished): ";
513
		$vlan['if'] = chop(fgets($fp));
514

    
515
		if ($vlan['if']) {
516
			if (!array_key_exists($vlan['if'], $iflist) or
517
			    !is_jumbo_capable($vlan['if'])) {
518
				echo "\nInvalid interface name '{$vlan['if']}'\n";
519
				continue;
520
			}
521
		} else {
522
			break;
523
		}
524

    
525
		echo "Enter the VLAN tag (1-4094): ";
526
		$vlan['tag'] = chop(fgets($fp));
527
		$vlan['vlanif'] = "{$vlan['if']}_vlan{$vlan['tag']}";
528
		if (!is_numericint($vlan['tag']) || ($vlan['tag'] < 1) || ($vlan['tag'] > 4094)) {
529
			echo "\nInvalid VLAN tag '{$vlan['tag']}'\n";
530
			continue;
531
		}
532
		
533
		$config['vlans']['vlan'][] = $vlan;
534
		$vlanif++;
535
	}
536
}
537

    
538
?>
(10-10/61)