Project

General

Profile

Download (15.4 KB) Statistics
| Branch: | Tag: | Revision:
1 8ccc8f1a Scott Ullrich
<?php
2 b46bfcf5 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	diag_backup.php
5 929db667 Scott Ullrich
	Copyright (C) 2004,2005,2006 Scott Ullrich
6
	All rights reserved.
7 8ccc8f1a Scott Ullrich
8 929db667 Scott Ullrich
	originally part of m0n0wall (http://m0n0.ch/wall)
9 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
10
	All rights reserved.
11 8ccc8f1a Scott Ullrich
12 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14 8ccc8f1a Scott Ullrich
15 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17 8ccc8f1a Scott Ullrich
18 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
19
	   notice, this list of conditions and the following disclaimer in the
20
	   documentation and/or other materials provided with the distribution.
21 8ccc8f1a Scott Ullrich
22 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33
34 6b07c15a Matthew Grooms
##|+PRIV
35
##|*IDENT=page-diagnostics-backup/restore
36
##|*NAME=Diagnostics: Backup/restore page
37
##|*DESCR=Allow access to the 'Diagnostics: Backup/restore' page.
38
##|*MATCH=diag_backup.php*
39
##|-PRIV
40
41
42 47d11b79 Mark Crane
/* Allow additional execution time 0 = no limit. */
43
ini_set('max_execution_time', '3600');
44
ini_set('max_input_time', '3600');
45
46 5b237745 Scott Ullrich
/* omit no-cache headers because it confuses IE with file downloads */
47
$omit_nocacheheaders = true;
48 8ccc8f1a Scott Ullrich
require("guiconfig.inc");
49 5b237745 Scott Ullrich
50 645ec835 Scott Ullrich
function remove_bad_chars($string) {
51
	return preg_replace('/[^a-z|_|0-9]/i','',$string);
52
}
53
54 b3277798 Scott Ullrich
function check_and_returnif_section_exists($section) {
55
	global $config;
56
	if(is_array($config[$section]))
57
		return true;
58
	return false;
59
}
60
61 3244aa21 Scott Ullrich
function spit_out_select_items($area, $showall) {
62 b3277798 Scott Ullrich
	global $config;
63 f47e0c8a Scott Ullrich
		
64 b3277798 Scott Ullrich
	$areas = array("aliases" => "Aliases", 
65 5542b8f5 Scott Ullrich
				   "captiveportal" => "Captive Portal",
66
				   "dnsmasq" => "DNS Forwarder",				
67
				   "dhcpd" => "DHCP Server",
68 b3277798 Scott Ullrich
				   "filter" => "Firewall Rules",
69 5542b8f5 Scott Ullrich
				   "interfaces" => "Interfaces",
70 b3277798 Scott Ullrich
				   "ipsec" => "IPSEC",
71 5542b8f5 Scott Ullrich
				   "nat" => "NAT",
72
				   "ovpn" => "OpenVPN",
73 b3277798 Scott Ullrich
				   "installedpackages" => "Package Manager",
74 5542b8f5 Scott Ullrich
				   "pptpd" => "PPTP Server",
75
				   "cron" => "Scheduled Tasks",				
76 b3277798 Scott Ullrich
				   "syslog" => "Syslog",
77
				   "system" => "System",
78 5542b8f5 Scott Ullrich
				   "staticroutes" => "Static routes",
79
				   "sysctl" => "System tunables",
80
				   "snmpd" => "SNMP Server",
81
				   "shaper" => "Traffic Shaper",
82
				   "vlans" => "VLANS",
83
				   "wol" => "Wake on LAN"
84 b3277798 Scott Ullrich
	);
85
86 f47e0c8a Scott Ullrich
	$select  = "<select name=\"{$area}\">\n";
87 b3277798 Scott Ullrich
	$select .= "<option VALUE=\"\">ALL</option>";
88
89 eef938b5 Scott Ullrich
	if($showall == true) 
90
		foreach($areas as $area => $areaname)
91
			$select .= "<option value='{$area}'>{$areaname}</option>\n";
92 3244aa21 Scott Ullrich
	else 
93
		foreach($areas as $area => $areaname)
94
			if(check_and_returnif_section_exists($area) == true)
95
				$select .= "<option value='{$area}'>{$areaname}</option>\n";
96 b3277798 Scott Ullrich
97
	$select .= "</select>\n";
98
		
99 8e35abee Scott Ullrich
	echo $select;
100
101
}
102
103 5b237745 Scott Ullrich
if ($_POST) {
104
	unset($input_errors);
105 7cf03119 Bill Marquette
	if (stristr($_POST['Submit'], "Restore configuration"))
106 5b237745 Scott Ullrich
		$mode = "restore";
107 8ccc8f1a Scott Ullrich
	else if (stristr($_POST['Submit'], "Reinstall"))
108
		$mode = "reinstallpackages";
109 5b237745 Scott Ullrich
	else if (stristr($_POST['Submit'], "Download"))
110
		$mode = "download";
111 7cf03119 Bill Marquette
	else if (stristr($_POST['Submit'], "Restore version"))
112
		$mode = "restore_ver";
113
114 aab57926 Scott Ullrich
	if ($_POST["nopackages"] <> "")
115 528cad39 Colin Smith
		$options = "nopackages";
116 8ccc8f1a Scott Ullrich
117 7cf03119 Bill Marquette
	if ($_POST["ver"] <> "")
118
		$ver2restore = $_POST["ver"];
119
120 5b237745 Scott Ullrich
	if ($mode) {
121 8ff5ffcc Matthew Grooms
122 5b237745 Scott Ullrich
		if ($mode == "download") {
123 8ff5ffcc Matthew Grooms
124
			if ($_POST['encrypt']) {
125
				if(!$_POST['encrypt_password'] || !$_POST['encrypt_passconf'])
126
					$input_errors[] = "You must supply and confirm the password for encryption.";
127
				if($_POST['encrypt_password'] != $_POST['encrypt_passconf'])
128
					$input_errors[] = "The supplied 'Password' and 'Confirm' field values must match.";
129 528cad39 Colin Smith
			}
130 8ff5ffcc Matthew Grooms
131
			if (!$input_errors) {
132
133
				config_lock();
134
135
				$host = "{$config['system']['hostname']}.{$config['system']['domain']}";
136
				$name = "config-{$host}-".date("YmdHis").".xml";
137
				$data = "";
138
139
				if($options == "nopackages") {
140
					$sfn = "/tmp/config.xml.nopkg";
141
					exec("sed '/<installedpackages>/,/<\/installedpackages>/d' /conf/config.xml > {$sfn}");
142
					$data = file_get_contents($sfn);
143
				} else {
144
					if(!$_POST['backuparea']) {
145
						/* backup entire configuration */
146
						$data = file_get_contents("{$g['conf_path']}/config.xml");
147
					} else {
148
						/* backup specific area of configuration */
149
						$data = backup_config_section($_POST['backuparea']);
150
						$name = "{$_POST['backuparea']}-{$name}";
151
					}
152 181462b5 Scott Ullrich
				}
153 8ff5ffcc Matthew Grooms
154
				if ($_POST['encrypt']) {
155
					$data = encrypt_data($data, $_POST['encrypt_password']);
156
					tagfile_reformat($data, $data, "config.xml");
157 181462b5 Scott Ullrich
				}
158 8ff5ffcc Matthew Grooms
159
				$size = strlen($data);
160
				header("Content-Type: application/octet-stream");
161
				header("Content-Disposition: attachment; filename={$name}");
162
				header("Content-Length: $size");
163
				echo $data;
164
165
				config_unlock();
166
				exit;
167
			}
168
		}
169
170
		if ($mode == "restore") {
171
172
			if ($_POST['decrypt']) {
173
				if(!$_POST['decrypt_password'] || !$_POST['decrypt_passconf'])
174
					$input_errors[] = "You must supply and confirm the password for decryption.";
175
				if($_POST['decrypt_password'] != $_POST['decrypt_passconf'])
176
					$input_errors[] = "The supplied 'Password' and 'Confirm' field values must match.";
177
			}
178
179
			if (!$input_errors) {
180
181
				if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
182
183
					/* read the file contents */
184
					$data = file_get_contents($_FILES['conffile']['tmp_name']);
185
					if(!$data) {
186
						log_error("Warning, could not read file " . $_FILES['conffile']['tmp_name']);
187
						return 1;
188 9fd18b1f Scott Ullrich
					}
189 8ff5ffcc Matthew Grooms
190
					if ($_POST['decrypt']) {
191
						if (!tagfile_deformat($data, $data, "config.xml")) {
192
							$input_errors[] = "The uploaded file does not appear to contain an encrypted pfsense configuration.";
193
							return 1;
194
						}
195
						$data = decrypt_data($data, $_POST['decrypt_password']);
196
					}
197
198
					if(stristr($data, "m0n0wall")) {
199
						log_error("Upgrading m0n0wall configuration to pfsense.");
200
						/* m0n0wall was found in config.  convert it. */
201
						$data = str_replace("m0n0wall", "pfsense", $data);
202
						$m0n0wall_upgrade = true;
203
					}
204
205
					if($_POST['restorearea']) {
206
						/* restore a specific area of the configuration */
207
						if(!stristr($data, $_POST['restorearea'])) {
208
							$input_errors[] = "You have selected to restore a area but we could not locate the correct xml tag.";
209
						} else {
210
							restore_config_section($_POST['restorearea'], $data);
211
							filter_configure();
212
							$savemsg = "The configuration area has been restored.  You may need to reboot the firewall.";
213
						}
214 181462b5 Scott Ullrich
					} else {
215 8ff5ffcc Matthew Grooms
						if(!stristr($data, "<pfsense>")) {
216
							$input_errors[] = "You have selected to restore the full configuration but we could not locate a pfsense tag.";
217
						} else {
218
							/* restore the entire configuration */
219
							file_put_contents($_FILES['conffile']['tmp_name'], $data);
220
							if (config_install($_FILES['conffile']['tmp_name']) == 0) {
221
								/* this will be picked up by /index.php */
222
								conf_mount_rw();
223
								if($g['platform'] <> "cdrom")
224
									touch("/needs_package_sync");
225 56825c01 Scott Ullrich
								$reboot_needed = true;
226 8ff5ffcc Matthew Grooms
								$savemsg = "The configuration has been restored. The firewall is now rebooting.";
227
								/* remove cache, we will force a config reboot */
228
								if(file_exists("/tmp/config.cache"))
229
									unlink("/tmp/config.cache");
230
								$config = parse_config(true);
231
								if($m0n0wall_upgrade == true) {
232
									if($config['system']['gateway'] <> "")
233
										$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
234
									unset($config['shaper']);
235
									/* optional if list */
236
									$ifdescrs = get_configured_interface_list(true, true);
237
									/* remove special characters from interface descriptions */
238
									if(is_array($ifdescrs))
239
										foreach($ifdescrs as $iface)
240
											$config['interfaces'][$iface]['descr'] = remove_bad_chars($config['interfaces'][$iface]['descr']);
241
									unlink_if_exists("/tmp/config.cache");
242
									write_config();
243
									conf_mount_ro();
244
									$savemsg = "The m0n0wall configuration has been restored and upgraded to pfSense.<p>The firewall is now rebooting.";
245
									$reboot_needed = true;
246
								}
247
								if(isset($config['captiveportal']['enable'])) {
248
									/* for some reason ipfw doesn't init correctly except on bootup sequence */
249
									$savemsg = "The configuration has been restored.<p>The firewall is now rebooting.";
250
									$reboot_needed = true;
251
								}
252
								setup_serial_port();
253
								if(is_interface_mismatch() == true) {
254
									touch("/var/run/interface_mismatch_reboot_needed");
255
									$reboot_needed = false;
256
									header("Location: interfaces_assign.php");
257
								}
258
							} else {
259
								$input_errors[] = "The configuration could not be restored.";
260 db3996df Scott Ullrich
							}
261 9c72b993 Scott Ullrich
						}
262 181462b5 Scott Ullrich
					}
263 8ff5ffcc Matthew Grooms
				} else {
264
					$input_errors[] = "The configuration could not be restored (file upload error).";
265 db3996df Scott Ullrich
				}
266 5b237745 Scott Ullrich
			}
267 8ff5ffcc Matthew Grooms
		}
268
269
		if ($mode == "reinstallpackages") {
270
271 8ccc8f1a Scott Ullrich
			header("Location: pkg_mgr_install.php?mode=reinstallall");
272
			exit;
273 7cf03119 Bill Marquette
                } else if ($mode == "restore_ver") {
274
			$input_errors[] = "XXX - this feature may hose your config (do NOT backrev configs!) - billm";
275
			if ($ver2restore <> "") {
276
				$conf_file = "{$g['cf_conf_path']}/bak/config-" . strtotime($ver2restore) . ".xml";
277
                                if (config_install($conf_file) == 0) {
278 e57f259a Scott Ullrich
									$reboot_needed = true;
279
                                    $savemsg = "The configuration has been restored. The firewall is now rebooting.";
280 7cf03119 Bill Marquette
                                } else {
281 e57f259a Scott Ullrich
                                	$input_errors[] = "The configuration could not be restored.";
282 7cf03119 Bill Marquette
                                }
283
                        } else {
284
                                $input_errors[] = "No version selected.";
285
                        }
286 5b237745 Scott Ullrich
		}
287
	}
288
}
289 6a1e6651 Bill Marquette
290 02e1170d Scott Ullrich
$id = rand() . '.' . time();
291
292
$mth = ini_get('upload_progress_meter.store_method');
293
$dir = ini_get('upload_progress_meter.file.filename_template');
294
295 d88c6a9f Scott Ullrich
$pgtitle = array("Diagnostics","Backup/restore");
296 b63695db Scott Ullrich
include("head.inc");
297
298 5b237745 Scott Ullrich
?>
299
300
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
301
<?php include("fbegin.inc"); ?>
302 8ff5ffcc Matthew Grooms
<script language="JavaScript">
303
<!--
304
305
function encrypt_change() {
306
307
	if (!document.iform.encrypt.checked)
308
		document.getElementById("encrypt_opts").style.display="none";
309
	else
310
		document.getElementById("encrypt_opts").style.display="";
311
}
312
313
function decrypt_change() {
314
315
	if (!document.iform.decrypt.checked)
316
		document.getElementById("decrypt_opts").style.display="none";
317
	else
318
		document.getElementById("decrypt_opts").style.display="";
319
}
320
321
//-->
322
</script>
323
<form action="diag_backup.php" method="post" name="iform" enctype="multipart/form-data">
324 9e2a4fce Erik Kristensen
<?php if ($input_errors) print_input_errors($input_errors); ?>
325
<?php if ($savemsg) print_info_box($savemsg); ?>
326
<table width="100%" border="0" cellspacing="0" cellpadding="0">
327
	<tr>
