Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. exit_msg() {
  4. echo $1
  5. exit $2
  6. }
  7.  
  8. ask_passwd() {
  9. stty -echo
  10. echo -n "Enter password:"
  11. read PASSWD1
  12. echo
  13. echo -n "Confirm password:"
  14. read PASSWD2
  15. echo
  16.  
  17. if [ "$PASSWD1" != "$PASSWD2" ]; then
  18. exit_msg "Password mismatch" 1
  19. fi
  20. PASSWORD=$PASSWD1
  21. stty echo
  22. }
  23.  
  24. usage() {
  25. echo "$0 [ -c ] [ -m ] [ -D ] passwdfile username"
  26. echo "$0 -b [ -c ] [ -m | -d | -p | -s ] [ -D ] passwdfile username password"
  27. echo "$0 -nb [ -m | -d | -s | -p ] username password"
  28.  
  29. exit 1
  30. }
  31.  
  32. while getopts "bcdmnpsD" FLAG; do
  33. case "$FLAG" in
  34. b) BATCH_MODE=bopt ;;
  35. c) CREATE_PASSWD=copt ;;
  36. d) ALGO=crypt;;
  37. n) DISPLAY_RESULT=nopt ;;
  38. m) ALGO=md5;;
  39. s) ALGO=sha;;
  40. p) ALGO=plaintext;;
  41. D) DELETE_USER=Dopt;;
  42. *) usage;;
  43. esac
  44. done
  45.  
  46. shift $(( $OPTIND - 1 ))
  47.  
  48. test -z "${ALGO}" && ALGO=crypt
  49.  
  50. case "${DISPLAY_RESULT}${BATCH_MODE}$#" in
  51. noptbopt2)
  52. USERNAME=$1
  53. PASSWORD=$2
  54. ;;
  55. bopt3)
  56. FILE=$1
  57. USERNAME=$2
  58. PASSWORD=$3
  59. ;;
  60. nopt1)
  61. USERNAME=$1
  62. ;;
  63. bopt2|2)
  64. FILE=$1
  65. USERNAME=$2
  66. ;;
  67. *) usage;;
  68. esac
  69.  
  70. case "${DISPLAY_RESULT}${CREATE_PASSWD}" in
  71. noptcopt)
  72. echo "you cannot display the result and create the passwd file"
  73. exit 1
  74. ;;
  75. nopt|copt) ;;
  76. *)
  77. test -f ${FILE} || exit_msg "File ${FILE} not found, use -c option to create it" 1
  78. ;;
  79. esac
  80.  
  81. test -z $PASSWORD && ask_passwd
  82.  
  83. case "$ALGO" in
  84. crypt)
  85. GENPASSWD=$(openssl passwd -crypt ${PASSWORD})
  86. ;;
  87. md5)
  88. GENPASSWD=$(openssl passwd -apr1 ${PASSWORD})
  89. ;;
  90. sha)
  91. GENPASSWD="{SHA}$(echo -n ${PASSWORD} | openssl dgst -sha1 -binary | openssl base64)"
  92. ;;
  93. plaintext)
  94. GENPASSWD=$PASSWORD
  95. ;;
  96. esac
  97.  
  98. test -n "$DISPLAY_RESULT" && exit_msg "${USERNAME}:${GENPASSWD}" 0
  99.  
  100. case "${CREATE_PASSWD}" in
  101. copt)
  102. echo "${USERNAME}:${GENPASSWD}" > $FILE
  103. ;;
  104. *)
  105. echo "${USERNAME}:${GENPASSWD}" >> $FILE
  106. ;;
  107. esac
  108.  
  109. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement