Project

General

Profile

Download (4.82 KB) Statistics
| Branch: | Tag: | Revision:
1
<?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
	Currently supported file system types: MS-Dos, FreeBSD UFS
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 = exec("/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(exec("/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 = explode(" ", $slices);
53
	return $slices_array;
54
}
55

    
56
function get_disks() {
57
	global $g, $debug;
58
	$disks_array = array();
59
	$disks = exec("/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
	if(!$file_location) 
86
		return;
87
	// config.xml was found.  ensure it is sound.
88
	$root_obj = trim("<{$g['xml_rootobj']}>");
89
	$xml_file_head = exec("/usr/bin/head -2 {$file_location} | /usr/bin/tail -n1");
90
	if($debug) {
91
		echo "\nroot obj  = $root_obj";
92
		echo "\nfile head = $xml_file_head";
93
	}
94
	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
// Probes all disks looking for config.xml
104
function find_config_xml() {
105
	global $g, $debug;
106
	$disks = get_disks();
107
	// Safety check.
108
	if(!is_array($disks)) 
109
		return;
110
	$boot_disk = get_boot_disk();
111
	exec("/bin/mkdir -p /tmp/mnt/cf");
112
	foreach($disks as $disk) {
113
		$slices = get_disk_slices($disk);
114
		if(is_array($slices)) {
115
			foreach($slices as $slice) {
116
				if($slice == "")
117
					continue;
118
				if(stristr($slice, $boot_disk)) {
119
					if($debug) 
120
						echo "\nSkipping boot device slice $slice";
121
					continue;
122
				}
123
				echo " $slice";
124
				// First try msdos fs
125
				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
				// Next try regular fs (ufs)
129
				if(!$result) {
130
					if($debug) 
131
						echo "\n/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
132
					$result = exec("/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
133
				}
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
					// 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
							echo " -> found config.xml\n";
144
							echo "Backing up old configuration...\n";
145
							backup_config();
146
							echo "Restoring [{$slice}] {$config_location}...\n";
147
							restore_backup($config_location);
148
							echo "Cleaning up...\n";
149
							exec("/sbin/umount /tmp/mnt/cf");
150
							exit;
151
						}
152
					}
153
					exec("/sbin/umount /tmp/mnt/cf");
154
				}
155
			}
156
		}
157
	}
158
}
159

    
160
echo "External config loader 1.0 is now starting...";
161
find_config_xml();
162
echo "\n";
163

    
164
?>
(13-13/107)