1
|
<?php
|
2
|
/*
|
3
|
config.inc
|
4
|
Copyright (C) 2004 Scott Ullrich
|
5
|
All rights reserved.
|
6
|
|
7
|
originally part of m0n0wall (http://m0n0.ch/wall)
|
8
|
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
9
|
All rights reserved.
|
10
|
|
11
|
Redistribution and use in source and binary forms, with or without
|
12
|
modification, are permitted provided that the following conditions are met:
|
13
|
|
14
|
1. Redistributions of source code must retain the above copyright notice,
|
15
|
this list of conditions and the following disclaimer.
|
16
|
|
17
|
2. Redistributions in binary form must reproduce the above copyright
|
18
|
notice, this list of conditions and the following disclaimer in the
|
19
|
documentation and/or other materials provided with the distribution.
|
20
|
|
21
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
22
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
23
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
24
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
25
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
26
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
27
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
28
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
29
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30
|
POSSIBILITY OF SUCH DAMAGE.
|
31
|
*/
|
32
|
|
33
|
/* include globals/utility/XML parser files */
|
34
|
require_once("globals.inc");
|
35
|
require_once("util.inc");
|
36
|
require_once("pfsense-utils.inc");
|
37
|
require_once("xmlparse.inc");
|
38
|
|
39
|
/* read platform */
|
40
|
if (file_exists("{$g['etc_path']}/platform")) {
|
41
|
$g['platform'] = chop(file_get_contents("{$g['etc_path']}/platform"));
|
42
|
} else {
|
43
|
$g['platform'] = "unknown";
|
44
|
}
|
45
|
|
46
|
if ($g['booting']) {
|
47
|
/* find the device where config.xml resides and write out an fstab */
|
48
|
unset($cfgdevice);
|
49
|
|
50
|
/* check if there's already an fstab (NFS booting?) */
|
51
|
if (!file_exists("{$g['etc_path']}/fstab")) {
|
52
|
|
53
|
if (strstr($g['platform'], "cdrom")) {
|
54
|
/* config is on floppy disk for CD-ROM version */
|
55
|
$cfgdevice = $cfgpartition = "fd0";
|
56
|
$cfgfstype = "msdos";
|
57
|
} else {
|
58
|
/* probe kernel known disks until we find one with config.xml */
|
59
|
$disks = explode(" ", trim(preg_replace("/kern.disks: /", "", exec("/sbin/sysctl kern.disks"))));
|
60
|
foreach ($disks as $mountdisk) {
|
61
|
/* skip mfs mounted filesystems */
|
62
|
if (strstr($mountdisk, "md"))
|
63
|
continue;
|
64
|
if (mwexec("/sbin/mount -r /dev/{$mountdisk}a {$g['cf_path']}") == 0) {
|
65
|
if (file_exists("{$g['cf_conf_path']}/config.xml")) {
|
66
|
/* found it */
|
67
|
$cfgdevice = $mountdisk;
|
68
|
$cfgpartition = $cfgdevice . "a";
|
69
|
$cfgfstype = "ufs";
|
70
|
echo "Found configuration on $cfgdevice.\n";
|
71
|
}
|
72
|
|
73
|
mwexec("/sbin/umount -f {$g['cf_path']}");
|
74
|
|
75
|
if ($cfgdevice)
|
76
|
break;
|
77
|
}
|
78
|
}
|
79
|
}
|
80
|
|
81
|
if (!$cfgdevice) {
|
82
|
/* no device found, print an error and die */
|
83
|
echo <<<EOD
|
84
|
|
85
|
|
86
|
*******************************************************************************
|
87
|
* FATAL ERROR *
|
88
|
* The device that contains the configuration file (config.xml) could not be *
|
89
|
* found. pfSense cannot continue booting. *
|
90
|
*******************************************************************************
|
91
|
|
92
|
|
93
|
EOD;
|
94
|
|
95
|
mwexec("/sbin/halt");
|
96
|
exit;
|
97
|
}
|
98
|
|
99
|
/* write device name to a file for rc.firmware */
|
100
|
$fd = fopen("{$g['varetc_path']}/cfdevice", "w");
|
101
|
fwrite($fd, $cfgdevice . "\n");
|
102
|
fclose($fd);
|
103
|
|
104
|
/* write out an fstab */
|
105
|
$fd = fopen("{$g['etc_path']}/fstab", "w");
|
106
|
|
107
|
$fstab = "/dev/{$cfgpartition} {$g['cf_path']} {$cfgfstype} ro 1 1\n";
|
108
|
$fstab .= "proc /proc procfs rw 0 0\n";
|
109
|
|
110
|
fwrite($fd, $fstab);
|
111
|
fclose($fd);
|
112
|
}
|
113
|
|
114
|
/* mount all filesystems */
|
115
|
mwexec("/sbin/mount -a");
|
116
|
}
|
117
|
|
118
|
/* parse configuration */
|
119
|
if (!$noparseconfig) {
|
120
|
|
121
|
config_lock();
|
122
|
|
123
|
/* see if there's a newer cache file */
|
124
|
if (file_exists("{$g['tmp_path']}/config.cache") &&
|
125
|
(filemtime("{$g['tmp_path']}/config.cache") >=
|
126
|
filemtime("{$g['conf_path']}/config.xml"))) {
|
127
|
|
128
|
/* read cache */
|
129
|
$config = unserialize(file_get_contents("{$g['tmp_path']}/config.cache"));
|
130
|
|
131
|
} else {
|
132
|
|
133
|
if (!file_exists("{$g['conf_path']}/config.xml")) {
|
134
|
if ($g['booting']) {
|
135
|
if (strstr($g['platform'], "cdrom")) {
|
136
|
/* try copying the default config. to the floppy */
|
137
|
reset_factory_defaults();
|
138
|
|
139
|
echo "No XML configuration file found - using factory defaults.\n";
|
140
|
echo "Make sure that the configuration floppy disk with the conf/config.xml\n";
|
141
|
echo "file is inserted. If it isn't, your configuration changes will be lost\n";
|
142
|
echo "on reboot.\n";
|
143
|
} else {
|
144
|
echo "XML configuration file not found. pfSense cannot continue booting.\n";
|
145
|
mwexec("/sbin/halt");
|
146
|
exit;
|
147
|
}
|
148
|
} else {
|
149
|
config_unlock();
|
150
|
exit(0);
|
151
|
}
|
152
|
}
|
153
|
|
154
|
$config = parse_xml_config("{$g['conf_path']}/config.xml", $g['xml_rootobj']);
|
155
|
|
156
|
if ((float)$config['version'] > (float)$g['latest_config']) {
|
157
|
if ($g['booting']) {
|
158
|
echo <<<EOD
|
159
|
|
160
|
|
161
|
*******************************************************************************
|
162
|
* WARNING! *
|
163
|
* The current configuration has been created with a newer version of pfSense *
|
164
|
* than this one! This can lead to serious misbehavior and even security *
|
165
|
* holes! You are urged to either upgrade to a newer version of pfSense or *
|
166
|
* revert to the default configuration immediately! *
|
167
|
*******************************************************************************
|
168
|
|
169
|
|
170
|
EOD;
|
171
|
}
|
172
|
}
|
173
|
|
174
|
/* write config cache */
|
175
|
$fd = @fopen("{$g['tmp_path']}/config.cache", "wb");
|
176
|
if ($fd) {
|
177
|
fwrite($fd, serialize($config));
|
178
|
fclose($fd);
|
179
|
}
|
180
|
}
|
181
|
|
182
|
config_unlock();
|
183
|
|
184
|
/* make alias table (for faster lookups) */
|
185
|
alias_make_table();
|
186
|
}
|
187
|
|
188
|
/* mount flash card read/write */
|
189
|
function conf_mount_rw() {
|
190
|
global $g;
|
191
|
|
192
|
/* don't use mount -u anymore
|
193
|
(doesn't sync the files properly and /bin/sync won't help either) */
|
194
|
mwexec("/sbin/umount -f {$g['cf_path']}");
|
195
|
mwexec("/sbin/mount -w -o noatime {$g['cf_path']}");
|
196
|
}
|
197
|
|
198
|
/* mount flash card read only */
|
199
|
function conf_mount_ro() {
|
200
|
global $g;
|
201
|
|
202
|
mwexec("/sbin/umount -f {$g['cf_path']}");
|
203
|
mwexec("/sbin/mount -r {$g['cf_path']}");
|
204
|
}
|
205
|
|
206
|
/* convert configuration, if necessary */
|
207
|
function convert_config() {
|
208
|
global $config, $pkg_config, $g;
|
209
|
|
210
|
if ($config['version'] == $g['latest_config'])
|
211
|
return; /* already at latest version */
|
212
|
|
213
|
if ($g['booting'])
|
214
|
echo "Converting configuration... ";
|
215
|
|
216
|
/* convert 1.0 -> 1.1 */
|
217
|
if ($config['version'] == "1.0") {
|
218
|
$opti = 1;
|
219
|
$ifmap = array('lan' => 'lan', 'wan' => 'wan', 'pptp' => 'pptp');
|
220
|
|
221
|
/* convert DMZ to optional, if necessary */
|
222
|
if (isset($config['interfaces']['dmz'])) {
|
223
|
|
224
|
$dmzcfg = &$config['interfaces']['dmz'];
|
225
|
|
226
|
if ($dmzcfg['if']) {
|
227
|
$config['interfaces']['opt' . $opti] = array();
|
228
|
$optcfg = &$config['interfaces']['opt' . $opti];
|
229
|
|
230
|
$optcfg['enable'] = $dmzcfg['enable'];
|
231
|
$optcfg['descr'] = "DMZ";
|
232
|
$optcfg['if'] = $dmzcfg['if'];
|
233
|
$optcfg['ipaddr'] = $dmzcfg['ipaddr'];
|
234
|
$optcfg['subnet'] = $dmzcfg['subnet'];
|
235
|
|
236
|
$ifmap['dmz'] = "opt" . $opti;
|
237
|
$opti++;
|
238
|
}
|
239
|
|
240
|
unset($config['interfaces']['dmz']);
|
241
|
}
|
242
|
|
243
|
/* convert WLAN1/2 to optional, if necessary */
|
244
|
for ($i = 1; isset($config['interfaces']['wlan' . $i]); $i++) {
|
245
|
|
246
|
if (!$config['interfaces']['wlan' . $i]['if']) {
|
247
|
unset($config['interfaces']['wlan' . $i]);
|
248
|
continue;
|
249
|
}
|
250
|
|
251
|
$wlancfg = &$config['interfaces']['wlan' . $i];
|
252
|
$config['interfaces']['opt' . $opti] = array();
|
253
|
$optcfg = &$config['interfaces']['opt' . $opti];
|
254
|
|
255
|
$optcfg['enable'] = $wlancfg['enable'];
|
256
|
$optcfg['descr'] = "WLAN" . $i;
|
257
|
$optcfg['if'] = $wlancfg['if'];
|
258
|
$optcfg['ipaddr'] = $wlancfg['ipaddr'];
|
259
|
$optcfg['subnet'] = $wlancfg['subnet'];
|
260
|
$optcfg['bridge'] = $wlancfg['bridge'];
|
261
|
|
262
|
$optcfg['wireless'] = array();
|
263
|
$optcfg['wireless']['mode'] = $wlancfg['mode'];
|
264
|
$optcfg['wireless']['ssid'] = $wlancfg['ssid'];
|
265
|
$optcfg['wireless']['channel'] = $wlancfg['channel'];
|
266
|
$optcfg['wireless']['wep'] = $wlancfg['wep'];
|
267
|
|
268
|
$ifmap['wlan' . $i] = "opt" . $opti;
|
269
|
|
270
|
unset($config['interfaces']['wlan' . $i]);
|
271
|
$opti++;
|
272
|
}
|
273
|
|
274
|
/* convert filter rules */
|
275
|
$n = count($config['filter']['rule']);
|
276
|
for ($i = 0; $i < $n; $i++) {
|
277
|
|
278
|
$fr = &$config['filter']['rule'][$i];
|
279
|
|
280
|
/* remap interface */
|
281
|
if (array_key_exists($fr['interface'], $ifmap))
|
282
|
$fr['interface'] = $ifmap[$fr['interface']];
|
283
|
else {
|
284
|
/* remove the rule */
|
285
|
echo "\nWarning: filter rule removed " .
|
286
|
"(interface '{$fr['interface']}' does not exist anymore).";
|
287
|
unset($config['filter']['rule'][$i]);
|
288
|
continue;
|
289
|
}
|
290
|
|
291
|
/* remap source network */
|
292
|
if (isset($fr['source']['network'])) {
|
293
|
if (array_key_exists($fr['source']['network'], $ifmap))
|
294
|
$fr['source']['network'] = $ifmap[$fr['source']['network']];
|
295
|
else {
|
296
|
/* remove the rule */
|
297
|
echo "\nWarning: filter rule removed " .
|
298
|
"(source network '{$fr['source']['network']}' does not exist anymore).";
|
299
|
unset($config['filter']['rule'][$i]);
|
300
|
continue;
|
301
|
}
|
302
|
}
|
303
|
|
304
|
/* remap destination network */
|
305
|
if (isset($fr['destination']['network'])) {
|
306
|
if (array_key_exists($fr['destination']['network'], $ifmap))
|
307
|
$fr['destination']['network'] = $ifmap[$fr['destination']['network']];
|
308
|
else {
|
309
|
/* remove the rule */
|
310
|
echo "\nWarning: filter rule removed " .
|
311
|
"(destination network '{$fr['destination']['network']}' does not exist anymore).";
|
312
|
unset($config['filter']['rule'][$i]);
|
313
|
continue;
|
314
|
}
|
315
|
}
|
316
|
}
|
317
|
|
318
|
/* convert shaper rules */
|
319
|
$n = count($config['pfqueueing']['rule']);
|
320
|
if (is_array($config['pfqueueing']['rule']))
|
321
|
for ($i = 0; $i < $n; $i++) {
|
322
|
|
323
|
$fr = &$config['pfqueueing']['rule'][$i];
|
324
|
|
325
|
/* remap interface */
|
326
|
if (array_key_exists($fr['interface'], $ifmap))
|
327
|
$fr['interface'] = $ifmap[$fr['interface']];
|
328
|
else {
|
329
|
/* remove the rule */
|
330
|
echo "\nWarning: traffic shaper rule removed " .
|
331
|
"(interface '{$fr['interface']}' does not exist anymore).";
|
332
|
unset($config['pfqueueing']['rule'][$i]);
|
333
|
continue;
|
334
|
}
|
335
|
|
336
|
/* remap source network */
|
337
|
if (isset($fr['source']['network'])) {
|
338
|
if (array_key_exists($fr['source']['network'], $ifmap))
|
339
|
$fr['source']['network'] = $ifmap[$fr['source']['network']];
|
340
|
else {
|
341
|
/* remove the rule */
|
342
|
echo "\nWarning: traffic shaper rule removed " .
|
343
|
"(source network '{$fr['source']['network']}' does not exist anymore).";
|
344
|
unset($config['pfqueueing']['rule'][$i]);
|
345
|
continue;
|
346
|
}
|
347
|
}
|
348
|
|
349
|
/* remap destination network */
|
350
|
if (isset($fr['destination']['network'])) {
|
351
|
if (array_key_exists($fr['destination']['network'], $ifmap))
|
352
|
$fr['destination']['network'] = $ifmap[$fr['destination']['network']];
|
353
|
else {
|
354
|
/* remove the rule */
|
355
|
echo "\nWarning: traffic shaper rule removed " .
|
356
|
"(destination network '{$fr['destination']['network']}' does not exist anymore).";
|
357
|
unset($config['pfqueueing']['rule'][$i]);
|
358
|
continue;
|
359
|
}
|
360
|
}
|
361
|
}
|
362
|
|
363
|
$config['version'] = "1.1";
|
364
|
}
|
365
|
|
366
|
/* convert 1.1 -> 1.2 */
|
367
|
if ($config['version'] == "1.1") {
|
368
|
/* move LAN DHCP server config */
|
369
|
$tmp = $config['dhcpd'];
|
370
|
$config['dhcpd'] = array();
|
371
|
$config['dhcpd']['lan'] = $tmp;
|
372
|
|
373
|
/* encrypt password */
|
374
|
$config['system']['password'] = crypt($config['system']['password']);
|
375
|
|
376
|
$config['version'] = "1.2";
|
377
|
}
|
378
|
|
379
|
/* convert 1.2 -> 1.3 */
|
380
|
if ($config['version'] == "1.2") {
|
381
|
/* convert advanced outbound NAT config */
|
382
|
for ($i = 0; isset($config['nat']['advancedoutbound']['rule'][$i]); $i++) {
|
383
|
$curent = &$config['nat']['advancedoutbound']['rule'][$i];
|
384
|
$src = $curent['source'];
|
385
|
$curent['source'] = array();
|
386
|
$curent['source']['network'] = $src;
|
387
|
$curent['destination'] = array();
|
388
|
$curent['destination']['any'] = true;
|
389
|
}
|
390
|
|
391
|
/* add an explicit type="pass" to all filter rules to make things consistent */
|
392
|
for ($i = 0; isset($config['filter']['rule'][$i]); $i++) {
|
393
|
$config['filter']['rule'][$i]['type'] = "pass";
|
394
|
}
|
395
|
|
396
|
$config['version'] = "1.3";
|
397
|
}
|
398
|
|
399
|
/* convert 1.3 -> 1.4 */
|
400
|
if ($config['version'] == "1.3") {
|
401
|
/* convert shaper rules (make pipes) */
|
402
|
if (is_array($config['pfqueueing']['rule'])) {
|
403
|
$config['pfqueueing']['pipe'] = array();
|
404
|
|
405
|
for ($i = 0; isset($config['pfqueueing']['rule'][$i]); $i++) {
|
406
|
$curent = &$config['pfqueueing']['rule'][$i];
|
407
|
|
408
|
/* make new pipe and associate with this rule */
|
409
|
$newpipe = array();
|
410
|
$newpipe['descr'] = $curent['descr'];
|
411
|
$newpipe['bandwidth'] = $curent['bandwidth'];
|
412
|
$newpipe['delay'] = $curent['delay'];
|
413
|
$newpipe['mask'] = $curent['mask'];
|
414
|
$config['pfqueueing']['pipe'][$i] = $newpipe;
|
415
|
|
416
|
$curent['targetpipe'] = $i;
|
417
|
|
418
|
unset($curent['bandwidth']);
|
419
|
unset($curent['delay']);
|
420
|
unset($curent['mask']);
|
421
|
}
|
422
|
}
|
423
|
|
424
|
$config['version'] = "1.4";
|
425
|
}
|
426
|
|
427
|
/* Convert 1.4 -> 1.5 */
|
428
|
if ($config['version'] == "1.4") {
|
429
|
|
430
|
/* Default route moved */
|
431
|
if (isset($config['interfaces']['wan']['gateway']))
|
432
|
if ($config['interfaces']['wan']['gateway'] <> "")
|
433
|
$config['system']['gateway'] = $config['interfaces']['wan']['gateway'];
|
434
|
unset($config['interfaces']['wan']['gateway']);
|
435
|
|
436
|
/* Queues are no longer interface specific */
|
437
|
if (isset($config['interfaces']['lan']['schedulertype']))
|
438
|
unset($config['interfaces']['lan']['schedulertype']);
|
439
|
if (isset($config['interfaces']['wan']['schedulertype']))
|
440
|
unset($config['interfaces']['wan']['schedulertype']);
|
441
|
|
442
|
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
|
443
|
if(isset($config['interfaces']['opt' . $i]['schedulertype']))
|
444
|
unset($config['interfaces']['opt' . $i]['schedulertype']);
|
445
|
}
|
446
|
|
447
|
$config['version'] = "1.5";
|
448
|
}
|
449
|
|
450
|
write_config();
|
451
|
|
452
|
if ($g['booting'])
|
453
|
echo "done\n";
|
454
|
}
|
455
|
|
456
|
/* save the system configuration */
|
457
|
function write_config() {
|
458
|
|
459
|
global $config, $g;
|
460
|
|
461
|
config_lock();
|
462
|
|
463
|
conf_mount_rw();
|
464
|
|
465
|
if (time() > mktime(0, 0, 0, 9, 1, 2004)) /* make sure the clock settings is plausible */
|
466
|
$config['lastchange'] = time();
|
467
|
|
468
|
/* generate configuration XML */
|
469
|
$xmlconfig = dump_xml_config($config, $g['xml_rootobj']);
|
470
|
|
471
|
/* write configuration */
|
472
|
$fd = fopen("{$g['cf_conf_path']}/config.xml", "w");
|
473
|
|
474
|
if (!$fd)
|
475
|
die("Unable to open config.xml for writing in write_config()\n");
|
476
|
|
477
|
fwrite($fd, $xmlconfig);
|
478
|
fclose($fd);
|
479
|
|
480
|
conf_mount_ro();
|
481
|
|
482
|
/* re-read configuration */
|
483
|
$config = parse_xml_config("{$g['conf_path']}/config.xml", $g['xml_rootobj']);
|
484
|
|
485
|
/* write config cache */
|
486
|
$fd = @fopen("{$g['tmp_path']}/config.cache", "wb");
|
487
|
if ($fd) {
|
488
|
fwrite($fd, serialize($config));
|
489
|
fclose($fd);
|
490
|
}
|
491
|
|
492
|
config_unlock();
|
493
|
}
|
494
|
|
495
|
function reset_factory_defaults() {
|
496
|
|
497
|
global $g;
|
498
|
|
499
|
config_lock();
|
500
|
|
501
|
conf_mount_rw();
|
502
|
|
503
|
/* create conf directory, if necessary */
|
504
|
if (!file_exists("{$g['cf_conf_path']}"))
|
505
|
@mkdir("{$g['cf_conf_path']}");
|
506
|
|
507
|
/* clear out /conf */
|
508
|
$dh = opendir($g['conf_path']);
|
509
|
while ($filename = readdir($dh)) {
|
510
|
if (($filename != ".") && ($filename != "..")) {
|
511
|
unlink($g['conf_path'] . "/" . $filename);
|
512
|
}
|
513
|
}
|
514
|
closedir($dh);
|
515
|
|
516
|
/* copy default configuration */
|
517
|
@copy("{$g['conf_default_path']}/config.xml", "{$g['conf_path']}/config.xml");
|
518
|
|
519
|
conf_mount_ro();
|
520
|
|
521
|
config_unlock();
|
522
|
|
523
|
return 0;
|
524
|
}
|
525
|
|
526
|
function config_install($conffile) {
|
527
|
|
528
|
global $config, $g;
|
529
|
|
530
|
if (!file_exists($conffile))
|
531
|
return 1;
|
532
|
|
533
|
config_lock();
|
534
|
conf_mount_rw();
|
535
|
|
536
|
copy($conffile, "{$g['conf_path']}/config.xml");
|
537
|
|
538
|
conf_mount_ro();
|
539
|
config_unlock();
|
540
|
|
541
|
return 0;
|
542
|
}
|
543
|
|
544
|
/* lock configuration file, decide that the lock file is stale after
|
545
|
10 seconds */
|
546
|
function config_lock() {
|
547
|
|
548
|
global $g;
|
549
|
|
550
|
$lockfile = "{$g['varrun_path']}/config.lock";
|
551
|
|
552
|
$n = 0;
|
553
|
while ($n < 10) {
|
554
|
/* open the lock file in append mode to avoid race condition */
|
555
|
if ($fd = @fopen($lockfile, "x")) {
|
556
|
/* succeeded */
|
557
|
fclose($fd);
|
558
|
return;
|
559
|
} else {
|
560
|
/* file locked, wait and try again */
|
561
|
sleep(1);
|
562
|
$n++;
|
563
|
}
|
564
|
}
|
565
|
}
|
566
|
|
567
|
/* unlock configuration file */
|
568
|
function config_unlock() {
|
569
|
|
570
|
global $g;
|
571
|
|
572
|
$lockfile = "{$g['varrun_path']}/config.lock";
|
573
|
|
574
|
if (file_exists($lockfile))
|
575
|
unlink($lockfile);
|
576
|
}
|
577
|
|
578
|
function set_networking_interfaces_ports() {
|
579
|
global $noreboot;
|
580
|
global $config;
|
581
|
global $g;
|
582
|
global $fp;
|
583
|
|
584
|
$fp = fopen('php://stdin', 'r');
|
585
|
|
586
|
$iflist = get_interface_list();
|
587
|
|
588
|
echo <<<EOD
|
589
|
|
590
|
Valid interfaces are:
|
591
|
|
592
|
|
593
|
EOD;
|
594
|
|
595
|
foreach ($iflist as $iface => $ifa) {
|
596
|
echo sprintf("% -8s%s%s\n", $iface, $ifa['mac'],
|
597
|
$ifa['up'] ? " (up)" : "");
|
598
|
}
|
599
|
|
600
|
echo <<<EOD
|
601
|
|
602
|
Do you want to set up VLANs first?
|
603
|
If you are not going to use VLANs, or only for optional interfaces, you
|
604
|
should say no here and use the webGUI to configure VLANs later, if required.
|
605
|
|
606
|
Do you want to set up VLANs now [y|n]?
|
607
|
EOD;
|
608
|
|
609
|
if (strcasecmp(chop(fgets($fp)), "y") == 0)
|
610
|
vlan_setup();
|
611
|
|
612
|
if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
|
613
|
|
614
|
echo "\n\nVLAN interfaces:\n\n";
|
615
|
$i = 0;
|
616
|
foreach ($config['vlans']['vlan'] as $vlan) {
|
617
|
|
618
|
echo sprintf("% -8s%s\n", "vlan{$i}",
|
619
|
"VLAN tag {$vlan['tag']}, interface {$vlan['if']}");
|
620
|
|
621
|
$iflist['vlan' . $i] = array();
|
622
|
$i++;
|
623
|
}
|
624
|
}
|
625
|
|
626
|
echo <<<EOD
|
627
|
|
628
|
If you do not know the names of your interfaces, you may choose to use
|
629
|
auto-detection. In that case, disconnect all interfaces before you begin,
|
630
|
and reconnect each one when prompted to do so.
|
631
|
|
632
|
EOD;
|
633
|
|
634
|
do {
|
635
|
echo "\nEnter the LAN interface name or 'a' for auto-detection: ";
|
636
|
$lanif = chop(fgets($fp));
|
637
|
if ($lanif === "") {
|
638
|
exit(0);
|
639
|
}
|
640
|
|
641
|
if ($lanif === "a")
|
642
|
$lanif = autodetect_interface("LAN", $fp);
|
643
|
else if (!array_key_exists($lanif, $iflist)) {
|
644
|
echo "\nInvalid interface name '{$lanif}'\n";
|
645
|
unset($lanif);
|
646
|
continue;
|
647
|
}
|
648
|
} while (!$lanif);
|
649
|
|
650
|
do {
|
651
|
echo "\nEnter the WAN interface name or 'a' for auto-detection: ";
|
652
|
$wanif = chop(fgets($fp));
|
653
|
if ($wanif === "") {
|
654
|
exit(0);
|
655
|
}
|
656
|
if ($wanif === "a")
|
657
|
$wanif = autodetect_interface("WAN", $fp);
|
658
|
else if (!array_key_exists($wanif, $iflist)) {
|
659
|
echo "\nInvalid interface name '{$wanif}'\n";
|
660
|
unset($wanif);
|
661
|
continue;
|
662
|
}
|
663
|
} while (!$wanif);
|
664
|
|
665
|
/* optional interfaces */
|
666
|
$i = 0;
|
667
|
$optif = array();
|
668
|
|
669
|
while (1) {
|
670
|
if ($optif[$i])
|
671
|
$i++;
|
672
|
$i1 = $i + 1;
|
673
|
echo "\nEnter the Optional {$i1} interface name or 'a' for auto-detection\n" .
|
674
|
"(or nothing if finished): ";
|
675
|
$optif[$i] = chop(fgets($fp));
|
676
|
|
677
|
if ($optif[$i]) {
|
678
|
if ($optif[$i] === "a") {
|
679
|
$ad = autodetect_interface("Optional " . $i1, $fp);
|
680
|
if ($ad)
|
681
|
$optif[$i] = $ad;
|
682
|
else
|
683
|
unset($optif[$i]);
|
684
|
} else if (!array_key_exists($optif[$i], $iflist)) {
|
685
|
echo "\nInvalid interface name '{$optif[$i]}'\n";
|
686
|
unset($optif[$i]);
|
687
|
continue;
|
688
|
}
|
689
|
} else {
|
690
|
unset($optif[$i]);
|
691
|
break;
|
692
|
}
|
693
|
}
|
694
|
|
695
|
/* check for double assignments */
|
696
|
$ifarr = array_merge(array($lanif, $wanif), $optif);
|
697
|
|
698
|
for ($i = 0; $i < (count($ifarr)-1); $i++) {
|
699
|
for ($j = ($i+1); $j < count($ifarr); $j++) {
|
700
|
if ($ifarr[$i] == $ifarr[$j]) {
|
701
|
echo <<<EOD
|
702
|
|
703
|
Error: you cannot assign the same interface name twice!
|
704
|
|
705
|
EOD;
|
706
|
|
707
|
exit(0);
|
708
|
}
|
709
|
}
|
710
|
}
|
711
|
|
712
|
echo <<<EOD
|
713
|
|
714
|
The interfaces will be assigned as follows:
|
715
|
|
716
|
LAN -> {$lanif}
|
717
|
WAN -> {$wanif}
|
718
|
|
719
|
EOD;
|
720
|
|
721
|
for ($i = 0; $i < count($optif); $i++) {
|
722
|
echo "OPT" . ($i+1) . " -> " . $optif[$i] . "\n";
|
723
|
}
|
724
|
|
725
|
if(!$noreboot) echo "\npfSense will reboot after saving the changes.\n";
|
726
|
|
727
|
echo <<<EOD
|
728
|
|
729
|
Do you want to proceed [y|n]?
|
730
|
EOD;
|
731
|
|
732
|
if (strcasecmp(chop(fgets($fp)), "y") == 0) {
|
733
|
|
734
|
$config['interfaces']['lan']['if'] = $lanif;
|
735
|
if (preg_match("/^(wi|awi|an)/", $lanif)) {
|
736
|
if (!is_array($config['interfaces']['lan']['wireless']))
|
737
|
$config['interfaces']['lan']['wireless'] = array();
|
738
|
} else {
|
739
|
unset($config['interfaces']['lan']['wireless']);
|
740
|
}
|
741
|
|
742
|
$config['interfaces']['wan']['if'] = $wanif;
|
743
|
if (preg_match("/^(wi|awi|an)/", $wanif)) {
|
744
|
if (!is_array($config['interfaces']['wan']['wireless']))
|
745
|
$config['interfaces']['wan']['wireless'] = array();
|
746
|
} else {
|
747
|
unset($config['interfaces']['wan']['wireless']);
|
748
|
}
|
749
|
|
750
|
for ($i = 0; $i < count($optif); $i++) {
|
751
|
if (!is_array($config['interfaces']['opt' . ($i+1)]))
|
752
|
$config['interfaces']['opt' . ($i+1)] = array();
|
753
|
|
754
|
$config['interfaces']['opt' . ($i+1)]['if'] = $optif[$i];
|
755
|
|
756
|
/* wireless interface? */
|
757
|
if (preg_match("/^(wi|awi|an)/", $optif[$i])) {
|
758
|
if (!is_array($config['interfaces']['opt' . ($i+1)]['wireless']))
|
759
|
$config['interfaces']['opt' . ($i+1)]['wireless'] = array();
|
760
|
} else {
|
761
|
unset($config['interfaces']['opt' . ($i+1)]['wireless']);
|
762
|
}
|
763
|
|
764
|
unset($config['interfaces']['opt' . ($i+1)]['enable']);
|
765
|
$config['interfaces']['opt' . ($i+1)]['descr'] = "OPT" . ($i+1);
|
766
|
}
|
767
|
|
768
|
/* remove all other (old) optional interfaces */
|
769
|
for (; isset($config['interfaces']['opt' . ($i+1)]); $i++)
|
770
|
unset($config['interfaces']['opt' . ($i+1)]);
|
771
|
|
772
|
write_config();
|
773
|
|
774
|
if(!$noreboot) echo "\npfSense is now rebooting.\n";
|
775
|
|
776
|
echo <<<EOD
|
777
|
|
778
|
|
779
|
|
780
|
EOD;
|
781
|
|
782
|
if($noreboot <> true)
|
783
|
system_reboot_sync();
|
784
|
}
|
785
|
}
|
786
|
|
787
|
function autodetect_interface($ifname, $fp) {
|
788
|
$iflist_prev = get_interface_list();
|
789
|
echo <<<EOD
|
790
|
|
791
|
Connect the {$ifname} interface now and make sure that the link is up.
|
792
|
Then press ENTER to continue.
|
793
|
|
794
|
EOD;
|
795
|
fgets($fp);
|
796
|
$iflist = get_interface_list();
|
797
|
|
798
|
foreach ($iflist_prev as $ifn => $ifa) {
|
799
|
if (!$ifa['up'] && $iflist[$ifn]['up']) {
|
800
|
echo "Detected link-up on interface {$ifn}.\n";
|
801
|
return $ifn;
|
802
|
}
|
803
|
}
|
804
|
|
805
|
echo "No link-up detected.\n";
|
806
|
|
807
|
return null;
|
808
|
}
|
809
|
|
810
|
function vlan_setup() {
|
811
|
global $iflist, $config, $g, $fp;
|
812
|
|
813
|
$iflist = get_interface_list();
|
814
|
|
815
|
if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
|
816
|
|
817
|
echo <<<EOD
|
818
|
|
819
|
WARNING: all existing VLANs will be cleared if you proceed!
|
820
|
|
821
|
Do you want to proceed [y|n]?
|
822
|
EOD;
|
823
|
|
824
|
if (strcasecmp(chop(fgets($fp)), "y") != 0)
|
825
|
return;
|
826
|
}
|
827
|
|
828
|
$config['vlans']['vlan'] = array();
|
829
|
echo "\n";
|
830
|
|
831
|
while (1) {
|
832
|
$vlan = array();
|
833
|
|
834
|
echo "\nEnter the parent interface name for the new VLAN (or nothing if finished): ";
|
835
|
$vlan['if'] = chop(fgets($fp));
|
836
|
|
837
|
if ($vlan['if']) {
|
838
|
if (!array_key_exists($vlan['if'], $iflist)) {
|
839
|
echo "\nInvalid interface name '{$vlan['if']}'\n";
|
840
|
continue;
|
841
|
}
|
842
|
} else {
|
843
|
break;
|
844
|
}
|
845
|
|
846
|
echo "Enter the VLAN tag (1-4094): ";
|
847
|
$vlan['tag'] = chop(fgets($fp));
|
848
|
|
849
|
if (!is_numericint($vlan['tag']) || ($vlan['tag'] < 1) || ($vlan['tag'] > 4094)) {
|
850
|
echo "\nInvalid VLAN tag '{$vlan['tag']}'\n";
|
851
|
continue;
|
852
|
}
|
853
|
|
854
|
$config['vlans']['vlan'][] = $vlan;
|
855
|
}
|
856
|
}
|
857
|
|
858
|
function system_start_ftp_helpers() {
|
859
|
global $config;
|
860
|
global $g;
|
861
|
if($config['system']['disableftpproxy'] <> "") return;
|
862
|
if ($g['booting'])
|
863
|
echo "Starting INETD and FTP Helpers for FTP-PROXY...\n";
|
864
|
$wanif = get_real_wan_interface();
|
865
|
mwexec("/usr/sbin/inetd -wW -C 60");
|
866
|
mwexec("/usr/bin/killall ftpsesame 2>/dev/null");
|
867
|
mwexec("/usr/local/sbin/ftpsesame -i {$wanif} -q ftpproxy");
|
868
|
}
|
869
|
|
870
|
|
871
|
?>
|