IdAuthenticationNTLM.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 10077: IdAuthenticationNTLM.pas
  11. {
  12. { Rev 1.1 01.2.2003 ã. 11:54:04 DBondzhev
  13. }
  14. {
  15. { Rev 1.0 2002.11.12 10:31:06 PM czhower
  16. }
  17. {
  18. Implementation of the NTLM authentication as specified in
  19. http://www.innovation.ch/java/ntlm.html with some fixes
  20. Author: Doychin Bondzhev ([email protected])
  21. Copyright: (c) Chad Z. Hower and The Winshoes Working Group.
  22. }
  23. unit IdAuthenticationNTLM;
  24. interface
  25. Uses
  26. Classes, SysUtils,
  27. IdAuthentication;
  28. Type
  29. TIdNTLMAuthentication = class(TIdAuthentication)
  30. protected
  31. FNTLMInfo: String;
  32. LDomain, LUser: String;
  33. function DoNext: TIdAuthWhatsNext; override;
  34. function GetSteps: Integer; override;
  35. procedure SetUserName(const Value: String); override;
  36. public
  37. constructor Create; override;
  38. function Authentication: String; override;
  39. function KeepAlive: Boolean; override;
  40. procedure Reset; override;
  41. end;
  42. implementation
  43. Uses
  44. IdGlobal,
  45. IdException,
  46. IdCoderMIME,
  47. IdSSLOpenSSLHeaders,
  48. IdNTLM;
  49. { TIdNTLMAuthentication }
  50. constructor TIdNTLMAuthentication.Create;
  51. begin
  52. inherited Create;
  53. // Load Open SSL Library
  54. if not IdSSLOpenSSLHeaders.Load then
  55. begin
  56. Unload;
  57. Abort;
  58. end;
  59. end;
  60. function TIdNTLMAuthentication.DoNext: TIdAuthWhatsNext;
  61. begin
  62. result := wnDoRequest;
  63. case FCurrentStep of
  64. 0:
  65. begin
  66. result := wnDoRequest;
  67. FCurrentStep := 1;
  68. end;
  69. 1:
  70. begin
  71. FCurrentStep := 2;
  72. if (Length(Username) > 0) {and (Length(Password) > 0)} then
  73. begin
  74. result := wnDoRequest;
  75. end
  76. else begin
  77. result := wnAskTheProgram;
  78. end;
  79. end;
  80. 3:
  81. begin
  82. FCurrentStep := 4;
  83. result := wnDoRequest;
  84. end;
  85. 4:
  86. begin
  87. Reset;
  88. result := wnFail;
  89. end;
  90. end;
  91. end;
  92. function TIdNTLMAuthentication.Authentication: String;
  93. Var
  94. S: String;
  95. Type2: type_2_message_header;
  96. LDomain: String;
  97. LHost: String;
  98. begin
  99. result := ''; {do not localize}
  100. case FCurrentStep of
  101. 1:
  102. begin
  103. LHost := IndyGetHostName;
  104. result := 'NTLM ' + BuildType1Message(LDomain, LHost); {do not localize}
  105. FNTLMInfo := ''; {Do not Localize}
  106. end;
  107. 2:
  108. begin
  109. if Length(FNTLMInfo) = 0 then
  110. begin
  111. FNTLMInfo := ReadAuthInfo('NTLM'); {do not localize}
  112. Fetch(FNTLMInfo);
  113. end;
  114. if Length(FNTLMInfo) = 0 then
  115. begin
  116. Reset;
  117. Abort;
  118. end;
  119. S := TIdDecoderMIME.DecodeString(FNTLMInfo);
  120. move(S[1], type2, sizeof(type2));
  121. Delete(S, 1, sizeof(type2));
  122. S := Type2.Nonce;
  123. S := BuildType3Message(LDomain, LHost, Username, Password, Type2.Nonce);
  124. result := 'NTLM ' + S; {do not localize}
  125. FCurrentStep := 3;
  126. Inc(FAuthRetries);
  127. end;
  128. end;
  129. end;
  130. procedure TIdNTLMAuthentication.Reset;
  131. begin
  132. inherited Reset;
  133. FCurrentStep := 1;
  134. end;
  135. function TIdNTLMAuthentication.KeepAlive: Boolean;
  136. begin
  137. result := true;
  138. end;
  139. function TIdNTLMAuthentication.GetSteps: Integer;
  140. begin
  141. result := 3;
  142. end;
  143. procedure TIdNTLMAuthentication.SetUserName(const Value: String);
  144. var
  145. i: integer;
  146. begin
  147. if Value <> Username then
  148. begin
  149. inherited;
  150. i := Pos('\', Username);
  151. if i > -1 then
  152. begin
  153. LDomain := Copy(Username, 1, i - 1);
  154. LUser := Copy(Username, i + 1, Length(UserName));
  155. end
  156. else
  157. begin
  158. LDomain := ' '; {do not localize}
  159. LUser := UserName;
  160. end;
  161. end;
  162. end;
  163. initialization
  164. RegisterAuthenticationMethod('NTLM', TIdNTLMAuthentication); {do not localize}
  165. end.