#!/bin/bash

######################################################################################################################
# BASH Server-Side Login/Generic script
# by Giovanni Frainer <user-e1d5cb184a81@xymon.invalid>
#
# Based on the Perl version and Hobbit documentation made by
# Henrik Storner <user-ce4a2c883f75@xymon.invalid> 
#
#
# 24.03.2009:
# Modified by Alexander Bech <user-bd67b1e4c445@xymon.invalid> for better performance
# - removed all cats, greps, awks and seds
# - skipping the rest of client message after section are grab
# - skipping the messages from clients without defined check in the bb-hosts line
# - periodic reload the bb-hosts file
#
#
# Instalation:
# 	1. Put this script in server/ext/rootlogin.sh (chmod +x on the file)
#	2. Insert the name of this script inside every host monitored by in server/etc/bb-hosts
#		Ex: 10.10.10.10 servername.domain.com # login
#	3. Register the script to HobbitLaunch, in server/etc/hobbitlaunch.cfg
#		[rootlogin]
#			ENVFILE /home/hobbit/server/etc/hobbitserver.cfg
#			NEEDS hobbitd
#			CMD hobbitd_channel --channel=client --log=$BBSERVERLOGS/rootlogin.log $BBHOME/ext/rootlogin.sh
#	4. Restart hobbit and wait 5 min max. :-)
#######################################################################################################################
#
LC_ALL=C
LANG=C
LANGUAGE=C
#
# The home of your Hobbit Server
# BBHOME="/home/hobbit/server"
echo "start..."
# Global Variables
#. ${BBHOME}/etc/hobbitserver.cfg

# The name of the column
COLUMN="login"

# The section in client-data that
# will be stored
GRABSECTION="who"

######################### The Test itself ###############################
# this function will be called on every event

readhostservice() {
    #echo -n "${READDELAY} "
    if [ ${READDELAY} -gt 1 ]
    then 
	((READDELAY--))
	return 0
    fi
    echo -n "loading bb-hosts..."
    BBHOSTSLINES=$(${BBHOME}/bin/bbhostgrep ${COLUMN})
    READDELAY=10
}

findhostservice() {
    CLIENT=$1; COLUMN=$2
    ORIGIFS=${IFS}; IFS=$'\n'
    for HOSTLINE in ${BBHOSTSLINES}; do
	IFS=$' 	\n'
	set $HOSTLINE
	if [ "$2" == "$CLIENT" ]; then return 0; fi	
    done
    return 1
}

process() {
	COLOR="green"
        MSG="&green No root login at the moment."

	MACHINE="$1"
	ORIGIFS=${IFS}; IFS=$'\n'
	for USERLOGIN in ${LINES}; do

	    ### The script itself ###
	    if [ "${USERLOGIN:0:4}" == "root" ]; then
		# red maybe ?
		MSG="&yellow Root logged in!"
		COLOR="yellow"
	    fi
	done
	IFS=${ORIGIFS}
	echo "$COLOR"
	$BB $BBDISP "status $MACHINE.$COLUMN $COLOR `date`
${MSG}

${LINES}"
}

###########################################################################

# Part of the script who handles the requests and send them to the
# process function

declare -i READDELAY=0
BBHOSTSLINES=""
FOUND=0
STARTED=0
SKIP=0
SKIPCLIENT=0

while read LINE; do

	# Skip the rest of message, if section filled
	if [ ${SKIP} -eq 1 ] && [ ${#LINE} -ne 2 ]; then continue
	    elif [ "${LINE}" == "@@" ]
	    then
		#echo "process"
		if [ "${SKIPCLIENT}" == "0" ]; then
		    process ${CLIENT}
		else
                    echo " no ${COLUMN}-check defined. skip."
		fi
		STARTED=0
		SKIP=0; SKIPCLIENT=0
		continue
	    fi

	# Register the Client Name	
	if [ ${STARTED} -eq 0 ] && [ ${LINE:0:8} == "@@client" ]; then
	    ORIGIFS=${IFS}
	    IFS="|"
	    set $LINE
		CLIENT=$4
		echo -n "$CLIENT..."
		IFS=${ORIGIFS}
		readhostservice
		findhostservice ${CLIENT} ${COLUMN}
		result=$?
		if [ "${result}" == "0" ]
		then
		    STARTED=1
		else
		    SKIP=1; SKIPCLIENT=1
		fi
	    continue
	fi

	# Found the section!
	if [ "${LINE}" == "[${GRABSECTION}]" ]; then
		FOUND=1
		LINES=""
		continue
	fi

	# Register the section of the client-data to the LINES variable
	if [ "${LINE:0:1}" != "[" ] && [ ${FOUND} -eq 1 ]; then 
		#echo ${LINE}
		LINES=$(echo -e "${LINES}\n${LINE}")
	# Section ended
	elif [ ${FOUND} -eq 1 ]; then
		FOUND=0
		SKIP=1
		continue
	fi
done

# Cleanup
exit 0

