#!/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= ${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."