Project

General

Profile

Download (5.13 KB) Statistics
| Branch: | Tag: | Revision:
1 206f684d Scott Ullrich
<?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 c58b5f44 Scott Ullrich
	Currently supported file system types: MS-Dos, FreeBSD UFS
29
30 206f684d Scott Ullrich
*/
31
32 f7eb043c smos
require_once("globals.inc");
33
require_once("functions.inc");
34
require_once("config.lib.inc");
35
require_once("config.inc");
36 6edc4c0c Scott Ullrich
37
$debug = false;
38 206f684d Scott Ullrich
39
function get_boot_disk() {
40 6edc4c0c Scott Ullrich
	global $g, $debug;
41 84cf0b3e Scott Ullrich
	$disk = exec("/sbin/mount | /usr/bin/grep \"on / \" | /usr/bin/cut -d'/' -f3 | /usr/bin/cut -d' ' -f1");
42 206f684d Scott Ullrich
	return $disk;
43
}
44
45 774aedf0 Renato Botelho
function get_swap_disks() {
46
	exec("/usr/sbin/swapinfo | /usr/bin/sed '/^\/dev/!d; s,^/dev/,,; s, .*\$,,'", $disks);
47
	return $disks;
48
}
49
50 58ba038a Scott Ullrich
function get_disk_slices($disk) {
51 6edc4c0c Scott Ullrich
	global $g, $debug;
52 58ba038a Scott Ullrich
	$slices_array = array();
53 84cf0b3e Scott Ullrich
	$slices = trim(exec("/bin/ls /dev/{$disk}s* 2>/dev/null"));
54 6edc4c0c Scott Ullrich
	$slices = str_replace("/dev/", "", $slices);
55 58ba038a Scott Ullrich
	if($slices == "ls: No match.") 
56
		return;
57 cfbfd941 smos
	$slices_array = explode(" ", $slices);
58 58ba038a Scott Ullrich
	return $slices_array;
59
}
60
61 206f684d Scott Ullrich
function get_disks() {
62 6edc4c0c Scott Ullrich
	global $g, $debug;
63 206f684d Scott Ullrich
	$disks_array = array();
64 84cf0b3e Scott Ullrich
	$disks = exec("/sbin/sysctl kern.disks | cut -d':' -f2");
65 206f684d Scott Ullrich
	$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 6edc4c0c Scott Ullrich
	global $g, $debug;
74 46dd9586 Scott Ullrich
	$locations_to_check = array("/", "/config");
75 206f684d Scott Ullrich
	foreach($locations_to_check as $ltc) {
76 70bea648 Scott Ullrich
		$tocheck = "/tmp/mnt/cf{$ltc}config.xml";
77 6edc4c0c Scott Ullrich
		if($debug) {
78
			echo "\nChecking for $tocheck";
79
			if(file_exists($tocheck)) 
80
				echo " -> found!";
81
		}
82 206f684d Scott Ullrich
		if(file_exists($tocheck)) 
83
			return $tocheck;
84
	}
85
	return "";
86
}
87
88
function test_config($file_location) {
89 6edc4c0c Scott Ullrich
	global $g, $debug;
90 651a6867 Scott Ullrich
	if(!$file_location) 
91
		return;
92 206f684d Scott Ullrich
	// config.xml was found.  ensure it is sound.
93 6edc4c0c Scott Ullrich
	$root_obj = trim("<{$g['xml_rootobj']}>");
94 651a6867 Scott Ullrich
	$xml_file_head = exec("/usr/bin/head -2 {$file_location} | /usr/bin/tail -n1");
95 6edc4c0c Scott Ullrich
	if($debug) {
96
		echo "\nroot obj  = $root_obj";
97
		echo "\nfile head = $xml_file_head";
98
	}
99 206f684d Scott Ullrich
	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 58ba038a Scott Ullrich
// Probes all disks looking for config.xml
109
function find_config_xml() {
110 6edc4c0c Scott Ullrich
	global $g, $debug;
111 58ba038a Scott Ullrich
	$disks = get_disks();
112 c58b5f44 Scott Ullrich
	// Safety check.
113
	if(!is_array($disks)) 
114
		return;
115 58ba038a Scott Ullrich
	$boot_disk = get_boot_disk();
116 774aedf0 Renato Botelho
	$swap_disks = get_swap_disks();
117 6edc4c0c Scott Ullrich
	exec("/bin/mkdir -p /tmp/mnt/cf");
118 58ba038a Scott Ullrich
	foreach($disks as $disk) {
119
		$slices = get_disk_slices($disk);
120
		if(is_array($slices)) {
121
			foreach($slices as $slice) {
122 6edc4c0c Scott Ullrich
				if($slice == "")
123
					continue;
124 c58b5f44 Scott Ullrich
				if(stristr($slice, $boot_disk)) {
125
					if($debug) 
126
						echo "\nSkipping boot device slice $slice";
127
					continue;
128
				}
129 774aedf0 Renato Botelho
				if(in_array($slice, $swap_disks)) {
130
					if($debug)
131
						echo "\nSkipping swap device slice $slice";
132
					continue;
133
				}
134 58ba038a Scott Ullrich
				echo " $slice";
135
				// First try msdos fs
136 6edc4c0c Scott Ullrich
				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 58ba038a Scott Ullrich
				// Next try regular fs (ufs)
140 6edc4c0c Scott Ullrich
				if(!$result) {
141
					if($debug) 
142
						echo "\n/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
143 58ba038a Scott Ullrich
					$result = exec("/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
144 6edc4c0c Scott Ullrich
				}
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 58ba038a Scott Ullrich
					// 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 70bea648 Scott Ullrich
							echo " -> found config.xml\n";
155
							echo "Backing up old configuration...\n";
156 58ba038a Scott Ullrich
							backup_config();
157 c58b5f44 Scott Ullrich
							echo "Restoring [{$slice}] {$config_location}...\n";
158 58ba038a Scott Ullrich
							restore_backup($config_location);
159 70bea648 Scott Ullrich
							echo "Cleaning up...\n";
160 6edc4c0c Scott Ullrich
							exec("/sbin/umount /tmp/mnt/cf");
161 70bea648 Scott Ullrich
							exit;
162 58ba038a Scott Ullrich
						}
163
					}
164 de7222fb Chris Buechler
					exec("/sbin/umount /tmp/mnt/cf");
165 58ba038a Scott Ullrich
				}
166
			}
167
		}
168
	}
169
}
170
171 206f684d Scott Ullrich
echo "External config loader 1.0 is now starting...";
172
find_config_xml();
173
echo "\n";
174
175 cfbfd941 smos
?>