WebServiceZonesApi.cs 146 KB

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