mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-01 04:16:24 -05:00
* Fix media permissions: make the library readable by every service The script only made sure WROLPi (the owner) could read and write, so a 700 directory passed its check while Caddy, which serves media as its own user, answered 403 for every file in it. Library directories now get at least 755 and files 644, only ever adding bits. config/ keeps only owner fixes: it holds the VPN's key and login. The summary counts ownership fixes too, instead of reporting 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Fix permissions: skip config/ even when the media path ends in a slash find prints paths as given, so a MEDIA_DIRECTORY with a trailing slash (from .env) never matched the config/ prune, and the library pass opened the VPN key to other users. The tests also no longer depend on the umask. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
3.8 KiB
Bash
Executable file
102 lines
3.8 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Fix ownership and permissions on the WROLPi media directory so WROLPi can read and write all files.
|
|
|
|
# Re-execute this script if it wasn't called with sudo.
|
|
if [ $EUID != 0 ]; then
|
|
sudo "$0" "$@"
|
|
exit $?
|
|
fi
|
|
|
|
MEDIA_DIRECTORY="/media/wrolpi"
|
|
|
|
# Directories: WROLPi can list, enter, and write them; every service can list and enter them. Files: WROLPi can read and
|
|
# write them; every service can read them (Caddy serves media as its own user, so a 700 directory is a 403). Bits are
|
|
# only ever added. config/ holds private files (the VPN's key and login, and more): there, only WROLPi's own bits are
|
|
# fixed.
|
|
fix_modes() {
|
|
local media=$1
|
|
# find prints paths as given, so "/media/wrolpi/" would never match the "/media/wrolpi//config" prune.
|
|
while [ "${media}" != "/" ] && [ "${media%/}" != "${media}" ]; do
|
|
media=${media%/}
|
|
done
|
|
local skip=( \( -path "${media}/config" -o -path "${media}/lost+found" \) -prune -o )
|
|
dirs_fixed=$(find "${media}" "${skip[@]}" -type d ! -perm -755 -print 2>/dev/null | wc -l | tr -d ' ')
|
|
if [ "${dirs_fixed}" -gt 0 ]; then
|
|
find "${media}" "${skip[@]}" -type d ! -perm -755 -exec chmod u+rwx,go+rx {} + 2>/dev/null || errors=$((errors + 1))
|
|
fi
|
|
files_fixed=$(find "${media}" "${skip[@]}" -type f ! -perm -644 -print 2>/dev/null | wc -l | tr -d ' ')
|
|
if [ "${files_fixed}" -gt 0 ]; then
|
|
find "${media}" "${skip[@]}" -type f ! -perm -644 -exec chmod u+rw,go+r {} + 2>/dev/null || errors=$((errors + 1))
|
|
fi
|
|
local config="${media}/config" count
|
|
if [ -d "${config}" ]; then
|
|
count=$(find "${config}" -type d ! -perm -700 -print 2>/dev/null | wc -l | tr -d ' ')
|
|
if [ "${count}" -gt 0 ]; then
|
|
find "${config}" -type d ! -perm -700 -exec chmod u+rwx {} + 2>/dev/null || errors=$((errors + 1))
|
|
dirs_fixed=$((dirs_fixed + count))
|
|
fi
|
|
count=$(find "${config}" -type f ! -perm -600 -print 2>/dev/null | wc -l | tr -d ' ')
|
|
if [ "${count}" -gt 0 ]; then
|
|
find "${config}" -type f ! -perm -600 -exec chmod u+rw {} + 2>/dev/null || errors=$((errors + 1))
|
|
files_fixed=$((files_fixed + count))
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Source .env override if it exists.
|
|
if [ -f /opt/wrolpi/.env ]; then
|
|
# shellcheck disable=SC1091
|
|
source /opt/wrolpi/.env
|
|
fi
|
|
|
|
if [ ! -d "${MEDIA_DIRECTORY}" ]; then
|
|
echo "ERROR: Media directory does not exist: ${MEDIA_DIRECTORY}"
|
|
exit 1
|
|
fi
|
|
|
|
# Detect the filesystem type of the media directory.
|
|
fstype=$(findmnt -n -o FSTYPE --target "${MEDIA_DIRECTORY}" 2>/dev/null)
|
|
|
|
if [[ "${fstype}" == "vfat" || "${fstype}" == "exfat" || "${fstype}" == "ntfs" || "${fstype}" == "fuseblk" ]]; then
|
|
echo "ERROR: ${MEDIA_DIRECTORY} is on a ${fstype} filesystem."
|
|
echo ""
|
|
echo "FAT, exFAT, and NTFS filesystems do not support Unix file permissions."
|
|
echo "Ownership and permissions are controlled by mount options instead."
|
|
echo ""
|
|
echo "To fix permissions, remount the drive with the correct options:"
|
|
echo " sudo mount -o remount,uid=wrolpi,gid=wrolpi ${MEDIA_DIRECTORY}"
|
|
echo ""
|
|
echo "Or add to /etc/fstab for a permanent fix:"
|
|
echo " UUID=<your-drive-uuid> ${MEDIA_DIRECTORY} ${fstype} uid=wrolpi,gid=wrolpi,nofail 0 0"
|
|
exit 1
|
|
fi
|
|
|
|
echo "Fixing permissions on ${MEDIA_DIRECTORY}..."
|
|
|
|
errors=0
|
|
dirs_fixed=0
|
|
files_fixed=0
|
|
|
|
# Fix ownership.
|
|
ownership_fixed=$(find "${MEDIA_DIRECTORY}" \( ! -user wrolpi -o ! -group wrolpi \) -print 2>/dev/null | wc -l | tr -d ' ')
|
|
if ! chown -R wrolpi:wrolpi "${MEDIA_DIRECTORY}" 2>/dev/null; then
|
|
echo "WARNING: Could not change ownership."
|
|
errors=$((errors + 1))
|
|
fi
|
|
|
|
fix_modes "${MEDIA_DIRECTORY}"
|
|
|
|
echo ""
|
|
echo "=== Summary ==="
|
|
echo "Ownership fixed: ${ownership_fixed}"
|
|
echo "Directories fixed: ${dirs_fixed}"
|
|
echo "Files fixed: ${files_fixed}"
|
|
echo "Errors: ${errors}"
|
|
|
|
if [ "$errors" -gt 0 ]; then
|
|
echo ""
|
|
echo "Some operations failed."
|
|
fi
|
|
|
|
echo ""
|
|
echo "Done."
|