328
		<td>
329 12af52d9 Scott Ullrich
<?php
330 9e2a4fce Erik Kristensen
		$tab_array = array();
331 2dfdb028 Chris Buechler
		$tab_array[0] = array("Config History", false, "diag_confbak.php");
332
		$tab_array[1] = array("Backup/Restore", true, "diag_backup.php");
333 9e2a4fce Erik Kristensen
		display_top_tabs($tab_array);
334 645ec835 Scott Ullrich
?>
335 9e2a4fce Erik Kristensen
		</td>
336
	</tr>
337
	<tr>
338
		<td>
339
			<div id="mainarea">
340
			<table class="tabcont" align="center" width="100%" border="0" cellpadding="6" cellspacing="0">
341
				<tr>
342
					<td colspan="2" class="listtopic">Backup configuration</td>
343
				</tr>
344
				<tr>
345
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
346
					<td width="78%" class="vtable">
347 3244aa21 Scott Ullrich
						<p>Click this button to download the system configuration in XML format.<br /><br /> Backup area: <?php spit_out_select_items("backuparea", false); ?></p>
348 8ff5ffcc Matthew Grooms
						<table>
349
							<tr>
350
								<td>
351
									<input name="nopackages" type="checkbox" class="formcheckbox" id="nopackages">
352
								</td>
