WebServiceZonesApi.cs 174 KB

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