diff options
| author | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2025-03-29 21:43:26 +0900 |
|---|---|---|
| committer | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2025-03-29 21:43:26 +0900 |
| commit | ecd7d0f73cfdd2a1d5edb0d337e91f207215c62a (patch) | |
| tree | c999859a9955776281e2619946ddcc7953a835ff | |
| parent | ed047ac633d956754131e6f70733b5a8a91a9464 (diff) | |
modified add-domain.sh
| -rwxr-xr-x | add-domain.sh | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/add-domain.sh b/add-domain.sh index e50c7b7..eac88e2 100755 --- a/add-domain.sh +++ b/add-domain.sh @@ -1,28 +1,33 @@ #!/bin/sh domain="$1" -[ -z "$1" ] && exit -domain="$1" +# Input validation to allow only valid domain characters +if ! [[ "$domain" =~ ^[a-zA-Z0-9.-]+$ ]]; then + echo "Give a valid domain as an argument to add mail server for it. Only alphanumeric characters, dashes, and dots are allowed." + exit 1 +fi + subdom="mail" +maildomain="mail.$(cat /etc/mailname)" -# Add the domain to the valid postfix addresses. +# 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 -# Create DKIM for new domain. +# Create DKIM for the new domain. mkdir -p "/etc/postfix/dkim/$domain" opendkim-genkey -D "/etc/postfix/dkim/$domain" -d "$domain" -s "$subdom" chgrp -R opendkim /etc/postfix/dkim/* chmod -R g+r /etc/postfix/dkim/* -# Add entries to keytable and signing table. +# 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 systemctl reload opendkim postfix -# Print out DKIM TXT entry. +# 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=.*')" dkimentry="$subdom._domainkey.$domain TXT v=DKIM1; k=rsa; $pval" |
