PrimarySubDomainZone.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using DnsServerCore.Dns.ResourceRecords;
  16. using System;
  17. using System.Collections.Generic;
  18. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  19. namespace DnsServerCore.Dns.Zones
  20. {
  21. class PrimarySubDomainZone : SubDomainZone
  22. {
  23. #region variables
  24. readonly PrimaryZone _primaryZone;
  25. #endregion
  26. #region constructor
  27. public PrimarySubDomainZone(PrimaryZone primaryZone, string name)
  28. : base(primaryZone, name)
  29. {
  30. _primaryZone = primaryZone;
  31. }
  32. #endregion
  33. #region DNSSEC
  34. internal override IReadOnlyList<DnsResourceRecord> SignRRSet(IReadOnlyList<DnsResourceRecord> records)
  35. {
  36. return _primaryZone.SignRRSet(records);
  37. }
  38. #endregion
  39. #region public
  40. public override void SetRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records)
  41. {
  42. if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
  43. {
  44. switch (type)
  45. {
  46. case DnsResourceRecordType.ANAME:
  47. case DnsResourceRecordType.APP:
  48. throw new DnsServerException("The record type is not supported by DNSSEC signed primary zones.");
  49. default:
  50. foreach (DnsResourceRecord record in records)
  51. {
  52. if (record.GetAuthGenericRecordInfo().Disabled)
  53. throw new DnsServerException("Cannot set records: disabling records in a signed zones is not supported.");
  54. }
  55. break;
  56. }
  57. }
  58. switch (type)
  59. {
  60. case DnsResourceRecordType.SOA:
  61. throw new InvalidOperationException("Cannot set SOA record on sub domain.");
  62. case DnsResourceRecordType.DNSKEY:
  63. case DnsResourceRecordType.RRSIG:
  64. case DnsResourceRecordType.NSEC:
  65. case DnsResourceRecordType.NSEC3PARAM:
  66. case DnsResourceRecordType.NSEC3:
  67. throw new InvalidOperationException("Cannot set DNSSEC records.");
  68. case DnsResourceRecordType.FWD:
  69. throw new DnsServerException("The record type is not supported by primary zones.");
  70. default:
  71. if (records[0].OriginalTtlValue > _primaryZone.GetZoneSoaExpire())
  72. throw new DnsServerException("Failed to set records: TTL cannot be greater than SOA EXPIRE.");
  73. if (!TrySetRecords(type, records, out IReadOnlyList<DnsResourceRecord> deletedRecords))
  74. throw new DnsServerException("Failed to set records. Please try again.");
  75. _primaryZone.CommitAndIncrementSerial(deletedRecords, records);
  76. if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
  77. _primaryZone.UpdateDnssecRecordsFor(this, type);
  78. _primaryZone.TriggerNotify();
  79. break;
  80. }
  81. }
  82. public override void AddRecord(DnsResourceRecord record)
  83. {
  84. if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
  85. {
  86. switch (record.Type)
  87. {
  88. case DnsResourceRecordType.ANAME:
  89. case DnsResourceRecordType.APP:
  90. throw new DnsServerException("The record type is not supported by DNSSEC signed primary zones.");
  91. default:
  92. if (record.GetAuthGenericRecordInfo().Disabled)
  93. throw new DnsServerException("Cannot add record: disabling records in a signed zones is not supported.");
  94. break;
  95. }
  96. }
  97. switch (record.Type)
  98. {
  99. case DnsResourceRecordType.DNSKEY:
  100. case DnsResourceRecordType.RRSIG:
  101. case DnsResourceRecordType.NSEC:
  102. case DnsResourceRecordType.NSEC3PARAM:
  103. case DnsResourceRecordType.NSEC3:
  104. throw new InvalidOperationException("Cannot add DNSSEC record.");
  105. case DnsResourceRecordType.FWD:
  106. throw new DnsServerException("The record type is not supported by primary zones.");
  107. default:
  108. if (record.OriginalTtlValue > _primaryZone.GetZoneSoaExpire())
  109. throw new DnsServerException("Failed to add record: TTL cannot be greater than SOA EXPIRE.");
  110. AddRecord(record, out IReadOnlyList<DnsResourceRecord> addedRecords, out IReadOnlyList<DnsResourceRecord> deletedRecords);
  111. if (addedRecords.Count > 0)
  112. {
  113. _primaryZone.CommitAndIncrementSerial(deletedRecords, addedRecords);
  114. if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
  115. _primaryZone.UpdateDnssecRecordsFor(this, record.Type);
  116. _primaryZone.TriggerNotify();
  117. }
  118. break;
  119. }
  120. }
  121. public override bool DeleteRecords(DnsResourceRecordType type)
  122. {
  123. switch (type)
  124. {
  125. case DnsResourceRecordType.DNSKEY:
  126. case DnsResourceRecordType.RRSIG:
  127. case DnsResourceRecordType.NSEC:
  128. case DnsResourceRecordType.NSEC3PARAM:
  129. case DnsResourceRecordType.NSEC3:
  130. throw new InvalidOperationException("Cannot delete DNSSEC records.");
  131. default:
  132. if (_entries.TryRemove(type, out IReadOnlyList<DnsResourceRecord> removedRecords))
  133. {
  134. _primaryZone.CommitAndIncrementSerial(removedRecords);
  135. if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
  136. _primaryZone.UpdateDnssecRecordsFor(this, type);
  137. _primaryZone.TriggerNotify();
  138. return true;
  139. }
  140. return false;
  141. }
  142. }
  143. public override bool DeleteRecord(DnsResourceRecordType type, DnsResourceRecordData rdata)
  144. {
  145. switch (type)
  146. {
  147. case DnsResourceRecordType.DNSKEY:
  148. case DnsResourceRecordType.RRSIG:
  149. case DnsResourceRecordType.NSEC:
  150. case DnsResourceRecordType.NSEC3PARAM:
  151. case DnsResourceRecordType.NSEC3:
  152. throw new InvalidOperationException("Cannot delete DNSSEC records.");
  153. default:
  154. if (TryDeleteRecord(type, rdata, out DnsResourceRecord deletedRecord))
  155. {
  156. _primaryZone.CommitAndIncrementSerial(new DnsResourceRecord[] { deletedRecord });
  157. if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
  158. _primaryZone.UpdateDnssecRecordsFor(this, type);
  159. _primaryZone.TriggerNotify();
  160. return true;
  161. }
  162. return false;
  163. }
  164. }
  165. public override void UpdateRecord(DnsResourceRecord oldRecord, DnsResourceRecord newRecord)
  166. {
  167. switch (oldRecord.Type)
  168. {
  169. case DnsResourceRecordType.SOA:
  170. throw new InvalidOperationException("Cannot update record: use SetRecords() for " + oldRecord.Type.ToString() + " record");
  171. case DnsResourceRecordType.DNSKEY:
  172. case DnsResourceRecordType.RRSIG:
  173. case DnsResourceRecordType.NSEC:
  174. case DnsResourceRecordType.NSEC3PARAM:
  175. case DnsResourceRecordType.NSEC3:
  176. throw new InvalidOperationException("Cannot update DNSSEC records.");
  177. default:
  178. if (oldRecord.Type != newRecord.Type)
  179. throw new InvalidOperationException("Old and new record types do not match.");
  180. if ((_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned) && newRecord.GetAuthGenericRecordInfo().Disabled)
  181. throw new DnsServerException("Cannot update record: disabling records in a signed zones is not supported.");
  182. if (newRecord.OriginalTtlValue > _primaryZone.GetZoneSoaExpire())
  183. throw new DnsServerException("Failed to update record: TTL cannot be greater than SOA EXPIRE.");
  184. if (!TryDeleteRecord(oldRecord.Type, oldRecord.RDATA, out DnsResourceRecord deletedRecord))
  185. throw new InvalidOperationException("Cannot update record: the record does not exists to be updated.");
  186. AddRecord(newRecord, out IReadOnlyList<DnsResourceRecord> addedRecords, out IReadOnlyList<DnsResourceRecord> deletedRecords);
  187. List<DnsResourceRecord> allDeletedRecords = new List<DnsResourceRecord>(deletedRecords.Count + 1);
  188. allDeletedRecords.Add(deletedRecord);
  189. allDeletedRecords.AddRange(deletedRecords);
  190. _primaryZone.CommitAndIncrementSerial(allDeletedRecords, addedRecords);
  191. if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
  192. _primaryZone.UpdateDnssecRecordsFor(this, oldRecord.Type);
  193. _primaryZone.TriggerNotify();
  194. break;
  195. }
  196. }
  197. #endregion
  198. }
  199. }