Project

General

Profile

Download (6.82 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 (is_file_included("/etc/inc/config.inc"))
53
	return;
54

    
55
// 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
/* include globals from notices.inc /utility/XML parser files */
61
require_once('config.lib.inc');
62
if($g['booting']) echo ".";
63
require_once("notices.inc");
64
if($g['booting']) echo ".";
65
require_once("util.inc");
66
if($g['booting']) echo ".";
67
if(file_exists("/cf/conf/use_xmlreader"))
68
	require_once("xmlreader.inc");
69
else
70
	require_once("xmlparse.inc");
71
if($g['booting']) echo ".";
72
require_once("crypt.inc");
73

    
74
/* read platform */
75
if($g['booting']) echo ".";
76
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
/* if /debugging exists, lets set $debugging
83
   so we can output more information */
84
if(file_exists("/debugging")) {
85
	$debugging = true;
86
	$g['debug'] = true;
87
}
88

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

    
107
/* 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
	if($g['booting']) echo ".";
110
	/* find the device where config.xml resides and write out an fstab */
111
	unset($cfgdevice);
112
	if($g['booting']) echo ".";
113
	/* check if there's already an fstab (NFS booting?) */
114
	if (!file_exists("{$g['etc_path']}/fstab")) {
115
		if($g['booting']) echo ".";
116
		if (strstr($g['platform'], "cdrom")) {
117
			/* config is on floppy disk for CD-ROM version */
118
			$cfgdevice = $cfgpartition = "fd0";
119
			$dmesg = `dmesg -a`;
120
			if(ereg("da0", $dmesg) == true) {
121
				$cfgdevice = $cfgpartition = "da0" ;
122
				if (mwexec("/sbin/mount -r /dev/{$cfgdevice} /cf")) {
123
					/* could not mount, fallback to floppy */
124
					$cfgdevice = $cfgpartition = "fd0";
125
				}
126
			}
127
			$cfgfstype = "msdosfs";
128
			echo "CDROM build\n";
129
			echo "   CFG: {$cfgpartition}\n";
130
			echo "  TYPE: {$cfgfstype}\n";
131
		} else {
132
			if($g['booting']) echo ".";
133
			/* 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
					if ($cfgdevice)
151
						break;
152
				}
153
				if (mwexec("/sbin/mount -r /dev/{$mountdisk}d {$g['cf_path']}") == 0) {
154
					if($g['booting']) echo ".";
155
					if (file_exists("{$g['cf_conf_path']}/config.xml")) {
156
						/* found it */
157
						$cfgdevice = $mountdisk;
158
						$cfgpartition = $cfgdevice . "d";
159
						$cfgfstype = "ufs";
160
						echo "Found configuration on $cfgdevice.\n";
161
					}
162

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

    
165
					if ($cfgdevice)
166
						break;
167
				}
168
			}
169
		}
170
		if($g['booting']) echo ".";
171
		if (!$cfgdevice) {
172
			$last_backup = discover_last_backup();
173
			if($last_backup) {
174
				log_error("No config.xml found, attempting last known config restore.");
175
				file_notice("config.xml", "No config.xml found, attempting last known config restore.", "pfSenseConfigurator", "");
176
				restore_backup("/cf/conf/backup/{$last_backup}");
177
			} else {
178
				log_error("No config.xml or config backups found, resetting to factory defaults.");
179
				restore_backup('/conf.default/config.xml');
180
			}
181
		}
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
	if($g['booting']) echo ".";
198
	/* mount all filesystems */
199
	mwexec("/sbin/mount -a");
200
}
201

    
202

    
203

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

    
207
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
?>
(10-10/50)