Highly Available and Scalable Information System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

340 lines
10 KiB

#!/bin/bash
#EuryBOX tools functions file
#Desc: change file(s) permissions
#3 arg required: file dac_owner dac_mask (chmod mask) mac_mask (SE context)
eurybox_change_file_permissions ()
{
local FILE=$1
local DAC_OWNER=$2
local DAC_MASK=$3
local MAC_MASK=$4
local PERMISSIONS_OUT
local LS_OPTIONS="-al"
PERMISSIONS_OUT=`sudo sh -c "sudo chmod -R $DAC_MASK $FILE" 2>&1`
STATUS=$?
if [[ !($STATUS -eq 0) ]]
then
eurybox_display_message warning TOOLS "DAC mask change failed - error $STATUS:\n$PERMISSIONS_OUT"
else
eurybox_display_message message TOOLS "DAC mask changed with success"
eurybox_display_message debug TOOLS "DAC mask output command:\n$PERMISSIONS_OUT"
fi
PERMISSIONS_OUT=`sudo sh -c "sudo chown -R $DAC_OWNER $FILE" 2>&1`
STATUS=$?
if [[ !($STATUS -eq 0) ]]
then
eurybox_display_message warning TOOLS "DAC owner change failed - error $STATUS:\n$PERMISSIONS_OUT"
else
eurybox_display_message message TOOLS "DAC owner changed with success"
eurybox_display_message debug TOOLS "DAC owner output command:\n$PERMISSIONS_OUT"
fi
if [[ $EURYBOX_DISTRIB_MAC_TYPE == "selinux" ]]
then
LS_OPTIONS="-aZ"
PERMISSIONS_OUT=`sudo sh -c "sudo chcon -R $MAC_MASK $FILE" 2>&1`
STATUS=$?
if [[ !($STATUS -eq 0) ]]
then
eurybox_display_message warning TOOLS "MAC label change failed - error $STATUS:\n$PERMISSIONS_OUT"
else
eurybox_display_message message TOOLS "MAC label changed with success"
eurybox_display_message debug TOOLS "MAC label output command:\n$PERMISSIONS_OUT"
fi
fi
PERMISSIONS_OUT=`sudo sh -c "ls $LS_OPTIONS $FILE" 2>&1`
STATUS=$?
if [[ !($STATUS -eq 0) ]]
then
eurybox_display_message warning TOOLS "Permissions listing failed - error $STATUS:\n$PERMISSIONS_OUT"
else
eurybox_display_message message TOOLS "Final permissions:\n$PERMISSIONS_OUT"
fi
}
#Desc: echo a log message depending on configuration log level and message level
#3 args required: level(debug, message, warning, error) category message
eurybox_display_message ()
{
local LEVEL=$1
local CATEGORY=$2
local MESSAGE=$3
local NOW=$(date +"%Y_%m_%d@%H:%M:%S")
case $LEVEL in
debug )
if [[ $EURYBOX_LOG_LEVEL = "debug" || $EURYBOX_LOG_LEVEL = "verbose" ]]
then
if [ -t 1 ]
then
tput setb 0
tput setaf 6
fi
printf "$NOW [EURYBOX][$CATEGORY][DEBUG] %b\n" "$MESSAGE"
fi
;;
message )
if [[ $EURYBOX_LOG_LEVEL = "debug" || $EURYBOX_LOG_LEVEL = "info" || $EURYBOX_LOG_LEVEL = "verbose" ]]
then
if [ -t 1 ]
then
tput setb 0
tput setaf 2
fi
printf "$NOW [EURYBOX][$CATEGORY][INFO] %b\n" "$MESSAGE"
fi
;;
warning )
if [[ $EURYBOX_LOG_LEVEL = "debug" || $EURYBOX_LOG_LEVEL = "info" || $EURYBOX_LOG_LEVEL = "verbose" || $EURYBOX_LOG_LEVEL = "warning" ]]
then
if [ -t 1 ]
then
tput setb 0
tput setaf 1
fi
printf "$NOW [EURYBOX][$CATEGORY][WARNING] %b\n" "$MESSAGE"
fi
;;
error )
if [[ $EURYBOX_LOG_LEVEL = "debug" || $EURYBOX_LOG_LEVEL = "info" || $EURYBOX_LOG_LEVEL = "verbose" || $EURYBOX_LOG_LEVEL = "warning" || $EURYBOX_LOG_LEVEL = "error" ]]
then
if [ -t 1 ]
then
tput setb 0
tput setaf 1
fi
printf "$NOW [EURYBOX][$CATEGORY][ERROR] %b\n" "$MESSAGE"
if [ -t 1 ]
then
tput setb 0
tput sgr0
fi
printf "Stopping script execution\n"
exit 1
fi
;;
* )
if [ -t 1 ]
then
tput sgr0
fi
printf "$NOW [EURYBOX][SYNTAX][ERROR] Bad log level message. Stopping script execution\n"
exit 1
;;
esac
if [ -t 1 ]
then
tput setb 0
tput sgr0
fi
}
#Desc: echo detected archives, sorted by name, year and month
#No arg required
eurybox_display_detected_archives ()
{
local ARCH_DESC
for (( ARCH_NUM=0;ARCH_NUM<${EURYBOX_DETECTED_ARCHIVES[NUM]};ARCH_NUM++ ))
do
local NAME=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,NAME]}
local TIME=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]}
local ENC=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,ENC]}
local COMP=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,COMP]}
local TYPE=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TYPE]}
local VERS=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,VERS]}
local FEC=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,FEC]}
local HASH=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,HASH]}
local YEAR=`echo ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]} | awk -F"_" '{ print $1 }'`
local MONTH=`echo ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]} | awk -F"_" '{ print $2 }'`
local DAY=`echo ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]} | awk -F"_" '{ print $3 }'`
local HOUR=`echo ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]} | awk -F"_" '{ print $4 }'`
local MINUTE=`echo ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]} | awk -F"_" '{ print $5 }'`
local SECOND=`echo ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]} | awk -F"_" '{ print $6 }'`
if [[ $NAME != $PREVIOUS_NAME ]]
then
echo "------------ [[ ARCHIVE: $NAME ]] ------------"
echo "YEAR \\ MONTH \\ DAY @ HOUR:MINUTE:SECOND | TYPE | VERS | ENCR | COMP | FECC | HASH | ID"
PREVIOUS_YEAR=""
PREVIOUS_MONTH=""
fi
if [[ $YEAR != $PREVIOUS_YEAR ]]
then
ARCH_DESC="$YEAR"
PREVIOUS_MONTH=""
else
ARCH_DESC=' '
fi
if [[ $MONTH != $PREVIOUS_MONTH ]]
then
ARCH_DESC="$ARCH_DESC \\ $MONTH "
else
ARCH_DESC="$ARCH_DESC "
fi
ARCH_DESC="$ARCH_DESC \\ $DAY @ $HOUR : $MINUTE : $SECOND "
case $TYPE in
vm_cold|vm_hot )
ARCH_DESC="$ARCH_DESC | VM "
;;
full_hot|full_cold )
ARCH_DESC="$ARCH_DESC | FULL"
;;
* )
ARCH_DESC="$ARCH_DESC | ? "
;;
esac
case $VERS in
"" )
ARCH_DESC="$ARCH_DESC | ? "
;;
* )
ARCH_DESC="$ARCH_DESC | $VERS "
;;
esac
case $ENC in
false )
ARCH_DESC="$ARCH_DESC | "
;;
true )
ARCH_DESC="$ARCH_DESC | X "
;;
* )
ARCH_DESC="$ARCH_DESC | ? "
;;
esac
case $COMP in
none )
ARCH_DESC="$ARCH_DESC | "
;;
tar.bz2 )
ARCH_DESC="$ARCH_DESC |BZIP2"
;;
tar.gz )
ARCH_DESC="$ARCH_DESC | GZIP"
;;
* )
ARCH_DESC="$ARCH_DESC | ? "
;;
esac
case $FEC in
none )
ARCH_DESC="$ARCH_DESC | "
;;
par2 )
ARCH_DESC="$ARCH_DESC | PAR2"
;;
zfec )
ARCH_DESC="$ARCH_DESC | ZFEC"
;;
* )
ARCH_DESC="$ARCH_DESC | ? "
;;
esac
case $HASH in
none )
ARCH_DESC="$ARCH_DESC | "
;;
sha512|sha256 )
ARCH_DESC="$ARCH_DESC | SHA "
;;
* )
ARCH_DESC="$ARCH_DESC | ? "
;;
esac
ARCH_DESC="$ARCH_DESC | $ARCH_NUM"
echo "$ARCH_DESC"
PREVIOUS_NAME=$NAME
PREVIOUS_YEAR=$YEAR
PREVIOUS_MONTH=$MONTH
done
}
#Desc: echo detected vms from archive
#1 arg required: archive_id
eurybox_display_detected_archive_vms ()
{
local ARCH_NUM=$1
local VM_DESC
local VM_NUM=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,VM_NUM]}
local NAME=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,NAME]}
local TIME=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]}
local ENC=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,ENC]}
local COMP=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,COMP]}
local TYPE=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TYPE]}
local VERS=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,VERS]}
local FEC=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,FEC]}
local HASH=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,HASH]}
local YEAR=`echo ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]} | awk -F"_" '{ print $1 }'`
local MONTH=`echo ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]} | awk -F"_" '{ print $2 }'`
local DAY=`echo ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]} | awk -F"_" '{ print $3 }'`
local HOUR=`echo ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]} | awk -F"_" '{ print $4 }'`
local MINUTE=`echo ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]} | awk -F"_" '{ print $5 }'`
local SECOND=`echo ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]} | awk -F"_" '{ print $6 }'`
echo "------------ [[ ARCHIVE: $NAME ]] ------------"
echo "Date: $YEAR/$MONTH/$DAY @ $HOUR:$MINUTE:$SECOND"
echo "Encrypted: $ENC"
echo "Compression: $COMP"
echo "FEC: $FEC"
echo "Version: $VERS"
echo "Hash: $HASH"
echo "VMs: $VM_NUM"
for (( VM_NUM=0;VM_NUM<${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,VM_NUM]};VM_NUM++ ))
do
NAME=${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,$VM_NUM,NAME]}
echo "ID: $VM_NUM => VM: $NAME"
done
}
#Desc: execute a command through ssh
#4 args required: host port user command
eurybox_exec_ssh_command ()
{
local HOST=$1
local PORT=$2
local USER=$3
local CMD=$4
local CMD_OUT
local NB_TRY_LEFT=$EURYBOX_MAX_RETRY_ON_FAILURE
local STATUS=1234
while [[ !( $STATUS -eq 0 ) && !( $NB_TRY_LEFT -eq 0 ) ]]
do
CMD_OUT=`ssh -p ${PORT} ${USER}@${HOST} "${CMD}" 2>&1`
STATUS=$?
if [[ $STATUS -eq 0 ]]
then
eurybox_display_message message TOOLS "Command execution - OK: $CMD"
eurybox_display_message debug TOOLS "Command execution output:\n$CMD_OUT"
else
NB_TRY_LEFT=$(( $NB_TRY_LEFT - 1 ))
eurybox_display_message warning TOOLS "Command execution failed - trying again in ${EURYBOX_ERROR_TEMPORISATION_TIME} seconds - error $STATUS:\n$CMD_OUT\n$NB_TRY_LEFT left"
sleep ${EURYBOX_ERROR_TEMPORISATION_TIME}
fi
done
if [[ $NB_TRY_LEFT -eq 0 ]]
then
eurybox_display_message error TOOLS "Command execution failed - $CMD:\n$CMD_OUT"
fi
}
#Desc: flush a directory
#1 arg required: flushed_directory
eurybox_flush_directory ()
{
local T_PATH=$1
local FLUSH_OUT
local STATUS
if [[ !( $T_PATH = "") && !( $T_PATH = "/" ) && !( $T_PATH == *"/etc"* ) && !( $T_PATH == *"/proc"* ) && !( $T_PATH = "/home" ) && !( $T_PATH == *"/bin"* ) && !( $T_PATH == *"/boot"*) && !( $T_PATH == *"/lib"*) && !($T_PATH == *"/lib64"*) && !($T_PATH == *"/sbin"*) && !($T_PATH == *"/usr"*) ]]
then
FLUSH_OUT=`sudo rm -Rf $T_PATH/* 2>&1`
STATUS=$?
if [[ !($STATUS -eq 0) ]]
then
eurybox_display_message warning TOOLS "Directory $T_PATH flush failed - error $STATUS:\n$FLUSH_OUT"
else
eurybox_display_message message TOOLS "Directory $T_PATH flushed"
eurybox_display_message debug TOOLS "Flush command output:\n$FLUSH_OUT"
fi
else
eurybox_display_message warning TOOLS "Forbidden directory flush attempt: $T_PATH is not removable"
fi
}