Guest User

Untitled

a guest
Feb 4th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. #####
  4. # Script to install postfix
  5. #####
  6.  
  7.  
  8. DOMAIN="example.com"
  9. EMAIL="haisum@example.com"
  10. PASSWORD="example.com1*"
  11. DATABASE="servermail"
  12. DB_USER="usermail"
  13. DB_USER_PASS="mailpassword"
  14.  
  15. export DEBIAN_FRONTEND=noninteractive
  16.  
  17. installpkg(){
  18. dpkg-query --status $1 >/dev/null || apt-get install -y $1
  19. }
  20.  
  21. apt-get update
  22. installpkg mysql-server
  23. installpkg mysql-client
  24. debconf-set-selections <<< "postfix postfix/mailname string $DOMAIN"
  25. debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"
  26. installpkg postfix
  27. installpkg postfix-mysql
  28. installpkg dovecot-core
  29. installpkg dovecot-imapd
  30. installpkg dovecot-pop3d
  31. installpkg dovecot-lmtpd
  32. installpkg dovecot-mysql
  33.  
  34. bootstrapdb(){
  35. cat <<EOF | mysql -uroot
  36.  
  37. CREATE DATABASE IF NOT EXISTS $DATABASE;
  38.  
  39. GRANT SELECT ON $DATABASE.* TO '$DB_USER'@'127.0.0.1' IDENTIFIED BY '$DB_USER_PASS';
  40.  
  41. FLUSH PRIVILEGES;
  42.  
  43. USE servermail;
  44.  
  45. CREATE TABLE IF NOT EXISTS virtual_domains (
  46. id INT NOT NULL AUTO_INCREMENT,
  47. name VARCHAR(50) NOT NULL,
  48. PRIMARY KEY (id)
  49. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  50.  
  51. CREATE TABLE IF NOT EXISTS virtual_users (
  52. id INT NOT NULL AUTO_INCREMENT,
  53. domain_id INT NOT NULL,
  54. password VARCHAR(106) NOT NULL,
  55. email VARCHAR(120) NOT NULL,
  56. PRIMARY KEY (id),
  57. UNIQUE KEY email (email),
  58. FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
  59. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  60.  
  61. CREATE TABLE IF NOT EXISTS virtual_aliases (
  62. id INT NOT NULL AUTO_INCREMENT,
  63. domain_id INT NOT NULL,
  64. source varchar(100) NOT NULL,
  65. destination varchar(100) NOT NULL,
  66. PRIMARY KEY (id),
  67. FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
  68. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  69.  
  70. INSERT INTO servermail.virtual_domains
  71. (id ,name)
  72. VALUES
  73. ('1', '$DOMAIN'),
  74. ('2', 'mail.$DOMAIN');
  75.  
  76. INSERT INTO servermail.virtual_users
  77. (id, domain_id, password , email)
  78. VALUES
  79. ('1', '1', ENCRYPT('$PASSWORD', CONCAT('\$6\$', SUBSTRING(SHA(RAND()), -16))), '$EMAIL');
  80.  
  81. EOF
  82. }
  83. bootstrapdb
  84.  
  85. ##Configure postfix main.cf config
  86. postconf smtpd_recipient_restrictions="permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination"
  87. postconf smtpd_sasl_auth_enable=yes
  88. postconf smtpd_sasl_path=private/auth
  89. postconf smtpd_sasl_type=dovecot
  90. postconf mydestination=localhost
  91. postconf myhostname=`hostname`
  92. postconf virtual_transport=lmtp:unix:private/dovecot-lmtp
  93.  
  94. postconf virtual_mailbox_domains=mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
  95. postconf virtual_mailbox_maps=mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
  96. postconf virtual_alias_maps=mysql:/etc/postfix/mysql-virtual-alias-maps.cf
  97.  
  98. #set IFS to blank so we preserve new lines in multiline strings
  99. IFS=""
  100.  
  101. echo "user = $DB_USER
  102. password = $DB_USER_PASS
  103. hosts = 127.0.0.1
  104. dbname = $DATABASE
  105. query = SELECT 1 FROM virtual_domains WHERE name='%s'" > /etc/postfix/mysql-virtual-mailbox-domains.cf
  106.  
  107. service postfix restart
  108.  
  109. status=`postmap -q techtalik.co mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf`
  110. if [ $status -ne 1 ]; then
  111. echo "Virtual Domains config failed."
  112. fi
  113.  
  114. echo "user = $DB_USER
  115. password = $DB_USER_PASS
  116. hosts = 127.0.0.1
  117. dbname = $DATABASE
  118. query = SELECT 1 FROM virtual_users WHERE email='%s'" > /etc/postfix/mysql-virtual-mailbox-maps.cf
  119.  
  120. service postfix restart
  121.  
  122. status=`postmap -q umair@techtalik.co mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf`
  123.  
  124. status=`postmap -q umair@techtalik.co mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf`
  125. if [ $status -ne 1 ]; then
  126. echo "Virtual users config failed."
  127. fi
  128.  
  129. echo "user = $DB_USER
  130. password = $DB_USER_PASS
  131. hosts = 127.0.0.1
  132. dbname = $DATABASE
  133. query = SELECT destination FROM virtual_aliases WHERE source='%s'" > /etc/postfix/mysql-virtual-alias-maps.cf
  134.  
  135. service postfix restart
  136.  
  137. #master.cf config
  138. postconf -M submission/inet="submission inet n - - - - smtpd"
  139. postconf -P submission/inet/syslog_name=postfix/submission
  140. postconf -P submission/inet/smtpd_tls_security_level=may
  141. postconf -P submission/inet/smtpd_sasl_auth_enable=yes
  142. postconf -P submission/inet/smtpd_client_restrictions=permit_sasl_authenticated,reject
  143.  
  144. service postfix restart
  145.  
  146. ##Dovecot
  147.  
  148. cp /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.orig
  149. cp /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/10-mail.conf.orig
  150. cp /etc/dovecot/conf.d/10-auth.conf /etc/dovecot/conf.d/10-auth.conf.orig
  151. cp /etc/dovecot/dovecot-sql.conf.ext /etc/dovecot/dovecot-sql.conf.ext.orig
  152. cp /etc/dovecot/conf.d/10-master.conf /etc/dovecot/conf.d/10-master.conf.orig
  153. cp /etc/dovecot/conf.d/10-ssl.conf /etc/dovecot/conf.d/10-ssl.conf.orig
  154. #uncomment !include conf.d/*.conf
  155. sed -i '/\!include conf\.d\/\*\.conf/s/^#//' /etc/dovecot/dovecot.conf
  156. status = `grep "protocols = imap lmtp" /etc/dovecot/dovecot.conf`
  157. if [ -z $status ];then
  158. echo "protocols = imap lmtp pop3" >> /etc/dovecot/dovecot.conf
  159. fi
  160.  
  161. sed -i '/^mail_location =.*/s/^/#/g' /etc/dovecot/conf.d/10-mail.conf #comment default mail_location
  162. echo "mail_location = maildir:/var/mail/vhosts/%d/%n" >> /etc/dovecot/conf.d/10-mail.conf
  163.  
  164.  
  165. sed -i '/^mail_privileged_group =.*/s/^/#/g' /etc/dovecot/conf.d/10-mail.conf
  166. echo "mail_privileged_group = mail" >> /etc/dovecot/conf.d/10-mail.conf
  167.  
  168. mkdir -p /var/mail/vhosts/"$DOMAIN"
  169. groupadd -g 5000 vmail
  170. useradd -g vmail -u 5000 vmail -d /var/mail
  171. chown -R vmail:vmail /var/mail
  172.  
  173. sed -i '/^auth_mechanisms =.*/s/^/#/g' /etc/dovecot/conf.d/10-auth.conf
  174. echo "auth_mechanisms = plain login" >> /etc/dovecot/conf.d/10-auth.conf
  175.  
  176. sed -i '/\!include auth-system\.conf\.ext/s/^/#/g' /etc/dovecot/conf.d/10-auth.conf
  177.  
  178. sed -i '/\!include auth-sql\.conf\.ext/s/^#//g' /etc/dovecot/conf.d/10-auth.conf
  179.  
  180.  
  181.  
  182. if [[ ! -f /etc/dovecot/conf.d/auth-sql.conf.ext.orig ]]; then
  183. mv /etc/dovecot/conf.d/auth-sql.conf.ext /etc/dovecot/conf.d/auth-sql.conf.ext.orig
  184. fi
  185.  
  186. auth10="
  187. passdb {
  188. driver = sql
  189. args = /etc/dovecot/dovecot-sql.conf.ext
  190. }
  191. userdb {
  192. driver = static
  193. args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n
  194. }
  195. "
  196. echo $auth10 > /etc/dovecot/conf.d/auth-sql.conf.ext
  197.  
  198. sed -i '/^driver =.*/s/^/#/g' /etc/dovecot/dovecot-sql.conf.ext
  199. echo "driver = mysql" >> /etc/dovecot/dovecot-sql.conf.ext
  200.  
  201. sed -i '/^connect =.*/s/^/#/g' /etc/dovecot/dovecot-sql.conf.ext
  202. echo "connect = host=127.0.0.1 dbname=$DATABASE user=$DB_USER password=$DB_USER_PASS" >> /etc/dovecot/dovecot-sql.conf.ext
  203.  
  204. sed -i '/^default_pass_scheme =.*/s/^/#/g' /etc/dovecot/dovecot-sql.conf.ext
  205. echo "default_pass_scheme = SHA512-CRYPT" >> /etc/dovecot/dovecot-sql.conf.ext
  206.  
  207. sed -i '/^password_query =.*/s/^/#/g' /etc/dovecot/dovecot-sql.conf.ext
  208. echo "password_query = SELECT email as user, password FROM virtual_users WHERE email='%u';" >> /etc/dovecot/dovecot-sql.conf.ext
  209.  
  210. chown -R vmail:dovecot /etc/dovecot
  211. chmod -R o-rwx /etc/dovecot
  212.  
  213. if [[ ! -f /etc/dovecot/conf.d/10-master.conf.orig ]]; then
  214. mv /etc/dovecot/conf.d/10-master.conf /etc/dovecot/conf.d/10-master.conf.orig
  215. fi
  216. dovecotmaster="service imap-login {
  217. inet_listener imap {
  218. port = 0
  219. }
  220. inet_listener imaps {
  221. #port = 993
  222. #ssl = yes
  223. }
  224. }
  225. service pop3-login {
  226. inet_listener pop3 {
  227. #port = 110
  228. }
  229. inet_listener pop3s {
  230. #port = 995
  231. #ssl = yes
  232. }
  233. }
  234.  
  235. service lmtp {
  236. unix_listener /var/spool/postfix/private/dovecot-lmtp {
  237. mode = 0600
  238. user = postfix
  239. group = postfix
  240. }
  241. }
  242.  
  243. service imap {
  244. }
  245.  
  246. service pop3 {
  247. }
  248.  
  249. service auth {
  250. unix_listener /var/spool/postfix/private/auth {
  251. mode = 0666
  252. user = postfix
  253. group = postfix
  254. }
  255.  
  256. unix_listener auth-userdb {
  257. mode = 0600
  258. user = vmail
  259. #group =
  260. }
  261. # Auth process is run as this user.
  262. user = dovecot
  263. }
  264.  
  265. service auth-worker {
  266. user = vmail
  267. }
  268.  
  269. service dict {
  270. unix_listener dict {
  271. }
  272. }"
  273. echo $dovecotmaster > /etc/dovecot/conf.d/10-master.conf
  274. service dovecot restart
  275. service postfix restart
  276. echo "\n\nYour mail server should be accessible now."
  277. unset $IFS
Add Comment
Please, Sign In to add comment