Project

General

Profile

Download (4.97 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * ecl.php
4
 *
5
 * Copyright (c) 2010-2013 BSD Perimeter
6
 * Copyright (c) 2013-2016 Electric Sheep Fencing
7
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * 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
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * 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
 */
22

    
23
require_once("globals.inc");
24
require_once("functions.inc");
25
require_once("config.lib.inc");
26
require_once("config.inc");
27

    
28
$debug = false;
29

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

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

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

    
48
function get_disks() {
49
	global $g, $debug;
50
	$disks_array = array();
51
	$disks_s = explode(" ", get_single_sysctl("kern.disks"));
52
	foreach ($disks_s as $disk) {
53
		/* Ignore the flash devices (ARM). */
54
		if (strstr($disk, "flash")) {
55
			continue;
56
		}
57
		if (trim($disk)) {
58
			$disks_array[] = $disk;
59
		}
60
	}
61
	return $disks_array;
62
}
63

    
64
function discover_config($mountpoint) {
65
	global $g, $debug;
66
	/* List of locations to check. Requires trailing slash.
67
	 * See https://redmine.pfsense.org/issues/9066 */
68
	$locations_to_check = array("/", "/config/");
69
	foreach ($locations_to_check as $ltc) {
70
		$tocheck = "/tmp/mnt/cf{$ltc}config.xml";
71
		if ($debug) {
72
			echo "\nChecking for $tocheck";
73
			if (file_exists($tocheck)) {
74
				echo " -> found!";
75
			}
76
		}
77
		if (file_exists($tocheck)) {
78
			return $tocheck;
79
		}
80
	}
81
	return "";
82
}
83

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

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

    
181
echo "External config loader 1.0 is now starting...";
182
find_config_xml();
183
echo "\n";
184

    
185
?>
(9-9/82)