Project

General

Profile

Download (36.3 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 f5da67d0 Bill Marquette
/****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 5b237745 Scott Ullrich
	config.inc
12 cfc707f7 Scott Ullrich
	Copyright (C) 2004 Scott Ullrich
13
	All rights reserved.
14 5b237745 Scott Ullrich
15 cfc707f7 Scott Ullrich
	originally part of m0n0wall (http://m0n0.ch/wall)
16 5b237745 Scott Ullrich
	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 96447b25 Scott Ullrich
/* do not load this file twice. */
42
if($config_inc_loaded == true)
43
	return;
44
else
45
	$config_inc_loaded = true;
46
47 5b237745 Scott Ullrich
/* include globals/utility/XML parser files */
48
require_once("globals.inc");
49
require_once("util.inc");
50 5957111e Scott Ullrich
require_once("pfsense-utils.inc");
51 5b237745 Scott Ullrich
require_once("xmlparse.inc");
52
53
/* read platform */
54
if (file_exists("{$g['etc_path']}/platform")) {
55
	$g['platform'] = chop(file_get_contents("{$g['etc_path']}/platform"));
56
} else {
57
	$g['platform'] = "unknown";
58
}
59
60 6e8c1d1c Scott Ullrich
/* if our config file exists bail out, we're already set. */
61
if ($g['booting'] and !file_exists($g['cf_conf_path'] . "/config.xml")  ) {
62 5b237745 Scott Ullrich
	/* find the device where config.xml resides and write out an fstab */
63
	unset($cfgdevice);
64
65
	/* check if there's already an fstab (NFS booting?) */
66
	if (!file_exists("{$g['etc_path']}/fstab")) {
67
68
		if (strstr($g['platform'], "cdrom")) {
69
			/* config is on floppy disk for CD-ROM version */
70
			$cfgdevice = $cfgpartition = "fd0";
71 e76de94e Scott Ullrich
			$dmesg = `dmesg -a`;
72 da9de371 Scott Ullrich
			if(ereg("da0", $dmesg) == true) {
73 e76de94e Scott Ullrich
				$cfgdevice = $cfgpartition = "da0" ;
74 da9de371 Scott Ullrich
				if (mwexec("/sbin/mount -r /dev/{$cfgdevice} /cf")) {
75 e76de94e Scott Ullrich
					/* could not mount, fallback to floppy */
76
					$cfgdevice = $cfgpartition = "fd0";
77
				}
78
			}
79 5b237745 Scott Ullrich
			$cfgfstype = "msdos";
80 c3cbc094 Scott Ullrich
			echo "CDROM build\n";
81
			echo "   CFG: {$cfgpartition}\n";
82
			echo "  TYPE: {$cfgfstype}\n";
83 5b237745 Scott Ullrich
		} else {
84
			/* probe kernel known disks until we find one with config.xml */
85
			$disks = explode(" ", trim(preg_replace("/kern.disks: /", "", exec("/sbin/sysctl kern.disks"))));
86
			foreach ($disks as $mountdisk) {
87
				/* skip mfs mounted filesystems */
88
				if (strstr($mountdisk, "md"))
89
					continue;
90
				if (mwexec("/sbin/mount -r /dev/{$mountdisk}a {$g['cf_path']}") == 0) {
91
					if (file_exists("{$g['cf_conf_path']}/config.xml")) {
92
						/* found it */
93
						$cfgdevice = $mountdisk;
94
						$cfgpartition = $cfgdevice . "a";
95
						$cfgfstype = "ufs";
96
						echo "Found configuration on $cfgdevice.\n";
97
					}
98
99
					mwexec("/sbin/umount -f {$g['cf_path']}");
100
101 655a80eb Scott Ullrich
					if ($cfgdevice)
102
						break;
103
				}
104
				if (mwexec("/sbin/mount -r /dev/{$mountdisk}d {$g['cf_path']}") == 0) {
105
					if (file_exists("{$g['cf_conf_path']}/config.xml")) {
106
						/* found it */
107
						$cfgdevice = $mountdisk;
108 4de945fa Scott Ullrich
						$cfgpartition = $cfgdevice . "d";
109 655a80eb Scott Ullrich
						$cfgfstype = "ufs";
110
						echo "Found configuration on $cfgdevice.\n";
111
					}
112
113
					mwexec("/sbin/umount -f {$g['cf_path']}");
114
115 5b237745 Scott Ullrich
					if ($cfgdevice)
116
						break;
117
				}
118
			}
119
		}
120
121
		if (!$cfgdevice) {
122
			/* no device found, print an error and die */
123
			echo <<<EOD
124
125
126
*******************************************************************************
127
* FATAL ERROR                                                                 *
128
* The device that contains the configuration file (config.xml) could not be   *
129 ee11cc6e Scott Ullrich
* found. pfSense cannot continue booting.                                     *
130 5b237745 Scott Ullrich
*******************************************************************************
131
132
133
EOD;
134
135
			mwexec("/sbin/halt");
136
			exit;
137
		}
138
139
		/* write device name to a file for rc.firmware */
140
		$fd = fopen("{$g['varetc_path']}/cfdevice", "w");
141
		fwrite($fd, $cfgdevice . "\n");
142
		fclose($fd);
143
144
		/* write out an fstab */
145
		$fd = fopen("{$g['etc_path']}/fstab", "w");
146
147
		$fstab = "/dev/{$cfgpartition} {$g['cf_path']} {$cfgfstype} ro 1 1\n";
148
		$fstab .= "proc /proc procfs rw 0 0\n";
149
150
		fwrite($fd, $fstab);
151
		fclose($fd);
152
	}
153
154
	/* mount all filesystems */
155
	mwexec("/sbin/mount -a");
156
}
157
158 5cabfeb3 Scott Ullrich
$config = parse_config();
159
160 f5da67d0 Bill Marquette
/****f* config/parse_config
161
 * NAME
162
 *   parse_config - Read in config.cache or config.xml if needed and return $config array
163
 * INPUTS
164
 *   $parse       - boolean to force parse_config() to read config.xml and generate config.cache
165
 * RESULT
166
 *   $config      - array containing all configuration variables
167
 ******/
168 781beaaa Colin Smith
function parse_config($parse = false) {
169 baa7645c Colin Smith
	global $g;
170 d9e8c3bc Bill Marquette
171
	config_lock();
172 781beaaa Colin Smith
	if(!$parse) {
173
		if(file_exists($g['tmp_path'] . '/config.cache')) {
174
			$config = unserialize(file_get_contents($g['tmp_path'] . '/config.cache'));
175 baa7645c Colin Smith
			if(is_null($config)) {
176 d9e8c3bc Bill Marquette
				config_unlock();
177 baa7645c Colin Smith
				parse_config(true);
178
			}
179 781beaaa Colin Smith
		} else {
180 d9e8c3bc Bill Marquette
			config_unlock();
181 baa7645c Colin Smith
			$config = parse_config(true);
182 781beaaa Colin Smith
		}
183 ffd1b445 Scott Ullrich
	} else {
184 781beaaa Colin Smith
		$config = parse_xml_config($g['conf_path'] . '/config.xml', $g['xml_rootobj']);
185 baa7645c Colin Smith
		generate_config_cache($config);
186 781beaaa Colin Smith
	}
187 1886ba69 Scott Ullrich
	
188 918a884d Bill Marquette
	alias_make_table($config);
189 d9e8c3bc Bill Marquette
	config_unlock();
190 baa7645c Colin Smith
	return $config;
191 41508358 Scott Ullrich
}
192 5b237745 Scott Ullrich
193 840c97dc Colin Smith
/****f* config/generate_config_cache
194
 * NAME
195
 *   generate_config_cache - Write serialized configuration to cache.
196
 * INPUTS
197
 *   $config	- array containing current firewall configuration
198
 * RESULT
199
 *   boolean	- true on completion
200
 ******/
201 baa7645c Colin Smith
function generate_config_cache($config) {
202
	global $g;
203 842bf9f1 Scott Ullrich
	conf_mount_rw();
204 41508358 Scott Ullrich
	$configcache = fopen($g['tmp_path'] . '/config.cache', "w");
205
	fwrite($configcache, serialize($config));
206
	fclose($configcache);
207 842bf9f1 Scott Ullrich
	conf_mount_ro();
208 41508358 Scott Ullrich
	return true;
209
}
210
211 840c97dc Colin Smith
/****f* config/parse_config_bootup
212
 * NAME
213
 *   parse_config_bootup - Bootup-specific configuration checks.
214
 * RESULT
215
 *   null
216
 ******/
217 41508358 Scott Ullrich
function parse_config_bootup() {
218
	global $config, $g;
219
	if (!$noparseconfig) {
220 5b237745 Scott Ullrich
		if (!file_exists("{$g['conf_path']}/config.xml")) {
221 41508358 Scott Ullrich
			config_lock();
222 5b237745 Scott Ullrich
			if ($g['booting']) {
223
				if (strstr($g['platform'], "cdrom")) {
224
					/* try copying the default config. to the floppy */
225 c3cbc094 Scott Ullrich
					echo "Resetting factory defaults...\n";
226 5b237745 Scott Ullrich
					reset_factory_defaults();
227 41508358 Scott Ullrich
	
228 5b237745 Scott Ullrich
					echo "No XML configuration file found - using factory defaults.\n";
229
					echo "Make sure that the configuration floppy disk with the conf/config.xml\n";
230
					echo "file is inserted. If it isn't, your configuration changes will be lost\n";
231
					echo "on reboot.\n";
232
				} else {
233 6e8c1d1c Scott Ullrich
					echo "XML configuration file not found.  pfSense cannot continue booting.\n";
234 5b237745 Scott Ullrich
					mwexec("/sbin/halt");
235
					exit;
236
				}
237
			} else {
238
				config_unlock();
239
				exit(0);
240
			}
241
		}
242 41508358 Scott Ullrich
	}
243 5b237745 Scott Ullrich
244 5c6d0f65 Colin Smith
	parse_config(true);
245
	
246 41508358 Scott Ullrich
	if ((float)$config['version'] > (float)$g['latest_config']) {
247 5c6d0f65 Colin Smith
		echo <<<EOD
248 5b237745 Scott Ullrich
249
250
*******************************************************************************
251
* WARNING!                                                                    *
252 ee11cc6e Scott Ullrich
* The current configuration has been created with a newer version of pfSense  *
253 5b237745 Scott Ullrich
* than this one! This can lead to serious misbehavior and even security       *
254 ee11cc6e Scott Ullrich
* holes! You are urged to either upgrade to a newer version of pfSense or     *
255 5b237745 Scott Ullrich
* revert to the default configuration immediately!                            *
256
*******************************************************************************
257
258
259
EOD;
260
		}
261
262
	/* make alias table (for faster lookups) */
263 918a884d Bill Marquette
	alias_make_table($config);
264 d9e8c3bc Bill Marquette
	config_unlock();
265 5b237745 Scott Ullrich
}
266
267 840c97dc Colin Smith
/****f* config/conf_mount_rw
268
 * NAME
269
 *   conf_mount_rw - Mount filesystems read/write.
270
 * RESULT
271
 *   null
272
 ******/
273 5b237745 Scott Ullrich
/* mount flash card read/write */
274
function conf_mount_rw() {
275
	global $g;
276
277 875e24be Scott Ullrich
	/* do not mount on cdrom platform */
278
	if($g['platform'] == "cdrom")
279
		return;
280
281 5b237745 Scott Ullrich
	/* don't use mount -u anymore
282
	   (doesn't sync the files properly and /bin/sync won't help either) */
283 14249fda Scott Ullrich
	$status = mwexec("/sbin/umount -f {$g['cf_path']}");
284
285 18859f3b Scott Ullrich
	$status = mwexec("/sbin/mount -w -o noatime {$g['cf_path']}");
286 4fde4ce4 Colin Smith
	if($status <> 0) {
287
		mwexec("/sbin/fsck -y {$g['cf_path']}");
288 18859f3b Scott Ullrich
		$status = mwexec("/sbin/mount -w -o noatime {$g['cf_path']}");
289
	}
290 14249fda Scott Ullrich
291 578d4f38 Scott Ullrich
	/*    if the platform is soekris or wrap or pfSense, lets mount the
292
	 *    compact flash cards root.
293
         */
294 3e52930a Scott Ullrich
	if($g['platform'] == "wrap" or $g['platform'] == "net45xx"
295
	   or $g['platform'] == "embedded") {
296 1ef6e981 Scott Ullrich
		mwexec("/sbin/umount -f /");
297 578d4f38 Scott Ullrich
		$status = mwexec("/sbin/mount -w /");
298 1ef6e981 Scott Ullrich
		/* we could not mount this correctly.  kick off fsck */
299 4fde4ce4 Colin Smith
		if($status <> 0) {
300 1ef6e981 Scott Ullrich
			log_error("File system is dirty.  Launching FSCK for /");
301
			mwexec("/sbin/fsck -y");
302
			$status = mwexec("/sbin/mount -w /");
303
		}
304 c8b8a2b7 Scott Ullrich
	}
305 5b237745 Scott Ullrich
}
306
307 840c97dc Colin Smith
/****f* config/conf_mount_ro
308
 * NAME         
309
 *   conf_mount_ro - Mount filesystems readonly.
310
 * RESULT
311
 *   null        
312
 ******/
313 5b237745 Scott Ullrich
function conf_mount_ro() {
314 669e1adb Bill Marquette
	global $g;
315 f699da52 Scott Ullrich
316 81e3bbc7 Scott Ullrich
	if($g['booting'] == true)
317
		return;
318
	
319
	/* do not umount if generating ssh keys */
320
	if(file_exists("/tmp/keys_generating"))
321
		return;
322 855ec374 Scott Ullrich
	
323
	/* do not umount on cdrom platform */
324
	if($g['platform'] == "cdrom")
325 ac4878f9 Scott Ullrich
		return;
326 5b237745 Scott Ullrich
327
	mwexec("/sbin/umount -f {$g['cf_path']}");
328
	mwexec("/sbin/mount -r {$g['cf_path']}");
329 578d4f38 Scott Ullrich
	/*    if the platform is soekris or wrap, lets unmount the
330
	 *    compact flash card.
331
         */
332 3e52930a Scott Ullrich
	if($g['platform'] == "wrap" or $g['platform'] == "net45xx"
333
	   or $g['platform'] == "embedded") {
334 1ef6e981 Scott Ullrich
		mwexec("/sbin/umount -f /");
335
		mwexec("/sbin/mount -f -r /");
336
	}
337 5b237745 Scott Ullrich
}
338
339 840c97dc Colin Smith
/****f* config/convert_config
340
 * NAME         
341
 *   convert_config - Attempt to update config.xml.
342
 * DESCRIPTION
343
 *   convert_config() reads the current global configuration
344
 *   and attempts to convert it to conform to the latest
345
 *   config.xml version. This allows major formatting changes
346
 *   to be made with a minimum of breakage.
347
 * RESULT
348
 *   null        
349
 ******/
350 5b237745 Scott Ullrich
/* convert configuration, if necessary */
351
function convert_config() {
352 669e1adb Bill Marquette
	global $config, $g;
353 5b237745 Scott Ullrich
354
	if ($config['version'] == $g['latest_config'])
355
		return;		/* already at latest version */
356
357 d05dff82 Bill Marquette
	// Save off config version
358
	$prev_version = $config['version'];
359
360 5b237745 Scott Ullrich
	/* convert 1.0 -> 1.1 */
361
	if ($config['version'] == "1.0") {
362
		$opti = 1;
363
		$ifmap = array('lan' => 'lan', 'wan' => 'wan', 'pptp' => 'pptp');
364
365
		/* convert DMZ to optional, if necessary */
366
		if (isset($config['interfaces']['dmz'])) {
367
368
			$dmzcfg = &$config['interfaces']['dmz'];
369
370
			if ($dmzcfg['if']) {
371
				$config['interfaces']['opt' . $opti] = array();
372
				$optcfg = &$config['interfaces']['opt' . $opti];
373
374
				$optcfg['enable'] = $dmzcfg['enable'];
375
				$optcfg['descr'] = "DMZ";
376
				$optcfg['if'] = $dmzcfg['if'];
377
				$optcfg['ipaddr'] = $dmzcfg['ipaddr'];
378
				$optcfg['subnet'] = $dmzcfg['subnet'];
379
380
				$ifmap['dmz'] = "opt" . $opti;
381
				$opti++;
382
			}
383
384
			unset($config['interfaces']['dmz']);
385
		}
386
387
		/* convert WLAN1/2 to optional, if necessary */
388
		for ($i = 1; isset($config['interfaces']['wlan' . $i]); $i++) {
389
390
			if (!$config['interfaces']['wlan' . $i]['if']) {
391
				unset($config['interfaces']['wlan' . $i]);
392
				continue;
393
			}
394
395
			$wlancfg = &$config['interfaces']['wlan' . $i];
396
			$config['interfaces']['opt' . $opti] = array();
397
			$optcfg = &$config['interfaces']['opt' . $opti];
398
399
			$optcfg['enable'] = $wlancfg['enable'];
400
			$optcfg['descr'] = "WLAN" . $i;
401
			$optcfg['if'] = $wlancfg['if'];
402
			$optcfg['ipaddr'] = $wlancfg['ipaddr'];
403
			$optcfg['subnet'] = $wlancfg['subnet'];
404
			$optcfg['bridge'] = $wlancfg['bridge'];
405
406
			$optcfg['wireless'] = array();
407
			$optcfg['wireless']['mode'] = $wlancfg['mode'];
408
			$optcfg['wireless']['ssid'] = $wlancfg['ssid'];
409
			$optcfg['wireless']['channel'] = $wlancfg['channel'];
410
			$optcfg['wireless']['wep'] = $wlancfg['wep'];
411
412
			$ifmap['wlan' . $i] = "opt" . $opti;
413
414
			unset($config['interfaces']['wlan' . $i]);
415
			$opti++;
416
		}
417
418
		/* convert filter rules */
419
		$n = count($config['filter']['rule']);
420
		for ($i = 0; $i < $n; $i++) {
421
422
			$fr = &$config['filter']['rule'][$i];
423
424
			/* remap interface */
425
			if (array_key_exists($fr['interface'], $ifmap))
426
				$fr['interface'] = $ifmap[$fr['interface']];
427
			else {
428
				/* remove the rule */
429
				echo "\nWarning: filter rule removed " .
430
					"(interface '{$fr['interface']}' does not exist anymore).";
431
				unset($config['filter']['rule'][$i]);
432
				continue;
433
			}
434
435
			/* remap source network */
436
			if (isset($fr['source']['network'])) {
437
				if (array_key_exists($fr['source']['network'], $ifmap))
438
					$fr['source']['network'] = $ifmap[$fr['source']['network']];
439
				else {
440
					/* remove the rule */
441
					echo "\nWarning: filter rule removed " .
442
						"(source network '{$fr['source']['network']}' does not exist anymore).";
443
					unset($config['filter']['rule'][$i]);
444
					continue;
445
				}
446
			}
447
448
			/* remap destination network */
449
			if (isset($fr['destination']['network'])) {
450
				if (array_key_exists($fr['destination']['network'], $ifmap))
451
					$fr['destination']['network'] = $ifmap[$fr['destination']['network']];
452
				else {
453
					/* remove the rule */
454
					echo "\nWarning: filter rule removed " .
455
						"(destination network '{$fr['destination']['network']}' does not exist anymore).";
456
					unset($config['filter']['rule'][$i]);
457
					continue;
458
				}
459
			}
460
		}
461
462
		/* convert shaper rules */
463
		$n = count($config['pfqueueing']['rule']);
464
		if (is_array($config['pfqueueing']['rule']))
465
			for ($i = 0; $i < $n; $i++) {
466
467
			$fr = &$config['pfqueueing']['rule'][$i];
468
469
			/* remap interface */
470
			if (array_key_exists($fr['interface'], $ifmap))
471
				$fr['interface'] = $ifmap[$fr['interface']];
472
			else {
473
				/* remove the rule */
474
				echo "\nWarning: traffic shaper rule removed " .
475
					"(interface '{$fr['interface']}' does not exist anymore).";
476
				unset($config['pfqueueing']['rule'][$i]);
477
				continue;
478
			}
479
480
			/* remap source network */
481
			if (isset($fr['source']['network'])) {
482
				if (array_key_exists($fr['source']['network'], $ifmap))
483
					$fr['source']['network'] = $ifmap[$fr['source']['network']];
484
				else {
485
					/* remove the rule */
486
					echo "\nWarning: traffic shaper rule removed " .
487
						"(source network '{$fr['source']['network']}' does not exist anymore).";
488
					unset($config['pfqueueing']['rule'][$i]);
489
					continue;
490
				}
491
			}
492
493
			/* remap destination network */
494
			if (isset($fr['destination']['network'])) {
495
				if (array_key_exists($fr['destination']['network'], $ifmap))
496
					$fr['destination']['network'] = $ifmap[$fr['destination']['network']];
497
				else {
498
					/* remove the rule */
499
					echo "\nWarning: traffic shaper rule removed " .
500
						"(destination network '{$fr['destination']['network']}' does not exist anymore).";
501
					unset($config['pfqueueing']['rule'][$i]);
502
					continue;
503
				}
504
			}
505
		}
506
507
		$config['version'] = "1.1";
508
	}
509
510
	/* convert 1.1 -> 1.2 */
511
	if ($config['version'] == "1.1") {
512
		/* move LAN DHCP server config */
513
		$tmp = $config['dhcpd'];
514
		$config['dhcpd'] = array();
515
		$config['dhcpd']['lan'] = $tmp;
516
517
		/* encrypt password */
518
		$config['system']['password'] = crypt($config['system']['password']);
519
520
		$config['version'] = "1.2";
521
	}
522
523
	/* convert 1.2 -> 1.3 */
524
	if ($config['version'] == "1.2") {
525
		/* convert advanced outbound NAT config */
526
		for ($i = 0; isset($config['nat']['advancedoutbound']['rule'][$i]); $i++) {
527
			$curent = &$config['nat']['advancedoutbound']['rule'][$i];
528
			$src = $curent['source'];
529
			$curent['source'] = array();
530
			$curent['source']['network'] = $src;
531
			$curent['destination'] = array();
532
			$curent['destination']['any'] = true;
533
		}
534
535
		/* add an explicit type="pass" to all filter rules to make things consistent */
536
		for ($i = 0; isset($config['filter']['rule'][$i]); $i++) {
537
			$config['filter']['rule'][$i]['type'] = "pass";
538
		}
539
540
		$config['version'] = "1.3";
541
	}
542
543
	/* convert 1.3 -> 1.4 */
544
	if ($config['version'] == "1.3") {
545
		/* convert shaper rules (make pipes) */
546
		if (is_array($config['pfqueueing']['rule'])) {
547
			$config['pfqueueing']['pipe'] = array();
548
549
			for ($i = 0; isset($config['pfqueueing']['rule'][$i]); $i++) {
550
				$curent = &$config['pfqueueing']['rule'][$i];
551
552
				/* make new pipe and associate with this rule */
553
				$newpipe = array();
554
				$newpipe['descr'] = $curent['descr'];
555
				$newpipe['bandwidth'] = $curent['bandwidth'];
556
				$newpipe['delay'] = $curent['delay'];
557
				$newpipe['mask'] = $curent['mask'];
558
				$config['pfqueueing']['pipe'][$i] = $newpipe;
559
560
				$curent['targetpipe'] = $i;
561
562
				unset($curent['bandwidth']);
563
				unset($curent['delay']);
564
				unset($curent['mask']);
565
			}
566
		}
567
568
		$config['version'] = "1.4";
569
	}
570
571 88f66e13 Bill Marquette
	/* Convert 1.4 -> 1.5 */
572
	if ($config['version'] == "1.4") {
573
574
		/* Default route moved */
575
		if (isset($config['interfaces']['wan']['gateway']))
576
			if ($config['interfaces']['wan']['gateway'] <> "")
577
				$config['system']['gateway'] = $config['interfaces']['wan']['gateway'];
578
		unset($config['interfaces']['wan']['gateway']);
579
580
                /* Queues are no longer interface specific */
581
                if (isset($config['interfaces']['lan']['schedulertype']))
582
                        unset($config['interfaces']['lan']['schedulertype']);
583
                if (isset($config['interfaces']['wan']['schedulertype']))
584
                        unset($config['interfaces']['wan']['schedulertype']);
585
586
                for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
587
                        if(isset($config['interfaces']['opt' . $i]['schedulertype']))
588
                                unset($config['interfaces']['opt' . $i]['schedulertype']);
589
                }
590
591
		$config['version'] = "1.5";
592
	}
593
594 45cb953d Colin Smith
	/* Convert 1.5 -> 1.6 */
595 208c4390 Colin Smith
	if ($config['version'] == "1.5") {
596
		/* Alternate firmware URL moved */
597
		if (isset($config['system']['firmwareurl']) && isset($config['system']['firmwarename'])) { // Only convert if *both* are defined.
598
			$config['system']['alt_firmware_url'] = array();
599
			$config['system']['alt_firmware_url']['enabled'] = "";
600
			$config['system']['alt_firmware_url']['firmware_base_url'] = $config['system']['firmwareurl'];
601
			$config['system']['alt_firmware_url']['firmware_filename'] = $config['system']['firmwarename'];
602
			unset($config['system']['firmwareurl'], $config['system']['firmwarename']);
603
		} else {
604
			unset($config['system']['firmwareurl'], $config['system']['firmwarename']);
605
		}
606 45cb953d Colin Smith
607 208c4390 Colin Smith
		$config['version'] = "1.6";
608
	}
609 7756e2b7 Scott Ullrich
	
610
	/* Convert 1.6 -> 1.7 */
611
	if ($config['version'] == "1.6") {
612
		/* wipe previous shaper configuration */
613
		unset($config['shaper']['queue']);
614
		unset($config['shaper']['rule']);
615
		unset($config['interfaces']['wan']['bandwidth']);
616
		unset($config['interfaces']['wan']['bandwidthtype']);
617
		unset($config['interfaces']['lan']['bandwidth']);
618
		unset($config['interfaces']['lan']['bandwidthtype']);		
619
		$config['shaper']['enable'] = FALSE;
620
		$config['version'] = "1.7";	
621
	}
622 1425e067 Bill Marquette
	/* Convert 1.7 -> 1.8 */
623
	if ($config['version'] == "1.7") {
624
		if(isset($config['proxyarp']) && is_array($config['proxyarp']['proxyarpnet'])) {
625
			$proxyarp = &$config['proxyarp']['proxyarpnet'];
626
			foreach($proxyarp as $arpent){
627
				$vip = array();
628
				$vip['mode'] = "proxyarp";
629
				$vip['interface'] = $arpent['interface'];
630
				$vip['descr'] = $arpent['descr'];
631
				if (isset($arpent['range'])) {
632
					$vip['range'] = $arpent['range'];
633
					$vip['type'] = "range";
634
				} else {
635
					$subnet = explode('/', $arpent['network']);
636
					$vip['subnet'] = $subnet[0];
637
					if (isset($subnet[1])) {
638
						$vip['subnet_bits'] = $subnet[1];
639
						$vip['type'] = "network";
640
					} else {
641
						$vip['subnet_bits'] = "32";
642
						$vip['type'] = "single";
643
					}
644
				}
645
				$config['virtualip']['vip'][] = $vip;
646
			}
647
			unset($config['proxyarp']);
648
		}
649
		if(isset($config['installedpackages']) && isset($config['installedpackages']['carp']) && is_array($config['installedpackages']['carp']['config'])) {
650
			$carp = &$config['installedpackages']['carp']['config'];
651
			foreach($carp as $carpent){
652
				$vip = array();
653
				$vip['mode'] = "carp";
654
				$vip['interface'] = "AUTO";
655
				$vip['descr'] = "CARP vhid {$carpent['vhid']}";
656
				$vip['type'] = "single";
657
				$vip['vhid'] = $carpent['vhid'];
658
				$vip['advskew'] = $carpent['advskew'];
659
				$vip['password'] = $carpent['password'];
660
				$vip['subnet'] = $carpent['ipaddress'];
661 c153d430 Bill Marquette
				$vip['subnet_bits'] = $carpent['netmask'];
662 1425e067 Bill Marquette
				$config['virtualip']['vip'][] = $vip;
663
			}
664
			unset($config['installedpackages']['carp']);
665
		}
666 e82f32ef Bill Marquette
		/* Server NAT is no longer needed */
667
		unset($config['nat']['servernat']);
668 1425e067 Bill Marquette
		
669 e4662fc5 Scott Ullrich
		/* enable SSH */
670
		if ($config['version'] == "1.8") {
671
			$config['system']['sshenabled'] = true;
672
		}
673
		
674
		$config['version'] = "1.9";
675 1425e067 Bill Marquette
	}
676 e42cac89 Scott Ullrich
677
	/* Convert 1.8 -> 1.9 */
678 8cfa169c Scott Ullrich
	if ($config['version'] == "1.8") {
679 e42cac89 Scott Ullrich
		$config['theme']="metallic";
680
		$config['version'] = "1.9";
681
	}
682 1a82cbcb Bill Marquette
	/* Convert 1.9 -> 2.0 */
683 faee6cd6 Bill Marquette
	if ($config['version'] == "1.9") {
684
		if(is_array($config['ipsec']['tunnel'])) {
685
			reset($config['ipsec']['tunnel']);
686 0e8f4e7b Bill Marquette
			while (list($index, $tunnel) = each($config['ipsec']['tunnel'])) {
687 faee6cd6 Bill Marquette
				/* Sanity check on required variables */
688
				/* This fixes bogus <tunnel> entries - remnant of bug #393 */
689
				if (!isset($tunnel['local-subnet']) && !isset($tunnel['remote-subnet'])) {
690
					unset($config['ipsec']['tunnel'][$tunnel]);
691
				}
692
			}
693
        	}
694 6823bfb6 Scott Ullrich
		$config['version'] = "2.0";
695 faee6cd6 Bill Marquette
	}
696 0f20c092 Bill Marquette
	/* Convert 2.0 -> 2.1 */
697
	if ($config['version'] == "2.0") {
698
		/* shaper scheduler moved */
699
		if(isset($config['system']['schedulertype'])) {
700
			$config['shaper']['schedulertype'] = $config['system']['schedulertype'];
701
			unset($config['system']['schedulertype']);
702
		}
703
		$config['version'] = "2.1";
704
	}
705 faee6cd6 Bill Marquette
706 45cb953d Colin Smith
707 d05dff82 Bill Marquette
	if ($prev_version != $config['version'])
708 fc13ca75 Bill Marquette
		write_config("Upgraded config version level from {$prev_version} to {$config['version']}");
709 5b237745 Scott Ullrich
}
710
711 840c97dc Colin Smith
/****f* config/write_config
712
 * NAME
713
 *   write_config - Backup and write the firewall configuration.
714
 * DESCRIPTION
715
 *   write_config() handles backing up the current configuration,
716
 *   applying changes, and regenerating the configuration cache.
717
 * INPUTS
718
 *   $desc	- string containing the a description of configuration changes
719
 *   $backup	- boolean: do not back up current configuration if false.
720
 * RESULT
721
 *   null       
722
 ******/
723 5b237745 Scott Ullrich
/* save the system configuration */
724 41508358 Scott Ullrich
function write_config($desc="Unknown", $backup = true) {
725 5b237745 Scott Ullrich
	global $config, $g;
726
727 ac24ba53 Scott Ullrich
	if($g['platform'] <> "wrap") {
728
		if($backup) backup_config();
729
	}
730 5b237745 Scott Ullrich
731 41508358 Scott Ullrich
	if (time() > mktime(0, 0, 0, 9, 1, 2004))       /* make sure the clock settings are plausible */
732
                $changetime = time();
733 a331fd27 Colin Smith
734 41508358 Scott Ullrich
	/* Log the running script so it's not entirely unlogged what changed */ 
735
        if ($desc == "Unknown") 
736
        	$desc = "{$_SERVER['SCRIPT_NAME']} made unknown change";  
737 b638fcfd Colin Smith
738 41508358 Scott Ullrich
	$config['revision']['description'] = $desc;
739
	$config['revision']['time'] = $changetime;
740
	
741
	config_lock();
742
	conf_mount_rw();
743 5b237745 Scott Ullrich
744
	/* generate configuration XML */
745
	$xmlconfig = dump_xml_config($config, $g['xml_rootobj']);
746
747 41508358 Scott Ullrich
	/* write new configuration */
748 5b237745 Scott Ullrich
	$fd = fopen("{$g['cf_conf_path']}/config.xml", "w");
749
	if (!$fd)
750 2e523ffe Bill Marquette
		die("Unable to open {$g['cf_conf_path']}/config.xml for writing in write_config()\n");
751 5b237745 Scott Ullrich
	fwrite($fd, $xmlconfig);
752
	fclose($fd);
753
754 bc89f7d3 Scott Ullrich
	if($g['booting'] <> true) {
755 02facbd0 Scott Ullrich
		conf_mount_ro();
756
	}
757
758 5b237745 Scott Ullrich
	config_unlock();
759 412ebed9 Bill Marquette
760 7cc29855 Colin Smith
	// Always reparse the config after it's written - something is getting lost in serialize().
761 1b720c35 Colin Smith
	$config = parse_config(true);
762 917d4a96 Colin Smith
	return $config;
763 5b237745 Scott Ullrich
}
764
765 840c97dc Colin Smith
/****f* config/reset_factory_defaults
766
 * NAME
767
 *   reset_factory_defaults - Reset the system to its default configuration.
768
 * RESULT
769
 *   integer	- indicates completion
770
 ******/
