entrypoint.sh 3.0 KB

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