Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
187 lines
7.6 KiB
Bash
Executable File
187 lines
7.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
# setup-sshd-ldap.sh
|
||
#
|
||
# Bindet sshd über SSSD an den authentik-LDAP-Outpost an (Debian/Ubuntu).
|
||
# Siehe docs/authentik-ldap-sshd.md.
|
||
#
|
||
# Verwendung:
|
||
# sudo ./setup-sshd-ldap.sh --ldap-uri ldaps://ldap.example.org:636 \
|
||
# --base-dn dc=ldap,dc=goauthentik,dc=io \
|
||
# --bind-dn cn=ldapsvc,ou=users,dc=ldap,dc=goauthentik,dc=io \
|
||
# [--server H1] [--ca-cert /pfad/ca.pem] [--bind-password-file /pfad]
|
||
set -Eeuo pipefail
|
||
IFS=$'\n\t'
|
||
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
||
|
||
readonly SSSD_CONF="/etc/sssd/sssd.conf"
|
||
readonly SSHD_DROPIN="/etc/ssh/sshd_config.d/50-authentik-ldap.conf"
|
||
readonly SUDOERS_FILE="/etc/sudoers.d/docker-deploy"
|
||
readonly DEPLOY_CONF="/etc/docker-deploy.conf"
|
||
readonly CA_TARGET="/etc/sssd/authentik-ldap-ca.pem"
|
||
|
||
SERVER="H1"
|
||
LDAP_URI="" BASE_DN="" BIND_DN="" CA_CERT="" PW_FILE=""
|
||
INSTALL_SUDOERS=true
|
||
|
||
log() { printf '[%(%H:%M:%S)T] %s\n' -1 "$*"; }
|
||
die() { printf '❌ %s\n' "$*" >&2; exit 1; }
|
||
|
||
usage() {
|
||
cat >&2 <<EOF
|
||
Verwendung: sudo $(basename "$0") --ldap-uri URI --base-dn DN --bind-dn DN [Optionen]
|
||
|
||
Pflicht:
|
||
--ldap-uri URI z. B. ldaps://ldap.example.org:636
|
||
--base-dn DN Base-DN des Outposts (authentik-Standard: dc=ldap,dc=goauthentik,dc=io)
|
||
--bind-dn DN Bind-DN des LDAP-Service-Accounts
|
||
|
||
Optional:
|
||
--bind-password-file FILE Passwort/Token des Bind-Accounts (sonst Abfrage)
|
||
--server NAME Servername, Standard H1 -> server_H1_ssh_access / server_H1_docker_compose
|
||
--ca-cert FILE CA-Zertifikat für das LDAPS-Zertifikat des Outposts
|
||
--no-sudoers sudoers-Regel für docker-deploy.sh nicht anlegen
|
||
EOF
|
||
exit 1
|
||
}
|
||
|
||
while [[ $# -gt 0 ]]; do
|
||
case "$1" in
|
||
--ldap-uri) LDAP_URI="${2:?}"; shift 2 ;;
|
||
--base-dn) BASE_DN="${2:?}"; shift 2 ;;
|
||
--bind-dn) BIND_DN="${2:?}"; shift 2 ;;
|
||
--bind-password-file) PW_FILE="${2:?}"; shift 2 ;;
|
||
--server) SERVER="${2:?}"; shift 2 ;;
|
||
--ca-cert) CA_CERT="${2:?}"; shift 2 ;;
|
||
--no-sudoers) INSTALL_SUDOERS=false; shift ;;
|
||
-h|--help) usage ;;
|
||
*) usage ;;
|
||
esac
|
||
done
|
||
|
||
[[ $EUID -eq 0 ]] || die "Muss als root laufen"
|
||
[[ -n "$LDAP_URI" && -n "$BASE_DN" && -n "$BIND_DN" ]] || usage
|
||
[[ "$LDAP_URI" == ldaps://* ]] || die "Nur ldaps:// ist erlaubt (Passwörter dürfen nicht im Klartext übertragen werden)"
|
||
[[ "$SERVER" =~ ^[A-Za-z0-9_-]+$ ]] || die "Ungültiger Servername: '$SERVER'"
|
||
[[ -z "$CA_CERT" || -r "$CA_CERT" ]] || die "CA-Zertifikat nicht lesbar: $CA_CERT"
|
||
command -v apt-get >/dev/null || die "Nur Debian/Ubuntu (apt) unterstützt"
|
||
|
||
if [[ -n "$PW_FILE" ]]; then
|
||
[[ -r "$PW_FILE" ]] || die "Passwortdatei nicht lesbar: $PW_FILE"
|
||
BIND_PW="$(<"$PW_FILE")"
|
||
else
|
||
read -rsp "Bind-Passwort für $BIND_DN: " BIND_PW; echo
|
||
fi
|
||
[[ -n "$BIND_PW" ]] || die "Leeres Bind-Passwort"
|
||
|
||
SSH_GROUP="server_${SERVER}_ssh_access"
|
||
DOCKER_GROUP="server_${SERVER}_docker_compose"
|
||
|
||
# ── Pakete ───────────────────────────────────────────────────────────────────
|
||
log "📦 Pakete installieren"
|
||
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
|
||
sssd sssd-ldap libnss-sss libpam-sss libsss-sudo openssh-server ldap-utils >/dev/null
|
||
|
||
# ── CA ───────────────────────────────────────────────────────────────────────
|
||
TLS_CACERT_LINE="# ldap_tls_cacert: System-CA-Store"
|
||
if [[ -n "$CA_CERT" ]]; then
|
||
install -m 0644 -o root -g root "$CA_CERT" "$CA_TARGET"
|
||
TLS_CACERT_LINE="ldap_tls_cacert = $CA_TARGET"
|
||
fi
|
||
|
||
# ── SSSD ─────────────────────────────────────────────────────────────────────
|
||
log "🔧 $SSSD_CONF schreiben"
|
||
install -d -m 0711 /etc/sssd
|
||
[[ -e "$SSSD_CONF" ]] && cp -a "$SSSD_CONF" "${SSSD_CONF}.bak.$(date +%s)"
|
||
umask 077
|
||
cat >"$SSSD_CONF" <<EOF
|
||
# Verwaltet von setup-sshd-ldap.sh
|
||
[sssd]
|
||
services = nss, pam, ssh
|
||
domains = authentik
|
||
|
||
[domain/authentik]
|
||
id_provider = ldap
|
||
auth_provider = ldap
|
||
chpass_provider = none
|
||
access_provider = ldap
|
||
|
||
ldap_uri = $LDAP_URI
|
||
ldap_search_base = $BASE_DN
|
||
ldap_default_bind_dn = $BIND_DN
|
||
ldap_default_authtok_type = password
|
||
ldap_default_authtok = $BIND_PW
|
||
ldap_tls_reqcert = demand
|
||
$TLS_CACERT_LINE
|
||
|
||
ldap_schema = rfc2307
|
||
ldap_user_search_base = ou=users,$BASE_DN
|
||
ldap_group_search_base = ou=groups,$BASE_DN
|
||
ldap_user_object_class = posixAccount
|
||
ldap_group_object_class = posixGroup
|
||
ldap_user_name = cn
|
||
ldap_user_gecos = name
|
||
ldap_user_home_directory = homeDirectory
|
||
ldap_user_ssh_public_key = sshPublicKey
|
||
ldap_group_member = member
|
||
|
||
# Nur Mitglieder der Server-Gruppe dürfen sich anmelden
|
||
ldap_access_order = filter
|
||
ldap_access_filter = (memberOf=cn=$SSH_GROUP,ou=groups,$BASE_DN)
|
||
|
||
fallback_homedir = /home/%u
|
||
default_shell = /bin/bash
|
||
enumerate = false
|
||
cache_credentials = true
|
||
entry_cache_timeout = 300
|
||
use_fully_qualified_names = false
|
||
EOF
|
||
chown root:root "$SSSD_CONF"
|
||
chmod 0600 "$SSSD_CONF"
|
||
umask 022
|
||
|
||
# ── NSS / PAM ────────────────────────────────────────────────────────────────
|
||
log "🔧 NSS/PAM konfigurieren"
|
||
for db in passwd group; do
|
||
grep -Eq "^${db}:.*\bsss\b" /etc/nsswitch.conf || sed -i -E "s/^(${db}:.*)$/\1 sss/" /etc/nsswitch.conf
|
||
done
|
||
install -d -m 0755 /etc/skel/deployment
|
||
pam-auth-update --enable sss mkhomedir >/dev/null
|
||
|
||
# ── sshd ─────────────────────────────────────────────────────────────────────
|
||
log "🔧 $SSHD_DROPIN schreiben"
|
||
install -d -m 0755 /etc/ssh/sshd_config.d
|
||
grep -Eq '^\s*Include\s+/etc/ssh/sshd_config.d/\*\.conf' /etc/ssh/sshd_config \
|
||
|| sed -i '1i Include /etc/ssh/sshd_config.d/*.conf' /etc/ssh/sshd_config
|
||
cat >"$SSHD_DROPIN" <<EOF
|
||
# Verwaltet von setup-sshd-ldap.sh
|
||
UsePAM yes
|
||
PubkeyAuthentication yes
|
||
# Service-Accounts (CI/CD) melden sich mit dem authentik-App-Passwort an
|
||
PasswordAuthentication yes
|
||
AuthorizedKeysCommand /usr/bin/sss_ssh_authorizedkeys %u
|
||
AuthorizedKeysCommandUser nobody
|
||
EOF
|
||
sshd -t || die "sshd-Konfiguration ungültig – Drop-in $SSHD_DROPIN prüfen"
|
||
|
||
# ── docker-deploy.sh ─────────────────────────────────────────────────────────
|
||
log "🔧 $DEPLOY_CONF schreiben"
|
||
printf 'DEPLOY_SERVER=%s\n' "$SERVER" >"$DEPLOY_CONF"
|
||
chown root:root "$DEPLOY_CONF"; chmod 0644 "$DEPLOY_CONF"
|
||
|
||
if $INSTALL_SUDOERS; then
|
||
log "🔧 $SUDOERS_FILE schreiben"
|
||
tmp="$(mktemp)"
|
||
printf '%%%s ALL=(root) NOPASSWD: /usr/local/bin/docker-deploy.sh\n' "$DOCKER_GROUP" >"$tmp"
|
||
visudo -cf "$tmp" >/dev/null || { rm -f "$tmp"; die "sudoers-Regel ungültig"; }
|
||
install -m 0440 -o root -g root "$tmp" "$SUDOERS_FILE"
|
||
rm -f "$tmp"
|
||
fi
|
||
|
||
# ── Dienste ──────────────────────────────────────────────────────────────────
|
||
log "🔄 Dienste (neu) starten"
|
||
systemctl enable --now sssd >/dev/null
|
||
systemctl restart sssd
|
||
systemctl reload ssh 2>/dev/null || systemctl reload sshd
|
||
|
||
log "✅ Fertig. Prüfen mit: ./check-sshd-ldap.sh <username>"
|