Projet

Général

Profil

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

univnautes / etc / ecl.php @ master

1
<?php
2
/*  
3
	external config loader
4
	Copyright (C) 2010 Scott Ullrich
5
        Copyright (C) 2013-2014 Electric Sheep Fencing, LP
6
	All rights reserved.
7

    
8
	Redistribution and use in source and binary forms, with or without
9
	modification, are permitted provided that the following conditions are met:
10

    
11
	1. Redistributions of source code must retain the above copyright notice,
12
	   this list of conditions and the following disclaimer.
13

    
14
	2. Redistributions in binary form must reproduce the above copyright
15
	   notice, this list of conditions and the following disclaimer in the
16
	   documentation and/or other materials provided with the distribution.
17

    
18
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
	POSSIBILITY OF SUCH DAMAGE.
28
 
29
	Currently supported file system types: MS-Dos, FreeBSD UFS
30

    
31
*/
32

    
33
require_once("globals.inc");
34
require_once("functions.inc");
35
require_once("config.lib.inc");
36
require_once("config.inc");
37

    
38
$debug = false;
39

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

    
46
function get_swap_disks() {
47
	exec("/usr/sbin/swapinfo | /usr/bin/sed '/^\/dev/!d; s,^/dev/,,; s, .*\$,,'", $disks);
48
	return $disks;
49
}
50

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

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

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

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

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

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

    
175
?>
(11-11/103)