Project

General

Profile

Download (24.6 KB) Statistics
| Branch: | Tag: | Revision:
1 8ccc8f1a Scott Ullrich
<?php
2 5b237745 Scott Ullrich
/*
3 c5d81585 Renato Botelho
 * diag_backup.php
4 9da2cf1c Stephen Beaver
 *
5 c5d81585 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 38809d47 Renato Botelho do Couto
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8 0284d79e jim-p
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9 c5d81585 Renato Botelho
 * All rights reserved.
10 fd9ebcd5 Stephen Beaver
 *
11 c5d81585 Renato Botelho
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14 fd9ebcd5 Stephen Beaver
 *
15 b12ea3fb Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 *
19 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
20 fd9ebcd5 Stephen Beaver
 *
21 b12ea3fb Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 */
27 5b237745 Scott Ullrich
28 6b07c15a Matthew Grooms
##|+PRIV
29 5d916fc7 Stephen Beaver
##|*IDENT=page-diagnostics-backup-restore
30 0f298138 k-paulius
##|*NAME=Diagnostics: Backup & Restore
31
##|*DESCR=Allow access to the 'Diagnostics: Backup & Restore' page.
32 57188e47 Phil Davis
##|*WARN=standard-warning-root
33 6b07c15a Matthew Grooms
##|*MATCH=diag_backup.php*
34
##|-PRIV
35
36 47d11b79 Mark Crane
/* Allow additional execution time 0 = no limit. */
37 3420028c Scott Ullrich
ini_set('max_execution_time', '0');
38
ini_set('max_input_time', '0');
39 47d11b79 Mark Crane
40 5b237745 Scott Ullrich
/* omit no-cache headers because it confuses IE with file downloads */
41
$omit_nocacheheaders = true;
42 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
43 7a927e67 Scott Ullrich
require_once("functions.inc");
44
require_once("filter.inc");
45
require_once("shaper.inc");
46 4be5ed9f Renato Botelho
require_once("pkg-utils.inc");
47 5b237745 Scott Ullrich
48 8bdb6879 Darren Embry
$rrddbpath = "/var/db/rrd";
49
$rrdtool = "/usr/bin/nice -n20 /usr/local/bin/rrdtool";
50
51
function rrd_data_xml() {
52
	global $rrddbpath;
53
	global $rrdtool;
54 73ed069b Renato Botelho
55 8bdb6879 Darren Embry
	$result = "\t<rrddata>\n";
56
	$rrd_files = glob("{$rrddbpath}/*.rrd");
57
	$xml_files = array();
58
	foreach ($rrd_files as $rrd_file) {
59
		$basename = basename($rrd_file);
60
		$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
61
		exec("$rrdtool dump '{$rrd_file}' '{$xml_file}'");
62
		$xml_data = file_get_contents($xml_file);
63
		unlink($xml_file);
64
		if ($xml_data !== false) {
65
			$result .= "\t\t<rrddatafile>\n";
66
			$result .= "\t\t\t<filename>{$basename}</filename>\n";
67
			$result .= "\t\t\t<xmldata>" . base64_encode(gzdeflate($xml_data)) . "</xmldata>\n";
68
			$result .= "\t\t</rrddatafile>\n";
69
		}
70
	}
71
	$result .= "\t</rrddata>\n";
72
	return $result;
73
}
74
75 754b75d0 Darren Embry
function restore_rrddata() {
76 7eead1fc Darren Embry
	global $config, $g, $rrdtool, $input_errors;
77 5f601060 Phil Davis
	foreach ($config['rrddata']['rrddatafile'] as $rrd) {
78 754b75d0 Darren Embry
		if ($rrd['xmldata']) {
79
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
80
			$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
81 08877746 Darren Embry
			if (file_put_contents($xml_file, gzinflate(base64_decode($rrd['xmldata']))) === false) {
82 ff30e319 bruno
				log_error(sprintf(gettext("Cannot write %s"), $xml_file));
83 08877746 Darren Embry
				continue;
84
			}
85
			$output = array();
86
			$status = null;
87
			exec("$rrdtool restore -f '{$xml_file}' '{$rrd_file}'", $output, $status);
88
			if ($status) {
89 f757431d Darren Embry
				log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
90 08877746 Darren Embry
				continue;
91
			}
92 754b75d0 Darren Embry
			unlink($xml_file);
93 5f601060 Phil Davis
		} else if ($rrd['data']) {
94 7eead1fc Darren Embry
			$rrd_file = "{$g['vardb_path']}/rrd/{$rrd['filename']}";
95
			$rrd_fd = fopen($rrd_file, "w");
96 08877746 Darren Embry
			if (!$rrd_fd) {
97 ff30e319 bruno
				log_error(sprintf(gettext("Cannot write %s"), $rrd_file));
98 08877746 Darren Embry
				continue;
99
			}
100 754b75d0 Darren Embry
			$data = base64_decode($rrd['data']);
101
			/* Try to decompress the data. */
102
			$dcomp = @gzinflate($data);
103
			if ($dcomp) {
104
				/* If the decompression worked, write the decompressed data */
105 08877746 Darren Embry
				if (fwrite($rrd_fd, $dcomp) === false) {
106 ff30e319 bruno
					log_error(sprintf(gettext("fwrite %s failed"), $rrd_file));
107 08877746 Darren Embry
					continue;
108
				}
109 754b75d0 Darren Embry
			} else {
110
				/* If the decompression failed, it wasn't compressed, so write raw data */
111 08877746 Darren Embry
				if (fwrite($rrd_fd, $data) === false) {
112 ff30e319 bruno
					log_error(sprintf(gettext("fwrite %s failed"), $rrd_file));
113 08877746 Darren Embry
					continue;
114
				}
115
			}
116
			if (fclose($rrd_fd) === false) {
117 ff30e319 bruno
				log_error(sprintf(gettext("fclose %s failed"), $rrd_file));
118 08877746 Darren Embry
				continue;
119 754b75d0 Darren Embry
			}
120
		}
121
	}
122
}
123
124 f21d4f79 Viktor G
function voucher_data_xml($tab=false) {
125
	global $g;
126
127
	$voucher_files = glob("{$g['vardb_path']}/voucher_*.db");
128
	if (empty($voucher_files)) {
129
		return;
130
	}
131
	$t = ($tab) ? "\t" : "";
132
	$result = "\t<voucherdata>\n";
133
	foreach ($voucher_files as $voucher_file) {
134
		$basename = basename($voucher_file);
135
		$db_data = file_get_contents($voucher_file);
136
		if ($db_data !== false) {
137
			$result .= "{$t}\t\t<voucherdbfile>\n";
138
			$result .= "{$t}\t\t\t<filename>{$basename}</filename>\n";
139
			$result .= "{$t}\t\t\t<dbdata>" . base64_encode(gzdeflate($db_data)) . "</dbdata>\n";
140
			$result .= "{$t}\t\t</voucherdbfile>\n";
141
		}
142
	}
143
	$result .= "{$t}\t</voucherdata>\n";
144
145
	return $result;
146
}
147
148
function restore_voucher_data() {
149
	global $config, $g;
150
	foreach ($config['voucher']['voucherdata']['voucherdbfile'] as $db) {
151
		$voucherdb_file = "{$g['vardb_path']}/{$db['filename']}";
152
		if (file_put_contents($voucherdb_file, gzinflate(base64_decode($db['dbdata']))) === false) {
153
			log_error(sprintf(gettext("Cannot write %s"), $voucherdb_file));
154
			continue;
155
		}
156
	}
157
}
158
159 645ec835 Scott Ullrich
function remove_bad_chars($string) {
160 699737d9 Phil Davis
	return preg_replace('/[^a-z_0-9]/i', '', $string);
161 645ec835 Scott Ullrich
}
162
163 b3277798 Scott Ullrich
function check_and_returnif_section_exists($section) {
164
	global $config;
165 5f601060 Phil Davis
	if (is_array($config[$section])) {
166 b3277798 Scott Ullrich
		return true;
167 5f601060 Phil Davis
	}
168 b3277798 Scott Ullrich
	return false;
169
}
170
171 da17d77e Ermal Lu?i
if ($_POST['apply']) {
172 73ed069b Renato Botelho
	ob_flush();
173
	flush();
174 c9a19238 Darren Embry
	clear_subsystem_dirty("restore");
175 73ed069b Renato Botelho
	exit;
176 da17d77e Ermal Lu?i
}
177
178 5b237745 Scott Ullrich
if ($_POST) {
179
	unset($input_errors);
180 9a7e1c95 Phil Davis
	if ($_POST['restore']) {
181 5b237745 Scott Ullrich
		$mode = "restore";
182 9a7e1c95 Phil Davis
	} else if ($_POST['reinstallpackages']) {
183 8ccc8f1a Scott Ullrich
		$mode = "reinstallpackages";
184 9a7e1c95 Phil Davis
	} else if ($_POST['clearpackagelock']) {
185 039cb920 jim-p
		$mode = "clearpackagelock";
186 9a7e1c95 Phil Davis
	} else if ($_POST['download']) {
187 5b237745 Scott Ullrich
		$mode = "download";
188 5f601060 Phil Davis
	}
189
	if ($_POST["nopackages"] <> "") {
190 528cad39 Colin Smith
		$options = "nopackages";
191 5f601060 Phil Davis
	}
192 5b237745 Scott Ullrich
	if ($mode) {
193
		if ($mode == "download") {
194 8ff5ffcc Matthew Grooms
			if ($_POST['encrypt']) {
195 06a1ba14 Viktor G
				if (!$_POST['encrypt_password'] || ($_POST['encrypt_password'] != $_POST['encrypt_password_confirm'])) {
196
					$input_errors[] = gettext("Supplied password and confirmation do not match.");
197 5f601060 Phil Davis
				}
198 528cad39 Colin Smith
			}
199 73ed069b Renato Botelho
200 8ff5ffcc Matthew Grooms
			if (!$input_errors) {
201 73ed069b Renato Botelho
202 07b1797b sullrich
				//$lockbckp = lock('config');
203 73ed069b Renato Botelho
204 8ff5ffcc Matthew Grooms
				$host = "{$config['system']['hostname']}.{$config['system']['domain']}";
205
				$name = "config-{$host}-".date("YmdHis").".xml";
206
				$data = "";
207 73ed069b Renato Botelho
208 5f601060 Phil Davis
				if ($options == "nopackages") {
209
					if (!$_POST['backuparea']) {
210 4195c967 sullrich
						/* backup entire configuration */
211
						$data = file_get_contents("{$g['conf_path']}/config.xml");
212
					} else {
213
						/* backup specific area of configuration */
214
						$data = backup_config_section($_POST['backuparea']);
215
						$name = "{$_POST['backuparea']}-{$name}";
216
					}
217 79955411 Renato Botelho
					$data = preg_replace('/\t*<installedpackages>.*<\/installedpackages>\n/sm', '', $data);
218 8ff5ffcc Matthew Grooms
				} else {
219 5f601060 Phil Davis
					if (!$_POST['backuparea']) {
220 8ff5ffcc Matthew Grooms
						/* backup entire configuration */
221
						$data = file_get_contents("{$g['conf_path']}/config.xml");
222 7eead1fc Darren Embry
					} else if ($_POST['backuparea'] === "rrddata") {
223
						$data = rrd_data_xml();
224
						$name = "{$_POST['backuparea']}-{$name}";
225 f21d4f79 Viktor G
					} else if ($_POST['backuparea'] === "voucher") {
226
						$data = backup_config_section($_POST['backuparea']);
227
						$voucher_data_xml = voucher_data_xml();
228
						$closing_tag = "</voucher>";
229
						$data = str_replace($closing_tag, $voucher_data_xml . $closing_tag, $data);
230
						$name = "{$_POST['backuparea']}-{$name}";
231 8ff5ffcc Matthew Grooms
					} else {
232
						/* backup specific area of configuration */
233
						$data = backup_config_section($_POST['backuparea']);
234
						$name = "{$_POST['backuparea']}-{$name}";
235
					}
236 181462b5 Scott Ullrich
				}
237 73ed069b Renato Botelho
238 07b1797b sullrich
				//unlock($lockbckp);
239 73ed069b Renato Botelho
240 a250f179 Renato Botelho
				/*
241 0e834fc5 Stephen Beaver
				 *	Backup RRD Data
242 1390b049 Scott Ullrich
				 */
243 6c1b20af Viktor G
244
				/* If the config on disk had rrddata tags already, remove that section first.
245 4213d677 jim-p
				 * See https://redmine.pfsense.org/issues/8994 and
246
				 *     https://redmine.pfsense.org/issues/10508 */
247
				$data = preg_replace("/[[:blank:]]*<rrddata>.*<\\/rrddata>[[:blank:]]*\n*/s", "", $data);
248
				$data = preg_replace("/[[:blank:]]*<rrddata\\/>[[:blank:]]*\n*/", "", $data);
249 6c1b20af Viktor G
250 f21d4f79 Viktor G
				if (!$_POST['backuparea'] && !empty($config['voucher'])) {
251
					$voucher_data_xml = voucher_data_xml(true);
252
					$closing_tag = "</voucher>";
253
					$data = str_replace($closing_tag, $voucher_data_xml . $closing_tag, $data);
254
				}
255
256 7eead1fc Darren Embry
				if ($_POST['backuparea'] !== "rrddata" && !$_POST['donotbackuprrd']) {
257 8bdb6879 Darren Embry
					$rrd_data_xml = rrd_data_xml();
258
					$closing_tag = "</" . $g['xml_rootobj'] . ">";
259
					$data = str_replace($closing_tag, $rrd_data_xml . $closing_tag, $data);
260 1390b049 Scott Ullrich
				}
261 73ed069b Renato Botelho
262 4cfd2390 Renato Botelho
				if ($_POST['encrypt']) {
263
					$data = encrypt_data($data, $_POST['encrypt_password']);
264
					tagfile_reformat($data, $data, "config.xml");
265
				}
266
267 ecb594d0 jim-p
				send_user_download('data', $data, $name);
268 8ff5ffcc Matthew Grooms
			}
269
		}
270 73ed069b Renato Botelho
271 8ff5ffcc Matthew Grooms
		if ($mode == "restore") {
272
			if ($_POST['decrypt']) {
273 76d6d925 Stephen Beaver
				if (!$_POST['decrypt_password']) {
274 89854d71 NewEraCracker
					$input_errors[] = gettext("A password for decryption must be supplied and confirmed.");
275 5f601060 Phil Davis
				}
276 8ff5ffcc Matthew Grooms
			}
277 73ed069b Renato Botelho
278 8ff5ffcc Matthew Grooms
			if (!$input_errors) {
279
				if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
280 73ed069b Renato Botelho
281 8ff5ffcc Matthew Grooms
					/* read the file contents */
282
					$data = file_get_contents($_FILES['conffile']['tmp_name']);
283 5f601060 Phil Davis
					if (!$data) {
284 153f78af Viktor Gurov
						$input_errors[] = gettext("Warning, could not read file {$_FILES['conffile']['tmp_name']}");
285
					} elseif ($_POST['decrypt']) {
286 8ff5ffcc Matthew Grooms
						if (!tagfile_deformat($data, $data, "config.xml")) {
287 205da5a0 Neriberto C.Prado
							$input_errors[] = gettext("The uploaded file does not appear to contain an encrypted pfsense configuration.");
288 153f78af Viktor Gurov
						} else {
289
							$data = decrypt_data($data, $_POST['decrypt_password']);
290
							if (empty($data)) {
291
								$input_errors[] = gettext("File decryption failed. Incorrect password or file is invalid.");
292
							}
293 8ff5ffcc Matthew Grooms
						}
294
					}
295 5f601060 Phil Davis
					if (stristr($data, "<m0n0wall>")) {
296 205da5a0 Neriberto C.Prado
						log_error(gettext("Upgrading m0n0wall configuration to pfsense."));
297 8ff5ffcc Matthew Grooms
						/* m0n0wall was found in config.  convert it. */
298
						$data = str_replace("m0n0wall", "pfsense", $data);
299
						$m0n0wall_upgrade = true;
300
					}
301 93867844 jim-p
302
					/* If the config on disk had empty rrddata tags, remove them to
303
					 * avoid an XML parsing error.
304
					 * See https://redmine.pfsense.org/issues/8994 */
305
					$data = preg_replace("/<rrddata><\\/rrddata>/", "", $data);
306
					$data = preg_replace("/<rrddata\\/>/", "", $data);
307
308 153f78af Viktor Gurov
					if ($_POST['restorearea'] && !$input_errors) {
309 8ff5ffcc Matthew Grooms
						/* restore a specific area of the configuration */
310 5f601060 Phil Davis
						if (!stristr($data, "<" . $_POST['restorearea'] . ">")) {
311 b6078bed NOYB
							$input_errors[] = gettext("An area to restore was selected but the correct xml tag could not be located.");
312 8ff5ffcc Matthew Grooms
						} else {
313 8dcca9b5 Darren Embry
							if (!restore_config_section($_POST['restorearea'], $data)) {
314 b6078bed NOYB
								$input_errors[] = gettext("An area to restore was selected but the correct xml tag could not be located.");
315 8dcca9b5 Darren Embry
							} else {
316 f21d4f79 Viktor G
								if ($config['rrddata'] || $config['voucher']['voucherdata']) {
317
									if ($config['rrddata']) {
318
										restore_rrddata();
319
										unset($config['rrddata']);
320
										write_config(sprintf(gettext("Unset RRD data from configuration after restoring %s configuration area"), $_POST['restorearea']));
321
									}
322
									if (($_POST['restorearea'] == 'voucher') && $config['voucher']['voucherdata']) {
323
										restore_voucher_data();
324
										unset($config['voucher']['voucherdata']);
325
										write_config(sprintf(gettext("Unset Voucher data from configuration after restoring %s configuration area"), $_POST['restorearea']));
326
									}
327 8dcca9b5 Darren Embry
									unlink_if_exists("{$g['tmp_path']}/config.cache");
328
									convert_config();
329
								}
330
								filter_configure();
331 b6078bed NOYB
								$savemsg = gettext("The configuration area has been restored. The firewall may need to be rebooted.");
332 7eead1fc Darren Embry
							}
333 8ff5ffcc Matthew Grooms
						}
334 153f78af Viktor Gurov
					} elseif (!$input_errors) {
335 5f601060 Phil Davis
						if (!stristr($data, "<" . $g['xml_rootobj'] . ">")) {
336 b6078bed NOYB
							$input_errors[] = sprintf(gettext("A full configuration restore was selected but a %s tag could not be located."), $g['xml_rootobj']);
337 8ff5ffcc Matthew Grooms
						} else {
338
							/* restore the entire configuration */
339
							file_put_contents($_FILES['conffile']['tmp_name'], $data);
340
							if (config_install($_FILES['conffile']['tmp_name']) == 0) {
341 0430b1b4 Renato Botelho
								/* Save current pkg repo to re-add on new config */
342
								unset($pkg_repo_conf_path);
343
								if (isset($config['system']['pkg_repo_conf_path'])) {
344
									$pkg_repo_conf_path = $config['system']['pkg_repo_conf_path'];
345
								}
346
347 8ff5ffcc Matthew Grooms
								/* this will be picked up by /index.php */
348 da17d77e Ermal Lu?i
								mark_subsystem_dirty("restore");
349 10511c3b Renato Botelho
								touch("/conf/needs_package_sync");
350 8ff5ffcc Matthew Grooms
								/* remove cache, we will force a config reboot */
351 5f601060 Phil Davis
								if (file_exists("{$g['tmp_path']}/config.cache")) {
352 da17d77e Ermal Lu?i
									unlink("{$g['tmp_path']}/config.cache");
353 5f601060 Phil Davis
								}
354 8ff5ffcc Matthew Grooms
								$config = parse_config(true);
355 0430b1b4 Renato Botelho
356
								/* Restore previously pkg repo configured */
357
								$pkg_repo_restored = false;
358
								if (isset($pkg_repo_conf_path)) {
359
									$config['system']['pkg_repo_conf_path'] =
360
									    $pkg_repo_conf_path;
361
									$pkg_repo_restored = true;
362
								} elseif (isset($config['system']['pkg_repo_conf_path'])) {
363
									unset($config['system']['pkg_repo_conf_path']);
364
									$pkg_repo_restored = true;
365
								}
366
367
								if ($pkg_repo_restored) {
368
									write_config(gettext("Removing pkg repository set after restoring full configuration"));
369 4be5ed9f Renato Botelho
									pkg_update(true);
370 0430b1b4 Renato Botelho
								}
371
372 0174c480 Ermal LUÇI
								if (file_exists("/boot/loader.conf")) {
373
									$loaderconf = file_get_contents("/boot/loader.conf");
374 c91af4ac Luiz Souza
									if (strpos($loaderconf, "console=\"comconsole") ||
375 e0b32eb9 jim-p
									    strpos($loaderconf, "boot_serial=\"YES")) {
376 4ff3adec Luiz Souza
										$config['system']['enableserial'] = true;
377
										write_config(gettext("Restore serial console enabling in configuration."));
378
									}
379
									unset($loaderconf);
380
								}
381
								if (file_exists("/boot/loader.conf.local")) {
382
									$loaderconf = file_get_contents("/boot/loader.conf.local");
383 c91af4ac Luiz Souza
									if (strpos($loaderconf, "console=\"comconsole") ||
384 e0b32eb9 jim-p
									    strpos($loaderconf, "boot_serial=\"YES")) {
385 0174c480 Ermal LUÇI
										$config['system']['enableserial'] = true;
386 ff30e319 bruno
										write_config(gettext("Restore serial console enabling in configuration."));
387 0174c480 Ermal LUÇI
									}
388 5d4b8830 Ermal LUÇI
									unset($loaderconf);
389 0174c480 Ermal LUÇI
								}
390 67bc955d Chris Buechler
								/* extract out rrd items, unset from $config when done */
391 f21d4f79 Viktor G
								if ($config['rrddata'] || $config['voucher']['voucherdata']) {
392
									if ($config['rrddata']) {
393
										restore_rrddata();
394
										unset($config['rrddata']);
395
									}
396
									if ($config['voucher']['voucherdata']) {
397
										restore_voucher_data();
398
										unset($config['voucher']['voucherdata']);
399
									}
400
									write_config(gettext("Unset RRD and Voucher data from configuration after full restore."));
401 da17d77e Ermal Lu?i
									unlink_if_exists("{$g['tmp_path']}/config.cache");
402 d442e4e2 Scott Ullrich
									convert_config();
403 1390b049 Scott Ullrich
								}
404 5f601060 Phil Davis
								if ($m0n0wall_upgrade == true) {
405
									if ($config['system']['gateway'] <> "") {
406 8ff5ffcc Matthew Grooms
										$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
407 5f601060 Phil Davis
									}
408 8ff5ffcc Matthew Grooms
									unset($config['shaper']);
409
									/* optional if list */
410 80fe8369 Phil Davis
									$ifdescrs = get_configured_interface_list(true);
411 8ff5ffcc Matthew Grooms
									/* remove special characters from interface descriptions */
412 5f601060 Phil Davis
									if (is_array($ifdescrs)) {
413
										foreach ($ifdescrs as $iface) {
414 8ff5ffcc Matthew Grooms
											$config['interfaces'][$iface]['descr'] = remove_bad_chars($config['interfaces'][$iface]['descr']);
415 5f601060 Phil Davis
										}
416
									}
417 b6db8ea3 sullrich
									/* check for interface names with an alias */
418 5f601060 Phil Davis
									if (is_array($ifdescrs)) {
419
										foreach ($ifdescrs as $iface) {
420
											if (is_alias($config['interfaces'][$iface]['descr'])) {
421 b6db8ea3 sullrich
												$origname = $config['interfaces'][$iface]['descr'];
422 24807bfe Phil Davis
												update_alias_name($origname . "Alias", $origname);
423 b6db8ea3 sullrich
											}
424
										}
425
									}
426 da17d77e Ermal Lu?i
									unlink_if_exists("{$g['tmp_path']}/config.cache");
427 888e7a27 Scott Ullrich
									// Reset configuration version to something low
428 a250f179 Renato Botelho
									// in order to force the config upgrade code to
429 888e7a27 Scott Ullrich
									// run through with all steps that are required.
430
									$config['system']['version'] = "1.0";
431 798cb31a Scott Ullrich
									// Deal with descriptions longer than 63 characters
432
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
433 5f601060 Phil Davis
										if (count($config['filter']['rule'][$i]['descr']) > 63) {
434 798cb31a Scott Ullrich
											$config['filter']['rule'][$i]['descr'] = substr($config['filter']['rule'][$i]['descr'], 0, 63);
435 5f601060 Phil Davis
										}
436 798cb31a Scott Ullrich
									}
437
									// Move interface from ipsec to enc0
438
									for ($i = 0; isset($config["filter"]["rule"][$i]); $i++) {
439 5f601060 Phil Davis
										if ($config['filter']['rule'][$i]['interface'] == "ipsec") {
440 798cb31a Scott Ullrich
											$config['filter']['rule'][$i]['interface'] = "enc0";
441 5f601060 Phil Davis
										}
442 798cb31a Scott Ullrich
									}
443
									// Convert icmp types
444
									// http://www.openbsd.org/cgi-bin/man.cgi?query=icmp&sektion=4&arch=i386&apropos=0&manpath=OpenBSD+Current
445 0ce1667b stilez
									$convert = array('echo' => 'echoreq', 'timest' => 'timereq', 'timestrep' => 'timerep');
446
									foreach ($config["filter"]["rule"] as $ruleid => &$ruledata) {
447
										if ($convert[$ruledata['icmptype']]) {
448
											$ruledata['icmptype'] = $convert[$ruledata['icmptype']];
449 798cb31a Scott Ullrich
										}
450 dec5cb85 Scott Ullrich
									}
451
									$config['diag']['ipv6nat'] = true;
452 919a43a7 doktornotor
									write_config(gettext("Imported m0n0wall configuration"));
453 d442e4e2 Scott Ullrich
									convert_config();
454 205da5a0 Neriberto C.Prado
									$savemsg = gettext("The m0n0wall configuration has been restored and upgraded to pfSense.");
455 da17d77e Ermal Lu?i
									mark_subsystem_dirty("restore");
456 8ff5ffcc Matthew Grooms
								}
457 5f601060 Phil Davis
								if (is_array($config['captiveportal'])) {
458
									foreach ($config['captiveportal'] as $cp) {
459 2e19a683 Renato Botelho
										if (isset($cp['enable'])) {
460
											/* for some reason ipfw doesn't init correctly except on bootup sequence */
461
											mark_subsystem_dirty("restore");
462
											break;
463
										}
464
									}
465 8ff5ffcc Matthew Grooms
								}
466 749dfdb7 Luiz Souza
								console_configure();
467 5f601060 Phil Davis
								if (is_interface_mismatch() == true) {
468 8ff5ffcc Matthew Grooms
									touch("/var/run/interface_mismatch_reboot_needed");
469 da17d77e Ermal Lu?i
									clear_subsystem_dirty("restore");
470 f5a57bb0 Scott Ullrich
									convert_config();
471 8ff5ffcc Matthew Grooms
									header("Location: interfaces_assign.php");
472 bafaf123 Scott Ullrich
									exit;
473 8ff5ffcc Matthew Grooms
								}
474 66bcba1b Ermal
								if (is_interface_vlan_mismatch() == true) {
475
									touch("/var/run/interface_mismatch_reboot_needed");
476
									clear_subsystem_dirty("restore");
477
									convert_config();
478
									header("Location: interfaces_assign.php");
479
									exit;
480
								}
481 8ff5ffcc Matthew Grooms
							} else {
482 205da5a0 Neriberto C.Prado
								$input_errors[] = gettext("The configuration could not be restored.");
483 db3996df Scott Ullrich
							}
484 9c72b993 Scott Ullrich
						}
485 181462b5 Scott Ullrich
					}
486 8ff5ffcc Matthew Grooms
				} else {
487 205da5a0 Neriberto C.Prado
					$input_errors[] = gettext("The configuration could not be restored (file upload error).");
488 db3996df Scott Ullrich
				}
489 5b237745 Scott Ullrich
			}
490 8ff5ffcc Matthew Grooms
		}
