Project

General

Profile

Download (4.88 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-2019 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
		if (trim($disk)) {
54
			$disks_array[] = $disk;
55
		}
56
	}
57
	return $disks_array;
58
}
59

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

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

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

    
177
echo "External config loader 1.0 is now starting...";
178
find_config_xml();
179
echo "\n";
180

    
181
?>
(9-9/81)