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

guard_sourcing

#######################################
# Build Ultra Hardened dropbear-2025.88 from sources.
# Globals:
#   DIR_TMP
#   PATH
#   PATH_SEPARATOR
#   SHELLOPTS
#   VAR_DEBUG_TRACE
#   VAR_SETUP_PATH
# Arguments:
#   None
# Returns:
#   0: on success
#   ERR_PATH_NOT_VALID
#######################################
dropbear_build() {
  ### Declare Arrays, HashMaps, and Variables.
  declare     var_dropbear_version="2025.88"
  declare     var_tar="${VAR_SETUP_PATH}/upgrades/dropbear/dropbear-${var_dropbear_version}.tar.bz2"
  declare     var_build_dir="${DIR_TMP}/build/dropbear-${var_dropbear_version}"
  declare -r  var_logfile="/root/.ciss/cdi/log/4310_dropbear_build.log"

  touch "${TARGET}${var_logfile}" && chmod 0600 "${TARGET}${var_logfile}"

  apt-get install -y autoconf automake build-essential libtool libtomcrypt-dev libtommath-dev musl-tools

  mkdir -p "${DIR_TMP}/build"
  cp "${var_tar}" "${DIR_TMP}/build"
  tar xjf "${DIR_TMP}/build/dropbear-${var_dropbear_version}.tar.bz2" -C "${DIR_TMP}/build" || return "${ERR_PATH_NOT_VALID}"
  cp "${VAR_SETUP_PATH}/upgrades/dropbear/localoptions.h" "${var_build_dir}"
  cd "${var_build_dir}" || return "${ERR_PATH_NOT_VALID}"

  ### Flag Purpose:
  #  -fPIE              : Generate position-independent executable code
  #  -pie               : Link the executable as PIE (so that ASLR works)
  #  -static            : Fully statically linked against musl
  #  -s                 : Strip unnecessary symbols directly during linking
  #  -Wl,-z,relro,-z,now: Enables full RELRO (symbol resolution at program startup)

  [[ "${VAR_DEBUG_TRACE,,}" == "true" ]] && set +x
  # shellcheck disable=SC2016,SC2312
  setsid bash -c '
    ### Sterile environment for the build-process.
    export -n SHELLOPTS
    set +u
    unset PATH_SEPARATOR
    PATH_SEPARATOR=":"
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    CC=musl-gcc \
      CFLAGS="-Os -fPIE -Wno-undef -fstack-protector-strong -D_FORTIFY_SOURCE=2" \
      LDFLAGS="-static -pie -s -Wl,-z,relro,-z,now" \
      ./configure \
      --enable-static \
      --enable-openpty \
      --disable-pam \
      --disable-zlib

    # shellcheck disable=2312
    make -j"$(nproc)"
  ' 2>&1 | tee -a "${TARGET}${var_logfile}"
  [[ "${VAR_DEBUG_TRACE,,}" == "true" ]] && set -x

  guard_dir && return 0
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