771 5b237745 Scott Ullrich
function reset_factory_defaults() {
772
	global $g;
773
774
	config_lock();
775
	conf_mount_rw();
776
777
	/* create conf directory, if necessary */
778 d058b0be Colin Smith
	safe_mkdir("{$g['cf_conf_path']}");
779 5b237745 Scott Ullrich
780
	/* clear out /conf */
781
	$dh = opendir($g['conf_path']);
782
	while ($filename = readdir($dh)) {
783
		if (($filename != ".") && ($filename != "..")) {
784 e50b0c5d Bill Marquette
			unlink_if_exists($g['conf_path'] . "/" . $filename);
785 5b237745 Scott Ullrich
		}
786
	}
787
	closedir($dh);
788
789
	/* copy default configuration */
790 e50b0c5d Bill Marquette
	copy("{$g['conf_default_path']}/config.xml", "{$g['conf_path']}/config.xml");
791 4973d941 Scott Ullrich
	
792
	/* call the wizard */
793
	touch("/trigger_initial_wizard");
794
	
795 5b237745 Scott Ullrich
	conf_mount_ro();
796
	config_unlock();
797
798
	return 0;
799
}
800
801 41508358 Scott Ullrich
function config_restore($conffile) {
802 5b237745 Scott Ullrich
	global $config, $g;
803 1f4adc3e Colin Smith
       
804 41508358 Scott Ullrich
        if (!file_exists($conffile))
805
                return 1;
806
        
807
        config_lock();
808
        conf_mount_rw();        
809
        
810
        backup_config();
811 1f4adc3e Colin Smith
        copy($conffile, "{$g['cf_conf_path']}/config.xml");
812
	$config = parse_config(true);
813 41508358 Scott Ullrich
        write_config("Reverted to " . array_pop(explode("/", $conffile)) . ".", false);
814
        
815
        conf_mount_ro();
816
        config_unlock();
817
818
        return 0;
819
}
820 5b237745 Scott Ullrich
821 ffd1b445 Scott Ullrich
822
823 41508358 Scott Ullrich
function config_install($conffile) {
824
        global $config, $g;
825
        
826
        if (!file_exists($conffile))
827
                return 1;
828 7f8d1f3f Scott Ullrich
829
	if($g['booting'] == true)
830
		echo "Installing configuration...\n";
831 41508358 Scott Ullrich
 
832
        config_lock();
833
        conf_mount_rw();
834
        
835
        copy($conffile, "{$g['conf_path']}/config.xml");
836
                
837
        conf_mount_ro();
838
        config_unlock();
839
840
        return 0;
841 5b237745 Scott Ullrich
}
842
843
/* lock configuration file, decide that the lock file is stale after
844
   10 seconds */
