#!/bin/bash set -euo pipefail REALM="${SAMBA_REALM}" DOMAIN="${SAMBA_DOMAIN}" HOSTNAME_SHORT="${SAMBA_HOSTNAME,,}" FQDN="${SAMBA_FQDN}" DC_IP="${SAMBA_IP}" SAMBA_NETWORK="${SAMBA_NETWORK}" DNS_FORWARDER="${DNS_FORWARDER}" ADMIN_PASSWORD="${SAMBA_ADMIN_PASSWORD}" # ------------------------------------------------------------ # Generate Chrony configuration from environment variables # ------------------------------------------------------------ IFS='.' read -r IP1 IP2 IP3 IP4 <<< "${DC_IP}" if [[ ! "${IP1:-}" =~ ^[0-9]+$ || ! "${IP2:-}" =~ ^[0-9]+$ || ! "${IP3:-}" =~ ^[0-9]+$ || ! "${IP4:-}" =~ ^[0-9]+$ ]] || (( IP1 > 255 || IP2 > 255 || IP3 > 255 || IP4 > 255 )); then echo "ERROR: Invalid SAMBA_IP: ${DC_IP}" exit 1 fi LAN_SUBNET="${SAMBA_NETWORK:?SAMBA_NETWORK is not set}" if [ ! -f /etc/chrony/chrony.conf ]; then echo "ERROR: /etc/chrony/chrony.conf template not found." exit 1 fi cat >> /etc/chrony/chrony.conf < /etc/hosts </dev/null command -v samba >/dev/null command -v chronyd >/dev/null command -v chronyc >/dev/null command -v setfacl >/dev/null command -v getfacl >/dev/null echo "samba-tool: $(command -v samba-tool)" echo "samba: $(command -v samba)" echo "chronyd: $(command -v chronyd)" echo "setfacl: $(command -v setfacl)" echo # ------------------------------------------------------------ # Provision AD domain if necessary # ------------------------------------------------------------ if [ ! -f /var/lib/samba/private/sam.ldb ]; then echo "==============================================" echo " Provisioning new Active Directory domain" echo "==============================================" rm -f /etc/samba/smb.conf samba-tool domain provision \ --realm="${REALM}" \ --domain="${DOMAIN}" \ --server-role=dc \ --dns-backend=SAMBA_INTERNAL \ --use-rfc2307 \ --adminpass="${ADMIN_PASSWORD}" \ --option="dns forwarder = ${DNS_FORWARDER}" echo echo "==============================================" echo " Provisioning completed" echo "==============================================" echo else echo "Existing Samba AD database detected." echo "Skipping domain provisioning." echo fi # ------------------------------------------------------------ # Kerberos configuration # ------------------------------------------------------------ # # Generate krb5.conf dynamically from environment variables. # The domain name is derived from SAMBA_FQDN. # DOMAIN_SUFFIX="${FQDN#*.}" if [[ -z "${DOMAIN_SUFFIX}" || "${DOMAIN_SUFFIX}" == "${FQDN}" ]]; then echo "ERROR: Cannot derive DNS domain from SAMBA_FQDN: ${FQDN}" exit 1 fi cat > /etc/krb5.conf </dev/null; then echo "ERROR: Samba exited before creating the NTP signing socket." wait "${SAMBA_PID}" || true exit 1 fi sleep 1 done if [ "${SOCKET_READY}" -ne 1 ]; then echo "ERROR: NTP signing socket was not created within 30 seconds." kill "${SAMBA_PID}" 2>/dev/null || true wait "${SAMBA_PID}" 2>/dev/null || true exit 1 fi echo "NTP signing socket found: ${SOCKET_PATH}" # ------------------------------------------------------------ # Grant Chrony access to the Samba signing socket directory # ------------------------------------------------------------ # # Chrony runs as _chrony. It needs execute/traverse permission # on the directory to access the signing socket inside it. # echo echo "Setting ACL for Chrony..." setfacl -m u:_chrony:--x /var/lib/samba/ntp_signd echo "Current ACL:" getfacl /var/lib/samba/ntp_signd # ------------------------------------------------------------ # Start Chrony # ------------------------------------------------------------ echo echo "==============================================" echo " Starting Chrony" echo "==============================================" echo chronyd # ------------------------------------------------------------ # Signal handling and process supervision # ------------------------------------------------------------ shutdown() { echo "Stopping Samba AD DC and Chrony..." chronyc shutdown >/dev/null 2>&1 || true kill -TERM "${SAMBA_PID}" 2>/dev/null || true wait "${SAMBA_PID}" 2>/dev/null || true } trap shutdown TERM INT # Keep the container running while Samba is running. wait "${SAMBA_PID}"