update ngxpasswd

This commit is contained in:
Wonderfall 2016-05-30 14:19:44 +02:00
parent ef3c04060d
commit 0bc0bd5839
2 changed files with 39 additions and 17 deletions

View File

@ -1,29 +1,32 @@
#!/bin/sh
NAME="$1"
USER="$2"
PASSWORD="$3"
echo ""
echo "Welcome to ngxpasswd utility."
echo "We're about to create a password file."
echo ""
cd /passwds || exit 1
if [ -z "$NAME" ]; then
echo "Service name must be defined" 1>&2
while [ "$NAME" == "" ]; do
read -p "Name: " NAME
if [ -f "/passwds/$NAME.htpasswd" ]; then
echo "ERROR: /passwds/$NAME.htpasswd already exists."
exit 1
elif [ -f $NAME.htpasswd ]; then
echo "$NAME.htpasswd exists, aborting" 1>&2
exit 1
fi
fi
done
if [ -z "$USER" ]; then
echo "User must be defined" 1>&2
exit 1
fi
while [ "$USER" == "" ]; do
read -p "User: " USER
done
if [ -z "$PASSWORD" ]; then
PASSWORD=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1`
read -p "Password (leave blank to generate one): " PASSWORD
if [ "$PASSWORD" == "" ]; then
echo "Password was not defined, generating a random one..."
PASSWORD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1)
fi
echo -n $USER:`openssl passwd -apr1 $PASSWORD` >> $NAME.htpasswd
echo -n $USER:$(openssl passwd -apr1 $PASSWORD) >> $NAME.htpasswd
chown $UID:$GID $NAME.htpasswd
chmod 640 $NAME.htpasswd
@ -33,6 +36,23 @@ echo "- Service : $NAME"
echo "- User : $USER"
echo "- Password : $PASSWORD"
echo
if [ -f "/sites-enabled/$NAME.conf" ]; then
echo "vhost at /sites-enabled/$NAME.conf detected."
while [[ "$ADD" != "y" && "$ADD" != "n" ]]; do
read -p "Add authentication to $NAME.conf? [y/n]: " ADD
done
if [ "$ADD" == "y" ]; then
cd /etc/nginx/conf
sed -i "/location/r vhost_passwd.conf" /sites-enabled/$NAME.conf
sed -i "s/<NAME>/$NAME/g" /sites-enabled/$NAME.conf
echo "Automatically added, please verify. Otherwise follow these instructions."
echo
fi
fi
echo "Paste this to your vhost in order to enable auth :"
echo " auth_basic \"Who's this?\";"
echo " auth_basic_user_file /passwds/$NAME.htpasswd;"

View File

@ -0,0 +1,2 @@
auth_basic "Who's this?";
auth_basic_user_file /passwds/<NAME>.htpasswd;