353
								<td>
354
									<span class="vexpl">Do not backup package information.</span>
355
								</td>
356
							</tr>
357
						</table>
358
						<table>
359
							<tr>
360
								<td>
361
									<input name="encrypt" type="checkbox" class="formcheckbox" id="nopackages" onClick="encrypt_change()">
362
								</td>
363
								<td>
364
									<span class="vexpl">Encrypt this configuration file.</span>
365
								</td>
366
							</tr>
367
						</table>
368
						<table id="encrypt_opts">
369
							<tr>
370
								<td>
371
									<span class="vexpl">Password :</span>
372
								</td>
373
								<td>
374
									<input name="encrypt_password" type="password" class="formfld pwd" size="20" value="" />
375
								</td>
376
							</tr>
377
							<tr>
378
								<td>
379
									<span class="vexpl">confirm :</span>
380
								</td>
381
								<td>
382
									<input name="encrypt_passconf" type="password" class="formfld pwd" size="20" value="" />
383
								</td>
384
							</tr>
385
						</table>
386 9e2a4fce Erik Kristensen
						<p><input name="Submit" type="submit" class="formbtn" id="download" value="Download configuration"></p>
387
					</td>
388
				</tr>
389
				<tr>
390
					<td colspan="2" class="list" height="12">&nbsp;</td>
