WebServiceZonesApi.cs 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2022 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;
  16. using DnsServerCore.Dns.Dnssec;
  17. using DnsServerCore.Dns.ResourceRecords;
  18. using DnsServerCore.Dns.Zones;
  19. using Newtonsoft.Json;
  20. using System;
  21. using System.Collections.Generic;
  22. using System.IO;
  23. using System.Net;
  24. using System.Threading.Tasks;
  25. using TechnitiumLibrary.Net.Dns;
  26. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  27. using TechnitiumLibrary.Net.Proxy;
  28. namespace DnsServerCore
  29. {
  30. class WebServiceZonesApi
  31. {
  32. #region variables
  33. readonly DnsWebService _dnsWebService;
  34. uint _defaultRecordTtl = 3600;
  35. #endregion
  36. #region constructor
  37. public WebServiceZonesApi(DnsWebService dnsWebService)
  38. {
  39. _dnsWebService = dnsWebService;
  40. }
  41. #endregion
  42. #region public
  43. public void ListZones(JsonTextWriter jsonWriter)
  44. {
  45. List<AuthZoneInfo> zones = _dnsWebService.DnsServer.AuthZoneManager.ListZones();
  46. zones.Sort();
  47. jsonWriter.WritePropertyName("zones");
  48. jsonWriter.WriteStartArray();
  49. foreach (AuthZoneInfo zone in zones)
  50. {
  51. jsonWriter.WriteStartObject();
  52. jsonWriter.WritePropertyName("name");
  53. jsonWriter.WriteValue(zone.Name);
  54. jsonWriter.WritePropertyName("type");
  55. jsonWriter.WriteValue(zone.Type.ToString());
  56. switch (zone.Type)
  57. {
  58. case AuthZoneType.Primary:
  59. jsonWriter.WritePropertyName("internal");
  60. jsonWriter.WriteValue(zone.Internal);
  61. jsonWriter.WritePropertyName("dnssecStatus");
  62. jsonWriter.WriteValue(zone.DnssecStatus.ToString());
  63. break;
  64. case AuthZoneType.Secondary:
  65. jsonWriter.WritePropertyName("dnssecStatus");
  66. jsonWriter.WriteValue(zone.DnssecStatus.ToString());
  67. jsonWriter.WritePropertyName("expiry");
  68. jsonWriter.WriteValue(zone.Expiry);
  69. jsonWriter.WritePropertyName("isExpired");
  70. jsonWriter.WriteValue(zone.IsExpired);
  71. break;
  72. case AuthZoneType.Stub:
  73. jsonWriter.WritePropertyName("expiry");
  74. jsonWriter.WriteValue(zone.Expiry);
  75. jsonWriter.WritePropertyName("isExpired");
  76. jsonWriter.WriteValue(zone.IsExpired);
  77. break;
  78. }
  79. jsonWriter.WritePropertyName("disabled");
  80. jsonWriter.WriteValue(zone.Disabled);
  81. jsonWriter.WriteEndObject();
  82. }
  83. jsonWriter.WriteEndArray();
  84. }
  85. public async Task CreateZoneAsync(HttpListenerRequest request, JsonTextWriter jsonWriter)
  86. {
  87. string zoneName = request.QueryString["zone"];
  88. if (string.IsNullOrEmpty(zoneName))
  89. zoneName = request.QueryString["domain"];
  90. if (string.IsNullOrEmpty(zoneName))
  91. throw new DnsWebServiceException("Parameter 'zone' missing.");
  92. if (zoneName.Contains('*'))
  93. throw new DnsWebServiceException("Domain name for a zone cannot contain wildcard character.");
  94. if (IPAddress.TryParse(zoneName, out IPAddress ipAddress))
  95. {
  96. zoneName = new DnsQuestionRecord(ipAddress, DnsClass.IN).Name.ToLower();
  97. }
  98. else if (zoneName.Contains("/"))
  99. {
  100. string[] parts = zoneName.Split('/');
  101. if ((parts.Length == 2) && IPAddress.TryParse(parts[0], out ipAddress) && int.TryParse(parts[1], out int subnetMaskWidth))
  102. zoneName = Zone.GetReverseZone(ipAddress, subnetMaskWidth);
  103. }
  104. else if (zoneName.EndsWith("."))
  105. {
  106. zoneName = zoneName.Substring(0, zoneName.Length - 1);
  107. }
  108. AuthZoneType type = AuthZoneType.Primary;
  109. string strType = request.QueryString["type"];
  110. if (!string.IsNullOrEmpty(strType))
  111. type = Enum.Parse<AuthZoneType>(strType, true);
  112. switch (type)
  113. {
  114. case AuthZoneType.Primary:
  115. if (_dnsWebService.DnsServer.AuthZoneManager.CreatePrimaryZone(zoneName, _dnsWebService.DnsServer.ServerDomain, false) is null)
  116. throw new DnsWebServiceException("Zone already exists: " + zoneName);
  117. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Authoritative primary zone was created: " + zoneName);
  118. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  119. break;
  120. case AuthZoneType.Secondary:
  121. {
  122. string primaryNameServerAddresses = request.QueryString["primaryNameServerAddresses"];
  123. if (string.IsNullOrEmpty(primaryNameServerAddresses))
  124. primaryNameServerAddresses = null;
  125. DnsTransportProtocol zoneTransferProtocol;
  126. string strZoneTransferProtocol = request.QueryString["zoneTransferProtocol"];
  127. if (string.IsNullOrEmpty(strZoneTransferProtocol))
  128. zoneTransferProtocol = DnsTransportProtocol.Tcp;
  129. else
  130. zoneTransferProtocol = Enum.Parse<DnsTransportProtocol>(strZoneTransferProtocol, true);
  131. string tsigKeyName = request.QueryString["tsigKeyName"];
  132. if (string.IsNullOrEmpty(tsigKeyName))
  133. tsigKeyName = null;
  134. if (await _dnsWebService.DnsServer.AuthZoneManager.CreateSecondaryZoneAsync(zoneName, primaryNameServerAddresses, zoneTransferProtocol, tsigKeyName) is null)
  135. throw new DnsWebServiceException("Zone already exists: " + zoneName);
  136. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Authoritative secondary zone was created: " + zoneName);
  137. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  138. }
  139. break;
  140. case AuthZoneType.Stub:
  141. {
  142. string strPrimaryNameServerAddresses = request.QueryString["primaryNameServerAddresses"];
  143. if (string.IsNullOrEmpty(strPrimaryNameServerAddresses))
  144. strPrimaryNameServerAddresses = null;
  145. if (await _dnsWebService.DnsServer.AuthZoneManager.CreateStubZoneAsync(zoneName, strPrimaryNameServerAddresses) is null)
  146. throw new DnsWebServiceException("Zone already exists: " + zoneName);
  147. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Stub zone was created: " + zoneName);
  148. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  149. }
  150. break;
  151. case AuthZoneType.Forwarder:
  152. {
  153. DnsTransportProtocol forwarderProtocol = DnsTransportProtocol.Udp;
  154. string strForwarderProtocol = request.QueryString["protocol"];
  155. if (!string.IsNullOrEmpty(strForwarderProtocol))
  156. forwarderProtocol = Enum.Parse<DnsTransportProtocol>(strForwarderProtocol, true);
  157. string strForwarder = request.QueryString["forwarder"];
  158. if (string.IsNullOrEmpty(strForwarder))
  159. throw new DnsWebServiceException("Parameter 'forwarder' missing.");
  160. bool dnssecValidation = false;
  161. string strDnssecValidation = request.QueryString["dnssecValidation"];
  162. if (!string.IsNullOrEmpty(strDnssecValidation))
  163. dnssecValidation = bool.Parse(strDnssecValidation);
  164. NetProxyType proxyType = NetProxyType.None;
  165. string strProxyType = request.QueryString["proxyType"];
  166. if (!string.IsNullOrEmpty(strProxyType))
  167. proxyType = Enum.Parse<NetProxyType>(strProxyType, true);
  168. string proxyAddress = null;
  169. ushort proxyPort = 0;
  170. string proxyUsername = null;
  171. string proxyPassword = null;
  172. if (proxyType != NetProxyType.None)
  173. {
  174. proxyAddress = request.QueryString["proxyAddress"];
  175. if (string.IsNullOrEmpty(proxyAddress))
  176. throw new DnsWebServiceException("Parameter 'proxyAddress' missing.");
  177. string strProxyPort = request.QueryString["proxyPort"];
  178. if (string.IsNullOrEmpty(strProxyPort))
  179. throw new DnsWebServiceException("Parameter 'proxyPort' missing.");
  180. proxyPort = ushort.Parse(strProxyPort);
  181. proxyUsername = request.QueryString["proxyUsername"];
  182. proxyPassword = request.QueryString["proxyPassword"];
  183. }
  184. if (_dnsWebService.DnsServer.AuthZoneManager.CreateForwarderZone(zoneName, forwarderProtocol, strForwarder, dnssecValidation, proxyType, proxyAddress, proxyPort, proxyUsername, proxyPassword, null) is null)
  185. throw new DnsWebServiceException("Zone already exists: " + zoneName);
  186. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Forwarder zone was created: " + zoneName);
  187. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  188. }
  189. break;
  190. default:
  191. throw new NotSupportedException("Zone type not supported.");
  192. }
  193. //delete cache for this zone to allow rebuilding cache data as needed by stub or forwarder zones
  194. _dnsWebService.DnsServer.CacheZoneManager.DeleteZone(zoneName);
  195. jsonWriter.WritePropertyName("domain");
  196. jsonWriter.WriteValue(string.IsNullOrEmpty(zoneName) ? "." : zoneName);
  197. }
  198. public void SignPrimaryZone(HttpListenerRequest request)
  199. {
  200. string zoneName = request.QueryString["zone"];
  201. if (string.IsNullOrEmpty(zoneName))
  202. throw new DnsWebServiceException("Parameter 'zone' missing.");
  203. zoneName = zoneName.TrimEnd('.');
  204. string algorithm = request.QueryString["algorithm"];
  205. if (string.IsNullOrEmpty(algorithm))
  206. throw new DnsWebServiceException("Parameter 'algorithm' missing.");
  207. uint dnsKeyTtl;
  208. string strDnsKeyTtl = request.QueryString["dnsKeyTtl"];
  209. if (string.IsNullOrEmpty(strDnsKeyTtl))
  210. dnsKeyTtl = 24 * 60 * 60;
  211. else
  212. dnsKeyTtl = uint.Parse(strDnsKeyTtl);
  213. ushort zskRolloverDays;
  214. string strZskRolloverDays = request.QueryString["zskRolloverDays"];
  215. if (string.IsNullOrEmpty(strZskRolloverDays))
  216. zskRolloverDays = 90;
  217. else
  218. zskRolloverDays = ushort.Parse(strZskRolloverDays);
  219. bool useNSEC3 = false;
  220. string strNxProof = request.QueryString["nxProof"];
  221. if (!string.IsNullOrEmpty(strNxProof))
  222. {
  223. switch (strNxProof.ToUpper())
  224. {
  225. case "NSEC":
  226. useNSEC3 = false;
  227. break;
  228. case "NSEC3":
  229. useNSEC3 = true;
  230. break;
  231. default:
  232. throw new NotSupportedException("Non-existence proof type is not supported: " + strNxProof);
  233. }
  234. }
  235. ushort iterations = 0;
  236. byte saltLength = 0;
  237. if (useNSEC3)
  238. {
  239. string strIterations = request.QueryString["iterations"];
  240. if (!string.IsNullOrEmpty(strIterations))
  241. iterations = ushort.Parse(strIterations);
  242. string strSaltLength = request.QueryString["saltLength"];
  243. if (!string.IsNullOrEmpty(strSaltLength))
  244. saltLength = byte.Parse(strSaltLength);
  245. }
  246. switch (algorithm.ToUpper())
  247. {
  248. case "RSA":
  249. string hashAlgorithm = request.QueryString["hashAlgorithm"];
  250. if (string.IsNullOrEmpty(hashAlgorithm))
  251. throw new DnsWebServiceException("Parameter 'hashAlgorithm' missing.");
  252. string strKSKKeySize = request.QueryString["kskKeySize"];
  253. if (string.IsNullOrEmpty(strKSKKeySize))
  254. throw new DnsWebServiceException("Parameter 'kskKeySize' missing.");
  255. string strZSKKeySize = request.QueryString["zskKeySize"];
  256. if (string.IsNullOrEmpty(strZSKKeySize))
  257. throw new DnsWebServiceException("Parameter 'zskKeySize' missing.");
  258. int kskKeySize = int.Parse(strKSKKeySize);
  259. int zskKeySize = int.Parse(strZSKKeySize);
  260. if (useNSEC3)
  261. _dnsWebService.DnsServer.AuthZoneManager.SignPrimaryZoneWithRsaNSEC3(zoneName, hashAlgorithm, kskKeySize, zskKeySize, iterations, saltLength, dnsKeyTtl, zskRolloverDays);
  262. else
  263. _dnsWebService.DnsServer.AuthZoneManager.SignPrimaryZoneWithRsaNSEC(zoneName, hashAlgorithm, kskKeySize, zskKeySize, dnsKeyTtl, zskRolloverDays);
  264. break;
  265. case "ECDSA":
  266. string curve = request.QueryString["curve"];
  267. if (string.IsNullOrEmpty(curve))
  268. throw new DnsWebServiceException("Parameter 'curve' missing.");
  269. if (useNSEC3)
  270. _dnsWebService.DnsServer.AuthZoneManager.SignPrimaryZoneWithEcdsaNSEC3(zoneName, curve, iterations, saltLength, dnsKeyTtl, zskRolloverDays);
  271. else
  272. _dnsWebService.DnsServer.AuthZoneManager.SignPrimaryZoneWithEcdsaNSEC(zoneName, curve, dnsKeyTtl, zskRolloverDays);
  273. break;
  274. default:
  275. throw new NotSupportedException("Algorithm is not supported: " + algorithm);
  276. }
  277. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Primary zone was signed successfully: " + zoneName);
  278. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  279. }
  280. public void UnsignPrimaryZone(HttpListenerRequest request)
  281. {
  282. string zoneName = request.QueryString["zone"];
  283. if (string.IsNullOrEmpty(zoneName))
  284. throw new DnsWebServiceException("Parameter 'zone' missing.");
  285. zoneName = zoneName.TrimEnd('.');
  286. _dnsWebService.DnsServer.AuthZoneManager.UnsignPrimaryZone(zoneName);
  287. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Primary zone was unsigned successfully: " + zoneName);
  288. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  289. }
  290. public void GetPrimaryZoneDnssecProperties(HttpListenerRequest request, JsonTextWriter jsonWriter)
  291. {
  292. string zoneName = request.QueryString["zone"];
  293. if (string.IsNullOrEmpty(zoneName))
  294. throw new DnsWebServiceException("Parameter 'zone' missing.");
  295. zoneName = zoneName.TrimEnd('.');
  296. AuthZoneInfo zoneInfo = _dnsWebService.DnsServer.AuthZoneManager.GetAuthZoneInfo(zoneName);
  297. if (zoneInfo is null)
  298. throw new DnsWebServiceException("No such zone was found: " + zoneName);
  299. if (zoneInfo.Internal)
  300. throw new DnsWebServiceException("Access was denied to manage internal DNS Server zone.");
  301. if (zoneInfo.Type != AuthZoneType.Primary)
  302. throw new DnsWebServiceException("The zone must be a primary zone.");
  303. jsonWriter.WritePropertyName("name");
  304. jsonWriter.WriteValue(zoneInfo.Name);
  305. jsonWriter.WritePropertyName("type");
  306. jsonWriter.WriteValue(zoneInfo.Type.ToString());
  307. jsonWriter.WritePropertyName("internal");
  308. jsonWriter.WriteValue(zoneInfo.Internal);
  309. jsonWriter.WritePropertyName("disabled");
  310. jsonWriter.WriteValue(zoneInfo.Disabled);
  311. jsonWriter.WritePropertyName("dnssecStatus");
  312. jsonWriter.WriteValue(zoneInfo.DnssecStatus.ToString());
  313. if (zoneInfo.DnssecStatus == AuthZoneDnssecStatus.SignedWithNSEC3)
  314. {
  315. IReadOnlyList<DnsResourceRecord> nsec3ParamRecords = zoneInfo.GetRecords(DnsResourceRecordType.NSEC3PARAM);
  316. DnsNSEC3PARAMRecordData nsec3Param = nsec3ParamRecords[0].RDATA as DnsNSEC3PARAMRecordData;
  317. jsonWriter.WritePropertyName("nsec3Iterations");
  318. jsonWriter.WriteValue(nsec3Param.Iterations);
  319. jsonWriter.WritePropertyName("nsec3SaltLength");
  320. jsonWriter.WriteValue(nsec3Param.SaltValue.Length);
  321. }
  322. jsonWriter.WritePropertyName("dnsKeyTtl");
  323. jsonWriter.WriteValue(zoneInfo.DnsKeyTtl);
  324. jsonWriter.WritePropertyName("dnssecPrivateKeys");
  325. jsonWriter.WriteStartArray();
  326. IReadOnlyCollection<DnssecPrivateKey> dnssecPrivateKeys = zoneInfo.DnssecPrivateKeys;
  327. if (dnssecPrivateKeys is not null)
  328. {
  329. List<DnssecPrivateKey> sortedDnssecPrivateKey = new List<DnssecPrivateKey>(dnssecPrivateKeys);
  330. sortedDnssecPrivateKey.Sort(delegate (DnssecPrivateKey key1, DnssecPrivateKey key2)
  331. {
  332. int value = key1.KeyType.CompareTo(key2.KeyType);
  333. if (value == 0)
  334. value = key1.StateChangedOn.CompareTo(key2.StateChangedOn);
  335. return value;
  336. });
  337. foreach (DnssecPrivateKey dnssecPrivateKey in sortedDnssecPrivateKey)
  338. {
  339. jsonWriter.WriteStartObject();
  340. jsonWriter.WritePropertyName("keyTag");
  341. jsonWriter.WriteValue(dnssecPrivateKey.KeyTag);
  342. jsonWriter.WritePropertyName("keyType");
  343. jsonWriter.WriteValue(dnssecPrivateKey.KeyType.ToString());
  344. jsonWriter.WritePropertyName("algorithm");
  345. switch (dnssecPrivateKey.Algorithm)
  346. {
  347. case DnssecAlgorithm.RSAMD5:
  348. case DnssecAlgorithm.RSASHA1:
  349. case DnssecAlgorithm.RSASHA1_NSEC3_SHA1:
  350. case DnssecAlgorithm.RSASHA256:
  351. case DnssecAlgorithm.RSASHA512:
  352. jsonWriter.WriteValue(dnssecPrivateKey.Algorithm.ToString() + " (" + (dnssecPrivateKey as DnssecRsaPrivateKey).KeySize + " bits)");
  353. break;
  354. default:
  355. jsonWriter.WriteValue(dnssecPrivateKey.Algorithm.ToString());
  356. break;
  357. }
  358. jsonWriter.WritePropertyName("state");
  359. jsonWriter.WriteValue(dnssecPrivateKey.State.ToString());
  360. jsonWriter.WritePropertyName("stateChangedOn");
  361. jsonWriter.WriteValue(dnssecPrivateKey.StateChangedOn);
  362. jsonWriter.WritePropertyName("isRetiring");
  363. jsonWriter.WriteValue(dnssecPrivateKey.IsRetiring);
  364. jsonWriter.WritePropertyName("rolloverDays");
  365. jsonWriter.WriteValue(dnssecPrivateKey.RolloverDays);
  366. jsonWriter.WriteEndObject();
  367. }
  368. }
  369. jsonWriter.WriteEndArray();
  370. }
  371. public void ConvertPrimaryZoneToNSEC(HttpListenerRequest request)
  372. {
  373. string zoneName = request.QueryString["zone"];
  374. if (string.IsNullOrEmpty(zoneName))
  375. throw new DnsWebServiceException("Parameter 'zone' missing.");
  376. zoneName = zoneName.TrimEnd('.');
  377. _dnsWebService.DnsServer.AuthZoneManager.ConvertPrimaryZoneToNSEC(zoneName);
  378. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Primary zone was converted to NSEC successfully: " + zoneName);
  379. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  380. }
  381. public void ConvertPrimaryZoneToNSEC3(HttpListenerRequest request)
  382. {
  383. string zoneName = request.QueryString["zone"];
  384. if (string.IsNullOrEmpty(zoneName))
  385. throw new DnsWebServiceException("Parameter 'zone' missing.");
  386. zoneName = zoneName.TrimEnd('.');
  387. ushort iterations = 0;
  388. string strIterations = request.QueryString["iterations"];
  389. if (!string.IsNullOrEmpty(strIterations))
  390. iterations = ushort.Parse(strIterations);
  391. byte saltLength = 0;
  392. string strSaltLength = request.QueryString["saltLength"];
  393. if (!string.IsNullOrEmpty(strSaltLength))
  394. saltLength = byte.Parse(strSaltLength);
  395. _dnsWebService.DnsServer.AuthZoneManager.ConvertPrimaryZoneToNSEC3(zoneName, iterations, saltLength);
  396. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Primary zone was converted to NSEC3 successfully: " + zoneName);
  397. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  398. }
  399. public void UpdatePrimaryZoneNSEC3Parameters(HttpListenerRequest request)
  400. {
  401. string zoneName = request.QueryString["zone"];
  402. if (string.IsNullOrEmpty(zoneName))
  403. throw new DnsWebServiceException("Parameter 'zone' missing.");
  404. zoneName = zoneName.TrimEnd('.');
  405. ushort iterations = 0;
  406. string strIterations = request.QueryString["iterations"];
  407. if (!string.IsNullOrEmpty(strIterations))
  408. iterations = ushort.Parse(strIterations);
  409. byte saltLength = 0;
  410. string strSaltLength = request.QueryString["saltLength"];
  411. if (!string.IsNullOrEmpty(strSaltLength))
  412. saltLength = byte.Parse(strSaltLength);
  413. _dnsWebService.DnsServer.AuthZoneManager.UpdatePrimaryZoneNSEC3Parameters(zoneName, iterations, saltLength);
  414. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Primary zone NSEC3 parameters were updated successfully: " + zoneName);
  415. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  416. }
  417. public void UpdatePrimaryZoneDnssecDnsKeyTtl(HttpListenerRequest request)
  418. {
  419. string zoneName = request.QueryString["zone"];
  420. if (string.IsNullOrEmpty(zoneName))
  421. throw new DnsWebServiceException("Parameter 'zone' missing.");
  422. zoneName = zoneName.TrimEnd('.');
  423. string strDnsKeyTtl = request.QueryString["ttl"];
  424. if (string.IsNullOrEmpty(strDnsKeyTtl))
  425. throw new DnsWebServiceException("Parameter 'ttl' missing.");
  426. uint dnsKeyTtl = uint.Parse(strDnsKeyTtl);
  427. _dnsWebService.DnsServer.AuthZoneManager.UpdatePrimaryZoneDnsKeyTtl(zoneName, dnsKeyTtl);
  428. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Primary zone DNSKEY TTL was updated successfully: " + zoneName);
  429. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  430. }
  431. public void GenerateAndAddPrimaryZoneDnssecPrivateKey(HttpListenerRequest request)
  432. {
  433. string zoneName = request.QueryString["zone"];
  434. if (string.IsNullOrEmpty(zoneName))
  435. throw new DnsWebServiceException("Parameter 'zone' missing.");
  436. zoneName = zoneName.TrimEnd('.');
  437. string strKeyType = request.QueryString["keyType"];
  438. if (string.IsNullOrEmpty(strKeyType))
  439. throw new DnsWebServiceException("Parameter 'keyType' missing.");
  440. DnssecPrivateKeyType keyType = Enum.Parse<DnssecPrivateKeyType>(strKeyType, true);
  441. ushort rolloverDays;
  442. string strRolloverDays = request.QueryString["rolloverDays"];
  443. if (string.IsNullOrEmpty(strRolloverDays))
  444. rolloverDays = (ushort)(keyType == DnssecPrivateKeyType.ZoneSigningKey ? 90 : 0);
  445. else
  446. rolloverDays = ushort.Parse(strRolloverDays);
  447. string algorithm = request.QueryString["algorithm"];
  448. if (string.IsNullOrEmpty(algorithm))
  449. throw new DnsWebServiceException("Parameter 'algorithm' missing.");
  450. switch (algorithm.ToUpper())
  451. {
  452. case "RSA":
  453. string hashAlgorithm = request.QueryString["hashAlgorithm"];
  454. if (string.IsNullOrEmpty(hashAlgorithm))
  455. throw new DnsWebServiceException("Parameter 'hashAlgorithm' missing.");
  456. string strKeySize = request.QueryString["keySize"];
  457. if (string.IsNullOrEmpty(strKeySize))
  458. throw new DnsWebServiceException("Parameter 'keySize' missing.");
  459. int keySize = int.Parse(strKeySize);
  460. _dnsWebService.DnsServer.AuthZoneManager.GenerateAndAddPrimaryZoneDnssecRsaPrivateKey(zoneName, keyType, hashAlgorithm, keySize, rolloverDays);
  461. break;
  462. case "ECDSA":
  463. string curve = request.QueryString["curve"];
  464. if (string.IsNullOrEmpty(curve))
  465. throw new DnsWebServiceException("Parameter 'curve' missing.");
  466. _dnsWebService.DnsServer.AuthZoneManager.GenerateAndAddPrimaryZoneDnssecEcdsaPrivateKey(zoneName, keyType, curve, rolloverDays);
  467. break;
  468. default:
  469. throw new NotSupportedException("Algorithm is not supported: " + algorithm);
  470. }
  471. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DNSSEC private key was generated and added to the primary zone successfully: " + zoneName);
  472. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  473. }
  474. public void UpdatePrimaryZoneDnssecPrivateKey(HttpListenerRequest request)
  475. {
  476. string zoneName = request.QueryString["zone"];
  477. if (string.IsNullOrEmpty(zoneName))
  478. throw new DnsWebServiceException("Parameter 'zone' missing.");
  479. zoneName = zoneName.TrimEnd('.');
  480. string strKeyTag = request.QueryString["keyTag"];
  481. if (string.IsNullOrEmpty(strKeyTag))
  482. throw new DnsWebServiceException("Parameter 'keyTag' missing.");
  483. ushort keyTag = ushort.Parse(strKeyTag);
  484. string strRolloverDays = request.QueryString["rolloverDays"];
  485. if (string.IsNullOrEmpty(strRolloverDays))
  486. throw new DnsWebServiceException("Parameter 'rolloverDays' missing.");
  487. ushort rolloverDays = ushort.Parse(strRolloverDays);
  488. _dnsWebService.DnsServer.AuthZoneManager.UpdatePrimaryZoneDnssecPrivateKey(zoneName, keyTag, rolloverDays);
  489. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Primary zone DNSSEC private key config was updated successfully: " + zoneName);
  490. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  491. }
  492. public void DeletePrimaryZoneDnssecPrivateKey(HttpListenerRequest request)
  493. {
  494. string zoneName = request.QueryString["zone"];
  495. if (string.IsNullOrEmpty(zoneName))
  496. throw new DnsWebServiceException("Parameter 'zone' missing.");
  497. zoneName = zoneName.TrimEnd('.');
  498. string strKeyTag = request.QueryString["keyTag"];
  499. if (string.IsNullOrEmpty(strKeyTag))
  500. throw new DnsWebServiceException("Parameter 'keyTag' missing.");
  501. ushort keyTag = ushort.Parse(strKeyTag);
  502. _dnsWebService.DnsServer.AuthZoneManager.DeletePrimaryZoneDnssecPrivateKey(zoneName, keyTag);
  503. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DNSSEC private key was deleted from primary zone successfully: " + zoneName);
  504. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  505. }
  506. public void PublishAllGeneratedPrimaryZoneDnssecPrivateKeys(HttpListenerRequest request)
  507. {
  508. string zoneName = request.QueryString["zone"];
  509. if (string.IsNullOrEmpty(zoneName))
  510. throw new DnsWebServiceException("Parameter 'zone' missing.");
  511. zoneName = zoneName.TrimEnd('.');
  512. _dnsWebService.DnsServer.AuthZoneManager.PublishAllGeneratedPrimaryZoneDnssecPrivateKeys(zoneName);
  513. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] All DNSSEC private keys from the primary zone were published successfully: " + zoneName);
  514. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  515. }
  516. public void RolloverPrimaryZoneDnsKey(HttpListenerRequest request)
  517. {
  518. string zoneName = request.QueryString["zone"];
  519. if (string.IsNullOrEmpty(zoneName))
  520. throw new DnsWebServiceException("Parameter 'zone' missing.");
  521. zoneName = zoneName.TrimEnd('.');
  522. string strKeyTag = request.QueryString["keyTag"];
  523. if (string.IsNullOrEmpty(strKeyTag))
  524. throw new DnsWebServiceException("Parameter 'keyTag' missing.");
  525. ushort keyTag = ushort.Parse(strKeyTag);
  526. _dnsWebService.DnsServer.AuthZoneManager.RolloverPrimaryZoneDnsKey(zoneName, keyTag);
  527. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] The DNSKEY (" + keyTag + ") from the primary zone was rolled over successfully: " + zoneName);
  528. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  529. }
  530. public void RetirePrimaryZoneDnsKey(HttpListenerRequest request)
  531. {
  532. string zoneName = request.QueryString["zone"];
  533. if (string.IsNullOrEmpty(zoneName))
  534. throw new DnsWebServiceException("Parameter 'zone' missing.");
  535. zoneName = zoneName.TrimEnd('.');
  536. string strKeyTag = request.QueryString["keyTag"];
  537. if (string.IsNullOrEmpty(strKeyTag))
  538. throw new DnsWebServiceException("Parameter 'keyTag' missing.");
  539. ushort keyTag = ushort.Parse(strKeyTag);
  540. _dnsWebService.DnsServer.AuthZoneManager.RetirePrimaryZoneDnsKey(zoneName, keyTag);
  541. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] The DNSKEY (" + keyTag + ") from the primary zone was retired successfully: " + zoneName);
  542. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneName);
  543. }
  544. public void DeleteZone(HttpListenerRequest request)
  545. {
  546. string zoneName = request.QueryString["zone"];
  547. if (string.IsNullOrEmpty(zoneName))
  548. zoneName = request.QueryString["domain"];
  549. if (string.IsNullOrEmpty(zoneName))
  550. throw new DnsWebServiceException("Parameter 'zone' missing.");
  551. zoneName = zoneName.TrimEnd('.');
  552. AuthZoneInfo zoneInfo = _dnsWebService.DnsServer.AuthZoneManager.GetAuthZoneInfo(zoneName);
  553. if (zoneInfo is null)
  554. throw new DnsWebServiceException("No such zone was found: " + zoneName);
  555. if (zoneInfo.Internal)
  556. throw new DnsWebServiceException("Access was denied to manage internal DNS Server zone.");
  557. if (!_dnsWebService.DnsServer.AuthZoneManager.DeleteZone(zoneName))
  558. throw new DnsWebServiceException("No authoritative zone was not found for domain: " + zoneName);
  559. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] " + zoneInfo.Type.ToString() + " zone was deleted: " + zoneName);
  560. _dnsWebService.DnsServer.AuthZoneManager.DeleteZoneFile(zoneInfo.Name);
  561. }
  562. public void EnableZone(HttpListenerRequest request)
  563. {
  564. string zoneName = request.QueryString["zone"];
  565. if (string.IsNullOrEmpty(zoneName))
  566. zoneName = request.QueryString["domain"];
  567. if (string.IsNullOrEmpty(zoneName))
  568. throw new DnsWebServiceException("Parameter 'zone' missing.");
  569. zoneName = zoneName.TrimEnd('.');
  570. AuthZoneInfo zoneInfo = _dnsWebService.DnsServer.AuthZoneManager.GetAuthZoneInfo(zoneName);
  571. if (zoneInfo == null)
  572. throw new DnsWebServiceException("No authoritative zone was not found for domain: " + zoneName);
  573. if (zoneInfo.Internal)
  574. throw new DnsWebServiceException("Access was denied to manage internal DNS Server zone.");
  575. zoneInfo.Disabled = false;
  576. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] " + zoneInfo.Type.ToString() + " zone was enabled: " + zoneInfo.Name);
  577. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneInfo.Name);
  578. //delete cache for this zone to allow rebuilding cache data as needed by stub or forwarder zones
  579. _dnsWebService.DnsServer.CacheZoneManager.DeleteZone(zoneInfo.Name);
  580. }
  581. public void DisableZone(HttpListenerRequest request)
  582. {
  583. string zoneName = request.QueryString["zone"];
  584. if (string.IsNullOrEmpty(zoneName))
  585. zoneName = request.QueryString["domain"];
  586. if (string.IsNullOrEmpty(zoneName))
  587. throw new DnsWebServiceException("Parameter 'zone' missing.");
  588. zoneName = zoneName.TrimEnd('.');
  589. AuthZoneInfo zoneInfo = _dnsWebService.DnsServer.AuthZoneManager.GetAuthZoneInfo(zoneName);
  590. if (zoneInfo == null)
  591. throw new DnsWebServiceException("No authoritative zone was not found for domain: " + zoneName);
  592. if (zoneInfo.Internal)
  593. throw new DnsWebServiceException("Access was denied to manage internal DNS Server zone.");
  594. zoneInfo.Disabled = true;
  595. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] " + zoneInfo.Type.ToString() + " zone was disabled: " + zoneInfo.Name);
  596. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneInfo.Name);
  597. }
  598. public void GetZoneOptions(HttpListenerRequest request, JsonTextWriter jsonWriter)
  599. {
  600. string zoneName = request.QueryString["zone"];
  601. if (string.IsNullOrEmpty(zoneName))
  602. zoneName = request.QueryString["domain"];
  603. if (string.IsNullOrEmpty(zoneName))
  604. throw new DnsWebServiceException("Parameter 'zone' missing.");
  605. zoneName = zoneName.TrimEnd('.');
  606. AuthZoneInfo zoneInfo = _dnsWebService.DnsServer.AuthZoneManager.GetAuthZoneInfo(zoneName);
  607. if (zoneInfo is null)
  608. throw new DnsWebServiceException("No such zone was found: " + zoneName);
  609. if (zoneInfo.Internal)
  610. throw new DnsWebServiceException("Access was denied to manage internal DNS Server zone.");
  611. jsonWriter.WritePropertyName("name");
  612. jsonWriter.WriteValue(zoneInfo.Name);
  613. jsonWriter.WritePropertyName("type");
  614. jsonWriter.WriteValue(zoneInfo.Type.ToString());
  615. switch (zoneInfo.Type)
  616. {
  617. case AuthZoneType.Primary:
  618. jsonWriter.WritePropertyName("internal");
  619. jsonWriter.WriteValue(zoneInfo.Internal);
  620. jsonWriter.WritePropertyName("dnssecStatus");
  621. jsonWriter.WriteValue(zoneInfo.DnssecStatus.ToString());
  622. break;
  623. case AuthZoneType.Secondary:
  624. jsonWriter.WritePropertyName("dnssecStatus");
  625. jsonWriter.WriteValue(zoneInfo.DnssecStatus.ToString());
  626. break;
  627. }
  628. jsonWriter.WritePropertyName("disabled");
  629. jsonWriter.WriteValue(zoneInfo.Disabled);
  630. jsonWriter.WritePropertyName("zoneTransfer");
  631. jsonWriter.WriteValue(zoneInfo.ZoneTransfer.ToString());
  632. jsonWriter.WritePropertyName("zoneTransferNameServers");
  633. {
  634. jsonWriter.WriteStartArray();
  635. if (zoneInfo.ZoneTransferNameServers is not null)
  636. {
  637. foreach (IPAddress nameServer in zoneInfo.ZoneTransferNameServers)
  638. jsonWriter.WriteValue(nameServer.ToString());
  639. }
  640. jsonWriter.WriteEndArray();
  641. }
  642. jsonWriter.WritePropertyName("notify");
  643. jsonWriter.WriteValue(zoneInfo.Notify.ToString());
  644. jsonWriter.WritePropertyName("notifyNameServers");
  645. {
  646. jsonWriter.WriteStartArray();
  647. if (zoneInfo.NotifyNameServers is not null)
  648. {
  649. foreach (IPAddress nameServer in zoneInfo.NotifyNameServers)
  650. jsonWriter.WriteValue(nameServer.ToString());
  651. }
  652. jsonWriter.WriteEndArray();
  653. }
  654. jsonWriter.WritePropertyName("zoneTransferTsigKeyNames");
  655. {
  656. jsonWriter.WriteStartArray();
  657. if (zoneInfo.TsigKeyNames is not null)
  658. {
  659. foreach (KeyValuePair<string, object> tsigKeyName in zoneInfo.TsigKeyNames)
  660. jsonWriter.WriteValue(tsigKeyName.Key);
  661. }
  662. jsonWriter.WriteEndArray();
  663. }
  664. jsonWriter.WritePropertyName("availableTsigKeyNames");
  665. {
  666. jsonWriter.WriteStartArray();
  667. if (_dnsWebService.DnsServer.TsigKeys is not null)
  668. {
  669. foreach (KeyValuePair<string, TsigKey> tsigKey in _dnsWebService.DnsServer.TsigKeys)
  670. jsonWriter.WriteValue(tsigKey.Key);
  671. }
  672. jsonWriter.WriteEndArray();
  673. }
  674. }
  675. public void SetZoneOptions(HttpListenerRequest request)
  676. {
  677. string zoneName = request.QueryString["zone"];
  678. if (string.IsNullOrEmpty(zoneName))
  679. zoneName = request.QueryString["domain"];
  680. if (string.IsNullOrEmpty(zoneName))
  681. throw new DnsWebServiceException("Parameter 'zone' missing.");
  682. zoneName = zoneName.TrimEnd('.');
  683. AuthZoneInfo zoneInfo = _dnsWebService.DnsServer.AuthZoneManager.GetAuthZoneInfo(zoneName);
  684. if (zoneInfo == null)
  685. throw new DnsWebServiceException("No authoritative zone was not found for domain: " + zoneName);
  686. if (zoneInfo.Internal)
  687. throw new DnsWebServiceException("Access was denied to manage internal DNS Server zone.");
  688. string strDisabled = request.QueryString["disabled"];
  689. if (!string.IsNullOrEmpty(strDisabled))
  690. zoneInfo.Disabled = bool.Parse(strDisabled);
  691. string strZoneTransfer = request.QueryString["zoneTransfer"];
  692. if (!string.IsNullOrEmpty(strZoneTransfer))
  693. zoneInfo.ZoneTransfer = Enum.Parse<AuthZoneTransfer>(strZoneTransfer, true);
  694. string strZoneTransferNameServers = request.QueryString["zoneTransferNameServers"];
  695. if (!string.IsNullOrEmpty(strZoneTransferNameServers))
  696. {
  697. if (strZoneTransferNameServers == "false")
  698. {
  699. zoneInfo.ZoneTransferNameServers = null;
  700. }
  701. else
  702. {
  703. string[] strNameServers = strZoneTransferNameServers.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  704. IPAddress[] nameServers = new IPAddress[strNameServers.Length];
  705. for (int i = 0; i < strNameServers.Length; i++)
  706. nameServers[i] = IPAddress.Parse(strNameServers[i]);
  707. zoneInfo.ZoneTransferNameServers = nameServers;
  708. }
  709. }
  710. string strNotify = request.QueryString["notify"];
  711. if (!string.IsNullOrEmpty(strNotify))
  712. zoneInfo.Notify = Enum.Parse<AuthZoneNotify>(strNotify, true);
  713. string strNotifyNameServers = request.QueryString["notifyNameServers"];
  714. if (!string.IsNullOrEmpty(strNotifyNameServers))
  715. {
  716. if (strNotifyNameServers == "false")
  717. {
  718. zoneInfo.NotifyNameServers = null;
  719. }
  720. else
  721. {
  722. string[] strNameServers = strNotifyNameServers.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  723. IPAddress[] nameServers = new IPAddress[strNameServers.Length];
  724. for (int i = 0; i < strNameServers.Length; i++)
  725. nameServers[i] = IPAddress.Parse(strNameServers[i]);
  726. zoneInfo.NotifyNameServers = nameServers;
  727. }
  728. }
  729. string strZoneTransferTsigKeyNames = request.QueryString["zoneTransferTsigKeyNames"];
  730. if (!string.IsNullOrEmpty(strZoneTransferTsigKeyNames))
  731. {
  732. if (strZoneTransferTsigKeyNames == "false")
  733. {
  734. zoneInfo.TsigKeyNames = null;
  735. }
  736. else
  737. {
  738. string[] strZoneTransferTsigKeyNamesParts = strZoneTransferTsigKeyNames.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  739. Dictionary<string, object> zoneTransferTsigKeyNames = new Dictionary<string, object>(strZoneTransferTsigKeyNamesParts.Length);
  740. for (int i = 0; i < strZoneTransferTsigKeyNamesParts.Length; i++)
  741. zoneTransferTsigKeyNames.Add(strZoneTransferTsigKeyNamesParts[i].ToLower(), null);
  742. zoneInfo.TsigKeyNames = zoneTransferTsigKeyNames;
  743. }
  744. }
  745. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] " + zoneInfo.Type.ToString() + " zone options were updated successfully: " + zoneInfo.Name);
  746. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneInfo.Name);
  747. }
  748. public void ResyncZone(HttpListenerRequest request)
  749. {
  750. string domain = request.QueryString["domain"];
  751. if (string.IsNullOrEmpty(domain))
  752. throw new DnsWebServiceException("Parameter 'domain' missing.");
  753. domain = domain.TrimEnd('.');
  754. AuthZoneInfo zoneInfo = _dnsWebService.DnsServer.AuthZoneManager.GetAuthZoneInfo(domain);
  755. if (zoneInfo == null)
  756. throw new DnsWebServiceException("No authoritative zone was not found for domain: " + domain);
  757. if (zoneInfo.Internal)
  758. throw new DnsWebServiceException("Access was denied to manage internal DNS Server zone.");
  759. switch (zoneInfo.Type)
  760. {
  761. case AuthZoneType.Secondary:
  762. case AuthZoneType.Stub:
  763. zoneInfo.TriggerResync();
  764. break;
  765. default:
  766. throw new DnsWebServiceException("Only Secondary and Stub zones support resync.");
  767. }
  768. }
  769. public void AddRecord(HttpListenerRequest request)
  770. {
  771. string domain = request.QueryString["domain"];
  772. if (string.IsNullOrEmpty(domain))
  773. throw new DnsWebServiceException("Parameter 'domain' missing.");
  774. domain = domain.TrimEnd('.');
  775. string zoneName = request.QueryString["zone"];
  776. if (zoneName is not null)
  777. zoneName = zoneName.TrimEnd('.');
  778. AuthZoneInfo zoneInfo = _dnsWebService.DnsServer.AuthZoneManager.FindAuthZoneInfo(string.IsNullOrEmpty(zoneName) ? domain : zoneName);
  779. if (zoneInfo is null)
  780. throw new DnsWebServiceException("No authoritative zone was not found for domain: " + domain);
  781. if (zoneInfo.Internal)
  782. throw new DnsWebServiceException("Access was denied to manage internal DNS Server zone.");
  783. if (string.IsNullOrEmpty(zoneName))
  784. zoneName = zoneInfo.Name;
  785. string strType = request.QueryString["type"];
  786. if (string.IsNullOrEmpty(strType))
  787. throw new DnsWebServiceException("Parameter 'type' missing.");
  788. DnsResourceRecordType type = (DnsResourceRecordType)Enum.Parse(typeof(DnsResourceRecordType), strType);
  789. string value = request.QueryString["value"];
  790. uint ttl;
  791. string strTtl = request.QueryString["ttl"];
  792. if (string.IsNullOrEmpty(strTtl))
  793. ttl = _defaultRecordTtl;
  794. else
  795. ttl = uint.Parse(strTtl);
  796. bool overwrite = false;
  797. string strOverwrite = request.QueryString["overwrite"];
  798. if (!string.IsNullOrEmpty(strOverwrite))
  799. overwrite = bool.Parse(strOverwrite);
  800. string comments = request.QueryString["comments"];
  801. switch (type)
  802. {
  803. case DnsResourceRecordType.A:
  804. case DnsResourceRecordType.AAAA:
  805. {
  806. string strIPAddress = request.QueryString["ipAddress"];
  807. if (string.IsNullOrEmpty(strIPAddress))
  808. {
  809. if (string.IsNullOrEmpty(value))
  810. throw new DnsWebServiceException("Parameter 'ipAddress' missing.");
  811. strIPAddress = value;
  812. }
  813. IPAddress ipAddress;
  814. if (strIPAddress.Equals("request-ip-address"))
  815. ipAddress = DnsWebService.GetRequestRemoteEndPoint(request).Address;
  816. else
  817. ipAddress = IPAddress.Parse(strIPAddress);
  818. bool ptr = false;
  819. string strPtr = request.QueryString["ptr"];
  820. if (!string.IsNullOrEmpty(strPtr))
  821. ptr = bool.Parse(strPtr);
  822. if (ptr)
  823. {
  824. string ptrDomain = Zone.GetReverseZone(ipAddress, type == DnsResourceRecordType.A ? 32 : 128);
  825. AuthZoneInfo reverseZoneInfo = _dnsWebService.DnsServer.AuthZoneManager.FindAuthZoneInfo(ptrDomain);
  826. if (reverseZoneInfo == null)
  827. {
  828. bool createPtrZone = false;
  829. string strCreatePtrZone = request.QueryString["createPtrZone"];
  830. if (!string.IsNullOrEmpty(strCreatePtrZone))
  831. createPtrZone = bool.Parse(strCreatePtrZone);
  832. if (!createPtrZone)
  833. throw new DnsServerException("No reverse zone available to add PTR record.");
  834. string ptrZone = Zone.GetReverseZone(ipAddress, type == DnsResourceRecordType.A ? 24 : 64);
  835. reverseZoneInfo = _dnsWebService.DnsServer.AuthZoneManager.CreatePrimaryZone(ptrZone, _dnsWebService.DnsServer.ServerDomain, false);
  836. if (reverseZoneInfo == null)
  837. throw new DnsServerException("Failed to create reverse zone to add PTR record: " + ptrZone);
  838. }
  839. if (reverseZoneInfo.Internal)
  840. throw new DnsServerException("Reverse zone '" + reverseZoneInfo.Name + "' is an internal zone.");
  841. if (reverseZoneInfo.Type != AuthZoneType.Primary)
  842. throw new DnsServerException("Reverse zone '" + reverseZoneInfo.Name + "' is not a primary zone.");
  843. _dnsWebService.DnsServer.AuthZoneManager.SetRecords(reverseZoneInfo.Name, ptrDomain, DnsResourceRecordType.PTR, ttl, new DnsPTRRecordData[] { new DnsPTRRecordData(domain) });
  844. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(reverseZoneInfo.Name);
  845. }
  846. DnsResourceRecord newRecord;
  847. if (type == DnsResourceRecordType.A)
  848. newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsARecordData(ipAddress));
  849. else
  850. newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsAAAARecordData(ipAddress));
  851. if (!string.IsNullOrEmpty(comments))
  852. newRecord.SetComments(comments);
  853. if (overwrite)
  854. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newRecord);
  855. else
  856. _dnsWebService.DnsServer.AuthZoneManager.AddRecord(zoneName, newRecord);
  857. }
  858. break;
  859. case DnsResourceRecordType.NS:
  860. {
  861. string nameServer = request.QueryString["nameServer"];
  862. if (string.IsNullOrEmpty(nameServer))
  863. {
  864. if (string.IsNullOrEmpty(value))
  865. throw new DnsWebServiceException("Parameter 'nameServer' missing.");
  866. nameServer = value;
  867. }
  868. string glueAddresses = request.QueryString["glue"];
  869. if (string.IsNullOrEmpty(glueAddresses))
  870. glueAddresses = null;
  871. DnsResourceRecord newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsNSRecordData(nameServer.TrimEnd('.')));
  872. if (glueAddresses != null)
  873. newRecord.SetGlueRecords(glueAddresses);
  874. if (!string.IsNullOrEmpty(comments))
  875. newRecord.SetComments(comments);
  876. if (overwrite)
  877. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newRecord);
  878. else
  879. _dnsWebService.DnsServer.AuthZoneManager.AddRecord(zoneName, newRecord);
  880. }
  881. break;
  882. case DnsResourceRecordType.CNAME:
  883. {
  884. if (!overwrite)
  885. {
  886. IReadOnlyList<DnsResourceRecord> existingRecords = _dnsWebService.DnsServer.AuthZoneManager.GetRecords(zoneName, domain, type);
  887. if (existingRecords.Count > 0)
  888. throw new DnsWebServiceException("Record already exists. Use overwrite option if you wish to overwrite existing records.");
  889. }
  890. string cname = request.QueryString["cname"];
  891. if (string.IsNullOrEmpty(cname))
  892. {
  893. if (string.IsNullOrEmpty(value))
  894. throw new DnsWebServiceException("Parameter 'cname' missing.");
  895. cname = value;
  896. }
  897. DnsResourceRecord newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsCNAMERecordData(cname.TrimEnd('.')));
  898. if (!string.IsNullOrEmpty(comments))
  899. newRecord.SetComments(comments);
  900. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newRecord);
  901. }
  902. break;
  903. case DnsResourceRecordType.PTR:
  904. {
  905. string ptrName = request.QueryString["ptrName"];
  906. if (string.IsNullOrEmpty(ptrName))
  907. {
  908. if (string.IsNullOrEmpty(value))
  909. throw new DnsWebServiceException("Parameter 'ptrName' missing.");
  910. ptrName = value;
  911. }
  912. DnsResourceRecord newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsPTRRecordData(ptrName.TrimEnd('.')));
  913. if (!string.IsNullOrEmpty(comments))
  914. newRecord.SetComments(comments);
  915. if (overwrite)
  916. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newRecord);
  917. else
  918. _dnsWebService.DnsServer.AuthZoneManager.AddRecord(zoneName, newRecord);
  919. }
  920. break;
  921. case DnsResourceRecordType.MX:
  922. {
  923. string exchange = request.QueryString["exchange"];
  924. if (string.IsNullOrEmpty(exchange))
  925. {
  926. if (string.IsNullOrEmpty(value))
  927. throw new DnsWebServiceException("Parameter 'exchange' missing.");
  928. exchange = value;
  929. }
  930. string preference = request.QueryString["preference"];
  931. if (string.IsNullOrEmpty(preference))
  932. throw new DnsWebServiceException("Parameter 'preference' missing.");
  933. DnsResourceRecord newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsMXRecordData(ushort.Parse(preference), exchange.TrimEnd('.')));
  934. if (!string.IsNullOrEmpty(comments))
  935. newRecord.SetComments(comments);
  936. if (overwrite)
  937. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newRecord);
  938. else
  939. _dnsWebService.DnsServer.AuthZoneManager.AddRecord(zoneName, newRecord);
  940. }
  941. break;
  942. case DnsResourceRecordType.TXT:
  943. {
  944. string text = request.QueryString["text"];
  945. if (string.IsNullOrEmpty(text))
  946. {
  947. if (string.IsNullOrEmpty(value))
  948. throw new DnsWebServiceException("Parameter 'text' missing.");
  949. text = value;
  950. }
  951. DnsResourceRecord newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsTXTRecordData(text));
  952. if (!string.IsNullOrEmpty(comments))
  953. newRecord.SetComments(comments);
  954. if (overwrite)
  955. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newRecord);
  956. else
  957. _dnsWebService.DnsServer.AuthZoneManager.AddRecord(zoneName, newRecord);
  958. }
  959. break;
  960. case DnsResourceRecordType.SRV:
  961. {
  962. string priority = request.QueryString["priority"];
  963. if (string.IsNullOrEmpty(priority))
  964. throw new DnsWebServiceException("Parameter 'priority' missing.");
  965. string weight = request.QueryString["weight"];
  966. if (string.IsNullOrEmpty(weight))
  967. throw new DnsWebServiceException("Parameter 'weight' missing.");
  968. string port = request.QueryString["port"];
  969. if (string.IsNullOrEmpty(port))
  970. throw new DnsWebServiceException("Parameter 'port' missing.");
  971. string target = request.QueryString["target"];
  972. if (string.IsNullOrEmpty(target))
  973. {
  974. if (string.IsNullOrEmpty(value))
  975. throw new DnsWebServiceException("Parameter 'target' missing.");
  976. target = value;
  977. }
  978. DnsResourceRecord newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsSRVRecordData(ushort.Parse(priority), ushort.Parse(weight), ushort.Parse(port), target.TrimEnd('.')));
  979. if (!string.IsNullOrEmpty(comments))
  980. newRecord.SetComments(comments);
  981. if (overwrite)
  982. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newRecord);
  983. else
  984. _dnsWebService.DnsServer.AuthZoneManager.AddRecord(zoneName, newRecord);
  985. }
  986. break;
  987. case DnsResourceRecordType.DNAME:
  988. {
  989. if (!overwrite)
  990. {
  991. IReadOnlyList<DnsResourceRecord> existingRecords = _dnsWebService.DnsServer.AuthZoneManager.GetRecords(zoneName, domain, type);
  992. if (existingRecords.Count > 0)
  993. throw new DnsWebServiceException("Record already exists. Use overwrite option if you wish to overwrite existing records.");
  994. }
  995. string dname = request.QueryString["dname"];
  996. if (string.IsNullOrEmpty(dname))
  997. {
  998. if (string.IsNullOrEmpty(value))
  999. throw new DnsWebServiceException("Parameter 'dname' missing.");
  1000. dname = value;
  1001. }
  1002. DnsResourceRecord newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsDNAMERecordData(dname.TrimEnd('.')));
  1003. if (!string.IsNullOrEmpty(comments))
  1004. newRecord.SetComments(comments);
  1005. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newRecord);
  1006. }
  1007. break;
  1008. case DnsResourceRecordType.DS:
  1009. {
  1010. string strKeyTag = request.QueryString["keyTag"];
  1011. if (string.IsNullOrEmpty(strKeyTag))
  1012. throw new DnsWebServiceException("Parameter 'keyTag' missing.");
  1013. string strAlgorithm = request.QueryString["algorithm"];
  1014. if (string.IsNullOrEmpty(strAlgorithm))
  1015. throw new DnsWebServiceException("Parameter 'algorithm' missing.");
  1016. string strDigestType = request.QueryString["digestType"];
  1017. if (string.IsNullOrEmpty(strDigestType))
  1018. throw new DnsWebServiceException("Parameter 'digestType' missing.");
  1019. string digest = request.QueryString["digest"];
  1020. if (string.IsNullOrEmpty(digest))
  1021. {
  1022. if (string.IsNullOrEmpty(value))
  1023. throw new DnsWebServiceException("Parameter 'digest' missing.");
  1024. digest = value;
  1025. }
  1026. DnsResourceRecord newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsDSRecordData(ushort.Parse(strKeyTag), Enum.Parse<DnssecAlgorithm>(strAlgorithm), Enum.Parse<DnssecDigestType>(strDigestType), Convert.FromHexString(digest)));
  1027. if (!string.IsNullOrEmpty(comments))
  1028. newRecord.SetComments(comments);
  1029. if (overwrite)
  1030. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newRecord);
  1031. else
  1032. _dnsWebService.DnsServer.AuthZoneManager.AddRecord(zoneName, newRecord);
  1033. }
  1034. break;
  1035. case DnsResourceRecordType.CAA:
  1036. {
  1037. string flags = request.QueryString["flags"];
  1038. if (string.IsNullOrEmpty(flags))
  1039. throw new DnsWebServiceException("Parameter 'flags' missing.");
  1040. string tag = request.QueryString["tag"];
  1041. if (string.IsNullOrEmpty(tag))
  1042. throw new DnsWebServiceException("Parameter 'tag' missing.");
  1043. if (string.IsNullOrEmpty(value))
  1044. throw new DnsWebServiceException("Parameter 'value' missing.");
  1045. DnsResourceRecord newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsCAARecordData(byte.Parse(flags), tag, value));
  1046. if (!string.IsNullOrEmpty(comments))
  1047. newRecord.SetComments(comments);
  1048. if (overwrite)
  1049. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newRecord);
  1050. else
  1051. _dnsWebService.DnsServer.AuthZoneManager.AddRecord(zoneName, newRecord);
  1052. }
  1053. break;
  1054. case DnsResourceRecordType.ANAME:
  1055. {
  1056. string aname = request.QueryString["aname"];
  1057. if (string.IsNullOrEmpty(aname))
  1058. {
  1059. if (string.IsNullOrEmpty(value))
  1060. throw new DnsWebServiceException("Parameter 'aname' missing.");
  1061. aname = value;
  1062. }
  1063. DnsResourceRecord newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsANAMERecordData(aname.TrimEnd('.')));
  1064. if (!string.IsNullOrEmpty(comments))
  1065. newRecord.SetComments(comments);
  1066. if (overwrite)
  1067. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newRecord);
  1068. else
  1069. _dnsWebService.DnsServer.AuthZoneManager.AddRecord(zoneName, newRecord);
  1070. }
  1071. break;
  1072. case DnsResourceRecordType.FWD:
  1073. {
  1074. DnsTransportProtocol protocol = DnsTransportProtocol.Udp;
  1075. string strProtocol = request.QueryString["protocol"];
  1076. if (!string.IsNullOrEmpty(strProtocol))
  1077. protocol = Enum.Parse<DnsTransportProtocol>(strProtocol, true);
  1078. string forwarder = request.QueryString["forwarder"];
  1079. if (string.IsNullOrEmpty(forwarder))
  1080. {
  1081. if (string.IsNullOrEmpty(value))
  1082. throw new DnsWebServiceException("Parameter 'forwarder' missing.");
  1083. forwarder = value;
  1084. }
  1085. bool dnssecValidation = false;
  1086. string strDnssecValidation = request.QueryString["dnssecValidation"];
  1087. if (!string.IsNullOrEmpty(strDnssecValidation))
  1088. dnssecValidation = bool.Parse(strDnssecValidation);
  1089. NetProxyType proxyType = NetProxyType.None;
  1090. string proxyAddress = null;
  1091. ushort proxyPort = 0;
  1092. string proxyUsername = null;
  1093. string proxyPassword = null;
  1094. if (!forwarder.Equals("this-server"))
  1095. {
  1096. string strProxyType = request.QueryString["proxyType"];
  1097. if (!string.IsNullOrEmpty(strProxyType))
  1098. proxyType = Enum.Parse<NetProxyType>(strProxyType, true);
  1099. if (proxyType != NetProxyType.None)
  1100. {
  1101. proxyAddress = request.QueryString["proxyAddress"];
  1102. if (string.IsNullOrEmpty(proxyAddress))
  1103. throw new DnsWebServiceException("Parameter 'proxyAddress' missing.");
  1104. string strProxyPort = request.QueryString["proxyPort"];
  1105. if (string.IsNullOrEmpty(strProxyPort))
  1106. throw new DnsWebServiceException("Parameter 'proxyPort' missing.");
  1107. proxyPort = ushort.Parse(strProxyPort);
  1108. proxyUsername = request.QueryString["proxyUsername"];
  1109. proxyPassword = request.QueryString["proxyPassword"];
  1110. }
  1111. }
  1112. DnsResourceRecord newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsForwarderRecordData(protocol, forwarder, dnssecValidation, proxyType, proxyAddress, proxyPort, proxyUsername, proxyPassword));
  1113. if (!string.IsNullOrEmpty(comments))
  1114. newRecord.SetComments(comments);
  1115. if (overwrite)
  1116. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newRecord);
  1117. else
  1118. _dnsWebService.DnsServer.AuthZoneManager.AddRecord(zoneName, newRecord);
  1119. }
  1120. break;
  1121. case DnsResourceRecordType.APP:
  1122. {
  1123. string appName = request.QueryString["appName"];
  1124. if (string.IsNullOrEmpty(appName))
  1125. {
  1126. if (string.IsNullOrEmpty(value))
  1127. throw new DnsWebServiceException("Parameter 'appName' missing.");
  1128. appName = value;
  1129. }
  1130. string classPath = request.QueryString["classPath"];
  1131. if (string.IsNullOrEmpty(classPath))
  1132. throw new DnsWebServiceException("Parameter 'classPath' missing.");
  1133. string recordData = request.QueryString["recordData"];
  1134. if (string.IsNullOrEmpty(recordData))
  1135. recordData = "";
  1136. if (!overwrite)
  1137. {
  1138. IReadOnlyList<DnsResourceRecord> existingRecords = _dnsWebService.DnsServer.AuthZoneManager.GetRecords(zoneName, domain, type);
  1139. if (existingRecords.Count > 0)
  1140. throw new DnsWebServiceException("Record already exists. Use overwrite option if you wish to overwrite existing records.");
  1141. }
  1142. DnsResourceRecord newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsApplicationRecordData(appName, classPath, recordData));
  1143. if (!string.IsNullOrEmpty(comments))
  1144. newRecord.SetComments(comments);
  1145. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newRecord);
  1146. }
  1147. break;
  1148. default:
  1149. throw new DnsWebServiceException("Type not supported for AddRecords().");
  1150. }
  1151. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] New record was added to authoritative zone {domain: " + domain + "; type: " + type + "; value: " + value + "; ttl: " + ttl + ";}");
  1152. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneInfo.Name);
  1153. }
  1154. public void GetRecords(HttpListenerRequest request, JsonTextWriter jsonWriter)
  1155. {
  1156. string domain = request.QueryString["domain"];
  1157. if (string.IsNullOrEmpty(domain))
  1158. throw new DnsWebServiceException("Parameter 'domain' missing.");
  1159. domain = domain.TrimEnd('.');
  1160. AuthZoneInfo zoneInfo = _dnsWebService.DnsServer.AuthZoneManager.FindAuthZoneInfo(domain);
  1161. if (zoneInfo is null)
  1162. throw new DnsWebServiceException("No authoritative zone was not found for domain: " + domain);
  1163. jsonWriter.WritePropertyName("zone");
  1164. jsonWriter.WriteStartObject();
  1165. jsonWriter.WritePropertyName("name");
  1166. jsonWriter.WriteValue(zoneInfo.Name);
  1167. jsonWriter.WritePropertyName("type");
  1168. jsonWriter.WriteValue(zoneInfo.Type.ToString());
  1169. switch (zoneInfo.Type)
  1170. {
  1171. case AuthZoneType.Primary:
  1172. jsonWriter.WritePropertyName("internal");
  1173. jsonWriter.WriteValue(zoneInfo.Internal);
  1174. jsonWriter.WritePropertyName("dnssecStatus");
  1175. jsonWriter.WriteValue(zoneInfo.DnssecStatus.ToString());
  1176. break;
  1177. case AuthZoneType.Secondary:
  1178. jsonWriter.WritePropertyName("dnssecStatus");
  1179. jsonWriter.WriteValue(zoneInfo.DnssecStatus.ToString());
  1180. jsonWriter.WritePropertyName("expiry");
  1181. jsonWriter.WriteValue(zoneInfo.Expiry);
  1182. jsonWriter.WritePropertyName("isExpired");
  1183. jsonWriter.WriteValue(zoneInfo.IsExpired);
  1184. break;
  1185. case AuthZoneType.Stub:
  1186. jsonWriter.WritePropertyName("expiry");
  1187. jsonWriter.WriteValue(zoneInfo.Expiry);
  1188. jsonWriter.WritePropertyName("isExpired");
  1189. jsonWriter.WriteValue(zoneInfo.IsExpired);
  1190. break;
  1191. }
  1192. jsonWriter.WritePropertyName("disabled");
  1193. jsonWriter.WriteValue(zoneInfo.Disabled);
  1194. jsonWriter.WriteEndObject();
  1195. List<DnsResourceRecord> records = new List<DnsResourceRecord>();
  1196. _dnsWebService.DnsServer.AuthZoneManager.ListAllRecords(domain, records);
  1197. WriteRecordsAsJson(records, jsonWriter, true, zoneInfo);
  1198. }
  1199. public static void WriteRecordsAsJson(List<DnsResourceRecord> records, JsonTextWriter jsonWriter, bool authoritativeZoneRecords, AuthZoneInfo zoneInfo = null)
  1200. {
  1201. if (records is null)
  1202. {
  1203. jsonWriter.WritePropertyName("records");
  1204. jsonWriter.WriteStartArray();
  1205. jsonWriter.WriteEndArray();
  1206. return;
  1207. }
  1208. records.Sort();
  1209. Dictionary<string, Dictionary<DnsResourceRecordType, List<DnsResourceRecord>>> groupedByDomainRecords = DnsResourceRecord.GroupRecords(records);
  1210. jsonWriter.WritePropertyName("records");
  1211. jsonWriter.WriteStartArray();
  1212. foreach (KeyValuePair<string, Dictionary<DnsResourceRecordType, List<DnsResourceRecord>>> groupedByTypeRecords in groupedByDomainRecords)
  1213. {
  1214. foreach (KeyValuePair<DnsResourceRecordType, List<DnsResourceRecord>> groupedRecords in groupedByTypeRecords.Value)
  1215. {
  1216. foreach (DnsResourceRecord record in groupedRecords.Value)
  1217. {
  1218. jsonWriter.WriteStartObject();
  1219. if (authoritativeZoneRecords)
  1220. {
  1221. jsonWriter.WritePropertyName("disabled");
  1222. jsonWriter.WriteValue(record.IsDisabled());
  1223. }
  1224. jsonWriter.WritePropertyName("name");
  1225. jsonWriter.WriteValue(record.Name);
  1226. jsonWriter.WritePropertyName("type");
  1227. jsonWriter.WriteValue(record.Type.ToString());
  1228. jsonWriter.WritePropertyName("ttl");
  1229. if (authoritativeZoneRecords)
  1230. jsonWriter.WriteValue(record.TtlValue);
  1231. else
  1232. jsonWriter.WriteValue(record.TTL);
  1233. if (authoritativeZoneRecords)
  1234. {
  1235. string comments = record.GetComments();
  1236. if (!string.IsNullOrEmpty(comments))
  1237. {
  1238. jsonWriter.WritePropertyName("comments");
  1239. jsonWriter.WriteValue(comments);
  1240. }
  1241. }
  1242. jsonWriter.WritePropertyName("rData");
  1243. jsonWriter.WriteStartObject();
  1244. DnsResourceRecordInfo recordInfo = record.GetRecordInfo();
  1245. switch (record.Type)
  1246. {
  1247. case DnsResourceRecordType.A:
  1248. {
  1249. if (record.RDATA is DnsARecordData rdata)
  1250. {
  1251. jsonWriter.WritePropertyName("ipAddress");
  1252. jsonWriter.WriteValue(rdata.IPAddress);
  1253. }
  1254. else
  1255. {
  1256. jsonWriter.WritePropertyName("dataType");
  1257. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1258. jsonWriter.WritePropertyName("data");
  1259. jsonWriter.WriteValue(record.RDATA.ToString());
  1260. }
  1261. }
  1262. break;
  1263. case DnsResourceRecordType.NS:
  1264. {
  1265. if (record.RDATA is DnsNSRecordData rdata)
  1266. {
  1267. jsonWriter.WritePropertyName("nameServer");
  1268. jsonWriter.WriteValue(rdata.NameServer.Length == 0 ? "." : rdata.NameServer);
  1269. }
  1270. else
  1271. {
  1272. jsonWriter.WritePropertyName("dataType");
  1273. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1274. jsonWriter.WritePropertyName("data");
  1275. jsonWriter.WriteValue(record.RDATA.ToString());
  1276. }
  1277. }
  1278. break;
  1279. case DnsResourceRecordType.CNAME:
  1280. {
  1281. if (record.RDATA is DnsCNAMERecordData rdata)
  1282. {
  1283. jsonWriter.WritePropertyName("cname");
  1284. jsonWriter.WriteValue(rdata.Domain.Length == 0 ? "." : rdata.Domain);
  1285. }
  1286. else
  1287. {
  1288. jsonWriter.WritePropertyName("dataType");
  1289. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1290. jsonWriter.WritePropertyName("data");
  1291. jsonWriter.WriteValue(record.RDATA.ToString());
  1292. }
  1293. }
  1294. break;
  1295. case DnsResourceRecordType.SOA:
  1296. {
  1297. if (record.RDATA is DnsSOARecordData rdata)
  1298. {
  1299. jsonWriter.WritePropertyName("primaryNameServer");
  1300. jsonWriter.WriteValue(rdata.PrimaryNameServer);
  1301. jsonWriter.WritePropertyName("responsiblePerson");
  1302. jsonWriter.WriteValue(rdata.ResponsiblePerson);
  1303. jsonWriter.WritePropertyName("serial");
  1304. jsonWriter.WriteValue(rdata.Serial);
  1305. jsonWriter.WritePropertyName("refresh");
  1306. jsonWriter.WriteValue(rdata.Refresh);
  1307. jsonWriter.WritePropertyName("retry");
  1308. jsonWriter.WriteValue(rdata.Retry);
  1309. jsonWriter.WritePropertyName("expire");
  1310. jsonWriter.WriteValue(rdata.Expire);
  1311. jsonWriter.WritePropertyName("minimum");
  1312. jsonWriter.WriteValue(rdata.Minimum);
  1313. }
  1314. else
  1315. {
  1316. jsonWriter.WritePropertyName("dataType");
  1317. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1318. jsonWriter.WritePropertyName("data");
  1319. jsonWriter.WriteValue(record.RDATA.ToString());
  1320. }
  1321. if (authoritativeZoneRecords)
  1322. {
  1323. IReadOnlyList<NameServerAddress> primaryNameServers = record.GetPrimaryNameServers();
  1324. if (primaryNameServers.Count > 0)
  1325. {
  1326. string primaryAddresses = null;
  1327. foreach (NameServerAddress primaryNameServer in primaryNameServers)
  1328. {
  1329. if (primaryAddresses == null)
  1330. primaryAddresses = primaryNameServer.OriginalAddress;
  1331. else
  1332. primaryAddresses = primaryAddresses + ", " + primaryNameServer.OriginalAddress;
  1333. }
  1334. jsonWriter.WritePropertyName("primaryAddresses");
  1335. jsonWriter.WriteValue(primaryAddresses);
  1336. }
  1337. if (recordInfo.ZoneTransferProtocol != DnsTransportProtocol.Udp)
  1338. {
  1339. jsonWriter.WritePropertyName("zoneTransferProtocol");
  1340. jsonWriter.WriteValue(recordInfo.ZoneTransferProtocol.ToString());
  1341. }
  1342. if (!string.IsNullOrEmpty(recordInfo.TsigKeyName))
  1343. {
  1344. jsonWriter.WritePropertyName("tsigKeyName");
  1345. jsonWriter.WriteValue(recordInfo.TsigKeyName);
  1346. }
  1347. }
  1348. }
  1349. break;
  1350. case DnsResourceRecordType.PTR:
  1351. {
  1352. if (record.RDATA is DnsPTRRecordData rdata)
  1353. {
  1354. jsonWriter.WritePropertyName("ptrName");
  1355. jsonWriter.WriteValue(rdata.Domain.Length == 0 ? "." : rdata.Domain);
  1356. }
  1357. else
  1358. {
  1359. jsonWriter.WritePropertyName("dataType");
  1360. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1361. jsonWriter.WritePropertyName("data");
  1362. jsonWriter.WriteValue(record.RDATA.ToString());
  1363. }
  1364. }
  1365. break;
  1366. case DnsResourceRecordType.MX:
  1367. {
  1368. if (record.RDATA is DnsMXRecordData rdata)
  1369. {
  1370. jsonWriter.WritePropertyName("preference");
  1371. jsonWriter.WriteValue(rdata.Preference);
  1372. jsonWriter.WritePropertyName("exchange");
  1373. jsonWriter.WriteValue(rdata.Exchange.Length == 0 ? "." : rdata.Exchange);
  1374. }
  1375. else
  1376. {
  1377. jsonWriter.WritePropertyName("dataType");
  1378. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1379. jsonWriter.WritePropertyName("data");
  1380. jsonWriter.WriteValue(record.RDATA.ToString());
  1381. }
  1382. }
  1383. break;
  1384. case DnsResourceRecordType.TXT:
  1385. {
  1386. if (record.RDATA is DnsTXTRecordData rdata)
  1387. {
  1388. jsonWriter.WritePropertyName("text");
  1389. jsonWriter.WriteValue(rdata.Text);
  1390. }
  1391. else
  1392. {
  1393. jsonWriter.WritePropertyName("dataType");
  1394. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1395. jsonWriter.WritePropertyName("data");
  1396. jsonWriter.WriteValue(record.RDATA.ToString());
  1397. }
  1398. }
  1399. break;
  1400. case DnsResourceRecordType.AAAA:
  1401. {
  1402. if (record.RDATA is DnsAAAARecordData rdata)
  1403. {
  1404. jsonWriter.WritePropertyName("ipAddress");
  1405. jsonWriter.WriteValue(rdata.IPAddress);
  1406. }
  1407. else
  1408. {
  1409. jsonWriter.WritePropertyName("dataType");
  1410. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1411. jsonWriter.WritePropertyName("data");
  1412. jsonWriter.WriteValue(record.RDATA.ToString());
  1413. }
  1414. }
  1415. break;
  1416. case DnsResourceRecordType.SRV:
  1417. {
  1418. if (record.RDATA is DnsSRVRecordData rdata)
  1419. {
  1420. jsonWriter.WritePropertyName("priority");
  1421. jsonWriter.WriteValue(rdata.Priority);
  1422. jsonWriter.WritePropertyName("weight");
  1423. jsonWriter.WriteValue(rdata.Weight);
  1424. jsonWriter.WritePropertyName("port");
  1425. jsonWriter.WriteValue(rdata.Port);
  1426. jsonWriter.WritePropertyName("target");
  1427. jsonWriter.WriteValue(rdata.Target.Length == 0 ? "." : rdata.Target);
  1428. }
  1429. else
  1430. {
  1431. jsonWriter.WritePropertyName("dataType");
  1432. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1433. jsonWriter.WritePropertyName("data");
  1434. jsonWriter.WriteValue(record.RDATA.ToString());
  1435. }
  1436. }
  1437. break;
  1438. case DnsResourceRecordType.DNAME:
  1439. {
  1440. if (record.RDATA is DnsDNAMERecordData rdata)
  1441. {
  1442. jsonWriter.WritePropertyName("dname");
  1443. jsonWriter.WriteValue(rdata.Domain.Length == 0 ? "." : rdata.Domain);
  1444. }
  1445. else
  1446. {
  1447. jsonWriter.WritePropertyName("dataType");
  1448. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1449. jsonWriter.WritePropertyName("data");
  1450. jsonWriter.WriteValue(record.RDATA.ToString());
  1451. }
  1452. }
  1453. break;
  1454. case DnsResourceRecordType.DS:
  1455. {
  1456. if (record.RDATA is DnsDSRecordData rdata)
  1457. {
  1458. jsonWriter.WritePropertyName("keyTag");
  1459. jsonWriter.WriteValue(rdata.KeyTag);
  1460. jsonWriter.WritePropertyName("algorithm");
  1461. jsonWriter.WriteValue(rdata.Algorithm.ToString());
  1462. jsonWriter.WritePropertyName("digestType");
  1463. jsonWriter.WriteValue(rdata.DigestType.ToString());
  1464. jsonWriter.WritePropertyName("digest");
  1465. jsonWriter.WriteValue(rdata.Digest);
  1466. }
  1467. else
  1468. {
  1469. jsonWriter.WritePropertyName("dataType");
  1470. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1471. jsonWriter.WritePropertyName("data");
  1472. jsonWriter.WriteValue(record.RDATA.ToString());
  1473. }
  1474. }
  1475. break;
  1476. case DnsResourceRecordType.RRSIG:
  1477. {
  1478. if (record.RDATA is DnsRRSIGRecordData rdata)
  1479. {
  1480. jsonWriter.WritePropertyName("typeCovered");
  1481. jsonWriter.WriteValue(rdata.TypeCovered.ToString());
  1482. jsonWriter.WritePropertyName("algorithm");
  1483. jsonWriter.WriteValue(rdata.Algorithm.ToString());
  1484. jsonWriter.WritePropertyName("labels");
  1485. jsonWriter.WriteValue(rdata.Labels);
  1486. jsonWriter.WritePropertyName("originalTtl");
  1487. jsonWriter.WriteValue(rdata.OriginalTtl);
  1488. jsonWriter.WritePropertyName("signatureExpiration");
  1489. jsonWriter.WriteValue(rdata.SignatureExpiration);
  1490. jsonWriter.WritePropertyName("signatureInception");
  1491. jsonWriter.WriteValue(rdata.SignatureInception);
  1492. jsonWriter.WritePropertyName("keyTag");
  1493. jsonWriter.WriteValue(rdata.KeyTag);
  1494. jsonWriter.WritePropertyName("signersName");
  1495. jsonWriter.WriteValue(rdata.SignersName.Length == 0 ? "." : rdata.SignersName);
  1496. jsonWriter.WritePropertyName("signature");
  1497. jsonWriter.WriteValue(Convert.ToBase64String(rdata.Signature));
  1498. }
  1499. else
  1500. {
  1501. jsonWriter.WritePropertyName("dataType");
  1502. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1503. jsonWriter.WritePropertyName("data");
  1504. jsonWriter.WriteValue(record.RDATA.ToString());
  1505. }
  1506. }
  1507. break;
  1508. case DnsResourceRecordType.NSEC:
  1509. {
  1510. if (record.RDATA is DnsNSECRecordData rdata)
  1511. {
  1512. jsonWriter.WritePropertyName("nextDomainName");
  1513. jsonWriter.WriteValue(rdata.NextDomainName);
  1514. jsonWriter.WritePropertyName("types");
  1515. jsonWriter.WriteStartArray();
  1516. foreach (DnsResourceRecordType type in rdata.Types)
  1517. jsonWriter.WriteValue(type.ToString());
  1518. jsonWriter.WriteEndArray();
  1519. }
  1520. else
  1521. {
  1522. jsonWriter.WritePropertyName("dataType");
  1523. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1524. jsonWriter.WritePropertyName("data");
  1525. jsonWriter.WriteValue(record.RDATA.ToString());
  1526. }
  1527. }
  1528. break;
  1529. case DnsResourceRecordType.DNSKEY:
  1530. {
  1531. if (record.RDATA is DnsDNSKEYRecordData rdata)
  1532. {
  1533. jsonWriter.WritePropertyName("flags");
  1534. jsonWriter.WriteValue(rdata.Flags.ToString());
  1535. jsonWriter.WritePropertyName("protocol");
  1536. jsonWriter.WriteValue(rdata.Protocol);
  1537. jsonWriter.WritePropertyName("algorithm");
  1538. jsonWriter.WriteValue(rdata.Algorithm.ToString());
  1539. jsonWriter.WritePropertyName("publicKey");
  1540. jsonWriter.WriteValue(rdata.PublicKey.ToString());
  1541. jsonWriter.WritePropertyName("computedKeyTag");
  1542. jsonWriter.WriteValue(rdata.ComputedKeyTag);
  1543. if (authoritativeZoneRecords)
  1544. {
  1545. if (zoneInfo.Type == AuthZoneType.Primary)
  1546. {
  1547. foreach (DnssecPrivateKey dnssecPrivateKey in zoneInfo.DnssecPrivateKeys)
  1548. {
  1549. if (dnssecPrivateKey.KeyTag == rdata.ComputedKeyTag)
  1550. {
  1551. jsonWriter.WritePropertyName("dnsKeyState");
  1552. jsonWriter.WriteValue(dnssecPrivateKey.State.ToString());
  1553. break;
  1554. }
  1555. }
  1556. }
  1557. if (rdata.Flags.HasFlag(DnsDnsKeyFlag.SecureEntryPoint))
  1558. {
  1559. jsonWriter.WritePropertyName("computedDigests");
  1560. jsonWriter.WriteStartArray();
  1561. {
  1562. jsonWriter.WriteStartObject();
  1563. jsonWriter.WritePropertyName("digestType");
  1564. jsonWriter.WriteValue("SHA1");
  1565. jsonWriter.WritePropertyName("digest");
  1566. jsonWriter.WriteValue(rdata.CreateDS(record.Name, DnssecDigestType.SHA1).Digest);
  1567. jsonWriter.WriteEndObject();
  1568. }
  1569. {
  1570. jsonWriter.WriteStartObject();
  1571. jsonWriter.WritePropertyName("digestType");
  1572. jsonWriter.WriteValue("SHA256");
  1573. jsonWriter.WritePropertyName("digest");
  1574. jsonWriter.WriteValue(rdata.CreateDS(record.Name, DnssecDigestType.SHA256).Digest);
  1575. jsonWriter.WriteEndObject();
  1576. }
  1577. {
  1578. jsonWriter.WriteStartObject();
  1579. jsonWriter.WritePropertyName("digestType");
  1580. jsonWriter.WriteValue("SHA384");
  1581. jsonWriter.WritePropertyName("digest");
  1582. jsonWriter.WriteValue(rdata.CreateDS(record.Name, DnssecDigestType.SHA384).Digest);
  1583. jsonWriter.WriteEndObject();
  1584. }
  1585. jsonWriter.WriteEndArray();
  1586. }
  1587. }
  1588. }
  1589. else
  1590. {
  1591. jsonWriter.WritePropertyName("dataType");
  1592. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1593. jsonWriter.WritePropertyName("data");
  1594. jsonWriter.WriteValue(record.RDATA.ToString());
  1595. }
  1596. }
  1597. break;
  1598. case DnsResourceRecordType.NSEC3:
  1599. {
  1600. if (record.RDATA is DnsNSEC3RecordData rdata)
  1601. {
  1602. jsonWriter.WritePropertyName("hashAlgorithm");
  1603. jsonWriter.WriteValue(rdata.HashAlgorithm.ToString());
  1604. jsonWriter.WritePropertyName("flags");
  1605. jsonWriter.WriteValue(rdata.Flags.ToString());
  1606. jsonWriter.WritePropertyName("iterations");
  1607. jsonWriter.WriteValue(rdata.Iterations);
  1608. jsonWriter.WritePropertyName("salt");
  1609. jsonWriter.WriteValue(rdata.Salt);
  1610. jsonWriter.WritePropertyName("nextHashedOwnerName");
  1611. jsonWriter.WriteValue(rdata.NextHashedOwnerName);
  1612. jsonWriter.WritePropertyName("types");
  1613. jsonWriter.WriteStartArray();
  1614. foreach (DnsResourceRecordType type in rdata.Types)
  1615. jsonWriter.WriteValue(type.ToString());
  1616. jsonWriter.WriteEndArray();
  1617. }
  1618. else
  1619. {
  1620. jsonWriter.WritePropertyName("dataType");
  1621. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1622. jsonWriter.WritePropertyName("data");
  1623. jsonWriter.WriteValue(record.RDATA.ToString());
  1624. }
  1625. }
  1626. break;
  1627. case DnsResourceRecordType.NSEC3PARAM:
  1628. {
  1629. if (record.RDATA is DnsNSEC3PARAMRecordData rdata)
  1630. {
  1631. jsonWriter.WritePropertyName("hashAlgorithm");
  1632. jsonWriter.WriteValue(rdata.HashAlgorithm.ToString());
  1633. jsonWriter.WritePropertyName("flags");
  1634. jsonWriter.WriteValue(rdata.Flags.ToString());
  1635. jsonWriter.WritePropertyName("iterations");
  1636. jsonWriter.WriteValue(rdata.Iterations);
  1637. jsonWriter.WritePropertyName("salt");
  1638. jsonWriter.WriteValue(rdata.Salt);
  1639. }
  1640. else
  1641. {
  1642. jsonWriter.WritePropertyName("dataType");
  1643. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1644. jsonWriter.WritePropertyName("data");
  1645. jsonWriter.WriteValue(record.RDATA.ToString());
  1646. }
  1647. }
  1648. break;
  1649. case DnsResourceRecordType.CAA:
  1650. {
  1651. if (record.RDATA is DnsCAARecordData rdata)
  1652. {
  1653. jsonWriter.WritePropertyName("flags");
  1654. jsonWriter.WriteValue(rdata.Flags);
  1655. jsonWriter.WritePropertyName("tag");
  1656. jsonWriter.WriteValue(rdata.Tag);
  1657. jsonWriter.WritePropertyName("value");
  1658. jsonWriter.WriteValue(rdata.Value);
  1659. }
  1660. else
  1661. {
  1662. jsonWriter.WritePropertyName("dataType");
  1663. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1664. jsonWriter.WritePropertyName("data");
  1665. jsonWriter.WriteValue(record.RDATA.ToString());
  1666. }
  1667. }
  1668. break;
  1669. case DnsResourceRecordType.ANAME:
  1670. {
  1671. if (record.RDATA is DnsANAMERecordData rdata)
  1672. {
  1673. jsonWriter.WritePropertyName("aname");
  1674. jsonWriter.WriteValue(rdata.Domain.Length == 0 ? "." : rdata.Domain);
  1675. }
  1676. else
  1677. {
  1678. jsonWriter.WritePropertyName("dataType");
  1679. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1680. jsonWriter.WritePropertyName("data");
  1681. jsonWriter.WriteValue(record.RDATA.ToString());
  1682. }
  1683. }
  1684. break;
  1685. case DnsResourceRecordType.FWD:
  1686. {
  1687. if (record.RDATA is DnsForwarderRecordData rdata)
  1688. {
  1689. jsonWriter.WritePropertyName("protocol");
  1690. jsonWriter.WriteValue(rdata.Protocol.ToString());
  1691. jsonWriter.WritePropertyName("forwarder");
  1692. jsonWriter.WriteValue(rdata.Forwarder);
  1693. jsonWriter.WritePropertyName("dnssecValidation");
  1694. jsonWriter.WriteValue(rdata.DnssecValidation);
  1695. jsonWriter.WritePropertyName("proxyType");
  1696. jsonWriter.WriteValue(rdata.ProxyType.ToString());
  1697. if (rdata.ProxyType != NetProxyType.None)
  1698. {
  1699. jsonWriter.WritePropertyName("proxyAddress");
  1700. jsonWriter.WriteValue(rdata.ProxyAddress);
  1701. jsonWriter.WritePropertyName("proxyPort");
  1702. jsonWriter.WriteValue(rdata.ProxyPort);
  1703. jsonWriter.WritePropertyName("proxyUsername");
  1704. jsonWriter.WriteValue(rdata.ProxyUsername);
  1705. jsonWriter.WritePropertyName("proxyPassword");
  1706. jsonWriter.WriteValue(rdata.ProxyPassword);
  1707. }
  1708. }
  1709. }
  1710. break;
  1711. case DnsResourceRecordType.APP:
  1712. {
  1713. if (record.RDATA is DnsApplicationRecordData rdata)
  1714. {
  1715. jsonWriter.WritePropertyName("appName");
  1716. jsonWriter.WriteValue(rdata.AppName);
  1717. jsonWriter.WritePropertyName("classPath");
  1718. jsonWriter.WriteValue(rdata.ClassPath);
  1719. jsonWriter.WritePropertyName("data");
  1720. jsonWriter.WriteValue(rdata.Data);
  1721. }
  1722. }
  1723. break;
  1724. default:
  1725. {
  1726. if (record.RDATA is DnsUnknownRecordData)
  1727. {
  1728. jsonWriter.WritePropertyName("value");
  1729. using (MemoryStream mS = new MemoryStream())
  1730. {
  1731. record.RDATA.WriteTo(mS);
  1732. jsonWriter.WriteValue(Convert.ToBase64String(mS.ToArray()));
  1733. }
  1734. }
  1735. else
  1736. {
  1737. jsonWriter.WritePropertyName("dataType");
  1738. jsonWriter.WriteValue(record.RDATA.GetType().Name);
  1739. jsonWriter.WritePropertyName("data");
  1740. jsonWriter.WriteValue(record.RDATA.ToString());
  1741. }
  1742. }
  1743. break;
  1744. }
  1745. jsonWriter.WriteEndObject();
  1746. IReadOnlyList<DnsResourceRecord> glueRecords = recordInfo.GlueRecords;
  1747. if (glueRecords is not null)
  1748. {
  1749. string glue = null;
  1750. foreach (DnsResourceRecord glueRecord in glueRecords)
  1751. {
  1752. if (glue == null)
  1753. glue = glueRecord.RDATA.ToString();
  1754. else
  1755. glue = glue + ", " + glueRecord.RDATA.ToString();
  1756. }
  1757. jsonWriter.WritePropertyName("glueRecords");
  1758. jsonWriter.WriteValue(glue);
  1759. }
  1760. IReadOnlyList<DnsResourceRecord> rrsigRecords = recordInfo.RRSIGRecords;
  1761. IReadOnlyList<DnsResourceRecord> nsecRecords = recordInfo.NSECRecords;
  1762. if ((rrsigRecords is not null) || (nsecRecords is not null))
  1763. {
  1764. jsonWriter.WritePropertyName("dnssecRecords");
  1765. jsonWriter.WriteStartArray();
  1766. if (rrsigRecords is not null)
  1767. {
  1768. foreach (DnsResourceRecord rrsigRecord in rrsigRecords)
  1769. jsonWriter.WriteValue(rrsigRecord.ToString());
  1770. }
  1771. if (nsecRecords is not null)
  1772. {
  1773. foreach (DnsResourceRecord nsecRecord in nsecRecords)
  1774. jsonWriter.WriteValue(nsecRecord.ToString());
  1775. }
  1776. jsonWriter.WriteEndArray();
  1777. }
  1778. jsonWriter.WritePropertyName("dnssecStatus");
  1779. jsonWriter.WriteValue(record.DnssecStatus.ToString());
  1780. jsonWriter.WriteEndObject();
  1781. }
  1782. }
  1783. }
  1784. jsonWriter.WriteEndArray();
  1785. }
  1786. public void DeleteRecord(HttpListenerRequest request)
  1787. {
  1788. string domain = request.QueryString["domain"];
  1789. if (string.IsNullOrEmpty(domain))
  1790. throw new DnsWebServiceException("Parameter 'domain' missing.");
  1791. domain = domain.TrimEnd('.');
  1792. string zoneName = request.QueryString["zone"];
  1793. if (zoneName is not null)
  1794. zoneName = zoneName.TrimEnd('.');
  1795. AuthZoneInfo zoneInfo = _dnsWebService.DnsServer.AuthZoneManager.FindAuthZoneInfo(string.IsNullOrEmpty(zoneName) ? domain : zoneName);
  1796. if (zoneInfo is null)
  1797. throw new DnsWebServiceException("No authoritative zone was not found for domain: " + domain);
  1798. if (zoneInfo.Internal)
  1799. throw new DnsWebServiceException("Access was denied to manage internal DNS Server zone.");
  1800. if (string.IsNullOrEmpty(zoneName))
  1801. zoneName = zoneInfo.Name;
  1802. string strType = request.QueryString["type"];
  1803. if (string.IsNullOrEmpty(strType))
  1804. throw new DnsWebServiceException("Parameter 'type' missing.");
  1805. DnsResourceRecordType type = (DnsResourceRecordType)Enum.Parse(typeof(DnsResourceRecordType), strType);
  1806. string value = request.QueryString["value"];
  1807. switch (type)
  1808. {
  1809. case DnsResourceRecordType.A:
  1810. case DnsResourceRecordType.AAAA:
  1811. {
  1812. string strIPAddress = request.QueryString["ipAddress"];
  1813. if (string.IsNullOrEmpty(strIPAddress))
  1814. {
  1815. if (string.IsNullOrEmpty(value))
  1816. throw new DnsWebServiceException("Parameter 'ipAddress' missing.");
  1817. strIPAddress = value;
  1818. }
  1819. IPAddress ipAddress = IPAddress.Parse(strIPAddress);
  1820. if (type == DnsResourceRecordType.A)
  1821. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecord(zoneName, domain, type, new DnsARecordData(ipAddress));
  1822. else
  1823. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecord(zoneName, domain, type, new DnsAAAARecordData(ipAddress));
  1824. string ptrDomain = Zone.GetReverseZone(ipAddress, type == DnsResourceRecordType.A ? 32 : 128);
  1825. AuthZoneInfo reverseZoneInfo = _dnsWebService.DnsServer.AuthZoneManager.FindAuthZoneInfo(ptrDomain);
  1826. if ((reverseZoneInfo != null) && !reverseZoneInfo.Internal && (reverseZoneInfo.Type == AuthZoneType.Primary))
  1827. {
  1828. IReadOnlyList<DnsResourceRecord> ptrRecords = _dnsWebService.DnsServer.AuthZoneManager.GetRecords(reverseZoneInfo.Name, ptrDomain, DnsResourceRecordType.PTR);
  1829. if (ptrRecords.Count > 0)
  1830. {
  1831. foreach (DnsResourceRecord ptrRecord in ptrRecords)
  1832. {
  1833. if ((ptrRecord.RDATA as DnsPTRRecordData).Domain.Equals(domain, StringComparison.OrdinalIgnoreCase))
  1834. {
  1835. //delete PTR record and save reverse zone
  1836. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecord(reverseZoneInfo.Name, ptrDomain, DnsResourceRecordType.PTR, ptrRecord.RDATA);
  1837. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(reverseZoneInfo.Name);
  1838. break;
  1839. }
  1840. }
  1841. }
  1842. }
  1843. }
  1844. break;
  1845. case DnsResourceRecordType.NS:
  1846. {
  1847. string nameServer = request.QueryString["nameServer"];
  1848. if (string.IsNullOrEmpty(nameServer))
  1849. {
  1850. if (string.IsNullOrEmpty(value))
  1851. throw new DnsWebServiceException("Parameter 'nameServer' missing.");
  1852. nameServer = value;
  1853. }
  1854. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecord(zoneName, domain, type, new DnsNSRecordData(nameServer));
  1855. }
  1856. break;
  1857. case DnsResourceRecordType.CNAME:
  1858. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecords(zoneName, domain, type);
  1859. break;
  1860. case DnsResourceRecordType.PTR:
  1861. {
  1862. string ptrName = request.QueryString["ptrName"];
  1863. if (string.IsNullOrEmpty(ptrName))
  1864. {
  1865. if (string.IsNullOrEmpty(value))
  1866. throw new DnsWebServiceException("Parameter 'ptrName' missing.");
  1867. ptrName = value;
  1868. }
  1869. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecord(zoneName, domain, type, new DnsPTRRecordData(ptrName));
  1870. }
  1871. break;
  1872. case DnsResourceRecordType.MX:
  1873. {
  1874. string exchange = request.QueryString["exchange"];
  1875. if (string.IsNullOrEmpty(exchange))
  1876. {
  1877. if (string.IsNullOrEmpty(value))
  1878. throw new DnsWebServiceException("Parameter 'exchange' missing.");
  1879. exchange = value;
  1880. }
  1881. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecord(zoneName, domain, type, new DnsMXRecordData(0, exchange));
  1882. }
  1883. break;
  1884. case DnsResourceRecordType.TXT:
  1885. {
  1886. string text = request.QueryString["text"];
  1887. if (string.IsNullOrEmpty(text))
  1888. {
  1889. if (string.IsNullOrEmpty(value))
  1890. throw new DnsWebServiceException("Parameter 'text' missing.");
  1891. text = value;
  1892. }
  1893. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecord(zoneName, domain, type, new DnsTXTRecordData(text));
  1894. }
  1895. break;
  1896. case DnsResourceRecordType.SRV:
  1897. {
  1898. string port = request.QueryString["port"];
  1899. if (string.IsNullOrEmpty(port))
  1900. throw new DnsWebServiceException("Parameter 'port' missing.");
  1901. string target = request.QueryString["target"];
  1902. if (string.IsNullOrEmpty(target))
  1903. {
  1904. if (string.IsNullOrEmpty(value))
  1905. throw new DnsWebServiceException("Parameter 'target' missing.");
  1906. target = value;
  1907. }
  1908. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecord(zoneName, domain, type, new DnsSRVRecordData(0, 0, ushort.Parse(port), target));
  1909. }
  1910. break;
  1911. case DnsResourceRecordType.DNAME:
  1912. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecords(zoneName, domain, type);
  1913. break;
  1914. case DnsResourceRecordType.DS:
  1915. {
  1916. string strKeyTag = request.QueryString["keyTag"];
  1917. if (string.IsNullOrEmpty(strKeyTag))
  1918. throw new DnsWebServiceException("Parameter 'keyTag' missing.");
  1919. string strAlgorithm = request.QueryString["algorithm"];
  1920. if (string.IsNullOrEmpty(strAlgorithm))
  1921. throw new DnsWebServiceException("Parameter 'algorithm' missing.");
  1922. string strDigestType = request.QueryString["digestType"];
  1923. if (string.IsNullOrEmpty(strDigestType))
  1924. throw new DnsWebServiceException("Parameter 'digestType' missing.");
  1925. string digest = request.QueryString["digest"];
  1926. if (string.IsNullOrEmpty(digest))
  1927. {
  1928. if (string.IsNullOrEmpty(value))
  1929. throw new DnsWebServiceException("Parameter 'digest' missing.");
  1930. digest = value;
  1931. }
  1932. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecord(zoneName, domain, type, new DnsDSRecordData(ushort.Parse(strKeyTag), Enum.Parse<DnssecAlgorithm>(strAlgorithm), Enum.Parse<DnssecDigestType>(strDigestType), Convert.FromHexString(digest)));
  1933. }
  1934. break;
  1935. case DnsResourceRecordType.CAA:
  1936. {
  1937. string flags = request.QueryString["flags"];
  1938. if (string.IsNullOrEmpty(flags))
  1939. throw new DnsWebServiceException("Parameter 'flags' missing.");
  1940. string tag = request.QueryString["tag"];
  1941. if (string.IsNullOrEmpty(tag))
  1942. throw new DnsWebServiceException("Parameter 'tag' missing.");
  1943. if (string.IsNullOrEmpty(value))
  1944. throw new DnsWebServiceException("Parameter 'value' missing.");
  1945. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecord(zoneName, domain, type, new DnsCAARecordData(byte.Parse(flags), tag, value));
  1946. }
  1947. break;
  1948. case DnsResourceRecordType.ANAME:
  1949. {
  1950. string aname = request.QueryString["aname"];
  1951. if (string.IsNullOrEmpty(aname))
  1952. {
  1953. if (string.IsNullOrEmpty(value))
  1954. throw new DnsWebServiceException("Parameter 'aname' missing.");
  1955. aname = value;
  1956. }
  1957. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecord(zoneName, domain, type, new DnsANAMERecordData(aname));
  1958. }
  1959. break;
  1960. case DnsResourceRecordType.FWD:
  1961. {
  1962. string strProtocol = request.QueryString["protocol"];
  1963. if (string.IsNullOrEmpty(strProtocol))
  1964. strProtocol = "Udp";
  1965. string forwarder = request.QueryString["forwarder"];
  1966. if (string.IsNullOrEmpty(forwarder))
  1967. {
  1968. if (string.IsNullOrEmpty(value))
  1969. throw new DnsWebServiceException("Parameter 'forwarder' missing.");
  1970. forwarder = value;
  1971. }
  1972. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecord(zoneName, domain, type, new DnsForwarderRecordData(Enum.Parse<DnsTransportProtocol>(strProtocol, true), forwarder));
  1973. }
  1974. break;
  1975. case DnsResourceRecordType.APP:
  1976. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecords(zoneName, domain, type);
  1977. break;
  1978. default:
  1979. throw new DnsWebServiceException("Type not supported for DeleteRecord().");
  1980. }
  1981. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Record was deleted from authoritative zone {domain: " + domain + "; type: " + type + ";}");
  1982. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneInfo.Name);
  1983. }
  1984. public void UpdateRecord(HttpListenerRequest request)
  1985. {
  1986. string strType = request.QueryString["type"];
  1987. if (string.IsNullOrEmpty(strType))
  1988. throw new DnsWebServiceException("Parameter 'type' missing.");
  1989. DnsResourceRecordType type = (DnsResourceRecordType)Enum.Parse(typeof(DnsResourceRecordType), strType);
  1990. string domain = request.QueryString["domain"];
  1991. if (string.IsNullOrEmpty(domain))
  1992. throw new DnsWebServiceException("Parameter 'domain' missing.");
  1993. domain = domain.TrimEnd('.');
  1994. string zoneName = request.QueryString["zone"];
  1995. if (zoneName is not null)
  1996. zoneName = zoneName.TrimEnd('.');
  1997. AuthZoneInfo zoneInfo = _dnsWebService.DnsServer.AuthZoneManager.FindAuthZoneInfo(string.IsNullOrEmpty(zoneName) ? domain : zoneName);
  1998. if (zoneInfo is null)
  1999. throw new DnsWebServiceException("No authoritative zone was not found for domain: " + domain);
  2000. if (zoneInfo.Internal)
  2001. throw new DnsWebServiceException("Access was denied to manage internal DNS Server zone.");
  2002. if (string.IsNullOrEmpty(zoneName))
  2003. zoneName = zoneInfo.Name;
  2004. string newDomain = request.QueryString["newDomain"];
  2005. if (string.IsNullOrEmpty(newDomain))
  2006. newDomain = domain;
  2007. newDomain = newDomain.TrimEnd('.');
  2008. uint ttl;
  2009. string strTtl = request.QueryString["ttl"];
  2010. if (string.IsNullOrEmpty(strTtl))
  2011. ttl = _defaultRecordTtl;
  2012. else
  2013. ttl = uint.Parse(strTtl);
  2014. string value = request.QueryString["value"];
  2015. string newValue = request.QueryString["newValue"];
  2016. bool disable = false;
  2017. string strDisable = request.QueryString["disable"];
  2018. if (!string.IsNullOrEmpty(strDisable))
  2019. disable = bool.Parse(strDisable);
  2020. string comments = request.QueryString["comments"];
  2021. switch (type)
  2022. {
  2023. case DnsResourceRecordType.A:
  2024. case DnsResourceRecordType.AAAA:
  2025. {
  2026. string strIPAddress = request.QueryString["ipAddress"];
  2027. if (string.IsNullOrEmpty(strIPAddress))
  2028. {
  2029. if (string.IsNullOrEmpty(value))
  2030. throw new DnsWebServiceException("Parameter 'ipAddress' missing.");
  2031. strIPAddress = value;
  2032. }
  2033. IPAddress oldIpAddress = IPAddress.Parse(strIPAddress);
  2034. string strNewIPAddress = request.QueryString["newIpAddress"];
  2035. if (string.IsNullOrEmpty(strNewIPAddress))
  2036. {
  2037. if (string.IsNullOrEmpty(newValue))
  2038. newValue = strIPAddress;
  2039. strNewIPAddress = newValue;
  2040. }
  2041. IPAddress newIpAddress = IPAddress.Parse(strNewIPAddress);
  2042. bool ptr = false;
  2043. string strPtr = request.QueryString["ptr"];
  2044. if (!string.IsNullOrEmpty(strPtr))
  2045. ptr = bool.Parse(strPtr);
  2046. if (ptr)
  2047. {
  2048. string ptrDomain = Zone.GetReverseZone(newIpAddress, type == DnsResourceRecordType.A ? 32 : 128);
  2049. AuthZoneInfo reverseZoneInfo = _dnsWebService.DnsServer.AuthZoneManager.FindAuthZoneInfo(ptrDomain);
  2050. if (reverseZoneInfo == null)
  2051. {
  2052. bool createPtrZone = false;
  2053. string strCreatePtrZone = request.QueryString["createPtrZone"];
  2054. if (!string.IsNullOrEmpty(strCreatePtrZone))
  2055. createPtrZone = bool.Parse(strCreatePtrZone);
  2056. if (!createPtrZone)
  2057. throw new DnsServerException("No reverse zone available to add PTR record.");
  2058. string ptrZone = Zone.GetReverseZone(newIpAddress, type == DnsResourceRecordType.A ? 24 : 64);
  2059. reverseZoneInfo = _dnsWebService.DnsServer.AuthZoneManager.CreatePrimaryZone(ptrZone, _dnsWebService.DnsServer.ServerDomain, false);
  2060. if (reverseZoneInfo == null)
  2061. throw new DnsServerException("Failed to create reverse zone to add PTR record: " + ptrZone);
  2062. }
  2063. if (reverseZoneInfo.Internal)
  2064. throw new DnsServerException("Reverse zone '" + reverseZoneInfo.Name + "' is an internal zone.");
  2065. if (reverseZoneInfo.Type != AuthZoneType.Primary)
  2066. throw new DnsServerException("Reverse zone '" + reverseZoneInfo.Name + "' is not a primary zone.");
  2067. string oldPtrDomain = Zone.GetReverseZone(oldIpAddress, type == DnsResourceRecordType.A ? 32 : 128);
  2068. AuthZoneInfo oldReverseZoneInfo = _dnsWebService.DnsServer.AuthZoneManager.FindAuthZoneInfo(oldPtrDomain);
  2069. if ((oldReverseZoneInfo != null) && !oldReverseZoneInfo.Internal && (oldReverseZoneInfo.Type == AuthZoneType.Primary))
  2070. {
  2071. //delete old PTR record if any and save old reverse zone
  2072. _dnsWebService.DnsServer.AuthZoneManager.DeleteRecords(oldReverseZoneInfo.Name, oldPtrDomain, DnsResourceRecordType.PTR);
  2073. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(oldReverseZoneInfo.Name);
  2074. }
  2075. //add new PTR record and save reverse zone
  2076. _dnsWebService.DnsServer.AuthZoneManager.SetRecords(reverseZoneInfo.Name, ptrDomain, DnsResourceRecordType.PTR, ttl, new DnsPTRRecordData[] { new DnsPTRRecordData(domain) });
  2077. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(reverseZoneInfo.Name);
  2078. }
  2079. DnsResourceRecord oldRecord;
  2080. DnsResourceRecord newRecord;
  2081. if (type == DnsResourceRecordType.A)
  2082. {
  2083. oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsARecordData(oldIpAddress));
  2084. newRecord = new DnsResourceRecord(newDomain, type, DnsClass.IN, ttl, new DnsARecordData(newIpAddress));
  2085. }
  2086. else
  2087. {
  2088. oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsAAAARecordData(oldIpAddress));
  2089. newRecord = new DnsResourceRecord(newDomain, type, DnsClass.IN, ttl, new DnsAAAARecordData(newIpAddress));
  2090. }
  2091. if (disable)
  2092. newRecord.Disable();
  2093. if (!string.IsNullOrEmpty(comments))
  2094. newRecord.SetComments(comments);
  2095. _dnsWebService.DnsServer.AuthZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);
  2096. }
  2097. break;
  2098. case DnsResourceRecordType.NS:
  2099. {
  2100. string nameServer = request.QueryString["nameServer"];
  2101. if (string.IsNullOrEmpty(nameServer))
  2102. {
  2103. if (string.IsNullOrEmpty(value))
  2104. throw new DnsWebServiceException("Parameter 'nameServer' missing.");
  2105. nameServer = value;
  2106. }
  2107. string newNameServer = request.QueryString["newNameServer"];
  2108. if (string.IsNullOrEmpty(newNameServer))
  2109. {
  2110. if (string.IsNullOrEmpty(newValue))
  2111. newValue = nameServer;
  2112. newNameServer = newValue;
  2113. }
  2114. DnsResourceRecord oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsNSRecordData(nameServer.TrimEnd('.')));
  2115. DnsResourceRecord newRecord = new DnsResourceRecord(newDomain, type, DnsClass.IN, ttl, new DnsNSRecordData(newNameServer.TrimEnd('.')));
  2116. if (disable)
  2117. newRecord.Disable();
  2118. if (!string.IsNullOrEmpty(comments))
  2119. newRecord.SetComments(comments);
  2120. string glueAddresses = request.QueryString["glue"];
  2121. if (!string.IsNullOrEmpty(glueAddresses))
  2122. newRecord.SetGlueRecords(glueAddresses);
  2123. _dnsWebService.DnsServer.AuthZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);
  2124. }
  2125. break;
  2126. case DnsResourceRecordType.CNAME:
  2127. {
  2128. string cname = request.QueryString["cname"];
  2129. if (string.IsNullOrEmpty(cname))
  2130. {
  2131. if (string.IsNullOrEmpty(value))
  2132. throw new DnsWebServiceException("Parameter 'cname' missing.");
  2133. cname = value;
  2134. }
  2135. DnsResourceRecord oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsCNAMERecordData(cname.TrimEnd('.')));
  2136. DnsResourceRecord newRecord = new DnsResourceRecord(newDomain, type, DnsClass.IN, ttl, new DnsCNAMERecordData(cname.TrimEnd('.')));
  2137. if (disable)
  2138. newRecord.Disable();
  2139. if (!string.IsNullOrEmpty(comments))
  2140. newRecord.SetComments(comments);
  2141. _dnsWebService.DnsServer.AuthZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);
  2142. }
  2143. break;
  2144. case DnsResourceRecordType.SOA:
  2145. {
  2146. string primaryNameServer = request.QueryString["primaryNameServer"];
  2147. if (string.IsNullOrEmpty(primaryNameServer))
  2148. throw new DnsWebServiceException("Parameter 'primaryNameServer' missing.");
  2149. string responsiblePerson = request.QueryString["responsiblePerson"];
  2150. if (string.IsNullOrEmpty(responsiblePerson))
  2151. throw new DnsWebServiceException("Parameter 'responsiblePerson' missing.");
  2152. string serial = request.QueryString["serial"];
  2153. if (string.IsNullOrEmpty(serial))
  2154. throw new DnsWebServiceException("Parameter 'serial' missing.");
  2155. string refresh = request.QueryString["refresh"];
  2156. if (string.IsNullOrEmpty(refresh))
  2157. throw new DnsWebServiceException("Parameter 'refresh' missing.");
  2158. string retry = request.QueryString["retry"];
  2159. if (string.IsNullOrEmpty(retry))
  2160. throw new DnsWebServiceException("Parameter 'retry' missing.");
  2161. string expire = request.QueryString["expire"];
  2162. if (string.IsNullOrEmpty(expire))
  2163. throw new DnsWebServiceException("Parameter 'expire' missing.");
  2164. string minimum = request.QueryString["minimum"];
  2165. if (string.IsNullOrEmpty(minimum))
  2166. throw new DnsWebServiceException("Parameter 'minimum' missing.");
  2167. DnsResourceRecord newSoaRecord = new DnsResourceRecord(domain, type, DnsClass.IN, ttl, new DnsSOARecordData(primaryNameServer, responsiblePerson, uint.Parse(serial), uint.Parse(refresh), uint.Parse(retry), uint.Parse(expire), uint.Parse(minimum)));
  2168. switch (zoneInfo.Type)
  2169. {
  2170. case AuthZoneType.Secondary:
  2171. case AuthZoneType.Stub:
  2172. string primaryAddresses = request.QueryString["primaryAddresses"];
  2173. if (!string.IsNullOrEmpty(primaryAddresses))
  2174. newSoaRecord.SetPrimaryNameServers(primaryAddresses);
  2175. break;
  2176. }
  2177. if (zoneInfo.Type == AuthZoneType.Secondary)
  2178. {
  2179. DnsResourceRecordInfo recordInfo = newSoaRecord.GetRecordInfo();
  2180. string zoneTransferProtocol = request.QueryString["zoneTransferProtocol"];
  2181. if (string.IsNullOrEmpty(zoneTransferProtocol))
  2182. recordInfo.ZoneTransferProtocol = DnsTransportProtocol.Tcp;
  2183. else
  2184. recordInfo.ZoneTransferProtocol = Enum.Parse<DnsTransportProtocol>(zoneTransferProtocol, true);
  2185. string tsigKeyName = request.QueryString["tsigKeyName"];
  2186. if (!string.IsNullOrEmpty(tsigKeyName))
  2187. recordInfo.TsigKeyName = tsigKeyName;
  2188. }
  2189. if (!string.IsNullOrEmpty(comments))
  2190. newSoaRecord.SetComments(comments);
  2191. _dnsWebService.DnsServer.AuthZoneManager.SetRecord(zoneName, newSoaRecord);
  2192. }
  2193. break;
  2194. case DnsResourceRecordType.PTR:
  2195. {
  2196. string ptrName = request.QueryString["ptrName"];
  2197. if (string.IsNullOrEmpty(ptrName))
  2198. {
  2199. if (string.IsNullOrEmpty(value))
  2200. throw new DnsWebServiceException("Parameter 'ptrName' missing.");
  2201. ptrName = value;
  2202. }
  2203. string newPtrName = request.QueryString["newPtrName"];
  2204. if (string.IsNullOrEmpty(newPtrName))
  2205. {
  2206. if (string.IsNullOrEmpty(newValue))
  2207. newValue = ptrName;
  2208. newPtrName = newValue;
  2209. }
  2210. DnsResourceRecord oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsPTRRecordData(ptrName.TrimEnd('.')));
  2211. DnsResourceRecord newRecord = new DnsResourceRecord(newDomain, type, DnsClass.IN, ttl, new DnsPTRRecordData(newPtrName.TrimEnd('.')));
  2212. if (disable)
  2213. newRecord.Disable();
  2214. if (!string.IsNullOrEmpty(comments))
  2215. newRecord.SetComments(comments);
  2216. _dnsWebService.DnsServer.AuthZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);
  2217. }
  2218. break;
  2219. case DnsResourceRecordType.MX:
  2220. {
  2221. string preference = request.QueryString["preference"];
  2222. if (string.IsNullOrEmpty(preference))
  2223. preference = "1";
  2224. string exchange = request.QueryString["exchange"];
  2225. if (string.IsNullOrEmpty(exchange))
  2226. {
  2227. if (string.IsNullOrEmpty(value))
  2228. throw new DnsWebServiceException("Parameter 'exchange' missing.");
  2229. exchange = value;
  2230. }
  2231. string newExchange = request.QueryString["newExchange"];
  2232. if (string.IsNullOrEmpty(newExchange))
  2233. {
  2234. if (string.IsNullOrEmpty(newValue))
  2235. newValue = exchange;
  2236. newExchange = newValue;
  2237. }
  2238. DnsResourceRecord oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsMXRecordData(0, exchange.TrimEnd('.')));
  2239. DnsResourceRecord newRecord = new DnsResourceRecord(newDomain, type, DnsClass.IN, ttl, new DnsMXRecordData(ushort.Parse(preference), newExchange.TrimEnd('.')));
  2240. if (disable)
  2241. newRecord.Disable();
  2242. if (!string.IsNullOrEmpty(comments))
  2243. newRecord.SetComments(comments);
  2244. _dnsWebService.DnsServer.AuthZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);
  2245. }
  2246. break;
  2247. case DnsResourceRecordType.TXT:
  2248. {
  2249. string text = request.QueryString["text"];
  2250. if (string.IsNullOrEmpty(text))
  2251. {
  2252. if (string.IsNullOrEmpty(value))
  2253. throw new DnsWebServiceException("Parameter 'text' missing.");
  2254. text = value;
  2255. }
  2256. string newText = request.QueryString["newText"];
  2257. if (string.IsNullOrEmpty(newText))
  2258. {
  2259. if (string.IsNullOrEmpty(newValue))
  2260. newValue = text;
  2261. newText = newValue;
  2262. }
  2263. DnsResourceRecord oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsTXTRecordData(text));
  2264. DnsResourceRecord newRecord = new DnsResourceRecord(newDomain, type, DnsClass.IN, ttl, new DnsTXTRecordData(newText));
  2265. if (disable)
  2266. newRecord.Disable();
  2267. if (!string.IsNullOrEmpty(comments))
  2268. newRecord.SetComments(comments);
  2269. _dnsWebService.DnsServer.AuthZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);
  2270. }
  2271. break;
  2272. case DnsResourceRecordType.SRV:
  2273. {
  2274. string priority = request.QueryString["priority"];
  2275. if (string.IsNullOrEmpty(priority))
  2276. throw new DnsWebServiceException("Parameter 'priority' missing.");
  2277. string weight = request.QueryString["weight"];
  2278. if (string.IsNullOrEmpty(weight))
  2279. throw new DnsWebServiceException("Parameter 'weight' missing.");
  2280. string port = request.QueryString["port"];
  2281. if (string.IsNullOrEmpty(port))
  2282. throw new DnsWebServiceException("Parameter 'port' missing.");
  2283. string newPort = request.QueryString["newPort"];
  2284. if (string.IsNullOrEmpty(newPort))
  2285. newPort = port;
  2286. string target = request.QueryString["target"];
  2287. if (string.IsNullOrEmpty(target))
  2288. {
  2289. if (string.IsNullOrEmpty(value))
  2290. throw new DnsWebServiceException("Parameter 'target' missing.");
  2291. target = value;
  2292. }
  2293. string newTarget = request.QueryString["newTarget"];
  2294. if (string.IsNullOrEmpty(newTarget))
  2295. {
  2296. if (string.IsNullOrEmpty(newValue))
  2297. newValue = target;
  2298. newTarget = newValue;
  2299. }
  2300. DnsResourceRecord oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsSRVRecordData(0, 0, ushort.Parse(port), target.TrimEnd('.')));
  2301. DnsResourceRecord newRecord = new DnsResourceRecord(newDomain, type, DnsClass.IN, ttl, new DnsSRVRecordData(ushort.Parse(priority), ushort.Parse(weight), ushort.Parse(newPort), newTarget.TrimEnd('.')));
  2302. if (disable)
  2303. newRecord.Disable();
  2304. if (!string.IsNullOrEmpty(comments))
  2305. newRecord.SetComments(comments);
  2306. _dnsWebService.DnsServer.AuthZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);
  2307. }
  2308. break;
  2309. case DnsResourceRecordType.DNAME:
  2310. {
  2311. string dname = request.QueryString["dname"];
  2312. if (string.IsNullOrEmpty(dname))
  2313. {
  2314. if (string.IsNullOrEmpty(value))
  2315. throw new DnsWebServiceException("Parameter 'dname' missing.");
  2316. dname = value;
  2317. }
  2318. DnsResourceRecord oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsDNAMERecordData(dname.TrimEnd('.')));
  2319. DnsResourceRecord newRecord = new DnsResourceRecord(newDomain, type, DnsClass.IN, ttl, new DnsDNAMERecordData(dname.TrimEnd('.')));
  2320. if (disable)
  2321. newRecord.Disable();
  2322. if (!string.IsNullOrEmpty(comments))
  2323. newRecord.SetComments(comments);
  2324. _dnsWebService.DnsServer.AuthZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);
  2325. }
  2326. break;
  2327. case DnsResourceRecordType.DS:
  2328. {
  2329. string strKeyTag = request.QueryString["keyTag"];
  2330. if (string.IsNullOrEmpty(strKeyTag))
  2331. throw new DnsWebServiceException("Parameter 'keyTag' missing.");
  2332. string strNewKeyTag = request.QueryString["newKeyTag"];
  2333. if (string.IsNullOrEmpty(strNewKeyTag))
  2334. strNewKeyTag = strKeyTag;
  2335. string strAlgorithm = request.QueryString["algorithm"];
  2336. if (string.IsNullOrEmpty(strAlgorithm))
  2337. throw new DnsWebServiceException("Parameter 'algorithm' missing.");
  2338. string strNewAlgorithm = request.QueryString["newAlgorithm"];
  2339. if (string.IsNullOrEmpty(strNewAlgorithm))
  2340. strNewAlgorithm = strAlgorithm;
  2341. string strDigestType = request.QueryString["digestType"];
  2342. if (string.IsNullOrEmpty(strDigestType))
  2343. throw new DnsWebServiceException("Parameter 'digestType' missing.");
  2344. string strNewDigestType = request.QueryString["newDigestType"];
  2345. if (string.IsNullOrEmpty(strNewDigestType))
  2346. strNewDigestType = strDigestType;
  2347. string digest = request.QueryString["digest"];
  2348. if (string.IsNullOrEmpty(digest))
  2349. {
  2350. if (string.IsNullOrEmpty(value))
  2351. throw new DnsWebServiceException("Parameter 'digest' missing.");
  2352. digest = value;
  2353. }
  2354. string newDigest = request.QueryString["newDigest"];
  2355. if (string.IsNullOrEmpty(newDigest))
  2356. {
  2357. if (string.IsNullOrEmpty(newValue))
  2358. newValue = digest;
  2359. newDigest = newValue;
  2360. }
  2361. DnsResourceRecord oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsDSRecordData(ushort.Parse(strKeyTag), Enum.Parse<DnssecAlgorithm>(strAlgorithm), Enum.Parse<DnssecDigestType>(strDigestType), Convert.FromHexString(digest)));
  2362. DnsResourceRecord newRecord = new DnsResourceRecord(newDomain, type, DnsClass.IN, ttl, new DnsDSRecordData(ushort.Parse(strNewKeyTag), Enum.Parse<DnssecAlgorithm>(strNewAlgorithm), Enum.Parse<DnssecDigestType>(strNewDigestType), Convert.FromHexString(newDigest)));
  2363. if (disable)
  2364. newRecord.Disable();
  2365. if (!string.IsNullOrEmpty(comments))
  2366. newRecord.SetComments(comments);
  2367. _dnsWebService.DnsServer.AuthZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);
  2368. }
  2369. break;
  2370. case DnsResourceRecordType.CAA:
  2371. {
  2372. string flags = request.QueryString["flags"];
  2373. if (string.IsNullOrEmpty(flags))
  2374. throw new DnsWebServiceException("Parameter 'flags' missing.");
  2375. string newFlags = request.QueryString["newFlags"];
  2376. if (string.IsNullOrEmpty(newFlags))
  2377. newFlags = flags;
  2378. string tag = request.QueryString["tag"];
  2379. if (string.IsNullOrEmpty(tag))
  2380. throw new DnsWebServiceException("Parameter 'tag' missing.");
  2381. string newTag = request.QueryString["newTag"];
  2382. if (string.IsNullOrEmpty(newTag))
  2383. newTag = tag;
  2384. if (string.IsNullOrEmpty(value))
  2385. throw new DnsWebServiceException("Parameter 'value' missing.");
  2386. if (string.IsNullOrEmpty(newValue))
  2387. newValue = value;
  2388. DnsResourceRecord oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsCAARecordData(byte.Parse(flags), tag, value));
  2389. DnsResourceRecord newRecord = new DnsResourceRecord(newDomain, type, DnsClass.IN, ttl, new DnsCAARecordData(byte.Parse(newFlags), newTag, newValue));
  2390. if (disable)
  2391. newRecord.Disable();
  2392. if (!string.IsNullOrEmpty(comments))
  2393. newRecord.SetComments(comments);
  2394. _dnsWebService.DnsServer.AuthZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);
  2395. }
  2396. break;
  2397. case DnsResourceRecordType.ANAME:
  2398. {
  2399. string aname = request.QueryString["aname"];
  2400. if (string.IsNullOrEmpty(aname))
  2401. {
  2402. if (string.IsNullOrEmpty(value))
  2403. throw new DnsWebServiceException("Parameter 'aname' missing.");
  2404. aname = value;
  2405. }
  2406. string newAName = request.QueryString["newAName"];
  2407. if (string.IsNullOrEmpty(newAName))
  2408. {
  2409. if (string.IsNullOrEmpty(newValue))
  2410. newValue = aname;
  2411. newAName = newValue;
  2412. }
  2413. DnsResourceRecord oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsANAMERecordData(aname.TrimEnd('.')));
  2414. DnsResourceRecord newRecord = new DnsResourceRecord(newDomain, type, DnsClass.IN, ttl, new DnsANAMERecordData(newAName.TrimEnd('.')));
  2415. if (disable)
  2416. newRecord.Disable();
  2417. if (!string.IsNullOrEmpty(comments))
  2418. newRecord.SetComments(comments);
  2419. _dnsWebService.DnsServer.AuthZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);
  2420. }
  2421. break;
  2422. case DnsResourceRecordType.FWD:
  2423. {
  2424. DnsTransportProtocol protocol = DnsTransportProtocol.Udp;
  2425. string strProtocol = request.QueryString["protocol"];
  2426. if (!string.IsNullOrEmpty(strProtocol))
  2427. protocol = Enum.Parse<DnsTransportProtocol>(strProtocol, true);
  2428. DnsTransportProtocol newProtocol = protocol;
  2429. string strNewProtocol = request.QueryString["newProtocol"];
  2430. if (!string.IsNullOrEmpty(strNewProtocol))
  2431. newProtocol = Enum.Parse<DnsTransportProtocol>(strNewProtocol, true);
  2432. string forwarder = request.QueryString["forwarder"];
  2433. if (string.IsNullOrEmpty(forwarder))
  2434. {
  2435. if (string.IsNullOrEmpty(value))
  2436. throw new DnsWebServiceException("Parameter 'forwarder' missing.");
  2437. forwarder = value;
  2438. }
  2439. string newForwarder = request.QueryString["newForwarder"];
  2440. if (string.IsNullOrEmpty(newForwarder))
  2441. {
  2442. if (string.IsNullOrEmpty(newValue))
  2443. newValue = forwarder;
  2444. newForwarder = newValue;
  2445. }
  2446. bool dnssecValidation = false;
  2447. string strDnssecValidation = request.QueryString["dnssecValidation"];
  2448. if (!string.IsNullOrEmpty(strDnssecValidation))
  2449. dnssecValidation = bool.Parse(strDnssecValidation);
  2450. NetProxyType proxyType = NetProxyType.None;
  2451. string proxyAddress = null;
  2452. ushort proxyPort = 0;
  2453. string proxyUsername = null;
  2454. string proxyPassword = null;
  2455. if (!newForwarder.Equals("this-server"))
  2456. {
  2457. string strProxyType = request.QueryString["proxyType"];
  2458. if (!string.IsNullOrEmpty(strProxyType))
  2459. proxyType = Enum.Parse<NetProxyType>(strProxyType, true);
  2460. if (proxyType != NetProxyType.None)
  2461. {
  2462. proxyAddress = request.QueryString["proxyAddress"];
  2463. if (string.IsNullOrEmpty(proxyAddress))
  2464. throw new DnsWebServiceException("Parameter 'proxyAddress' missing.");
  2465. string strProxyPort = request.QueryString["proxyPort"];
  2466. if (string.IsNullOrEmpty(strProxyPort))
  2467. throw new DnsWebServiceException("Parameter 'proxyPort' missing.");
  2468. proxyPort = ushort.Parse(strProxyPort);
  2469. proxyUsername = request.QueryString["proxyUsername"];
  2470. proxyPassword = request.QueryString["proxyPassword"];
  2471. }
  2472. }
  2473. DnsResourceRecord oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsForwarderRecordData(protocol, forwarder));
  2474. DnsResourceRecord newRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsForwarderRecordData(newProtocol, newForwarder, dnssecValidation, proxyType, proxyAddress, proxyPort, proxyUsername, proxyPassword));
  2475. if (disable)
  2476. newRecord.Disable();
  2477. if (!string.IsNullOrEmpty(comments))
  2478. newRecord.SetComments(comments);
  2479. _dnsWebService.DnsServer.AuthZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);
  2480. }
  2481. break;
  2482. case DnsResourceRecordType.APP:
  2483. {
  2484. string appName = request.QueryString["appName"];
  2485. if (string.IsNullOrEmpty(appName))
  2486. {
  2487. if (string.IsNullOrEmpty(value))
  2488. throw new DnsWebServiceException("Parameter 'appName' missing.");
  2489. appName = value;
  2490. }
  2491. string classPath = request.QueryString["classPath"];
  2492. if (string.IsNullOrEmpty(classPath))
  2493. throw new DnsWebServiceException("Parameter 'classPath' missing.");
  2494. string recordData = request.QueryString["recordData"];
  2495. if (string.IsNullOrEmpty(recordData))
  2496. recordData = "";
  2497. DnsResourceRecord oldRecord = new DnsResourceRecord(domain, type, DnsClass.IN, 0, new DnsApplicationRecordData(appName, classPath, recordData));
  2498. DnsResourceRecord newRecord = new DnsResourceRecord(newDomain, type, DnsClass.IN, ttl, new DnsApplicationRecordData(appName, classPath, recordData));
  2499. if (disable)
  2500. newRecord.Disable();
  2501. if (!string.IsNullOrEmpty(comments))
  2502. newRecord.SetComments(comments);
  2503. _dnsWebService.DnsServer.AuthZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);
  2504. }
  2505. break;
  2506. default:
  2507. throw new DnsWebServiceException("Type not supported for UpdateRecords().");
  2508. }
  2509. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Record was updated for authoritative zone {oldDomain: " + domain + "; domain: " + newDomain + "; type: " + type + "; oldValue: " + value + "; value: " + newValue + "; ttl: " + ttl + "; disabled: " + disable + ";}");
  2510. _dnsWebService.DnsServer.AuthZoneManager.SaveZoneFile(zoneInfo.Name);
  2511. }
  2512. #endregion
  2513. #region properties
  2514. public uint DefaultRecordTtl
  2515. {
  2516. get { return _defaultRecordTtl; }
  2517. set { _defaultRecordTtl = value; }
  2518. }
  2519. #endregion
  2520. }
  2521. }