v-check-user-password 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/bash
  2. # info: check user password
  3. # options: USER PASSWORD [IP]
  4. #
  5. # example: v-check-user-password admin qwerty1234
  6. #
  7. # This function verifies user password from file
  8. #----------------------------------------------------------#
  9. # Variables & Functions #
  10. #----------------------------------------------------------#
  11. # Argument definition
  12. user=$1
  13. password=$2; HIDE=2
  14. ip=${3-127.0.0.1}
  15. # Includes
  16. # shellcheck source=/etc/hestiacp/hestia.conf
  17. source /etc/hestiacp/hestia.conf
  18. # shellcheck source=/usr/local/hestia/func/main.sh
  19. source $HESTIA/func/main.sh
  20. # load config file
  21. source_conf "$HESTIA/conf/hestia.conf"
  22. time_n_date=$(date +'%T %F')
  23. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  24. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  25. #----------------------------------------------------------#
  26. # Verifications #
  27. #----------------------------------------------------------#
  28. check_args '2' "$#" 'USER PASSWORD'
  29. is_format_valid 'user'
  30. # Checking user
  31. if [ ! -d "$HESTIA/data/users/$user" ] && [ "$user" != 'root' ]; then
  32. echo "Error: password missmatch"
  33. echo "$date $time $user $ip failed to login" >> $HESTIA/log/auth.log
  34. exit 9
  35. fi
  36. # Checking user password
  37. is_password_valid
  38. # Checking empty password
  39. if [[ -z "$password" ]]; then
  40. echo "Error: password missmatch"
  41. echo "$date $time $user $ip failed to login" >> $HESTIA/log/auth.log
  42. exit 9
  43. fi
  44. #----------------------------------------------------------#
  45. # Action #
  46. #----------------------------------------------------------#
  47. # Parsing user's salt
  48. shadow=$(grep "^$user:" /etc/shadow | cut -f 2 -d :)
  49. if echo "$shadow" | grep -qE '^\$[0-9a-z]+\$[^\$]+\$'
  50. then
  51. salt=$(echo "$shadow" |cut -f 3 -d \$)
  52. method=$(echo "$shadow" |cut -f 2 -d \$)
  53. if [ "$method" = "y" ]; then
  54. echo "Unsuported hash method";
  55. exit 1;
  56. elif [ "$method" -eq '1' ]; then
  57. method='md5'
  58. elif [ "$method" -eq '6' ]; then
  59. method='sha-512'
  60. else
  61. echo "Error: password missmatch"
  62. echo "$date $time $user $ip failed to login" >> $HESTIA/log/auth.log
  63. exit 9
  64. fi
  65. else
  66. salt=${shadow:0:2}
  67. method='des'
  68. fi
  69. if [ -z "$salt" ]; then
  70. echo "Error: password missmatch"
  71. echo "$date $time $user $ip failed to login" >> $HESTIA/log/auth.log
  72. exit 9
  73. fi
  74. # Generating hash
  75. set -o noglob
  76. hash=$($BIN/v-generate-password-hash "$method" "$salt" <<< "$password")
  77. if [[ -z "$hash" ]]; then
  78. echo "Error: password missmatch"
  79. echo "$date $time $user $ip failed to login" >> $HESTIA/log/auth.log
  80. exit 9
  81. fi
  82. # Checking hash
  83. result=$(grep "^$user:$hash:" /etc/shadow 2>/dev/null)
  84. if [[ -z "$result" ]]; then
  85. echo "Error: password missmatch"
  86. echo "$date $time $user $ip failed to login" >> $HESTIA/log/auth.log
  87. exit 9
  88. fi
  89. #----------------------------------------------------------#
  90. # Hestia #
  91. #----------------------------------------------------------#
  92. # Logging
  93. echo "$date $time $user $ip successfully logged in" >> $HESTIA/log/auth.log
  94. exit