[ Team LiB ] Previous Section Next Section

27.4 Creating and Manipulating Resource Records

Resource records are the basic unit of information in DNS. A DNS server's primary job is to respond to queries for resource records. Most people don't realize they are generating resource record queries with nearly every network-based operation they do, including accessing a website, pinging a host, or logging into Active Directory.

Resource records come in many different flavors or types. Each type corresponds to a certain type of name or address lookup. Each record type also has additional information encoded with the record that represents things such as the time to live of the record. The following is a textual example of what a CNAME record looks like:

www.mycorp.com.  1800  IN  CNAME  www1.mycorp.com.

Or more generically:

Owner  TTL  Class  Type  RR-Data

Now let's break the record down into its individual parts:

Owner

The owner of the resource record. This field is typically what is specified during a query for the particular type.

TTL

The time to live, or length of time a nonauthoritative DNS server should cache the record. After the TTL expires, a nonauthoritative server should re-query for a authoritative answer.

Class

Resource record classification. In nearly all cases, this will be "IN" for Internet.

Type

Name of the resource record type. Each type has a standard name that is used in zones (e.g., CNAME, A, PTR, SRV).

RR-Data

Resource record specific data. When you perform a query, you are typically looking for the information returned as part of the RR-Data.

The WMI DNS provider fully supports querying and manipulating resource records. In Table 27-5 and Table 27-6, the supported properties and methods are listed for the MicrosoftDNS_ResourceRecord class, which implements a generic interface for resource records.

Table 27-5. MicrosoftDNS_ResourceRecord class properties

Property name

Property description

ContainerName

Name of container (e.g., zone name) that holds the RR

DomainName

FQDN of the domain that contains the RR

DnsServerName

FQDN of the server that contains the RR

OwnerName

Owner of the RR

RecordClass

Class of the RR; 1 represents IN

RecordData

Resource record data

TextRepresentation

Textual representation of the RR, e.g.: www.mycorp.com. 1800 IN CNAME www1.mycorp.com.

Timestamp

Time RR was last refreshed

TTL

Time to live or maximum time a DNS server is supposed to cache the RR

Table 27-6. MicrosoftDNS_ResourceRecord class methods

Method name

Method description

CreateInstanceFromTextRepresentation

Creates a new instance of a MicrosoftDNS_ResourceRecord subclass based on the textual representation of the resource record, server name, and container or zone name. A reference to the new object is returned as an out parameter.

GetObjectByTextRepresentation

Gets an instance of the appropriate MicrosoftDNS_ResourceRecord subclass as specified by the textual representation of the resource record, server name, and container or zone name.

The MicrosoftDNS_ResourceRecord class by itself is not enough. There are over two dozen different types of resource records with many having additional fields that would not have corresponding methods in the generic interface. To solve this problem, subclasses of MicrosoftDNS_ResourceRecord were created for each supported record type. Each subclass provides specific methods to access any field supported by the resource record type. Each supported resource record has a subclass with a name in the format of MicrosoftDNS_<RR Type>Type.

To show just how different resource records can be, let's take a look at an A record:

www.mycorp.com.  1800  IN  A  192.10.4.5

Now let's compare that with an SRV record:

_ldap._tcp.dc._msdcs.mycorp.com 1800  IN  SRV  0 100 389 dc1.mycorp.com.

As you can see, the SRV record has several additional fields. By using the MicrosoftDNS_SRVType subclass, we can access each field with methods provided by the class.

The complete list of supported resource record types is provided in Table 27-7.

Table 27-7. DNS provider supported resource records

Resource record type

DNS provider class

RFC

Description

A

MicrosoftDNS_Atype

RFC1035

Name-to-IPv4 address mapping

AAAA

MicrosoftDNS_AAAAType

RFC1886

Name-to-IPv6 address mapping

AFSDB

MicrosoftDNS_AFSDBType

RFC1183

Andrew File System (AFS) Database Server record

ATMA

MicrosoftDNS_ATMAType

N/A

ATM-address-to-name mapping

CNAME

MicrosoftDNS_CNAMEType

RFC1035

Canonical (alias) name

HINFO

MicrosoftDNS_HINFOType

RFC1035

Host information

ISDN

MicrosoftDNS_ISDNType

RFC1183

Integrated services digital network (ISDN) record

KEY

MicrosoftDNS_KEYType

RFC2535

KEY record

MB

MicrosoftDNS_MBType

RFC1035

Mailbox record

MD

MicrosoftDNS_MDType

RFC1035

Mail agent

MF

MicrosoftDNS_MFType

RFC1035

Mail forwarding agent

MG

MicrosoftDNS_MGType

RFC1035

Mail group record

MINFO

MicrosoftDNS_MINFOType

RFC1035

Mail information record

MR

MicrosoftDNS_MRType

RFC1035

Mailbox rename record

MX

MicrosoftDNS_MXType

RFC1035

Mail exchanger

NS

MicrosoftDNS_NSType

RFC1035

Name server

NXT

MicrosoftDNS_NXTType

RFC2535

Next record

PTR

MicrosoftDNS_PTRType

RFC1035

Address-to-name mapping record

