Initial modular

pull/2/head
teldra 2016-10-04 20:49:35 +02:00
parent 37a4399293
commit 391983a659
1 changed files with 73 additions and 49 deletions

122
hashboot
View File

@ -29,6 +29,9 @@ CONFIG_FILE="/etc/hashboot.cfg"
COUNTER=0 COUNTER=0
DD_STATUS="none" DD_STATUS="none"
PROGRAMMER="no" #standard change enables bios mode PROGRAMMER="no" #standard change enables bios mode
#1=mbr 2=files 3=bios 4=mbr/files 5=mbr/bios 6=files/bios 7=all
test="5"
#Umount /boot if we mounted it, exit with given exit code #Umount /boot if we mounted it, exit with given exit code
die () die ()
@ -46,17 +49,26 @@ write_hashes ()
{ {
#Write header to ${1} #Write header to ${1}
echo "#hashboot ${VERSION} - Algorithm: $(basename ${HASHER})" > ${1} echo "#hashboot ${VERSION} - Algorithm: $(basename ${HASHER})" > ${1}
#Write MBR of MBR_DEVICE to ${1}
dd if=${MBR_DEVICE} of=${MBR_TMP} bs=${MBR_SIZE}K count=1 status=${DD_STATUS} || die 8 case $test in
#Write bios to ${1} 1|4|5|7)
#if we set an programmer chip in config, read bios and write it to digestfile. #copy mbr to file
if [ ! ${PROGRAMMER} == "no" ]; then dd if=${MBR_DEVICE} of=${MBR_TMP} bs=${MBR_SIZE}K count=1 status=${DD_STATUS} || die 8
flashrom --programmer ${PROGRAMMER} -r ${BIOS_TMP} > /dev/null 2>&1 #Write hash of MBR to ${1}
${HASHER} ${BIOS_TMP} >> ${1} ${HASHER} ${MBR_TMP} >> ${1} ;; esac
fi case $test in
#Write hashes of all regular files to ${1} 3|5|6|7)
${HASHER} ${MBR_TMP} >> ${1} #if we set an programmer chip in config
find /boot -type f -exec ${HASHER} --binary {} >> ${1} + if [ ! ${PROGRAMMER} == "no" ]; then
#read bios to file
flashrom --programmer ${PROGRAMMER} -r ${BIOS_TMP} > /dev/null 2>&1
#and write hashes of bios files to ${1}
${HASHER} ${BIOS_TMP} >> ${1}
fi ;; esac
case $test in
2|4|6|7)
#Write hashes of all regular files to ${1}
find /boot -type f -exec ${HASHER} --binary {} >> ${1} + ;; esac
} }
#If we're not root: exit #If we're not root: exit
@ -118,29 +130,33 @@ else
echo "MBR_DEVICE=${MBR_DEVICE}" >> ${CONFIG_FILE} echo "MBR_DEVICE=${MBR_DEVICE}" >> ${CONFIG_FILE}
echo "#Where the Backup files are stored" >> ${CONFIG_FILE} echo "#Where the Backup files are stored" >> ${CONFIG_FILE}
echo "BACKUP_FILE=${BACKUP_FILE}" >> ${CONFIG_FILE} echo "BACKUP_FILE=${BACKUP_FILE}" >> ${CONFIG_FILE}
echo "test=5" >> ${CONFIG_FILE}
echo "PROGRAMMER=${PROGRAMMER}" >> ${CONFIG_FILE} echo "PROGRAMMER=${PROGRAMMER}" >> ${CONFIG_FILE}
fi fi
fi fi
# Find out where the first partition starts and set ${MBR_SIZE} in KiB case $test in
sectorsize=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep '^Units' | awk '{print $8}' ) 1|4|5|7)
if [ "${sectorsize}" == "=" ] # Older versions of util-linux # Find out where the first partition starts and set ${MBR_SIZE} in KiB
then sectorsize=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep '^Units' | awk '{print $8}' )
sectorsize=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep '^Units' | awk '{print $9}' ) if [ "${sectorsize}" == "=" ] # Older versions of util-linux
fi then
startsector=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep -A1 'Device' | tail -n1 | awk '{print $2}' ) sectorsize=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep '^Units' | awk '{print $9}' )
if [ "${startsector}" == "*" ] # If partition is marked as boot, read next field fi
then startsector=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep -A1 'Device' | tail -n1 | awk '{print $2}' )
startsector=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep -A1 'Device' | tail -n1 | awk '{print $3}' ) if [ "${startsector}" == "*" ] # If partition is marked as boot, read next field
fi then
startsector=$(LC_ALL=C fdisk -l ${MBR_DEVICE} | grep -A1 'Device' | tail -n1 | awk '{print $3}' )
fi
MBR_SIZE=$(expr ${sectorsize} \* ${startsector} / 1024) MBR_SIZE=$(expr ${sectorsize} \* ${startsector} / 1024)
if [ ${?} != 0 ] if [ ${?} != 0 ]
then then
echo "Something went wrong. Most likely your partition table is corrupt." >&2 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}" die 1 "You have to recover the MBR manually by copying the mbr from ${BACKUP_FILE}"
fi fi
;; esac
if [ "${1}" == "index" ] if [ "${1}" == "index" ]
then then
@ -189,28 +205,36 @@ elif [ "${1}" == "check" ]
then then
[ -f ${DIGEST_FILE} ] || die 9 "No digestfile" [ -f ${DIGEST_FILE} ] || die 9 "No digestfile"
HASHER=$(head -n1 ${DIGEST_FILE} | awk '{print $5}') HASHER=$(head -n1 ${DIGEST_FILE} | awk '{print $5}')
dd if=${MBR_DEVICE} of=${MBR_TMP} bs=${MBR_SIZE}K count=1 status=${DD_STATUS} || die 8 case $test in
flashrom --programmer ${PROGRAMMER} -r ${BIOS_TMP} > /dev/null 2>&1 1|4|5|7)
if ! grep ${MBR_TMP} ${DIGEST_FILE} | ${HASHER} --check --warn --quiet --strict | tee ${LOG_FILE} dd if=${MBR_DEVICE} of=${MBR_TMP} bs=${MBR_SIZE}K count=1 status=${DD_STATUS} || die 8
then if ! grep ${MBR_TMP} ${DIGEST_FILE} | ${HASHER} --check --warn --quiet --strict | tee ${LOG_FILE}
echo " !! TIME TO PANIK: MBR WAS MODIFIED !!" then
COUNTER=$((COUNTER + 1)) echo " !! TIME TO PANIK: MBR WAS MODIFIED !!"
fi COUNTER=$((COUNTER + 1))
if ! grep -v ${MBR_TMP} ${DIGEST_FILE} | grep -v ${BIOS_TMP} | ${HASHER} --check --warn --quiet --strict | tee -a ${LOG_FILE} fi;; esac
then
echo " !! TIME TO PANIK: AT LEAST 1 FILE WAS MODIFIED !!" case $test in
COUNTER=$((COUNTER + 2)) 3|5|6|7)
die $COUNTER flashrom --programmer ${PROGRAMMER} -r ${BIOS_TMP} > /dev/null 2>&1
fi #if we set an programmer chip in config, find line with hash for bios and compare. if smthg wrong, panic
#if we set an programmer chip in config, find line with hash for bios and compare. if smthg wrong, panic if [ ! ${PROGRAMMER} == "no" ]; then
if [ ! ${PROGRAMMER} == "no" ]; then if grep ${BIOS_TMP} ${DIGEST_FILE} | ${HASHER} --check --warn --quiet --strict | tee -a ${LOG_FILE}
if grep ${BIOS_TMP} ${DIGEST_FILE} | ${HASHER} --check --warn --quiet --strict | tee -a ${LOG_FILE} then
then echo " !! TIME TO PANIK: BIOS WAS MODIFIED !!"
echo " !! TIME TO PANIK: BIOS WAS MODIFIED !!" COUNTER=$((COUNTER + 10))
COUNTER=$((COUNTER + 10)) die $COUNTER
die $COUNTER
fi fi
fi fi ;; esac
case $test in
2|4|6|7)
if ! grep -v ${MBR_TMP} ${DIGEST_FILE} | grep -v ${BIOS_TMP} | ${HASHER} --check --warn --quiet --strict | tee -a ${LOG_FILE}
then
echo " !! TIME TO PANIK: AT LEAST 1 FILE WAS MODIFIED !!"
COUNTER=$((COUNTER + 2))
die $COUNTER
fi ;; esac
elif [ "${1}" == "recover" ] elif [ "${1}" == "recover" ]
then then