AllowedZoneManager.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2020 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. DnsSOARecord _soaRecord;
  31. DnsNSRecord _nsRecord;
  32. int _totalZonesAllowed;
  33. #endregion
  34. #region constructor
  35. public AllowedZoneManager(DnsServer dnsServer)
  36. {
  37. _dnsServer = dnsServer;
  38. _zoneManager = new AuthZoneManager(_dnsServer);
  39. UpdateServerDomain(_dnsServer.ServerDomain);
  40. }
  41. #endregion
  42. #region private
  43. private void UpdateServerDomain(string serverDomain)
  44. {
  45. _soaRecord = new DnsSOARecord(serverDomain, "hostadmin." + serverDomain, 1, 14400, 3600, 604800, 900);
  46. _nsRecord = new DnsNSRecord(serverDomain);
  47. _zoneManager.ServerDomain = serverDomain;
  48. }
  49. #endregion
  50. #region public
  51. public void LoadAllowedZoneFile()
  52. {
  53. string allowedZoneFile = Path.Combine(_dnsServer.ConfigFolder, "allowed.config");
  54. try
  55. {
  56. LogManager log = _dnsServer.LogManager;
  57. if (log != null)
  58. log.Write("DNS Server is loading allowed zone file: " + allowedZoneFile);
  59. using (FileStream fS = new FileStream(allowedZoneFile, FileMode.Open, FileAccess.Read))
  60. {
  61. BinaryReader bR = new BinaryReader(fS);
  62. if (Encoding.ASCII.GetString(bR.ReadBytes(2)) != "AZ") //format
  63. throw new InvalidDataException("DnsServer allowed zone file format is invalid.");
  64. byte version = bR.ReadByte();
  65. switch (version)
  66. {
  67. case 1:
  68. int length = bR.ReadInt32();
  69. for (int i = 0; i < length; i++)
  70. AllowZone(bR.ReadShortString());
  71. break;
  72. default:
  73. throw new InvalidDataException("DnsServer allowed zone file version not supported.");
  74. }
  75. }
  76. if (log != null)
  77. log.Write("DNS Server allowed zone file was loaded: " + allowedZoneFile);
  78. }
  79. catch (FileNotFoundException)
  80. { }
  81. catch (Exception ex)
  82. {
  83. LogManager log = _dnsServer.LogManager;
  84. if (log != null)
  85. log.Write("DNS Server encountered an error while loading allowed zone file: " + allowedZoneFile + "\r\n" + ex.ToString());
  86. }
  87. }
  88. public bool AllowZone(string domain)
  89. {
  90. if (_zoneManager.CreateInternalPrimaryZone(domain, _soaRecord, _nsRecord) != null)
  91. {
  92. _totalZonesAllowed++;
  93. return true;
  94. }
  95. return false;
  96. }
  97. public bool DeleteZone(string domain)
  98. {
  99. if (_zoneManager.DeleteZone(domain))
  100. {
  101. _totalZonesAllowed--;
  102. return true;
  103. }
  104. return false;
  105. }
  106. public List<AuthZoneInfo> ListZones()
  107. {
  108. return _zoneManager.ListZones();
  109. }
  110. public List<string> ListSubDomains(string domain)
  111. {
  112. return _zoneManager.ListSubDomains(domain);
  113. }
  114. public IReadOnlyList<DnsResourceRecord> QueryRecords(string domain, DnsResourceRecordType type)
  115. {
  116. return _zoneManager.QueryRecords(domain, type);
  117. }
  118. public void SaveZoneFile()
  119. {
  120. List<AuthZoneInfo> allowedZones = _dnsServer.AllowedZoneManager.ListZones();
  121. _totalZonesAllowed = allowedZones.Count;
  122. string allowedZoneFile = Path.Combine(_dnsServer.ConfigFolder, "allowed.config");
  123. using (FileStream fS = new FileStream(allowedZoneFile, FileMode.Create, FileAccess.Write))
  124. {
  125. BinaryWriter bW = new BinaryWriter(fS);
  126. bW.Write(Encoding.ASCII.GetBytes("AZ")); //format
  127. bW.Write((byte)1); //version
  128. bW.Write(allowedZones.Count);
  129. foreach (AuthZoneInfo zone in allowedZones)
  130. bW.WriteShortString(zone.Name);
  131. }
  132. LogManager log = _dnsServer.LogManager;
  133. if (log != null)
  134. log.Write("DNS Server allowed zone file was saved: " + allowedZoneFile);
  135. }
  136. public DnsDatagram Query(DnsDatagram request)
  137. {
  138. return _zoneManager.Query(request);
  139. }
  140. #endregion
  141. #region properties
  142. public string ServerDomain
  143. {
  144. get { return _soaRecord.PrimaryNameServer; }
  145. set { UpdateServerDomain(value); }
  146. }
  147. public int TotalZonesAllowed
  148. { get { return _totalZonesAllowed; } }
  149. #endregion
  150. }
  151. }