2018-02-20 23:35:37 +00:00
#!/bin/sh
# Boot from a local disk installation
2018-02-21 23:58:54 +00:00
CONFIG_BOOT_GUI_MENU_NAME='Heads Boot Menu'
2018-02-20 23:35:37 +00:00
. /etc/functions
2018-12-06 23:24:28 +00:00
. /tmp/config
2018-02-20 23:35:37 +00:00
mount_boot()
{
2019-07-08 01:47:04 +00:00
2018-02-23 20:13:21 +00:00
# Mount local disk if it is not already mounted
2019-07-08 01:47:04 +00:00
while ! grep -q /boot /proc/mounts ; do
# ensure default boot device is set
if [ ! -e "$CONFIG_BOOT_DEV" ]; then
if (whiptail $CONFIG_ERROR_BG_COLOR --clear --title "ERROR: $CONFIG_BOOT_DEV missing!" \
--yesno "The /boot device $CONFIG_BOOT_DEV could not be found!\n\nYou will need to configure the correct device for /boot.\n\nWould you like to configure the /boot device now?" 30 90) then
config-gui.sh
else
# exit to main menu
break
fi
fi
# update CONFIG_BOOT_DEV
. /tmp/config
mount -o ro $CONFIG_BOOT_DEV /boot
2019-05-27 16:45:09 +00:00
if [ $? -ne 0 ]; then
if (whiptail $CONFIG_ERROR_BG_COLOR --clear --title 'ERROR: Cannot mount /boot' \
--yesno "The /boot partition at $CONFIG_BOOT_DEV could not be mounted!\n\nWould you like to configure the /boot device now?" 30 90) then
config-gui.sh
else
recovery "Unable to mount /boot"
fi
fi
2019-07-08 01:47:04 +00:00
done
2018-02-20 23:35:37 +00:00
}
2018-04-03 22:20:34 +00:00
verify_global_hashes()
{
# Check the hashes of all the files, ignoring signatures for now
check_config /boot force
TMP_HASH_FILE="/tmp/kexec/kexec_hashes.txt"
TMP_PACKAGE_TRIGGER_PRE="/tmp/kexec/kexec_package_trigger_pre.txt"
TMP_PACKAGE_TRIGGER_POST="/tmp/kexec/kexec_package_trigger_post.txt"
if cd /boot && sha256sum -c "$TMP_HASH_FILE" > /tmp/hash_output ; then
return 0
elif [ ! -f $TMP_HASH_FILE ]; then
2018-04-25 20:21:56 +00:00
if (whiptail $CONFIG_ERROR_BG_COLOR --clear --title 'ERROR: Missing Hash File!' \
2018-05-03 17:45:45 +00:00
--yesno "The file containing hashes for /boot is missing!\n\nIf you are setting this system up for the first time, select Yes to update\nyour list of checksums.\n\nOtherwise this could indicate a compromise and you should select No to\nreturn to the main menu.\n\nWould you like to update your checksums now?" 30 90) then
2019-07-05 22:04:00 +00:00
prompt_update_checksums
2018-04-03 22:20:34 +00:00
fi
return 1
else
CHANGED_FILES=$(grep -v 'OK$' /tmp/hash_output | cut -f1 -d ':')
# if files changed before package manager started, show stern warning
if [ -f "$TMP_PACKAGE_TRIGGER_PRE" ]; then
PRE_CHANGED_FILES=$(grep '^CHANGED_FILES' $TMP_PACKAGE_TRIGGER_POST | cut -f 2 -d '=' | tr -d '"')
2018-05-01 21:23:56 +00:00
TEXT="The following files failed the verification process BEFORE package updates ran:\n${PRE_CHANGED_FILES}\n\nCompare against the files Heads has detected have changed:\n${CHANGED_FILES}\n\nThis could indicate a compromise!\n\nWould you like to update your checksums anyway?"
2018-04-03 22:20:34 +00:00
# if files changed after package manager started, probably caused by package manager
elif [ -f "$TMP_PACKAGE_TRIGGER_POST" ]; then
LAST_PACKAGE_LIST=$(grep -E "^(Install|Remove|Upgrade|Reinstall):" $TMP_PACKAGE_TRIGGER_POST)
UPDATE_INITRAMFS_PACKAGE=$(grep '^UPDATE_INITRAMFS_PACKAGE' $TMP_PACKAGE_TRIGGER_POST | cut -f 2 -d '=' | tr -d '"')
if [ "$UPDATE_INITRAMFS_PACKAGE" != "" ]; then
TEXT="The following files failed the verification process AFTER package updates ran:\n${CHANGED_FILES}\n\nThis is likely due to package triggers in$UPDATE_INITRAMFS_PACKAGE.\n\nYou will need to update your checksums for all files in /boot.\n\nWould you like to update your checksums now?"
else
TEXT="The following files failed the verification process AFTER package updates ran:\n${CHANGED_FILES}\n\nThis might be due to the following package updates:\n$LAST_PACKAGE_LIST.\n\nYou will need to update your checksums for all files in /boot.\n\nWould you like to update your checksums now?"
fi
else
TEXT="The following files failed the verification process:\n${CHANGED_FILES}\n\nThis could indicate a compromise!\n\nWould you like to update your checksums now?"
fi
2018-05-01 22:20:35 +00:00
if (whiptail $CONFIG_ERROR_BG_COLOR --clear --title 'ERROR: Boot Hash Mismatch' --yesno "$TEXT" 30 90) then
2019-07-05 22:04:00 +00:00
prompt_update_checksums
2018-04-03 22:20:34 +00:00
fi
return 1
fi
}
2019-07-05 22:04:00 +00:00
prompt_update_checksums()
2018-04-03 22:20:34 +00:00
{
if (whiptail --title 'Update Checksums and sign all files in /boot' \
2018-05-01 22:20:35 +00:00
--yesno "You have chosen to update the checksums and sign all of the files in /boot.\n\nThis means that you trust that the files in /boot have not been tampered with.\n\nYou will need your GPG key to continue and this change will modify your disk.\n\nDo you want to continue?" 16 90) then
2019-07-05 22:04:00 +00:00
update_checksums
2018-04-03 22:20:34 +00:00
else
echo "Returning to the main menu"
fi
}
2018-06-19 19:27:27 +00:00
update_totp()
{
echo "Scan the QR code to add the new TOTP secret"
/bin/seal-totp
if [ -x /bin/libremkey_hotp_verification ]; then
echo "Once you have scanned the QR code, hit Enter to configure your Librem Key"
read
/bin/seal-libremkey
else
2019-07-09 03:25:57 +00:00
echo "Once you have scanned the QR code, hit Enter to continue"
2018-06-19 19:27:27 +00:00
read
fi
}
2018-02-20 23:35:37 +00:00
2019-06-19 21:27:05 +00:00
# enable USB to load modules for external kb
enable_usb
# ensure /boot is sane and mount it
2019-05-27 16:45:09 +00:00
mount_boot
2018-02-21 23:58:54 +00:00
last_half=X
2018-02-20 23:35:37 +00:00
while true; do
2018-02-23 20:13:21 +00:00
MAIN_MENU_OPTIONS=""
2018-06-21 23:04:46 +00:00
MAIN_MENU_BG_COLOR=""
2018-02-20 23:35:37 +00:00
unset totp_confirm
2018-12-04 00:09:55 +00:00
# detect whether any GPG keys exist in the keyring, if not, initialize that first
GPG_KEY_COUNT=`gpg -k 2>/dev/null | wc -l`
if [ $GPG_KEY_COUNT -eq 0 ]; then
whiptail $CONFIG_ERROR_BG_COLOR --clear --title "ERROR: GPG keyring empty!" \
2018-12-12 22:09:19 +00:00
--menu "ERROR: Heads couldn't find any GPG keys in your keyring.\n\nIf this is the first time the system has booted,\nyou should add a public GPG key to the BIOS now.\n\nIf you just reflashed a new BIOS, you'll need to add at least one\npublic key to the keyring.\n\nIf you have not just reflashed your BIOS, THIS COULD INDICATE TAMPERING!\n\nHow would you like to proceed?" 30 90 4 \
2019-02-11 22:29:13 +00:00
'G' ' Add a GPG key to the running BIOS' \
2018-12-04 00:09:55 +00:00
'i' ' Ignore error and continue to default boot menu' \
'x' ' Exit to recovery shell' \
2>/tmp/whiptail || recovery "GUI menu failed"
2018-02-23 20:13:21 +00:00
totp_confirm=$(cat /tmp/whiptail)
2018-12-04 00:09:55 +00:00
fi
if [ "$totp_confirm" = "i" -o -z "$totp_confirm" ]; then
# update the TOTP code every thirty seconds
date=`date "+%Y-%m-%d %H:%M:%S"`
seconds=`date "+%s"`
half=`expr \( $seconds % 60 \) / 30`
if [ "$CONFIG_TPM" = n ]; then
TOTP="NO TPM"
elif [ "$half" != "$last_half" ]; then
last_half=$half;
TOTP=`unseal-totp`
if [ $? -ne 0 ]; then
2018-11-30 23:32:29 +00:00
whiptail $CONFIG_ERROR_BG_COLOR --clear --title "ERROR: TOTP Generation Failed!" \
--menu "ERROR: Heads couldn't generate the TOTP code.\n\nIf this is the first time the system has booted, you should reset the TPM\nand set your own password\n\nIf you just reflashed your BIOS, you'll need to generate a new TOTP secret.\n\nIf you have not just reflashed your BIOS, THIS COULD INDICATE TAMPERING!\n\nHow would you like to proceed?" 30 90 4 \
'g' ' Generate new TOTP/HOTP secret' \
'i' ' Ignore error and continue to default boot menu' \
'p' ' Reset the TPM' \
'x' ' Exit to recovery shell' \
2>/tmp/whiptail || recovery "GUI menu failed"
2018-12-04 00:09:55 +00:00
totp_confirm=$(cat /tmp/whiptail)
2018-11-30 23:32:29 +00:00
fi
2018-02-23 20:13:21 +00:00
fi
fi
if [ "$totp_confirm" = "i" -o -z "$totp_confirm" ]; then
2018-06-19 19:27:27 +00:00
if [ -x /bin/libremkey_hotp_verification ]; then
HOTP=`unseal-hotp`
enable_usb
2018-12-05 22:51:53 +00:00
if ! libremkey_hotp_verification info ; then
whiptail $CONFIG_WARNING_BG_COLOR --clear --title 'WARNING: Please Insert Your Librem Key' --msgbox "Your Librem Key was not detected.\n\nPlease insert your Librem Key" 30 90
fi
2018-06-19 19:27:27 +00:00
# Don't output HOTP codes to screen, so as to make replay attacks harder
libremkey_hotp_verification check $HOTP
case "$?" in
0 )
2018-07-03 19:40:52 +00:00
HOTP="Success"
2018-06-19 19:27:27 +00:00
;;
4 )
2018-07-03 19:40:52 +00:00
HOTP="Invalid code"
2018-06-21 23:04:46 +00:00
MAIN_MENU_BG_COLOR=$CONFIG_ERROR_BG_COLOR
2018-06-19 19:27:27 +00:00
;;
* )
2018-07-03 19:40:52 +00:00
HOTP="Error checking code, Insert Librem Key and retry"
2018-06-21 23:30:35 +00:00
MAIN_MENU_BG_COLOR=$CONFIG_WARNING_BG_COLOR
2018-06-19 19:27:27 +00:00
;;
esac
else
HOTP='N/A'
fi
2018-06-21 23:04:46 +00:00
whiptail $MAIN_MENU_BG_COLOR --clear --title "$CONFIG_BOOT_GUI_MENU_NAME" \
2018-06-19 19:27:27 +00:00
--menu "$date\nTOTP: $TOTP | HOTP: $HOTP" 20 90 10 \
2018-02-23 20:13:21 +00:00
'y' ' Default boot' \
2019-05-26 17:00:41 +00:00
'r' ' Refresh TOTP/HOTP' \
2019-06-27 22:30:54 +00:00
'a' ' Options -->' \
2019-05-24 09:23:28 +00:00
'P' ' Power Off' \
2018-03-14 17:14:22 +00:00
2>/tmp/whiptail || recovery "GUI menu failed"
totp_confirm=$(cat /tmp/whiptail)
fi
if [ "$totp_confirm" = "a" ]; then
2019-06-27 22:30:54 +00:00
whiptail --clear --title "HEADS Options" \
--menu "" 20 90 10 \
'o' ' Boot Options -->' \
2019-02-11 22:29:13 +00:00
't' ' TPM/TOTP/HOTP Options -->' \
2018-03-14 17:14:22 +00:00
's' ' Update checksums and sign all files in /boot' \
2018-12-06 18:43:34 +00:00
'c' ' Change configuration settings -->' \
2018-05-17 22:31:23 +00:00
'f' ' Flash/Update the BIOS -->' \
2019-02-11 22:29:13 +00:00
'G' ' GPG Options -->' \
2019-05-24 21:25:22 +00:00
'x' ' Exit to recovery shell' \
2018-03-14 17:14:22 +00:00
'r' ' <-- Return to main menu' \
2018-02-23 20:13:21 +00:00
2>/tmp/whiptail || recovery "GUI menu failed"
totp_confirm=$(cat /tmp/whiptail)
fi
2018-11-30 23:32:29 +00:00
if [ "$totp_confirm" = "o" ]; then
2019-06-27 22:30:54 +00:00
whiptail --clear --title "Boot Options" \
2018-11-30 23:32:29 +00:00
--menu "Select A Boot Option" 20 90 10 \
'm' ' Show OS boot menu' \
'u' ' USB boot' \
'i' ' Ignore tampering and force a boot (Unsafe!)' \
'r' ' <-- Return to main menu' \
2>/tmp/whiptail || recovery "GUI menu failed"
totp_confirm=$(cat /tmp/whiptail)
fi
2019-02-11 22:29:13 +00:00
if [ "$totp_confirm" = "t" ]; then
whiptail --clear --title "TPM/TOTP/HOTP Options" \
--menu "Select An Option" 20 90 10 \
'g' ' Generate new TOTP/HOTP secret' \
'p' ' Reset the TPM' \
'n' ' TOTP/HOTP does not match after refresh, troubleshoot' \
'r' ' <-- Return to main menu' \
2>/tmp/whiptail || recovery "GUI menu failed"
totp_confirm=$(cat /tmp/whiptail)
fi
2018-02-23 20:13:21 +00:00
if [ "$totp_confirm" = "x" ]; then
recovery "User requested recovery shell"
fi
if [ "$totp_confirm" = "r" ]; then
continue
fi
if [ "$totp_confirm" = "n" ]; then
2018-06-19 19:27:27 +00:00
if (whiptail $CONFIG_WARNING_BG_COLOR --title "TOTP/HOTP code mismatched" \
--yesno "TOTP/HOTP code mismatches could indicate either TPM tampering or clock drift:\n\nTo correct clock drift: 'date -s HH:MM:SS'\nand save it to the RTC: 'hwclock -w'\nthen reboot and try again.\n\nWould you like to exit to a recovery console?" 30 90) then
2018-02-23 20:13:21 +00:00
echo ""
echo "To correct clock drift: 'date -s HH:MM:SS'"
echo "and save it to the RTC: 'hwclock -w'"
echo "then reboot and try again"
echo ""
2018-06-19 19:27:27 +00:00
recovery "TOTP/HOTP mismatch"
2018-02-23 20:13:21 +00:00
else
continue
fi
fi
if [ "$totp_confirm" = "u" ]; then
exec /bin/usb-init
continue
fi
if [ "$totp_confirm" = "g" ]; then
2018-06-19 19:27:27 +00:00
if (whiptail --title 'Generate new TOTP/HOTP secret' \
2018-05-01 22:20:35 +00:00
--yesno "This will erase your old secret and replace it with a new one!\n\nDo you want to proceed?" 16 90) then
2018-06-19 19:27:27 +00:00
update_totp
2018-03-09 00:36:56 +00:00
else
echo "Returning to the main menu"
fi
continue
fi
if [ "$totp_confirm" = "p" ]; then
if (whiptail --title 'Reset the TPM' \
2018-06-19 19:27:27 +00:00
--yesno "This will clear the TPM and TPM password, replace them with new ones!\n\nDo you want to proceed?" 16 90) then
2018-03-09 00:36:56 +00:00
/bin/tpm-reset
2018-04-03 22:20:34 +00:00
2018-06-19 19:27:27 +00:00
# now that the TPM is reset, remove invalid TPM counter files
2018-04-03 22:20:34 +00:00
mount_boot
mount -o rw,remount /boot
2018-06-20 16:20:39 +00:00
rm -f /boot/kexec_rollback.txt
2018-06-19 19:27:27 +00:00
# create Heads TPM counter before any others
check_tpm_counter /boot/kexec_rollback.txt \
|| die "Unable to find/create tpm counter"
counter="$TPM_COUNTER"
2018-06-19 23:28:37 +00:00
increment_tpm_counter $counter \
|| die "Unable to increment tpm counter"
2018-06-19 19:27:27 +00:00
sha256sum /tmp/counter-$counter > /boot/kexec_rollback.txt \
|| die "Unable to create rollback file"
2018-04-03 22:20:34 +00:00
mount -o ro,remount /boot
2018-06-19 19:27:27 +00:00
update_totp
2018-02-23 20:13:21 +00:00
else
echo "Returning to the main menu"
fi
continue
fi
if [ "$totp_confirm" = "m" ]; then
# Try to select a kernel from the menu
mount_boot
2018-04-03 22:20:34 +00:00
verify_global_hashes
if [ $? -ne 0 ]; then
continue
fi
2018-02-23 20:13:21 +00:00
kexec-select-boot -m -b /boot -c "grub.cfg" -g
continue
fi
2018-03-05 22:46:15 +00:00
if [ "$totp_confirm" = "i" ]; then
# Run the menu selection in "force" mode, bypassing hash checks
2018-04-25 20:21:56 +00:00
if (whiptail $CONFIG_WARNING_BG_COLOR --title 'Unsafe Forced Boot Selected!' \
2018-05-01 22:20:35 +00:00
--yesno "WARNING: You have chosen to skip all tamper checks and boot anyway.\n\nThis is an unsafe option!\n\nDo you want to proceed?" 16 90) then
2018-03-05 22:46:15 +00:00
mount_boot
kexec-select-boot -m -b /boot -c "grub.cfg" -g -f
else
echo "Returning to the main menu"
fi
continue
fi
2018-03-14 17:14:22 +00:00
if [ "$totp_confirm" = "s" ]; then
2019-07-05 22:04:00 +00:00
prompt_update_checksums
2018-03-14 17:14:22 +00:00
continue
fi
2018-12-06 18:43:34 +00:00
if [ "$totp_confirm" = "c" ]; then
config-gui.sh
continue
fi
2018-05-11 19:27:50 +00:00
if [ "$totp_confirm" = "f" ]; then
2018-05-11 21:08:31 +00:00
flash-gui.sh
2018-05-11 19:27:50 +00:00
continue
fi
2019-02-11 22:29:13 +00:00
if [ "$totp_confirm" = "G" ]; then
gpg-gui.sh
continue
fi
2019-05-24 09:23:28 +00:00
if [ "$totp_confirm" = "P" ]; then
poweroff
fi
2018-02-23 20:13:21 +00:00
if [ "$totp_confirm" = "y" -o -n "$totp_confirm" ]; then
# Try to boot the default
mount_boot
2018-04-03 22:20:34 +00:00
verify_global_hashes
if [ $? -ne 0 ]; then
continue
fi
DEFAULT_FILE=`find /boot/kexec_default.*.txt 2>/dev/null | head -1`
2018-02-23 20:13:21 +00:00
if [ -r "$DEFAULT_FILE" ]; then
2018-03-08 19:41:44 +00:00
kexec-select-boot -b /boot -c "grub.cfg" -g \
2018-02-23 20:13:21 +00:00
|| recovery "Failed default boot"
else
if (whiptail --title 'No Default Boot Option Configured' \
2018-05-01 22:20:35 +00:00
--yesno "There is no default boot option configured yet.\nWould you like to load a menu of boot options?\nOtherwise you will return to the main menu." 16 90) then
2018-02-23 20:13:21 +00:00
kexec-select-boot -m -b /boot -c "grub.cfg" -g
else
echo "Returning to the main menu"
fi
continue
fi
fi
2018-02-20 23:35:37 +00:00
done
recovery "Something failed during boot"