예제 #1
0
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                foreach (AuthZone zone in _root)
                {
                    AuthZoneInfo zoneInfo = new AuthZoneInfo(zone);
                    switch (zoneInfo.Type)
                    {
                    case AuthZoneType.Primary:
                    case AuthZoneType.Secondary:
                    case AuthZoneType.Stub:
                    case AuthZoneType.Forwarder:
                        zone.Dispose();
                        break;
                    }
                }
            }

            _disposed = true;
        }
예제 #2
0
        private AuthZone CreateEmptyZone(AuthZoneInfo zoneInfo)
        {
            AuthZone zone;

            switch (zoneInfo.Type)
            {
            case AuthZoneType.Primary:
                zone = new PrimaryZone(_dnsServer, zoneInfo);
                break;

            case AuthZoneType.Secondary:
                zone = new SecondaryZone(_dnsServer, zoneInfo);
                break;

            case AuthZoneType.Stub:
                zone = new StubZone(_dnsServer, zoneInfo);
                break;

            case AuthZoneType.Forwarder:
                zone = new ForwarderZone(zoneInfo);
                break;

            default:
                throw new InvalidDataException("DNS zone type not supported.");
            }

            if (_root.TryAdd(zone))
            {
                return(zone);
            }

            throw new DnsServerException("Zone already exists: " + zoneInfo.Name);
        }
예제 #3
0
        public void WriteZoneTo(string domain, Stream s)
        {
            List <AuthZone> zones = _root.GetZoneWithSubDomainZones(domain);

            if (zones.Count == 0)
            {
                throw new DnsServerException("Zone was not found: " + domain);
            }

            //serialize zone
            BinaryWriter bW = new BinaryWriter(s);

            bW.Write(Encoding.ASCII.GetBytes("DZ")); //format
            bW.Write((byte)4);                       //version

            //write zone info
            AuthZoneInfo zoneInfo = new AuthZoneInfo(zones[0]);

            if (zoneInfo.Internal)
            {
                throw new InvalidOperationException("Cannot save zones marked as internal.");
            }

            zoneInfo.WriteTo(bW);

            //write all zone records
            List <DnsResourceRecord> records = new List <DnsResourceRecord>();

            foreach (AuthZone zone in zones)
            {
                records.AddRange(zone.ListAllRecords());
            }

            bW.Write(records.Count);

            foreach (DnsResourceRecord record in records)
            {
                record.WriteTo(s);

                DnsResourceRecordInfo rrInfo = record.Tag as DnsResourceRecordInfo;
                if (rrInfo == null)
                {
                    rrInfo = new DnsResourceRecordInfo(); //default info
                }
                rrInfo.WriteTo(bW);
            }
        }
예제 #4
0
        public List <AuthZoneInfo> ListZones()
        {
            List <AuthZoneInfo> zones = new List <AuthZoneInfo>();

            foreach (AuthZone zone in _root)
            {
                AuthZoneInfo zoneInfo = new AuthZoneInfo(zone);
                switch (zoneInfo.Type)
                {
                case AuthZoneType.Primary:
                case AuthZoneType.Secondary:
                case AuthZoneType.Stub:
                case AuthZoneType.Forwarder:
                    zones.Add(zoneInfo);
                    break;
                }
            }

            return(zones);
        }
