entrypoint.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/sh
  2. # The setup follows the OpenLDAP admin guide on
  3. # http://www.openldap.org/doc/admin24/guide.html
  4. # When not limiting the open file descriptors, the memory consumption
  5. # of slapd is absurdly high. See https://github.com/docker/docker/issues/8231
  6. ulimit -n 8192
  7. # If there's no cn=config database, initialize one.
  8. # Take the original slapd.ldif file as template.
  9. if [ ! -d '/etc/openldap/slapd.d/cn=config' ]; then
  10. # Create the run directory
  11. if [ ! -d /var/lib/openldap/run ]; then
  12. mkdir -p /var/lib/openldap/run
  13. chown -R ${USER}:${GROUP} /var/lib/openldap/run
  14. fi
  15. # Use the original slapd.conf file as template
  16. cp /etc/openldap/slapd.ldif /tmp/slapd.ldif
  17. # Set the correct suffix
  18. # https://www.openldap.org/doc/admin24/quickstart.html
  19. # Point 8ff
  20. sed -i "s/dc=my-domain,dc=com/${SUFFIX}/g" /tmp/slapd.ldif
  21. # Set the root password for the database
  22. sed -i "s|olcRootPW: secret|olcRootPW: ${LDAP_ROOT_HASH}|" /tmp/slapd.ldif
  23. # Remove those newlines, or slapadd will fail to initialize the DB.
  24. sed -i '/^olcRootDN/ {n; s/^[[:blank:]]*$/#/}' /tmp/slapd.ldif
  25. sed -i '/^olcRootPW/ {n; s/^[[:blank:]]*$/#/}' /tmp/slapd.ldif
  26. sed -i '/^olcDbDirectory/ {n; s/^[[:blank:]]*$/#/}' /tmp/slapd.ldif
  27. # Set up authentication patterns for -Y EXTERNAL over ldapi:/// (SASL).
  28. #sed -i "/^olcSuffix: ${SUFFIX}/a olcAccess: to * by * read" /tmp/slapd.ldif
  29. #sed -i "/^olcSuffix: ${SUFFIX}/a olcAccess: to attrs=userPassword by self write by anonymous auth by * none" /tmp/slapd.ldif
  30. #sed -i "/^olcSuffix: ${SUFFIX}/a olcAccess: to * by dn.exact=gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth manage by * break" /tmp/slapd.ldif
  31. # Add more schemas
  32. sed -i "/core.ldif/a include: file:///etc/openldap/schema/cosine.ldif" /tmp/slapd.ldif
  33. sed -i "/cosine.ldif/a include: file:///etc/openldap/schema/inetorgperson.ldif" /tmp/slapd.ldif
  34. # Create config admin to allow binding to config database.
  35. # https://www.openldap.org/doc/admin24/slapdconf2.html#Configuration%20Example
  36. # Lines 21ff
  37. sed -i "/^############/d" /tmp/slapd.ldif
  38. sed -i "/^# LMDB/i \
  39. # Config settings\n\
  40. #\n\
  41. dn: olcDatabase=config,cn=config\n\
  42. objectClass: olcDatabaseConfig\n\
  43. olcDatabase: config\n\
  44. # Don't allow access to cn=config with password, only over SASL via -Y EXTERNAL ldapi:///
  45. #olcRootPW: ${LDAP_ADMIN_HASH}\n\
  46. olcAccess: to * by dn.exact=gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth manage by * break\n\
  47. olcAccess: to * by * none\n\
  48. " /tmp/slapd.ldif
  49. # Generate config database from slapd.conf file.
  50. echo Generating configuration
  51. slapadd -n 0 -F /etc/openldap/slapd.d -l /tmp/slapd.ldif
  52. # Copy the generated slapd.ldif to the ldifs mount.
  53. cp /tmp/slapd.ldif /var/ldifs
  54. # if [ -f /etc/ssl/certs/sourceme ]; then
  55. # cp /var/ldifs/tls.ldif /tmp/tls.ldif
  56. # source /etc/ssl/certs/sourceme
  57. # sed -i -e "s#{{CA_CERT}}#${CA_CERT}#g" /tmp/tls.ldif
  58. # sed -i -e "s#{{LDAP_TLS_CERT}}#${LDAP_TLS_CERT}#g" /tmp/tls.ldif
  59. # sed -i -e "s#{{LDAP_TLS_KEY}}#${LDAP_TLS_KEY}#g" /tmp/tls.ldif
  60. # echo Adding TLS certificates to configuration
  61. # slapadd -n 0 -F /etc/openldap/slapd.d -l /tmp/tls.ldif
  62. # fi
  63. # Set all ownerships straight.
  64. chown -R ${USER}:${GROUP} /etc/openldap/slapd.d
  65. fi
  66. echo Starting slapd
  67. exec /usr/sbin/slapd -u ${USER} -g ${GROUP} -d ${DEBUG} -h "ldap:/// ldapi:///" -F /etc/openldap/slapd.d