aido-smb-connector/entrypoint.sh
2026-10-01 23:33:06 +02:00

105 lines
4 KiB
Bash

#!/bin/sh
# ─────────────────────────────────────────────────────────────────────────────
# AIDO SMB connector entrypoint.
#
# Mounts each configured SMB/CIFS share *inside the container* (no host mount
# needed), then drops privileges and runs the sync app. Mounting is opt-in per
# source: a source is CIFS-mounted only if it provides SMB connection details
# (SMB_SOURCE_<N>_UNC or _SERVER + _SHARE). Sources without them are assumed to
# be bind-mounted from the host at their _MOUNT path (the original Phase-1 mode),
# so both styles work side by side.
#
# Per-source env (indexed style):
# SMB_SOURCE_<N>_NAME label (also used by the app)
# SMB_SOURCE_<N>_MOUNT where to mount inside the container (e.g. /mnt/smb/finance)
# SMB_SOURCE_<N>_UNC //server/share (or use _SERVER + _SHARE)
# SMB_SOURCE_<N>_SERVER server host/ip \_ combined into //server/share
# SMB_SOURCE_<N>_SHARE share name /
# SMB_SOURCE_<N>_USERNAME CIFS username (omit for guest)
# SMB_SOURCE_<N>_PASSWORD CIFS password
# SMB_SOURCE_<N>_DOMAIN AD domain/workgroup (optional)
# SMB_SOURCE_<N>_VERS SMB dialect, default 3.0
# SMB_SOURCE_<N>_MOUNT_OPTS extra raw mount -o options (optional)
#
# Requires the container to have CAP_SYS_ADMIN + DAC_READ_SEARCH and an
# unconfined apparmor profile (see docker-compose.smb.yml).
# ─────────────────────────────────────────────────────────────────────────────
set -eu
APP_UID="${APP_UID:-10001}"
APP_GID="${APP_GID:-10001}"
MOUNTED=""
log() { echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) [entrypoint] $*"; }
cleanup() {
# Unmount in reverse so the app's file handles are released cleanly on stop.
for m in $MOUNTED; do
umount "$m" 2>/dev/null && log "unmounted $m" || log "warn: could not unmount $m"
done
}
trap cleanup EXIT INT TERM
mount_one() {
n="$1"
eval unc="\${SMB_SOURCE_${n}_UNC:-}"
eval server="\${SMB_SOURCE_${n}_SERVER:-}"
eval share="\${SMB_SOURCE_${n}_SHARE:-}"
eval mnt="\${SMB_SOURCE_${n}_MOUNT:-}"
eval user="\${SMB_SOURCE_${n}_USERNAME:-}"
eval pass="\${SMB_SOURCE_${n}_PASSWORD:-}"
eval domain="\${SMB_SOURCE_${n}_DOMAIN:-}"
eval vers="\${SMB_SOURCE_${n}_VERS:-3.0}"
eval extra="\${SMB_SOURCE_${n}_MOUNT_OPTS:-}"
# No SMB connection details → host-bind-mount mode; nothing to do here.
if [ -z "$unc" ] && [ -z "$server" ]; then
return 0
fi
[ -n "$unc" ] || unc="//${server}/${share}"
if [ -z "$mnt" ]; then
log "ERROR: SMB_SOURCE_${n}_MOUNT is required when mounting $unc inside the container"
exit 1
fi
mkdir -p "$mnt"
# Credentials via a 0600 file so the password never appears in the process list.
cred="/tmp/.smbcred_${n}"
{
[ -n "$user" ] && echo "username=$user"
[ -n "$pass" ] && echo "password=$pass"
[ -n "$domain" ] && echo "domain=$domain"
} > "$cred"
chmod 600 "$cred"
opts="ro,vers=${vers},uid=${APP_UID},gid=${APP_GID},file_mode=0440,dir_mode=0550,nounix,serverino"
if [ -n "$user" ]; then
opts="${opts},credentials=${cred}"
else
opts="${opts},guest"
fi
[ -n "$extra" ] && opts="${opts},${extra}"
log "mounting $unc -> $mnt (vers=${vers}${user:+, user=$user}${domain:+, domain=$domain})"
if mount -t cifs "$unc" "$mnt" -o "$opts"; then
MOUNTED="$mnt $MOUNTED"
rm -f "$cred"
else
rm -f "$cred"
log "ERROR: failed to mount $unc at $mnt — check credentials, host reachability, and that the container has CAP_SYS_ADMIN"
exit 1
fi
}
# Walk SMB_SOURCE_1.., mounting any that carry connection details.
i=1
while :; do
eval name="\${SMB_SOURCE_${i}_NAME:-}"
[ -n "$name" ] || break
mount_one "$i"
i=$((i + 1))
done
# Hand off to the sync app as the unprivileged runner.
log "starting sync app as uid=${APP_UID}"
exec gosu "${APP_UID}:${APP_GID}" python -m app.main