aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron LI <aly@aaronly.me>2017-09-09 13:33:02 +0800
committerAaron LI <aly@aaronly.me>2017-09-09 13:33:02 +0800
commit55731ee0a77d3798d2ce10ed72b9c59ef342e09d (patch)
tree2191e9bba93879ded4c3d16ce8d1d2ce0fa07e4f
parent9fbeb8cb3aafa86f83787c660ae1354c997cb490 (diff)
downloadatoolbox-55731ee0a77d3798d2ce10ed72b9c59ef342e09d.tar.bz2
Add htpasswd.sh script
-rwxr-xr-xcli/htpasswd.sh40
1 files changed, 40 insertions, 0 deletions
diff --git a/cli/htpasswd.sh b/cli/htpasswd.sh
new file mode 100755
index 0000000..df5c297
--- /dev/null
+++ b/cli/htpasswd.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+#
+# Generate htpasswd (Apache password file) without installing Apache tools.
+#
+# Credit:
+# * NGINX: FAQ
+# https://www.nginx.com/resources/wiki/community/faq/
+# * How to get a password from a shell script without echoing
+# https://stackoverflow.com/a/3980713/4856091
+#
+#
+# Aaron LI
+# 2017-09-09
+#
+
+if [ $# -ne 1 ]; then
+ echo "usage: ${0##*/} <username>"
+ exit 1
+fi
+
+USER="$1"
+
+# Disable echo ...
+stty -echo
+printf "Password: "
+read PASS1
+printf "\n"
+printf "Password (again): "
+read PASS2
+printf "\n"
+# Enable echo
+stty echo
+
+if [ "${PASS1}" != "${PASS2}" ]; then
+ echo "ERROR: passwords do not match!"
+ exit 2
+fi
+
+# Use the UNIX "crypt" encryption
+printf "${USER}:$(openssl passwd -crypt ${PASS1})\n"