123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- /*
- Technitium DNS Server
- Copyright (C) 2023 Shreyas Zare (shreyas@technitium.com)
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- using DnsServerCore.Auth;
- using DnsServerCore.Dns.Zones;
- using Microsoft.AspNetCore.Http;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TechnitiumLibrary.Net;
- using TechnitiumLibrary.Net.Dns;
- using TechnitiumLibrary.Net.Dns.ResourceRecords;
- namespace DnsServerCore
- {
- class WebServiceOtherZonesApi
- {
- #region variables
- readonly DnsWebService _dnsWebService;
- #endregion
- #region constructor
- public WebServiceOtherZonesApi(DnsWebService dnsWebService)
- {
- _dnsWebService = dnsWebService;
- }
- #endregion
- #region public
- #region cache api
- public void FlushCache(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Cache, session.User, PermissionFlag.Delete))
- throw new DnsWebServiceException("Access was denied.");
- _dnsWebService.DnsServer.CacheZoneManager.Flush();
- _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Cache was flushed.");
- }
- public void ListCachedZones(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Cache, session.User, PermissionFlag.View))
- throw new DnsWebServiceException("Access was denied.");
- HttpRequest request = context.Request;
- string domain = request.GetQueryOrForm("domain", "");
- if (DnsClient.IsDomainNameUnicode(domain))
- domain = DnsClient.ConvertDomainNameToAscii(domain);
- string direction = request.QueryOrForm("direction");
- if (direction is not null)
- direction = direction.ToLower();
- List<string> subZones = new List<string>();
- List<DnsResourceRecord> records = new List<DnsResourceRecord>();
- while (true)
- {
- subZones.Clear();
- records.Clear();
- _dnsWebService.DnsServer.CacheZoneManager.ListSubDomains(domain, subZones);
- _dnsWebService.DnsServer.CacheZoneManager.ListAllRecords(domain, records);
- if (records.Count > 0)
- break;
- if (subZones.Count != 1)
- break;
- if (direction == "up")
- {
- if (domain.Length == 0)
- break;
- int i = domain.IndexOf('.');
- if (i < 0)
- domain = "";
- else
- domain = domain.Substring(i + 1);
- }
- else if (domain.Length == 0)
- {
- domain = subZones[0];
- }
- else
- {
- domain = subZones[0] + "." + domain;
- }
- }
- subZones.Sort();
- Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
- jsonWriter.WriteString("domain", domain);
- if (DnsClient.TryConvertDomainNameToUnicode(domain, out string idn))
- jsonWriter.WriteString("domainIdn", idn);
- jsonWriter.WritePropertyName("zones");
- jsonWriter.WriteStartArray();
- if (domain.Length != 0)
- domain = "." + domain;
- foreach (string subZone in subZones)
- {
- string zone = subZone + domain;
- if (DnsClient.TryConvertDomainNameToUnicode(zone, out string zoneIdn))
- zone = zoneIdn;
- jsonWriter.WriteStringValue(zone);
- }
- jsonWriter.WriteEndArray();
- WebServiceZonesApi.WriteRecordsAsJson(records, jsonWriter, false);
- }
- public void DeleteCachedZone(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Cache, session.User, PermissionFlag.Delete))
- throw new DnsWebServiceException("Access was denied.");
- string domain = context.Request.GetQueryOrForm("domain");
- if (DnsClient.IsDomainNameUnicode(domain))
- domain = DnsClient.ConvertDomainNameToAscii(domain);
- if (_dnsWebService.DnsServer.CacheZoneManager.DeleteZone(domain))
- _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Cached zone was deleted: " + domain);
- }
- #endregion
- #region allowed zones api
- public void ListAllowedZones(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Allowed, session.User, PermissionFlag.View))
- throw new DnsWebServiceException("Access was denied.");
- HttpRequest request = context.Request;
- string domain = request.GetQueryOrForm("domain", "");
- if (DnsClient.IsDomainNameUnicode(domain))
- domain = DnsClient.ConvertDomainNameToAscii(domain);
- string direction = request.QueryOrForm("direction");
- if (direction is not null)
- direction = direction.ToLower();
- List<string> subZones = new List<string>();
- List<DnsResourceRecord> records = new List<DnsResourceRecord>();
- while (true)
- {
- subZones.Clear();
- records.Clear();
- _dnsWebService.DnsServer.AllowedZoneManager.ListSubDomains(domain, subZones);
- _dnsWebService.DnsServer.AllowedZoneManager.ListAllRecords(domain, records);
- if (records.Count > 0)
- break;
- if (subZones.Count != 1)
- break;
- if (direction == "up")
- {
- if (domain.Length == 0)
- break;
- int i = domain.IndexOf('.');
- if (i < 0)
- domain = "";
- else
- domain = domain.Substring(i + 1);
- }
- else if (domain.Length == 0)
- {
- domain = subZones[0];
- }
- else
- {
- domain = subZones[0] + "." + domain;
- }
- }
- subZones.Sort();
- Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
- jsonWriter.WriteString("domain", domain);
- if (DnsClient.TryConvertDomainNameToUnicode(domain, out string idn))
- jsonWriter.WriteString("domainIdn", idn);
- jsonWriter.WritePropertyName("zones");
- jsonWriter.WriteStartArray();
- if (domain.Length != 0)
- domain = "." + domain;
- foreach (string subZone in subZones)
- {
- string zone = subZone + domain;
- if (DnsClient.TryConvertDomainNameToUnicode(zone, out string zoneIdn))
- zone = zoneIdn;
- jsonWriter.WriteStringValue(zone);
- }
- jsonWriter.WriteEndArray();
- WebServiceZonesApi.WriteRecordsAsJson(records, jsonWriter, true);
- }
- public void ImportAllowedZones(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Allowed, session.User, PermissionFlag.Modify))
- throw new DnsWebServiceException("Access was denied.");
- HttpRequest request = context.Request;
- string allowedZones = request.GetQueryOrForm("allowedZones");
- string[] allowedZonesList = allowedZones.Split(',');
- for (int i = 0; i < allowedZonesList.Length; i++)
- {
- if (DnsClient.IsDomainNameUnicode(allowedZonesList[i]))
- allowedZonesList[i] = DnsClient.ConvertDomainNameToAscii(allowedZonesList[i]);
- }
- _dnsWebService.DnsServer.AllowedZoneManager.ImportZones(allowedZonesList);
- _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Total " + allowedZonesList.Length + " zones were imported into allowed zone successfully.");
- _dnsWebService.DnsServer.AllowedZoneManager.SaveZoneFile();
- }
- public async Task ExportAllowedZonesAsync(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Allowed, session.User, PermissionFlag.View))
- throw new DnsWebServiceException("Access was denied.");
- IReadOnlyList<AuthZoneInfo> zoneInfoList = _dnsWebService.DnsServer.AllowedZoneManager.GetAllZones();
- HttpResponse response = context.Response;
- response.ContentType = "text/plain";
- response.Headers.ContentDisposition = "attachment;filename=AllowedZones.txt";
- await using (StreamWriter sW = new StreamWriter(response.Body))
- {
- foreach (AuthZoneInfo zoneInfo in zoneInfoList)
- await sW.WriteLineAsync(zoneInfo.Name);
- }
- }
- public void DeleteAllowedZone(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Allowed, session.User, PermissionFlag.Delete))
- throw new DnsWebServiceException("Access was denied.");
- string domain = context.Request.GetQueryOrForm("domain");
- if (DnsClient.IsDomainNameUnicode(domain))
- domain = DnsClient.ConvertDomainNameToAscii(domain);
- if (_dnsWebService.DnsServer.AllowedZoneManager.DeleteZone(domain))
- {
- _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Allowed zone was deleted: " + domain);
- _dnsWebService.DnsServer.AllowedZoneManager.SaveZoneFile();
- }
- }
- public void FlushAllowedZone(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Allowed, session.User, PermissionFlag.Delete))
- throw new DnsWebServiceException("Access was denied.");
- _dnsWebService.DnsServer.AllowedZoneManager.Flush();
- _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Allowed zone was flushed successfully.");
- _dnsWebService.DnsServer.AllowedZoneManager.SaveZoneFile();
- }
- public void AllowZone(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Allowed, session.User, PermissionFlag.Modify))
- throw new DnsWebServiceException("Access was denied.");
- string domain = context.Request.GetQueryOrForm("domain");
- if (DnsClient.IsDomainNameUnicode(domain))
- domain = DnsClient.ConvertDomainNameToAscii(domain);
- if (IPAddress.TryParse(domain, out IPAddress ipAddress))
- domain = ipAddress.GetReverseDomain();
- if (_dnsWebService.DnsServer.AllowedZoneManager.AllowZone(domain))
- {
- _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Zone was allowed: " + domain);
- _dnsWebService.DnsServer.AllowedZoneManager.SaveZoneFile();
- }
- }
- #endregion
- #region blocked zones api
- public void ListBlockedZones(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Blocked, session.User, PermissionFlag.View))
- throw new DnsWebServiceException("Access was denied.");
- HttpRequest request = context.Request;
- string domain = request.GetQueryOrForm("domain", "");
- if (DnsClient.IsDomainNameUnicode(domain))
- domain = DnsClient.ConvertDomainNameToAscii(domain);
- string direction = request.QueryOrForm("direction");
- if (direction is not null)
- direction = direction.ToLower();
- List<string> subZones = new List<string>();
- List<DnsResourceRecord> records = new List<DnsResourceRecord>();
- while (true)
- {
- subZones.Clear();
- records.Clear();
- _dnsWebService.DnsServer.BlockedZoneManager.ListSubDomains(domain, subZones);
- _dnsWebService.DnsServer.BlockedZoneManager.ListAllRecords(domain, records);
- if (records.Count > 0)
- break;
- if (subZones.Count != 1)
- break;
- if (direction == "up")
- {
- if (domain.Length == 0)
- break;
- int i = domain.IndexOf('.');
- if (i < 0)
- domain = "";
- else
- domain = domain.Substring(i + 1);
- }
- else if (domain.Length == 0)
- {
- domain = subZones[0];
- }
- else
- {
- domain = subZones[0] + "." + domain;
- }
- }
- subZones.Sort();
- Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
- jsonWriter.WriteString("domain", domain);
- if (DnsClient.TryConvertDomainNameToUnicode(domain, out string idn))
- jsonWriter.WriteString("domainIdn", idn);
- jsonWriter.WritePropertyName("zones");
- jsonWriter.WriteStartArray();
- if (domain.Length != 0)
- domain = "." + domain;
- foreach (string subZone in subZones)
- {
- string zone = subZone + domain;
- if (DnsClient.TryConvertDomainNameToUnicode(zone, out string zoneIdn))
- zone = zoneIdn;
- jsonWriter.WriteStringValue(zone);
- }
- jsonWriter.WriteEndArray();
- WebServiceZonesApi.WriteRecordsAsJson(records, jsonWriter, true);
- }
- public void ImportBlockedZones(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Blocked, session.User, PermissionFlag.Modify))
- throw new DnsWebServiceException("Access was denied.");
- HttpRequest request = context.Request;
- string blockedZones = request.GetQueryOrForm("blockedZones");
- string[] blockedZonesList = blockedZones.Split(',');
- for (int i = 0; i < blockedZonesList.Length; i++)
- {
- if (DnsClient.IsDomainNameUnicode(blockedZonesList[i]))
- blockedZonesList[i] = DnsClient.ConvertDomainNameToAscii(blockedZonesList[i]);
- }
- _dnsWebService.DnsServer.BlockedZoneManager.ImportZones(blockedZonesList);
- _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Total " + blockedZonesList.Length + " zones were imported into blocked zone successfully.");
- _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile();
- }
- public async Task ExportBlockedZonesAsync(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Blocked, session.User, PermissionFlag.View))
- throw new DnsWebServiceException("Access was denied.");
- IReadOnlyList<AuthZoneInfo> zoneInfoList = _dnsWebService.DnsServer.BlockedZoneManager.GetAllZones();
- HttpResponse response = context.Response;
- response.ContentType = "text/plain";
- response.Headers.ContentDisposition = "attachment;filename=BlockedZones.txt";
- await using (StreamWriter sW = new StreamWriter(response.Body))
- {
- foreach (AuthZoneInfo zoneInfo in zoneInfoList)
- await sW.WriteLineAsync(zoneInfo.Name);
- }
- }
- public void DeleteBlockedZone(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Blocked, session.User, PermissionFlag.Delete))
- throw new DnsWebServiceException("Access was denied.");
- string domain = context.Request.GetQueryOrForm("domain");
- if (DnsClient.IsDomainNameUnicode(domain))
- domain = DnsClient.ConvertDomainNameToAscii(domain);
- if (_dnsWebService.DnsServer.BlockedZoneManager.DeleteZone(domain))
- {
- _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Blocked zone was deleted: " + domain);
- _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile();
- }
- }
- public void FlushBlockedZone(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Blocked, session.User, PermissionFlag.Delete))
- throw new DnsWebServiceException("Access was denied.");
- _dnsWebService.DnsServer.BlockedZoneManager.Flush();
- _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Blocked zone was flushed successfully.");
- _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile();
- }
- public void BlockZone(HttpContext context)
- {
- UserSession session = context.GetCurrentSession();
- if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Blocked, session.User, PermissionFlag.Modify))
- throw new DnsWebServiceException("Access was denied.");
- string domain = context.Request.GetQueryOrForm("domain");
- if (DnsClient.IsDomainNameUnicode(domain))
- domain = DnsClient.ConvertDomainNameToAscii(domain);
- if (IPAddress.TryParse(domain, out IPAddress ipAddress))
- domain = ipAddress.GetReverseDomain();
- if (_dnsWebService.DnsServer.BlockedZoneManager.BlockZone(domain))
- {
- _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] Domain was added to blocked zone: " + domain);
- _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile();
- }
- }
- #endregion
- #endregion
- }
- }
|