Project

General

Profile

Download (12.2 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
/* Allow additional execution time 0 = no limit. */
35
ini_set('max_execution_time', '3600');
36
ini_set('max_input_time', '3600');
37

    
38
/* omit no-cache headers because it confuses IE with file downloads */
39
$omit_nocacheheaders = true;
40
require("guiconfig.inc");
41

    
42
function remove_bad_chars($string) {
43
	return preg_replace('/[^a-z|_|0-9]/i','',$string);
44
}
45

    
46
function spit_out_select_items($area) {
47
	$select = <<<EOD
48
	<select name="{$area}">
49
		<option VALUE="">ALL</option>
50
		<option VALUE="aliases">Aliases</option>
51
		<option VALUE="shaper">Traffic Shaper</option>
52
		<option VALUE="filter">Firewall Rules</option>
53
		<option VALUE="nat">NAT</option>
54
		<option VALUE="pptpd">PPTP Server</option>
55
		<option VALUE="ipsec">IPsec VPN</option>
56
		<option VALUE="captiveportal">Captive Portal</option>
57
		<option VALUE="installedpackages">Package Manager</option>
58
		<option VALUE="interfaces">Interfaces</option>
59
		<option VALUE="dhcpd">DHCP Server</option>
60
		<option VALUE="syslog">Syslog</option>
61
		<option VALUE="system">System</option>
62
		<option VALUE="staticroutes">Static routes</option>
63
	</select>
64
EOD;
65
	echo $select;
66

    
67
}
68

    
69
if ($_POST) {
70
	unset($input_errors);
71
	if (stristr($_POST['Submit'], "Restore configuration"))
72
		$mode = "restore";
73
	else if (stristr($_POST['Submit'], "Reinstall"))
74
		$mode = "reinstallpackages";
75
	else if (stristr($_POST['Submit'], "Download"))
76
		$mode = "download";
77
	else if (stristr($_POST['Submit'], "Restore version"))
78
		$mode = "restore_ver";
79

    
80
	if ($_POST["nopackages"] <> "")
81
		$options = "nopackages";
82

    
83
	if ($_POST["ver"] <> "")
84
		$ver2restore = $_POST["ver"];
85

    
86
	if ($mode) {
87
		if ($mode == "download") {
88
			config_lock();
89
			$fn = "config-" . $config['system']['hostname'] . "." .
90
				$config['system']['domain'] . "-" . date("YmdHis") . ".xml";
91
			if($options == "nopackages") {
92
				exec("sed '/<installedpackages>/,/<\/installedpackages>/d' /conf/config.xml > /tmp/config.xml.nopkg");
93
				$fs = filesize("{$g['tmp_path']}/config.xml.nopkg");
94
				header("Content-Type: application/octet-stream");
95
                        	header("Content-Disposition: attachment; filename=$fn");
96
                        	header("Content-Length: $fs");
97
				readfile("{$g['tmp_path']}/config.xml.nopkg");
98
			} else {
99
				if($_POST['backuparea'] <> "") {
100
					/* user wishes to backup specific area of configuration */
101
					$current_trafficshaper_section = backup_config_section($_POST['backuparea']);
102
					/* generate aliases xml */
103
					$fout = fopen("{$g['tmp_path']}/backup_section.txt","w");
104
					fwrite($fout, $current_trafficshaper_section);
105
					fclose($fout);
106
					$fs = filesize($g['tmp_path'] . "/backup_section.txt");
107
					header("Content-Type: application/octet-stream");
108
					$fn = $_POST['backuparea'] . "-" . $fn;
109
					header("Content-Disposition: attachment; filename=$fn");
110
					header("Content-Length: $fs");
111
					readfile($g['tmp_path'] . "/backup_section.txt");
112
					unlink($g['tmp_path'] . "/backup_section.txt");
113
				} else {
114
					$fs = filesize($g['conf_path'] . "/config.xml");
115
					header("Content-Type: application/octet-stream");
116
					header("Content-Disposition: attachment; filename=$fn");
117
					header("Content-Length: $fs");
118
					readfile($g['conf_path'] . "/config.xml");
119
				}
120
			}
121
			config_unlock();
122
			exit;
123
		} else if ($mode == "restore") {
124
			if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
125
				$fd = fopen($_FILES['conffile']['tmp_name'], "r");
126
				if(!$fd) {
127
					log_error("Warning, could not open " . $_FILES['conffile']['tmp_name']);
128
					return 1;
129
				}
130
				while(!feof($fd)) {
131
					    $tmp .= fread($fd,49);
132
				}
133
				fclose($fd);
134
				if(stristr($tmp, "m0n0wall") == true) {
135
					log_error("Upgrading m0n0wall configuration to pfsense.");
136
					/* m0n0wall was found in config.  convert it. */
137
					$upgradedconfig = str_replace("m0n0wall", "pfsense", $tmp);
138
					$fd = fopen($_FILES['conffile']['tmp_name'], "w");
139
					fwrite($fd, $upgradedconfig);
140
					fclose($fd);
141
					$m0n0wall_upgrade = true;
142
				}
143
				if($_POST['restorearea'] <> "") {
144
					/* restore a specific area of the configuration */
145
					$rules = file_get_contents($_FILES['conffile']['tmp_name']);
146
					if(stristr($rules, $_POST['restorearea']) == false) {
147
						$input_errors[] = "You have selected to restore a area but we could not locate the correct xml tag.";
148
					} else {
149
						restore_config_section($_POST['restorearea'], $rules);
150
						filter_configure();
151
						$savemsg = "The configuration area has been restored.  You may need to reboot the firewall.";
152
					}
153
				} else {
154
					$rules = file_get_contents($_FILES['conffile']['tmp_name']);
155
					if(stristr($rules, "pfsense") == false) {
156
						$input_errors[] = "You have selected to restore the full configuration but we could not locate a pfsense tag.";
157
					} else {
158
						/* restore the entire configuration */
159
						if (config_install($_FILES['conffile']['tmp_name']) == 0) {
160
							/* this will be picked up by /index.php */
161
							conf_mount_rw();
162
							if($g['platform'] <> "cdrom")
163
								touch("/needs_package_sync");
164
							$reboot_needed = true;
165
							$savemsg = "The configuration has been restored. The firewall is now rebooting.";
166
							/* remove cache, we will force a config reboot */
167
							if(file_exists("/tmp/config.cache"))
168
								unlink("/tmp/config.cache");
169
							$config = parse_config(true);
170
							if($m0n0wall_upgrade == true) {
171
								if($config['system']['gateway'] <> "")
172
									$config['interfaces']['wan']['gateway'] = $config['system']['gateway'];
173
								unset($config['shaper']);
174
								/* build an interface collection */
175
								for ($j = 1; isset ($config['interfaces']['opt' . $j]); $j++)
176
									$ifdescrs['opt' . $j] = "opt" . $j;
177
								/* remove special characters from interface descriptions */
178
								if(is_array($ifdescrs))
179
									foreach($ifdescrs as $iface)
180
										$config['interfaces'][$iface]['descr'] = remove_bad_chars($config['interfaces'][$iface]['descr']);
181
								unlink_if_exists("/tmp/config.cache");
182
								write_config();
183
								conf_mount_ro();
184
								$savemsg = "The m0n0wall configuration has been restored and upgraded to pfSense.<p>The firewall is now rebooting.";
185
								$reboot_needed = true;
186
							}
187
							if(isset($config['captiveportal']['enable'])) {
188
								/* for some reason ipfw doesn't init correctly except on bootup sequence */
189
								$savemsg = "The configuration has been restored.<p>The firewall is now rebooting.";
190
								$reboot_needed = true;
191
							}
192
							setup_serial_port();
193
							if(is_interface_mismatch() == true) {
194
								touch("/var/run/interface_mismatch_reboot_needed");
195
								$reboot_needed = false;
196
								header("Location: interfaces_assign.php");
197
							}
198
						} else {
199
							$input_errors[] = "The configuration could not be restored.";
200
						}
201
					}
202
				}
203
			} else {
204
				$input_errors[] = "The configuration could not be restored (file upload error).";
205
			}
206
		} else if ($mode == "reinstallpackages") {
207
			header("Location: pkg_mgr_install.php?mode=reinstallall");
208
			exit;
209
                } else if ($mode == "restore_ver") {
210
			$input_errors[] = "XXX - this feature may hose your config (do NOT backrev configs!) - billm";
211
			if ($ver2restore <> "") {
212
				$conf_file = "{$g['cf_conf_path']}/bak/config-" . strtotime($ver2restore) . ".xml";
213
                                if (config_install($conf_file) == 0) {
214
									$reboot_needed = true;
215
                                    $savemsg = "The configuration has been restored. The firewall is now rebooting.";
216
                                } else {
217
                                	$input_errors[] = "The configuration could not be restored.";
218
                                }
219
                        } else {
220
                                $input_errors[] = "No version selected.";
221
                        }
222
		}
223
	}
224
}
225

    
226
$id = rand() . '.' . time();
227

    
228
$mth = ini_get('upload_progress_meter.store_method');
229
$dir = ini_get('upload_progress_meter.file.filename_template');
230

    
231
$pgtitle = array("Diagnostics","Backup/restore");
232
include("head.inc");
233

    
234
?>
235

    
236
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
237
<?php include("fbegin.inc"); ?>
238
<form action="diag_backup.php" method="post" enctype="multipart/form-data">
239
<?php if ($input_errors) print_input_errors($input_errors); ?>
240
<?php if ($savemsg) print_info_box($savemsg); ?>
241
<table width="100%" border="0" cellspacing="0" cellpadding="0">
242
	<tr>
243
		<td>
244
<?php
245
		$tab_array = array();
246
		$tab_array[0] = array("Config History", false, "diag_confbak.php");
247
		$tab_array[1] = array("Backup/Restore", true, "diag_backup.php");
248
		display_top_tabs($tab_array);
249
?>
250
		</td>
251
	</tr>
252
	<tr>
253
		<td>
254
			<div id="mainarea">
255
			<table class="tabcont" align="center" width="100%" border="0" cellpadding="6" cellspacing="0">
256
				<tr>
257
					<td colspan="2" class="listtopic">Backup configuration</td>
258
				</tr>
259
				<tr>
260
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
261
					<td width="78%" class="vtable">
262
						<p>Click this button to download the system configuration in XML format.<br /><br /> Backup area: <?php spit_out_select_items("backuparea"); ?></p>
263
						<p><input name="nopackages" type="checkbox" class="formcheckbox" id="nopackages">Do not backup package information.</p>
264
						<p><input name="Submit" type="submit" class="formbtn" id="download" value="Download configuration"></p>
265
					</td>
266
				</tr>
267
				<tr>
268
					<td colspan="2" class="list" height="12">&nbsp;</td>
269
                </tr>
270
                <tr>
271
					<td colspan="2" class="listtopic">Restore configuration</td>
