| 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 |
5ba5a8de
|
Scott Ullrich
|
pfSense_BUILDER_BINARIES: /sbin/mount /sbin/sysctl /sbin/umount /sbin/halt /sbin/fsck
|
| 42 |
523855b0
|
Scott Ullrich
|
pfSense_MODULE: config
|
| 43 |
5b237745
|
Scott Ullrich
|
*/
|
| 44 |
|
|
|
| 45 |
1d9652cf
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 46 |
|
|
|
| 47 |
96447b25
|
Scott Ullrich
|
/* do not load this file twice. */
|
| 48 |
cba19251
|
Chris Buechler
|
//if (in_array("/etc/inc/config.inc", get_included_files()))
|
| 49 |
|
|
// return;
|
| 50 |
96447b25
|
Scott Ullrich
|
|
| 51 |
31c3942c
|
Scott Ullrich
|
// Set the memory limit to 128M on i386. When someone has something like 500+ tunnels
|
| 52 |
8ecc250a
|
Scott Ullrich
|
// 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 |
31c3942c
|
Scott Ullrich
|
if(!$ARCH)
|
| 55 |
7a755156
|
Ermal
|
$ARCH = php_uname("m");
|
| 56 |
31c3942c
|
Scott Ullrich
|
|
| 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 |
8ecc250a
|
Scott Ullrich
|
|
| 63 |
bb1546f6
|
Ermal Lu?i
|
/* include globals from notices.inc /utility/XML parser files */
|
| 64 |
|
|
require_once("notices.inc");
|
| 65 |
1d9652cf
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 66 |
5b237745
|
Scott Ullrich
|
require_once("util.inc");
|
| 67 |
1d9652cf
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 68 |
22b5abac
|
Seth Mos
|
require_once("IPv6.inc");
|
| 69 |
|
|
if($g['booting']) echo ".";
|
| 70 |
730b6148
|
smos
|
require_once('config.lib.inc');
|
| 71 |
|
|
if($g['booting']) echo ".";
|
| 72 |
093bcebc
|
Scott Ullrich
|
if(file_exists("/cf/conf/use_xmlreader"))
|
| 73 |
|
|
require_once("xmlreader.inc");
|
| 74 |
|
|
else
|
| 75 |
|
|
require_once("xmlparse.inc");
|
| 76 |
67fc82b5
|
Ermal Lu?i
|
if($g['booting']) echo ".";
|
| 77 |
|
|
require_once("crypt.inc");
|
| 78 |
5b237745
|
Scott Ullrich
|
|
| 79 |
|
|
/* read platform */
|
| 80 |
1d9652cf
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 81 |
5b237745
|
Scott Ullrich
|
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 |
727a2b91
|
Scott Ullrich
|
/* if /debugging exists, lets set $debugging
|
| 88 |
|
|
so we can output more information */
|
| 89 |
1324a93a
|
Scott Ullrich
|
if(file_exists("/debugging")) {
|
| 90 |
727a2b91
|
Scott Ullrich
|
$debugging = true;
|
| 91 |
1324a93a
|
Scott Ullrich
|
$g['debug'] = true;
|
| 92 |
|
|
}
|
| 93 |
727a2b91
|
Scott Ullrich
|
|
| 94 |
1d9652cf
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 95 |
6280b10e
|
Ermal Lu?i
|
if($g['booting'] && file_exists("/cf/conf/config.xml")) {
|
| 96 |
49ed889c
|
Scott Ullrich
|
$config_contents = file_get_contents("/cf/conf/config.xml");
|
| 97 |
0394c816
|
Scott Ullrich
|
if(stristr($config_contents, "<m0n0wall>") == true) {
|
| 98 |
87294955
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 99 |
49ed889c
|
Scott Ullrich
|
/* user has just upgraded to m0n0wall, replace root xml tags */
|
| 100 |
10e91486
|
Carlos Eduardo Ramos
|
log_error(gettext("Upgrading m0n0wall configuration to pfSense... "));
|
| 101 |
d9413241
|
Scott Ullrich
|
$config_contents = str_replace("m0n0wall","pfsense", $config_contents);
|
| 102 |
d2b70684
|
Scott Ullrich
|
if (!config_validate("{$g['conf_path']}/config.xml"))
|
| 103 |
10e91486
|
Carlos Eduardo Ramos
|
log_error(gettext("ERROR! Could not convert m0n0wall -> pfsense in config.xml"));
|
| 104 |
9bbf0b4e
|
Scott Ullrich
|
conf_mount_rw();
|
| 105 |
49ed889c
|
Scott Ullrich
|
$fd = fopen("/cf/conf/config.xml", "w");
|
| 106 |
|
|
fwrite($fd, $config_contents);
|
| 107 |
|
|
fclose($fd);
|
| 108 |
37db27bb
|
Scott Ullrich
|
conf_mount_ro();
|
| 109 |
49ed889c
|
Scott Ullrich
|
}
|
| 110 |
e9208daf
|
Scott Ullrich
|
}
|
| 111 |
727a2b91
|
Scott Ullrich
|
|
| 112 |
6e8c1d1c
|
Scott Ullrich
|
/* if our config file exists bail out, we're already set. */
|
| 113 |
6280b10e
|
Ermal Lu?i
|
else if ($g['booting'] and !file_exists($g['cf_conf_path'] . "/config.xml") ) {
|
| 114 |
1d9652cf
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 115 |
5b237745
|
Scott Ullrich
|
/* find the device where config.xml resides and write out an fstab */
|
| 116 |
|
|
unset($cfgdevice);
|
| 117 |
87294955
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 118 |
5b237745
|
Scott Ullrich
|
/* check if there's already an fstab (NFS booting?) */
|
| 119 |
|
|
if (!file_exists("{$g['etc_path']}/fstab")) {
|
| 120 |
87294955
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 121 |
5b237745
|
Scott Ullrich
|
if (strstr($g['platform'], "cdrom")) {
|
| 122 |
|
|
/* config is on floppy disk for CD-ROM version */
|
| 123 |
|
|
$cfgdevice = $cfgpartition = "fd0";
|
| 124 |
e76de94e
|
Scott Ullrich
|
$dmesg = `dmesg -a`;
|
| 125 |
52108aca
|
ccesario
|
if(preg_match("/da0/", $dmesg) == true) {
|
| 126 |
e76de94e
|
Scott Ullrich
|
$cfgdevice = $cfgpartition = "da0" ;
|
| 127 |
da9de371
|
Scott Ullrich
|
if (mwexec("/sbin/mount -r /dev/{$cfgdevice} /cf")) {
|
| 128 |
e76de94e
|
Scott Ullrich
|
/* could not mount, fallback to floppy */
|
| 129 |
|
|
$cfgdevice = $cfgpartition = "fd0";
|
| 130 |
|
|
}
|
| 131 |
|
|
}
|
| 132 |
a7592ba0
|
Scott Ullrich
|
$cfgfstype = "msdosfs";
|
| 133 |
10e91486
|
Carlos Eduardo Ramos
|
echo gettext("CDROM build") . "\n";
|
| 134 |
|
|
echo " " . gettext("CFG:") . " {$cfgpartition}\n";
|
| 135 |
|
|
echo " " . gettext("CFG:") . " {$cfgpartition}\n";
|
| 136 |
|
|
echo " " . gettext("TYPE:") . " {$cfgfstype}\n";
|
| 137 |
5b237745
|
Scott Ullrich
|
} else {
|
| 138 |
1d9652cf
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 139 |
5b237745
|
Scott Ullrich
|
/* probe kernel known disks until we find one with config.xml */
|
| 140 |
|
|
$disks = explode(" ", trim(preg_replace("/kern.disks: /", "", exec("/sbin/sysctl kern.disks"))));
|
| 141 |
|
|
foreach ($disks as $mountdisk) {
|
| 142 |
|
|
/* skip mfs mounted filesystems */
|
| 143 |
|
|
if (strstr($mountdisk, "md"))
|
| 144 |
|
|
continue;
|
| 145 |
|
|
if (mwexec("/sbin/mount -r /dev/{$mountdisk}a {$g['cf_path']}") == 0) {
|
| 146 |
|
|
if (file_exists("{$g['cf_conf_path']}/config.xml")) {
|
| 147 |
|
|
/* found it */
|
| 148 |
|
|
$cfgdevice = $mountdisk;
|
| 149 |
|
|
$cfgpartition = $cfgdevice . "a";
|
| 150 |
|
|
$cfgfstype = "ufs";
|
| 151 |
10e91486
|
Carlos Eduardo Ramos
|
printf(gettext("Found configuration on %s.%s"), $cfgdevice, "\n");
|
| 152 |
5b237745
|
Scott Ullrich
|
}
|
| 153 |
|
|
|
| 154 |
|
|
mwexec("/sbin/umount -f {$g['cf_path']}");
|
| 155 |
|
|
|
| 156 |
655a80eb
|
Scott Ullrich
|
if ($cfgdevice)
|
| 157 |
|
|
break;
|
| 158 |
|
|
}
|
| 159 |
|
|
if (mwexec("/sbin/mount -r /dev/{$mountdisk}d {$g['cf_path']}") == 0) {
|
| 160 |
87294955
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 161 |
655a80eb
|
Scott Ullrich
|
if (file_exists("{$g['cf_conf_path']}/config.xml")) {
|
| 162 |
|
|
/* found it */
|
| 163 |
|
|
$cfgdevice = $mountdisk;
|
| 164 |
4de945fa
|
Scott Ullrich
|
$cfgpartition = $cfgdevice . "d";
|
| 165 |
655a80eb
|
Scott Ullrich
|
$cfgfstype = "ufs";
|
| 166 |
10e91486
|
Carlos Eduardo Ramos
|
printf(gettext("Found configuration on %s.%s"), $cfgdevice, "\n");
|
| 167 |
655a80eb
|
Scott Ullrich
|
}
|
| 168 |
|
|
|
| 169 |
|
|
mwexec("/sbin/umount -f {$g['cf_path']}");
|
| 170 |
|
|
|
| 171 |
5b237745
|
Scott Ullrich
|
if ($cfgdevice)
|
| 172 |
|
|
break;
|
| 173 |
|
|
}
|
| 174 |
|
|
}
|
| 175 |
|
|
}
|
| 176 |
1d9652cf
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 177 |
5b237745
|
Scott Ullrich
|
if (!$cfgdevice) {
|
| 178 |
1a23410c
|
Scott Ullrich
|
$last_backup = discover_last_backup();
|
| 179 |
|
|
if($last_backup) {
|
| 180 |
10e91486
|
Carlos Eduardo Ramos
|
log_error(gettext("No config.xml found, attempting last known config restore."));
|
| 181 |
|
|
file_notice("config.xml", gettext("No config.xml found, attempting last known config restore."), "pfSenseConfigurator", "");
|
| 182 |
1a23410c
|
Scott Ullrich
|
restore_backup("/cf/conf/backup/{$last_backup}");
|
| 183 |
|
|
} else {
|
| 184 |
10e91486
|
Carlos Eduardo Ramos
|
log_error(gettext("No config.xml or config backups found, resetting to factory defaults."));
|
| 185 |
c755c016
|
Bill Marquette
|
restore_backup('/conf.default/config.xml');
|
| 186 |
1a23410c
|
Scott Ullrich
|
}
|
| 187 |
5b237745
|
Scott Ullrich
|
}
|
| 188 |
|
|
|
| 189 |
|
|
/* write device name to a file for rc.firmware */
|
| 190 |
|
|
$fd = fopen("{$g['varetc_path']}/cfdevice", "w");
|
| 191 |
|
|
fwrite($fd, $cfgdevice . "\n");
|
| 192 |
|
|
fclose($fd);
|
| 193 |
|
|
|
| 194 |
|
|
/* write out an fstab */
|
| 195 |
|
|
$fd = fopen("{$g['etc_path']}/fstab", "w");
|
| 196 |
|
|
|
| 197 |
f2033fc7
|
Ermal
|
$fstab = "/dev/{$cfgpartition} {$g['cf_path']} {$cfgfstype} ro,noatime 1 1\n";
|
| 198 |
5b237745
|
Scott Ullrich
|
$fstab .= "proc /proc procfs rw 0 0\n";
|
| 199 |
|
|
|
| 200 |
|
|
fwrite($fd, $fstab);
|
| 201 |
|
|
fclose($fd);
|
| 202 |
|
|
}
|
| 203 |
87294955
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 204 |
5b237745
|
Scott Ullrich
|
/* mount all filesystems */
|
| 205 |
|
|
mwexec("/sbin/mount -a");
|
| 206 |
|
|
}
|
| 207 |
|
|
|
| 208 |
f1d634bb
|
Scott Ullrich
|
if($g['booting']) echo ".";
|
| 209 |
|
|
$config = parse_config();
|
| 210 |
|
|
|
| 211 |
34d70cc0
|
Scott Ullrich
|
/* set timezone */
|
| 212 |
|
|
$timezone = $config['system']['timezone'];
|
| 213 |
|
|
if (!$timezone)
|
| 214 |
|
|
$timezone = "Etc/UTC";
|
| 215 |
|
|
date_default_timezone_set("$timezone");
|
| 216 |
|
|
|
| 217 |
135ef84a
|
Scott Ullrich
|
if($config_parsed == true) {
|
| 218 |
|
|
/* process packager manager custom rules */
|
| 219 |
|
|
if(is_dir("/usr/local/pkg/parse_config")) {
|
| 220 |
|
|
run_plugins("/usr/local/pkg/parse_config/");
|
| 221 |
|
|
}
|
| 222 |
|
|
}
|
| 223 |
|
|
|
| 224 |
730b6148
|
smos
|
?>
|