|
|
@@ -1,6 +1,8 @@
|
|
|
#!/bin/bash -e
|
|
|
|
|
|
FIRST_START_DONE="/etc/docker-openldap-first-start-done"
|
|
|
+WAS_STARTED_WITH_TLS="/etc/ldap/slapd.d/docker-openldap-was-started-with-tls"
|
|
|
+WAS_STARTED_WITH_REPLICATION="/etc/ldap/slapd.d/docker-openldap-was-started-with-replication"
|
|
|
|
|
|
# Reduce maximum number of number of open file descriptors to 1024
|
|
|
# otherwise slapd consumes two orders of magnitude more of RAM
|
|
|
@@ -8,13 +10,16 @@ FIRST_START_DONE="/etc/docker-openldap-first-start-done"
|
|
|
ulimit -n 1024
|
|
|
|
|
|
#fix file permissions
|
|
|
-chown -R openldap:openldap /var/lib/ldap
|
|
|
+chown -R openldap:openldap /var/lib/ldap
|
|
|
chown -R openldap:openldap /etc/ldap
|
|
|
+chown -R openldap:openldap /osixia/slapd
|
|
|
+
|
|
|
+/etc/init.d/ntp restart
|
|
|
|
|
|
# container first start
|
|
|
if [ ! -e "$FIRST_START_DONE" ]; then
|
|
|
|
|
|
- function get_base_dn(){
|
|
|
+ function get_base_dn() {
|
|
|
BASE_DN=""
|
|
|
IFS='.' read -ra BASE_DN_TABLE <<< "$LDAP_DOMAIN"
|
|
|
for i in "${BASE_DN_TABLE[@]}"; do
|
|
|
@@ -25,7 +30,7 @@ if [ ! -e "$FIRST_START_DONE" ]; then
|
|
|
BASE_DN=${BASE_DN::-1}
|
|
|
}
|
|
|
|
|
|
- function is_new_schema(){
|
|
|
+ function is_new_schema() {
|
|
|
local COUNT=$(ldapsearch -Q -Y EXTERNAL -H ldapi:/// -b cn=schema,cn=config cn | grep -c $1)
|
|
|
if [ "$COUNT" -eq 0 ]; then
|
|
|
echo 1
|
|
|
@@ -34,8 +39,31 @@ if [ ! -e "$FIRST_START_DONE" ]; then
|
|
|
fi
|
|
|
}
|
|
|
|
|
|
- # database is uninitialized
|
|
|
- if [ -z "$(ls -A /var/lib/ldap)" ]; then
|
|
|
+ function check_tls_files() {
|
|
|
+
|
|
|
+ local CA_CRT=$1
|
|
|
+ local LDAP_CRT=$2
|
|
|
+ local LDAP_KEY=$3
|
|
|
+
|
|
|
+ # check certificat and key or create it
|
|
|
+ /sbin/ssl-kit "/osixia/slapd/assets/ssl/$LDAP_CRT" "/osixia/slapd/assets/ssl/$LDAP_KEY" --ca-crt=/osixia/slapd/assets/ssl/$CA_CRT --gnutls
|
|
|
+
|
|
|
+ # create DHParamFile if not found
|
|
|
+ [ -f /osixia/slapd/assets/ssl/dhparam.pem ] || openssl dhparam -out /osixia/slapd/assets/ssl/dhparam.pem 2048
|
|
|
+
|
|
|
+ # fix file permissions
|
|
|
+ chown -R openldap:openldap /osixia/slapd
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ BOOTSTRAP=false
|
|
|
+
|
|
|
+ # database and config directory are empty -> set bootstrap config
|
|
|
+ if [ -z "$(ls -A /var/lib/ldap)" ] && [ -z "$(ls -A /etc/ldap/slapd.d)" ]; then
|
|
|
+
|
|
|
+ BOOTSTRAP=true
|
|
|
+ echo "database and config directory are empty"
|
|
|
+ echo "-> set bootstrap config"
|
|
|
|
|
|
cat <<EOF | debconf-set-selections
|
|
|
slapd slapd/internal/generated_adminpw password ${LDAP_ADMIN_PASSWORD}
|
|
|
@@ -55,78 +83,160 @@ EOF
|
|
|
|
|
|
dpkg-reconfigure -f noninteractive slapd
|
|
|
|
|
|
- # start OpenLDAP
|
|
|
- slapd -h "ldapi:///" -u openldap -g openldap
|
|
|
-
|
|
|
- get_base_dn
|
|
|
- sed -i "s|dc=example,dc=org|$BASE_DN|g" /osixia/slapd/security.ldif
|
|
|
-
|
|
|
- ldapmodify -Y EXTERNAL -Q -H ldapi:/// -f /osixia/slapd/security.ldif
|
|
|
+ elif [ -z "$(ls -A /var/lib/ldap)" ] && [ ! -z "$(ls -A /etc/ldap/slapd.d)" ]; then
|
|
|
+ echo "Error: the database directory (/var/lib/ldap) is empty but not the config directory (/etc/ldap/slapd.d)"
|
|
|
+ exit 1
|
|
|
+ elif [ ! -z "$(ls -A /var/lib/ldap)" ] && [ -z "$(ls -A /etc/ldap/slapd.d)" ]; then
|
|
|
+ echo "the config directory (/etc/ldap/slapd.d) is empty but not the database directory (/var/lib/ldap)"
|
|
|
+ exit 1
|
|
|
|
|
|
else
|
|
|
+ # there is an existing database and config
|
|
|
|
|
|
- # start OpenLDAP
|
|
|
- slapd -h "ldapi:///" -u openldap -g openldap
|
|
|
+ # if the config was bootstraped with TLS
|
|
|
+ # to avoid error (#6) we check tls files
|
|
|
+ if [ -e "$WAS_STARTED_WITH_TLS" ]; then
|
|
|
|
|
|
+ . $WAS_STARTED_WITH_TLS
|
|
|
+
|
|
|
+ check_tls_files $PREVIOUS_SSL_CA_CRT_FILENAME $PREVIOUS_SSL_CRT_FILENAME $PREVIOUS_SSL_KEY_FILENAME
|
|
|
+ fi
|
|
|
fi
|
|
|
|
|
|
+ # start OpenLDAP
|
|
|
+ echo "Starting openldap..."
|
|
|
+ slapd -h "ldapi:///" -u openldap -g openldap
|
|
|
+ echo "ok"
|
|
|
+
|
|
|
+ # set bootstrap config part 2
|
|
|
+ if $BOOTSTRAP; then
|
|
|
+
|
|
|
+ # add ppolicy schema if not already exists
|
|
|
+ ADD_PPOLICY=$(is_new_schema ppolicy)
|
|
|
+ if [ "$ADD_PPOLICY" -eq 1 ]; then
|
|
|
+ ldapadd -c -Y EXTERNAL -Q -H ldapi:/// -f /etc/ldap/schema/ppolicy.ldif
|
|
|
+ fi
|
|
|
+
|
|
|
+ # convert schemas to ldif
|
|
|
+ SCHEMAS=""
|
|
|
+ for f in $(find /osixia/slapd/assets/config/bootstrap/schema -name \*.schema -type f); do
|
|
|
+ SCHEMAS="$SCHEMAS ${f}"
|
|
|
+ done
|
|
|
+ /osixia/slapd/assets/schema-to-ldif.sh "$SCHEMAS"
|
|
|
+
|
|
|
+ # add schemas
|
|
|
+ for f in $(find /osixia/slapd/assets/config/bootstrap/schema -name \*.ldif -type f); do
|
|
|
+ echo "Processing file ${f}"
|
|
|
+ # add schema if not already exists
|
|
|
+ SCHEMA=$(basename "${f}" .ldif)
|
|
|
+ ADD_SCHEMA=$(is_new_schema $SCHEMA)
|
|
|
+ if [ "$ADD_SCHEMA" -eq 1 ]; then
|
|
|
+ echo "add schema ${SCHEMA}"
|
|
|
+ ldapadd -c -Y EXTERNAL -Q -H ldapi:/// -f $f
|
|
|
+ else
|
|
|
+ echo "schema ${f} already exists"
|
|
|
+ fi
|
|
|
+ done
|
|
|
+
|
|
|
+ # set config password
|
|
|
+ CONFIG_PASSWORD_ENCRYPTED=$(slappasswd -s $LDAP_CONFIG_PASSWORD)
|
|
|
+ sed -i "s|{{ CONFIG_PASSWORD_ENCRYPTED }}|$CONFIG_PASSWORD_ENCRYPTED|g" /osixia/slapd/assets/config/bootstrap/ldif/config-password.ldif
|
|
|
+
|
|
|
+ # adapt security config file
|
|
|
+ get_base_dn
|
|
|
+ sed -i "s|dc=example,dc=org|$BASE_DN|g" /osixia/slapd/assets/config/bootstrap/ldif/security.ldif
|
|
|
+
|
|
|
+ # process config files
|
|
|
+ for f in $(find /osixia/slapd/assets/config/bootstrap/ldif -name \*.ldif -type f); do
|
|
|
+ echo "Processing file ${f}"
|
|
|
+ ldapmodify -Y EXTERNAL -Q -H ldapi:/// -f $f
|
|
|
+ done
|
|
|
+
|
|
|
+ fi
|
|
|
|
|
|
# TLS config
|
|
|
if [ "${USE_TLS,,}" == "true" ]; then
|
|
|
|
|
|
- # check certificat and key or create it
|
|
|
- /sbin/ssl-kit "/osixia/slapd/ssl/$SSL_CRT_FILENAME" "/osixia/slapd/ssl/$SSL_KEY_FILENAME" --ca-crt=/osixia/slapd/ssl/$SSL_CA_CRT_FILENAME --gnutls
|
|
|
+ echo "Use TLS"
|
|
|
|
|
|
- # create DHParamFile if not found
|
|
|
- [ -f /osixia/slapd/ssl/dhparam.pem ] || openssl dhparam -out /osixia/slapd/ssl/dhparam.pem 2048
|
|
|
+ check_tls_files $SSL_CA_CRT_FILENAME $SSL_CRT_FILENAME $SSL_KEY_FILENAME
|
|
|
|
|
|
# adapt tls ldif
|
|
|
- sed -i "s,/osixia/slapd/ssl/ca.crt,/osixia/slapd/ssl/${SSL_CA_CRT_FILENAME},g" /osixia/slapd/tls.ldif
|
|
|
- sed -i "s,/osixia/slapd/ssl/ldap.crt,/osixia/slapd/ssl/${SSL_CRT_FILENAME},g" /osixia/slapd/tls.ldif
|
|
|
- sed -i "s,/osixia/slapd/ssl/ldap.key,/osixia/slapd/ssl/${SSL_KEY_FILENAME},g" /osixia/slapd/tls.ldif
|
|
|
+ sed -i "s,/osixia/slapd/assets/ssl/ca.crt,/osixia/slapd/assets/ssl/${SSL_CA_CRT_FILENAME},g" /osixia/slapd/assets/config/tls/tls-enable.ldif
|
|
|
+ sed -i "s,/osixia/slapd/assets/ssl/ldap.crt,/osixia/slapd/assets/ssl/${SSL_CRT_FILENAME},g" /osixia/slapd/assets/config/tls/tls-enable.ldif
|
|
|
+ sed -i "s,/osixia/slapd/assets/ssl/ldap.key,/osixia/slapd/assets/ssl/${SSL_KEY_FILENAME},g" /osixia/slapd/assets/config/tls/tls-enable.ldif
|
|
|
|
|
|
- ldapmodify -Y EXTERNAL -Q -H ldapi:/// -f /osixia/slapd/tls.ldif
|
|
|
+ ldapmodify -Y EXTERNAL -Q -H ldapi:/// -f /osixia/slapd/assets/config/tls/tls-enable.ldif
|
|
|
|
|
|
- # add localhost route to certificate cn (need docker 1.5.0)
|
|
|
- cn=$(openssl x509 -in /osixia/slapd/ssl/$SSL_CRT_FILENAME -subject -noout | sed -n 's/.*CN=\(.*\)\/*\(.*\)/\1/p')
|
|
|
- echo "127.0.0.1 $cn" >> /etc/hosts
|
|
|
+ [[ -f "$WAS_STARTED_WITH_TLS" ]] && rm -f "$WAS_STARTED_WITH_TLS"
|
|
|
+ touch $WAS_STARTED_WITH_TLS
|
|
|
+ echo "export PREVIOUS_SSL_CA_CRT_FILENAME=${SSL_CA_CRT_FILENAME}" >> $WAS_STARTED_WITH_TLS
|
|
|
+ echo "export PREVIOUS_SSL_CRT_FILENAME=${SSL_CRT_FILENAME}" >> $WAS_STARTED_WITH_TLS
|
|
|
+ echo "export PREVIOUS_SSL_KEY_FILENAME=${SSL_KEY_FILENAME}" >> $WAS_STARTED_WITH_TLS
|
|
|
+ chmod +x $WAS_STARTED_WITH_TLS
|
|
|
|
|
|
- # local ldap tls client config
|
|
|
- sed -i "s,TLS_CACERT.*,TLS_CACERT /osixia/slapd/ssl/${SSL_CA_CRT_FILENAME},g" /etc/ldap/ldap.conf
|
|
|
- fi
|
|
|
+ # ldap client config
|
|
|
+ sed -i "s,TLS_CACERT.*,TLS_CACERT /osixia/slapd/assets/ssl/${SSL_CA_CRT_FILENAME},g" /etc/ldap/ldap.conf
|
|
|
+ echo "TLS_REQCERT demand" >> /etc/ldap/ldap.conf
|
|
|
+
|
|
|
+ [[ -f "$HOME/.ldaprc" ]] && rm -f $HOME/.ldaprc
|
|
|
+ touch $HOME/.ldaprc
|
|
|
+ echo "TLS_CERT /osixia/slapd/assets/ssl/${SSL_CRT_FILENAME}" >> $HOME/.ldaprc
|
|
|
+ echo "TLS_KEY /osixia/slapd/assets/ssl/${SSL_KEY_FILENAME}" >> $HOME/.ldaprc
|
|
|
+
|
|
|
+ else
|
|
|
+
|
|
|
+ echo "Don't use TLS"
|
|
|
+
|
|
|
+ [[ -f "$WAS_STARTED_WITH_TLS" ]] && rm -f "$WAS_STARTED_WITH_TLS"
|
|
|
+ ldapmodify -c -Y EXTERNAL -Q -H ldapi:/// -f /osixia/slapd/assets/config/tls/tls-disable.ldif || true
|
|
|
|
|
|
- # add ppolicy schema if not already exists
|
|
|
- ADD_PPOLICY=$(is_new_schema ppolicy)
|
|
|
- if [ "$ADD_PPOLICY" -eq 1 ]; then
|
|
|
- ldapadd -c -Y EXTERNAL -Q -H ldapi:/// -f /etc/ldap/schema/ppolicy.ldif
|
|
|
fi
|
|
|
|
|
|
- # convert schemas to ldif
|
|
|
- SCHEMAS=""
|
|
|
- for f in $(find /osixia/slapd/schema -name \*.schema -type f); do
|
|
|
- SCHEMAS="$SCHEMAS ${f}"
|
|
|
- done
|
|
|
- /osixia/slapd/schema-to-ldif.sh "$SCHEMAS"
|
|
|
-
|
|
|
- for f in $(find /osixia/slapd/schema -name \*.ldif -type f); do
|
|
|
- echo "Processing file ${f}"
|
|
|
- # add schema if not already exists
|
|
|
- SCHEMA=$(basename "${f}" .ldif)
|
|
|
- ADD_SCHEMA=$(is_new_schema $SCHEMA)
|
|
|
- if [ "$ADD_SCHEMA" -eq 1 ]; then
|
|
|
- echo "add schema ${SCHEMA}"
|
|
|
- ldapadd -c -Y EXTERNAL -Q -H ldapi:/// -f $f
|
|
|
- else
|
|
|
- echo "schema ${f} already exists"
|
|
|
- fi
|
|
|
|
|
|
- done
|
|
|
+ # replication config
|
|
|
+ if [ "${USE_REPLICATION,,}" == "true" ]; then
|
|
|
+
|
|
|
+ echo "Use replication"
|
|
|
+
|
|
|
+ # copy template file
|
|
|
+ cp /osixia/slapd/assets/config/replication/replication-enable-template.ldif /osixia/slapd/assets/config/replication/replication-enable.ldif
|
|
|
+
|
|
|
+ REPLICATION_HOSTS=($REPLICATION_HOSTS)
|
|
|
+ i=1
|
|
|
+ for host in "${REPLICATION_HOSTS[@]}"
|
|
|
+ do
|
|
|
+
|
|
|
+ #host var contain a variable name, we access to the variable value and cast it to a table
|
|
|
+ host=${!host}
|
|
|
+
|
|
|
+ sed -i "s|{{ REPLICATION_HOSTS }}|olcServerID: $i ${host}\n{{ REPLICATION_HOSTS }}|g" /osixia/slapd/assets/config/replication/replication-enable.ldif
|
|
|
+ sed -i "s|{{ REPLICATION_HOSTS_CONFIG_SYNC_REPL }}|olcSyncRepl: rid=00$i provider=${host} ${REPLICATION_CONFIG_SYNCPROV}\n{{ REPLICATION_HOSTS_CONFIG_SYNC_REPL }}|g" /osixia/slapd/assets/config/replication/replication-enable.ldif
|
|
|
+ sed -i "s|{{ REPLICATION_HOSTS_HDB_SYNC_REPL }}|olcSyncRepl: rid=10$i provider=${host} ${REPLICATION_HDB_SYNCPROV}\n{{ REPLICATION_HOSTS_HDB_SYNC_REPL }}|g" /osixia/slapd/assets/config/replication/replication-enable.ldif
|
|
|
+
|
|
|
+ ((i++))
|
|
|
+ done
|
|
|
+
|
|
|
+ get_base_dn
|
|
|
+ sed -i "s|\$BASE_DN|$BASE_DN|g" /osixia/slapd/assets/config/replication/replication-enable.ldif
|
|
|
+ sed -i "s|\$LDAP_ADMIN_PASSWORD|$LDAP_ADMIN_PASSWORD|g" /osixia/slapd/assets/config/replication/replication-enable.ldif
|
|
|
+ sed -i "s|\$LDAP_CONFIG_PASSWORD|$LDAP_CONFIG_PASSWORD|g" /osixia/slapd/assets/config/replication/replication-enable.ldif
|
|
|
+
|
|
|
+ sed -i "/{{ REPLICATION_HOSTS }}/d" /osixia/slapd/assets/config/replication/replication-enable.ldif
|
|
|
+ sed -i "/{{ REPLICATION_HOSTS_CONFIG_SYNC_REPL }}/d" /osixia/slapd/assets/config/replication/replication-enable.ldif
|
|
|
+ sed -i "/{{ REPLICATION_HOSTS_HDB_SYNC_REPL }}/d" /osixia/slapd/assets/config/replication/replication-enable.ldif
|
|
|
+
|
|
|
+ ldapmodify -c -Y EXTERNAL -Q -H ldapi:/// -f /osixia/slapd/assets/config/replication/replication-enable.ldif
|
|
|
+ touch $WAS_STARTED_WITH_REPLICATION
|
|
|
+
|
|
|
+ else
|
|
|
+
|
|
|
+ echo "Don't use replication"
|
|
|
+ [[ -f "$WAS_STARTED_WITH_REPLICATION" ]] && rm -f "$WAS_STARTED_WITH_REPLICATION"
|
|
|
+ ldapmodify -c -Y EXTERNAL -Q -H ldapi:/// -f /osixia/slapd/assets/config/replication/replication-disable.ldif || true
|
|
|
+
|
|
|
+ fi
|
|
|
|
|
|
- # OpenLDAP config
|
|
|
- for f in $(find /osixia/slapd/config -name \*.ldif -type f); do
|
|
|
- echo "Processing file ${f}"
|
|
|
- ldapmodify -Y EXTERNAL -Q -H ldapi:/// -f $f
|
|
|
- done
|
|
|
|
|
|
# stop OpenLDAP
|
|
|
kill -INT `cat /run/slapd/slapd.pid`
|
|
|
@@ -134,7 +244,4 @@ EOF
|
|
|
touch $FIRST_START_DONE
|
|
|
fi
|
|
|
|
|
|
-# fix file permissions
|
|
|
-chown openldap:openldap -R /osixia/slapd
|
|
|
-
|
|
|
exit 0
|