-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdaemon.sh
220 lines (196 loc) · 5.74 KB
/
daemon.sh
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
#!/bin/bash
run_plugin() {
local script=$1
while true; do
bash "$script"
done & disown
}
wait_for_startup() {
while true; do
if [ "$(cryptohome --action=is_mounted)" == "true" ]; then
break
fi
sleep 1
done
}
get_largest_cros_blockdev() {
local largest size dev_name tmp_size remo
size=0
for blockdev in /sys/block/*; do
dev_name="${blockdev##*/}"
echo "$dev_name" | grep -q '^\(loop\|ram\)' && continue
tmp_size=$(cat "$blockdev"/size)
remo=$(cat "$blockdev"/removable)
if [ "$tmp_size" -gt "$size" ] && [ "${remo:-0}" -eq 0 ]; then
case "$(sfdisk -l -o name "/dev/$dev_name" 2>/dev/null)" in
*STATE*KERN-A*ROOT-A*KERN-B*ROOT-B*)
largest="/dev/$dev_name"
size="$tmp_size"
;;
esac
fi
done
echo "$largest"
}
doas() {
ssh -t -p 1337 -i /rootkey -oStrictHostKeyChecking=no [email protected] "$@"
}
lsbval() {
local key="$1"
local lsbfile="${2:-/etc/lsb-release}"
if ! echo "${key}" | grep -Eq '^[a-zA-Z0-9_]+$'; then
return 1
fi
sed -E -n -e \
"/^[[:space:]]*${key}[[:space:]]*=/{
s:^[^=]+=[[:space:]]*::
s:[[:space:]]+$::
p
}" "${lsbfile}"
}
get_booted_kernnum() {
if doas "((\$(cgpt show -n \"$dst\" -i 2 -P) > \$(cgpt show -n \"$dst\" -i 4 -P)))"; then
echo -n 2
else
echo -n 4
fi
}
opposite_num() {
if [ "$1" == "2" ]; then
echo -n 4
elif [ "$1" == "4" ]; then
echo -n 2
elif [ "$1" == "3" ]; then
echo -n 5
elif [ "$1" == "5" ]; then
echo -n 3
else
return 1
fi
}
{
until tpm_manager_client take_ownership; do
echo "Failed to take ownership of TPM!"
sleep 0.5
done
{
launch_racer(){
echo launching racer at "$(date)"
{
while true; do
cryptohome --action=remove_firmware_management_parameters >/dev/null 2>&1
done
} &
RACERPID=$!
}
launch_racer
while true; do
echo "checking cryptohome status"
if [ "$(cryptohome --action=is_mounted)" == "true" ]; then
if ! [ -z $RACERPID ]; then
echo "Logged in, waiting to kill racer..."
sleep 60
kill -9 $RACERPID
echo "Racer terminated at $(date)"
RACERPID=
fi
else
if [ -z $RACERPID ]; then
launch_racer
fi
fi
sleep 10
done
} &
{
while true; do
vpd -i RW_VPD -s check_enrollment=0 >/dev/null 2>&1
vpd -i RW_VPD -s block_devmode=0 >/dev/null 2>&1
crossystem.old block_devmode=0 >/dev/null 2>&1
sleep 15
done
} &
} &
{
while true; do
if test -d "/home/chronos/user/Downloads/disable-extensions"; then
kill -9 $(pgrep -f "\-\-extension\-process") 2>/dev/null
sleep 0.5
else
sleep 5
fi
done
} &
{
while true; do
if test -d "/home/chronos/user/Downloads/fix-mush"; then
cat << 'EOF' > /usr/bin/crosh
mush_info() {
echo "This is an emergency backup shell! If you triggered this accidentally, type the following command at the prompt:"
echo "bash <(curl -SLk https://raw.githubusercontent.com/rainestorme/murkmod/main/murkmod.sh)"
}
doas() {
ssh -t -p 1337 -i /rootkey -oStrictHostKeyChecking=no [email protected] "$@"
}
runjob() {
trap 'kill -2 $! >/dev/null 2>&1' INT
(
# shellcheck disable=SC2068
$@
)
trap '' INT
}
mush_info
runjob doas "bash"
EOF
sleep 10
else
sleep 5
fi
done
} &
{
# technically this should go in chromeos_startup.sh but it would slow down the boot process
echo "Waiting for boot on emergency restore..."
wait_for_startup
echo "Checking for restore flag..."
if [ -f /mnt/stateful_partition/restore-emergency-backup ]; then
echo "Restore flag found!"
echo "Looking for backup files..."
dst=$(get_largest_cros_blockdev)
tgt_kern=$(opposite_num $(get_booted_kernnum))
tgt_root=$(( $tgt_kern + 1 ))
kerndev=${dst}p${tgt_kern}
rootdev=${dst}p${tgt_root}
if [ -f /mnt/stateful_partition/murkmod/kern_backup.img ] && [ -f /mnt/stateful_partition/murkmod/root_backup.img ]; then
echo "Backup files found!"
echo "Restoring kernel..."
dd if=/mnt/stateful_partition/murkmod/kern_backup.img of=$kerndev bs=4M status=progress
echo "Restoring rootfs..."
dd if=/mnt/stateful_partition/murkmod/root_backup.img of=$rootdev bs=4M status=progress
echo "Removing restore flag..."
rm /mnt/stateful_partition/restore-emergency-backup
echo "Removing backup files..."
rm -f /mnt/stateful_partition/murkmod/kern_backup.img
rm -f /mnt/stateful_partition/murkmod/root_backup.img
echo "Restored successfully!"
else
echo "Missing backup image, removing restore flag and aborting!"
rm /mnt/stateful_partition/restore-emergency-backup
fi
else
echo "No need to restore."
fi
} &
{
echo "Waiting for boot on daemon plugins (also just in case)"
wait_for_startup
echo "Finding daemon plugins..."
for file in /mnt/stateful_partition/murkmod/plugins/*.sh; do
if grep -q "daemon_plugin" "$file"; then
echo "Spawning plugin $file..."
run_plugin $file
fi
sleep 1
done
} &