Project

General

Profile

Download (13.3 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-2021 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[] = sprintf(gettext(
209
							    "The uploaded file does not appear to contain an encrypted %s configuration."),
210
							    $g['product_label']);
211
						} else {
212
							$data = decrypt_data($data, $post['decrypt_password']);
213
							if (empty($data)) {
214
								$input_errors[] = gettext("File decryption failed. Incorrect password or file is invalid.");
215
							}
216
						}
217
					}
218

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

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

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

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

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

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

    
368
	return array("input_errors" => $input_errors, "savemsg" => $savemsg);
369
}
370

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

    
375
	cleanup_backupcache(false);
376

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

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

    
384
	return json_encode($backups);
385
}
386

    
387
?>
(2-2/6)