1
|
<?php
|
2
|
/*
|
3
|
* ecl.php
|
4
|
*
|
5
|
* Copyright (c) 2010-2015 Electric Sheep Fencing, LLC. 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
|
15
|
* the documentation and/or other materials provided with the
|
16
|
* distribution.
|
17
|
*
|
18
|
* 3. All advertising materials mentioning features or use of this software
|
19
|
* must display the following acknowledgment:
|
20
|
* "This product includes software developed by the pfSense Project
|
21
|
* for use in the pfSense® software distribution. (http://www.pfsense.org/).
|
22
|
*
|
23
|
* 4. The names "pfSense" and "pfSense Project" must not be used to
|
24
|
* endorse or promote products derived from this software without
|
25
|
* prior written permission. For written permission, please contact
|
26
|
* coreteam@pfsense.org.
|
27
|
*
|
28
|
* 5. Products derived from this software may not be called "pfSense"
|
29
|
* nor may "pfSense" appear in their names without prior written
|
30
|
* permission of the Electric Sheep Fencing, LLC.
|
31
|
*
|
32
|
* 6. Redistributions of any form whatsoever must retain the following
|
33
|
* acknowledgment:
|
34
|
*
|
35
|
* "This product includes software developed by the pfSense Project
|
36
|
* for use in the pfSense software distribution (http://www.pfsense.org/).
|
37
|
*
|
38
|
* THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
|
39
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
40
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
41
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
|
42
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
43
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
44
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
45
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
46
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
47
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
48
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
49
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
50
|
*/
|
51
|
|
52
|
require_once("globals.inc");
|
53
|
require_once("functions.inc");
|
54
|
require_once("config.lib.inc");
|
55
|
require_once("config.inc");
|
56
|
|
57
|
$debug = false;
|
58
|
|
59
|
function get_boot_disk() {
|
60
|
global $g, $debug;
|
61
|
$disk = exec("/sbin/mount | /usr/bin/grep \"on / \" | /usr/bin/cut -d'/' -f3 | /usr/bin/cut -d' ' -f1");
|
62
|
return $disk;
|
63
|
}
|
64
|
|
65
|
function get_swap_disks() {
|
66
|
exec("/usr/sbin/swapinfo | /usr/bin/sed '/^\/dev/!d; s,^/dev/,,; s, .*\$,,'", $disks);
|
67
|
return $disks;
|
68
|
}
|
69
|
|
70
|
function get_disk_slices($disk) {
|
71
|
global $g, $debug;
|
72
|
$slices = glob("/dev/" . $disk . "s*");
|
73
|
$slices = str_replace("/dev/", "", $slices);
|
74
|
return $slices;
|
75
|
}
|
76
|
|
77
|
function get_disks() {
|
78
|
global $g, $debug;
|
79
|
$disks_array = array();
|
80
|
$disks_s = explode(" ", get_single_sysctl("kern.disks"));
|
81
|
foreach ($disks_s as $disk) {
|
82
|
if (trim($disk)) {
|
83
|
$disks_array[] = $disk;
|
84
|
}
|
85
|
}
|
86
|
return $disks_array;
|
87
|
}
|
88
|
|
89
|
function discover_config($mountpoint) {
|
90
|
global $g, $debug;
|
91
|
$locations_to_check = array("/", "/config");
|
92
|
foreach ($locations_to_check as $ltc) {
|
93
|
$tocheck = "/tmp/mnt/cf{$ltc}config.xml";
|
94
|
if ($debug) {
|
95
|
echo "\nChecking for $tocheck";
|
96
|
if (file_exists($tocheck)) {
|
97
|
echo " -> found!";
|
98
|
}
|
99
|
}
|
100
|
if (file_exists($tocheck)) {
|
101
|
return $tocheck;
|
102
|
}
|
103
|
}
|
104
|
return "";
|
105
|
}
|
106
|
|
107
|
function test_config($file_location) {
|
108
|
global $g, $debug;
|
109
|
if (!$file_location) {
|
110
|
return;
|
111
|
}
|
112
|
// config.xml was found. ensure it is sound.
|
113
|
$root_obj = trim("<{$g['xml_rootobj']}>");
|
114
|
$xml_file_head = exec("/usr/bin/head -2 " . escapeshellarg($file_location) . " | /usr/bin/tail -n1");
|
115
|
if ($debug) {
|
116
|
echo "\nroot obj = $root_obj";
|
117
|
echo "\nfile head = $xml_file_head";
|
118
|
}
|
119
|
if ($xml_file_head == $root_obj) {
|
120
|
// Now parse config to make sure
|
121
|
$config_status = config_validate($file_location);
|
122
|
if ($config_status) {
|
123
|
return true;
|
124
|
}
|
125
|
}
|
126
|
return false;
|
127
|
}
|
128
|
|
129
|
// Probes all disks looking for config.xml
|
130
|
function find_config_xml() {
|
131
|
global $g, $debug;
|
132
|
$disks = get_disks();
|
133
|
// Safety check.
|
134
|
if (!is_array($disks)) {
|
135
|
return;
|
136
|
}
|
137
|
$boot_disk = get_boot_disk();
|
138
|
$swap_disks = get_swap_disks();
|
139
|
exec("/bin/mkdir -p /tmp/mnt/cf");
|
140
|
foreach ($disks as $disk) {
|
141
|
$slices = get_disk_slices($disk);
|
142
|
if (is_array($slices)) {
|
143
|
foreach ($slices as $slice) {
|
144
|
if ($slice == "") {
|
145
|
continue;
|
146
|
}
|
147
|
if (stristr($slice, $boot_disk)) {
|
148
|
if ($debug) {
|
149
|
echo "\nSkipping boot device slice $slice";
|
150
|
}
|
151
|
continue;
|
152
|
}
|
153
|
if (in_array($slice, $swap_disks)) {
|
154
|
if ($debug) {
|
155
|
echo "\nSkipping swap device slice $slice";
|
156
|
}
|
157
|
continue;
|
158
|
}
|
159
|
echo " $slice";
|
160
|
// First try msdos fs
|
161
|
if ($debug) {
|
162
|
echo "\n/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
|
163
|
}
|
164
|
$result = exec("/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
|
165
|
// Next try regular fs (ufs)
|
166
|
if (!$result) {
|
167
|
if ($debug) {
|
168
|
echo "\n/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
|
169
|
}
|
170
|
$result = exec("/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
|
171
|
}
|
172
|
$mounted = trim(exec("/sbin/mount | /usr/bin/grep -v grep | /usr/bin/grep '/tmp/mnt/cf' | /usr/bin/wc -l"));
|
173
|
if ($debug) {
|
174
|
echo "\nmounted: $mounted ";
|
175
|
}
|
176
|
if (intval($mounted) > 0) {
|
177
|
// Item was mounted - look for config.xml file
|
178
|
$config_location = discover_config($slice);
|
179
|
if ($config_location) {
|
180
|
if (test_config($config_location)) {
|
181
|
// We have a valid configuration. Install it.
|
182
|
echo " -> found config.xml\n";
|
183
|
echo "Backing up old configuration...\n";
|
184
|
backup_config();
|
185
|
echo "Restoring [{$slice}] {$config_location}...\n";
|
186
|
restore_backup($config_location);
|
187
|
echo "Cleaning up...\n";
|
188
|
exec("/sbin/umount /tmp/mnt/cf");
|
189
|
exit;
|
190
|
}
|
191
|
}
|
192
|
exec("/sbin/umount /tmp/mnt/cf");
|
193
|
}
|
194
|
}
|
195
|
}
|
196
|
}
|
197
|
}
|
198
|
|
199
|
echo "External config loader 1.0 is now starting...";
|
200
|
find_config_xml();
|
201
|
echo "\n";
|
202
|
|
203
|
?>
|