2015-09-29 20:13:16 -04:00
|
|
|
#!/bin/bash
|
2015-06-05 08:04:44 -04:00
|
|
|
#Hashes all files in /boot to check them during early boot
|
2015-09-26 05:57:14 -04:00
|
|
|
#Exit codes: 0 = success, 1 = checksum mbr mismatch, 2 = checksum /boot mismatch,
|
|
|
|
#3 = checksum mbr/boot mismatch, 4 = not root, 5 = no hasher found, 6 = wrong usage,
|
2015-09-29 14:17:52 -04:00
|
|
|
#7 = write error, 8 = dd error, 9 config file error
|
2015-06-05 08:04:44 -04:00
|
|
|
|
2015-09-29 20:16:59 -04:00
|
|
|
VERSION="0.7.4"
|
2015-09-29 18:26:27 -04:00
|
|
|
PATH="/bin:/usr/bin:/sbin:/usr/sbin:${PATH}"
|
2015-06-07 19:43:23 -04:00
|
|
|
|
2015-06-05 08:04:44 -04:00
|
|
|
DIGEST_FILE="/var/lib/hashboot.digest"
|
|
|
|
LOG_FILE="/tmp/hashboot.log"
|
2015-09-29 20:16:59 -04:00
|
|
|
MBR_DEVICE=""
|
2015-09-26 05:57:14 -04:00
|
|
|
MBR_TMP="/tmp/mbr"
|
2015-06-05 08:04:44 -04:00
|
|
|
BACKUP_FILE="/var/cache/boot-backup.tar.gz"
|
|
|
|
HASHER=""
|
2015-09-29 10:14:21 -04:00
|
|
|
BOOT_MOUNTED=0
|
2015-09-29 14:17:52 -04:00
|
|
|
CONFIG_FILE="/etc/hashboot.cfg"
|
2015-06-05 08:04:44 -04:00
|
|
|
|
2015-09-29 18:24:56 -04:00
|
|
|
|
2015-06-05 08:04:44 -04:00
|
|
|
#Umount /boot if we mounted it, exit with given exit code
|
2015-09-29 20:13:16 -04:00
|
|
|
die ()
|
2015-06-05 08:04:44 -04:00
|
|
|
{
|
2015-09-29 10:35:59 -04:00
|
|
|
if [ ${BOOT_MOUNTED} -gt 0 ]
|
2015-09-29 09:55:28 -04:00
|
|
|
then
|
2015-09-29 10:14:21 -04:00
|
|
|
umount /boot
|
2015-09-29 09:55:28 -04:00
|
|
|
fi
|
2015-06-05 08:04:44 -04:00
|
|
|
|
2015-09-29 15:05:29 -04:00
|
|
|
[ -z "${2}" ] || echo "${2}" >&2
|
2015-09-29 09:55:28 -04:00
|
|
|
exit ${1}
|
2015-06-05 08:04:44 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 20:16:59 -04:00
|
|
|
read_config ()
|
|
|
|
{
|
|
|
|
#Look for config file and set ${MBR_DEVICE}.
|
|
|
|
if [ -f ${CONFIG_FILE} ]
|
|
|
|
then
|
|
|
|
MBR_DEVICE=$(grep ^mbr_device ${CONFIG_FILE} | awk '{print $3}')
|
|
|
|
[ $? != 0 ] && die 9 "Error reading config file"
|
|
|
|
#If not found, create one and ask for ${MBR_DEVICE}
|
|
|
|
else
|
|
|
|
echo -n "Which device contains the MBR? [/dev/sda] "
|
|
|
|
read -r MBR_DEVICE
|
|
|
|
[ -z "${MBR_DEVICE}" ] && MBR_DEVICE="/dev/sda"
|
|
|
|
echo "#Device with the MBR on it" > ${CONFIG_FILE}
|
|
|
|
echo "mbr_device = ${MBR_DEVICE}" >> ${CONFIG_FILE}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-06-05 08:04:44 -04:00
|
|
|
#If we're not root: exit
|
|
|
|
if [ ${UID} -ne 0 ]
|
|
|
|
then
|
2015-09-29 15:05:29 -04:00
|
|
|
die 4 "You have to be root"
|
2015-06-05 08:04:44 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
#If /boot is in fstab but not mounted: mount, mark as mounted
|
|
|
|
if grep -q '/boot.*noauto' /etc/fstab && ! grep -q /boot /etc/mtab
|
|
|
|
then
|
2015-09-29 09:55:28 -04:00
|
|
|
mount /boot
|
|
|
|
BOOT_MOUNTED=1
|
2015-06-05 08:04:44 -04:00
|
|
|
fi
|
|
|
|
|
2015-09-29 18:24:56 -04:00
|
|
|
|
2015-06-05 08:04:44 -04:00
|
|
|
if [ "${1}" == "index" ]
|
|
|
|
then
|
2015-09-29 18:24:56 -04:00
|
|
|
#Try different hashers, use the most secure
|
2015-09-29 20:13:16 -04:00
|
|
|
HASHER=$(/usr/bin/which sha512sum 2> /dev/null)
|
|
|
|
test -z "${HASHER}" && HASHER=$(/usr/bin/which sha384sum 2> /dev/null)
|
|
|
|
test -z "${HASHER}" && HASHER=$(/usr/bin/which sha256sum 2> /dev/null)
|
|
|
|
test -z "${HASHER}" && HASHER=$(/usr/bin/which sha224sum 2> /dev/null)
|
2015-09-29 18:24:56 -04:00
|
|
|
#It gets insecure below here, but better than nothing?
|
2015-09-29 20:13:16 -04:00
|
|
|
test -z "${HASHER}" && HASHER=$(/usr/bin/which sha1sum 2> /dev/null)
|
|
|
|
test -z "${HASHER}" && HASHER=$(/usr/bin/which md5sum 2> /dev/null)
|
2015-09-29 18:24:56 -04:00
|
|
|
#If we found no hasher: exit
|
2015-09-29 20:13:16 -04:00
|
|
|
[ -z "${HASHER}" ] && die 5 "No hash calculator found"
|
2015-09-29 18:24:56 -04:00
|
|
|
|
2015-09-29 20:16:59 -04:00
|
|
|
read_config
|
2015-09-29 14:17:52 -04:00
|
|
|
|
2015-09-29 09:55:28 -04:00
|
|
|
#Write header
|
|
|
|
echo "#hashboot ${VERSION} - Algorithm: $(basename ${HASHER})" > ${DIGEST_FILE}
|
2015-09-29 13:31:20 -04:00
|
|
|
#Write MBR of MBR_DEVICE to ${DIGEST_FILE}
|
2015-09-29 20:13:16 -04:00
|
|
|
dd if=${MBR_DEVICE} of=${MBR_TMP} bs=1M count=1 status=noxfer || die 8
|
2015-09-29 09:55:28 -04:00
|
|
|
#Write hashes of all regular files to ${DIGEST_FILE}
|
2015-09-29 18:24:56 -04:00
|
|
|
${HASHER} ${MBR_TMP} >> ${DIGEST_FILE}
|
2015-09-29 09:55:28 -04:00
|
|
|
find /boot -type f -exec ${HASHER} --binary {} >> ${DIGEST_FILE} +
|
|
|
|
if [ $? == 0 ]
|
|
|
|
then
|
|
|
|
echo "List of hashes written to ${DIGEST_FILE}"
|
|
|
|
else
|
2015-09-29 15:05:29 -04:00
|
|
|
die 7 "Error writing ${DIGEST_FILE}"
|
2015-09-29 09:55:28 -04:00
|
|
|
fi
|
2015-09-29 18:12:40 -04:00
|
|
|
|
2015-09-29 09:55:28 -04:00
|
|
|
#Backup of good files
|
2015-09-29 13:31:20 -04:00
|
|
|
tar -czpPf ${BACKUP_FILE} ${MBR_TMP} /boot ${DIGEST_FILE}
|
2015-09-29 09:55:28 -04:00
|
|
|
if [ $? == 0 ]
|
|
|
|
then
|
|
|
|
echo "Backup written to ${BACKUP_FILE}"
|
|
|
|
else
|
2015-09-29 15:05:29 -04:00
|
|
|
die 7 "Error writing ${BACKUP_FILE}"
|
2015-09-29 09:55:28 -04:00
|
|
|
fi
|
2015-06-05 08:04:44 -04:00
|
|
|
elif [ "${1}" == "check" ]
|
|
|
|
then
|
2015-09-29 10:03:52 -04:00
|
|
|
COUNTER=0
|
2015-09-29 18:24:56 -04:00
|
|
|
HASHER=$(head -n1 ${DIGEST_FILE} | awk '{print $5}')
|
2015-09-29 20:16:59 -04:00
|
|
|
read_config
|
2015-09-29 18:24:56 -04:00
|
|
|
|
2015-09-29 20:43:34 -04:00
|
|
|
dd if=${MBR_DEVICE} of=${MBR_TMP} bs=1M count=1 status=noxfer || die 8
|
2015-09-29 18:33:23 -04:00
|
|
|
if ! $(grep ${MBR_TMP} ${DIGEST_FILE} | ${HASHER} --check --warn --quiet --strict > ${LOG_FILE})
|
2015-09-29 09:55:28 -04:00
|
|
|
then
|
|
|
|
echo " !! TIME TO PANIK: MBR WAS MODIFIED !!"
|
2015-09-29 10:03:52 -04:00
|
|
|
COUNTER=$((COUNTER + 1))
|
2015-09-29 09:55:28 -04:00
|
|
|
fi
|
2015-09-29 18:33:23 -04:00
|
|
|
if ! $(grep -v ${MBR_TMP} ${DIGEST_FILE} | ${HASHER} --check --warn --quiet --strict >> ${LOG_FILE})
|
2015-09-29 09:55:28 -04:00
|
|
|
then
|
|
|
|
echo " !! TIME TO PANIK: AT LEAST 1 FILE WAS MODIFIED !!"
|
2015-09-29 10:03:52 -04:00
|
|
|
COUNTER=$((COUNTER + 2))
|
|
|
|
die $COUNTER
|
2015-09-29 09:55:28 -04:00
|
|
|
fi
|
2015-06-05 19:58:48 -04:00
|
|
|
elif [ "${1}" == "recover" ]
|
|
|
|
then
|
2015-09-29 10:14:21 -04:00
|
|
|
echo "Restoring files from backup... (type yes or no for each file)"
|
|
|
|
|
|
|
|
#For each failed file: ask if it should be recovered from backup
|
|
|
|
for file in $(cut -d: -f1 ${LOG_FILE})
|
|
|
|
do
|
|
|
|
tar -xzpPvwf ${BACKUP_FILE} ${file}
|
2015-09-29 13:44:01 -04:00
|
|
|
[ $? != 0 ] && echo "Error restoring ${file} from backup, continuing" >&2
|
|
|
|
#If the MBR is to be recovered, copy to ${MBR_DEVICE}
|
|
|
|
if [ "${file}" == ${MBR_TMP} ]
|
2015-09-29 11:00:37 -04:00
|
|
|
then
|
2015-09-29 13:44:01 -04:00
|
|
|
cp ${MBR_TMP} ${MBR_DEVICE}
|
|
|
|
[ $? != 0 ] && echo "Error restoring MBR from backup, continuing" >&2
|
2015-09-29 11:00:37 -04:00
|
|
|
fi
|
2015-09-29 10:14:21 -04:00
|
|
|
done
|
2015-06-05 08:04:44 -04:00
|
|
|
else
|
2015-09-29 15:05:29 -04:00
|
|
|
die 6 "Usage: ${0} index|check|recover"
|
2015-06-05 19:58:48 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
die 0
|