slapd.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/sh
  2. set -eu
  3. status () {
  4. echo "---> ${@}" >&2
  5. }
  6. set -x
  7. : LDAP_ADMIN_PWD=${LDAP_ADMIN_PWD}
  8. : LDAP_DOMAIN=${LDAP_DOMAIN}
  9. : LDAP_ORGANISATION=${LDAP_ORGANISATION}
  10. ############ Base config ############
  11. if [ ! -e /var/lib/ldap/docker_bootstrapped ]; then
  12. status "configuring slapd database"
  13. cat <<EOF | debconf-set-selections
  14. slapd slapd/internal/generated_adminpw password ${LDAP_ADMIN_PWD}
  15. slapd slapd/internal/adminpw password ${LDAP_ADMIN_PWD}
  16. slapd slapd/password2 password ${LDAP_ADMIN_PWD}
  17. slapd slapd/password1 password ${LDAP_ADMIN_PWD}
  18. slapd slapd/dump_database_destdir string /var/backups/slapd-VERSION
  19. slapd slapd/domain string ${LDAP_DOMAIN}
  20. slapd shared/organization string ${LDAP_ORGANISATION}
  21. slapd slapd/backend string HDB
  22. slapd slapd/purge_database boolean true
  23. slapd slapd/move_old_database boolean true
  24. slapd slapd/allow_ldap_v2 boolean false
  25. slapd slapd/no_configuration boolean false
  26. slapd slapd/dump_database select when needed
  27. EOF
  28. dpkg-reconfigure -f noninteractive slapd
  29. touch /var/lib/ldap/docker_bootstrapped
  30. else
  31. status "slapd database found"
  32. fi
  33. ############ Custom config ############
  34. if [ ! -e /etc/ldap/config/docker_bootstrapped ]; then
  35. status "Custom config"
  36. slapd -h "ldapi:///" -u openldap -g openldap
  37. chown -R openldap:openldap /etc/ldap
  38. # TLS
  39. if [ -e /etc/ldap/ssl/ldap.crt ] && [ -e /etc/ldap/ssl/ldap.key ] && [ -e /etc/ldap/ssl/ca.crt ]; then
  40. status "certificates found"
  41. chmod 600 /etc/ldap/ssl/ldap.key
  42. # create DHParamFile if not found
  43. [ -f /etc/ldap/ssl/dhparam.pem ] || openssl dhparam -out /etc/ldap/ssl/dhparam.pem 2048
  44. ldapmodify -Y EXTERNAL -H ldapi:/// -f /etc/ldap/config/auto/tls.ldif -Q
  45. # add fake dnsmasq route to certificate cn
  46. cn=$(openssl x509 -in /etc/ldap/ssl/ldap.crt -subject -noout | sed -n 's/.*CN=\(.*\)\/*\(.*\)/\1/p')
  47. echo "127.0.0.1 " $cn >> /etc/dhosts
  48. fi
  49. # Replication
  50. # todo :)
  51. # Other config files
  52. for f in $(find /etc/ldap/config -maxdepth 1 -name \*.ldif -type f); do
  53. status "Processing file ${f}"
  54. ldapmodify -Y EXTERNAL -H ldapi:/// -f $f -Q
  55. done
  56. kill -INT `cat /run/slapd/slapd.pid`
  57. touch /etc/ldap/config/docker_bootstrapped
  58. else
  59. status "found already-configured slapd"
  60. fi
  61. status "starting slapd on default port 389"
  62. set -x
  63. exec /usr/sbin/slapd -h "ldap:///" -u openldap -g openldap -d -1