From 48c149b4a2363cf79840f09f66857471ae2edbd1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Oliver=20Schl=C3=BCter?=
 <10252511+oschlueter@users.noreply.github.com>
Date: Wed, 11 Aug 2021 11:40:28 +0200
Subject: [PATCH] enable passing FQDN via environment variable

---
 example/docker-compose.yml     | 6 ++++--
 image/service/slapd/process.sh | 8 +++++---
 image/service/slapd/startup.sh | 6 ++++--
 3 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/example/docker-compose.yml b/example/docker-compose.yml
index 04a810a..43802ec 100644
--- a/example/docker-compose.yml
+++ b/example/docker-compose.yml
@@ -4,6 +4,7 @@ services:
     image: osixia/openldap:1.5.0
     container_name: openldap
     environment:
+      #FQDN: "ldap-server.example.org"
       LDAP_LOG_LEVEL: "256"
       LDAP_ORGANISATION: "Example Inc."
       LDAP_DOMAIN: "example.org"
@@ -39,9 +40,10 @@ services:
     ports:
       - "389:389"
       - "636:636"
-    # For replication to work correctly, domainname and hostname must be
+    # For replication to work correctly, either domainname and hostname must be
     # set correctly so that "hostname"."domainname" equates to the
-    # fully-qualified domain name for the host.
+    # fully-qualified domain name for the host
+    # or the FQDN is provided directly as environment variable (see above).
     domainname: "example.org"
     hostname: "ldap-server"
   phpldapadmin:
diff --git a/image/service/slapd/process.sh b/image/service/slapd/process.sh
index a669300..4e7738b 100755
--- a/image/service/slapd/process.sh
+++ b/image/service/slapd/process.sh
@@ -9,8 +9,10 @@ log-helper level eq trace && set -x
 # see https://github.com/docker/docker/issues/8231
 ulimit -n $LDAP_NOFILE
 
-# Call hostname to determine the fully qualified domain name. We want OpenLDAP to listen
-# to the named host for the ldap:// and ldaps:// protocols.
-FQDN="$(/bin/hostname --fqdn)"
+# We want OpenLDAP to listen to the named host for the ldap:// and ldaps:// protocols.
+if [ -z "$FQDN" ]; then
+  # Only call hostname if the fully qualified domain name wasn't provided as environment variable.
+  FQDN="$(/bin/hostname --fqdn)"
+fi
 HOST_PARAM="ldap://$FQDN:$LDAP_PORT ldaps://$FQDN:$LDAPS_PORT"
 exec /usr/sbin/slapd -h "$HOST_PARAM ldapi:///" -u openldap -g openldap -d "$LDAP_LOG_LEVEL"
diff --git a/image/service/slapd/startup.sh b/image/service/slapd/startup.sh
index dae1bd2..700699e 100755
--- a/image/service/slapd/startup.sh
+++ b/image/service/slapd/startup.sh
@@ -567,8 +567,10 @@ ln -sf ${CONTAINER_SERVICE_DIR}/slapd/assets/ldap.conf /etc/ldap/ldap.conf
 # force OpenLDAP to listen on all interfaces
 # We need to make sure that /etc/hosts continues to include the
 # fully-qualified domain name and not just the specified hostname.
-# Without the FQDN, /bin/hostname --fqdn stops working.
-FQDN="$(/bin/hostname --fqdn)"
+if [ -z "$FQDN" ]; then
+  # Only call hostname if the fully qualified domain name wasn't provided as environment variable.
+  FQDN="$(/bin/hostname --fqdn)"
+fi
 if [ "$FQDN" != "$HOSTNAME" ]; then
     FQDN_PARAM="$FQDN"
 else