Projet

Général

Profil

Télécharger (14,7 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / config.console.inc @ f049d544

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
	echo <<<EOD
77

    
78
Valid interfaces are:
79

    
80

    
81
EOD;
82

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

    
93
	if ($auto_assign) {
94
		echo <<<EOD
95
		
96
		!!! LiveCD Detected: Auto Interface Option !!!!
97
BEGIN MANUAL CONFIGURATION OR WE WILL PROCEED WITH AUTO CONFIGURATION.
98

    
99
EOD;
100
	}	
101
	
102
	echo <<<EOD
103

    
104
Do you want to set up VLANs first? 
105

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

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

    
112
	if ($auto_assign) {
113
		$key = timeout();
114

    
115
	} else
116
		$key = chop(fgets($fp));
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
		} while (!isset($wanif));
172

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

    
176
	} else {		//Manually assign interfaces	
177
		if (in_array($key, array('y', 'Y')))
178
			vlan_setup();
179
	
180
		if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
181
	
182
			echo "\n\n" . gettext("VLAN interfaces:") . "\n\n";
183
			foreach ($config['vlans']['vlan'] as $vlan) {
184
	
185
				echo sprintf("% -16s%s\n", "{$vlan['if']}_vlan{$vlan['tag']}",
186
					"VLAN tag {$vlan['tag']}, parent interface {$vlan['if']}");
187
	
188
				$iflist[$vlan['if'] . '_vlan' . $vlan['tag']] = array();
189
			}
190
		}
191
	
192
		echo <<<EOD
193
	
194
*NOTE*  {$g['product_name']} requires {$g['minimum_nic_count_text']} assigned interface(s) to function.
195
        If you do not have {$g['minimum_nic_count_text']} interfaces you CANNOT continue. 
196

    
197
        If you do not have at least {$g['minimum_nic_count']} *REAL* network interface card(s)
198
        or one interface with multiple VLANs then {$g['product_name']}
199
        *WILL NOT* function correctly.
200

    
201
If you do not know the names of your interfaces, you may choose to use
202
auto-detection. In that case, disconnect all interfaces now before
203
hitting 'a' to initiate auto detection.
204
	
205
EOD;
206
	
207
		do {
208
			echo "\n" . gettext("Enter the WAN interface name or 'a' for auto-detection:") . " ";
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
		} while (!$wanif);
221
	
222
		do {
223
			printf(gettext("%sEnter the LAN interface name or 'a' for auto-detection %s" .
224
			    "NOTE: this enables full Firewalling/NAT mode.%s" .
225
				"(or nothing if finished):%s"), "\n", "\n", "\n", " ");
226
	
227
			$lanif = chop(fgets($fp));
228
			
229
			if($lanif == "exit") {
230
				exit;
231
			}
232
			
233
			if($lanif == "") {
234
				if($g['minimum_nic_count'] < 2) {
235
					break;	
236
				} else {
237
					fclose($fp);
238
					return;
239
				}
240
			}
241
	
242
			if ($lanif === "a")
243
				$lanif = autodetect_interface("LAN", $fp);
244
			else if (!array_key_exists($lanif, $iflist)) {
245
				printf(gettext("%sInvalid interface name '%s'%s"), "\n", $lanif, "\n");
246
				unset($lanif);
247
				continue;
248
			}
249
		} while (!$lanif);
250
	
251
		/* optional interfaces */
252
		$i = 0;
253
		$optif = array();
254
	
255
		if($lanif <> "") {
256
			while (1) {
257
				if ($optif[$i])
258
					$i++;
259
				$io = $i + 1;
260

    
261
				if($config['interfaces']['opt' . $io]['descr'])
262
					printf(gettext("%sOptional interface %s description found: %s"), "\n", $io, $config['interfaces']['opt' . $io]['descr']);
263
	
264
				printf(gettext("%sEnter the Optional %s interface name or 'a' for auto-detection%s" .
265
					"(or nothing if finished):%s"), "\n", $io, "\n", " ");
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
					} else if (!array_key_exists($optif[$i], $iflist)) {
277
						printf(gettext("%sInvalid interface name '%s'%s"), "\n", $optif[$i], "\n");
278
						unset($optif[$i]);
279
						continue;
280
					}
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
		for ($i = 0; $i < count($optif); $i++) {
311
			echo "OPT" . ($i+1) . " -> " . $optif[$i] . "\n";
312
		}
313
	
314
		echo <<<EOD
315
	
316
Do you want to proceed [y|n]?
317
EOD;
318
			$key = chop(fgets($fp));		
319
	}
