ldapsend.pas 37 KB

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