StubZone.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 System.Threading;
  19. using System.Threading.Tasks;
  20. using TechnitiumLibrary;
  21. using TechnitiumLibrary.Net.Dns;
  22. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  23. namespace DnsServerCore.Dns.Zones
  24. {
  25. class StubZone : ApexZone
  26. {
  27. #region variables
  28. readonly object _refreshTimerLock = new object();
  29. Timer _refreshTimer;
  30. bool _refreshTimerTriggered;
  31. const int REFRESH_TIMER_INTERVAL = 5000;
  32. const int REFRESH_TIMEOUT = 10000;
  33. const int REFRESH_RETRIES = 5;
  34. IReadOnlyList<NameServerAddress> _primaryNameServerAddresses;
  35. DateTime _expiry;
  36. bool _isExpired;
  37. bool _resync;
  38. #endregion
  39. #region constructor
  40. public StubZone(DnsServer dnsServer, AuthZoneInfo zoneInfo)
  41. : base(dnsServer, zoneInfo)
  42. {
  43. _primaryNameServerAddresses = zoneInfo.PrimaryNameServerAddresses;
  44. _expiry = zoneInfo.Expiry;
  45. _isExpired = DateTime.UtcNow > _expiry;
  46. _refreshTimer = new Timer(RefreshTimerCallback, null, Timeout.Infinite, Timeout.Infinite);
  47. }
  48. private StubZone(DnsServer dnsServer, string name, IReadOnlyList<NameServerAddress> primaryNameServerAddresses)
  49. : base(dnsServer, name)
  50. {
  51. PrimaryNameServerAddresses = primaryNameServerAddresses?.Convert(delegate (NameServerAddress nameServer)
  52. {
  53. if (nameServer.Protocol != DnsTransportProtocol.Udp)
  54. nameServer = nameServer.ChangeProtocol(DnsTransportProtocol.Udp);
  55. return nameServer;
  56. });
  57. _isExpired = true; //new stub zone is considered expired till it refreshes
  58. _refreshTimer = new Timer(RefreshTimerCallback, null, Timeout.Infinite, Timeout.Infinite);
  59. }
  60. #endregion
  61. #region static
  62. public static async Task<StubZone> CreateAsync(DnsServer dnsServer, string name, IReadOnlyList<NameServerAddress> primaryNameServerAddresses = null, bool ignoreSoaFailure = false)
  63. {
  64. StubZone stubZone = new StubZone(dnsServer, name, primaryNameServerAddresses);
  65. try
  66. {
  67. DnsDatagram soaResponse;
  68. try
  69. {
  70. DnsQuestionRecord soaQuestion = new DnsQuestionRecord(name, DnsResourceRecordType.SOA, DnsClass.IN);
  71. if (stubZone.PrimaryNameServerAddresses is null)
  72. {
  73. soaResponse = await stubZone._dnsServer.DirectQueryAsync(soaQuestion);
  74. }
  75. else
  76. {
  77. DnsClient dnsClient = new DnsClient(stubZone.PrimaryNameServerAddresses);
  78. foreach (NameServerAddress nameServerAddress in dnsClient.Servers)
  79. {
  80. if (nameServerAddress.IsIPEndPointStale)
  81. await nameServerAddress.ResolveIPAddressAsync(stubZone._dnsServer, stubZone._dnsServer.PreferIPv6);
  82. }
  83. dnsClient.Proxy = stubZone._dnsServer.Proxy;
  84. dnsClient.PreferIPv6 = stubZone._dnsServer.PreferIPv6;
  85. DnsDatagram soaRequest = new DnsDatagram(0, false, DnsOpcode.StandardQuery, false, false, false, false, false, false, DnsResponseCode.NoError, [soaQuestion], null, null, null, dnsServer.UdpPayloadSize);
  86. soaResponse = await dnsClient.RawResolveAsync(soaRequest);
  87. }
  88. }
  89. catch (Exception ex)
  90. {
  91. throw new DnsServerException("DNS Server failed to find SOA record for: " + name, ex);
  92. }
  93. if ((soaResponse.Answer.Count == 0) || (soaResponse.Answer[0].Type != DnsResourceRecordType.SOA))
  94. throw new DnsServerException("DNS Server did not receive SOA record in response from any of the primary name servers for: " + name);
  95. DnsResourceRecord receivedSoaRecord = soaResponse.Answer[0];
  96. DnsSOARecordData receivedSoa = receivedSoaRecord.RDATA as DnsSOARecordData;
  97. DnsSOARecordData soa = new DnsSOARecordData(receivedSoa.PrimaryNameServer, receivedSoa.ResponsiblePerson, 0u, receivedSoa.Refresh, receivedSoa.Retry, receivedSoa.Expire, receivedSoa.Minimum);
  98. DnsResourceRecord soaRecord = new DnsResourceRecord(stubZone._name, DnsResourceRecordType.SOA, DnsClass.IN, receivedSoaRecord.TTL, soa);
  99. stubZone._entries[DnsResourceRecordType.SOA] = [soaRecord];
  100. }
  101. catch
  102. {
  103. if (!ignoreSoaFailure)
  104. throw;
  105. //continue with dummy SOA
  106. DnsSOARecordData soa = new DnsSOARecordData(stubZone._dnsServer.ServerDomain, "invalid", 0, 300, 60, 604800, 900);
  107. DnsResourceRecord soaRecord = new DnsResourceRecord(stubZone._name, DnsResourceRecordType.SOA, DnsClass.IN, 0, soa);
  108. soaRecord.GetAuthGenericRecordInfo().LastModified = DateTime.UtcNow;
  109. stubZone._entries[DnsResourceRecordType.SOA] = [soaRecord];
  110. }
  111. return stubZone;
  112. }
  113. #endregion
  114. #region IDisposable
  115. bool _disposed;
  116. protected override void Dispose(bool disposing)
  117. {
  118. try
  119. {
  120. if (_disposed)
  121. return;
  122. if (disposing)
  123. {
  124. lock (_refreshTimerLock)
  125. {
  126. if (_refreshTimer != null)
  127. {
  128. _refreshTimer.Dispose();
  129. _refreshTimer = null;
  130. }
  131. }
  132. }
  133. _disposed = true;
  134. }
  135. finally
  136. {
  137. base.Dispose(disposing);
  138. }
  139. }
  140. #endregion
  141. #region private
  142. private async void RefreshTimerCallback(object state)
  143. {
  144. try
  145. {
  146. if (Disabled && !_resync)
  147. return;
  148. _isExpired = DateTime.UtcNow > _expiry;
  149. //get primary name server addresses
  150. IReadOnlyList<NameServerAddress> primaryNameServers = await GetResolvedPrimaryNameServerAddressesAsync();
  151. if (primaryNameServers.Count == 0)
  152. {
  153. _dnsServer.LogManager?.Write("DNS Server could not find primary name server IP addresses for Stub zone: " + ToString());
  154. //set timer for retry
  155. DnsSOARecordData soa1 = _entries[DnsResourceRecordType.SOA][0].RDATA as DnsSOARecordData;
  156. ResetRefreshTimer(soa1.Retry * 1000);
  157. _syncFailed = true;
  158. return;
  159. }
  160. //refresh zone
  161. if (await RefreshZoneAsync(primaryNameServers))
  162. {
  163. //zone refreshed; set timer for refresh
  164. DnsSOARecordData latestSoa = _entries[DnsResourceRecordType.SOA][0].RDATA as DnsSOARecordData;
  165. ResetRefreshTimer(latestSoa.Refresh * 1000);
  166. _syncFailed = false;
  167. _expiry = DateTime.UtcNow.AddSeconds(latestSoa.Expire);
  168. _isExpired = false;
  169. _resync = false;
  170. _dnsServer.AuthZoneManager.SaveZoneFile(_name);
  171. return;
  172. }
  173. //no response from any of the name servers; set timer for retry
  174. DnsSOARecordData soa = _entries[DnsResourceRecordType.SOA][0].RDATA as DnsSOARecordData;
  175. ResetRefreshTimer(soa.Retry * 1000);
  176. _syncFailed = true;
  177. }
  178. catch (Exception ex)
  179. {
  180. _dnsServer.LogManager?.Write(ex);
  181. //set timer for retry
  182. DnsSOARecordData soa = _entries[DnsResourceRecordType.SOA][0].RDATA as DnsSOARecordData;
  183. ResetRefreshTimer(soa.Retry * 1000);
  184. _syncFailed = true;
  185. }
  186. finally
  187. {
  188. _refreshTimerTriggered = false;
  189. }
  190. }
  191. private void ResetRefreshTimer(long dueTime)
  192. {
  193. lock (_refreshTimerLock)
  194. {
  195. _refreshTimer?.Change(dueTime, Timeout.Infinite);
  196. }
  197. }
  198. private async Task<bool> RefreshZoneAsync(IReadOnlyList<NameServerAddress> nameServers)
  199. {
  200. try
  201. {
  202. _dnsServer.LogManager?.Write("DNS Server has started zone refresh for Stub zone: " + ToString());
  203. DnsClient client = new DnsClient(nameServers);
  204. client.Proxy = _dnsServer.Proxy;
  205. client.PreferIPv6 = _dnsServer.PreferIPv6;
  206. client.Timeout = REFRESH_TIMEOUT;
  207. client.Retries = REFRESH_RETRIES;
  208. client.Concurrency = 1;
  209. DnsDatagram soaRequest = new DnsDatagram(0, false, DnsOpcode.StandardQuery, false, false, false, false, false, false, DnsResponseCode.NoError, [new DnsQuestionRecord(_name, DnsResourceRecordType.SOA, DnsClass.IN)], null, null, null, _dnsServer.UdpPayloadSize);
  210. DnsDatagram soaResponse = await client.RawResolveAsync(soaRequest);
  211. if (soaResponse.RCODE != DnsResponseCode.NoError)
  212. {
  213. _dnsServer.LogManager?.Write("DNS Server received RCODE=" + soaResponse.RCODE.ToString() + " for '" + ToString() + "' Stub zone refresh from: " + soaResponse.Metadata.NameServer.ToString());
  214. return false;
  215. }
  216. if ((soaResponse.Answer.Count < 1) || (soaResponse.Answer[0].Type != DnsResourceRecordType.SOA) || !_name.Equals(soaResponse.Answer[0].Name, StringComparison.OrdinalIgnoreCase))
  217. {
  218. _dnsServer.LogManager?.Write("DNS Server received an empty response for SOA query for '" + ToString() + "' Stub zone refresh from: " + soaResponse.Metadata.NameServer.ToString());
  219. return false;
  220. }
  221. DnsResourceRecord currentSoaRecord = _entries[DnsResourceRecordType.SOA][0];
  222. DnsResourceRecord receivedSoaRecord = soaResponse.Answer[0];
  223. DnsSOARecordData currentSoa = currentSoaRecord.RDATA as DnsSOARecordData;
  224. DnsSOARecordData receivedSoa = receivedSoaRecord.RDATA as DnsSOARecordData;
  225. //compare using sequence space arithmetic
  226. if (!_resync && !currentSoa.IsZoneUpdateAvailable(receivedSoa))
  227. {
  228. _dnsServer.LogManager?.Write("DNS Server successfully checked for '" + ToString() + "' Stub zone update from: " + soaResponse.Metadata.NameServer.ToString());
  229. return true;
  230. }
  231. //update available; do zone sync with TCP transport
  232. List<NameServerAddress> tcpNameServers = new List<NameServerAddress>();
  233. foreach (NameServerAddress nameServer in nameServers)
  234. tcpNameServers.Add(nameServer.ChangeProtocol(DnsTransportProtocol.Tcp));
  235. client = new DnsClient(tcpNameServers);
  236. client.Proxy = _dnsServer.Proxy;
  237. client.PreferIPv6 = _dnsServer.PreferIPv6;
  238. client.Timeout = REFRESH_TIMEOUT;
  239. client.Retries = REFRESH_RETRIES;
  240. client.Concurrency = 1;
  241. DnsDatagram nsRequest = new DnsDatagram(0, false, DnsOpcode.StandardQuery, false, false, false, false, false, false, DnsResponseCode.NoError, new DnsQuestionRecord[] { new DnsQuestionRecord(_name, DnsResourceRecordType.NS, DnsClass.IN) });
  242. DnsDatagram nsResponse = await client.RawResolveAsync(nsRequest);
  243. if (nsResponse.RCODE != DnsResponseCode.NoError)
  244. {
  245. _dnsServer.LogManager?.Write("DNS Server received RCODE=" + nsResponse.RCODE.ToString() + " for '" + ToString() + "' Stub zone refresh from: " + nsResponse.Metadata.NameServer.ToString());
  246. return false;
  247. }
  248. if (nsResponse.Answer.Count < 1)
  249. {
  250. _dnsServer.LogManager?.Write("DNS Server received an empty response for NS query for '" + ToString() + "' Stub zone from: " + nsResponse.Metadata.NameServer.ToString());
  251. return false;
  252. }
  253. //prepare sync records
  254. List<DnsResourceRecord> nsRecords = new List<DnsResourceRecord>(nsResponse.Answer.Count);
  255. foreach (DnsResourceRecord record in nsResponse.Answer)
  256. {
  257. if ((record.Type == DnsResourceRecordType.NS) && record.Name.Equals(_name, StringComparison.OrdinalIgnoreCase))
  258. {
  259. record.SyncGlueRecords(nsResponse.Additional);
  260. nsRecords.Add(record);
  261. }
  262. }
  263. receivedSoaRecord.CopyRecordInfoFrom(currentSoaRecord);
  264. //sync records
  265. _entries[DnsResourceRecordType.NS] = nsRecords;
  266. _entries[DnsResourceRecordType.SOA] = [receivedSoaRecord];
  267. _lastModified = DateTime.UtcNow;
  268. _dnsServer.LogManager?.Write("DNS Server successfully refreshed '" + ToString() + "' Stub zone from: " + nsResponse.Metadata.NameServer.ToString());
  269. return true;
  270. }
  271. catch (Exception ex)
  272. {
  273. LogManager log = _dnsServer.LogManager;
  274. if (log != null)
  275. {
  276. string strNameServers = null;
  277. foreach (NameServerAddress nameServer in nameServers)
  278. {
  279. if (strNameServers == null)
  280. strNameServers = nameServer.ToString();
  281. else
  282. strNameServers += ", " + nameServer.ToString();
  283. }
  284. log.Write("DNS Server failed to refresh '" + ToString() + "' Stub zone from: " + strNameServers + "\r\n" + ex.ToString());
  285. }
  286. return false;
  287. }
  288. }
  289. #endregion
  290. #region public
  291. public override string GetZoneTypeName()
  292. {
  293. return "Stub";
  294. }
  295. public void TriggerRefresh(int refreshInterval = REFRESH_TIMER_INTERVAL)
  296. {
  297. if (Disabled)
  298. return;
  299. if (_refreshTimerTriggered)
  300. return;
  301. _refreshTimerTriggered = true;
  302. ResetRefreshTimer(refreshInterval);
  303. }
  304. public void TriggerResync()
  305. {
  306. if (_refreshTimerTriggered)
  307. return;
  308. _resync = true;
  309. _refreshTimerTriggered = true;
  310. ResetRefreshTimer(0);
  311. }
  312. public override void SetRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records)
  313. {
  314. throw new InvalidOperationException("Cannot set records in Stub zone.");
  315. }
  316. public override void AddRecord(DnsResourceRecord record)
  317. {
  318. throw new InvalidOperationException("Cannot add record in Stub zone.");
  319. }
  320. public override bool DeleteRecords(DnsResourceRecordType type)
  321. {
  322. throw new InvalidOperationException("Cannot delete record in Stub zone.");
  323. }
  324. public override bool DeleteRecord(DnsResourceRecordType type, DnsResourceRecordData record)
  325. {
  326. throw new InvalidOperationException("Cannot delete records in Stub zone.");
  327. }
  328. public override void UpdateRecord(DnsResourceRecord oldRecord, DnsResourceRecord newRecord)
  329. {
  330. throw new InvalidOperationException("Cannot update record in Stub zone.");
  331. }
  332. public override IReadOnlyList<DnsResourceRecord> QueryRecords(DnsResourceRecordType type, bool dnssecOk)
  333. {
  334. return []; //stub zone has no authority so cant return any records as query response to allow generating referral response
  335. }
  336. #endregion
  337. #region properties
  338. public override bool Disabled
  339. {
  340. get { return base.Disabled; }
  341. set
  342. {
  343. if (base.Disabled == value)
  344. return;
  345. base.Disabled = value; //set value early to be able to use it for refresh
  346. if (value)
  347. ResetRefreshTimer(Timeout.Infinite);
  348. else
  349. TriggerRefresh();
  350. }
  351. }
  352. public override bool OverrideCatalogZoneTransfer
  353. {
  354. get { throw new InvalidOperationException(); }
  355. set { throw new InvalidOperationException(); }
  356. }
  357. public override bool OverrideCatalogNotify
  358. {
  359. get { throw new InvalidOperationException(); }
  360. set { throw new InvalidOperationException(); }
  361. }
  362. public override AuthZoneQueryAccess QueryAccess
  363. {
  364. get { return base.QueryAccess; }
  365. set
  366. {
  367. switch (value)
  368. {
  369. case AuthZoneQueryAccess.AllowOnlyZoneNameServers:
  370. case AuthZoneQueryAccess.AllowZoneNameServersAndUseSpecifiedNetworkACL:
  371. throw new ArgumentException("The Query Access option is invalid for Stub zones: " + value.ToString(), nameof(QueryAccess));
  372. }
  373. base.QueryAccess = value;
  374. }
  375. }
  376. public override AuthZoneTransfer ZoneTransfer
  377. {
  378. get { return base.ZoneTransfer; }
  379. set { throw new InvalidOperationException(); }
  380. }
  381. public override AuthZoneNotify Notify
  382. {
  383. get { return base.Notify; }
  384. set { throw new InvalidOperationException(); }
  385. }
  386. public override AuthZoneUpdate Update
  387. {
  388. get { return base.Update; }
  389. set { throw new InvalidOperationException(); }
  390. }
  391. public IReadOnlyList<NameServerAddress> PrimaryNameServerAddresses
  392. {
  393. get { return _primaryNameServerAddresses; }
  394. set
  395. {
  396. if ((value is null) || (value.Count == 0))
  397. {
  398. _primaryNameServerAddresses = null;
  399. }
  400. else if (value.Count > byte.MaxValue)
  401. {
  402. throw new ArgumentOutOfRangeException(nameof(PrimaryNameServerAddresses), "Name server addresses cannot have more than 255 entries.");
  403. }
  404. else
  405. {
  406. foreach (NameServerAddress nameServer in value)
  407. {
  408. if (nameServer.Port != 53)
  409. throw new ArgumentException("Name server address must use port 53 for Stub zones.", nameof(PrimaryNameServerAddresses));
  410. }
  411. _primaryNameServerAddresses = value;
  412. }
  413. //update catalog zone property
  414. CatalogZone?.SetPrimaryAddressesProperty(_primaryNameServerAddresses, _name);
  415. }
  416. }
  417. public DateTime Expiry
  418. { get { return _expiry; } }
  419. public bool IsExpired
  420. { get { return _isExpired; } }
  421. public override bool IsActive
  422. {
  423. get { return !Disabled && !_isExpired; }
  424. }
  425. #endregion
  426. }
  427. }