845
function config_lock() {
846 7cf78912 Bill Marquette
	global $g, $process_lock;
847 5b237745 Scott Ullrich
848 7cf78912 Bill Marquette
	/* No need to continue if we're the ones holding the lock */
849
	if ($process_lock)
850
		return;
851 5b237745 Scott Ullrich
852
	$lockfile = "{$g['varrun_path']}/config.lock";
853
854
	$n = 0;
855
	while ($n < 10) {
856
		/* open the lock file in append mode to avoid race condition */
857
		if ($fd = @fopen($lockfile, "x")) {
858
			/* succeeded */
859 7cf78912 Bill Marquette
			$process_lock = true;
860 5b237745 Scott Ullrich
			fclose($fd);
861
			return;
862
		} else {
863
			/* file locked, wait and try again */
864 7cf78912 Bill Marquette
			$process_lock = false;
865 5b237745 Scott Ullrich
			sleep(1);
866
			$n++;
867
		}
868
	}
869
}
870
871
/* unlock configuration file */
872
function config_unlock() {
873 7cf78912 Bill Marquette
	global $g, $process_lock;
874 5b237745 Scott Ullrich
875
	$lockfile = "{$g['varrun_path']}/config.lock";
876 7cf78912 Bill Marquette
	$process_lock = false;
877 5b237745 Scott Ullrich
878 e50b0c5d Bill Marquette
	unlink_if_exists($lockfile);
879 5b237745 Scott Ullrich
}
880
881 afe53428 Scott Ullrich
function set_networking_interfaces_ports() {
882 bb17ff99 Scott Ullrich
	global $noreboot;
883 c1666878 Scott Ullrich
	global $config;
884
	global $g;
885 e522f83c Scott Ullrich
	global $fp;
886 bb17ff99 Scott Ullrich
887 afe53428 Scott Ullrich
	$fp = fopen('php://stdin', 'r');
888
889 4fde4ce4 Colin Smith
	$iflist = get_interface_list();
890 afe53428 Scott Ullrich
891
	echo <<<EOD
892
893
Valid interfaces are:
894
895
896
EOD;
897
898
	foreach ($iflist as $iface => $ifa) {
899
		echo sprintf("% -8s%s%s\n", $iface, $ifa['mac'],
900
			$ifa['up'] ? "   (up)" : "");
901
	}
902
903
	echo <<<EOD
904
905
Do you want to set up VLANs first?
906 763f6238 Scott Ullrich
If you are not going to use VLANs, or only for optional interfaces, you
907 afe53428 Scott Ullrich
should say no here and use the webGUI to configure VLANs later, if required.
908
909
Do you want to set up VLANs now [y|n]?
910
EOD;
911
912
	if (strcasecmp(chop(fgets($fp)), "y") == 0)
913
		vlan_setup();
914
915
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
916
917
		echo "\n\nVLAN interfaces:\n\n";
918
		$i = 0;
919
		foreach ($config['vlans']['vlan'] as $vlan) {
920
921
			echo sprintf("% -8s%s\n", "vlan{$i}",
922
				"VLAN tag {$vlan['tag']}, interface {$vlan['if']}");
923
924
			$iflist['vlan' . $i] = array();
925
			$i++;
926
		}
927
	}
