1
1
Fork 0
mirror of https://github.com/docker-mailserver/docker-mailserver synced 2024-06-08 01:46:06 +02:00
docker-mailserver/target/scripts/helpers/dns.sh
Georg Lauterbach ab55343d8e
scripts: rework environment variables setup (#2716)
* outsourcing env variable setup

This commit contains major parts of the work of refactoring the setup
and usage of environment variables. It outsources the setup into its own
script and provides dedicated functions to be executed at a later point in time.

A **new** env variable was added: `USER_PROVISIONG` which provides a
better way of defining which method / protocol to use when it comes to
setting up users. This way, the `ENABLE_LDAP` variable is deprecated,
but all of this is backwards compatible due to a "compatibility layer", a function provided by the new variables script.

This is not a breaking change. It mostly refators internal scripts. The
only change facing the user-side is the deprecation of `ENABLE_LDAP`. We
can prolong the period of deprecation for this variable as long as we
want, because the new function that ensures backwards compatibility
provides a clean interface for the future.

Co-authored-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com>
Co-authored-by: Casper <casperklein@users.noreply.github.com>
2022-08-22 08:31:32 +02:00

66 lines
3.1 KiB
Bash

#! /bin/bash
# Outputs the DNS label count (delimited by `.`) for the given input string.
# Useful for determining an FQDN like `mail.example.com` (3), vs `example.com` (2).
function _get_label_count
{
awk -F '.' '{ print NF }' <<< "${1}"
}
# Sets HOSTNAME and DOMAINNAME globals used throughout the scripts,
# and any subprocesses called that intereact with it.
function _obtain_hostname_and_domainname
{
# Normally this value would match the output of `hostname` which mirrors `/proc/sys/kernel/hostname`,
# However for legacy reasons, the system ENV `HOSTNAME` was replaced here with `hostname -f` instead.
#
# TODO: Consider changing to `DMS_FQDN`; a more accurate name, and removing the `export`, assuming no
# subprocess like postconf would be called that would need access to the same value via `$HOSTNAME` ENV.
#
# ! There is already a stub in variables.sh which contains DMS_FQDN. One will just need to uncomment the
# ! correct lines in variables.sh.
#
# TODO: `OVERRIDE_HOSTNAME` was introduced for non-Docker runtimes that could not configure an explicit hostname.
# Kubernetes was the particular runtime in 2017. This does not update `/etc/hosts` or other locations, thus risking
# inconsistency with expected behaviour. Investigate if it's safe to remove support. (--net=host also uses this as a workaround)
export HOSTNAME="${OVERRIDE_HOSTNAME:-$(hostname -f)}"
# If the container is misconfigured.. `hostname -f` (which derives it's return value from `/etc/hosts` or DNS query),
# will result in an error that returns an empty value. This warrants a panic.
if [[ -z ${HOSTNAME} ]]
then
dms_panic__misconfigured 'obtain_hostname' '/etc/hosts'
fi
# If the `HOSTNAME` is more than 2 labels long (eg: mail.example.com),
# We take the FQDN from it, minus the 1st label (aka _short hostname_, `hostname -s`).
#
# TODO: For some reason we're explicitly separating out a domain name from our FQDN,
# `hostname -d` was probably not the correct command for this intention either.
# Needs further investigation for relevance, and if `/etc/hosts` is important for consumers
# of this variable or if a more deterministic approach with `cut` should be relied on.
if [[ $(_get_label_count "${HOSTNAME}") -gt 2 ]]
then
if [[ -n ${OVERRIDE_HOSTNAME} ]]
then
# Emulates the intended behaviour of `hostname -d`:
# Assign the HOSTNAME value minus everything up to and including the first `.`
DOMAINNAME=${HOSTNAME#*.}
else
# Operates on the FQDN returned from querying `/etc/hosts` or fallback DNS:
#
# Note if you want the actual NIS `domainname`, use the `domainname` command,
# or `cat /proc/sys/kernel/domainname`.
# Our usage of `domainname` is under consideration as legacy, and not advised
# going forward. In future our docs should drop any mention of it.
#shellcheck disable=SC2034
DOMAINNAME=$(hostname -d)
fi
fi
# Otherwise we assign the same value (eg: example.com):
# Not an else statement in the previous conditional in the event that `hostname -d` fails.
DOMAINNAME="${DOMAINNAME:-${HOSTNAME}}"
}