diff --git a/main.inc.php b/main.inc.php index a6d7950..ba4c30e 100644 --- a/main.inc.php +++ b/main.inc.php @@ -1,7 +1,7 @@ }" >&2; exit 1; } @@ -22,6 +23,38 @@ exec 9>"$LOCK_FILE" flock -n 9 || exit 0 PREVIEW_CACHE="$PIWIGO_ROOT/_data/bratonien-tools/nc-webdav-preview/connection-$CONNECTION_ID" +SOURCE_CACHE="$PIWIGO_ROOT/_data/bratonien-tools/nc-webdav-gallery/connection-$CONNECTION_ID" +DERIVATIVE_CACHE="$PIWIGO_ROOT/_data/i/_data/bratonien-tools/nc-webdav-gallery/connection-$CONNECTION_ID" +PIWIGO_DATA="$PIWIGO_ROOT/_data" + +normalize_connector_cache_permissions() { + [[ -d "$PIWIGO_DATA" ]] || return 0 + + local data_uid data_gid current_uid path + data_uid="$(stat -c '%u' "$PIWIGO_DATA")" + data_gid="$(stat -c '%g' "$PIWIGO_DATA")" + current_uid="$(id -u)" + + for path in "$SOURCE_CACHE" "$PREVIEW_CACHE" "$DERIVATIVE_CACHE"; do + [[ -e "$path" ]] || continue + + if [[ "$current_uid" -eq 0 ]]; then + chown -R "$data_uid:$data_gid" -- "$path" + fi + + if ! find "$path" -type d -exec chmod 2775 {} + 2>/dev/null; then + echo "Hinweis: Verzeichnisrechte konnten ohne Root-Rechte nicht vollständig repariert werden: $path" >&2 + fi + if ! find "$path" -type f -exec chmod 0664 {} + 2>/dev/null; then + echo "Hinweis: Dateirechte konnten ohne Root-Rechte nicht vollständig repariert werden: $path" >&2 + fi + done +} + +# Altbestände aus früheren root/systemd-Läufen werden repariert, sobald dieser +# Lauf mit ausreichenden Rechten ausgeführt wird. Bei normalen Webserver-Läufen +# werden zumindest alle eigenen Dateien gruppenschreibbar gehalten. +normalize_connector_cache_permissions php "$SCRIPT_DIR/lib/precache-webdav-previews.php" \ --mapping="$WEBDAV_MAPPING_FILE" \ @@ -33,3 +66,5 @@ php "$SCRIPT_DIR/lib/precache-webdav-previews.php" \ php "$SCRIPT_DIR/lib/build-webdav-derivatives.php" \ --piwigo-root="$PIWIGO_ROOT" \ --connection-id="$CONNECTION_ID" + +normalize_connector_cache_permissions diff --git a/runtime/repair-webdav-cache-permissions.sh b/runtime/repair-webdav-cache-permissions.sh new file mode 100644 index 0000000..92bf21f --- /dev/null +++ b/runtime/repair-webdav-cache-permissions.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +PIWIGO_ROOT="${1:-/var/www/piwigo}" +PIWIGO_ROOT="${PIWIGO_ROOT%/}" +DATA_DIR="$PIWIGO_ROOT/_data" + +[[ -d "$DATA_DIR" ]] || { echo "Piwigo-_data wurde nicht gefunden: $DATA_DIR" >&2; exit 1; } +[[ "$(id -u)" -eq 0 ]] || { echo "Die Reparatur muss als root ausgeführt werden." >&2; exit 1; } + +DATA_UID="$(stat -c '%u' "$DATA_DIR")" +DATA_GID="$(stat -c '%g' "$DATA_DIR")" + +PATHS=( + "$DATA_DIR/bratonien-tools/nc-webdav-gallery" + "$DATA_DIR/bratonien-tools/nc-webdav-preview" + "$DATA_DIR/i/_data/bratonien-tools/nc-webdav-gallery" + "$DATA_DIR/i/bratonien-watermark" +) + +found=0 +for path in "${PATHS[@]}"; do + [[ -e "$path" ]] || continue + found=1 + echo "Repariere: $path" + chown -R "$DATA_UID:$DATA_GID" -- "$path" + find "$path" -type d -exec chmod 2775 {} + + find "$path" -type f -exec chmod 0664 {} + +done + +if [[ "$found" -eq 0 ]]; then + echo "Keine Bratonien-Cache-Verzeichnisse gefunden." + exit 0 +fi + +echo "Bratonien-Cache-Rechte wurden an $DATA_DIR angeglichen (UID $DATA_UID, GID $DATA_GID)."