From 8f03827990748942a97418cbbd2c47769109546c Mon Sep 17 00:00:00 2001 From: xarraxyl <110544951+xarraxyl@users.noreply.github.com> Date: Fri, 30 Aug 2024 18:16:26 +0200 Subject: [PATCH 1/3] Add `maildomain` to adddomain.sh --- adddomain.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/adddomain.sh b/adddomain.sh index d44b567..d456801 100755 --- a/adddomain.sh +++ b/adddomain.sh @@ -5,6 +5,7 @@ domain="$1" domain="$1" subdom="mail" +maildomain="$subdom.$domain" # Add the domain to the valid postfix addresses. grep -q "^mydestination.*$domain" /etc/postfix/main.cf || From 2b690810acd80eae9d4a0806ee66ed11d4d9abd6 Mon Sep 17 00:00:00 2001 From: xarraxyl <110544951+xarraxyl@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:02:37 +0200 Subject: [PATCH 2/3] Update adddomain.sh Two Parameters: The script now accepts two arguments: $1: The new domain you are adding. $2: The existing mail service domain. Variable Assignment: - new_domain="$1" assigns the first parameter to the new domain being added. - mail_service_domain="$2" assigns the second parameter to the existing mail service domain. - maildomain="$subdom.$mail_service_domain" constructs the mail domain using the existing mail service domain. Error Checking: - The script now checks that both parameters are provided before proceeding, ensuring the script is called correctly. DKIM, DMARC, SPF, and MX Records: - The DNS records generated now correctly reflect the new domain and the existing mail service domain. --- adddomain.sh | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/adddomain.sh b/adddomain.sh index d456801..d398f3b 100755 --- a/adddomain.sh +++ b/adddomain.sh @@ -1,35 +1,38 @@ #!/bin/sh -domain="$1" -[ -z "$1" ] && exit +new_domain="$1" +mail_service_domain="$2" + +# Check if both domains are provided +[ -z "$new_domain" ] || [ -z "$mail_service_domain" ] && { echo "Usage: $0 new_domain mail_service_domain"; exit 1; } -domain="$1" subdom="mail" -maildomain="$subdom.$domain" +maildomain="$subdom.$mail_service_domain" -# Add the domain to the valid postfix addresses. -grep -q "^mydestination.*$domain" /etc/postfix/main.cf || - sed -i "s/^mydestination.*/&, $domain/" /etc/postfix/main.cf +# Add the new domain to the valid postfix addresses. +if ! grep -q "^mydestination.*$new_domain" /etc/postfix/main.cf; then + sed -i "s/^mydestination.*/&, $new_domain/" /etc/postfix/main.cf +fi -# Create DKIM for new domain. -mkdir -p "/etc/postfix/dkim/$domain" -opendkim-genkey -D "/etc/postfix/dkim/$domain" -d "$domain" -s "$subdom" +# Create DKIM for the new domain. +mkdir -p "/etc/postfix/dkim/$new_domain" +opendkim-genkey -D "/etc/postfix/dkim/$new_domain" -d "$new_domain" -s "$subdom" chgrp -R opendkim /etc/postfix/dkim/* chmod -R g+r /etc/postfix/dkim/* # Add entries to keytable and signing table. -echo "$subdom._domainkey.$domain $domain:$subdom:/etc/postfix/dkim/$domain/$subdom.private" >> /etc/postfix/dkim/keytable -echo "*@$domain $subdom._domainkey.$domain" >> /etc/postfix/dkim/signingtable +echo "$subdom._domainkey.$new_domain $new_domain:$subdom:/etc/postfix/dkim/$new_domain/$subdom.private" >> /etc/postfix/dkim/keytable +echo "*@$new_domain $subdom._domainkey.$new_domain" >> /etc/postfix/dkim/signingtable systemctl reload opendkim postfix # Print out DKIM TXT entry. -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=.*')" +pval="$(tr -d '\n' <"/etc/postfix/dkim/$new_domain/$subdom.txt" | sed "s/k=rsa.*p=/k=rsa; p=/;s/\"\s*\"//g;s/\"\s*.*//g" | grep -o 'p=.*')" -dkimentry="$subdom._domainkey.$domain TXT v=DKIM1; k=rsa; $pval" -dmarcentry="_dmarc.$domain TXT v=DMARC1; p=reject; rua=mailto:dmarc@$domain; fo=1" -spfentry="$domain TXT v=spf1 mx a:$maildomain -all" -mxentry="$domain MX 10 $maildomain 300" +dkimentry="$subdom._domainkey.$new_domain TXT v=DKIM1; k=rsa; $pval" +dmarcentry="_dmarc.$new_domain TXT v=DMARC1; p=reject; rua=mailto:dmarc@$new_domain; fo=1" +spfentry="$new_domain TXT v=spf1 mx a:$maildomain -all" +mxentry="$new_domain MX 10 $maildomain" echo "$dkimentry $dmarcentry @@ -41,4 +44,4 @@ echo "$dkimentry $dmarcentry $spfentry $mxentry" -echo "They have also been stored in ~/dns_emailwizard_added" +echo "They have also been stored in $HOME/dns_emailwizard_added" From 51b07402a5717b7055adb557fcc4aa0ca5f61387 Mon Sep 17 00:00:00 2001 From: xarraxyl <110544951+xarraxyl@users.noreply.github.com> Date: Fri, 30 Aug 2024 22:17:24 +0200 Subject: [PATCH 3/3] Restore the original sed command --- adddomain.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adddomain.sh b/adddomain.sh index d398f3b..2138d6c 100755 --- a/adddomain.sh +++ b/adddomain.sh @@ -27,7 +27,7 @@ echo "*@$new_domain $subdom._domainkey.$new_domain" >> /etc/postfix/dkim/signing systemctl reload opendkim postfix # Print out DKIM TXT entry. -pval="$(tr -d '\n' <"/etc/postfix/dkim/$new_domain/$subdom.txt" | sed "s/k=rsa.*p=/k=rsa; p=/;s/\"\s*\"//g;s/\"\s*.*//g" | grep -o 'p=.*')" +pval="$(tr -d '\n' <"/etc/postfix/dkim/$new_domain/$subdom.txt" | sed sed "s/k=rsa.* \"p=/k=rsa; p=/;s/\"\s*\"//;s/\"\s*).*//" | grep -o 'p=.*')" dkimentry="$subdom._domainkey.$new_domain TXT v=DKIM1; k=rsa; $pval" dmarcentry="_dmarc.$new_domain TXT v=DMARC1; p=reject; rua=mailto:dmarc@$new_domain; fo=1"