Project

General

Profile

Download (9.84 KB) Statistics
| Branch: | Tag: | Revision:
1 054f0ed0 Steve Beaver
<?php
2
/*
3
 * autoconfigbackup.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 38809d47 Renato Botelho do Couto
 * Copyright (c) 2008-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2019 Rubicon Communications, LLC (Netgate)
9 054f0ed0 Steve Beaver
 * All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23
24
require_once("filter.inc");
25
require_once("notices.inc");
26
27
// If there is no ssh key in the system to identify this firewall, generate a pair now
28
function get_device_key() {
29
	if (!file_exists("/etc/ssh/ssh_host_ed25519_key.pub")) {
30
		exec("/usr/bin/nice -n20 /usr/bin/ssh-keygen -t ed25519 -b 4096 -N '' -f /etc/ssh//ssh_host_ed25519_key");
31
	}
32
33 ce9eb0fb Steve Beaver
	// The userkey (firewall identifier) is an sha256 hash of the ed25519 public key
34 054f0ed0 Steve Beaver
	return hash("sha256", file_get_contents("/etc/ssh/ssh_host_ed25519_key.pub"));
35
}
36
37
$origkey = get_device_key();
38
39
if (isset($_REQUEST['userkey']) && !empty($_REQUEST['userkey'])) {
40
	$userkey = htmlentities($_REQUEST['userkey']);
41
} else {
42
	$userkey = get_device_key();
43
}
44
45
$uniqueID = system_get_uniqueid();
46
47
/* Check whether ACB is enabled */
48
function acb_enabled() {
49
	global $config;
50
	$acb_enabled = false;
51
52
	if (is_array($config['system']['acb'])) {
53
		if ($config['system']['acb']['enable'] == "yes") {
54
			$acb_enabled = true;
55
		}
56
	}
57
58
	return $acb_enabled;
59
}
60
61
// Ensures patches match
62
function acb_custom_php_validation_command($post, &$input_errors) {
63
	global $_POST, $savemsg, $config;
64
65
	// Do nothing when ACB is disabled in configuration
66
	// This also makes it possible to delete the credentials from config.xml
67
	if (!acb_enabled()) {
68
		// We do not need to store this value.
69
		unset($_POST['testconnection']);
70
		return;
71
	}
72
73
	if (!$post['crypto_password'] or !$post['crypto_password2']) {
74
		$input_errors[] = "The encryption password is required.";
75
	}
76
77
	if ($post['crypto_password'] <> $post['crypto_password2']) {
78
		$input_errors[] = "Sorry, the entered encryption passwords do not match.";
79
	}
80
81
	if ($post['testconnection']) {
82
		$status = test_connection($post);
83
		if ($status) {
84
			$savemsg = "Connection to the ACB server was tested with no errors.";
85
		}
86
	}
87
88
	// We do not need to store this value.
89
	unset($_POST['testconnection']);
90
}
91
92
function acb_custom_php_resync_config_command() {
93
	// Do nothing when ACB is disabled in configuration
94
	if (!acb_enabled()) {
95
		return;
96
	}
97
98 522388a7 Renato Botelho
	unlink_if_exists("/cf/conf/lastpfSbackup.txt");
99 af0edce6 Steve Beaver
100 054f0ed0 Steve Beaver
	if (!function_exists("filter_configure")) {
101
		require_once("filter.inc");
102
	}
103 af0edce6 Steve Beaver
104 054f0ed0 Steve Beaver
	filter_configure();
105 af0edce6 Steve Beaver
106 054f0ed0 Steve Beaver
	if ($savemsg) {
107
		$savemsg .= "<br/>";
108
	}
109 af0edce6 Steve Beaver
110 054f0ed0 Steve Beaver
	$savemsg .= "A configuration backup has been queued.";
111
}
112
113
function configure_proxy() {
114
	global $config;
115
	$ret = array();
116
	if (!empty($config['system']['proxyurl'])) {
117
		$ret[CURLOPT_PROXY] = $config['system']['proxyurl'];
118
		if (!empty($config['system']['proxyport'])) {
119
			$ret[CURLOPT_PROXYPORT] = $config['system']['proxyport'];
120
		}
121
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
122
			$ret[CURLOPT_PROXYAUTH] = CURLAUTH_ANY | CURLAUTH_ANYSAFE;
123
			$ret[CURLOPT_PROXYUSERPWD] = "{$config['system']['proxyuser']}:{$config['system']['proxypass']}";
124
		}
125
	}
126
	return $ret;
