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.

219 lines
8.1 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. domain="$(cat /etc/mailname)"
  14. subdom="mail"
  15. maildomain="$subdom.$domain"
  16. # NOTE ON POSTCONF COMMANDS
  17. # The `postconf` command literally just adds the line in question to /etc/postfix/main.cf so if you need to debug something, go there.
  18. # It replaces any other line that sets the same setting, otherwise it is appended to the end of the file.
  19. echo "Configuring Postfix's main.cf..."
  20. # Change the cert/key files to the default locations of the Let's Encrypt cert/key
  21. postconf -e "smtpd_tls_key_file=/etc/letsencrypt/live/$maildomain/privkey.pem"
  22. postconf -e "smtpd_tls_cert_file=/etc/letsencrypt/live/$maildomain/fullchain.pem"
  23. postconf -e "smtpd_use_tls = yes"
  24. postconf -e "smtpd_tls_auth_only = yes"
  25. postconf -e "smtp_tls_security_level = may"
  26. postconf -e "smtp_tls_loglevel = 1"
  27. postconf -e "smtp_tls_CAfile = /etc/letsencrypt/live/$maildomain/cert.pem"
  28. # Here we tell Postfix to look to Dovecot for authenticating users/passwords.
  29. # Dovecot will be putting an authentication socket in /var/spool/postfix/private/auth
  30. postconf -e "smtpd_sasl_auth_enable = yes"
  31. postconf -e "smtpd_sasl_type = dovecot"
  32. postconf -e "smtpd_sasl_path = private/auth"
  33. #postconf -e "smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination"
  34. # 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).
  35. postconf -e "home_mailbox = Mail/Inbox/"
  36. # Research this one:
  37. #postconf -e "mailbox_command ="
  38. # master.cf
  39. echo "Configuring Postfix's master.cf..."
  40. sed -i "/^\s*-o/d;/^\s*submission/d;/^\s*smtp/d" /etc/postfix/master.cf
  41. echo "smtp unix - - n - - smtp
  42. smtp inet n - y - - smtpd
  43. submission inet n - y - - smtpd
  44. -o syslog_name=postfix/submission
  45. -o smtpd_tls_security_level=encrypt
  46. -o content_filter=spamassassin
  47. -o smtpd_sasl_auth_enable=yes
  48. -o smtpd_tls_auth_only=yes
  49. smtps inet n - y - - smtpd
  50. -o syslog_name=postfix/smtps
  51. -o content_filter=spamassassin
  52. -o smtpd_tls_wrappermode=yes
  53. -o smtpd_sasl_auth_enable=yes
  54. spamassassin unix - n n - - pipe
  55. user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f \${sender} \${recipient}" >> /etc/postfix/master.cf
  56. # By default, dovecot has a bunch of configs in /etc/dovecot/conf.d/
  57. # These files have nice documentation if you want to read it, but it's a huge pain to go through them to organize.
  58. # 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.
  59. echo "Creating Dovecot config..."
  60. echo "# Dovecot config
  61. # Note that in the dovecot conf, you can use:
  62. # %u for username
  63. # %n for the name in name@domain.tld
  64. # %d for the domain
  65. # %h the user's home directory
  66. # If you're not a brainlet, SSL must be set to required.
  67. ssl = required
  68. ssl_cert = </etc/letsencrypt/live/$maildomain/fullchain.pem
  69. ssl_key = </etc/letsencrypt/live/$maildomain/privkey.pem
  70. # Plaintext login. This is safe and easy thanks to SSL.
  71. auth_mechanisms = plain
  72. protocols = \$protocols imap
  73. # Search for valid users in /etc/passwd
  74. userdb {
  75. driver = passwd
  76. }
  77. # Use plain old PAM to find user passwords
  78. passdb {
  79. driver = pam
  80. }
  81. # Our mail for each user will be in ~/Mail, and the inbox will be ~/Mail/Inbox
  82. # The LAYOUT option is also important because otherwise, the boxes will be \`.Sent\` instead of \`Sent\`.
  83. mail_location = maildir:~/Mail:INBOX=~/Mail/Inbox:LAYOUT=fs
  84. namespace inbox {
  85. inbox = yes
  86. mailbox Drafts {
  87. special_use = \\Drafts
  88. auto = subscribe
  89. }
  90. mailbox Junk {
  91. special_use = \\Junk
  92. auto = subscribe
  93. autoexpunge = 30d
  94. }
  95. mailbox Sent {
  96. special_use = \\Sent
  97. auto = subscribe
  98. }
  99. mailbox Trash {
  100. special_use = \\Trash
  101. }
  102. mailbox Archive {
  103. special_use = \\Archive
  104. }
  105. }
  106. # Here we let Postfix use Dovecot's authetication system.
  107. service auth {
  108. unix_listener /var/spool/postfix/private/auth {
  109. mode = 0660
  110. user = postfix
  111. group = postfix
  112. }
  113. }
  114. " > /etc/dovecot/dovecot.conf
  115. echo "Preparing user authetication..."
  116. grep nullok /etc/pam.d/dovecot >/dev/null ||
  117. echo "auth required pam_unix.so nullok
  118. account required pam_unix.so" >> /etc/pam.d/dovecot
  119. # OpenDKIM
  120. # 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.
  121. # OpenDKIM is a way to authenticate your email so you can send to such services without a problem.
  122. # add opendkim-tools ?
  123. # Create an OpenDKIM key and put in in the proper place with proper permissions.
  124. echo "Generating OpenDKIM keys..."
  125. mkdir -p /etc/postfix/dkim
  126. opendkim-genkey -D /etc/postfix/dkim/ -d $ "$domain" -s "$subdom"
  127. chgrp opendkim /etc/postfix/dkim/*
  128. chmod g+r /etc/postfix/dkim/*
  129. # Generate the OpenDKIM info:
  130. echo "Configuring OpenDKIM..."
  131. grep "$domain" >/dev/null 2>&1 /etc/postfix/dkim/keytable ||
  132. echo "$subdom._domainkey.$domain $domain:mail:/etc/postfix/dkim/mail.private" >> /etc/postfix/dkim/keytable
  133. grep "$domain" >/dev/null 2>&1 /etc/postfix/dkim/signingtable ||
  134. echo "*@$domain $subdom._domainkey.$domain" >> /etc/postfix/dkim/signingtable
  135. grep "127.0.0.1" >/dev/null 2>&1 /etc/postfix/dkim/trustedhosts ||
  136. echo "127.0.0.1
  137. 10.1.0.0/16
  138. 1.2.3.4/24" >> /etc/postfix/dkim/trustedhosts
  139. # ...and source it from opendkim.conf
  140. grep ^KeyTable /etc/opendkim.conf >/dev/null || echo "KeyTable file:/etc/postfix/dkim/keytable
  141. SigningTable refile:/etc/postfix/dkim/signingtable
  142. InternalHosts refile:/etc/postfix/dkim/trustedhosts" >> /etc/opendkim.conf
  143. # OpenDKIM daemon settings, removing previously activated socket.
  144. sed -i "/^SOCKET/d" /etc/default/opendkim && echo "SOCKET=\"inet:8891@localhost\"" >> /etc/default/opendkim
  145. # Here we add to postconf the needed settings for working with OpenDKIM
  146. echo "Configuring Postfix with OpenDKIM settings..."
  147. postconf -e "milter_default_action = accept"
  148. postconf -e "milter_protocol = 2"
  149. postconf -e "smtpd_milters = inet:localhost:8891"
  150. postconf -e "non_smtpd_milters = inet:localhost:8891"
  151. echo "Restarting Dovecot..."
  152. service dovecot restart && echo "Dovecot restarted."
  153. echo "Restarting Postfix..."
  154. service postfix restart && echo "Postfix restarted."
  155. echo "Restarting OpenDKIM..."
  156. service opendkim restart && echo "OpenDKIM restarted."
  157. echo "Restarting Spam Assassin..."
  158. service spamassassin restart && echo "Spamassassin restarted."
  159. pval="$(tr -d "\n" </etc/postfix/dkim/mail.txt | sed "s/k=rsa.* \"p=/k=rsa; p=/;s/\"\s*\"//;s/\"\s*).*//" | grep -o p=.*)"
  160. echo "Here is your TXT entry:"
  161. echo
  162. echo
  163. echo
  164. printf "Record Name\\tRecord Type\\tText of entry\\n"
  165. printf "%s._domainkey\\tTXT\\t\\tv=DKIM1; k=rsa; %s\\n" "$subdom" "$pval"
  166. echo
  167. echo
  168. echo "$pval"