Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Manage .htpasswd files
  4.  
  5.  
  6. # Store script name for use in output.
  7. me=$( basename $0 )
  8.  
  9.  
  10. # Utility function for exiting.
  11. die () {
  12. echo -e "\n${me}: ${1}, exitting...\n" >&2
  13. exit 1
  14. }
  15.  
  16.  
  17. # Die immediately if not root.
  18. [ $( id -u ) -gt 0 ] && die "You are not root, do 'sudo $0'"
  19.  
  20.  
  21. # Read options.
  22. for i in "$@"
  23. do
  24. case $i in
  25. --files=*)
  26. FILES="${i#*=}" # Space seperated list of .htpasswd file paths under /var/www.
  27. shift # past argument=value
  28. ;;
  29. --name=*)
  30. USERNAME="${i#*=}" # Username to set password for.
  31. shift # past argument=value
  32. ;;
  33. --password=*)
  34. PLAINTEXT_PASSWORD="${i#*=}" # Plaintext password to be hashed.
  35. shift # past argument=value
  36. ;;
  37. *)
  38. # Unknown option.
  39. ;;
  40. esac
  41. done
  42.  
  43.  
  44. # Prompt for needed vars if missing.
  45. [ -z "$USERNAME" ] && read -p "What username do you want to create/update: " USERNAME
  46. [ -z "$PLAINTEXT_PASSWORD" ] && read -p "Enter the new (plain text) password: " PLAINTEXT_PASSWORD
  47.  
  48.  
  49. # Limit things to the webroot.
  50. www_dir='/var/www';
  51. if [[ ! -d $www_dir ]]; then
  52. die "Error: webroot not found"
  53. fi
  54.  
  55.  
  56. # If no site(s) passed as arg, find all within webroot.
  57. if [ -z "${FILES}" ]; then
  58. for htpasswd_path in $( cd $www_dir ; find . -type f -iname '.htpasswd' | sort -n ); do
  59. FILES="${FILES} ${htpasswd_path}"
  60. done
  61. fi
  62.  
  63.  
  64. # Make sure we're in the webroot.
  65. cd $www_dir
  66.  
  67.  
  68. echo -e "\n<== Starting ==>\n"
  69.  
  70.  
  71. # Loop over files.
  72. for file in ${FILES}; do
  73.  
  74. # Pre-flight checks.
  75. [ -z $file ] && \
  76. die "Error: no .htpasswd file(s) found"
  77. [ -r ${file} ] || \
  78. die "Error: '${file}' does not exist"
  79.  
  80. # Create / modify value for user in *existing* .htpasswd.
  81. echo -e "==> Processing ${file}"
  82. htpasswd -b ${file} "${USERNAME}" "${PLAINTEXT_PASSWORD}"
  83.  
  84. done;
  85.  
  86.  
  87. echo -e "\n<== Done ==>"
  88. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement