Project

General

Profile

Download (6.98 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	/bin/sync
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.  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
ini_set("memory_limit","128M");
55

    
56
/* include globals from notices.inc /utility/XML parser files */
57
require_once("notices.inc");
58
if($g['booting']) echo ".";
59
require_once('config.lib.inc');
60
if($g['booting']) echo ".";
61
require_once("util.inc");
62
if($g['booting']) echo ".";
63
require_once("IPv6.inc");
64
if($g['booting']) echo ".";
65
if(file_exists("/cf/conf/use_xmlreader"))
66
	require_once("xmlreader.inc");
67
else
68
	require_once("xmlparse.inc");
69
if($g['booting']) echo ".";
70
require_once("crypt.inc");
71

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

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

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

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

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

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

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

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

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

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

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

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

    
200
if($g['booting']) echo ".";
201
$config = parse_config();
202

    
203
/* set timezone */
204
$timezone = $config['system']['timezone'];
205
if (!$timezone)
206
	$timezone = "Etc/UTC";
207
date_default_timezone_set("$timezone");
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
?>
(11-11/54)