Given a linux username and a password how can I test if it is a valid account?

前端 未结 3 842
隐瞒了意图╮
隐瞒了意图╮ 2020-12-24 06:38

So my question is straight forward given a linux username and a password how can I test if it is a valid account?

3条回答
  •  醉话见心
    2020-12-24 06:45

    #! /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 ${@}
    

提交回复
热议问题