#!/bin/bash
# OSSEC Updater Modifed
# Name: oum
# Copyright Atomicorp 2020
# Summary:  OSSEC Updater Modified Installer
# License: Opensource

# Globals
ARCH=`uname -i`
GET=/usr/bin/wget
ROOT_UID="0"
LOG=/root/oum-install.log
DATE=$(date +%Y%m%d-%H:%M)
INSTALLER_VER=2.2
ATOMIC_VER="5.0.1"
VERSION="1.0-23"
SERVER=updates.atomicorp.com
GPG_KEY_FILE="RPM-GPG-KEY.atomicorp.txt"
ARCH=`uname -i`



#Check if run as root
# For environments that do not use bash.
if [ ! "$UID" ]; then
        UID=`id -u`
fi

if [ "$UID" -ne "$ROOT_UID" ] ; then
        echo "ERROR: You must be root to run this program."
        exit 1
fi

echo "OUM install begin: $DATE" >> $LOG


echo
echo "OSSEC Updater Modifed (OUM) Installer (v$INSTALLER_VER)"
echo "Further Documentation is available at: "
echo "https://ossec.net"
echo
echo "Support: https://ossec.net"



# Input validation function
# check_input <msg> <valid responses regex> <default>
# if <default> is passed on as null, then there is no default
# Example: check_input  "Some question (yes/no) " "yes|no"  "yes"
check_input () {
  message=$1
  validate=$2
  default=$3

  while [ $? -ne 1 ]; do
    echo -n "$message "
    read INPUTTEXT < /dev/tty
    if [ "$INPUTTEXT" == "" -a "$default" != "" ]; then
      INPUTTEXT=$default
      return 1
    fi
    echo $INPUTTEXT | egrep -q "$validate" && return 1
    echo "Invalid input"
  done

}

#
if [ -f /var/awp/bin/awp ] || [ -f /var/asl/bin/asl ]; then
	echo "Atomic OSSEC is installed."
	echo "  OUM is not necessary in this environment"
	echo "exiting..."
	exit 1
fi


if [ -f /etc/redhat-release ]; then
  RELEASE=/etc/redhat-release
elif [ -f /etc/os-release ]; then
  RELEASE=/etc/os-release
elif [ -f /etc/openvz-release ]; then
  RELEASE=/etc/openvz-release
elif [ -f /etc/virtuozzo-release ]; then
  RELEASE_FILE=/etc/virtuozzo-release
elif [ -f /etc/SuSE-release ]; then
  RELEASE=/etc/SuSE-release
elif [ -f /etc/lsb-release ]; then
  RELEASE=/etc/lsb-release
elif [ -f /etc/debian_version ]; then
  RELEASE=/etc/debian_version
else
  echo "Error: Unable to identify operating system"
  exit 1
fi

PKG=rpm
if egrep -q "^Fedora|release 3|release 4" $RELEASE ; then
  cat /etc/redhat-release 2>/dev/null || cat $RELEASE
  echo "This platform is no longer supported..." | tee -a $LOG
  echo "Exiting..."
  exit 1
elif egrep -q "release 5|release 2011" $RELEASE ; then
  DIST="el5"
  DIR=centos/5
elif egrep -q "release 6|release 2012" $RELEASE ; then
  DIST="el6"
  DIR=centos/6
elif egrep -q "release 7" $RELEASE ; then
  DIST="el7"
  DIR=centos/7
# Rocky / RHEL / Alma / CentOS Stream 8–10 (redhat-release or os-release VERSION_ID)
elif grep -E -q 'release 8(\.| |$)|VERSION_ID="8(\.|")|PLATFORM_ID="platform:el8"' $RELEASE ; then
  DIST="el8"
  DIR=centos/8
elif grep -E -q 'release 9(\.| |$)|VERSION_ID="9(\.|")|PLATFORM_ID="platform:el9"' $RELEASE ; then
  DIST="el9"
  DIR=rocky/9
elif grep -E -q 'release 10(\.| |$)|VERSION_ID="10(\.|")|PLATFORM_ID="platform:el10"' $RELEASE ; then
  DIST="el10"
  DIR=rocky/10
  VERSION="1.0-24"
  GPG_KEY_FILE="RPM-GPG-KEY.atomicorp.2026.txt"
elif egrep -q "openSUSE 12" $RELEASE ; then
  DIST="suse12"
  DIR=opensuse/12
  PKG=suse
elif egrep -q "openSUSE 13" $RELEASE ; then
  DIST="suse13"
  DIR=opensuse/13
  PKG=suse
elif egrep -qi 'SUSE Linux Enterprise Server 15|^ID=.sles.|^ID="sles"' $RELEASE; then
  DIST="suse15"
  DIR=opensuse/15.6
  PKG=suse
elif egrep -qi 'openSUSE Leap|^ID=.opensuse-leap.|^ID="opensuse-leap"' $RELEASE; then
  DIST="suse15"
  DIR=opensuse/15.6
  PKG=suse
elif egrep -q "^6.0" $RELEASE ; then
  DIST="debian"
  DIR="squeeze"
  PKG=deb
elif egrep -q "wheezy" $RELEASE ; then
  DIR="wheezy"
  DIST="debian"
  PKG=deb
elif egrep -q "jessie" $RELEASE ; then
  DIR="jessie"
  DIST="debian"
  PKG=deb
elif egrep -q "stretch" $RELEASE ; then
  DIR="stretch"
  DIST="debian"
  PKG=deb
elif egrep -q "buster" $RELEASE ; then
  DIR="buster"
  DIST="debian"
  PKG=deb
elif egrep -q "bullseye" $RELEASE ; then
  DIR="bullseye"
  DIST="debian"
  PKG=deb2
elif egrep -q "bookworm" $RELEASE ; then
  DIR="bookworm"
  DIST="debian"
  PKG=deb2
elif grep -E -qi "trixie|VERSION_CODENAME=trixie" $RELEASE ; then
  DIR="trixie"
  DIST="debian"
  PKG=deb2
  GPG_KEY_FILE="RPM-GPG-KEY.atomicorp.2026.txt"
elif egrep -q "lucid" $RELEASE ; then
  DIR="lucid"
  DIST="ubuntu"
  PKG=deb
elif egrep -q "precise" $RELEASE ; then
  DIR="precise"
  DIST="ubuntu"
  PKG=deb
elif egrep -q "Raring Ringtail" $RELEASE ; then
  DIR="raring"
  DIST="ubuntu"
  PKG=deb
elif egrep -q "Trusty Tahr" $RELEASE ; then
  DIR="trusty"
  DIST="ubuntu"
  PKG=deb
elif egrep -q "Xenial" $RELEASE ; then
  DIR="xenial"
  DIST="ubuntu"
  PKG=deb
elif egrep -q "Bionic" $RELEASE ; then
  DIR="bionic"
  DIST="ubuntu"
  PKG=deb
elif grep -E -qi "focal( fossa)?|VERSION_CODENAME=focal|UBUNTU_CODENAME=focal" $RELEASE ; then
  DIR="focal"
  DIST="ubuntu"
  PKG=deb2
elif grep -E -qi "jammy( jellyfish)?|VERSION_CODENAME=jammy|UBUNTU_CODENAME=jammy" $RELEASE ; then
  DIR="jammy"
  DIST="ubuntu"
  PKG=deb2
elif grep -E -qi "noble( numbat)?|VERSION_CODENAME=noble|UBUNTU_CODENAME=noble" $RELEASE ; then
  DIR="noble"
  DIST="ubuntu"
  PKG=deb2
elif grep -E -qi "resolute|VERSION_CODENAME=resolute|UBUNTU_CODENAME=resolute" $RELEASE ; then
  DIR="resolute"
  DIST="ubuntu"
  PKG=deb2
  GPG_KEY_FILE="RPM-GPG-KEY.atomicorp.2026.txt"
