No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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