391 5b237745 Scott Ullrich
                </tr>
392 8ccc8f1a Scott Ullrich
                <tr>
393 9e2a4fce Erik Kristensen
					<td colspan="2" class="listtopic">Restore configuration</td>
394
				</tr>
395
				<tr>
396
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
397
					<td width="78%" class="vtable">
398 3244aa21 Scott Ullrich
						Open a <?=$g['[product_name']?> configuration XML file and click the button below to restore the configuration. <br /><br /> Restore area: <?php spit_out_select_items("restorearea", true); ?>
399 b5c78501 Seth Mos
						<p><input name="conffile" type="file" class="formfld unknown" id="conffile" size="40"></p>
400 8ff5ffcc Matthew Grooms
						<table>
401
							<tr>
402
								<td>
403
									<input name="decrypt" type="checkbox" class="formcheckbox" id="nopackages" onClick="decrypt_change()">
404
								</td>
405
								<td>
406
									<span class="vexpl">Configuration file is encrypted.</span>
407
								</td>
408
							</tr>
409
						</table>
410
						<table id="decrypt_opts">
411
							<tr>
412
								<td>
413
									<span class="vexpl">Password :</span>
414
								</td>
415
								<td>
416
									<input name="decrypt_password" type="password" class="formfld pwd" size="20" value="" />
