Project

General

Profile

Download (4.82 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
require("globals.inc");
33
require("functions.inc");
34 6edc4c0c Scott Ullrich
require("config.lib.inc");
35
require("config.inc");
36
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 58ba038a Scott Ullrich
function get_disk_slices($disk) {
46 6edc4c0c Scott Ullrich
	global $g, $debug;
47 58ba038a Scott Ullrich
	$slices_array = array();
48 84cf0b3e Scott Ullrich
	$slices = trim(exec("/bin/ls /dev/{$disk}s* 2>/dev/null"));
49 6edc4c0c Scott Ullrich
	$slices = str_replace("/dev/", "", $slices);
50 58ba038a Scott Ullrich
	if($slices == "ls: No match.") 
51
		return;
52 cfbfd941 smos
	$slices_array = explode(" ", $slices);
53 58ba038a Scott Ullrich
	return $slices_array;
54
}
55
56 206f684d Scott Ullrich
function get_disks() {
57 6edc4c0c Scott Ullrich
	global $g, $debug;
58 206f684d Scott Ullrich
	$disks_array = array();
59 84cf0b3e Scott Ullrich
	$disks = exec("/sbin/sysctl kern.disks | cut -d':' -f2");
60 206f684d Scott Ullrich
	$disks_s = explode(" ", $disks);
61
	foreach($disks_s as $disk) 
62
		if(trim($disk))
63
			$disks_array[] = $disk;
64
	return $disks_array;
65
}
66
67
function discover_config($mountpoint) {
68 6edc4c0c Scott Ullrich
	global $g, $debug;
69 46dd9586 Scott Ullrich
	$locations_to_check = array("/", "/config");
70 206f684d Scott Ullrich
	foreach($locations_to_check as $ltc) {
71 70bea648 Scott Ullrich
		$tocheck = "/tmp/mnt/cf{$ltc}config.xml";
72 6edc4c0c Scott Ullrich
		if($debug) {
73
			echo "\nChecking for $tocheck";
74
			if(file_exists($tocheck)) 
75
				echo " -> found!";
76
		}
77 206f684d Scott Ullrich
		if(file_exists($tocheck)) 
78
			return $tocheck;
79
	}
80
	return "";
81
}
82
83
function test_config($file_location) {
84 6edc4c0c Scott Ullrich
	global $g, $debug;
85 651a6867 Scott Ullrich
	if(!$file_location) 
86
		return;
87 206f684d Scott Ullrich
	// config.xml was found.  ensure it is sound.
88 6edc4c0c Scott Ullrich
	$root_obj = trim("<{$g['xml_rootobj']}>");
89 651a6867 Scott Ullrich
	$xml_file_head = exec("/usr/bin/head -2 {$file_location} | /usr/bin/tail -n1");
90 6edc4c0c Scott Ullrich
	if($debug) {
91
		echo "\nroot obj  = $root_obj";
92
		echo "\nfile head = $xml_file_head";
93
	}
94 206f684d Scott Ullrich
	if($xml_file_head == $root_obj) {
95
		// Now parse config to make sure
96
		$config_status = config_validate($file_location);
97
		if($config_status) 	
98
			return true;
99
	}
100
	return false;
101
}
102
103 58ba038a Scott Ullrich
// Probes all disks looking for config.xml
104
function find_config_xml() {
105 6edc4c0c Scott Ullrich
	global $g, $debug;
106 58ba038a Scott Ullrich
	$disks = get_disks();
107 c58b5f44 Scott Ullrich
	// Safety check.
108
	if(!is_array($disks)) 
109
		return;
110 58ba038a Scott Ullrich
	$boot_disk = get_boot_disk();
111 6edc4c0c Scott Ullrich
	exec("/bin/mkdir -p /tmp/mnt/cf");
112 58ba038a Scott Ullrich
	foreach($disks as $disk) {
113
		$slices = get_disk_slices($disk);
114
		if(is_array($slices)) {
115
			foreach($slices as $slice) {
116 6edc4c0c Scott Ullrich
				if($slice == "")
117
					continue;
118 c58b5f44 Scott Ullrich
				if(stristr($slice, $boot_disk)) {
119
					if($debug) 
120
						echo "\nSkipping boot device slice $slice";
121
					continue;
122
				}
123 58ba038a Scott Ullrich
				echo " $slice";
124
				// First try msdos fs
125 6edc4c0c Scott Ullrich
				if($debug) 
126
					echo "\n/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
127
				$result = exec("/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
128 58ba038a Scott Ullrich
				// Next try regular fs (ufs)
129 6edc4c0c Scott Ullrich
				if(!$result) {
130
					if($debug) 
131
						echo "\n/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
132 58ba038a Scott Ullrich
					$result = exec("/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
133 6edc4c0c Scott Ullrich
				}
134
				$mounted = trim(exec("/sbin/mount | /usr/bin/grep -v grep | /usr/bin/grep '/tmp/mnt/cf' | /usr/bin/wc -l"));
135
				if($debug) 
136
					echo "\nmounted: $mounted ";
137
				if(intval($mounted) > 0) {
138 58ba038a Scott Ullrich
					// Item was mounted - look for config.xml file
139
					$config_location = discover_config($slice);
140
					if($config_location) {
141
						if(test_config($config_location)) {
142
							// We have a valid configuration.  Install it.
143 70bea648 Scott Ullrich
							echo " -> found config.xml\n";
144
							echo "Backing up old configuration...\n";
145 58ba038a Scott Ullrich
							backup_config();
146 c58b5f44 Scott Ullrich
							echo "Restoring [{$slice}] {$config_location}...\n";
147 58ba038a Scott Ullrich
							restore_backup($config_location);
148 70bea648 Scott Ullrich
							echo "Cleaning up...\n";
149 6edc4c0c Scott Ullrich
							exec("/sbin/umount /tmp/mnt/cf");
150 70bea648 Scott Ullrich
							exit;
151 58ba038a Scott Ullrich
						}
152
					}
153 de7222fb Chris Buechler
					exec("/sbin/umount /tmp/mnt/cf");
154 58ba038a Scott Ullrich
				}
155
			}
156
		}
157
	}
158
}
159
160 206f684d Scott Ullrich
echo "External config loader 1.0 is now starting...";
161
find_config_xml();
162
echo "\n";
163
164 cfbfd941 smos
?>