lynis/include/helper_update

303 lines
11 KiB
Plaintext
Raw Normal View History

#!/bin/sh
2016-03-13 16:00:39 +01:00
#################################################################################
#
# Lynis
# ------------------
#
# Copyright 2007-2013, Michael Boelen
# Copyright 2013-2016, CISOfy
#
# Website : https://cisofy.com
# Blog : http://linux-audit.com
# GitHub : https://github.com/CISOfy/lynis
#
# Lynis comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
# welcome to redistribute it under the terms of the GNU General Public License.
# See LICENSE file for usage of this software.
#
######################################################################
#
# Helper program to support automatic updates of Lynis
#
######################################################################
#
# Options:
# ---------
# 1) lynis update info - Show version information (external)
# 2) lynis update release - Check and install new release (internal)
#
# How to use:
# ------------
# Run option 1 to know about current and latest release information.
# Run option 2 to query internal server for possible upgrade of Lynis.
#
# Steps for updating to new release:
# 1) Run Lynis with: lynis update release
# 2) Lynis will use this helper and check the profile
# 3) The configured web server will be queried (lynis-latest-version)
# 4) The contents of this file will be compared with a local file
# 5) If there is a difference, download package
# 6) Check paths and extract files
# 7) Quit program
#
# Suggested documentation if you want to use this functionality:
# https://cisofy.com/documentation/lynis/upgrading/
#
######################################################################
LOCAL_VERSION="-"
RUN_UPDATE_CHECK=1
SERVER_VERSION=""
PERFORM_UPGRADE=0
2016-05-14 17:42:51 +02:00
QUIET=0
WGET_EXISTS=`which wget 2> /dev/null`
CURL_EXISTS=`which curl 2> /dev/null`
FETCH_EXISTS=`which fetch 2> /dev/null`
# Update version
if [ "$1" = "release" ]; then
if [ "${UPDATE_SERVER_PROTOCOL}" = "" ] ; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: Unknown protocol, please specify (http, https) in profile (update_server_protocol)"
ExitFatal
fi
if [ "${UPDATE_SERVER_ADDRESS}" = "" ] ; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: Unknown download address, please specify in profile (update_server_address)"
ExitFatal
fi
if [ "${UPDATE_LATEST_VERSION_DOWNLOAD}" = "" ] ; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: No URL to latest download has been specifiedrsion on the server, please specify in profile (update_latest_version_download)"
ExitFatal
fi
if [ "${UPDATE_LATEST_VERSION_INFO}" = "" ] ; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: No URL has been specified to know the latest version on the server, please specify in profile (update_latest_version_info)"
ExitFatal
fi
if [ "${UPDATE_LOCAL_DIRECTORY}" = "" ] ; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: No local directory has been specified to store Lynis files. Please specify in profile (update_local_directory)"
ExitFatal
else
if [ ! -d ${UPDATE_LOCAL_DIRECTORY} ]; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: Directory ${UPDATE_LOCAL_DIRECTORY} does not exist"
ExitFatal
fi
fi
if [ "${UPDATE_LOCAL_VERSION_INFO}" = "" ] ; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: No data file has been specified to determine local Lynis version, please specify in profile (update_local_version_info)"
ExitFatal
fi
if [ ! -f ${UPDATE_LOCAL_VERSION_INFO} ]; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Note: local data file ${UPDATE_LOCAL_VERSION_INFO} does not exist. It will be created after updating. (update_local_version_info)"
else
LOCAL_VERSION=`cat ${UPDATE_LOCAL_VERSION_INFO}`
fi
# Normal update
FULLPATH="${UPDATE_SERVER_PROTOCOL}://${UPDATE_SERVER_ADDRESS}${UPDATE_LATEST_VERSION_INFO}"
# Create temporary file
CreateTempFile
TMP_FILE="${TEMP_FILE}"
2016-05-14 17:42:51 +02:00
if [ "${TMP_FILE}" = "" ]; then ${ECHOCMD} "Could not create a temporary file. Exiting..."; ExitFatal; fi
2016-05-14 17:42:51 +02:00
${ECHOCMD} "${CYAN}[Phase 1] Downloading details${NORMAL}"
if [ ! "${WGET_EXISTS}" = "" ]; then
2016-04-28 09:15:54 +02:00
LogText "Using wget to download release information"
LAST_COMMAND_HELP="wget --output-document ${TMP_FILE} ${FULLPATH}"
wget --output-document ${TMP_FILE} ${FULLPATH} 2> /dev/null
EXIT_CODE=$?
elif [ ! "${CURL_EXISTS}" = "" ]; then
2016-04-28 09:15:54 +02:00
LogText "Using curl to download release information"
LAST_COMMAND_HELP="curl --fail -o ${TMP_FILE} ${FULLPATH}"
curl --fail -o ${TMP_FILE} ${FULLPATH} 2> /dev/null
EXIT_CODE=$?
else
2016-05-14 17:42:51 +02:00
${ECHOCMD} "No download tool available to perform download"
ExitFatal
fi
if [ ! "${TMP_FILE}" = "" ]; then
if [ -f ${TMP_FILE} ]; then
SERVER_VERSION=`cat ${TMP_FILE}`
rm -f ${TMP_FILE}
fi
else
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Temporary file variable is empty, which is unexpected. Aborting.."
ExitFatal
fi
# Determine if downloading meta data was successful
if [ ${EXIT_CODE} -eq 0 ]; then
if [ "${SERVER_VERSION}" = "" ]; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "No version found on the server. Aborting.."
ExitFatal
else
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Version found on server: ${SERVER_VERSION}"
${ECHOCMD} "Local version found: ${LOCAL_VERSION}"
fi
else
2016-05-14 17:42:51 +02:00
${ECHOCMD} "${RED}Error: ${WHITE}Download utility returned an unexpected error code.${NORMAL} Aborting.."
${ECHOCMD} "Error code: ${EXIT_CODE}"
${ECHOCMD} "Suggested command: ${LAST_COMMAND_HELP}"
ExitFatal
fi
#==========================================================================================================================================
2016-05-14 17:42:51 +02:00
${ECHOCMD} " "
${ECHOCMD} "${CYAN}[Phase 2] Compare results${NORMAL}"
if [ ! "${LOCAL_VERSION}" = "${SERVER_VERSION}" ]; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Different version available, moving to upgrade phase"
PERFORM_UPGRADE=1
else
2016-05-14 17:42:51 +02:00
${ECHOCMD} "${GREEN}No upgrade needed${NORMAL}"
fi
# Go to phase 3 if upgrade is needed
if [ ${PERFORM_UPGRADE} -eq 1 ]; then
FULLPATH="${UPDATE_SERVER_PROTOCOL}://${UPDATE_SERVER_ADDRESS}${UPDATE_LATEST_VERSION_DOWNLOAD}"
2016-05-14 17:42:51 +02:00
${ECHOCMD} " "
${ECHOCMD} "[Phase 3] Downloading latest release"
${ECHOCMD} "Download location: ${FULLPATH}"
if [ ! "${WGET_EXISTS}" = "" ]; then
2016-04-28 09:15:54 +02:00
LogText "Using wget to download latest release"
LAST_COMMAND_HELP="wget --output-document ${TMP_FILE} ${FULLPATH}"
wget --output-document ${TMP_FILE} ${FULLPATH} 2> /dev/null
EXIT_CODE=$?
elif [ ! "${CURL_EXISTS}" = "" ]; then
2016-04-28 09:15:54 +02:00
LogText "Using curl to download latest release"
LAST_COMMAND_HELP="curl --fail -o ${TMP_FILE} ${FULLPATH}"
curl --fail -o ${TMP_FILE} ${FULLPATH} 2> /dev/null
EXIT_CODE=$?
fi
if [ ${EXIT_CODE} -eq 0 ]; then
if [ -f ${TMP_FILE} ]; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Download successful"
# Extract the file to the related path, with 'lynis' appended
# Note: by default the tarball includes 'lynis' as directory
if [ ! -d ${UPDATE_LOCAL_DIRECTORY} ]; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: directory ${UPDATE_LOCAL_DIRECTORY} does not exist"
ExitFatal
fi
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Extracting latest version to path ${UPDATE_LOCAL_DIRECTORY}"
if [ ! -d ${UPDATE_LOCAL_DIRECTORY}/lynis ]; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Creating 'lynis' directory in ${UPDATE_LOCAL_DIRECTORY}"
mkdir ${UPDATE_LOCAL_DIRECTORY}/lynis
if [ $? -gt 0 ]; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: could not create directory ${UPDATE_LOCAL_DIRECTORY}/lynis"
ExitFatal
fi
fi
if [ -d ${UPDATE_LOCAL_DIRECTORY}/lynis ]; then
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Extracting files to ${UPDATE_LOCAL_DIRECTORY}"
tar xzf ${TMP_FILE} -C ${UPDATE_LOCAL_DIRECTORY}
if [ $? -eq 0 ]; then
# Check if we can find the Lynis binary (in the created 'lynis' directory)
if [ -f ${UPDATE_LOCAL_DIRECTORY}/lynis/lynis ]; then
# If version was downloaded, update local version
echo ${SERVER_VERSION} > ${UPDATE_LOCAL_VERSION_INFO}
else
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: could not find downloaded file on disk"
fi
else
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: File extraction failed"
ExitFatal
fi
else
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: could not find lynis directory"
fi
else
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: could not find downloaded file on disk"
ExitFatal
fi
else
2016-05-14 17:42:51 +02:00
${ECHOCMD} "Error: could not download latest release"
${ECHOCMD} "Suggestion: ${LAST_COMMAND_HELP}"
ExitFatal
fi
fi
# Removing temp file
2016-04-28 09:15:54 +02:00
LogText "Action: Removing temporary file ${TMP_FILE}"
if [ "${TMP_FILE}" = "" ]; then
if [ -f ${TMP_FILE} ]; then
rm -f ${TMP_FILE}
fi
fi
2016-05-14 17:42:51 +02:00
${ECHOCMD} " "
${ECHOCMD} "Done"
${ECHOCMD} " "
# Update check
elif [ "$1" = "info" ]; then
# CV - Current Version
2016-04-05 11:31:21 +02:00
PROGRAM_AC=`echo ${PROGRAM_VERSION} | awk '{ print $1 }' | sed 's/[.]//g'`
PROGRAM_LV=0
CheckUpdates
# Reset everything if we can't determine our current version or the latest
# available version (due lack of internet connectivity for example)
if [ "${PROGRAM_AC}" = "" -o "${PROGRAM_LV}" = "" ]; then
# Set both to safe values
PROGRAM_AC=0; PROGRAM_LV=0
fi
2016-04-05 11:31:21 +02:00
echo ""; echo " == ${WHITE}${PROGRAM_NAME}${NORMAL} =="
echo ""
echo " Version : ${PROGRAM_VERSION}"
echo -n " Status : "
if [ ${PROGRAM_LV} -eq 0 ]; then
echo "${RED}Unknown${NORMAL}";
elif [ ${PROGRAM_LV} -gt ${PROGRAM_AC} ]; then
echo "${YELLOW}Outdated${NORMAL}";
echo " Installed version : ${PROGRAM_AC}"
echo " Latest version : ${PROGRAM_LV}"
else
echo "${GREEN}Up-to-date${NORMAL}"
fi
echo " Release date : ${PROGRAM_RELEASE_DATE}"
echo " Update location : ${PROGRAM_WEBSITE}"
echo ""; echo ""
2016-04-05 11:31:21 +02:00
echo "${PROGRAM_COPYRIGHT}"
echo ""
2016-07-18 10:33:52 +02:00
# Check if there is an update, display status on screen and use exit code to tell status as well
elif [ "$1" = "check" ]; then
# CV - Current Version, LV - Latest Version
PROGRAM_CV=$(echo ${PROGRAM_VERSION} | awk '{ print $1 }' | sed 's/[.]//g')
PROGRAM_LV=0
CheckUpdates
if [ "${PROGRAM_CV}" = "" -o "${PROGRAM_LV}" = "" ]; then PROGRAM_AC=0; PROGRAM_LV=0; fi
if [ ${PROGRAM_LV} -eq 0 ]; then
echo "status=unknown";
ExitCustom 1
elif [ ${PROGRAM_LV} -gt ${PROGRAM_CV} ]; then
echo "status=outdated";
ExitCustom 1
else
echo "status=up-to-date"
ExitClean
fi
else
2016-07-18 10:33:52 +02:00
${ECHOCMD} "${RED}Error: ${WHITE}Unknown parameter $1.${NORMAL} Aborting.."
ExitFatal
fi
2016-07-18 10:33:52 +02:00
ExitClean
2016-05-14 17:42:51 +02:00
QUIET=1
# The End