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.

52 lines
1.7 KiB

  1. #!/bin/sh
  2. domain="$1"
  3. # Input validation to allow only valid domain characters
  4. case "$domain" in
  5. ""|*[!a-zA-Z0-9.-]*)
  6. echo "Give a valid domain as an argument to add mail server for it. Only alphanumeric characters, dashes, and dots are allowed."
  7. exit 1
  8. ;;
  9. esac
  10. subdom="mail"
  11. maildomain="mail.$(cat /etc/mailname)"
  12. # Add the domain to the valid postfix addresses
  13. grep -q "^mydestination.*$domain" /etc/postfix/main.cf ||
  14. sed -i "s/^mydestination.*/&, $domain/" /etc/postfix/main.cf
  15. # Create DKIM for the new domain
  16. mkdir -p "/etc/postfix/dkim/$domain"
  17. opendkim-genkey -D "/etc/postfix/dkim/$domain" -d "$domain" -s "$subdom"
  18. chgrp -R opendkim /etc/postfix/dkim/*
  19. chmod -R g+r /etc/postfix/dkim/*
  20. # Add entries to keytable and signing table
  21. echo "$subdom._domainkey.$domain $domain:$subdom:/etc/postfix/dkim/$domain/$subdom.private" >> /etc/postfix/dkim/keytable
  22. echo "*@$domain $subdom._domainkey.$domain" >> /etc/postfix/dkim/signingtable
  23. echo "/^(.*)@$(echo "$domain" | sed 's/\./\\./')\$/ \${1}" >> /etc/postfix/login_maps.pcre
  24. systemctl reload opendkim postfix
  25. # Print out DKIM TXT entry
  26. 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=.*')"
  27. dkimentry="$subdom._domainkey.$domain TXT v=DKIM1; k=rsa; $pval"
  28. dmarcentry="_dmarc.$domain TXT v=DMARC1; p=reject; rua=mailto:dmarc@$domain; fo=1"
  29. spfentry="$domain TXT v=spf1 mx a:$maildomain -all"
  30. mxentry="$domain MX 10 $maildomain 300"
  31. echo "$dkimentry
  32. $dmarcentry
  33. $spfentry
  34. $mxentry" >> "$HOME/dns_emailwizard_added"
  35. echo "=== ADD THE FOLLOWING TO YOUR DNS TXT RECORDS ==="
  36. echo "$dkimentry
  37. $dmarcentry
  38. $spfentry
  39. $mxentry"
  40. echo "They have also been stored in ~/dns_emailwizard_added"