metznet-channel/metznet/machines/ldap.scm

306 lines
13 KiB
Scheme

(define-module (metznet machines ldap)
#:use-module (gnu)
#:use-module (gnu services certbot)
#:use-module (metznet system base-system)
#:use-module (metznet services certificates)
#:use-module (metznet services slapd)
#: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).
;; Deliberately no seed data here, matching that original -- the real
;; ldap.metznet.ca server already has a populated, live directory tree;
;; servers/ldap.scm's elaborate LDIF seeding (dc=metznet,dc=ca org,
;; ou=user/ou=group) doesn't match the ou=system,ou=accounts structure
;; the rest of this channel (sssd, kerberos.scm) actually binds against,
;; so it looks like a divergent prototype that was never really deployed.
(define-public ldap-services
(append (list (service certbot-service-type
(certbot-configuration
(email "admin@metznet.ca")
(certificates (list (certificate-configuration
(domains '("ldap.metznet.ca"))
;; slapd serves copies it owns
(deploy-hook
(certificate-copy-deploy-hook
"ldap.metznet.ca" "/var/lib/slapd" "slapd"
#:restart '(slapd))))))))
(service slapd-service-type
(slapd-configuration (uris "ldap:// ldaps://"))))
%metznet-aws-server-services))
;; ldap.metznet.ca's cn=config as Scheme: the single description of it. Not
;; wired into slapd-service-type yet -- the server still runs from its
;; on-disk slapd.d -- but kept equal to it: tests/ldap/generated-config.sh
;; builds build/slapd-config.ldif, and the live server is checked against
;; that with `ldap-verify.sh -b cn=config -X -e build/slapd-config.ldif`.
(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-aws-system)
(host-name "ldap.metznet.ca")
(services ldap-services)))
ldap.metznet.ca