選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

emailwiz.sh 13 KiB

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