Project

General

Profile

Download (6.82 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 0e16b9ca Scott Ullrich
	Copyright (C) 2004-2006 Scott Ullrich
13 cfc707f7 Scott Ullrich
	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 523855b0 Scott Ullrich
40
41
	pfSense_BUILDER_BINARIES:	/sbin/mount	/sbin/sysctl	/sbin/umount	/sbin/halt	/sbin/fsck	/bin/sync
42
	pfSense_MODULE:	config
43 5b237745 Scott Ullrich
*/
44 11f869ea Ermal Luçi
/*
45
 * XXX: Hack around the cvs syntax checks. 
46
 * DISABLE_PHP_LINT_CHECKING
47
 */
48 5b237745 Scott Ullrich
49 1d9652cf Scott Ullrich
if($g['booting']) echo ".";
50
51 96447b25 Scott Ullrich
/* do not load this file twice. */
52 ab94ba00 Ermal Lu?i
if (is_file_included("/etc/inc/config.inc"))
53 96447b25 Scott Ullrich
	return;
54
55 8ecc250a Scott Ullrich
// Set the memory limit to 128M.  When someone has something like 500+ tunnels
56
// the parser needs quite a bit of ram.   Do not remove this line unless you 
57
// know what you are doing.  If in doubt, check with dev@ _/FIRST/_!
58
ini_set("memory_limit","128M");
59
60 bb1546f6 Ermal Lu?i
/* include globals from notices.inc /utility/XML parser files */
61 12df7edc Erik
require_once('config.lib.inc');
62
if($g['booting']) echo ".";
63 bb1546f6 Ermal Lu?i
require_once("notices.inc");
64 1d9652cf Scott Ullrich
if($g['booting']) echo ".";
65 5b237745 Scott Ullrich
require_once("util.inc");
66 1d9652cf Scott Ullrich
if($g['booting']) echo ".";
67 093bcebc Scott Ullrich
if(file_exists("/cf/conf/use_xmlreader"))
68
	require_once("xmlreader.inc");
69
else
70
	require_once("xmlparse.inc");
71 67fc82b5 Ermal Lu?i
if($g['booting']) echo ".";
72
require_once("crypt.inc");
73 5b237745 Scott Ullrich
74
/* read platform */
75 1d9652cf Scott Ullrich
if($g['booting']) echo ".";
76 5b237745 Scott Ullrich
if (file_exists("{$g['etc_path']}/platform")) {
77
	$g['platform'] = chop(file_get_contents("{$g['etc_path']}/platform"));
78
} else {
79
	$g['platform'] = "unknown";
80
}
81
82 727a2b91 Scott Ullrich
/* if /debugging exists, lets set $debugging
83
   so we can output more information */
84 1324a93a Scott Ullrich
if(file_exists("/debugging")) {
85 727a2b91 Scott Ullrich
	$debugging = true;
86 1324a93a Scott Ullrich
	$g['debug'] = true;
87
}
88 727a2b91 Scott Ullrich
89 1d9652cf Scott Ullrich
if($g['booting']) echo ".";
90 49ed889c Scott Ullrich
if(file_exists("/cf/conf/config.xml")) {
91
	$config_contents = file_get_contents("/cf/conf/config.xml");
92 0394c816 Scott Ullrich
	if(stristr($config_contents, "<m0n0wall>") == true) {
93 87294955 Scott Ullrich
		if($g['booting']) echo ".";
94 49ed889c Scott Ullrich
		/* user has just upgraded to m0n0wall, replace root xml tags */
95 5e2a2a1e Scott Ullrich
		log_error("Upgrading m0n0wall configuration to pfSense... ");
96 d9413241 Scott Ullrich
		$config_contents = str_replace("m0n0wall","pfsense", $config_contents);
97 d2b70684 Scott Ullrich
		if (!config_validate("{$g['conf_path']}/config.xml"))
98 37db27bb Scott Ullrich
			log_error("ERROR!  Could not convert m0n0wall -> pfsense in config.xml");
99 9bbf0b4e Scott Ullrich
		conf_mount_rw();
100 49ed889c Scott Ullrich
		$fd = fopen("/cf/conf/config.xml", "w");
101
		fwrite($fd, $config_contents);
102
		fclose($fd);
103 37db27bb Scott Ullrich
		conf_mount_ro();
104 49ed889c Scott Ullrich
	}
105 e9208daf Scott Ullrich
}
106 727a2b91 Scott Ullrich
107 6e8c1d1c Scott Ullrich
/* if our config file exists bail out, we're already set. */
108
if ($g['booting'] and !file_exists($g['cf_conf_path'] . "/config.xml")  ) {
109 1d9652cf Scott Ullrich
	if($g['booting']) echo ".";
110 5b237745 Scott Ullrich
	/* find the device where config.xml resides and write out an fstab */
111
	unset($cfgdevice);
112 87294955 Scott Ullrich
	if($g['booting']) echo ".";
113 5b237745 Scott Ullrich
	/* check if there's already an fstab (NFS booting?) */
114
	if (!file_exists("{$g['etc_path']}/fstab")) {
115 87294955 Scott Ullrich
		if($g['booting']) echo ".";
116 5b237745 Scott Ullrich
		if (strstr($g['platform'], "cdrom")) {
117
			/* config is on floppy disk for CD-ROM version */
118
			$cfgdevice = $cfgpartition = "fd0";
119 e76de94e Scott Ullrich
			$dmesg = `dmesg -a`;
120 da9de371 Scott Ullrich
			if(ereg("da0", $dmesg) == true) {
121 e76de94e Scott Ullrich
				$cfgdevice = $cfgpartition = "da0" ;
122 da9de371 Scott Ullrich
				if (mwexec("/sbin/mount -r /dev/{$cfgdevice} /cf")) {
123 e76de94e Scott Ullrich
					/* could not mount, fallback to floppy */
124
					$cfgdevice = $cfgpartition = "fd0";
125
				}
126
			}
127 a7592ba0 Scott Ullrich
			$cfgfstype = "msdosfs";
128 c3cbc094 Scott Ullrich
			echo "CDROM build\n";
129
			echo "   CFG: {$cfgpartition}\n";
130
			echo "  TYPE: {$cfgfstype}\n";
131 5b237745 Scott Ullrich
		} else {
132 1d9652cf Scott Ullrich
			if($g['booting']) echo ".";
133 5b237745 Scott Ullrich
			/* probe kernel known disks until we find one with config.xml */
134
			$disks = explode(" ", trim(preg_replace("/kern.disks: /", "", exec("/sbin/sysctl kern.disks"))));
135
			foreach ($disks as $mountdisk) {
136
				/* skip mfs mounted filesystems */
137
				if (strstr($mountdisk, "md"))
138
					continue;
139
				if (mwexec("/sbin/mount -r /dev/{$mountdisk}a {$g['cf_path']}") == 0) {
140
					if (file_exists("{$g['cf_conf_path']}/config.xml")) {
141
						/* found it */
142
						$cfgdevice = $mountdisk;
143
						$cfgpartition = $cfgdevice . "a";
144
						$cfgfstype = "ufs";
145
						echo "Found configuration on $cfgdevice.\n";
146
					}
147
148
					mwexec("/sbin/umount -f {$g['cf_path']}");
149
150 655a80eb Scott Ullrich
					if ($cfgdevice)
151
						break;
152
				}
153
				if (mwexec("/sbin/mount -r /dev/{$mountdisk}d {$g['cf_path']}") == 0) {
154 87294955 Scott Ullrich
					if($g['booting']) echo ".";
155 655a80eb Scott Ullrich
					if (file_exists("{$g['cf_conf_path']}/config.xml")) {
156
						/* found it */
157
						$cfgdevice = $mountdisk;
158 4de945fa Scott Ullrich
						$cfgpartition = $cfgdevice . "d";
159 655a80eb Scott Ullrich
						$cfgfstype = "ufs";
160
						echo "Found configuration on $cfgdevice.\n";
161
					}
162
163
					mwexec("/sbin/umount -f {$g['cf_path']}");
164
165 5b237745 Scott Ullrich
					if ($cfgdevice)
166
						break;
167
				}
168
			}
169
		}
170 1d9652cf Scott Ullrich
		if($g['booting']) echo ".";
171 5b237745 Scott Ullrich
		if (!$cfgdevice) {
172 1a23410c Scott Ullrich
			$last_backup = discover_last_backup();
173
			if($last_backup) {
174
				log_error("No config.xml found, attempting last known config restore.");
175 5b95638a Scott Ullrich
				file_notice("config.xml", "No config.xml found, attempting last known config restore.", "pfSenseConfigurator", "");
176 1a23410c Scott Ullrich
				restore_backup("/cf/conf/backup/{$last_backup}");
177
			} else {
178 791bcfd4 Bill Marquette
				log_error("No config.xml or config backups found, resetting to factory defaults.");
179 c755c016 Bill Marquette
				restore_backup('/conf.default/config.xml');
180 1a23410c Scott Ullrich
			}
181 5b237745 Scott Ullrich
		}
182
183
		/* write device name to a file for rc.firmware */
184
		$fd = fopen("{$g['varetc_path']}/cfdevice", "w");
185
		fwrite($fd, $cfgdevice . "\n");
186
		fclose($fd);
187
188
		/* write out an fstab */
189
		$fd = fopen("{$g['etc_path']}/fstab", "w");
190
191
		$fstab = "/dev/{$cfgpartition} {$g['cf_path']} {$cfgfstype} ro 1 1\n";
192
		$fstab .= "proc /proc procfs rw 0 0\n";
193
194
		fwrite($fd, $fstab);
195
		fclose($fd);
196
	}
197 87294955 Scott Ullrich
	if($g['booting']) echo ".";
198 5b237745 Scott Ullrich
	/* mount all filesystems */
199
	mwexec("/sbin/mount -a");
200
}
201
202 6ec72f51 Scott Ullrich
203 09b949e1 Scott Ullrich
204 f1d634bb Scott Ullrich
if($g['booting']) echo ".";
205
$config = parse_config();
206
207 135ef84a Scott Ullrich
if($config_parsed == true) {
208
	/* process packager manager custom rules */
209
	if(is_dir("/usr/local/pkg/parse_config")) {
210
		run_plugins("/usr/local/pkg/parse_config/");
211
	}
212
}
213
214 ab94ba00 Ermal Lu?i
?>