-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
DirectorySearcher.cs
1014 lines (906 loc) · 42.1 KB
/
DirectorySearcher.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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Specialized;
using System.DirectoryServices.Interop;
using System.ComponentModel;
using INTPTR_INTPTRCAST = System.IntPtr;
namespace System.DirectoryServices
{
/// <devdoc>
/// Performs queries against the Active Directory hierarchy.
/// </devdoc>
public class DirectorySearcher : Component
{
private DirectoryEntry _searchRoot;
private string _filter = defaultFilter;
private StringCollection _propertiesToLoad;
private bool _disposed = false;
private static readonly TimeSpan s_minusOneSecond = new TimeSpan(0, 0, -1);
// search preference variables
private SearchScope _scope = System.DirectoryServices.SearchScope.Subtree;
private bool _scopeSpecified = false;
private int _sizeLimit = 0;
private TimeSpan _serverTimeLimit = s_minusOneSecond;
private TimeSpan _clientTimeout = s_minusOneSecond;
private int _pageSize = 0;
private TimeSpan _serverPageTimeLimit = s_minusOneSecond;
private ReferralChasingOption _referralChasing = ReferralChasingOption.External;
private SortOption _sort = new SortOption();
private bool _cacheResults = true;
private bool _cacheResultsSpecified = false;
private bool _rootEntryAllocated = false; // true: if a temporary entry inside Searcher has been created
private string _assertDefaultNamingContext = null;
private string _attributeScopeQuery = "";
private bool _attributeScopeQuerySpecified = false;
private DereferenceAlias _derefAlias = DereferenceAlias.Never;
private SecurityMasks _securityMask = SecurityMasks.None;
private ExtendedDN _extendedDN = ExtendedDN.None;
private DirectorySynchronization _sync = null;
internal bool directorySynchronizationSpecified = false;
private DirectoryVirtualListView _vlv = null;
internal bool directoryVirtualListViewSpecified = false;
internal SearchResultCollection searchResult = null;
private const string defaultFilter = "(objectClass=*)";
/// <devdoc>
/// Initializes a new instance of the <see cref='System.DirectoryServices.DirectorySearcher'/> class with <see cref='System.DirectoryServices.DirectorySearcher.SearchRoot'/>,
/// <see cref='System.DirectoryServices.DirectorySearcher.Filter'/>, <see cref='System.DirectoryServices.DirectorySearcher.PropertiesToLoad'/>, and <see cref='System.DirectoryServices.DirectorySearcher.SearchScope'/> set to their default values.
/// </devdoc>
public DirectorySearcher() : this(null, defaultFilter, null, System.DirectoryServices.SearchScope.Subtree)
{
_scopeSpecified = false;
}
/// <devdoc>
/// Initializes a new instance of the <see cref='System.DirectoryServices.DirectorySearcher'/> class with
/// <see cref='System.DirectoryServices.DirectorySearcher.Filter'/>, <see cref='System.DirectoryServices.DirectorySearcher.PropertiesToLoad'/>, and <see cref='System.DirectoryServices.DirectorySearcher.SearchScope'/> set to their default
/// values, and <see cref='System.DirectoryServices.DirectorySearcher.SearchRoot'/> set to the given value.
/// </devdoc>
public DirectorySearcher(DirectoryEntry searchRoot) : this(searchRoot, defaultFilter, null, System.DirectoryServices.SearchScope.Subtree)
{
_scopeSpecified = false;
}
/// <devdoc>
/// Initializes a new instance of the <see cref='System.DirectoryServices.DirectorySearcher'/> class with
/// <see cref='System.DirectoryServices.DirectorySearcher.PropertiesToLoad'/> and <see cref='System.DirectoryServices.DirectorySearcher.SearchScope'/> set to their default
/// values, and <see cref='System.DirectoryServices.DirectorySearcher.SearchRoot'/> and <see cref='System.DirectoryServices.DirectorySearcher.Filter'/> set to the respective given values.
/// </devdoc>
public DirectorySearcher(DirectoryEntry searchRoot, string filter) : this(searchRoot, filter, null, System.DirectoryServices.SearchScope.Subtree)
{
_scopeSpecified = false;
}
/// <devdoc>
/// Initializes a new instance of the <see cref='System.DirectoryServices.DirectorySearcher'/> class with
/// <see cref='System.DirectoryServices.DirectorySearcher.SearchScope'/> set to its default
/// value, and <see cref='System.DirectoryServices.DirectorySearcher.SearchRoot'/>, <see cref='System.DirectoryServices.DirectorySearcher.Filter'/>, and <see cref='System.DirectoryServices.DirectorySearcher.PropertiesToLoad'/> set to the respective given values.
/// </devdoc>
public DirectorySearcher(DirectoryEntry searchRoot, string filter, string[] propertiesToLoad) : this(searchRoot, filter, propertiesToLoad, System.DirectoryServices.SearchScope.Subtree)
{
_scopeSpecified = false;
}
/// <devdoc>
/// Initializes a new instance of the <see cref='System.DirectoryServices.DirectorySearcher'/> class with <see cref='System.DirectoryServices.DirectorySearcher.SearchRoot'/>,
/// <see cref='System.DirectoryServices.DirectorySearcher.PropertiesToLoad'/>, and <see cref='System.DirectoryServices.DirectorySearcher.SearchScope'/> set to their default
/// values, and <see cref='System.DirectoryServices.DirectorySearcher.Filter'/> set to the given value.
/// </devdoc>
public DirectorySearcher(string filter) : this(null, filter, null, System.DirectoryServices.SearchScope.Subtree)
{
_scopeSpecified = false;
}
/// <devdoc>
/// Initializes a new instance of the <see cref='System.DirectoryServices.DirectorySearcher'/> class with <see cref='System.DirectoryServices.DirectorySearcher.SearchRoot'/>
/// and <see cref='System.DirectoryServices.DirectorySearcher.SearchScope'/> set to their default
/// values, and <see cref='System.DirectoryServices.DirectorySearcher.Filter'/> and <see cref='System.DirectoryServices.DirectorySearcher.PropertiesToLoad'/> set to the respective given values.
/// </devdoc>
public DirectorySearcher(string filter, string[] propertiesToLoad) : this(null, filter, propertiesToLoad, System.DirectoryServices.SearchScope.Subtree)
{
_scopeSpecified = false;
}
/// <devdoc>
/// Initializes a new instance of the <see cref='System.DirectoryServices.DirectorySearcher'/> class with <see cref='System.DirectoryServices.DirectorySearcher.SearchRoot'/> set to its default
/// value, and <see cref='System.DirectoryServices.DirectorySearcher.Filter'/>, <see cref='System.DirectoryServices.DirectorySearcher.PropertiesToLoad'/>, and <see cref='System.DirectoryServices.DirectorySearcher.SearchScope'/> set to the respective given values.
/// </devdoc>
public DirectorySearcher(string filter, string[] propertiesToLoad, SearchScope scope) : this(null, filter, propertiesToLoad, scope)
{
}
/// <devdoc>
/// Initializes a new instance of the <see cref='System.DirectoryServices.DirectorySearcher'/> class with the <see cref='System.DirectoryServices.DirectorySearcher.SearchRoot'/>, <see cref='System.DirectoryServices.DirectorySearcher.Filter'/>, <see cref='System.DirectoryServices.DirectorySearcher.PropertiesToLoad'/>, and <see cref='System.DirectoryServices.DirectorySearcher.SearchScope'/> properties set to the given
/// values.
/// </devdoc>
public DirectorySearcher(DirectoryEntry searchRoot, string filter, string[] propertiesToLoad, SearchScope scope)
{
_searchRoot = searchRoot;
_filter = filter;
if (propertiesToLoad != null)
PropertiesToLoad.AddRange(propertiesToLoad);
this.SearchScope = scope;
}
protected override void Dispose(bool disposing)
{
// safe to call while finalizing or disposing
//
if (!_disposed && disposing)
{
if (_rootEntryAllocated)
_searchRoot.Dispose();
_rootEntryAllocated = false;
_disposed = true;
}
base.Dispose(disposing);
}
/// <devdoc>
/// Gets or sets a value indicating whether the result should be cached on the
/// client machine.
/// </devdoc>
[DefaultValue(true)]
public bool CacheResults
{
get => _cacheResults;
set
{
// user explicitly set CacheResults to true and also want VLV
if (directoryVirtualListViewSpecified == true && value == true)
throw new ArgumentException(SR.DSBadCacheResultsVLV);
_cacheResults = value;
_cacheResultsSpecified = true;
}
}
/// <devdoc>
/// Gets or sets the maximum amount of time that the client waits for
/// the server to return results. If the server does not respond within this time,
/// the search is aborted, and no results are returned.
/// </devdoc>
public TimeSpan ClientTimeout
{
get => _clientTimeout;
set
{
// prevent integer overflow
if (value.TotalSeconds > int.MaxValue)
{
throw new ArgumentException(SR.TimespanExceedMax, nameof(value));
}
_clientTimeout = value;
}
}
/// <devdoc>
/// Gets or sets a value indicating whether the search should retrieve only the names of requested
/// properties or the names and values of requested properties.
/// </devdoc>
[DefaultValue(false)]
public bool PropertyNamesOnly { get; set; }
/// <devdoc>
/// Gets or sets the Lightweight Directory Access Protocol (LDAP) filter string format.
/// </devdoc>
[
DefaultValue(defaultFilter),
// CoreFXPort - Remove design support
// TypeConverter("System.Diagnostics.Design.StringValueConverter, " + AssemblyRef.SystemDesign)
]
public string Filter
{
get => _filter;
set
{
if (value == null || value.Length == 0)
value = defaultFilter;
_filter = value;
}
}
/// <devdoc>
/// Gets or sets the page size in a paged search.
/// </devdoc>
[DefaultValue(0)]
public int PageSize
{
get => _pageSize;
set
{
if (value < 0)
throw new ArgumentException(SR.DSBadPageSize);
// specify non-zero pagesize explicitly and also want dirsync
if (directorySynchronizationSpecified == true && value != 0)
throw new ArgumentException(SR.DSBadPageSizeDirsync);
_pageSize = value;
}
}
/// <devdoc>
/// Gets the set of properties retrieved during the search. By default, the <see cref='System.DirectoryServices.DirectoryEntry.Path'/>
/// and <see cref='System.DirectoryServices.DirectoryEntry.Name'/> properties are retrieved.
/// </devdoc>
public StringCollection PropertiesToLoad
{
get
{
if (_propertiesToLoad == null)
{
_propertiesToLoad = new StringCollection();
}
return _propertiesToLoad;
}
}
/// <devdoc>
/// Gets or sets how referrals are chased.
/// </devdoc>
[DefaultValue(ReferralChasingOption.External)]
public ReferralChasingOption ReferralChasing
{
get => _referralChasing;
set
{
if (value != ReferralChasingOption.None &&
value != ReferralChasingOption.Subordinate &&
value != ReferralChasingOption.External &&
value != ReferralChasingOption.All)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ReferralChasingOption));
_referralChasing = value;
}
}
/// <devdoc>
/// Gets or sets the scope of the search that should be observed by the server.
/// </devdoc>
[DefaultValue(SearchScope.Subtree)]
public SearchScope SearchScope
{
get => _scope;
set
{
if (value < SearchScope.Base || value > SearchScope.Subtree)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(SearchScope));
// user explicitly set SearchScope to something other than Base and also want to do ASQ, it is not supported
if (_attributeScopeQuerySpecified == true && value != SearchScope.Base)
{
throw new ArgumentException(SR.DSBadASQSearchScope);
}
_scope = value;
_scopeSpecified = true;
}
}
/// <devdoc>
/// Gets or sets the time limit that the server should observe to search a page of results (as
/// opposed to the time limit for the entire search).
/// </devdoc>
public TimeSpan ServerPageTimeLimit
{
get => _serverPageTimeLimit;
set
{
// prevent integer overflow
if (value.TotalSeconds > int.MaxValue)
{
throw new ArgumentException(SR.TimespanExceedMax, nameof(value));
}
_serverPageTimeLimit = value;
}
}
/// <devdoc>
/// Gets or sets the maximum amount of time the server spends searching. If the
/// time limit is reached, only entries found up to that point will be returned.
/// </devdoc>
public TimeSpan ServerTimeLimit
{
get => _serverTimeLimit;
set
{
// prevent integer overflow
if (value.TotalSeconds > int.MaxValue)
{
throw new ArgumentException(SR.TimespanExceedMax, nameof(value));
}
_serverTimeLimit = value;
}
}
/// <devdoc>
/// Gets or sets the maximum number of objects that the
/// server should return in a search.
/// </devdoc>
[DefaultValue(0)]
public int SizeLimit
{
get => _sizeLimit;
set
{
if (value < 0)
throw new ArgumentException(SR.DSBadSizeLimit);
_sizeLimit = value;
}
}
/// <devdoc>
/// Gets or sets the node in the Active Directory hierarchy
/// at which the search will start.
/// </devdoc>
[DefaultValue(null)]
public DirectoryEntry SearchRoot
{
get
{
if (_searchRoot == null && !DesignMode)
{
// get the default naming context. This should be the default root for the search.
DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE", true, null, null, AuthenticationTypes.Secure);
//SECREVIEW: Searching the root of the DS will demand browse permissions
// on "*" or "LDAP://RootDSE".
string defaultNamingContext = (string)rootDSE.Properties["defaultNamingContext"][0];
rootDSE.Dispose();
_searchRoot = new DirectoryEntry("LDAP://" + defaultNamingContext, true, null, null, AuthenticationTypes.Secure);
_rootEntryAllocated = true;
_assertDefaultNamingContext = "LDAP://" + defaultNamingContext;
}
return _searchRoot;
}
set
{
if (_rootEntryAllocated)
_searchRoot.Dispose();
_rootEntryAllocated = false;
_assertDefaultNamingContext = null;
_searchRoot = value;
}
}
/// <devdoc>
/// Gets the property on which the results should be sorted.
/// </devdoc>
[TypeConverter(typeof(ExpandableObjectConverter))]
public SortOption Sort
{
get => _sort;
set => _sort = value ?? throw new ArgumentNullException(nameof(value));
}
/// <devdoc>
/// Gets or sets a value indicating whether searches should be carried out in an asynchronous
/// way.
/// </devdoc>
[DefaultValue(false)]
public bool Asynchronous { get; set; }
/// <devdoc>
/// Gets or sets a value indicating whether the search should also return deleted objects that match the search
/// filter.
/// </devdoc>
[DefaultValue(false)]
public bool Tombstone { get; set; }
/// <devdoc>
/// Gets or sets an attribute name to indicate that an attribute-scoped query search should be
/// performed.
/// </devdoc>
[
DefaultValue(""),
// CoreFXPort - Remove design support
// TypeConverter("System.Diagnostics.Design.StringValueConverter, " + AssemblyRef.SystemDesign)
]
public string AttributeScopeQuery
{
get => _attributeScopeQuery;
set
{
if (value == null)
value = "";
// user explicitly set AttributeScopeQuery and value is not null or empty string
if (value.Length != 0)
{
if (_scopeSpecified == true && SearchScope != SearchScope.Base)
{
throw new ArgumentException(SR.DSBadASQSearchScope);
}
// if user did not explicitly set search scope
_scope = SearchScope.Base;
_attributeScopeQuerySpecified = true;
}
else
// user explicitly sets the value to default one and doesn't want to do asq
{
_attributeScopeQuerySpecified = false;
}
_attributeScopeQuery = value;
}
}
/// <devdoc>
/// Gets or sets a value to indicate how the aliases of found objects are to be
/// resolved.
/// </devdoc>
[DefaultValue(DereferenceAlias.Never)]
public DereferenceAlias DerefAlias
{
get => _derefAlias;
set
{
if (value < DereferenceAlias.Never || value > DereferenceAlias.Always)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(DereferenceAlias));
_derefAlias = value;
}
}
/// <devdoc>
/// Gets or sets a value to indicate the search should return security access information for the specified
/// attributes.
/// </devdoc>
[DefaultValue(SecurityMasks.None)]
public SecurityMasks SecurityMasks
{
get => _securityMask;
set
{
// make sure the behavior is consistent with native ADSI
if (value > (SecurityMasks.None | SecurityMasks.Owner | SecurityMasks.Group | SecurityMasks.Dacl | SecurityMasks.Sacl))
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(SecurityMasks));
_securityMask = value;
}
}
/// <devdoc>
/// Gets or sets a value to return extended DNs according to the requested
/// format.
/// </devdoc>
[DefaultValue(ExtendedDN.None)]
public ExtendedDN ExtendedDN
{
get => _extendedDN;
set
{
if (value < ExtendedDN.None || value > ExtendedDN.Standard)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ExtendedDN));
_extendedDN = value;
}
}
/// <devdoc>
/// Gets or sets a value to indicate a directory synchronization search, which returns all changes since a specified
/// state.
/// </devdoc>
[DefaultValue(null)]
public DirectorySynchronization DirectorySynchronization
{
get
{
// if user specifies dirsync search preference and search is executed
if (directorySynchronizationSpecified && searchResult != null)
{
_sync.ResetDirectorySynchronizationCookie(searchResult.DirsyncCookie);
}
return _sync;
}
set
{
// specify non-zero pagesize explicitly and also want dirsync
if (value != null)
{
if (PageSize != 0)
throw new ArgumentException(SR.DSBadPageSizeDirsync);
directorySynchronizationSpecified = true;
}
else
// user explicitly sets the value to default one and doesn't want to do dirsync
{
directorySynchronizationSpecified = false;
}
_sync = value;
}
}
/// <devdoc>
/// Gets or sets a value to indicate the search should use the LDAP virtual list view (VLV)
/// control.
/// </devdoc>
[DefaultValue(null)]
public DirectoryVirtualListView VirtualListView
{
get
{
// if user specifies dirsync search preference and search is executed
if (directoryVirtualListViewSpecified && searchResult != null)
{
DirectoryVirtualListView tempval = searchResult.VLVResponse;
_vlv.Offset = tempval.Offset;
_vlv.ApproximateTotal = tempval.ApproximateTotal;
_vlv.DirectoryVirtualListViewContext = tempval.DirectoryVirtualListViewContext;
if (_vlv.ApproximateTotal != 0)
_vlv.TargetPercentage = (int)((double)_vlv.Offset / _vlv.ApproximateTotal * 100);
else
_vlv.TargetPercentage = 0;
}
return _vlv;
}
set
{
// if user explicitly set CacheResults to true and also want to set VLV
if (value != null)
{
if (_cacheResultsSpecified == true && CacheResults == true)
throw new ArgumentException(SR.DSBadCacheResultsVLV);
directoryVirtualListViewSpecified = true;
// if user does not explicit specify cache results to true and also do vlv, then cache results is default to false
_cacheResults = false;
}
else
// user explicitly sets the value to default one and doesn't want to do vlv
{
directoryVirtualListViewSpecified = false;
}
_vlv = value;
}
}
/// <devdoc>
/// Executes the search and returns only the first entry that is found.
/// </devdoc>
public SearchResult FindOne()
{
DirectorySynchronization tempsync = null;
DirectoryVirtualListView tempvlv = null;
SearchResult resultEntry = null;
SearchResultCollection results = FindAll(false);
try
{
foreach (SearchResult entry in results)
{
// need to get the dirsync cookie
if (directorySynchronizationSpecified)
tempsync = DirectorySynchronization;
// need to get the vlv response
if (directoryVirtualListViewSpecified)
tempvlv = VirtualListView;
resultEntry = entry;
break;
}
}
finally
{
searchResult = null;
// still need to properly release the resource
results.Dispose();
}
return resultEntry;
}
/// <devdoc>
/// Executes the search and returns a collection of the entries that are found.
/// </devdoc>
public SearchResultCollection FindAll() => FindAll(true);
private SearchResultCollection FindAll(bool findMoreThanOne)
{
searchResult = null;
DirectoryEntry clonedRoot = null;
if (_assertDefaultNamingContext == null)
{
clonedRoot = SearchRoot.CloneBrowsable();
}
else
{
clonedRoot = SearchRoot.CloneBrowsable();
}
UnsafeNativeMethods.IAds adsObject = clonedRoot.AdsObject;
if (!(adsObject is UnsafeNativeMethods.IDirectorySearch))
throw new NotSupportedException(SR.Format(SR.DSSearchUnsupported , SearchRoot.Path));
// this is a little bit hacky, but we need to perform a bind here, so we make sure the LDAP connection that we hold has more than
// one reference count, one by SearchResultCollection object, one by DirectorySearcher object. In this way, when user calls
// Dispose on SearchResultCollection, the connection is still there instead of reference count dropping to zero and being closed.
// It is especially important for virtuallistview case, in order to reuse the vlv response, the search must be performed on the same ldap connection
// only do it when vlv is used
if (directoryVirtualListViewSpecified)
{
SearchRoot.Bind(true);
}
UnsafeNativeMethods.IDirectorySearch adsSearch = (UnsafeNativeMethods.IDirectorySearch)adsObject;
SetSearchPreferences(adsSearch, findMoreThanOne);
string[] properties = null;
if (PropertiesToLoad.Count > 0)
{
if (!PropertiesToLoad.Contains("ADsPath"))
{
// if we don't get this property, we won't be able to return a list of DirectoryEntry objects!
PropertiesToLoad.Add("ADsPath");
}
properties = new string[PropertiesToLoad.Count];
PropertiesToLoad.CopyTo(properties, 0);
}
IntPtr resultsHandle;
if (properties != null)
adsSearch.ExecuteSearch(Filter, properties, properties.Length, out resultsHandle);
else
{
adsSearch.ExecuteSearch(Filter, null, -1, out resultsHandle);
properties = Array.Empty<string>();
}
SearchResultCollection result = new SearchResultCollection(clonedRoot, resultsHandle, properties, this);
searchResult = result;
return result;
}
private unsafe void SetSearchPreferences(UnsafeNativeMethods.IDirectorySearch adsSearch, bool findMoreThanOne)
{
ArrayList prefList = new ArrayList();
AdsSearchPreferenceInfo info;
// search scope
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.SEARCH_SCOPE;
info.vValue = new AdsValueHelper((int)SearchScope).GetStruct();
prefList.Add(info);
// size limit
if (_sizeLimit != 0 || !findMoreThanOne)
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.SIZE_LIMIT;
info.vValue = new AdsValueHelper(findMoreThanOne ? SizeLimit : 1).GetStruct();
prefList.Add(info);
}
// time limit
if (ServerTimeLimit >= new TimeSpan(0))
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.TIME_LIMIT;
info.vValue = new AdsValueHelper((int)ServerTimeLimit.TotalSeconds).GetStruct();
prefList.Add(info);
}
// propertyNamesOnly
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.ATTRIBTYPES_ONLY;
info.vValue = new AdsValueHelper(PropertyNamesOnly).GetStruct();
prefList.Add(info);
// Timeout
if (ClientTimeout >= new TimeSpan(0))
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.TIMEOUT;
info.vValue = new AdsValueHelper((int)ClientTimeout.TotalSeconds).GetStruct();
prefList.Add(info);
}
// page size
if (PageSize != 0)
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.PAGESIZE;
info.vValue = new AdsValueHelper(PageSize).GetStruct();
prefList.Add(info);
}
// page time limit
if (ServerPageTimeLimit >= new TimeSpan(0))
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.PAGED_TIME_LIMIT;
info.vValue = new AdsValueHelper((int)ServerPageTimeLimit.TotalSeconds).GetStruct();
prefList.Add(info);
}
// chase referrals
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.CHASE_REFERRALS;
info.vValue = new AdsValueHelper((int)ReferralChasing).GetStruct();
prefList.Add(info);
// asynchronous
if (Asynchronous == true)
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.ASYNCHRONOUS;
info.vValue = new AdsValueHelper(Asynchronous).GetStruct();
prefList.Add(info);
}
// tombstone
if (Tombstone == true)
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.TOMBSTONE;
info.vValue = new AdsValueHelper(Tombstone).GetStruct();
prefList.Add(info);
}
// attributescopequery
if (_attributeScopeQuerySpecified)
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.ATTRIBUTE_QUERY;
info.vValue = new AdsValueHelper(AttributeScopeQuery, AdsType.ADSTYPE_CASE_IGNORE_STRING).GetStruct();
prefList.Add(info);
}
// derefalias
if (DerefAlias != DereferenceAlias.Never)
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.DEREF_ALIASES;
info.vValue = new AdsValueHelper((int)DerefAlias).GetStruct();
prefList.Add(info);
}
// securitymask
if (SecurityMasks != SecurityMasks.None)
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.SECURITY_MASK;
info.vValue = new AdsValueHelper((int)SecurityMasks).GetStruct();
prefList.Add(info);
}
// extendeddn
if (ExtendedDN != ExtendedDN.None)
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.EXTENDED_DN;
info.vValue = new AdsValueHelper((int)ExtendedDN).GetStruct();
prefList.Add(info);
}
// dirsync
if (directorySynchronizationSpecified)
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.DIRSYNC;
info.vValue = new AdsValueHelper(DirectorySynchronization.GetDirectorySynchronizationCookie(), AdsType.ADSTYPE_PROV_SPECIFIC).GetStruct();
prefList.Add(info);
if (DirectorySynchronization.Option != DirectorySynchronizationOptions.None)
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.DIRSYNC_FLAG;
info.vValue = new AdsValueHelper((int)DirectorySynchronization.Option).GetStruct();
prefList.Add(info);
}
}
IntPtr ptrToFree = (IntPtr)0;
IntPtr ptrVLVToFree = (IntPtr)0;
IntPtr ptrVLVContexToFree = (IntPtr)0;
try
{
// sort
if (Sort.PropertyName != null && Sort.PropertyName.Length > 0)
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.SORT_ON;
AdsSortKey sortKey = new AdsSortKey();
sortKey.pszAttrType = Marshal.StringToCoTaskMemUni(Sort.PropertyName);
ptrToFree = sortKey.pszAttrType; // so we can free it later.
sortKey.pszReserved = (IntPtr)0;
sortKey.fReverseOrder = (Sort.Direction == SortDirection.Descending) ? -1 : 0;
byte[] sortKeyBytes = new byte[Marshal.SizeOf(sortKey)];
Marshal.Copy((INTPTR_INTPTRCAST)(&sortKey), sortKeyBytes, 0, sortKeyBytes.Length);
info.vValue = new AdsValueHelper(sortKeyBytes, AdsType.ADSTYPE_PROV_SPECIFIC).GetStruct();
prefList.Add(info);
}
// vlv
if (directoryVirtualListViewSpecified)
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.VLV;
AdsVLV vlvValue = new AdsVLV();
vlvValue.beforeCount = _vlv.BeforeCount;
vlvValue.afterCount = _vlv.AfterCount;
vlvValue.offset = _vlv.Offset;
//we need to treat the empty string as null here
if (_vlv.Target.Length != 0)
vlvValue.target = Marshal.StringToCoTaskMemUni(_vlv.Target);
else
vlvValue.target = IntPtr.Zero;
ptrVLVToFree = vlvValue.target;
if (_vlv.DirectoryVirtualListViewContext == null)
{
vlvValue.contextIDlength = 0;
vlvValue.contextID = (IntPtr)0;
}
else
{
vlvValue.contextIDlength = _vlv.DirectoryVirtualListViewContext._context.Length;
vlvValue.contextID = Marshal.AllocCoTaskMem(vlvValue.contextIDlength);
ptrVLVContexToFree = vlvValue.contextID;
Marshal.Copy(_vlv.DirectoryVirtualListViewContext._context, 0, vlvValue.contextID, vlvValue.contextIDlength);
}
IntPtr vlvPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(AdsVLV)));
byte[] vlvBytes = new byte[Marshal.SizeOf(vlvValue)];
try
{
Marshal.StructureToPtr(vlvValue, vlvPtr, false);
Marshal.Copy(vlvPtr, vlvBytes, 0, vlvBytes.Length);
}
finally
{
Marshal.FreeHGlobal(vlvPtr);
}
info.vValue = new AdsValueHelper(vlvBytes, AdsType.ADSTYPE_PROV_SPECIFIC).GetStruct();
prefList.Add(info);
}
// cacheResults
if (_cacheResultsSpecified)
{
info = new AdsSearchPreferenceInfo();
info.dwSearchPref = (int)AdsSearchPreferences.CACHE_RESULTS;
info.vValue = new AdsValueHelper(CacheResults).GetStruct();
prefList.Add(info);
}
//
// now make the call
//
AdsSearchPreferenceInfo[] prefs = new AdsSearchPreferenceInfo[prefList.Count];
for (int i = 0; i < prefList.Count; i++)
{
prefs[i] = (AdsSearchPreferenceInfo)prefList[i];
}
DoSetSearchPrefs(adsSearch, prefs);
}
finally
{
if (ptrToFree != (IntPtr)0)
Marshal.FreeCoTaskMem(ptrToFree);
if (ptrVLVToFree != (IntPtr)0)
Marshal.FreeCoTaskMem(ptrVLVToFree);
if (ptrVLVContexToFree != (IntPtr)0)
Marshal.FreeCoTaskMem(ptrVLVContexToFree);
}
}
private static void DoSetSearchPrefs(UnsafeNativeMethods.IDirectorySearch adsSearch, AdsSearchPreferenceInfo[] prefs)
{
int structSize = Marshal.SizeOf(typeof(AdsSearchPreferenceInfo));
IntPtr ptr = Marshal.AllocHGlobal((IntPtr)(structSize * prefs.Length));
try
{
IntPtr tempPtr = ptr;
for (int i = 0; i < prefs.Length; i++)
{
Marshal.StructureToPtr(prefs[i], tempPtr, false);
tempPtr = IntPtr.Add(tempPtr, structSize);
}
adsSearch.SetSearchPreference(ptr, prefs.Length);
// Check for the result status for all preferences
tempPtr = ptr;
for (int i = 0; i < prefs.Length; i++)
{
int status = Marshal.ReadInt32(tempPtr, 32);
if (status != 0)
{
int prefIndex = prefs[i].dwSearchPref;
string property = "";
switch (prefIndex)
{
case (int)AdsSearchPreferences.SEARCH_SCOPE:
property = "SearchScope";
break;
case (int)AdsSearchPreferences.SIZE_LIMIT:
property = "SizeLimit";
break;
case (int)AdsSearchPreferences.TIME_LIMIT:
property = "ServerTimeLimit";
break;
case (int)AdsSearchPreferences.ATTRIBTYPES_ONLY:
property = "PropertyNamesOnly";
break;
case (int)AdsSearchPreferences.TIMEOUT:
property = "ClientTimeout";
break;
case (int)AdsSearchPreferences.PAGESIZE:
property = "PageSize";
break;
case (int)AdsSearchPreferences.PAGED_TIME_LIMIT:
property = "ServerPageTimeLimit";
break;
case (int)AdsSearchPreferences.CHASE_REFERRALS:
property = "ReferralChasing";
break;
case (int)AdsSearchPreferences.SORT_ON:
property = "Sort";
break;
case (int)AdsSearchPreferences.CACHE_RESULTS:
property = "CacheResults";
break;
case (int)AdsSearchPreferences.ASYNCHRONOUS:
property = "Asynchronous";
break;
case (int)AdsSearchPreferences.TOMBSTONE:
property = "Tombstone";
break;
case (int)AdsSearchPreferences.ATTRIBUTE_QUERY:
property = "AttributeScopeQuery";
break;
case (int)AdsSearchPreferences.DEREF_ALIASES:
property = "DerefAlias";
break;
case (int)AdsSearchPreferences.SECURITY_MASK:
property = "SecurityMasks";
break;
case (int)AdsSearchPreferences.EXTENDED_DN:
property = "ExtendedDn";
break;
case (int)AdsSearchPreferences.DIRSYNC:
property = "DirectorySynchronization";
break;
case (int)AdsSearchPreferences.DIRSYNC_FLAG:
property = "DirectorySynchronizationFlag";
break;
case (int)AdsSearchPreferences.VLV:
property = "VirtualListView";
break;