else
  echo -e "Error: Unable to determine distribution type.\n" | tee -a $LOG
  echo -e "Supported distributions for OUM\n----------------------------\n" | tee -a $LOG
  echo -e "Rocky Linux / RHEL / Alma 8, 9, 10\nUbuntu 20.04, 22.04, 24.04, 26.04\nDebian 11, 12, 13\n" | tee -a $LOG
  echo -e "If you believe this message was received in error or to register interest in additional distribution support, please send the contents of /etc/os-release to support@atomicorp.com" | tee -a $LOG
  exit 1
fi

yum_install () {
  ATOMIC=atomic-release-$VERSION.$DIST.art.noarch.rpm

  if [ ! -f /usr/bin/yum ] && [ ! -f /usr/bin/dnf ]; then
    echo
    echo "Error: Yum/DNF was not detected. Contact your provider for support." | tee -a $LOG
    echo
    exit 1
  else
    YUM=1
  fi

  ## yum, make sure yum is up to date
  echo -n "Ensuring yum is up to date: " | tee -a $LOG
  /usr/bin/yum -y upgrade yum >> $LOG 2>&1
  echo "Done" | tee -a $LOG

  if rpm -q atomic-release > /dev/null ; then
    if [ ! -f /etc/yum.repos.d/atomic.repo ]; then
      rpm -e atomic-release
    fi

  fi

  rpm -import RPM-GPG-KEY.art.txt >/dev/null 2>&1
  rpm -import RPM-GPG-KEY.atomicorp.txt >/dev/null 2>&1
  if [ -f RPM-GPG-KEY.atomicorp.2026.txt ]; then
    rpm -import RPM-GPG-KEY.atomicorp.2026.txt >/dev/null 2>&1
  fi
  if [ -f "${GPG_KEY_FILE}" ] && [ "${GPG_KEY_FILE}" != "RPM-GPG-KEY.atomicorp.txt" ]; then
    rpm -import "${GPG_KEY_FILE}" >/dev/null 2>&1
  fi


  echo -n "Downloading $ATOMIC: "
  wget -q https://$SERVER/channels/atomic/$DIR/$ARCH/RPMS/$ATOMIC >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    echo "Error: File $ATOMIC not found."
    echo
    exit
  fi

  if [ -f $ATOMIC ]; then
      rpm -Uvh $ATOMIC
      rm -f $ATOMIC
  else
      echo "ERROR: $ATOMIC was not downloaded."
      exit 1
  fi

  echo "OK"

  if [ ! -f /etc/yum.repos.d/atomic.repo ]; then
    echo "Error: /etc/yum.repos.d/atomic.repo was not detected."
    exit 1
  fi

  yum --enablerepo=atomic  -y install oum | tee -a $LOG

}


# DEB Distros
apt_install () {

  /usr/bin/apt-get update

  /usr/bin/apt-get -y --force-yes install gpg

  wget -O -  https://www.atomicorp.com/RPM-GPG-KEY.atomicorp.txt | apt-key add -
  if [ $? -ne 0 ]; then
    echo
    echo "Error: Installation failed"
    echo
    exit 1
  fi


  if [ -d /etc/apt/sources.list.d/ ]; then
    APT_SOURCES="/etc/apt/sources.list.d/atomic.list"

    echo -n "Adding [atomic] to $APT_SOURCES: "
    if [ ! -f $APT_SOURCES ]; then
      echo "deb https://updates.atomicorp.com/channels/atomic/$DIST $DIR main" > $APT_SOURCES
      echo "OK"
    else
      echo "Already installed"
    fi

  else
    APT_SOURCES="/etc/apt/sources.list"
    echo -n "Adding [atomic] to $APT_SOURCES: "
    if ! grep -q "atomic/$DIST $DIR" $APT_SOURCES ; then
      echo "deb https://updates.atomicorp.com/channels/atomic/$DIST $DIR main" >> /etc/apt/sources.list
      echo "OK"
    else
      echo "Already installed"
    fi
  fi

  /usr/bin/apt update && /usr/bin/apt -y install oum
  if [ $? -ne 0 ]; then
    echo
    echo "Error: apt installation failed"
    echo
    exit 1
  fi


}

