Project

General

Profile

Download (4.45 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php -q
2

    
3
<?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
require("config.lib.inc");
35
require("config.inc");
36

    
37
$debug = false;
38

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

    
45
function get_disk_slices($disk) {
46
	global $g, $debug;
47
	$slices_array = array();
48
	$slices = trim(`/bin/ls /dev/{$disk}s* 2>/dev/null`);
49
	$slices = str_replace("/dev/", "", $slices);
50
	if($slices == "ls: No match.") 
51
		return;
52
	$slices_array = split(" ", $slices);
53
	return $slices_array;
54
}
55

    
56
function get_disks() {
57
	global $g, $debug;
58
	$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
	global $g, $debug;
69
	$locations_to_check = array("/", "/config");
70
	foreach($locations_to_check as $ltc) {
71
		$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
		if(file_exists($tocheck)) 
78
			return $tocheck;
79
	}
80
	return "";
81
}
82

    
83
function test_config($file_location) {
84
	global $g, $debug;
85
	// config.xml was found.  ensure it is sound.
86
	$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
	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
// Probes all disks looking for config.xml
102
function find_config_xml() {
103
	global $g, $debug;
104
	$disks = get_disks();
105
	$boot_disk = get_boot_disk();
106
	exec("/bin/mkdir -p /tmp/mnt/cf");
107
	foreach($disks as $disk) {
108
		$slices = get_disk_slices($disk);
109
		if(is_array($slices)) {
110
			foreach($slices as $slice) {
111
				if($slice == "")
112
					continue;
113
				echo " $slice";
114
				// First try msdos fs
115
				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
				// Next try regular fs (ufs)
119
				if(!$result) {
120
					if($debug) 
121
						echo "\n/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
122
					$result = exec("/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
123
				}
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
					// 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
							echo " -> found config.xml ";
135
							backup_config();
136
							restore_backup($config_location);
137
							exec("/sbin/umount /tmp/mnt/cf");
138
							break;
139
						}
140
					}
141
					exec("/sbin/umount /tmp/mnt/cf");
142
				}
143
			}
144
		}
145
	}
146
}
147

    
148
echo "External config loader 1.0 is now starting...";
149
find_config_xml();
150
echo "\n";
151

    
152
?>
(10-10/99)