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