928
929
	echo <<<EOD
930
931 4fde4ce4 Colin Smith
*NOTE*  pfSense requires *ATLEAST* 2 assigned interfaces to function.
932
        If you do not have two interfaces turn off the machine until
933
	you do.
934
935 763f6238 Scott Ullrich
If you do not know the names of your interfaces, you may choose to use
936 03efea5e Scott Ullrich
auto-detection... In that case, disconnect all interfaces now before
937
hitting a.   The system will then prompt you to plug in each nic to
938
autodetect.
939 afe53428 Scott Ullrich
940
EOD;
941
942
	do {
943
		echo "\nEnter the LAN interface name or 'a' for auto-detection: ";
944
		$lanif = chop(fgets($fp));
945
		if ($lanif === "") {
946
			exit(0);
947
		}
948
949
		if ($lanif === "a")
950
			$lanif = autodetect_interface("LAN", $fp);
951
		else if (!array_key_exists($lanif, $iflist)) {
952
			echo "\nInvalid interface name '{$lanif}'\n";
953
			unset($lanif);
954
			continue;
955
		}
956
	} while (!$lanif);
957
958
	do {
959
		echo "\nEnter the WAN interface name or 'a' for auto-detection: ";
960
		$wanif = chop(fgets($fp));
961
		if ($wanif === "") {
962
			exit(0);
963
		}
964
		if ($wanif === "a")
965
			$wanif = autodetect_interface("WAN", $fp);
966
		else if (!array_key_exists($wanif, $iflist)) {
967
			echo "\nInvalid interface name '{$wanif}'\n";
968
			unset($wanif);
969
			continue;
970
		}
971
	} while (!$wanif);
