ldap.scm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu tests ldap)
  19. #:use-module (gnu tests)
  20. #:use-module (gnu system)
  21. #:use-module (gnu system nss)
  22. #:use-module (gnu system vm)
  23. #:use-module (gnu services)
  24. #:use-module (gnu services authentication)
  25. #:use-module (gnu services networking)
  26. #:use-module (gnu packages base)
  27. #:use-module (gnu packages openldap)
  28. #:use-module (guix gexp)
  29. #:use-module (guix store)
  30. #:export (%test-ldap))
  31. (define %ldap-os
  32. (let ((simple
  33. (simple-operating-system
  34. (service dhcp-client-service-type)
  35. (service nslcd-service-type))))
  36. (operating-system
  37. (inherit simple)
  38. (name-service-switch
  39. (let ((services (list (name-service (name "db"))
  40. (name-service (name "files"))
  41. (name-service (name "ldap")))))
  42. (name-service-switch
  43. (inherit %mdns-host-lookup-nss)
  44. (password services)
  45. (shadow services)
  46. (group services)
  47. (netgroup services)
  48. (gshadow services)))))))
  49. (define (run-ldap-test)
  50. "Run tests in %LDAP-OS."
  51. (define os
  52. (marionette-operating-system
  53. %ldap-os
  54. #:imported-modules '((gnu services herd)
  55. (guix combinators))))
  56. (define vm
  57. (virtual-machine os))
  58. (define test
  59. (with-imported-modules '((gnu build marionette))
  60. #~(begin
  61. (use-modules (srfi srfi-11) (srfi srfi-64)
  62. (gnu build marionette))
  63. (define marionette
  64. (make-marionette (list #$vm)))
  65. (mkdir #$output)
  66. (chdir #$output)
  67. (test-begin "ldap")
  68. ;; Set up LDAP directory server
  69. (test-assert "LDAP server instance running"
  70. (marionette-eval
  71. '(begin
  72. (with-output-to-file "instance.inf"
  73. (lambda ()
  74. (display "[general]
  75. config_version = 2
  76. \n[slapd]
  77. root_password = SECRET_PASS
  78. user = root
  79. group = root
  80. \n[backend-userroot]
  81. sample_entries = yes
  82. suffix = dc=example,dc=com")))
  83. (and
  84. ;; Create instance
  85. (zero? (system* #$(file-append 389-ds-base "/sbin/dscreate")
  86. "-v" "from-file" "instance.inf"))
  87. ;; Start instance
  88. (zero? (system* #$(file-append 389-ds-base "/sbin/dsctl")
  89. "localhost" "start"))
  90. ;; Create user account
  91. (zero? (system* #$(file-append 389-ds-base "/sbin/dsidm")
  92. "-b" "dc=example,dc=com"
  93. "localhost" "user" "create"
  94. "--uid" "eva" "--cn" "Eva Lu Ator"
  95. "--displayName" "Eva Lu Ator"
  96. "--uidNumber" "1234" "--gidNumber" "2345"
  97. "--homeDirectory" "/home/eva"))))
  98. marionette))
  99. (test-assert "Manager can bind to LDAP server instance"
  100. (marionette-eval
  101. '(zero? (system* #$(file-append openldap "/bin/ldapwhoami")
  102. "-H" "ldap://localhost" "-D"
  103. "cn=Directory Manager" "-w" "SECRET_PASS"))
  104. marionette))
  105. ;; Wait for nslcd to be up and running.
  106. (test-assert "nslcd service running"
  107. (marionette-eval
  108. '(begin
  109. (use-modules (gnu services herd))
  110. (match (start-service 'nslcd)
  111. (#f #f)
  112. (('service response-parts ...)
  113. (match (assq-ref response-parts 'running)
  114. ((pid) (number? pid))))))
  115. marionette))
  116. (test-assert "nslcd produces a log file"
  117. (marionette-eval
  118. '(file-exists? "/var/log/nslcd")
  119. marionette))
  120. (test-assert "Can query LDAP user accounts"
  121. (marionette-eval
  122. '(begin
  123. ;; TODO: This shouldn't be necessary, but unfortunately it
  124. ;; really is needed to discover LDAP accounts with "id".
  125. (setenv "LD_LIBRARY_PATH"
  126. #$(file-append nss-pam-ldapd "/lib"))
  127. (zero? (system* #$(file-append coreutils "/bin/id") "eva")))
  128. marionette))
  129. (test-assert "Can become LDAP user"
  130. (marionette-eval
  131. '(zero? (system* "/run/setuid-programs/su" "eva" "-c"
  132. #$(file-append coreutils "/bin/true")))
  133. marionette))
  134. (test-end)
  135. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  136. (gexp->derivation "ldap-test" test))
  137. (define %test-ldap
  138. (system-test
  139. (name "ldap")
  140. (description "Run an LDAP directory server and authenticate against it.")
  141. (value (run-ldap-test))))