Project

General

Profile

Download (15.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	diag_backup.php
5
	Copyright (C) 2004,2005,2006 Scott Ullrich
6
	All rights reserved.
7

    
8
	originally part of m0n0wall (http://m0n0.ch/wall)
9
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
10
	All rights reserved.
11

    
12
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14

    
15
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17

    
18
	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

    
22
	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
##|+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
/* Allow additional execution time 0 = no limit. */
43
ini_set('max_execution_time', '3600');
44
ini_set('max_input_time', '3600');
45

    
46
/* omit no-cache headers because it confuses IE with file downloads */
47
$omit_nocacheheaders = true;
48
require("guiconfig.inc");
49

    
50
function remove_bad_chars($string) {
51
	return preg_replace('/[^a-z|_|0-9]/i','',$string);
52
}
53

    
54
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
function spit_out_select_items($area, $showall) {
62
	global $config;
63
		
64
	$areas = array("aliases" => "Aliases", 
65
				   "captiveportal" => "Captive Portal",
66
				   "dnsmasq" => "DNS Forwarder",				
67
				   "dhcpd" => "DHCP Server",
68
				   "filter" => "Firewall Rules",
69
				   "interfaces" => "Interfaces",
70
				   "ipsec" => "IPSEC",
71
				   "nat" => "NAT",
72
				   "ovpn" => "OpenVPN",
73
				   "installedpackages" => "Package Manager",
74
				   "pptpd" => "PPTP Server",
75
				   "cron" => "Scheduled Tasks",				
76
				   "syslog" => "Syslog",
77
				   "system" => "System",
78
				   "staticroutes" => "Static routes",
79
				   "sysctl" => "System tunables",
80
				   "snmpd" => "SNMP Server",
81
				   "shaper" => "Traffic Shaper",
82
				   "vlans" => "VLANS",
83
				   "wol" => "Wake on LAN"
84
	);
85

    
86
	$select  = "<select name=\"{$area}\">\n";
87
	$select .= "<option VALUE=\"\">ALL</option>";
88

    
89
	if($showall == true) 
90
		foreach($areas as $area => $areaname)
91
			$select .= "<option value='{$area}'>{$areaname}</option>\n";
92
	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

    
97
	$select .= "</select>\n";
98
		
99
	echo $select;
100

    
101
}
102

    
103
if ($_POST) {
104
	unset($input_errors);
105
	if (stristr($_POST['Submit'], "Restore configuration"))
106
		$mode = "restore";
107
	else if (stristr($_POST['Submit'], "Reinstall"))
108
		$mode = "reinstallpackages";
109
	else if (stristr($_POST['Submit'], "Download"))
110
		$mode = "download";
111
	else if (stristr($_POST['Submit'], "Restore version"))
112
		$mode = "restore_ver";
113

    
114
	if ($_POST["nopackages"] <> "")
115
		$options = "nopackages";
116

    
117
	if ($_POST["ver"] <> "")
118
		$ver2restore = $_POST["ver"];
119

    
120
	if ($mode) {
121

    
122
		if ($mode == "download") {
123

    
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
			}
130

    
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
				}
153

    
154
				if ($_POST['encrypt']) {
155
					$data = encrypt_data($data, $_POST['encrypt_password']);
156
					tagfile_reformat($data, $data, "config.xml");
157
				}
158

    
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
					}
189

    
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
					} else {
215
						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
								$reboot_needed = true;
226
								$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
							}
261
						}
262
					}
263
				} else {
264
					$input_errors[] = "The configuration could not be restored (file upload error).";
265
				}
266
			}
267
		}
268

    
269
		if ($mode == "reinstallpackages") {
270

    
271
			header("Location: pkg_mgr_install.php?mode=reinstallall");
272
			exit;
273
                } 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
									$reboot_needed = true;
279
                                    $savemsg = "The configuration has been restored. The firewall is now rebooting.";
280
                                } else {
281
                                	$input_errors[] = "The configuration could not be restored.";
282
                                }
283
                        } else {
284
                                $input_errors[] = "No version selected.";
285
                        }
286
		}
287
	}
288
}
289

    
290
$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
$pgtitle = array("Diagnostics","Backup/restore");
296
include("head.inc");
297

    
298
?>
299

    
300
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
301
<?php include("fbegin.inc"); ?>
302
<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
<?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
<?php
330
		$tab_array = array();
331
		$tab_array[0] = array("Config History", false, "diag_confbak.php");
332
		$tab_array[1] = array("Backup/Restore", true, "diag_backup.php");
333
		display_top_tabs($tab_array);
334
?>
335
		</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
						<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
						<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
						<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
                </tr>
392
                <tr>
393
					<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
						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
						<p><input name="conffile" type="file" class="formfld unknown" id="conffile" size="40"></p>
400
						<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
						<p><input name="Submit" type="submit" class="formbtn" id="restore" value="Restore configuration"></p>
429
                      	<p><strong><span class="red">Note:</span></strong><br />The firewall may need a reboot after restoring the configuration.<br /></p>
430
					</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
		</td>
450
	</tr>
451
</table>
452
</form>
453

    
454
<script language="JavaScript">
455
<!--
456
encrypt_change();
457
decrypt_change();
458
//-->
459
</script>
460

    
461
<?php include("fend.inc"); ?>
462
</body>
463
</html>
464

    
465
<?php
466

    
467
if($reboot_needed == true) {
468
	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
	exit;
475
}
476

    
477
?>
(5-5/210)