shell script to add directory protection or htaccess

This is a shell script to add directory protection or htaccess
the following is what the script does
 
1.it will read the domain nae and the folder name to be protected 
2. it will chk heatehr the domin dir exist 
3. it will chk if the htpasswd file exist .. if not it create 
4. if all the abel is true then it adds content in httpd_vhsot.conf file  
 
#!/bin/bash -x
#script to add directory protection

read -p "enter the domain name: " d
read -p "enter the dir that you want to protect: " f

var=`sed -n "/\#dir_protection_${d}_${f}\#/,/\#dir_protection_${d}_${f}\#/P" /usr/local/apache2/conf/extra/httpd_vhost.conf`

if [ -d /websites/"$d"/web/"$f" ]; then

    if [ -d /websites/privatefolderpasswd/"$d" -a -e /websites/privatefolderpasswd/"$d"/htpasswd  ]; then

         if [ -n "$var" ]; then
read -p "Enter user name for the folder you want to protect: " u
read -p "Enter  password for the user: " p
/usr/local/apache2/bin/htpasswd -bc /websites/privatefolderpasswd/"$d"/htpasswd "$u" "$p"

sed -i "/\#DIR_PROTECT_${d}2631\#/a\#################dir_protection_${d}_${f}#################\n<Directory \/websites\/${d}\/web\/awstats>\nAllowOverride  AuthConfig\nAuthName "MyPrivateStuff"\nAuthType Basic\nAuthGroupFile \/dev\/null\nAuthUserFile /websites\/privatefolderpasswd\/${d}/htpasswd\nAuthName Enterpassword\nRequire valid-user\n </Directory>\n#################dir_protection_${d}_${f}#################" /usr/local/apache2/conf/extra/httpd_vhost.conf

exit 1

         else
echo -e "this should not be here"
         fi
    else
echo -e "creating  dir structure to store htacess password file"

mkdir /websites/privatefolderpasswd/"$d"
touch /websites/privatefolderpasswd/"$d"/htpasswd

read -p "Enter user name for the folder you want to protect: " u
read -p "Enter  password for the user: " p
/usr/local/apache2/bin/htpasswd -bc /websites/privatefolderpasswd/"$d"/htpasswd "$u" "$p"

sed -i "/\#DIR_PROTECT_${d}2631\#/a\#################dir_protection_${d}_${f}#################\n<Directory \/websites\/${d}\/web\/awstats>\nAllowOverride  AuthConfig\nAuthName "MyPrivateStuff"\nAuthType Basic\nAuthGroupFile \/dev\/null\nAuthUserFile /websites\/privatefolderpasswd\/${d}/htpasswd\nAuthName Enterpassword\nRequire valid-user\n </Directory>\n#################dir_protection_${d}_${f}#################" /usr/local/apache2/conf/extra/httpd_vhost.conf
echo -e "The htaccess is created now"
    fi
else
echo -e "The domain does not exist"
exit 1
fi



--

No comments:

Other Articles

Enter your email address: