Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.42 KB | None | 0 0
  1. #!/bin/sh
  2. #################################################################################
  3. # Ubuntu 14.04 LTS Server #
  4. # Automated Bash script install and configure / Postfix / Dovecot / with mysql #
  5. # By https://github.com/Saleh7 #
  6. # https://github.com/Saleh7/Scripts/blob/master/postfix-dovecot.sh #
  7. #################################################################################
  8.  
  9. # Edit here ..
  10. mysqlPass='PasswordRoot' # mysql root password here
  11.  
  12. database="email_server" # name database email
  13. dbUser="user_db" # user database email
  14. dbUserPass="pass_user_db" # password user database email
  15.  
  16. Domain="example.com" # your Domain
  17. Email="saleh@example.com" # Email with your domain
  18. EmailPass="password4email" # password email
  19. #
  20. # update your system's package list
  21. #
  22. echo 'update your system ..'
  23. apt-get -qq update
  24. echo "+-----------------------------+"
  25.  
  26. #
  27. # Installing mysql with the root password set to $mysqlPass
  28. #
  29. echo "mysql-server mysql-server/root_password password $mysqlPass" | debconf-set-selections
  30. echo "mysql-server mysql-server/root_password_again password $mysqlPass" | debconf-set-selections
  31. echo 'Installing mysql ..'
  32. sudo apt-get install mysql-server -y > /dev/null 2>&1
  33. sudo apt-get install mysql-client expect -y > /dev/null 2>&1
  34. echo "+-----------------------------+"
  35.  
  36. #
  37. # running mysql_secure_installation
  38. #
  39. echo 'running mysql_secure_installation ..'
  40. installationMySql=$(expect -c '
  41. spawn /usr/bin/mysql_secure_installation
  42. expect "Enter current password for root (enter for none):"
  43. send "'$mysqlPass'\r"
  44. expect "Change the root password?"
  45. send "n\r"
  46. expect "Remove anonymous users?"
  47. send "y\r"
  48. expect "Disallow root login remotely?"
  49. send "y\r"
  50. expect "Remove test database and access to it?"
  51. send "y\r"
  52. expect "Reload privilege tables now?"
  53. send "y\r"
  54. expect eof
  55. ')
  56. echo "$installationMySql" > /dev/null 2>&1
  57. echo "+-----------------------------+"
  58.  
  59. #
  60. # Installing postfix - postfix-mysql
  61. #
  62. echo 'Installing postfix ..'
  63. echo "postfix postfix/main_mailer_type string 'Internet Site'" | debconf-set-selections
  64. echo "postfix postfix/mailname string $Domain" | debconf-set-selections
  65. sudo apt-get install postfix -y > /dev/null 2>&1
  66. sudo apt-get install postfix-mysql -y > /dev/null 2>&1
  67. echo "+-----------------------------+"
  68. #
  69. # Installing dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd dovecot-mysql
  70. #
  71. echo 'Installing dovecot ..'
  72. echo "dovecot-core dovecot-core/create-ssl-cert boolean true" | debconf-set-selections
  73. echo "dovecot-core dovecot-core/ssl-cert-name string 'localhost'" | debconf-set-selections
  74. sudo apt-get install dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd dovecot-mysql -y > /dev/null 2>&1
  75. echo "+-----------------------------+"
  76.  
  77. #
  78. # Create Database and add domain - email
  79. #
  80. createDB(){
  81. cat <<EOF | mysql -uroot -p$mysqlPass
  82. CREATE DATABASE IF NOT EXISTS $database;
  83. GRANT SELECT ON $database.* TO '$dbUser'@'127.0.0.1' IDENTIFIED BY '$dbUserPass';
  84. FLUSH PRIVILEGES;
  85. USE $database;
  86. CREATE TABLE IF NOT EXISTS $database.domains (
  87. id INT NOT NULL AUTO_INCREMENT,
  88. name VARCHAR(50) NOT NULL,
  89. PRIMARY KEY (id)
  90. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  91. CREATE TABLE IF NOT EXISTS $database.users (
  92. id INT NOT NULL AUTO_INCREMENT,
  93. domain_id INT NOT NULL,
  94. password VARCHAR(106) NOT NULL,
  95. email VARCHAR(120) NOT NULL,
  96. PRIMARY KEY (id),
  97. UNIQUE KEY email (email),
  98. FOREIGN KEY (domain_id) REFERENCES domains(id) ON DELETE CASCADE
  99. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  100. CREATE TABLE IF NOT EXISTS $database.aliases (
  101. id INT NOT NULL AUTO_INCREMENT,
  102. domain_id INT NOT NULL,
  103. source varchar(100) NOT NULL,
  104. destination varchar(100) NOT NULL,
  105. PRIMARY KEY (id),
  106. FOREIGN KEY (domain_id) REFERENCES domains(id) ON DELETE CASCADE
  107. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  108. INSERT INTO $database.domains
  109. (id ,name)
  110. VALUES
  111. ('1', '$Domain');
  112. INSERT INTO $database.users
  113. (id, domain_id, password , email)
  114. VALUES
  115. ('1', '1', MD5('$EmailPass'), '$Email');
  116. EOF
  117. }
  118. echo 'Create Database ..'
  119. createDB
  120. echo "+-----------------------------+"
  121.  
  122. #
  123. # Configure postfix main.cf config
  124. #
  125. echo 'Configure postfix main.cf'
  126. postconf -e 'smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination'
  127. postconf -e 'smtpd_sasl_auth_enable = yes'
  128. postconf -e 'smtpd_sasl_path = private/auth'
  129. postconf -e 'smtpd_sasl_type = dovecot'
  130. postconf -e 'mydestination = localhost'
  131. postconf -e "myhostname=`hostname`"
  132. postconf -e 'virtual_transport = lmtp:unix:private/dovecot-lmtp'
  133. postconf -e 'virtual_mailbox_domains = mysql:/etc/postfix/mysql-mailbox-domains.cf'
  134. postconf -e 'virtual_mailbox_maps = mysql:/etc/postfix/mysql-mailbox-maps.cf'
  135. postconf -e 'virtual_alias_maps = mysql:/etc/postfix/mysql-alias-maps.cf'
  136. echo "+-----------------------------+"
  137.  
  138. #
  139. # Connecting Postfix to the database
  140. #
  141. echo 'Configure Postfix database'
  142. echo "user = $dbUser
  143. password = $dbUserPass
  144. hosts = 127.0.0.1
  145. dbname = $database
  146. query = SELECT 1 FROM domains WHERE name='%s'
  147. " > /etc/postfix/mysql-mailbox-domains.cf
  148.  
  149. echo "user = $dbUser
  150. password = $dbUserPass
  151. hosts = 127.0.0.1
  152. dbname = $database
  153. query = SELECT 1 FROM users WHERE email='%s'
  154. " > /etc/postfix/mysql-mailbox-maps.cf
  155.  
  156. echo "user = $dbUser
  157. password = $dbUserPass
  158. hosts = 127.0.0.1
  159. dbname = $database
  160. query = SELECT destination FROM aliases WHERE source='%s'
  161. " > /etc/postfix/mysql-alias-maps.cf
  162. echo "+-----------------------------+"
  163.  
  164. #
  165. # Configure postfix master.cf config
  166. #
  167. echo 'Configure postfix master.cf ..'
  168. postconf -M submission/inet="submission inet n - - - - smtpd"
  169. postconf -P submission/inet/syslog_name=postfix/submission
  170. postconf -P submission/inet/smtpd_tls_security_level=may
  171. postconf -P submission/inet/smtpd_sasl_auth_enable=yes
  172. postconf -P submission/inet/smtpd_client_restrictions=permit_sasl_authenticated,reject
  173. echo "+-----------------------------+"
  174.  
  175. #
  176. # Configure mail location
  177. #
  178. echo 'Configure mail location ..'
  179. sudo sed -i '/\!include conf\.d\/\*\.conf/s/^#//' /etc/dovecot/dovecot.conf
  180. echo "protocols = imap lmtp pop3" >> /etc/dovecot/dovecot.conf
  181. sudo sed -i 's/#mail_location = mbox:~\/mail:INBOX=\/var\/mail\/%u/mail_location = maildir:\/var\/mail\/vhosts\/%d\/%n/' /etc/dovecot/conf.d/10-mail.conf
  182. sudo sed -i 's/#mail_privileged_group =/mail_privileged_group = mail/' /etc/dovecot/conf.d/10-mail.conf
  183. echo "+-----------------------------+"
  184.  
  185. #
  186. # Add mailuser "vmail"
  187. #
  188. echo 'Add mailuser vmail ..'
  189. mkdir -p /var/mail/vhosts/"$Domain"
  190. groupadd -g 5000 vmail
  191. useradd -g vmail -u 5000 vmail -d /var/mail
  192. chown -R vmail:vmail /var/mail
  193. echo "+-----------------------------+"
  194.  
  195. #
  196. # Configuration file /etc/dovecot/conf.d/10-auth.conf
  197. #
  198. echo 'Configuration 10-auth.conf ..'
  199. sudo sed -i 's/auth_mechanisms = plain/auth_mechanisms = plain login/' /etc/dovecot/conf.d/10-auth.conf
  200. sed -i '/\!include auth-system\.conf\.ext/s/^/#/g' /etc/dovecot/conf.d/10-auth.conf
  201. sed -i '/\!include auth-sql\.conf\.ext/s/^#//g' /etc/dovecot/conf.d/10-auth.conf
  202. echo "passdb {
  203. driver = sql
  204. args = /etc/dovecot/dovecot-sql.conf.ext
  205. }
  206. userdb {
  207. driver = static
  208. args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n
  209. }
  210. " > /etc/dovecot/conf.d/auth-sql.conf.ext
  211. echo "+-----------------------------+"
  212.  
  213. #
  214. # Authenticate using SQL database
  215. #
  216. echo 'Authenticate database ..'
  217. sudo sed -i 's/#driver =/driver = mysql/' /etc/dovecot/dovecot-sql.conf.ext
  218. sudo sed -i 's/#connect =/connect = host=127.0.0.1 dbname='$database' user='$dbUser' password='$dbUserPass'/' /etc/dovecot/dovecot-sql.conf.ext
  219. sudo sed -i 's/#default_pass_scheme = MD5/default_pass_scheme = MD5/' /etc/dovecot/dovecot-sql.conf.ext
  220. sed -i '/^password_query =.*/s/^/#/g' /etc/dovecot/dovecot-sql.conf.ext
  221. echo "password_query = SELECT email as user, password FROM users WHERE email='%u';" >> /etc/dovecot/dovecot-sql.conf.ext
  222. echo "+-----------------------------+"
  223.  
  224. #
  225. chown -R vmail:dovecot /etc/dovecot
  226. chmod -R o-rwx /etc/dovecot
  227.  
  228. #
  229. # Configure Dovecot Master
  230. #
  231. echo "service imap-login {
  232. inet_listener imap {
  233. port = 0
  234. }
  235. inet_listener imaps {
  236. #port = 993
  237. #ssl = yes
  238. }
  239. }
  240. service pop3-login {
  241. inet_listener pop3 {
  242. #port = 110
  243. }
  244. inet_listener pop3s {
  245. #port = 995
  246. #ssl = yes
  247. }
  248. }
  249. service lmtp {
  250. unix_listener /var/spool/postfix/private/dovecot-lmtp {
  251. mode = 0600
  252. user = postfix
  253. group = postfix
  254. }
  255. }
  256. service imap {
  257. }
  258. service pop3 {
  259. }
  260. service auth {
  261. unix_listener /var/spool/postfix/private/auth {
  262. mode = 0666
  263. user = postfix
  264. group = postfix
  265. }
  266. unix_listener auth-userdb {
  267. mode = 0600
  268. user = vmail
  269. #group =
  270. }
  271. # Auth process is run as this user.
  272. user = dovecot
  273. }
  274. service auth-worker {
  275. user = vmail
  276. }
  277. service dict {
  278. unix_listener dict {
  279. }
  280. }" > /etc/dovecot/conf.d/10-master.conf
  281.  
  282. #
  283. # Restart postfix - dovecot
  284. #
  285. service postfix restart
  286. service dovecot restart
  287. echo "+-----------------------------------------+"
  288. echo ""
  289. echo " Email: $Email"
  290. echo " test send email: https://emkei.cz"
  291. echo " To list the mail queue: 'postqueue -p'"
  292. echo " Read Email: 'postcat -q MESSAGE_ID'"
  293. echo ""
  294. echo "+-----------------------------------------+"
  295. echo ""
  296. echo "Done! ......"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement