ldap.scm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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
  58. (operating-system os)
  59. (memory-size 1024)))
  60. (define test
  61. (with-imported-modules '((gnu build marionette))
  62. #~(begin
  63. (use-modules (srfi srfi-11) (srfi srfi-64)
  64. (gnu build marionette))
  65. (define marionette
  66. (make-marionette (list #$vm)))
  67. (test-runner-current (system-test-runner #$output))
  68. (test-begin "ldap")
  69. ;; Set up LDAP directory server
  70. (test-assert "LDAP server instance running"
  71. (marionette-eval
  72. '(begin
  73. (with-output-to-file "instance.inf"
  74. (lambda ()
  75. (display "[general]
  76. config_version = 2
  77. \n[slapd]
  78. root_password = SECRET_PASS
  79. user = root
  80. group = root
  81. \n[backend-userroot]
  82. sample_entries = yes
  83. suffix = dc=example,dc=com")))
  84. (and
  85. ;; Create instance
  86. (zero? (system* #$(file-append 389-ds-base "/sbin/dscreate")
  87. "-v" "from-file" "instance.inf"))
  88. ;; Start instance
  89. (zero? (system* #$(file-append 389-ds-base "/sbin/dsctl")
  90. "localhost" "start"))
  91. ;; Create user account
  92. (zero? (system* #$(file-append 389-ds-base "/sbin/dsidm")
  93. "-b" "dc=example,dc=com"
  94. "localhost" "user" "create"
  95. "--uid" "eva" "--cn" "Eva Lu Ator"
  96. "--displayName" "Eva Lu Ator"
  97. "--uidNumber" "1234" "--gidNumber" "2345"
  98. "--homeDirectory" "/home/eva"))))
  99. marionette))
  100. (test-assert "Manager can bind to LDAP server instance"
  101. (marionette-eval
  102. '(zero? (system* #$(file-append openldap "/bin/ldapwhoami")
  103. "-H" "ldap://localhost" "-D"
  104. "cn=Directory Manager" "-w" "SECRET_PASS"))
  105. marionette))
  106. ;; Wait for nslcd to be up and running.
  107. (test-assert "nslcd service running"
  108. (marionette-eval
  109. '(begin
  110. (use-modules (gnu services herd))
  111. (match (start-service 'nslcd)
  112. (#f #f)
  113. (('service response-parts ...)
  114. (match (assq-ref response-parts 'running)
  115. ((pid) (number? pid))))))
  116. marionette))
  117. (test-assert "nslcd produces a log file"
  118. (marionette-eval
  119. '(file-exists? "/var/log/nslcd")
  120. marionette))
  121. (test-assert "Can query LDAP user accounts"
  122. (marionette-eval
  123. '(begin
  124. ;; TODO: This shouldn't be necessary, but unfortunately it
  125. ;; really is needed to discover LDAP accounts with "id".
  126. (setenv "LD_LIBRARY_PATH"
  127. #$(file-append nss-pam-ldapd "/lib"))
  128. (zero? (system* #$(file-append coreutils "/bin/id") "eva")))
  129. marionette))
  130. (test-assert "Can become LDAP user"
  131. (marionette-eval
  132. '(zero? (system* "/run/setuid-programs/su" "eva" "-c"
  133. #$(file-append coreutils "/bin/true")))
  134. marionette))
  135. (test-end))))
  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))))