WebServiceOtherZonesApi.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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.Auth;
  16. using DnsServerCore.Dns.Zones;
  17. using Microsoft.AspNetCore.Http;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.IO;
  21. using System.Net;
  22. using System.Text.Json;
  23. using System.Threading.Tasks;
  24. using TechnitiumLibrary.Net;
  25. using TechnitiumLibrary.Net.Dns;
  26. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  27. namespace DnsServerCore
  28. {
  29. class WebServiceOtherZonesApi
  30. {
  31. #region variables
  32. readonly DnsWebService _dnsWebService;
  33. #endregion
  34. #region constructor
  35. public WebServiceOtherZonesApi(DnsWebService dnsWebService)
  36. {
  37. _dnsWebService = dnsWebService;
  38. }
  39. #endregion
  40. #region public
  41. #region cache api
  42. public void FlushCache(HttpContext context)
  43. {
  44. UserSession session = context.GetCurrentSession();
  45. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Cache, session.User, PermissionFlag.Delete))
  46. throw new DnsWebServiceException("Access was denied.");
  47. _dnsWebService.DnsServer.CacheZoneManager.Flush();
  48. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Cache was flushed.");
  49. }
  50. public void ListCachedZones(HttpContext context)
  51. {
  52. UserSession session = context.GetCurrentSession();
  53. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Cache, session.User, PermissionFlag.View))
  54. throw new DnsWebServiceException("Access was denied.");
  55. HttpRequest request = context.Request;
  56. string domain = request.GetQueryOrForm("domain", "");
  57. if (DnsClient.IsDomainNameUnicode(domain))
  58. domain = DnsClient.ConvertDomainNameToAscii(domain);
  59. string direction = request.QueryOrForm("direction");
  60. if (direction is not null)
  61. direction = direction.ToLower();
  62. List<string> subZones = new List<string>();
  63. List<DnsResourceRecord> records = new List<DnsResourceRecord>();
  64. while (true)
  65. {
  66. subZones.Clear();
  67. records.Clear();
  68. _dnsWebService.DnsServer.CacheZoneManager.ListSubDomains(domain, subZones);
  69. _dnsWebService.DnsServer.CacheZoneManager.ListAllRecords(domain, records);
  70. if (records.Count > 0)
  71. break;
  72. if (subZones.Count != 1)
  73. break;
  74. if (direction == "up")
  75. {
  76. if (domain.Length == 0)
  77. break;
  78. int i = domain.IndexOf('.');
  79. if (i < 0)
  80. domain = "";
  81. else
  82. domain = domain.Substring(i + 1);
  83. }
  84. else if (domain.Length == 0)
  85. {
  86. domain = subZones[0];
  87. }
  88. else
  89. {
  90. domain = subZones[0] + "." + domain;
  91. }
  92. }
  93. subZones.Sort();
  94. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  95. jsonWriter.WriteString("domain", domain);
  96. if (DnsClient.TryConvertDomainNameToUnicode(domain, out string idn))
  97. jsonWriter.WriteString("domainIdn", idn);
  98. jsonWriter.WritePropertyName("zones");
  99. jsonWriter.WriteStartArray();
  100. if (domain.Length != 0)
  101. domain = "." + domain;
  102. foreach (string subZone in subZones)
  103. {
  104. string zone = subZone + domain;
  105. if (DnsClient.TryConvertDomainNameToUnicode(zone, out string zoneIdn))
  106. zone = zoneIdn;
  107. jsonWriter.WriteStringValue(zone);
  108. }
  109. jsonWriter.WriteEndArray();
  110. WebServiceZonesApi.WriteRecordsAsJson(records, jsonWriter, false);
  111. }
  112. public void DeleteCachedZone(HttpContext context)
  113. {
  114. UserSession session = context.GetCurrentSession();
  115. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Cache, session.User, PermissionFlag.Delete))
  116. throw new DnsWebServiceException("Access was denied.");
  117. string domain = context.Request.GetQueryOrForm("domain");
  118. if (DnsClient.IsDomainNameUnicode(domain))
  119. domain = DnsClient.ConvertDomainNameToAscii(domain);
  120. if (_dnsWebService.DnsServer.CacheZoneManager.DeleteZone(domain))
  121. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Cached zone was deleted: " + domain);
  122. }
  123. #endregion
  124. #region allowed zones api
  125. public void ListAllowedZones(HttpContext context)
  126. {
  127. UserSession session = context.GetCurrentSession();
  128. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Allowed, session.User, PermissionFlag.View))
  129. throw new DnsWebServiceException("Access was denied.");
  130. HttpRequest request = context.Request;
  131. string domain = request.GetQueryOrForm("domain", "");
  132. if (DnsClient.IsDomainNameUnicode(domain))
  133. domain = DnsClient.ConvertDomainNameToAscii(domain);
  134. string direction = request.QueryOrForm("direction");
  135. if (direction is not null)
  136. direction = direction.ToLower();
  137. List<string> subZones = new List<string>();
  138. List<DnsResourceRecord> records = new List<DnsResourceRecord>();
  139. while (true)
  140. {
  141. subZones.Clear();
  142. records.Clear();
  143. _dnsWebService.DnsServer.AllowedZoneManager.ListSubDomains(domain, subZones);
  144. _dnsWebService.DnsServer.AllowedZoneManager.ListAllRecords(domain, records);
  145. if (records.Count > 0)
  146. break;
  147. if (subZones.Count != 1)
  148. break;
  149. if (direction == "up")
  150. {
  151. if (domain.Length == 0)
  152. break;
  153. int i = domain.IndexOf('.');
  154. if (i < 0)
  155. domain = "";
  156. else
  157. domain = domain.Substring(i + 1);
  158. }
  159. else if (domain.Length == 0)
  160. {
  161. domain = subZones[0];
  162. }
  163. else
  164. {
  165. domain = subZones[0] + "." + domain;
  166. }
  167. }
  168. subZones.Sort();
  169. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  170. jsonWriter.WriteString("domain", domain);
  171. if (DnsClient.TryConvertDomainNameToUnicode(domain, out string idn))
  172. jsonWriter.WriteString("domainIdn", idn);
  173. jsonWriter.WritePropertyName("zones");
  174. jsonWriter.WriteStartArray();
  175. if (domain.Length != 0)
  176. domain = "." + domain;
  177. foreach (string subZone in subZones)
  178. {
  179. string zone = subZone + domain;
  180. if (DnsClient.TryConvertDomainNameToUnicode(zone, out string zoneIdn))
  181. zone = zoneIdn;
  182. jsonWriter.WriteStringValue(zone);
  183. }
  184. jsonWriter.WriteEndArray();
  185. WebServiceZonesApi.WriteRecordsAsJson(records, jsonWriter, true);
  186. }
  187. public void ImportAllowedZones(HttpContext context)
  188. {
  189. UserSession session = context.GetCurrentSession();
  190. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Allowed, session.User, PermissionFlag.Modify))
  191. throw new DnsWebServiceException("Access was denied.");
  192. HttpRequest request = context.Request;
  193. string allowedZones = request.GetQueryOrForm("allowedZones");
  194. string[] allowedZonesList = allowedZones.Split(',');
  195. for (int i = 0; i < allowedZonesList.Length; i++)
  196. {
  197. if (DnsClient.IsDomainNameUnicode(allowedZonesList[i]))
  198. allowedZonesList[i] = DnsClient.ConvertDomainNameToAscii(allowedZonesList[i]);
  199. }
  200. _dnsWebService.DnsServer.AllowedZoneManager.ImportZones(allowedZonesList);
  201. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Total " + allowedZonesList.Length + " zones were imported into allowed zone successfully.");
  202. _dnsWebService.DnsServer.AllowedZoneManager.SaveZoneFile();
  203. }
  204. public async Task ExportAllowedZonesAsync(HttpContext context)
  205. {
  206. UserSession session = context.GetCurrentSession();
  207. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Allowed, session.User, PermissionFlag.View))
  208. throw new DnsWebServiceException("Access was denied.");
  209. IReadOnlyList<AuthZoneInfo> zoneInfoList = _dnsWebService.DnsServer.AllowedZoneManager.GetAllZones();
  210. HttpResponse response = context.Response;
  211. response.ContentType = "text/plain";
  212. response.Headers.ContentDisposition = "attachment;filename=AllowedZones.txt";
  213. await using (StreamWriter sW = new StreamWriter(response.Body))
  214. {
  215. foreach (AuthZoneInfo zoneInfo in zoneInfoList)
  216. await sW.WriteLineAsync(zoneInfo.Name);
  217. }
  218. }
  219. public void DeleteAllowedZone(HttpContext context)
  220. {
  221. UserSession session = context.GetCurrentSession();
  222. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Allowed, session.User, PermissionFlag.Delete))
  223. throw new DnsWebServiceException("Access was denied.");
  224. string domain = context.Request.GetQueryOrForm("domain");
  225. if (DnsClient.IsDomainNameUnicode(domain))
  226. domain = DnsClient.ConvertDomainNameToAscii(domain);
  227. if (_dnsWebService.DnsServer.AllowedZoneManager.DeleteZone(domain))
  228. {
  229. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Allowed zone was deleted: " + domain);
  230. _dnsWebService.DnsServer.AllowedZoneManager.SaveZoneFile();
  231. }
  232. }
  233. public void FlushAllowedZone(HttpContext context)
  234. {
  235. UserSession session = context.GetCurrentSession();
  236. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Allowed, session.User, PermissionFlag.Delete))
  237. throw new DnsWebServiceException("Access was denied.");
  238. _dnsWebService.DnsServer.AllowedZoneManager.Flush();
  239. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Allowed zone was flushed successfully.");
  240. _dnsWebService.DnsServer.AllowedZoneManager.SaveZoneFile();
  241. }
  242. public void AllowZone(HttpContext context)
  243. {
  244. UserSession session = context.GetCurrentSession();
  245. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Allowed, session.User, PermissionFlag.Modify))
  246. throw new DnsWebServiceException("Access was denied.");
  247. string domain = context.Request.GetQueryOrForm("domain");
  248. if (DnsClient.IsDomainNameUnicode(domain))
  249. domain = DnsClient.ConvertDomainNameToAscii(domain);
  250. if (IPAddress.TryParse(domain, out IPAddress ipAddress))
  251. domain = ipAddress.GetReverseDomain();
  252. if (_dnsWebService.DnsServer.AllowedZoneManager.AllowZone(domain))
  253. {
  254. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Zone was allowed: " + domain);
  255. _dnsWebService.DnsServer.AllowedZoneManager.SaveZoneFile();
  256. }
  257. }
  258. #endregion
  259. #region blocked zones api
  260. public void ListBlockedZones(HttpContext context)
  261. {
  262. UserSession session = context.GetCurrentSession();
  263. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Blocked, session.User, PermissionFlag.View))
  264. throw new DnsWebServiceException("Access was denied.");
  265. HttpRequest request = context.Request;
  266. string domain = request.GetQueryOrForm("domain", "");
  267. if (DnsClient.IsDomainNameUnicode(domain))
  268. domain = DnsClient.ConvertDomainNameToAscii(domain);
  269. string direction = request.QueryOrForm("direction");
  270. if (direction is not null)
  271. direction = direction.ToLower();
  272. List<string> subZones = new List<string>();
  273. List<DnsResourceRecord> records = new List<DnsResourceRecord>();
  274. while (true)
  275. {
  276. subZones.Clear();
  277. records.Clear();
  278. _dnsWebService.DnsServer.BlockedZoneManager.ListSubDomains(domain, subZones);
  279. _dnsWebService.DnsServer.BlockedZoneManager.ListAllRecords(domain, records);
  280. if (records.Count > 0)
  281. break;
  282. if (subZones.Count != 1)
  283. break;
  284. if (direction == "up")
  285. {
  286. if (domain.Length == 0)
  287. break;
  288. int i = domain.IndexOf('.');
  289. if (i < 0)
  290. domain = "";
  291. else
  292. domain = domain.Substring(i + 1);
  293. }
  294. else if (domain.Length == 0)
  295. {
  296. domain = subZones[0];
  297. }
  298. else
  299. {
  300. domain = subZones[0] + "." + domain;
  301. }
  302. }
  303. subZones.Sort();
  304. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  305. jsonWriter.WriteString("domain", domain);
  306. if (DnsClient.TryConvertDomainNameToUnicode(domain, out string idn))
  307. jsonWriter.WriteString("domainIdn", idn);
  308. jsonWriter.WritePropertyName("zones");
  309. jsonWriter.WriteStartArray();
  310. if (domain.Length != 0)
  311. domain = "." + domain;
  312. foreach (string subZone in subZones)
  313. {
  314. string zone = subZone + domain;
  315. if (DnsClient.TryConvertDomainNameToUnicode(zone, out string zoneIdn))
  316. zone = zoneIdn;
  317. jsonWriter.WriteStringValue(zone);
  318. }
  319. jsonWriter.WriteEndArray();
  320. WebServiceZonesApi.WriteRecordsAsJson(records, jsonWriter, true);
  321. }
  322. public void ImportBlockedZones(HttpContext context)
  323. {
  324. UserSession session = context.GetCurrentSession();
  325. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Blocked, session.User, PermissionFlag.Modify))
  326. throw new DnsWebServiceException("Access was denied.");
  327. HttpRequest request = context.Request;
  328. string blockedZones = request.GetQueryOrForm("blockedZones");
  329. string[] blockedZonesList = blockedZones.Split(',');
  330. for (int i = 0; i < blockedZonesList.Length; i++)
  331. {
  332. if (DnsClient.IsDomainNameUnicode(blockedZonesList[i]))
  333. blockedZonesList[i] = DnsClient.ConvertDomainNameToAscii(blockedZonesList[i]);
  334. }
  335. _dnsWebService.DnsServer.BlockedZoneManager.ImportZones(blockedZonesList);
  336. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Total " + blockedZonesList.Length + " zones were imported into blocked zone successfully.");
  337. _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile();
  338. }
  339. public async Task ExportBlockedZonesAsync(HttpContext context)
  340. {
  341. UserSession session = context.GetCurrentSession();
  342. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Blocked, session.User, PermissionFlag.View))
  343. throw new DnsWebServiceException("Access was denied.");
  344. IReadOnlyList<AuthZoneInfo> zoneInfoList = _dnsWebService.DnsServer.BlockedZoneManager.GetAllZones();
  345. HttpResponse response = context.Response;
  346. response.ContentType = "text/plain";
  347. response.Headers.ContentDisposition = "attachment;filename=BlockedZones.txt";
  348. await using (StreamWriter sW = new StreamWriter(response.Body))
  349. {
  350. foreach (AuthZoneInfo zoneInfo in zoneInfoList)
  351. await sW.WriteLineAsync(zoneInfo.Name);
  352. }
  353. }
  354. public void DeleteBlockedZone(HttpContext context)
  355. {
  356. UserSession session = context.GetCurrentSession();
  357. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Blocked, session.User, PermissionFlag.Delete))
  358. throw new DnsWebServiceException("Access was denied.");
  359. string domain = context.Request.GetQueryOrForm("domain");
  360. if (DnsClient.IsDomainNameUnicode(domain))
  361. domain = DnsClient.ConvertDomainNameToAscii(domain);
  362. if (_dnsWebService.DnsServer.BlockedZoneManager.DeleteZone(domain))
  363. {
  364. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Blocked zone was deleted: " + domain);
  365. _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile();
  366. }
  367. }
  368. public void FlushBlockedZone(HttpContext context)
  369. {
  370. UserSession session = context.GetCurrentSession();
  371. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Blocked, session.User, PermissionFlag.Delete))
  372. throw new DnsWebServiceException("Access was denied.");
  373. _dnsWebService.DnsServer.BlockedZoneManager.Flush();
  374. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Blocked zone was flushed successfully.");
  375. _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile();
  376. }
  377. public void BlockZone(HttpContext context)
  378. {
  379. UserSession session = context.GetCurrentSession();
  380. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Blocked, session.User, PermissionFlag.Modify))
  381. throw new DnsWebServiceException("Access was denied.");
  382. string domain = context.Request.GetQueryOrForm("domain");
  383. if (DnsClient.IsDomainNameUnicode(domain))
  384. domain = DnsClient.ConvertDomainNameToAscii(domain);
  385. if (IPAddress.TryParse(domain, out IPAddress ipAddress))
  386. domain = ipAddress.GetReverseDomain();
  387. if (_dnsWebService.DnsServer.BlockedZoneManager.BlockZone(domain))
  388. {
  389. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Domain was added to blocked zone: " + domain);
  390. _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile();
  391. }
  392. }
  393. #endregion
  394. #endregion
  395. }
  396. }