320

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

    
329
echo <<<EODD
330

    
331
You have chosen to remove the LAN interface.
332

    
333
Would you like to remove the LAN IP address and
334
unload the interface now? [y|n]? 
335
EODD;
336

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

    
382
		if (!is_array($config['interfaces']['wan']))
383
			$config['interfaces']['wan'] = array();
384
		$config['interfaces']['wan']['if'] = $wanif;
385
		$config['interfaces']['wan']['enable'] = true;
386
		if (preg_match($g['wireless_regex'], $wanif)) {
387
			if (is_array($config['interfaces']['wan']) &&
388
				(!is_array($config['interfaces']['wan']['wireless'])))
389
				$config['interfaces']['wan']['wireless'] = array();
390
		} else {
391
			if (isset($config['interfaces']['wan']))
392
				unset($config['interfaces']['wan']['wireless']);
393
		}
394

    
395
		for ($i = 0; $i < count($optif); $i++) {
396
			if (!is_array($config['interfaces']['opt' . ($i+1)]))
397
				$config['interfaces']['opt' . ($i+1)] = array();
398

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

    
401
			/* wireless interface? */
402
			if (preg_match($g['wireless_regex'], $optif[$i])) {
403
				if (!is_array($config['interfaces']['opt' . ($i+1)]['wireless']))
404
					$config['interfaces']['opt' . ($i+1)]['wireless'] = array();
405
			} else {
406
				unset($config['interfaces']['opt' . ($i+1)]['wireless']);
407
			}
408

    
409
			if (empty($config['interfaces']['opt' . ($i+1)]['descr'])) {
410
				$config['interfaces']['opt' . ($i+1)]['descr'] = "OPT" . ($i+1);
411
				unset($config['interfaces']['opt' . ($i+1)]['enable']);
412
			}
413
		}
414

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

    
419
		printf(gettext("%sWriting configuration..."), "\n");
420
		write_config("Console assignment of interfaces");
421
		printf(gettext("done.%s"), "\n");
422

    
423
		fclose($fp);
424

    
425
		if($g['booting'])
426
			return;
427

    
428
		echo gettext("One moment while we reload the settings...");
429

    
430
		$g['booting'] = false;
431

    
432
		echo gettext(" done!") . "\n";
433

    
434
		touch("{$g['tmp_path']}/assign_complete");
435

    
436
	}
437
}
438

    
439
function autodetect_interface($ifname, $fp) {
440
	$iflist_prev = get_interface_list("media");
441
	echo <<<EOD
442

    
443
Connect the {$ifname} interface now and make sure that the link is up.
444
Then press ENTER to continue.
445

    
446
EOD;
447
	fgets($fp);
448
	$iflist = get_interface_list("media");
449

    
450
	foreach ($iflist_prev as $ifn => $ifa) {
451
		if (!$ifa['up'] && $iflist[$ifn]['up']) {
452
			printf(gettext("Detected link-up on interface %s.%s"), $ifn, "\n");
453
			return $ifn;
454
		}
455
	}
456

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

    
459
	return null;
460
}
461

    
462
function interfaces_setup() {
463
	global $iflist, $config, $g, $fp;
464

    
465
	$iflist = get_interface_list();
466

    
467
	
468
}
469

    
470
function vlan_setup() {
471
	global $iflist, $config, $g, $fp;
472

    
473
	$iflist = get_interface_list();
474

    
475
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
476

    
477
	echo <<<EOD
478

    
479
WARNING: all existing VLANs will be cleared if you proceed!
480

    
481
Do you want to proceed [y|n]?
482
EOD;
483

    
484
	if (strcasecmp(chop(fgets($fp)), "y") != 0)
485
		return;
486
	}
487

    
488
	$config['vlans']['vlan'] = array();
489
	echo "\n";
490

    
491
	$vlanif = 0;
492

    
493
	while (1) {
494
		$vlan = array();
495

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

    
510
		if($vlan_capable == 0) {
511
			echo gettext("No VLAN capable interfaces detected.") . "\n";
512
			return;
513
		}
514

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

    
518
		if ($vlan['if']) {
519
			if (!array_key_exists($vlan['if'], $iflist) or
520
			    !is_jumbo_capable($vlan['if'])) {
521
				printf(gettext("%sInvalid interface name '%s'%s"), "\n", $vlan['if'], "\n");
522
				continue;
523
			}
524
		} else {
525
			break;
526
		}
527

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

    
541
?>
(10-10/68)