rc.recover 7.33 KB
Newer Older
1 2 3 4
#!/usr/local/bin/php
<?php

/*
Franco Fichtner's avatar
Franco Fichtner committed
5
 * Copyright (c) 2015-2016 Franco Fichtner <franco@opnsense.org>
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
 *
 * 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 BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
 */

$etc_group = <<<EOF
# \$FreeBSD$
#
wheel:*:0:root
daemon:*:1:
kmem:*:2:
sys:*:3:
tty:*:4:
operator:*:5:root
mail:*:6:
bin:*:7:
news:*:8:
man:*:9:
games:*:13:
ftp:*:14:
staff:*:20:
sshd:*:22:
smmsp:*:25:
mailnull:*:26:
guest:*:31:
bind:*:53:
unbound:*:59:
proxy:*:62:
authpf:*:63:
_pflogd:*:64:
_dhcp:*:65:
uucp:*:66:
dialer:*:68:
network:*:69:
audit:*:77:
www:*:80:
hast:*:845:
nogroup:*:65533:
nobody:*:65534:
Franco Fichtner's avatar
Franco Fichtner committed
65

66 67 68 69 70 71 72
EOF;

$etc_master_passwd = <<<EOF
# \$FreeBSD$
#
root::0:0::0:0:Charlie &:/root:/bin/csh
toor:*:0:0::0:0:Bourne-again Superuser:/root:
73
installer:*:0:0::0:0:Installer Superuser:/root:/usr/sbin/nologin
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
daemon:*:1:1::0:0:Owner of many system processes:/root:/usr/sbin/nologin
operator:*:2:5::0:0:System &:/:/usr/sbin/nologin
bin:*:3:7::0:0:Binaries Commands and Source:/:/usr/sbin/nologin
tty:*:4:65533::0:0:Tty Sandbox:/:/usr/sbin/nologin
kmem:*:5:65533::0:0:KMem Sandbox:/:/usr/sbin/nologin
games:*:7:13::0:0:Games pseudo-user:/usr/games:/usr/sbin/nologin
news:*:8:8::0:0:News Subsystem:/:/usr/sbin/nologin
man:*:9:9::0:0:Mister Man Pages:/usr/share/man:/usr/sbin/nologin
sshd:*:22:22::0:0:Secure Shell Daemon:/var/empty:/usr/sbin/nologin
smmsp:*:25:25::0:0:Sendmail Submission User:/var/spool/clientmqueue:/usr/sbin/nologin
mailnull:*:26:26::0:0:Sendmail Default User:/var/spool/mqueue:/usr/sbin/nologin
bind:*:53:53::0:0:Bind Sandbox:/:/usr/sbin/nologin
unbound:*:59:59::0:0:Unbound DNS Resolver:/var/unbound:/usr/sbin/nologin
proxy:*:62:62::0:0:Packet Filter pseudo-user:/nonexistent:/usr/sbin/nologin
_pflogd:*:64:64::0:0:pflogd privsep user:/var/empty:/usr/sbin/nologin
_dhcp:*:65:65::0:0:dhcp programs:/var/empty:/usr/sbin/nologin
uucp:*:66:66::0:0:UUCP pseudo-user:/var/spool/uucppublic:/usr/local/libexec/uucp/uucico
pop:*:68:6::0:0:Post Office Owner:/nonexistent:/usr/sbin/nologin
auditdistd:*:78:77::0:0:Auditdistd unprivileged user:/var/empty:/usr/sbin/nologin
www:*:80:80::0:0:World Wide Web Owner:/nonexistent:/usr/sbin/nologin
hast:*:845:845::0:0:HAST unprivileged user:/var/empty:/usr/sbin/nologin
nobody:*:65534:65534::0:0:Unprivileged user:/nonexistent:/usr/sbin/nologin
Franco Fichtner's avatar
Franco Fichtner committed
96

97 98
EOF;

99 100 101 102 103 104 105 106 107 108 109
$etc_shells = <<<EOF
# \$FreeBSD$
#
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/sh
/bin/csh
/bin/tcsh
/usr/local/etc/rc.initial
110
/usr/local/etc/rc.installer
Franco Fichtner's avatar
Franco Fichtner committed
111

