IdTelnetServer.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.13 12/2/2004 4:24:00 PM JPMugaas
  18. Adjusted for changes in Core.
  19. Rev 1.12 2004.02.03 5:44:32 PM czhower
  20. Name changes
  21. Rev 1.11 2004.01.22 2:33:58 PM czhower
  22. Matched visibility of DoConnect
  23. Rev 1.10 1/21/2004 4:20:52 PM JPMugaas
  24. InitComponent
  25. Rev 1.9 2003.10.21 9:13:16 PM czhower
  26. Now compiles.
  27. Rev 1.8 9/19/2003 04:27:02 PM JPMugaas
  28. Removed IdFTPServer so Indy can compile with Kudzu's new changes.
  29. Rev 1.7 7/6/2003 7:55:36 PM BGooijen
  30. Removed unused units from the uses
  31. Rev 1.6 2/24/2003 10:32:50 PM JPMugaas
  32. Rev 1.5 1/17/2003 07:11:04 PM JPMugaas
  33. Now compiles under new framework.
  34. Rev 1.4 1/17/2003 04:05:40 PM JPMugaas
  35. Now compiles under new design.
  36. Rev 1.3 1/9/2003 06:09:42 AM JPMugaas
  37. Updated for IdContext API change.
  38. Rev 1.2 1/8/2003 05:53:58 PM JPMugaas
  39. Switched stuff to IdContext.
  40. Rev 1.1 12/7/2002 06:43:36 PM JPMugaas
  41. These should now compile except for Socks server. IPVersion has to be a
  42. property someplace for that.
  43. Rev 1.0 11/13/2002 08:02:56 AM JPMugaas
  44. }
  45. unit IdTelnetServer;
  46. interface
  47. {$i IdCompilerDefines.inc}
  48. uses
  49. Classes,
  50. IdGlobal,
  51. IdAssignedNumbers, IdContext,
  52. IdCustomTCPServer,
  53. IdTCPConnection, IdYarn;
  54. const
  55. GLoginAttempts = 3;
  56. type
  57. // SG 16/02/2001: Moved the TTelnetData object from TIdPeerThread to custom TIdPeerThread descendant
  58. TTelnetData = class(TObject)
  59. public
  60. Username, Password: String;
  61. HUserToken: UInt32;
  62. end;
  63. // Custom Peer thread class
  64. TIdTelnetServerContext = class(TIdServerContext)
  65. private
  66. FTelnetData: TTelnetData;
  67. public
  68. constructor Create(
  69. AConnection: TIdTCPConnection;
  70. AYarn: TIdYarn;
  71. AList: TIdContextThreadList = nil
  72. ); override;
  73. destructor Destroy; override;
  74. Property TelnetData: TTelnetData read FTelnetData;
  75. end; //class
  76. TIdTelnetNegotiateEvent = procedure(AContext: TIdContext) of object;
  77. TAuthenticationEvent = procedure(AContext: TIdContext;
  78. const AUsername, APassword: string; var AAuthenticated: Boolean) of object;
  79. TIdTelnetServer = class(TIdCustomTCPServer)
  80. protected
  81. FLoginAttempts: Integer;
  82. FOnAuthentication: TAuthenticationEvent;
  83. FLoginMessage: string;
  84. FOnNegotiate: TIdTelnetNegotiateEvent;
  85. //
  86. procedure DoConnect(AContext: TIdContext); override;
  87. procedure InitComponent; override;
  88. public
  89. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  90. constructor Create(AOwner: TComponent); reintroduce; overload;
  91. {$ENDIF}
  92. function DoAuthenticate(AContext: TIdContext; const AUsername, APassword: string)
  93. : boolean; virtual;
  94. procedure DoNegotiate(AContext: TIdContext); virtual;
  95. published
  96. property DefaultPort default IdPORT_TELNET;
  97. property LoginAttempts: Integer read FLoginAttempts write FLoginAttempts Default GLoginAttempts;
  98. property LoginMessage: String read FLoginMessage write FLoginMessage;
  99. property OnAuthentication: TAuthenticationEvent read FOnAuthentication write FOnAuthentication;
  100. property OnNegotiate: TIdTelnetNegotiateEvent read FOnNegotiate write FOnNegotiate;
  101. property OnExecute;
  102. end;
  103. implementation
  104. uses
  105. IdException, IdResourceStringsProtocols, SysUtils;
  106. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  107. constructor TIdTelnetServer.Create(AOwner: TComponent);
  108. begin
  109. inherited Create(AOwner);
  110. end;
  111. {$ENDIF}
  112. procedure TIdTelnetServer.InitComponent;
  113. begin
  114. inherited InitComponent;
  115. LoginAttempts := GLoginAttempts;
  116. LoginMessage := RSTELNETSRVWelcomeString;
  117. DefaultPort := IdPORT_TELNET;
  118. FContextClass := TIdTelnetServerContext;
  119. end;
  120. function TIdTelnetServer.DoAuthenticate;
  121. begin
  122. if not Assigned(OnAuthentication) then begin
  123. raise EIdException.Create(RSTELNETSRVNoAuthHandler); // TODO: create a new Exception class for this
  124. end;
  125. Result := False;
  126. OnAuthentication(AContext, AUsername, APassword, result);
  127. end;
  128. procedure TIdTelnetServer.DoConnect(AContext: TIdContext);
  129. Var
  130. Data: TTelnetData;
  131. i: integer;
  132. begin
  133. try
  134. inherited;
  135. Data := (AContext as TIdTelnetServerContext).TelnetData;
  136. // do protocol negotiation first
  137. DoNegotiate(AContext);
  138. // Welcome the user
  139. if Length(LoginMessage) > 0 then
  140. begin
  141. AContext.Connection.IOHandler.WriteLn(LoginMessage);
  142. AContext.Connection.IOHandler.WriteLn; {Do not Localize}
  143. end;
  144. // Only prompt for creditentials if there is an authentication handler
  145. if Assigned(OnAuthentication) then
  146. begin
  147. // ask for username/password.
  148. for i := 1 to LoginAttempts do
  149. begin
  150. // UserName
  151. AContext.Connection.IOHandler.Write(RSTELNETSRVUsernamePrompt);
  152. Data.Username := AContext.Connection.IOHandler.InputLn;
  153. // Password
  154. AContext.Connection.IOHandler.Write(RSTELNETSRVPasswordPrompt);
  155. Data.Password := AContext.Connection.IOHandler.InputLn('*'); {Do not Localize}
  156. AContext.Connection.IOHandler.WriteLn;
  157. // Check authentication
  158. if DoAuthenticate(AContext, Data.Username, Data.Password) then begin
  159. Break; // exit the loop
  160. end;
  161. AContext.Connection.IOHandler.WriteLn(RSTELNETSRVInvalidLogin);
  162. if i = FLoginAttempts then begin
  163. raise EIdException.Create(RSTELNETSRVMaxloginAttempt); // TODO: create a new Exception class for this
  164. end;
  165. end;
  166. end;
  167. except
  168. on E: Exception do begin
  169. AContext.Connection.IOHandler.WriteLn(E.Message);
  170. AContext.Connection.Disconnect;
  171. end;
  172. end;
  173. end;
  174. procedure TIdTelnetServer.DoNegotiate(AContext: TIdContext);
  175. begin
  176. if Assigned(FOnNegotiate) then begin
  177. FOnNegotiate(AContext);
  178. end;
  179. end;
  180. { TIdTelnetServerContext }
  181. constructor TIdTelnetServerContext.Create(AConnection: TIdTCPConnection;
  182. AYarn: TIdYarn; AList: TIdContextThreadList = nil);
  183. begin
  184. inherited Create(AConnection, AYarn, AList);
  185. FTelnetData := TTelnetData.Create;
  186. end;
  187. destructor TIdTelnetServerContext.Destroy;
  188. begin
  189. FreeAndNil(FTelnetData);
  190. inherited Destroy;
  191. end;
  192. end.