Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $id$salt$encrypted
- ID | Method
- ---------------------------------------------------------
- 1 | MD5
- 2a | Blowfish (not in mainline glibc; added in some
- | Linux distributions)
- 5 | SHA-256 (since glibc 2.7)
- 6 | SHA-512 (since glibc 2.7)
- mkpasswd -5 <the_salt> <the_password>
- #!/bin/bash
- #
- # login.sh $USERNAME $PASSWORD
- #this script doesn't work if it is run as root, since then we don't have to specify a pw for 'su'
- if [ $(id -u) -eq 0 ]; then
- echo "This script can't be run as root." 1>&2
- exit 1
- fi
- if [ ! $# -eq 2 ]; then
- echo "Wrong Number of Arguments (expected 2, got $#)" 1>&2
- exit 1
- fi
- USERNAME=$1
- PASSWORD=$2
- #since we use expect inside a bash-script, we have to escape tcl-$.
- expect << EOF
- spawn su $USERNAME -c "exit"
- expect "Password:"
- send "$PASSWORDr"
- #expect eof
- set wait_result [wait]
- # check if it is an OS error or a return code from our command
- # index 2 should be -1 for OS erro, 0 for command return code
- if {[lindex $wait_result 2] == 0} {
- exit [lindex $wait_result 3]
- }
- else {
- exit 1
- }
- EOF
- #! /bin/bash
- # (GPL3+) Alberto Salvia Novella (es20490446e)
- passwordHash () {
- password=${1}
- salt=${2}
- encryption=${3}
- hashes=$(echo ${password} | openssl passwd -${encryption} -salt ${salt} -stdin)
- echo $(substring ${hashes} "$" "3")
- }
- passwordIsValid () {
- user=${1}
- password=${2}
- encryption=$(secret "encryption" ${user})
- salt=$(secret "salt" ${user})
- salted=$(secret "salted" ${user})
- hash=$(passwordHash ${password} ${salt} ${encryption})
- [ ${salted} = ${hash} ] && echo "true" || echo "false"
- }
- secret () {
- secret=${1}
- user=${2}
- shadow=$(shadow ${user})
- if [ ${secret} = "encryption" ]; then
- position=1
- elif [ ${secret} = "salt" ]; then
- position=2
- elif [ ${secret} = "salted" ]; then
- position=3
- fi
- echo $(substring ${shadow} "$" ${position})
- }
- shadow () {
- user=${1}
- shadow=$(cat /etc/shadow | grep ${user})
- shadow=$(substring ${shadow} ":" "1")
- echo ${shadow}
- }
- substring () {
- string=${1}
- separator=${2}
- position=${3}
- substring=${string//"${separator}"/$'2'}
- IFS=$'2' read -a substring <<< "${substring}"
- echo ${substring[${position}]}
- }
- passwordIsValid ${@}
Add Comment
Please, Sign In to add comment