ForwarderZone.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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;
  19. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  20. namespace DnsServerCore.Dns.Zones
  21. {
  22. class ForwarderZone : ApexZone
  23. {
  24. #region constructor
  25. public ForwarderZone(DnsServer dnsServer, AuthZoneInfo zoneInfo)
  26. : base(dnsServer, zoneInfo)
  27. {
  28. InitNotify();
  29. InitRecordExpiry();
  30. }
  31. public ForwarderZone(DnsServer dnsServer, string name)
  32. : base(dnsServer, name)
  33. {
  34. InitZone();
  35. InitNotify();
  36. InitRecordExpiry();
  37. }
  38. public ForwarderZone(DnsServer dnsServer, string name, DnsTransportProtocol forwarderProtocol, string forwarder, bool dnssecValidation, DnsForwarderRecordProxyType proxyType, string proxyAddress, ushort proxyPort, string proxyUsername, string proxyPassword, string fwdRecordComments)
  39. : base(dnsServer, name)
  40. {
  41. DnsResourceRecord fwdRecord = new DnsResourceRecord(name, DnsResourceRecordType.FWD, DnsClass.IN, 0, new DnsForwarderRecordData(forwarderProtocol, forwarder, dnssecValidation, proxyType, proxyAddress, proxyPort, proxyUsername, proxyPassword, 0));
  42. if (!string.IsNullOrEmpty(fwdRecordComments))
  43. fwdRecord.GetAuthGenericRecordInfo().Comments = fwdRecordComments;
  44. fwdRecord.GetAuthGenericRecordInfo().LastModified = DateTime.UtcNow;
  45. _entries[DnsResourceRecordType.FWD] = [fwdRecord];
  46. InitZone();
  47. InitNotify();
  48. InitRecordExpiry();
  49. }
  50. #endregion
  51. #region internal
  52. internal virtual void InitZone()
  53. {
  54. //init forwarder zone with dummy SOA record
  55. DnsSOARecordData soa = new DnsSOARecordData(_dnsServer.ServerDomain, "invalid", 1, 900, 300, 604800, 900);
  56. DnsResourceRecord soaRecord = new DnsResourceRecord(_name, DnsResourceRecordType.SOA, DnsClass.IN, 0, soa);
  57. soaRecord.GetAuthGenericRecordInfo().LastModified = DateTime.UtcNow;
  58. _entries[DnsResourceRecordType.SOA] = [soaRecord];
  59. }
  60. #endregion
  61. #region public
  62. public override string GetZoneTypeName()
  63. {
  64. return "Conditional Forwarder";
  65. }
  66. public override void SetRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records)
  67. {
  68. switch (type)
  69. {
  70. case DnsResourceRecordType.CNAME:
  71. throw new InvalidOperationException("Cannot set CNAME record at zone apex.");
  72. case DnsResourceRecordType.SOA:
  73. if ((records.Count != 1) || !records[0].Name.Equals(_name, StringComparison.OrdinalIgnoreCase))
  74. throw new InvalidOperationException("Invalid SOA record.");
  75. DnsResourceRecord newSoaRecord = records[0];
  76. DnsSOARecordData newSoa = newSoaRecord.RDATA as DnsSOARecordData;
  77. if (newSoaRecord.OriginalTtlValue > newSoa.Expire)
  78. throw new DnsServerException("Failed to set records: TTL cannot be greater than SOA EXPIRE.");
  79. if (newSoa.Retry > newSoa.Refresh)
  80. throw new DnsServerException("Failed to set records: SOA RETRY cannot be greater than SOA REFRESH.");
  81. if (newSoa.Refresh > newSoa.Expire)
  82. throw new DnsServerException("Failed to set records: SOA REFRESH cannot be greater than SOA EXPIRE.");
  83. {
  84. //reset fixed record values
  85. DnsSOARecordData modifiedSoa = new DnsSOARecordData(newSoa.PrimaryNameServer, "invalid", newSoa.Serial, newSoa.Refresh, newSoa.Retry, newSoa.Expire, newSoa.Minimum);
  86. newSoaRecord = new DnsResourceRecord(_name, DnsResourceRecordType.SOA, DnsClass.IN, 0, modifiedSoa) { Tag = newSoaRecord.Tag };
  87. records = [newSoaRecord];
  88. }
  89. //remove any record info except serial date scheme and comments
  90. bool useSoaSerialDateScheme;
  91. string comments;
  92. {
  93. SOARecordInfo recordInfo = newSoaRecord.GetAuthSOARecordInfo();
  94. useSoaSerialDateScheme = recordInfo.UseSoaSerialDateScheme;
  95. comments = recordInfo.Comments;
  96. }
  97. newSoaRecord.Tag = null; //remove old record info
  98. {
  99. SOARecordInfo recordInfo = newSoaRecord.GetAuthSOARecordInfo();
  100. recordInfo.UseSoaSerialDateScheme = useSoaSerialDateScheme;
  101. recordInfo.Comments = comments;
  102. recordInfo.LastModified = DateTime.UtcNow;
  103. }
  104. //setting new SOA
  105. CommitAndIncrementSerial(null, records);
  106. TriggerNotify();
  107. break;
  108. case DnsResourceRecordType.DS:
  109. case DnsResourceRecordType.DNSKEY:
  110. case DnsResourceRecordType.RRSIG:
  111. case DnsResourceRecordType.NSEC:
  112. case DnsResourceRecordType.NSEC3PARAM:
  113. case DnsResourceRecordType.NSEC3:
  114. throw new InvalidOperationException("Cannot set DNSSEC records.");
  115. default:
  116. if (records[0].OriginalTtlValue > GetZoneSoaExpire())
  117. throw new DnsServerException("Failed to set records: TTL cannot be greater than SOA EXPIRE.");
  118. if (!TrySetRecords(type, records, out IReadOnlyList<DnsResourceRecord> deletedRecords))
  119. throw new DnsServerException("Failed to set records. Please try again.");
  120. CommitAndIncrementSerial(deletedRecords, records);
  121. TriggerNotify();
  122. break;
  123. }
  124. }
  125. public override void AddRecord(DnsResourceRecord record)
  126. {
  127. switch (record.Type)
  128. {
  129. case DnsResourceRecordType.DS:
  130. case DnsResourceRecordType.DNSKEY:
  131. case DnsResourceRecordType.RRSIG:
  132. case DnsResourceRecordType.NSEC:
  133. case DnsResourceRecordType.NSEC3PARAM:
  134. case DnsResourceRecordType.NSEC3:
  135. throw new InvalidOperationException("Cannot set DNSSEC records.");
  136. default:
  137. if (record.OriginalTtlValue > GetZoneSoaExpire())
  138. throw new DnsServerException("Failed to add record: TTL cannot be greater than SOA EXPIRE.");
  139. AddRecord(record, out IReadOnlyList<DnsResourceRecord> addedRecords, out IReadOnlyList<DnsResourceRecord> deletedRecords);
  140. if (addedRecords.Count > 0)
  141. {
  142. CommitAndIncrementSerial(deletedRecords, addedRecords);
  143. TriggerNotify();
  144. }
  145. break;
  146. }
  147. }
  148. public override bool DeleteRecords(DnsResourceRecordType type)
  149. {
  150. switch (type)
  151. {
  152. case DnsResourceRecordType.SOA:
  153. throw new InvalidOperationException("Cannot delete SOA record.");
  154. default:
  155. if (_entries.TryRemove(type, out IReadOnlyList<DnsResourceRecord> removedRecords))
  156. {
  157. CommitAndIncrementSerial(removedRecords);
  158. TriggerNotify();
  159. return true;
  160. }
  161. return false;
  162. }
  163. }
  164. public override bool DeleteRecord(DnsResourceRecordType type, DnsResourceRecordData rdata)
  165. {
  166. switch (type)
  167. {
  168. case DnsResourceRecordType.SOA:
  169. throw new InvalidOperationException("Cannot delete SOA record.");
  170. default:
  171. if (TryDeleteRecord(type, rdata, out DnsResourceRecord deletedRecord))
  172. {
  173. CommitAndIncrementSerial([deletedRecord]);
  174. TriggerNotify();
  175. return true;
  176. }
  177. return false;
  178. }
  179. }
  180. public override void UpdateRecord(DnsResourceRecord oldRecord, DnsResourceRecord newRecord)
  181. {
  182. switch (oldRecord.Type)
  183. {
  184. case DnsResourceRecordType.SOA:
  185. throw new InvalidOperationException("Cannot update record: use SetRecords() for " + oldRecord.Type.ToString() + " record");
  186. default:
  187. if (oldRecord.Type != newRecord.Type)
  188. throw new InvalidOperationException("Old and new record types do not match.");
  189. if (newRecord.OriginalTtlValue > GetZoneSoaExpire())
  190. throw new DnsServerException("Cannot update record: TTL cannot be greater than SOA EXPIRE.");
  191. if (!TryDeleteRecord(oldRecord.Type, oldRecord.RDATA, out DnsResourceRecord deletedRecord))
  192. throw new DnsServerException("Cannot update record: the record does not exists to be updated.");
  193. AddRecord(newRecord, out IReadOnlyList<DnsResourceRecord> addedRecords, out IReadOnlyList<DnsResourceRecord> deletedRecords);
  194. List<DnsResourceRecord> allDeletedRecords = new List<DnsResourceRecord>(deletedRecords.Count + 1);
  195. allDeletedRecords.Add(deletedRecord);
  196. allDeletedRecords.AddRange(deletedRecords);
  197. CommitAndIncrementSerial(allDeletedRecords, addedRecords);
  198. TriggerNotify();
  199. break;
  200. }
  201. }
  202. public override IReadOnlyList<DnsResourceRecord> QueryRecords(DnsResourceRecordType type, bool dnssecOk)
  203. {
  204. if (type == DnsResourceRecordType.SOA)
  205. return []; //forwarder zone is not authoritative and contains dummy SOA record
  206. return base.QueryRecords(type, dnssecOk);
  207. }
  208. #endregion
  209. #region properties
  210. public override bool Disabled
  211. {
  212. get { return base.Disabled; }
  213. set
  214. {
  215. if (base.Disabled == value)
  216. return;
  217. base.Disabled = value; //set value early to be able to use it for notify
  218. if (value)
  219. DisableNotifyTimer();
  220. else
  221. TriggerNotify();
  222. }
  223. }
  224. public override AuthZoneQueryAccess QueryAccess
  225. {
  226. get { return base.QueryAccess; }
  227. set
  228. {
  229. switch (value)
  230. {
  231. case AuthZoneQueryAccess.AllowOnlyZoneNameServers:
  232. case AuthZoneQueryAccess.AllowZoneNameServersAndUseSpecifiedNetworkACL:
  233. throw new ArgumentException("The Query Access option is invalid for " + GetZoneTypeName() + " zones: " + value.ToString(), nameof(QueryAccess));
  234. }
  235. base.QueryAccess = value;
  236. }
  237. }
  238. public override AuthZoneTransfer ZoneTransfer
  239. {
  240. get { return base.ZoneTransfer; }
  241. set
  242. {
  243. switch (value)
  244. {
  245. case AuthZoneTransfer.AllowOnlyZoneNameServers:
  246. case AuthZoneTransfer.AllowZoneNameServersAndUseSpecifiedNetworkACL:
  247. throw new ArgumentException("The Zone Transfer option is invalid for " + GetZoneTypeName() + " zones: " + value.ToString(), nameof(ZoneTransfer));
  248. }
  249. base.ZoneTransfer = value;
  250. }
  251. }
  252. public override AuthZoneNotify Notify
  253. {
  254. get { return base.Notify; }
  255. set
  256. {
  257. switch (value)
  258. {
  259. case AuthZoneNotify.ZoneNameServers:
  260. case AuthZoneNotify.BothZoneAndSpecifiedNameServers:
  261. throw new ArgumentException("The Notify option is invalid for " + GetZoneTypeName() + " zones: " + value.ToString(), nameof(Notify));
  262. case AuthZoneNotify.SeparateNameServersForCatalogAndMemberZones:
  263. if (this is CatalogZone)
  264. break;
  265. throw new ArgumentException("The Notify option is invalid for " + GetZoneTypeName() + " zones: " + value.ToString(), nameof(Notify));
  266. }
  267. base.Notify = value;
  268. }
  269. }
  270. public override AuthZoneUpdate Update
  271. {
  272. get { return base.Update; }
  273. set
  274. {
  275. switch (value)
  276. {
  277. case AuthZoneUpdate.AllowOnlyZoneNameServers:
  278. case AuthZoneUpdate.AllowZoneNameServersAndUseSpecifiedNetworkACL:
  279. throw new ArgumentException("The Dynamic Updates option is invalid for " + GetZoneTypeName() + " zones: " + value.ToString(), nameof(Update));
  280. }
  281. base.Update = value;
  282. }
  283. }
  284. #endregion
  285. }
  286. }