Project

General

Profile

Download (7.11 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-2010 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
42
	pfSense_MODULE:	config
43
*/
44

    
45
if($g['booting']) echo ".";
46

    
47
/* do not load this file twice. */
48
//if (in_array("/etc/inc/config.inc", get_included_files()))
49
//	return;
50

    
51
// Set the memory limit to 128M on i386.  When someone has something like 500+ tunnels
52
// the parser needs quite a bit of ram.   Do not remove this line unless you 
53
// know what you are doing.  If in doubt, check with dev@ _/FIRST/_!
54
if(!$ARCH) 
55
	$ARCH = php_uname("m");
56

    
57
// Set memory limit to 256M on amd64.
58
if($ARCH == "amd64") 
59
	ini_set("memory_limit","256M");
60
else
61
	ini_set("memory_limit","128M");
62

    
63
/* include globals from notices.inc /utility/XML parser files */
64
require_once("notices.inc");
65
if($g['booting']) echo ".";
66
require_once('config.lib.inc');
67
if($g['booting']) echo ".";
68
require_once("util.inc");
69
if($g['booting']) echo ".";
70
require_once("IPv6.inc");
71
if($g['booting']) echo ".";
72
if(file_exists("/cf/conf/use_xmlreader"))
73
	require_once("xmlreader.inc");
74
else
75
	require_once("xmlparse.inc");
76
if($g['booting']) echo ".";
77
require_once("crypt.inc");
78

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

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

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

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

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

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

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

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

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

    
193
		/* write out an fstab */
194
		$fd = fopen("{$g['etc_path']}/fstab", "w");
195

    
196
		$fstab = "/dev/{$cfgpartition} {$g['cf_path']} {$cfgfstype} ro,noatime 1 1\n";
197
		$fstab .= "proc /proc procfs rw 0 0\n";
198

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

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

    
210
/* set timezone */
211
$timezone = $config['system']['timezone'];
212
if (!$timezone)
213
	$timezone = "Etc/UTC";
214
date_default_timezone_set("$timezone");
215

    
216
if($config_parsed == true) {
217
	/* process packager manager custom rules */
218
	if(is_dir("/usr/local/pkg/parse_config")) {
219
		run_plugins("/usr/local/pkg/parse_config/");
220
	}
221
}
222

    
223
?>
(12-12/61)