Project

General

Profile

Download (4.88 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * ecl.php
4
 *
5
 * Copyright (c) 2010-2020 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
		/* Ignore the flash devices (ARM). */
51
		if (strstr($disk, "flash")) {
52
			continue;
53
		}
54
		if (trim($disk)) {
55
			$disks_array[] = $disk;
56
		}
57
	}
58
	return $disks_array;
59
}
60

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

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

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

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

    
182
?>
(9-9/82)