summaryrefslogtreecommitdiffstats
path: root/usr/local/www/diag_backup.php
blob: 17c9374bca520aaf7d7d01a3d0c8b5e4afdcf0a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/local/bin/php
<?php
/*
	diag_backup.php
	part of m0n0wall (http://m0n0.ch/wall)

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

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

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

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

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

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

function spit_out_select_items($area) {
	$select = <<<EOD
	<select name="{$area}">
		<option VALUE="">ALL</option>
		<option VALUE="shaper">Traffic Shaper</option>
		<option VALUE="filter">Firewall Rules</option>
		<option VALUE="nat">NAT</option>
		<option VALUE="pptpd">PPTP Server</option>
		<option VALUE="ipsec">IPSEC VPN</option>
		<option VALUE="captiveportal">Captive Portal</option>
		<option VALUE="installedpackages">Package Manager</option>
		<option VALUE="interfaces">Interfaces</option>
		<option VALUE="dhcpd">DHCP Server</option>
		<option VALUE="syslog">Syslog</option>
		<option VALUE="system">System</option>
	</select>
EOD;
	echo $select;

}

if ($_POST) {
	unset($input_errors);
	if (stristr($_POST['Submit'], "Restore configuration"))
		$mode = "restore";
	else if (stristr($_POST['Submit'], "Reinstall"))
		$mode = "reinstallpackages";
	else if (stristr($_POST['Submit'], "Download"))
		$mode = "download";
	else if (stristr($_POST['Submit'], "Restore version"))
		$mode = "restore_ver";

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

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

	if ($mode) {
		if ($mode == "download") {
			config_lock();
			$fn = "config-" . $config['system']['hostname'] . "." .
				$config['system']['domain'] . "-" . date("YmdHis") . ".xml";
			if($options == "nopackages") {
				exec("sed '/<installedpackages>/,/<\/installedpackages>/d' /conf/config.xml > /tmp/config.xml.nopkg");
				$fs = filesize("/tmp/config.xml.nopkg");
				header("Content-Type: application/octet-stream");
                        	header("Content-Disposition: attachment; filename=$fn");
                        	header("Content-Length: $fs");
				readfile("/tmp/config.xml.nopkg");
			} else {
				if($_POST['backuparea'] <> "") {
					/* user wishes to backup specific area of configuration */
					$current_trafficshaper_section = backup_config_section($_POST['backuparea']);
					/* generate aliases xml */
					$fout = fopen("{$g['tmp_path']}/backup_section.txt","w");
					fwrite($fout, $current_trafficshaper_section);
					fclose($fout);
					$fs = filesize($g['tmp_path'] . "/backup_section.txt");
					header("Content-Type: application/octet-stream");
					$fn = $_POST['backuparea'] . "-" . $fn;
					header("Content-Disposition: attachment; filename=$fn");
					header("Content-Length: $fs");
					readfile($g['tmp_path'] . "/backup_section.txt","w");
					unlink($g['tmp_path'] . "/backup_section.txt");
				} else {
					$fs = filesize($g['conf_path'] . "/config.xml");
					header("Content-Type: application/octet-stream");
					header("Content-Disposition: attachment; filename=$fn");
					header("Content-Length: $fs");
					readfile($g['conf_path'] . "/config.xml");
				}
			}
			config_unlock();
			exit;
		} else if ($mode == "restore") {
			if (is_uploaded_file($_FILES['conffile']['tmp_name'])) {
				$command = "/sbin/sysctl -a | grep carp";
				$fd = fopen($_FILES['conffile']['tmp_name'], "r");
				if(!$fd) {
					log_error("Warning, could not open " . $_FILES['conffile']['tmp_name']);
					return 1;
				}
				while(!feof($fd)) {
					    $tmp .= fread($fd,49);
				}
				fclose($fd);
				if(stristr($tmp, "m0n0wall" != false)) {
					log_error("Upgrading m0n0wall configuration to pfsense.");
					/* m0n0wall was found in config.  convert it. */
					$upgradedconfig = str_replace("m0n0wall", "pfsense", $tmp);
					fopen($_FILES['conffile']['tmp_name'], "w");
					fwrite($fd, $upgradedconfig);
					fclose($fd);
				}
				if($_POST['backuparea'] <> "") {
					/* restore a specific area of the configuration */
					$rules = return_filename_as_string($_FILES['conffile']['tmp_name']);
					restore_config_section($_POST['backuparea'], $rules);
					filter_configure();
					$savemsg = "The configuration area has been restored.";
				} else {
					/* restore the entire configuration */
					if (config_install($_FILES['conffile']['tmp_name']) == 0) {
						system_reboot();
						$savemsg = "The configuration has been restored. The firewall is now rebooting.";
					} else {
						$input_errors[] = "The configuration could not be restored.";
					}
				}
			} else {
				$input_errors[] = "The configuration could not be restored (file upload error).";
			}
		} else if ($mode == "reinstallpackages") {
			header("Location: pkg_mgr_install.php?mode=reinstallall");
			exit;
                } else if ($mode == "restore_ver") {
			$input_errors[] = "XXX - this feature may hose your config (do NOT backrev configs!) - billm";
			if ($ver2restore <> "") {
				$conf_file = "{$g['cf_conf_path']}/bak/config-" . strtotime($ver2restore) . ".xml";
                                if (config_install($conf_file) == 0) {
                                        system_reboot();
                                        $savemsg = "The configuration has been restored. The firewall is now rebooting.";
                                } else {
                                        $input_errors[] = "The configuration could not be restored.";
                                }
                        } else {
                                $input_errors[] = "No version selected.";
                        }
		}
	}
}


