Files
pi-farm 84834321e9
build-and-push.yaml / release-and-build (push) Successful in 48s
chmod 750 for ntp
2026-10-29 09:00:44 +00:00

275 lines
7.0 KiB
Bash

#!/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 <<CHRONY
allow ${LAN_SUBNET}
bindaddress ${DC_IP}
CHRONY
echo "Generated Chrony configuration:"
cat /etc/chrony/chrony.conf
echo
echo "=============================================="
echo " Samba Active Directory Domain Controller"
echo "=============================================="
echo
echo "Hostname : ${HOSTNAME_SHORT}"
echo "FQDN : ${FQDN}"
echo "Realm : ${REALM}"
echo "Domain : ${DOMAIN}"
echo "DC IP : ${DC_IP}"
echo "SUBNET : ${LAN_SUBNET}"
echo "Forwarder: ${DNS_FORWARDER}"
echo
# ------------------------------------------------------------
# Hostname
# ------------------------------------------------------------
hostname "${HOSTNAME_SHORT}"
cat > /etc/hosts <<HOSTS
127.0.0.1 localhost
${DC_IP} ${FQDN} ${HOSTNAME_SHORT}
HOSTS
# ------------------------------------------------------------
# Required tools
# ------------------------------------------------------------
command -v samba-tool >/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 <<KRB5
[libdefaults]
default_realm = ${REALM}
dns_lookup_realm = false
dns_lookup_kdc = false
rdns = false
[realms]
${REALM} = {
kdc = ${FQDN}
admin_server = ${FQDN}
default_domain = ${DOMAIN_SUFFIX}
}
[domain_realm]
.${DOMAIN_SUFFIX} = ${REALM}
${DOMAIN_SUFFIX} = ${REALM}
KRB5
echo "Kerberos configuration:"
cat /etc/krb5.conf
echo
# ------------------------------------------------------------
# Validate Samba configuration
# ------------------------------------------------------------
echo "Validating Samba configuration..."
testparm -s
# ------------------------------------------------------------
# Prepare NTP signing socket directory
# ------------------------------------------------------------
echo
echo "=============================================="
echo " Preparing NTP signing socket"
echo "=============================================="
mkdir -p /var/lib/samba/ntp_signd
if [ -d "/var/lib/samba/ntp_signd" ]; then
chmod 0750 /var/lib/samba/ntp_signd
else
mkdir -p /var/lib/samba/ntp_signd
chmod 0750 /var/lib/samba/ntp_signd
fi
# ------------------------------------------------------------
# Start Samba first
# ------------------------------------------------------------
#
# Samba creates the NTP signing socket.
# Chrony must be able to traverse the socket directory.
#
echo
echo "=============================================="
echo " Starting Samba AD DC"
echo "=============================================="
echo
samba -i -M single &
SAMBA_PID=$!
# Wait for Samba to create its NTP signing socket.
SOCKET_PATH="/var/lib/samba/ntp_signd/socket"
SOCKET_READY=0
for i in $(seq 1 30); do
if [ -S "${SOCKET_PATH}" ]; then
SOCKET_READY=1
break
fi
if ! kill -0 "${SAMBA_PID}" 2>/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}"