127
}
128
129
function test_connection($post) {
130
	global $savemsg, $config, $g, $legacy;
131
132
	// Do nothing when booting or when not enabled
133
	if (platform_booting() || !acb_enabled()) {
134
		return;
135
	}
136
137 f3f98e97 Phil Davis
	// Separator used during client / server communications
138 054f0ed0 Steve Beaver
	$oper_sep = "\|\|";
139
140
	// Encryption password
141
	$decrypt_password = $post['crypto_password'];
142
143
	// Defined username. Username must be sent lowercase. See Redmine #7127 and Netgate Redmine #163
144
	$username = strtolower($post['username']);
145
146
	// Defined password
147
	$password = $post['password'];
148
149
	// Set hostname
150
	$hostname = $config['system']['hostname'] . "." . $config['system']['domain'];
151
152
	// URL to restore.php
153
	$get_url = $legacy ? "https://portal.pfsense.org/pfSconfigbackups/restore.php":"https://acb.netgate.com/getbkp";
154
155
	// Populate available backups
156
	$curl_session = curl_init();
157
	curl_setopt($curl_session, CURLOPT_URL, $get_url);
158 af0edce6 Steve Beaver
159 054f0ed0 Steve Beaver
	if ($legacy) {
160
		curl_setopt($curl_session, CURLOPT_HTTPHEADER, array("Authorization: Basic " . base64_encode("{$username}:{$password}")));
161
	}
162 af0edce6 Steve Beaver
163 054f0ed0 Steve Beaver
	curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, 1);
164
	curl_setopt($curl_session, CURLOPT_POST, 1);
165
	curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1);
166
	curl_setopt($curl_session, CURLOPT_CONNECTTIMEOUT, 55);
167
	curl_setopt($curl_session, CURLOPT_TIMEOUT, 30);
168
	curl_setopt($curl_session, CURLOPT_USERAGENT, $g['product_name'] . '/' . rtrim(file_get_contents("/etc/version")));
169
	// Proxy
170
	curl_setopt_array($curl_session, configure_proxy());
171
172
	curl_setopt($curl_session, CURLOPT_POSTFIELDS, "action=showbackups&hostname={$hostname}");
173
	$data = curl_exec($curl_session);
174
175
	if (curl_errno($curl_session)) {
176
		return("An error occurred " . curl_error($curl_session));
177
	} else {
178
		curl_close($curl_session);
179
	}
180
181
	return;
182
}
183
184 463d5d11 Steve Beaver
function upload_config($manual = false) {
185 054f0ed0 Steve Beaver
	global $config, $g, $input_errors, $userkey, $uniqueID;
186
187
	if (empty($userkey)) {
188
		$userkey = get_device_key();
189
	}
190
191
	if (empty($uniqueID)) {
192
		$uniqueID = system_get_uniqueid();
193
	}
194
195
	// Do nothing when booting or when not enabled
196
	if (platform_booting() || !acb_enabled()) {
197
		return;
198
	}
199
200
	/*
201
	 * pfSense upload config to pfSense.org script
202
	 * This file plugs into config.inc (/usr/local/pkg/parse_config)
203
	 * and runs every time the running firewall filter changes.
204
	 *
205
	 */
206
207
	if (file_exists("/tmp/acb_nooverwrite")) {
208
		unlink("/tmp/acb_nooverwrite");
209
		$nooverwrite = "true";
210
	} else {
211
		$nooverwrite = "false";
212
	}
213
214
	// Define some needed variables
215
	if (file_exists("/cf/conf/lastpfSbackup.txt")) {
216
		$last_backup_date = str_replace("\n", "", file_get_contents("/cf/conf/lastpfSbackup.txt"));
217
	} else {
218
		$last_backup_date = "";
219
	}
220
221
	$last_config_change = $config['revision']['time'];
222
	$hostname = $config['system']['hostname'] . "." . $config['system']['domain'];
223 6f6299a3 Steve Beaver
	$reason	= $config['revision']['description'];
224 af0edce6 Steve Beaver
225 6f6299a3 Steve Beaver
	if ($manual && array_key_exists('numman', $config['system']['acb'])) {
226
		$manmax = $config['system']['acb']['numman'];
227 054f0ed0 Steve Beaver
	} else {
228 6f6299a3 Steve Beaver
		$manmax = "0";
229 054f0ed0 Steve Beaver
	}
230
231 587315d5 Steve Beaver
	$encryptpw = $config['system']['acb']['encryption_password'];
232 054f0ed0 Steve Beaver
233
	// Define upload_url, must be present after other variable definitions due to username, password
234
	$upload_url = "https://acb.netgate.com/save";
235
236
	if (!$encryptpw) {
237
		if (!file_exists("/cf/conf/autoconfigback.notice")) {
238
			$notice_text = "The encryption password is not set for Automatic Configuration Backup.";
239 587315d5 Steve Beaver
			$notice_text .= " Please correct this in Services -> AutoConfigBackup -> Settings.";
240 ce9eb0fb Steve Beaver
			log_error($notice_text);
241
			file_notice("AutoConfigBackup", $notice_text, $notice_text, "");
242 054f0ed0 Steve Beaver
			touch("/cf/conf/autoconfigback.notice");
243
		}
244
	} else {
245
		/* If configuration has changed, upload to pfS */
246
		if ($last_backup_date <> $last_config_change) {
247
248
			$notice_text = "Beginning configuration backup to ." . $upload_url;
249
			log_error($notice_text);
250
			update_filter_reload_status($notice_text);
251
252
			// Encrypt config.xml
253
			$data = file_get_contents("/cf/conf/config.xml");
254
			$raw_config_sha256_hash = trim(shell_exec("/sbin/sha256 /cf/conf/config.xml | /usr/bin/awk '{ print $4 }'"));
255
			$data = encrypt_data($data, $encryptpw);
256
			$tmpname = "/tmp/" . $raw_config_sha256_hash . ".tmp";
257
			tagfile_reformat($data, $data, "config.xml");
258
			file_put_contents($tmpname, $data);
259
260
			$post_fields = array(
261
				'reason' => htmlspecialchars($reason),
262
				'uid' => $uniqueID,
263
				'file' => curl_file_create($tmpname, 'image/jpg', 'config.jpg'),
264
				'userkey' => htmlspecialchars($userkey),
265
				'sha256_hash' => $raw_config_sha256_hash,
266
				'version' => $g['product_version'],
267 6f6299a3 Steve Beaver
				'hint' => $config['system']['acb']['hint'],
268
				'manmax' => $manmax
269 054f0ed0 Steve Beaver
			);
270
271
			// Check configuration into the ESF repo
272
			$curl_session = curl_init();
273
274
			curl_setopt($curl_session, CURLOPT_URL, "https://acb.netgate.com/save");
275
			curl_setopt($curl_session, CURLOPT_POST, count($post_fields));
276
			curl_setopt($curl_session, CURLOPT_POSTFIELDS, $post_fields);
277
			curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1);
278 ce9eb0fb Steve Beaver
			curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, 1);
279 054f0ed0 Steve Beaver
			curl_setopt($curl_session, CURLOPT_CONNECTTIMEOUT, 55);
280
			curl_setopt($curl_session, CURLOPT_TIMEOUT, 30);
281
			curl_setopt($curl_session, CURLOPT_USERAGENT, $g['product_name'] . '/' . rtrim(file_get_contents("/etc/version")));
282
			// Proxy
283
			curl_setopt_array($curl_session, configure_proxy());
284
285
			$data = curl_exec($curl_session);
286
287 c3c79c8b Renato Botelho
			unlink_if_exists($tmpname);
288 054f0ed0 Steve Beaver
289
			if (curl_errno($curl_session)) {
290
				$fd = fopen("/tmp/backupdebug.txt", "w");
291
				fwrite($fd, $upload_url . "" . $fields_string . "\n\n");
292
				fwrite($fd, $data);
293
				fwrite($fd, curl_error($curl_session));
294
				fclose($fd);
295
			} else {
296
				curl_close($curl_session);
297
			}
298
299
			if (strpos($data, "500") != false) {
300 ce9eb0fb Steve Beaver
				$notice_text = gettext("An error occurred while uploading your pfSense configuration to ") . $upload_url . " (" . htmlspecialchars($data) . ")";
301 054f0ed0 Steve Beaver
				log_error($notice_text . " - " . $data);
302 ce9eb0fb Steve Beaver
				file_notice("AutoConfigBackup", $notice_text);
303 054f0ed0 Steve Beaver
				update_filter_reload_status($notice_text);
304
				$input_errors["acb_upload"] = $notice_text;
305
			} else {
306
				// Update last pfS backup time
307
				$fd = fopen("/cf/conf/lastpfSbackup.txt", "w");
308
				fwrite($fd, $config['revision']['time']);
309
				fclose($fd);
310
				$notice_text = "End of configuration backup to " . $upload_url . " (success).";
311
				log_error($notice_text);
312
				update_filter_reload_status($notice_text);
313
			}
314
315
		} else {
316
			// Debugging
317
			//log_error("No https://portal.pfsense.org backup required.");
318
		}
319
	}
320
}