92a5d50082
- load_config blockiert Shell-Operatoren (;|&`) in .env-Werten - validate_path prueft Sonderzeichen und Path-Traversal in Pfad-Variablen - validate_config prüft DARKTABLE_BIN-basename und ruft validate_path auf - Lockdir-Trap erst nach erfolgreicher Lock-Akquisition registriert (verhindert dass externer Lockdir bei gescheitertem Lock entfernt wird) - uninstall.sh nutzt rmdir statt rm -rf fuer Lockdir - security.bats mit 10 Tests fuer alle Sicherheitsanforderungen Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
106 lines
3.0 KiB
Bash
106 lines
3.0 KiB
Bash
#!/usr/bin/env bash
|
|
# Gemeinsame Hilfsfunktionen fuer darktable-sync Scripts.
|
|
# Dieses Script wird per `source` eingebunden, nicht direkt ausgefuehrt.
|
|
|
|
CONFIG_DIR="$HOME/.config/darktable-sync"
|
|
|
|
load_config() {
|
|
local env_file="$CONFIG_DIR/.env"
|
|
if [ ! -f "$env_file" ]; then
|
|
echo "Fehler: Konfiguration nicht gefunden: $env_file" >&2
|
|
echo "Vorlage kopieren mit: cp .env.example $env_file" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Berechtigungen pruefen: .env darf nicht world-readable sein
|
|
local perms
|
|
perms=$(stat -c '%a' "$env_file" 2>/dev/null || stat -f '%A' "$env_file" 2>/dev/null)
|
|
if [[ "${perms: -1}" != "0" ]]; then
|
|
echo "Warnung: $env_file ist world-readable. Empfehlung: chmod 600 $env_file" >&2
|
|
fi
|
|
|
|
# Zeilen mit Shell-Operatoren abweisen (Kommentare und Leerzeilen ignorieren)
|
|
if grep -vE '^\s*#|^\s*$' "$env_file" | grep -qE '[;|&`]'; then
|
|
echo "Fehler: $env_file enthaelt unerlaubte Zeichen (; | & \`). Bitte pruefen." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# shellcheck source=/dev/null
|
|
. "$env_file"
|
|
}
|
|
|
|
require_var() {
|
|
local var_name="$1"
|
|
if [ -z "${!var_name:-}" ]; then
|
|
echo "Fehler: Variable '$var_name' ist nicht gesetzt in $CONFIG_DIR/.env" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
validate_path() {
|
|
local var_name="$1" value="${!1:-}"
|
|
# Pfade duerfen keine Shell-Sonderzeichen oder Path-Traversal enthalten
|
|
if echo "$value" | grep -qE "['\";|&\`\$()\\\\]" || [[ "$value" == *".."* ]]; then
|
|
echo "Fehler: '$var_name' enthaelt unerlaubte Zeichen: $value" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
validate_config() {
|
|
require_var SERVER_IP
|
|
require_var SERVER_USER
|
|
require_var SERVER_SSH_PORT
|
|
require_var SERVER_DB_DIR
|
|
require_var SERVER_PHOTO_DIR
|
|
require_var LOCAL_DARKTABLE_DB_DIR
|
|
require_var LOCAL_PHOTO_DIR
|
|
require_var SYNC_BIN
|
|
require_var DARKTABLE_BIN
|
|
|
|
validate_path SERVER_DB_DIR
|
|
validate_path SERVER_PHOTO_DIR
|
|
|
|
# DARKTABLE_BIN: basename muss 'darktable' sein
|
|
if [[ "$(basename "$DARKTABLE_BIN")" != "darktable" ]]; then
|
|
echo "Fehler: DARKTABLE_BIN muss auf 'darktable' zeigen, nicht auf '$(basename "$DARKTABLE_BIN")'." >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
check_dependency() {
|
|
local cmd="$1" pkg="${2:-$1}"
|
|
if ! command -v "$cmd" &>/dev/null; then
|
|
echo "Fehler: '$cmd' ist nicht installiert." >&2
|
|
echo "Installieren mit: sudo apt install $pkg" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
log() {
|
|
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
|
|
}
|
|
|
|
ssh_server() {
|
|
ssh -o ConnectTimeout=5 -o BatchMode=yes \
|
|
-p "$SERVER_SSH_PORT" "$SERVER_USER@$SERVER_IP" "$@"
|
|
}
|
|
|
|
server_reachable() {
|
|
ssh_server true 2>/dev/null
|
|
}
|
|
|
|
ask_user() {
|
|
local title="$1" text="$2" ans
|
|
if command -v zenity &>/dev/null; then
|
|
zenity --question --title="$title" --text="$text" 2>/dev/null
|
|
return $?
|
|
elif command -v kdialog &>/dev/null; then
|
|
kdialog --title "$title" --yesno "$text" 2>/dev/null
|
|
return $?
|
|
else
|
|
read -r -p "$text [j/N] " ans || true
|
|
[[ "$ans" =~ ^[jJyY] ]]
|
|
return $?
|
|
fi
|
|
}
|