diff --git a/.gitignore b/.gitignore index 255b862..474dbad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ .env *.ldif pki/ +build/ +# test expectations, not dumps (see scripts/ldap-verify.sh) +!tests/ldap/*.ldif +!ldap/changes/*.ldif diff --git a/.sops.yaml b/.sops.yaml new file mode 100644 index 0000000..84bcde7 --- /dev/null +++ b/.sops.yaml @@ -0,0 +1,16 @@ +# Recipients for secrets/secrets.enc.yaml. Each entry is meant to be the +# *public* half of an age key whose private half exists only sealed +# inside one target machine's TPM (see scripts/seal-age-key.sh) -- never +# on disk in plain text. Re-encrypt for a new/rotated recipient with: +# sops updatekeys secrets/secrets.enc.yaml +# +# TEST FIXTURE: the key below is a disposable age key generated purely to +# prove the sops mechanism structurally (guix system build / docker-image +# succeeding) -- it was never sealed into a real TPM and secrets.enc.yaml +# holds a placeholder value, not a real credential. Run +# scripts/seal-age-key.sh on the real kerberos.metznet.ca hardware and +# replace this recipient (and %kerberos-secrets-tpm-handle in +# metznet/machines/kerberos.scm) before relying on this for real. +creation_rules: + - path_regex: secrets/secrets\.enc\.yaml$ + age: age1cfqm9lzjf7y3kvjca5r06kvy84p6a5efh5p79m3xxwehyfh3gqgqefqgvw diff --git a/docker-compose.yml b/docker-compose.yml index 87a7f12..240a2f8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,17 @@ services: platform: linux/amd64 build: context: docker + # Guix's build sandbox calls personality(ADDR_NO_RANDOMIZE) to disable + # ASLR for reproducible builds. Docker's default seccomp profile denies + # that specific personality() flag combination, so every `guix pull`/ + # `guix system` build fails with "cannot set personality: Operation + # not permitted" regardless of amd64 emulation backend (reproduces + # under both Rosetta and QEMU) -- it's a seccomp restriction, not an + # emulation gap. We're already trading away sandboxing here (see + # entrypoint.sh's --disable-chroot), so drop seccomp filtering too + # rather than hand-maintain an allowlist. + security_opt: + - seccomp:unconfined volumes: - guix-store:/gnu - guix-state:/var/guix diff --git a/docker/Dockerfile b/docker/Dockerfile index c02da6e..4320bb5 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,4 +1,4 @@ -# Guix build environment for building `guix system docker-image` images +# Guix build environment for building `guix system image -t docker` images # from this repo on a machine (e.g. macOS) that can't run Guix natively. # # Guix state lives under /gnu and /var/guix, which the compose file mounts @@ -20,7 +20,14 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ # entrypoint.sh does. `yes ''` answers every prompt with its default, # which includes authorizing the ci.guix.gnu.org / bordeaux substitute # servers so builds don't compile everything from source. +# +# The installer's default GNU_URL (ftpmirror.gnu.org) redirects through +# GNU's mirror picker, whose Location header currently double-slashes the +# path (.../gnu//gnu/guix/) -- 404s against whatever mirror it lands on, +# consistently, not just flaky. Point it at ftp.gnu.org directly instead, +# which doesn't go through that redirector. RUN wget -O /tmp/guix-install.sh https://guix.gnu.org/guix-install.sh \ + && sed -i 's#^GNU_URL=.*#GNU_URL="https://ftp.gnu.org/gnu/guix/"#' /tmp/guix-install.sh \ && chmod +x /tmp/guix-install.sh \ && yes '' | /tmp/guix-install.sh \ && rm -f /tmp/guix-install.sh \ diff --git a/docker/README.md b/docker/README.md index 5922404..b1db3f9 100644 --- a/docker/README.md +++ b/docker/README.md @@ -109,6 +109,32 @@ them before calling `guix-docker.sh`, or in a `.env` file next to Once running, `herd status` inside the container shows shepherd service state — useful for checking whether e.g. `slapd` actually came up. +## Secrets (sops + TPM) + +Machine-specific secrets (e.g. `kerberos.scm`'s LDAP bind password) are +managed by `metznet/services/secrets.scm`: a single sops-encrypted YAML +per repo (`secrets/secrets.enc.yaml`, safe to commit) holding one key per +secret, decrypted at system activation using an age key whose *private* +half lives only sealed inside that machine's TPM (never on disk in plain +text) — sealed once per machine via `scripts/seal-age-key.sh`, which also +prints the public key to add to `.sops.yaml`. + +**This can't be exercised end-to-end in Docker Desktop on macOS** — there's +no TPM device passthrough, so `tpm2_unseal` has nothing real to unseal +against. What this environment *can* verify is that the mechanism is +structurally sound: `scripts/guix-docker.sh image metznet/machines/kerberos.scm` +succeeding proves `metznet/services/secrets.scm` and its wiring in +`kerberos.scm` evaluate and build correctly. The committed +`secrets/secrets.enc.yaml` and the recipient in `.sops.yaml` are a **test +fixture** — encrypted to a disposable age key that was never sealed into +any real TPM, holding a placeholder value, not a real credential. Real +provisioning (`scripts/seal-age-key.sh` against the actual +`kerberos.metznet.ca` hardware, then `sops updatekeys` to re-encrypt for +the real recipient) is a separate, later step against real hardware — or +against a software TPM (`swtpm`) if you want to exercise the unseal path +itself without real hardware. The provisioning steps are written up in +`metznet/machines/README.md` under `kerberos.scm`. + ## Known gotchas - `channels.d/metznet.scm` has no `introduction`, so `guix pull` doesn't @@ -129,7 +155,7 @@ state — useful for checking whether e.g. `slapd` actually came up. `--install-layout=deb` flag setuptools no longer accepts; `metznet-kitty` disables tests for the same reason (`O_TMPFILE` atomicity isn't honored by Docker Desktop's virtualized filesystem on - macOS); `openldap-slapd` forces `-std=gnu17` plus several `-Wno-*` - flags because GCC 14 turned some of its 25-year-old autoconf checks - into hard errors, and adds `libxcrypt` as an input since glibc dropped - `crypt()`. + macOS); `openldap-slapd` is pinned to 2.6.13, which builds cleanly + under GCC 14 (2.6.4 needed `-std=gnu17` plus several `-Wno-*` flags + because GCC 14 turned some of its autoconf checks into hard errors), + and adds `libxcrypt` as an input since glibc dropped `crypt()`. diff --git a/ldap/changes/0001-admin-read-config.ldif b/ldap/changes/0001-admin-read-config.ldif new file mode 100644 index 0000000..89eb2f2 --- /dev/null +++ b/ldap/changes/0001-admin-read-config.ldif @@ -0,0 +1,7 @@ +# Let {1}mdb's root DN read (not write) cn=config over ldaps://, so +# the live config can be checked with ldap-verify.sh -b cn=config. +# {0} hands everyone but local root on to this rule with "by * break". +dn: olcDatabase={0}config,cn=config +changetype: modify +add: olcAccess +olcAccess: {1}to * by dn.exact="uid=admin,ou=sudoers,ou=accounts,dc=metznet,dc=ca" read by * none diff --git a/ldap/changes/0002-split-userpassword-acl.ldif b/ldap/changes/0002-split-userpassword-acl.ldif new file mode 100644 index 0000000..c2da68e --- /dev/null +++ b/ldap/changes/0002-split-userpassword-acl.ldif @@ -0,0 +1,26 @@ +# Split userPassword and sshPublicKey into separate rules so system +# accounts (uid=guix, uid=kerberos: everything under ou=system) can still +# read SSH keys for sshd's AuthorizedKeysCommand and sssd's ssh responder, +# but no longer read userPassword. uid=guix's password is in /gnu/store on +# every machine (LDAP_AUTHTOK), so that read gave any local user every +# password hash and pw-totp secret. +# +# Password checks are unaffected: sssd (pam_sss, auth_provider ldap) binds +# as the user, which is "by anonymous auth"; offline logins use sssd's own +# credential cache; passwd changes as the user are "by self write". +# +# replace rewrites the whole list; {1}..{8} are the old {1}..{8} shifted down +# one and otherwise unchanged. +dn: olcDatabase={1}mdb,cn=config +changetype: modify +replace: olcAccess +olcAccess: {0}to attrs=userPassword by self write by dn.exact="uid=daemon,ou=system,ou=accounts,dc=metznet,dc=ca" write by anonymous auth by * none +olcAccess: {1}to attrs=sshPublicKey by self write by dn.exact="uid=daemon,ou=system,ou=accounts,dc=metznet,dc=ca" write by dn.children="ou=system,ou=accounts,dc=metznet,dc=ca" read by * none +olcAccess: {2}to attrs=shadowLastChange by self write by * read +olcAccess: {3}to attrs=krbPrincipalKey by anonymous auth by dn.exact="uid=kerberos,ou=system,ou=accounts,dc=metznet,dc=ca" write by self write by * none +olcAccess: {4}to dn.subtree="ou=groups,dc=metznet,dc=ca" by * read break +olcAccess: {5}to dn.subtree="cn=kerberos,dc=metznet,dc=ca" by dn.exact="uid=kerberos,ou=system,ou=accounts,dc=metznet,dc=ca" write by * none break +olcAccess: {6}to dn.subtree="ou=accounts,dc=metznet,dc=ca" by dn.exact="uid=kerberos,ou=system,ou=accounts,dc=metznet,dc=ca" write by dn.exact="uid=daemon,ou=system,ou=accounts,dc=metznet,dc=ca" write by * none break +olcAccess: {7}to dn.subtree="ou=services,dc=metznet,dc=ca" by dn.exact="uid=kerberos,ou=system,ou=accounts,dc=metznet,dc=ca" write by dn.exact="uid=daemon,ou=system,ou=accounts,dc=metznet,dc=ca" write by * none break +olcAccess: {8}to attrs=krbPrincipalName,krbLastPwdChange,krbLastFailedAuth,krbLoginFailedCount,krbLastSuccessfulAuth,krbExtraData,krbTicketFlags,krbPasswordExpiration by self read by dn.exact="uid=kerberos,ou=system,ou=accounts,dc=metznet,dc=ca" write by dn.exact="uid=daemon,ou=system,ou=accounts,dc=metznet,dc=ca" write by * none +olcAccess: {9}to * by * read diff --git a/ldap/changes/0003-move-refint-to-mdb.ldif b/ldap/changes/0003-move-refint-to-mdb.ldif new file mode 100644 index 0000000..908da99 --- /dev/null +++ b/ldap/changes/0003-move-refint-to-mdb.ldif @@ -0,0 +1,19 @@ +# refint was attached to the cn=config database, where it does nothing. +# Attach it to {1}mdb so deleting/renaming an entry also fixes up member, +# manager, owner and memberof values pointing at it (same attributes as +# before -- only the database it is attached to changes). One value per +# attribute: slapd warns that a space-separated list in one value is +# unsupported and will be rejected by a future version. +dn: olcOverlay={1}refint,olcDatabase={0}config,cn=config +changetype: delete + +dn: olcOverlay={3}refint,olcDatabase={1}mdb,cn=config +changetype: add +objectClass: olcConfig +objectClass: olcOverlayConfig +objectClass: olcRefintConfig +olcOverlay: {3}refint +olcRefintAttribute: memberof +olcRefintAttribute: member +olcRefintAttribute: manager +olcRefintAttribute: owner diff --git a/metznet/aws.scm b/metznet/aws.scm index 2d104f6..f66c0a2 100644 --- a/metznet/aws.scm +++ b/metznet/aws.scm @@ -89,7 +89,11 @@ (user-account (name "aws") (group "aws") - (password (crypt "aws" "$6$change")) + ;; key-only: "*" matches no password, so su/console logins + ;; fail. Not "!", which sshd treats as locked when UsePAM + ;; is off and then refuses keys too. #f would keep whatever + ;; hash /etc/shadow already has. + (password "*") (shell (file-append zsh "/bin/zsh"))))) (define-public aws-service-type diff --git a/metznet/machines/README.md b/metznet/machines/README.md new file mode 100644 index 0000000..945a3ab --- /dev/null +++ b/metznet/machines/README.md @@ -0,0 +1,510 @@ +# metznet/machines + +One file per host. Each file defines an `operating-system` and ends with +that value as its last expression, so you can pass the file straight to +`guix system` (`guix system reconfigure -L metznet/machines/ldap.scm`). +You can also reference the exported variable, e.g. +`(@ (metznet machines ldap) ldap.metznet.ca)`. + +Everything shared lives in `metznet/system/base-system.scm`: + +| Base | Used by | Provides | +|---------------------------------|----------------------------------|----------| +| `%metznet-base-server-system` | `base-server` | GRUB EFI, labelled disks (`guix-data` = `/`, `guix-boot` = `/boot/efi`), serial console, `%metznet-server-services` | +| `%metznet-base-aws-system` | `ldap`, `kerberos`, `vpn` | Server base plus `aws-service-type` (`%metznet-aws-server-services`): the EC2 launch key pair for the `aws` account, see below | +| `%metznet-base-desktop-system` | `otto` | Server base plus SDDM/i3, NetworkManager, nonguix substitutes, desktop setuid programs | + +Machines that set their own `services` must append to the base's list +(`%metznet-server-services` or `%metznet-aws-server-services`); otherwise they +silently lose the base's services. + +**AWS (`metznet/aws.scm`).** The `aws-pubkey` one-shot fetches the instance's +launch key pair from the EC2 metadata service at boot and writes it to +`/etc/ssh/authorized_keys.d/aws`, which sshd reads for the local `aws` +account. That login keeps working when LDAP is down, so it is the way back +into the AWS hosts. The service also adds `le-certs` and `nss-certs` to the +system profile. + +Every machine (server or desktop) gets `%metznet-services`: + +- **sssd** reads users and groups from `ldaps://ldap.metznet.ca` and binds as + `uid=guix,ou=system,ou=accounts,dc=metznet,dc=ca`. +- **krb5 + pam_krb5** use realm `METZNET.CA`, with the KDC and kadmin at + `kerberos.metznet.ca`. +- **openssh** has passwords disabled. Its `AuthorizedKeysCommand` looks up SSH + public keys in LDAP. +- **openvpn client** connects to `vpn.metznet.ca`. +- **pam_mkhomedir** and **metznet-service-type** (shell symlinks, `le-certs`). + +## Service dependency graph + +``` +ldap.metznet.ca ◄── sssd / ssh pubkey lookup on every machine + ▲ + │ ldaps:// (KDC database lives in LDAP under cn=kerberos,dc=metznet,dc=ca) +kerberos.metznet.ca ◄── krb5 / pam_krb5 on every machine +``` + +`ldap` is the root of the tree. If it is down, nobody can log in on any +machine except as root or from cached sssd credentials, and the KDC stops +working too. Plan any LDAP work with that in mind. + +## Build-time environment variables + +`base-system.scm` reads some values with `getenv` when the system is +**evaluated**, not at boot. They must be set in the environment of +`guix system build|reconfigure|image`: + +| Variable | Effect | If unset | +|---------------------|--------|----------| +| `GUIX_ROOT_PW` | root password (crypted with a fixed salt) | root login disabled (`!`) | +| `LDAP_AUTHTOK` | sssd bind password and ssh pubkey-command bind password | sssd binds with no password and LDAP user lookups fail | +| `LDAP_BIND_DN` | sssd bind DN | `uid=guix,ou=system,ou=accounts,dc=metznet,dc=ca` | +| `LDAP_AUTHTOK_TYPE` | sssd authtok type | `password` | + +> ⚠️ These values end up in the world-readable `/gnu/store` (in `sssd.conf` +> and the pubkey-command script). This is a known problem and is planned to +> move to `metznet/services/secrets.scm`. + +`scripts/qemu.sh` and `docker-compose.yml` also export `DOMAIN_CAPS`/`DOMAIN_NAME`. + +## Out-of-band state (not in this repo) + +Guix rebuilds everything under `/gnu/store` and `/etc`, but these paths +hold state that must be provisioned or migrated by hand: + +| Path / thing | Machines | What it is | +|---|---|---| +| `/var/lib/openvpn/{ca.crt,client.crt,client.key,ta.key}` | all | VPN client credentials | +| `/etc/letsencrypt/` | `ldap`, `kerberos` | certbot certificates (re-issuable) | +| `/var/lib/slapd/{cert,chain,privkey}.pem` | `ldap` | the copies of the certbot certificate slapd actually serves (`olcTLS*`), owned by `slapd`. The certbot deploy hook refreshes them and restarts slapd after each renewal | +| `/var/lib/slapd/slapd.d/` | `ldap` | slapd `cn=config` (OLC) database: schema, ACLs, TLS paths, root DN | +| `/var/lib/slapd/data/` | `ldap` | MDB data directory (the actual directory tree) | +| `/var/lib/slapd/.last_slapadd_restore` | `ldap` | timestamp of the last seed restore (see below) | +| EC2 key pair | `ldap`, `kerberos`, `vpn` | the launch key pair `aws-pubkey` installs for the `aws` account; managed in AWS | +| `/var/lib/openvpn/{dh2048.pem,ipp.txt}` + server cert/key | `vpn` | OpenVPN server state | +| `/var/lib/kerberos/stash` | `kerberos` | KDC master key stash. **Without it the KDC cannot decrypt principals.** | +| TPM persistent handle `0x81010001` | `kerberos` | sealed age key for `secrets/secrets.enc.yaml` | + +## Day-to-day management + +### Test locally + +On macOS, use Docker (see `docker/README.md`): + +```sh +scripts/guix-docker.sh image metznet/machines/ldap.scm +scripts/guix-docker.sh load-run metznet/machines/ldap.scm +``` + +On Linux with KVM, use a VM on a tap bridge: + +```sh +sudo scripts/network.sh start_bridge +sudo scripts/network.sh add_tap tap0 +scripts/qemu.sh start metznet/machines/ldap.scm tap0 52:54:00:12:34:56 +``` + +### Deploy a change to a real host + +On the host, from a checkout of this repo: + +```sh +git pull +guix pull -C channels.scm # only when channels.d/ changed or to update Guix +export LDAP_AUTHTOK=... GUIX_ROOT_PW=... # see table above +sudo -E guix system reconfigure -L . metznet/machines/.scm +``` + +`-L .` builds from the working tree, so you can deploy uncommitted edits +without pushing them through the channel first. Check the result with +`sudo herd status` (and `herd status slapd` / `herd status kdc`). + +### Roll back + +```sh +sudo guix system roll-back # or pick an older generation in GRUB +guix system list-generations +``` + +A roll-back only reverts the system generation. It does not touch the +out-of-band state listed above. + +### Logs + +Services log via shepherd and syslog: `/var/log/messages` and +`/var/log/shepherd.log`. slapd runs in the foreground with `-d `, +so its output goes to shepherd. The KDC logs to `SYSLOG:DEBUG:DAEMON`. + +--- + +## Machines + +### `base-server.scm` — `base-server.metznet.ca` + +The bare server base with only a host name. It runs no services beyond +`%metznet-server-services`. Use it to bring up and smoke-test new hardware +(disk labels, EFI boot, sssd/krb5/VPN client), or copy it as the template +for a new server. + +### `ldap.scm` — `ldap.metznet.ca` + +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: + +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). + +Tree layout that other machines rely on: + +- `dc=metznet,dc=ca`: search base for sssd (rfc2307bis, `posixGroupAux`, `member`) +- `ou=system,ou=accounts,dc=metznet,dc=ca`: service bind accounts + (`uid=guix`, `uid=kerberos`) +- `cn=kerberos,dc=metznet,dc=ca`: Kerberos realm container + +#### Seeding (`backups` / `backup-date`) + +`slapd-configuration` takes an ordered list of LDIF files in `backups`. +Entry *i* is `slapadd`-ed into database number *i*. So the list is +`(config.ldif data.ldif)`: `-n 0` is `cn=config`, `-n 1` is the first data +DB. The seeds are only applied when +`/var/lib/slapd/.last_slapadd_restore` is missing **or** older than +`backup-date`. After a successful restore it writes `backup-date` into that +file. + +Caveats: + +- `backup-date` defaults to `(current-date)` **at evaluation time**. If you + leave seeds configured without pinning `backup-date`, every reconfigure + looks "newer" and slapd tries to `slapadd` again. That fails because the + entries already exist, and activation errors out. Always set + `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. + +--- + +### Runbook: migrate LDAP to a new instance + +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 +`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. + +**2. 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 1 -l /root/data.ldif +sudo herd start slapd # keep serving until cutover, but treat it as read-only from here +``` + +`slapcat` and `slapadd` are in the system profile via `openldap-slapd` in +`%metznet-base-packages`. Check which database numbers exist first +(`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`. + +```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 -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 +``` + +**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 +new host can only complete the ACME challenge once `ldap.metznet.ca` +resolves to it, so renewal there starts working after cutover. After each +renewal, certbot's deploy hook (`certificate-copy-deploy-hook` in +`metznet/services/certificates.scm`) copies the new files over and restarts +slapd. To copy the current certificate by hand, run the hook itself: +`sudo $(guix gc -R /run/current-system | grep 'ldap.metznet.ca-certificate-copy-hook$')`. +The KDC validates the cert (`SSL_CERT_DIR=/etc/ssl/certs`), so +a self-signed or expired cert will break Kerberos even though sssd +(`ldap_tls_reqcert never`) keeps working. + +**6. Start and verify before cutover.** + +```sh +sudo herd start slapd && sudo herd status slapd +ldapsearch -H ldaps:// -x -D uid=guix,ou=system,ou=accounts,dc=metznet,dc=ca -W \ + -b dc=metznet,dc=ca '(uid=)' +ldapsearch ... -b cn=kerberos,dc=metznet,dc=ca -s one dn # realm container is present +``` + +Compare entry counts against the old host (`grep -c '^dn:' data.ldif`), +and check the schema and the entries other machines depend on: + +```sh +docker compose run --rm -T guix bash -lc tests/ldap/generated-config.sh # builds build/*.ldif +LDAPTLS_REQCERT=never scripts/ldap-verify.sh -H ldaps:// -D -y pwfile \ + -b dc=metznet,dc=ca -X -e build/ldap-structure.ldif +LDAPTLS_REQCERT=never scripts/ldap-verify.sh -H ldaps:// -D -y pwfile \ + -b cn=config -X -e build/slapd-config.ldif +``` + +The first checks the directory structure (`%ldap-structure` in `ldap.scm`: +containers, realm, policy, groups and the accounts this repo binds as) +exactly while ignoring data; the second, the config (`%ldap-slapd-config`). +Run them against the old host first so you know what a passing run looks +like. + +**7. Cut over.** Repoint `ldap.metznet.ca` DNS at the new host, then stop +slapd on the old host so nothing can write to it. Clients resolve by name, +so no reconfigure is needed. sssd will reconnect, and the KDC reconnects +(`ldap_conns_per_server` = 5) or can be kicked with +`sudo herd restart kdc kadmind` on `kerberos.metznet.ca`. + +**8. Verify from clients.** + +```sh +getent passwd # sssd (may need: sudo sss_cache -E) +kinit # KDC → LDAP backend +ssh @ # AuthorizedKeysCommand → LDAP +``` + +**9. Decommission** the old host after a soak period. Keep `config.ldif`/ +`data.ldif` somewhere safe and encrypted. They are a complete backup. + +**Rollback:** until step 7, nothing has changed for clients. After step 7, +point DNS back and `herd start slapd` on the old host. Any writes made on +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: + +1. Edit `%ldap-slapd-config`. +2. Write the same change as a numbered LDIF modify in `ldap/changes/`, to + apply to the running server. + +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: + +```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' +``` + +Then apply it with slapd stopped. `slapmodify` needs OpenLDAP 2.5 or later: + +```sh +# on ldap.metznet.ca, as root +cp -a /var/lib/slapd/slapd.d /root/slapd.d.$(date +%F) # backup +herd stop slapd +sudo -u slapd slapmodify -F /var/lib/slapd/slapd.d -n 0 -l ldap/changes/NNNN-name.ldif +sudo -u slapd slaptest -F /var/lib/slapd/slapd.d -u +herd start slapd +``` + +Then check the live server matches the generated config exactly +(`generated-config.sh` writes `build/slapd-config.ldif`): + +```sh +scripts/ldap-verify.sh -H ldaps://ldap.metznet.ca \ + -D uid=admin,ou=sudoers,ou=accounts,dc=metznet,dc=ca -y pwfile \ + -b cn=config -X -e build/slapd-config.ldif +``` + +Schema definitions aren't compared textually (the live server stores them +reformatted); the schema check against `cn=Subschema` covers what's used. + +If something goes wrong, stop slapd and restore the copy of `slapd.d`. + +#### Changing the directory structure + +Containers, groups (and their gidNumbers), the realm, the policy and the +accounts this repo binds as are `%ldap-structure` in `ldap.scm`; everything +under a `(children-data? #t)` container is data and is managed live as +usual. To add or change a structure entry, edit `%ldap-structure`, make the +same change on the server with `ldapadd`/`ldapmodify` as +`uid=admin,ou=sudoers,ou=accounts,dc=metznet,dc=ca`, then rebuild and check: + +```sh +docker compose run --rm -T guix bash -lc tests/ldap/generated-config.sh +scripts/ldap-verify.sh -H ldaps://ldap.metznet.ca \ + -D uid=admin,ou=sudoers,ou=accounts,dc=metznet,dc=ca -y pwfile \ + -b dc=metznet,dc=ca -X -e build/ldap-structure.ldif +``` + +An account's primary gid comes from the group record it names, and every +DN from its parent, so those can't drift. Passwords are never part of the +structure. + +#### Plain backups + +Take backups the same way as step 2 (`slapcat -n 0` and `-n 1`). Restore +them the same way as step 4. + +--- + +### `kerberos.scm` — `kerberos.metznet.ca` + +MIT Kerberos KDC and kadmind (`mit-krb5-ldap`) for realm `METZNET.CA`, plus +certbot and `secrets-service-type`. + +- The **database lives in LDAP** (`ldap_kerberos_container_dn = + cn=kerberos,dc=metznet,dc=ca`) and is reached over `ldaps://ldap.metznet.ca`. + It binds as `uid=kerberos,ou=system,ou=accounts,dc=metznet,dc=ca` for both + KDC and kadmind operations. +- That DN's password is the **`ldap_service_password_file`** + `/run/secrets/kerberos-ldap-bind-password`. At activation, + `metznet/services/secrets.scm` decrypts it from `secrets/secrets.enc.yaml` + using an age key unsealed from TPM handle `0x81010001`, with PCR policy + `sha256:7`. The file is owned by `kerberos:kerberos` with mode `0400`. +- `kadm5.acl` grants `*/admin@METZNET.CA` full rights. +- New principals default to `+preauth`. +- `/etc/kdc.conf` is generated. The KDC and kadmind run with + `KRB5_KDC_PROFILE=/etc/kdc.conf`. + +> ⚠️ The committed `secrets/secrets.enc.yaml` and `.sops.yaml` recipient +> are a **test fixture**. They hold a placeholder password encrypted to a key +> that is not in any real TPM. Do the provisioning below before relying on +> it. + +#### Provision secrets on real hardware + +```sh +# on kerberos.metznet.ca +guix shell sops age -- scripts/seal-age-key.sh 0x81010001 # optional 2nd arg: PCR selection +``` + +`seal-age-key.sh` needs `tpm2-tools` 5.8 or later (`metznet-tpm2-tools`). +Guix's own 5.7 fails against OpenSSL 3. + +1. Put the printed `age1…` recipient in `.sops.yaml`, replacing the + fixture key. +2. Run `sops updatekeys secrets/secrets.enc.yaml`, then + `sops secrets/secrets.enc.yaml` and set `kerberos-ldap-bind-password` to + the real password of `uid=kerberos,…`. Updating keys needs a key that can + currently decrypt the file. For the fixture, it may be simpler to recreate + the file from scratch. +3. If you chose a different handle or PCRs, update + `%kerberos-secrets-tpm-handle` / `pcr-policy`. The `sha256:7` default + has not yet been checked against this machine's measured boot. Firmware + or Secure Boot changes alter PCR 7 and make unsealing fail, so you will + need to re-seal after them. +4. Reconfigure, then check `/run/secrets/kerberos-ldap-bind-password` exists. + +#### First-time KDC setup + +There is no automatic stash yet (see the TODO in `metznet/services/kdc.scm`). +On a brand-new realm, create the realm in LDAP with +`kdb5_ldap_util ... create` and then run: + +```sh +sudo env KRB5_KDC_PROFILE=/etc/kdc.conf kdb5_util stash +``` + +For an existing realm, run only the stash step. You will be prompted for the +master password. + +#### Migrate the KDC to a new instance + +Principals live in LDAP, so the KDC host itself is mostly stateless. + +1. Bring up the new host with `metznet/machines/kerberos.scm`. +2. Copy `/var/lib/kerberos/stash` (mode `0600`, root-only), or re-create it + with `kdb5_util stash` and the master password. +3. **New hardware means a new TPM.** Run `seal-age-key.sh` there, add the + new recipient to `.sops.yaml` (keep the old one until cutover is done), + run `sops updatekeys`, and update the handle if it changed. +4. Reconfigure, then check that `herd status kdc kadmind` shows both + running and `/run/secrets/kerberos-ldap-bind-password` is present. +5. Repoint `kerberos.metznet.ca` DNS. Clients find the KDC through + `krb5.conf` by name. Test with `kinit` and `kadmin -p /admin`. +6. Remove the old host's recipient from `.sops.yaml` and run + `sops updatekeys` again. + +--- + +### `vpn.scm` — `vpn.metznet.ca` + +OpenVPN server on `10.0.80.0/24`, plus certbot for `vpn.metznet.ca`. It drops +the OpenVPN client every other machine runs. The file was restored from +commit `425b173^`, where system configs moved to a separate repo, so check it +against the live host before the first reconfigure from here. Its key and +certificates live in `/var/lib/openvpn/` (it reuses the `client.crt`/`client.key` +names). + +--- + +### `otto.scm` — `otto` + +Noah's desktop/laptop, built on `%metznet-base-desktop-system`. + +- **Disks:** uses fixed UUIDs (EFI `6E88-FE62`, root + `ba93a043-…`), not the server labels. Update these if you reinstall. +- **Users:** local `nmetz` (zsh; groups `wheel netdev audio video usb + kvm`) in addition to LDAP users via sssd. +- **Hardware/desktop:** `nouveau` is blacklisted. The NVIDIA driver setup is + commented out. CUPS has its web UI on, SANE is enabled, and the USB udev + rule is applied. +- **Hosts:** a static `/etc/hosts` entry for `geofff.homelab` + (`192.168.86.2`). +- **Packages:** qemu, texlive-scheme-full, gcc-toolchain, vulkan, etc. `firefox` and the ARM + embedded toolchain are currently left out because they are impractical to build (see + `docker/README.md` → Known gotchas). +- Requires the **nonguix** channel (`channels.d/nonguix.scm`, declared in + `.guix-channel`). Its substitute key is authorized by the desktop base. + +Reconfigure with +`sudo -E guix system reconfigure -L . metznet/machines/otto.scm`. + +--- + +## Adding a new machine + +1. Copy `base-server.scm` to `.scm`. Rename the module to + `(metznet machines )`, because it must match the path or + `guix pull` fails to load the channel. Set the host name, and make + the file's last expression the `operating-system` value. +2. Add services in front of `%metznet-server-services`, following the + `-services` + `append` pattern used in `ldap.scm`/`kerberos.scm`. +3. If it needs secrets: seal a key on the machine, add its recipient to + `.sops.yaml`, run `sops updatekeys`, and add a `secrets-service-type` + like `kerberos.scm` does. +4. Test with `scripts/guix-docker.sh image metznet/machines/.scm`. +5. On the hardware, label partitions `guix-data`/`guix-boot` (or override + `file-systems` like `otto.scm` does), install, provision the + out-of-band state, and reconfigure. diff --git a/metznet/machines/kerberos.scm b/metznet/machines/kerberos.scm index 45210ca..009ba8b 100644 --- a/metznet/machines/kerberos.scm +++ b/metznet/machines/kerberos.scm @@ -3,6 +3,7 @@ #:use-module (gnu services certbot) #:use-module (metznet system base-system) #:use-module (metznet services kdc) + #:use-module (metznet services secrets) #:use-module (metznet packages kdc) #:export (kerberos.metznet.ca kerberos-services)) @@ -14,6 +15,14 @@ (define %kerberos-dn "uid=kerberos,ou=system,ou=accounts,dc=metznet,dc=ca") +;; See scripts/seal-age-key.sh: sealed into this handle on kerberos.metznet.ca +;; itself, PCR policy is seal-age-key.sh's default (sha256:7). +(define %kerberos-secrets-tpm-handle + "0x81010001") + +(define %kerberos-ldap-bind-password-file + "/run/secrets/kerberos-ldap-bind-password") + (define-public kerberos-services (append (list (service kdc-service-type (kdc-configuration @@ -25,23 +34,36 @@ (ldap_kadmind_dn %kerberos-dn) (ldap_servers "ldaps://ldap.metznet.ca") (ldap_service_password_file - "/var/lib/kerberos/service.keyfile"))))) + %kerberos-ldap-bind-password-file))))) (realms (list (kdc-realm-configuration (name "METZNET.CA") (database_module "openldap_ldapconf") (default_principal_flags "+preauth") (acl_file (plain-file "kadm5.acl" "*/admin@METZNET.CA *\n"))))))) + (service secrets-service-type + (secrets-configuration + (sops-file (local-file + (string-append (dirname (current-filename)) + "/../../secrets/secrets.enc.yaml") + "secrets.enc.yaml")) + (tpm-handle %kerberos-secrets-tpm-handle) + (secrets (list (secret + (name 'kerberos-ldap-bind-password) + (sops-key "kerberos-ldap-bind-password") + (destination %kerberos-ldap-bind-password-file) + (owner "kerberos") + (group "kerberos")))))) (service certbot-service-type (certbot-configuration (email "admin@metznet.ca") (certificates (list (certificate-configuration (domains '("kerberos.metznet.ca")))))))) - %metznet-server-services)) + %metznet-aws-server-services)) (define-public kerberos.metznet.ca (operating-system - (inherit %metznet-base-server-system) + (inherit %metznet-base-aws-system) (host-name "kerberos.metznet.ca") (services kerberos-services))) diff --git a/metznet/machines/ldap.scm b/metznet/machines/ldap.scm index dd42153..9e7a5b7 100644 --- a/metznet/machines/ldap.scm +++ b/metznet/machines/ldap.scm @@ -2,8 +2,14 @@ #:use-module (gnu) #:use-module (gnu services certbot) #:use-module (metznet system base-system) + #:use-module (metznet services certificates) #:use-module (metznet services slapd) - #:export (ldap.metznet.ca ldap-services)) + #:use-module (metznet services slapd-config) + #:use-module (metznet services ldap-structure) + #:use-module (metznet packages slapd) + #:export (ldap.metznet.ca ldap-services + %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). @@ -18,14 +24,281 @@ (certbot-configuration (email "admin@metznet.ca") (certificates (list (certificate-configuration - (domains '("ldap.metznet.ca"))))))) + (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-server-services)) + %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`. +(define %suffix "dc=metznet,dc=ca") + +(define (system-account uid) + (string-append "dn.exact=\"uid=" uid ",ou=system,ou=accounts," %suffix "\"")) + +(define %root-peercred + "dn.exact=gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth") + +(define %admin-dn + (string-append "uid=admin,ou=sudoers,ou=accounts," %suffix)) + +(define-public %ldap-slapd-config + (slapd-config + (log-level '("stats")) + ;; copies of the certbot certificate, refreshed on renewal by the + ;; certbot deploy hook in ldap-services + (tls-ca-certificate-file "/var/lib/slapd/chain.pem") + (tls-certificate-file "/var/lib/slapd/cert.pem") + (tls-certificate-key-file "/var/lib/slapd/privkey.pem") + (tool-threads 1) + ;; slapd-activation copies pw-totp.so here + (module-path "/var/lib/slapd/modules") + (modules '("back_mdb" "memberof" "ppolicy" "refint" "pw-totp")) + (schemas + (list (slapd-schema + (name "core") + (file (file-append openldap-slapd "/etc/openldap/schema/core.ldif")) + ;; as deployed: groups may be empty (stock core requires a member) + (patches + '(("NAME 'groupOfNames' DESC 'RFC2256: a group of names (DNs)' SUP top STRUCTURAL MUST ( member $ cn ) MAY ( " + . "NAME 'groupOfNames' DESC 'RFC2256: a group of names (DNs)' SUP top STRUCTURAL MUST ( cn ) MAY ( member $ ")))) + (slapd-schema + (name "cosine") + (file (file-append openldap-slapd "/etc/openldap/schema/cosine.ldif"))) + (slapd-schema + (name "nis") + (file (file-append openldap-slapd "/etc/openldap/schema/nis.ldif"))) + (slapd-schema + (name "inetorgperson") + (file (file-append openldap-slapd "/etc/openldap/schema/inetorgperson.ldif"))) + (slapd-schema + (name "openssh-lpk-openldap") + (file (local-file "../../ldap/schema/openssh-lpk-openldap.ldif"))) + (slapd-schema + (name "groupaux") + (file (local-file "../../ldap/schema/groupaux.ldif"))) + (slapd-schema + (name "kerberos") + (file (local-file "../../ldap/schema/kerberos.ldif"))))) + (frontend + (slapd-frontend + (access (list (slapd-access "*" (string-append %root-peercred " manage") "* break") + (slapd-access "dn.exact=\"\"" "* read") + (slapd-access "dn.base=\"cn=Subschema\"" "* read"))) + (requires '("authc")) + (size-limit 500) + (password-hash "{SSHA}"))) + (config-database + (slapd-config-database + (root-dn "gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth") + (access (list (slapd-access "*" (string-append %root-peercred " manage") "* break") + ;; ldap/changes/0001: the mdb root DN may read cn=config + (slapd-access "*" (string-append "dn.exact=\"" %admin-dn "\" read") "* none"))) + ;; no-op on the config database; kept to match the live server + (overlays (list (slapd-memberof-overlay))))) + (databases + (list + (slapd-mdb + (suffix %suffix) + (directory "/var/lib/slapd/data") + (root-dn %admin-dn) + (access + (list + ;; ldap/changes/0002: userPassword is auth-only for system accounts + (slapd-access "attrs=userPassword" + "self write" + (string-append (system-account "daemon") " write") + "anonymous auth" + "* none") + (slapd-access "attrs=sshPublicKey" + "self write" + (string-append (system-account "daemon") " write") + (string-append "dn.children=\"ou=system,ou=accounts," %suffix "\" read") + "* none") + (slapd-access "attrs=shadowLastChange" "self write" "* read") + (slapd-access "attrs=krbPrincipalKey" + "anonymous auth" + (string-append (system-account "kerberos") " write") + "self write" + "* none") + (slapd-access (string-append "dn.subtree=\"ou=groups," %suffix "\"") + "* read break") + (slapd-access (string-append "dn.subtree=\"cn=kerberos," %suffix "\"") + (string-append (system-account "kerberos") " write") + "* none break") + (slapd-access (string-append "dn.subtree=\"ou=accounts," %suffix "\"") + (string-append (system-account "kerberos") " write") + (string-append (system-account "daemon") " write") + "* none break") + (slapd-access (string-append "dn.subtree=\"ou=services," %suffix "\"") + (string-append (system-account "kerberos") " write") + (string-append (system-account "daemon") " write") + "* none break") + (slapd-access (string-append "attrs=krbPrincipalName,krbLastPwdChange," + "krbLastFailedAuth,krbLoginFailedCount," + "krbLastSuccessfulAuth,krbExtraData," + "krbTicketFlags,krbPasswordExpiration") + "self read" + (string-append (system-account "kerberos") " write") + (string-append (system-account "daemon") " write") + "* none") + (slapd-access "*" "* read"))) + (requires '("authc")) + (checkpoint "512 30") + ;; what sssd, the KDC and metznet-pubkey search on + (indexes '("objectClass eq" + "cn,uid eq" + "uidNumber,gidNumber eq" + "member,memberUid eq" + "krbPrincipalName eq,pres,sub")) + (max-size 1073741824) + (overlays + (list (slapd-memberof-overlay) + (slapd-totp-overlay) + (slapd-ppolicy-overlay) + ;; ldap/changes/0003: refint on the data database + (slapd-refint-overlay + (attributes '("memberof" "member" "manager" "owner")))))))))) + +(define-public %ldap-slapd-config-ldif + (slapd-config->ldif %ldap-slapd-config)) + +;; ldap.metznet.ca's directory structure: containers, the realm, the +;; password policy, groups (so gidNumbers are fixed here) and the accounts this +;; repo binds as or names. Everything under a (children-data? #t) container is +;; data, as are passwords and group membership. tests/ldap/generated-config.sh +;; builds build/ldap-structure.ldif; check the live server with +;; `ldap-verify.sh -b dc=metznet,dc=ca -X -e build/ldap-structure.ldif`. +(define %root + (ldap-entry + (rdn %suffix) + (attributes '(("objectClass" "top" "dcObject" "organization") + ("o" "metznet") + ("dc" "metznet"))))) + +;; KDC ldap_kerberos_container_dn (metznet/machines/kerberos.scm) +(define %kerberos-container + (ldap-entry + (rdn "cn=kerberos") + (parent %root) + (attributes '(("cn" "kerberos") + ("objectClass" "krbContainer"))))) + +;; krbSubTrees: principals live anywhere under dc=metznet,dc=ca (e.g. on +;; uid=admin and uid=guix); the ones under the realm itself are data +(define %realm + (ldap-entry + (rdn "cn=METZNET.CA") + (parent %kerberos-container) + (attributes '(("cn" "METZNET.CA") + ("objectClass" "top" "krbRealmContainer" "krbTicketPolicyAux") + ("krbSubTrees" "dc=metznet,dc=ca"))) + (children-data? #t))) + +(define %accounts (organizational-unit "accounts" %root)) +(define %sudoers (organizational-unit "sudoers" %accounts #:also '("accounts") + #:children-data? #t)) +(define %system (organizational-unit "system" %accounts #:children-data? #t)) +(define %users (organizational-unit "users" %accounts #:children-data? #t)) +(define %groups (organizational-unit "groups" %root)) +(define %policies (organizational-unit "policies" %root)) + +(define %admin-group (ldap-posix-group (name "admin") (gid 5000) (parent %groups))) +(define %users-group (ldap-posix-group (name "users") (gid 5001) (parent %groups))) +;; machine/service accounts' primary group +(define %system-group (ldap-posix-group (name "system") (gid 5002) (parent %groups))) + +(define-public %ldap-structure + (list + %root + %kerberos-container + %realm + %accounts + %sudoers + ;; olcRootDN of olcDatabase={1}mdb; root/admin@METZNET.CA matches + ;; kadm5.acl's */admin@METZNET.CA in metznet/machines/kerberos.scm + (ldap-account + (uid "admin") + (parent %sudoers) + (object-classes '("inetOrgPerson" "posixAccount" "shadowAccount")) + (uid-number 10000) + (group %admin-group) + (gecos "Administrator") + (shell "/bin/zsh") + (home "/home/admin") + (principal "root/admin@METZNET.CA") + (attributes '(("sn" "admin")))) + %system + ;; write access in olcAccess on olcDatabase={1}mdb (%ldap-slapd-config) + (ldap-account + (uid "daemon") + (parent %system) + (uid-number 11003) + (group %system-group) + (gecos "daemon") + (shell "/bin/zsh") + (home "/var/lib/daemon")) + ;; sssd ldap_default_bind_dn and the ssh pubkey-command bind DN + ;; (metznet/system/base-system.scm, LDAP_AUTHTOK) + (ldap-account + (uid "guix") + (parent %system) + (uid-number 11019) + (group %system-group) + (gecos "guix") + (shell "/bin/nologin") + (home "/var/lib/guix") + (principal "guix@METZNET.CA")) + ;; KDC ldap_kdc_dn / ldap_kadmind_dn (metznet/machines/kerberos.scm). Same + ;; name as the local "kerberos" account kdc-accounts creates; nsswitch puts + ;; sss before files, so this one wins while sssd is up + (ldap-account + (uid "kerberos") + (parent %system) + (uid-number 11005) + (group %system-group) + (gecos "kerberos") + (shell "/bin/zsh") + (home "/var/lib/kerberos")) + %users + %groups + %admin-group + %users-group + %system-group + (organizational-unit "machines" %root #:children-data? #t) + %policies + ;; Sets no limits (no lockout, length or expiry) and isn't + ;; olcPPolicyDefault, so it only applies to entries naming it in + ;; pwdPolicySubentry + (ldap-entry + (rdn "cn=default_password") + (parent %policies) + (attributes '(("objectClass" "top" "device" "pwdPolicy") + ("cn" "default_password") + ("pwdAttribute" "userPassword") + ("pwdAllowUserChange" "TRUE")))) + (organizational-unit "services" %root #:children-data? #t))) + +(define-public %ldap-structure-ldif + (ldap-structure->ldif + "ldap-structure.ldif" %ldap-structure + #:comment "# Generated from %ldap-structure in metznet/machines/ldap.scm.\n" + #:ignore-attributes '("userPassword" "krbPrincipalKey" + "krbLastSuccessfulAuth" "krbLastFailedAuth" + "krbLoginFailedCount" "krbLastPwdChange" + "krbExtraData" "member" "memberUid" "memberOf"))) (define-public ldap.metznet.ca (operating-system - (inherit %metznet-base-server-system) + (inherit %metznet-base-aws-system) (host-name "ldap.metznet.ca") (services ldap-services))) diff --git a/metznet/machines/vpn.scm b/metznet/machines/vpn.scm new file mode 100644 index 0000000..101887c --- /dev/null +++ b/metznet/machines/vpn.scm @@ -0,0 +1,46 @@ +(define-module (metznet machines vpn) + #:use-module (guix gexp) + #:use-module (guix modules) + #:use-module (gnu packages tls) + #:use-module (gnu system) + #:use-module (gnu services) + #:use-module (gnu services certbot) + #:use-module (gnu services vpn) + #:use-module (metznet system base-system) + + #:export (vpn.metznet.ca vpn-services metznet-vpn-service-type)) + +;; Restored from metznet-channel commit 425b173^ (deleted there when system +;; configs were split into a separate repo), moved onto the AWS base. +(define-public vpn-services + (append (list (service openvpn-server-service-type + (openvpn-server-configuration (ca + "/var/lib/openvpn/ca.crt") + (cert + "/var/lib/openvpn/client.crt") + (key + "/var/lib/openvpn/client.key") + (tls-auth + "/var/lib/openvpn/ta.key") + (dh + "/var/lib/openvpn/dh2048.pem") + (ifconfig-pool-persist + "/var/lib/openvpn/ipp.txt") + (server + "10.0.80.0 255.255.255.0"))) + (service certbot-service-type + (certbot-configuration (email "admin@metznet.ca") + (certificates (list (certificate-configuration + (domains ' + ("vpn.metznet.ca")))))))) + (modify-services %metznet-aws-server-services + (delete openvpn-client-service-type)))) + +(define-public vpn.metznet.ca + (operating-system + (inherit %metznet-base-aws-system) + (host-name "vpn.metznet.ca") + (services + vpn-services))) + +vpn.metznet.ca diff --git a/metznet/packages/slapd.scm b/metznet/packages/slapd.scm index 2948250..f8eba4c 100644 --- a/metznet/packages/slapd.scm +++ b/metznet/packages/slapd.scm @@ -18,7 +18,7 @@ (define-public openldap-slapd (package (name "openldap-slapd") - (version "2.6.4") + (version "2.6.13") (source (origin (method url-fetch) ;; See for a list of @@ -34,7 +34,7 @@ "openldap-release/openldap-" version ".tgz"))) (sha256 (base32 - "1489li52sjxm1f97v927jxaxzfk6v9sa32ixrw30qhvq07jh85ym")))) + "0mmnziwblgzg646nrd6i53a56snimq8a6r53l62znbm42yav94yn")))) (build-system gnu-build-system) (inputs (list bdb-5.3 cyrus-sasl @@ -59,19 +59,6 @@ "--enable-overlays" "--enable-shared" "--with-cyrus-sasl" - ;; OpenLDAP's ~25-year-old C predates - ;; several conformance rules GCC 14 - ;; made hard errors by default: - ;; implicit-function-declaration, - ;; implicit-int, int-conversion, and - ;; incompatible-pointer-types (e.g. the - ;; configure-time regex/pthread_detach - ;; checks, and valsort.c's `void **` - ;; assignment). Explicitly downgrading - ;; all four restores pre-GCC-14 - ;; behavior across both configure and - ;; the actual build. - "CFLAGS=-std=gnu17 -Wno-implicit-function-declaration -Wno-implicit-int -Wno-int-conversion -Wno-incompatible-pointer-types" #$@(if (%current-target-system) '("--with-yielding_select=yes" "ac_cv_func_memcmp_working=yes") diff --git a/metznet/packages/tpm2.scm b/metznet/packages/tpm2.scm new file mode 100644 index 0000000..b69f38e --- /dev/null +++ b/metznet/packages/tpm2.scm @@ -0,0 +1,83 @@ +(define-module (metznet packages tpm2) + #:use-module ((guix licenses) #:prefix license:) + #:use-module (guix packages) + #:use-module (guix download) + #:use-module (guix build-system gnu) + #:use-module (gnu packages autotools) + #:use-module (gnu packages curl) + #:use-module (gnu packages gettext) + #:use-module (gnu packages haskell-xyz) + #:use-module (gnu packages linux) + #:use-module (gnu packages pkg-config) + #:use-module (gnu packages textutils) + #:use-module (gnu packages tls) + #:use-module (gnu packages web) + #:export (metznet-tpm2-tss + metznet-tpm2-tools)) + +;; Guix's own tpm2-tss (3.0.3, released 2020-11-25) predates upstream's +;; OpenSSL 3.0 compatibility fix (EVP_PKEY_new_raw_private_key instead of +;; the now-broken EVP_PKEY_new_mac_key, landed upstream 2021-07/08) -- +;; every hierarchy/HMAC-authenticated command (tpm2_createprimary and +;; friends) fails against Guix's current default openssl (3.5.x) with +;; "Esys_CreatePrimary(0x70001) ... EVP_PKEY_new_mac_key". Rather than +;; pin an EOL openssl as a workaround, track a current upstream release +;; that already has the real fix (same approach as openldap-slapd/ +;; mit-krb5-ldap elsewhere in this channel: maintain our own version when +;; upstream Guix's is unsuitable). +(define-public metznet-tpm2-tss + (package + (name "tpm2-tss") + (version "4.2.0") + (source + (origin + (method url-fetch) + (uri (string-append "https://github.com/tpm2-software/tpm2-tss" + "/releases/download/" version "/tpm2-tss-" version + ".tar.gz")) + (sha256 + (base32 "0951zsqss3ndx904dg69h2imqwlgd2lhqh8sf02pzqaciif0qgxm")))) + (build-system gnu-build-system) + (native-inputs + (list pkg-config)) + (inputs + (list curl json-c openssl `(,util-linux "lib"))) + (home-page "https://tpm2-software.github.io/") + (synopsis "OSS Implementation of the TCG TPM2 Software Stack (TSS2)") + (description + "This package provides the @acronym{TCG, Trusted Computing Group} +@acronym{TSS2, TPM2 Software Stack}. The stack contains libtss2-fapi, +libtss2-esys, libtss2-sys, libtss2-mu, libtss2-tcti-device, libtss2-tcti-swtpm +and libtss2-tcti-mssim.") + (license license:bsd-2))) + +(define-public metznet-tpm2-tools + (package + (name "tpm2-tools") + (version "5.8") + (source + (origin + (method url-fetch) + (uri (string-append "https://github.com/tpm2-software/tpm2-tools/" + "releases/download/" version "/" + "tpm2-tools-" version ".tar.gz")) + (sha256 + (base32 "0wzg9gwfv4pxl5b42pvp92phyr1dchi0nbbwbkhv8578ra2k3dqw")))) + (build-system gnu-build-system) + (native-inputs + (list autoconf + automake + curl + libtool + gettext-minimal + openssl + pandoc + pkg-config + metznet-tpm2-tss)) + (home-page "https://github.com/tpm2-software/tpm2-tools") + (synopsis "Tools for the Trusted Platform Module (TPM 2.0)") + (description + "This package provides user tools for the Trusted Computing Group's (TCG) +TPM2 Software Stack (TSS). These programs help with common tasks such as key +management, attestation, encryption, and signing.") + (license license:bsd-3))) diff --git a/metznet/services/certificates.scm b/metznet/services/certificates.scm new file mode 100644 index 0000000..805988d --- /dev/null +++ b/metznet/services/certificates.scm @@ -0,0 +1,46 @@ +(define-module (metznet services certificates) + #:use-module (guix gexp) + #:export (certificate-copy-deploy-hook)) + +;;; Commentary: +;;; +;;; Daemons that drop privileges can't read certbot's files under +;;; /etc/letsencrypt (root-only), so they serve copies they own. This makes a +;;; certbot deploy hook that refreshes those copies after each renewal: +;;; +;;; (certificate-configuration +;;; (domains '("ldap.metznet.ca")) +;;; (deploy-hook (certificate-copy-deploy-hook "ldap.metznet.ca" +;;; "/var/lib/slapd" "slapd" #:restart '(slapd)))) +;;; +;;; The hook is a plain program, so running it by hand copies the current +;;; certificate too. +;;; +;;; Code: + +(define* (certificate-copy-deploy-hook domain directory user + #:key + (files '("cert.pem" "chain.pem" + "privkey.pem")) + (restart '())) + "Return a certbot deploy hook copying FILES of DOMAIN's live certificate +into DIRECTORY, owned by USER (and USER's primary group), then restarting the +Shepherd services in RESTART. privkey.pem is mode 0600, the rest 0644." + (program-file + (string-append domain "-certificate-copy-hook") + (with-imported-modules '((gnu services herd)) + #~(begin + (use-modules (gnu services herd)) + (let ((live #$(string-append "/etc/letsencrypt/live/" domain)) + (owner (getpw #$user))) + (for-each + (lambda (name) + ;; copy then rename, so a reader never sees a partial file + (let ((new (string-append #$directory "/" name ".new"))) + (copy-file (string-append live "/" name) new) + (chown new (passwd:uid owner) (passwd:gid owner)) + (chmod new (if (string=? name "privkey.pem") #o600 #o644)) + (rename-file new (string-append #$directory "/" name)))) + '#$files) + ;; most daemons only load their certificate at startup + (for-each restart-service '#$restart)))))) diff --git a/metznet/services/ldap-structure.scm b/metznet/services/ldap-structure.scm new file mode 100644 index 0000000..5893cf7 --- /dev/null +++ b/metznet/services/ldap-structure.scm @@ -0,0 +1,172 @@ +(define-module (metznet services ldap-structure) + #:use-module (guix gexp) + #:use-module (guix records) + #:use-module (ice-9 match) + #:use-module (ice-9 regex) + #:use-module (srfi srfi-1) + #:export (ldap-entry + ldap-entry? + organizational-unit + ldap-posix-group + ldap-posix-group? + ldap-posix-group-gid + ldap-account + ldap-account? + ldap-dn + ldap-structure->ldif)) + +;;; Commentary: +;;; +;;; A directory's *structure* -- containers and the few entries a repo +;;; depends on -- as Scheme records, serialized to LDIF. Everything else in +;;; the directory is data: containers marked (children-data? #t) say so for +;;; their subtrees, and the generated file carries that as the +;;; `# ldap-verify:` directives scripts/ldap-verify.sh -X reads, so +;;; +;;; scripts/ldap-verify.sh -b -X -e +;;; +;;; checks a live server's structure exactly while ignoring its data. +;;; Records have no password fields: passwords are always data. +;;; +;;; Code: + +;; Any entry. RDN is "attr=value"; PARENT is another record (or #f for the +;; suffix itself, whose RDN is then the whole DN). ATTRIBUTES is a list of +;; (NAME VALUE ...). CHILDREN-DATA? marks everything below it as data. +(define-record-type* ldap-entry make-ldap-entry + ldap-entry? + (rdn ldap-entry-rdn) + (parent ldap-entry-parent (default #f)) + (attributes ldap-entry-attributes (default '())) + (children-data? ldap-entry-children-data? (default #f))) + +;; groupOfNames + posixGroupAux, as groups are stored here. Membership is +;; data and isn't representable. +(define-record-type* ldap-posix-group make-ldap-posix-group + ldap-posix-group? + (name ldap-posix-group-name) + (gid ldap-posix-group-gid) + (parent ldap-posix-group-parent)) + +;; A POSIX account. GROUP is an ldap-posix-group, so its primary gid always +;; names a group that exists in the structure. +(define-record-type* ldap-account make-ldap-account + ldap-account? + (uid ldap-account-uid) + (parent ldap-account-parent) + (object-classes ldap-account-object-classes + (default '("account" "posixAccount" "shadowAccount"))) + (uid-number ldap-account-uid-number) + (group ldap-account-group) + (gecos ldap-account-gecos (default #f)) + (shell ldap-account-shell) + (home ldap-account-home) + ;; Kerberos principal on the entry itself (krbPrincipalAux) + (principal ldap-account-principal (default #f)) + (attributes ldap-account-attributes (default '()))) + +(define* (organizational-unit name parent #:key (also '()) children-data?) + "An organizationalUnit NAME under PARENT. ALSO lists extra ou values the +entry carries." + (ldap-entry + (rdn (string-append "ou=" name)) + (parent parent) + (attributes `(("objectClass" "organizationalUnit") + ("ou" ,@also ,name))) + (children-data? children-data?))) + +(define (->entry record) + "RECORD as a plain ldap-entry." + (match record + ((? ldap-entry?) record) + (($ name gid parent) + (ldap-entry + (rdn (string-append "cn=" name)) + (parent parent) + (attributes `(("objectClass" "groupOfNames" "posixGroupAux") + ("cn" ,name) + ("gidNumber" ,(number->string gid)))))) + (($ uid parent object-classes uid-number group gecos shell + home principal attributes) + (ldap-entry + (rdn (string-append "uid=" uid)) + (parent parent) + (attributes + (append + `(("objectClass" ,@object-classes + ,@(if principal '("krbPrincipalAux" "krbTicketPolicyAux") '())) + ("uid" ,uid) + ("uidNumber" ,(number->string uid-number)) + ("gidNumber" ,(number->string (ldap-posix-group-gid group))) + ("loginShell" ,shell) + ("homeDirectory" ,home)) + (if (assoc "cn" attributes) '() `(("cn" ,uid))) + (if gecos `(("gecos" ,gecos)) '()) + (if principal `(("krbPrincipalName" ,principal)) '()) + attributes)))))) + +(define (ldap-dn record) + (let ((entry (->entry record))) + (match (ldap-entry-parent entry) + (#f (ldap-entry-rdn entry)) + (parent (string-append (ldap-entry-rdn entry) "," (ldap-dn parent)))))) + +(define (ancestors record) + (match (ldap-entry-parent (->entry record)) + (#f '()) + (parent (cons parent (ancestors parent))))) + +(define (dn-regex dn) + ;; "]" first and "[" last so neither pairs up as "[." / "[:" / "[=" + (regexp-substitute/global #f "[]\\.^$*+?(){}|[]" dn 'pre "\\" 0 'post)) + +(define (directives records ignore-attributes) + (let* ((data-containers (filter (compose ldap-entry-children-data? ->entry) + records)) + ;; structure entries that live inside a data subtree + (kept (filter (lambda (r) + (any (compose ldap-entry-children-data? ->entry) + (ancestors r))) + records))) + (append + (map (lambda (a) (string-append "# ldap-verify: ignore-attr " a)) + ignore-attributes) + (map (lambda (r) + (string-append "# ldap-verify: ignore-dn ," (dn-regex (ldap-dn r)) "$")) + data-containers) + (map (lambda (r) + (string-append "# ldap-verify: keep-dn ^" (dn-regex (ldap-dn r)) "$")) + kept)))) + +(define (entry->ldif record) + (let ((entry (->entry record))) + (string-append + "dn: " (ldap-dn record) "\n" + (string-concatenate + (append-map (match-lambda + ((name . values) + (map (lambda (v) (string-append name ": " v "\n")) values))) + (ldap-entry-attributes entry)))))) + +(define* (ldap-structure->ldif name records #:key (ignore-attributes '()) + (comment "")) + "Return a file-like NAME holding RECORDS as LDIF, preceded by COMMENT and +the ldap-verify directives. Every record's parent must come before it." + (fold (lambda (record seen) + (let ((parent (ldap-entry-parent (->entry record)))) + (when (and parent (not (member (ldap-dn parent) seen))) + (error "LDAP structure entry listed before its parent:" + (ldap-dn record))) + (when (member (ldap-dn record) seen) + (error "LDAP structure entry listed twice:" (ldap-dn record))) + (cons (ldap-dn record) seen))) + '() records) + ;; a computed-file rather than plain-file so `guix build -e` can build it + (let ((text (string-append + comment + (string-join (directives records ignore-attributes) "\n" 'suffix) + "\n" + (string-join (map entry->ldif records) "\n")))) + (computed-file name + #~(call-with-output-file #$output + (lambda (port) (display #$text port)))))) diff --git a/metznet/services/secrets.scm b/metznet/services/secrets.scm new file mode 100644 index 0000000..4e3c537 --- /dev/null +++ b/metznet/services/secrets.scm @@ -0,0 +1,102 @@ +(define-module (metznet services secrets) + #:use-module (guix gexp) + #:use-module (guix records) + #:use-module (gnu packages base) + #:use-module (metznet packages tpm2) + #:use-module (gnu packages password-utils) + #:use-module (gnu services) + #:export (secret + secret? + secret-name + secret-sops-key + secret-destination + secret-owner + secret-group + secret-mode + + secrets-configuration + secrets-configuration? + secrets-service-type)) + +;; A single value pulled out of the shared sops-encrypted YAML (see +;; secrets/secrets.enc.yaml) and written to its own runtime file under +;; /run/secrets. +(define-record-type* secret make-secret + secret? + (name secret-name) + (sops-key secret-sops-key) + (destination secret-destination) + (owner secret-owner (default "root")) + (group secret-group (default "root")) + (mode secret-mode (default #o400))) + +;; sops-file: file-like, the sops-encrypted YAML holding this machine's +;; secrets. tpm-handle: string, the persistent TPM object handle the age +;; key was sealed under (see scripts/seal-age-key.sh). pcr-policy: string, +;; the PCR bank:indices the seal's policy session is bound to -- must +;; match what seal-age-key.sh used. +(define-record-type* secrets-configuration + make-secrets-configuration + secrets-configuration? + (sops-file secrets-configuration-sops-file) + (tpm-handle secrets-configuration-tpm-handle) + (pcr-policy secrets-configuration-pcr-policy + (default "sha256:7")) + (secrets secrets-configuration-secrets (default '()))) + +;; One secret's decrypt step: sops writes straight to `dest` under a tight +;; umask (set once, up front, in secrets-activation) so there's no window +;; where the plaintext is world-readable, then owner/group/mode are fixed +;; up to whatever the consuming service expects. +(define (secret-activation-gexp sops-file s) + #~(let ((dest #$(secret-destination s)) + (owner (getpw #$(secret-owner s))) + (group (getgr #$(secret-group s)))) + (invoke #$(file-append sops "/bin/sops") "decrypt" + "--output-type" "binary" + "--extract" #$(string-append "[\"" (secret-sops-key s) "\"]") + "--output" dest + #$sops-file) + (chmod dest #$(secret-mode s)) + (chown dest (passwd:uid owner) (group:gid group)))) + +;; Runs once at system activation, before any shepherd service starts (see +;; slapd-activation in metznet/services/slapd.scm for the same pattern of +;; shelling out via `invoke` from an activation gexp). Guix has no +;; systemd, so this drives tpm2-tools/sops directly rather than relying on +;; systemd-creds-style TPM integration: unseal the age key from the TPM +;; (policy-bound to pcr-policy, so it only unseals in the expected boot +;; state), use it to sops-decrypt each configured secret, then shred the +;; transient unsealed key. +(define (secrets-activation configuration) + (let ((tpm-handle (secrets-configuration-tpm-handle configuration)) + (pcr-policy (secrets-configuration-pcr-policy configuration)) + (sops-file (secrets-configuration-sops-file configuration)) + (secrets (secrets-configuration-secrets configuration))) + #~(let ((session-ctx "/run/secrets/.tpm-session.ctx") + (age-key-file "/run/secrets/.age-key")) + (mkdir-p/perms "/run/secrets" (getpw "root") #o700) + (umask #o077) + (invoke #$(file-append metznet-tpm2-tools "/bin/tpm2_startauthsession") + "--policy-session" "-S" session-ctx) + (invoke #$(file-append metznet-tpm2-tools "/bin/tpm2_policypcr") + "-S" session-ctx "-l" #$pcr-policy) + (invoke #$(file-append metznet-tpm2-tools "/bin/tpm2_unseal") + "-c" #$tpm-handle + "-p" (string-append "session:" session-ctx) + "-o" age-key-file) + (setenv "SOPS_AGE_KEY_FILE" age-key-file) + #$@(map (lambda (s) (secret-activation-gexp sops-file s)) secrets) + (unsetenv "SOPS_AGE_KEY_FILE") + (invoke #$(file-append metznet-tpm2-tools "/bin/tpm2_flushcontext") session-ctx) + (delete-file session-ctx) + (invoke #$(file-append coreutils "/bin/shred") "-u" age-key-file)))) + +(define secrets-service-type + (service-type (name 'secrets) + (description + "Unseal a TPM-sealed age key and use it to sops-decrypt +this machine's secrets into /run/secrets at boot.") + (extensions (list (service-extension activation-service-type + secrets-activation))) + (default-value #f))) diff --git a/metznet/services/slapd-config.scm b/metznet/services/slapd-config.scm new file mode 100644 index 0000000..489fbee --- /dev/null +++ b/metznet/services/slapd-config.scm @@ -0,0 +1,309 @@ +(define-module (metznet services slapd-config) + #:use-module (guix gexp) + #:use-module (guix records) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) + #:export (slapd-config + slapd-config? + slapd-schema + slapd-frontend + slapd-config-database + slapd-mdb + slapd-memberof-overlay + slapd-refint-overlay + slapd-totp-overlay + slapd-ppolicy-overlay + slapd-access + slapd-config->ldif)) + +;;; Commentary: +;;; +;;; slapd's cn=config (OLC) database described as Scheme records and +;;; serialized to one LDIF file in the store, in slapcat's shape (explicit +;;; {N} ordering prefixes), so it can be loaded with `slapadd -n 0` and +;;; checked against a dump of a live server with +;;; `scripts/ldap-verify.sh -b cn=config -X -e `. +;;; +;;; Nothing here is secret: olcRootPW is deliberately not representable. +;;; +;;; Code: + +;; A schema loaded as cn={N},cn=schema,cn=config. FILE is an +;; olcSchemaConfig LDIF (e.g. the package's etc/openldap/schema/*.ldif). +;; PATCHES is a list of (FROM . TO) literal substitutions applied to it +;; after unfolding LDIF continuation lines; each must match exactly once or +;; the build fails, so a patch can't silently stop applying. +(define-record-type* slapd-schema make-slapd-schema + slapd-schema? + (name slapd-schema-name) + (file slapd-schema-file) + (patches slapd-schema-patches (default '()))) + +(define-record-type* slapd-frontend make-slapd-frontend + slapd-frontend? + (access slapd-frontend-access (default '())) + (requires slapd-frontend-requires (default '())) + (size-limit slapd-frontend-size-limit (default #f)) + (password-hash slapd-frontend-password-hash (default #f))) + +(define-record-type* slapd-config-database + make-slapd-config-database + slapd-config-database? + (root-dn slapd-config-database-root-dn (default #f)) + (access slapd-config-database-access (default '())) + (overlays slapd-config-database-overlays (default '()))) + +(define-record-type* slapd-mdb make-slapd-mdb + slapd-mdb? + (suffix slapd-mdb-suffix) + (directory slapd-mdb-directory) + (root-dn slapd-mdb-root-dn (default #f)) + (access slapd-mdb-access (default '())) + (requires slapd-mdb-requires (default '())) + (last-mod? slapd-mdb-last-mod? (default #t)) + (checkpoint slapd-mdb-checkpoint (default #f)) + (indexes slapd-mdb-indexes (default '())) + (max-size slapd-mdb-max-size (default #f)) + (overlays slapd-mdb-overlays (default '()))) + +;; Overlays. Their objectClass lists match how ldap.metznet.ca's were +;; created (olcConfig/top are superclasses slapd doesn't add by itself), so +;; the generated entries compare equal to the live server's. +(define-record-type* slapd-memberof-overlay + make-slapd-memberof-overlay + slapd-memberof-overlay? + (dangling slapd-memberof-overlay-dangling (default "ignore")) + (refint? slapd-memberof-overlay-refint? (default #t)) + (group-object-class slapd-memberof-overlay-group-object-class + (default "groupOfNames")) + (member-attribute slapd-memberof-overlay-member-attribute + (default "member")) + (memberof-attribute slapd-memberof-overlay-memberof-attribute + (default "memberOf"))) + +(define-record-type* slapd-refint-overlay + make-slapd-refint-overlay + slapd-refint-overlay? + (attributes slapd-refint-overlay-attributes)) + +(define-record-type* slapd-totp-overlay + make-slapd-totp-overlay + slapd-totp-overlay?) + +(define-record-type* slapd-ppolicy-overlay + make-slapd-ppolicy-overlay + slapd-ppolicy-overlay? + (default-policy slapd-ppolicy-overlay-default-policy (default #f)) + (hash-cleartext? slapd-ppolicy-overlay-hash-cleartext? (default #f))) + +(define-record-type* slapd-config make-slapd-config + slapd-config? + (args-file slapd-config-args-file (default "/var/run/slapd/slapd.args")) + (pid-file slapd-config-pid-file (default "/var/run/slapd/slapd.pid")) + (log-level slapd-config-log-level (default '())) + (tls-ca-certificate-file slapd-config-tls-ca-certificate-file (default #f)) + (tls-certificate-file slapd-config-tls-certificate-file (default #f)) + (tls-certificate-key-file slapd-config-tls-certificate-key-file + (default #f)) + (tool-threads slapd-config-tool-threads (default #f)) + (module-path slapd-config-module-path (default #f)) + (modules slapd-config-modules (default '())) + (schemas slapd-config-schemas (default '())) + (frontend slapd-config-frontend (default (slapd-frontend))) + (config-database slapd-config-config-database + (default (slapd-config-database))) + (databases slapd-config-databases (default '()))) + +;; One olcAccess value: (slapd-access "attrs=userPassword" "self write" +;; "* none") => "to attrs=userPassword by self write by * none". +(define (slapd-access what . by) + (string-join (cons (string-append "to " what) + (map (lambda (clause) (string-append "by " clause)) by)) + " ")) + +;;; Serialization (host side: plain strings). + +(define (ldif-entry dn attributes) + "DN plus ATTRIBUTES, a list of (NAME . VALUE) where VALUE is a string, a +list of strings, or #f (omitted), as one LDIF entry and its blank line." + (string-append + "dn: " dn "\n" + (string-concatenate + (append-map (match-lambda + ((_ . #f) '()) + ((name . (? list? values)) + (map (lambda (v) (string-append name ": " v "\n")) values)) + ((name . value) (list (string-append name ": " value "\n")))) + attributes)) + "\n")) + +(define (ordered values) + "VALUES with X-ORDERED {N} prefixes, as slapcat prints them." + (map (lambda (i v) (string-append "{" (number->string i) "}" v)) + (iota (length values)) values)) + +(define (boolean->ldap b) (if b "TRUE" "FALSE")) +(define (maybe-number n) (and n (number->string n))) + +(define (overlay-name overlay) + (match overlay + ((? slapd-memberof-overlay?) "memberof") + ((? slapd-refint-overlay?) "refint") + ((? slapd-totp-overlay?) "totp") + ((? slapd-ppolicy-overlay?) "ppolicy"))) + +(define (overlay-attributes overlay) + (match overlay + (($ dangling refint? group-oc member-ad memberof-ad) + `(("objectClass" . ("olcConfig" "olcMemberOfConfig" "olcOverlayConfig" "top")) + ("olcMemberOfDangling" . ,dangling) + ("olcMemberOfRefInt" . ,(boolean->ldap refint?)) + ("olcMemberOfGroupOC" . ,group-oc) + ("olcMemberOfMemberAD" . ,member-ad) + ("olcMemberOfMemberOfAD" . ,memberof-ad))) + (($ attributes) + `(("objectClass" . ("olcConfig" "olcOverlayConfig" "olcRefintConfig")) + ;; one value per attribute: slapd rejects space-separated lists in + ;; future versions + ("olcRefintAttribute" . ,attributes))) + (($ ) + '(("objectClass" . "olcOverlayConfig"))) + (($ default-policy hash-cleartext?) + `(("objectClass" . ("olcOverlayConfig" "olcPPolicyConfig")) + ("olcPPolicyDefault" . ,default-policy) + ("olcPPolicyHashCleartext" . ,(boolean->ldap hash-cleartext?)))))) + +(define (overlay-entries overlays database-dn) + (string-concatenate + (map (lambda (i overlay) + (let ((ordered-name (string-append "{" (number->string i) "}" + (overlay-name overlay)))) + (ldif-entry (string-append "olcOverlay=" ordered-name "," + database-dn) + (append (overlay-attributes overlay) + `(("olcOverlay" . ,ordered-name)))))) + (iota (length overlays)) overlays))) + +(define (global-entries config) + (match-record config + (args-file pid-file log-level tls-ca-certificate-file tls-certificate-file + tls-certificate-key-file tool-threads module-path modules) + (string-append + (ldif-entry "cn=config" + `(("objectClass" . "olcGlobal") + ("cn" . "config") + ("olcArgsFile" . ,args-file) + ("olcLogLevel" . ,log-level) + ("olcPidFile" . ,pid-file) + ("olcTLSCACertificateFile" . ,tls-ca-certificate-file) + ("olcTLSCertificateFile" . ,tls-certificate-file) + ("olcTLSCertificateKeyFile" . ,tls-certificate-key-file) + ("olcToolThreads" . ,(maybe-number tool-threads)))) + (if (null? modules) + "" + (ldif-entry "cn=module{0},cn=config" + `(("objectClass" . "olcModuleList") + ("cn" . "module{0}") + ("olcModulePath" . ,module-path) + ("olcModuleLoad" . ,(ordered modules))))) + (ldif-entry "cn=schema,cn=config" + '(("objectClass" . "olcSchemaConfig") + ("cn" . "schema")))))) + +(define (database-entries config) + (match-record config (frontend config-database databases) + (string-append + (match-record frontend + (access requires size-limit password-hash) + (ldif-entry "olcDatabase={-1}frontend,cn=config" + `(("objectClass" . ("olcDatabaseConfig" "olcFrontendConfig")) + ("olcDatabase" . "{-1}frontend") + ("olcAccess" . ,(ordered access)) + ("olcRequires" . ,requires) + ("olcSizeLimit" . ,(maybe-number size-limit)) + ("olcPasswordHash" . ,password-hash)))) + (match-record config-database + (root-dn access overlays) + (let ((dn "olcDatabase={0}config,cn=config")) + (string-append + (ldif-entry dn + `(("objectClass" . "olcDatabaseConfig") + ("olcDatabase" . "{0}config") + ("olcAccess" . ,(ordered access)) + ("olcRootDN" . ,root-dn))) + (overlay-entries overlays dn)))) + (string-concatenate + (map (lambda (i db) + (match-record db + (suffix directory root-dn access requires last-mod? checkpoint + indexes max-size overlays) + (let* ((name (string-append "{" (number->string i) "}mdb")) + (dn (string-append "olcDatabase=" name ",cn=config"))) + (string-append + (ldif-entry dn + `(("objectClass" . ("olcDatabaseConfig" "olcMdbConfig")) + ("olcDatabase" . ,name) + ("olcDbDirectory" . ,directory) + ("olcSuffix" . ,suffix) + ("olcAccess" . ,(ordered access)) + ("olcLastMod" . ,(boolean->ldap last-mod?)) + ("olcRequires" . ,requires) + ("olcRootDN" . ,root-dn) + ("olcDbCheckpoint" . ,checkpoint) + ("olcDbIndex" . ,indexes) + ("olcDbMaxSize" . ,(maybe-number max-size)))) + (overlay-entries overlays dn))))) + (iota (length databases) 1) databases))))) + +;;; The LDIF file (build side: schema files are only readable there). + +(define (slapd-config->ldif config) + "Return a file-like object for CONFIG as slapcat-shaped cn=config LDIF." + (let ((schemas (slapd-config-schemas config))) + (computed-file + "slapd-config.ldif" + #~(begin + (use-modules (ice-9 textual-ports) (ice-9 regex) (srfi srfi-1)) + + (define (unfold text) + (regexp-substitute/global #f "\n " text 'pre 'post)) + + (define (apply-patch text patch) + (let* ((from (car patch)) + (matches (list-matches (regexp-quote from) text))) + (unless (= 1 (length matches)) + (error "schema patch must match exactly once:" from + (length matches))) + (regexp-substitute #f (car matches) 'pre (cdr patch) 'post))) + + ;; The schema file with its dn/cn renamed to cn={INDEX}NAME, comments + ;; dropped and continuation lines unfolded. + (define (schema-entry index name file patches) + (let* ((ordered (string-append "{" (number->string index) "}" name)) + (text (fold (lambda (p t) (apply-patch t p)) + (unfold (call-with-input-file file get-string-all)) + patches))) + (string-append + (string-join + (filter-map + (lambda (line) + (cond ((or (string-null? line) (string-prefix? "#" line)) #f) + ((string-prefix? "dn:" line) + (string-append "dn: cn=" ordered ",cn=schema,cn=config")) + ((string-prefix? "cn:" line) + (string-append "cn: " ordered)) + (else line))) + (string-split text #\newline)) + "\n") + "\n\n"))) + + (call-with-output-file #$output + (lambda (port) + (display #$(global-entries config) port) + (for-each (lambda (index name file patches) + (display (schema-entry index name file patches) port)) + (iota #$(length schemas)) + '#$(map slapd-schema-name schemas) + (list #$@(map slapd-schema-file schemas)) + '#$(map slapd-schema-patches schemas)) + (display #$(database-entries config) port))))))) diff --git a/metznet/system/base-system.scm b/metznet/system/base-system.scm index 57b0183..4015f30 100644 --- a/metznet/system/base-system.scm +++ b/metznet/system/base-system.scm @@ -2,6 +2,7 @@ #:use-module (guix gexp) #:use-module (metznet golang) + #:use-module (metznet aws) #:use-module (gnu bootloader) #:use-module (gnu bootloader grub) @@ -39,7 +40,6 @@ #:use-module (gnu packages version-control) #:use-module (gnu packages vim) #:use-module (gnu packages certs) - #:use-module (gnu packages nss) #:use-module (gnu packages kerberos) #:use-module (gnu packages vpn) #:use-module (gnu packages wm) @@ -59,11 +59,13 @@ #:export (%metznet-desktop-services) #:export (%metznet-server-services) + #:export (%metznet-aws-server-services) #:export (metznet-service-type) #:export (metznet-system-configuration) #:export (%metznet-base-server-system) + #:export (%metznet-base-aws-system) #:export (%metznet-base-desktop-system)) (define %domain-realm @@ -106,7 +108,6 @@ neovim zsh le-certs - nss-certs mit-krb5) %base-packages)) (define %metznet-desktop-packages @@ -414,6 +415,12 @@ (define %metznet-server-services (append %server-services %base-services-nscd)) +;; EC2 instances: aws-pubkey writes the instance's launch key pair to +;; /etc/ssh/authorized_keys.d/aws at boot, so the `aws` account can log in +;; even when LDAP (and so the AuthorizedKeysCommand) is unreachable. +(define %metznet-aws-server-services + (cons (service aws-service-type) %metznet-server-services)) + (define %metznet-base-server-system (operating-system (host-name "metznet-base-server") @@ -442,6 +449,12 @@ (services %metznet-server-services))) +(define %metznet-base-aws-system + (operating-system + (inherit %metznet-base-server-system) + (host-name "metznet-base-aws") + (services %metznet-aws-server-services))) + (define %metznet-base-desktop-system (operating-system (inherit %metznet-base-server-system) diff --git a/scripts/guix-docker.sh b/scripts/guix-docker.sh index 1be5b2a..cec9fea 100755 --- a/scripts/guix-docker.sh +++ b/scripts/guix-docker.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash # Drive the dockerized Guix build environment (see docker/) to build the -# operating-system definitions in this repo as `guix system docker-image` +# operating-system definitions in this repo as `guix system image -t docker` # tarballs, loadable straight into Docker Desktop. set -euo pipefail @@ -55,9 +55,12 @@ case "$cmd" in # guix-daemon's store-path grafting (security-patch rewriting) hits # a permission error creating output files -- skipping grafts # avoids it entirely and just uses the un-grafted originals. - out=\$(guix system docker-image -N --no-grafts -L /workspace '$scm') + out=\$(guix system image -t docker -N --no-grafts -L /workspace '$scm') mkdir -p /workspace/build - cp \"\$out\" '/workspace/build/$name.tar.gz' + # install, not cp: store outputs are read-only, and a stale + # read-only build/$name.tar.gz from a previous run would make a + # plain cp fail with 'Permission denied' on the second pass. + install -m 644 \"\$out\" '/workspace/build/$name.tar.gz' echo \"wrote build/$name.tar.gz\" " ;; diff --git a/scripts/ldap-verify.sh b/scripts/ldap-verify.sh new file mode 100755 index 0000000..c74eae3 --- /dev/null +++ b/scripts/ldap-verify.sh @@ -0,0 +1,396 @@ +#!/usr/bin/env bash +# Check an LDAP server (or a slapcat dump of one) against what this repo +# expects: that the schema it needs is loaded, and that a set of entries +# exists with (at least) the listed attribute values. Extra attributes/ +# values/entries are fine -- this is a subset check, not a diff -- so it can +# run against the live ldap.metznet.ca as well as a freshly seeded/restored +# test instance. +# +# usage: ldap-verify.sh [-H uri] [-D binddn] [-w pw | -y pwfile] [-x] +# [-b base | -f dump.ldif] [-X] [-i attr]... [-I dn-regex]... [-K dn-regex]... [-p] +# [-s schema.txt] [-e expected.ldif]... +# -H/-D/-w/-y/-x passed through to ldapsearch/ldapcompare (simple bind) +# -b BASE fetch the whole subtree under BASE with ldapsearch and check +# it like a dump (-f). Use for cn=config (-b cn=config), whose +# ordered attributes don't compare well with ldapcompare. +# -f FILE check an LDIF dump (slapcat or ldapsearch) instead of a +# live server. Values are compared case-insensitively. +# -s FILE schema expectations, default tests/ldap/schema.txt: +# objectClass +# attributeType +# Checked against the live server's subschema entry only: +# skipped with -b/-f, since cn=config doesn't list +# attributes built into slapd (uidNumber) or registered +# by overlays (memberOf). +# -e FILE expected entries as LDIF (repeatable), default +# build/ldap-structure.ldif (generated from %ldap-structure +# by tests/ldap/generated-config.sh). Against a live server every +# attribute line is checked with ldapcompare, so values +# match using the server's own matching rules. A value +# written without a leading {N} matches any position of an +# X-ORDERED value (olcAccess, olcModuleLoad, ...); with one, +# only that position -- or a value the server stores with +# no position (e.g. a lone olcOverlay: memberof). +# -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 +# in cn=schema,cn=config and the schema entries under it +# (compared by entry only; the live subschema check +# covers what's used). +# -i ATTR another attribute to ignore under -X/-p (repeatable) +# -I REGEX under -X/-p, skip entries whose DN matches this extended +# regex (case-insensitive; repeatable), e.g. user entries +# -K REGEX keep entries matching this regex even if an -I matches, +# e.g. the system accounts the repo binds as (repeatable) +# -p print the dump (-b/-f) as a snapshot -- everything -X +# would compare -- instead of checking, e.g. +# ... -b dc=metznet,dc=ca -p -I ',ou=users,...$' > dump.ldif +# Any -i/-I are written into it as directive comments +# that -X reads back from -e files: +# # ldap-verify: ignore-attr +# # ldap-verify: ignore-dn +# # ldap-verify: keep-dn +# +# Bind as a DN that can read everything you list -- entries hidden by ACLs +# show up as failures. TLS is ldap.conf/LDAPTLS_* as usual (e.g. +# LDAPTLS_REQCERT=never against a test instance with a self-signed cert). +# +# exit: 0 all checks passed, 1 some check failed, 2 usage/connection error +set -uo pipefail + +here="$(cd "$(dirname "$0")/.." && pwd)" +schema_file="$here/tests/ldap/schema.txt" +ldif_files=() +conn=() +dump="" +base="" +exact="" +print="" +ignore_dns=() +keep_dns=() +extra_ignores="" +ignore="entryuuid creatorsname createtimestamp entrycsn modifiersname modifytimestamp structuralobjectclass contextcsn entrydn hassubordinates subschemasubentry olcrootpw" +schema_defs="olcattributetypes olcobjectclasses olcobjectidentifier olcldapsyntaxes olcditcontentrules" + +while getopts "H:D:w:y:xb:f:Xi:I:K:ps:e:h" opt; do + case $opt in + H|D|w|y) conn+=("-$opt" "$OPTARG") ;; + x) conn+=(-x) ;; + b) base="$OPTARG" ;; + f) dump="$OPTARG" ;; + X) exact=1 ;; + i) ignore="$ignore $(tr 'A-Z' 'a-z' <<<"$OPTARG")"; extra_ignores="$extra_ignores $OPTARG" ;; + I) ignore_dns+=("$OPTARG") ;; + K) keep_dns+=("$OPTARG") ;; + p) print=1 ;; + s) schema_file="$OPTARG" ;; + e) ldif_files+=("$OPTARG") ;; + *) sed -n '2,/^set /s/^# \{0,1\}//p' "$0"; exit 2 ;; + esac +done +if [[ ${#ldif_files[@]} -eq 0 ]]; then + ldif_files=("$here/build/ldap-structure.ldif") + [[ -r "${ldif_files[0]}" ]] || { + echo "no ${ldif_files[0]}; build it with:" >&2 + echo " docker compose run --rm -T guix bash -lc tests/ldap/generated-config.sh" >&2 + exit 2 + } +fi +if [[ -n "$exact$print" && -z "$dump$base" ]]; then + echo "-X/-p need a dump: -b BASE or -f FILE" >&2; exit 2 +fi +# Directives saved into a snapshot by -p. +if [[ -n "$exact" ]]; then + while read -r _ _ kind arg; do + case $kind in + ignore-attr) ignore="$ignore $(tr 'A-Z' 'a-z' <<<"$arg")" ;; + ignore-dn) ignore_dns+=("$arg") ;; + keep-dn) keep_dns+=("$arg") ;; + *) printf 'unknown directive "%s"\n' "$kind" >&2; exit 2 ;; + esac + done < <(grep -h '^# ldap-verify: ' "${ldif_files[@]}") +fi +# Anonymous simple bind unless told otherwise, rather than SASL. +[[ " ${conn[*]-} " == *" -x "* ]] || conn+=(-x) + +pass=0 +fail=0 +ok() { pass=$((pass + 1)); printf ' ok %s\n' "$*"; } +bad() { fail=$((fail + 1)); printf ' FAIL %s\n' "$*"; } + +search() { ldapsearch "${conn[@]}" -LLL -o ldif-wrap=no "$@"; } + +# Unfold LDIF continuation lines, drop comments, and emit one record per +# attribute: "\t\t\t\t", sep being ":" or "::" +# (base64), with a bare "\t" record starting each entry. +parse_ldif() { + awk ' + function flush() { + if (line == "") return + if (line !~ /^#/) { + i = index(line, ":") + attr = substr(line, 1, i - 1) + sep = ":"; rest = substr(line, i + 1) + if (substr(rest, 1, 1) == ":") { sep = "::"; rest = substr(rest, 2) } + else if (substr(rest, 1, 1) == "<") { printf "unsupported URL value: %s\n", line > "/dev/stderr"; exit 2 } + sub(/^ +/, "", rest) + if (tolower(attr) == "dn") { dnsep = sep; dn = rest; print dnsep "\t" dn } + else if (dn != "" && attr != "changetype") print dnsep "\t" dn "\t" attr "\t" sep "\t" rest + } + line = "" + } + /^ / { line = line substr($0, 2); next } + { flush() } + /^$/ { dn = "" } + { line = $0 } + END { flush() } + ' "$1" +} + +# Comparable forms for the dump index: DNs lowercased with no spaces around +# "," or "=", attribute names lowercased, base64 values decoded. +norm_dn() { tr 'A-Z' 'a-z' <<<"$1" | sed -E 's/ *([,=]) */\1/g'; } +decode() { if [[ "$1" == "::" ]]; then base64 --decode <<<"$2" 2>/dev/null || printf '%s' "$2"; else printf '%s' "$2"; fi; } +# skipped_dn NDN -> 0 if -X/-p skip this whole entry (-I, unless -K) +skipped_dn() { + local re + for re in ${keep_dns[@]+"${keep_dns[@]}"}; do + [[ "$1" =~ $(tr 'A-Z' 'a-z' <<<"$re") ]] && return 1 + done + for re in ${ignore_dns[@]+"${ignore_dns[@]}"}; do + [[ "$1" =~ $(tr 'A-Z' 'a-z' <<<"$re") ]] && return 0 + done + return 1 +} +# ignored LATTR NDN -> 0 if -X/-p skip this attribute on this entry +ignored() { + [[ " $ignore " == *" $1 "* ]] && return 0 + [[ ( "$2" == "cn=schema,cn=config" || "$2" == *",cn=schema,cn=config" ) && " $schema_defs " == *" $1 "* ]] +} + +# --- connectivity / load ---------------------------------------------------- + +tmp="$(mktemp -d)" +trap 'rm -rf "$tmp"' EXIT + +if [[ -n "$base" ]]; then + dump="$tmp/subtree.ldif" + if ! search -b "$base" '(objectClass=*)' >"$dump" 2>"$tmp/err"; then + printf 'cannot read %s:\n%s\n' "$base" "$(cat "$tmp/err")" >&2 + exit 2 + fi +fi + +if [[ -n "$dump" ]]; then + [[ -r "$dump" ]] || { echo "cannot read $dump" >&2; exit 2; } + index="$tmp/index" + # "|" per entry and "|\t\t=" per value, plus + # "|\t\t~" for the value without its {N} ordering + # prefix. The leading "|" anchors substring matches to the start of a line. + parse_ldif "$dump" | while IFS=$'\t' read -r dnsep dn attr sep value; do + [[ "$dnsep" == "::" ]] && dn="$(base64 --decode <<<"$dn")" + ndn="$(norm_dn "$dn")" + if [[ -z "${attr:-}" ]]; then + printf '|%s\n' "$ndn" + else + lattr="$(tr 'A-Z' 'a-z' <<<"$attr")" + v="$(decode "$sep" "$value")" + printf '|%s\t%s\t=%s\n' "$ndn" "$lattr" "$v" + if [[ "$v" =~ ^\{-?[0-9]+\}(.*)$ ]]; then + printf '|%s\t%s\t~%s\n' "$ndn" "$lattr" "${BASH_REMATCH[1]}" + fi + fi + done >"$index" || exit 2 + + if [[ -n "$print" ]]; then + first=1 + for a in $extra_ignores; do echo "# ldap-verify: ignore-attr $a"; first=""; done + for re in ${ignore_dns[@]+"${ignore_dns[@]}"}; do echo "# ldap-verify: ignore-dn $re"; first=""; done + for re in ${keep_dns[@]+"${keep_dns[@]}"}; do echo "# ldap-verify: keep-dn $re"; first=""; done + parse_ldif "$dump" | while IFS=$'\t' read -r dnsep dn attr sep value; do + if [[ -z "${attr:-}" ]]; then + ndn="$(norm_dn "$([[ "$dnsep" == "::" ]] && base64 --decode <<<"$dn" || printf '%s' "$dn")")" + skip="" + if skipped_dn "$ndn"; then skip=1; continue; fi + [[ -n "$first" ]] || echo + first="" + printf 'dn%s %s\n' "$dnsep" "$dn" + elif [[ -z "$skip" ]] && ! ignored "$(tr 'A-Z' 'a-z' <<<"$attr")" "$ndn"; then + printf '%s%s %s\n' "$attr" "$sep" "$value" + fi + done + exit 0 + fi +else + if ! rootdse="$(search -s base -b "" '(objectClass=*)' subschemaSubentry 2>&1)"; then + printf 'cannot query root DSE:\n%s\n' "$rootdse" >&2 + exit 2 + fi + subschema="$(sed -n 's/^subschemaSubentry: //p' <<<"$rootdse")" + subschema="${subschema:-cn=Subschema}" + if ! schema="$(search -s base -b "$subschema" '(objectClass=*)' objectClasses attributeTypes 2>&1)"; then + printf 'cannot read %s:\n%s\n' "$subschema" >&2 + exit 2 + fi +fi + +# has_entry DN -> 0 if present +# compare DN ATTR SEP VALUE -> 6 match, 5 other value, 16 attribute absent +# (ldapcompare's exit codes), else error +if [[ -n "$dump" ]]; then + has_entry() { grep -Fixq -- "|$(norm_dn "$1")" "$index"; } + compare() { + local key + key="|$(norm_dn "$1")"$'\t'"$(tr 'A-Z' 'a-z' <<<"$2")"$'\t' + local v; v="$(decode "$3" "$4")" + if [[ "$v" =~ ^\{-?[0-9]+\}(.*)$ ]]; then + # positioned: that exact position, or a value stored with no position + grep -Fixq -- "$key=$v" "$index" && return 6 + grep -Fixq -- "$key=${BASH_REMATCH[1]}" "$index" && return 6 + else + grep -Fixq -- "$key=$v" "$index" && return 6 + grep -Fixq -- "$key~$v" "$index" && return 6 + fi + if grep -Fiq -- "$key" "$index"; then return 5; fi + return 16 + } +else + has_entry() { search -s base -b "$1" '(objectClass=*)' 1.1 >/dev/null 2>&1; } + compare() { out="$(ldapcompare "${conn[@]}" -z "$1" "$2$3$4" 2>&1)"; } +fi + +# --- schema ----------------------------------------------------------------- + +echo "schema ($schema_file)" +if [[ -n "$dump" ]]; then + echo " (skipped: only checked against a live server's subschema entry)" +else + while read -r kind name _; do + [[ -z "${kind:-}" || "$kind" == \#* ]] && continue + case $kind in + objectClass) attr=objectClasses ;; + attributeType) attr=attributeTypes ;; + *) printf 'unknown schema kind "%s" in %s\n' "$kind" "$schema_file" >&2; exit 2 ;; + esac + # "NAME 'x'" or "NAME ( 'x' 'y' )" + if grep -Eiq "^$attr: \(.* NAME (\\( [^)]*)?'$name'" <<<"$schema"; then + ok "$kind $name" + else + bad "$kind $name not in $subschema" + fi + done <"$schema_file" +fi + +# --- entries ---------------------------------------------------------------- + +# lint FILE: problems in an expected-entries file that would otherwise be +# silently merged or never match -- a DN listed twice, or an RDN whose value +# isn't among that entry's own values of the RDN attribute. +lint_ldif() { + parse_ldif "$1" | awk -F'\t' ' + function check() { + if (dn != "" && has && !match_) printf "%s: RDN %s=%s but its %s values don'"'"'t include it\n", orig, rattr, rval, rattr + } + NF == 2 { + check() + orig = $2; dn = tolower($2); gsub(/ *, */, ",", dn); gsub(/ *= */, "=", dn) + if (seen[dn]++) printf "%s: listed more than once\n", orig + rdn = dn; sub(/,.*/, "", rdn) + rattr = rdn; sub(/=.*/, "", rattr); rval = rdn; sub(/^[^=]*=/, "", rval) + sub(/^\{-?[0-9]+\}/, "", rval) # X-ORDERED RDNs, e.g. olcOverlay={0}memberof + if (rdn ~ /\+/ || $1 == "::") rattr = "" + has = 0; match_ = 0; next + } + NF == 5 && rattr != "" && tolower($3) == rattr && $4 == ":" { + v = tolower($5); sub(/^\{-?[0-9]+\}/, "", v) + has = 1; if (v == rval) match_ = 1 + } + END { check() } + ' +} + +for ldif in "${ldif_files[@]}"; do + echo "entries ($ldif)" + while IFS= read -r problem; do + [[ -n "$problem" ]] && bad "$problem" + done < <(lint_ldif "$ldif") + records="$(parse_ldif "$ldif")" || exit 2 + skip="" + while IFS=$'\t' read -r dnsep dn attr sep value; do + [[ "$dnsep" == "::" ]] && dn="$(base64 --decode <<<"$dn")" + if [[ -z "${attr:-}" ]]; then + if has_entry "$dn"; then + ok "$dn"; skip="" + else + bad "$dn missing (or not readable by this bind DN)"; skip="$dn" + fi + 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 + # generated config can be the expected file + if [[ -n "$dump" ]] && ignored "$(tr 'A-Z' 'a-z' <<<"$attr")" "$(norm_dn "$dn")"; then continue; fi + out="" + compare "$dn" "$attr" "$sep" "$value" + case $? in + 6) ok "$dn $attr: $value" ;; + 5) bad "$dn $attr: $value (different value)" ;; + 16) bad "$dn $attr: $value (attribute absent)" ;; + *) bad "$dn $attr: $value ($(tr '\n' ' ' <<<"$out"))" ;; + esac + done <<<"$records" +done + +# --- exact: nothing beyond what the -e files list --------------------------- + +if [[ -n "$exact" ]]; then + echo "exact (no entries or values beyond the above)" + expected="$tmp/expected" + for ldif in "${ldif_files[@]}"; do + parse_ldif "$ldif" | while IFS=$'\t' read -r dnsep dn attr sep value; do + [[ "$dnsep" == "::" ]] && dn="$(base64 --decode <<<"$dn")" + if [[ -z "${attr:-}" ]]; then + printf '|%s\n' "$(norm_dn "$dn")" + else + ev="$(decode "$sep" "$value")" + printf '|%s\t%s\t=%s\n' "$(norm_dn "$dn")" "$(tr 'A-Z' 'a-z' <<<"$attr")" "$ev" + if [[ "$ev" =~ ^\{-?[0-9]+\}(.*)$ ]]; then + printf '|%s\t%s\t~%s\n' "$(norm_dn "$dn")" "$(tr 'A-Z' 'a-z' <<<"$attr")" "${BASH_REMATCH[1]}" + fi + fi + done + done >"$expected" + before=$fail + skip="" + while IFS=$'\t' read -r dnsep dn attr sep value; do + [[ "$dnsep" == "::" ]] && dn="$(base64 --decode <<<"$dn")" + ndn="$(norm_dn "$dn")" + if [[ -z "${attr:-}" ]]; then + skip="" + if skipped_dn "$ndn"; then skip="$ndn"; continue; fi + grep -Fixq -- "|$ndn" "$expected" || { bad "unexpected entry $dn"; skip="$ndn"; } + continue + fi + [[ "$skip" == "$ndn" ]] && continue + lattr="$(tr 'A-Z' 'a-z' <<<"$attr")" + ignored "$lattr" "$ndn" && continue + v="$(decode "$sep" "$value")" + key="|$ndn"$'\t'"$lattr"$'\t' + grep -Fixq -- "$key=$v" "$expected" && continue + if [[ "$v" =~ ^\{-?[0-9]+\}(.*)$ ]]; then + # positioned on the server: listed unpositioned in the expected file + grep -Fixq -- "$key=${BASH_REMATCH[1]}" "$expected" && continue + else + # unpositioned on the server: listed at any position + grep -Fixq -- "$key~$v" "$expected" && continue + fi + bad "$dn unexpected $attr: $value" + done < <(parse_ldif "$dump") + [[ $fail -eq $before ]] && ok "nothing unexpected in ${base:-$dump}" +fi + +echo +echo "$pass passed, $fail failed" +[[ $fail -eq 0 ]] diff --git a/scripts/seal-age-key.sh b/scripts/seal-age-key.sh new file mode 100755 index 0000000..2ef20fb --- /dev/null +++ b/scripts/seal-age-key.sh @@ -0,0 +1,86 @@ +#!/usr/bin/env bash +# One-time, per-machine provisioning: generate an age keypair and seal the +# private half inside that machine's TPM, bound to a PCR policy so it only +# unseals on that same machine in the same (measured) boot state. The +# *public* key becomes a recipient in .sops.yaml (safe to commit); the +# *sealed* TPM object is what metznet/services/secrets.scm's activation +# script unseals at boot via tpm2_unseal -- the raw private key never +# touches disk outside this script's own tmpdir, and that tmpdir is wiped +# on exit. +# +# Requires a real TPM 2.0 device (or a software one, e.g. swtpm, for +# testing -- see docker/README.md's "Secrets (sops + TPM)" section). +# Docker Desktop on macOS exposes neither, so this only runs on real +# Linux hardware (or inside a container with /dev/tpmrm0 passed through, +# or TPM2TOOLS_TCTI pointed at a swtpm instance). +# +# usage: seal-age-key.sh [pcr-selection] +# persistent-handle e.g. 0x81010001 -- must match the tpm-handle field +# of the using this key +# pcr-selection default: sha256:7 (UEFI Secure Boot state). Adjust +# to match your actual measured-boot setup -- this +# default has NOT been validated against a real +# metznet machine's PCR layout yet. +set -euo pipefail + +handle="${1:?usage: $0 [pcr-selection]}" +pcrs="${2:-sha256:7}" + +command -v tpm2_createprimary >/dev/null || { echo "tpm2-tools not found" >&2; exit 1; } +command -v age-keygen >/dev/null || { echo "age-keygen not found" >&2; exit 1; } + +tmpdir="$(mktemp -d)" +trap 'shred -u "$tmpdir"/age-key.txt "$tmpdir"/age-key-raw.txt 2>/dev/null; rm -rf "$tmpdir"' EXIT + +age-keygen -o "$tmpdir/age-key.txt" 2>"$tmpdir/pubkey.txt" +pubkey="$(grep -o 'age1[a-z0-9]*' "$tmpdir/pubkey.txt")" + +# TPM2 sealed-data objects cap out around 128 bytes -- age-keygen's file +# (comments + key, ~190 bytes) doesn't fit, but age/sops don't need the +# comments: just the bare AGE-SECRET-KEY-1... line (~74 bytes) is a fully +# valid identity file on its own. +grep -v '^#' "$tmpdir/age-key.txt" > "$tmpdir/age-key-raw.txt" + +tpm2_createprimary -C o -c "$tmpdir/primary.ctx" >/dev/null + +tpm2_startauthsession -S "$tmpdir/session.ctx" >/dev/null +tpm2_policypcr -S "$tmpdir/session.ctx" -l "$pcrs" -L "$tmpdir/pcr.policy" >/dev/null +tpm2_flushcontext "$tmpdir/session.ctx" >/dev/null + +# Real and virtual TPMs alike commonly support only 2-3 transient object +# slots at once. Each of these commands reloads primary.ctx from disk +# (that's the whole point of context files), so flushing every loaded +# transient object between steps is always safe -- nothing but the +# freshly-saved context files is needed to continue. Without this, +# chaining create -> load -> evictcontrol reliably exhausts a 3-slot TPM +# ("out of memory for object contexts"). +tpm2_flushcontext -t >/dev/null + +tpm2_create -C "$tmpdir/primary.ctx" -u "$tmpdir/seal.pub" -r "$tmpdir/seal.priv" \ + -a "fixedtpm|fixedparent|adminwithpolicy" \ + -i "$tmpdir/age-key-raw.txt" -L "$tmpdir/pcr.policy" >/dev/null + +tpm2_flushcontext -t >/dev/null + +tpm2_load -C "$tmpdir/primary.ctx" -u "$tmpdir/seal.pub" -r "$tmpdir/seal.priv" \ + -c "$tmpdir/seal.ctx" >/dev/null + +tpm2_flushcontext -t >/dev/null + +tpm2_evictcontrol -C o -c "$tmpdir/seal.ctx" "$handle" >/dev/null + +cat < to use: + (tpm-handle "$handle") + (pcr-policy "$pcrs") + +Re-encrypt secrets/secrets.enc.yaml against the new recipient with: + sops updatekeys secrets/secrets.enc.yaml + +The unsealed private key existed only in $tmpdir and has been shredded. +EOF diff --git a/secrets/secrets.enc.yaml b/secrets/secrets.enc.yaml new file mode 100644 index 0000000..44abc8b --- /dev/null +++ b/secrets/secrets.enc.yaml @@ -0,0 +1,16 @@ +kerberos-ldap-bind-password: ENC[AES256_GCM,data:IAB9f6PYpvRWx2DQuzAjeoVr6xqreiEVp1yVhg==,iv:baPm7qzJs5XnpcCYLYT510Dtr7JDbE8Z6sFy35vGkuU=,tag:Ff8ByDbgu/rFF5btYLVhXw==,type:str] +sops: + age: + - enc: | + -----BEGIN AGE ENCRYPTED FILE----- + YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBZVlEyTVg0U3ZkV1F6Ulcv + ZGt4KzhGZzZZbHA2TmpYV0IyWE1rMEQ3djBBClN5OEdNc2w1MDZHSGVtc3k2SnFt + aGc3akJ4VFg1alZzRm5KS0F2TzVBUzgKLS0tIC9nd21Ga2pPOExTTVBoVjVlRU5H + NzB6ZGhGTzhGSUlQVU9TWDdQUlNwSDgK/OnHr/j+GkLRGB9CO3HUtOdZTpaoK9qg + 6BwSXmrwmeMv1w0Baw5T/6lUajtnKNxyYQFqDLyJtTeZQKUnM8LRmQ== + -----END AGE ENCRYPTED FILE----- + recipient: age1cfqm9lzjf7y3kvjca5r06kvy84p6a5efh5p79m3xxwehyfh3gqgqefqgvw + lastmodified: "2026-09-09T18:58:54Z" + mac: ENC[AES256_GCM,data:BQybVBikEDOxFVQGRa/Vuq6acm/j2bb1T2pi3QyImpnLCl3aP1sLKw/EPw/GiB0c3Mpk2crjwrSvXvqbitcoHyaj9cqOs/3inPoBemVnK2RCEBhlmYxUuvWRUZmpbMRwEj5ovwCI+R30IHTVwP4QcSirNN1rTux4tafvROy72Lk=,iv:tRKKjkhLddI2ZuzA4TJpJunZ8Yfjah2HiXU+ogxHFx0=,tag:TEfBSLMbhE0GbH6LYQUEzA==,type:str] + unencrypted_suffix: _unencrypted + version: 3.13.3 diff --git a/tests/ldap/apply-changes.sh b/tests/ldap/apply-changes.sh new file mode 100755 index 0000000..50a00e3 --- /dev/null +++ b/tests/ldap/apply-changes.sh @@ -0,0 +1,80 @@ +#!/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' +# where config.ldif is `slapcat -n 0` from the server (olcRootPW is replaced +# with a test password, so a redacted dump is fine). +# +# The "before changes" checks describe the config *before* ldap/changes/ +# 0002-0003 -- update them once a dump taken after applying those is used. +set -uo pipefail +cd "$(dirname "$0")/../.." +dump="${1:?usage: $0 }" +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 +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; } +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: secret/' "$dump" > /tmp/config.ldif +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 + +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 +stop + +echo "apply ldap/changes with slapmodify" +for f in ldap/changes/*.ldif; do + 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 +start + +echo "after changes" +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 secret -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'" +stop +echo; echo "$pass passed, $fail failed" +[ $fail -eq 0 ] diff --git a/tests/ldap/fixture-data.ldif b/tests/ldap/fixture-data.ldif new file mode 100644 index 0000000..afb6121 --- /dev/null +++ b/tests/ldap/fixture-data.ldif @@ -0,0 +1,76 @@ +# Fixture directory for tests/ldap/apply-changes.sh. Test-only accounts and +# passwords; nothing here exists on the real server. +dn: dc=metznet,dc=ca +objectClass: dcObject +objectClass: organization +o: MetzNet +dc: metznet + +dn: ou=accounts,dc=metznet,dc=ca +objectClass: organizationalUnit +ou: accounts + +dn: ou=system,ou=accounts,dc=metznet,dc=ca +objectClass: organizationalUnit +ou: system + +dn: ou=groups,dc=metznet,dc=ca +objectClass: organizationalUnit +ou: groups + +dn: uid=guix,ou=system,ou=accounts,dc=metznet,dc=ca +objectClass: account +objectClass: simpleSecurityObject +uid: guix +userPassword: guixpw + +dn: uid=kerberos,ou=system,ou=accounts,dc=metznet,dc=ca +objectClass: account +objectClass: simpleSecurityObject +uid: kerberos +userPassword: krbpw + +dn: uid=alice,ou=accounts,dc=metznet,dc=ca +objectClass: inetOrgPerson +objectClass: posixAccount +objectClass: ldapPublicKey +cn: alice +sn: alice +uid: alice +uidNumber: 10001 +gidNumber: 5001 +homeDirectory: /home/alice +userPassword: alicepw +sshPublicKey: ssh-ed25519 AAAAtestkey alice + +dn: uid=bob,ou=accounts,dc=metznet,dc=ca +objectClass: inetOrgPerson +objectClass: posixAccount +cn: bob +sn: bob +uid: bob +uidNumber: 10002 +gidNumber: 5001 +homeDirectory: /home/bob +userPassword: bobpw + +dn: cn=staff,ou=groups,dc=metznet,dc=ca +objectClass: groupOfNames +objectClass: posixGroupAux +cn: staff +gidNumber: 5001 +member: uid=alice,ou=accounts,dc=metznet,dc=ca +member: uid=bob,ou=accounts,dc=metznet,dc=ca + +dn: uid=carol,ou=accounts,dc=metznet,dc=ca +objectClass: inetOrgPerson +cn: carol +sn: carol +uid: carol + +dn: uid=dave,ou=accounts,dc=metznet,dc=ca +objectClass: inetOrgPerson +cn: dave +sn: dave +uid: dave +manager: uid=carol,ou=accounts,dc=metznet,dc=ca diff --git a/tests/ldap/generated-config.sh b/tests/ldap/generated-config.sh new file mode 100755 index 0000000..9492b18 --- /dev/null +++ b/tests/ldap/generated-config.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +# Build the cn=config and directory structure generated from Scheme +# (%ldap-slapd-config and %ldap-structure in metznet/machines/ldap.scm) into +# build/slapd-config.ldif and build/ldap-structure.ldif, check the structure +# loads under the config, then load the config into a scratch slapd with +# tests/ldap/fixture-data.ldif and check it behaves: logins, password +# changes, ACLs, overlays. Given a slapcat -n 0 dump, also +# checks the dump matches it exactly. +# +# Runs inside the guix builder container (it wipes /var/lib/slapd there): +# docker compose run --rm -T guix bash -lc 'tests/ldap/generated-config.sh [dump.ldif]' +# +# Then check the live server against it from the host: +# scripts/ldap-verify.sh -H ldaps://ldap.metznet.ca -D -y pwfile \ +# -b cn=config -X -e build/slapd-config.ldif +# scripts/ldap-verify.sh -H ldaps://ldap.metznet.ca -D -y pwfile \ +# -b dc=metznet,dc=ca -X -e build/ldap-structure.ldif +set -uo pipefail +cd "$(dirname "$0")/../.." +dump="${1:-}" + +LDIF=$(guix build --no-grafts -L . -e '(@ (metznet machines ldap) %ldap-slapd-config-ldif)' 2>/tmp/build.log | tail -1) +[[ -n "$LDIF" && -f "$LDIF" ]] || { tail -20 /tmp/build.log; exit 2; } +P=$(guix build --no-grafts -L . -e '(@ (metznet packages slapd) openldap-slapd)' 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 +STRUCTURE=$(guix build --no-grafts -L . -e '(@ (metznet machines ldap) %ldap-structure-ldif)' 2>/tmp/build.log | tail -1) +[[ -n "$STRUCTURE" && -f "$STRUCTURE" ]] || { tail -20 /tmp/build.log; exit 2; } +mkdir -p build +for f in "$LDIF:slapd-config.ldif" "$STRUCTURE:ldap-structure.ldif"; do + rm -f "build/${f#*:}" && cp "${f%%:*}" "build/${f#*:}" && chmod 644 "build/${f#*:}" +done +echo "generated: build/slapd-config.ldif build/ldap-structure.ldif" + +H=ldap://127.0.0.1:3890 +GUIX=uid=guix,ou=system,ou=accounts,dc=metznet,dc=ca +ALICE=uid=alice,ou=accounts,dc=metznet,dc=ca +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() { s -D "$1" -w "$2" -b "$3" -s base "(objectClass=*)" "$4" | grep -qi "^$4:"; } + +echo "generated LDIF" +if [[ -n "$dump" ]]; then + check "$dump matches it exactly" scripts/ldap-verify.sh -f "$dump" -X -e "$LDIF" +fi +check "groupOfNames patched: member optional" grep -q "NAME 'groupOfNames' DESC 'RFC2256: a group of names (DNs)' SUP top STRUCTURAL MUST ( cn ) MAY ( member \\$" "$LDIF" + +echo "load into slapd" +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 +check "slapadd -n 0" slapadd -n0 -F /var/lib/slapd/slapd.d -l "$LDIF" +check "slaptest" slaptest -F /var/lib/slapd/slapd.d -u +# the structure must load under the generated config (schema-valid, parents +# first) and come back out unchanged +check "slapadd structure" slapadd -n1 -F /var/lib/slapd/slapd.d -l "$STRUCTURE" +check "structure round-trips through slapcat" bash -c "slapcat -n1 -F /var/lib/slapd/slapd.d -o ldif-wrap=no > /tmp/structure-dump.ldif && scripts/ldap-verify.sh -f /tmp/structure-dump.ldif -X -e '$STRUCTURE'" +rm -rf /var/lib/slapd/data && mkdir -p /var/lib/slapd/data +check "slapadd fixture data" slapadd -n1 -F /var/lib/slapd/slapd.d -l tests/ldap/fixture-data.ldif -q +# slapadd checks schema, so this fails if groupOfNames still requires member +check "empty group allowed (patched groupOfNames)" bash -c "printf 'dn: cn=empty,ou=groups,dc=metznet,dc=ca\nobjectClass: groupOfNames\nobjectClass: posixGroupAux\ncn: empty\ngidNumber: 5999\n' | slapadd -n1 -F /var/lib/slapd/slapd.d" +slapd -F /var/lib/slapd/slapd.d -h "$H/" +for i in $(seq 20); do ldapwhoami -x -H $H -D $GUIX -w guixpw >/dev/null 2>&1 && break; sleep 0.5; done + +echo "behaviour" +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 "anonymous search refused (olcRequires authc)" not s -b dc=metznet,dc=ca -s base "(objectClass=*)" +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 "guix reads group members (sssd)" attr_visible $GUIX guixpw cn=staff,ou=groups,dc=metznet,dc=ca member +check "alice changes own password (passwd)" ldappasswd -x -H $H -D $ALICE -w alicepw -s alicepw2 +check "guix can NOT read cn=config" not attr_visible $GUIX guixpw cn=config cn +kill "$(cat /var/run/slapd/slapd.pid)" + +echo; echo "$pass passed, $fail failed" +[ $fail -eq 0 ] diff --git a/tests/ldap/schema.txt b/tests/ldap/schema.txt new file mode 100644 index 0000000..0487fa3 --- /dev/null +++ b/tests/ldap/schema.txt @@ -0,0 +1,29 @@ +# Schema that ldap.metznet.ca must have loaded, checked by +# scripts/ldap-verify.sh against the server's subschema entry. +# objectClass | attributeType + +# sssd (metznet/system/base-system.scm): ldap_schema rfc2307bis, +# ldap_group_object_class posixGroupAux, ldap_group_member member +objectClass posixAccount +objectClass posixGroupAux +attributeType uidNumber +attributeType gidNumber +attributeType homeDirectory +attributeType loginShell +attributeType member + +# MIT Kerberos LDAP backend (metznet/machines/kerberos.scm) +objectClass krbContainer +objectClass krbRealmContainer +objectClass krbPrincipal +objectClass krbPrincipalAux +attributeType krbPrincipalName + +# sshd AuthorizedKeysCommand (metznet-pubkey): openssh-lpk schema +objectClass ldapPublicKey +attributeType sshPublicKey + +# groups are groupOfNames + posixGroupAux (the groupaux schema), kept +# consistent by the memberof overlay +objectClass groupOfNames +attributeType memberOf