예제 #5
0
        public void LoadZoneFrom(Stream s)
        {
            BinaryReader bR = new BinaryReader(s);

            if (Encoding.ASCII.GetString(bR.ReadBytes(2)) != "DZ")
            {
                throw new InvalidDataException("DnsServer zone file format is invalid.");
            }

            switch (bR.ReadByte())
            {
            case 2:
            {
                DnsResourceRecord[] records = new DnsResourceRecord[bR.ReadInt32()];
                if (records.Length > 0)
                {
                    DnsResourceRecord soaRecord = null;

                    for (int i = 0; i < records.Length; i++)
                    {
                        records[i] = new DnsResourceRecord(s);

                        if (records[i].Type == DnsResourceRecordType.SOA)
                        {
                            soaRecord = records[i];
                        }
                    }

                    if (soaRecord == null)
                    {
                        throw new InvalidDataException("Zone does not contain SOA record.");
                    }

                    //make zone info
                    AuthZoneType zoneType;
                    if (_dnsServer.ServerDomain.Equals((soaRecord.RDATA as DnsSOARecord).PrimaryNameServer, StringComparison.OrdinalIgnoreCase))
                    {
                        zoneType = AuthZoneType.Primary;
                    }
                    else
                    {
                        zoneType = AuthZoneType.Stub;
                    }

                    AuthZoneInfo zoneInfo = new AuthZoneInfo(records[0].Name, zoneType, false);

                    //create zone
                    AuthZone authZone = CreateEmptyZone(zoneInfo);

                    try
                    {
                        //load records
                        LoadRecords(authZone, records);
                    }
                    catch
                    {
                        DeleteZone(zoneInfo.Name);
                        throw;
                    }

                    //init zone
                    switch (zoneInfo.Type)
                    {
                    case AuthZoneType.Primary:
                        (authZone as PrimaryZone).NotifyNameServers();
                        break;
                    }
                }
            }
            break;

            case 3:
            {
                bool zoneDisabled           = bR.ReadBoolean();
                DnsResourceRecord[] records = new DnsResourceRecord[bR.ReadInt32()];
                if (records.Length > 0)
                {
                    DnsResourceRecord soaRecord = null;

                    for (int i = 0; i < records.Length; i++)
                    {
                        records[i]     = new DnsResourceRecord(s);
                        records[i].Tag = new DnsResourceRecordInfo(bR);

                        if (records[i].Type == DnsResourceRecordType.SOA)
                        {
                            soaRecord = records[i];
                        }
                    }

                    if (soaRecord == null)
                    {
                        throw new InvalidDataException("Zone does not contain SOA record.");
                    }

                    //make zone info
                    AuthZoneType zoneType;
                    if (_dnsServer.ServerDomain.Equals((soaRecord.RDATA as DnsSOARecord).PrimaryNameServer, StringComparison.OrdinalIgnoreCase))
                    {
                        zoneType = AuthZoneType.Primary;
                    }
                    else
                    {
                        zoneType = AuthZoneType.Stub;
                    }

                    AuthZoneInfo zoneInfo = new AuthZoneInfo(records[0].Name, zoneType, zoneDisabled);

                    //create zone
                    AuthZone authZone = CreateEmptyZone(zoneInfo);

                    try
                    {
                        //load records
                        LoadRecords(authZone, records);
                    }
                    catch
                    {
                        DeleteZone(zoneInfo.Name);
                        throw;
                    }

                    //init zone
                    switch (zoneInfo.Type)
                    {
                    case AuthZoneType.Primary:
                        (authZone as PrimaryZone).NotifyNameServers();
                        break;
                    }
                }
            }
            break;

            case 4:
            {
                //read zone info
                AuthZoneInfo zoneInfo = new AuthZoneInfo(bR);

                //create zone
                AuthZone authZone = CreateEmptyZone(zoneInfo);

                //read all zone records
                DnsResourceRecord[] records = new DnsResourceRecord[bR.ReadInt32()];
                if (records.Length > 0)
                {
                    for (int i = 0; i < records.Length; i++)
                    {
                        records[i]     = new DnsResourceRecord(s);
                        records[i].Tag = new DnsResourceRecordInfo(bR);
                    }

                    try
                    {
                        //load records
                        LoadRecords(authZone, records);
                    }
                    catch
                    {
                        DeleteZone(zoneInfo.Name);
                        throw;
                    }

                    //init zone
                    switch (zoneInfo.Type)
                    {
                    case AuthZoneType.Primary:
                        (authZone as PrimaryZone).NotifyNameServers();
                        break;

                    case AuthZoneType.Secondary:
                        (authZone as SecondaryZone).RefreshZone();
                        break;

                    case AuthZoneType.Stub:
                        (authZone as StubZone).RefreshZone();
                        break;
                    }
                }
            }
            break;

            default:
                throw new InvalidDataException("DNS Zone file version not supported.");
            }
        }