Project

General

Profile

Download (5.14 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	external config loader
4
	Copyright (C) 2010 Scott Ullrich
5
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
6
	All rights reserved.
7

    
8
	Redistribution and use in source and binary forms, with or without
9
	modification, are permitted provided that the following conditions are met:
10

    
11
	1. Redistributions of source code must retain the above copyright notice,
12
	   this list of conditions and the following disclaimer.
13

    
14
	2. Redistributions in binary form must reproduce the above copyright
15
	   notice, this list of conditions and the following disclaimer in the
16
	   documentation and/or other materials provided with the distribution.
17

    
18
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
	POSSIBILITY OF SUCH DAMAGE.
28

    
29
	Currently supported file system types: MS-Dos, FreeBSD UFS
30

    
31
*/
32

    
33
require_once("globals.inc");
34
require_once("functions.inc");
35
require_once("config.lib.inc");
36
require_once("config.inc");
37

    
38
$debug = false;
39

    
40
function get_boot_disk() {
41
	global $g, $debug;
42
	$disk = exec("/sbin/mount | /usr/bin/grep \"on / \" | /usr/bin/cut -d'/' -f3 | /usr/bin/cut -d' ' -f1");
43
	return $disk;
44
}
45

    
46
function get_swap_disks() {
47
	exec("/usr/sbin/swapinfo | /usr/bin/sed '/^\/dev/!d; s,^/dev/,,; s, .*\$,,'", $disks);
48
	return $disks;
49
}
50

    
51
function get_disk_slices($disk) {
52
	global $g, $debug;
53
	$slices = glob("/dev/" . $disk . "s*");
54
	$slices = str_replace("/dev/", "", $slices);
55
	return $slices;
56
}
57

    
58
function get_disks() {
59
	global $g, $debug;
60
	$disks_array = array();
61
	$disks_s = explode(" ", get_single_sysctl("kern.disks"));
62
	foreach ($disks_s as $disk) {
63
		if (trim($disk)) {
64
			$disks_array[] = $disk;
65
		}
66
	}
67
	return $disks_array;
68
}
69

    
70
function discover_config($mountpoint) {
71
	global $g, $debug;
72
	$locations_to_check = array("/", "/config");
73
	foreach ($locations_to_check as $ltc) {
74
		$tocheck = "/tmp/mnt/cf{$ltc}config.xml";
75
		if ($debug) {
76
			echo "\nChecking for $tocheck";
77
			if (file_exists($tocheck)) {
78
				echo " -> found!";
79
			}
80
		}
81
		if (file_exists($tocheck)) {
82
			return $tocheck;
83
		}
84
	}
85
	return "";
86
}
87

    
88
function test_config($file_location) {
89
	global $g, $debug;
90
	if (!$file_location) {
91
		return;
92
	}
93
	// config.xml was found.  ensure it is sound.
94
	$root_obj = trim("<{$g['xml_rootobj']}>");
95
	$xml_file_head = exec("/usr/bin/head -2 " . escapeshellarg($file_location) . " | /usr/bin/tail -n1");
96
	if ($debug) {
97
		echo "\nroot obj  = $root_obj";
98
		echo "\nfile head = $xml_file_head";
99
	}
100
	if ($xml_file_head == $root_obj) {
101
		// Now parse config to make sure
102
		$config_status = config_validate($file_location);
103
		if ($config_status) {
104
			return true;
105
		}
106
	}
107
	return false;
108
}
109

    
110
// Probes all disks looking for config.xml
111
function find_config_xml() {
112
	global $g, $debug;
113
	$disks = get_disks();
114
	// Safety check.
115
	if (!is_array($disks)) {
116
		return;
117
	}
118
	$boot_disk = get_boot_disk();
119
	$swap_disks = get_swap_disks();
120
	exec("/bin/mkdir -p /tmp/mnt/cf");
121
	foreach ($disks as $disk) {
122
		$slices = get_disk_slices($disk);
123
		if (is_array($slices)) {
124
			foreach ($slices as $slice) {
125
				if ($slice == "") {
126
					continue;
127
				}
128
				if (stristr($slice, $boot_disk)) {
129
					if ($debug) {
130
						echo "\nSkipping boot device slice $slice";
131
					}
132
					continue;
133
				}
134
				if (in_array($slice, $swap_disks)) {
135
					if ($debug) {
136
						echo "\nSkipping swap device slice $slice";
137
					}
138
					continue;
139
				}
140
				echo " $slice";
141
				// First try msdos fs
142
				if ($debug) {
143
					echo "\n/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
144
				}
145
				$result = exec("/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
146
				// Next try regular fs (ufs)
147
				if (!$result) {
148
					if ($debug) {
149
						echo "\n/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
150
					}
151
					$result = exec("/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
152
				}
153
				$mounted = trim(exec("/sbin/mount | /usr/bin/grep -v grep | /usr/bin/grep '/tmp/mnt/cf' | /usr/bin/wc -l"));
154
				if ($debug) {
155
					echo "\nmounted: $mounted ";
156
				}
157
				if (intval($mounted) > 0) {
158
					// Item was mounted - look for config.xml file
159
					$config_location = discover_config($slice);
160
					if ($config_location) {
161
						if (test_config($config_location)) {
162
							// We have a valid configuration.  Install it.
163
							echo " -> found config.xml\n";
164
							echo "Backing up old configuration...\n";
165
							backup_config();
166
							echo "Restoring [{$slice}] {$config_location}...\n";
167
							restore_backup($config_location);
168
							echo "Cleaning up...\n";
169
							exec("/sbin/umount /tmp/mnt/cf");
170
							exit;
171
						}
172
					}
173
					exec("/sbin/umount /tmp/mnt/cf");
174
				}
175
			}
176
		}
177
	}
178
}
179

    
180
echo "External config loader 1.0 is now starting...";
181
find_config_xml();
182
echo "\n";
183

    
184
?>
(11-11/94)