public List <MsDnsZone> GetZones(bool getRecords) { // Select SOA records where the owner is that of domain. ObjectQuery query = new ObjectQuery("SELECT * FROM MicrosoftDNS_Zone"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(WmiScope, query); ManagementObjectCollection recordCollection = searcher.Get(); List <MsDnsZone> zoneList = new List <MsDnsZone>(); foreach (ManagementObject record in recordCollection) { MsDnsZone zone = new MsDnsZone( (string)record.Properties["Name"].Value, (ZoneType)(UInt32)record.Properties["ZoneType"].Value, string.Empty, string.Empty); if (getRecords) { zone.ARecords = GetARecords(zone); zone.CnameRecords = GetCnameRecords(zone); zone.MxRecords = GetMxRecords(zone); zone.NsRecords = GetNsRecords(zone); zone.ARecords = GetARecords(zone); } zoneList.Add(zone); } return(zoneList); }
/// <summary> /// Creates a new DNS zone and all associated record types. /// </summary> /// <param name="zone">DNS zone to create.</param> public void CreateZone(MsDnsZone zone) { ManagementPath path = new ManagementPath("MicrosoftDNS_Zone"); ManagementClass newZone = new ManagementClass(WmiScope, path, null); ManagementBaseObject inParams = newZone.GetMethodParameters("CreateZone"); inParams.Properties["ZoneName"].Value = zone.Name; inParams.Properties["ZoneType"].Value = (int)zone.Type; try { newZone.InvokeMethod("CreateZone", inParams, null); } catch (ManagementException ex) { throw new Exception("Could not create new DNS zone. " + "It is possible that this domain already exists.", ex); } // Defaults the SOA, and removes NS records. DefaultSoaRecord(zone); DeleteNsRecords(zone); // Now create the records for the zone. CreateRecords(zone.NsRecords); CreateRecords(zone.MxRecords); CreateRecords(zone.ARecords); CreateRecords(zone.CnameRecords); }
internal static MsDnsNsRecord Parse(ManagementObject record, MsDnsZone zone) { MsDnsNsRecord dnsRecord = new MsDnsNsRecord( (string)record.Properties["OwnerName"].Value, (string)record.Properties["RecordData"].Value, zone, (int)(UInt32)record.Properties["TTL"].Value); return(dnsRecord); }
/// <summary> /// Initialize a full DNS record with all properties set. /// </summary> /// <param name="name">First part of the "owner" property.</param> /// <param name="value">IP address, hostname, etc.</param> /// <param name="zone">Parent zone for the record.</param> /// <param name="ttl">Record time to live value.</param> public MsDnsRecord(string name, string value, MsDnsZone zone, int ttl) { if (!string.IsNullOrEmpty(name)) { // Convert container style name to small form name. name = name.Replace(zone.Name, null).Trim('.'); } this.Name = name; this.Value = value; this.Zone = zone; this.TTL = ttl; }
/// <summary> /// Initialize a new DNS SOA record with default values. /// </summary> public MsDnsSoaRecord( MsDnsZone zone, string primaryServer, string responsibleParty) : this( zone, primaryServer, responsibleParty, 1209600, // RFC ExpireLimit 86400, // RFC MinimumTTL 3600, // RFC RefreshInterval 600) { } // RFC RetryDelay
/// <summary> /// Deletes a DNS zone. /// </summary> /// <param name="zone">Zone to delete.</param> public void DeleteZone(MsDnsZone zone) { // Select NS records where the owner is that of domain. ObjectQuery query = new ObjectQuery("SELECT * FROM " + "MicrosoftDNS_Zone WHERE Name = '" + zone.Name + "'"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(WmiScope, query); ManagementObjectCollection foundZones = searcher.Get(); // Delete all NS records. foreach (ManagementObject foundZone in foundZones) { foundZone.Delete(); } }
/// <summary> /// Deletes all the NS records from a specified zone. /// </summary> /// <param name="zone">Zone to remove NS records from.</param> public void DeleteNsRecords(MsDnsZone zone) { // Select NS records where the owner is that of domain. ObjectQuery query = new ObjectQuery("SELECT * FROM " + "MicrosoftDNS_NSType WHERE OwnerName = '" + zone.Name + "'"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(WmiScope, query); ManagementObjectCollection records = searcher.Get(); // Forceably remove all ns records. foreach (ManagementObject record in records) { record.Delete(); } }
public List <MsDnsNsRecord> GetNsRecords(MsDnsZone zone) { ObjectQuery query = new ObjectQuery( "SELECT * FROM MicrosoftDNS_NSType WHERE ContainerName = '" + zone.Name + "'"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(WmiScope, query); ManagementObjectCollection recordCollection = searcher.Get(); List <MsDnsNsRecord> recordList = new List <MsDnsNsRecord>(); foreach (ManagementObject record in recordCollection) { recordList.Add(MsDnsNsRecord.Parse(record, zone)); } return(recordList); }
/// <summary> /// Initialize a new DNS SOA record. /// </summary> public MsDnsSoaRecord( MsDnsZone zone, string primaryServer, string responsibleParty, int expireLimit, int minimumTtl, int refreshInterval, int retryDelay) : base(null, null, zone, minimumTtl) { this.PrimaryServer = primaryServer; this.ResponsibleParty = responsibleParty; this.ExpireLimit = expireLimit; this.MinimumTTL = minimumTtl; this.RefreshInterval = refreshInterval; this.RetryDelay = retryDelay; }
public static MsDnsMxRecord Parse(ManagementObject record, MsDnsZone zone) { string data = (string)record.Properties["RecordData"].Value; string[] dataSplit = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int priority = int.Parse(dataSplit[0]); string value = dataSplit[1]; MsDnsMxRecord dnsRecord = new MsDnsMxRecord( (string)record.Properties["OwnerName"].Value, value, zone, (int)(UInt32)record.Properties["TTL"].Value, priority); return(dnsRecord); }
/// <summary> /// This will default the SOA record from a zone so that /// the TTL, refresh and expiry times comply with RFC standard /// as well as setting the responsible person and name servers. /// <param name="zone">Zone to be defaulted.</param> /// </summary> public void DefaultSoaRecord(MsDnsZone zone) { // Select SOA records where the owner is that of domain. ObjectQuery query = new ObjectQuery("SELECT * FROM " + "MicrosoftDNS_SOAType WHERE OwnerName = '" + zone.Name + "'"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(WmiScope, query); ManagementObjectCollection recordCollection = searcher.Get(); foreach (ManagementObject record in recordCollection) { ManagementBaseObject p = record.GetMethodParameters("Modify"); p.Properties["ExpireLimit"].Value = zone.SoaRecord.ExpireLimit; p.Properties["MinimumTTL"].Value = zone.SoaRecord.MinimumTTL; p.Properties["PrimaryServer"].Value = zone.SoaRecord.PrimaryServer; p.Properties["RefreshInterval"].Value = zone.SoaRecord.RefreshInterval; p.Properties["ResponsibleParty"].Value = zone.SoaRecord.ResponsibleParty; p.Properties["RetryDelay"].Value = zone.SoaRecord.RetryDelay; p.Properties["SerialNumber"].Value = zone.SoaRecord.SerialNumber; record.InvokeMethod("Modify", p, null); } }
public MsDnsNsRecord(string name, string value, MsDnsZone zone, int ttl) : base(name, value, zone, ttl) { }
public MsDnsMxRecord(string name, string value, MsDnsZone zone, int ttl, int priority) : base(name, value, zone, ttl) { this.Priority = priority; }