Project

General

Profile

Download (7.14 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 f5da67d0 Bill Marquette
/****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 5b237745 Scott Ullrich
	config.inc
12 032c40c7 Scott Ullrich
	Copyright (C) 2004-2010 Scott Ullrich
13 cfc707f7 Scott Ullrich
	All rights reserved.
14 5b237745 Scott Ullrich
15 cfc707f7 Scott Ullrich
	originally part of m0n0wall (http://m0n0.ch/wall)
16 5b237745 Scott Ullrich
	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 523855b0 Scott Ullrich
40
41 971de1f9 Renato Botelho
	pfSense_BUILDER_BINARIES:	/sbin/mount	/sbin/umount	/sbin/halt	/sbin/fsck
42 523855b0 Scott Ullrich
	pfSense_MODULE:	config
43 5b237745 Scott Ullrich
*/
44
45 1e0b1727 Phil Davis
if (!function_exists('platform_booting')) {
46 ca301b52 Ermal LUÇI
	require_once('globals.inc');
47 1e0b1727 Phil Davis
}
48 ca301b52 Ermal LUÇI
49 96447b25 Scott Ullrich
/* do not load this file twice. */
50 cba19251 Chris Buechler
//if (in_array("/etc/inc/config.inc", get_included_files()))
51
//	return;
52 96447b25 Scott Ullrich
53 31c3942c Scott Ullrich
// Set the memory limit to 128M on i386.  When someone has something like 500+ tunnels
54 5acce704 Renato Botelho
// the parser needs quite a bit of ram.   Do not remove this line unless you
55 8ecc250a Scott Ullrich
// know what you are doing.  If in doubt, check with dev@ _/FIRST/_!
56 1e0b1727 Phil Davis
if (!$ARCH) {
57 7a755156 Ermal
	$ARCH = php_uname("m");
58 1e0b1727 Phil Davis
}
59 31c3942c Scott Ullrich
60
// Set memory limit to 256M on amd64.
61 1e0b1727 Phil Davis
if ($ARCH == "amd64") {
62 31c3942c Scott Ullrich
	ini_set("memory_limit","256M");
63 1e0b1727 Phil Davis
} else {
64 31c3942c Scott Ullrich
	ini_set("memory_limit","128M");
65 1e0b1727 Phil Davis
}
66 8ecc250a Scott Ullrich
67 bb1546f6 Ermal Lu?i
/* include globals from notices.inc /utility/XML parser files */
68
require_once("notices.inc");
69 5b237745 Scott Ullrich
require_once("util.inc");
70 22b5abac Seth Mos
require_once("IPv6.inc");
71 730b6148 smos
require_once('config.lib.inc');
72 1e0b1727 Phil Davis
if (file_exists("/cf/conf/use_xmlreader")) {
73 093bcebc Scott Ullrich
	require_once("xmlreader.inc");
74 1e0b1727 Phil Davis
} else {
75 093bcebc Scott Ullrich
	require_once("xmlparse.inc");
76 1e0b1727 Phil Davis
}
77 67fc82b5 Ermal Lu?i
require_once("crypt.inc");
78 5b237745 Scott Ullrich
79
/* read platform */
80
if (file_exists("{$g['etc_path']}/platform")) {
81
	$g['platform'] = chop(file_get_contents("{$g['etc_path']}/platform"));
82
} else {
83
	$g['platform'] = "unknown";
84
}
85
86 727a2b91 Scott Ullrich
/* if /debugging exists, lets set $debugging
87
   so we can output more information */
88 1e0b1727 Phil Davis
if (file_exists("/debugging")) {
89 727a2b91 Scott Ullrich
	$debugging = true;
90 1324a93a Scott Ullrich
	$g['debug'] = true;
91
}
92 727a2b91 Scott Ullrich
93 1e0b1727 Phil Davis
if (platform_booting(true)) {
94 6878d797 Ermal LUÇI
	echo ".";
95 1e0b1727 Phil Davis
	if (file_exists("/cf/conf/config.xml")) {
96 6878d797 Ermal LUÇI
		$config_contents = file_get_contents("/cf/conf/config.xml");
97 1e0b1727 Phil Davis
		if (stristr($config_contents, "<m0n0wall>") == true) {
98 6878d797 Ermal LUÇI
			echo ".";
99 1e0b1727 Phil Davis
			/* user has just upgraded from m0n0wall, replace root xml tags */
100 6878d797 Ermal LUÇI
			log_error(gettext("Upgrading m0n0wall configuration to pfSense... "));
101
			$config_contents = str_replace("m0n0wall","pfsense", $config_contents);
102 1e0b1727 Phil Davis
			if (!config_validate("{$g['conf_path']}/config.xml")) {
103 6878d797 Ermal LUÇI
				log_error(gettext("ERROR!  Could not convert m0n0wall -> pfsense in config.xml"));
104 1e0b1727 Phil Davis
			}
105 6878d797 Ermal LUÇI
			conf_mount_rw();
106
			file_put_contents("/cf/conf/config.xml", $config_contents);
107
			conf_mount_ro();
108
		}
109
		unset($config_contents);
110 49ed889c Scott Ullrich
	}
111 6878d797 Ermal LUÇI
	/* if our config file exists bail out, we're already set. */
112
	else if (!file_exists($g['cf_conf_path'] . "/config.xml")  ) {
113
		echo ".";
114
		/* find the device where config.xml resides and write out an fstab */
115
		unset($cfgdevice);
116
		echo ".";
117
		/* check if there's already an fstab (NFS booting?) */
118
		if (!file_exists("{$g['etc_path']}/fstab")) {
119
			echo ".";
120
			if (strstr($g['platform'], "cdrom")) {
121
				/* config is on floppy disk for CD-ROM version */
122
				$cfgdevice = $cfgpartition = "fd0";
123
				$_gb = exec('/sbin/dmesg -a', $dmesg);
124 1e0b1727 Phil Davis
				if (preg_match("/da0/", $dmesg) == true) {
125 6878d797 Ermal LUÇI
					$cfgdevice = $cfgpartition = "da0" ;
126
					if (mwexec("/sbin/mount -r /dev/{$cfgdevice} /cf")) {
127
						/* could not mount, fallback to floppy */
128
						$cfgdevice = $cfgpartition = "fd0";
129 5b237745 Scott Ullrich
					}
130 655a80eb Scott Ullrich
				}
131 6878d797 Ermal LUÇI
				unset($dmesg);
132
				$cfgfstype = "msdosfs";
133
				echo gettext("CDROM build") . "\n";
134
				echo "   " . gettext("CFG:") . " {$cfgpartition}\n";
135
				echo "   " . gettext("CFG:") . " {$cfgpartition}\n";
136
				echo "  " . gettext("TYPE:") . " {$cfgfstype}\n";
137
			} else {
138
				echo ".";
139
				/* probe kernel known disks until we find one with config.xml */
140
				$disks = explode(" ", get_single_sysctl("kern.disks"));
141
				foreach ($disks as $mountdisk) {
142
					/* skip mfs mounted filesystems */
143 1e0b1727 Phil Davis
					if (strstr($mountdisk, "md")) {
144 6878d797 Ermal LUÇI
						continue;
145 1e0b1727 Phil Davis
					}
146 6878d797 Ermal LUÇI
					if (mwexec("/sbin/mount -r /dev/{$mountdisk}a {$g['cf_path']}") == 0) {
147
						if (file_exists("{$g['cf_conf_path']}/config.xml")) {
148
							/* found it */
149
							$cfgdevice = $mountdisk;
150
							$cfgpartition = $cfgdevice . "a";
151
							$cfgfstype = "ufs";
152
							printf(gettext("Found configuration on %s.%s"), $cfgdevice, "\n");
153
						}
154
155
						mwexec("/sbin/umount -f {$g['cf_path']}");
156
157 1e0b1727 Phil Davis
						if ($cfgdevice) {
158 6878d797 Ermal LUÇI
							break;
159 1e0b1727 Phil Davis
						}
160 6878d797 Ermal LUÇI
					}
161
					if (mwexec("/sbin/mount -r /dev/{$mountdisk}d {$g['cf_path']}") == 0) {
162 1e0b1727 Phil Davis
						if (platform_booting()) {
163
							echo ".";
164
						}
165 6878d797 Ermal LUÇI
						if (file_exists("{$g['cf_conf_path']}/config.xml")) {
166
							/* found it */
167
							$cfgdevice = $mountdisk;
168
							$cfgpartition = $cfgdevice . "d";
169
							$cfgfstype = "ufs";
170
							printf(gettext("Found configuration on %s.%s"), $cfgdevice, "\n");
171
						}
172
173
						mwexec("/sbin/umount -f {$g['cf_path']}");
174
175 1e0b1727 Phil Davis
						if ($cfgdevice) {
176 6878d797 Ermal LUÇI
							break;
177 1e0b1727 Phil Davis
						}
178 655a80eb Scott Ullrich
					}
179 5b237745 Scott Ullrich
				}
180
			}
181 6878d797 Ermal LUÇI
			echo ".";
182
			if (!$cfgdevice) {
183
				$last_backup = discover_last_backup();
184 1e0b1727 Phil Davis
				if ($last_backup) {
185 6878d797 Ermal LUÇI
					log_error(gettext("No config.xml found, attempting last known config restore."));
186
					file_notice("config.xml", gettext("No config.xml found, attempting last known config restore."), "pfSenseConfigurator", "");
187
					restore_backup("/cf/conf/backup/{$last_backup}");
188
				} else {
189
					log_error(gettext("No config.xml or config backups found, resetting to factory defaults."));
190
					restore_backup('/conf.default/config.xml');
191
				}
192 1a23410c Scott Ullrich
			}
193 5b237745 Scott Ullrich
194 6878d797 Ermal LUÇI
			/* write device name to a file for rc.firmware */
195
			file_put_contents("{$g['varetc_path']}/cfdevice", $cfgdevice . "\n");
196 5b237745 Scott Ullrich
197 6878d797 Ermal LUÇI
			/* write out an fstab */
198 5b237745 Scott Ullrich
199 6878d797 Ermal LUÇI
			$fstab = "/dev/{$cfgpartition} {$g['cf_path']} {$cfgfstype} ro,noatime 1 1\n";
200
			$fstab .= "proc /proc procfs rw 0 0\n";
201
			file_put_contents("{$g['etc_path']}/fstab", $fstab);
202
		}
203
		echo ".";
204
		/* mount all filesystems */
205
		mwexec("/sbin/mount -a");
206 5b237745 Scott Ullrich
	}
207 6878d797 Ermal LUÇI
	echo ".";
208 5b237745 Scott Ullrich
}
209
210 f1d634bb Scott Ullrich
$config = parse_config();
211
212 34d70cc0 Scott Ullrich
/* set timezone */
213
$timezone = $config['system']['timezone'];
214 1e0b1727 Phil Davis
if (!$timezone) {
215 34d70cc0 Scott Ullrich
	$timezone = "Etc/UTC";
216 1e0b1727 Phil Davis
}
217 34d70cc0 Scott Ullrich
date_default_timezone_set("$timezone");
218
219 1e0b1727 Phil Davis
if ($config_parsed == true) {
220 135ef84a Scott Ullrich
	/* process packager manager custom rules */
221 1e0b1727 Phil Davis
	if (is_dir("/usr/local/pkg/parse_config")) {
222 135ef84a Scott Ullrich
		run_plugins("/usr/local/pkg/parse_config/");
223
	}
224
}
225
226 730b6148 smos
?>