Project

General

Profile

Download (4.42 KB) Statistics
| Branch: | Tag: | Revision:
1 206f684d Scott Ullrich
<?php
2 6c07db48 Phil Davis
/*
3 ac24dc24 Renato Botelho
 * ecl.php
4
 *
5 81299b5c Renato Botelho
 * Copyright (c) 2010-2015 Rubicon Communications, LLC (Netgate). All rights reserved.
6 ac24dc24 Renato Botelho
 *
7 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 *
11 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
12 ac24dc24 Renato Botelho
 *
13 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 */
19 206f684d Scott Ullrich
20 f7eb043c smos
require_once("globals.inc");
21
require_once("functions.inc");
22
require_once("config.lib.inc");
23
require_once("config.inc");
24 6edc4c0c Scott Ullrich
25
$debug = false;
26 206f684d Scott Ullrich
27
function get_boot_disk() {
28 6edc4c0c Scott Ullrich
	global $g, $debug;
29 84cf0b3e Scott Ullrich
	$disk = exec("/sbin/mount | /usr/bin/grep \"on / \" | /usr/bin/cut -d'/' -f3 | /usr/bin/cut -d' ' -f1");
30 206f684d Scott Ullrich
	return $disk;
31
}
32
33 774aedf0 Renato Botelho
function get_swap_disks() {
34
	exec("/usr/sbin/swapinfo | /usr/bin/sed '/^\/dev/!d; s,^/dev/,,; s, .*\$,,'", $disks);
35
	return $disks;
36
}
37
38 58ba038a Scott Ullrich
function get_disk_slices($disk) {
39 6edc4c0c Scott Ullrich
	global $g, $debug;
40 921e237f Nick Holloway
	$slices = glob("/dev/" . $disk . "s*");
41 6edc4c0c Scott Ullrich
	$slices = str_replace("/dev/", "", $slices);
42 921e237f Nick Holloway
	return $slices;
43 58ba038a Scott Ullrich
}
44
45 206f684d Scott Ullrich
function get_disks() {
46 6edc4c0c Scott Ullrich
	global $g, $debug;
47 206f684d Scott Ullrich
	$disks_array = array();
48 971de1f9 Renato Botelho
	$disks_s = explode(" ", get_single_sysctl("kern.disks"));
49 6c07db48 Phil Davis
	foreach ($disks_s as $disk) {
50
		if (trim($disk)) {
51 206f684d Scott Ullrich
			$disks_array[] = $disk;
52 6c07db48 Phil Davis
		}
53
	}
54 206f684d Scott Ullrich
	return $disks_array;
55
}
56
57
function discover_config($mountpoint) {
58 6edc4c0c Scott Ullrich
	global $g, $debug;
59 46dd9586 Scott Ullrich
	$locations_to_check = array("/", "/config");
60 6c07db48 Phil Davis
	foreach ($locations_to_check as $ltc) {
61 70bea648 Scott Ullrich
		$tocheck = "/tmp/mnt/cf{$ltc}config.xml";
62 6c07db48 Phil Davis
		if ($debug) {
63 6edc4c0c Scott Ullrich
			echo "\nChecking for $tocheck";
64 6c07db48 Phil Davis
			if (file_exists($tocheck)) {
65 6edc4c0c Scott Ullrich
				echo " -> found!";
66 6c07db48 Phil Davis
			}
67 6edc4c0c Scott Ullrich
		}
68 6c07db48 Phil Davis
		if (file_exists($tocheck)) {
69 206f684d Scott Ullrich
			return $tocheck;
70 6c07db48 Phil Davis
		}
71 206f684d Scott Ullrich
	}
72
	return "";
73
}
74
75
function test_config($file_location) {
76 6edc4c0c Scott Ullrich
	global $g, $debug;
77 6c07db48 Phil Davis
	if (!$file_location) {
78 651a6867 Scott Ullrich
		return;
79 6c07db48 Phil Davis
	}
80 206f684d Scott Ullrich
	// config.xml was found.  ensure it is sound.
81 6edc4c0c Scott Ullrich
	$root_obj = trim("<{$g['xml_rootobj']}>");
82 873c1701 Renato Botelho
	$xml_file_head = exec("/usr/bin/head -2 " . escapeshellarg($file_location) . " | /usr/bin/tail -n1");
83 6c07db48 Phil Davis
	if ($debug) {
84 6edc4c0c Scott Ullrich
		echo "\nroot obj  = $root_obj";
85
		echo "\nfile head = $xml_file_head";
86
	}
87 6c07db48 Phil Davis
	if ($xml_file_head == $root_obj) {
88 206f684d Scott Ullrich
		// Now parse config to make sure
89
		$config_status = config_validate($file_location);
90 6c07db48 Phil Davis
		if ($config_status) {
91 206f684d Scott Ullrich
			return true;
92 6c07db48 Phil Davis
		}
93 206f684d Scott Ullrich
	}
94
	return false;
95
}
96
97 58ba038a Scott Ullrich
// Probes all disks looking for config.xml
98
function find_config_xml() {
99 6edc4c0c Scott Ullrich
	global $g, $debug;
100 58ba038a Scott Ullrich
	$disks = get_disks();
101 c58b5f44 Scott Ullrich
	// Safety check.
102 6c07db48 Phil Davis
	if (!is_array($disks)) {
103 c58b5f44 Scott Ullrich
		return;
104 6c07db48 Phil Davis
	}
105 58ba038a Scott Ullrich
	$boot_disk = get_boot_disk();
106 774aedf0 Renato Botelho
	$swap_disks = get_swap_disks();
107 6edc4c0c Scott Ullrich
	exec("/bin/mkdir -p /tmp/mnt/cf");
108 6c07db48 Phil Davis
	foreach ($disks as $disk) {
109 58ba038a Scott Ullrich
		$slices = get_disk_slices($disk);
110 6c07db48 Phil Davis
		if (is_array($slices)) {
111
			foreach ($slices as $slice) {
112
				if ($slice == "") {
113 6edc4c0c Scott Ullrich
					continue;
114 6c07db48 Phil Davis
				}
115
				if (stristr($slice, $boot_disk)) {
116
					if ($debug) {
117 c58b5f44 Scott Ullrich
						echo "\nSkipping boot device slice $slice";
118 6c07db48 Phil Davis
					}
119 c58b5f44 Scott Ullrich
					continue;
120
				}
121 6c07db48 Phil Davis
				if (in_array($slice, $swap_disks)) {
122
					if ($debug) {
123 774aedf0 Renato Botelho
						echo "\nSkipping swap device slice $slice";
124 6c07db48 Phil Davis
					}
125 774aedf0 Renato Botelho
					continue;
126
				}
127 58ba038a Scott Ullrich
				echo " $slice";
128
				// First try msdos fs
129 6c07db48 Phil Davis
				if ($debug) {
130 6edc4c0c Scott Ullrich
					echo "\n/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
131 6c07db48 Phil Davis
				}
132 6edc4c0c Scott Ullrich
				$result = exec("/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
133 58ba038a Scott Ullrich
				// Next try regular fs (ufs)
134 6c07db48 Phil Davis
				if (!$result) {
135
					if ($debug) {
136 6edc4c0c Scott Ullrich
						echo "\n/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
137 6c07db48 Phil Davis
					}
138 58ba038a Scott Ullrich
					$result = exec("/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
139 6edc4c0c Scott Ullrich
				}
140
				$mounted = trim(exec("/sbin/mount | /usr/bin/grep -v grep | /usr/bin/grep '/tmp/mnt/cf' | /usr/bin/wc -l"));
141 6c07db48 Phil Davis
				if ($debug) {
142 6edc4c0c Scott Ullrich
					echo "\nmounted: $mounted ";
143 6c07db48 Phil Davis
				}
144
				if (intval($mounted) > 0) {
145 58ba038a Scott Ullrich
					// Item was mounted - look for config.xml file
146
					$config_location = discover_config($slice);
147 6c07db48 Phil Davis
					if ($config_location) {
148
						if (test_config($config_location)) {
149 58ba038a Scott Ullrich
							// We have a valid configuration.  Install it.
150 70bea648 Scott Ullrich
							echo " -> found config.xml\n";
151
							echo "Backing up old configuration...\n";
152 58ba038a Scott Ullrich
							backup_config();
153 c58b5f44 Scott Ullrich
							echo "Restoring [{$slice}] {$config_location}...\n";
154 58ba038a Scott Ullrich
							restore_backup($config_location);
155 70bea648 Scott Ullrich
							echo "Cleaning up...\n";
156 6edc4c0c Scott Ullrich
							exec("/sbin/umount /tmp/mnt/cf");
157 70bea648 Scott Ullrich
							exit;
158 58ba038a Scott Ullrich
						}
159
					}
160 de7222fb Chris Buechler
					exec("/sbin/umount /tmp/mnt/cf");
161 58ba038a Scott Ullrich
				}
162
			}
163
		}
164
	}
165
}
166
167 206f684d Scott Ullrich
echo "External config loader 1.0 is now starting...";
168
find_config_xml();
169
echo "\n";
170
171 cfbfd941 smos
?>