AllowedZoneManager.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.Zones;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Text;
  20. using System.Threading;
  21. using TechnitiumLibrary.IO;
  22. using TechnitiumLibrary.Net.Dns;
  23. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  24. namespace DnsServerCore.Dns.ZoneManagers
  25. {
  26. public sealed class AllowedZoneManager : IDisposable
  27. {
  28. #region variables
  29. readonly DnsServer _dnsServer;
  30. readonly AuthZoneManager _zoneManager;
  31. DnsSOARecordData _soaRecord;
  32. DnsNSRecordData _nsRecord;
  33. readonly object _saveLock = new object();
  34. bool _pendingSave;
  35. readonly Timer _saveTimer;
  36. const int SAVE_TIMER_INITIAL_INTERVAL = 10000;
  37. #endregion
  38. #region constructor
  39. public AllowedZoneManager(DnsServer dnsServer)
  40. {
  41. _dnsServer = dnsServer;
  42. _zoneManager = new AuthZoneManager(_dnsServer);
  43. UpdateServerDomain();
  44. _saveTimer = new Timer(delegate (object state)
  45. {
  46. lock (_saveLock)
  47. {
  48. if (_pendingSave)
  49. {
  50. try
  51. {
  52. SaveZoneFileInternal();
  53. _pendingSave = false;
  54. }
  55. catch (Exception ex)
  56. {
  57. _dnsServer.LogManager.Write(ex);
  58. //set timer to retry again
  59. _saveTimer.Change(SAVE_TIMER_INITIAL_INTERVAL, Timeout.Infinite);
  60. }
  61. }
  62. }
  63. });
  64. }
  65. #endregion
  66. #region IDisposable
  67. bool _disposed;
  68. public void Dispose()
  69. {
  70. if (_disposed)
  71. return;
  72. lock (_saveLock)
  73. {
  74. _saveTimer?.Dispose();
  75. if (_pendingSave)
  76. {
  77. try
  78. {
  79. SaveZoneFileInternal();
  80. }
  81. catch (Exception ex)
  82. {
  83. _dnsServer.LogManager.Write(ex);
  84. }
  85. finally
  86. {
  87. _pendingSave = false;
  88. }
  89. }
  90. }
  91. _disposed = true;
  92. }
  93. #endregion
  94. #region private
  95. internal void UpdateServerDomain()
  96. {
  97. _soaRecord = new DnsSOARecordData(_dnsServer.ServerDomain, _dnsServer.ResponsiblePerson.Address, 1, 900, 300, 604800, 60);
  98. _nsRecord = new DnsNSRecordData(_dnsServer.ServerDomain);
  99. _zoneManager.UpdateServerDomain();
  100. }
  101. private void SaveZoneFileInternal()
  102. {
  103. IReadOnlyList<AuthZoneInfo> allowedZones = _dnsServer.AllowedZoneManager.GetAllZones();
  104. string allowedZoneFile = Path.Combine(_dnsServer.ConfigFolder, "allowed.config");
  105. using (FileStream fS = new FileStream(allowedZoneFile, FileMode.Create, FileAccess.Write))
  106. {
  107. BinaryWriter bW = new BinaryWriter(fS);
  108. bW.Write(Encoding.ASCII.GetBytes("AZ")); //format
  109. bW.Write((byte)1); //version
  110. bW.Write(allowedZones.Count);
  111. foreach (AuthZoneInfo zone in allowedZones)
  112. bW.WriteShortString(zone.Name);
  113. }
  114. _dnsServer.LogManager?.Write("DNS Server allowed zone file was saved: " + allowedZoneFile);
  115. }
  116. #endregion
  117. #region public
  118. public void LoadAllowedZoneFile()
  119. {
  120. _zoneManager.Flush();
  121. string allowedZoneFile = Path.Combine(_dnsServer.ConfigFolder, "allowed.config");
  122. try
  123. {
  124. _dnsServer.LogManager?.Write("DNS Server is loading allowed zone file: " + allowedZoneFile);
  125. using (FileStream fS = new FileStream(allowedZoneFile, FileMode.Open, FileAccess.Read))
  126. {
  127. BinaryReader bR = new BinaryReader(fS);
  128. if (Encoding.ASCII.GetString(bR.ReadBytes(2)) != "AZ") //format
  129. throw new InvalidDataException("DnsServer allowed zone file format is invalid.");
  130. byte version = bR.ReadByte();
  131. switch (version)
  132. {
  133. case 1:
  134. int length = bR.ReadInt32();
  135. int i = 0;
  136. _zoneManager.LoadSpecialPrimaryZones(delegate ()
  137. {
  138. if (i++ < length)
  139. return bR.ReadShortString();
  140. return null;
  141. }, _soaRecord, _nsRecord);
  142. break;
  143. default:
  144. throw new InvalidDataException("DnsServer allowed zone file version not supported.");
  145. }
  146. }
  147. _dnsServer.LogManager?.Write("DNS Server allowed zone file was loaded: " + allowedZoneFile);
  148. }
  149. catch (FileNotFoundException)
  150. { }
  151. catch (Exception ex)
  152. {
  153. _dnsServer.LogManager?.Write("DNS Server encountered an error while loading allowed zone file: " + allowedZoneFile + "\r\n" + ex.ToString());
  154. }
  155. }
  156. public void ImportZones(string[] domains)
  157. {
  158. _zoneManager.LoadSpecialPrimaryZones(domains, _soaRecord, _nsRecord);
  159. }
  160. public bool AllowZone(string domain)
  161. {
  162. if (_zoneManager.CreateSpecialPrimaryZone(domain, _soaRecord, _nsRecord) != null)
  163. return true;
  164. return false;
  165. }
  166. public bool DeleteZone(string domain)
  167. {
  168. if (_zoneManager.DeleteZone(domain))
  169. return true;
  170. return false;
  171. }
  172. public void Flush()
  173. {
  174. _zoneManager.Flush();
  175. }
  176. public IReadOnlyList<AuthZoneInfo> GetAllZones()
  177. {
  178. return _zoneManager.GetAllZones();
  179. }
  180. public void ListAllRecords(string domain, List<DnsResourceRecord> records)
  181. {
  182. _zoneManager.ListAllRecords(domain, domain, records);
  183. }
  184. public void ListSubDomains(string domain, List<string> subDomains)
  185. {
  186. _zoneManager.ListSubDomains(domain, subDomains);
  187. }
  188. public void SaveZoneFile()
  189. {
  190. lock (_saveLock)
  191. {
  192. if (_pendingSave)
  193. return;
  194. _pendingSave = true;
  195. _saveTimer.Change(SAVE_TIMER_INITIAL_INTERVAL, Timeout.Infinite);
  196. }
  197. }
  198. public bool IsAllowed(DnsDatagram request)
  199. {
  200. if (_zoneManager.TotalZones < 1)
  201. return false;
  202. return _zoneManager.Query(request, false) is not null;
  203. }
  204. #endregion
  205. #region properties
  206. public int TotalZonesAllowed
  207. { get { return _zoneManager.TotalZones; } }
  208. #endregion
  209. }
  210. }