123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- /*
- Technitium DNS Server
- Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com)
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- using DnsServerCore.Dns.ResourceRecords;
- using System;
- using System.Collections.Generic;
- using TechnitiumLibrary.Net.Dns.ResourceRecords;
- namespace DnsServerCore.Dns.Zones
- {
- class PrimarySubDomainZone : SubDomainZone
- {
- #region variables
- readonly PrimaryZone _primaryZone;
- #endregion
- #region constructor
- public PrimarySubDomainZone(PrimaryZone primaryZone, string name)
- : base(primaryZone, name)
- {
- _primaryZone = primaryZone;
- }
- #endregion
- #region DNSSEC
- internal override IReadOnlyList<DnsResourceRecord> SignRRSet(IReadOnlyList<DnsResourceRecord> records)
- {
- return _primaryZone.SignRRSet(records);
- }
- #endregion
- #region public
- public override void SetRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records)
- {
- if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
- {
- switch (type)
- {
- case DnsResourceRecordType.ANAME:
- case DnsResourceRecordType.APP:
- throw new DnsServerException("The record type is not supported by DNSSEC signed primary zones.");
- default:
- foreach (DnsResourceRecord record in records)
- {
- if (record.GetAuthGenericRecordInfo().Disabled)
- throw new DnsServerException("Cannot set records: disabling records in a signed zones is not supported.");
- }
- break;
- }
- }
- switch (type)
- {
- case DnsResourceRecordType.SOA:
- throw new InvalidOperationException("Cannot set SOA record on sub domain.");
- case DnsResourceRecordType.DNSKEY:
- case DnsResourceRecordType.RRSIG:
- case DnsResourceRecordType.NSEC:
- case DnsResourceRecordType.NSEC3PARAM:
- case DnsResourceRecordType.NSEC3:
- throw new InvalidOperationException("Cannot set DNSSEC records.");
- case DnsResourceRecordType.FWD:
- throw new DnsServerException("The record type is not supported by primary zones.");
- default:
- if (records[0].OriginalTtlValue > _primaryZone.GetZoneSoaExpire())
- throw new DnsServerException("Failed to set records: TTL cannot be greater than SOA EXPIRE.");
- if (!TrySetRecords(type, records, out IReadOnlyList<DnsResourceRecord> deletedRecords))
- throw new DnsServerException("Failed to set records. Please try again.");
- _primaryZone.CommitAndIncrementSerial(deletedRecords, records);
- if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
- _primaryZone.UpdateDnssecRecordsFor(this, type);
- _primaryZone.TriggerNotify();
- break;
- }
- }
- public override void AddRecord(DnsResourceRecord record)
- {
- if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
- {
- switch (record.Type)
- {
- case DnsResourceRecordType.ANAME:
- case DnsResourceRecordType.APP:
- throw new DnsServerException("The record type is not supported by DNSSEC signed primary zones.");
- default:
- if (record.GetAuthGenericRecordInfo().Disabled)
- throw new DnsServerException("Cannot add record: disabling records in a signed zones is not supported.");
- break;
- }
- }
- switch (record.Type)
- {
- case DnsResourceRecordType.DNSKEY:
- case DnsResourceRecordType.RRSIG:
- case DnsResourceRecordType.NSEC:
- case DnsResourceRecordType.NSEC3PARAM:
- case DnsResourceRecordType.NSEC3:
- throw new InvalidOperationException("Cannot add DNSSEC record.");
- case DnsResourceRecordType.FWD:
- throw new DnsServerException("The record type is not supported by primary zones.");
- default:
- if (record.OriginalTtlValue > _primaryZone.GetZoneSoaExpire())
- throw new DnsServerException("Failed to add record: TTL cannot be greater than SOA EXPIRE.");
- AddRecord(record, out IReadOnlyList<DnsResourceRecord> addedRecords, out IReadOnlyList<DnsResourceRecord> deletedRecords);
- if (addedRecords.Count > 0)
- {
- _primaryZone.CommitAndIncrementSerial(deletedRecords, addedRecords);
- if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
- _primaryZone.UpdateDnssecRecordsFor(this, record.Type);
- _primaryZone.TriggerNotify();
- }
- break;
- }
- }
- public override bool DeleteRecords(DnsResourceRecordType type)
- {
- switch (type)
- {
- case DnsResourceRecordType.DNSKEY:
- case DnsResourceRecordType.RRSIG:
- case DnsResourceRecordType.NSEC:
- case DnsResourceRecordType.NSEC3PARAM:
- case DnsResourceRecordType.NSEC3:
- throw new InvalidOperationException("Cannot delete DNSSEC records.");
- default:
- if (_entries.TryRemove(type, out IReadOnlyList<DnsResourceRecord> removedRecords))
- {
- _primaryZone.CommitAndIncrementSerial(removedRecords);
- if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
- _primaryZone.UpdateDnssecRecordsFor(this, type);
- _primaryZone.TriggerNotify();
- return true;
- }
- return false;
- }
- }
- public override bool DeleteRecord(DnsResourceRecordType type, DnsResourceRecordData rdata)
- {
- switch (type)
- {
- case DnsResourceRecordType.DNSKEY:
- case DnsResourceRecordType.RRSIG:
- case DnsResourceRecordType.NSEC:
- case DnsResourceRecordType.NSEC3PARAM:
- case DnsResourceRecordType.NSEC3:
- throw new InvalidOperationException("Cannot delete DNSSEC records.");
- default:
- if (TryDeleteRecord(type, rdata, out DnsResourceRecord deletedRecord))
- {
- _primaryZone.CommitAndIncrementSerial(new DnsResourceRecord[] { deletedRecord });
- if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
- _primaryZone.UpdateDnssecRecordsFor(this, type);
- _primaryZone.TriggerNotify();
- return true;
- }
- return false;
- }
- }
- public override void UpdateRecord(DnsResourceRecord oldRecord, DnsResourceRecord newRecord)
- {
- switch (oldRecord.Type)
- {
- case DnsResourceRecordType.SOA:
- throw new InvalidOperationException("Cannot update record: use SetRecords() for " + oldRecord.Type.ToString() + " record");
- case DnsResourceRecordType.DNSKEY:
- case DnsResourceRecordType.RRSIG:
- case DnsResourceRecordType.NSEC:
- case DnsResourceRecordType.NSEC3PARAM:
- case DnsResourceRecordType.NSEC3:
- throw new InvalidOperationException("Cannot update DNSSEC records.");
- default:
- if (oldRecord.Type != newRecord.Type)
- throw new InvalidOperationException("Old and new record types do not match.");
- if ((_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned) && newRecord.GetAuthGenericRecordInfo().Disabled)
- throw new DnsServerException("Cannot update record: disabling records in a signed zones is not supported.");
- if (newRecord.OriginalTtlValue > _primaryZone.GetZoneSoaExpire())
- throw new DnsServerException("Failed to update record: TTL cannot be greater than SOA EXPIRE.");
- if (!TryDeleteRecord(oldRecord.Type, oldRecord.RDATA, out DnsResourceRecord deletedRecord))
- throw new InvalidOperationException("Cannot update record: the record does not exists to be updated.");
- AddRecord(newRecord, out IReadOnlyList<DnsResourceRecord> addedRecords, out IReadOnlyList<DnsResourceRecord> deletedRecords);
- List<DnsResourceRecord> allDeletedRecords = new List<DnsResourceRecord>(deletedRecords.Count + 1);
- allDeletedRecords.Add(deletedRecord);
- allDeletedRecords.AddRange(deletedRecords);
- _primaryZone.CommitAndIncrementSerial(allDeletedRecords, addedRecords);
- if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
- _primaryZone.UpdateDnssecRecordsFor(this, oldRecord.Type);
- _primaryZone.TriggerNotify();
- break;
- }
- }
- #endregion
- }
- }
|