#!/bin/sh
# opkg_md5sums.sh
# 2021-03-12
# by Gernot WALZL
# Check files of opkg packages using md5sums.
OPKG_INFO_DIR=${OPKG_INFO_DIR:-"/usr/lib/opkg/info"}
OPKG_MD5SUMS_DIR=${OPKG_MD5SUMS_DIR:-"/usr/lib/opkg/md5sums"}
print_usage () {
echo "Usage: $0 {generate|check} [PACKAGE]"
}
generate_md5sums () {
local PKG="$1"
local FILELIST="${OPKG_INFO_DIR}/${PKG}.list"
local MD5SUMS="${OPKG_MD5SUMS_DIR}/${PKG}.md5sums"
if [ ! -r "$FILELIST" ]; then
echo "ERROR: $FILELIST missing"
return 1
fi
echo "Generating ${PKG}.md5sums"
mkdir -p "$OPKG_MD5SUMS_DIR"
rm -f "$MD5SUMS"
cat "$FILELIST" \
| while read FILEPATH; do
if [ "$(dirname $FILEPATH)" = "/etc/config" \
-o "$FILEPATH" = "/etc/dropbear/dropbear_rsa_host_key" \
-o "$FILEPATH" = "/etc/dropbear/dropbear_ed25519_host_key" \
-o "$FILEPATH" = "/etc/passwd" \
-o "$FILEPATH" = "/etc/shadow" \
-o "$FILEPATH" = "/etc/group" \
-o "$FILEPATH" = "/etc/firewall.user" \
-o "$FILEPATH" = "/etc/mtab" ]; then
continue
fi
md5sum "$FILEPATH" >> "$MD5SUMS"
done
}
check_md5sums () {
local PKG="$1"
local MD5SUMS="${OPKG_MD5SUMS_DIR}/${PKG}.md5sums"
if [ ! -r "$MD5SUMS" ]; then
echo "WARNING: $MD5SUMS missing"
return 1
fi
if md5sum -cs "$MD5SUMS" ; then
echo "${PKG}: OK"
return 0
else
echo "${PKG}: FAILED"
return 1
fi
}
list_installed () {
find "$OPKG_INFO_DIR" -name '*.list' -exec basename {} .list \;
}
generate_all () {
list_installed \
| while read PKG; do
generate_md5sums "$PKG"
done
}
check_all () {
list_installed \
| while read PKG; do
check_md5sums "$PKG"
done
}
case "$1" in
'generate')
if [ -z "$2" ]; then
generate_all
else
generate_md5sums "$2"
fi
;;
'check')
if [ -z "$2" ]; then
check_all
else
check_md5sums "$2"
fi
;;
*)
print_usage
esac