#!/bin/bash
# SPDX-Version: 3.0
# SPDX-CreationInfo: 2025-02-13; WEIDNER, Marc S.; <cendev@coresecret.eu>
# SPDX-ExternalRef: GIT https://cendev.eu/marc.weidner/CISS.2025.debian.installer.git
# SPDX-FileContributor: WEIDNER, Marc S.; Centurion Intelligence Consulting Agency
# SPDX-FileCopyrightText: 2024-2025; WEIDNER, Marc S.; <cendev@coresecret.eu>
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: EUPL-1.2 OR LicenseRef-CCLA-1.0
# SPDX-LicenseComment: This file is part of the CISS.2025.hardened.installer framework.
# SPDX-PackageName: CISS.2025.hardened.installer
# SPDX-Security-Contact: security@coresecret.eu

###########################################################################################
# 3.8.6. Functions - installation - setup chrony                                          #
###########################################################################################

###########################################################################################
# Setup chrony NTPSec client
# Globals:
#   DIR_BAK
#   MODULE_ERR
#   MODULE_TXT
#   NTPSRVR
#   TARGET
# Arguments:
#   None
###########################################################################################
3_8_6_functions_installation_setup_crony() {
  declare -g -x MODULE_ERR="3_8_6_functions_installation_setup_chrony"
  declare -g -x MODULE_TXT="Installing 'chrony client'"
  do_show_header "${MODULE_TXT}"

  # Create NTPSec Server file from Array 'NTPSRVR'

  # shellcheck disable=SC2155
  declare OUTPUT_FILE=$(mktemp /tmp/ntp_servers.XXXXXX)

  declare NTPSERVER

  for NTPSERVER in "${NTPSRVR[@]}"; do
    echo "server ${NTPSERVER} iburst nts minpoll 5 maxpoll 9" >> "${OUTPUT_FILE}"
  done

  # do_remove_service "systemd-timesyncd.service" "systemd-timesyncd"

  mkdir -p "${TARGET}"/var/log/chrony
  do_in_target "${TARGET}" apt-get install chrony -y
  do_log "info" "true" "Command: 'apt-get install chrony -y' executed in: '${TARGET}'."

  if [ ! -e "${TARGET}/etc/systemd/system/multi-user.target.wants/chrony.service" ]; then
    ln -s "${TARGET}"/lib/systemd/system/chrony.service "${TARGET}"/etc/systemd/system/multi-user.target.wants/chrony.service
  fi

  mv "${TARGET}"/etc/chrony/chrony.conf "${DIR_BAK}"chrony.conf.bak
  chmod 644 "${DIR_BAK}"chrony.conf.bak
  touch "${TARGET}"/etc/chrony/chrony.conf

  cat << EOF >> "${TARGET}"/etc/chrony/chrony.conf
# Include configuration files found in /etc/chrony/conf.d.
confdir /etc/chrony/conf.d
driftfile /var/lib/chrony/chrony.drift
keyfile /etc/chrony/chrony.keys
logdir /var/log/chrony
ntsdumpdir /var/lib/chrony
sourcedir /run/chrony-dhcp
sourcedir /etc/chrony/sources.d

log tracking measurements statistics

authselectmode require

leapsectz right/UTC

leapsecmode system

maxupdateskew 100.0

rtcsync

makestep 1 3

EOF

  cat "${OUTPUT_FILE}" >> "${TARGET}"/etc/chrony/chrony.conf

  chmod 644 /etc/chrony/chrony.conf
  do_log "info" "false" "Chrony NTPsec client installed."

  do_show_footer "${MODULE_TXT}"
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh:
