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.

364 lines
13 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. umask 0022
  26. apt-get install -y postfix postfix-pcre dovecot-imapd dovecot-sieve opendkim spamassassin spamc net-tools
  27. # Check if OpenDKIM is installed and install it if not.
  28. which opendkim-genkey >/dev/null 2>&1 || apt-get install opendkim-tools
  29. domain="$(cat /etc/mailname)"
  30. subdom=${MAIL_SUBDOM:-mail}
  31. maildomain="$subdom.$domain"
  32. certdir="/etc/letsencrypt/live/$maildomain"
  33. # Open required mail ports, and 80, for Certbot.
  34. for port in 80 993 465 25 587; do
  35. ufw allow "$port" 2>/dev/null
  36. done
  37. [ ! -d "$certdir" ] &&
  38. possiblecert="$(certbot certificates 2>/dev/null | grep "Domains:\.* $maildomain\(\s\|$\)" -A 2 | awk '/Certificate Path/ {print $3}' | head -n1)" &&
  39. certdir="${possiblecert%/*}"
  40. [ ! -d "$certdir" ] && case "$(netstat -tulpn | grep ":80\s")" in
  41. *nginx*)
  42. apt install -y python3-certbot-nginx
  43. certbot -d "$maildomain" certonly --nginx --register-unsafely-without-email --agree-tos
  44. ;;
  45. *apache*)
  46. apt install -y python3-certbot-apache
  47. certbot -d "$maildomain" certonly --apache --register-unsafely-without-email --agree-tos
  48. ;;
  49. *)
  50. apt install -y python3-certbot
  51. certbot -d "$maildomain" certonly --standalone --register-unsafely-without-email --agree-tos
  52. ;;
  53. esac || exit $1
  54. echo "Configuring Postfix's main.cf..."
  55. # Change the cert/key files to the default locations of the Let's Encrypt cert/key
  56. postconf -e "smtpd_tls_key_file=$certdir/privkey.pem"
  57. postconf -e "smtpd_tls_cert_file=$certdir/fullchain.pem"
  58. postconf -e "smtp_tls_CAfile=$certdir/cert.pem"
  59. # Enable, but do not require TLS. Requiring it with other server would cause
  60. # mail delivery problems and requiring it locally would cause many other
  61. # issues.
  62. postconf -e 'smtpd_tls_security_level = may'
  63. postconf -e 'smtp_tls_security_level = may'
  64. # TLS required for authentication.
  65. postconf -e 'smtpd_tls_auth_only = yes'
  66. # Exclude obsolete, insecure and obsolete encryption protocols.
  67. postconf -e 'smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1'
  68. postconf -e 'smtp_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1'
  69. postconf -e 'smtpd_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1'
  70. postconf -e 'smtp_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1'
  71. # Exclude suboptimal ciphers.
  72. postconf -e 'tls_preempt_cipherlist = yes'
  73. postconf -e 'smtpd_tls_exclude_ciphers = aNULL, LOW, EXP, MEDIUM, ADH, AECDH, MD5, DSS, ECDSA, CAMELLIA128, 3DES, CAMELLIA256, RSA+AES, eNULL'
  74. # Here we tell Postfix to look to Dovecot for authenticating users/passwords.
  75. # Dovecot will be putting an authentication socket in /var/spool/postfix/private/auth
  76. postconf -e 'smtpd_sasl_auth_enable = yes'
  77. postconf -e 'smtpd_sasl_type = dovecot'
  78. postconf -e 'smtpd_sasl_path = private/auth'
  79. # Sender, relay and recipient restrictions
  80. postconf -e "smtpd_sender_login_maps = pcre:/etc/postfix/login_maps.pcre"
  81. postconf -e 'smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination, reject_unknown_recipient_domain'
  82. postconf -e 'smtpd_relay_restrictions = permit_sasl_authenticated, reject_unauth_destination'
  83. # NOTE: the trailing slash here, or for any directory name in the home_mailbox
  84. # command, is necessary as it distinguishes a maildir (which is the actual
  85. # directories that what we want) from a spoolfile (which is what old unix
  86. # boomers want and no one else).
  87. postconf -e 'home_mailbox = Mail/Inbox/'
  88. # Prevent "Received From:" header in sent emails in order to prevent leakage of public ip addresses
  89. postconf -e "header_checks = regexp:/etc/postfix/header_checks"
  90. # strips "Received From:" in sent emails
  91. echo "/^Received:.*/ IGNORE
  92. /^X-Originating-IP:/ IGNORE" >> /etc/postfix/header_checks
  93. # Create a login map file that ensures that if a sender wants to send a mail from a user at our local
  94. # domain, they must be authenticated as that user
  95. echo "/^(.*)@$(sh -c "echo $domain | sed 's/\./\\\./'")$/ \${1}" > /etc/postfix/login_maps.pcre
  96. # master.cf
  97. echo "Configuring Postfix's master.cf..."
  98. sed -i '/^\s*-o/d;/^\s*submission/d;/^\s*smtp/d' /etc/postfix/master.cf
  99. echo "smtp unix - - n - - smtp
  100. smtp inet n - y - - smtpd
  101. -o content_filter=spamassassin
  102. submission inet n - y - - smtpd
  103. -o syslog_name=postfix/submission
  104. -o smtpd_tls_security_level=encrypt
  105. -o smtpd_sasl_auth_enable=yes
  106. -o smtpd_tls_auth_only=yes
  107. smtps inet n - y - - smtpd
  108. -o syslog_name=postfix/smtps
  109. -o smtpd_tls_wrappermode=yes
  110. -o smtpd_sasl_auth_enable=yes
  111. spamassassin unix - n n - - pipe
  112. user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f \${sender} \${recipient}" >> /etc/postfix/master.cf
  113. # By default, dovecot has a bunch of configs in /etc/dovecot/conf.d/ These
  114. # files have nice documentation if you want to read it, but it's a huge pain to
  115. # go through them to organize. Instead, we simply overwrite
  116. # /etc/dovecot/dovecot.conf because it's easier to manage. You can get a backup
  117. # of the original in /usr/share/dovecot if you want.
  118. mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.backup.conf
  119. echo "Creating Dovecot config..."
  120. echo "# Dovecot config
  121. # Note that in the dovecot conf, you can use:
  122. # %u for username
  123. # %n for the name in name@domain.tld
  124. # %d for the domain
  125. # %h the user's home directory
  126. # If you're not a brainlet, SSL must be set to required.
  127. ssl = required
  128. ssl_cert = <$certdir/fullchain.pem
  129. ssl_key = <$certdir/privkey.pem
  130. ssl_min_protocol = TLSv1.2
  131. 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'"
  132. ssl_prefer_server_ciphers = yes
  133. ssl_dh = </usr/share/dovecot/dh.pem
  134. # Plaintext login. This is safe and easy thanks to SSL.
  135. auth_mechanisms = plain login
  136. auth_username_format = %n
  137. protocols = \$protocols imap
  138. # Search for valid users in /etc/passwd
  139. userdb {
  140. driver = passwd
  141. }
  142. #Fallback: Use plain old PAM to find user passwords
  143. passdb {
  144. driver = pam
  145. }
  146. # Our mail for each user will be in ~/Mail, and the inbox will be ~/Mail/Inbox
  147. # The LAYOUT option is also important because otherwise, the boxes will be \`.Sent\` instead of \`Sent\`.
  148. mail_location = maildir:~/Mail:INBOX=~/Mail/Inbox:LAYOUT=fs
  149. namespace inbox {
  150. inbox = yes
  151. mailbox Drafts {
  152. special_use = \\Drafts
  153. auto = subscribe
  154. }
  155. mailbox Junk {
  156. special_use = \\Junk
  157. auto = subscribe
  158. autoexpunge = 30d
  159. }
  160. mailbox Sent {
  161. special_use = \\Sent
  162. auto = subscribe
  163. }
  164. mailbox Trash {
  165. special_use = \\Trash
  166. }
  167. mailbox Archive {
  168. special_use = \\Archive
  169. }
  170. }
  171. # Here we let Postfix use Dovecot's authetication system.
  172. service auth {
  173. unix_listener /var/spool/postfix/private/auth {
  174. mode = 0660
  175. user = postfix
  176. group = postfix
  177. }
  178. }
  179. protocol lda {
  180. mail_plugins = \$mail_plugins sieve
  181. }
  182. protocol lmtp {
  183. mail_plugins = \$mail_plugins sieve
  184. }
  185. plugin {
  186. sieve = ~/.dovecot.sieve
  187. sieve_default = /var/lib/dovecot/sieve/default.sieve
  188. #sieve_global_path = /var/lib/dovecot/sieve/default.sieve
  189. sieve_dir = ~/.sieve
  190. sieve_global_dir = /var/lib/dovecot/sieve/
  191. }
  192. " > /etc/dovecot/dovecot.conf
  193. # If using an old version of Dovecot, remove the ssl_dl line.
  194. case "$(dovecot --version)" in
  195. 1|2.1*|2.2*) sed -i '/^ssl_dh/d' /etc/dovecot/dovecot.conf ;;
  196. esac
  197. mkdir /var/lib/dovecot/sieve/
  198. echo "require [\"fileinto\", \"mailbox\"];
  199. if header :contains \"X-Spam-Flag\" \"YES\"
  200. {
  201. fileinto \"Junk\";
  202. }" > /var/lib/dovecot/sieve/default.sieve
  203. grep -q '^vmail:' /etc/passwd || useradd vmail
  204. chown -R vmail:vmail /var/lib/dovecot
  205. sievec /var/lib/dovecot/sieve/default.sieve
  206. echo 'Preparing user authentication...'
  207. grep -q nullok /etc/pam.d/dovecot ||
  208. echo 'auth required pam_unix.so nullok
  209. account required pam_unix.so' >> /etc/pam.d/dovecot
  210. # OpenDKIM
  211. # A lot of the big name email services, like Google, will automatically reject
  212. # as spam unfamiliar and unauthenticated email addresses. As in, the server
  213. # will flatly reject the email, not even delivering it to someone's Spam
  214. # folder.
  215. # OpenDKIM is a way to authenticate your email so you can send to such services
  216. # without a problem.
  217. # Create an OpenDKIM key in the proper place with proper permissions.
  218. echo 'Generating OpenDKIM keys...'
  219. mkdir -p "/etc/postfix/dkim/$domain"
  220. opendkim-genkey -D "/etc/postfix/dkim/$domain" -d "$domain" -s "$subdom"
  221. chgrp -R opendkim /etc/postfix/dkim/*
  222. chmod -R g+r /etc/postfix/dkim/*
  223. # Generate the OpenDKIM info:
  224. echo 'Configuring OpenDKIM...'
  225. grep -q "$domain" /etc/postfix/dkim/keytable 2>/dev/null ||
  226. echo "$subdom._domainkey.$domain $domain:$subdom:/etc/postfix/dkim/$domain/$subdom.private" >> /etc/postfix/dkim/keytable
  227. grep -q "$domain" /etc/postfix/dkim/signingtable 2>/dev/null ||
  228. echo "*@$domain $subdom._domainkey.$domain" >> /etc/postfix/dkim/signingtable
  229. grep -q '127.0.0.1' /etc/postfix/dkim/trustedhosts 2>/dev/null ||
  230. echo '127.0.0.1
  231. 10.1.0.0/16' >> /etc/postfix/dkim/trustedhosts
  232. # ...and source it from opendkim.conf
  233. grep -q '^KeyTable' /etc/opendkim.conf 2>/dev/null || echo 'KeyTable file:/etc/postfix/dkim/keytable
  234. SigningTable refile:/etc/postfix/dkim/signingtable
  235. InternalHosts refile:/etc/postfix/dkim/trustedhosts' >> /etc/opendkim.conf
  236. sed -i '/^#Canonicalization/s/simple/relaxed\/simple/' /etc/opendkim.conf
  237. sed -i '/^#Canonicalization/s/^#//' /etc/opendkim.conf
  238. sed -i '/Socket/s/^#*/#/' /etc/opendkim.conf
  239. grep -q '^Socket\s*inet:12301@localhost' /etc/opendkim.conf || echo 'Socket inet:12301@localhost' >> /etc/opendkim.conf
  240. # OpenDKIM daemon settings, removing previously activated socket.
  241. sed -i '/^SOCKET/d' /etc/default/opendkim && echo "SOCKET=\"inet:12301@localhost\"" >> /etc/default/opendkim
  242. # Here we add to postconf the needed settings for working with OpenDKIM
  243. echo 'Configuring Postfix with OpenDKIM settings...'
  244. postconf -e 'smtpd_sasl_security_options = noanonymous, noplaintext'
  245. postconf -e 'smtpd_sasl_tls_security_options = noanonymous'
  246. postconf -e "myhostname = $domain"
  247. postconf -e 'milter_default_action = accept'
  248. postconf -e 'milter_protocol = 6'
  249. postconf -e 'smtpd_milters = inet:localhost:12301'
  250. postconf -e 'non_smtpd_milters = inet:localhost:12301'
  251. postconf -e 'mailbox_command = /usr/lib/dovecot/deliver'
  252. postconf -e 'smtpd_helo_required = yes'
  253. postconf -e 'smtpd_helo_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_invalid_helo_hostname, reject_non_fqdn_helo_hostname, reject_unknown_helo_hostname'
  254. postconf -e 'smtpd_sender_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_sender_login_mismatch, reject_unknown_reverse_client_hostname, reject_unknown_sender_domain'
  255. # A fix for "Opendkim won't start: can't open PID file?", as specified here: https://serverfault.com/a/847442
  256. /lib/opendkim/opendkim.service.generate
  257. systemctl daemon-reload
  258. for x in spamassassin opendkim dovecot postfix; do
  259. printf "Restarting %s..." "$x"
  260. service "$x" restart && printf " ...done\\n"
  261. systemctl enable "$x"
  262. done
  263. pval="$(tr -d '\n' <"/etc/postfix/dkim/$domain/$subdom.txt" | sed "s/k=rsa.* \"p=/k=rsa; p=/;s/\"\s*\"//;s/\"\s*).*//" | grep -o 'p=.*')"
  264. dkimentry="$subdom._domainkey.$domain TXT v=DKIM1; k=rsa; $pval"
  265. dmarcentry="_dmarc.$domain TXT v=DMARC1; p=reject; rua=mailto:dmarc@$domain; fo=1"
  266. spfentry="$domain TXT v=spf1 mx a:$maildomain -all"
  267. useradd -m -G mail dmarc
  268. grep -q '^deploy-hook = echo "$RENEWED_DOMAINS" | grep -q' /etc/letsencrypt/cli.ini ||
  269. echo "
  270. deploy-hook = echo \"\$RENEWED_DOMAINS\" | grep -q '$maildomain' && service postfix reload && service dovecot reload" >> /etc/letsencrypt/cli.ini
  271. echo "$dkimentry
  272. $dmarcentry
  273. $spfentry" > "$HOME/dns_emailwizard"
  274. printf "\033[31m
  275. _ _
  276. | \ | | _____ ___
  277. | \| |/ _ \ \ /\ / (_)
  278. | |\ | (_) \ V V / _
  279. |_| \_|\___/ \_/\_/ (_)\033[0m
  280. Add these three records to your DNS TXT records on either your registrar's site
  281. or your DNS server:
  282. \033[32m
  283. $dkimentry
  284. $dmarcentry
  285. $spfentry
  286. \033[0m
  287. NOTE: You may need to omit the \`.$domain\` portion at the beginning if
  288. inputting them in a registrar's web interface.
  289. Also, these are now saved to \033[34m~/dns_emailwizard\033[0m in case you want them in a file.
  290. Once you do that, you're done! Check the README for how to add users/accounts
  291. and how to log in.\n"