Skip to content

[client] Add reverse dns zone #3217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 21, 2025
Merged

[client] Add reverse dns zone #3217

merged 11 commits into from
Feb 21, 2025

Conversation

lixmal
Copy link
Collaborator

@lixmal lixmal commented Jan 19, 2025

Describe your changes

Issue ticket number and link

Closes #3214

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • Extended the README / documentation, if necessary

@mlsmaycon
Copy link
Collaborator

are we adding the PTR zone as search domain?

@lixmal
Copy link
Collaborator Author

lixmal commented Feb 14, 2025

are we adding the PTR zone as search domain?

I've excluded it but seems it's not marked by OS tools anyway

Copy link
Member

@hakansa hakansa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can split the logic to make it more readable

func addReverseZone(config *nbdns.Config, ipNet *net.IPNet) {
	zoneName, err := reverseZoneName(ipNet)
	if err != nil {
		log.Warn(err)
		return
	}

	if zoneExists(config, zoneName) {
		log.Debugf("reverse DNS zone %s already exists", zoneName)
		return
	}

	records := collectPTRRecords(config.CustomZones, ipNet)
	config.CustomZones = append(config.CustomZones, nbdns.CustomZone{
		Domain:  zoneName,
		Records: records,
	})

	log.Debugf("added reverse DNS zone: %s with %d records", zoneName, len(records))
}

// reverseZoneName computes the reverse DNS zone name
func reverseZoneName(ipNet *net.IPNet) (string, error) {
	networkIP := ipNet.IP.Mask(ipNet.Mask)
	maskOnes, _ := ipNet.Mask.Size()
	// round up to nearest byte
	octetsToUse := (maskOnes + 7) / 8

	octets := strings.Split(ip4.String(), ".")
	if octetsToUse > len(octets) {
		return "", fmt.Errorf("invalid network mask size for reverse DNS: %d", maskOnes)
	}

	// reverse the first octetsToUse octets.
	reversed := make([]string, octetsToUse)
	for i := 0; i < octetsToUse; i++ {
		reversed[octetsToUse-1-i] = octets[i]
	}

	zone := fmt.Sprintf("%s.in-addr.arpa", strings.Join(reversed, "."))
	return dns.Fqdn(zone), nil
}

// zoneExists returns true if the given zone name already exists in the configuration.
func zoneExists(config *nbdns.Config, zoneName string) bool {
	for _, zone := range config.CustomZones {
		if zone.Domain == zoneName {
			return true
		}
	}
	return false
}

// collectPTRRecords iterates over all A records in the existing zones,
// creating PTR records where applicable.
func collectPTRRecords(zones []nbdns.CustomZone, ipNet *net.IPNet) []nbdns.SimpleRecord {
	var records []nbdns.SimpleRecord
	for _, zone := range zones {
		for _, record := range zone.Records {
			if record.Type != int(dns.TypeA) {
				continue
			}
			if ptrRecord, ok := createPTRRecord(record, ipNet); ok {
				records = append(records, ptrRecord)
			}
		}
	}
	return records
}

Copy link

@lixmal lixmal merged commit 5134e3a into main Feb 21, 2025
33 of 34 checks passed
@lixmal lixmal deleted the rdns-zone branch February 21, 2025 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Question: Any possible way to get rDNS / PTR records for netbird clients?
3 participants