v-add-web-domain-httpauth 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/bash
  2. # info: add password protection for web domain
  3. # options: USER DOMAIN AUTH_USER AUTH_PASSWORD [RESTART]
  4. #
  5. # The call is used for securing web domain with http auth
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. user=$1
  11. domain=$2
  12. auth_user=$3
  13. password=$4; HIDE=4
  14. restart=${5-yes}
  15. # Includes
  16. source $VESTA/func/main.sh
  17. source $VESTA/func/domain.sh
  18. source $VESTA/conf/vesta.conf
  19. # Defining htpasswd file
  20. htaccess="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.$domain.conf_htaccess"
  21. htpasswd="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.$domain.htpasswd"
  22. docroot="$HOMEDIR/$user/web/$domain/public_html"
  23. #----------------------------------------------------------#
  24. # Verifications #
  25. #----------------------------------------------------------#
  26. check_args '4' "$#" 'USER DOMAIN AUTH_USER AUTH_PASSWORD [RESTART]'
  27. is_format_valid 'user' 'domain'
  28. is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
  29. is_object_valid 'user' 'USER' "$user"
  30. is_object_unsuspended 'user' 'USER' "$user"
  31. is_object_valid 'web' 'DOMAIN' "$domain"
  32. is_object_unsuspended 'web' 'DOMAIN' "$domain"
  33. is_password_valid
  34. get_domain_values 'web'
  35. if [ ! -z "$(echo "$AUTH_USER" |tr : '\n' |grep ^$auth_user$)" ]; then
  36. echo "Error: auth user $auth_user already exists"
  37. log_event "$E_EXISTS" "$ARGUMENTS"
  38. exit $E_EXISTS
  39. fi
  40. #----------------------------------------------------------#
  41. # Action #
  42. #----------------------------------------------------------#
  43. # Adding htaccess password protection
  44. if [ ! -e "$htaccess" ]; then
  45. if [ "$WEB_SYSTEM" != 'nginx' ]; then
  46. echo "<Directory $docroot>" > $htaccess
  47. echo " AuthUserFile $htpasswd" >> $htaccess
  48. echo " AuthName \"$domain access\"" >> $htaccess
  49. echo " AuthType Basic" >> $htaccess
  50. echo " Require valid-user" >> $htaccess
  51. echo "</Directory>" >> $htaccess
  52. else
  53. echo "auth_basic \"$domain password access\";" > $htaccess
  54. echo "auth_basic_user_file $htpasswd;" >> $htaccess
  55. fi
  56. restart_required='yes'
  57. fi
  58. # Adding httpasswd user
  59. auth_hash=$($BIN/v-generate-password-hash htpasswd htpasswd $password)
  60. touch $htpasswd
  61. chmod 640 $htpasswd $htaccess
  62. sed -i "/^$auth_user:/d" $htpasswd
  63. echo "$auth_user:$auth_hash" >> $htpasswd
  64. # Restarting web server
  65. if [ "$restart" != 'no' ] && [ "$restart_required" = 'yes' ]; then
  66. $BIN/v-restart-web
  67. fi
  68. #----------------------------------------------------------#
  69. # Vesta #
  70. #----------------------------------------------------------#
  71. # Preparing web.conf keys
  72. if [ ! -z "$AUTH_USER" ]; then
  73. auth_user="$AUTH_USER:$auth_user"
  74. auth_hash="$AUTH_HASH:$auth_hash"
  75. else
  76. # Adding new key into web.conf
  77. add_object_key "web" 'DOMAIN' "$domain" 'AUTH_USER' 'U_DISK'
  78. add_object_key "web" 'DOMAIN' "$domain" 'AUTH_HASH' 'U_DISK'
  79. fi
  80. # Updating config
  81. update_object_value 'web' 'DOMAIN' "$domain" '$AUTH_USER' "$auth_user"
  82. update_object_value 'web' 'DOMAIN' "$domain" '$AUTH_HASH' "$auth_hash"
  83. # Logging
  84. log_history "added http auth user $httpauth_user on $domain"
  85. log_event "$OK" "$ARGUMENTS"
  86. exit