Added slapd.d creation from scheme

master
Joan 2026-11-20 17:07:47 -06:00
parent aa3bdcdb59
commit 95b3801b1e
8 changed files with 246 additions and 81 deletions

@ -0,0 +1,11 @@
# Drop {1}mdb's separate root password. uid=admin stays olcRootDN, so it is
# still exempt from the data ACLs, but it now authenticates with its own
# userPassword: data, restored with the rest of the directory, so cn=config
# holds no secret and can be built from the repo. cn=config stays read-only
# for it ({0}config's root is local root over ldapi).
#
# Before applying, check the entry has a userPassword you know (a bind with
# it succeeds), or admin is locked out until one is set with slapmodify.
dn: olcDatabase={1}mdb,cn=config
changetype: modify
delete: olcRootPW

@ -153,21 +153,39 @@ OpenLDAP (`openldap-slapd` from `metznet/packages/slapd.scm`, which ships
the `pw-totp` module) plus certbot for `ldap.metznet.ca`. slapd listens on
`ldap://` and `ldaps://` (no `ldapi://`).
`metznet/services/slapd.scm` does **not** generate slapd's configuration.
slapd runs with `-F /var/lib/slapd/slapd.d`, so the schema, ACLs, TLS cert
paths, root DN and database definitions all live in the on-disk
`cn=config` database. Its intended contents are described in Scheme
(`%ldap-slapd-config` in `ldap.scm`), and the live server is checked against
that (see *Changing cn=config*). Only root over `ldapi://` can write `cn=config`, and slapd
doesn't listen on `ldapi://`, so changes are made offline (see *Changing
cn=config* below). `{1}mdb`'s root DN (`uid=admin,ou=sudoers,ou=accounts,dc=metznet,dc=ca`)
can read it over `ldaps://`. On every activation the service:
paths, root DN and database definitions live in the on-disk `cn=config`
database. Its contents are described in Scheme (`%ldap-slapd-config` in
`ldap.scm`), which the service's `config` field points at. A host with no
`slapd.d` builds it from that. An existing `slapd.d` is never touched, so a
change to a running server is applied with a change file (see *Changing
cn=config*), and the live server is checked against the Scheme. Only root
over `ldapi://` can write `cn=config`, and slapd doesn't listen on
`ldapi://`, so changes are made offline. `{1}mdb`'s root DN
(`uid=admin,ou=sudoers,ou=accounts,dc=metznet,dc=ca`) can read it over
`ldaps://`. On every activation the service:
1. Creates `/var/run/slapd`, `/var/lib/slapd/{slapd.d,modules,data}`, all
owned by `slapd`.
2. Copies `pw-totp.so` into `/var/lib/slapd/modules` (so `olcModulePath`
can point at a stable path rather than a store path).
3. Runs the seed check (see *Seeding* below).
can point at a stable path rather than a store path). The other modules
in `olcModuleLoad` are built into slapd.
3. If `slapd.d` has no `cn=config.ldif`, `slapadd -n 0`s the generated
config into it, owned by `slapd` (`slapd-init`). Data is never loaded.
4. Runs the seed check (see *Seeding* below).
**Admin login.** `uid=admin` is `{1}mdb`'s root DN, so it bypasses the data
ACLs, but there is no `olcRootPW` (removed by `ldap/changes/0004`). It logs in
with its own `userPassword`, which is data, so it comes back with every
restore and `cn=config` holds no secret. On a brand-new directory with no
backup to restore, set it offline:
```sh
herd stop slapd
printf 'dn: uid=admin,ou=sudoers,ou=accounts,dc=metznet,dc=ca\nchangetype: modify\nreplace: userPassword\nuserPassword: %s\n' \
"$(slappasswd)" | sudo -u slapd slapmodify -F /var/lib/slapd/slapd.d -n 1
herd start slapd
```
Tree layout that other machines rely on:
@ -195,8 +213,8 @@ Caveats:
`backup-date` to when the dump was taken, or remove `backups` once the
restore is done.
- Seed LDIFs become `local-file`s in the world-readable store. They contain
password hashes (including `olcRootPW`), so prefer the manual restore
below for production data.
password hashes, so prefer the manual restore below for production data.
- `backups` and `config` can't both be set, since both would load `-n 0`.
---
@ -205,19 +223,26 @@ Caveats:
This moves `ldap.metznet.ca` to new hardware or a new VM while keeping the
host name, so no client config changes are needed.
**1. Bring up the new host without data.** Partition with labels
**1. Check the old instance matches the repo.** The new host's `cn=config`
is built from the repo, not copied, so anything live that the repo lacks
would be lost. Run the two exact checks from step 6 against the old host. If
the config differs, fix `%ldap-slapd-config` or apply the missing change
files first.
**2. Bring up the new host without data.** Partition with labels
`guix-data`/`guix-boot`, install, and reconfigure with
`metznet/machines/ldap.scm` (no `backups`). slapd will fail to start
because `slapd.d` is empty. That is expected. Copy the
`/var/lib/openvpn/*` credentials across if the host needs the VPN.
`metznet/machines/ldap.scm`. Activation builds `slapd.d` from the
generated config. slapd won't start yet because it has no TLS certificate
(step 5). That is expected. Copy the `/var/lib/openvpn/*` credentials across
if the host needs the VPN.
**2. Dump the old instance.** MDB supports consistent online `slapcat`,
**3. Dump the old instance.** MDB supports consistent online `slapcat`,
but stopping slapd first guarantees nothing changes after the dump:
```sh
# on the OLD host
sudo herd stop slapd
sudo -u slapd slapcat -F /var/lib/slapd/slapd.d -n 0 -l /root/config.ldif
sudo -u slapd slapcat -F /var/lib/slapd/slapd.d -n 0 -l /root/config.ldif # for the record
sudo -u slapd slapcat -F /var/lib/slapd/slapd.d -n 1 -l /root/data.ldif
sudo herd start slapd # keep serving until cutover, but treat it as read-only from here
```
@ -227,25 +252,20 @@ sudo herd start slapd # keep serving until cutover, but treat it as read-on
(`grep '^dn: olcDatabase' config.ldif`). If there is more than one data DB,
dump each by its number.
**3. Review `config.ldif` for host-specific paths.** Look at
`olcDbDirectory` (should be `/var/lib/slapd/data`), `olcModulePath`
(`/var/lib/slapd/modules`), `olcTLSCertificateFile`/`KeyFile`/`CACertificateFile`
(`/var/lib/slapd/{cert,privkey,chain}.pem`), and any
`/gnu/store/...` paths. Store paths from the old host will not exist on the
new one, so point them at stable locations.
**4. Restore on the new host.** Move the dumps over with `scp`. Keep them out
of `/gnu/store`.
**4. Restore the data on the new host.** Move `data.ldif` over with `scp`.
Keep it out of `/gnu/store`. Only the data is restored; `config.ldif` stays
with the backups.
```sh
# on the NEW host
sudo herd stop slapd
sudo rm -rf /var/lib/slapd/slapd.d/* /var/lib/slapd/data/*
sudo -u slapd slapadd -F /var/lib/slapd/slapd.d -n 0 -l config.ldif
sudo rm -rf /var/lib/slapd/data/*
sudo -u slapd slapadd -F /var/lib/slapd/slapd.d -n 1 -l data.ldif
sudo -u slapd slaptest -F /var/lib/slapd/slapd.d -u # sanity-check config
```
`tests/ldap/first-boot.sh` rehearses steps 2 and 4 in the container.
**5. TLS.** slapd serves copies of the certificate in
`/var/lib/slapd/{cert,chain,privkey}.pem`, not certbot's files. Copy those
three from the old host and make them readable by `slapd`. certbot on the
@ -308,9 +328,10 @@ the new instance in between are lost unless you `slapcat` them back.
#### Changing cn=config
The intended config is `%ldap-slapd-config` in `ldap.scm` (records from
`metznet/services/slapd-config.scm`). Until slapd is started from it, a
change is made in two places, in the same commit:
The config is `%ldap-slapd-config` in `ldap.scm` (records from
`metznet/services/slapd-config.scm`). New hosts are built from it, but a
running server's `slapd.d` is never rebuilt, so a change is made in two
places, in the same commit:
1. Edit `%ldap-slapd-config`.
2. Write the same change as a numbered LDIF modify in `ldap/changes/`, to
@ -318,12 +339,13 @@ change is made in two places, in the same commit:
Check the Scheme builds and behaves, and rehearse the change file against a
dump of the live config (`slapcat -n 0` output; a redacted `olcRootPW` is
fine). The rehearsal fails unless the dump plus the change files equals the
generated config exactly:
fine). Pass the number of the first change the dump doesn't have yet. The
rehearsal fails unless the dump plus those changes equals the generated
config exactly:
```sh
docker compose run --rm -T guix bash -lc tests/ldap/generated-config.sh
docker compose run --rm -T guix bash -lc 'tests/ldap/apply-changes.sh config.ldif'
docker compose run --rm -T guix bash -lc 'tests/ldap/apply-changes.sh config.ldif 0004'
```
Then apply it with slapd stopped. `slapmodify` needs OpenLDAP 2.5 or later:

@ -11,34 +11,10 @@
%ldap-slapd-config %ldap-slapd-config-ldif
%ldap-structure %ldap-structure-ldif))
;; Restored from metznet-channel commit 425b173's metznet/machines/ldap.scm
;; (deleted there when system configs were split into a separate repo).
;; Deliberately no seed data here, matching that original -- the real
;; ldap.metznet.ca server already has a populated, live directory tree;
;; servers/ldap.scm's elaborate LDIF seeding (dc=metznet,dc=ca org,
;; ou=user/ou=group) doesn't match the ou=system,ou=accounts structure
;; the rest of this channel (sssd, kerberos.scm) actually binds against,
;; so it looks like a divergent prototype that was never really deployed.
(define-public ldap-services
(append (list (service certbot-service-type
(certbot-configuration
(email "admin@metznet.ca")
(certificates (list (certificate-configuration
(domains '("ldap.metznet.ca"))
;; slapd serves copies it owns
(deploy-hook
(certificate-copy-deploy-hook
"ldap.metznet.ca" "/var/lib/slapd" "slapd"
#:restart '(slapd))))))))
(service slapd-service-type
(slapd-configuration (uris "ldap:// ldaps://"))))
%metznet-aws-server-services))
;; ldap.metznet.ca's cn=config as Scheme: the single description of it. Not
;; wired into slapd-service-type yet -- the server still runs from its
;; on-disk slapd.d -- but kept equal to it: tests/ldap/generated-config.sh
;; builds build/slapd-config.ldif, and the live server is checked against
;; that with `ldap-verify.sh -b cn=config -X -e build/slapd-config.ldif`.
;; ldap.metznet.ca's cn=config as Scheme: the single description of it. A
;; new host's slapd.d is built from it; an existing one is kept equal to it
;; with ldap/changes, checked by
;; `ldap-verify.sh -b cn=config -X -e build/slapd-config.ldif`.
(define %suffix "dc=metznet,dc=ca")
(define (system-account uid)
@ -296,6 +272,26 @@
"krbLoginFailedCount" "krbLastPwdChange"
"krbExtraData" "member" "memberUid" "memberOf")))
;; slapd builds slapd.d from %ldap-slapd-config-ldif on a host that has none
;; and leaves an existing one alone (change that with ldap/changes). No data
;; is loaded: a new host gets its directory by restoring a backup.
(define-public ldap-services
(append (list (service certbot-service-type
(certbot-configuration
(email "admin@metznet.ca")
(certificates (list (certificate-configuration
(domains '("ldap.metznet.ca"))
;; slapd serves copies it owns
(deploy-hook
(certificate-copy-deploy-hook
"ldap.metznet.ca" "/var/lib/slapd" "slapd"
#:restart '(slapd))))))))
(service slapd-service-type
(slapd-configuration
(uris "ldap:// ldaps://")
(config %ldap-slapd-config-ldif))))
%metznet-aws-server-services))
(define-public ldap.metznet.ca
(operating-system
(inherit %metznet-base-aws-system)

@ -13,11 +13,18 @@
(define list-of-file-like?
(list-of file-like?))
(define (file-like-or-false? x)
(or (not x) (file-like? x)))
(define-configuration/no-serialization slapd-configuration
(openldap (file-like openldap-slapd)
"openldap package to use")
(home (string "/var/lib/slapd")
"slapd home directory")
(config (file-like-or-false #f)
"cn=config LDIF (slapcat -n 0 shape) to build slapd.d from when
it has none yet. An existing slapd.d is never touched: change it with
ldap/changes and slapmodify.")
(backups (list-of-file-like '())
"alist of databases and their backup ldif files")
(backup-date (date (current-date))
@ -38,10 +45,41 @@
(home-directory "/var/lib/slapd")
(shell #~(string-append #$shadow "/sbin/nologin")))))
(define (slapd-init-program config)
"A program that builds HOME/slapd.d from CONFIG's cn=config LDIF, owned by
slapd, unless it already holds a config. Data is never loaded: restore it
with slapadd -n 1."
(let ((homedir (slapd-configuration-home config))
(ldif (slapd-configuration-config config))
(ldapdir (slapd-configuration-openldap config)))
(program-file
"slapd-init"
(with-imported-modules '((guix build utils))
#~(begin
(use-modules (guix build utils))
(let ((dir #$(string-append homedir "/slapd.d"))
(user (getpw "slapd")))
(if (file-exists? (string-append dir "/cn=config.ldif"))
(format #t "slapd-init: keeping existing ~a~%" dir)
(begin
(format #t "slapd-init: building ~a from ~a~%" dir #$ldif)
(mkdir-p dir)
(invoke #$(file-append ldapdir "/sbin/slapadd")
"-n" "0" "-F" dir "-l" #$ldif)
(for-each (lambda (f)
(chown f (passwd:uid user) (passwd:gid user)))
(cons dir (find-files dir #:directories? #t)))))))))))
(define (slapd-activation config)
(when (and (slapd-configuration-config config)
(pair? (slapd-configuration-backups config)))
;; both would slapadd -n 0
(error "slapd-configuration: set config or backups, not both"))
(let ((homedir (slapd-configuration-home config))
(backups (slapd-configuration-backups config))
(ldapdir (slapd-configuration-openldap config)))
(ldapdir (slapd-configuration-openldap config))
(init (and (slapd-configuration-config config)
(slapd-init-program config))))
#~(begin
(use-modules (srfi srfi-19)
(ice-9 textual-ports))
@ -115,6 +153,9 @@
(mkdir-p/perms (string-append #$homedir
"/data") user
488))
#$@(if init
(list #~(invoke #$init))
'())
(with-exception-handler slapadd-seeds
check-slapadd-seed-date
#:unwind? #t))))

@ -34,7 +34,7 @@
# -X exact (needs -b/-f): additionally fail on any entry or
# value in the dump that no -e file lists, so the -e files
# are a complete snapshot rather than a subset. Ignores
# operational attributes, olcRootPW, and the definitions
# operational attributes and the definitions
# in cn=schema,cn=config and the schema entries under it
# (compared by entry only; the live subschema check
# covers what's used).
@ -70,7 +70,7 @@ print=""
ignore_dns=()
keep_dns=()
extra_ignores=""
ignore="entryuuid creatorsname createtimestamp entrycsn modifiersname modifytimestamp structuralobjectclass contextcsn entrydn hassubordinates subschemasubentry olcrootpw"
ignore="entryuuid creatorsname createtimestamp entrycsn modifiersname modifytimestamp structuralobjectclass contextcsn entrydn hassubordinates subschemasubentry"
schema_defs="olcattributetypes olcobjectclasses olcobjectidentifier olcldapsyntaxes olcditcontentrules"
while getopts "H:D:w:y:xb:f:Xi:I:K:ps:e:h" opt; do
@ -328,8 +328,7 @@ for ldif in "${ldif_files[@]}"; do
continue
fi
[[ "$skip" == "$dn" ]] && continue
# what -X ignores in a dump (schema definitions, olcRootPW, operational
# attributes) isn't compared from the expected side either, so a
# what -X ignores in a dump (schema definitions, operational attributes) isn't compared from the expected side either, so a
# generated config can be the expected file
if [[ -n "$dump" ]] && ignored "$(tr 'A-Z' 'a-z' <<<"$attr")" "$(norm_dn "$dn")"; then continue; fi
out=""

@ -6,15 +6,18 @@
# 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'
# where config.ldif is `slapcat -n 0` from the server (olcRootPW is replaced
# with a test password, so a redacted dump is fine).
# 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* ldap/changes/
# 0002-0003 -- update them once a dump taken after applying those is used.
# 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>}"
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)
@ -26,12 +29,15 @@ 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 secret >/dev/null 2>&1 && return; sleep 0.5; done; echo "slapd did not start"; exit 2; }
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
@ -39,25 +45,33 @@ mkdir -p /var/lib/slapd/slapd.d /var/lib/slapd/data /var/lib/slapd/modules /var/
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: secret/' "$dump" > /tmp/config.ldif
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
@ -69,12 +83,12 @@ check "alice rejected with wrong password" not ldapwhoami -x -H $H -D $ALICE -w
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 secret -b cn=config -s base "(objectClass=*)" cn
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 secret"
check "result equals the Scheme-generated config" scripts/ldap-verify.sh -H $H -D $ADMIN -w secret -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 secret >/dev/null; ldapdelete -x -H $H -D $ADMIN -w secret $BOB; sleep 1; ! ldapsearch -x -H $H -D $ADMIN -w secret -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 secret -LLL -b cn=staff,ou=groups,dc=metznet,dc=ca -s base member | grep -qi 'uid=bob'"
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 ]

@ -0,0 +1,70 @@
#!/usr/bin/env bash
# What slapd-service-type does on a host with no slapd.d yet: run its
# slapd-init program (built from ldap.scm's slapd-configuration), check it
# builds slapd.d from %ldap-slapd-config-ldif owned by slapd, that running it
# again keeps what's there, and that slapd then starts as slapd and serves a
# restored backup (tests/ldap/fixture-data.ldif stands in for one).
#
# Runs inside the guix builder container (it wipes /var/lib/slapd there):
# docker compose run --rm -T guix bash -lc tests/ldap/first-boot.sh
set -uo pipefail
cd "$(dirname "$0")/../.."
build() { guix build --no-grafts -L . -e "$1" 2>/tmp/build.log | tail -1; }
INIT=$(build '((@@ (metznet services slapd) slapd-init-program)
((@ (gnu services) service-parameters)
((@ (srfi srfi-1) find)
(lambda (s) (eq? ((@ (gnu services) service-type-name) ((@ (gnu services) service-kind) s)) (quote slapd)))
(@ (metznet machines ldap) ldap-services))))')
[[ -n "$INIT" && -x "$INIT" ]] || { tail -20 /tmp/build.log; exit 2; }
GENERATED=$(build '(@ (metznet machines ldap) %ldap-slapd-config-ldif)')
P=$(build '(@ (metznet packages slapd) openldap-slapd)')
OSSL=$(guix build openssl 2>/dev/null | grep -v -- '-doc$' | tail -1)
export PATH=$P/bin:$P/sbin:$P/libexec:$OSSL/bin:$PATH
H=ldap://127.0.0.1:3892
ADMIN=uid=admin,ou=sudoers,ou=accounts,dc=metznet,dc=ca
D=/var/lib/slapd/slapd.d
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() { ! "$@"; }
starts() { slapd -F $D -h "$H/" -u slapd -g slapd || return 1
for i in $(seq 20); do ldapwhoami -x -H $H -D $ADMIN -w adminpw >/dev/null 2>&1 && return; sleep 0.5; done; return 1; }
stop() { kill "$(cat /var/run/slapd/slapd.pid 2>/dev/null)" 2>/dev/null; for i in $(seq 20); do [ -e /var/run/slapd/slapd.pid ] || return 0; sleep 0.5; done; }
owned_by_slapd() { [ -z "$(find "$1" ! -user slapd -o ! -group slapd)" ]; }
# what activation sets up before running slapd-init
id slapd >/dev/null 2>&1 || useradd --system --user-group --home-dir /var/lib/slapd slapd
rm -rf /var/lib/slapd /var/run/slapd
install -d -o slapd -g slapd -m 755 /var/run/slapd /var/lib/slapd
install -d -o slapd -g slapd -m 750 $D /var/lib/slapd/modules /var/lib/slapd/data
cp $P/libexec/openldap/pw-totp.so /var/lib/slapd/modules/
echo "first boot"
check "slapd-init builds slapd.d" "$INIT"
check "slapd.d has a config" test -f $D/cn=config.ldif
check "slapd.d is owned by slapd" owned_by_slapd $D
check "slaptest" slaptest -F $D -u
check "slapd.d equals the generated config" bash -c "slapcat -n0 -F $D -o ldif-wrap=no > /tmp/n0.ldif && scripts/ldap-verify.sh -f /tmp/n0.ldif -X -e '$GENERATED'"
check "no olcRootPW" not grep -qri '^olcRootPW' $D
echo "second boot"
printf 'dn: cn=config\nchangetype: modify\nreplace: olcLogLevel\nolcLogLevel: stats sync\n' | slapmodify -n0 -F $D -q >/dev/null 2>&1
chown -R slapd:slapd $D
check "slapd-init keeps an existing slapd.d" bash -c "'$INIT' | grep -q keeping"
check "a change made since is still there" bash -c "slapcat -n0 -F $D -a '(objectClass=olcGlobal)' | grep -qi '^olcLogLevel: stats sync'"
echo "start and restore"
check "slapd won't start before the certificate is copied" not starts
stop
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
chown slapd:slapd /var/lib/slapd/*.pem
check "restore a backup as slapd (slapadd -n 1)" runuser -u slapd -- slapadd -q -n1 -F $D -l tests/ldap/fixture-data.ldif
check "slapd starts as slapd" starts
check "admin binds with its restored userPassword" ldapwhoami -x -H $H -D $ADMIN -w adminpw
check "alice binds" ldapwhoami -x -H $H -D uid=alice,ou=accounts,dc=metznet,dc=ca -w alicepw
stop
echo; echo "$pass passed, $fail failed"
[ $fail -eq 0 ]

@ -14,6 +14,18 @@ dn: ou=system,ou=accounts,dc=metznet,dc=ca
objectClass: organizationalUnit
ou: system
dn: ou=sudoers,ou=accounts,dc=metznet,dc=ca
objectClass: organizationalUnit
ou: sudoers
# the mdb olcRootDN; with no olcRootPW it binds with this password
dn: uid=admin,ou=sudoers,ou=accounts,dc=metznet,dc=ca
objectClass: inetOrgPerson
cn: admin
sn: admin
uid: admin
userPassword: adminpw
dn: ou=groups,dc=metznet,dc=ca
objectClass: organizationalUnit
ou: groups