2017-09-16 20:37:13 +00:00
|
|
|
#!/bin/bash
|
|
|
|
source /opt/webinoly/lib/general
|
|
|
|
opt="$1"
|
|
|
|
|
|
|
|
# Http-Authentication Plugin
|
|
|
|
# Syntax: httpauth <option>
|
|
|
|
# Options: -add, -delete, -list, -wp-admin-on, -wp-admin-off
|
|
|
|
|
|
|
|
if [[ ! $(conf_read nginx) == "true" ]]; then
|
|
|
|
echo ""
|
|
|
|
echo "${red} NGINX is required to get HTTP Authentication working properly! ${end}"
|
|
|
|
echo ""
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2018-04-03 02:42:29 +00:00
|
|
|
# Extract value if exist
|
|
|
|
if [[ $opt == "-add="* || $opt == "-delete="* ]]; then
|
|
|
|
value=$(echo "${opt}" | cut -d'=' -f 2 -s)
|
|
|
|
opt=$(echo "${opt}" | cut -d'=' -f 1 -s)
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
if [[ $opt == "-add" ]]; then
|
|
|
|
if [[ -z $value ]]; then
|
|
|
|
echo ""
|
|
|
|
read -p "${blu}HTTP-Auth User: ${end}" user
|
|
|
|
read -p "${blu}HTTP-Auth Password: ${end}" pass
|
|
|
|
echo ""
|
|
|
|
else
|
|
|
|
userdata=${value:1:-1}
|
|
|
|
user=$(echo "${userdata}" | cut -d',' -f 1 -s)
|
|
|
|
pass=$(echo "${userdata}" | cut -d',' -f 2 -s)
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [[ -n $user && -n $pass && $user =~ ^[^:]+$ && $pass =~ ^[^:]+$ ]]; then
|
|
|
|
echo "${red}Please, enter a valid username and password!${end}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
[[ -a /etc/nginx/.htpasswd ]] && exist=$( grep -E "^${user}:" /etc/nginx/.htpasswd )
|
2017-09-16 20:37:13 +00:00
|
|
|
if [[ -z $exist ]]; then
|
|
|
|
sudo sh -c "echo -n '$user:$(openssl passwd -1 $pass)\n' >> /etc/nginx/.htpasswd"
|
|
|
|
echo "${gre}User '$user' has been added successfully!${end}"
|
|
|
|
else
|
|
|
|
echo "${red}User '$user' already exist!${end}"
|
|
|
|
fi
|
|
|
|
echo ""
|
2018-04-03 02:42:29 +00:00
|
|
|
elif [[ $opt == "-delete" ]]; then
|
|
|
|
[[ -z $value ]] && read -p "${blu}HTTP-Auth User: ${end}" userpurge || userpurge=$value
|
2018-04-25 20:13:45 +00:00
|
|
|
[[ -a /etc/nginx/.htpasswd ]] && exist=$( grep -E "^${userpurge}:" /etc/nginx/.htpasswd )
|
|
|
|
if [[ -z $exist ]]; then
|
2018-04-03 02:42:29 +00:00
|
|
|
echo "${red}User '$userpurge' does not exist!${end}"
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-04-05 17:37:20 +00:00
|
|
|
sudo sed -i "/^$userpurge:/d" /etc/nginx/.htpasswd
|
2017-09-16 20:37:13 +00:00
|
|
|
echo "${gre}User '$userpurge has been deleted successfully!${end}"
|
2018-04-03 02:42:29 +00:00
|
|
|
elif [[ $opt == "-list" ]]; then
|
|
|
|
[[ ! -a /etc/nginx/.htpasswd ]] && exit 0
|
2017-09-16 20:37:13 +00:00
|
|
|
echo "${gre}"
|
|
|
|
cat /etc/nginx/.htpasswd | while read line
|
|
|
|
do
|
|
|
|
# Show only the user_name part, cut encrypted password string
|
|
|
|
echo "- $line" | cut -f 1 -d ':'
|
|
|
|
done
|
|
|
|
echo "${end}"
|
2018-04-03 02:42:29 +00:00
|
|
|
elif [[ $opt == "-wp-admin-on" ]]; then
|
2017-09-16 20:37:13 +00:00
|
|
|
iswpadon=$( grep -F "acl.conf;" /etc/nginx/common/wpcommon.conf )
|
|
|
|
if [[ -z $iswpadon ]]; then
|
|
|
|
sudo sed -i "/zone=one/a \ include common/acl.conf;" /etc/nginx/common/wpcommon.conf
|
|
|
|
sudo service nginx reload
|
|
|
|
conf_write wp-admin-auth true
|
|
|
|
echo "${gre} WordPress admin authentication has been enabled! ${end}"
|
|
|
|
else
|
|
|
|
echo "${gre} HTTP Authentication for WP Admin pages is already enabled! ${end}"
|
|
|
|
fi
|
|
|
|
|
2018-04-03 02:42:29 +00:00
|
|
|
elif [[ $opt == "-wp-admin-off" ]]; then
|
2017-09-16 20:37:13 +00:00
|
|
|
sudo sed -i "/acl.conf/d" /etc/nginx/common/wpcommon.conf
|
|
|
|
sudo service nginx reload
|
|
|
|
conf_write wp-admin-auth purged
|
|
|
|
echo "${gre} WordPress admin authentication has been disabled! ${end}"
|
|
|
|
else
|
|
|
|
echo "${red}Please enter a valid option!${end}"
|
|
|
|
fi
|