Projet

Général

Profil

Télécharger (5,15 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / ecl.php @ c650b2f7

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_once("globals.inc");
33
require_once("functions.inc");
34
require_once("config.lib.inc");
35
require_once("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_swap_disks() {
46
	exec("/usr/sbin/swapinfo | /usr/bin/sed '/^\/dev/!d; s,^/dev/,,; s, .*\$,,'", $disks);
47
	return $disks;
48
}
49

    
50
function get_disk_slices($disk) {
51
	global $g, $debug;
52
	$slices_array = array();
53
	$slices = trim(exec("/bin/ls " . escapeshellarg("/dev/" . $disk . "s*") . " 2>/dev/null"));
54
	$slices = str_replace("/dev/", "", $slices);
55
	if($slices == "ls: No match.") 
56
		return;
57
	$slices_array = explode(" ", $slices);
58
	return $slices_array;
59
}
60

    
61
function get_disks() {
62
	global $g, $debug;
63
	$disks_array = array();
64
	$disks_s = explode(" ", get_single_sysctl("kern.disks"));
65
	foreach($disks_s as $disk)
66
		if(trim($disk))
67
			$disks_array[] = $disk;
68
	return $disks_array;
69
}
70

    
71
function discover_config($mountpoint) {
72
	global $g, $debug;
73
	$locations_to_check = array("/", "/config");
74
	foreach($locations_to_check as $ltc) {
75
		$tocheck = "/tmp/mnt/cf{$ltc}config.xml";
76
		if($debug) {
77
			echo "\nChecking for $tocheck";
78
			if(file_exists($tocheck)) 
79
				echo " -> found!";
80
		}
81
		if(file_exists($tocheck)) 
82
			return $tocheck;
83
	}
84
	return "";
85
}
86

    
87
function test_config($file_location) {
88
	global $g, $debug;
89
	if(!$file_location) 
90
		return;
91
	// config.xml was found.  ensure it is sound.
92
	$root_obj = trim("<{$g['xml_rootobj']}>");
93
	$xml_file_head = exec("/usr/bin/head -2 " . escapeshellarg($file_location) . " | /usr/bin/tail -n1");
94
	if($debug) {
95
		echo "\nroot obj  = $root_obj";
96
		echo "\nfile head = $xml_file_head";
97
	}
98
	if($xml_file_head == $root_obj) {
99
		// Now parse config to make sure
100
		$config_status = config_validate($file_location);
101
		if($config_status) 	
102
			return true;
103
	}
104
	return false;
105
}
106

    
107
// Probes all disks looking for config.xml
108
function find_config_xml() {
109
	global $g, $debug;
110
	$disks = get_disks();
111
	// Safety check.
112
	if(!is_array($disks)) 
113
		return;
114
	$boot_disk = get_boot_disk();
115
	$swap_disks = get_swap_disks();
116
	exec("/bin/mkdir -p /tmp/mnt/cf");
117
	foreach($disks as $disk) {
118
		$slices = get_disk_slices($disk);
119
		if(is_array($slices)) {
120
			foreach($slices as $slice) {
121
				if($slice == "")
122
					continue;
123
				if(stristr($slice, $boot_disk)) {
124
					if($debug) 
125
						echo "\nSkipping boot device slice $slice";
126
					continue;
127
				}
128
				if(in_array($slice, $swap_disks)) {
129
					if($debug)
130
						echo "\nSkipping swap device slice $slice";
131
					continue;
132
				}
133
				echo " $slice";
134
				// First try msdos fs
135
				if($debug) 
136
					echo "\n/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
137
				$result = exec("/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
138
				// Next try regular fs (ufs)
139
				if(!$result) {
140
					if($debug) 
141
						echo "\n/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
142
					$result = exec("/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
143
				}
144
				$mounted = trim(exec("/sbin/mount | /usr/bin/grep -v grep | /usr/bin/grep '/tmp/mnt/cf' | /usr/bin/wc -l"));
145
				if($debug) 
146
					echo "\nmounted: $mounted ";
147
				if(intval($mounted) > 0) {
148
					// Item was mounted - look for config.xml file
149
					$config_location = discover_config($slice);
150
					if($config_location) {
151
						if(test_config($config_location)) {
152
							// We have a valid configuration.  Install it.
153
							echo " -> found config.xml\n";
154
							echo "Backing up old configuration...\n";
155
							backup_config();
156
							echo "Restoring [{$slice}] {$config_location}...\n";
157
							restore_backup($config_location);
158
							echo "Cleaning up...\n";
159
							exec("/sbin/umount /tmp/mnt/cf");
160
							exit;
161
						}
162
					}
163
					exec("/sbin/umount /tmp/mnt/cf");
164
				}
165
			}
166
		}
167
	}
168
}
169

    
170
echo "External config loader 1.0 is now starting...";
171
find_config_xml();
172
echo "\n";
173

    
174
?>
(11-11/102)