Project

General

Profile

Download (3.25 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
*/
29

    
30
require("globals.inc");
31
require("functions.inc");
32

    
33
function get_boot_disk() {
34
	global $g;
35
	$disk = `/sbin/mount | /usr/bin/grep "on / " | /usr/bin/cut -d'/' -f3 | /usr/bin/cut -d' ' -f1`;
36
	return $disk;
37
}
38

    
39
function get_disks() {
40
	global $g;
41
	$disks_array = array();
42
	$disks = `/sbin/sysctl kern.disks | cut -d':' -f2`;
43
	$disks_s = explode(" ", $disks);
44
	foreach($disks_s as $disk) 
45
		if(trim($disk))
46
			$disks_array[] = $disk;
47
	return $disks_array;
48
}
49

    
50
// Probes all disks looking for config.xml
51
function find_config_xml() {
52
	global $g;
53
	$disks = get_disks();
54
	$boot_disk = get_boot_disk();
55
	exec("mkdir -p /tmp/mnt/cf");
56
	$found_config = false;
57
	foreach($disks as $disk) {
58
		echo " $disk";
59
		// First try msdos fs
60
		$result = exec("/sbin/mount -t msdos /dev/{$disk} /tmp/mnt/cf 2>/dev/null");
61
		// Next try regular fs (ufs)
62
		if(!$result) 
63
			$result = exec("/sbin/mount /dev/{$disk} /tmp/mnt/cf 2>/dev/null");
64
		if($result == "0") {
65
			// Item was mounted - look for config.xml file
66
			$config_location = discover_config($disk);
67
			if($config_location) {
68
				if(test_config($config_location)) {
69
					// We have a valid configuration.  Install it.
70
					echo " -> found ";
71
					backup_config();
72
					restore_backup($config_location);
73
					exec("/sbin/unount /tmp/mnt/cf");
74
					break;
75
				}
76
			}
77
		}
78
	}
79
}
80

    
81
function discover_config($mountpoint) {
82
	global $g;
83
	$locations_to_check = array("/", "/config");
84
	foreach($locations_to_check as $ltc) {
85
		$tocheck = "{$mountpoint}{$ltc}/config.xml";
86
		if(file_exists($tocheck)) 
87
			return $tocheck;
88
	}
89
	return "";
90
}
91

    
92
function test_config($file_location) {
93
	global $g;
94
	// config.xml was found.  ensure it is sound.
95
	$root_obj = $g['xml_rootobj'];
96
	$xml_file_head = trim(`/bin/cat {$file_location} | /usr/bin/head -n1`);
97
	if($xml_file_head == $root_obj) {
98
		// Now parse config to make sure
99
		$config_status = config_validate($file_location);
100
		if($config_status) 	
101
			return true;
102
	}
103
	return false;
104
}
105

    
106
echo "External config loader 1.0 is now starting...";
107
find_config_xml();
108
echo "\n";
109

    
110
?>
(10-10/99)