PrimarySubDomainZone.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2023 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.GetAuthRecordInfo().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.GetAuthRecordInfo().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. base.AddRecord(record);
  111. _primaryZone.CommitAndIncrementSerial(null, new DnsResourceRecord[] { record });
  112. if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
  113. _primaryZone.UpdateDnssecRecordsFor(this, record.Type);
  114. _primaryZone.TriggerNotify();
  115. break;
  116. }
  117. }
  118. public override bool DeleteRecords(DnsResourceRecordType type)
  119. {
  120. switch (type)
  121. {
  122. case DnsResourceRecordType.DNSKEY:
  123. case DnsResourceRecordType.RRSIG:
  124. case DnsResourceRecordType.NSEC:
  125. case DnsResourceRecordType.NSEC3PARAM:
  126. case DnsResourceRecordType.NSEC3:
  127. throw new InvalidOperationException("Cannot delete DNSSEC records.");
  128. default:
  129. if (_entries.TryRemove(type, out IReadOnlyList<DnsResourceRecord> removedRecords))
  130. {
  131. _primaryZone.CommitAndIncrementSerial(removedRecords);
  132. if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
  133. _primaryZone.UpdateDnssecRecordsFor(this, type);
  134. _primaryZone.TriggerNotify();
  135. return true;
  136. }
  137. return false;
  138. }
  139. }
  140. public override bool DeleteRecord(DnsResourceRecordType type, DnsResourceRecordData rdata)
  141. {
  142. switch (type)
  143. {
  144. case DnsResourceRecordType.DNSKEY:
  145. case DnsResourceRecordType.RRSIG:
  146. case DnsResourceRecordType.NSEC:
  147. case DnsResourceRecordType.NSEC3PARAM:
  148. case DnsResourceRecordType.NSEC3:
  149. throw new InvalidOperationException("Cannot delete DNSSEC records.");
  150. default:
  151. if (TryDeleteRecord(type, rdata, out DnsResourceRecord deletedRecord))
  152. {
  153. _primaryZone.CommitAndIncrementSerial(new DnsResourceRecord[] { deletedRecord });
  154. if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
  155. _primaryZone.UpdateDnssecRecordsFor(this, type);
  156. _primaryZone.TriggerNotify();
  157. return true;
  158. }
  159. return false;
  160. }
  161. }
  162. public override void UpdateRecord(DnsResourceRecord oldRecord, DnsResourceRecord newRecord)
  163. {
  164. switch (oldRecord.Type)
  165. {
  166. case DnsResourceRecordType.SOA:
  167. throw new InvalidOperationException("Cannot update record: use SetRecords() for " + oldRecord.Type.ToString() + " record");
  168. case DnsResourceRecordType.DNSKEY:
  169. case DnsResourceRecordType.RRSIG:
  170. case DnsResourceRecordType.NSEC:
  171. case DnsResourceRecordType.NSEC3PARAM:
  172. case DnsResourceRecordType.NSEC3:
  173. throw new InvalidOperationException("Cannot update DNSSEC records.");
  174. default:
  175. if (oldRecord.Type != newRecord.Type)
  176. throw new InvalidOperationException("Old and new record types do not match.");
  177. if ((_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned) && newRecord.GetAuthRecordInfo().Disabled)
  178. throw new DnsServerException("Cannot update record: disabling records in a signed zones is not supported.");
  179. if (newRecord.OriginalTtlValue > _primaryZone.GetZoneSoaExpire())
  180. throw new DnsServerException("Failed to update record: TTL cannot be greater than SOA EXPIRE.");
  181. if (!TryDeleteRecord(oldRecord.Type, oldRecord.RDATA, out DnsResourceRecord deletedRecord))
  182. throw new InvalidOperationException("Cannot update record: the record does not exists to be updated.");
  183. base.AddRecord(newRecord);
  184. _primaryZone.CommitAndIncrementSerial(new DnsResourceRecord[] { deletedRecord }, new DnsResourceRecord[] { newRecord });
  185. if (_primaryZone.DnssecStatus != AuthZoneDnssecStatus.Unsigned)
  186. _primaryZone.UpdateDnssecRecordsFor(this, oldRecord.Type);
  187. _primaryZone.TriggerNotify();
  188. break;
  189. }
  190. }
  191. #endregion
  192. }
  193. }