mmv

mailserver bootstrap script
git clone git://git.yotsev.xyz/mmv.git
Log | Files | Refs | README | LICENSE

madduser (1073B)


      1 #!/bin/sh
      2 
      3 if [ $# != 1 ]; then
      4     echo "Error: Invalid number of arguments"
      5     echo "Usage: madduser username@domain.tld"
      6     return 1
      7 fi
      8 
      9 user="$(echo $1 | cut -d '@' -f 1)"
     10 domain="$(echo $1 | cut -d '@' -f 2)"
     11 
     12 if [ -z "$(grep "$domain" /etc/mail/domains 2> /dev/null)" ]; then
     13     echo "Error: Invalid domain"
     14     echo "Info: It should be one of the following:"
     15     cat /etc/mail/domains
     16     return 1
     17 fi
     18 
     19 if [ -n "$(grep "^$user@$domain" /etc/mail/credentials 2> /dev/null)" ]; then
     20     echo "Error: User already exists in /etc/mail/credentials"
     21     return 1
     22 fi
     23 
     24 if [ -n "$(grep "^$user@$domain" /etc/mail/virtuals 2> /dev/null)" ]; then
     25     echo "Error: User already exists in /etc/mail/virtuals"
     26     return 1
     27 fi
     28 
     29 echo "Please enter a password."
     30 password="$(encrypt -p)"
     31 
     32 record="$user@$domain:$password:vmail:2000:2000:/var/vmail/$domain/$user::userdb_mail=maildir:/var/vmail/$domain/$user"
     33 
     34 echo "$record" >> /etc/mail/credentials
     35 echo "$user@$domain: vmail" >> /etc/mail/virtuals
     36 
     37 rcctl restart smtpd
     38 rcctl restart dovecot
     39 
     40 echo "Welcome" | mail -s "Welcome" $1