Project

General

Profile

Download (4.45 KB) Statistics
| Branch: | Tag: | Revision:
1 6edc4c0c Scott Ullrich
#!/usr/local/bin/php -q
2 a620ea36 Scott Ullrich
3 206f684d Scott Ullrich
<?php
4
/*  
5
	external config loader
6
	Copyright (C) 2010 Scott Ullrich
7
	All rights reserved.
8
9
	Redistribution and use in source and binary forms, with or without
10
	modification, are permitted provided that the following conditions are met:
11
12
	1. Redistributions of source code must retain the above copyright notice,
13
	   this list of conditions and the following disclaimer.
14
15
	2. Redistributions in binary form must reproduce the above copyright
16
	   notice, this list of conditions and the following disclaimer in the
17
	   documentation and/or other materials provided with the distribution.
18
19
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
	POSSIBILITY OF SUCH DAMAGE.
29
 
30
*/
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 206f684d Scott Ullrich
	$disk = `/sbin/mount | /usr/bin/grep "on / " | /usr/bin/cut -d'/' -f3 | /usr/bin/cut -d' ' -f1`;
42
	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 6edc4c0c Scott Ullrich
	$slices = trim(`/bin/ls /dev/{$disk}s* 2>/dev/null`);
49
	$slices = str_replace("/dev/", "", $slices);
50 58ba038a Scott Ullrich
	if($slices == "ls: No match.") 
51
		return;
52
	$slices_array = split(" ", $slices);
53
	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
	$disks = `/sbin/sysctl kern.disks | cut -d':' -f2`;
60
	$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 6edc4c0c Scott Ullrich
		$tocheck = "/tmp/mnt/cf/{$ltc}/config.xml";
72
		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 206f684d Scott Ullrich
	// config.xml was found.  ensure it is sound.
86 6edc4c0c Scott Ullrich
	$root_obj = trim("<{$g['xml_rootobj']}>");
87
	$xml_file_head = trim(`/bin/cat {$file_location} | /usr/bin/head -n2 | /usr/bin/tail -n1`);
88
	if($debug) {
89
		echo "\nroot obj  = $root_obj";
90
		echo "\nfile head = $xml_file_head";
91
	}
92 206f684d Scott Ullrich
	if($xml_file_head == $root_obj) {
93
		// Now parse config to make sure
94
		$config_status = config_validate($file_location);
95
		if($config_status) 	
96
			return true;
97
	}
98
	return false;
99
}
100
101 58ba038a Scott Ullrich
// Probes all disks looking for config.xml
102
function find_config_xml() {
103 6edc4c0c Scott Ullrich
	global $g, $debug;
104 58ba038a Scott Ullrich
	$disks = get_disks();
105
	$boot_disk = get_boot_disk();
106 6edc4c0c Scott Ullrich
	exec("/bin/mkdir -p /tmp/mnt/cf");
107 58ba038a Scott Ullrich
	foreach($disks as $disk) {
108
		$slices = get_disk_slices($disk);
109
		if(is_array($slices)) {
110
			foreach($slices as $slice) {
111 6edc4c0c Scott Ullrich
				if($slice == "")
112
					continue;
113 58ba038a Scott Ullrich
				echo " $slice";
114
				// First try msdos fs
115 6edc4c0c Scott Ullrich
				if($debug) 
116
					echo "\n/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
117
				$result = exec("/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
118 58ba038a Scott Ullrich
				// Next try regular fs (ufs)
119 6edc4c0c Scott Ullrich
				if(!$result) {
120
					if($debug) 
121
						echo "\n/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
122 58ba038a Scott Ullrich
					$result = exec("/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
123 6edc4c0c Scott Ullrich
				}
124
				$mounted = trim(exec("/sbin/mount | /usr/bin/grep -v grep | /usr/bin/grep '/tmp/mnt/cf' | /usr/bin/wc -l"));
125
				if($debug) 
126
					echo "\nmounted: $mounted ";
127
				if(intval($mounted) > 0) {
128
					echo " ! ";
129 58ba038a Scott Ullrich
					// Item was mounted - look for config.xml file
130
					$config_location = discover_config($slice);
131
					if($config_location) {
132
						if(test_config($config_location)) {
133
							// We have a valid configuration.  Install it.
134 6edc4c0c Scott Ullrich
							echo " -> found config.xml ";
135 58ba038a Scott Ullrich
							backup_config();
136
							restore_backup($config_location);
137 6edc4c0c Scott Ullrich
							exec("/sbin/umount /tmp/mnt/cf");
138 58ba038a Scott Ullrich
							break;
139
						}
140
					}
141 6edc4c0c Scott Ullrich
					exec("/sbin/umount /tmp/mnt/cf");
142 58ba038a Scott Ullrich
				}
143
			}
144
		}
145
	}
146
}
147
148 206f684d Scott Ullrich
echo "External config loader 1.0 is now starting...";
149
find_config_xml();
150
echo "\n";
151
152
?>