310 lines
14 KiB
Scheme
310 lines
14 KiB
Scheme
(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 <file>`.
|
|
;;;
|
|
;;; Nothing here is secret: olcRootPW is deliberately not representable.
|
|
;;;
|
|
;;; Code:
|
|
|
|
;; A schema loaded as cn={N}<name>,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> 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> 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> 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> 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> 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> slapd-refint-overlay
|
|
make-slapd-refint-overlay
|
|
slapd-refint-overlay?
|
|
(attributes slapd-refint-overlay-attributes))
|
|
|
|
(define-record-type* <slapd-totp-overlay> slapd-totp-overlay
|
|
make-slapd-totp-overlay
|
|
slapd-totp-overlay?)
|
|
|
|
(define-record-type* <slapd-ppolicy-overlay> 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> 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
|
|
(($ <slapd-memberof-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)))
|
|
(($ <slapd-refint-overlay> attributes)
|
|
`(("objectClass" . ("olcConfig" "olcOverlayConfig" "olcRefintConfig"))
|
|
;; one value per attribute: slapd rejects space-separated lists in
|
|
;; future versions
|
|
("olcRefintAttribute" . ,attributes)))
|
|
(($ <slapd-totp-overlay>)
|
|
'(("objectClass" . "olcOverlayConfig")))
|
|
(($ <slapd-ppolicy-overlay> 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 <slapd-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 <slapd-config> (frontend config-database databases)
|
|
(string-append
|
|
(match-record frontend <slapd-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 <slapd-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 <slapd-mdb>
|
|
(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)))))))
|