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.

347 lines
12 KiB

  1. #!/bin/sh
  2. # THE SETUP
  3. # Mail will be stored in non-retarded Maildirs because it's $currentyear. This
  4. # makes it easier for use with isync, which is what I care about so I can have
  5. # an offline repo of mail.
  6. # The mailbox names are: Inbox, Sent, Drafts, Archive, Junk, Trash
  7. # Use the typical unix login system for mail users. Users will log into their
  8. # email with their passnames on the server. No usage of a redundant mySQL
  9. # database to do this.
  10. # DEPENDENCIES BEFORE RUNNING
  11. # 1. Have a Debian system with a static IP and all that. Pretty much any
  12. # default VPS offered by a company will have all the basic stuff you need. This
  13. # script might run on Ubuntu as well. Haven't tried it. If you have, tell me
  14. # what happens.
  15. # 2. Have a Let's Encrypt SSL certificate for $maildomain. You might need one
  16. # for $domain as well, but they're free with Let's Encypt so you should have
  17. # them anyway.
  18. # 3. If you've been toying around with your server settings trying to get
  19. # postfix/dovecot/etc. working before running this, I recommend you `apt purge`
  20. # everything first because this script is build on top of only the defaults.
  21. # Clear out /etc/postfix and /etc/dovecot yourself if needbe.
  22. # NOTE WHILE INSTALLING
  23. # On installation of Postfix, select "Internet Site" and put in TLD (without
  24. # `mail.` before it).
  25. echo "Installing programs..."
  26. apt install postfix dovecot-imapd dovecot-sieve opendkim spamassassin spamc
  27. # Check if OpenDKIM is installed and install it if not.
  28. which opendkim-genkey >/dev/null 2>&1 || apt install opendkim-tools
  29. domain="$(cat /etc/mailname)"
  30. subdom=${MAIL_SUBDOM:-mail}
  31. maildomain="$subdom.$domain"
  32. certdir="/etc/letsencrypt/live/$maildomain"
  33. [ ! -d "$certdir" ] && certdir="$(dirname "$(certbot certificates 2>/dev/null | grep "$maildomain\|*.$domain" -A 2 | awk '/Certificate Path/ {print $3}' | head -n1)")"
  34. [ ! -d "$certdir" ] && echo "Note! You must first have a Let's Encrypt Certbot HTTPS/SSL Certificate for $maildomain.
  35. Use Let's Encrypt's Certbot to get that and then rerun this script.
  36. You may need to set up a dummy $maildomain site in nginx or Apache for that to work." && exit 1
  37. # NOTE ON POSTCONF COMMANDS
  38. # The `postconf` command literally just adds the line in question to
  39. # /etc/postfix/main.cf so if you need to debug something, go there. It replaces
  40. # any other line that sets the same setting, otherwise it is appended to the
  41. # end of the file.
  42. echo "Configuring Postfix's main.cf..."
  43. # Change the cert/key files to the default locations of the Let's Encrypt cert/key
  44. postconf -e "smtpd_tls_key_file=$certdir/privkey.pem"
  45. postconf -e "smtpd_tls_cert_file=$certdir/fullchain.pem"
  46. postconf -e "smtp_tls_CAfile=$certdir/cert.pem"
  47. # Enable, but do not require TLS. Requiring it with other server would cause
  48. # mail delivery problems and requiring it locally would cause many other
  49. # issues.
  50. postconf -e "smtpd_tls_security_level = may"
  51. postconf -e "smtp_tls_security_level = may"
  52. # TLS required for authentication.
  53. postconf -e "smtpd_tls_auth_only = yes"
  54. # Exclude obsolete, insecure and obsolete encryption protocols.
  55. postconf -e "smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1"
  56. postconf -e "smtp_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1"
  57. postconf -e "smtpd_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1"
  58. postconf -e "smtp_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1"
  59. # Exclude suboptimal ciphers.
  60. postconf -e "tls_preempt_cipherlist = yes"
  61. postconf -e "smtpd_tls_exclude_ciphers = aNULL, LOW, EXP, MEDIUM, ADH, AECDH, MD5, DSS, ECDSA, CAMELLIA128, 3DES, CAMELLIA256, RSA+AES, eNULL"
  62. # Here we tell Postfix to look to Dovecot for authenticating users/passwords.
  63. # Dovecot will be putting an authentication socket in /var/spool/postfix/private/auth
  64. postconf -e "smtpd_sasl_auth_enable = yes"
  65. postconf -e "smtpd_sasl_type = dovecot"
  66. postconf -e "smtpd_sasl_path = private/auth"
  67. # Sender and recipient restrictions
  68. postconf -e "smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination"
  69. # NOTE: the trailing slash here, or for any directory name in the home_mailbox
  70. # command, is necessary as it distinguishes a maildir (which is the actual
  71. # directories that what we want) from a spoolfile (which is what old unix
  72. # boomers want and no one else).
  73. postconf -e "home_mailbox = Mail/Inbox/"
  74. # A fix referenced in issue #178 - Postfix configuration leaks ip addresses (https://github.com/LukeSmithxyz/emailwiz/issues/178)
  75. # Prevent "Received From:" header in sent emails in order to prevent leakage of public ip addresses
  76. postconf -e "header_checks = regexp:/etc/postfix/header_checks"
  77. # strips "Received From:" in sent emails
  78. echo "/^Received:.*/ IGNORE
  79. /^X-Originating-IP:/ IGNORE" >> /etc/postfix/header_checks
  80. # master.cf
  81. echo "Configuring Postfix's master.cf..."
  82. sed -i "/^\s*-o/d;/^\s*submission/d;/^\s*smtp/d" /etc/postfix/master.cf
  83. echo "smtp unix - - n - - smtp
  84. smtp inet n - y - - smtpd
  85. -o content_filter=spamassassin
  86. submission inet n - y - - smtpd
  87. -o syslog_name=postfix/submission
  88. -o smtpd_tls_security_level=encrypt
  89. -o smtpd_sasl_auth_enable=yes
  90. -o smtpd_tls_auth_only=yes
  91. smtps inet n - y - - smtpd
  92. -o syslog_name=postfix/smtps
  93. -o smtpd_tls_wrappermode=yes
  94. -o smtpd_sasl_auth_enable=yes
  95. spamassassin unix - n n - - pipe
  96. user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f \${sender} \${recipient}" >> /etc/postfix/master.cf
  97. # By default, dovecot has a bunch of configs in /etc/dovecot/conf.d/ These
  98. # files have nice documentation if you want to read it, but it's a huge pain to
  99. # go through them to organize. Instead, we simply overwrite
  100. # /etc/dovecot/dovecot.conf because it's easier to manage. You can get a backup
  101. # of the original in /usr/share/dovecot if you want.
  102. mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.backup.conf
  103. echo "Creating Dovecot config..."
  104. echo "# Dovecot config
  105. # Note that in the dovecot conf, you can use:
  106. # %u for username
  107. # %n for the name in name@domain.tld
  108. # %d for the domain
  109. # %h the user's home directory
  110. # If you're not a brainlet, SSL must be set to required.
  111. ssl = required
  112. ssl_cert = <$certdir/fullchain.pem
  113. ssl_key = <$certdir/privkey.pem
  114. ssl_min_protocol = TLSv1.2
  115. ssl_cipher_list = EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA256:EECDH+ECDSA+SHA384:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA384:EDH+aRSA+AESGCM:EDH+aRSA+SHA256:EDH+aRSA:EECDH:!aNULL:!eNULL:!MEDIUM:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS:!RC4:!SEED
  116. ssl_prefer_server_ciphers = yes
  117. ssl_dh = </usr/share/dovecot/dh.pem
  118. # Plaintext login. This is safe and easy thanks to SSL.
  119. auth_mechanisms = plain login
  120. auth_username_format = %n
  121. protocols = \$protocols imap
  122. # Search for valid users in /etc/passwd
  123. userdb {
  124. driver = passwd
  125. }
  126. #Fallback: Use plain old PAM to find user passwords
  127. passdb {
  128. driver = pam
  129. }
  130. # Our mail for each user will be in ~/Mail, and the inbox will be ~/Mail/Inbox
  131. # The LAYOUT option is also important because otherwise, the boxes will be \`.Sent\` instead of \`Sent\`.
  132. mail_location = maildir:~/Mail:INBOX=~/Mail/Inbox:LAYOUT=fs
  133. namespace inbox {
  134. inbox = yes
  135. mailbox Drafts {
  136. special_use = \\Drafts
  137. auto = subscribe
  138. }
  139. mailbox Junk {
  140. special_use = \\Junk
  141. auto = subscribe
  142. autoexpunge = 30d
  143. }
  144. mailbox Sent {
  145. special_use = \\Sent
  146. auto = subscribe
  147. }
  148. mailbox Trash {
  149. special_use = \\Trash
  150. }
  151. mailbox Archive {
  152. special_use = \\Archive
  153. }
  154. }
  155. # Here we let Postfix use Dovecot's authetication system.
  156. service auth {
  157. unix_listener /var/spool/postfix/private/auth {
  158. mode = 0660
  159. user = postfix
  160. group = postfix
  161. }
  162. }
  163. protocol lda {
  164. mail_plugins = \$mail_plugins sieve
  165. }
  166. protocol lmtp {
  167. mail_plugins = \$mail_plugins sieve
  168. }
  169. plugin {
  170. sieve = ~/.dovecot.sieve
  171. sieve_default = /var/lib/dovecot/sieve/default.sieve
  172. #sieve_global_path = /var/lib/dovecot/sieve/default.sieve
  173. sieve_dir = ~/.sieve
  174. sieve_global_dir = /var/lib/dovecot/sieve/
  175. }
  176. " > /etc/dovecot/dovecot.conf
  177. # If using an old version of Dovecot, remove the ssl_dl line.
  178. case "$(dovecot --version)" in
  179. 1|2.1*|2.2*) sed -i "/^ssl_dh/d" /etc/dovecot/dovecot.conf ;;
  180. esac
  181. mkdir /var/lib/dovecot/sieve/
  182. echo "require [\"fileinto\", \"mailbox\"];
  183. if header :contains \"X-Spam-Flag\" \"YES\"
  184. {
  185. fileinto \"Junk\";
  186. }" > /var/lib/dovecot/sieve/default.sieve
  187. grep -q "^vmail:" /etc/passwd || useradd vmail
  188. chown -R vmail:vmail /var/lib/dovecot
  189. sievec /var/lib/dovecot/sieve/default.sieve
  190. echo "Preparing user authentication..."
  191. grep -q nullok /etc/pam.d/dovecot ||
  192. echo "auth required pam_unix.so nullok
  193. account required pam_unix.so" >> /etc/pam.d/dovecot
  194. # OpenDKIM
  195. # A lot of the big name email services, like Google, will automatically reject
  196. # as spam unfamiliar and unauthenticated email addresses. As in, the server
  197. # will flatly reject the email, not even delivering it to someone's Spam
  198. # folder.
  199. # OpenDKIM is a way to authenticate your email so you can send to such services
  200. # without a problem.
  201. # Create an OpenDKIM key in the proper place with proper permissions.
  202. echo "Generating OpenDKIM keys..."
  203. mkdir -p /etc/postfix/dkim
  204. opendkim-genkey -D /etc/postfix/dkim/ -d "$domain" -s "$subdom"
  205. chgrp opendkim /etc/postfix/dkim/*
  206. chmod g+r /etc/postfix/dkim/*
  207. # Generate the OpenDKIM info:
  208. echo "Configuring OpenDKIM..."
  209. grep -q "$domain" /etc/postfix/dkim/keytable 2>/dev/null ||
  210. echo "$subdom._domainkey.$domain $domain:$subdom:/etc/postfix/dkim/$subdom.private" >> /etc/postfix/dkim/keytable
  211. grep -q "$domain" /etc/postfix/dkim/signingtable 2>/dev/null ||
  212. echo "*@$domain $subdom._domainkey.$domain" >> /etc/postfix/dkim/signingtable
  213. grep -q "127.0.0.1" /etc/postfix/dkim/trustedhosts 2>/dev/null ||
  214. echo "127.0.0.1
  215. 10.1.0.0/16
  216. 1.2.3.4/24" >> /etc/postfix/dkim/trustedhosts
  217. # ...and source it from opendkim.conf
  218. grep -q "^KeyTable" /etc/opendkim.conf 2>/dev/null || echo "KeyTable file:/etc/postfix/dkim/keytable
  219. SigningTable refile:/etc/postfix/dkim/signingtable
  220. InternalHosts refile:/etc/postfix/dkim/trustedhosts" >> /etc/opendkim.conf
  221. sed -i '/^#Canonicalization/s/simple/relaxed\/simple/' /etc/opendkim.conf
  222. sed -i '/^#Canonicalization/s/^#//' /etc/opendkim.conf
  223. sed -i '/Socket/s/^#*/#/' /etc/opendkim.conf
  224. grep -q "^Socket\s*inet:12301@localhost" /etc/opendkim.conf || echo "Socket inet:12301@localhost" >> /etc/opendkim.conf
  225. # OpenDKIM daemon settings, removing previously activated socket.
  226. sed -i "/^SOCKET/d" /etc/default/opendkim && echo "SOCKET=\"inet:12301@localhost\"" >> /etc/default/opendkim
  227. # Here we add to postconf the needed settings for working with OpenDKIM
  228. echo "Configuring Postfix with OpenDKIM settings..."
  229. postconf -e "smtpd_sasl_security_options = noanonymous, noplaintext"
  230. postconf -e "smtpd_sasl_tls_security_options = noanonymous"
  231. postconf -e "myhostname = $domain"
  232. postconf -e "milter_default_action = accept"
  233. postconf -e "milter_protocol = 6"
  234. postconf -e "smtpd_milters = inet:localhost:12301"
  235. postconf -e "non_smtpd_milters = inet:localhost:12301"
  236. postconf -e "mailbox_command = /usr/lib/dovecot/deliver"
  237. # A fix for "Opendkim won't start: can't open PID file?", as specified here: https://serverfault.com/a/847442
  238. /lib/opendkim/opendkim.service.generate
  239. systemctl daemon-reload
  240. for x in spamassassin opendkim dovecot postfix; do
  241. printf "Restarting %s..." "$x"
  242. service "$x" restart && printf " ...done\\n"
  243. done
  244. # If ufw is used, enable the mail ports.
  245. pgrep ufw >/dev/null && { ufw allow 993; ufw allow 465 ; ufw allow 587; ufw allow 25 ;}
  246. pval="$(tr -d "\n" </etc/postfix/dkim/$subdom.txt | sed "s/k=rsa.* \"p=/k=rsa; p=/;s/\"\s*\"//;s/\"\s*).*//" | grep -o "p=.*")"
  247. dkimentry="$subdom._domainkey.$domain TXT v=DKIM1; k=rsa; $pval"
  248. dmarcentry="_dmarc.$domain TXT v=DMARC1; p=reject; rua=mailto:dmarc@$domain; fo=1"
  249. spfentry="@ TXT v=spf1 mx a:$maildomain -all"
  250. useradd -m -G mail dmarc
  251. echo "$dkimentry
  252. $dmarcentry
  253. $spfentry" > "$HOME/dns_emailwizard"
  254. printf "\033[31m
  255. _ _
  256. | \ | | _____ ___
  257. | \| |/ _ \ \ /\ / (_)
  258. | |\ | (_) \ V V / _
  259. |_| \_|\___/ \_/\_/ (_)\033[0m
  260. Add these three records to your DNS TXT records on either your registrar's site
  261. or your DNS server:
  262. \033[32m
  263. $dkimentry
  264. $dmarcentry
  265. $spfentry
  266. \033[0m
  267. NOTE: You may need to omit the \`.$domain\` portion at the beginning if
  268. inputting them in a registrar's web interface.
  269. Also, these are now saved to \033[34m~/dns_emailwizard\033[0m in case you want them in a file.
  270. Once you do that, you're done! Check the README for how to add users/accounts
  271. and how to log in."