#!/usr/bin/env bash
set -euo pipefail

# Set HARRESI_ROOT_PREFIX when preparing an offline root filesystem.
ROOT_PREFIX="${HARRESI_ROOT_PREFIX:-}"
AUDIT_ROOT_REL="/var/log/harresi/audit"

root_path() {
  printf '%s%s\n' "$ROOT_PREFIX" "$1"
}

AUDIT_ROOT="$(root_path "$AUDIT_ROOT_REL")"
AUDITD_DIR="$AUDIT_ROOT/auditd"
SECURITY_DIR="$AUDIT_ROOT/security"
PRODUCT_DIR="$AUDIT_ROOT/product"
AIDE_DIR="$AUDIT_ROOT/aide"

install -d -m 0755 -o root -g root "$AUDIT_ROOT"
install -d -m 0700 -o root -g root "$AUDITD_DIR"
install -d -m 0755 -o root -g root "$SECURITY_DIR"
install -d -m 1777 -o root -g root "$SECURITY_DIR/tmux-status"
install -d -m 0750 -o root -g root "$PRODUCT_DIR"
install -d -m 0755 -o root -g root "$AIDE_DIR"

ensure_file() {
  local mode="$1" path="$2"
  [[ -e "$path" ]] || install -m "$mode" -o root -g root /dev/null "$path"
  chown root:root "$path"
  chmod "$mode" "$path"
}

ensure_file 0600 "$AUDITD_DIR/audit.log"
ensure_file 0640 "$SECURITY_DIR/audit-security.log"
ensure_file 0640 "$PRODUCT_DIR/audit-product.log"
ensure_file 0640 "$PRODUCT_DIR/ids-ips-events.log"
ensure_file 0644 "$AIDE_DIR/aide.log"
ensure_file 0644 "$AIDE_DIR/aide_history.log"

enable_audit_syslog_plugin() {
  local relative_conf conf plugin_path target_conf found=0

  # Debian uses /etc/audit/plugins.d; older auditd installations use
  # /etc/audisp/plugins.d. Enable every existing syslog plugin so the
  # audit-security rsyslog route receives USER_LOGIN and related events.
  for relative_conf in /etc/audit/plugins.d/syslog.conf /etc/audisp/plugins.d/syslog.conf; do
    conf="$(root_path "$relative_conf")"
    if [[ ! -f "$conf" ]]; then
      continue
    fi

    found=1
    if grep -Eq '^[[:space:]]*active[[:space:]]*=' "$conf"; then
      sed -i -E 's/^[[:space:]]*active[[:space:]]*=.*/active = yes/' "$conf"
    else
      printf '\nactive = yes\n' >> "$conf"
    fi
    if grep -Eq '^[[:space:]]*args[[:space:]]*=' "$conf"; then
      sed -i -E 's/^[[:space:]]*args[[:space:]]*=.*/args = LOG_INFO/' "$conf"
    else
      printf 'args = LOG_INFO\n' >> "$conf"
    fi
  done

  if (( found != 0 )); then
    return 0
  fi

  # The package normally supplies syslog.conf. Create a usable default if
  # it is missing but audisp-syslog is installed (also works in offline
  # rootfs preparation through HARRESI_ROOT_PREFIX).
  for relative_conf in /sbin/audisp-syslog /usr/sbin/audisp-syslog; do
    plugin_path="$(root_path "$relative_conf")"
    if [[ -x "$plugin_path" ]]; then
      if [[ -d "$(root_path /etc/audisp/plugins.d)" && ! -d "$(root_path /etc/audit/plugins.d)" ]]; then
        target_conf="$(root_path /etc/audisp/plugins.d/syslog.conf)"
      else
        target_conf="$(root_path /etc/audit/plugins.d/syslog.conf)"
      fi
      install -d -m 0755 "$(dirname "$target_conf")"
      printf '%s\n' \
        'active = yes' \
        'direction = out' \
        "path = $relative_conf" \
        'type = always' \
        'args = LOG_INFO' \
        'format = string' > "$target_conf"
      chmod 0640 "$target_conf"
      return 0
    fi
  done

  printf '[WARN] No se encontro el plugin audisp-syslog; no se pudo habilitar el envio a rsyslog.\n' >&2
}

enable_audit_syslog_plugin

AUDITD_CONF="$(root_path /etc/audit/auditd.conf)"
if [[ -f "$AUDITD_CONF" ]]; then
  set_auditd_option() {
    local key="$1" value="$2"
    if grep -Eq "^[[:space:]]*${key}[[:space:]]*=" "$AUDITD_CONF"; then
      sed -i -E "s#^[[:space:]]*${key}[[:space:]]*=.*#${key} = ${value}#" "$AUDITD_CONF"
    else
      printf '\n%s = %s\n' "$key" "$value" >> "$AUDITD_CONF"
    fi
  }

  set_auditd_option log_file "$AUDIT_ROOT_REL/auditd/audit.log"
  set_auditd_option log_group root
  set_auditd_option flush DATA
  set_auditd_option max_log_file_action ROTATE
  set_auditd_option num_logs 5
else
  printf '[WARN] auditd.conf not found; auditd storage path was not configured: %s\n' "$AUDITD_CONF" >&2
fi