491 73ed069b Renato Botelho
492 8ff5ffcc Matthew Grooms
		if ($mode == "reinstallpackages") {
493 8ccc8f1a Scott Ullrich
			header("Location: pkg_mgr_install.php?mode=reinstallall");
494
			exit;
495 039cb920 jim-p
		} else if ($mode == "clearpackagelock") {
496
			clear_subsystem_dirty('packagelock');
497 8545adde k-paulius
			$savemsg = "Package lock cleared.";
498 5b237745 Scott Ullrich
		}
499
	}
500
}
501 6a1e6651 Bill Marquette
502 02e1170d Scott Ullrich
$id = rand() . '.' . time();
503
504
$mth = ini_get('upload_progress_meter.store_method');
505
$dir = ini_get('upload_progress_meter.file.filename_template');
506
507 a4af095c Renato Botelho
function build_area_list($showall) {
508
	global $config;
509 b63695db Scott Ullrich
510 0e834fc5 Stephen Beaver
	$areas = array(
511
		"aliases" => gettext("Aliases"),
512 a4af095c Renato Botelho
		"captiveportal" => gettext("Captive Portal"),
513
		"voucher" => gettext("Captive Portal Vouchers"),
514
		"dnsmasq" => gettext("DNS Forwarder"),
515
		"unbound" => gettext("DNS Resolver"),
516
		"dhcpd" => gettext("DHCP Server"),
517
		"dhcpdv6" => gettext("DHCPv6 Server"),
518 fb3b20c3 Viktor G
		"dyndnses" => gettext("Dynamic DNS"),
519 a4af095c Renato Botelho
		"filter" => gettext("Firewall Rules"),
520
		"interfaces" => gettext("Interfaces"),
521
		"ipsec" => gettext("IPSEC"),
522 b9d689e7 Viktor G
		"dnshaper" => gettext("Limiters"),
523 a4af095c Renato Botelho
		"nat" => gettext("NAT"),
524
		"openvpn" => gettext("OpenVPN"),
525
		"installedpackages" => gettext("Package Manager"),
526
		"rrddata" => gettext("RRD Data"),
527
		"cron" => gettext("Scheduled Tasks"),
528
		"syslog" => gettext("Syslog"),
529
		"system" => gettext("System"),
530
		"staticroutes" => gettext("Static routes"),
531
		"sysctl" => gettext("System tunables"),
532
		"snmpd" => gettext("SNMP Server"),
533
		"shaper" => gettext("Traffic Shaper"),
534
		"vlans" => gettext("VLANS"),
535 7ca42d47 k-paulius
		"wol" => gettext("Wake-on-LAN")
536 a4af095c Renato Botelho
		);
537 0e834fc5 Stephen Beaver
538
	$list = array("" => gettext("All"));
539
540 288a2a0f Phil Davis
	if ($showall) {
541 0e834fc5 Stephen Beaver
		return($list + $areas);
542 288a2a0f Phil Davis
	} else {
543 a4af095c Renato Botelho
		foreach ($areas as $area => $areaname) {
544
			if ($area === "rrddata" || check_and_returnif_section_exists($area) == true) {
545
				$list[$area] = $areaname;
546
			}
547
		}
548 8ff5ffcc Matthew Grooms
549 0e834fc5 Stephen Beaver
		return($list);
550 5f601060 Phil Davis
	}
551 8ff5ffcc Matthew Grooms
}
552
553 0f298138 k-paulius
$pgtitle = array(gettext("Diagnostics"), htmlspecialchars(gettext("Backup & Restore")), htmlspecialchars(gettext("Backup & Restore")));
554 edcd7535 Phil Davis
$pglinks = array("", "@self", "@self");
555 b63695db Scott Ullrich
include("head.inc");
556
557 947141fd Phil Davis
if ($input_errors) {
558 a4af095c Renato Botelho
	print_input_errors($input_errors);
559 947141fd Phil Davis
}
560 0e834fc5 Stephen Beaver
561 947141fd Phil Davis
if ($savemsg) {
562 a4af095c Renato Botelho
	print_info_box($savemsg, 'success');
563 947141fd Phil Davis
}
564 0e834fc5 Stephen Beaver
565 a4af095c Renato Botelho
if (is_subsystem_dirty('restore')):
566 5b237745 Scott Ullrich
?>
567 a4af095c Renato Botelho
	<br/>
