Project

General

Profile

Download (7.41 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
if (!function_exists('platform_booting')) {
42
	require_once('globals.inc');
43
}
44

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

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

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

    
63
/* include globals from notices.inc /utility/XML parser files */
64
require_once("notices.inc");
65
require_once("util.inc");
66
require_once("IPv6.inc");
67
require_once('config.lib.inc');
68
if (file_exists("/cf/conf/use_xmlreader")) {
69
	require_once("xmlreader.inc");
70
} else {
71
	require_once("xmlparse.inc");
72
}
73
require_once("crypt.inc");
74

    
75
/* read platform */
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 (platform_booting(true)) {
90
	echo ".";
91
	if (file_exists("/cf/conf/config.xml")) {
92
		$config_contents = file_get_contents("/cf/conf/config.xml");
93
		if (stristr($config_contents, "<m0n0wall>") == true) {
94
			echo ".";
95
			/* user has just upgraded from m0n0wall, replace root xml tags */
96
			log_error(gettext("Upgrading m0n0wall configuration to pfSense... "));
97
			$config_contents = str_replace("m0n0wall", "pfsense", $config_contents);
98
			if (!config_validate("{$g['conf_path']}/config.xml")) {
99
				log_error(gettext("ERROR!  Could not convert m0n0wall -> pfsense in config.xml"));
100
			}
101
			conf_mount_rw();
102
			file_put_contents("/cf/conf/config.xml", $config_contents);
103
			conf_mount_ro();
104
		}
105
		unset($config_contents);
106
	}
107
	/* if our config file exists bail out, we're already set. */
108
	else if (!file_exists($g['cf_conf_path'] . "/config.xml")) {
109
		echo ".";
110
		/* find the device where config.xml resides and write out an fstab */
111
		unset($cfgdevice);
112
		echo ".";
113
		/* check if there's already an fstab (NFS booting?) */
114
		if (!file_exists("{$g['etc_path']}/fstab")) {
115
			echo ".";
116
			if (strstr($g['platform'], "cdrom")) {
117
				/* config is on floppy disk for CD-ROM version */
118
				$cfgdevice = $cfgpartition = "fd0";
119
				$_gb = exec('/sbin/dmesg -a', $dmesg);
120
				if (preg_match("/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
				unset($dmesg);
128
				$cfgfstype = "msdosfs";
129
				echo gettext("CDROM build") . "\n";
130
				echo "   " . gettext("CFG:") . " {$cfgpartition}\n";
131
				echo "   " . gettext("CFG:") . " {$cfgpartition}\n";
132
				echo "  " . gettext("TYPE:") . " {$cfgfstype}\n";
133
			} else {
134
				echo ".";
135
				/* probe kernel known disks until we find one with config.xml */
136
				$disks = explode(" ", get_single_sysctl("kern.disks"));
137
				foreach ($disks as $mountdisk) {
138
					/* skip mfs mounted filesystems */
139
					if (strstr($mountdisk, "md")) {
140
						continue;
141
					}
142
					if (mwexec("/sbin/mount -r /dev/{$mountdisk}a {$g['cf_path']}") == 0) {
143
						if (file_exists("{$g['cf_conf_path']}/config.xml")) {
144
							/* found it */
145
							$cfgdevice = $mountdisk;
146
							$cfgpartition = $cfgdevice . "a";
147
							$cfgfstype = "ufs";
148
							printf(gettext("Found configuration on %s.%s"), $cfgdevice, "\n");
149
						}
150

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

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

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

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

    
190
			/* write out an fstab */
191

    
192
			$fstab = "/dev/{$cfgpartition} {$g['cf_path']} {$cfgfstype} ro,noatime 1 1\n";
193
			$fstab .= "proc /proc procfs rw 0 0\n";
194
			file_put_contents("{$g['etc_path']}/fstab", $fstab);
195
		}
196
		echo ".";
197
		/* mount all filesystems */
198
		mwexec("/sbin/mount -a");
199
	}
200
	echo ".";
201
}
202

    
203
$config = parse_config();
204

    
205
/* set timezone */
206
if (isset($config['system']['timezone']) &&
207
    !empty($config['system']['timezone'])) {
208
	$timezone = $config['system']['timezone'];
209
} elseif (isset($g['default_timezone']) && !empty($g['default_timezone'])) {
210
	$timezone = $g['default_timezone'];
211
} else {
212
	$timezone = "Etc/UTC";
213
}
214
date_default_timezone_set("$timezone");
215

    
216
/* Set the default interface language */
217
if ($config['system']['language'] <> "") {
218
	$g['language'] = $config['system']['language'];
219
} elseif ($g['language'] == "") {
220
	$g['language'] = 'en_US';
221
}
222

    
223
if (!function_exists('set_language')) {
224
	require_once("pfsense-utils.inc");
225
}
226
set_language($g['language']);
227

    
228
if ($config_parsed == true) {
229
	/* process packager manager custom rules */
230
	if (is_dir("/usr/local/pkg/parse_config")) {
231
		run_plugins("/usr/local/pkg/parse_config/");
232
	}
233
}
234

    
235
?>
(11-11/65)