417
								</td>
418
							</tr>
419
							<tr>
420
								<td>
421
									<span class="vexpl">confirm :</span>
422
								</td>
423
								<td>
424
									<input name="decrypt_passconf" type="password" class="formfld pwd" size="20" value="" />
425
								</td>
426
							</tr>
427
						</table>
428 9e2a4fce Erik Kristensen
						<p><input name="Submit" type="submit" class="formbtn" id="restore" value="Restore configuration"></p>
429 aea4964f Bill Marquette
                      	<p><strong><span class="red">Note:</span></strong><br />The firewall may need a reboot after restoring the configuration.<br /></p>
430 9e2a4fce Erik Kristensen
					</td>
431
				</tr>
432
				<?php if($config['installedpackages']['package'] != "") { ?>
433
				<tr>
434
					<td colspan="2" class="list" height="12">&nbsp;</td>
435
				</tr>
436
				<tr>
437
					<td colspan="2" class="listtopic">Reinstall packages</td>
438
				</tr>
439
				<tr>
440
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
441
					<td width="78%" class="vtable">
442
						<p>Click this button to reinstall all system packages.  This may take a while. <br /><br />
443
		  				<input name="Submit" type="submit" class="formbtn" id="reinstallpackages" value="Reinstall packages">
444
					</td>
445
				</tr>
446
				<?php } ?>
447
			</table>
448
			</div>
449 b24bf37b Colin Smith
		</td>
450 9e2a4fce Erik Kristensen
	</tr>
451
</table>
452
</form>
453
454 8ff5ffcc Matthew Grooms
<script language="JavaScript">
455
<!--
456
encrypt_change();
457
decrypt_change();
458
//-->
459
</script>
460
461 5b237745 Scott Ullrich
<?php include("fend.inc"); ?>
462
</body>
463 38d48421 Colin Smith
</html>
464 8ed1f1a9 Scott Ullrich
465
<?php
466
467 56825c01 Scott Ullrich
if($reboot_needed == true) {
468 ff598ca2 Scott Ullrich
	ob_flush();
469
	flush();
470
	sleep(5);
471
	while(file_exists("{$g['varrun_path']}/config.lock"))
472
		sleep(3);
473
	mwexec("/sbin/shutdown -r now");
474 56825c01 Scott Ullrich
	exit;
475
}
476
477 b5c78501 Seth Mos
?>