Project

General

Profile

Download (6.84 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
if(file_exists("/cf/conf/use_xmlreader"))
70
	require_once("xmlreader.inc");
71
else
72
	require_once("xmlparse.inc");
73
if($g['booting']) echo ".";
74
require_once("crypt.inc");
75

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

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

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

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

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

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

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

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

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

    
190
		/* write out an fstab */
191
		$fd = fopen("{$g['etc_path']}/fstab", "w");
192

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

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

    
204

    
205

    
206
if($g['booting']) echo ".";
207
$config = parse_config();
208

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

    
216
?>
(10-10/51)