Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2018
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1.  
  2. Ask Question
  3. 12
  4.  
  5. The scenario:
  6.  
  7. In a bash script, I have to check if a password given by a user is a valid user password.
  8.  
  9. I.e suppose I have a user A with password PA.. In the script I asked user A to enter his password, So how to check if the string entered is really his password?...
  10. bash
  11. shareimprove this question
  12. edited May 7 '15 at 11:46
  13. asked Apr 20 '15 at 10:45
  14. Maythux
  15. 50.2k32165214
  16.  
  17. What do you mean with a valid password? Want to test if it really is the user's password? – A.B. Apr 20 '15 at 10:51
  18. @A.B. valid password=login password whatever you want to call... I mean its a password for this user – Maythux Apr 20 '15 at 10:51
  19. @A.B. I know it will be a security hole but here you already know the user name also... In other words its just a test if the password is for this user.. – Maythux Apr 20 '15 at 10:54
  20. 3
  21. Here is your answer: unix.stackexchange.com/a/21728/107084 – A.B. Apr 20 '15 at 10:54
  22. A solution based on expect at stackoverflow.com/a/1503831/320594. – Jaime Hablutzel Feb 1 at 16:25
  23.  
  24. add a comment
  25. 3 Answers
  26. active
  27. oldest
  28. votes
  29. 11
  30.  
  31. Since you want to do this in a shell script, a couple of contributions in How to check password with Linux? (on Unix.SE, suggested by A.B.) are especially relevant:
  32.  
  33. rozcietrzewiacz's answer on generating a password hash that matches an entry in /etc/shadow gives part of the solution.
  34. Daniel Alder's comment explains the different syntax of the mkpasswd command present in Debian (and Ubuntu).
  35.  
  36. To manually check if a string is really some user's password, you must hash it with the same hash algorithm as in the user's shadow entry, with the same salt as in the user's shadow entry. Then it can be compared with the password hash stored there.
  37.  
  38. I've written a complete, working script demonstrating how to do this.
  39.  
  40. If you name it chkpass, you can run chkpass user and it will read a line from standard input and check if it's user's password.
  41. Install the whois Install whois package to obtain the mkpasswd utility on which this script depends.
  42. This script must be run as root to succeed.
  43. Before using this script or any part of it to do real work, please see Security Notes below.
  44.  
  45. #!/usr/bin/env bash
  46.  
  47. xcorrect=0 xwrong=1 enouser=2 enodata=3 esyntax=4 ehash=5 IFS=$
  48. die() {
  49. printf '%s: %s\n' "$0" "$2" >&2
  50. exit $1
  51. }
  52. report() {
  53. if (($1 == xcorrect))
  54. then echo 'Correct password.'
  55. else echo 'Wrong password.'
  56. fi
  57. exit $1
  58. }
  59.  
  60. (($# == 1)) || die $esyntax "Usage: $(basename "$0") <username>"
  61. case "$(getent passwd "$1" | awk -F: '{print $2}')" in
  62. x) ;;
  63. '') die $enouser "error: user '$1' not found";;
  64. *) die $enodata "error: $1's password appears unshadowed!";;
  65. esac
  66.  
  67. if [ -t 0 ]; then
  68. IFS= read -rsp "[$(basename "$0")] password for $1: " pass
  69. printf '\n'
  70. else
  71. IFS= read -r pass
  72. fi
  73.  
  74. set -f; ent=($(getent shadow "$1" | awk -F: '{print $2}')); set +f
  75. case "${ent[1]}" in
  76. 1) hashtype=md5;; 5) hashtype=sha-256;; 6) hashtype=sha-512;;
  77. '') case "${ent[0]}" in
  78. \*|!) report $xwrong;;
  79. '') die $enodata "error: no shadow entry (are you root?)";;
  80. *) die $enodata 'error: failure parsing shadow entry';;
  81. esac;;
  82. *) die $ehash "error: password hash type is unsupported";;
  83. esac
  84.  
  85. if [[ "${ent[*]}" = "$(mkpasswd -sm $hashtype -S "${ent[2]}" <<<"$pass")" ]]
  86. then report $xcorrect
  87. else report $xwrong
  88. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement