larpaso-website/claude-remote/sftp-upload.sh
2026-11-02 22:10:08 +00:00

65 lines
2.9 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Richtet einen reinen SFTP-Zugang zum gemeinsamen Ordner /shared der
# Claude-Container ein (für große Dateien: Videos, Audio, Hörproben).
# Keine Shell, kein Docker, nur Dateien hoch- und herunterladen.
#
# sudo bash sftp-upload.sh "ssh-ed25519 AAAA… lara" Nutzer lara-upload
# sudo bash sftp-upload.sh "ssh-ed25519 AAAA… …" anderer-name
#
# Nochmal ausführen ersetzt den Schlüssel. Entfernen siehe DEPLOYMENT_CLAUDE.md.
set -euo pipefail
SCHLUESSEL=${1:?Öffentlichen SSH-Schlüssel (Inhalt der .pub-Datei) angeben}
NUTZER=${2:-lara-upload}
VOLUME=/var/lib/docker/volumes/claude_shared/_data
CHROOT=/srv/sftp/$NUTZER
[[ $SCHLUESSEL == ssh-* || $SCHLUESSEL == ecdsa-* ]] || { echo "Das sieht nicht nach einem öffentlichen Schlüssel aus (ssh-ed25519 …)."; exit 1; }
[ -d "$VOLUME" ] || { echo "Volume $VOLUME fehlt – läuft der Claude-Stack?"; exit 1; }
# Gleiche Gruppe wie die Dateien im Volume (Container-Nutzer node, gid 1000),
# damit hochgeladene Dateien in den Containern les- und schreibbar sind.
GID=$(stat -c %g "$VOLUME")
getent group "$GID" >/dev/null || groupadd -g "$GID" claude-shared
id "$NUTZER" >/dev/null 2>&1 || useradd --system --no-create-home --home-dir / --shell /usr/sbin/nologin -g "$GID" "$NUTZER"
# Chroot muss root gehören; darin /shared als Bind-Mount auf das Volume
install -d -o root -g root -m 755 /srv/sftp "$CHROOT"
# (nicht anfassen, wenn schon eingehängt – sonst gehörte das Volume danach root)
mountpoint -q "$CHROOT/shared" || install -d -o root -g root -m 755 "$CHROOT/shared"
if ! grep -q " $CHROOT/shared " /etc/fstab; then
echo "$VOLUME $CHROOT/shared none bind 0 0" >>/etc/fstab
fi
mountpoint -q "$CHROOT/shared" || mount "$CHROOT/shared"
# Eingangsordner, in den die Container-Dateien schreibbar sind
install -d -o "$(stat -c %u "$VOLUME")" -g "$GID" -m 2775 "$VOLUME/youtube/upload"
# Schlüssel außerhalb des Chroot
install -d -o root -g root -m 755 /etc/ssh/authorized_keys
echo "$SCHLUESSEL" >"/etc/ssh/authorized_keys/$NUTZER"
chmod 644 "/etc/ssh/authorized_keys/$NUTZER"
cat >"/etc/ssh/sshd_config.d/sftp-$NUTZER.conf" <<EOF
# Reiner SFTP-Zugang zu /shared (eingerichtet mit claude-remote/sftp-upload.sh)
Match User $NUTZER
AuthorizedKeysFile /etc/ssh/authorized_keys/%u
AuthenticationMethods publickey
PasswordAuthentication no
ChrootDirectory $CHROOT
ForceCommand internal-sftp -u 0002 -d /shared/youtube/upload
AllowTcpForwarding no
AllowAgentForwarding no
X11Forwarding no
PermitTTY no
Match all
EOF
# Falls sshd nur bestimmte Nutzer zulässt, muss der neue Nutzer dazu
if sshd -T 2>/dev/null | grep -qi '^allowusers'; then
echo "Hinweis: In der sshd-Konfiguration steht AllowUsers. $NUTZER dort ergänzen, sonst wird der Zugang abgelehnt."
fi
sshd -t
systemctl reload ssh 2>/dev/null || systemctl reload sshd
echo "Fertig. Test: sftp $NUTZER@$(hostname -f) (Start im Ordner /shared/youtube/upload)"