972
973
	/* optional interfaces */
974
	$i = 0;
975
	$optif = array();
976
977
	while (1) {
978
		if ($optif[$i])
979
			$i++;
980
		$i1 = $i + 1;
981
		echo "\nEnter the Optional {$i1} interface name or 'a' for auto-detection\n" .
982
			"(or nothing if finished): ";
983
		$optif[$i] = chop(fgets($fp));
984
985
		if ($optif[$i]) {
986
			if ($optif[$i] === "a") {
987
				$ad = autodetect_interface("Optional " . $i1, $fp);
988
				if ($ad)
989
					$optif[$i] = $ad;
990
				else
991
					unset($optif[$i]);
992
			} else if (!array_key_exists($optif[$i], $iflist)) {
993
				echo "\nInvalid interface name '{$optif[$i]}'\n";
994
				unset($optif[$i]);
995
				continue;
996
			}
997
		} else {
998
			unset($optif[$i]);
999
			break;
1000
		}
1001
	}
1002
1003
	/* check for double assignments */
1004
	$ifarr = array_merge(array($lanif, $wanif), $optif);
1005
1006
	for ($i = 0; $i < (count($ifarr)-1); $i++) {
1007
		for ($j = ($i+1); $j < count($ifarr); $j++) {
1008
			if ($ifarr[$i] == $ifarr[$j]) {
1009
				echo <<<EOD
1010
1011 763f6238 Scott Ullrich
Error: you cannot assign the same interface name twice!
1012 afe53428 Scott Ullrich
1013
EOD;
1014
1015
				exit(0);
1016
			}
1017
		}
1018
	}
1019
1020
	echo <<<EOD
1021
1022
The interfaces will be assigned as follows:
1023
1024
LAN  -> {$lanif}
1025
WAN  -> {$wanif}
1026
1027
EOD;
1028
1029
	for ($i = 0; $i < count($optif); $i++) {
1030
		echo "OPT" . ($i+1) . " -> " . $optif[$i] . "\n";
1031
	}
1032
1033 bb17ff99 Scott Ullrich
	if(!$noreboot) echo "\npfSense will reboot after saving the changes.\n";
