Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- exit_msg() {
- echo $1
- exit $2
- }
- ask_passwd() {
- stty -echo
- echo -n "Enter password:"
- read PASSWD1
- echo
- echo -n "Confirm password:"
- read PASSWD2
- echo
- if [ "$PASSWD1" != "$PASSWD2" ]; then
- exit_msg "Password mismatch" 1
- fi
- PASSWORD=$PASSWD1
- stty echo
- }
- usage() {
- echo "$0 [ -c ] [ -m ] [ -D ] passwdfile username"
- echo "$0 -b [ -c ] [ -m | -d | -p | -s ] [ -D ] passwdfile username password"
- echo "$0 -nb [ -m | -d | -s | -p ] username password"
- exit 1
- }
- while getopts "bcdmnpsD" FLAG; do
- case "$FLAG" in
- b) BATCH_MODE=bopt ;;
- c) CREATE_PASSWD=copt ;;
- d) ALGO=crypt;;
- n) DISPLAY_RESULT=nopt ;;
- m) ALGO=md5;;
- s) ALGO=sha;;
- p) ALGO=plaintext;;
- D) DELETE_USER=Dopt;;
- *) usage;;
- esac
- done
- shift $(( $OPTIND - 1 ))
- test -z "${ALGO}" && ALGO=crypt
- case "${DISPLAY_RESULT}${BATCH_MODE}$#" in
- noptbopt2)
- USERNAME=$1
- PASSWORD=$2
- ;;
- bopt3)
- FILE=$1
- USERNAME=$2
- PASSWORD=$3
- ;;
- nopt1)
- USERNAME=$1
- ;;
- bopt2|2)
- FILE=$1
- USERNAME=$2
- ;;
- *) usage;;
- esac
- case "${DISPLAY_RESULT}${CREATE_PASSWD}" in
- noptcopt)
- echo "you cannot display the result and create the passwd file"
- exit 1
- ;;
- nopt|copt) ;;
- *)
- test -f ${FILE} || exit_msg "File ${FILE} not found, use -c option to create it" 1
- ;;
- esac
- test -z $PASSWORD && ask_passwd
- case "$ALGO" in
- crypt)
- GENPASSWD=$(openssl passwd -crypt ${PASSWORD})
- ;;
- md5)
- GENPASSWD=$(openssl passwd -apr1 ${PASSWORD})
- ;;
- sha)
- GENPASSWD="{SHA}$(echo -n ${PASSWORD} | openssl dgst -sha1 -binary | openssl base64)"
- ;;
- plaintext)
- GENPASSWD=$PASSWORD
- ;;
- esac
- test -n "$DISPLAY_RESULT" && exit_msg "${USERNAME}:${GENPASSWD}" 0
- case "${CREATE_PASSWD}" in
- copt)
- echo "${USERNAME}:${GENPASSWD}" > $FILE
- ;;
- *)
- echo "${USERNAME}:${GENPASSWD}" >> $FILE
- ;;
- esac
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement