Project

General

Profile

Download (4.79 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * ecl.php
4
 *
5
 * Copyright (c) 2010-2018 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
	/* List of locations to check. Requires trailing slash.
60
	 * See https://redmine.pfsense.org/issues/9066 */
61
	$locations_to_check = array("/", "/config/");
62
	foreach ($locations_to_check as $ltc) {
63
		$tocheck = "/tmp/mnt/cf{$ltc}config.xml";
64
		if ($debug) {
65
			echo "\nChecking for $tocheck";
66
			if (file_exists($tocheck)) {
67
				echo " -> found!";
68
			}
69
		}
70
		if (file_exists($tocheck)) {
71
			return $tocheck;
72
		}
73
	}
74
	return "";
75
}
76

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

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

    
174
echo "External config loader 1.0 is now starting...";
175
find_config_xml();
176
echo "\n";
177

    
178
?>
(10-10/83)