1034 afe53428 Scott Ullrich
1035 bb17ff99 Scott Ullrich
echo <<<EOD
1036 afe53428 Scott Ullrich
1037
Do you want to proceed [y|n]?
1038
EOD;
1039
1040
	if (strcasecmp(chop(fgets($fp)), "y") == 0) {
1041
1042
		$config['interfaces']['lan']['if'] = $lanif;
1043 fd91e85c Scott Ullrich
		if (preg_match($g['wireless_regex'], $lanif)) {
1044 afe53428 Scott Ullrich
			if (!is_array($config['interfaces']['lan']['wireless']))
1045
				$config['interfaces']['lan']['wireless'] = array();
1046
		} else {
1047
			unset($config['interfaces']['lan']['wireless']);
1048
		}
1049 fd91e85c Scott Ullrich
		
1050 afe53428 Scott Ullrich
		$config['interfaces']['wan']['if'] = $wanif;
1051 fd91e85c Scott Ullrich
		if (preg_match($g['wireless_regex'], $wanif)) {
1052 afe53428 Scott Ullrich
			if (!is_array($config['interfaces']['wan']['wireless']))
1053
				$config['interfaces']['wan']['wireless'] = array();
1054
		} else {
1055
			unset($config['interfaces']['wan']['wireless']);
1056
		}
1057 fd91e85c Scott Ullrich
		
1058 afe53428 Scott Ullrich
		for ($i = 0; $i < count($optif); $i++) {
1059
			if (!is_array($config['interfaces']['opt' . ($i+1)]))
1060
				$config['interfaces']['opt' . ($i+1)] = array();
1061 fd91e85c Scott Ullrich
			
1062 afe53428 Scott Ullrich
			$config['interfaces']['opt' . ($i+1)]['if'] = $optif[$i];
1063 fd91e85c Scott Ullrich
			
1064 afe53428 Scott Ullrich
			/* wireless interface? */
1065 fd91e85c Scott Ullrich
			if (preg_match($g['wireless_regex'], $optif[$i])) {
1066 afe53428 Scott Ullrich
				if (!is_array($config['interfaces']['opt' . ($i+1)]['wireless']))
1067
					$config['interfaces']['opt' . ($i+1)]['wireless'] = array();
1068
			} else {
1069
				unset($config['interfaces']['opt' . ($i+1)]['wireless']);
1070
			}
1071 fd91e85c Scott Ullrich
			
1072 afe53428 Scott Ullrich
			unset($config['interfaces']['opt' . ($i+1)]['enable']);
1073
			$config['interfaces']['opt' . ($i+1)]['descr'] = "OPT" . ($i+1);
1074
		}
1075 fd91e85c Scott Ullrich
		
1076 afe53428 Scott Ullrich
		/* remove all other (old) optional interfaces */
1077
		for (; isset($config['interfaces']['opt' . ($i+1)]); $i++)
1078
			unset($config['interfaces']['opt' . ($i+1)]);
1079 fd91e85c Scott Ullrich
		
1080 4973d941 Scott Ullrich
		conf_mount_rw();
1081
		
1082
		/* call the wizard */
1083
		touch("/trigger_initial_wizard");
1084
		
1085 fd91e85c Scott Ullrich
		write_config();
1086
		
1087 afe53428 Scott Ullrich
		echo <<<EOD
1088
1089 bb17ff99 Scott Ullrich
1090 afe53428 Scott Ullrich
1091
EOD;
1092
1093
		if($noreboot <> true)
1094
			system_reboot_sync();
1095
	}
1096 1fc6d183 Scott Ullrich
}
1097 afe53428 Scott Ullrich
1098 1fc6d183 Scott Ullrich
function autodetect_interface($ifname, $fp) {
1099 9249b756 Colin Smith
	$iflist_prev = get_interface_list("media");
1100 1fc6d183 Scott Ullrich
	echo <<<EOD
1101 afe53428 Scott Ullrich
1102
Connect the {$ifname} interface now and make sure that the link is up.
1103
Then press ENTER to continue.
1104
1105
EOD;
1106 1fc6d183 Scott Ullrich
	fgets($fp);
1107 9249b756 Colin Smith
	$iflist = get_interface_list("media");
1108 4fde4ce4 Colin Smith
1109 1fc6d183 Scott Ullrich
	foreach ($iflist_prev as $ifn => $ifa) {
1110
		if (!$ifa['up'] && $iflist[$ifn]['up']) {
1111
			echo "Detected link-up on interface {$ifn}.\n";
1112
			return $ifn;
1113 afe53428 Scott Ullrich
		}
1114
	}
1115
1116 1fc6d183 Scott Ullrich
	echo "No link-up detected.\n";
1117
1118
	return null;
1119 e522f83c Scott Ullrich
}
1120 afe53428 Scott Ullrich
1121 e522f83c Scott Ullrich
function vlan_setup() {
1122
	global $iflist, $config, $g, $fp;
1123 afe53428 Scott Ullrich
1124 d8dc587b Scott Ullrich
	$iflist = get_interface_list();
1125
1126 e522f83c Scott Ullrich
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
1127
1128
	echo <<<EOD
1129 afe53428 Scott Ullrich
1130
WARNING: all existing VLANs will be cleared if you proceed!
1131
1132
Do you want to proceed [y|n]?
1133
EOD;
1134
1135 e522f83c Scott Ullrich
	if (strcasecmp(chop(fgets($fp)), "y") != 0)
1136
		return;
1137
	}
1138 afe53428 Scott Ullrich
1139 e522f83c Scott Ullrich
	$config['vlans']['vlan'] = array();
1140
	echo "\n";
1141 afe53428 Scott Ullrich
1142 e522f83c Scott Ullrich
	while (1) {
1143
		$vlan = array();
1144 afe53428 Scott Ullrich
1145 e522f83c Scott Ullrich
		echo "\nEnter the parent interface name for the new VLAN (or nothing if finished): ";
1146
		$vlan['if'] = chop(fgets($fp));
1147 afe53428 Scott Ullrich
1148 e522f83c Scott Ullrich
		if ($vlan['if']) {
1149 6a32d1e5 Scott Ullrich
			if (!array_key_exists($vlan['if'], $iflist) or
1150
			    !is_jumbo_capable($vlan['if'])) {
1151 e522f83c Scott Ullrich
				echo "\nInvalid interface name '{$vlan['if']}'\n";
1152 afe53428 Scott Ullrich
				continue;
1153
			}
1154 e522f83c Scott Ullrich
		} else {
1155
			break;
1156
		}
1157
1158
		echo "Enter the VLAN tag (1-4094): ";
1159
		$vlan['tag'] = chop(fgets($fp));
1160 afe53428 Scott Ullrich
1161 e522f83c Scott Ullrich
		if (!is_numericint($vlan['tag']) || ($vlan['tag'] < 1) || ($vlan['tag'] > 4094)) {
1162
			echo "\nInvalid VLAN tag '{$vlan['tag']}'\n";
1163
			continue;
1164 afe53428 Scott Ullrich
		}
1165 e522f83c Scott Ullrich
1166
		$config['vlans']['vlan'][] = $vlan;
1167 afe53428 Scott Ullrich
	}
