PageRenderTime 65ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/iRedMail/tools/create_mail_user_OpenLDAP.sh

https://bitbucket.org/zhb/iredmail
Shell | 295 lines | 163 code | 40 blank | 92 comment | 12 complexity | 5ae2d4e1ef4df4132a75c70daeae1750 MD5 | raw file
  1. #!/usr/bin/env bash
  2. # Author: Zhang Huangbin (zhb _at_ iredmail.org)
  3. # Purpose: Add new OpenLDAP user for postfix mail server.
  4. # Project: iRedMail (http://www.iredmail.org/)
  5. # --------------------------- WARNING ------------------------------
  6. # This script only works under iRedMail >= 0.8.4 due to ldap schema
  7. # changes.
  8. # ------------------------------------------------------------------
  9. # --------------------------- USAGE --------------------------------
  10. # 1) Please change variables below to fit your env:
  11. #
  12. # - In 'Global Setting' section:
  13. # * STORAGE_BASE_DIRECTORY
  14. #
  15. # - In 'LDAP Setting' section:
  16. # * LDAP_SUFFIX
  17. # * BINDDN
  18. # * BINDPW
  19. # * QUOTA
  20. #
  21. # - In 'Virtual Domains & Users' section:
  22. # * QUOTA
  23. # * TRANSPORT
  24. # * PASSWORD_SCHEME # SSHA is recommended.
  25. # * DEFAULT_PASSWD
  26. # * USE_DEFAULT_PASSWD
  27. #
  28. # - Pure-FTPd integration:
  29. # * PUREFTPD_INTEGRATION # <- set to 'YES' if you want to integrate it.
  30. # * FTP_BASE_DIRECTORY # <- directory used to store FTP data.
  31. #
  32. # - Optional variables:
  33. # * SEND_WELCOME_MSG
  34. #
  35. # 2) Execute this script with domain name and username (without @domain) directly:
  36. #
  37. # shell# bash create_mail_user_OpenLDAP.sh example.com new_user
  38. #
  39. # It will create a mail user with mail address "new_user@example.com".
  40. # To add multiple mail users, just list all usernames:
  41. #
  42. # shell# bash create_mail_user_OpenLDAP.sh example.com new_user new_user2 new_user3
  43. #
  44. # ------------------------------------------------------------------
  45. # Source functions.
  46. . ../conf/global
  47. . ../conf/core
  48. # ----------------------------------------------
  49. # ------------ Global Setting ------------------
  50. # ----------------------------------------------
  51. # Storage base directory used to store users' mail.
  52. # mailbox of LDAP user will be:
  53. # ${STORAGE_BASE_DIRECTORY}/${DOMAIN_NAME}/${USERNAME}/
  54. # Such as:
  55. # /var/vmail/vmail1/iredmail.org/zhb/
  56. # -------------------|===========|-----|
  57. # STORAGE_BASE_DIRECTORY|DOMAIN_NAME|USERNAME
  58. #
  59. STORAGE_BASE_DIRECTORY="/var/vmail/vmail1"
  60. # ------------------------------------------------------------------
  61. # -------------------------- LDAP Setting --------------------------
  62. # ------------------------------------------------------------------
  63. LDAP_SUFFIX="dc=example,dc=com"
  64. # Setting 'BASE_DN'.
  65. BASE_DN="o=domains,${LDAP_SUFFIX}"
  66. # Setting 'DOMAIN_NAME' and DOMAIN_DN':
  67. # * DOMAIN will be used in mail address: ${USERNAME}@${DOMAIN}
  68. # * DOMAIN_DN will be used in LDAP dn.
  69. DOMAIN_NAME="$1"
  70. DOMAIN_DN="domainName=${DOMAIN_NAME}"
  71. OU_USER_DN="ou=Users"
  72. # ---------- rootdn of LDAP Server ----------
  73. # Setting rootdn of LDAP.
  74. BINDDN="cn=Manager,${LDAP_SUFFIX}"
  75. # Setting rootpw of LDAP.
  76. BINDPW='passwd'
  77. # ---------- Virtual Domains & Users --------------
  78. # Set default quota for LDAP users: 104857600 = 100M
  79. QUOTA='1048576000'
  80. # Default MTA Transport (Defined in postfix master.cf).
  81. TRANSPORT='dovecot'
  82. # Password setting.
  83. PASSWORD_SCHEME='SSHA' # MD5, SSHA. SSHA is recommended.
  84. DEFAULT_PASSWD='888888'
  85. USE_DEFAULT_PASSWD='NO'
  86. # ------------------------------------------------------------------
  87. # -------------------- Pure-FTPd Integration -----------------------
  88. # ------------------------------------------------------------------
  89. # Add objectClass and attributes for pure-ftpd integration.
  90. # Note: You must inlucde pureftpd.schema in OpenLDAP slapd.conf first.
  91. PUREFTPD_INTEGRATION='NO'
  92. FTP_BASE_DIRECTORY='/home/ftp'
  93. # ------------------------------------------------------------------
  94. # ------------------------- Welcome Msg ----------------------------
  95. # ------------------------------------------------------------------
  96. # Send a welcome mail after user created.
  97. SEND_WELCOME_MSG='NO'
  98. # Set welcome mail info.
  99. WELCOME_MSG_SUBJECT="Welcome!"
  100. WELCOME_MSG_BODY="Welcome, new user."
  101. # -------------------------------------------
  102. # ----------- End Global Setting ------------
  103. # -------------------------------------------
  104. # Time stamp, will be appended in maildir.
  105. DATE="$(date +%Y.%m.%d.%H.%M.%S)"
  106. STORAGE_BASE="$(dirname ${STORAGE_BASE_DIRECTORY})"
  107. STORAGE_NODE="$(basename ${STORAGE_BASE_DIRECTORY})"
  108. add_new_domain()
  109. {
  110. domain="$(echo ${1} | tr '[A-Z]' '[a-z]')"
  111. ldapsearch -x -D "${BINDDN}" -w "${BINDPW}" -b "${BASE_DN}" | grep "domainName: ${domain}" >/dev/null
  112. if [ X"$?" != X"0" ]; then
  113. echo "Add new domain: ${domain}."
  114. ldapadd -x -D "${BINDDN}" -w "${BINDPW}" <<EOF
  115. dn: ${DOMAIN_DN},${BASE_DN}
  116. objectClass: mailDomain
  117. domainName: ${domain}
  118. mtaTransport: ${TRANSPORT}
  119. accountStatus: active
  120. enabledService: mail
  121. EOF
  122. else
  123. :
  124. fi
  125. ldapadd -x -D "${BINDDN}" -w "${BINDPW}" <<EOF
  126. dn: ${OU_USER_DN},${DOMAIN_DN},${BASE_DN}
  127. objectClass: organizationalUnit
  128. objectClass: top
  129. ou: Users
  130. EOF
  131. ldapadd -x -D "${BINDDN}" -w "${BINDPW}" <<EOF
  132. dn: ou=Groups,${DOMAIN_DN},${BASE_DN}
  133. objectClass: organizationalUnit
  134. objectClass: top
  135. ou: Groups
  136. EOF
  137. ldapadd -x -D "${BINDDN}" -w "${BINDPW}" <<EOF
  138. dn: ou=Aliases,${DOMAIN_DN},${BASE_DN}
  139. objectClass: organizationalUnit
  140. objectClass: top
  141. ou: Aliases
  142. EOF
  143. ldapadd -x -D "${BINDDN}" -w "${BINDPW}" <<EOF
  144. dn: ou=Externals,${DOMAIN_DN},${BASE_DN}
  145. objectClass: organizationalUnit
  146. objectClass: top
  147. ou: Externals
  148. EOF
  149. }
  150. add_new_user()
  151. {
  152. USERNAME="$(echo $1 | tr [A-Z] [a-z])"
  153. MAIL="$( echo $2 | tr [A-Z] [a-z])"
  154. # Create template LDIF file for this new user and add it.
  155. # If you do *NOT* want to keep rootpw in script, use '-W' instead of
  156. # '-w "${BINDPW}".
  157. maildir="${DOMAIN_NAME}/$(hash_maildir ${USERNAME})"
  158. # Generate user password.
  159. if [ X"${USE_DEFAULT_PASSWD}" == X'YES' ]; then
  160. PASSWD="$(python ./generate_password_hash.py ${PASSWORD_SCHEME} ${DEFAULT_PASSWD})"
  161. else
  162. PASSWD="$(python ./generate_password_hash.py ${PASSWORD_SCHEME} ${USERNAME})"
  163. fi
  164. if [ X"${PUREFTPD_INTEGRATION}" == X'YES' ]; then
  165. LDIF_PUREFTPD_USER="objectClass: PureFTPdUser
  166. FTPStatus: enabled
  167. FTPQuotaFiles: 50
  168. FTPQuotaMBytes: 10
  169. FTPDownloadBandwidth: 50
  170. FTPUploadBandwidth: 50
  171. FTPDownloadRatio: 5
  172. FTPUploadRatio: 1
  173. FTPHomeDir: ${FTP_BASE_DIRECTORY}/${DOMAIN_NAME}/${USERNAME}/
  174. "
  175. else
  176. LDIF_PUREFTPD_USER=''
  177. fi
  178. ldapadd -x -D "${BINDDN}" -w "${BINDPW}" <<EOF
  179. dn: mail=${MAIL},${OU_USER_DN},${DOMAIN_DN},${BASE_DN}
  180. objectClass: inetOrgPerson
  181. objectClass: shadowAccount
  182. objectClass: amavisAccount
  183. objectClass: mailUser
  184. objectClass: top
  185. accountStatus: active
  186. storageBaseDirectory: ${STORAGE_BASE}
  187. homeDirectory: ${STORAGE_BASE_DIRECTORY}/${maildir}
  188. mailMessageStore: ${STORAGE_NODE}/${maildir}
  189. mail: ${MAIL}
  190. mailQuota: ${QUOTA}
  191. userPassword: ${PASSWD}
  192. cn: ${USERNAME}
  193. sn: ${USERNAME}
  194. givenName: ${USERNAME}
  195. uid: ${USERNAME}
  196. shadowLastChange: 0
  197. amavisLocal: TRUE
  198. enabledService: internal
  199. enabledService: doveadm
  200. enabledService: lib-storage
  201. enabledService: indexer-worker
  202. enabledService: dsync
  203. enabledService: mail
  204. enabledService: pop3
  205. enabledService: pop3secured
  206. enabledService: imap
  207. enabledService: imapsecured
  208. enabledService: managesieve
  209. enabledService: managesievesecured
  210. enabledService: sieve
  211. enabledService: sievesecured
  212. enabledService: smtp
  213. enabledService: smtpsecured
  214. enabledService: deliver
  215. enabledService: lda
  216. enabledService: lmtp
  217. enabledService: forward
  218. enabledService: senderbcc
  219. enabledService: recipientbcc
  220. enabledService: shadowaddress
  221. enabledService: displayedInGlobalAddressBook
  222. enabledService: sogo
  223. ${LDIF_PUREFTPD_USER}
  224. EOF
  225. }
  226. send_welcome_mail()
  227. {
  228. MAIL="$1"
  229. echo "Send a welcome mail to new user: ${MAIL}"
  230. echo "${WELCOME_MSG_BODY}" | mail -s "${WELCOME_MSG_SUBJECT}" ${MAIL}
  231. }
  232. usage()
  233. {
  234. echo "Usage:"
  235. echo -e "\t$0 DOMAIN USERNAME"
  236. echo -e "\t$0 DOMAIN USER1 USER2 USER3 ..."
  237. }
  238. if [ $# -lt 2 ]; then
  239. usage
  240. else
  241. # Promopt to check settings.
  242. [ X"${LDAP_SUFFIX}" == X"dc=example,dc=com" ] && echo "You should change 'LDAP_SUFFIX' in $0."
  243. # Get domain name.
  244. DOMAIN_NAME="$(echo $1 | tr '[A-Z]' '[a-z]')"
  245. shift 1
  246. add_new_domain ${DOMAIN_NAME}
  247. for i in $@; do
  248. USERNAME="$(echo $i | tr '[A-Z]' '[a-z]')"
  249. MAIL="${USERNAME}@${DOMAIN_NAME}"
  250. # Add new user in LDAP.
  251. add_new_user ${USERNAME} ${MAIL}
  252. # Send welcome msg to new user.
  253. [ X"${SEND_WELCOME_MSG}" == X'YES' ] && send_welcome_mail ${MAIL}
  254. done
  255. fi