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.

243 lines
8.7 KiB

  1. #!/bin/sh
  2. # THE SETUP
  3. # - Mail will be stored in non-retarded Maildirs because it's $currentyear. This makes it easier for use with isync, which is what I care about so I can have an offline repo of mail.
  4. # - Mail boxes will be sensible: Inbox, Sent, Drafts, Archive, Junk, Trash
  5. # - Use the typical unix login system for mail users. Users will log into their email with their passnames on the server. No usage of a redundant mySQL database to do this.
  6. # BEFORE YOU RUN THIS
  7. # - Have a Debian system with a static IP and all that. Pretty much any default VPS offered by a company will have all the basic stuff you need. This script might run on Ubuntu as well. Haven't tried it.
  8. # - Have a Let's Encrypt SSL certificate for $maildomain. You might need one for $domain as well, but they're free with Let's Encypt so you should have them anyway.
  9. # - If you've been toying around with your server settings trying to get postfix/dovecot/etc. working before running this, I recommend you `apt purge` everything first because this script is build on top of only the defaults. Clearr out /etc/postfix and /etc/dovecot yourself if needbe.
  10. # On installation of Postfix, select "Internet Site" and put in TLD (without before it mail.)
  11. echo "Installing programs..."
  12. apt install postfix dovecot-imapd opendkim spamassassin spamc
  13. # Install another requirement for opendikm only if the above command didn't get it already
  14. [ -e $(which opendkim-genkey) ] || apt install opendkim-tools
  15. domain="$(cat /etc/mailname)"
  16. subdom="mail"
  17. maildomain="$subdom.$domain"
  18. # NOTE ON POSTCONF COMMANDS
  19. # The `postconf` command literally just adds the line in question to /etc/postfix/main.cf so if you need to debug something, go there.
  20. # It replaces any other line that sets the same setting, otherwise it is appended to the end of the file.
  21. echo "Configuring Postfix's main.cf..."
  22. # Change the cert/key files to the default locations of the Let's Encrypt cert/key
  23. postconf -e "smtpd_tls_key_file=/etc/letsencrypt/live/$maildomain/privkey.pem"
  24. postconf -e "smtpd_tls_cert_file=/etc/letsencrypt/live/$maildomain/fullchain.pem"
  25. postconf -e "smtpd_use_tls = yes"
  26. postconf -e "smtpd_tls_auth_only = yes"
  27. # Here we tell Postfix to look to Dovecot for authenticating users/passwords.
  28. # Dovecot will be putting an authentication socket in /var/spool/postfix/private/auth
  29. postconf -e "smtpd_sasl_auth_enable = yes"
  30. postconf -e "smtpd_sasl_type = dovecot"
  31. postconf -e "smtpd_sasl_path = private/auth"
  32. #postconf -e "smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination"
  33. # NOTE: the trailing slash here, or for any directory name in the home_mailbox command, is necessary as it distinguishes a maildir (which is the actual directories that what we want) from a spoolfile (which is what old unix boomers want and no one else).
  34. postconf -e "home_mailbox = Mail/Inbox/"
  35. # Research this one:
  36. #postconf -e "mailbox_command ="
  37. # master.cf
  38. echo "Configuring Postfix's master.cf..."
  39. sed -i "/^\s*-o/d;/^\s*submission/d;/^\s*smtp/d" /etc/postfix/master.cf
  40. echo "smtp unix - - n - - smtp
  41. smtp inet n - y - - smtpd
  42. -o content_filter=spamassassin
  43. submission inet n - y - - smtpd
  44. -o syslog_name=postfix/submission
  45. -o smtpd_tls_security_level=encrypt
  46. -o smtpd_sasl_auth_enable=yes
  47. -o smtpd_tls_auth_only=yes
  48. smtps inet n - y - - smtpd
  49. -o syslog_name=postfix/smtps
  50. -o smtpd_tls_wrappermode=yes
  51. -o smtpd_sasl_auth_enable=yes
  52. spamassassin unix - n n - - pipe
  53. user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f \${sender} \${recipient}" >> /etc/postfix/master.cf
  54. # By default, dovecot has a bunch of configs in /etc/dovecot/conf.d/
  55. # These files have nice documentation if you want to read it, but it's a huge pain to go through them to organize.
  56. # Instead, we simply overwrite /etc/dovecot/dovecot.conf because it's easier to manage. You can get a backup of the original in /usr/share/dovecot if you want.
  57. echo "Creating Dovecot config..."
  58. echo "# Dovecot config
  59. # Note that in the dovecot conf, you can use:
  60. # %u for username
  61. # %n for the name in name@domain.tld
  62. # %d for the domain
  63. # %h the user's home directory
  64. # If you're not a brainlet, SSL must be set to required.
  65. ssl = required
  66. ssl_cert = </etc/letsencrypt/live/$maildomain/fullchain.pem
  67. ssl_key = </etc/letsencrypt/live/$maildomain/privkey.pem
  68. # Plaintext login. This is safe and easy thanks to SSL.
  69. auth_mechanisms = plain
  70. protocols = \$protocols imap
  71. # Search for valid users in /etc/passwd
  72. userdb {
  73. driver = passwd
  74. }
  75. # Use plain old PAM to find user passwords
  76. passdb {
  77. driver = pam
  78. }
  79. # Our mail for each user will be in ~/Mail, and the inbox will be ~/Mail/Inbox
  80. # The LAYOUT option is also important because otherwise, the boxes will be \`.Sent\` instead of \`Sent\`.
  81. mail_location = maildir:~/Mail:INBOX=~/Mail/Inbox:LAYOUT=fs
  82. namespace inbox {
  83. inbox = yes
  84. mailbox Drafts {
  85. special_use = \\Drafts
  86. auto = subscribe
  87. }
  88. mailbox Junk {
  89. special_use = \\Junk
  90. auto = subscribe
  91. autoexpunge = 30d
  92. }
  93. mailbox Sent {
  94. special_use = \\Sent
  95. auto = subscribe
  96. }
  97. mailbox Trash {
  98. special_use = \\Trash
  99. }
  100. mailbox Archive {
  101. special_use = \\Archive
  102. }
  103. }
  104. # Here we let Postfix use Dovecot's authetication system.
  105. service auth {
  106. unix_listener /var/spool/postfix/private/auth {
  107. mode = 0660
  108. user = postfix
  109. group = postfix
  110. }
  111. }
  112. protocol lda {
  113. mail_plugins = \$mail_plugins sieve
  114. }
  115. protocol lmtp {
  116. mail_plugins = \$mail_plugins sieve
  117. }
  118. plugin {
  119. sieve = ~/.dovecot.sieve
  120. sieve_default = /var/lib/dovecot/sieve/default.sieve
  121. #sieve_global_path = /var/lib/dovecot/sieve/default.sieve
  122. sieve_dir = ~/.sieve
  123. sieve_global_dir = /var/lib/dovecot/sieve/
  124. }
  125. " > /etc/dovecot/dovecot.conf
  126. mkdir /var/lib/dovecot/sieve/
  127. echo "require [\"fileinto\", \"mailbox\"];
  128. if header :contains \"X-Spam-Flag\" \"YES\"
  129. {
  130. fileinto \"Junk\";
  131. }" > /var/lib/dovecot/sieve/default.sieve
  132. chown -R vmail:vmail /var/lib/dovecot
  133. sievec /var/lib/dovecot/sieve/default.sieve
  134. echo "Preparing user authetication..."
  135. grep nullok /etc/pam.d/dovecot >/dev/null ||
  136. echo "auth required pam_unix.so nullok
  137. account required pam_unix.so" >> /etc/pam.d/dovecot
  138. # OpenDKIM
  139. # A lot of the big name email services, like Google, will automatically rejectmark as spam unfamiliar and unauthenticated email addresses. As in, the server will flattly reject the email, not even deliverring it to someone's Spam folder.
  140. # OpenDKIM is a way to authenticate your email so you can send to such services without a problem.
  141. # add opendkim-tools ?
  142. # Create an OpenDKIM key and put in in the proper place with proper permissions.
  143. echo "Generating OpenDKIM keys..."
  144. mkdir -p /etc/postfix/dkim
  145. opendkim-genkey -D /etc/postfix/dkim/ -d $ "$domain" -s "$subdom"
  146. chgrp opendkim /etc/postfix/dkim/*
  147. chmod g+r /etc/postfix/dkim/*
  148. # Generate the OpenDKIM info:
  149. echo "Configuring OpenDKIM..."
  150. grep "$domain" >/dev/null 2>&1 /etc/postfix/dkim/keytable ||
  151. echo "$subdom._domainkey.$domain $domain:mail:/etc/postfix/dkim/mail.private" >> /etc/postfix/dkim/keytable
  152. grep "$domain" >/dev/null 2>&1 /etc/postfix/dkim/signingtable ||
  153. echo "*@$domain $subdom._domainkey.$domain" >> /etc/postfix/dkim/signingtable
  154. grep "127.0.0.1" >/dev/null 2>&1 /etc/postfix/dkim/trustedhosts ||
  155. echo "127.0.0.1
  156. 10.1.0.0/16
  157. 1.2.3.4/24" >> /etc/postfix/dkim/trustedhosts
  158. # ...and source it from opendkim.conf
  159. grep ^KeyTable /etc/opendkim.conf >/dev/null || echo "KeyTable file:/etc/postfix/dkim/keytable
  160. SigningTable refile:/etc/postfix/dkim/signingtable
  161. InternalHosts refile:/etc/postfix/dkim/trustedhosts" >> /etc/opendkim.conf
  162. # OpenDKIM daemon settings, removing previously activated socket.
  163. sed -i "/^SOCKET/d" /etc/default/opendkim && echo "SOCKET=\"inet:8891@localhost\"" >> /etc/default/opendkim
  164. # Here we add to postconf the needed settings for working with OpenDKIM
  165. echo "Configuring Postfix with OpenDKIM settings..."
  166. postconf -e "milter_default_action = accept"
  167. postconf -e "milter_protocol = 2"
  168. postconf -e "smtpd_milters = inet:localhost:8891"
  169. postconf -e "non_smtpd_milters = inet:localhost:8891"
  170. postconf -e "mailbox_command = /usr/lib/dovecot/deliver"
  171. echo "Restarting Dovecot..."
  172. service dovecot restart && echo "Dovecot restarted."
  173. echo "Restarting Postfix..."
  174. service postfix restart && echo "Postfix restarted."
  175. echo "Restarting OpenDKIM..."
  176. service opendkim restart && echo "OpenDKIM restarted."
  177. echo "Restarting Spam Assassin..."
  178. service spamassassin restart && echo "Spamassassin restarted."
  179. pval="$(tr -d "\n" </etc/postfix/dkim/mail.txt | sed "s/k=rsa.* \"p=/k=rsa; p=/;s/\"\s*\"//;s/\"\s*).*//" | grep -o p=.*)"
  180. echo "Here is your TXT entry:"
  181. echo
  182. echo
  183. echo
  184. printf "Record Name\\tRecord Type\\tText of entry\\n"
  185. printf "%s._domainkey\\tTXT\\t\\tv=DKIM1; k=rsa; %s\\n" "$subdom" "$pval"
  186. echo
  187. echo
  188. echo "$pval"