forked from TechnitiumSoftware/DnsServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebServiceDhcpApi.cs
856 lines (633 loc) · 36.1 KB
/
WebServiceDhcpApi.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/*
Technitium DNS Server
Copyright (C) 2024 Shreyas Zare ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using DnsServerCore.Auth;
using DnsServerCore.Dhcp;
using DnsServerCore.Dhcp.Options;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using TechnitiumLibrary;
namespace DnsServerCore
{
class WebServiceDhcpApi
{
#region variables
static readonly char[] _commaSeparator = new char[] { ',' };
readonly DnsWebService _dnsWebService;
#endregion
#region constructor
public WebServiceDhcpApi(DnsWebService dnsWebService)
{
_dnsWebService = dnsWebService;
}
#endregion
#region public
public void ListDhcpLeases(HttpContext context)
{
UserSession session = context.GetCurrentSession();
if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.View))
throw new DnsWebServiceException("Access was denied.");
IReadOnlyDictionary<string, Scope> scopes = _dnsWebService.DhcpServer.Scopes;
//sort by name
List<Scope> sortedScopes = new List<Scope>(scopes.Count);
foreach (KeyValuePair<string, Scope> entry in scopes)
sortedScopes.Add(entry.Value);
sortedScopes.Sort();
Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
jsonWriter.WritePropertyName("leases");
jsonWriter.WriteStartArray();
foreach (Scope scope in sortedScopes)
{
IReadOnlyDictionary<ClientIdentifierOption, Lease> leases = scope.Leases;
//sort by address
List<Lease> sortedLeases = new List<Lease>(leases.Count);
foreach (KeyValuePair<ClientIdentifierOption, Lease> entry in leases)
sortedLeases.Add(entry.Value);
sortedLeases.Sort();
foreach (Lease lease in sortedLeases)
{
jsonWriter.WriteStartObject();
jsonWriter.WriteString("scope", scope.Name);
jsonWriter.WriteString("type", lease.Type.ToString());
jsonWriter.WriteString("hardwareAddress", BitConverter.ToString(lease.HardwareAddress));
jsonWriter.WriteString("clientIdentifier", lease.ClientIdentifier.ToString());
jsonWriter.WriteString("address", lease.Address.ToString());
jsonWriter.WriteString("hostName", lease.HostName);
jsonWriter.WriteString("leaseObtained", lease.LeaseObtained);
jsonWriter.WriteString("leaseExpires", lease.LeaseExpires);
jsonWriter.WriteEndObject();
}
}
jsonWriter.WriteEndArray();
}
public void ListDhcpScopes(HttpContext context)
{
UserSession session = context.GetCurrentSession();
if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.View))
throw new DnsWebServiceException("Access was denied.");
IReadOnlyDictionary<string, Scope> scopes = _dnsWebService.DhcpServer.Scopes;
//sort by name
List<Scope> sortedScopes = new List<Scope>(scopes.Count);
foreach (KeyValuePair<string, Scope> entry in scopes)
sortedScopes.Add(entry.Value);
sortedScopes.Sort();
Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
jsonWriter.WritePropertyName("scopes");
jsonWriter.WriteStartArray();
foreach (Scope scope in sortedScopes)
{
jsonWriter.WriteStartObject();
jsonWriter.WriteString("name", scope.Name);
jsonWriter.WriteBoolean("enabled", scope.Enabled);
jsonWriter.WriteString("startingAddress", scope.StartingAddress.ToString());
jsonWriter.WriteString("endingAddress", scope.EndingAddress.ToString());
jsonWriter.WriteString("subnetMask", scope.SubnetMask.ToString());
jsonWriter.WriteString("networkAddress", scope.NetworkAddress.ToString());
jsonWriter.WriteString("broadcastAddress", scope.BroadcastAddress.ToString());
if (scope.InterfaceAddress is not null)
jsonWriter.WriteString("interfaceAddress", scope.InterfaceAddress.ToString());
jsonWriter.WriteEndObject();
}
jsonWriter.WriteEndArray();
}
public void GetDhcpScope(HttpContext context)
{
UserSession session = context.GetCurrentSession();
if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.View))
throw new DnsWebServiceException("Access was denied.");
string scopeName = context.Request.GetQueryOrForm("name");
Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
if (scope is null)
throw new DnsWebServiceException("DHCP scope was not found: " + scopeName);
Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
jsonWriter.WriteString("name", scope.Name);
jsonWriter.WriteString("startingAddress", scope.StartingAddress.ToString());
jsonWriter.WriteString("endingAddress", scope.EndingAddress.ToString());
jsonWriter.WriteString("subnetMask", scope.SubnetMask.ToString());
jsonWriter.WriteNumber("leaseTimeDays", scope.LeaseTimeDays);
jsonWriter.WriteNumber("leaseTimeHours", scope.LeaseTimeHours);
jsonWriter.WriteNumber("leaseTimeMinutes", scope.LeaseTimeMinutes);
jsonWriter.WriteNumber("offerDelayTime", scope.OfferDelayTime);
jsonWriter.WriteBoolean("pingCheckEnabled", scope.PingCheckEnabled);
jsonWriter.WriteNumber("pingCheckTimeout", scope.PingCheckTimeout);
jsonWriter.WriteNumber("pingCheckRetries", scope.PingCheckRetries);
if (!string.IsNullOrEmpty(scope.DomainName))
jsonWriter.WriteString("domainName", scope.DomainName);
if (scope.DomainSearchList is not null)
{
jsonWriter.WritePropertyName("domainSearchList");
jsonWriter.WriteStartArray();
foreach (string domainSearchString in scope.DomainSearchList)
jsonWriter.WriteStringValue(domainSearchString);
jsonWriter.WriteEndArray();
}
jsonWriter.WriteBoolean("dnsUpdates", scope.DnsUpdates);
jsonWriter.WriteNumber("dnsTtl", scope.DnsTtl);
if (scope.ServerAddress is not null)
jsonWriter.WriteString("serverAddress", scope.ServerAddress.ToString());
if (scope.ServerHostName is not null)
jsonWriter.WriteString("serverHostName", scope.ServerHostName);
if (scope.BootFileName is not null)
jsonWriter.WriteString("bootFileName", scope.BootFileName);
if (scope.RouterAddress is not null)
jsonWriter.WriteString("routerAddress", scope.RouterAddress.ToString());
jsonWriter.WriteBoolean("useThisDnsServer", scope.UseThisDnsServer);
if (scope.DnsServers is not null)
{
jsonWriter.WritePropertyName("dnsServers");
jsonWriter.WriteStartArray();
foreach (IPAddress dnsServer in scope.DnsServers)
jsonWriter.WriteStringValue(dnsServer.ToString());
jsonWriter.WriteEndArray();
}
if (scope.WinsServers is not null)
{
jsonWriter.WritePropertyName("winsServers");
jsonWriter.WriteStartArray();
foreach (IPAddress winsServer in scope.WinsServers)
jsonWriter.WriteStringValue(winsServer.ToString());
jsonWriter.WriteEndArray();
}
if (scope.NtpServers is not null)
{
jsonWriter.WritePropertyName("ntpServers");
jsonWriter.WriteStartArray();
foreach (IPAddress ntpServer in scope.NtpServers)
jsonWriter.WriteStringValue(ntpServer.ToString());
jsonWriter.WriteEndArray();
}
if (scope.NtpServerDomainNames is not null)
{
jsonWriter.WritePropertyName("ntpServerDomainNames");
jsonWriter.WriteStartArray();
foreach (string ntpServerDomainName in scope.NtpServerDomainNames)
jsonWriter.WriteStringValue(ntpServerDomainName);
jsonWriter.WriteEndArray();
}
if (scope.StaticRoutes is not null)
{
jsonWriter.WritePropertyName("staticRoutes");
jsonWriter.WriteStartArray();
foreach (ClasslessStaticRouteOption.Route route in scope.StaticRoutes)
{
jsonWriter.WriteStartObject();
jsonWriter.WriteString("destination", route.Destination.ToString());
jsonWriter.WriteString("subnetMask", route.SubnetMask.ToString());
jsonWriter.WriteString("router", route.Router.ToString());
jsonWriter.WriteEndObject();
}
jsonWriter.WriteEndArray();
}
if (scope.VendorInfo is not null)
{
jsonWriter.WritePropertyName("vendorInfo");
jsonWriter.WriteStartArray();
foreach (KeyValuePair<string, VendorSpecificInformationOption> entry in scope.VendorInfo)
{
jsonWriter.WriteStartObject();
jsonWriter.WriteString("identifier", entry.Key);
jsonWriter.WriteString("information", BitConverter.ToString(entry.Value.Information).Replace('-', ':'));
jsonWriter.WriteEndObject();
}
jsonWriter.WriteEndArray();
}
if (scope.CAPWAPAcIpAddresses is not null)
{
jsonWriter.WritePropertyName("capwapAcIpAddresses");
jsonWriter.WriteStartArray();
foreach (IPAddress acIpAddress in scope.CAPWAPAcIpAddresses)
jsonWriter.WriteStringValue(acIpAddress.ToString());
jsonWriter.WriteEndArray();
}
if (scope.TftpServerAddresses is not null)
{
jsonWriter.WritePropertyName("tftpServerAddresses");
jsonWriter.WriteStartArray();
foreach (IPAddress address in scope.TftpServerAddresses)
jsonWriter.WriteStringValue(address.ToString());
jsonWriter.WriteEndArray();
}
if (scope.GenericOptions is not null)
{
jsonWriter.WritePropertyName("genericOptions");
jsonWriter.WriteStartArray();
foreach (DhcpOption genericOption in scope.GenericOptions)
{
jsonWriter.WriteStartObject();
jsonWriter.WriteNumber("code", (byte)genericOption.Code);
jsonWriter.WriteString("value", BitConverter.ToString(genericOption.RawValue).Replace('-', ':'));
jsonWriter.WriteEndObject();
}
jsonWriter.WriteEndArray();
}
if (scope.Exclusions is not null)
{
jsonWriter.WritePropertyName("exclusions");
jsonWriter.WriteStartArray();
foreach (Exclusion exclusion in scope.Exclusions)
{
jsonWriter.WriteStartObject();
jsonWriter.WriteString("startingAddress", exclusion.StartingAddress.ToString());
jsonWriter.WriteString("endingAddress", exclusion.EndingAddress.ToString());
jsonWriter.WriteEndObject();
}
jsonWriter.WriteEndArray();
}
jsonWriter.WritePropertyName("reservedLeases");
jsonWriter.WriteStartArray();
foreach (Lease reservedLease in scope.ReservedLeases)
{
jsonWriter.WriteStartObject();
jsonWriter.WriteString("hostName", reservedLease.HostName);
jsonWriter.WriteString("hardwareAddress", BitConverter.ToString(reservedLease.HardwareAddress));
jsonWriter.WriteString("address", reservedLease.Address.ToString());
jsonWriter.WriteString("comments", reservedLease.Comments);
jsonWriter.WriteEndObject();
}
jsonWriter.WriteEndArray();
jsonWriter.WriteBoolean("allowOnlyReservedLeases", scope.AllowOnlyReservedLeases);
jsonWriter.WriteBoolean("blockLocallyAdministeredMacAddresses", scope.BlockLocallyAdministeredMacAddresses);
jsonWriter.WriteBoolean("ignoreClientIdentifierOption", scope.IgnoreClientIdentifierOption);
}
public async Task SetDhcpScopeAsync(HttpContext context)
{
UserSession session = context.GetCurrentSession();
if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
throw new DnsWebServiceException("Access was denied.");
HttpRequest request = context.Request;
string scopeName = request.GetQueryOrForm("name");
string strStartingAddress = request.QueryOrForm("startingAddress");
string strEndingAddress = request.QueryOrForm("endingAddress");
string strSubnetMask = request.QueryOrForm("subnetMask");
bool scopeExists;
Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
if (scope is null)
{
//scope does not exists; create new scope
if (string.IsNullOrEmpty(strStartingAddress))
throw new DnsWebServiceException("Parameter 'startingAddress' missing.");
if (string.IsNullOrEmpty(strEndingAddress))
throw new DnsWebServiceException("Parameter 'endingAddress' missing.");
if (string.IsNullOrEmpty(strSubnetMask))
throw new DnsWebServiceException("Parameter 'subnetMask' missing.");
scopeExists = false;
scope = new Scope(scopeName, true, IPAddress.Parse(strStartingAddress), IPAddress.Parse(strEndingAddress), IPAddress.Parse(strSubnetMask), _dnsWebService._log, _dnsWebService.DhcpServer);
scope.IgnoreClientIdentifierOption = true;
}
else
{
scopeExists = true;
IPAddress startingAddress = string.IsNullOrEmpty(strStartingAddress) ? scope.StartingAddress : IPAddress.Parse(strStartingAddress);
IPAddress endingAddress = string.IsNullOrEmpty(strEndingAddress) ? scope.EndingAddress : IPAddress.Parse(strEndingAddress);
IPAddress subnetMask = string.IsNullOrEmpty(strSubnetMask) ? scope.SubnetMask : IPAddress.Parse(strSubnetMask);
//validate scope address
foreach (KeyValuePair<string, Scope> entry in _dnsWebService.DhcpServer.Scopes)
{
Scope existingScope = entry.Value;
if (existingScope.Equals(scope))
continue;
if (existingScope.IsAddressInRange(startingAddress) || existingScope.IsAddressInRange(endingAddress))
throw new DhcpServerException("Scope with overlapping range already exists: " + existingScope.StartingAddress.ToString() + "-" + existingScope.EndingAddress.ToString());
}
scope.ChangeNetwork(startingAddress, endingAddress, subnetMask);
}
if (request.TryGetQueryOrForm("leaseTimeDays", ushort.Parse, out ushort leaseTimeDays))
scope.LeaseTimeDays = leaseTimeDays;
if (request.TryGetQueryOrForm("leaseTimeHours", byte.Parse, out byte leaseTimeHours))
scope.LeaseTimeHours = leaseTimeHours;
if (request.TryGetQueryOrForm("leaseTimeMinutes", byte.Parse, out byte leaseTimeMinutes))
scope.LeaseTimeMinutes = leaseTimeMinutes;
if (request.TryGetQueryOrForm("offerDelayTime", ushort.Parse, out ushort offerDelayTime))
scope.OfferDelayTime = offerDelayTime;
if (request.TryGetQueryOrForm("pingCheckEnabled", bool.Parse, out bool pingCheckEnabled))
scope.PingCheckEnabled = pingCheckEnabled;
if (request.TryGetQueryOrForm("pingCheckTimeout", ushort.Parse, out ushort pingCheckTimeout))
scope.PingCheckTimeout = pingCheckTimeout;
if (request.TryGetQueryOrForm("pingCheckRetries", byte.Parse, out byte pingCheckRetries))
scope.PingCheckRetries = pingCheckRetries;
string domainName = request.QueryOrForm("domainName");
if (domainName is not null)
scope.DomainName = domainName.Length == 0 ? null : domainName;
string domainSearchList = request.QueryOrForm("domainSearchList");
if (domainSearchList is not null)
{
if (domainSearchList.Length == 0)
scope.DomainSearchList = null;
else
scope.DomainSearchList = domainSearchList.Split(_commaSeparator, StringSplitOptions.RemoveEmptyEntries);
}
if (request.TryGetQueryOrForm("dnsUpdates", bool.Parse, out bool dnsUpdates))
scope.DnsUpdates = dnsUpdates;
if (request.TryGetQueryOrForm("dnsTtl", uint.Parse, out uint dnsTtl))
scope.DnsTtl = dnsTtl;
string serverAddress = request.QueryOrForm("serverAddress");
if (serverAddress is not null)
scope.ServerAddress = serverAddress.Length == 0 ? null : IPAddress.Parse(serverAddress);
string serverHostName = request.QueryOrForm("serverHostName");
if (serverHostName is not null)
scope.ServerHostName = serverHostName.Length == 0 ? null : serverHostName;
string bootFileName = request.QueryOrForm("bootFileName");
if (bootFileName is not null)
scope.BootFileName = bootFileName.Length == 0 ? null : bootFileName;
string routerAddress = request.QueryOrForm("routerAddress");
if (routerAddress is not null)
scope.RouterAddress = routerAddress.Length == 0 ? null : IPAddress.Parse(routerAddress);
if (request.TryGetQueryOrForm("useThisDnsServer", bool.Parse, out bool useThisDnsServer))
scope.UseThisDnsServer = useThisDnsServer;
if (!scope.UseThisDnsServer)
{
string dnsServers = request.QueryOrForm("dnsServers");
if (dnsServers is not null)
{
if (dnsServers.Length == 0)
scope.DnsServers = null;
else
scope.DnsServers = dnsServers.Split(IPAddress.Parse, ',');
}
}
string winsServers = request.QueryOrForm("winsServers");
if (winsServers is not null)
{
if (winsServers.Length == 0)
scope.WinsServers = null;
else
scope.WinsServers = winsServers.Split(IPAddress.Parse, ',');
}
string ntpServers = request.QueryOrForm("ntpServers");
if (ntpServers is not null)
{
if (ntpServers.Length == 0)
scope.NtpServers = null;
else
scope.NtpServers = ntpServers.Split(IPAddress.Parse, ',');
}
string ntpServerDomainNames = request.QueryOrForm("ntpServerDomainNames");
if (ntpServerDomainNames is not null)
{
if (ntpServerDomainNames.Length == 0)
scope.NtpServerDomainNames = null;
else
scope.NtpServerDomainNames = ntpServerDomainNames.Split(_commaSeparator, StringSplitOptions.RemoveEmptyEntries);
}
string strStaticRoutes = request.QueryOrForm("staticRoutes");
if (strStaticRoutes is not null)
{
if (strStaticRoutes.Length == 0)
{
scope.StaticRoutes = null;
}
else
{
string[] strStaticRoutesParts = strStaticRoutes.Split('|');
List<ClasslessStaticRouteOption.Route> staticRoutes = new List<ClasslessStaticRouteOption.Route>();
for (int i = 0; i < strStaticRoutesParts.Length; i += 3)
staticRoutes.Add(new ClasslessStaticRouteOption.Route(IPAddress.Parse(strStaticRoutesParts[i + 0]), IPAddress.Parse(strStaticRoutesParts[i + 1]), IPAddress.Parse(strStaticRoutesParts[i + 2])));
scope.StaticRoutes = staticRoutes;
}
}
string strVendorInfo = request.QueryOrForm("vendorInfo");
if (strVendorInfo is not null)
{
if (strVendorInfo.Length == 0)
{
scope.VendorInfo = null;
}
else
{
string[] strVendorInfoParts = strVendorInfo.Split('|');
Dictionary<string, VendorSpecificInformationOption> vendorInfo = new Dictionary<string, VendorSpecificInformationOption>();
for (int i = 0; i < strVendorInfoParts.Length; i += 2)
vendorInfo.Add(strVendorInfoParts[i + 0], new VendorSpecificInformationOption(strVendorInfoParts[i + 1]));
scope.VendorInfo = vendorInfo;
}
}
string capwapAcIpAddresses = request.QueryOrForm("capwapAcIpAddresses");
if (capwapAcIpAddresses is not null)
{
if (capwapAcIpAddresses.Length == 0)
scope.CAPWAPAcIpAddresses = null;
else
scope.CAPWAPAcIpAddresses = capwapAcIpAddresses.Split(IPAddress.Parse, ',');
}
string tftpServerAddresses = request.QueryOrForm("tftpServerAddresses");
if (tftpServerAddresses is not null)
{
if (tftpServerAddresses.Length == 0)
scope.TftpServerAddresses = null;
else
scope.TftpServerAddresses = tftpServerAddresses.Split(IPAddress.Parse, ',');
}
string strGenericOptions = request.QueryOrForm("genericOptions");
if (strGenericOptions is not null)
{
if (strGenericOptions.Length == 0)
{
scope.GenericOptions = null;
}
else
{
string[] strGenericOptionsParts = strGenericOptions.Split('|');
List<DhcpOption> genericOptions = new List<DhcpOption>();
for (int i = 0; i < strGenericOptionsParts.Length; i += 2)
genericOptions.Add(new DhcpOption((DhcpOptionCode)byte.Parse(strGenericOptionsParts[i + 0]), strGenericOptionsParts[i + 1]));
scope.GenericOptions = genericOptions;
}
}
string strExclusions = request.QueryOrForm("exclusions");
if (strExclusions is not null)
{
if (strExclusions.Length == 0)
{
scope.Exclusions = null;
}
else
{
string[] strExclusionsParts = strExclusions.Split('|');
List<Exclusion> exclusions = new List<Exclusion>();
for (int i = 0; i < strExclusionsParts.Length; i += 2)
exclusions.Add(new Exclusion(IPAddress.Parse(strExclusionsParts[i + 0]), IPAddress.Parse(strExclusionsParts[i + 1])));
scope.Exclusions = exclusions;
}
}
string strReservedLeases = request.QueryOrForm("reservedLeases");
if (strReservedLeases is not null)
{
if (strReservedLeases.Length == 0)
{
scope.ReservedLeases = null;
}
else
{
string[] strReservedLeaseParts = strReservedLeases.Split('|');
List<Lease> reservedLeases = new List<Lease>();
for (int i = 0; i < strReservedLeaseParts.Length; i += 4)
reservedLeases.Add(new Lease(LeaseType.Reserved, strReservedLeaseParts[i + 0], DhcpMessageHardwareAddressType.Ethernet, strReservedLeaseParts[i + 1], IPAddress.Parse(strReservedLeaseParts[i + 2]), strReservedLeaseParts[i + 3]));
scope.ReservedLeases = reservedLeases;
}
}
if (request.TryGetQueryOrForm("allowOnlyReservedLeases", bool.Parse, out bool allowOnlyReservedLeases))
scope.AllowOnlyReservedLeases = allowOnlyReservedLeases;
if (request.TryGetQueryOrForm("blockLocallyAdministeredMacAddresses", bool.Parse, out bool blockLocallyAdministeredMacAddresses))
scope.BlockLocallyAdministeredMacAddresses = blockLocallyAdministeredMacAddresses;
if (request.TryGetQueryOrForm("ignoreClientIdentifierOption", bool.Parse, out bool ignoreClientIdentifierOption))
scope.IgnoreClientIdentifierOption = ignoreClientIdentifierOption;
if (scopeExists)
{
_dnsWebService.DhcpServer.SaveScope(scopeName);
_dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope was updated successfully: " + scopeName);
}
else
{
await _dnsWebService.DhcpServer.AddScopeAsync(scope);
_dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope was added successfully: " + scopeName);
}
if (request.TryGetQueryOrForm("newName", out string newName) && !newName.Equals(scopeName))
{
_dnsWebService.DhcpServer.RenameScope(scopeName, newName);
_dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope was renamed successfully: '" + scopeName + "' to '" + newName + "'");
}
}
public void AddReservedLease(HttpContext context)
{
UserSession session = context.GetCurrentSession();
if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
throw new DnsWebServiceException("Access was denied.");
HttpRequest request = context.Request;
string scopeName = request.GetQueryOrForm("name");
Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
if (scope is null)
throw new DnsWebServiceException("No such scope exists: " + scopeName);
string hostName = request.QueryOrForm("hostName");
string hardwareAddress = request.GetQueryOrForm("hardwareAddress");
string strIpAddress = request.GetQueryOrForm("ipAddress");
string comments = request.QueryOrForm("comments");
Lease reservedLease = new Lease(LeaseType.Reserved, hostName, DhcpMessageHardwareAddressType.Ethernet, hardwareAddress, IPAddress.Parse(strIpAddress), comments);
if (!scope.AddReservedLease(reservedLease))
throw new DnsWebServiceException("Failed to add reserved lease for scope: " + scopeName);
_dnsWebService.DhcpServer.SaveScope(scopeName);
_dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope reserved lease was added successfully: " + scopeName);
}
public void RemoveReservedLease(HttpContext context)
{
UserSession session = context.GetCurrentSession();
if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
throw new DnsWebServiceException("Access was denied.");
HttpRequest request = context.Request;
string scopeName = request.GetQueryOrForm("name");
Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
if (scope is null)
throw new DnsWebServiceException("No such scope exists: " + scopeName);
string hardwareAddress = request.GetQueryOrForm("hardwareAddress");
if (!scope.RemoveReservedLease(hardwareAddress))
throw new DnsWebServiceException("Failed to remove reserved lease for scope: " + scopeName);
_dnsWebService.DhcpServer.SaveScope(scopeName);
_dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope reserved lease was removed successfully: " + scopeName);
}
public async Task EnableDhcpScopeAsync(HttpContext context)
{
UserSession session = context.GetCurrentSession();
if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
throw new DnsWebServiceException("Access was denied.");
string scopeName = context.Request.GetQueryOrForm("name");
await _dnsWebService.DhcpServer.EnableScopeAsync(scopeName, true);
_dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope was enabled successfully: " + scopeName);
}
public void DisableDhcpScope(HttpContext context)
{
UserSession session = context.GetCurrentSession();
if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
throw new DnsWebServiceException("Access was denied.");
string scopeName = context.Request.GetQueryOrForm("name");
_dnsWebService.DhcpServer.DisableScope(scopeName, true);
_dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope was disabled successfully: " + scopeName);
}
public void DeleteDhcpScope(HttpContext context)
{
UserSession session = context.GetCurrentSession();
if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Delete))
throw new DnsWebServiceException("Access was denied.");
string scopeName = context.Request.GetQueryOrForm("name");
_dnsWebService.DhcpServer.DeleteScope(scopeName);
_dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope was deleted successfully: " + scopeName);
}
public void RemoveDhcpLease(HttpContext context)
{
UserSession session = context.GetCurrentSession();
if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Delete))
throw new DnsWebServiceException("Access was denied.");
HttpRequest request = context.Request;
string scopeName = request.GetQueryOrForm("name");
Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
if (scope is null)
throw new DnsWebServiceException("DHCP scope does not exists: " + scopeName);
string clientIdentifier = request.QueryOrForm("clientIdentifier");
string hardwareAddress = request.QueryOrForm("hardwareAddress");
if (!string.IsNullOrEmpty(clientIdentifier))
scope.RemoveLease(ClientIdentifierOption.Parse(clientIdentifier));
else if (!string.IsNullOrEmpty(hardwareAddress))
scope.RemoveLease(hardwareAddress);
else
throw new DnsWebServiceException("Parameter 'hardwareAddress' or 'clientIdentifier' missing. At least one of them must be specified.");
_dnsWebService.DhcpServer.SaveScope(scopeName);
_dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope's lease was removed successfully: " + scopeName);
}
public void ConvertToReservedLease(HttpContext context)
{
UserSession session = context.GetCurrentSession();
if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
throw new DnsWebServiceException("Access was denied.");
HttpRequest request = context.Request;
string scopeName = request.GetQueryOrForm("name");
Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
if (scope == null)
throw new DnsWebServiceException("DHCP scope does not exists: " + scopeName);
string clientIdentifier = request.QueryOrForm("clientIdentifier");
string hardwareAddress = request.QueryOrForm("hardwareAddress");
if (!string.IsNullOrEmpty(clientIdentifier))
scope.ConvertToReservedLease(ClientIdentifierOption.Parse(clientIdentifier));
else if (!string.IsNullOrEmpty(hardwareAddress))
scope.ConvertToReservedLease(hardwareAddress);
else
throw new DnsWebServiceException("Parameter 'hardwareAddress' or 'clientIdentifier' missing. At least one of them must be specified.");
_dnsWebService.DhcpServer.SaveScope(scopeName);
_dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope's lease was reserved successfully: " + scopeName);
}
public void ConvertToDynamicLease(HttpContext context)
{
UserSession session = context.GetCurrentSession();
if (!_dnsWebService._authManager.IsPermitted(PermissionSection.DhcpServer, session.User, PermissionFlag.Modify))
throw new DnsWebServiceException("Access was denied.");
HttpRequest request = context.Request;
string scopeName = request.GetQueryOrForm("name");
Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
if (scope == null)
throw new DnsWebServiceException("DHCP scope does not exists: " + scopeName);
string clientIdentifier = request.QueryOrForm("clientIdentifier");
string hardwareAddress = request.QueryOrForm("hardwareAddress");
if (!string.IsNullOrEmpty(clientIdentifier))
scope.ConvertToDynamicLease(ClientIdentifierOption.Parse(clientIdentifier));
else if (!string.IsNullOrEmpty(hardwareAddress))
scope.ConvertToDynamicLease(hardwareAddress);
else
throw new DnsWebServiceException("Parameter 'hardwareAddress' or 'clientIdentifier' missing. At least one of them must be specified.");
_dnsWebService.DhcpServer.SaveScope(scopeName);
_dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DHCP scope's lease was unreserved successfully: " + scopeName);
}
#endregion
}
}