/* XXX - billm: begginnings of version control code
 * don't set system/version_control :)
 */
if (isset($config['system']['version_control'])) {
	$dir="{$g['cf_conf_path']}/bak";
	$old_versions = array();
	if (is_dir($dir)) {
		if ($dh = opendir($dir)) {
			while (($file = readdir($dh)) !== false) {
				if ($file != "." && $file != "..") {
					if (ereg("config-([0-9]+).xml", $file, $regs)) {
						$old_ver = array();
						$old_ver['date']=date("Y-m-d H:i:s", $regs[1]);
						$old_ver['desc']= "XXX - description not used yet";

						array_push(&$old_versions, $old_ver);
					}
                                }
                        }
                        closedir($dh);
                }
	}
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><?=gentitle("Diagnostics: Backup/restore");?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="gui.css" rel="stylesheet" type="text/css">
</head>

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
      <p class="pgtitle">Diagnostics: Backup/restore</p>
            <form action="diag_backup.php" method="post" enctype="multipart/form-data">
            <?php if ($input_errors) print_input_errors($input_errors); ?>
            <?php if ($savemsg) print_info_box($savemsg); ?>
              <table width="100%" border="0" cellspacing="0" cellpadding="6">
                <tr>
                  <td colspan="2" class="listtopic">Backup configuration</td>
                </tr>
                <tr>
                  <td width="22%" valign="baseline" class="vncell">&nbsp;</td>
                  <td width="78%" class="vtable">
                    <p> Click this button to download the system configuration
                      in XML format.<br>
                      <br>
		      Backup area: <?php spit_out_select_items("backuparea"); ?>
		      <p>
		      <input name="nopackages" type="checkbox" class="formcheckbox" id="nopackages">Do not backup package information.<p>
                      <input name="Submit" type="submit" class="formbtn" id="download" value="Download configuration"></td>
                </tr>
                <tr>
                  <td colspan="2" class="list" height="12"></td>
                </tr>
                <tr>
                  <td colspan="2" class="listtopic">Restore configuration</td>
                </tr>
                <tr>
                  <td width="22%" valign="baseline" class="vncell">&nbsp;</td>
                  <td width="78%" class="vtable">
                    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"); ?>
		      <p>
                      <input name="conffile" type="file" class="formfld" id="conffile" size="40">
                      <p>
                      <input name="Submit" type="submit" class="formbtn" id="restore" value="Restore configuration">
                      <p>
                      <strong><span class="red">Note:</span></strong><br>
                      The firewall will reboot after restoring the configuration.<br>
                  </td>
                </tr>
		<tr>
		 <td colspan="2">&nbsp;</td>
		</tr>
		<tr>
		 <td colspan="2" class="listtopic">Reinstall packages</td>
		</tr>
		<tr>
		  <td width="22%" valign="baseline" class="vncell">&nbsp;</td>
		  <td width="78%" class="vtable">
		  <p> Click this button to reinstall all system packages.  This may take a while.<br>
		  <br>
		  <input name="Submit" type="submit" class="formbtn" id="reinstallpackages" value="Reinstall packages"></td>
		</tr>
              </table>
            </form>
<?php
	/* XXX - billm: begginnings of version control code
	 * This code sucks and is really more of a PoC - it needs cleanup (and doesn't work)
	 * don't set system/version_control :)
	 */
	if (isset($config['system']['version_control'])) {
?>
	    <br>
            <form action="diag_backup.php" method="post" name="version_control" id="version_control">
            <table width="100%" border="0" cellspacing="0" cellpadding="6">
              <tr>
                <td colspan="3" class="listtopic">Config backups</td>
              </tr>
<?php foreach ($old_versions as $ver): ?>
              <tr>
		<td width="22%" valign="baseline" class="vncell"><?php echo $ver['date']; ?></td>
                <td width="56%" valign="baseline" class="vtable"><?php echo $ver['desc']; ?></td>
                <td width="22%" valign="baseline" align="right" class="vtable"><input name="ver" type="radio" class="formfld" value="<?php echo $ver; ?>"></td>
              </tr>
<?php endforeach; ?>
	   <tr>
		<td colspan="2" valign="baseline" class="vtable"></td>
                <td width="22%" valign="baseline"><input name="Submit" type="submit" class="formbtn" id="ver_restore" value="Restore version"></td>
	   </tr>
           </table>
           </form>
<?php }; ?>
<?php include("fend.inc"); ?>
</body>
</html>
OpenPOWER on IntegriCloud