Project

General

Profile

Download (11.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	diag_backup.php
5
	part of m0n0wall (http://m0n0.ch/wall)
6

    
7
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9

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

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

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31

    
32
/* omit no-cache headers because it confuses IE with file downloads */
33
$omit_nocacheheaders = true;
34
require("guiconfig.inc");
35

    
36
function spit_out_select_items($area) {
37
	$select = <<<EOD
38
	<select name="{$area}">
39
		<option VALUE="">ALL</option>
40
		<option VALUE="aliases">Aliases</option>
41
		<option VALUE="shaper">Traffic Shaper</option>
42
		<option VALUE="filter">Firewall Rules</option>
43
		<option VALUE="nat">NAT</option>
44
		<option VALUE="pptpd">PPTP Server</option>
45
		<option VALUE="ipsec">IPSEC VPN</option>
46
		<option VALUE="captiveportal">Captive Portal</option>
47
		<option VALUE="installedpackages">Package Manager</option>
48
		<option VALUE="interfaces">Interfaces</option>
49
		<option VALUE="dhcpd">DHCP Server</option>
50
		<option VALUE="syslog">Syslog</option>
51
		<option VALUE="system">System</option>
52
	</select>
53
EOD;
54
	echo $select;
55

    
56
}
57

    
58
if ($_POST) {
59
	unset($input_errors);
60
	if (stristr($_POST['Submit'], "Restore configuration"))
61
		$mode = "restore";
62
	else if (stristr($_POST['Submit'], "Reinstall"))
63
		$mode = "reinstallpackages";
64
	else if (stristr($_POST['Submit'], "Download"))
65
		$mode = "download";
66
	else if (stristr($_POST['Submit'], "Restore version"))
67
		$mode = "restore_ver";
68

    
69
	if ($_POST["nopackages"] <> "")
70
		$options = "nopackages";
71

    
72
	if ($_POST["ver"] <> "")
73
		$ver2restore = $_POST["ver"];
74

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

    
203
$id = rand() . '.' . time();
204

    
205
$mth = ini_get('upload_progress_meter.store_method');
206
$dir = ini_get('upload_progress_meter.file.filename_template');
207

    
208
$pgtitle = "Diagnostics: Backup/restore";
209
include("head.inc");
210

    
211
?>
212

    
213
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
214
<?php include("fbegin.inc"); ?>
215
<p class="pgtitle"><?=$pgtitle?></p>
216

    
217
<form action="diag_backup.php" method="post" enctype="multipart/form-data">
218
<?php if ($input_errors) print_input_errors($input_errors); ?>
219
<?php if ($savemsg) print_info_box($savemsg); ?>
220
<table width="100%" border="0" cellspacing="0" cellpadding="0">
221
	<tr>
222
		<td>
223
<?php
224
		$tab_array = array();
225
		$tab_array[0] = array("Remote", false, "diag_confbak.php");
226
		$tab_array[1] = array("Local", true, "diag_backup.php");
227
		display_top_tabs($tab_array);
228
?>			
229
		</td>
230
	</tr>
231
	<tr>
232
		<td>
233
			<div id="mainarea">
234
			<table class="tabcont" align="center" width="100%" border="0" cellpadding="6" cellspacing="0">
235
				<tr>
236
					<td colspan="2" class="listtopic">Backup configuration</td>
237
				</tr>
238
				<tr>
239
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
240
					<td width="78%" class="vtable">
241
						<p>Click this button to download the system configuration in XML format.<br /><br /> Backup area: <?php spit_out_select_items("backuparea"); ?></p>
242
						<p><input name="nopackages" type="checkbox" class="formcheckbox" id="nopackages">Do not backup package information.</p>
243
						<p><input name="Submit" type="submit" class="formbtn" id="download" value="Download configuration"></p>
244
					</td>
245
				</tr>
246
				<tr>
247
					<td colspan="2" class="list" height="12">&nbsp;</td>
248
                </tr>
249
                <tr>
250
					<td colspan="2" class="listtopic">Restore configuration</td>
251
				</tr>
252
				<tr>
253
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
254
					<td width="78%" class="vtable">
255
						Open a pfSense configuration XML file and click the button below to restore the configuration. <br /><br /> Restore area: <?php spit_out_select_items("restorearea"); ?>
256
						<p><input name="conffile" type="file" class="formfld" id="conffile" size="40"></p>
257
						<p><input name="Submit" type="submit" class="formbtn" id="restore" value="Restore configuration"></p>
258
                      	<p><strong><span class="red">Note:</span></strong><br />The firewall will reload the settings after restoring the configuration.<br /></p>
259
					</td>
260
				</tr>
261
				<?php if($config['installedpackages']['package'] != "") { ?>
262
				<tr>
263
					<td colspan="2" class="list" height="12">&nbsp;</td>
264
				</tr>
265
				<tr>
266
					<td colspan="2" class="listtopic">Reinstall packages</td>
267
				</tr>
268
				<tr>
269
					<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
270
					<td width="78%" class="vtable">
271
						<p>Click this button to reinstall all system packages.  This may take a while. <br /><br />
272
		  				<input name="Submit" type="submit" class="formbtn" id="reinstallpackages" value="Reinstall packages">
273
					</td>
274
				</tr>
275
				<?php } ?>
276
			</table>
277
			</div>
278
		</td>
279
	</tr>
280
</table>
281
</form>
282

    
283
<?php include("fend.inc"); ?>
284
</body>
285
</html>
286

    
287
<?php
288

    
289
if($reboot_needed == true) {
290
	mwexec("/etc/rc.reboot");
291
	exit;
292
}
293

    
294
if($reloadall == true) {
295
	
296
	if(file_exists("{$g['tmp_path']}/config.cache"))
297
		unlink("{$g['tmp_path']}/config.cache");
298
		
299
	parse_config();
300
	
301
	reload_all();
302
	
303
}
304

    
305
?>
(5-5/162)