1
|
<?php
|
2
|
/*
|
3
|
external config loader
|
4
|
Copyright (C) 2010 Scott Ullrich
|
5
|
All rights reserved.
|
6
|
|
7
|
Redistribution and use in source and binary forms, with or without
|
8
|
modification, are permitted provided that the following conditions are met:
|
9
|
|
10
|
1. Redistributions of source code must retain the above copyright notice,
|
11
|
this list of conditions and the following disclaimer.
|
12
|
|
13
|
2. Redistributions in binary form must reproduce the above copyright
|
14
|
notice, this list of conditions and the following disclaimer in the
|
15
|
documentation and/or other materials provided with the distribution.
|
16
|
|
17
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
18
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
19
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
20
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
21
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
22
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
23
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
24
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
25
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
26
|
POSSIBILITY OF SUCH DAMAGE.
|
27
|
|
28
|
Currently supported file system types: MS-Dos, FreeBSD UFS
|
29
|
|
30
|
*/
|
31
|
|
32
|
require_once("globals.inc");
|
33
|
require_once("functions.inc");
|
34
|
require_once("config.lib.inc");
|
35
|
require_once("config.inc");
|
36
|
|
37
|
$debug = false;
|
38
|
|
39
|
function get_boot_disk() {
|
40
|
global $g, $debug;
|
41
|
$disk = exec("/sbin/mount | /usr/bin/grep \"on / \" | /usr/bin/cut -d'/' -f3 | /usr/bin/cut -d' ' -f1");
|
42
|
return $disk;
|
43
|
}
|
44
|
|
45
|
function get_swap_disks() {
|
46
|
exec("/usr/sbin/swapinfo | /usr/bin/sed '/^\/dev/!d; s,^/dev/,,; s, .*\$,,'", $disks);
|
47
|
return $disks;
|
48
|
}
|
49
|
|
50
|
function get_disk_slices($disk) {
|
51
|
global $g, $debug;
|
52
|
$slices_array = array();
|
53
|
$slices = trim(exec("/bin/ls " . escapeshellarg("/dev/" . $disk . "s*") . " 2>/dev/null"));
|
54
|
$slices = str_replace("/dev/", "", $slices);
|
55
|
if($slices == "ls: No match.")
|
56
|
return;
|
57
|
$slices_array = explode(" ", $slices);
|
58
|
return $slices_array;
|
59
|
}
|
60
|
|
61
|
function get_disks() {
|
62
|
global $g, $debug;
|
63
|
$disks_array = array();
|
64
|
$disks = exec("/sbin/sysctl -n kern.disks");
|
65
|
$disks_s = explode(" ", $disks);
|
66
|
foreach($disks_s as $disk)
|
67
|
if(trim($disk))
|
68
|
$disks_array[] = $disk;
|
69
|
return $disks_array;
|
70
|
}
|
71
|
|
72
|
function discover_config($mountpoint) {
|
73
|
global $g, $debug;
|
74
|
$locations_to_check = array("/", "/config");
|
75
|
foreach($locations_to_check as $ltc) {
|
76
|
$tocheck = "/tmp/mnt/cf{$ltc}config.xml";
|
77
|
if($debug) {
|
78
|
echo "\nChecking for $tocheck";
|
79
|
if(file_exists($tocheck))
|
80
|
echo " -> found!";
|
81
|
}
|
82
|
if(file_exists($tocheck))
|
83
|
return $tocheck;
|
84
|
}
|
85
|
return "";
|
86
|
}
|
87
|
|
88
|
function test_config($file_location) {
|
89
|
global $g, $debug;
|
90
|
if(!$file_location)
|
91
|
return;
|
92
|
// config.xml was found. ensure it is sound.
|
93
|
$root_obj = trim("<{$g['xml_rootobj']}>");
|
94
|
$xml_file_head = exec("/usr/bin/head -2 " . escapeshellarg($file_location) . " | /usr/bin/tail -n1");
|
95
|
if($debug) {
|
96
|
echo "\nroot obj = $root_obj";
|
97
|
echo "\nfile head = $xml_file_head";
|
98
|
}
|
99
|
if($xml_file_head == $root_obj) {
|
100
|
// Now parse config to make sure
|
101
|
$config_status = config_validate($file_location);
|
102
|
if($config_status)
|
103
|
return true;
|
104
|
}
|
105
|
return false;
|
106
|
}
|
107
|
|
108
|
// Probes all disks looking for config.xml
|
109
|
function find_config_xml() {
|
110
|
global $g, $debug;
|
111
|
$disks = get_disks();
|
112
|
// Safety check.
|
113
|
if(!is_array($disks))
|
114
|
return;
|
115
|
$boot_disk = get_boot_disk();
|
116
|
$swap_disks = get_swap_disks();
|
117
|
exec("/bin/mkdir -p /tmp/mnt/cf");
|
118
|
foreach($disks as $disk) {
|
119
|
$slices = get_disk_slices($disk);
|
120
|
if(is_array($slices)) {
|
121
|
foreach($slices as $slice) {
|
122
|
if($slice == "")
|
123
|
continue;
|
124
|
if(stristr($slice, $boot_disk)) {
|
125
|
if($debug)
|
126
|
echo "\nSkipping boot device slice $slice";
|
127
|
continue;
|
128
|
}
|
129
|
if(in_array($slice, $swap_disks)) {
|
130
|
if($debug)
|
131
|
echo "\nSkipping swap device slice $slice";
|
132
|
continue;
|
133
|
}
|
134
|
echo " $slice";
|
135
|
// First try msdos fs
|
136
|
if($debug)
|
137
|
echo "\n/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
|
138
|
$result = exec("/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
|
139
|
// Next try regular fs (ufs)
|
140
|
if(!$result) {
|
141
|
if($debug)
|
142
|
echo "\n/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
|
143
|
$result = exec("/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
|
144
|
}
|
145
|
$mounted = trim(exec("/sbin/mount | /usr/bin/grep -v grep | /usr/bin/grep '/tmp/mnt/cf' | /usr/bin/wc -l"));
|
146
|
if($debug)
|
147
|
echo "\nmounted: $mounted ";
|
148
|
if(intval($mounted) > 0) {
|
149
|
// Item was mounted - look for config.xml file
|
150
|
$config_location = discover_config($slice);
|
151
|
if($config_location) {
|
152
|
if(test_config($config_location)) {
|
153
|
// We have a valid configuration. Install it.
|
154
|
echo " -> found config.xml\n";
|
155
|
echo "Backing up old configuration...\n";
|
156
|
backup_config();
|
157
|
echo "Restoring [{$slice}] {$config_location}...\n";
|
158
|
restore_backup($config_location);
|
159
|
echo "Cleaning up...\n";
|
160
|
exec("/sbin/umount /tmp/mnt/cf");
|
161
|
exit;
|
162
|
}
|
163
|
}
|
164
|
exec("/sbin/umount /tmp/mnt/cf");
|
165
|
}
|
166
|
}
|
167
|
}
|
168
|
}
|
169
|
}
|
170
|
|
171
|
echo "External config loader 1.0 is now starting...";
|
172
|
find_config_xml();
|
173
|
echo "\n";
|
174
|
|
175
|
?>
|