IdSASL.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.3 10/26/2004 10:55:32 PM JPMugaas
  18. Updated refs.
  19. Rev 1.2 2004.02.03 5:44:18 PM czhower
  20. Name changes
  21. Rev 1.1 1/21/2004 3:11:34 PM JPMugaas
  22. InitComponent
  23. Rev 1.0 11/13/2002 08:00:06 AM JPMugaas
  24. 2002 - 5-19 - J. Peter Mugaas
  25. started this class definition for Indy 10.
  26. 2002 - 08 - J.M. Berg
  27. reworked, restructured a bit, made work with Indy 9 (most changes
  28. are in other units though)
  29. }
  30. {
  31. SASL Base mechanism for Indy.
  32. See RFC 2222
  33. This class is not useful in and of itself. It is for deriving descendant classes
  34. for implementing reusable SASL authentication mechanism classes for components
  35. such as IdPOP3, IdSMTP, and IdIMAP4.
  36. But since they tie into the SASLList, its not restricted to message clients.
  37. Descendant classes will be responsible for implementing the SASL mechanism
  38. completely and holding any data required for authentication, unless descend
  39. from the UserPass mechanism and link to a UserPass provider.
  40. }
  41. {$BOOLEVAL OFF}
  42. unit IdSASL;
  43. interface
  44. {$i IdCompilerDefines.inc}
  45. uses
  46. Classes,
  47. IdGlobal,
  48. IdBaseComponent;
  49. type
  50. TIdSASLResult = (srSuccess, srFailure, srAborted);
  51. TIdSASLServiceName = string;
  52. TIdSASL = class(TIdBaseComponent)
  53. protected
  54. FSecurityLevel : UInt32;
  55. function GetSecurityLevel : UInt32;
  56. procedure InitComponent; override;
  57. public
  58. destructor Destroy; override;
  59. {
  60. The following 5 methods are called when SASL Authentication is
  61. used. The challenge etc. is already Base64 decoded, if the protocol
  62. uses Base64 encoding, the mechanism should only process the data
  63. according to the mechanism, not for any transmission. The same holds
  64. for return values.
  65. TryStartAuthenticate() is for handling Initial Client Responses,
  66. which can remove an unnecessary round-trip if both parties support it.
  67. }
  68. //SASL AProtocolName must be a name from "http://www.iana.org/assignments/gssapi-service-names"
  69. function TryStartAuthenticate(const AHost, AProtocolName : string; var VInitialResponse: string): Boolean; overload; virtual;
  70. function StartAuthenticate(const AChallenge, AHost, AProtocolName : string): string; overload; virtual;
  71. function ContinueAuthenticate(const ALastResponse, AHost, AProtocolName : string): string; overload; virtual;
  72. function TryStartAuthenticate(const AHost: string; const APort: TIdPort; const AProtocolName : string; var VInitialResponse: string): Boolean; overload; virtual;
  73. function StartAuthenticate(const AChallenge, AHost: string; const APort: TIdPort; const AProtocolName : string): string; overload; virtual;
  74. function ContinueAuthenticate(const ALastResponse, AHost: string; const APort: TIdPort; const AProtocolName : string): string; overload; virtual;
  75. { For cleaning up after Authentication }
  76. procedure FinishAuthenticate; virtual;
  77. // for checking if Authentication is ready to start.
  78. // useful with TIdSASLLogin so login is not performed if no username is specified.
  79. function IsReadyToStart: Boolean; virtual;
  80. {
  81. For determining if the SASL Mechanism is supported from a list of SASL Mechanism.
  82. (Those can be obtained with EHLO with SMTP.)
  83. }
  84. function IsAuthProtocolAvailable(AFeatStrings : TStrings) : Boolean; virtual;
  85. {
  86. Level of security offered by SASL mechanism
  87. 0 - no security, public - broadcast it on the even news, and post it to
  88. every newsgroup for good measure
  89. 100 - well, at least there's a lock. Of course, any locksmith or crook
  90. has a skeleton key
  91. 200 - well, maybe it would take a little fiddling but not much
  92. $FFFFFFFF - Best security. So secret that users are screened
  93. thouroughly, for example, the user has to account for
  94. every second of their life under a polygraph and their
  95. distant relatives are under 24 hour surveillance :-)
  96. This value is advisory only, and programmers are free if they decide
  97. to honour it or not. I suggest the mechanisms are tried in order,
  98. higher security level first.
  99. }
  100. property SecurityLevel : UInt32 read GetSecurityLevel;
  101. {
  102. Returns the service name of the descendant class,
  103. this is a string[20] in accordance with the SASL specification.
  104. }
  105. class function ServiceName: TIdSASLServiceName; virtual;
  106. end;
  107. var
  108. GlobalSASLList: TThreadList;
  109. // this is used at design time to get a list of all
  110. // SASL mechanism components that are available
  111. // because they add at runtime as well, it must be a threadlist
  112. implementation
  113. uses
  114. {$IFDEF VCL_XE3_OR_ABOVE}
  115. System.Types,
  116. {$ENDIF}
  117. SysUtils;
  118. { TIdSASL }
  119. procedure TIdSASL.InitComponent;
  120. begin
  121. inherited InitComponent;
  122. GlobalSASLList.Add(Self);
  123. end;
  124. destructor TIdSASL.Destroy;
  125. begin
  126. GlobalSASLList.Remove(Self);
  127. inherited Destroy;
  128. end;
  129. function TIdSASL.TryStartAuthenticate(const AHost, AProtocolName : string; var VInitialResponse: string): Boolean;
  130. begin
  131. Result := False;
  132. end;
  133. function TIdSASL.TryStartAuthenticate(const AHost: string; const APort: TIdPort; const AProtocolName : string; var VInitialResponse: string): Boolean;
  134. begin
  135. Result := TryStartAuthenticate(AHost, AProtocolName, VInitialResponse);
  136. end;
  137. function TIdSASL.StartAuthenticate(const AChallenge, AHost, AProtocolName : string): string;
  138. begin
  139. Result := '';
  140. end;
  141. function TIdSASL.StartAuthenticate(const AChallenge, AHost: string; const APort: TIdPort; const AProtocolName : string): string;
  142. begin
  143. Result := StartAuthenticate(AChallenge, AHost, AProtocolName);
  144. end;
  145. function TIdSASL.ContinueAuthenticate(const ALastResponse, AHost, AProtocolName : string): string;
  146. begin
  147. Result := '';
  148. end;
  149. function TIdSASL.ContinueAuthenticate(const ALastResponse, AHost: string; const APort: TIdPort; const AProtocolName : string): string;
  150. begin
  151. Result := ContinueAuthenticate(ALastResponse, AHost, AProtocolName);
  152. end;
  153. procedure TIdSASL.FinishAuthenticate;
  154. begin
  155. // do nothing, deliberately
  156. end;
  157. function TIdSASL.GetSecurityLevel: UInt32;
  158. begin
  159. Result := FSecurityLevel;
  160. end;
  161. function TIdSASL.IsAuthProtocolAvailable(AFeatStrings: TStrings): Boolean;
  162. begin
  163. Result := Assigned(AFeatStrings) and (AFeatStrings.IndexOf(String(ServiceName)) > -1);
  164. end;
  165. function TIdSASL.IsReadyToStart;
  166. begin
  167. Result := True;
  168. end;
  169. class function TIdSASL.ServiceName: TIdSASLServiceName;
  170. begin
  171. Result := ''; {do not localize}
  172. // this class should never be instantiated or added to the list
  173. // but BCB required class methods to not be abstract!!
  174. end;
  175. initialization
  176. GlobalSASLList := TThreadList.Create;
  177. finalization
  178. FreeAndNil(GlobalSASLList);
  179. end.