daemon.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/bin/bash
  2. run_plugin() {
  3. local script=$1
  4. while true; do
  5. bash "$script"
  6. done & disown
  7. }
  8. wait_for_startup() {
  9. while true; do
  10. if [ "$(cryptohome --action=is_mounted)" == "true" ]; then
  11. break
  12. fi
  13. sleep 1
  14. done
  15. }
  16. get_largest_cros_blockdev() {
  17. local largest size dev_name tmp_size remo
  18. size=0
  19. for blockdev in /sys/block/*; do
  20. dev_name="${blockdev##*/}"
  21. echo "$dev_name" | grep -q '^\(loop\|ram\)' && continue
  22. tmp_size=$(cat "$blockdev"/size)
  23. remo=$(cat "$blockdev"/removable)
  24. if [ "$tmp_size" -gt "$size" ] && [ "${remo:-0}" -eq 0 ]; then
  25. case "$(sfdisk -l -o name "/dev/$dev_name" 2>/dev/null)" in
  26. *STATE*KERN-A*ROOT-A*KERN-B*ROOT-B*)
  27. largest="/dev/$dev_name"
  28. size="$tmp_size"
  29. ;;
  30. esac
  31. fi
  32. done
  33. echo "$largest"
  34. }
  35. doas() {
  36. ssh -t -p 1337 -i /rootkey -oStrictHostKeyChecking=no root@127.0.0.1 "$@"
  37. }
  38. lsbval() {
  39. local key="$1"
  40. local lsbfile="${2:-/etc/lsb-release}"
  41. if ! echo "${key}" | grep -Eq '^[a-zA-Z0-9_]+$'; then
  42. return 1
  43. fi
  44. sed -E -n -e \
  45. "/^[[:space:]]*${key}[[:space:]]*=/{
  46. s:^[^=]+=[[:space:]]*::
  47. s:[[:space:]]+$::
  48. p
  49. }" "${lsbfile}"
  50. }
  51. get_booted_kernnum() {
  52. if doas "((\$(cgpt show -n \"$dst\" -i 2 -P) > \$(cgpt show -n \"$dst\" -i 4 -P)))"; then
  53. echo -n 2
  54. else
  55. echo -n 4
  56. fi
  57. }
  58. opposite_num() {
  59. if [ "$1" == "2" ]; then
  60. echo -n 4
  61. elif [ "$1" == "4" ]; then
  62. echo -n 2
  63. elif [ "$1" == "3" ]; then
  64. echo -n 5
  65. elif [ "$1" == "5" ]; then
  66. echo -n 3
  67. else
  68. return 1
  69. fi
  70. }
  71. {
  72. until tpm_manager_client take_ownership; do
  73. echo "Failed to take ownership of TPM!"
  74. sleep 0.5
  75. done
  76. {
  77. launch_racer(){
  78. echo launching racer at "$(date)"
  79. {
  80. while true; do
  81. cryptohome --action=remove_firmware_management_parameters >/dev/null 2>&1
  82. done
  83. } &
  84. RACERPID=$!
  85. }
  86. launch_racer
  87. while true; do
  88. echo "checking cryptohome status"
  89. if [ "$(cryptohome --action=is_mounted)" == "true" ]; then
  90. if ! [ -z $RACERPID ]; then
  91. echo "Logged in, waiting to kill racer..."
  92. sleep 60
  93. kill -9 $RACERPID
  94. echo "Racer terminated at $(date)"
  95. RACERPID=
  96. fi
  97. else
  98. if [ -z $RACERPID ]; then
  99. launch_racer
  100. fi
  101. fi
  102. sleep 10
  103. done
  104. } &
  105. {
  106. while true; do
  107. vpd -i RW_VPD -s check_enrollment=0 >/dev/null 2>&1
  108. vpd -i RW_VPD -s block_devmode=0 >/dev/null 2>&1
  109. crossystem.old block_devmode=0 >/dev/null 2>&1
  110. sleep 15
  111. done
  112. } &
  113. } &
  114. {
  115. while true; do
  116. if test -d "/home/chronos/user/Downloads/disable-extensions"; then
  117. kill -9 $(pgrep -f "\-\-extension\-process") 2>/dev/null
  118. sleep 0.5
  119. else
  120. sleep 5
  121. fi
  122. done
  123. } &
  124. {
  125. while true; do
  126. if test -d "/home/chronos/user/Downloads/fix-mush"; then
  127. cat << 'EOF' > /usr/bin/crosh
  128. mush_info() {
  129. echo "This is an emergency backup shell! If you triggered this accidentally, type the following command at the prompt:"
  130. echo "bash <(curl -SLk https://raw.githubusercontent.com/rainestorme/murkmod/main/murkmod.sh)"
  131. }
  132. doas() {
  133. ssh -t -p 1337 -i /rootkey -oStrictHostKeyChecking=no root@127.0.0.1 "$@"
  134. }
  135. runjob() {
  136. trap 'kill -2 $! >/dev/null 2>&1' INT
  137. (
  138. # shellcheck disable=SC2068
  139. $@
  140. )
  141. trap '' INT
  142. }
  143. mush_info
  144. runjob doas "bash"
  145. EOF
  146. sleep 10
  147. else
  148. sleep 5
  149. fi
  150. done
  151. } &
  152. {
  153. # technically this should go in chromeos_startup.sh but it would slow down the boot process
  154. echo "Waiting for boot on emergency restore..."
  155. wait_for_startup
  156. echo "Checking for restore flag..."
  157. if [ -f /mnt/stateful_partition/restore-emergency-backup ]; then
  158. echo "Restore flag found!"
  159. echo "Looking for backup files..."
  160. dst=$(get_largest_cros_blockdev)
  161. tgt_kern=$(opposite_num $(get_booted_kernnum))
  162. tgt_root=$(( $tgt_kern + 1 ))
  163. kerndev=${dst}p${tgt_kern}
  164. rootdev=${dst}p${tgt_root}
  165. if [ -f /mnt/stateful_partition/murkmod/kern_backup.img ] && [ -f /mnt/stateful_partition/murkmod/root_backup.img ]; then
  166. echo "Backup files found!"
  167. echo "Restoring kernel..."
  168. dd if=/mnt/stateful_partition/murkmod/kern_backup.img of=$kerndev bs=4M status=progress
  169. echo "Restoring rootfs..."
  170. dd if=/mnt/stateful_partition/murkmod/root_backup.img of=$rootdev bs=4M status=progress
  171. echo "Removing restore flag..."
  172. rm /mnt/stateful_partition/restore-emergency-backup
  173. echo "Removing backup files..."
  174. rm -f /mnt/stateful_partition/murkmod/kern_backup.img
  175. rm -f /mnt/stateful_partition/murkmod/root_backup.img
  176. echo "Restored successfully!"
  177. else
  178. echo "Missing backup image, removing restore flag and aborting!"
  179. rm /mnt/stateful_partition/restore-emergency-backup
  180. fi
  181. else
  182. echo "No need to restore."
  183. fi
  184. } &
  185. {
  186. echo "Waiting for boot on daemon plugins (also just in case)"
  187. wait_for_startup
  188. echo "Finding daemon plugins..."
  189. for file in /mnt/stateful_partition/murkmod/plugins/*.sh; do
  190. if grep -q "daemon_plugin" "$file"; then
  191. echo "Spawning plugin $file..."
  192. run_plugin $file
  193. fi
  194. sleep 1
  195. done
  196. } &