Example #1
0
func exportReverse(db *database.DB, domain *database.Domain) (string, error) {
	_, net, err := net.ParseCIDR(domain.Name)
	if err != nil {
		panic("Export reverse on a non-CIDR")
	}
	if ones, _ := net.Mask.Size(); ones%8 != 0 {
		return "", fmt.Errorf("Reverse zone CIDR must be 8-bit aligned, cannot generate zone for %s", net)
	}

	ret := []string{
		fmt.Sprintf("$ORIGIN %s", arpaZone(net)),
		"$TTL 600",
		domain.SOA(),
		"",
	}

	for _, host := range db.Hosts {
		for _, addr := range sortedAddrs(host) {
			if !net.Contains(addr) {
				continue
			}

			if fqdn := host.Attrs["fqdn"]; fqdn != "" {
				ret = append(ret, fmt.Sprintf("%s IN PTR %s.", arpaHost(net, addr), fqdn))
			} else if hostname := host.Attrs["hostname"]; hostname != "" {
				if domain := ipDomain(host, addr); domain != "" {
					ret = append(ret, fmt.Sprintf("%s IN PTR %s.%s.", arpaHost(net, addr), hostname, domain))
				}
			}
		}
	}

	return strings.Join(ret, "\n"), nil
}
Example #2
0
func exportDirect(db *database.DB, domain *database.Domain) (string, error) {
	suffix := "." + domain.Name

	ret := []string{
		fmt.Sprintf("$ORIGIN %s.", domain.Name),
		"$TTL 600",
		domain.SOA(),
		"",
	}

	for _, ns := range domain.NS {
		ret = append(ret, fmt.Sprintf("@ IN NS %s.", ns))
	}
	if len(domain.NS) > 0 {
		ret = append(ret, "")
	}
	for _, rr := range domain.RR {
		ret = append(ret, rr)
	}
	if len(domain.RR) > 0 {
		ret = append(ret, "")
	}

	for _, host := range db.Hosts {
		var hostname string
		fqdn := host.Attrs["fqdn"]
		if fqdn != "" {
			if !strings.HasSuffix(fqdn, suffix) {
				continue
			}
			hostname = strings.TrimSuffix(fqdn, suffix)
			for _, addr := range sortedAddrs(host) {
				ret = append(ret, fmt.Sprintf("%s IN %s %s", hostname, rrtype(addr), addr))
			}
		} else {
			hostname = host.Attrs["hostname"]
			if hostname == "" {
				continue
			}
			for _, addr := range sortedAddrs(host) {
				addrDomain := ipDomain(host, addr)
				if addrDomain == "" || addrDomain != domain.Name {
					continue
				}
				ret = append(ret, fmt.Sprintf("%s IN %s %s", hostname, rrtype(addr), addr))
			}
		}

		if cnames := host.Attrs["cname"]; cnames != "" {
			for _, cname := range strings.Split(cnames, ",") {
				ret = append(ret, fmt.Sprintf("%s IN CNAME %s", cname, hostname))
			}
		}
	}

	for _, subnet := range db.Subnets {
		ret = append(ret, walkDirect(db, subnet, domain)...)
	}

	return strings.Join(ret, "\n"), nil
}