Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
#!/usr/local/bin/php
2 8ccc8f1a Scott Ullrich
<?php
3 b46bfcf5 Bill Marquette
/* $Id$ */
4 5b237745 Scott Ullrich
/*
5
	diag_backup.php
6 07bd3f83 Scott Ullrich
	part of m0n0wall (http://m0n0.ch/wall)
7 8ccc8f1a Scott Ullrich
8 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10 8ccc8f1a Scott Ullrich
11 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13 8ccc8f1a Scott Ullrich
14 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16 8ccc8f1a Scott Ullrich
17 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in the
19
	   documentation and/or other materials provided with the distribution.
20 8ccc8f1a Scott Ullrich
21 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32
33
/* omit no-cache headers because it confuses IE with file downloads */
34
$omit_nocacheheaders = true;
35 8ccc8f1a Scott Ullrich
require("guiconfig.inc");
36 f4f07798 Scott Ullrich
require("xmlparse_pkg.inc");
37 5b237745 Scott Ullrich
38 df157bb7 Scott Ullrich
function spit_out_select_items($area) {
39 8e35abee Scott Ullrich
	$select = <<<EOD
40 df157bb7 Scott Ullrich
	<select name="{$area}">
41 8e35abee Scott Ullrich
		<option VALUE="">ALL</option>
42 5161a6e6 Scott Ullrich
		<option VALUE="aliases">Aliases</option>
43 8e35abee Scott Ullrich
		<option VALUE="shaper">Traffic Shaper</option>
44
		<option VALUE="filter">Firewall Rules</option>
45
		<option VALUE="nat">NAT</option>
46
		<option VALUE="pptpd">PPTP Server</option>
47
		<option VALUE="ipsec">IPSEC VPN</option>
48
		<option VALUE="captiveportal">Captive Portal</option>
49
		<option VALUE="installedpackages">Package Manager</option>
50
		<option VALUE="interfaces">Interfaces</option>
51
		<option VALUE="dhcpd">DHCP Server</option>
52
		<option VALUE="syslog">Syslog</option>
53
		<option VALUE="system">System</option>
54
	</select>
55
EOD;
56
	echo $select;
57
58
}
59
60 5b237745 Scott Ullrich
if ($_POST) {
61
	unset($input_errors);
62 7cf03119 Bill Marquette
	if (stristr($_POST['Submit'], "Restore configuration"))
63 5b237745 Scott Ullrich
		$mode = "restore";
64 8ccc8f1a Scott Ullrich
	else if (stristr($_POST['Submit'], "Reinstall"))
65
		$mode = "reinstallpackages";
66 5b237745 Scott Ullrich
	else if (stristr($_POST['Submit'], "Download"))
67
		$mode = "download";
68 7cf03119 Bill Marquette
	else if (stristr($_POST['Submit'], "Restore version"))
69
		$mode = "restore_ver";
70
71 aab57926 Scott Ullrich
	if ($_POST["nopackages"] <> "")
72 528cad39 Colin Smith
		$options = "nopackages";
73 8ccc8f1a Scott Ullrich
74 7cf03119 Bill Marquette
	if ($_POST["ver"] <> "")
75
		$ver2restore = $_POST["ver"];
76
77 5b237745 Scott Ullrich
	if ($mode) {
78
		if ($mode == "download") {
79
			config_lock();
80 8ccc8f1a Scott Ullrich
			$fn = "config-" . $config['system']['hostname'] . "." .
81 5b237745 Scott Ullrich
				$config['system']['domain'] . "-" . date("YmdHis") . ".xml";
82 528cad39 Colin Smith
			if($options == "nopackages") {
83
				exec("sed '/<installedpackages>/,/<\/installedpackages>/d' /conf/config.xml > /tmp/config.xml.nopkg");
84 f66683e6 Bill Marquette
				$fs = filesize("{$g['tmp_path']}/config.xml.nopkg");
85 528cad39 Colin Smith
				header("Content-Type: application/octet-stream");
86
                        	header("Content-Disposition: attachment; filename=$fn");
87
                        	header("Content-Length: $fs");
88 f66683e6 Bill Marquette
				readfile("{$g['tmp_path']}/config.xml.nopkg");
89 528cad39 Colin Smith
			} else {
90 181462b5 Scott Ullrich
				if($_POST['backuparea'] <> "") {
91
					/* user wishes to backup specific area of configuration */
92
					$current_trafficshaper_section = backup_config_section($_POST['backuparea']);
93
					/* generate aliases xml */
94
					$fout = fopen("{$g['tmp_path']}/backup_section.txt","w");
95
					fwrite($fout, $current_trafficshaper_section);
96
					fclose($fout);
97
					$fs = filesize($g['tmp_path'] . "/backup_section.txt");
98
					header("Content-Type: application/octet-stream");
99
					$fn = $_POST['backuparea'] . "-" . $fn;
100
					header("Content-Disposition: attachment; filename=$fn");
101
					header("Content-Length: $fs");
102 e55f5da6 Scott Ullrich
					readfile($g['tmp_path'] . "/backup_section.txt");
103 7e036443 Scott Ullrich
					unlink($g['tmp_path'] . "/backup_section.txt");
104 181462b5 Scott Ullrich
				} else {
105
					$fs = filesize($g['conf_path'] . "/config.xml");
106
					header("Content-Type: application/octet-stream");
107
					header("Content-Disposition: attachment; filename=$fn");
108
					header("Content-Length: $fs");
109
					readfile($g['conf_path'] . "/config.xml");
110
				}
111 528cad39 Colin Smith
			}
112 5b237745 Scott Ullrich
			config_unlock();
113
			exit;
114
		} else if ($mode == "restore") {
115
			if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
116 181462b5 Scott Ullrich
				$command = "/sbin/sysctl -a | grep carp";
117
				$fd = fopen($_FILES['conffile']['tmp_name'], "r");
118
				if(!$fd) {
119
					log_error("Warning, could not open " . $_FILES['conffile']['tmp_name']);
120
					return 1;
121
				}
122
				while(!feof($fd)) {
123
					    $tmp .= fread($fd,49);
124
				}
125
				fclose($fd);
126
				if(stristr($tmp, "m0n0wall" != false)) {
127
					log_error("Upgrading m0n0wall configuration to pfsense.");
128
					/* m0n0wall was found in config.  convert it. */
129
					$upgradedconfig = str_replace("m0n0wall", "pfsense", $tmp);
130
					fopen($_FILES['conffile']['tmp_name'], "w");
131
					fwrite($fd, $upgradedconfig);
132 bfd3e487 Scott Ullrich
					fclose($fd);
133 181462b5 Scott Ullrich
				}
134 fdef6434 Bill Marquette
				if($_POST['restorearea'] <> "") {
135 181462b5 Scott Ullrich
					/* restore a specific area of the configuration */
136
					$rules = return_filename_as_string($_FILES['conffile']['tmp_name']);
137 fdef6434 Bill Marquette
					restore_config_section($_POST['restorearea'], $rules);
138 f4f07798 Scott Ullrich
					filter_configure();
139
					$savemsg = "The configuration area has been restored.";
140 5b237745 Scott Ullrich
				} else {
141 181462b5 Scott Ullrich
					/* restore the entire configuration */
142
					if (config_install($_FILES['conffile']['tmp_name']) == 0) {
143
						system_reboot();
144
						$savemsg = "The configuration has been restored. The firewall is now rebooting.";
145
					} else {
146
						$input_errors[] = "The configuration could not be restored.";
147
					}
148 5b237745 Scott Ullrich
				}
149
			} else {
150
				$input_errors[] = "The configuration could not be restored (file upload error).";
151
			}
152 8ccc8f1a Scott Ullrich
		} else if ($mode == "reinstallpackages") {
153
			header("Location: pkg_mgr_install.php?mode=reinstallall");
154
			exit;
155 7cf03119 Bill Marquette
                } else if ($mode == "restore_ver") {
156
			$input_errors[] = "XXX - this feature may hose your config (do NOT backrev configs!) - billm";
157
			if ($ver2restore <> "") {
158
				$conf_file = "{$g['cf_conf_path']}/bak/config-" . strtotime($ver2restore) . ".xml";
159
                                if (config_install($conf_file) == 0) {
160
                                        system_reboot();
161
                                        $savemsg = "The configuration has been restored. The firewall is now rebooting.";
162
                                } else {
163
                                        $input_errors[] = "The configuration could not be restored.";
164
                                }
165
                        } else {
166
                                $input_errors[] = "No version selected.";
167
                        }
168 5b237745 Scott Ullrich
		}
169
	}
