v-log-user-login 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/bash
  2. # info: add user login
  3. # options: USER IP STATUS [FINGERPRINT]
  4. #----------------------------------------------------------#
  5. # Variables & Functions #
  6. #----------------------------------------------------------#
  7. # Argument definition
  8. user=$1
  9. ip=$2
  10. status=$3
  11. session_id=$4
  12. user_agent=$5
  13. authlog="${6-no}"
  14. reason="${7}"
  15. active="yes"
  16. if [ "$status" = "failed" ]; then
  17. active="no"
  18. fi
  19. # Includes
  20. # shellcheck source=/etc/hestiacp/hestia.conf
  21. source /etc/hestiacp/hestia.conf
  22. # shellcheck source=/usr/local/hestia/func/main.sh
  23. source $HESTIA/func/main.sh
  24. # load config file
  25. source_conf "$HESTIA/conf/hestia.conf"
  26. #----------------------------------------------------------#
  27. # Verifications #
  28. #----------------------------------------------------------#
  29. check_args '2' "$#" 'USER IP SESSION_ID USER_AGENT [AUTHLOG] [REASON]'
  30. is_format_valid 'user' 'ip'
  31. is_common_format_valid "$session_id" "SESSION_ID"
  32. format_no_quotes "$user_agent" "USER_AGENT"
  33. is_common_format_valid "$authlog" "AUTHLOG"
  34. format_no_quotes "$reason" "REASON"
  35. is_object_valid 'user' 'USER' "$user"
  36. # Generating timestamp
  37. time_n_date=$(date +'%T %F')
  38. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  39. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  40. if [ ! -f "$USER_DATA/auth.log" ]; then
  41. touch "$USER_DATA/auth.log"
  42. fi
  43. #----------------------------------------------------------#
  44. # Action #
  45. #----------------------------------------------------------#
  46. echo "DATE='$date' TIME='$time' IP='$ip' ACTION='login' STATUS='$status' USER_AGENT='$user_agent' SESSION='$session_id' ACTIVE='$active'" >> "$USER_DATA/auth.log"
  47. if [ "$authlog" = "yes" ]; then
  48. echo "$date $time $user $ip failed to login ($reason)" >> "$HESTIA/log/auth.log"
  49. fi
  50. #----------------------------------------------------------#
  51. # Hestia #
  52. #----------------------------------------------------------#
  53. exit