Project

General

Profile

Download (4.97 KB) Statistics
| Branch: | Tag: | Revision:
1 206f684d Scott Ullrich
<?php
2 6c07db48 Phil Davis
/*
3 ac24dc24 Renato Botelho
 * ecl.php
4
 *
5 38809d47 Renato Botelho do Couto
 * Copyright (c) 2010-2013 BSD Perimeter
6
 * Copyright (c) 2013-2016 Electric Sheep Fencing
7 0284d79e jim-p
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
8 f83416bd Renato Botelho do Couto
 * All rights reserved.
9 ac24dc24 Renato Botelho
 *
10 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13 ac24dc24 Renato Botelho
 *
14 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
15 ac24dc24 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21 ac24dc24 Renato Botelho
 */
22 206f684d Scott Ullrich
23 f7eb043c smos
require_once("globals.inc");
24
require_once("functions.inc");
25
require_once("config.lib.inc");
26
require_once("config.inc");
27 6edc4c0c Scott Ullrich
28
$debug = false;
29 206f684d Scott Ullrich
30
function get_boot_disk() {
31 6edc4c0c Scott Ullrich
	global $g, $debug;
32 84cf0b3e Scott Ullrich
	$disk = exec("/sbin/mount | /usr/bin/grep \"on / \" | /usr/bin/cut -d'/' -f3 | /usr/bin/cut -d' ' -f1");
33 206f684d Scott Ullrich
	return $disk;
34
}
35
36 774aedf0 Renato Botelho
function get_swap_disks() {
37
	exec("/usr/sbin/swapinfo | /usr/bin/sed '/^\/dev/!d; s,^/dev/,,; s, .*\$,,'", $disks);
38
	return $disks;
39
}
40
41 58ba038a Scott Ullrich
function get_disk_slices($disk) {
42 6edc4c0c Scott Ullrich
	global $g, $debug;
43 921e237f Nick Holloway
	$slices = glob("/dev/" . $disk . "s*");
44 6edc4c0c Scott Ullrich
	$slices = str_replace("/dev/", "", $slices);
45 921e237f Nick Holloway
	return $slices;
46 58ba038a Scott Ullrich
}
47
48 206f684d Scott Ullrich
function get_disks() {
49 6edc4c0c Scott Ullrich
	global $g, $debug;
50 206f684d Scott Ullrich
	$disks_array = array();
51 971de1f9 Renato Botelho
	$disks_s = explode(" ", get_single_sysctl("kern.disks"));
52 6c07db48 Phil Davis
	foreach ($disks_s as $disk) {
53 99a641df Luiz Souza
		/* Ignore the flash devices (ARM). */
54
		if (strstr($disk, "flash")) {
55
			continue;
56
		}
57 6c07db48 Phil Davis
		if (trim($disk)) {
58 206f684d Scott Ullrich
			$disks_array[] = $disk;
59 6c07db48 Phil Davis
		}
60
	}
61 206f684d Scott Ullrich
	return $disks_array;
62
}
63
64
function discover_config($mountpoint) {
65 6edc4c0c Scott Ullrich
	global $g, $debug;
66 c688c59b jim-p
	/* List of locations to check. Requires trailing slash.
67
	 * See https://redmine.pfsense.org/issues/9066 */
68
	$locations_to_check = array("/", "/config/");
69 6c07db48 Phil Davis
	foreach ($locations_to_check as $ltc) {
70 70bea648 Scott Ullrich
		$tocheck = "/tmp/mnt/cf{$ltc}config.xml";
71 6c07db48 Phil Davis
		if ($debug) {
72 6edc4c0c Scott Ullrich
			echo "\nChecking for $tocheck";
73 6c07db48 Phil Davis
			if (file_exists($tocheck)) {
74 6edc4c0c Scott Ullrich
				echo " -> found!";
75 6c07db48 Phil Davis
			}
76 6edc4c0c Scott Ullrich
		}
77 6c07db48 Phil Davis
		if (file_exists($tocheck)) {
78 206f684d Scott Ullrich
			return $tocheck;
79 6c07db48 Phil Davis
		}
80 206f684d Scott Ullrich
	}
81
	return "";
82
}
83
84
function test_config($file_location) {
85 6edc4c0c Scott Ullrich
	global $g, $debug;
86 6c07db48 Phil Davis
	if (!$file_location) {
87 651a6867 Scott Ullrich
		return;
88 6c07db48 Phil Davis
	}
89 206f684d Scott Ullrich
	// config.xml was found.  ensure it is sound.
90 6edc4c0c Scott Ullrich
	$root_obj = trim("<{$g['xml_rootobj']}>");
91 873c1701 Renato Botelho
	$xml_file_head = exec("/usr/bin/head -2 " . escapeshellarg($file_location) . " | /usr/bin/tail -n1");
92 6c07db48 Phil Davis
	if ($debug) {
93 6edc4c0c Scott Ullrich
		echo "\nroot obj  = $root_obj";
94
		echo "\nfile head = $xml_file_head";
95
	}
96 6c07db48 Phil Davis
	if ($xml_file_head == $root_obj) {
97 206f684d Scott Ullrich
		// Now parse config to make sure
98
		$config_status = config_validate($file_location);
99 6c07db48 Phil Davis
		if ($config_status) {
100 206f684d Scott Ullrich
			return true;
101 6c07db48 Phil Davis
		}
102 206f684d Scott Ullrich
	}
103
	return false;
104
}
105
106 58ba038a Scott Ullrich
// Probes all disks looking for config.xml
107
function find_config_xml() {
108 6edc4c0c Scott Ullrich
	global $g, $debug;
109 58ba038a Scott Ullrich
	$disks = get_disks();
110 c58b5f44 Scott Ullrich
	// Safety check.
111 6c07db48 Phil Davis
	if (!is_array($disks)) {
112 c58b5f44 Scott Ullrich
		return;
113 6c07db48 Phil Davis
	}
114 58ba038a Scott Ullrich
	$boot_disk = get_boot_disk();
115 774aedf0 Renato Botelho
	$swap_disks = get_swap_disks();
116 6edc4c0c Scott Ullrich
	exec("/bin/mkdir -p /tmp/mnt/cf");
117 6c07db48 Phil Davis
	foreach ($disks as $disk) {
118 58ba038a Scott Ullrich
		$slices = get_disk_slices($disk);
119 6c07db48 Phil Davis
		if (is_array($slices)) {
120
			foreach ($slices as $slice) {
121
				if ($slice == "") {
122 6edc4c0c Scott Ullrich
					continue;
123 6c07db48 Phil Davis
				}
124
				if (stristr($slice, $boot_disk)) {
125
					if ($debug) {
126 c58b5f44 Scott Ullrich
						echo "\nSkipping boot device slice $slice";
127 6c07db48 Phil Davis
					}
128 c58b5f44 Scott Ullrich
					continue;
129
				}
130 6c07db48 Phil Davis
				if (in_array($slice, $swap_disks)) {
131
					if ($debug) {
132 774aedf0 Renato Botelho
						echo "\nSkipping swap device slice $slice";
133 6c07db48 Phil Davis
					}
134 774aedf0 Renato Botelho
					continue;
135
				}
136 58ba038a Scott Ullrich
				echo " $slice";
137
				// First try msdos fs
138 6c07db48 Phil Davis
				if ($debug) {
139 6edc4c0c Scott Ullrich
					echo "\n/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
140 6c07db48 Phil Davis
				}
141 6edc4c0c Scott Ullrich
				$result = exec("/sbin/mount -t msdosfs /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
142 58ba038a Scott Ullrich
				// Next try regular fs (ufs)
143 6c07db48 Phil Davis
				if (!$result) {
144
					if ($debug) {
145 6edc4c0c Scott Ullrich
						echo "\n/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null \n";
146 6c07db48 Phil Davis
					}
147 58ba038a Scott Ullrich
					$result = exec("/sbin/mount /dev/{$slice} /tmp/mnt/cf 2>/dev/null");
148 6edc4c0c Scott Ullrich
				}
149
				$mounted = trim(exec("/sbin/mount | /usr/bin/grep -v grep | /usr/bin/grep '/tmp/mnt/cf' | /usr/bin/wc -l"));
150 6c07db48 Phil Davis
				if ($debug) {
151 6edc4c0c Scott Ullrich
					echo "\nmounted: $mounted ";
152 6c07db48 Phil Davis
				}
153
				if (intval($mounted) > 0) {
154 58ba038a Scott Ullrich
					// Item was mounted - look for config.xml file
155
					$config_location = discover_config($slice);
156 6c07db48 Phil Davis
					if ($config_location) {
157
						if (test_config($config_location)) {
158 58ba038a Scott Ullrich
							// We have a valid configuration.  Install it.
159 70bea648 Scott Ullrich
							echo " -> found config.xml\n";
160
							echo "Backing up old configuration...\n";
161 58ba038a Scott Ullrich
							backup_config();
162 c58b5f44 Scott Ullrich
							echo "Restoring [{$slice}] {$config_location}...\n";
163 58ba038a Scott Ullrich
							restore_backup($config_location);
164 695f7d5c jim-p
							if (file_exists('/cf/conf/trigger_initial_wizard')) {
165
								echo "First boot after install, setting flag for package sync and disabling wizard...\n";
166
								touch('/cf/conf/needs_package_sync');
167
								@unlink('/cf/conf/trigger_initial_wizard');
168
							}
169 70bea648 Scott Ullrich
							echo "Cleaning up...\n";
170 6edc4c0c Scott Ullrich
							exec("/sbin/umount /tmp/mnt/cf");
171 70bea648 Scott Ullrich
							exit;
172 58ba038a Scott Ullrich
						}
173
					}
174 de7222fb Chris Buechler
					exec("/sbin/umount /tmp/mnt/cf");
175 58ba038a Scott Ullrich
				}
176
			}
177
		}
178
	}
179
}
180
181 206f684d Scott Ullrich
echo "External config loader 1.0 is now starting...";
182
find_config_xml();
183
echo "\n";
184
185 cfbfd941 smos
?>