AuthRecordInfo.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Net;
  19. using TechnitiumLibrary.IO;
  20. using TechnitiumLibrary.Net.Dns;
  21. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  22. namespace DnsServerCore.Dns.ResourceRecords
  23. {
  24. class AuthRecordInfo
  25. {
  26. #region variables
  27. public static readonly AuthRecordInfo Default = new AuthRecordInfo();
  28. bool _disabled;
  29. IReadOnlyList<DnsResourceRecord> _glueRecords;
  30. string _comments;
  31. DateTime _deletedOn;
  32. IReadOnlyList<NameServerAddress> _primaryNameServers;
  33. DnsTransportProtocol _zoneTransferProtocol;
  34. string _tsigKeyName = string.Empty;
  35. bool _useSoaSerialDateScheme;
  36. DateTime _lastUsedOn; //not serialized
  37. #endregion
  38. #region constructor
  39. public AuthRecordInfo()
  40. { }
  41. public AuthRecordInfo(BinaryReader bR, bool isSoa)
  42. {
  43. byte version = bR.ReadByte();
  44. switch (version)
  45. {
  46. case 1:
  47. _disabled = bR.ReadBoolean();
  48. break;
  49. case 2:
  50. case 3:
  51. case 4:
  52. case 5:
  53. case 6:
  54. case 7:
  55. case 8:
  56. _disabled = bR.ReadBoolean();
  57. if ((version < 5) && isSoa)
  58. {
  59. //read old glue records as NameServerAddress in case of SOA record
  60. int count = bR.ReadByte();
  61. if (count > 0)
  62. {
  63. NameServerAddress[] primaryNameServers = new NameServerAddress[count];
  64. for (int i = 0; i < primaryNameServers.Length; i++)
  65. {
  66. DnsResourceRecord glueRecord = new DnsResourceRecord(bR.BaseStream);
  67. IPAddress address;
  68. switch (glueRecord.Type)
  69. {
  70. case DnsResourceRecordType.A:
  71. address = (glueRecord.RDATA as DnsARecordData).Address;
  72. break;
  73. case DnsResourceRecordType.AAAA:
  74. address = (glueRecord.RDATA as DnsAAAARecordData).Address;
  75. break;
  76. default:
  77. continue;
  78. }
  79. primaryNameServers[i] = new NameServerAddress(address);
  80. }
  81. _primaryNameServers = primaryNameServers;
  82. }
  83. }
  84. else
  85. {
  86. int count = bR.ReadByte();
  87. if (count > 0)
  88. {
  89. DnsResourceRecord[] glueRecords = new DnsResourceRecord[count];
  90. for (int i = 0; i < glueRecords.Length; i++)
  91. glueRecords[i] = new DnsResourceRecord(bR.BaseStream);
  92. _glueRecords = glueRecords;
  93. }
  94. }
  95. if (version >= 3)
  96. _comments = bR.ReadShortString();
  97. if (version >= 4)
  98. _deletedOn = bR.ReadDateTime();
  99. if (version >= 5)
  100. {
  101. int count = bR.ReadByte();
  102. if (count > 0)
  103. {
  104. NameServerAddress[] primaryNameServers = new NameServerAddress[count];
  105. for (int i = 0; i < primaryNameServers.Length; i++)
  106. primaryNameServers[i] = new NameServerAddress(bR);
  107. _primaryNameServers = primaryNameServers;
  108. }
  109. }
  110. if (version >= 7)
  111. {
  112. _zoneTransferProtocol = (DnsTransportProtocol)bR.ReadByte();
  113. _tsigKeyName = bR.ReadShortString();
  114. }
  115. else if (version >= 6)
  116. {
  117. _zoneTransferProtocol = (DnsTransportProtocol)bR.ReadByte();
  118. _tsigKeyName = bR.ReadShortString();
  119. _ = bR.ReadShortString(); //_tsigSharedSecret (obsolete)
  120. _ = bR.ReadShortString(); //_tsigAlgorithm (obsolete)
  121. }
  122. if (version >= 8)
  123. _useSoaSerialDateScheme = bR.ReadBoolean();
  124. break;
  125. default:
  126. throw new InvalidDataException("AuthRecordInfo format version not supported.");
  127. }
  128. }
  129. #endregion
  130. #region public
  131. public void WriteTo(BinaryWriter bW)
  132. {
  133. bW.Write((byte)8); //version
  134. bW.Write(_disabled);
  135. if (_glueRecords is null)
  136. {
  137. bW.Write((byte)0);
  138. }
  139. else
  140. {
  141. bW.Write(Convert.ToByte(_glueRecords.Count));
  142. foreach (DnsResourceRecord glueRecord in _glueRecords)
  143. glueRecord.WriteTo(bW.BaseStream);
  144. }
  145. if (string.IsNullOrEmpty(_comments))
  146. bW.Write((byte)0);
  147. else
  148. bW.WriteShortString(_comments);
  149. bW.Write(_deletedOn);
  150. if (_primaryNameServers is null)
  151. {
  152. bW.Write((byte)0);
  153. }
  154. else
  155. {
  156. bW.Write(Convert.ToByte(_primaryNameServers.Count));
  157. foreach (NameServerAddress nameServer in _primaryNameServers)
  158. nameServer.WriteTo(bW);
  159. }
  160. bW.Write((byte)_zoneTransferProtocol);
  161. bW.WriteShortString(_tsigKeyName);
  162. bW.Write(_useSoaSerialDateScheme);
  163. }
  164. #endregion
  165. #region properties
  166. public bool Disabled
  167. {
  168. get { return _disabled; }
  169. set { _disabled = value; }
  170. }
  171. public IReadOnlyList<DnsResourceRecord> GlueRecords
  172. {
  173. get { return _glueRecords; }
  174. set
  175. {
  176. if ((value is null) || (value.Count == 0))
  177. _glueRecords = null;
  178. else
  179. _glueRecords = value;
  180. }
  181. }
  182. public string Comments
  183. {
  184. get { return _comments; }
  185. set
  186. {
  187. if ((value is not null) && (value.Length > 255))
  188. throw new ArgumentOutOfRangeException(nameof(Comments), "Resource record comment text cannot exceed 255 characters.");
  189. _comments = value;
  190. }
  191. }
  192. public DateTime DeletedOn
  193. {
  194. get { return _deletedOn; }
  195. set { _deletedOn = value; }
  196. }
  197. public IReadOnlyList<NameServerAddress> PrimaryNameServers
  198. {
  199. get { return _primaryNameServers; }
  200. set
  201. {
  202. if ((value is null) || (value.Count == 0))
  203. _primaryNameServers = null;
  204. else
  205. _primaryNameServers = value;
  206. }
  207. }
  208. public DnsTransportProtocol ZoneTransferProtocol
  209. {
  210. get { return _zoneTransferProtocol; }
  211. set
  212. {
  213. switch (value)
  214. {
  215. case DnsTransportProtocol.Tcp:
  216. case DnsTransportProtocol.Tls:
  217. case DnsTransportProtocol.Quic:
  218. _zoneTransferProtocol = value;
  219. break;
  220. default:
  221. throw new NotSupportedException("Zone transfer protocol is not supported: XFR-over-" + value.ToString().ToUpper());
  222. }
  223. }
  224. }
  225. public string TsigKeyName
  226. {
  227. get { return _tsigKeyName; }
  228. set
  229. {
  230. if (value is null)
  231. _tsigKeyName = string.Empty;
  232. else
  233. _tsigKeyName = value;
  234. }
  235. }
  236. public bool UseSoaSerialDateScheme
  237. {
  238. get { return _useSoaSerialDateScheme; }
  239. set { _useSoaSerialDateScheme = value; }
  240. }
  241. public DateTime LastUsedOn
  242. {
  243. get { return _lastUsedOn; }
  244. set { _lastUsedOn = value; }
  245. }
  246. #endregion
  247. }
  248. }