aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDennis Eriksen <d@ennis.no>2021-03-29 22:40:40 +0200
committerDennis Eriksen <d@ennis.no>2021-03-29 22:40:40 +0200
commitc794993e1c024a20ad035944fa2dd931b4109e19 (patch)
tree007ef7732cf09dbef8e135364141bb7de749afc1
downloadhtmpasswd-c794993e1c024a20ad035944fa2dd931b4109e19.tar.gz
Initial commit
-rwxr-xr-xhtmpasswd131
1 files changed, 131 insertions, 0 deletions
diff --git a/htmpasswd b/htmpasswd
new file mode 100755
index 0000000..9aff7b7
--- /dev/null
+++ b/htmpasswd
@@ -0,0 +1,131 @@
+#!/usr/bin/env bash
+#
+# made by Dennis Eriksen <dnns.no> in 2021
+#
+# This script helps create temporary users for htpasswd-files
+
+# Exit on error. Append "|| true" if you expect an error.
+set -o errexit
+# Exit on error inside any functions or subshells.
+set -o errtrace
+# Do not allow use of undefined vars. Use ${VAR:-} to use an undefined VAR
+set -o nounset
+set -x
+
+# Directory where htpasswd-files are stored
+HTDIR=/etc/nginx/htpasswd
+
+# Spool-directory where temporary users are stored until they can be deleted
+SPOOLDIR=/var/spool/htmpasswd
+
+# Temporary time, with minute precicion
+# Must be valid with `date`
+# Check by doing `date --date "$TIME"`
+TIME="+60 minutes"
+
+USER_LENGTH=6
+PASS_LENGTH=6
+
+die() {
+ echo "$@"
+ exit 1
+}
+
+checkrw() {
+ [[ -r "${1}" ]] || die "Can not read ${1}"
+ [[ -w "${1}" ]] || die "Can not write to ${1}"
+}
+
+
+
+main() {
+ local HTFILE SPOOLFILE USER PASS
+ # Check if $1 exists
+ [[ "${1:-}" == "" ]] && die "You need to spcecify the name of the htpasswd-file you wish to add a temporary user to."
+ HTFILE="${HTDIR}/${1}"
+ SPOOLFILE="${SPOOLDIR}/${1}"
+
+
+ # We need to be able to read and write $HTFILE
+ [[ -e "${HTFILE}" ]] || die "${HTFILE} does not exist."
+ checkrw "${HTFILE}"
+
+ # We need to be able to read and write to $SPOOLFILE
+ [[ -e "${SPOOLFILE}" ]] || touch "${SPOOLFILE}"
+ checkrw "${SPOOLFILE}"
+
+ if [[ "${2:-x}" != "x" ]]; then
+ USER="${2}"
+ else
+ USER="$(< /dev/urandom tr -dc a-z | head -c${USER_LENGTH})"
+ fi
+ PASS="$(cat /dev/urandom | tr -dc a-z0-9 | head -c${PASS_LENGTH})"
+
+ echo "${USER}: $(date --iso-8601=minutes --date="${TIME}")" >> "${SPOOLFILE}"
+ echo "${PASS}" | htpasswd -i "${HTFILE}" "${USER}"
+
+ echo "${USER}:${PASS}"
+
+}
+
+cronrun() {
+ local HTFILE SPOOLFILE TMPFILE USER DELTIME file line
+
+ # For every spoolfile in the $SPOOLDIR
+ for file in "${SPOOLDIR}"/*; do
+ # Skip loop if file does not exist - i.e., there are NO files in the dir.
+ [[ -e "${file}" ]] || continue
+
+ # $file should be just the filename
+ file=$(basename "${file}")
+
+ HTFILE="${HTDIR}/${file}"
+ checkrw "${HTFILE}"
+
+ SPOOLFILE="${SPOOLDIR}/${file}"
+ checkrw "${SPOOLFILE}"
+
+ # Need to create a copy of the spoolfile, since we can't read and write to the same file.
+ TMPFILE=$(mktemp)
+ cp "${SPOOLFILE}" "${TMPFILE}"
+
+ # Read the file!
+ while read -r line; do
+ USER=$(echo "${line}" | awk '{print $1}')
+ DELTIME=$(echo "${line}" | awk '{print $2}')
+
+ # If $DELTIME is in the past, delete it
+ if [[ $(date --date="${DELTIME}" +%s) -lt $(date +%s) ]]; then
+ echo "DELETE THAT BITCH"
+ sed -i "/^${USER}/d" "${HTFILE}"
+ sed -i "/^${USER}/d" "${SPOOLFILE}"
+ fi
+ done < "${TMPFILE}"
+
+ # Delete the ttempfile
+ rm "${TMPFILE}"
+
+ # Delete the spoolfile if it is now empty
+ [[ $(wc -c < "${SPOOLFILE}") == 0 ]] && rm "${SPOOLFILE}"
+
+ done
+
+}
+
+# Common checks
+# We need to be able to read $HTDIR
+# We don't have to be able to write to $HTDIR, since the file HAS to exist BEFORE we run this script
+[[ -e "${HTDIR}" ]] || die "${HTDIR} does not exist"
+checkrw "${HTDIR}"
+
+# We need to be able to read and write to $SPOOLDIR
+[[ -d "${SPOOLDIR}" ]] || mkdir -p "${SPOOLDIR}"
+checkrw "${SPOOLDIR}"
+
+# Run
+if ${RUN_BY_CRON:-false}; then
+ cronrun
+else
+ main "$@"
+fi
+