Project

General

Profile

Download (4.42 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * ecl.php
4
 *
5
 * Copyright (c) 2010-2015 Rubicon Communications, LLC (Netgate). All rights reserved.
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 * http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19

    
20
require_once("globals.inc");
21
require_once("functions.inc");
22
require_once("config.lib.inc");
23
require_once("config.inc");
24

    
25
$debug = false;
26

    
27
function get_boot_disk() {
28
	global $g, $debug;
29
	$disk = exec("/sbin/mount | /usr/bin/grep \"on / \" | /usr/bin/cut -d'/' -f3 | /usr/bin/cut -d' ' -f1");
30
	return $disk;
31
}
32

    
33
function get_swap_disks() {
34
	exec("/usr/sbin/swapinfo | /usr/bin/sed '/^\/dev/!d; s,^/dev/,,; s, .*\$,,'", $disks);
35
	return $disks;
36
}
37

    
38
function get_disk_slices($disk) {
39
	global $g, $debug;
40
	$slices = glob("/dev/" . $disk . "s*");
41
	$slices = str_replace("/dev/", "", $slices);
42
	return $slices;
43
}
44

    
45
function get_disks() {
46
	global $g, $debug;
47
	$disks_array = array();
48
	$disks_s = explode(" ", get_single_sysctl("kern.disks"));
49
	foreach ($disks_s as $disk) {
50
		if (trim($disk)) {
51
			$disks_array[] = $disk;
52
		}
53
	}
54
	return $disks_array;
55
}
56

    
57
function discover_config($mountpoint) {
58
	global $g, $debug;
59
	$locations_to_check = array("/", "/config");
60
	foreach ($locations_to_check as $ltc) {
61
		$tocheck = "/tmp/mnt/cf{$ltc}config.xml";
62
		if ($debug) {
63
			echo "\nChecking for $tocheck";
64
			if (file_exists($tocheck)) {
65
				echo " -> found!";
66
			}
67
		}
68
		if (file_exists($tocheck)) {
69
			return $tocheck;
70
		}
71
	}
72
	return "";
73
}
74

    
75
function test_config($file_location) {
76
	global $g, $debug;
77
	if (!$file_location) {
78
		return;
79
	}
80
	// config.xml was found.  ensure it is sound.
81
	$root_obj = trim("<{$g['xml_rootobj']}>");
82
	$xml_file_head = exec("/usr/bin/head -2 " . escapeshellarg($file_location) . " | /usr/bin/tail -n1");
83
	if ($debug) {
84
		echo "\nroot obj  = $root_obj";
85
		echo "\nfile head = $xml_file_head";
86
	}
87
	if ($xml_file_head == $root_obj) {
88
		// Now parse config to make sure
89
		$config_status = config_validate($file_location);
90
		if ($config_status) {
91
			return true;
92
		}
93
	}
94
	return false;
95
}
96

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

    
167
echo "External config loader 1.0 is now starting...";
168
find_config_xml();
169
echo "\n";
170

    
171
?>
(7-7/78)