170
}
171 6a1e6651 Bill Marquette
172 02e1170d Scott Ullrich
$id = rand() . '.' . time();
173
174
$mth = ini_get('upload_progress_meter.store_method');
175
$dir = ini_get('upload_progress_meter.file.filename_template');
176
177 5b237745 Scott Ullrich
?>
178
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
179
<html>
180
<head>
181
<title><?=gentitle("Diagnostics: Backup/restore");?></title>
182
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
183
<link href="gui.css" rel="stylesheet" type="text/css">
184
</head>
185
186
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
187
<?php include("fbegin.inc"); ?>
188
      <p class="pgtitle">Diagnostics: Backup/restore</p>
189 02e1170d Scott Ullrich
            <form action="diag_backup.php" method="post" enctype="multipart/form-data" onSubmit="window.open('progress.php?conffile=<?=$id?>','UploadMeter','width=370,height=115', true); return true; ">
190 5b237745 Scott Ullrich
            <?php if ($input_errors) print_input_errors($input_errors); ?>
191
            <?php if ($savemsg) print_info_box($savemsg); ?>
192 b24bf37b Colin Smith
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
193
		<tr><td><ul id="tabnav">
194
			<li class="tabact">Remote</a></li>
195 8024e5db Colin Smith
			<li class="tabinact"><a href="diag_confbak.php">Local</a></li>
