(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))))))