RP

MicrosoftDNS_RPTType

RFC1183

Responsible person

RT

MicrosoftDNS_RTType

RFC1183

Route through record

SIG

MicrosoftDNS_SIGType

RFC2535

Signature record

SOA

MicrosoftDNS_SOAType

RFC1035

Start of authority

SRV

MicrosoftDNS_SRVType

RFC2052

Service record

TXT

MicrosoftDNS_TXTType

RFC1035

Text record

WINS

MicrosoftDNS_WINSType

N/A

WINS server

WINSR

MicrosoftDNS_WINSRType

N/A

WINS reverse-lookup

WKS

MicrosoftDNS_WKSType

RFC1035

Well-known services

X25

MicrosoftDNS_X25Type

RFC1183

X.121 Address-to-name mapping

27.4.1 Finding Resource Records in a Zone

With the marriage of DNS and WMI, querying DNS has never been so easy. By using WQL, you can write complex query routines that would not have been possible previously. To list all of the resource records on a server, you simply need to execute the WQL query select * from MicrosoftDNS_ResourceRecord against the target server. The following example shows what this would look like if the script is run on a DNS server:

Set objDNS = GetObject("winMgmts:root\MicrosoftDNS")
set objRR = objDNS.ExecQuery("Select * from MicrosoftDNS_ResourceRecord ")
   
For Each objInst in objRR
   WScript.Echo objInst.TextRepresentation
Next

The TextRepresentation method is available to all resource record types since it is defined in MicrosoftDNS_ResourceRecord. It will return a text string representing the resource record, such as the following:

www.mycorp.com. IN  A  192.10.4.5

If you want to limit the query to only a specific zone, change the WQL query to include criteria for ContainerName, such as the following:

Select * from MicrosoftDNS_ResourceRecord 

Where ContainerName = 'ZoneName'

Since Active Directory uses DNS to store all of the Global Catalog servers in a forest and domain controllers in a domain, you can write scripts to utilize DNS to access this information and integrate it into your applications. The following example does exactly this by selecting all SRV records with a particular OwnerName. To find all Global Catalog servers in a forest, you can simply query _ldap._tcp.gc._msdcs.<ForestDNSName>, and to find all domain controllers in a domain, query _ldap._tcp.dc._msdcs.<DomainDNSName>.

option explicit
   
Dim strDomain
strDomain = "mycorp.com"
   
Dim objDNS, objRRs, objRR
Set objDNS = GetObject("winMgmts:root\MicrosoftDNS")
set objRRs = objDNS.ExecQuery("Select * from MicrosoftDNS_SRVType " & _
                             " Where OwnerName = '_ldap._tcp.gc._msdcs." & _ 
                             strDomain & "'")
WScript.Echo "Global Catalogs for " & strDomain
For Each objRR in objRRs
   Wscript.Echo " " & objRR.DomainName
Next
   
Wscript.Echo
   
set objRRs = objDNS.ExecQuery("Select * from MicrosoftDNS_SRVType " & _
                             " Where OwnerName = '_ldap._tcp.dc._msdcs." & _
                             strDomain & "'")  
WScript.Echo "Domain Controllers for " & strDomain
For Each objRR in objRRs
   Wscript.Echo " " & objRR.DomainName
Next

27.4.2 Creating Resource Records

With the DNS provider, creating resource records is also very easy to do. The MicrosoftDNS_ResourceRecord::CreateInstanceFromTextRepresentation method takes the server name to create the record on, the domain name, and the text representation of the resource record as in parameters. It also provides an out parameter which will be an object representing the newly created record.

Example 27-3 goes through the process of creating both A and PTR records. Both records are typically necessary when adding a new host to DNS.

Example 27-3. Creating A and PTR resource records
option explicit
   
Dim strRR, strReverseRR, strDomain, strReverseDomain
   
' A record to add
strRR = "testb.mycorp.com. IN A 192.32.64.13"
strDomain = "mycorp.com"
   
' PTR record to add
strReverseRR = "13.64.32.192.in-addr.arpa IN PTR testb.mycorp.com"
strReverseDomain = "192.in-addr.arpa."
   
Dim objDNS, objRR, objDNSServer, objRR2, objOutParam
Set objDNS = GetObject("winMgmts:root\MicrosoftDNS")
Set objRR = objDNS.Get("MicrosoftDNS_ResourceRecord")
Set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""")
   
' Create the A record
Dim strNull
strNull = objRR.CreateInstanceFromTextRepresentation( _
                  objDNSServer.Name, _
                  strDomain, _
                  strRR, _
                  objOutParam)       
   
Set objRR2 = objDNS.Get(objOutParam)
WScript.Echo "Created Record: " & objRR2.TextRepresentation
Set objOutParam = Nothing
   
' Create the PTR record
strNull = objRR.CreateInstanceFromTextRepresentation( _
                  objDNSServer.Name, _
                  strReverseDomain, _
                  strReverseRR, _
                  objOutParam)       
   
Set objRR2 = objDNS.Get(objOutParam)
WScript.Echo "Created Record: " & objRR2.TextRepresentation
    [ Team LiB ] Previous Section Next Section