Fix media permissions: make the library readable by every service (#1042)

* 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>
This commit is contained in:
lrnselfreliance 2026-11-30 19:32:39 -07:00 • committed by Roland
parent daae0b1d67
commit f2e3eba226
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 177 additions and 13 deletions

View file

@ -9,6 +9,40 @@ 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
@ -44,27 +78,17 @@ 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
# Ensure directories have at least u+rwx so they can be listed, traversed, and written to.
count=$(find "${MEDIA_DIRECTORY}" -type d ! -perm -u+rwx -print 2>/dev/null | wc -l)
if [ "$count" -gt 0 ]; then
find "${MEDIA_DIRECTORY}" -type d ! -perm -u+rwx -exec chmod u+rwx {} + 2>/dev/null || errors=$((errors + 1))
dirs_fixed=$count
fi
# Ensure files have at least u+rw so they can be read and modified.
count=$(find "${MEDIA_DIRECTORY}" -type f ! -perm -u+rw -print 2>/dev/null | wc -l)
if [ "$count" -gt 0 ]; then
find "${MEDIA_DIRECTORY}" -type f ! -perm -u+rw -exec chmod u+rw {} + 2>/dev/null || errors=$((errors + 1))
files_fixed=$count
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}"