196 b24bf37b Colin Smith
		</ul></td></tr>
197
		<tr><td class="tabcont"><table align="center" width="100%" border="0" cellpadding="6" cellspacing="0">
198 8ccc8f1a Scott Ullrich
                <tr>
199 5b237745 Scott Ullrich
                  <td colspan="2" class="listtopic">Backup configuration</td>
200
                </tr>
201 8ccc8f1a Scott Ullrich
                <tr>
202 07bd3f83 Scott Ullrich
                  <td width="22%" valign="baseline" class="vncell">&nbsp;</td>
203 8ccc8f1a Scott Ullrich
                  <td width="78%" class="vtable">
204
                    <p> Click this button to download the system configuration
205 5b237745 Scott Ullrich
                      in XML format.<br>
206
                      <br>
207 df157bb7 Scott Ullrich
		      Backup area: <?php spit_out_select_items("backuparea"); ?>
208 181462b5 Scott Ullrich
		      <p>
209
		      <input name="nopackages" type="checkbox" class="formcheckbox" id="nopackages">Do not backup package information.<p>
210 07bd3f83 Scott Ullrich
                      <input name="Submit" type="submit" class="formbtn" id="download" value="Download configuration"></td>
211
                </tr>
212 8ccc8f1a Scott Ullrich
                <tr>
213 07bd3f83 Scott Ullrich
                  <td colspan="2" class="list" height="12"></td>
214 5b237745 Scott Ullrich
                </tr>
215 8ccc8f1a Scott Ullrich
                <tr>
216 5b237745 Scott Ullrich
                  <td colspan="2" class="listtopic">Restore configuration</td>
217
                </tr>
218 8ccc8f1a Scott Ullrich
                <tr>
219 07bd3f83 Scott Ullrich
                  <td width="22%" valign="baseline" class="vncell">&nbsp;</td>
220 8ccc8f1a Scott Ullrich
                  <td width="78%" class="vtable">
221
                    Open a pfSense configuration XML file and click the button
222 5b237745 Scott Ullrich
                      below to restore the configuration.<br>
223
                      <br>
224 df157bb7 Scott Ullrich
		      Restore area: <?php spit_out_select_items("restorearea"); ?>
225 181462b5 Scott Ullrich
		      <p>
226 5b237745 Scott Ullrich
                      <input name="conffile" type="file" class="formfld" id="conffile" size="40">
227 181462b5 Scott Ullrich
                      <p>
228 5b237745 Scott Ullrich
                      <input name="Submit" type="submit" class="formbtn" id="restore" value="Restore configuration">
229 218df858 Scott Ullrich
                      <p>
230
                      <strong><span class="red">Note:</span></strong><br>
231
                      The firewall will reboot after restoring the configuration.<br>
232 07bd3f83 Scott Ullrich
                  </td>
233 5b237745 Scott Ullrich
                </tr>
234 8ccc8f1a Scott Ullrich
		<tr>
235
		 <td colspan="2">&nbsp;</td>
236
		</tr>
237
		<tr>
238
		 <td colspan="2" class="listtopic">Reinstall packages</td>
239
		</tr>
240
		<tr>
241
		  <td width="22%" valign="baseline" class="vncell">&nbsp;</td>
242
		  <td width="78%" class="vtable">
243 9ca391ca Scott Ullrich
		  <p> Click this button to reinstall all system packages.  This may take a while.<br>
244 8ccc8f1a Scott Ullrich
		  <br>
245
		  <input name="Submit" type="submit" class="formbtn" id="reinstallpackages" value="Reinstall packages"></td>
246
		</tr>
247 b24bf37b Colin Smith
		</table>
248
		</td>
249
		</tr>
250 5b237745 Scott Ullrich
              </table>
251
            </form>
252
<?php include("fend.inc"); ?>
253
</body>
254
</html>