WebServiceDhcpApi.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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.Dhcp;
  17. using DnsServerCore.Dhcp.Options;
  18. using Microsoft.AspNetCore.Http;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Net;
  22. using System.Text.Json;
  23. using System.Threading.Tasks;
  24. using TechnitiumLibrary;
  25. namespace DnsServerCore
  26. {
  27. class WebServiceDhcpApi
  28. {
  29. #region variables
  30. readonly DnsWebService _dnsWebService;
  31. #endregion
  32. #region constructor
  33. public WebServiceDhcpApi(DnsWebService dnsWebService)
  34. {
  35. _dnsWebService = dnsWebService;
  36. }
  37. #endregion
  38. #region public
  39. public void ListDhcpLeases(HttpContext context)
  40. {
  41. UserSession session = context.GetCurrentSession();
  42. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.View))
  43. throw new DnsWebServiceException("Access was denied.");
  44. IReadOnlyDictionary<string, Scope> scopes = _dnsWebService.DhcpServer.Scopes;
  45. //sort by name
  46. List<Scope> sortedScopes = new List<Scope>(scopes.Count);
  47. foreach (KeyValuePair<string, Scope> entry in scopes)
  48. sortedScopes.Add(entry.Value);
  49. sortedScopes.Sort();
  50. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  51. jsonWriter.WritePropertyName("leases");
  52. jsonWriter.WriteStartArray();
  53. foreach (Scope scope in sortedScopes)
  54. {
  55. IReadOnlyDictionary<ClientIdentifierOption, Lease> leases = scope.Leases;
  56. //sort by address
  57. List<Lease> sortedLeases = new List<Lease>(leases.Count);
  58. foreach (KeyValuePair<ClientIdentifierOption, Lease> entry in leases)
  59. sortedLeases.Add(entry.Value);
  60. sortedLeases.Sort();
  61. foreach (Lease lease in sortedLeases)
  62. {
  63. jsonWriter.WriteStartObject();
  64. jsonWriter.WriteString("scope", scope.Name);
  65. jsonWriter.WriteString("type", lease.Type.ToString());
  66. jsonWriter.WriteString("hardwareAddress", BitConverter.ToString(lease.HardwareAddress));
  67. jsonWriter.WriteString("clientIdentifier", lease.ClientIdentifier.ToString());
  68. jsonWriter.WriteString("address", lease.Address.ToString());
  69. jsonWriter.WriteString("hostName", lease.HostName);
  70. jsonWriter.WriteString("leaseObtained", lease.LeaseObtained);
  71. jsonWriter.WriteString("leaseExpires", lease.LeaseExpires);
  72. jsonWriter.WriteEndObject();
  73. }
  74. }
  75. jsonWriter.WriteEndArray();
  76. }
  77. public void ListDhcpScopes(HttpContext context)
  78. {
  79. UserSession session = context.GetCurrentSession();
  80. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.View))
  81. throw new DnsWebServiceException("Access was denied.");
  82. IReadOnlyDictionary<string, Scope> scopes = _dnsWebService.DhcpServer.Scopes;
  83. //sort by name
  84. List<Scope> sortedScopes = new List<Scope>(scopes.Count);
  85. foreach (KeyValuePair<string, Scope> entry in scopes)
  86. sortedScopes.Add(entry.Value);
  87. sortedScopes.Sort();
  88. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  89. jsonWriter.WritePropertyName("scopes");
  90. jsonWriter.WriteStartArray();
  91. foreach (Scope scope in sortedScopes)
  92. {
  93. jsonWriter.WriteStartObject();
  94. jsonWriter.WriteString("name", scope.Name);
  95. jsonWriter.WriteBoolean("enabled", scope.Enabled);
  96. jsonWriter.WriteString("startingAddress", scope.StartingAddress.ToString());
  97. jsonWriter.WriteString("endingAddress", scope.EndingAddress.ToString());
  98. jsonWriter.WriteString("subnetMask", scope.SubnetMask.ToString());
  99. jsonWriter.WriteString("networkAddress", scope.NetworkAddress.ToString());
  100. jsonWriter.WriteString("broadcastAddress", scope.BroadcastAddress.ToString());
  101. if (scope.InterfaceAddress is not null)
  102. jsonWriter.WriteString("interfaceAddress", scope.InterfaceAddress.ToString());
  103. jsonWriter.WriteEndObject();
  104. }
  105. jsonWriter.WriteEndArray();
  106. }
  107. public void GetDhcpScope(HttpContext context)
  108. {
  109. UserSession session = context.GetCurrentSession();
  110. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.View))
  111. throw new DnsWebServiceException("Access was denied.");
  112. string scopeName = context.Request.GetQueryOrForm("name");
  113. Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
  114. if (scope is null)
  115. throw new DnsWebServiceException("DHCP scope was not found: " + scopeName);
  116. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  117. jsonWriter.WriteString("name", scope.Name);
  118. jsonWriter.WriteString("startingAddress", scope.StartingAddress.ToString());
  119. jsonWriter.WriteString("endingAddress", scope.EndingAddress.ToString());
  120. jsonWriter.WriteString("subnetMask", scope.SubnetMask.ToString());
  121. jsonWriter.WriteNumber("leaseTimeDays", scope.LeaseTimeDays);
  122. jsonWriter.WriteNumber("leaseTimeHours", scope.LeaseTimeHours);
  123. jsonWriter.WriteNumber("leaseTimeMinutes", scope.LeaseTimeMinutes);
  124. jsonWriter.WriteNumber("offerDelayTime", scope.OfferDelayTime);
  125. jsonWriter.WriteBoolean("pingCheckEnabled", scope.PingCheckEnabled);
  126. jsonWriter.WriteNumber("pingCheckTimeout", scope.PingCheckTimeout);
  127. jsonWriter.WriteNumber("pingCheckRetries", scope.PingCheckRetries);
  128. if (!string.IsNullOrEmpty(scope.DomainName))
  129. jsonWriter.WriteString("domainName", scope.DomainName);
  130. if (scope.DomainSearchList is not null)
  131. {
  132. jsonWriter.WritePropertyName("domainSearchList");
  133. jsonWriter.WriteStartArray();
  134. foreach (string domainSearchString in scope.DomainSearchList)
  135. jsonWriter.WriteStringValue(domainSearchString);
  136. jsonWriter.WriteEndArray();
  137. }
  138. jsonWriter.WriteBoolean("dnsUpdates", scope.DnsUpdates);
  139. jsonWriter.WriteNumber("dnsTtl", scope.DnsTtl);
  140. if (scope.ServerAddress is not null)
  141. jsonWriter.WriteString("serverAddress", scope.ServerAddress.ToString());
  142. if (scope.ServerHostName is not null)
  143. jsonWriter.WriteString("serverHostName", scope.ServerHostName);
  144. if (scope.BootFileName is not null)
  145. jsonWriter.WriteString("bootFileName", scope.BootFileName);
  146. if (scope.RouterAddress is not null)
  147. jsonWriter.WriteString("routerAddress", scope.RouterAddress.ToString());
  148. jsonWriter.WriteBoolean("useThisDnsServer", scope.UseThisDnsServer);
  149. if (scope.DnsServers is not null)
  150. {
  151. jsonWriter.WritePropertyName("dnsServers");
  152. jsonWriter.WriteStartArray();
  153. foreach (IPAddress dnsServer in scope.DnsServers)
  154. jsonWriter.WriteStringValue(dnsServer.ToString());
  155. jsonWriter.WriteEndArray();
  156. }
  157. if (scope.WinsServers is not null)
  158. {
  159. jsonWriter.WritePropertyName("winsServers");
  160. jsonWriter.WriteStartArray();
  161. foreach (IPAddress winsServer in scope.WinsServers)
  162. jsonWriter.WriteStringValue(winsServer.ToString());
  163. jsonWriter.WriteEndArray();
  164. }
  165. if (scope.NtpServers is not null)
  166. {
  167. jsonWriter.WritePropertyName("ntpServers");
  168. jsonWriter.WriteStartArray();
  169. foreach (IPAddress ntpServer in scope.NtpServers)
  170. jsonWriter.WriteStringValue(ntpServer.ToString());
  171. jsonWriter.WriteEndArray();
  172. }
  173. if (scope.NtpServerDomainNames is not null)
  174. {
  175. jsonWriter.WritePropertyName("ntpServerDomainNames");
  176. jsonWriter.WriteStartArray();
  177. foreach (string ntpServerDomainName in scope.NtpServerDomainNames)
  178. jsonWriter.WriteStringValue(ntpServerDomainName);
  179. jsonWriter.WriteEndArray();
  180. }
  181. if (scope.StaticRoutes is not null)
  182. {
  183. jsonWriter.WritePropertyName("staticRoutes");
  184. jsonWriter.WriteStartArray();
  185. foreach (ClasslessStaticRouteOption.Route route in scope.StaticRoutes)
  186. {
  187. jsonWriter.WriteStartObject();
  188. jsonWriter.WriteString("destination", route.Destination.ToString());
  189. jsonWriter.WriteString("subnetMask", route.SubnetMask.ToString());
  190. jsonWriter.WriteString("router", route.Router.ToString());
  191. jsonWriter.WriteEndObject();
  192. }
  193. jsonWriter.WriteEndArray();
  194. }
  195. if (scope.VendorInfo is not null)
  196. {
  197. jsonWriter.WritePropertyName("vendorInfo");
  198. jsonWriter.WriteStartArray();
  199. foreach (KeyValuePair<string, VendorSpecificInformationOption> entry in scope.VendorInfo)
  200. {
  201. jsonWriter.WriteStartObject();
  202. jsonWriter.WriteString("identifier", entry.Key);
  203. jsonWriter.WriteString("information", BitConverter.ToString(entry.Value.Information).Replace('-', ':'));
  204. jsonWriter.WriteEndObject();
  205. }
  206. jsonWriter.WriteEndArray();
  207. }
  208. if (scope.CAPWAPAcIpAddresses is not null)
  209. {
  210. jsonWriter.WritePropertyName("capwapAcIpAddresses");
  211. jsonWriter.WriteStartArray();
  212. foreach (IPAddress acIpAddress in scope.CAPWAPAcIpAddresses)
  213. jsonWriter.WriteStringValue(acIpAddress.ToString());
  214. jsonWriter.WriteEndArray();
  215. }
  216. if (scope.TftpServerAddresses is not null)
  217. {
  218. jsonWriter.WritePropertyName("tftpServerAddresses");
  219. jsonWriter.WriteStartArray();
  220. foreach (IPAddress address in scope.TftpServerAddresses)
  221. jsonWriter.WriteStringValue(address.ToString());
  222. jsonWriter.WriteEndArray();
  223. }
  224. if (scope.GenericOptions is not null)
  225. {
  226. jsonWriter.WritePropertyName("genericOptions");
  227. jsonWriter.WriteStartArray();
  228. foreach (DhcpOption genericOption in scope.GenericOptions)
  229. {
  230. jsonWriter.WriteStartObject();
  231. jsonWriter.WriteNumber("code", (byte)genericOption.Code);
  232. jsonWriter.WriteString("value", BitConverter.ToString(genericOption.RawValue).Replace('-', ':'));
  233. jsonWriter.WriteEndObject();
  234. }
  235. jsonWriter.WriteEndArray();
  236. }
  237. if (scope.Exclusions is not null)
  238. {
  239. jsonWriter.WritePropertyName("exclusions");
  240. jsonWriter.WriteStartArray();
  241. foreach (Exclusion exclusion in scope.Exclusions)
  242. {
  243. jsonWriter.WriteStartObject();
  244. jsonWriter.WriteString("startingAddress", exclusion.StartingAddress.ToString());
  245. jsonWriter.WriteString("endingAddress", exclusion.EndingAddress.ToString());
  246. jsonWriter.WriteEndObject();
  247. }
  248. jsonWriter.WriteEndArray();
  249. }
  250. jsonWriter.WritePropertyName("reservedLeases");
  251. jsonWriter.WriteStartArray();
  252. foreach (Lease reservedLease in scope.ReservedLeases)
  253. {
  254. jsonWriter.WriteStartObject();
  255. jsonWriter.WriteString("hostName", reservedLease.HostName);
  256. jsonWriter.WriteString("hardwareAddress", BitConverter.ToString(reservedLease.HardwareAddress));
  257. jsonWriter.WriteString("address", reservedLease.Address.ToString());
  258. jsonWriter.WriteString("comments", reservedLease.Comments);
  259. jsonWriter.WriteEndObject();
  260. }
  261. jsonWriter.WriteEndArray();
  262. jsonWriter.WriteBoolean("allowOnlyReservedLeases", scope.AllowOnlyReservedLeases);
  263. jsonWriter.WriteBoolean("blockLocallyAdministeredMacAddresses", scope.BlockLocallyAdministeredMacAddresses);
  264. }
  265. public async Task SetDhcpScopeAsync(HttpContext context)
  266. {
  267. UserSession session = context.GetCurrentSession();
  268. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
  269. throw new DnsWebServiceException("Access was denied.");
  270. HttpRequest request = context.Request;
  271. string scopeName = request.GetQueryOrForm("name");
  272. string strStartingAddress = request.QueryOrForm("startingAddress");
  273. string strEndingAddress = request.QueryOrForm("endingAddress");
  274. string strSubnetMask = request.QueryOrForm("subnetMask");
  275. bool scopeExists;
  276. Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
  277. if (scope is null)
  278. {
  279. //scope does not exists; create new scope
  280. if (string.IsNullOrEmpty(strStartingAddress))
  281. throw new DnsWebServiceException("Parameter 'startingAddress' missing.");
  282. if (string.IsNullOrEmpty(strEndingAddress))
  283. throw new DnsWebServiceException("Parameter 'endingAddress' missing.");
  284. if (string.IsNullOrEmpty(strSubnetMask))
  285. throw new DnsWebServiceException("Parameter 'subnetMask' missing.");
  286. scopeExists = false;
  287. scope = new Scope(scopeName, true, IPAddress.Parse(strStartingAddress), IPAddress.Parse(strEndingAddress), IPAddress.Parse(strSubnetMask), _dnsWebService._log);
  288. }
  289. else
  290. {
  291. scopeExists = true;
  292. IPAddress startingAddress = string.IsNullOrEmpty(strStartingAddress) ? scope.StartingAddress : IPAddress.Parse(strStartingAddress);
  293. IPAddress endingAddress = string.IsNullOrEmpty(strEndingAddress) ? scope.EndingAddress : IPAddress.Parse(strEndingAddress);
  294. IPAddress subnetMask = string.IsNullOrEmpty(strSubnetMask) ? scope.SubnetMask : IPAddress.Parse(strSubnetMask);
  295. //validate scope address
  296. foreach (KeyValuePair<string, Scope> entry in _dnsWebService.DhcpServer.Scopes)
  297. {
  298. Scope existingScope = entry.Value;
  299. if (existingScope.Equals(scope))
  300. continue;
  301. if (existingScope.IsAddressInRange(startingAddress) || existingScope.IsAddressInRange(endingAddress))
  302. throw new DhcpServerException("Scope with overlapping range already exists: " + existingScope.StartingAddress.ToString() + "-" + existingScope.EndingAddress.ToString());
  303. }
  304. scope.ChangeNetwork(startingAddress, endingAddress, subnetMask);
  305. }
  306. if (request.TryGetQueryOrForm("leaseTimeDays", ushort.Parse, out ushort leaseTimeDays))
  307. scope.LeaseTimeDays = leaseTimeDays;
  308. if (request.TryGetQueryOrForm("leaseTimeHours", byte.Parse, out byte leaseTimeHours))
  309. scope.LeaseTimeHours = leaseTimeHours;
  310. if (request.TryGetQueryOrForm("leaseTimeMinutes", byte.Parse, out byte leaseTimeMinutes))
  311. scope.LeaseTimeMinutes = leaseTimeMinutes;
  312. if (request.TryGetQueryOrForm("offerDelayTime", ushort.Parse, out ushort offerDelayTime))
  313. scope.OfferDelayTime = offerDelayTime;
  314. if (request.TryGetQueryOrForm("pingCheckEnabled", bool.Parse, out bool pingCheckEnabled))
  315. scope.PingCheckEnabled = pingCheckEnabled;
  316. if (request.TryGetQueryOrForm("pingCheckTimeout", ushort.Parse, out ushort pingCheckTimeout))
  317. scope.PingCheckTimeout = pingCheckTimeout;
  318. if (request.TryGetQueryOrForm("pingCheckRetries", byte.Parse, out byte pingCheckRetries))
  319. scope.PingCheckRetries = pingCheckRetries;
  320. string domainName = request.QueryOrForm("domainName");
  321. if (domainName is not null)
  322. scope.DomainName = domainName.Length == 0 ? null : domainName;
  323. string domainSearchList = request.QueryOrForm("domainSearchList");
  324. if (domainSearchList is not null)
  325. {
  326. if (domainSearchList.Length == 0)
  327. scope.DomainSearchList = null;
  328. else
  329. scope.DomainSearchList = domainSearchList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  330. }
  331. if (request.TryGetQueryOrForm("dnsUpdates", bool.Parse, out bool dnsUpdates))
  332. scope.DnsUpdates = dnsUpdates;
  333. if (request.TryGetQueryOrForm("dnsTtl", uint.Parse, out uint dnsTtl))
  334. scope.DnsTtl = dnsTtl;
  335. string serverAddress = request.QueryOrForm("serverAddress");
  336. if (serverAddress is not null)
  337. scope.ServerAddress = serverAddress.Length == 0 ? null : IPAddress.Parse(serverAddress);
  338. string serverHostName = request.QueryOrForm("serverHostName");
  339. if (serverHostName is not null)
  340. scope.ServerHostName = serverHostName.Length == 0 ? null : serverHostName;
  341. string bootFileName = request.QueryOrForm("bootFileName");
  342. if (bootFileName is not null)
  343. scope.BootFileName = bootFileName.Length == 0 ? null : bootFileName;
  344. string routerAddress = request.QueryOrForm("routerAddress");
  345. if (routerAddress is not null)
  346. scope.RouterAddress = routerAddress.Length == 0 ? null : IPAddress.Parse(routerAddress);
  347. if (request.TryGetQueryOrForm("useThisDnsServer", bool.Parse, out bool useThisDnsServer))
  348. scope.UseThisDnsServer = useThisDnsServer;
  349. if (!scope.UseThisDnsServer)
  350. {
  351. string dnsServers = request.QueryOrForm("dnsServers");
  352. if (dnsServers is not null)
  353. {
  354. if (dnsServers.Length == 0)
  355. scope.DnsServers = null;
  356. else
  357. scope.DnsServers = dnsServers.Split(IPAddress.Parse, ',');
  358. }
  359. }
  360. string winsServers = request.QueryOrForm("winsServers");
  361. if (winsServers is not null)
  362. {
  363. if (winsServers.Length == 0)
  364. scope.WinsServers = null;
  365. else
  366. scope.WinsServers = winsServers.Split(IPAddress.Parse, ',');
  367. }
  368. string ntpServers = request.QueryOrForm("ntpServers");
  369. if (ntpServers is not null)
  370. {
  371. if (ntpServers.Length == 0)
  372. scope.NtpServers = null;
  373. else
  374. scope.NtpServers = ntpServers.Split(IPAddress.Parse, ',');
  375. }
  376. string ntpServerDomainNames = request.QueryOrForm("ntpServerDomainNames");
  377. if (ntpServerDomainNames is not null)
  378. {
  379. if (ntpServerDomainNames.Length == 0)
  380. scope.NtpServerDomainNames = null;
  381. else
  382. scope.NtpServerDomainNames = ntpServerDomainNames.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  383. }
  384. string strStaticRoutes = request.QueryOrForm("staticRoutes");
  385. if (strStaticRoutes is not null)
  386. {
  387. if (strStaticRoutes.Length == 0)
  388. {
  389. scope.StaticRoutes = null;
  390. }
  391. else
  392. {
  393. string[] strStaticRoutesParts = strStaticRoutes.Split('|');
  394. List<ClasslessStaticRouteOption.Route> staticRoutes = new List<ClasslessStaticRouteOption.Route>();
  395. for (int i = 0; i < strStaticRoutesParts.Length; i += 3)
  396. staticRoutes.Add(new ClasslessStaticRouteOption.Route(IPAddress.Parse(strStaticRoutesParts[i + 0]), IPAddress.Parse(strStaticRoutesParts[i + 1]), IPAddress.Parse(strStaticRoutesParts[i + 2])));
  397. scope.StaticRoutes = staticRoutes;
  398. }
  399. }
  400. string strVendorInfo = request.QueryOrForm("vendorInfo");
  401. if (strVendorInfo is not null)
  402. {
  403. if (strVendorInfo.Length == 0)
  404. {
  405. scope.VendorInfo = null;
  406. }
  407. else
  408. {
  409. string[] strVendorInfoParts = strVendorInfo.Split('|');
  410. Dictionary<string, VendorSpecificInformationOption> vendorInfo = new Dictionary<string, VendorSpecificInformationOption>();
  411. for (int i = 0; i < strVendorInfoParts.Length; i += 2)
  412. vendorInfo.Add(strVendorInfoParts[i + 0], new VendorSpecificInformationOption(strVendorInfoParts[i + 1]));
  413. scope.VendorInfo = vendorInfo;
  414. }
  415. }
  416. string capwapAcIpAddresses = request.QueryOrForm("capwapAcIpAddresses");
  417. if (capwapAcIpAddresses is not null)
  418. {
  419. if (capwapAcIpAddresses.Length == 0)
  420. scope.CAPWAPAcIpAddresses = null;
  421. else
  422. scope.CAPWAPAcIpAddresses = capwapAcIpAddresses.Split(IPAddress.Parse, ',');
  423. }
  424. string tftpServerAddresses = request.QueryOrForm("tftpServerAddresses");
  425. if (tftpServerAddresses is not null)
  426. {
  427. if (tftpServerAddresses.Length == 0)
  428. scope.TftpServerAddresses = null;
  429. else
  430. scope.TftpServerAddresses = tftpServerAddresses.Split(IPAddress.Parse, ',');
  431. }
  432. string strGenericOptions = request.QueryOrForm("genericOptions");
  433. if (strGenericOptions is not null)
  434. {
  435. if (strGenericOptions.Length == 0)
  436. {
  437. scope.GenericOptions = null;
  438. }
  439. else
  440. {
  441. string[] strGenericOptionsParts = strGenericOptions.Split('|');
  442. List<DhcpOption> genericOptions = new List<DhcpOption>();
  443. for (int i = 0; i < strGenericOptionsParts.Length; i += 2)
  444. genericOptions.Add(new DhcpOption((DhcpOptionCode)byte.Parse(strGenericOptionsParts[i + 0]), strGenericOptionsParts[i + 1]));
  445. scope.GenericOptions = genericOptions;
  446. }
  447. }
  448. string strExclusions = request.QueryOrForm("exclusions");
  449. if (strExclusions is not null)
  450. {
  451. if (strExclusions.Length == 0)
  452. {
  453. scope.Exclusions = null;
  454. }
  455. else
  456. {
  457. string[] strExclusionsParts = strExclusions.Split('|');
  458. List<Exclusion> exclusions = new List<Exclusion>();
  459. for (int i = 0; i < strExclusionsParts.Length; i += 2)
  460. exclusions.Add(new Exclusion(IPAddress.Parse(strExclusionsParts[i + 0]), IPAddress.Parse(strExclusionsParts[i + 1])));
  461. scope.Exclusions = exclusions;
  462. }
  463. }
  464. string strReservedLeases = request.QueryOrForm("reservedLeases");
  465. if (strReservedLeases is not null)
  466. {
  467. if (strReservedLeases.Length == 0)
  468. {
  469. scope.ReservedLeases = null;
  470. }
  471. else
  472. {
  473. string[] strReservedLeaseParts = strReservedLeases.Split('|');
  474. List<Lease> reservedLeases = new List<Lease>();
  475. for (int i = 0; i < strReservedLeaseParts.Length; i += 4)
  476. reservedLeases.Add(new Lease(LeaseType.Reserved, strReservedLeaseParts[i + 0], DhcpMessageHardwareAddressType.Ethernet, strReservedLeaseParts[i + 1], IPAddress.Parse(strReservedLeaseParts[i + 2]), strReservedLeaseParts[i + 3]));
  477. scope.ReservedLeases = reservedLeases;
  478. }
  479. }
  480. if (request.TryGetQueryOrForm("allowOnlyReservedLeases", bool.Parse, out bool allowOnlyReservedLeases))
  481. scope.AllowOnlyReservedLeases = allowOnlyReservedLeases;
  482. if (request.TryGetQueryOrForm("blockLocallyAdministeredMacAddresses", bool.Parse, out bool blockLocallyAdministeredMacAddresses))
  483. scope.BlockLocallyAdministeredMacAddresses = blockLocallyAdministeredMacAddresses;
  484. if (scopeExists)
  485. {
  486. _dnsWebService.DhcpServer.SaveScope(scopeName);
  487. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope was updated successfully: " + scopeName);
  488. }
  489. else
  490. {
  491. await _dnsWebService.DhcpServer.AddScopeAsync(scope);
  492. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope was added successfully: " + scopeName);
  493. }
  494. if (request.TryGetQueryOrForm("newName", out string newName) && !newName.Equals(scopeName))
  495. {
  496. _dnsWebService.DhcpServer.RenameScope(scopeName, newName);
  497. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope was renamed successfully: '" + scopeName + "' to '" + newName + "'");
  498. }
  499. }
  500. public void AddReservedLease(HttpContext context)
  501. {
  502. UserSession session = context.GetCurrentSession();
  503. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
  504. throw new DnsWebServiceException("Access was denied.");
  505. HttpRequest request = context.Request;
  506. string scopeName = request.GetQueryOrForm("name");
  507. Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
  508. if (scope is null)
  509. throw new DnsWebServiceException("No such scope exists: " + scopeName);
  510. string hostName = request.QueryOrForm("hostName");
  511. string hardwareAddress = request.GetQueryOrForm("hardwareAddress");
  512. string strIpAddress = request.GetQueryOrForm("ipAddress");
  513. string comments = request.QueryOrForm("comments");
  514. Lease reservedLease = new Lease(LeaseType.Reserved, hostName, DhcpMessageHardwareAddressType.Ethernet, hardwareAddress, IPAddress.Parse(strIpAddress), comments);
  515. if (!scope.AddReservedLease(reservedLease))
  516. throw new DnsWebServiceException("Failed to add reserved lease for scope: " + scopeName);
  517. _dnsWebService.DhcpServer.SaveScope(scopeName);
  518. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope reserved lease was added successfully: " + scopeName);
  519. }
  520. public void RemoveReservedLease(HttpContext context)
  521. {
  522. UserSession session = context.GetCurrentSession();
  523. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
  524. throw new DnsWebServiceException("Access was denied.");
  525. HttpRequest request = context.Request;
  526. string scopeName = request.GetQueryOrForm("name");
  527. Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
  528. if (scope is null)
  529. throw new DnsWebServiceException("No such scope exists: " + scopeName);
  530. string hardwareAddress = request.GetQueryOrForm("hardwareAddress");
  531. if (!scope.RemoveReservedLease(hardwareAddress))
  532. throw new DnsWebServiceException("Failed to remove reserved lease for scope: " + scopeName);
  533. _dnsWebService.DhcpServer.SaveScope(scopeName);
  534. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope reserved lease was removed successfully: " + scopeName);
  535. }
  536. public async Task EnableDhcpScopeAsync(HttpContext context)
  537. {
  538. UserSession session = context.GetCurrentSession();
  539. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
  540. throw new DnsWebServiceException("Access was denied.");
  541. string scopeName = context.Request.GetQueryOrForm("name");
  542. await _dnsWebService.DhcpServer.EnableScopeAsync(scopeName, true);
  543. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope was enabled successfully: " + scopeName);
  544. }
  545. public void DisableDhcpScope(HttpContext context)
  546. {
  547. UserSession session = context.GetCurrentSession();
  548. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
  549. throw new DnsWebServiceException("Access was denied.");
  550. string scopeName = context.Request.GetQueryOrForm("name");
  551. _dnsWebService.DhcpServer.DisableScope(scopeName, true);
  552. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope was disabled successfully: " + scopeName);
  553. }
  554. public void DeleteDhcpScope(HttpContext context)
  555. {
  556. UserSession session = context.GetCurrentSession();
  557. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Delete))
  558. throw new DnsWebServiceException("Access was denied.");
  559. string scopeName = context.Request.GetQueryOrForm("name");
  560. _dnsWebService.DhcpServer.DeleteScope(scopeName);
  561. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope was deleted successfully: " + scopeName);
  562. }
  563. public void RemoveDhcpLease(HttpContext context)
  564. {
  565. UserSession session = context.GetCurrentSession();
  566. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Delete))
  567. throw new DnsWebServiceException("Access was denied.");
  568. HttpRequest request = context.Request;
  569. string scopeName = request.GetQueryOrForm("name");
  570. Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
  571. if (scope is null)
  572. throw new DnsWebServiceException("DHCP scope does not exists: " + scopeName);
  573. string clientIdentifier = request.QueryOrForm("clientIdentifier");
  574. string hardwareAddress = request.QueryOrForm("hardwareAddress");
  575. if (!string.IsNullOrEmpty(clientIdentifier))
  576. scope.RemoveLease(ClientIdentifierOption.Parse(clientIdentifier));
  577. else if (!string.IsNullOrEmpty(hardwareAddress))
  578. scope.RemoveLease(hardwareAddress);
  579. else
  580. throw new DnsWebServiceException("Parameter 'hardwareAddress' or 'clientIdentifier' missing. At least one of them must be specified.");
  581. _dnsWebService.DhcpServer.SaveScope(scopeName);
  582. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope's lease was removed successfully: " + scopeName);
  583. }
  584. public void ConvertToReservedLease(HttpContext context)
  585. {
  586. UserSession session = context.GetCurrentSession();
  587. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
  588. throw new DnsWebServiceException("Access was denied.");
  589. HttpRequest request = context.Request;
  590. string scopeName = request.GetQueryOrForm("name");
  591. Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
  592. if (scope == null)
  593. throw new DnsWebServiceException("DHCP scope does not exists: " + scopeName);
  594. string clientIdentifier = request.QueryOrForm("clientIdentifier");
  595. string hardwareAddress = request.QueryOrForm("hardwareAddress");
  596. if (!string.IsNullOrEmpty(clientIdentifier))
  597. scope.ConvertToReservedLease(ClientIdentifierOption.Parse(clientIdentifier));
  598. else if (!string.IsNullOrEmpty(hardwareAddress))
  599. scope.ConvertToReservedLease(hardwareAddress);
  600. else
  601. throw new DnsWebServiceException("Parameter 'hardwareAddress' or 'clientIdentifier' missing. At least one of them must be specified.");
  602. _dnsWebService.DhcpServer.SaveScope(scopeName);
  603. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope's lease was reserved successfully: " + scopeName);
  604. }
  605. public void ConvertToDynamicLease(HttpContext context)
  606. {
  607. UserSession session = context.GetCurrentSession();
  608. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
  609. throw new DnsWebServiceException("Access was denied.");
  610. HttpRequest request = context.Request;
  611. string scopeName = request.GetQueryOrForm("name");
  612. Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
  613. if (scope == null)
  614. throw new DnsWebServiceException("DHCP scope does not exists: " + scopeName);
  615. string clientIdentifier = request.QueryOrForm("clientIdentifier");
  616. string hardwareAddress = request.QueryOrForm("hardwareAddress");
  617. if (!string.IsNullOrEmpty(clientIdentifier))
  618. scope.ConvertToDynamicLease(ClientIdentifierOption.Parse(clientIdentifier));
  619. else if (!string.IsNullOrEmpty(hardwareAddress))
  620. scope.ConvertToDynamicLease(hardwareAddress);
  621. else
  622. throw new DnsWebServiceException("Parameter 'hardwareAddress' or 'clientIdentifier' missing. At least one of them must be specified.");
  623. _dnsWebService.DhcpServer.SaveScope(scopeName);
  624. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope's lease was unreserved successfully: " + scopeName);
  625. }
  626. #endregion
  627. }
  628. }