#!/bin/bash
# info: add user login
# options: USER IP STATUS [FINGERPRINT]

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
user=$1
ip=$2
status=$3
session_id=$4
user_agent=$5
authlog="${6-no}"
reason="${7}"

active="yes"
if [ "$status" = "failed" ]; then
	active="no"
fi

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

check_args '2' "$#" 'USER IP SESSION_ID USER_AGENT [AUTHLOG] [REASON]'
is_format_valid 'user' 'ip'
is_common_format_valid "$session_id" "SESSION_ID"
format_no_quotes "$user_agent" "USER_AGENT"
is_common_format_valid "$authlog" "AUTHLOG"
format_no_quotes "$reason" "REASON"

is_object_valid 'user' 'USER' "$user"

# Generating timestamp
time_n_date=$(date +'%T %F')
time=$(echo "$time_n_date" | cut -f 1 -d \ )
date=$(echo "$time_n_date" | cut -f 2 -d \ )

if [ ! -f "$USER_DATA/auth.log" ]; then
	touch "$USER_DATA/auth.log"
fi

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

echo "DATE='$date' TIME='$time' IP='$ip' ACTION='login' STATUS='$status' USER_AGENT='$user_agent' SESSION='$session_id' ACTIVE='$active'" >> "$USER_DATA/auth.log"

if [ "$authlog" = "yes" ]; then
	echo "$date $time $user $ip failed to login ($reason)" >> "$HESTIA/log/auth.log"
fi

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

exit