568 1af5edbf Stephen Beaver
	<form action="diag_reboot.php" method="post">
569 7d5b007c Sjon Hortensius
		<input name="Submit" type="hidden" value="Yes" />
570 f6aebbcc NewEraCracker
		<?php print_info_box(gettext("The firewall configuration has been changed.") . "<br />" . gettext("The firewall is now rebooting.")); ?>
571 a4af095c Renato Botelho
		<br />
572 7d5b007c Sjon Hortensius
	</form>
573 0e834fc5 Stephen Beaver
<?php
574 a4af095c Renato Botelho
endif;
575
576
$tab_array = array();
577 0f298138 k-paulius
$tab_array[] = array(htmlspecialchars(gettext("Backup & Restore")), true, "diag_backup.php");
578 a4af095c Renato Botelho
$tab_array[] = array(gettext("Config History"), false, "diag_confbak.php");
579
display_top_tabs($tab_array);
580
581
$form = new Form(false);
582 3beeb66a Stephen Beaver
$form->setMultipartEncoding();	// Allow file uploads
583 a4af095c Renato Botelho
584 5f88f964 k-paulius
$section = new Form_Section('Backup Configuration');
585 a4af095c Renato Botelho
586
$section->addInput(new Form_Select(
587
	'backuparea',
588
	'Backup area',
589
	'',
590
	build_area_list(false)
591
));
592
593
$section->addInput(new Form_Checkbox(
594
	'nopackages',
595
	'Skip packages',
596
	'Do not backup package information.',
597
	false
598
));
599
600
$section->addInput(new Form_Checkbox(
601
	'donotbackuprrd',
602
	'Skip RRD data',
603
	'Do not backup RRD data (NOTE: RRD Data can consume 4+ megabytes of config.xml space!)',
604
	true
605
));
606
607
$section->addInput(new Form_Checkbox(
608
	'encrypt',
609
	'Encryption',
610
	'Encrypt this configuration file.',
611
	false
612 7c0f116b Jared Dillard
));
613 a4af095c Renato Botelho
614 06a1ba14 Viktor G
$section->addPassword(new Form_Input(
615 a4af095c Renato Botelho
	'encrypt_password',
616 c8b10b4c Stephen Beaver
	'Password',
617 a4af095c Renato Botelho
	'password',
618 c8b10b4c Stephen Beaver
	null
619 7c0f116b Jared Dillard
));
620 a4af095c Renato Botelho
621
$group = new Form_Group('');
622 1d80d714 NOYB
// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
623 a4af095c Renato Botelho
$group->add(new Form_Button(
624 9a7e1c95 Phil Davis
	'download',
625 faab522f Renato Botelho
	'Download configuration as XML',
626 37676f4e jim-p
	null,
627
	'fa-download'
628
))->setAttribute('id')->addClass('btn-primary');
629 a4af095c Renato Botelho
630
$section->add($group);
631
$form->add($section);
632
633 5f88f964 k-paulius
$section = new Form_Section('Restore Backup');
634 a4af095c Renato Botelho
635
$section->addInput(new Form_StaticText(
636
	null,
637 52e416cc Phil Davis
	sprintf(gettext("Open a %s configuration XML file and click the button below to restore the configuration."), $g['product_name'])
638 a4af095c Renato Botelho
));
639
640
$section->addInput(new Form_Select(
641
	'restorearea',
642
	'Restore area',
643
	'',
644 ce3eca6e Phil Davis
	build_area_list(true)
645 a4af095c Renato Botelho
));
646
647
$section->addInput(new Form_Input(
648
	'conffile',
649
	'Configuration file',
650
	'file',
651
	null
652
));
653
654
$section->addInput(new Form_Checkbox(
655 7c0f116b Jared Dillard
	'decrypt',
656 a4af095c Renato Botelho
	'Encryption',
657 7c0f116b Jared Dillard
	'Configuration file is encrypted.',
658 a4af095c Renato Botelho
	false
659 7c0f116b Jared Dillard
));
660 a4af095c Renato Botelho
661 76d6d925 Stephen Beaver
$section->addInput(new Form_Input(
662 a4af095c Renato Botelho
	'decrypt_password',
663 c8b10b4c Stephen Beaver
	'Password',
664 a4af095c Renato Botelho
	'password',
665
	null,
666
	['placeholder' => 'Password']
667 7c0f116b Jared Dillard
));
668 a4af095c Renato Botelho
669
$group = new Form_Group('');
670 1d80d714 NOYB
// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
671 a4af095c Renato Botelho
$group->add(new Form_Button(
672 9a7e1c95 Phil Davis
	'restore',
673 faab522f Renato Botelho
	'Restore Configuration',
674 37676f4e jim-p
	null,
675
	'fa-undo'
676
))->setHelp('The firewall will reboot after restoring the configuration.')->addClass('btn-danger restore')->setAttribute('id');
677 a4af095c Renato Botelho
678
$section->add($group);
679
680
$form->add($section);
681
682
if (($config['installedpackages']['package'] != "") || (is_subsystem_dirty("packagelock"))) {
683 5f88f964 k-paulius
	$section = new Form_Section('Package Functions');
684 0e834fc5 Stephen Beaver
685 a4af095c Renato Botelho
	if ($config['installedpackages']['package'] != "") {
686
		$group = new Form_Group('');
687 1d80d714 NOYB
		// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
688 a4af095c Renato Botelho
		$group->add(new Form_Button(
689 9a7e1c95 Phil Davis
			'reinstallpackages',
690 faab522f Renato Botelho
			'Reinstall Packages',
691 37676f4e jim-p
			null,
692
			'fa-retweet'
693
		))->setHelp('Click this button to reinstall all system packages.  This may take a while.')->addClass('btn-success')->setAttribute('id');
694 0e834fc5 Stephen Beaver
695 a4af095c Renato Botelho
		$section->add($group);
696 7eead1fc Darren Embry
	}
697 0e834fc5 Stephen Beaver
698 a4af095c Renato Botelho
	if (is_subsystem_dirty("packagelock")) {
699
		$group = new Form_Group('');
700 1d80d714 NOYB
		// Note: ID attribute of each element created is to be unique.  Not being used, suppressing it.
701 a4af095c Renato Botelho
		$group->add(new Form_Button(
702 9a7e1c95 Phil Davis
			'clearpackagelock',
703 faab522f Renato Botelho
			'Clear Package Lock',
704 37676f4e jim-p
			null,
705
			'fa-wrench'
706
		))->setHelp('Click this button to clear the package lock if a package fails to reinstall properly after an upgrade.')->addClass('btn-warning')->setAttribute('id');
707 0e834fc5 Stephen Beaver
708 a4af095c Renato Botelho
		$section->add($group);
709
	}
710 0e834fc5 Stephen Beaver
711 a4af095c Renato Botelho
	$form->add($section);
712 a1003d57 Ermal Lu?i
}
713 a4af095c Renato Botelho
714
print($form);
715 7c0f116b Jared Dillard
?>
716
<script type="text/javascript">
717
//<![CDATA[
718 947141fd Phil Davis
events.push(function() {
719 7c0f116b Jared Dillard
720
	// ------- Show/hide sections based on checkbox settings --------------------------------------
721 0da0d43e Phil Davis
722 7c0f116b Jared Dillard
	function hideSections(hide) {
723
		hidePasswords();
724
	}
725
726
	function hidePasswords() {
727 3beeb66a Stephen Beaver
728 7c0f116b Jared Dillard
		encryptHide = !($('input[name="encrypt"]').is(':checked'));
729
		decryptHide = !($('input[name="decrypt"]').is(':checked'));
730
731
		hideInput('encrypt_password', encryptHide);
732
		hideInput('decrypt_password', decryptHide);
733
	}
734
735 ff59b884 Stephen Beaver
	// ---------- Click handlers ------------------------------------------------------------------
736 7c0f116b Jared Dillard
737
	$('input[name="encrypt"]').on('change', function() {
738
		hidePasswords();
739
	});
740
741
	$('input[name="decrypt"]').on('change', function() {
742
		hidePasswords();
743 3beeb66a Stephen Beaver
	});
744 7c0f116b Jared Dillard
745 947141fd Phil Davis
	$('#conffile').change(function () {
746 40f146c5 Phil Davis
		if (document.getElementById("conffile").value) {
747
			$('.restore').prop('disabled', false);
748
		} else {
749
			$('.restore').prop('disabled', true);
750
		}
751 ff59b884 Stephen Beaver
    });
752 eef93144 Jared Dillard
	// ---------- On initial page load ------------------------------------------------------------
753
754 7c0f116b Jared Dillard
	hideSections();
755 ff59b884 Stephen Beaver
	$('.restore').prop('disabled', true);
756 7c0f116b Jared Dillard
});
757
//]]>
758
</script>
759
760
<?php
761 a4af095c Renato Botelho
include("foot.inc");
762 7a7f3af1 sullrich
763 5f601060 Phil Davis
if (is_subsystem_dirty('restore')) {
764 cf9a4467 jim-p
	system_reboot();
765 c9d46a8e Renato Botelho
}