22#
33# Add a virtual mail account to courier-mta.
44#
5- # VERSION :0.2
6- # DATE :2014-12-25
5+ # VERSION :0.3
6+ # DATE :2015-01-30
77# AUTHOR :Viktor Szépe <viktor@szepe.net>
88# LICENSE :The MIT License (MIT)
99# URL :https://github.com/szepeviktor/debian-server-tools
1010# BASH-VERSION :4.2+
1111# LOCATION :/usr/local/sbin/add-mailaccount.sh
12- # DEPENDS :apt-get install courier-authdaemon courier-mta-ssl
12+ # DEPENDS :apt-get install courier-authdaemon courier-mta-ssl pwgen
13+ # DEPENDS :security/password2remember.sh
1314
14- ACCOUNT=" $1 "
15- MAILROOT=" /var/mail"
1615VIRTUAL_UID=" 1999"
1716COURIER_AUTH_DBNAME=" horde4"
18- CA_CERTIFICATES=" /etc/ssl/certs/ca-certificates.crt"
1917
2018Error () {
2119 echo " ERROR: $* "
2220 exit $1
2321}
2422
25- [ -z " $ACCOUNT " ] && Error 1 " No account given."
26- [ -d " $MAILROOT " ] || Error 1 " Mail root (${MAILROOT} ) does not exist."
23+ ACCOUNT=" $1 "
24+ MAILROOT=" /var/mail"
25+ CA_CERTIFICATES=" /etc/ssl/certs/ca-certificates.crt"
2726
2827[ " $( id --user) " == 0 ] || Error 1 " Only root is allowed to add mail accounts."
28+ [ -z " $ACCOUNT " ] && Error 1 " No account given."
29+ [ -d " $MAILROOT " ] || Error 1 " Mail root (${MAILROOT} ) does not exist."
2930
3031# inputs
3132for V in EMAIL PASS DESC HOMEDIR; do
@@ -34,8 +35,11 @@ for V in EMAIL PASS DESC HOMEDIR; do
3435 DEFAULT=" $ACCOUNT "
3536 ;;
3637 PASS)
37- # TODO: xkcd-style password
3838 DEFAULT=" $( pwgen 8 1) $(( RANDOM % 10 )) "
39+ # xkcd-style password
40+ WORDLIST_HU=" /usr/local/share/password2remember/password2remember_hu.txt"
41+ [ -f " $WORDLIST_HU " ] \
42+ && DEFAULT=" $( xkcdpass -d . -w " $WORDLIST_HU " -n 4) "
3943 ;;
4044 HOMEDIR)
4145 DEFAULT=" ${MAILROOT} /${EMAIL##*@ } /${EMAIL%%@* } "
@@ -45,8 +49,6 @@ for V in EMAIL PASS DESC HOMEDIR; do
4549 ;;
4650 esac
4751
48- # read -e -p "${V}? " -i "$DEFAULT" VALUE
49- # eval "$V"="'$VALUE'"
5052 read -e -p " ${V} ? " -i " $DEFAULT " " $V "
5153done
5254
@@ -59,47 +61,62 @@ if ! getent passwd "$VIRTUAL_UID" &> /dev/null; then
5961 getent passwd " $VIRTUAL_UID "
6062fi
6163
62- # check domain
64+ # check email format
65+ # https://fightingforalostcause.net/content/misc/2006/compare-email-regex.php
66+ grep -qE ' ^[-a-z0-9_]+(\.[-a-z0-9_]+)*@([a-z0-9_][-a-z0-9_])*(\.[-a-z0-9_]+)+$' <<< " $EMAIL" || Error 8 ' Non-regular email address'
67+
6368NEW_DOMAIN=" ${EMAIL##*@ } "
64- grep -qr " ^${NEW_DOMAIN// ./ \\ .} $" /etc/courier/locals /etc/courier/esmtpacceptmailfor.dir || Error 10 " This domain is not accepted here (${NEW_DOMAIN} )"
65- grep -qr " ^${NEW_DOMAIN// ./ \\ .} $" /etc/courier/hosteddomains || echo " [WARNING] This domain is not hosted here (${NEW_DOMAIN} )" >&2
69+ NEW_MAILDIR=" ${MAILROOT} /${NEW_DOMAIN} /${EMAIL%%@* } /Maildir"
70+ # ?
71+
72+ # check home
73+ [ -d " $HOMEDIR " ] && Error 9 " This home ($HOMEDIR ) already exists."
74+
75+ # check domain
76+ grep -qFxr " ${NEW_DOMAIN} " /etc/courier/locals /etc/courier/esmtpacceptmailfor.dir || Error 10 " This domain is not accepted here (${NEW_DOMAIN} )"
77+ grep -qFxr " ${NEW_DOMAIN} " /etc/courier/hosteddomains || echo " [WARNING] This domain is not hosted here (${NEW_DOMAIN} )" >&2
6678
6779# account folder and maildir
68- NEW_MAILDIR=" ${MAILROOT} /${NEW_DOMAIN} /${EMAIL%%@* } "
69- mkdir -v -p " ${MAILROOT} /${NEW_DOMAIN} " || Error 12 " Failed to create dir: (${MAILROOT} /${NEW_DOMAIN} )"
70- chown -v " $VIRTUAL_UID " :" $VIRTUAL_UID " " ${MAILROOT} /${NEW_DOMAIN} " || Error 13 " Cannot chown (${MAILROOT} /${NEW_DOMAIN} )"
71- chmod -v o-rx " ${MAILROOT} /${NEW_DOMAIN} " || Error 14 " Cannot chmod (${MAILROOT} /${NEW_DOMAIN} )"
72- sudo -u virtual maildirmake " $NEW_MAILDIR " && echo " Maildir OK." || Error 15 " Cannot create maildir (${NEW_MAILDIR} )"
80+ install -o " $VIRTUAL_UID " -g " $VIRTUAL_UID " -m " u=rwx" -d " ${MAILROOT} /${NEW_DOMAIN} /${EMAIL%%@* } " || Error 12 " Failed to install dir: (${MAILROOT} /${NEW_DOMAIN} )"
81+ # ?
82+ sudo -u virtual -- maildirmake " $NEW_MAILDIR " && echo " Maildir OK." || Error 15 " Cannot create maildir (${NEW_MAILDIR} )"
7383
7484# special folders
75- sudo -u virtual maildirmake -f Drafts " $NEW_MAILDIR " && echo " Drafts OK." || Error 20 " Cannot create Drafts folder"
76- sudo -u virtual maildirmake -f Sent " $NEW_MAILDIR " && echo " Sent OK." || Error 21 " Cannot create Sent folder"
77- sudo -u virtual maildirmake -f Trash " $NEW_MAILDIR " && echo " Trash OK." || Error 22 " Cannot create Trash folder"
85+ sudo -u virtual -- maildirmake -f Drafts " $NEW_MAILDIR " && echo " Drafts OK." || Error 20 " Cannot create Drafts folder"
86+ sudo -u virtual -- maildirmake -f Sent " $NEW_MAILDIR " && echo " Sent OK." || Error 21 " Cannot create Sent folder"
87+ sudo -u virtual -- maildirmake -f Trash " $NEW_MAILDIR " && echo " Trash OK." || Error 22 " Cannot create Trash folder"
88+ # removal instruction
89+ echo " Remove home: rm -rf '${HOMEDIR} '"
7890
79- # MySQL output
91+ # MySQL authentication
8092if which mysql & > /dev/null \
8193 && grep -q " ^authmodulelist=.*\bauthmysql\b" /etc/courier/authdaemonrc; then
82- mysql " $COURIER_AUTH_DBNAME " << SQL
94+ mysql " $COURIER_AUTH_DBNAME " << SQL && echo "User inserted into database. OK."
8395-- USE ${COURIER_AUTH_DBNAME} ;
8496INSERT INTO \` courier_horde\` (\` id\` , \` crypt\` , \` clear\` , \` name\` , \` uid\` , \` gid\` , \` home\` , \` maildir\` ,
8597 \` defaultdelivery\` , \` quota\` , \` options\` , \` user_soft_expiration_date\` , \` user_hard_expiration_date\` , \` vac_msg\` , \` vac_subject\` , \` vac_stat\` ) VALUES
86- ('${EMAIL} ', ENCRYPT('${PASS} '), '', '${DESC} ', ${VIRTUAL_UID} , ${VIRTUAL_UID} , '${HOMEDIR} ', '', '', '', '', NULL, NULL, '', '', 'N');
98+ ('${EMAIL} ', ENCRYPT('${PASS} '), '', '${DESC} ', ${VIRTUAL_UID} , ${VIRTUAL_UID} , '${HOMEDIR} ', '${NEW_MAILDIR} ', '', '', '', NULL, NULL, '', '', 'N');
8799SQL
100+ # removal instruction
101+ echo " Remove user: -- USE ${COURIER_AUTH_DBNAME} ;"
102+ echo " Remove user: DELETE FROM \` courier_horde\` WHERE \` id\` = '${EMAIL} ' LIMIT 1;"
88103fi
89104
90- # userdb
105+ # userdb authentication
91106if which userdb userdbpw & > /dev/null \
92107 && [ -r /etc/courier/userdb ] \
93108 && grep -q " ^authmodulelist=.*\bauthuserdb\b" /etc/courier/authdaemonrc; then
94- userdb " $EMAIL " set " home=${NEW_MAILDIR } " || Error 30 " Failed to add to userdb"
109+ userdb " $EMAIL " set " home=${HOMEDIR } " || Error 30 " Failed to add to userdb"
95110 userdb " $EMAIL " set " mail=${NEW_MAILDIR} " || Error 31 " Failed to add to userdb"
96- # man makeuserdb
111+ # 'maildir' is not necessary, see: man makeuserdb
97112 # userdb "$EMAIL" set "maildir=${NEW_MAILDIR}" || Error 32 "Failed to add to userdb"
98113 userdb " $EMAIL " set " uid=${VIRTUAL_UID} " || Error 33 " Failed to add to userdb"
99114 userdb " $EMAIL " set " gid=${VIRTUAL_UID} " || Error 34 " Failed to add to userdb"
100115 echo " $PASS " | userdbpw -md5 | userdb " $EMAIL " set systempw || Error 35 " Failed to add to userdb"
101116 [ -z " $DESC " ] || userdb " $EMAIL " set " fullname=${DESC} " || Error 36 " Failed to add to userdb"
102117 makeuserdb || Error 37 " Failed to make userdb"
118+ # removal instruction
119+ echo " Remove user: userdb '$EMAIL ' del"
103120fi
104121
105122# SMTP authentication test
0 commit comments