Project

General

Profile

Download (13.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * backup.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
/* Allow additional execution time 0 = no limit. */
29
ini_set('max_execution_time', '0');
30
ini_set('max_input_time', '0');
31

    
32
/* omit no-cache headers because it confuses IE with file downloads */
33
$omit_nocacheheaders = true;
34
require_once("config.gui.inc");
35
require_once("config.lib.inc");
36
require_once("functions.inc");
37
require_once("filter.inc");
38
require_once("shaper.inc");
39
require_once("pkg-utils.inc");
40

    
41
$rrddbpath = "/var/db/rrd";
42
$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
43

    
44
function rrd_data_xml() {
45
	global $rrddbpath;
46
	global $rrdtool;
47

    
48
	$result = "\t<rrddata>\n";
49
	$rrd_files = glob("{$rrddbpath}/*.rrd");
50
	$xml_files = array();
51
	foreach ($rrd_files as $rrd_file) {
52
		$basename = basename($rrd_file);
53
		$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
54
		exec("$rrdtool dump '{$rrd_file}' '{$xml_file}'");
55
		$xml_data = file_get_contents($xml_file);
56
		unlink($xml_file);
57
		if ($xml_data !== false) {
58
			$result .= "\t\t<rrddatafile>\n";
59
			$result .= "\t\t\t<filename>{$basename}</filename>\n";
60
			$result .= "\t\t\t<xmldata>" . base64_encode(gzdeflate($xml_data)) . "</xmldata>\n";
61
			$result .= "\t\t</rrddatafile>\n";
62
		}
63
	}
64
	$result .= "\t</rrddata>\n";
65
	return $result;
66
}
67

    
68
function backup_xmldatafile($tab=false, $type='voucher') {
69
	global $g;
70

    
71
	$xmldata_files = glob("{$g['backuppath'][$type]}");
72
	if (empty($xmldata_files)) {
73
		return;
74
	}
75
	$t = ($tab) ? "\t" : "";
76
	$result = "{$t}\t<{$type}data>\n";
77
	foreach ($xmldata_files as $xmldata_file) {
78
		$basename = basename($xmldata_file);
79
		$data = file_get_contents($xmldata_file);
80
		if ($data !== false) {
81
			$result .= "{$t}\t\t<xmldatafile>\n";
82
			$result .= "{$t}\t\t\t<filename>{$basename}</filename>\n";
83
			$result .= "{$t}\t\t\t<data>" . base64_encode(gzdeflate($data)) . "</data>\n";
84
			$result .= "{$t}\t\t</xmldatafile>\n";
85
		}
86
	}
87
	$result .= "{$t}\t</{$type}data>\n";
88

    
89
	return $result;
90
}
91

    
92
function check_and_returnif_section_exists($section) {
93
	global $config;
94
	if (is_array($config[$section])) {
95
		return true;
96
	}
97
	return false;
98
}
99

    
100
function execPost($post, $postfiles, $ui = true) {
101
	global $config, $g;
102

    
103
	unset($input_errors);
104

    
105
	if ($post['restore']) {
106
		$mode = "restore";
107
	} else if ($post['download']) {
108
		$mode = "download";
109
	}
110
	if ($post["nopackages"] <> "") {
111
		$options = "nopackages";
112
	}
113

    
114
	if ($mode) {
115
		if ($mode == "download") {
116
			if ($post['encrypt']) {
117
				if (!$post['encrypt_password'] || ($post['encrypt_password'] != $post['encrypt_password_confirm'])) {
118
					$input_errors[] = gettext("Supplied password and confirmation do not match.");
119
				}
120
			}
121

    
122
			if (!$input_errors) {
123
				$host = "{$config['system']['hostname']}.{$config['system']['domain']}";
124
				$name = "config-{$host}-".date("YmdHis").".xml";
125
				$data = "";
126

    
127
				if ($options == "nopackages") {
128
					if (!$post['backuparea']) {
129
						/* backup entire configuration */
130
						$data = file_get_contents("{$g['conf_path']}/config.xml");
131
					} else {
132
						/* backup specific area of configuration */
133
						$data = backup_config_section($post['backuparea']);
134
						$name = "{$post['backuparea']}-{$name}";
135
					}
136
					$data = preg_replace('/\t*<installedpackages>.*<\/installedpackages>\n/sm', '', $data);
137
				} else {
138
					if (!$post['backuparea']) {
139
						/* backup entire configuration */
140
						$data = file_get_contents("{$g['conf_path']}/config.xml");
141
					} else if ($post['backuparea'] === "rrddata") {
142
						$data = rrd_data_xml();
143
						$name = "{$post['backuparea']}-{$name}";
144
					} else if (array_key_exists($post['backuparea'], $g['backuppath']) && $post['backupdata']) {
145
						$data = backup_config_section($post['backuparea']);
146
						$data = clear_tagdata($post['backuparea'], $data);
147
						$dataxml = backup_xmldatafile(false, $post['backuparea']);
148
						$closing_tag = "</{$post['backuparea']}>";
149
						$data = str_replace($closing_tag, $dataxml . $closing_tag, $data);
150
						$name = "{$post['backuparea']}-{$name}";
151
					} else {
152
						/* backup specific area of configuration */
153
						$data = backup_config_section($post['backuparea']);
154
						$name = "{$post['backuparea']}-{$name}";
155
					}
156
				}
157

    
158
				if ($post['backuparea'] != "rrddata") {
159
					$data = clear_tagdata('rrd', $data);
160
				}
161

    
162
				if (!$post['backuparea'] && $post['backupdata']) {
163
					foreach ($g['backuppath'] as $bk => $path) {
164
						if (!empty($config[$bk])) {
165
							$data = clear_tagdata($bk, $data);
166
							$dataxml = backup_xmldatafile(true, $bk);
167
							$closing_tag = "\t</{$bk}>";
168
							$data = str_replace($closing_tag, $dataxml . $closing_tag, $data);
169
						}
170
					}
171
				}
172

    
173
				if (($post['backuparea'] != "rrddata") && !$post['donotbackuprrd']) {
174
					$rrd_data_xml = rrd_data_xml();
175
					$closing_tag = "</" . $g['xml_rootobj'] . ">";
176
					$data = str_replace($closing_tag, $rrd_data_xml . $closing_tag, $data);
177
				}
178

    
179
				if ($post['encrypt']) {
180
					$data = encrypt_data($data, $post['encrypt_password']);
181
					tagfile_reformat($data, $data, "config.xml");
182
				}
183

    
184
				if ($ui) {
185
					send_user_download('data', $data, $name);
186
				} else {
187
					return json_encode(array("contents" => base64_encode($data), "name" => $name));
188
				}
189
			}
190
		}
191

    
192
		if ($mode == "restore") {
193
			if ($post['decrypt']) {
194
				if (!$post['decrypt_password']) {
195
					$input_errors[] = gettext("A password for decryption must be supplied and confirmed.");
196
				}
197
			}
198

    
199
			if (!$input_errors) {
200
				if (!$ui || is_uploaded_file($postfiles['conffile']['tmp_name'])) {
201

    
202
					/* read the file contents */
203
					$data = $ui ? file_get_contents($postfiles['conffile']['tmp_name']) : $postfiles['conffile']['tmp_name'];
204
					if (!$data) {
205
						$input_errors[] = gettext("Warning, could not read file {$postfiles['conffile']['tmp_name']}");
206
					} elseif ($post['decrypt']) {
207
						if (!tagfile_deformat($data, $data, "config.xml")) {
208
							$input_errors[] = gettext("The uploaded file does not appear to contain an encrypted pfsense configuration.");
209
						} else {
210
							$data = decrypt_data($data, $post['decrypt_password']);
211
							if (empty($data)) {
212
								$input_errors[] = gettext("File decryption failed. Incorrect password or file is invalid.");
213
							}
214
						}
215
					}
216

    
217
					/* If the config on disk had empty rrddata tags, remove them to
218
					 * avoid an XML parsing error.
219
					 * See https://redmine.pfsense.org/issues/8994 */
220
					$data = preg_replace("/<rrddata><\\/rrddata>/", "", $data);
221
					$data = preg_replace("/<rrddata\\/>/", "", $data);
222

    
223
					if ($post['restorearea'] && !$input_errors) {
224
						/* restore a specific area of the configuration */
225
						if (!stristr($data, "<" . $post['restorearea'] . ">")) {
226
							$input_errors[] = gettext("An area to restore was selected but the correct xml tag could not be located.");
227
						} else {
228
							if (!restore_config_section($post['restorearea'], $data)) {
229
								$input_errors[] = gettext("An area to restore was selected but the correct xml tag could not be located.");
230
							} else {
231
								$conf_change = false;
232
								if ($config['rrddata']) {
233
									restore_rrddata();
234
									unset($config['rrddata']);
235
									$conf_change = true;
236
								}
237
								if (!empty($config[$post['restorearea']][$post['restorearea'].'data'])) {
238
									restore_xmldatafile($post['restorearea']);
239
									unset($config[$post['restorearea']][$post['restorearea'].'data']);
240
									$conf_change = true;
241
								}
242
								if ($conf_change) {
243
									write_config(sprintf(gettext("Unset RRD and extra data from configuration after restoring %s configuration area"), $post['restorearea']));
244
									unlink_if_exists("{$g['tmp_path']}/config.cache");
245
									convert_config();
246
								}
247
								filter_configure();
248
								$savemsg = gettext("The configuration area has been restored. The firewall may need to be rebooted.");
249
							}
250
						}
251
					} elseif (!$input_errors) {
252
						if (!stristr($data, "<" . $g['xml_rootobj'] . ">")) {
253
							$input_errors[] = sprintf(gettext("A full configuration restore was selected but a %s tag could not be located."), $g['xml_rootobj']);
254
						} else {
255
							/* restore the entire configuration */
256
							file_put_contents($postfiles['conffile']['tmp_name'], $data);
257
							if (config_install($postfiles['conffile']['tmp_name']) == 0) {
258
								/* Save current pkg repo to re-add on new config */
259
								unset($pkg_repo_conf_path);
260
								if (isset($config['system']['pkg_repo_conf_path'])) {
261
									$pkg_repo_conf_path = $config['system']['pkg_repo_conf_path'];
262
								}
263

    
264
								/* this will be picked up by /index.php */
265
								mark_subsystem_dirty("restore");
266
								touch("/conf/needs_package_sync");
267
								/* remove cache, we will force a config reboot */
268
								if (file_exists("{$g['tmp_path']}/config.cache")) {
269
									unlink("{$g['tmp_path']}/config.cache");
270
								}
271
								$config = parse_config(true);
272

    
273
								/* Restore previously pkg repo configured */
274
								$pkg_repo_restored = false;
275
								if (isset($pkg_repo_conf_path)) {
276
									$config['system']['pkg_repo_conf_path'] =
277
									    $pkg_repo_conf_path;
278
									$pkg_repo_restored = true;
279
								} elseif (isset($config['system']['pkg_repo_conf_path'])) {
280
									unset($config['system']['pkg_repo_conf_path']);
281
									$pkg_repo_restored = true;
282
								}
283

    
284
								if ($pkg_repo_restored) {
285
									write_config(gettext("Removing pkg repository set after restoring full configuration"));
286
									pkg_update(true);
287
								}
288

    
289
								if (file_exists("/boot/loader.conf")) {
290
									$loaderconf = file_get_contents("/boot/loader.conf");
291
									if (strpos($loaderconf, "console=\"comconsole") ||
292
									    strpos($loaderconf, "boot_serial=\"YES")) {
293
										$config['system']['enableserial'] = true;
294
										write_config(gettext("Restore serial console enabling in configuration."));
295
									}
296
									unset($loaderconf);
297
								}
298
								if (file_exists("/boot/loader.conf.local")) {
299
									$loaderconf = file_get_contents("/boot/loader.conf.local");
300
									if (strpos($loaderconf, "console=\"comconsole") ||
301
									    strpos($loaderconf, "boot_serial=\"YES")) {
302
										$config['system']['enableserial'] = true;
303
										write_config(gettext("Restore serial console enabling in configuration."));
304
									}
305
									unset($loaderconf);
306
								}
307
								/* extract out rrd items, unset from $config when done */
308
								$conf_change = false;
309
								if ($config['rrddata']) {
310
									restore_rrddata();
311
									unset($config['rrddata']);
312
									$conf_change = true;
313
								}
314
								foreach ($g['backuppath'] as $bk => $path) {
315
									if (!empty($config[$bk][$bk.'data'])) {
316
										restore_xmldatafile($bk);
317
										unset($config[$bk][$bk.'data']);
318
										$conf_change = true;
319
									}
320
								}
321
								if ($conf_change) {
322
									write_config(gettext("Unset RRD and extra data from configuration after full restore."));
323
									unlink_if_exists("{$g['tmp_path']}/config.cache");
324
									convert_config();
325
								}
326
								if (is_array($config['captiveportal'])) {
327
									foreach ($config['captiveportal'] as $cp) {
328
										if (isset($cp['enable'])) {
329
											/* for some reason ipfw doesn't init correctly except on bootup sequence */
330
											mark_subsystem_dirty("restore");
331
											break;
332
										}
333
									}
334
								}
335
								console_configure();
336
								if (is_interface_mismatch() == true) {
337
									touch("/var/run/interface_mismatch_reboot_needed");
338
									clear_subsystem_dirty("restore");
339
									convert_config();
340
									if ($ui) {
341
										header("Location: interfaces_assign.php");
342
									}
343
									exit;
344
								}
345
								if (is_interface_vlan_mismatch() == true) {
346
									touch("/var/run/interface_mismatch_reboot_needed");
347
									clear_subsystem_dirty("restore");
348
									convert_config();
349
									if ($ui) {
350
										header("Location: interfaces_assign.php");
351
									}
352
									exit;
353
								}
354
							} else {
355
								$input_errors[] = gettext("The configuration could not be restored.");
356
							}
357
						}
358
					}
359
				} else {
360
					$input_errors[] = gettext("The configuration could not be restored (file upload error).");
361
				}
362
			}
363
		}
364
	}
365

    
366
	return array("input_errors" => $input_errors, "savemsg" => $savemsg);
367
}
368

    
369
// Compose a list of recent backups formatted as a JSON array
370
function listBackupsJSON() {
371
	global $g;
372

    
373
	cleanup_backupcache(false);
374

    
375
	$raw = unserialize(file_get_contents($g["cf_conf_path"] . "/backup/backup.cache"));
376

    
377
	$backups = array();
378
	foreach($raw as $key => $value) {
379
	    $backups[] = array("time" => $key, "desc" => $value['description'], "size" => $value['filesize'], "vers" => $value['version']);
380
	}
381

    
382
	return json_encode($backups);
383
}
384

    
385
?>
(2-2/2)