1168
}
1169
1170 dd78523c Scott Ullrich
function system_start_ftp_helpers() {
1171 613bdee0 Scott Ullrich
	require_once("interfaces.inc");
1172 5c6d0f65 Colin Smith
	global $config, $g;
1173 6873a9a4 Scott Ullrich
1174
	/* if the ftp proxy is disabled then killall pftpx and return */	
1175 dce949b0 Scott Ullrich
	if($config['system']['disableftpproxy'] <> "") {
1176
		mwexec("/usr/bin/killall pftpx");
1177 8cac5101 Scott Ullrich
		return;
1178 dce949b0 Scott Ullrich
	}
1179 6873a9a4 Scott Ullrich
	
1180
	/* grab the current WAN IP address */
1181 8cac5101 Scott Ullrich
	$wanip = get_current_wan_address();
1182 6873a9a4 Scott Ullrich
	
1183
	/* kill off pftpx if its already running */
1184 b609d80e Scott Ullrich
	if(is_process_running("pftpx"))
1185 110da661 Scott Ullrich
		mwexec("/usr/bin/killall pftpx 2>/dev/null");
1186 6873a9a4 Scott Ullrich
1187
	/* if we do not have a wanip, launch with just the -g flag */	
1188
	if($wanip <> "") {
1189
		$command = "/usr/local/sbin/pftpx -g 8021 {$wanip}";
1190
		mwexec($command);
1191
	} else {
1192
		mwexec("/usr/local/sbin/pftpx -g 8021");
1193
	}
1194 dd78523c Scott Ullrich
}
1195
1196 7cc29855 Colin Smith
function cleanup_backupcache($revisions = 30) {
1197 392a9bb8 Colin Smith
	global $g;
1198 41508358 Scott Ullrich
	$i = false;
1199 dc74c78a Colin Smith
	if(file_exists($g['cf_conf_path'] . '/backup/backup.cache')) {
1200 3fd7c6af Scott Ullrich
		conf_mount_rw();
1201 dc74c78a Colin Smith
		$backups = get_backups();
1202
		$newbaks = array();
1203 8a421967 Colin Smith
		$bakfiles = glob($g['cf_conf_path'] . "/backup/config-*");
1204 dc74c78a Colin Smith
		$baktimes = $backups['versions'];
1205
		$tocache = array();
1206
		unset($backups['versions']);
1207
       		foreach($bakfiles as $backup) { // Check for backups in the directory not represented in the cache.
1208
			$tocheck = array_shift(explode('.', array_pop(explode('-', $backup))));	
1209
                	if(!in_array($tocheck, $baktimes)) {
1210
				$i = true;
1211
				if($bootup) print " " . $tocheck . "a";
1212
				$newxml = parse_xml_config($backup, $g['xml_rootobj']);
1213
				if($newxml['revision']['description'] == "") $newxml['revision']['description'] = "Unknown";
1214
				$tocache[$tocheck] = array('description' => $newxml['revision']['description']);
1215
			}
1216
        	}
1217
		foreach($backups as $checkbak) {
1218
			if(count(preg_grep('/' . $checkbak['time'] . '/i', $bakfiles)) != 0) {
1219
				$newbaks[] = $checkbak;
1220
			} else {
1221
				$i = true;
1222
				if($bootup) print " " . $tocheck . "r";
1223
			}
1224 41508358 Scott Ullrich
		}
1225 dc74c78a Colin Smith
		foreach($newbaks as $todo) $tocache[$todo['time']] = array('description' => $todo['description']);	
1226 7cc29855 Colin Smith
		if(is_int($revisions) and (count($tocache) > $revisions)) {
1227 dc74c78a Colin Smith
			$toslice = array_slice(array_keys($tocache), 0, $revisions);
1228
			foreach($toslice as $sliced) $newcache[$sliced] = $tocache[$sliced];
1229
			foreach($tocache as $version => $versioninfo) {
1230
				if(!in_array($version, array_keys($newcache))) {
1231
					unlink_if_exists($g['conf_path'] . '/backup/config-' . $version . '.xml');
1232
					if($bootup) print " " . $tocheck . "d";
1233
				}
1234 41508358 Scott Ullrich
			}
1235 dc74c78a Colin Smith
			$tocache = $newcache;
1236 41508358 Scott Ullrich
		}
1237 dc74c78a Colin Smith
		$bakout = fopen($g['cf_conf_path'] . '/backup/backup.cache', "w");
1238
        	fwrite($bakout, serialize($tocache));
1239
  	        fclose($bakout);
1240 3fd7c6af Scott Ullrich
		conf_mount_ro();
1241 41508358 Scott Ullrich
	}
1242 5c6d0f65 Colin Smith
	if($g['booting']) {
1243 41508358 Scott Ullrich
		if($i) {
1244 deebaae1 Scott Ullrich
			print "done.\n";
1245 41508358 Scott Ullrich
		}
1246
	}
1247
}
1248
  	 
1249
function get_backups() { 	 
1250 e50b0c5d Bill Marquette
	global $g;
1251
1252 7e0e716a Bill Marquette
        if(file_exists("{$g['cf_conf_path']}/backup/backup.cache")) {
1253
                $confvers = unserialize(file_get_contents("{$g['cf_conf_path']}/backup/backup.cache"));
1254 41508358 Scott Ullrich
		$bakvers = array_keys($confvers);
1255
		$toreturn = array();
1256
		sort($bakvers);
1257
		// $bakvers = array_reverse($bakvers);
1258
		foreach(array_reverse($bakvers) as $bakver) $toreturn[] = array('time' => $bakver,
1259
								 'description' => $confvers[$bakver]['description']
1260
								);
1261
        } else { 	 
1262
                return false; 	 
1263
        }
1264
	$toreturn['versions'] = $bakvers;
1265
        return $toreturn;
1266
}
1267 926312b6 Colin Smith
1268 41508358 Scott Ullrich
function backup_config() {
1269
	global $config, $g;
1270 e50b0c5d Bill Marquette
1271 8fb3a072 Scott Ullrich
	if($g['platform'] == "cdrom")
1272
		return;
1273
1274 865e08c2 Scott Ullrich
	conf_mount_rw();
1275
1276 e50b0c5d Bill Marquette
	/* Create backup directory if needed */
1277
	safe_mkdir("{$g['cf_conf_path']}/backup");
1278
1279 d058b0be Colin Smith
        if($config['revision']['time'] == "") {
1280 41508358 Scott Ullrich
                $baktime = 0;
1281 d058b0be Colin Smith
        } else {
1282 41508358 Scott Ullrich
                $baktime = $config['revision']['time'];
1283 d058b0be Colin Smith
        }
1284
        if($config['revision']['description'] == "") {
1285 41508358 Scott Ullrich
                $bakdesc = "Unknown";
1286 d058b0be Colin Smith
        } else {
1287 41508358 Scott Ullrich
                $bakdesc = $config['revision']['description'];
1288 d058b0be Colin Smith
        }
1289
        copy($g['cf_conf_path'] . '/config.xml', $g['cf_conf_path'] . '/backup/config-' . $baktime . '.xml');
1290
        if(file_exists($g['cf_conf_path'] . '/backup/backup.cache')) {
1291 41508358 Scott Ullrich
                $backupcache = unserialize(file_get_contents($g['cf_conf_path'] . '/backup/backup.cache'));
1292 d058b0be Colin Smith
        } else {
1293 41508358 Scott Ullrich
                $backupcache = array();
1294 d058b0be Colin Smith
        }
1295 41508358 Scott Ullrich
        $backupcache[$baktime] = array('description' => $bakdesc);
1296 d058b0be Colin Smith
        $bakout = fopen($g['cf_conf_path'] . '/backup/backup.cache', "w");
1297
        fwrite($bakout, serialize($backupcache));
1298
        fclose($bakout);
1299 865e08c2 Scott Ullrich
	
1300
	conf_mount_ro();
1301
	
1302 41508358 Scott Ullrich
	return true;
1303
}
1304 dd2ab8f8 Scott Ullrich
1305
function mute_kernel_msgs() {
1306
	exec("/sbin/conscontrol mute on");
1307
}
1308
1309
function unmute_kernel_msgs() {
1310
	exec("/sbin/conscontrol mute off");
1311
}
1312
1313 caeb3b46 Scott Ullrich
function start_devd() {
1314
	exec("/sbin/devd");
1315
}
1316
1317 e82f32ef Bill Marquette
?>