def data_to_dns_record(record_type, data): if record_type == dnsp.DNS_TYPE_A: rec = ARecord(data) elif record_type == dnsp.DNS_TYPE_AAAA: rec = AAAARecord(data) elif record_type == dnsp.DNS_TYPE_PTR: rec = PTRRecord(data) elif record_type == dnsp.DNS_TYPE_CNAME: rec = CNameRecord(data) elif record_type == dnsp.DNS_TYPE_NS: rec = NSRecord(data) elif record_type == dnsp.DNS_TYPE_MX: tmp = data.split(' ') if len(tmp) != 2: raise CommandError( 'Data requires 2 elements - mail_server, preference') mail_server = tmp[0] preference = int(tmp[1]) rec = MXRecord(mail_server, preference) elif record_type == dnsp.DNS_TYPE_SRV: tmp = data.split(' ') if len(tmp) != 4: raise CommandError( 'Data requires 4 elements - server, port, priority, weight') server = tmp[0] port = int(tmp[1]) priority = int(tmp[2]) weight = int(tmp[3]) rec = SRVRecord(server, port, priority=priority, weight=weight) elif record_type == dnsp.DNS_TYPE_SOA: tmp = data.split(' ') if len(tmp) != 7: raise CommandError( 'Data requires 7 elements - nameserver, email, serial, ' 'refresh, retry, expire, minimumttl') nameserver = tmp[0] email = tmp[1] serial = int(tmp[2]) refresh = int(tmp[3]) retry = int(tmp[4]) expire = int(tmp[5]) minimum = int(tmp[6]) rec = SOARecord(nameserver, email, serial=serial, refresh=refresh, retry=retry, expire=expire, minimum=minimum) elif record_type == dnsp.DNS_TYPE_TXT: slist = shlex.split(data) rec = TXTRecord(slist) else: raise CommandError('Unsupported record type') return rec
def add_dns_records(samdb, name, dns_conn, change_owner_sd, server, ip_address_list, logger): """Add DNS A or AAAA records while creating computer. """ name = name.rstrip('$') client_version = dnsserver.DNS_CLIENT_VERSION_LONGHORN select_flags = dnsserver.DNS_RPC_VIEW_AUTHORITY_DATA | dnsserver.DNS_RPC_VIEW_NO_CHILDREN zone = samdb.domain_dns_name() name_found = True sd_helper = sd_utils.SDUtils(samdb) try: buflen, res = dns_conn.DnssrvEnumRecords2( client_version, 0, server, zone, name, None, dnsp.DNS_TYPE_ALL, select_flags, None, None, ) except WERRORError as e: if e.args[0] == werror.WERR_DNS_ERROR_NAME_DOES_NOT_EXIST: name_found = False pass if name_found: for rec in res.rec: for record in rec.records: if record.wType == dnsp.DNS_TYPE_A or record.wType == dnsp.DNS_TYPE_AAAA: # delete record del_rec_buf = dnsserver.DNS_RPC_RECORD_BUF() del_rec_buf.rec = record try: dns_conn.DnssrvUpdateRecord2( client_version, 0, server, zone, name, None, del_rec_buf, ) except WERRORError as e: if e.args[ 0] != werror.WERR_DNS_ERROR_NAME_DOES_NOT_EXIST: raise for ip_address in ip_address_list: if _is_valid_ipv6(ip_address): logger.info("Adding DNS AAAA record %s.%s for IPv6 IP: %s" % (name, zone, ip_address)) rec = AAAARecord(ip_address) elif _is_valid_ipv4(ip_address): logger.info("Adding DNS A record %s.%s for IPv4 IP: %s" % (name, zone, ip_address)) rec = ARecord(ip_address) else: raise ValueError('Invalid IP: {}'.format(ip_address)) # Add record add_rec_buf = dnsserver.DNS_RPC_RECORD_BUF() add_rec_buf.rec = rec dns_conn.DnssrvUpdateRecord2( client_version, 0, server, zone, name, add_rec_buf, None, ) if (len(ip_address_list) > 0): domaindns_zone_dn = ldb.Dn( samdb, 'DC=DomainDnsZones,%s' % samdb.get_default_basedn(), ) dns_a_dn, ldap_record = samdb.dns_lookup( "%s.%s" % (name, zone), dns_partition=domaindns_zone_dn, ) # Make the DC own the DNS record, not the administrator sd_helper.modify_sd_on_dn( dns_a_dn, change_owner_sd, controls=[ "sd_flags:1:%d" % (security.SECINFO_OWNER | security.SECINFO_GROUP) ], )