metznet-channel/tests/ldap/apply-changes.sh

95 lines
6.5 KiB
Bash

#!/usr/bin/env bash
# Rehearse ldap/changes/*.ldif before applying them to ldap.metznet.ca: load a
# cn=config dump of the live server into a scratch slapd (this channel's
# openldap-slapd), seed tests/ldap/fixture-data.ldif, apply every change with
# slapmodify exactly as the "Changing cn=config" runbook does, then check
# logins, password changes, ACLs and overlays still behave.
#
# Runs inside the guix builder container (it wipes /var/lib/slapd there):
# docker compose run --rm -T guix bash -lc 'tests/ldap/apply-changes.sh config.ldif [FIRST]'
# where config.ldif is `slapcat -n 0` from the server (any olcRootPW is
# replaced with a test password, so a redacted dump is fine). FIRST (e.g.
# 0004) applies only the changes from that number on, for a dump that already
# has the earlier ones.
#
# The "before changes" checks describe the config before 0001, so they only
# run without FIRST.
set -uo pipefail
cd "$(dirname "$0")/../.."
dump="${1:?usage: $0 <slapcat -n 0 dump> [first change number]}"
first="${2:-}"
P=$(guix build --no-grafts -L . -e '(@ (metznet packages slapd) openldap-slapd)' 2>/dev/null | tail -1)
GENERATED=$(guix build --no-grafts -L . -e '(@ (metznet machines ldap) %ldap-slapd-config-ldif)' 2>/dev/null | tail -1)
OSSL=$(guix build openssl 2>/dev/null | grep -v -- '-doc$' | tail -1)
export PATH=$P/bin:$P/sbin:$P/libexec:$OSSL/bin:$PATH
echo "openldap: $P"; ls $P/libexec/openldap/ | grep '\.so$' | tr '\n' ' '; echo
H=ldap://127.0.0.1:3890
ADMIN=uid=admin,ou=sudoers,ou=accounts,dc=metznet,dc=ca
GUIX=uid=guix,ou=system,ou=accounts,dc=metznet,dc=ca
KRB=uid=kerberos,ou=system,ou=accounts,dc=metznet,dc=ca
ALICE=uid=alice,ou=accounts,dc=metznet,dc=ca
BOB=uid=bob,ou=accounts,dc=metznet,dc=ca
# the dump's olcRootPW until 0004 drops it, then the fixture entry's own
ROOTPW=secret ADMINPW=adminpw
pw=$ROOTPW
pass=0; fail=0
check() { local name="$1"; shift; if "$@" >/tmp/out 2>&1; then pass=$((pass+1)); echo " ok $name"; else fail=$((fail+1)); echo " FAIL $name"; { grep -E '^ *FAIL' /tmp/out || head -8 /tmp/out; } | head -20 | sed 's/^/ /'; fi; }
not() { ! "$@"; }
s() { ldapsearch -x -H $H -LLL -o ldif-wrap=no "$@"; }
attr_visible() { local who=$1 pw=$2 dn=$3 a=$4; s -D "$who" -w "$pw" -b "$dn" -s base "(objectClass=*)" "$a" | grep -qi "^$a:"; }
start() { slapd -F /var/lib/slapd/slapd.d -h "$H/" ; for i in $(seq 20); do ldapwhoami -x -H $H -D $ADMIN -w $pw >/dev/null 2>&1 && return; sleep 0.5; done; echo "slapd did not start"; exit 2; }
stop() { kill "$(cat /var/run/slapd/slapd.pid)"; for i in $(seq 20); do [ -e /var/run/slapd/slapd.pid ] || return; sleep 0.5; done; }
rm -rf /var/lib/slapd /var/run/slapd
mkdir -p /var/lib/slapd/slapd.d /var/lib/slapd/data /var/lib/slapd/modules /var/run/slapd
cp $P/libexec/openldap/*.so* /var/lib/slapd/modules/ 2>/dev/null
openssl req -x509 -newkey rsa:2048 -nodes -keyout /var/lib/slapd/privkey.pem -out /var/lib/slapd/cert.pem -subj /CN=localhost -days 1 2>/dev/null
cp /var/lib/slapd/cert.pem /var/lib/slapd/chain.pem
sed "s/^olcRootPW::\? .*/olcRootPW: $ROOTPW/" "$dump" > /tmp/config.ldif
grep -qi '^olcRootPW' /tmp/config.ldif || pw=$ADMINPW
slapadd -n0 -F /var/lib/slapd/slapd.d -l /tmp/config.ldif || exit 2
slapadd -n1 -F /var/lib/slapd/slapd.d -l tests/ldap/fixture-data.ldif -q || exit 2
start
if [[ -z "$first" ]]; then
echo "before changes (live config)"
check "guix CAN read alice userPassword (the hole)" attr_visible $GUIX guixpw $ALICE userPassword
check "refint inactive: deleting carol leaves dave's manager dangling" bash -c "ldapdelete -x -H $H -D $ADMIN -w secret uid=carol,ou=accounts,dc=metznet,dc=ca && sleep 1 && ldapsearch -x -H $H -D $ADMIN -w secret -LLL -b uid=dave,ou=accounts,dc=metznet,dc=ca -s base manager | grep -qi 'uid=carol'"
check "admin cannot read cn=config yet" not s -D $ADMIN -w secret -b cn=config -s base "(objectClass=*)" cn
fi
stop
echo "apply ldap/changes with slapmodify"
for f in ldap/changes/*.ldif; do
[[ -n "$first" && "$(basename $f)" < "$first" ]] && continue
check "slapmodify $(basename $f)" slapmodify -F /var/lib/slapd/slapd.d -n 0 -l "$f"
done
check "slaptest" slaptest -F /var/lib/slapd/slapd.d -u
pw=$ADMINPW
start
echo "after changes"
check "old root password refused (no olcRootPW)" not ldapwhoami -x -H $H -D $ADMIN -w $ROOTPW
check "admin binds with its own userPassword" ldapwhoami -x -H $H -D $ADMIN -w $ADMINPW
check "admin (root DN) still bypasses ACLs: reads alice userPassword" attr_visible $ADMIN $ADMINPW $ALICE userPassword
check "guix can read alice sshPublicKey" attr_visible $GUIX guixpw $ALICE sshPublicKey
check "guix can NOT read alice userPassword" not attr_visible $GUIX guixpw $ALICE userPassword
check "kerberos can NOT read alice userPassword" not attr_visible $KRB krbpw $ALICE userPassword
check "guix can NOT compare alice userPassword" bash -c "ldapcompare -x -H $H -D $GUIX -w guixpw -z $ALICE userPassword:alicepw; [ \$? = 50 ]"
check "guix still reads alice posix attrs (sssd)" attr_visible $GUIX guixpw $ALICE uidNumber
check "guix reads group members (sssd)" attr_visible $GUIX guixpw cn=staff,ou=groups,dc=metznet,dc=ca member
check "alice binds with her password (pam_sss)" ldapwhoami -x -H $H -D $ALICE -w alicepw
check "alice rejected with wrong password" not ldapwhoami -x -H $H -D $ALICE -w nope
check "guix binds as itself" ldapwhoami -x -H $H -D $GUIX -w guixpw
check "alice changes own password (passwd)" ldappasswd -x -H $H -D $ALICE -w alicepw -s alicepw2
check "alice binds with new password" ldapwhoami -x -H $H -D $ALICE -w alicepw2
check "admin reads cn=config" s -D $ADMIN -w $ADMINPW -b cn=config -s base "(objectClass=*)" cn
check "guix can NOT read cn=config" not attr_visible $GUIX guixpw cn=config cn
check "admin can NOT write cn=config" not bash -c "printf 'dn: cn=config\nchangetype: modify\nreplace: olcLogLevel\nolcLogLevel: none\n' | ldapmodify -x -H $H -D $ADMIN -w $ADMINPW"
check "result equals the Scheme-generated config" scripts/ldap-verify.sh -H $H -D $ADMIN -w $ADMINPW -b cn=config -X -e "$GENERATED"
check "refint active: deleting bob clears alice's manager" bash -c "printf 'dn: $ALICE\nchangetype: modify\nadd: manager\nmanager: $BOB\n' | ldapmodify -x -H $H -D $ADMIN -w $ADMINPW >/dev/null; ldapdelete -x -H $H -D $ADMIN -w $ADMINPW $BOB; sleep 1; ! ldapsearch -x -H $H -D $ADMIN -w $ADMINPW -LLL -b $ALICE -s base manager | grep -qi 'uid=bob'"
check "memberof: deleting bob removes him from cn=staff" bash -c "! ldapsearch -x -H $H -D $ADMIN -w $ADMINPW -LLL -b cn=staff,ou=groups,dc=metznet,dc=ca -s base member | grep -qi 'uid=bob'"
stop
echo; echo "$pass passed, $fail failed"
[ $fail -eq 0 ]