ldapsend.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. {==============================================================================|
  2. | Project : Ararat Synapse | 001.007.001 |
  3. |==============================================================================|
  4. | Content: LDAP client |
  5. |==============================================================================|
  6. | Copyright (c)1999-2014, Lukas Gebauer |
  7. | All rights reserved. |
  8. | |
  9. | Redistribution and use in source and binary forms, with or without |
  10. | modification, are permitted provided that the following conditions are met: |
  11. | |
  12. | Redistributions of source code must retain the above copyright notice, this |
  13. | list of conditions and the following disclaimer. |
  14. | |
  15. | Redistributions in binary form must reproduce the above copyright notice, |
  16. | this list of conditions and the following disclaimer in the documentation |
  17. | and/or other materials provided with the distribution. |
  18. | |
  19. | Neither the name of Lukas Gebauer nor the names of its contributors may |
  20. | be used to endorse or promote products derived from this software without |
  21. | specific prior written permission. |
  22. | |
  23. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
  24. | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
  25. | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
  26. | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
  27. | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
  28. | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
  29. | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
  30. | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  31. | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
  32. | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
  33. | DAMAGE. |
  34. |==============================================================================|
  35. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  36. | Portions created by Lukas Gebauer are Copyright (c)2003-2014. |
  37. | All Rights Reserved. |
  38. |==============================================================================|
  39. | Contributor(s): |
  40. |==============================================================================|
  41. | History: see HISTORY.HTM from distribution package |
  42. | (Found at URL: http://www.ararat.cz/synapse/) |
  43. |==============================================================================}
  44. {:@abstract(LDAP client)
  45. Used RFC: RFC-2251, RFC-2254, RFC-2696, RFC-2829, RFC-2830
  46. }
  47. {$IFDEF FPC}
  48. {$MODE DELPHI}
  49. {$ENDIF}
  50. {$H+}
  51. {$IFDEF UNICODE}
  52. {$WARN IMPLICIT_STRING_CAST OFF}
  53. {$WARN IMPLICIT_STRING_CAST_LOSS OFF}
  54. {$ENDIF}
  55. unit ldapsend;
  56. interface
  57. uses
  58. SysUtils, Classes,
  59. blcksock, synautil, asn1util, synacode;
  60. const
  61. cLDAPProtocol = '389';
  62. LDAP_ASN1_BIND_REQUEST = $60;
  63. LDAP_ASN1_BIND_RESPONSE = $61;
  64. LDAP_ASN1_UNBIND_REQUEST = $42;
  65. LDAP_ASN1_SEARCH_REQUEST = $63;
  66. LDAP_ASN1_SEARCH_ENTRY = $64;
  67. LDAP_ASN1_SEARCH_DONE = $65;
  68. LDAP_ASN1_SEARCH_REFERENCE = $73;
  69. LDAP_ASN1_MODIFY_REQUEST = $66;
  70. LDAP_ASN1_MODIFY_RESPONSE = $67;
  71. LDAP_ASN1_ADD_REQUEST = $68;
  72. LDAP_ASN1_ADD_RESPONSE = $69;
  73. LDAP_ASN1_DEL_REQUEST = $4A;
  74. LDAP_ASN1_DEL_RESPONSE = $6B;
  75. LDAP_ASN1_MODIFYDN_REQUEST = $6C;
  76. LDAP_ASN1_MODIFYDN_RESPONSE = $6D;
  77. LDAP_ASN1_COMPARE_REQUEST = $6E;
  78. LDAP_ASN1_COMPARE_RESPONSE = $6F;
  79. LDAP_ASN1_ABANDON_REQUEST = $70;
  80. LDAP_ASN1_EXT_REQUEST = $77;
  81. LDAP_ASN1_EXT_RESPONSE = $78;
  82. LDAP_ASN1_CONTROLS = $A0;
  83. type
  84. {:@abstract(LDAP attribute with list of their values)
  85. This class holding name of LDAP attribute and list of their values. This is
  86. descendant of TStringList class enhanced by some new properties.}
  87. TLDAPAttribute = class(TStringList)
  88. private
  89. FAttributeName: AnsiString;
  90. FIsBinary: Boolean;
  91. protected
  92. function Get(Index: integer): string; override;
  93. procedure Put(Index: integer; const Value: string); override;
  94. procedure SetAttributeName(Value: AnsiString);
  95. public
  96. function Add(const S: string): Integer; override;
  97. published
  98. {:Name of LDAP attribute.}
  99. property AttributeName: AnsiString read FAttributeName Write SetAttributeName;
  100. {:Return @true when attribute contains binary data.}
  101. property IsBinary: Boolean read FIsBinary;
  102. end;
  103. {:@abstract(List of @link(TLDAPAttribute))
  104. This object can hold list of TLDAPAttribute objects.}
  105. TLDAPAttributeList = class(TObject)
  106. private
  107. FAttributeList: TList;
  108. function GetAttribute(Index: integer): TLDAPAttribute;
  109. public
  110. constructor Create;
  111. destructor Destroy; override;
  112. {:Clear list.}
  113. procedure Clear;
  114. {:Return count of TLDAPAttribute objects in list.}
  115. function Count: integer;
  116. {:Add new TLDAPAttribute object to list.}
  117. function Add: TLDAPAttribute;
  118. {:Delete one TLDAPAttribute object from list.}
  119. procedure Del(Index: integer);
  120. {:Find and return attribute with requested name. Returns nil if not found.}
  121. function Find(AttributeName: AnsiString): TLDAPAttribute;
  122. {:Find and return attribute value with requested name. Returns empty string if not found.}
  123. function Get(AttributeName: AnsiString): string;
  124. {:List of TLDAPAttribute objects.}
  125. property Items[Index: Integer]: TLDAPAttribute read GetAttribute; default;
  126. end;
  127. {:@abstract(LDAP result object)
  128. This object can hold LDAP object. (their name and all their attributes with
  129. values)}
  130. TLDAPResult = class(TObject)
  131. private
  132. FObjectName: AnsiString;
  133. FAttributes: TLDAPAttributeList;
  134. public
  135. constructor Create;
  136. destructor Destroy; override;
  137. published
  138. {:Name of this LDAP object.}
  139. property ObjectName: AnsiString read FObjectName write FObjectName;
  140. {:Here is list of object attributes.}
  141. property Attributes: TLDAPAttributeList read FAttributes;
  142. end;
  143. {:@abstract(List of LDAP result objects)
  144. This object can hold list of LDAP objects. (for example result of LDAP SEARCH.)}
  145. TLDAPResultList = class(TObject)
  146. private
  147. FResultList: TList;
  148. function GetResult(Index: integer): TLDAPResult;
  149. public
  150. constructor Create;
  151. destructor Destroy; override;
  152. {:Clear all TLDAPResult objects in list.}
  153. procedure Clear;
  154. {:Return count of TLDAPResult objects in list.}
  155. function Count: integer;
  156. {:Create and add new TLDAPResult object to list.}
  157. function Add: TLDAPResult;
  158. {:List of TLDAPResult objects.}
  159. property Items[Index: Integer]: TLDAPResult read GetResult; default;
  160. end;
  161. {:Define possible operations for LDAP MODIFY operations.}
  162. TLDAPModifyOp = (
  163. MO_Add,
  164. MO_Delete,
  165. MO_Replace
  166. );
  167. {:Specify possible values for search scope.}
  168. TLDAPSearchScope = (
  169. SS_BaseObject,
  170. SS_SingleLevel,
  171. SS_WholeSubtree
  172. );
  173. {:Specify possible values about alias dereferencing.}
  174. TLDAPSearchAliases = (
  175. SA_NeverDeref,
  176. SA_InSearching,
  177. SA_FindingBaseObj,
  178. SA_Always
  179. );
  180. {:@abstract(Implementation of LDAP client)
  181. (version 2 and 3)
  182. Note: Are you missing properties for setting Username and Password? Look to
  183. parent @link(TSynaClient) object!
  184. Are you missing properties for specify server address and port? Look to
  185. parent @link(TSynaClient) too!}
  186. TLDAPSend = class(TSynaClient)
  187. private
  188. FSock: TTCPBlockSocket;
  189. FResultCode: Integer;
  190. FResultString: AnsiString;
  191. FFullResult: AnsiString;
  192. FAutoTLS: Boolean;
  193. FFullSSL: Boolean;
  194. FSeq: integer;
  195. FResponseCode: integer;
  196. FResponseDN: AnsiString;
  197. FReferals: TStringList;
  198. FVersion: integer;
  199. FSearchScope: TLDAPSearchScope;
  200. FSearchAliases: TLDAPSearchAliases;
  201. FSearchSizeLimit: integer;
  202. FSearchTimeLimit: integer;
  203. FSearchPageSize: integer;
  204. FSearchCookie: AnsiString;
  205. FSearchResult: TLDAPResultList;
  206. FExtName: AnsiString;
  207. FExtValue: AnsiString;
  208. function Connect: Boolean;
  209. function BuildPacket(const Value: AnsiString): AnsiString;
  210. function ReceiveResponse: AnsiString;
  211. function DecodeResponse(const Value: AnsiString): AnsiString;
  212. function LdapSasl(Value: AnsiString): AnsiString;
  213. function TranslateFilter(Value: AnsiString): AnsiString;
  214. function GetErrorString(Value: integer): AnsiString;
  215. public
  216. constructor Create;
  217. destructor Destroy; override;
  218. {:Try to connect to LDAP server and start secure channel, when it is required.}
  219. function Login: Boolean;
  220. {:Try to bind to LDAP server with @link(TSynaClient.Username) and
  221. @link(TSynaClient.Password). If this is empty strings, then it do annonymous
  222. Bind. When you not call Bind on LDAPv3, then is automaticly used anonymous
  223. mode.
  224. This method using plaintext transport of password! It is not secure!}
  225. function Bind: Boolean;
  226. {:Try to bind to LDAP server with @link(TSynaClient.Username) and
  227. @link(TSynaClient.Password). If this is empty strings, then it do annonymous
  228. Bind. When you not call Bind on LDAPv3, then is automaticly used anonymous
  229. mode.
  230. This method using SASL with DIGEST-MD5 method for secure transfer of your
  231. password.}
  232. function BindSasl: Boolean;
  233. {:Close connection to LDAP server.}
  234. function Logout: Boolean;
  235. {:Modify content of LDAP attribute on this object.}
  236. function Modify(obj: AnsiString; Op: TLDAPModifyOp; const Value: TLDAPAttribute): Boolean;
  237. {:Add list of attributes to specified object.}
  238. function Add(obj: AnsiString; const Value: TLDAPAttributeList): Boolean;
  239. {:Delete this LDAP object from server.}
  240. function Delete(obj: AnsiString): Boolean;
  241. {:Modify object name of this LDAP object.}
  242. function ModifyDN(obj, newRDN, newSuperior: AnsiString; DeleteoldRDN: Boolean): Boolean;
  243. {:Try to compare Attribute value with this LDAP object.}
  244. function Compare(obj, AttributeValue: AnsiString): Boolean;
  245. {:Search LDAP base for LDAP objects by Filter.}
  246. function Search(obj: AnsiString; TypesOnly: Boolean; Filter: AnsiString;
  247. const Attributes: TStrings): Boolean;
  248. {:Call any LDAPv3 extended command.}
  249. function Extended(const Name, Value: AnsiString): Boolean;
  250. {:Try to start SSL/TLS connection to LDAP server.}
  251. function StartTLS: Boolean;
  252. published
  253. {:Specify version of used LDAP protocol. Default value is 3.}
  254. property Version: integer read FVersion Write FVersion;
  255. {:Result code of last LDAP operation.}
  256. property ResultCode: Integer read FResultCode;
  257. {:Human readable description of result code of last LDAP operation.}
  258. property ResultString: AnsiString read FResultString;
  259. {:Binary string with full last response of LDAP server. This string is
  260. encoded by ASN.1 BER encoding! You need this only for debugging.}
  261. property FullResult: AnsiString read FFullResult;
  262. {:If @true, then try to start TSL mode in Login procedure.}
  263. property AutoTLS: Boolean read FAutoTLS Write FAutoTLS;
  264. {:If @true, then use connection to LDAP server through SSL/TLS tunnel.}
  265. property FullSSL: Boolean read FFullSSL Write FFullSSL;
  266. {:Sequence number of last LDAp command. It is incremented by any LDAP command.}
  267. property Seq: integer read FSeq;
  268. {:Specify what search scope is used in search command.}
  269. property SearchScope: TLDAPSearchScope read FSearchScope Write FSearchScope;
  270. {:Specify how to handle aliases in search command.}
  271. property SearchAliases: TLDAPSearchAliases read FSearchAliases Write FSearchAliases;
  272. {:Specify result size limit in search command. Value 0 means without limit.}
  273. property SearchSizeLimit: integer read FSearchSizeLimit Write FSearchSizeLimit;
  274. {:Specify search time limit in search command (seconds). Value 0 means
  275. without limit.}
  276. property SearchTimeLimit: integer read FSearchTimeLimit Write FSearchTimeLimit;
  277. {:Specify number of results to return per search request. Value 0 means
  278. no paging.}
  279. property SearchPageSize: integer read FSearchPageSize Write FSearchPageSize;
  280. {:Cookie returned by paged search results. Use an empty string for the first
  281. search request.}
  282. property SearchCookie: AnsiString read FSearchCookie Write FSearchCookie;
  283. {:Here is result of search command.}
  284. property SearchResult: TLDAPResultList read FSearchResult;
  285. {:On each LDAP operation can LDAP server return some referals URLs. Here is
  286. their list.}
  287. property Referals: TStringList read FReferals;
  288. {:When you call @link(Extended) operation, then here is result Name returned
  289. by server.}
  290. property ExtName: AnsiString read FExtName;
  291. {:When you call @link(Extended) operation, then here is result Value returned
  292. by server.}
  293. property ExtValue: AnsiString read FExtValue;
  294. {:TCP socket used by all LDAP operations.}
  295. property Sock: TTCPBlockSocket read FSock;
  296. end;
  297. {:Dump result of LDAP SEARCH into human readable form. Good for debugging.}
  298. function LDAPResultDump(const Value: TLDAPResultList): AnsiString;
  299. implementation
  300. {==============================================================================}
  301. function TLDAPAttribute.Add(const S: string): Integer;
  302. begin
  303. Result := inherited Add('');
  304. Put(Result,S);
  305. end;
  306. function TLDAPAttribute.Get(Index: integer): string;
  307. begin
  308. Result := inherited Get(Index);
  309. if FIsbinary then
  310. Result := DecodeBase64(Result);
  311. end;
  312. procedure TLDAPAttribute.Put(Index: integer; const Value: string);
  313. var
  314. s: AnsiString;
  315. begin
  316. s := Value;
  317. if FIsbinary then
  318. s := EncodeBase64(Value)
  319. else
  320. s :=UnquoteStr(s, '"');
  321. inherited Put(Index, s);
  322. end;
  323. procedure TLDAPAttribute.SetAttributeName(Value: AnsiString);
  324. begin
  325. FAttributeName := Value;
  326. FIsBinary := Pos(';binary', Lowercase(value)) > 0;
  327. end;
  328. {==============================================================================}
  329. constructor TLDAPAttributeList.Create;
  330. begin
  331. inherited Create;
  332. FAttributeList := TList.Create;
  333. end;
  334. destructor TLDAPAttributeList.Destroy;
  335. begin
  336. Clear;
  337. FAttributeList.Free;
  338. inherited Destroy;
  339. end;
  340. procedure TLDAPAttributeList.Clear;
  341. var
  342. n: integer;
  343. x: TLDAPAttribute;
  344. begin
  345. for n := Count - 1 downto 0 do
  346. begin
  347. x := GetAttribute(n);
  348. if Assigned(x) then
  349. x.Free;
  350. end;
  351. FAttributeList.Clear;
  352. end;
  353. function TLDAPAttributeList.Count: integer;
  354. begin
  355. Result := FAttributeList.Count;
  356. end;
  357. function TLDAPAttributeList.Get(AttributeName: AnsiString): string;
  358. var
  359. x: TLDAPAttribute;
  360. begin
  361. Result := '';
  362. x := self.Find(AttributeName);
  363. if x <> nil then
  364. if x.Count > 0 then
  365. Result := x[0];
  366. end;
  367. function TLDAPAttributeList.GetAttribute(Index: integer): TLDAPAttribute;
  368. begin
  369. Result := nil;
  370. if Index < Count then
  371. Result := TLDAPAttribute(FAttributeList[Index]);
  372. end;
  373. function TLDAPAttributeList.Add: TLDAPAttribute;
  374. begin
  375. Result := TLDAPAttribute.Create;
  376. FAttributeList.Add(Result);
  377. end;
  378. procedure TLDAPAttributeList.Del(Index: integer);
  379. var
  380. x: TLDAPAttribute;
  381. begin
  382. x := GetAttribute(Index);
  383. if Assigned(x) then
  384. x.free;
  385. FAttributeList.Delete(Index);
  386. end;
  387. function TLDAPAttributeList.Find(AttributeName: AnsiString): TLDAPAttribute;
  388. var
  389. n: integer;
  390. x: TLDAPAttribute;
  391. begin
  392. Result := nil;
  393. AttributeName := lowercase(AttributeName);
  394. for n := 0 to Count - 1 do
  395. begin
  396. x := GetAttribute(n);
  397. if Assigned(x) then
  398. if lowercase(x.AttributeName) = Attributename then
  399. begin
  400. result := x;
  401. break;
  402. end;
  403. end;
  404. end;
  405. {==============================================================================}
  406. constructor TLDAPResult.Create;
  407. begin
  408. inherited Create;
  409. FAttributes := TLDAPAttributeList.Create;
  410. end;
  411. destructor TLDAPResult.Destroy;
  412. begin
  413. FAttributes.Free;
  414. inherited Destroy;
  415. end;
  416. {==============================================================================}
  417. constructor TLDAPResultList.Create;
  418. begin
  419. inherited Create;
  420. FResultList := TList.Create;
  421. end;
  422. destructor TLDAPResultList.Destroy;
  423. begin
  424. Clear;
  425. FResultList.Free;
  426. inherited Destroy;
  427. end;
  428. procedure TLDAPResultList.Clear;
  429. var
  430. n: integer;
  431. x: TLDAPResult;
  432. begin
  433. for n := Count - 1 downto 0 do
  434. begin
  435. x := GetResult(n);
  436. if Assigned(x) then
  437. x.Free;
  438. end;
  439. FResultList.Clear;
  440. end;
  441. function TLDAPResultList.Count: integer;
  442. begin
  443. Result := FResultList.Count;
  444. end;
  445. function TLDAPResultList.GetResult(Index: integer): TLDAPResult;
  446. begin
  447. Result := nil;
  448. if Index < Count then
  449. Result := TLDAPResult(FResultList[Index]);
  450. end;
  451. function TLDAPResultList.Add: TLDAPResult;
  452. begin
  453. Result := TLDAPResult.Create;
  454. FResultList.Add(Result);
  455. end;
  456. {==============================================================================}
  457. constructor TLDAPSend.Create;
  458. begin
  459. inherited Create;
  460. FReferals := TStringList.Create;
  461. FFullResult := '';
  462. FSock := TTCPBlockSocket.Create;
  463. FSock.Owner := self;
  464. FTimeout := 60000;
  465. FTargetPort := cLDAPProtocol;
  466. FAutoTLS := False;
  467. FFullSSL := False;
  468. FSeq := 0;
  469. FVersion := 3;
  470. FSearchScope := SS_WholeSubtree;
  471. FSearchAliases := SA_Always;
  472. FSearchSizeLimit := 0;
  473. FSearchTimeLimit := 0;
  474. FSearchPageSize := 0;
  475. FSearchCookie := '';
  476. FSearchResult := TLDAPResultList.Create;
  477. end;
  478. destructor TLDAPSend.Destroy;
  479. begin
  480. FSock.Free;
  481. FSearchResult.Free;
  482. FReferals.Free;
  483. inherited Destroy;
  484. end;
  485. function TLDAPSend.GetErrorString(Value: integer): AnsiString;
  486. begin
  487. case Value of
  488. 0:
  489. Result := 'Success';
  490. 1:
  491. Result := 'Operations error';
  492. 2:
  493. Result := 'Protocol error';
  494. 3:
  495. Result := 'Time limit Exceeded';
  496. 4:
  497. Result := 'Size limit Exceeded';
  498. 5:
  499. Result := 'Compare FALSE';
  500. 6:
  501. Result := 'Compare TRUE';
  502. 7:
  503. Result := 'Auth method not supported';
  504. 8:
  505. Result := 'Strong auth required';
  506. 9:
  507. Result := '-- reserved --';
  508. 10:
  509. Result := 'Referal';
  510. 11:
  511. Result := 'Admin limit exceeded';
  512. 12:
  513. Result := 'Unavailable critical extension';
  514. 13:
  515. Result := 'Confidentality required';
  516. 14:
  517. Result := 'Sasl bind in progress';
  518. 16:
  519. Result := 'No such attribute';
  520. 17:
  521. Result := 'Undefined attribute type';
  522. 18:
  523. Result := 'Inappropriate matching';
  524. 19:
  525. Result := 'Constraint violation';
  526. 20:
  527. Result := 'Attribute or value exists';
  528. 21:
  529. Result := 'Invalid attribute syntax';
  530. 32:
  531. Result := 'No such object';
  532. 33:
  533. Result := 'Alias problem';
  534. 34:
  535. Result := 'Invalid DN syntax';
  536. 36:
  537. Result := 'Alias dereferencing problem';
  538. 48:
  539. Result := 'Inappropriate authentication';
  540. 49:
  541. Result := 'Invalid credentials';
  542. 50:
  543. Result := 'Insufficient access rights';
  544. 51:
  545. Result := 'Busy';
  546. 52:
  547. Result := 'Unavailable';
  548. 53:
  549. Result := 'Unwilling to perform';
  550. 54:
  551. Result := 'Loop detect';
  552. 64:
  553. Result := 'Naming violation';
  554. 65:
  555. Result := 'Object class violation';
  556. 66:
  557. Result := 'Not allowed on non leaf';
  558. 67:
  559. Result := 'Not allowed on RDN';
  560. 68:
  561. Result := 'Entry already exists';
  562. 69:
  563. Result := 'Object class mods prohibited';
  564. 71:
  565. Result := 'Affects multiple DSAs';
  566. 80:
  567. Result := 'Other';
  568. else
  569. Result := '--unknown--';
  570. end;
  571. end;
  572. function TLDAPSend.Connect: Boolean;
  573. begin
  574. // Do not call this function! It is calling by LOGIN method!
  575. FSock.CloseSocket;
  576. FSock.LineBuffer := '';
  577. FSeq := 0;
  578. FSock.Bind(FIPInterface, cAnyPort);
  579. if FSock.LastError = 0 then
  580. FSock.Connect(FTargetHost, FTargetPort);
  581. if FSock.LastError = 0 then
  582. if FFullSSL then
  583. FSock.SSLDoConnect;
  584. Result := FSock.LastError = 0;
  585. end;
  586. function TLDAPSend.BuildPacket(const Value: AnsiString): AnsiString;
  587. begin
  588. Inc(FSeq);
  589. Result := ASNObject(ASNObject(ASNEncInt(FSeq), ASN1_INT) + Value, ASN1_SEQ);
  590. end;
  591. function TLDAPSend.ReceiveResponse: AnsiString;
  592. var
  593. x: Byte;
  594. i,j: integer;
  595. begin
  596. Result := '';
  597. FFullResult := '';
  598. x := FSock.RecvByte(FTimeout);
  599. if x <> ASN1_SEQ then
  600. Exit;
  601. Result := AnsiChar(x);
  602. x := FSock.RecvByte(FTimeout);
  603. Result := Result + AnsiChar(x);
  604. if x < $80 then
  605. i := 0
  606. else
  607. i := x and $7F;
  608. if i > 0 then
  609. Result := Result + FSock.RecvBufferStr(i, Ftimeout);
  610. if FSock.LastError <> 0 then
  611. begin
  612. Result := '';
  613. Exit;
  614. end;
  615. //get length of LDAP packet
  616. j := 2;
  617. i := ASNDecLen(j, Result);
  618. //retreive rest of LDAP packet
  619. if i > 0 then
  620. Result := Result + FSock.RecvBufferStr(i, Ftimeout);
  621. if FSock.LastError <> 0 then
  622. begin
  623. Result := '';
  624. Exit;
  625. end;
  626. FFullResult := Result;
  627. end;
  628. function TLDAPSend.DecodeResponse(const Value: AnsiString): AnsiString;
  629. var
  630. i, x: integer;
  631. Svt: Integer;
  632. s, t: AnsiString;
  633. begin
  634. Result := '';
  635. FResultCode := -1;
  636. FResultstring := '';
  637. FResponseCode := -1;
  638. FResponseDN := '';
  639. FReferals.Clear;
  640. i := 1;
  641. ASNItem(i, Value, Svt);
  642. x := StrToIntDef(ASNItem(i, Value, Svt), 0);
  643. if (svt <> ASN1_INT) or (x <> FSeq) then
  644. Exit;
  645. s := ASNItem(i, Value, Svt);
  646. FResponseCode := svt;
  647. if FResponseCode in [LDAP_ASN1_BIND_RESPONSE, LDAP_ASN1_SEARCH_DONE,
  648. LDAP_ASN1_MODIFY_RESPONSE, LDAP_ASN1_ADD_RESPONSE, LDAP_ASN1_DEL_RESPONSE,
  649. LDAP_ASN1_MODIFYDN_RESPONSE, LDAP_ASN1_COMPARE_RESPONSE,
  650. LDAP_ASN1_EXT_RESPONSE] then
  651. begin
  652. FResultCode := StrToIntDef(ASNItem(i, Value, Svt), -1);
  653. FResponseDN := ASNItem(i, Value, Svt);
  654. FResultString := ASNItem(i, Value, Svt);
  655. if FResultString = '' then
  656. FResultString := GetErrorString(FResultCode);
  657. if FResultCode = 10 then
  658. begin
  659. s := ASNItem(i, Value, Svt);
  660. if svt = $A3 then
  661. begin
  662. x := 1;
  663. while x < Length(s) do
  664. begin
  665. t := ASNItem(x, s, Svt);
  666. FReferals.Add(t);
  667. end;
  668. end;
  669. end;
  670. end;
  671. Result := Copy(Value, i, Length(Value) - i + 1);
  672. end;
  673. function TLDAPSend.LdapSasl(Value: AnsiString): AnsiString;
  674. var
  675. nonce, cnonce, nc, realm, qop, uri, response: AnsiString;
  676. s: AnsiString;
  677. a1, a2: AnsiString;
  678. l: TStringList;
  679. n: integer;
  680. begin
  681. l := TStringList.Create;
  682. try
  683. nonce := '';
  684. realm := '';
  685. l.CommaText := Value;
  686. n := IndexByBegin('nonce=', l);
  687. if n >= 0 then
  688. nonce := UnQuoteStr(Trim(SeparateRight(l[n], 'nonce=')), '"');
  689. n := IndexByBegin('realm=', l);
  690. if n >= 0 then
  691. realm := UnQuoteStr(Trim(SeparateRight(l[n], 'realm=')), '"');
  692. cnonce := IntToHex(GetTick, 8);
  693. nc := '00000001';
  694. qop := 'auth';
  695. uri := 'ldap/' + FSock.ResolveIpToName(FSock.GetRemoteSinIP);
  696. a1 := md5(FUsername + ':' + realm + ':' + FPassword)
  697. + ':' + nonce + ':' + cnonce;
  698. a2 := 'AUTHENTICATE:' + uri;
  699. s := strtohex(md5(a1))+':' + nonce + ':' + nc + ':' + cnonce + ':'
  700. + qop +':'+strtohex(md5(a2));
  701. response := strtohex(md5(s));
  702. Result := 'username="' + Fusername + '",realm="' + realm + '",nonce="';
  703. Result := Result + nonce + '",cnonce="' + cnonce + '",nc=' + nc + ',qop=';
  704. Result := Result + qop + ',digest-uri="' + uri + '",response=' + response;
  705. finally
  706. l.Free;
  707. end;
  708. end;
  709. function TLDAPSend.TranslateFilter(Value: AnsiString): AnsiString;
  710. var
  711. x: integer;
  712. s, t, l: AnsiString;
  713. r: string;
  714. c: Ansichar;
  715. attr, rule: AnsiString;
  716. dn: Boolean;
  717. begin
  718. Result := '';
  719. if Value = '' then
  720. Exit;
  721. s := Value;
  722. if Value[1] = '(' then
  723. begin
  724. x := RPos(')', Value);
  725. s := Copy(Value, 2, x - 2);
  726. end;
  727. if s = '' then
  728. Exit;
  729. case s[1] of
  730. '!':
  731. // NOT rule (recursive call)
  732. begin
  733. Result := ASNOBject(TranslateFilter(GetBetween('(', ')', s)), $A2);
  734. end;
  735. '&':
  736. // AND rule (recursive call)
  737. begin
  738. repeat
  739. t := GetBetween('(', ')', s);
  740. s := Trim(SeparateRight(s, t));
  741. if s <> '' then
  742. if s[1] = ')' then
  743. {$IFDEF CIL}Borland.Delphi.{$ENDIF}System.Delete(s, 1, 1);
  744. Result := Result + TranslateFilter(t);
  745. until s = '';
  746. Result := ASNOBject(Result, $A0);
  747. end;
  748. '|':
  749. // OR rule (recursive call)
  750. begin
  751. repeat
  752. t := GetBetween('(', ')', s);
  753. s := Trim(SeparateRight(s, t));
  754. if s <> '' then
  755. if s[1] = ')' then
  756. {$IFDEF CIL}Borland.Delphi.{$ENDIF}System.Delete(s, 1, 1);
  757. Result := Result + TranslateFilter(t);
  758. until s = '';
  759. Result := ASNOBject(Result, $A1);
  760. end;
  761. else
  762. begin
  763. l := Trim(SeparateLeft(s, '='));
  764. r := Trim(SeparateRight(s, '='));
  765. if l <> '' then
  766. begin
  767. c := l[Length(l)];
  768. case c of
  769. ':':
  770. // Extensible match
  771. begin
  772. {$IFDEF CIL}Borland.Delphi.{$ENDIF}System.Delete(l, Length(l), 1);
  773. dn := False;
  774. attr := '';
  775. rule := '';
  776. if Pos(':dn', l) > 0 then
  777. begin
  778. dn := True;
  779. l := ReplaceString(l, ':dn', '');
  780. end;
  781. attr := Trim(SeparateLeft(l, ':'));
  782. rule := Trim(SeparateRight(l, ':'));
  783. if rule = l then
  784. rule := '';
  785. if rule <> '' then
  786. Result := ASNObject(rule, $81);
  787. if attr <> '' then
  788. Result := Result + ASNObject(attr, $82);
  789. Result := Result + ASNObject(DecodeTriplet(r, '\'), $83);
  790. if dn then
  791. Result := Result + ASNObject(AsnEncInt($ff), $84)
  792. else
  793. Result := Result + ASNObject(AsnEncInt(0), $84);
  794. Result := ASNOBject(Result, $a9);
  795. end;
  796. '~':
  797. // Approx match
  798. begin
  799. {$IFDEF CIL}Borland.Delphi.{$ENDIF}System.Delete(l, Length(l), 1);
  800. Result := ASNOBject(l, ASN1_OCTSTR)
  801. + ASNOBject(DecodeTriplet(r, '\'), ASN1_OCTSTR);
  802. Result := ASNOBject(Result, $a8);
  803. end;
  804. '>':
  805. // Greater or equal match
  806. begin
  807. {$IFDEF CIL}Borland.Delphi.{$ENDIF}System.Delete(l, Length(l), 1);
  808. Result := ASNOBject(l, ASN1_OCTSTR)
  809. + ASNOBject(DecodeTriplet(r, '\'), ASN1_OCTSTR);
  810. Result := ASNOBject(Result, $a5);
  811. end;
  812. '<':
  813. // Less or equal match
  814. begin
  815. {$IFDEF CIL}Borland.Delphi.{$ENDIF}System.Delete(l, Length(l), 1);
  816. Result := ASNOBject(l, ASN1_OCTSTR)
  817. + ASNOBject(DecodeTriplet(r, '\'), ASN1_OCTSTR);
  818. Result := ASNOBject(Result, $a6);
  819. end;
  820. else
  821. // present
  822. if r = '*' then
  823. Result := ASNOBject(l, $87)
  824. else
  825. if Pos('*', r) > 0 then
  826. // substrings
  827. begin
  828. s := Fetch(r, '*');
  829. if s <> '' then
  830. Result := ASNOBject(DecodeTriplet(s, '\'), $80);
  831. while r <> '' do
  832. begin
  833. if Pos('*', r) <= 0 then
  834. break;
  835. s := Fetch(r, '*');
  836. Result := Result + ASNOBject(DecodeTriplet(s, '\'), $81);
  837. end;
  838. if r <> '' then
  839. Result := Result + ASNOBject(DecodeTriplet(r, '\'), $82);
  840. Result := ASNOBject(l, ASN1_OCTSTR)
  841. + ASNOBject(Result, ASN1_SEQ);
  842. Result := ASNOBject(Result, $a4);
  843. end
  844. else
  845. begin
  846. // Equality match
  847. Result := ASNOBject(l, ASN1_OCTSTR)
  848. + ASNOBject(DecodeTriplet(r, '\'), ASN1_OCTSTR);
  849. Result := ASNOBject(Result, $a3);
  850. end;
  851. end;
  852. end;
  853. end;
  854. end;
  855. end;
  856. function TLDAPSend.Login: Boolean;
  857. begin
  858. Result := False;
  859. if not Connect then
  860. Exit;
  861. Result := True;
  862. if FAutoTLS then
  863. Result := StartTLS;
  864. end;
  865. function TLDAPSend.Bind: Boolean;
  866. var
  867. s: AnsiString;
  868. begin
  869. s := ASNObject(ASNEncInt(FVersion), ASN1_INT)
  870. + ASNObject(FUsername, ASN1_OCTSTR)
  871. + ASNObject(FPassword, $80);
  872. s := ASNObject(s, LDAP_ASN1_BIND_REQUEST);
  873. Fsock.SendString(BuildPacket(s));
  874. s := ReceiveResponse;
  875. DecodeResponse(s);
  876. Result := FResultCode = 0;
  877. end;
  878. function TLDAPSend.BindSasl: Boolean;
  879. var
  880. s, t: AnsiString;
  881. x, xt: integer;
  882. digreq: AnsiString;
  883. begin
  884. Result := False;
  885. if FPassword = '' then
  886. Result := Bind
  887. else
  888. begin
  889. digreq := ASNObject(ASNEncInt(FVersion), ASN1_INT)
  890. + ASNObject('', ASN1_OCTSTR)
  891. + ASNObject(ASNObject('DIGEST-MD5', ASN1_OCTSTR), $A3);
  892. digreq := ASNObject(digreq, LDAP_ASN1_BIND_REQUEST);
  893. Fsock.SendString(BuildPacket(digreq));
  894. s := ReceiveResponse;
  895. t := DecodeResponse(s);
  896. if FResultCode = 14 then
  897. begin
  898. s := t;
  899. x := 1;
  900. t := ASNItem(x, s, xt);
  901. s := ASNObject(ASNEncInt(FVersion), ASN1_INT)
  902. + ASNObject('', ASN1_OCTSTR)
  903. + ASNObject(ASNObject('DIGEST-MD5', ASN1_OCTSTR)
  904. + ASNObject(LdapSasl(t), ASN1_OCTSTR), $A3);
  905. s := ASNObject(s, LDAP_ASN1_BIND_REQUEST);
  906. Fsock.SendString(BuildPacket(s));
  907. s := ReceiveResponse;
  908. DecodeResponse(s);
  909. if FResultCode = 14 then
  910. begin
  911. Fsock.SendString(BuildPacket(digreq));
  912. s := ReceiveResponse;
  913. DecodeResponse(s);
  914. end;
  915. Result := FResultCode = 0;
  916. end;
  917. end;
  918. end;
  919. function TLDAPSend.Logout: Boolean;
  920. begin
  921. Fsock.SendString(BuildPacket(ASNObject('', LDAP_ASN1_UNBIND_REQUEST)));
  922. FSock.CloseSocket;
  923. Result := True;
  924. end;
  925. function TLDAPSend.Modify(obj: AnsiString; Op: TLDAPModifyOp; const Value: TLDAPAttribute): Boolean;
  926. var
  927. s: AnsiString;
  928. n: integer;
  929. begin
  930. s := '';
  931. for n := 0 to Value.Count -1 do
  932. s := s + ASNObject(Value[n], ASN1_OCTSTR);
  933. s := ASNObject(Value.AttributeName, ASN1_OCTSTR) + ASNObject(s, ASN1_SETOF);
  934. s := ASNObject(ASNEncInt(Ord(Op)), ASN1_ENUM) + ASNObject(s, ASN1_SEQ);
  935. s := ASNObject(s, ASN1_SEQ);
  936. s := ASNObject(obj, ASN1_OCTSTR) + ASNObject(s, ASN1_SEQ);
  937. s := ASNObject(s, LDAP_ASN1_MODIFY_REQUEST);
  938. Fsock.SendString(BuildPacket(s));
  939. s := ReceiveResponse;
  940. DecodeResponse(s);
  941. Result := FResultCode = 0;
  942. end;
  943. function TLDAPSend.Add(obj: AnsiString; const Value: TLDAPAttributeList): Boolean;
  944. var
  945. s, t: AnsiString;
  946. n, m: integer;
  947. begin
  948. s := '';
  949. for n := 0 to Value.Count - 1 do
  950. begin
  951. t := '';
  952. for m := 0 to Value[n].Count - 1 do
  953. t := t + ASNObject(Value[n][m], ASN1_OCTSTR);
  954. t := ASNObject(Value[n].AttributeName, ASN1_OCTSTR)
  955. + ASNObject(t, ASN1_SETOF);
  956. s := s + ASNObject(t, ASN1_SEQ);
  957. end;
  958. s := ASNObject(obj, ASN1_OCTSTR) + ASNObject(s, ASN1_SEQ);
  959. s := ASNObject(s, LDAP_ASN1_ADD_REQUEST);
  960. Fsock.SendString(BuildPacket(s));
  961. s := ReceiveResponse;
  962. DecodeResponse(s);
  963. Result := FResultCode = 0;
  964. end;
  965. function TLDAPSend.Delete(obj: AnsiString): Boolean;
  966. var
  967. s: AnsiString;
  968. begin
  969. s := ASNObject(obj, LDAP_ASN1_DEL_REQUEST);
  970. Fsock.SendString(BuildPacket(s));
  971. s := ReceiveResponse;
  972. DecodeResponse(s);
  973. Result := FResultCode = 0;
  974. end;
  975. function TLDAPSend.ModifyDN(obj, newRDN, newSuperior: AnsiString; DeleteOldRDN: Boolean): Boolean;
  976. var
  977. s: AnsiString;
  978. begin
  979. s := ASNObject(obj, ASN1_OCTSTR) + ASNObject(newRDN, ASN1_OCTSTR);
  980. if DeleteOldRDN then
  981. s := s + ASNObject(ASNEncInt($ff), ASN1_BOOL)
  982. else
  983. s := s + ASNObject(ASNEncInt(0), ASN1_BOOL);
  984. if newSuperior <> '' then
  985. s := s + ASNObject(newSuperior, $80);
  986. s := ASNObject(s, LDAP_ASN1_MODIFYDN_REQUEST);
  987. Fsock.SendString(BuildPacket(s));
  988. s := ReceiveResponse;
  989. DecodeResponse(s);
  990. Result := FResultCode = 0;
  991. end;
  992. function TLDAPSend.Compare(obj, AttributeValue: AnsiString): Boolean;
  993. var
  994. s: AnsiString;
  995. begin
  996. s := ASNObject(Trim(SeparateLeft(AttributeValue, '=')), ASN1_OCTSTR)
  997. + ASNObject(Trim(SeparateRight(AttributeValue, '=')), ASN1_OCTSTR);
  998. s := ASNObject(obj, ASN1_OCTSTR) + ASNObject(s, ASN1_SEQ);
  999. s := ASNObject(s, LDAP_ASN1_COMPARE_REQUEST);
  1000. Fsock.SendString(BuildPacket(s));
  1001. s := ReceiveResponse;
  1002. DecodeResponse(s);
  1003. Result := FResultCode = 0;
  1004. end;
  1005. function TLDAPSend.Search(obj: AnsiString; TypesOnly: Boolean; Filter: AnsiString;
  1006. const Attributes: TStrings): Boolean;
  1007. var
  1008. s, t, u, c: AnsiString;
  1009. n, i, x: integer;
  1010. r: TLDAPResult;
  1011. a: TLDAPAttribute;
  1012. begin
  1013. FSearchResult.Clear;
  1014. FReferals.Clear;
  1015. s := ASNObject(obj, ASN1_OCTSTR);
  1016. s := s + ASNObject(ASNEncInt(Ord(FSearchScope)), ASN1_ENUM);
  1017. s := s + ASNObject(ASNEncInt(Ord(FSearchAliases)), ASN1_ENUM);
  1018. s := s + ASNObject(ASNEncInt(FSearchSizeLimit), ASN1_INT);
  1019. s := s + ASNObject(ASNEncInt(FSearchTimeLimit), ASN1_INT);
  1020. if TypesOnly then
  1021. s := s + ASNObject(ASNEncInt($ff), ASN1_BOOL)
  1022. else
  1023. s := s + ASNObject(ASNEncInt(0), ASN1_BOOL);
  1024. if Filter = '' then
  1025. Filter := '(objectclass=*)';
  1026. t := TranslateFilter(Filter);
  1027. if t = '' then
  1028. s := s + ASNObject('', ASN1_NULL)
  1029. else
  1030. s := s + t;
  1031. t := '';
  1032. for n := 0 to Attributes.Count - 1 do
  1033. t := t + ASNObject(Attributes[n], ASN1_OCTSTR);
  1034. s := s + ASNObject(t, ASN1_SEQ);
  1035. s := ASNObject(s, LDAP_ASN1_SEARCH_REQUEST);
  1036. if FSearchPageSize > 0 then
  1037. begin
  1038. c := ASNObject('1.2.840.113556.1.4.319', ASN1_OCTSTR); // controlType: pagedResultsControl
  1039. c := c + ASNObject(ASNEncInt(0), ASN1_BOOL); // criticality: FALSE
  1040. t := ASNObject(ASNEncInt(FSearchPageSize), ASN1_INT); // page size
  1041. t := t + ASNObject(FSearchCookie, ASN1_OCTSTR); // search cookie
  1042. t := ASNObject(t, ASN1_SEQ); // wrap with SEQUENCE
  1043. c := c + ASNObject(t, ASN1_OCTSTR); // add searchControlValue as OCTET STRING
  1044. c := ASNObject(c, ASN1_SEQ); // wrap with SEQUENCE
  1045. s := s + ASNObject(c, LDAP_ASN1_CONTROLS); // append Controls to SearchRequest
  1046. end;
  1047. Fsock.SendString(BuildPacket(s));
  1048. repeat
  1049. s := ReceiveResponse;
  1050. t := DecodeResponse(s);
  1051. if FResponseCode = LDAP_ASN1_SEARCH_ENTRY then
  1052. begin
  1053. //dekoduj zaznam
  1054. r := FSearchResult.Add;
  1055. n := 1;
  1056. r.ObjectName := ASNItem(n, t, x);
  1057. ASNItem(n, t, x);
  1058. if x = ASN1_SEQ then
  1059. begin
  1060. while n < Length(t) do
  1061. begin
  1062. s := ASNItem(n, t, x);
  1063. if x = ASN1_SEQ then
  1064. begin
  1065. i := n + Length(s);
  1066. a := r.Attributes.Add;
  1067. u := ASNItem(n, t, x);
  1068. a.AttributeName := u;
  1069. ASNItem(n, t, x);
  1070. if x = ASN1_SETOF then
  1071. while n < i do
  1072. begin
  1073. u := ASNItem(n, t, x);
  1074. a.Add(u);
  1075. end;
  1076. end;
  1077. end;
  1078. end;
  1079. end;
  1080. if FResponseCode = LDAP_ASN1_SEARCH_REFERENCE then
  1081. begin
  1082. n := 1;
  1083. while n < Length(t) do
  1084. FReferals.Add(ASNItem(n, t, x));
  1085. end;
  1086. until FResponseCode = LDAP_ASN1_SEARCH_DONE;
  1087. n := 1;
  1088. ASNItem(n, t, x);
  1089. if x = LDAP_ASN1_CONTROLS then
  1090. begin
  1091. ASNItem(n, t, x);
  1092. if x = ASN1_SEQ then
  1093. begin
  1094. s := ASNItem(n, t, x);
  1095. if s = '1.2.840.113556.1.4.319' then
  1096. begin
  1097. s := ASNItem(n, t, x); // searchControlValue
  1098. n := 1;
  1099. ASNItem(n, s, x);
  1100. if x = ASN1_SEQ then
  1101. begin
  1102. ASNItem(n, s, x); // total number of result records, if known, otherwise 0
  1103. FSearchCookie := ASNItem(n, s, x); // active search cookie, empty when done
  1104. end;
  1105. end;
  1106. end;
  1107. end;
  1108. Result := FResultCode = 0;
  1109. end;
  1110. function TLDAPSend.Extended(const Name, Value: AnsiString): Boolean;
  1111. var
  1112. s, t: AnsiString;
  1113. x, xt: integer;
  1114. begin
  1115. s := ASNObject(Name, $80);
  1116. if Value <> '' then
  1117. s := s + ASNObject(Value, $81);
  1118. s := ASNObject(s, LDAP_ASN1_EXT_REQUEST);
  1119. Fsock.SendString(BuildPacket(s));
  1120. s := ReceiveResponse;
  1121. t := DecodeResponse(s);
  1122. Result := FResultCode = 0;
  1123. if Result then
  1124. begin
  1125. x := 1;
  1126. FExtName := ASNItem(x, t, xt);
  1127. FExtValue := ASNItem(x, t, xt);
  1128. end;
  1129. end;
  1130. function TLDAPSend.StartTLS: Boolean;
  1131. begin
  1132. Result := Extended('1.3.6.1.4.1.1466.20037', '');
  1133. if Result then
  1134. begin
  1135. Fsock.SSLDoConnect;
  1136. Result := FSock.LastError = 0;
  1137. end;
  1138. end;
  1139. {==============================================================================}
  1140. function LDAPResultDump(const Value: TLDAPResultList): AnsiString;
  1141. var
  1142. n, m, o: integer;
  1143. r: TLDAPResult;
  1144. a: TLDAPAttribute;
  1145. begin
  1146. Result := 'Results: ' + IntToStr(Value.Count) + CRLF +CRLF;
  1147. for n := 0 to Value.Count - 1 do
  1148. begin
  1149. Result := Result + 'Result: ' + IntToStr(n) + CRLF;
  1150. r := Value[n];
  1151. Result := Result + ' Object: ' + r.ObjectName + CRLF;
  1152. for m := 0 to r.Attributes.Count - 1 do
  1153. begin
  1154. a := r.Attributes[m];
  1155. Result := Result + ' Attribute: ' + a.AttributeName + CRLF;
  1156. for o := 0 to a.Count - 1 do
  1157. Result := Result + ' ' + a[o] + CRLF;
  1158. end;
  1159. end;
  1160. end;
  1161. end.