112 113
EOF;

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
$etc_ttys = <<<EOF
#
# \$FreeBSD$
#	@(#)ttys	5.1 (Berkeley) 4/17/89
#
# This file specifies various information about terminals on the system.
# It is used by several different programs.  Common entries for the
# various columns include:
#
# name  The name of the terminal device.
#
# getty The program to start running on the terminal.  Typically a
#       getty program, as the name implies.  Other common entries
#       include none, when no getty is needed, and xdm, to start the
#       X Window System.
#
# type The initial terminal type for this port.  For hardwired
#      terminal lines, this will contain the type of terminal used.
#      For virtual consoles, the correct type is typically xterm.
#      Other common values include dialup for incoming modem ports, and
#      unknown when the terminal type cannot be predetermined.
#
# status Must be on or off.  If on, init will run the getty program on
#        the specified port.  If the word "secure" appears, this tty
#        allows root login.
#
# name	getty				type	status		comments
#
# If console is marked "insecure", then init will ask for the root password
# when going to single-user mode.
console	none				unknown	off secure
#
ttyv0	"/usr/libexec/getty Pc"		xterm	on  secure
# Virtual terminals
ttyv1	"/usr/libexec/getty Pc"		xterm	on  secure
ttyv2	"/usr/libexec/getty Pc"		xterm	on  secure
ttyv3	"/usr/libexec/getty Pc"		xterm	on  secure
ttyv4	"/usr/libexec/getty Pc"		xterm	on  secure
ttyv5	"/usr/libexec/getty Pc"		xterm	on  secure
ttyv6	"/usr/libexec/getty Pc"		xterm	on  secure
ttyv7	"/usr/libexec/getty Pc"		xterm	on  secure
ttyv8	"/usr/local/bin/xdm -nodaemon"	xterm	off secure
# Serial terminals
# The 'dialup' keyword identifies dialin lines to login, fingerd etc.
ttyu0	"/usr/libexec/getty 3wire"	vt100	onifconsole secure
ttyu1	"/usr/libexec/getty 3wire"	vt100	onifconsole secure
ttyu2	"/usr/libexec/getty 3wire"	vt100	onifconsole secure
ttyu3	"/usr/libexec/getty 3wire"	vt100	onifconsole secure
# Dumb console
dcons	"/usr/libexec/getty std.9600"	vt100	off secure

EOF;

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
function recover_ports()
{
	$actions = array(
		'pre-install',
		'post-install'
	);

	exec('/usr/sbin/pkg query -e \'%#U > 0 || %#G > 0\' %n', $pkgs);

	if ($pkgs[0] == '') {
		return;
	}

	$tempname = tempnam('/tmp', 'recover.');
	$tempfile = fopen($tempname, 'w');

	foreach ($pkgs as $pkg) {
		$raw = exec('/usr/sbin/pkg info --raw --raw-format json-compact ' . $pkg);
		$info = json_decode($raw, true);
		foreach($actions as $action) {
			if (!isset($info['scripts'][$action])) {
				continue;
			}

			ftruncate($tempfile, 0);
			rewind($tempfile);
			fwrite($tempfile, $info['scripts'][$action] . PHP_EOL);
			passthru('/bin/sh ' . $tempname);
		}
	}

	fclose($tempfile);
	unlink($tempname);
}

202 203 204 205
function recover_rebuild()
{
	passthru('/usr/sbin/pwd_mkdb -p /etc/master.passwd');
	passthru('/usr/sbin/pwd_mkdb /etc/master.passwd');
206
	passthru('/bin/sync');
207 208
}

209
function recover_base($etc_group, $etc_master_passwd, $etc_shells, $etc_ttys)
210
{
Franco Fichtner's avatar
Franco Fichtner committed
211
	echo "===> Restoring /etc/group\n";
212
	file_put_contents('/etc/group', $etc_group);
Franco Fichtner's avatar
Franco Fichtner committed
213 214

	echo "===> Restoring /etc/master.passwd\n";
215
	file_put_contents('/etc/master.passwd', $etc_master_passwd);
Franco Fichtner's avatar
Franco Fichtner committed
216 217

	echo "===> Restoring /etc/shells\n";
218
	file_put_contents('/etc/shells', $etc_shells);
Franco Fichtner's avatar
Franco Fichtner committed
219

220 221 222
	echo "===> Restoring /etc/ttys\n";
	file_put_contents('/etc/ttys', $etc_ttys);

223 224 225 226 227 228 229 230 231 232 233 234 235
	recover_rebuild();
}

function recover_pkg()
{
	recover_ports();
	recover_rebuild();
}

$stage = isset($argv[1]) ? $argv[1] : 'both';

switch ($stage) {
	case 'base':
236
		recover_base($etc_group, $etc_master_passwd, $etc_shells, $etc_ttys);
237 238 239 240 241
		break;
	case 'pkg':
		recover_pkg();
		break;
	default:
242
		recover_base($etc_group, $etc_master_passwd, $etc_shells, $etc_ttys);
243 244 245
		recover_pkg();
		break;
}