hashboot/hashboot

191 lines
6.8 KiB
Plaintext
Raw Normal View History

2015-09-30 02:13:16 +02:00
#!/bin/bash
2015-06-05 14:04:44 +02:00
#Hashes all files in /boot to check them during early boot
2015-09-26 11:57:14 +02: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,
#7 = write error, 8 = dd error, 9 = file not found
2015-06-05 14:04:44 +02:00
2015-10-03 19:54:53 +02:00
###################################################################################
# "THE HUG-WARE LICENSE" (Revision 1): #
# xo <xo@rotce.de> and tastytea <tastytea@tastytea.de> wrote these files. As long #
# as you retain this notice you can do whatever you want with this stuff. If we #
# meet some day, and you think this stuff is worth it, you can give us a hug. #
###################################################################################
2015-10-04 22:37:44 +02:00
VERSION="0.7.6"
2015-09-30 00:26:27 +02:00
PATH="/bin:/usr/bin:/sbin:/usr/sbin:${PATH}"
2015-06-05 14:04:44 +02:00
DIGEST_FILE="/var/lib/hashboot.digest"
DIGEST_FILE_TMP="/tmp/hashboot.digesttmp"
2015-06-05 14:04:44 +02:00
LOG_FILE="/tmp/hashboot.log"
2015-10-04 20:31:50 +02:00
MBR_DEVICE="/dev/sda"
MBR_SIZE=1024
2015-09-26 11:57:14 +02:00
MBR_TMP="/tmp/mbr"
BACKUP_FILE="/var/cache/boot-backup.tar"
2015-06-05 14:04:44 +02:00
HASHER=""
2015-09-29 16:14:21 +02:00
BOOT_MOUNTED=0
2015-09-29 20:17:52 +02:00
CONFIG_FILE="/etc/hashboot.cfg"
2015-10-04 19:22:12 +02:00
COUNTER=0
2015-06-05 14:04:44 +02:00
#Umount /boot if we mounted it, exit with given exit code
2015-09-30 02:13:16 +02:00
die ()
2015-06-05 14:04:44 +02:00
{
2015-09-29 16:35:59 +02:00
if [ ${BOOT_MOUNTED} -gt 0 ]
then
2015-09-29 16:14:21 +02:00
umount /boot
fi
2015-06-05 14:04:44 +02:00
2015-09-29 21:05:29 +02:00
[ -z "${2}" ] || echo "${2}" >&2
exit ${1}
2015-06-05 14:04:44 +02:00
}
write_hashes ()
{
2015-10-04 18:14:14 +02:00
#Write header to ${1}
echo "#hashboot ${VERSION} - Algorithm: $(basename ${HASHER})" > ${1}
2015-10-04 18:14:14 +02:00
#Write MBR of MBR_DEVICE to ${1}
2015-10-04 18:38:26 +02:00
dd if=${MBR_DEVICE} of=${MBR_TMP} bs=${MBR_SIZE}K count=1 status=noxfer || die 8
2015-10-04 18:14:14 +02:00
#Write hashes of all regular files to ${1}
${HASHER} ${MBR_TMP} >> ${1}
find /boot -type f -exec ${HASHER} --binary {} >> ${1} +
}
2015-06-05 14:04:44 +02:00
#If we're not root: exit
if [ ${UID} -ne 0 ]
then
2015-09-29 21:05:29 +02:00
die 4 "You have to be root"
2015-06-05 14:04:44 +02:00
fi
#If /boot is in fstab but not mounted: mount, mark as mounted
2015-10-04 20:35:47 +02:00
if grep -q '/boot' /etc/fstab && ! grep -q /boot /etc/mtab
2015-06-05 14:04:44 +02:00
then
mount /boot
BOOT_MOUNTED=1
2015-06-05 14:04:44 +02:00
fi
#Look for config file and set ${MBR_DEVICE}.
if [ -f ${CONFIG_FILE} ]
then
2015-10-04 20:07:12 +02:00
source ${CONFIG_FILE}
[ $? != 0 ] && die 9 "Error reading config file"
#If not found, create one and ask for ${MBR_DEVICE}
else
2015-10-04 20:27:14 +02:00
#Create ${CONFIG_FILE} with defaults if noninterctive
2015-10-04 20:44:47 +02:00
if [ -t "0" ]
2015-10-04 20:20:02 +02:00
then
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}
2015-10-04 19:09:10 +02:00
2015-10-04 20:20:02 +02:00
echo -n "Where should backupfile be stored?"
read -r BACKUP_FILE
[ -z "${BACKUP_FILE}" ] && BACKUP_FILE="/var/cache/boot-backup.tar.gz"
echo "#Where the Backup files are stored" >> ${CONFIG_FILE}
echo "BACKUP_FILE=${BACKUP_FILE}" >> ${CONFIG_FILE}
2015-10-04 20:47:15 +02:00
else
echo "#Device with the MBR on it" > ${CONFIG_FILE}
echo "MBR_DEVICE=${MBR_DEVICE}" >> ${CONFIG_FILE}
echo "#Where the Backup files are stored" >> ${CONFIG_FILE}
echo "BACKUP_FILE=${BACKUP_FILE}" >> ${CONFIG_FILE}
2015-10-04 20:20:02 +02:00
fi
fi
# Find out where the first partition starts and set ${MBR_SIZE} in KiB
2015-10-04 20:24:20 +02:00
sectorsize=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep '^Units' | awk '{print $8}' )
if [ "${sectorsize}" == "=" ] # Older versions of util-linux
then
2015-10-04 20:24:20 +02:00
sectorsize=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep '^Units' | awk '{print $9}' )
fi
2015-10-04 20:24:20 +02:00
startsector=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep -A1 'Device' | tail -n1 | awk '{print $3}' )
MBR_SIZE=$(expr ${sectorsize} \* ${startsector} / 1024)
2015-10-04 18:56:53 +02:00
if [ ${?} != 0 ]
then
2015-10-04 18:56:53 +02:00
echo "Something went wrong. Most likely your partition table is corrupt." >&2
die 1 "You have to recover the MBR manually by copying the mbr from ${BACKUP_FILE}"
fi
2015-06-05 14:04:44 +02:00
if [ "${1}" == "index" ]
then
[ -f ${DIGEST_FILE} ] || die 10 "No digestfile"
[ -f ${CONFIG_FILE} ] || die 9 "No configfile"
#Try different hashers, use the most secure
2015-09-30 02:13:16 +02: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)
#It gets insecure below here, but better than nothing?
2015-09-30 02:13:16 +02:00
test -z "${HASHER}" && HASHER=$(/usr/bin/which sha1sum 2> /dev/null)
test -z "${HASHER}" && HASHER=$(/usr/bin/which md5sum 2> /dev/null)
#If we found no hasher: exit
2015-09-30 02:13:16 +02:00
[ -z "${HASHER}" ] && die 5 "No hash calculator found"
2015-10-04 18:14:14 +02:00
2015-10-04 22:37:44 +02:00
#Exists ${DIGEST_FILE}, if true run do magic, else write ${DIGEST_FILE}
2015-10-04 17:56:36 +02:00
if [ -f ${DIGEST_FILE} ]
2015-10-04 18:00:48 +02:00
then
2015-10-04 18:53:51 +02:00
#Collect mbr and /boot hashes and write to $DIGEST_FILE_TMP
2015-10-04 17:56:36 +02:00
write_hashes $DIGEST_FILE_TMP
2015-10-04 18:53:51 +02:00
#Compare $DIGEST_FILE_TMP against ${DIGEST_FILE} and exit, when fine. Otherwise do magic.
2015-10-04 17:56:36 +02:00
if diff -q --ignore-matching-lines='^#' ${DIGEST_FILE} ${DIGEST_FILE_TMP} ;
then
2015-10-04 17:56:36 +02:00
die 0
else
2015-10-04 23:11:17 +02:00
for file in $(diff ${DIGEST_FILE} ${DIGEST_FILE_TMP} | grep -v '^#' | grep '<' | cut -d'*' -f2 | sed 's/\ /\\ /g' );
2015-10-04 22:37:44 +02:00
do
#lösche_aus_tar
2015-10-04 23:11:17 +02:00
tar --delete -v -f $BACKUP_FILE $file
2015-10-04 22:37:44 +02:00
done
2015-10-04 23:11:17 +02:00
for file in $(diff ${DIGEST_FILE} ${DIGEST_FILE_TMP} | grep -v '^#' | grep '>' | cut -d'*' -f2 | sed 's/\ /\\ /g' );
2015-10-04 22:43:47 +02:00
do
2015-10-04 23:11:17 +02:00
tar -r -v -f $BACKUP_FILE $file
2015-10-04 22:37:44 +02:00
done
fi
2015-10-04 17:56:36 +02:00
else
write_hashes $DIGEST_FILE
2015-10-04 23:11:17 +02:00
tar -cpPf ${BACKUP_FILE} ${MBR_TMP} /boot ${DIGEST_FILE} || die 7 "Error writing ${BACKUP_FILE}"
echo "Backup written to ${BACKUP_FILE}"
fi
2015-10-04 23:11:17 +02:00
2015-06-05 14:04:44 +02:00
elif [ "${1}" == "check" ]
then
[ -f ${DIGEST_FILE} ] || die 9 "No digestfile"
[ -f ${CONFIG_FILE} ] || die 9 "No configfile"
HASHER=$(head -n1 ${DIGEST_FILE} | awk '{print $5}')
dd if=${MBR_DEVICE} of=${MBR_TMP} bs=${MBR_SIZE}K count=1 status=noxfer || die 8
if ! $(grep ${MBR_TMP} ${DIGEST_FILE} | ${HASHER} --check --warn --quiet --strict > ${LOG_FILE})
then
echo " !! TIME TO PANIK: MBR WAS MODIFIED !!"
COUNTER=$((COUNTER + 1))
fi
if ! $(grep -v ${MBR_TMP} ${DIGEST_FILE} | ${HASHER} --check --warn --quiet --strict >> ${LOG_FILE})
then
echo " !! TIME TO PANIK: AT LEAST 1 FILE WAS MODIFIED !!"
COUNTER=$((COUNTER + 2))
die $COUNTER
fi
elif [ "${1}" == "recover" ]
then
2015-09-29 16:14:21 +02: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 -xpPvwf ${BACKUP_FILE} ${file}
2015-09-29 19:44:01 +02: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 17:00:37 +02:00
then
2015-09-29 19:44:01 +02:00
cp ${MBR_TMP} ${MBR_DEVICE}
[ $? != 0 ] && echo "Error restoring MBR from backup, continuing" >&2
2015-09-29 17:00:37 +02:00
fi
2015-09-29 16:14:21 +02:00
done
2015-06-05 14:04:44 +02:00
else
2015-09-29 21:05:29 +02:00
die 6 "Usage: ${0} index|check|recover"
fi
die 0