forked from ibv/LDAP-Admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.pas
874 lines (795 loc) · 26.6 KB
/
Connection.pas
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
{ LDAPAdmin - Connection.pas
* Copyright (C) 2012-2016 Tihomir Karlovic
*
* Author: Tihomir Karlovic
*
*
* This file 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 2 of the License, or
* (at your option) any later version.
*
* This file 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
}
unit Connection;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
interface
uses Config, Sorter, Schema, LdapClasses, CustomMenus, Controls, Templates,
{$ifdef mswindows}WinLDAP, {$else} LinLDAP, {$endif}
Constant, TextFile, Bookmarks;
const
{ Posix objects }
oidUnknown = -1;
oidEntry = 0;
oidRoot = oidEntry + 1;
oidPosixUser = oidRoot + 1;
oidSambaUser = oidPosixUser + 1;
oidComputer = oidSambaUser + 1;
oidGroup = oidComputer + 1;
oidSambaGroup = oidGroup + 1;
oidMailGroup = oidSambaGroup + 1;
oidTransportTable = oidMailGroup + 1;
oidOU = oidTransportTable + 1;
oidHost = oidOU + 1;
oidLocality = oidHost + 1;
oidGroupOfUN = oidLocality + 1;
oidAlias = oidGroupOfUN + 1;
oidSudoer = oidAlias + 1;
oidSambaDomain = oidSudoer + 1;
oidIdPool = oidSambaDomain + 1;
{ AD objects }
oidAdUser = oidIdPool + 1;
oidADComputer = oidAdUser + 1;
oidADContainer = oidAdComputer + 1;
oidADGroup = oidADContainer + 1;
oidADClassSchema = oidADGroup + 1;
oidADAttributeSchema = oidADClassSchema + 1;
oidADSchema = oidADAttributeSchema + 1;
oidADConfiguration = oidADSchema + 1;
ObjectIdToImage: array[oidEntry..oidADConfiguration] of Integer = (
bmEntry,
bmRoot,
bmPosixUser,
bmSamba3User,
bmComputer,
bmGroup,
bmSambaGroup,
bmMailGroup,
bmTransport,
bmOU,
bmHost,
bmLocality,
bmGrOfUnqNames,
bmAlias,
bmSudoer,
bmSambaDomain,
bmIdPool,
bmSamba3USer,
bmComputer,
bmContainer,
bmADGroup,
bmClassSchema,
bmAttributeSchema,
bmSchema,
bmConfiguration);
type
TDirectoryType = (dtAutodetect, dtPosix, dtActiveDirectory);
TConnection = class;
IDirectoryIdentity = Interface
function ClassifyLdapEntry(Entry: TLdapEntry): Integer;
function NewProperty(Owner: TControl; const Index: Integer; const dn: string): Boolean;
function EditProperty(Owner: TControl; const Index: Integer; const dn: string): Boolean;
function ChangePassword(Entry: TLdapEntry): Boolean;
function IsContainer(Index: Integer): Boolean;
function CreateMenu: TCustomActionMenu;
end;
TConnection = class(TLdapSession)
private
FAccount: TAccount;
FLVSorter: TListViewSorter;
FSchema: TLdapSchema;
FSelected: string;
FDirectoryIdentity: IDirectoryIdentity;
FActionMenu: TCustomActionMenu;
FBookmarks: TBookmarks;
function GetDirectoryType: TDirectoryType;
function GetDirectoryIdentity: IDirectoryIdentity;
function GetActionMenu: TCustomActionMenu;
function GetSchema: TLdapSchema;
function GetFreeRandomNumber(const Min, Max: Integer; const Objectclass, id: string): Integer;
function GetSequentialNumber(const Min, Max: Integer; const Objectclass, id: string): Integer;
public
constructor Create(Account: TAccount);
destructor Destroy; override;
function GetObjectId(Entry: TLdapEntry): Integer;
function GetImageIndex(Entry: TLdapEntry): Integer;
function GetFreeUidNumber(const MinUid, MaxUID: Integer; const Sequential: Boolean): Integer;
function GetFreeGidNumber(const MinGid, MaxGid: Integer; const Sequential: Boolean): Integer;
function GetUid: Integer;
function GetGid: Integer;
property Account: TAccount read FAccount;
property LVSorter: TListViewSorter read FLVSorter write FLVSorter;
property Schema: TLdapSchema read GetSchema;
property Selected: string read FSelected write FSelected;
property DirectoryType: TDirectoryType read GetDirectoryType;
property DI: IDirectoryIdentity read GetDirectoryIdentity;
property ActionMenu: TCustomActionMenu read GetActionMenu;
property Bookmarks: TBookmarks read FBookmarks;
end;
{ Local LDAP DB }
TDBAccount=class(TFakeAccount)
private
FFileName: string;
public
property FileName: string read FFileName write FFileName;
end;
TEntryNode = class(TLdapEntry)
private
fChildren: TLdapEntryList;
public
constructor Create(const ASession: TLDAPSession; const adn: string); override;
destructor Destroy; override;
property Children: TLdapEntryList read fChildren;
end;
TLoadCallback = procedure(BytesRead: Integer; var Abort: Boolean) of object;
TDBConnection = class(TConnection)
private
Root: TEntryNode;
fModified: Boolean;
fDestroying: Boolean;
fEncoding: TFileEncode;
function FindNode(const adn: string): TEntryNode;
protected
function ISConnected: Boolean; override;
public
constructor Create(Account: TDBAccount; CallbackProc: TLoadCallback = nil);
destructor Destroy; override;
procedure Search(const Filter, Base: string; const Scope: Cardinal; attrs: PCharArray; const NoValues: LongBool; Result: TLdapEntryList; SearchProc: TSearchCallback = nil); overload; override;
function Lookup(sBase, sFilter, sResult: string; Scope: Cardinal): string; override;
function GetDn(sFilter: string): string; override;
procedure WriteEntry(Entry: TLdapEntry); override;
procedure ReadEntry(Entry: TLdapEntry); override;
procedure DeleteEntry(const adn: string); override;
procedure SaveToFile(FileName: string);
procedure Connect; override;
procedure Disconnect; override;
end;
var
UseTemplateImages: Boolean;
implementation
uses SysUtils, User, Host, Locality, Computer, Group, LDIF, Dialogs,
MailGroup, Transport, Ou, Classes, PassDlg, ADPassDlg, Alias, Ast ;
{ IDirectoryIdentity }
type
TCustomDirectoryIdentity = class(TInterfacedObject)
private
FConnection: TConnection;
public
constructor Create(Connection: TConnection);
property Connection: TConnection read FConnection;
end;
TPosixDirectoryIdentity = class(TCustomDirectoryIdentity, IDirectoryIdentity)
public
function ClassifyLdapEntry(Entry: TLdapEntry): Integer;
function NewProperty(Owner: TControl; const Index: Integer; const dn: string): Boolean;
function EditProperty(Owner: TControl; const Index: Integer; const dn: string): Boolean;
function ChangePassword(Entry: TLdapEntry): Boolean;
function IsContainer(Index: Integer): Boolean;
function CreateMenu: TCustomActionMenu;
end;
TADDirectoryIdentity = class(TCustomDirectoryIdentity, IDirectoryIdentity)
public
function ClassifyLdapEntry(Entry: TLdapEntry): Integer;
function NewProperty(Owner: TControl; const Index: Integer; const dn: string): Boolean;
function EditProperty(Owner: TControl; const Index: Integer; const dn: string): Boolean;
function ChangePassword(Entry: TLdapEntry): Boolean;
function IsContainer(Index: Integer): Boolean;
function CreateMenu: TCustomActionMenu;
end;
function TConnection.GetDirectoryType: TDirectoryType;
begin
Result := TDirectoryType(Account.ReadInteger(rDirectoryType, Integer(dtAutodetect)));
if (Result = dtAutodetect) and Connected then
begin
if Lookup('', sAnyClass,'isGlobalCatalogReady', LDAP_SCOPE_BASE) <> '' then
Result := dtActiveDirectory
else
Result := dtPosix;
Account.WriteInteger(rDirectoryType, Integer(Result));
end;
end;
function TConnection.GetSchema: TLdapSchema;
begin
if not Assigned(FSchema) then
FSchema := TLdapSchema.Create(Self);
Result := FSchema;
end;
function TConnection.GetDirectoryIdentity: IDirectoryIdentity;
begin
if not Assigned(FDirectoryIdentity) then
begin
case DirectoryType of
dtActiveDirectory: fDirectoryIdentity := TADDirectoryIdentity.Create(Self);
else
fDirectoryIdentity := TPosixDirectoryIdentity.Create(Self);
end;
end;
Result := FDirectoryIdentity;
end;
function TConnection.GetActionMenu: TCustomActionMenu;
begin
if not Assigned(FActionMenu) then
FActionMenu := DI.CreateMenu;
Result := FActionMenu;
end;
constructor TConnection.Create(Account: TAccount);
begin
inherited Create;
FAccount := Account;
try
FLVSorter := TListViewSorter.Create;
FBookmarks := TBookmarks.Create(Self);
except
on E: Exception do
MessageDlg(E.Message, mtError, [mbOk], 0);
end;
end;
destructor TConnection.Destroy;
begin
try
FActionMenu.Free;
FSchema.Free;
FLVSorter.Free;
FBookmarks.Free;
except
on E: Exception do
MessageDlg(E.Message, mtError, [mbOk], 0);
end;
inherited;
end;
function TConnection.GetObjectId(Entry: TLdapEntry): Integer;
begin
Result := DI.ClassifyLdapEntry(Entry);
end;
function TConnection.GetImageIndex(Entry: TLdapEntry): Integer;
var
i: Integer;
begin
Result := ObjectIdToImage[GetObjectId(Entry)];
if (Result = bmEntry) and UseTemplateImages then
for i := 0 to TemplateParser.Count - 1 do with TemplateParser.Templates[i] do
if (ImageIndex <> -1 ) and Matches(Entry.ObjectClass) then
begin
Result := ImageIndex;
break;
end;
end;
{ Get random free uidNumber from the pool of available numbers, return -1 if
no more free numbers are available }
function TConnection.GetFreeRandomNumber(const Min, Max: Integer; const Objectclass, id: string): Integer;
var
i: Integer;
uidpool: array of Word;
r, N: Word;
begin
N := Max - Min + 1;
SetLength(uidpool, N);
{ Initialize the array }
for i := 0 to N - 1 do
uidpool[i] := i;
Randomize;
while N > 0 do
begin
r := Random(N);
Result := Min + uidpool[r];
if Lookup(Base, Format('(&(objectclass=%s)(%s=%d))', [Objectclass, id, Result]), 'objectclass', LDAP_SCOPE_SUBTREE) = '' then
exit;
uidpool[r] := uidpool[N - 1];
dec(N);
end;
Result := -1;
end;
{ Get sequential free uidNumber from the pool of available numbers, return -1 if
no more free numbers are available }
function TConnection.GetSequentialNumber(const Min, Max: Integer; const Objectclass, id: string): Integer;
var
attrs: PCharArray;
i, n: Integer;
SearchList: TLdapEntryList;
begin
Result := Min;
SetLength(attrs, 2);
attrs[0] := PChar(id);
attrs[1] := nil;
SearchList := TLdapEntryList.Create;
try
Search(Format('(objectclass=%s)', [Objectclass]), Base, LDAP_SCOPE_ONELEVEL, attrs, false, SearchList);
for i := 0 to SearchList.Count - 1 do
begin
n := StrToInt(SearchList[i].Attributes[0].AsString);
if (n <= Max) and (n >= Result) then
begin
Result := n + 1;
if Result > Max then
begin
Result := -1;
break;
end;
end;
end;
finally
SearchList.Free;
end;
end;
function TConnection.GetFreeUidNumber(const MinUid, MaxUID: Integer; const Sequential: Boolean): Integer;
begin
if Sequential then
Result := GetSequentialNumber(MinUid, MaxUid, 'posixAccount', 'uidNumber')
else
Result := GetFreeRandomNumber(MinUid, MaxUid, 'posixAccount', 'uidNumber');
if Result = -1 then
raise Exception.Create(Format(stNoMoreNums, ['uidNumber']));
end;
function TConnection.GetFreeGidNumber(const MinGid, MaxGid: Integer; const Sequential: Boolean): Integer;
begin
if Sequential then
Result := GetSequentialNumber(MinGid, MaxGid, 'posixGroup', 'gidNumber')
else
Result := GetFreeRandomNumber(MinGid, MaxGid, 'posixGroup', 'gidNumber');
if Result = -1 then
raise Exception.Create(Format(stNoMoreNums, ['gidNumber']));
end;
function TConnection.GetUid: Integer;
var
IdType: Integer;
begin
Result := -1;
idType := Account.ReadInteger(rPosixIDType, POSIX_ID_RANDOM);
if idType <> POSIX_ID_NONE then
Result := GetFreeUidNumber(Account.ReadInteger(rposixFirstUID, FIRST_UID),
Account.ReadInteger(rposixLastUID, LAST_UID),
IdType = POSIX_ID_SEQUENTIAL);
end;
function TConnection.GetGid: Integer;
var
IdType: Integer;
begin
Result := -1;
idType := Account.ReadInteger(rPosixIDType, POSIX_ID_RANDOM);
if idType <> POSIX_ID_NONE then
Result := GetFreeGidNumber(Account.ReadInteger(rposixFirstGid, FIRST_GID),
Account.ReadInteger(rposixLastGID, LAST_GID),
IdType = POSIX_ID_SEQUENTIAL);
end;
{ TCustomDirectoryIdentity }
constructor TCustomDirectoryIdentity.Create(Connection: TConnection);
begin
FConnection := Connection;
end;
{ TPosixDirectoryIDentity }
function TPosixDirectoryIdentity.ClassifyLdapEntry(Entry: TLdapEntry): Integer;
var
Attr: TLdapAttribute;
i: integer;
s: string;
function IsComputer(const s: string): Boolean;
var
i: Integer;
begin
i := Pos(',', s);
Result := (i > 1) and (s[i - 1] = '$');
end;
begin
Result := oidEntry;
Attr := Entry.Objectclass;
i := Attr.ValueCount - 1;
while i >= 0 do
begin
s := lowercase(Attr.Values[i].AsString);
if s = 'organizationalunit' then
Result := oidOu
else if s = 'posixaccount' then
begin
if Result = oidEntry then // if not yet assigned to Samba account
Result := oidPosixUser;
end
else if s = 'sambasamaccount' then
begin
if IsComputer(Entry.dn) then // it's samba computer account
Result := oidComputer // else
else // it's samba user account
Result := oidSambaUser;
end
else if s = 'sambagroupmapping' then
Result := oidSambaGroup
else if s = 'mailgroup' then
Result := oidMailGroup
else if s = 'posixgroup' then
begin
if Result = oidEntry then // if not yet assigned to Samba group
Result := oidGroup;
end
else if s = 'groupofuniquenames' then
Result := oidGroupOfUN
else if s = 'alias' then
Result := oidAlias
else if s = 'transporttable' then
Result := oidTransportTable
else if s = 'sudorole' then
Result := oidSudoer
else if s = 'iphost' then
Result := oidHost
else if s = 'locality' then
Result := oidLocality
else if s = 'sambadomain' then
Result := oidSambaDomain
else if s = 'sambaunixidpool' then
Result := oidIdPool;
Dec(i);
end;
end;
function TPosixDirectoryIdentity.NewProperty(Owner: TControl; const Index: Integer; const dn: string): Boolean;
begin
Result := true;
case Index of
aidUser: TUserDlg.Create(Owner, dn, Connection, EM_ADD).ShowModal;
aidComputer: TComputerDlg.Create(Owner, dn, Connection, EM_ADD).ShowModal;
aidGroup: TGroupDlg.Create(Owner, dn, Connection, EM_ADD, true, Connection.Account.ReadInteger(rPosixGroupOfUnames, 0)).ShowModal;
aidMailingList: TMailGroupDlg.Create(Owner, dn, Connection, EM_ADD).ShowModal;
aidTransportTable: TTransportDlg.Create(Owner, dn, Connection, EM_ADD).ShowModal;
aidOU: TOuDlg.Create(Owner, dn, Connection, EM_ADD).ShowModal;
aidHost: THostDlg.Create(Owner, dn, Connection, EM_ADD).ShowModal;
aidLocality: TLocalityDlg.Create(Owner, dn, Connection, EM_ADD).ShowModal;
aidGroupOfUN: TGroupDlg.Create(Owner, dn, Connection, EM_ADD, false, 1).ShowModal;
aidAlias: TAliasDlg.Create(Owner, dn, Connection, EM_ADD).ShowModal;
else
Result := false;
end;
end;
function TPosixDirectoryIdentity.EditProperty(Owner: TControl; const Index: Integer; const dn: string): Boolean;
begin
Result := true;
case Index of
aidUser: TUserDlg.Create(Owner, dn, Connection, EM_MODIFY).ShowModal;
aidGroup,
aidGroupOfUn: TGroupDlg.Create(Owner, dn, Connection, EM_MODIFY).ShowModal;
aidMailingList: TMailGroupDlg.Create(Owner, dn, Connection, EM_MODIFY).ShowModal;
aidComputer: TComputerDlg.Create(Owner, dn, Connection, EM_MODIFY).ShowModal;
aidTransportTable: TTransportDlg.Create(Owner, dn, Connection, EM_MODIFY).ShowModal;
aidOu: TOuDlg.Create(Owner, dn, Connection, EM_MODIFY).ShowModal;
aidLocality: TLocalityDlg.Create(Owner, dn, Connection, EM_MODIFY).ShowModal;
aidHost: THostDlg.Create(Owner, dn, Connection, EM_MODIFY).ShowModal;
aidAlias: TAliasDlg.Create(Owner, dn, Connection, EM_MODIFY).ShowModal;
else
Result := false;
end;
end;
function TPosixDirectoryIdentity.ChangePassword(Entry: TLdapEntry): Boolean;
begin
with TPasswordDlg.Create(nil, Entry) do
try
if ShowModal = mrOk then
begin
Entry.Write;
Result := true;
end
else
Result := false;
finally
Free;
end;
end;
function TPosixDirectoryIdentity.IsContainer(Index: Integer): Boolean;
begin
Result := Index in [oidOu, oidLocality, oidEntry, oidRoot];
end;
function TPosixDirectoryIdentity.CreateMenu: TCustomActionMenu;
begin
Result := TPosixActionMenu.Create(Connection.Account);
end;
{ TADDirectoryIdentity }
function TADDirectoryIdentity.ClassifyLdapEntry(Entry: TLdapEntry): Integer;
var
Attr: TLdapAttribute;
i: integer;
s: string;
begin
Result := oidEntry;
Attr := Entry.ObjectClass;
i := Attr.ValueCount - 1;
while i >= 0 do
begin
s := lowercase(Attr.Values[i].AsString);
if s = 'organizationalunit' then
Result := oidOu
else if s = 'container' then
Result := oidADContainer
else if s = 'user' then
begin
if Result = oidEntry then // if not yet assigned to computer account
Result := oidADUser;
end
else
if s = 'computer' then
Result := oidADComputer
else if s = 'group' then
Result := oidADGroup
else if s = 'locality' then
Result := oidLocality
else if s = 'configuration' then
Result := oidADConfiguration
else if s = 'classschema' then
Result := oidADClassSchema
else if s = 'attributeschema' then
Result := oidADAttributeSchema
else if (s = 'dmd') or (s = 'subschema') then
Result := oidADSchema;
Dec(i);
end;
end;
function TADDirectoryIdentity.NewProperty(Owner: TControl; const Index: Integer; const dn: string): Boolean;
begin
Result := true;
case Index of
aidOu: TOuDlg.Create(Owner, dn, Connection, EM_ADD).ShowModal;
aidHost: THostDlg.Create(Owner, dn, Connection, EM_ADD).ShowModal;
aidLocality: TLocalityDlg.Create(Owner, dn, Connection, EM_ADD).ShowModal;
else
Result := false;
end;
end;
function TADDirectoryIdentity.EditProperty(Owner: TControl; const Index: Integer; const dn: string): Boolean;
begin
Result := true;
case Index of
oidOu: TOuDlg.Create(Owner, dn, Connection, EM_MODIFY).ShowModal;
oidLocality: TLocalityDlg.Create(Owner, dn, Connection, EM_MODIFY).ShowModal;
oidHost: THostDlg.Create(Owner, dn, Connection, EM_MODIFY).ShowModal;
else
Result := false;
end;
end;
function TADDirectoryIdentity.ChangePassword(Entry: TLdapEntry): Boolean;
begin
with TADPassDlg.Create(nil, Entry) do
try
Result := ShowModal = mrOk;
finally
Free;
end;
end;
function TADDirectoryIdentity.IsContainer(Index: Integer): Boolean;
begin
Result := Index in [oidOu, oidLocality, oidEntry, oidRoot, oidADContainer, oidADConfiguration, oidADSchema];
end;
function TADDirectoryIdentity.CreateMenu: TCustomActionMenu;
begin
Result := TADActionMenu.Create(Connection.Account);
end;
{ Local LDAP DB }
constructor TEntryNode.Create(const ASession: TLDAPSession; const adn: string);
begin
inherited;
fChildren := TLdapEntryList.Create;
end;
destructor TEntryNode.Destroy;
begin
fChildren.Free;
inherited;
end;
{ Finds a matching node for a whole or a part of a dn }
function TDBConnection.FindNode(const adn: string): TEntryNode;
var
i: Integer;
Parent: TEntryNode;
begin
Parent := Root;
Result := nil;
while Parent <> Result do
begin
Result := Parent;
for i := 0 to Parent.Children.Count - 1 do with Parent.Children[i] do
begin
if AnsiSameText(dn, Copy(adn, Length(adn) - Length(dn) + 1, MaxInt)) then
begin
Parent := Parent.Children[i] as TEntryNode;
break; // for i
end;
end;
end;
end;
constructor TDBConnection.Create(Account: TDBAccount; CallbackProc: TLoadCallback = nil);
var
Entry, Parent: TEntryNode;
ldFile: TLdifFile;
Stop: Boolean;
begin
inherited Create(Account);
Root := TEntryNode.Create(Self, '');
Account.LdapVersion := LDAP_VERSION3;
ldFile := TLDIFFile.Create(Account.FileName, fmRead);
with ldFile do
try
fEncoding := Encoding;
while not (eof or Stop) do begin
Entry := TEntryNode.Create(Self, '');
ReadRecord(Entry);
if Entry.dn = '' then
begin
FreeAndNil(Entry);
break;
end;
if not (esDeleted in Entry.State) then
begin
Parent := FindNode(GetDirFromDn(Entry.dn));
if Assigned(Parent) then
Parent.Children.Add(Entry);
end;
if Assigned(CallbackProc) then
CallbackProc(NumRead, Stop);
end;
finally
FreeAndNil(ldFile);
end;
Account.Base := Base;
end;
destructor TDBConnection.Destroy;
begin
fDestroying := true;
inherited; // inherited calls disconnect
Root.Free;
end;
{ Note: NoValue and attrs are ignored, for the time being all attributes and values are returned }
{ Also: Implement FirstOnly to stop traversing of the tree tree when only one result is expected (usefull for Lookup and GetDn functions }
procedure TDBConnection.Search(const Filter, Base: string; const Scope: Cardinal; attrs: PCharArray; const NoValues: LongBool; Result: TLdapEntryList; SearchProc: TSearchCallback = nil);
var
BaseNode: TEntryNode;
FilterNode :TAstNode;
begin
if Base ='' then
BaseNode := Root
else
BaseNode := FindNode(Base);
if Assigned(BaseNode) then
begin
FilterNode := Parse(Filter);
FilterNode.ExecuteFilter(Self, BaseNode, Scope, Result);
end;
end;
procedure TDBConnection.WriteEntry(Entry: TLdapEntry);
var
Node, NewNode: TentryNode;
begin
if esNew in Entry.State then
begin
Node := FindNode(GetDirFromDN(Entry.dn)); // find parent
if not Assigned(Node) then
raise Exception.CreateFmt(stDirNotExists, [GetDirFromDN(Entry.dn)]);
NewNode := TEntryNode.Create(Self, Entry.dn);
Entry.Clone(NewNode);
Node.Children.Add(NewNode);
end
else begin
{ else update existing node }
Node := FindNode(Entry.dn);
if Assigned(Node) then
begin
Node.Attributes.Clear;
Node.OperationalAttributes.Clear;
Entry.Clone(Node);
end;
end;
fModified := true;
end;
procedure TDBConnection.ReadEntry(Entry: TLdapEntry);
var
Node: TentryNode;
begin
Node := FindNode(Entry.dn);
if Assigned(Node) then
Node.Clone(Entry);
end;
procedure TDBConnection.DeleteEntry(const adn: string);
var
ParentNode: TentryNode;
i: Integer;
begin
ParentNode := FindNode(GetDirFromDn(adn));
if not Assigned(ParentNode) then
raise Exception.CreateFmt(stPathNotFound, [GetDirFromDn(adn)]);
i := ParentNode.Children.IndexOf(adn);
if i = -1 then
raise Exception.CreateFmt(stObjectNotFound, [adn]);
if TEntryNode(ParentNode.Children[i]).Children.Count > 0 then
raise Exception.Create(stNodeHasChildren);
ParentNode.Children.Delete(i);
end;
procedure TDBConnection.SaveToFile(FileName: string);
var
ldif: TLDIFFile;
i: Integer;
procedure WriteNode(ANode: TEntryNode);
var
i: Integer;
begin
ldif.WriteRecord(ANode);
for i := 0 to ANode.Children.Count - 1 do
WriteNode(ANode.Children[i] as TEntryNode);
end;
begin
ldif := TLDIFFile.Create(FileName, fmWrite);
try
ldif.Encoding := fEncoding;
{ Skip the artificial root }
for i := 0 to Root.Children.Count - 1 do
WriteNode(Root.Children[i] as TEntryNode);
finally
ldif.Free;
end;
end;
procedure TDBConnection.Connect;
begin
end;
procedure TDBConnection.Disconnect;
var
Buttons: TMsgDlgButtons;
begin
if not Connected then exit;
if fModified then
begin
Buttons := [mbYes, mbNo];
if not fDestroying then
Buttons := Buttons + [mbCancel];
case MessageDlg(Format(stFileModified, [(Account as TDBAccount).FileName]),
mtConfirmation, Buttons, 0) of
mrYes: SaveToFile((Account as TDBAccount).FileName);
mrCancel: Abort;
end;
fModified := false;
end;
OnDisconnect.Execute(self);
end;
function TDBConnection.ISConnected: Boolean;
begin
Result := Assigned(Root);
end;
function TDBConnection.Lookup(sBase, sFilter, sResult: string; Scope: Cardinal): string;
var
l: TLdapEntryList;
begin
l := TLdapEntryList.Create;
try
Search(sFilter, sBase, Scope, nil, true, l);
if l.Count > 0 then
Result := l[0].AttributesByName[sResult].AsString
else
Result := '';
finally
l.Free;
end;
end;
function TDBConnection.GetDn(sFilter: string): string;
var
l: TLdapEntryList;
begin
l := TLdapEntryList.Create;
try
Search(sFilter, Base, LDAP_SCOPE_SUBTREE, nil, true, l);
if l.Count > 0 then
Result := l[0].dn
else
Result := '';
finally
l.Free;
end;
end;
end.