AllowedZoneManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 DnsServerCore.Dns.Zones;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Text;
  20. using TechnitiumLibrary.IO;
  21. using TechnitiumLibrary.Net.Dns;
  22. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  23. namespace DnsServerCore.Dns.ZoneManagers
  24. {
  25. public sealed class AllowedZoneManager
  26. {
  27. #region variables
  28. readonly DnsServer _dnsServer;
  29. readonly AuthZoneManager _zoneManager;
  30. DnsSOARecordData _soaRecord;
  31. DnsNSRecordData _nsRecord;
  32. #endregion
  33. #region constructor
  34. public AllowedZoneManager(DnsServer dnsServer)
  35. {
  36. _dnsServer = dnsServer;
  37. _zoneManager = new AuthZoneManager(_dnsServer);
  38. UpdateServerDomain(_dnsServer.ServerDomain);
  39. }
  40. #endregion
  41. #region private
  42. private void UpdateServerDomain(string serverDomain)
  43. {
  44. _soaRecord = new DnsSOARecordData(serverDomain, "hostadmin@" + serverDomain, 1, 900, 300, 604800, 60);
  45. _nsRecord = new DnsNSRecordData(serverDomain);
  46. _zoneManager.ServerDomain = serverDomain;
  47. }
  48. #endregion
  49. #region public
  50. public void LoadAllowedZoneFile()
  51. {
  52. _zoneManager.Flush();
  53. string allowedZoneFile = Path.Combine(_dnsServer.ConfigFolder, "allowed.config");
  54. try
  55. {
  56. _dnsServer.LogManager?.Write("DNS Server is loading allowed zone file: " + allowedZoneFile);
  57. using (FileStream fS = new FileStream(allowedZoneFile, FileMode.Open, FileAccess.Read))
  58. {
  59. BinaryReader bR = new BinaryReader(fS);
  60. if (Encoding.ASCII.GetString(bR.ReadBytes(2)) != "AZ") //format
  61. throw new InvalidDataException("DnsServer allowed zone file format is invalid.");
  62. byte version = bR.ReadByte();
  63. switch (version)
  64. {
  65. case 1:
  66. int length = bR.ReadInt32();
  67. int i = 0;
  68. _zoneManager.LoadSpecialPrimaryZones(delegate ()
  69. {
  70. if (i++ < length)
  71. return bR.ReadShortString();
  72. return null;
  73. }, _soaRecord, _nsRecord);
  74. break;
  75. default:
  76. throw new InvalidDataException("DnsServer allowed zone file version not supported.");
  77. }
  78. }
  79. _dnsServer.LogManager?.Write("DNS Server allowed zone file was loaded: " + allowedZoneFile);
  80. }
  81. catch (FileNotFoundException)
  82. { }
  83. catch (Exception ex)
  84. {
  85. _dnsServer.LogManager?.Write("DNS Server encountered an error while loading allowed zone file: " + allowedZoneFile + "\r\n" + ex.ToString());
  86. }
  87. }
  88. public void ImportZones(string[] domains)
  89. {
  90. _zoneManager.LoadSpecialPrimaryZones(domains, _soaRecord, _nsRecord);
  91. }
  92. public bool AllowZone(string domain)
  93. {
  94. if (_zoneManager.CreateSpecialPrimaryZone(domain, _soaRecord, _nsRecord) != null)
  95. return true;
  96. return false;
  97. }
  98. public bool DeleteZone(string domain)
  99. {
  100. if (_zoneManager.DeleteZone(domain))
  101. return true;
  102. return false;
  103. }
  104. public void Flush()
  105. {
  106. _zoneManager.Flush();
  107. }
  108. public IReadOnlyList<AuthZoneInfo> GetAllZones()
  109. {
  110. return _zoneManager.GetAllZones();
  111. }
  112. public void ListAllRecords(string domain, List<DnsResourceRecord> records)
  113. {
  114. _zoneManager.ListAllRecords(domain, domain, records);
  115. }
  116. public void ListSubDomains(string domain, List<string> subDomains)
  117. {
  118. _zoneManager.ListSubDomains(domain, subDomains);
  119. }
  120. public void SaveZoneFile()
  121. {
  122. IReadOnlyList<AuthZoneInfo> allowedZones = _dnsServer.AllowedZoneManager.GetAllZones();
  123. string allowedZoneFile = Path.Combine(_dnsServer.ConfigFolder, "allowed.config");
  124. using (FileStream fS = new FileStream(allowedZoneFile, FileMode.Create, FileAccess.Write))
  125. {
  126. BinaryWriter bW = new BinaryWriter(fS);
  127. bW.Write(Encoding.ASCII.GetBytes("AZ")); //format
  128. bW.Write((byte)1); //version
  129. bW.Write(allowedZones.Count);
  130. foreach (AuthZoneInfo zone in allowedZones)
  131. bW.WriteShortString(zone.Name);
  132. }
  133. _dnsServer.LogManager?.Write("DNS Server allowed zone file was saved: " + allowedZoneFile);
  134. }
  135. public bool IsAllowed(DnsDatagram request)
  136. {
  137. if (_zoneManager.TotalZones < 1)
  138. return false;
  139. return _zoneManager.Query(request, false) is not null;
  140. }
  141. #endregion
  142. #region properties
  143. public string ServerDomain
  144. {
  145. get { return _soaRecord.PrimaryNameServer; }
  146. set { UpdateServerDomain(value); }
  147. }
  148. public int TotalZonesAllowed
  149. { get { return _zoneManager.TotalZones; } }
  150. #endregion
  151. }
  152. }