apt_install_2 () {
        APT_SOURCES="/etc/apt/sources.list.d/atomic.list"
        ARCH=$(dpkg --print-architecture)
        local apt_key_file="${GPG_KEY_FILE:-RPM-GPG-KEY.atomicorp.txt}"

        # GPG dependency
        if [ ! -f /usr/bin/gpg ]; then
                /usr/bin/apt-get update
                /usr/bin/apt-get -y --force-yes install gpg
        fi

        # Debian 13+ / Ubuntu 26+ Release metadata uses the 2026 Chelon key only
        if [ ! -f "${apt_key_file}" ]; then
                wget -q "https://www.atomicorp.com/${apt_key_file}"
        fi
        if [ ! -f "${apt_key_file}" ] || [ ! -s "${apt_key_file}" ]; then
                echo "Error: could not download ${apt_key_file}"
                exit 1
        fi

        if [ -d /etc/apt/trusted.gpg.d ]; then
                gpg --batch --no-default-keyring --dearmor -o /etc/apt/trusted.gpg.d/atomic.gpg.new < "${apt_key_file}"
                if [ $? -ne 0 ]; then
                        echo "Error: failed to build /etc/apt/trusted.gpg.d/atomic.gpg"
                        exit 1
                fi
                chmod 644 /etc/apt/trusted.gpg.d/atomic.gpg.new
                mv -f /etc/apt/trusted.gpg.d/atomic.gpg.new /etc/apt/trusted.gpg.d/atomic.gpg
                _deb_line="deb [arch=${ARCH} signed-by=/etc/apt/trusted.gpg.d/atomic.gpg trusted=yes] https://updates.atomicorp.com/channels/atomic/${DIST} ${DIR}/${ARCH}/ "
        else
                cat "${apt_key_file}" | apt-key add -
                if [ $? -ne 0 ]; then
                        echo
                        echo "Error: Installation failed"
                        echo
                        exit 1
                fi
                _deb_line="deb [trusted=yes] https://updates.atomicorp.com/channels/atomic/${DIST} ${DIR}/${ARCH}/ "
        fi

        echo -n "Adding [atomic] to $APT_SOURCES: "
        echo "$_deb_line" > $APT_SOURCES
        echo "OK"

  /usr/bin/apt update && /usr/bin/apt -y install oum
  if [ $? -ne 0 ]; then
    echo
    echo "Error: apt installation failed"
    echo
    exit 1
  fi



}



# GPG Keys
echo -n "Installing the Atomic GPG keys: "
if [ ! -f RPM-GPG-KEY.art.txt ]; then
  wget -q https://www.atomicorp.com/RPM-GPG-KEY.art.txt 1>/dev/null 2>&1
fi

if [ ! -f RPM-GPG-KEY.atomicorp.txt ]; then
  wget -q https://www.atomicorp.com/RPM-GPG-KEY.atomicorp.txt 1>/dev/null 2>&1
fi
if [ ! -f RPM-GPG-KEY.atomicorp.2026.txt ]; then
  wget -q https://www.atomicorp.com/RPM-GPG-KEY.atomicorp.2026.txt 1>/dev/null 2>&1
fi
echo "OK"
echo

if [ "$PKG" == "rpm" ]; then
  yum_install
elif [ "$PKG" == "deb" ]; then
  apt_install
elif [ "$PKG" == "deb2" ]; then
  apt_install_2
fi


echo "install finished" >> $LOG
echo
echo "To complete installation run:"
echo "	oum configure"
echo
echo "and to update your rules:"
echo "	oum update"
echo
echo
