Project

General

Profile

Download (6.76 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/****h* pfSense/config
3
 * NAME
4
 *   config.inc - Functions to manipulate config.xml
5
 * DESCRIPTION
6
 *   This include contains various config.xml specific functions.
7
 * HISTORY
8
 * $Id$
9
 ******
10

    
11
	config.inc
12
	Copyright (C) 2004-2006 Scott Ullrich
13
	All rights reserved.
14

    
15
	originally part of m0n0wall (http://m0n0.ch/wall)
16
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
17
	All rights reserved.
18

    
19
	Redistribution and use in source and binary forms, with or without
20
	modification, are permitted provided that the following conditions are met:
21

    
22
	1. Redistributions of source code must retain the above copyright notice,
23
	   this list of conditions and the following disclaimer.
24

    
25
	2. Redistributions in binary form must reproduce the above copyright
26
	   notice, this list of conditions and the following disclaimer in the
27
	   documentation and/or other materials provided with the distribution.
28

    
29
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
30
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
31
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
33
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
	POSSIBILITY OF SUCH DAMAGE.
39

    
40

    
41
	pfSense_BUILDER_BINARIES:	/sbin/mount	/sbin/sysctl	/sbin/umount	/sbin/halt	/sbin/fsck	/bin/sync
42
	pfSense_MODULE:	config
43
*/
44
/*
45
 * XXX: Hack around the cvs syntax checks. 
46
 * DISABLE_PHP_LINT_CHECKING
47
 */
48

    
49
if($g['booting']) echo ".";
50

    
51
/* do not load this file twice. */
52
if($config_inc_loaded == true)
53
	return;
54
else
55
	$config_inc_loaded = true;
56

    
57
// Set the memory limit to 128M.  When someone has something like 500+ tunnels
58
// the parser needs quite a bit of ram.   Do not remove this line unless you 
59
// know what you are doing.  If in doubt, check with dev@ _/FIRST/_!
60
ini_set("memory_limit","128M");
61

    
62
/* include globals from notices.inc /utility/XML parser files */
63
require_once('config.lib.inc');
64
if($g['booting']) echo ".";
65
require_once("notices.inc");
66
if($g['booting']) echo ".";
67
require_once("util.inc");
68
if($g['booting']) echo ".";
69
require_once("xmlparse.inc");
70
if($g['booting']) echo ".";
71
require_once("crypt.inc");
72

    
73
/* read platform */
74
if($g['booting']) echo ".";
75
if (file_exists("{$g['etc_path']}/platform")) {
76
	$g['platform'] = chop(file_get_contents("{$g['etc_path']}/platform"));
77
} else {
78
	$g['platform'] = "unknown";
79
}
80

    
81
/* if /debugging exists, lets set $debugging
82
   so we can output more information */
83
if(file_exists("/debugging")) {
84
	$debugging = true;
85
	$g['debug'] = true;
86
}
87

    
88
if($g['booting']) echo ".";
89
if(file_exists("/cf/conf/config.xml")) {
90
	$config_contents = file_get_contents("/cf/conf/config.xml");
91
	if(stristr($config_contents, "<m0n0wall>") == true) {
92
		if($g['booting']) echo ".";
93
		/* user has just upgraded to m0n0wall, replace root xml tags */
94
		log_error("Upgrading m0n0wall configuration to pfSense... ");
95
		$config_contents = str_replace("m0n0wall","pfsense", $config_contents);
96
		if (!config_validate("{$g['conf_path']}/config.xml"))
97
			log_error("ERROR!  Could not convert m0n0wall -> pfsense in config.xml");
98
		conf_mount_rw();
99
		$fd = fopen("/cf/conf/config.xml", "w");
100
		fwrite($fd, $config_contents);
101
		fclose($fd);
102
		conf_mount_ro();
103
	}
104
}
105

    
106
/* if our config file exists bail out, we're already set. */
107
if ($g['booting'] and !file_exists($g['cf_conf_path'] . "/config.xml")  ) {
108
	if($g['booting']) echo ".";
109
	/* find the device where config.xml resides and write out an fstab */
110
	unset($cfgdevice);
111
	if($g['booting']) echo ".";
112
	/* check if there's already an fstab (NFS booting?) */
113
	if (!file_exists("{$g['etc_path']}/fstab")) {
114
		if($g['booting']) echo ".";
115
		if (strstr($g['platform'], "cdrom")) {
116
			/* config is on floppy disk for CD-ROM version */
117
			$cfgdevice = $cfgpartition = "fd0";
118
			$dmesg = `dmesg -a`;
119
			if(ereg("da0", $dmesg) == true) {
120
				$cfgdevice = $cfgpartition = "da0" ;
121
				if (mwexec("/sbin/mount -r /dev/{$cfgdevice} /cf")) {
122
					/* could not mount, fallback to floppy */
123
					$cfgdevice = $cfgpartition = "fd0";
124
				}
125
			}
126
			$cfgfstype = "msdosfs";
127
			echo "CDROM build\n";
128
			echo "   CFG: {$cfgpartition}\n";
129
			echo "  TYPE: {$cfgfstype}\n";
130
		} else {
131
			if($g['booting']) echo ".";
132
			/* probe kernel known disks until we find one with config.xml */
133
			$disks = explode(" ", trim(preg_replace("/kern.disks: /", "", exec("/sbin/sysctl kern.disks"))));
134
			foreach ($disks as $mountdisk) {
135
				/* skip mfs mounted filesystems */
136
				if (strstr($mountdisk, "md"))
137
					continue;
138
				if (mwexec("/sbin/mount -r /dev/{$mountdisk}a {$g['cf_path']}") == 0) {
139
					if (file_exists("{$g['cf_conf_path']}/config.xml")) {
140
						/* found it */
141
						$cfgdevice = $mountdisk;
142
						$cfgpartition = $cfgdevice . "a";
143
						$cfgfstype = "ufs";
144
						echo "Found configuration on $cfgdevice.\n";
145
					}
146

    
147
					mwexec("/sbin/umount -f {$g['cf_path']}");
148

    
149
					if ($cfgdevice)
150
						break;
151
				}
152
				if (mwexec("/sbin/mount -r /dev/{$mountdisk}d {$g['cf_path']}") == 0) {
153
					if($g['booting']) echo ".";
154
					if (file_exists("{$g['cf_conf_path']}/config.xml")) {
155
						/* found it */
156
						$cfgdevice = $mountdisk;
157
						$cfgpartition = $cfgdevice . "d";
158
						$cfgfstype = "ufs";
159
						echo "Found configuration on $cfgdevice.\n";
160
					}
161

    
162
					mwexec("/sbin/umount -f {$g['cf_path']}");
163

    
164
					if ($cfgdevice)
165
						break;
166
				}
167
			}
168
		}
169
		if($g['booting']) echo ".";
170
		if (!$cfgdevice) {
171
			$last_backup = discover_last_backup();
172
			if($last_backup) {
173
				log_error("No config.xml found, attempting last known config restore.");
174
				file_notice("config.xml", "No config.xml found, attempting last known config restore.", "pfSenseConfigurator", "");
175
				restore_backup("/cf/conf/backup/{$last_backup}");
176
			} else {
177
				log_error("No config.xml or config backups found, resetting to factory defaults.");
178
				restore_backup('/conf.default/config.xml');
179
			}
180
		}
181

    
182
		/* write device name to a file for rc.firmware */
183
		$fd = fopen("{$g['varetc_path']}/cfdevice", "w");
184
		fwrite($fd, $cfgdevice . "\n");
185
		fclose($fd);
186

    
187
		/* write out an fstab */
188
		$fd = fopen("{$g['etc_path']}/fstab", "w");
189

    
190
		$fstab = "/dev/{$cfgpartition} {$g['cf_path']} {$cfgfstype} ro 1 1\n";
191
		$fstab .= "proc /proc procfs rw 0 0\n";
192

    
193
		fwrite($fd, $fstab);
194
		fclose($fd);
195
	}
196
	if($g['booting']) echo ".";
197
	/* mount all filesystems */
198
	mwexec("/sbin/mount -a");
199
}
200

    
201

    
202

    
203
if($g['booting']) echo ".";
204
$config = parse_config();
205

    
206
if($config_parsed == true) {
207
	/* process packager manager custom rules */
208
	if(is_dir("/usr/local/pkg/parse_config")) {
209
		run_plugins("/usr/local/pkg/parse_config/");
210
	}
211
}
212

    
213
?>
(10-10/46)