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
|
If you do not know the names of your interfaces, you may choose to use
|
195
|
auto-detection. In that case, disconnect all interfaces now before
|
196
|
hitting 'a' to initiate auto detection.
|
197
|
|
198
|
EOD;
|
199
|
|
200
|
do {
|
201
|
echo "\n" . gettext("Enter the WAN interface name or 'a' for auto-detection:") . " ";
|
202
|
$wanif = chop(fgets($fp));
|
203
|
if ($wanif === "") {
|
204
|
return;
|
205
|
}
|
206
|
if ($wanif === "a")
|
207
|
$wanif = autodetect_interface("WAN", $fp);
|
208
|
else if (!array_key_exists($wanif, $iflist)) {
|
209
|
printf(gettext("%sInvalid interface name '%s'%s"), "\n", $wanif, "\n");
|
210
|
unset($wanif);
|
211
|
continue;
|
212
|
}
|
213
|
} while (!$wanif);
|
214
|
|
215
|
do {
|
216
|
printf(gettext("%sEnter the LAN interface name or 'a' for auto-detection %s" .
|
217
|
"NOTE: this enables full Firewalling/NAT mode.%s" .
|
218
|
"(or nothing if finished):%s"), "\n", "\n", "\n", " ");
|
219
|
|
220
|
$lanif = chop(fgets($fp));
|
221
|
|
222
|
if($lanif == "exit") {
|
223
|
exit;
|
224
|
}
|
225
|
|
226
|
if($lanif == "") {
|
227
|
fclose($fp);
|
228
|
return;
|
229
|
}
|
230
|
|
231
|
if ($lanif === "a")
|
232
|
$lanif = autodetect_interface("LAN", $fp);
|
233
|
else if (!array_key_exists($lanif, $iflist)) {
|
234
|
printf(gettext("%sInvalid interface name '%s'%s"), "\n", $lanif, "\n");
|
235
|
unset($lanif);
|
236
|
continue;
|
237
|
}
|
238
|
} while (!$lanif);
|
239
|
|
240
|
/* optional interfaces */
|
241
|
$i = 0;
|
242
|
$optif = array();
|
243
|
|
244
|
if($lanif <> "") {
|
245
|
while (1) {
|
246
|
if ($optif[$i])
|
247
|
$i++;
|
248
|
$io = $i + 1;
|
249
|
|
250
|
if($config['interfaces']['opt' . $io]['descr'])
|
251
|
printf(gettext("%sOptional interface %s description found: %s"), "\n", $io, $config['interfaces']['opt' . $io]['descr']);
|
252
|
|
253
|
printf(gettext("%sEnter the Optional %s interface name or 'a' for auto-detection%s" .
|
254
|
"(or nothing if finished):%s"), "\n", $io, "\n", " ");
|
255
|
|
256
|
$optif[$i] = chop(fgets($fp));
|
257
|
|
258
|
if ($optif[$i]) {
|
259
|
if ($optif[$i] === "a") {
|
260
|
$ad = autodetect_interface(gettext("Optional") . " " . $io, $fp);
|
261
|
if ($ad)
|
262
|
$optif[$i] = $ad;
|
263
|
else
|
264
|
unset($optif[$i]);
|
265
|
} else if (!array_key_exists($optif[$i], $iflist)) {
|
266
|
printf(gettext("%sInvalid interface name '%s'%s"), "\n", $optif[$i], "\n");
|
267
|
unset($optif[$i]);
|
268
|
continue;
|
269
|
}
|
270
|
} else {
|
271
|
unset($optif[$i]);
|
272
|
break;
|
273
|
}
|
274
|
}
|
275
|
}
|
276
|
|
277
|
/* check for double assignments */
|
278
|
$ifarr = array_merge(array($lanif, $wanif), $optif);
|
279
|
|
280
|
for ($i = 0; $i < (count($ifarr)-1); $i++) {
|
281
|
for ($j = ($i+1); $j < count($ifarr); $j++) {
|
282
|
if ($ifarr[$i] == $ifarr[$j]) {
|
283
|
echo <<<EOD
|
284
|
|
285
|
Error: you cannot assign the same interface name twice!
|
286
|
|
287
|
EOD;
|
288
|
fclose($fp);
|
289
|
return;
|
290
|
}
|
291
|
}
|
292
|
}
|
293
|
|
294
|
echo "\n" . gettext("The interfaces will be assigned as follows:") . "\n\n";
|
295
|
|
296
|
echo "WAN -> " . $wanif . "\n";
|
297
|
if ($lanif != "")
|
298
|
echo "LAN -> " . $lanif . "\n";
|
299
|
for ($i = 0; $i < count($optif); $i++) {
|
300
|
echo "OPT" . ($i+1) . " -> " . $optif[$i] . "\n";
|
301
|
}
|
302
|
|
303
|
echo <<<EOD
|
304
|
|
305
|
Do you want to proceed [y|n]?
|
306
|
EOD;
|
307
|
$key = chop(fgets($fp));
|
308
|
}
|
309
|
|
310
|
if (in_array($key, array('y', 'Y'))) {
|
311
|
if($lanif) {
|
312
|
if (!is_array($config['interfaces']['lan']))
|
313
|
$config['interfaces']['lan'] = array();
|
314
|
$config['interfaces']['lan']['if'] = $lanif;
|
315
|
$config['interfaces']['lan']['enable'] = true;
|
316
|
} elseif (!$g['booting'] && !$auto_assign) {
|
317
|
|
318
|
echo <<<EODD
|
319
|
|
320
|
You have chosen to remove the LAN interface.
|
321
|
|
322
|
Would you like to remove the LAN IP address and
|
323
|
unload the interface now? [y|n]?
|
324
|
EODD;
|
325
|
|
326
|
if (strcasecmp(chop(fgets($fp)), "y") == 0) {
|
327
|
if(isset($config['interfaces']['lan']) && $config['interfaces']['lan']['if'])
|
328
|
mwexec("/sbin/ifconfig " . $config['interfaces']['lan']['if'] . " delete");
|
329
|
}
|
330
|
if(isset($config['interfaces']['lan']))
|
331
|
unset($config['interfaces']['lan']);
|
332
|
if(isset($config['dhcpd']['lan']))
|
333
|
unset($config['dhcpd']['lan']);
|
334
|
if(isset($config['interfaces']['lan']['if']))
|
335
|
unset($config['interfaces']['lan']['if']);
|
336
|
if(isset($config['interfaces']['wan']['blockpriv']))
|
337
|
unset($config['interfaces']['wan']['blockpriv']);
|
338
|
if(isset($config['shaper']))
|
339
|
unset($config['shaper']);
|
340
|
if(isset($config['ezshaper']))
|
341
|
unset($config['ezshaper']);
|
342
|
if(isset($config['nat']))
|
343
|
unset($config['nat']);
|
344
|
} else {
|
345
|
if(isset($config['interfaces']['lan']['if']))
|
346
|
mwexec("/sbin/ifconfig " . $config['interfaces']['lan']['if'] . " delete");
|
347
|
if(isset($config['interfaces']['lan']))
|
348
|
unset($config['interfaces']['lan']);
|
349
|
if(isset($config['dhcpd']['lan']))
|
350
|
unset($config['dhcpd']['lan']);
|
351
|
if(isset($config['interfaces']['lan']['if']))
|
352
|
unset($config['interfaces']['lan']['if']);
|
353
|
if(isset($config['interfaces']['wan']['blockpriv']))
|
354
|
unset($config['interfaces']['wan']['blockpriv']);
|
355
|
if(isset($config['shaper']))
|
356
|
unset($config['shaper']);
|
357
|
if(isset($config['ezshaper']))
|
358
|
unset($config['ezshaper']);
|
359
|
if(isset($config['nat']))
|
360
|
unset($config['nat']);
|
361
|
}
|
362
|
if (preg_match($g['wireless_regex'], $lanif)) {
|
363
|
if (is_array($config['interfaces']['lan']) &&
|
364
|
(!is_array($config['interfaces']['lan']['wireless'])))
|
365
|
$config['interfaces']['lan']['wireless'] = array();
|
366
|
} else {
|
367
|
if (isset($config['interfaces']['lan']))
|
368
|
unset($config['interfaces']['lan']['wireless']);
|
369
|
}
|
370
|
|
371
|
if (!is_array($config['interfaces']['wan']))
|
372
|
$config['interfaces']['wan'] = array();
|
373
|
$config['interfaces']['wan']['if'] = $wanif;
|
374
|
$config['interfaces']['wan']['enable'] = true;
|
375
|
if (preg_match($g['wireless_regex'], $wanif)) {
|
376
|
if (is_array($config['interfaces']['wan']) &&
|
377
|
(!is_array($config['interfaces']['wan']['wireless'])))
|
378
|
$config['interfaces']['wan']['wireless'] = array();
|
379
|
} else {
|
380
|
if (isset($config['interfaces']['wan']))
|
381
|
unset($config['interfaces']['wan']['wireless']);
|
382
|
}
|
383
|
|
384
|
for ($i = 0; $i < count($optif); $i++) {
|
385
|
if (!is_array($config['interfaces']['opt' . ($i+1)]))
|
386
|
$config['interfaces']['opt' . ($i+1)] = array();
|
387
|
|
388
|
$config['interfaces']['opt' . ($i+1)]['if'] = $optif[$i];
|
389
|
|
390
|
/* wireless interface? */
|
391
|
if (preg_match($g['wireless_regex'], $optif[$i])) {
|
392
|
if (!is_array($config['interfaces']['opt' . ($i+1)]['wireless']))
|
393
|
$config['interfaces']['opt' . ($i+1)]['wireless'] = array();
|
394
|
} else {
|
395
|
unset($config['interfaces']['opt' . ($i+1)]['wireless']);
|
396
|
}
|
397
|
|
398
|
if (empty($config['interfaces']['opt' . ($i+1)]['descr'])) {
|
399
|
$config['interfaces']['opt' . ($i+1)]['descr'] = "OPT" . ($i+1);
|
400
|
unset($config['interfaces']['opt' . ($i+1)]['enable']);
|
401
|
}
|
402
|
}
|
403
|
|
404
|
/* remove all other (old) optional interfaces */
|
405
|
for (; isset($config['interfaces']['opt' . ($i+1)]); $i++)
|
406
|
unset($config['interfaces']['opt' . ($i+1)]);
|
407
|
|
408
|
printf(gettext("%sWriting configuration..."), "\n");
|
409
|
write_config("Console assignment of interfaces");
|
410
|
printf(gettext("done.%s"), "\n");
|
411
|
|
412
|
fclose($fp);
|
413
|
|
414
|
if($g['booting'])
|
415
|
return;
|
416
|
|
417
|
echo gettext("One moment while we reload the settings...");
|
418
|
|
419
|
$g['booting'] = false;
|
420
|
|
421
|
echo gettext(" done!") . "\n";
|
422
|
|
423
|
touch("{$g['tmp_path']}/assign_complete");
|
424
|
|
425
|
}
|
426
|
}
|
427
|
|
428
|
function autodetect_interface($ifname, $fp) {
|
429
|
$iflist_prev = get_interface_list("media");
|
430
|
echo <<<EOD
|
431
|
|
432
|
Connect the {$ifname} interface now and make sure that the link is up.
|
433
|
Then press ENTER to continue.
|
434
|
|
435
|
EOD;
|
436
|
fgets($fp);
|
437
|
$iflist = get_interface_list("media");
|
438
|
|
439
|
foreach ($iflist_prev as $ifn => $ifa) {
|
440
|
if (!$ifa['up'] && $iflist[$ifn]['up']) {
|
441
|
printf(gettext("Detected link-up on interface %s.%s"), $ifn, "\n");
|
442
|
return $ifn;
|
443
|
}
|
444
|
}
|
445
|
|
446
|
printf(gettext("No link-up detected.%s"), "\n");
|
447
|
|
448
|
return null;
|
449
|
}
|
450
|
|
451
|
function interfaces_setup() {
|
452
|
global $iflist, $config, $g, $fp;
|
453
|
|
454
|
$iflist = get_interface_list();
|
455
|
|
456
|
|
457
|
}
|
458
|
|
459
|
function vlan_setup() {
|
460
|
global $iflist, $config, $g, $fp;
|
461
|
|
462
|
$iflist = get_interface_list();
|
463
|
|
464
|
if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
|
465
|
|
466
|
echo <<<EOD
|
467
|
|
468
|
WARNING: all existing VLANs will be cleared if you proceed!
|
469
|
|
470
|
Do you want to proceed [y|n]?
|
471
|
EOD;
|
472
|
|
473
|
if (strcasecmp(chop(fgets($fp)), "y") != 0)
|
474
|
return;
|
475
|
}
|
476
|
|
477
|
$config['vlans']['vlan'] = array();
|
478
|
echo "\n";
|
479
|
|
480
|
$vlanif = 0;
|
481
|
|
482
|
while (1) {
|
483
|
$vlan = array();
|
484
|
|
485
|
echo "\n\n" . gettext("VLAN Capable interfaces:") . "\n\n";
|
486
|
if(!is_array($iflist)) {
|
487
|
echo gettext("No interfaces found!") . "\n";
|
488
|
} else {
|
489
|
$vlan_capable=0;
|
490
|
foreach ($iflist as $iface => $ifa) {
|
491
|
if (is_jumbo_capable($iface)) {
|
492
|
echo sprintf("% -8s%s%s\n", $iface, $ifa['mac'],
|
493
|
$ifa['up'] ? " (up)" : "");
|
494
|
$vlan_capable++;
|
495
|
}
|
496
|
}
|
497
|
}
|
498
|
|
499
|
if($vlan_capable == 0) {
|
500
|
echo gettext("No VLAN capable interfaces detected.") . "\n";
|
501
|
return;
|
502
|
}
|
503
|
|
504
|
echo "\n" . gettext("Enter the parent interface name for the new VLAN (or nothing if finished):") . " ";
|
505
|
$vlan['if'] = chop(fgets($fp));
|
506
|
|
507
|
if ($vlan['if']) {
|
508
|
if (!array_key_exists($vlan['if'], $iflist) or
|
509
|
!is_jumbo_capable($vlan['if'])) {
|
510
|
printf(gettext("%sInvalid interface name '%s'%s"), "\n", $vlan['if'], "\n");
|
511
|
continue;
|
512
|
}
|
513
|
} else {
|
514
|
break;
|
515
|
}
|
516
|
|
517
|
echo gettext("Enter the VLAN tag (1-4094):") . " ";
|
518
|
$vlan['tag'] = chop(fgets($fp));
|
519
|
$vlan['vlanif'] = "{$vlan['if']}_vlan{$vlan['tag']}";
|
520
|
if (!is_numericint($vlan['tag']) || ($vlan['tag'] < 1) || ($vlan['tag'] > 4094)) {
|
521
|
printf(gettext("%sInvalid VLAN tag '%s'%s"), "\n", $vlan['tag'], "\n");
|
522
|
continue;
|
523
|
}
|
524
|
|
525
|
$config['vlans']['vlan'][] = $vlan;
|
526
|
$vlanif++;
|
527
|
}
|
528
|
}
|
529
|
|
530
|
?>
|