AuthZone.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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;
  19. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  20. namespace DnsServerCore.Dns.Zones
  21. {
  22. abstract class AuthZone : Zone
  23. {
  24. #region variables
  25. bool _disabled;
  26. #endregion
  27. #region constructor
  28. protected AuthZone(AuthZoneInfo zoneInfo)
  29. : base(zoneInfo.Name)
  30. {
  31. _disabled = zoneInfo.Disabled;
  32. }
  33. protected AuthZone(string name)
  34. : base(name)
  35. { }
  36. #endregion
  37. #region private
  38. private IReadOnlyList<DnsResourceRecord> FilterDisabledRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records)
  39. {
  40. if (_disabled)
  41. return Array.Empty<DnsResourceRecord>();
  42. if (records.Count == 1)
  43. {
  44. GenericRecordInfo authRecordInfo = records[0].GetAuthGenericRecordInfo();
  45. if (authRecordInfo.Disabled)
  46. return Array.Empty<DnsResourceRecord>(); //record disabled
  47. //update last used on
  48. authRecordInfo.LastUsedOn = DateTime.UtcNow;
  49. return records;
  50. }
  51. List<DnsResourceRecord> newRecords = new List<DnsResourceRecord>(records.Count);
  52. DateTime utcNow = DateTime.UtcNow;
  53. foreach (DnsResourceRecord record in records)
  54. {
  55. GenericRecordInfo authRecordInfo = record.GetAuthGenericRecordInfo();
  56. if (authRecordInfo.Disabled)
  57. continue; //record disabled
  58. //update last used on
  59. authRecordInfo.LastUsedOn = utcNow;
  60. newRecords.Add(record);
  61. }
  62. if (newRecords.Count > 1)
  63. {
  64. switch (type)
  65. {
  66. case DnsResourceRecordType.A:
  67. case DnsResourceRecordType.AAAA:
  68. case DnsResourceRecordType.NS:
  69. newRecords.Shuffle(); //shuffle records to allow load balancing
  70. break;
  71. }
  72. }
  73. return newRecords;
  74. }
  75. private IReadOnlyList<DnsResourceRecord> AppendRRSigTo(IReadOnlyList<DnsResourceRecord> records)
  76. {
  77. IReadOnlyList<DnsResourceRecord> rrsigRecords = GetRecords(DnsResourceRecordType.RRSIG);
  78. if (rrsigRecords.Count == 0)
  79. return records;
  80. DnsResourceRecordType type = records[0].Type;
  81. List<DnsResourceRecord> newRecords = new List<DnsResourceRecord>(records.Count + 2);
  82. newRecords.AddRange(records);
  83. DateTime utcNow = DateTime.UtcNow;
  84. foreach (DnsResourceRecord rrsigRecord in rrsigRecords)
  85. {
  86. if ((rrsigRecord.RDATA as DnsRRSIGRecordData).TypeCovered == type)
  87. {
  88. rrsigRecord.GetAuthGenericRecordInfo().LastUsedOn = utcNow;
  89. newRecords.Add(rrsigRecord);
  90. }
  91. }
  92. return newRecords;
  93. }
  94. #endregion
  95. #region versioning
  96. internal bool TrySetRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records, out IReadOnlyList<DnsResourceRecord> deletedRecords)
  97. {
  98. switch (type)
  99. {
  100. case DnsResourceRecordType.CNAME:
  101. if ((!_entries.IsEmpty) && !_entries.ContainsKey(DnsResourceRecordType.CNAME))
  102. throw new InvalidOperationException("Cannot add record: a CNAME record cannot exists with other record types for the same name.");
  103. break;
  104. case DnsResourceRecordType.NSEC:
  105. case DnsResourceRecordType.RRSIG:
  106. break; //ignore
  107. default:
  108. if (_entries.ContainsKey(DnsResourceRecordType.CNAME))
  109. throw new InvalidOperationException("Cannot add record: a CNAME record cannot exists with other record types for the same name.");
  110. break;
  111. }
  112. if (_entries.TryGetValue(type, out IReadOnlyList<DnsResourceRecord> existingRecords))
  113. {
  114. deletedRecords = existingRecords;
  115. return _entries.TryUpdate(type, records, existingRecords);
  116. }
  117. else
  118. {
  119. deletedRecords = Array.Empty<DnsResourceRecord>();
  120. return _entries.TryAdd(type, records);
  121. }
  122. }
  123. internal bool TryDeleteRecord(DnsResourceRecordType type, DnsResourceRecordData rdata, out DnsResourceRecord deletedRecord)
  124. {
  125. if (_entries.TryGetValue(type, out IReadOnlyList<DnsResourceRecord> existingRecords))
  126. {
  127. if (existingRecords.Count == 1)
  128. {
  129. if (rdata.Equals(existingRecords[0].RDATA))
  130. {
  131. if (_entries.TryRemove(type, out IReadOnlyList<DnsResourceRecord> removedRecords))
  132. {
  133. deletedRecord = removedRecords[0];
  134. return true;
  135. }
  136. }
  137. }
  138. else
  139. {
  140. deletedRecord = null;
  141. List<DnsResourceRecord> updatedRecords = new List<DnsResourceRecord>(existingRecords.Count);
  142. foreach (DnsResourceRecord existingRecord in existingRecords)
  143. {
  144. if ((deletedRecord is null) && rdata.Equals(existingRecord.RDATA))
  145. deletedRecord = existingRecord;
  146. else
  147. updatedRecords.Add(existingRecord);
  148. }
  149. if (deletedRecord is null)
  150. return false; //not found
  151. return _entries.TryUpdate(type, updatedRecords, existingRecords);
  152. }
  153. }
  154. deletedRecord = null;
  155. return false;
  156. }
  157. internal bool TryDeleteRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records, out IReadOnlyList<DnsResourceRecord> deletedRecords)
  158. {
  159. if (_entries.TryGetValue(type, out IReadOnlyList<DnsResourceRecord> existingRecords))
  160. {
  161. if (existingRecords.Count == 1)
  162. {
  163. DnsResourceRecord existingRecord = existingRecords[0];
  164. foreach (DnsResourceRecord record in records)
  165. {
  166. if (record.RDATA.Equals(existingRecord.RDATA))
  167. {
  168. if (_entries.TryRemove(type, out IReadOnlyList<DnsResourceRecord> removedRecords))
  169. {
  170. deletedRecords = removedRecords;
  171. return true;
  172. }
  173. }
  174. }
  175. }
  176. else
  177. {
  178. List<DnsResourceRecord> deleted = new List<DnsResourceRecord>(records.Count);
  179. List<DnsResourceRecord> updatedRecords = new List<DnsResourceRecord>(existingRecords.Count);
  180. foreach (DnsResourceRecord existingRecord in existingRecords)
  181. {
  182. bool found = false;
  183. foreach (DnsResourceRecord record in records)
  184. {
  185. if (record.RDATA.Equals(existingRecord.RDATA))
  186. {
  187. found = true;
  188. break;
  189. }
  190. }
  191. if (found)
  192. deleted.Add(existingRecord);
  193. else
  194. updatedRecords.Add(existingRecord);
  195. }
  196. if (deleted.Count > 0)
  197. {
  198. deletedRecords = deleted;
  199. if (updatedRecords.Count > 0)
  200. return _entries.TryUpdate(type, updatedRecords, existingRecords);
  201. return _entries.TryRemove(type, out _);
  202. }
  203. }
  204. }
  205. deletedRecords = null;
  206. return false;
  207. }
  208. internal void AddOrUpdateRRSigRecords(IReadOnlyList<DnsResourceRecord> newRRSigRecords, out IReadOnlyList<DnsResourceRecord> deletedRRSigRecords)
  209. {
  210. IReadOnlyList<DnsResourceRecord> deleted = null;
  211. _entries.AddOrUpdate(DnsResourceRecordType.RRSIG, delegate (DnsResourceRecordType key)
  212. {
  213. deleted = Array.Empty<DnsResourceRecord>();
  214. return newRRSigRecords;
  215. },
  216. delegate (DnsResourceRecordType key, IReadOnlyList<DnsResourceRecord> existingRecords)
  217. {
  218. List<DnsResourceRecord> updatedRecords = new List<DnsResourceRecord>(existingRecords.Count + newRRSigRecords.Count);
  219. List<DnsResourceRecord> deletedRecords = new List<DnsResourceRecord>();
  220. foreach (DnsResourceRecord existingRecord in existingRecords)
  221. {
  222. bool found = false;
  223. DnsRRSIGRecordData existingRRSig = existingRecord.RDATA as DnsRRSIGRecordData;
  224. foreach (DnsResourceRecord newRRSigRecord in newRRSigRecords)
  225. {
  226. DnsRRSIGRecordData newRRSig = newRRSigRecord.RDATA as DnsRRSIGRecordData;
  227. if ((newRRSig.TypeCovered == existingRRSig.TypeCovered) && (newRRSig.KeyTag == existingRRSig.KeyTag))
  228. {
  229. deletedRecords.Add(existingRecord);
  230. found = true;
  231. break;
  232. }
  233. }
  234. if (!found)
  235. updatedRecords.Add(existingRecord);
  236. }
  237. updatedRecords.AddRange(newRRSigRecords);
  238. deleted = deletedRecords;
  239. return updatedRecords;
  240. });
  241. deletedRRSigRecords = deleted;
  242. }
  243. internal void AddRecord(DnsResourceRecord record, out IReadOnlyList<DnsResourceRecord> addedRecords, out IReadOnlyList<DnsResourceRecord> deletedRecords)
  244. {
  245. switch (record.Type)
  246. {
  247. case DnsResourceRecordType.CNAME:
  248. case DnsResourceRecordType.DNAME:
  249. case DnsResourceRecordType.SOA:
  250. throw new InvalidOperationException("Cannot add record: use SetRecords() for " + record.Type.ToString() + " record.");
  251. default:
  252. if (_entries.ContainsKey(DnsResourceRecordType.CNAME))
  253. throw new InvalidOperationException("Cannot add record: a CNAME record cannot exists with other record types for the same name.");
  254. break;
  255. }
  256. List<DnsResourceRecord> added = new List<DnsResourceRecord>();
  257. List<DnsResourceRecord> deleted = new List<DnsResourceRecord>();
  258. addedRecords = added;
  259. deletedRecords = deleted;
  260. _entries.AddOrUpdate(record.Type, delegate (DnsResourceRecordType key)
  261. {
  262. added.Add(record);
  263. return new DnsResourceRecord[] { record };
  264. },
  265. delegate (DnsResourceRecordType key, IReadOnlyList<DnsResourceRecord> existingRecords)
  266. {
  267. foreach (DnsResourceRecord existingRecord in existingRecords)
  268. {
  269. if (record.RDATA.Equals(existingRecord.RDATA))
  270. return existingRecords;
  271. }
  272. List<DnsResourceRecord> updatedRecords = new List<DnsResourceRecord>(existingRecords.Count + 1);
  273. foreach (DnsResourceRecord existingRecord in existingRecords)
  274. {
  275. if (existingRecord.OriginalTtlValue == record.OriginalTtlValue)
  276. {
  277. updatedRecords.Add(existingRecord);
  278. }
  279. else
  280. {
  281. DnsResourceRecord updatedExistingRecord = new DnsResourceRecord(existingRecord.Name, existingRecord.Type, existingRecord.Class, record.OriginalTtlValue, existingRecord.RDATA);
  282. updatedRecords.Add(updatedExistingRecord);
  283. added.Add(updatedExistingRecord);
  284. deleted.Add(existingRecord);
  285. }
  286. }
  287. updatedRecords.Add(record);
  288. added.Add(record);
  289. return updatedRecords;
  290. });
  291. }
  292. #endregion
  293. #region catalog zones
  294. protected IEnumerable<KeyValuePair<string, string>> EnumerateCatalogMemberZones(DnsServer dnsServer)
  295. {
  296. List<string> subDomains = new List<string>();
  297. dnsServer.AuthZoneManager.ListSubDomains("zones." + _name, subDomains);
  298. foreach (string subDomain in subDomains)
  299. {
  300. IReadOnlyList<DnsResourceRecord> ptrRecords = dnsServer.AuthZoneManager.GetRecords(_name, subDomain + ".zones." + _name, DnsResourceRecordType.PTR);
  301. if (ptrRecords.Count > 0)
  302. yield return new KeyValuePair<string, string>((ptrRecords[0].RDATA as DnsPTRRecordData).Domain, ptrRecords[0].Name);
  303. }
  304. }
  305. #endregion
  306. #region DNSSEC
  307. internal IReadOnlyList<DnsResourceRecord> SignAllRRSets()
  308. {
  309. List<DnsResourceRecord> rrsigRecords = new List<DnsResourceRecord>(_entries.Count);
  310. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> entry in _entries)
  311. {
  312. if (entry.Key == DnsResourceRecordType.RRSIG)
  313. continue;
  314. rrsigRecords.AddRange(SignRRSet(entry.Value));
  315. }
  316. return rrsigRecords;
  317. }
  318. internal IReadOnlyList<DnsResourceRecord> RemoveAllDnssecRecords()
  319. {
  320. List<DnsResourceRecord> allRemovedRecords = new List<DnsResourceRecord>();
  321. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> entry in _entries)
  322. {
  323. switch (entry.Key)
  324. {
  325. case DnsResourceRecordType.DNSKEY:
  326. case DnsResourceRecordType.RRSIG:
  327. case DnsResourceRecordType.NSEC:
  328. case DnsResourceRecordType.NSEC3PARAM:
  329. case DnsResourceRecordType.NSEC3:
  330. if (_entries.TryRemove(entry.Key, out IReadOnlyList<DnsResourceRecord> removedRecords))
  331. allRemovedRecords.AddRange(removedRecords);
  332. break;
  333. }
  334. }
  335. return allRemovedRecords;
  336. }
  337. internal IReadOnlyList<DnsResourceRecord> RemoveNSecRecordsWithRRSig()
  338. {
  339. List<DnsResourceRecord> allRemovedRecords = new List<DnsResourceRecord>(2);
  340. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> entry in _entries)
  341. {
  342. switch (entry.Key)
  343. {
  344. case DnsResourceRecordType.NSEC:
  345. if (_entries.TryRemove(entry.Key, out IReadOnlyList<DnsResourceRecord> removedRecords))
  346. allRemovedRecords.AddRange(removedRecords);
  347. break;
  348. case DnsResourceRecordType.RRSIG:
  349. List<DnsResourceRecord> recordsToRemove = new List<DnsResourceRecord>(1);
  350. foreach (DnsResourceRecord rrsigRecord in entry.Value)
  351. {
  352. DnsRRSIGRecordData rrsig = rrsigRecord.RDATA as DnsRRSIGRecordData;
  353. if (rrsig.TypeCovered == DnsResourceRecordType.NSEC)
  354. recordsToRemove.Add(rrsigRecord);
  355. }
  356. if (recordsToRemove.Count > 0)
  357. {
  358. if (TryDeleteRecords(DnsResourceRecordType.RRSIG, recordsToRemove, out IReadOnlyList<DnsResourceRecord> deletedRecords))
  359. allRemovedRecords.AddRange(deletedRecords);
  360. }
  361. break;
  362. }
  363. }
  364. return allRemovedRecords;
  365. }
  366. internal IReadOnlyList<DnsResourceRecord> RemoveNSec3RecordsWithRRSig()
  367. {
  368. List<DnsResourceRecord> allRemovedRecords = new List<DnsResourceRecord>(2);
  369. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> entry in _entries)
  370. {
  371. switch (entry.Key)
  372. {
  373. case DnsResourceRecordType.NSEC3:
  374. case DnsResourceRecordType.NSEC3PARAM:
  375. if (_entries.TryRemove(entry.Key, out IReadOnlyList<DnsResourceRecord> removedRecords))
  376. allRemovedRecords.AddRange(removedRecords);
  377. break;
  378. case DnsResourceRecordType.RRSIG:
  379. List<DnsResourceRecord> recordsToRemove = new List<DnsResourceRecord>(1);
  380. foreach (DnsResourceRecord rrsigRecord in entry.Value)
  381. {
  382. DnsRRSIGRecordData rrsig = rrsigRecord.RDATA as DnsRRSIGRecordData;
  383. switch (rrsig.TypeCovered)
  384. {
  385. case DnsResourceRecordType.NSEC3:
  386. case DnsResourceRecordType.NSEC3PARAM:
  387. recordsToRemove.Add(rrsigRecord);
  388. break;
  389. }
  390. }
  391. if (recordsToRemove.Count > 0)
  392. {
  393. if (TryDeleteRecords(DnsResourceRecordType.RRSIG, recordsToRemove, out IReadOnlyList<DnsResourceRecord> deletedRecords))
  394. allRemovedRecords.AddRange(deletedRecords);
  395. }
  396. break;
  397. }
  398. }
  399. return allRemovedRecords;
  400. }
  401. internal bool HasOnlyNSec3Records()
  402. {
  403. if (!_entries.ContainsKey(DnsResourceRecordType.NSEC3))
  404. return false;
  405. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> entry in _entries)
  406. {
  407. switch (entry.Key)
  408. {
  409. case DnsResourceRecordType.NSEC3:
  410. case DnsResourceRecordType.RRSIG:
  411. break;
  412. default:
  413. //found non NSEC3 records
  414. return false;
  415. }
  416. }
  417. return true;
  418. }
  419. internal IReadOnlyList<DnsResourceRecord> RefreshSignatures()
  420. {
  421. if (!_entries.TryGetValue(DnsResourceRecordType.RRSIG, out IReadOnlyList<DnsResourceRecord> rrsigRecords))
  422. {
  423. if ((_entries.Count == 1) && _entries.TryGetValue(DnsResourceRecordType.NS, out _))
  424. return Array.Empty<DnsResourceRecord>(); //delegation NS records are not signed
  425. throw new InvalidOperationException();
  426. }
  427. List<DnsResourceRecordType> typesToRefresh = new List<DnsResourceRecordType>();
  428. DateTime utcNow = DateTime.UtcNow;
  429. foreach (DnsResourceRecord rrsigRecord in rrsigRecords)
  430. {
  431. DnsRRSIGRecordData rrsig = rrsigRecord.RDATA as DnsRRSIGRecordData;
  432. uint signatureValidityPeriod = rrsig.SignatureExpiration - rrsig.SignatureInception;
  433. uint refreshPeriod = signatureValidityPeriod / 3;
  434. if (utcNow > DateTime.UnixEpoch.AddSeconds(rrsig.SignatureExpiration - refreshPeriod))
  435. typesToRefresh.Add(rrsig.TypeCovered);
  436. }
  437. List<DnsResourceRecord> newRRSigRecords = new List<DnsResourceRecord>(typesToRefresh.Count);
  438. foreach (DnsResourceRecordType type in typesToRefresh)
  439. {
  440. if (_entries.TryGetValue(type, out IReadOnlyList<DnsResourceRecord> records))
  441. newRRSigRecords.AddRange(SignRRSet(records));
  442. }
  443. return newRRSigRecords;
  444. }
  445. internal virtual IReadOnlyList<DnsResourceRecord> SignRRSet(IReadOnlyList<DnsResourceRecord> records)
  446. {
  447. throw new NotImplementedException();
  448. }
  449. internal IReadOnlyList<DnsResourceRecord> GetUpdatedNSecRRSet(string nextDomainName, uint ttl)
  450. {
  451. List<DnsResourceRecordType> types = new List<DnsResourceRecordType>(_entries.Count);
  452. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> entry in _entries)
  453. types.Add(entry.Key);
  454. if (!types.Contains(DnsResourceRecordType.NSEC))
  455. {
  456. types.Add(DnsResourceRecordType.NSEC);
  457. if (!types.Contains(DnsResourceRecordType.RRSIG))
  458. types.Add(DnsResourceRecordType.RRSIG);
  459. }
  460. types.Sort();
  461. DnsNSECRecordData newNSecRecord = new DnsNSECRecordData(nextDomainName, types);
  462. if (!_entries.TryGetValue(DnsResourceRecordType.NSEC, out IReadOnlyList<DnsResourceRecord> existingRecords) || (existingRecords[0].TTL != ttl) || !existingRecords[0].RDATA.Equals(newNSecRecord))
  463. return new DnsResourceRecord[] { new DnsResourceRecord(_name, DnsResourceRecordType.NSEC, DnsClass.IN, ttl, newNSecRecord) };
  464. return Array.Empty<DnsResourceRecord>();
  465. }
  466. internal IReadOnlyList<DnsResourceRecord> GetUpdatedNSec3RRSet(IReadOnlyList<DnsResourceRecord> newNSec3Records)
  467. {
  468. if (!_entries.TryGetValue(DnsResourceRecordType.NSEC3, out IReadOnlyList<DnsResourceRecord> existingRecords) || (existingRecords[0].TTL != newNSec3Records[0].TTL) || !existingRecords[0].RDATA.Equals(newNSec3Records[0].RDATA))
  469. return newNSec3Records;
  470. return Array.Empty<DnsResourceRecord>();
  471. }
  472. internal IReadOnlyList<DnsResourceRecord> CreateNSec3RRSet(string hashedOwnerName, byte[] nextHashedOwnerName, uint ttl, ushort iterations, byte[] salt)
  473. {
  474. List<DnsResourceRecordType> types = new List<DnsResourceRecordType>(_entries.Count);
  475. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> entry in _entries)
  476. {
  477. switch (entry.Key)
  478. {
  479. case DnsResourceRecordType.NSEC3:
  480. //rare case when there is a record created at the same name as that of an existing NSEC3
  481. continue;
  482. default:
  483. types.Add(entry.Key);
  484. break;
  485. }
  486. }
  487. types.Sort();
  488. DnsNSEC3RecordData newNSec3 = new DnsNSEC3RecordData(DnssecNSEC3HashAlgorithm.SHA1, DnssecNSEC3Flags.None, iterations, salt, nextHashedOwnerName, types);
  489. return new DnsResourceRecord[] { new DnsResourceRecord(hashedOwnerName, DnsResourceRecordType.NSEC3, DnsClass.IN, ttl, newNSec3) };
  490. }
  491. internal DnsResourceRecord GetPartialNSec3Record(string zoneName, uint ttl, ushort iterations, byte[] salt)
  492. {
  493. List<DnsResourceRecordType> types = new List<DnsResourceRecordType>(_entries.Count);
  494. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> entry in _entries)
  495. {
  496. switch (entry.Key)
  497. {
  498. case DnsResourceRecordType.NSEC3:
  499. //rare case when there is a record created at the same name as that of an existing NSEC3
  500. continue;
  501. default:
  502. types.Add(entry.Key);
  503. break;
  504. }
  505. }
  506. if (_name.Equals(zoneName, StringComparison.OrdinalIgnoreCase))
  507. {
  508. if (!types.Contains(DnsResourceRecordType.NSEC3PARAM))
  509. types.Add(DnsResourceRecordType.NSEC3PARAM); //add NSEC3PARAM type to NSEC3 for unsigned zone apex
  510. }
  511. types.Sort();
  512. DnsNSEC3RecordData newNSec3Record = new DnsNSEC3RecordData(DnssecNSEC3HashAlgorithm.SHA1, DnssecNSEC3Flags.None, iterations, salt, Array.Empty<byte>(), types);
  513. return new DnsResourceRecord(newNSec3Record.ComputeHashedOwnerName(_name) + (zoneName.Length > 0 ? "." + zoneName : ""), DnsResourceRecordType.NSEC3, DnsClass.IN, ttl, newNSec3Record);
  514. }
  515. #endregion
  516. #region public
  517. public void SyncRecords(Dictionary<DnsResourceRecordType, List<DnsResourceRecord>> newEntries)
  518. {
  519. //remove entires of type that do not exists in new entries
  520. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> entry in _entries)
  521. {
  522. if (!newEntries.ContainsKey(entry.Key))
  523. _entries.TryRemove(entry.Key, out _);
  524. }
  525. //set new entries into zone
  526. if (this is ForwarderZone)
  527. {
  528. //skip NS and SOA records from being added to ForwarderZone
  529. foreach (KeyValuePair<DnsResourceRecordType, List<DnsResourceRecord>> newEntry in newEntries)
  530. {
  531. switch (newEntry.Key)
  532. {
  533. case DnsResourceRecordType.NS:
  534. case DnsResourceRecordType.SOA:
  535. break;
  536. default:
  537. _entries[newEntry.Key] = newEntry.Value;
  538. break;
  539. }
  540. }
  541. }
  542. else
  543. {
  544. foreach (KeyValuePair<DnsResourceRecordType, List<DnsResourceRecord>> newEntry in newEntries)
  545. {
  546. if (newEntry.Key == DnsResourceRecordType.SOA)
  547. {
  548. if (newEntry.Value.Count != 1)
  549. continue; //skip invalid SOA record
  550. if (this is SecondaryZone)
  551. {
  552. //copy existing SOA record's info to new SOA record
  553. DnsResourceRecord existingSoaRecord = _entries[DnsResourceRecordType.SOA][0];
  554. DnsResourceRecord newSoaRecord = newEntry.Value[0];
  555. newSoaRecord.CopyRecordInfoFrom(existingSoaRecord);
  556. }
  557. }
  558. _entries[newEntry.Key] = newEntry.Value;
  559. }
  560. }
  561. }
  562. public void SyncRecords(Dictionary<DnsResourceRecordType, List<DnsResourceRecord>> deletedEntries, Dictionary<DnsResourceRecordType, List<DnsResourceRecord>> addedEntries)
  563. {
  564. if (deletedEntries is not null)
  565. {
  566. foreach (KeyValuePair<DnsResourceRecordType, List<DnsResourceRecord>> deletedEntry in deletedEntries)
  567. {
  568. if (_entries.TryGetValue(deletedEntry.Key, out IReadOnlyList<DnsResourceRecord> existingRecords))
  569. {
  570. List<DnsResourceRecord> updatedRecords = new List<DnsResourceRecord>(Math.Max(0, existingRecords.Count - deletedEntry.Value.Count));
  571. foreach (DnsResourceRecord existingRecord in existingRecords)
  572. {
  573. bool deleted = false;
  574. foreach (DnsResourceRecord deletedRecord in deletedEntry.Value)
  575. {
  576. if (existingRecord.RDATA.Equals(deletedRecord.RDATA))
  577. {
  578. deleted = true;
  579. break;
  580. }
  581. }
  582. if (!deleted)
  583. updatedRecords.Add(existingRecord);
  584. }
  585. if (existingRecords.Count > updatedRecords.Count)
  586. {
  587. if (updatedRecords.Count > 0)
  588. _entries[deletedEntry.Key] = updatedRecords;
  589. else
  590. _entries.TryRemove(deletedEntry.Key, out _);
  591. }
  592. }
  593. }
  594. }
  595. if (addedEntries is not null)
  596. {
  597. foreach (KeyValuePair<DnsResourceRecordType, List<DnsResourceRecord>> addedEntry in addedEntries)
  598. {
  599. _entries.AddOrUpdate(addedEntry.Key, addedEntry.Value, delegate (DnsResourceRecordType key, IReadOnlyList<DnsResourceRecord> existingRecords)
  600. {
  601. List<DnsResourceRecord> updatedRecords = new List<DnsResourceRecord>(existingRecords.Count + addedEntry.Value.Count);
  602. updatedRecords.AddRange(existingRecords);
  603. foreach (DnsResourceRecord addedRecord in addedEntry.Value)
  604. {
  605. bool exists = false;
  606. foreach (DnsResourceRecord existingRecord in existingRecords)
  607. {
  608. if (addedRecord.RDATA.Equals(existingRecord.RDATA))
  609. {
  610. exists = true;
  611. break;
  612. }
  613. }
  614. if (!exists)
  615. updatedRecords.Add(addedRecord);
  616. }
  617. if (updatedRecords.Count > existingRecords.Count)
  618. return updatedRecords;
  619. else
  620. return existingRecords;
  621. });
  622. }
  623. }
  624. }
  625. public void SyncGlueRecords(IReadOnlyCollection<DnsResourceRecord> deletedGlueRecords, IReadOnlyCollection<DnsResourceRecord> addedGlueRecords)
  626. {
  627. if (_entries.TryGetValue(DnsResourceRecordType.NS, out IReadOnlyList<DnsResourceRecord> nsRecords))
  628. {
  629. foreach (DnsResourceRecord nsRecord in nsRecords)
  630. nsRecord.SyncGlueRecords(deletedGlueRecords, addedGlueRecords);
  631. }
  632. }
  633. public void LoadRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records)
  634. {
  635. _entries[type] = records;
  636. }
  637. public virtual void SetRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records)
  638. {
  639. switch (type)
  640. {
  641. case DnsResourceRecordType.CNAME:
  642. if ((!_entries.IsEmpty) && !_entries.ContainsKey(DnsResourceRecordType.CNAME))
  643. throw new InvalidOperationException("Cannot add record: a CNAME record cannot exists with other record types for the same name.");
  644. break;
  645. case DnsResourceRecordType.NSEC:
  646. case DnsResourceRecordType.RRSIG:
  647. break; //ignore
  648. default:
  649. if (_entries.ContainsKey(DnsResourceRecordType.CNAME))
  650. throw new InvalidOperationException("Cannot add record: a CNAME record cannot exists with other record types for the same name.");
  651. break;
  652. }
  653. _entries[type] = records;
  654. }
  655. public virtual void AddRecord(DnsResourceRecord record)
  656. {
  657. AddRecord(record, out _, out _);
  658. }
  659. public virtual bool DeleteRecords(DnsResourceRecordType type)
  660. {
  661. return _entries.TryRemove(type, out _);
  662. }
  663. public virtual bool DeleteRecord(DnsResourceRecordType type, DnsResourceRecordData rdata)
  664. {
  665. return TryDeleteRecord(type, rdata, out _);
  666. }
  667. public virtual void UpdateRecord(DnsResourceRecord oldRecord, DnsResourceRecord newRecord)
  668. {
  669. if (oldRecord.Type == DnsResourceRecordType.SOA)
  670. throw new InvalidOperationException("Cannot update record: use SetRecords() for " + oldRecord.Type.ToString() + " record");
  671. if (oldRecord.Type != newRecord.Type)
  672. throw new InvalidOperationException("Old and new record types do not match.");
  673. if (!DeleteRecord(oldRecord.Type, oldRecord.RDATA))
  674. throw new DnsWebServiceException("Cannot update record: the old record does not exists.");
  675. AddRecord(newRecord);
  676. }
  677. public virtual IReadOnlyList<DnsResourceRecord> QueryRecords(DnsResourceRecordType type, bool dnssecOk)
  678. {
  679. switch (type)
  680. {
  681. case DnsResourceRecordType.APP:
  682. case DnsResourceRecordType.FWD:
  683. case DnsResourceRecordType.NSEC:
  684. case DnsResourceRecordType.NSEC3:
  685. {
  686. //return only exact type if exists
  687. if (_entries.TryGetValue(type, out IReadOnlyList<DnsResourceRecord> existingRecords))
  688. {
  689. IReadOnlyList<DnsResourceRecord> filteredRecords = FilterDisabledRecords(type, existingRecords);
  690. if (filteredRecords.Count > 0)
  691. {
  692. if (dnssecOk)
  693. return AppendRRSigTo(filteredRecords);
  694. return filteredRecords;
  695. }
  696. }
  697. }
  698. break;
  699. case DnsResourceRecordType.ANY:
  700. List<DnsResourceRecord> records = new List<DnsResourceRecord>(_entries.Count * 2);
  701. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> entry in _entries)
  702. {
  703. switch (entry.Key)
  704. {
  705. case DnsResourceRecordType.FWD:
  706. case DnsResourceRecordType.APP:
  707. //skip records
  708. continue;
  709. default:
  710. records.AddRange(entry.Value);
  711. break;
  712. }
  713. }
  714. return FilterDisabledRecords(type, records);
  715. default:
  716. {
  717. //check for CNAME
  718. if (_entries.TryGetValue(DnsResourceRecordType.CNAME, out IReadOnlyList<DnsResourceRecord> existingCNAMERecords))
  719. {
  720. IReadOnlyList<DnsResourceRecord> filteredRecords = FilterDisabledRecords(type, existingCNAMERecords);
  721. if (filteredRecords.Count > 0)
  722. {
  723. if (dnssecOk)
  724. return AppendRRSigTo(filteredRecords);
  725. return filteredRecords;
  726. }
  727. }
  728. //check for exact type
  729. if (_entries.TryGetValue(type, out IReadOnlyList<DnsResourceRecord> existingRecords))
  730. {
  731. IReadOnlyList<DnsResourceRecord> filteredRecords = FilterDisabledRecords(type, existingRecords);
  732. if (filteredRecords.Count > 0)
  733. {
  734. if (dnssecOk)
  735. return AppendRRSigTo(filteredRecords);
  736. return filteredRecords;
  737. }
  738. }
  739. //check special processing
  740. switch (type)
  741. {
  742. case DnsResourceRecordType.A:
  743. case DnsResourceRecordType.AAAA:
  744. //check for ANAME
  745. if (_entries.TryGetValue(DnsResourceRecordType.ANAME, out IReadOnlyList<DnsResourceRecord> anameRecords))
  746. return FilterDisabledRecords(type, anameRecords);
  747. //check for ALIAS
  748. if (_entries.TryGetValue(DnsResourceRecordType.ALIAS, out IReadOnlyList<DnsResourceRecord> aliasRecords))
  749. {
  750. List<DnsResourceRecord> newAliasRecords = new List<DnsResourceRecord>(aliasRecords.Count);
  751. foreach (DnsResourceRecord aliasRecord in aliasRecords)
  752. {
  753. if ((aliasRecord.RDATA is DnsALIASRecordData alias) && (alias.Type == type))
  754. newAliasRecords.Add(aliasRecord);
  755. }
  756. if (newAliasRecords.Count > 0)
  757. return FilterDisabledRecords(type, newAliasRecords);
  758. }
  759. break;
  760. }
  761. }
  762. break;
  763. }
  764. return Array.Empty<DnsResourceRecord>();
  765. }
  766. public IReadOnlyList<DnsResourceRecord> QueryRecordsWildcard(DnsResourceRecordType type, bool dnssecOk, string queryDomain)
  767. {
  768. IReadOnlyList<DnsResourceRecord> answers = QueryRecords(type, dnssecOk);
  769. if ((answers.Count > 0) && _name.StartsWith('*') && !_name.Equals(queryDomain, StringComparison.OrdinalIgnoreCase))
  770. {
  771. //wildcard zone; generate new answer records
  772. DnsResourceRecord[] wildcardAnswers = new DnsResourceRecord[answers.Count];
  773. for (int i = 0; i < answers.Count; i++)
  774. wildcardAnswers[i] = new DnsResourceRecord(queryDomain, answers[i].Type, answers[i].Class, answers[i].TTL, answers[i].RDATA) { Tag = answers[i].Tag };
  775. answers = wildcardAnswers;
  776. }
  777. return answers;
  778. }
  779. public IReadOnlyList<DnsResourceRecord> GetRecords(DnsResourceRecordType type)
  780. {
  781. if (_entries.TryGetValue(type, out IReadOnlyList<DnsResourceRecord> records))
  782. return records;
  783. return Array.Empty<DnsResourceRecord>();
  784. }
  785. public override bool ContainsNameServerRecords()
  786. {
  787. if (!_entries.TryGetValue(DnsResourceRecordType.NS, out IReadOnlyList<DnsResourceRecord> records))
  788. return false;
  789. foreach (DnsResourceRecord record in records)
  790. {
  791. if (record.GetAuthGenericRecordInfo().Disabled)
  792. continue;
  793. return true;
  794. }
  795. return false;
  796. }
  797. #endregion
  798. #region properties
  799. public IReadOnlyDictionary<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> Entries
  800. { get { return _entries; } }
  801. public virtual bool Disabled
  802. {
  803. get { return _disabled; }
  804. set { _disabled = value; }
  805. }
  806. public virtual bool IsActive
  807. {
  808. get { return !_disabled; }
  809. }
  810. #endregion
  811. }
  812. }