Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

216 linhas
7.9 KiB

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