entrypoint.sh 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. echo "Didn't find a config, creating a new one."
  11. SETUPDIR=/root/ldap-setup/
  12. DATETIME=$(date +%Y%m%d%H%M%S)
  13. ROOT_LDIF=${SETUPDIR}/slapd.ldif
  14. mkdir -p ${SETUPDIR}
  15. chmod 700 ${SETUPDIR}
  16. # Create the run directory
  17. if [ ! -d /var/lib/openldap/run ]; then
  18. mkdir -p /var/lib/openldap/run
  19. chown -R ${USER}:${GROUP} /var/lib/openldap/run
  20. fi
  21. # Create the data directory
  22. if [ ! -d /var/lib/openldap/openldap-data ]; then
  23. mkdir -p /var/lib/openldap/openldap-data
  24. chown -R ${USER}:${GROUP} /var/lib/openldap/openldap-data
  25. fi
  26. # Use the original slapd.conf file as template
  27. cp /etc/openldap/slapd.ldif ${ROOT_LDIF}
  28. # Set the correct suffix
  29. # https://www.openldap.org/doc/admin24/quickstart.html
  30. # Point 8ff
  31. sed -i "s/dc=my-domain,dc=com/${LDAPROOT}/g" ${ROOT_LDIF}
  32. # Set the root password for the database
  33. sed -i "s|olcRootPW: secret|olcRootPW: ${LDAP_ROOT_HASH}|" ${ROOT_LDIF}
  34. # Remove those newlines, or slapadd will fail to initialize the DB.
  35. sed -i '/^olcRootDN/ {n; s/^[[:blank:]]*$/#/}' ${ROOT_LDIF}
  36. sed -i '/^olcRootPW/ {n; s/^[[:blank:]]*$/#/}' ${ROOT_LDIF}
  37. sed -i '/^olcDbDirectory/ {n; s/^[[:blank:]]*$/#/}' ${ROOT_LDIF}
  38. # Set up authentication patterns for -Y EXTERNAL over ldapi:/// (SASL).
  39. sed -i "/^olcRootDN: cn=Manager,${LDAPROOT}/a olcAccess: to * by * read" ${ROOT_LDIF}
  40. sed -i "/^olcRootDN: cn=Manager,${LDAPROOT}/a olcAccess: to attrs=userPassword by self write by anonymous auth by * none" ${ROOT_LDIF}
  41. sed -i "/^olcRootDN: cn=Manager,${LDAPROOT}/a olcAccess: to * by dn.exact=gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth manage by * break" ${ROOT_LDIF}
  42. # Add more schemas
  43. sed -i "/core.ldif/a include: file:///etc/openldap/schema/cosine.ldif" ${ROOT_LDIF}
  44. sed -i "/cosine.ldif/a include: file:///etc/openldap/schema/inetorgperson.ldif" ${ROOT_LDIF}
  45. # Create config admin to allow binding to config database.
  46. # https://www.openldap.org/doc/admin24/slapdconf2.html#Configuration%20Example
  47. # Lines 21ff
  48. sed -i "/^############/d" ${ROOT_LDIF}
  49. sed -i "/^# LMDB/i \
  50. # Config settings\n\
  51. #\n\
  52. dn: olcDatabase=config,cn=config\n\
  53. objectClass: olcDatabaseConfig\n\
  54. olcDatabase: config\n\
  55. # Don't allow access to cn=config with password, only over SASL via -Y EXTERNAL ldapi:///\n\
  56. olcAccess: to * by dn.exact=gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth manage by * break\n\
  57. olcAccess: to * by * none\n\
  58. " ${ROOT_LDIF}
  59. # Generate config database from slapd.conf file.
  60. echo "Adding configuration to LDAP database."
  61. slapadd -n 0 -F /etc/openldap/slapd.d -l ${ROOT_LDIF}
  62. # Copy the generated slapd.ldif to the ldifs mount.
  63. cp ${ROOT_LDIF} /scripts
  64. # Set all ownerships straight.
  65. chown -R ${USER}:${GROUP} /etc/openldap/slapd.d
  66. fi
  67. echo Starting slapd
  68. exec /usr/sbin/slapd -u ${USER} -g ${GROUP} -d ${DEBUG} -h "ldaps:/// ldapi:///" -F /etc/openldap/slapd.d