Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

329 рядки
11 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" -A 2 | awk '/Certificate Path/ {print $3}')")"
  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
  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 "smtpd_use_tls = yes"
  47. postconf -e "smtpd_tls_auth_only = yes"
  48. postconf -e "smtp_tls_security_level = may"
  49. postconf -e "smtp_tls_loglevel = 1"
  50. postconf -e "smtp_tls_CAfile=$certdir/cert.pem"
  51. postconf -e "smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1"
  52. postconf -e "smtp_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1"
  53. postconf -e "smtpd_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1"
  54. postconf -e "smtp_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1"
  55. postconf -e "tls_preempt_cipherlist = yes"
  56. postconf -e "smtpd_tls_exclude_ciphers = aNULL, LOW, EXP, MEDIUM, ADH, AECDH, MD5,
  57. DSS, ECDSA, CAMELLIA128, 3DES, CAMELLIA256,
  58. RSA+AES, eNULL"
  59. # Here we tell Postfix to look to Dovecot for authenticating users/passwords.
  60. # Dovecot will be putting an authentication socket in /var/spool/postfix/private/auth
  61. postconf -e "smtpd_sasl_auth_enable = yes"
  62. postconf -e "smtpd_sasl_type = dovecot"
  63. postconf -e "smtpd_sasl_path = private/auth"
  64. #postconf -e "smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination"
  65. # NOTE: the trailing slash here, or for any directory name in the home_mailbox
  66. # command, is necessary as it distinguishes a maildir (which is the actual
  67. # directories that what we want) from a spoolfile (which is what old unix
  68. # boomers want and no one else).
  69. postconf -e "home_mailbox = Mail/Inbox/"
  70. # Research this one:
  71. #postconf -e "mailbox_command ="
  72. # master.cf
  73. echo "Configuring Postfix's master.cf..."
  74. sed -i "/^\s*-o/d;/^\s*submission/d;/^\s*smtp/d" /etc/postfix/master.cf
  75. echo "smtp unix - - n - - smtp
  76. smtp inet n - y - - smtpd
  77. -o content_filter=spamassassin
  78. submission inet n - y - - smtpd
  79. -o syslog_name=postfix/submission
  80. -o smtpd_tls_security_level=encrypt
  81. -o smtpd_sasl_auth_enable=yes
  82. -o smtpd_tls_auth_only=yes
  83. smtps inet n - y - - smtpd
  84. -o syslog_name=postfix/smtps
  85. -o smtpd_tls_wrappermode=yes
  86. -o smtpd_sasl_auth_enable=yes
  87. spamassassin unix - n n - - pipe
  88. user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f \${sender} \${recipient}" >> /etc/postfix/master.cf
  89. # By default, dovecot has a bunch of configs in /etc/dovecot/conf.d/ These
  90. # files have nice documentation if you want to read it, but it's a huge pain to
  91. # go through them to organize. Instead, we simply overwrite
  92. # /etc/dovecot/dovecot.conf because it's easier to manage. You can get a backup
  93. # of the original in /usr/share/dovecot if you want.
  94. dovecot_v="$(dovecot --version | cut -d '.' -f1-2)"
  95. dovecot_dh="ssl_dh = </usr/share/dovecot/dh.pem"
  96. [ ${dovecot_v%.*} -eq 2 ] && [ ${dovecot_v#*.} -lt 3 ] || [ ${dovecot_v%.*} -lt 2 ] && dovecot_dh=""
  97. echo "Creating Dovecot config (v $dovecot_v)..."
  98. echo "# Dovecot config
  99. # Note that in the dovecot conf, you can use:
  100. # %u for username
  101. # %n for the name in name@domain.tld
  102. # %d for the domain
  103. # %h the user's home directory
  104. # If you're not a brainlet, SSL must be set to required.
  105. ssl = required
  106. ssl_cert = <$certdir/fullchain.pem
  107. ssl_key = <$certdir/privkey.pem
  108. ssl_min_protocol = TLSv1.2
  109. ssl_cipher_list = ALL:!RSA:!CAMELLIA:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS:!RC4:!SHA1:!SHA256:!SHA384:!LOW@STRENGTH
  110. ssl_prefer_server_ciphers = yes
  111. $dovecot_dh
  112. # Plaintext login. This is safe and easy thanks to SSL.
  113. auth_mechanisms = plain login
  114. auth_username_format = %n
  115. protocols = \$protocols imap
  116. # Search for valid users in /etc/passwd
  117. userdb {
  118. driver = passwd
  119. }
  120. #Fallback: Use plain old PAM to find user passwords
  121. passdb {
  122. driver = pam
  123. }
  124. # Our mail for each user will be in ~/Mail, and the inbox will be ~/Mail/Inbox
  125. # The LAYOUT option is also important because otherwise, the boxes will be \`.Sent\` instead of \`Sent\`.
  126. mail_location = maildir:~/Mail:INBOX=~/Mail/Inbox:LAYOUT=fs
  127. namespace inbox {
  128. inbox = yes
  129. mailbox Drafts {
  130. special_use = \\Drafts
  131. auto = subscribe
  132. }
  133. mailbox Junk {
  134. special_use = \\Junk
  135. auto = subscribe
  136. autoexpunge = 30d
  137. }
  138. mailbox Sent {
  139. special_use = \\Sent
  140. auto = subscribe
  141. }
  142. mailbox Trash {
  143. special_use = \\Trash
  144. }
  145. mailbox Archive {
  146. special_use = \\Archive
  147. }
  148. }
  149. # Here we let Postfix use Dovecot's authetication system.
  150. service auth {
  151. unix_listener /var/spool/postfix/private/auth {
  152. mode = 0660
  153. user = postfix
  154. group = postfix
  155. }
  156. }
  157. protocol lda {
  158. mail_plugins = \$mail_plugins sieve
  159. }
  160. protocol lmtp {
  161. mail_plugins = \$mail_plugins sieve
  162. }
  163. plugin {
  164. sieve = ~/.dovecot.sieve
  165. sieve_default = /var/lib/dovecot/sieve/default.sieve
  166. #sieve_global_path = /var/lib/dovecot/sieve/default.sieve
  167. sieve_dir = ~/.sieve
  168. sieve_global_dir = /var/lib/dovecot/sieve/
  169. }
  170. " > /etc/dovecot/dovecot.conf
  171. mkdir /var/lib/dovecot/sieve/
  172. echo "require [\"fileinto\", \"mailbox\"];
  173. if header :contains \"X-Spam-Flag\" \"YES\"
  174. {
  175. fileinto \"Junk\";
  176. }" > /var/lib/dovecot/sieve/default.sieve
  177. cut -d: -f1 /etc/passwd | grep -q "^vmail" || useradd vmail
  178. chown -R vmail:vmail /var/lib/dovecot
  179. sievec /var/lib/dovecot/sieve/default.sieve
  180. echo "Preparing user authetication..."
  181. grep -q nullok /etc/pam.d/dovecot ||
  182. echo "auth required pam_unix.so nullok
  183. account required pam_unix.so" >> /etc/pam.d/dovecot
  184. # OpenDKIM
  185. # A lot of the big name email services, like Google, will automatically
  186. # rejectmark as spam unfamiliar and unauthenticated email addresses. As in, the
  187. # server will flattly reject the email, not even deliverring it to someone's
  188. # Spam folder.
  189. # OpenDKIM is a way to authenticate your email so you can send to such services
  190. # without a problem.
  191. # TODO: add opendkim-tools ?
  192. # Create an OpenDKIM key in the proper place with proper permissions.
  193. echo "Generating OpenDKIM keys..."
  194. mkdir -p /etc/postfix/dkim
  195. opendkim-genkey -D /etc/postfix/dkim/ -d "$domain" -s "$subdom"
  196. chgrp opendkim /etc/postfix/dkim/*
  197. chmod g+r /etc/postfix/dkim/*
  198. # Generate the OpenDKIM info:
  199. echo "Configuring OpenDKIM..."
  200. grep -q "$domain" /etc/postfix/dkim/keytable 2>/dev/null ||
  201. echo "$subdom._domainkey.$domain $domain:$subdom:/etc/postfix/dkim/$subdom.private" >> /etc/postfix/dkim/keytable
  202. grep -q "$domain" /etc/postfix/dkim/signingtable 2>/dev/null ||
  203. echo "*@$domain $subdom._domainkey.$domain" >> /etc/postfix/dkim/signingtable
  204. grep -q "127.0.0.1" /etc/postfix/dkim/trustedhosts 2>/dev/null ||
  205. echo "127.0.0.1
  206. 10.1.0.0/16
  207. 1.2.3.4/24" >> /etc/postfix/dkim/trustedhosts
  208. # ...and source it from opendkim.conf
  209. grep -q "^KeyTable" /etc/opendkim.conf 2>/dev/null || echo "KeyTable file:/etc/postfix/dkim/keytable
  210. SigningTable refile:/etc/postfix/dkim/signingtable
  211. InternalHosts refile:/etc/postfix/dkim/trustedhosts" >> /etc/opendkim.conf
  212. sed -i '/^#Canonicalization/s/simple/relaxed\/simple/' /etc/opendkim.conf
  213. sed -i '/^#Canonicalization/s/^#//' /etc/opendkim.conf
  214. sed -e '/Socket/s/^#*/#/' -i /etc/opendkim.conf
  215. grep -q "^Socket\s*inet:12301@localhost" /etc/opendkim.conf || echo "Socket inet:12301@localhost" >> /etc/opendkim.conf
  216. # OpenDKIM daemon settings, removing previously activated socket.
  217. sed -i "/^SOCKET/d" /etc/default/opendkim && echo "SOCKET=\"inet:12301@localhost\"" >> /etc/default/opendkim
  218. # Here we add to postconf the needed settings for working with OpenDKIM
  219. echo "Configuring Postfix with OpenDKIM settings..."
  220. postconf -e "smtpd_sasl_security_options = noanonymous, noplaintext"
  221. postconf -e "smtpd_sasl_tls_security_options = noanonymous"
  222. postconf -e "myhostname = $maildomain"
  223. postconf -e "milter_default_action = accept"
  224. postconf -e "milter_protocol = 6"
  225. postconf -e "smtpd_milters = inet:localhost:12301"
  226. postconf -e "non_smtpd_milters = inet:localhost:12301"
  227. postconf -e "mailbox_command = /usr/lib/dovecot/deliver"
  228. for x in dovecot postfix opendkim spamassassin; do
  229. printf "Restarting %s..." "$x"
  230. service "$x" restart && printf " ...done\\n"
  231. done
  232. pval="$(tr -d "\n" </etc/postfix/dkim/$subdom.txt | sed "s/k=rsa.* \"p=/k=rsa; p=/;s/\"\s*\"//;s/\"\s*).*//" | grep -o "p=.*")"
  233. dkimentry="$subdom._domainkey.$domain TXT v=DKIM1; k=rsa; $pval"
  234. dmarcentry="_dmarc.$domain TXT v=DMARC1; p=none; rua=mailto:dmarc@$domain; fo=1"
  235. spfentry="@ TXT v=spf1 mx a:$maildomain -all"
  236. useradd -m -G mail dmarc
  237. echo "$dkimentry
  238. $dmarcentry
  239. $spfentry" > "$HOME/dns_emailwizard"
  240. echo "
  241. _ _
  242. | \ | | _____ ___
  243. | \| |/ _ \ \ /\ / (_)
  244. | |\ | (_) \ V V / _
  245. |_| \_|\___/ \_/\_/ (_)
  246. Add these three records to your DNS TXT records on either your registrar's site
  247. or your DNS server:
  248. $dkimentry
  249. $dmarcentry
  250. $spfentry
  251. NOTE: You may need to omit the \`.$domain\` portion at the beginning if
  252. inputting them in a registrar's web interface.
  253. Also saving these to ~/dns_emailwizard in case you want them in a file.
  254. Once you do that, you're done! Check the README for how to add users/accounts
  255. and how to log in."