You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.5KB

  1. #!/bin/bash
  2. # set LDAP password from secret
  3. if [ ! -z $LDAP_BIND_PWD_FILE -a -f $LDAP_BIND_PWD_FILE ]; then
  4. LDAP_BIND_PWD=`cat $LDAP_BIND_PWD_FILE`;
  5. fi
  6. function replace {
  7. #echo $1
  8. sed -i "s/\${LDAP_SERVER_HOST}/${LDAP_SERVER_HOST}/g" $1
  9. sed -i "s/\${LDAP_BIND_DN}/${LDAP_BIND_DN}/g" $1
  10. sed -i "s/\${LDAP_SEARCH_BASE}/${LDAP_SEARCH_BASE}/g" $1
  11. sed -i "s/\${DOMAIN}/${DOMAIN}/g" $1
  12. sed -i "s/\${DOMAINS}/${DOMAINS[*]}/g" $1
  13. sed -i "s/\${LDAP_BIND_PWD}/${LDAP_BIND_PWD}/g" $1
  14. }
  15. for i in `find /tmp/config/postfix -type f -exec ls {} \;`; do
  16. replace $i
  17. done;
  18. for i in `find /tmp/config/dovecot -type f -exec ls {} \;`; do
  19. replace $i
  20. done;
  21. for i in `find /tmp/config/dovecot/conf.d -type f -exec ls {} \;`; do
  22. replace $i
  23. done;
  24. for i in `find /tmp/config/saslauth -type f -exec ls {} \;`; do
  25. replace $i
  26. done;
  27. # Postfix
  28. cp -f /tmp/config/postfix/* /etc/postfix/
  29. mkdir -p /etc/postfix/sasl
  30. cp -f /tmp/config/postfix/sasl/* /etc/postfix/sasl/sasl
  31. for i in ${DOMAINS[@]}; do
  32. echo "$i OK" >> /etc/postfix/virtual_domains;
  33. done;
  34. postmap hash:/etc/postfix/virtual_domains
  35. # TLS certs
  36. cd /tmp
  37. openssl genrsa -des3 -passout pass:${LDAP_BIND_PWD} -out mail.domain.tld.key 4096
  38. chmod 600 mail.domain.tld.key
  39. openssl req -new -key mail.domain.tld.key -out mail.domain.tld.csr \
  40. -passin pass:${LDAP_BIND_PWD} \
  41. -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.${DOMAIN}"
  42. openssl x509 -req -days 365 -in mail.domain.tld.csr -signkey mail.domain.tld.key \
  43. -out mail.domain.tld.crt -passin pass:${LDAP_BIND_PWD}
  44. openssl rsa -in mail.domain.tld.key -out mail.domain.tld.key.nopass \
  45. -passin pass:${LDAP_BIND_PWD}
  46. mv mail.domain.tld.key.nopass mail.domain.tld.key
  47. openssl req -new -x509 -extensions v3_ca -keyout cakey.pem -out cacert.pem -days 3650 \
  48. -passout pass:${LDAP_BIND_PWD} \
  49. -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.${DOMAIN}"
  50. chmod 600 mail.domain.tld.key
  51. chmod 600 cakey.pem
  52. mv mail.domain.tld.key /etc/ssl/private/
  53. mv mail.domain.tld.crt /etc/ssl/certs/
  54. mv cakey.pem /etc/ssl/private/
  55. mv cacert.pem /etc/ssl/certs/
  56. # DH
  57. mkdir -p /etc/postfix/certs
  58. cd /etc/postfix/certs
  59. openssl dhparam -2 -out dh_512.pem 512
  60. openssl dhparam -2 -out dh_1024.pem 1024
  61. chown -R root:root /etc/postfix/certs/
  62. chmod -R 600 /etc/postfix/certs/
  63. # Dovecot
  64. mkdir -p /etc/dovecot/private
  65. openssl req -new -x509 -nodes -out /etc/dovecot/dovecot.pem -keyout /etc/dovecot/private/dovecot.pem -days 3650 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.${DOMAIN}"
  66. cp -f /tmp/config/dovecot/* /etc/dovecot/
  67. cp -f /tmp/config/dovecot/conf.d/* /etc/dovecot/conf.d/
  68. #Saslauthd
  69. cp -f /tmp/config/saslauth/saslauthd /etc/default/
  70. cp -f /tmp/config/saslauth/saslauthd.conf /etc/
  71. chown root:sasl /etc/saslauthd.conf
  72. chmod 640 /etc/saslauthd.conf
  73. #rm -Rf /tmp/config
  74. # getmail
  75. # https://stackoverflow.com/a/9625233/1937418
  76. for i in `ls ${MAIL_DATA_PATH}/getmail/getmailrc-*`; do
  77. (crontab -l 2>/dev/null; echo "*/5 * * * * sudo -u vmail getmail -r $i --getmaildir ${MAIL_DATA_PATH}/getmail/ >> /dev/null") | crontab - ;
  78. done;
  79. touch ${MAIL_DATA_PATH}/getmail/getmail.log
  80. #chown -R vmail:vmail ${MAIL_DATA_PATH}/getmail
  81. if [ -z "${DATA_CHOWN}" -o "${DATA_CHOWN}" != "0" ]; then
  82. echo "Changing ownership of Data folder. It may take a while..."
  83. chown -R vmail:vmail ${MAIL_DATA_PATH}
  84. fi
  85. service rsyslog start
  86. service postfix start
  87. service dovecot start
  88. service saslauthd start
  89. service cron start
  90. tail -fn 0 /var/log/mail.log
  91. tail -f /dev/null
  92. exit 0