Last active
June 10, 2026 19:35
-
-
Save NQevxvEtg/a748ffee77a05f4a60dc4f0273ec5795 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # --- CONFIGURATION --- | |
| FORWARD_ZONES=("example.com" "internal.lan") | |
| # --------------------- | |
| if ! klist -s; then | |
| echo "β Error: No valid Kerberos ticket found. Please run 'kinit admin' first." | |
| exit 1 | |
| fi | |
| echo "π Starting FreeIPA PTR & Reverse Zone synchronization..." | |
| for ZONE in "${FORWARD_ZONES[@]}"; do | |
| echo "--------------------------------------------------" | |
| echo "π Processing forward zone: $ZONE" | |
| echo "--------------------------------------------------" | |
| ipa dnsrecord-find "$ZONE" --sizelimit=0 | awk ' | |
| /^ Record name:/ { host=$3 } | |
| /A record:/ { print host, $3 } | |
| ' | while read -r HOST IP; do | |
| if [[ -z "$HOST" || -z "$IP" || "$HOST" == "@" ]]; then | |
| continue | |
| fi | |
| # The backend payload MUST have the trailing dot | |
| FQDN_WITH_DOT="${HOST}.${ZONE}." | |
| # FreeIPA CLI outputs it WITHOUT a trailing dot, so we match against this | |
| FQDN_WITHOUT_DOT="${HOST}.${ZONE}" | |
| IFS='.' read -r o1 o2 o3 o4 <<< "$IP" | |
| REVERSE_ZONE="${o3}.${o2}.${o1}.in-addr.arpa" | |
| REVERSE_RECORD="$o4" | |
| echo "Checking: $FQDN_WITHOUT_DOT ($IP)" | |
| # Check if Reverse Zone exists | |
| ipa dnszone-show "$REVERSE_ZONE" > /dev/null 2>&1 | |
| if [ $? -ne 0 ]; then | |
| echo " π Reverse zone '$REVERSE_ZONE' is missing. Creating it..." | |
| ipa dnszone-add "$REVERSE_ZONE" > /dev/null 2>&1 | |
| if [ $? -eq 0 ]; then | |
| echo " β Successfully created reverse zone: $REVERSE_ZONE" | |
| else | |
| echo " β Failed to create reverse zone: $REVERSE_ZONE. Skipping." | |
| continue | |
| fi | |
| fi | |
| # Fetch what FreeIPA claims is registered (it will strip the dot visually) | |
| CURRENT_PTR=$(ipa dnsrecord-find "$REVERSE_ZONE" --name="$REVERSE_RECORD" 2>/dev/null | awk -F': ' '/PTR record/ {print $2}' | xargs) | |
| # Compare against the DOTLESS version since FreeIPA hides the dot on output | |
| if [ "$CURRENT_PTR" == "$FQDN_WITHOUT_DOT" ]; then | |
| echo " β PTR record is properly configured in FreeIPA backend. Skipping." | |
| else | |
| if [ -n "$CURRENT_PTR" ]; then | |
| echo " β οΈ Mismatched PTR record found ('$CURRENT_PTR'). Cleaning up..." | |
| ipa dnsrecord-del "$REVERSE_ZONE" "$REVERSE_RECORD" --ptr-rec="$CURRENT_PTR" > /dev/null 2>&1 | |
| fi | |
| echo " β Adding canonical PTR record: $FQDN_WITH_DOT" | |
| # Explicitly force the trailing dot into the API | |
| ipa dnsrecord-add "$REVERSE_ZONE" "$REVERSE_RECORD" --ptr-rec="${FQDN_WITH_DOT}" > /dev/null 2>&1 | |
| if [ $? -eq 0 ]; then | |
| echo " β Successfully pushed updated PTR for $IP" | |
| else | |
| echo " β Unexpected error updating PTR for $IP." | |
| fi | |
| fi | |
| done | |
| done | |
| echo "--------------------------------------------------" | |
| echo "π Synchronization complete!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment