v-check-user-password 2.8 KB

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