272
				</tr>
273
				<tr>
274
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
275
					<td width="78%" class="vtable">
276
						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"); ?>
277
						<p><input name="conffile" type="file" class="formfld unknown" id="conffile" size="40"></p>
278
						<p><input name="Submit" type="submit" class="formbtn" id="restore" value="Restore configuration"></p>
279
                      	<p><strong><span class="red">Note:</span></strong><br />The firewall may need a reboot after restoring the configuration.<br /></p>
280
					</td>
281
				</tr>
282
				<?php if($config['installedpackages']['package'] != "") { ?>
283
				<tr>
284
					<td colspan="2" class="list" height="12">&nbsp;</td>
285
				</tr>
286
				<tr>
287
					<td colspan="2" class="listtopic">Reinstall packages</td>
288
				</tr>
289
				<tr>
290
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
291
					<td width="78%" class="vtable">
292
						<p>Click this button to reinstall all system packages.  This may take a while. <br /><br />
293
		  				<input name="Submit" type="submit" class="formbtn" id="reinstallpackages" value="Reinstall packages">
294
					</td>
295
				</tr>
296
				<?php } ?>
297
			</table>
298
			</div>
299
		</td>
300
	</tr>
301
</table>
302
</form>
303

    
304
<?php include("fend.inc"); ?>
305
</body>
306
</html>
307

    
308
<?php
309

    
310
if($reboot_needed == true) {
311
	ob_flush();
312
	flush();
313
	sleep(5);
314
	while(file_exists("{$g['varrun_path']}/config.lock"))
315
		sleep(3);
316
	mwexec("/sbin/shutdown -r now");
317
	exit;
318
}
319

    
320
?>
(5-5/193)