diff --git a/boring-nginx/ngxpasswd b/boring-nginx/ngxpasswd index 03e834b..de1cc42 100644 --- a/boring-nginx/ngxpasswd +++ b/boring-nginx/ngxpasswd @@ -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 - exit 1 -elif [ -f $NAME.htpasswd ]; then - echo "$NAME.htpasswd exists, aborting" 1>&2 - exit 1 -fi +while [ "$NAME" == "" ]; do + read -p "Name: " NAME + if [ -f "/passwds/$NAME.htpasswd" ]; then + echo "ERROR: /passwds/$NAME.htpasswd already exists." + exit 1 + 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/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;" diff --git a/boring-nginx/vhost_passwd.conf b/boring-nginx/vhost_passwd.conf new file mode 100644 index 0000000..39ef4ca --- /dev/null +++ b/boring-nginx/vhost_passwd.conf @@ -0,0 +1,2 @@ + auth_basic "Who's this?"; + auth_basic_user_file /passwds/.htpasswd;