v-add-mail-account 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/bash
  2. # info: add mail domain account
  3. # options: USER DOMAIN ACCOUNT PASSWORD [QUOTA]
  4. #
  5. # The function add new email account.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. user=$1
  11. domain=$(idn -t --quiet -u "$2" )
  12. domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
  13. domain_idn=$(idn -t --quiet -a "$domain")
  14. account=$(echo $3 | tr '[:upper:]' '[:lower:]')
  15. password=$4; HIDE=4
  16. quota=${5-unlimited}
  17. # Includes
  18. source $VESTA/func/main.sh
  19. source $VESTA/func/domain.sh
  20. source $VESTA/conf/vesta.conf
  21. #----------------------------------------------------------#
  22. # Verifications #
  23. #----------------------------------------------------------#
  24. check_args '4' "$#" 'USER DOMAIN ACCOUNT PASSWORD [QUOTA]'
  25. is_format_valid 'user' 'domain' 'account'
  26. if [ "$quota" != 'unlimited' ]; then
  27. is_format_valid 'quota'
  28. fi
  29. is_system_enabled "$MAIL_SYSTEM" 'MAIL_SYSTEM'
  30. is_object_valid 'user' 'USER' "$user"
  31. is_object_unsuspended 'user' 'USER' "$user"
  32. is_object_valid 'mail' 'DOMAIN' "$domain"
  33. is_object_unsuspended 'mail' 'DOMAIN' "$domain"
  34. is_package_full 'MAIL_ACCOUNTS'
  35. is_mail_new "$account"
  36. is_password_valid
  37. #----------------------------------------------------------#
  38. # Action #
  39. #----------------------------------------------------------#
  40. # Generating hashed password
  41. salt=$(generate_password "$PW_MATRIX" "8")
  42. md5="{MD5}$($BIN/v-generate-password-hash md5 $salt <<<$password)"
  43. # Adding account info into password file
  44. if [[ "$MAIL_SYSTEM" =~ exim ]]; then
  45. if [ "$quota" = 'unlimited' ]; then
  46. quota='0'
  47. fi
  48. str="$account:$md5:$user:mail::$HOMEDIR/$user:$quota"
  49. echo $str >> $HOMEDIR/$user/conf/mail/$domain/passwd
  50. fi
  51. #----------------------------------------------------------#
  52. # Vesta #
  53. #----------------------------------------------------------#
  54. # Generating timestamp
  55. time_n_date=$(date +'%T %F')
  56. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  57. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  58. if [[ "$quota" -eq '0' ]]; then
  59. quota='unlimited'
  60. fi
  61. str="ACCOUNT='$account' ALIAS='' AUTOREPLY='no' FWD='' FWD_ONLY=''"
  62. str="$str MD5='$md5' QUOTA='$quota' U_DISK='0' SUSPENDED='no'"
  63. str="$str TIME='$time' DATE='$date'"
  64. echo "$str" >> $USER_DATA/mail/$domain.conf
  65. chmod 660 $USER_DATA/mail/$domain.conf
  66. # Increase mail accounts counter
  67. accounts=$(wc -l $USER_DATA/mail/$domain.conf | cut -f 1 -d ' ')
  68. increase_user_value "$user" '$U_MAIL_ACCOUNTS'
  69. update_object_value 'mail' 'DOMAIN' "$domain" '$ACCOUNTS' "$accounts"
  70. # Logging
  71. log_history "added mail account $account@$domain"
  72. log_event "$OK" "$ARGUMENTS"
  73. exit