IdTelnetServer.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. public
  88. constructor Create(AOwner: TComponent); override;
  89. function DoAuthenticate(AContext: TIdContext; const AUsername, APassword: string)
  90. : boolean; virtual;
  91. procedure DoNegotiate(AContext: TIdContext); virtual;
  92. published
  93. property DefaultPort default IdPORT_TELNET;
  94. property LoginAttempts: Integer read FLoginAttempts write FLoginAttempts Default GLoginAttempts;
  95. property LoginMessage: String read FLoginMessage write FLoginMessage;
  96. property OnAuthentication: TAuthenticationEvent read FOnAuthentication write FOnAuthentication;
  97. property OnNegotiate: TIdTelnetNegotiateEvent read FOnNegotiate write FOnNegotiate;
  98. property OnExecute;
  99. end;
  100. implementation
  101. uses
  102. IdException, IdResourceStringsProtocols, SysUtils;
  103. constructor TIdTelnetServer.Create(AOwner: TComponent);
  104. begin
  105. inherited Create(AOwner);
  106. LoginAttempts := GLoginAttempts;
  107. LoginMessage := RSTELNETSRVWelcomeString;
  108. DefaultPort := IdPORT_TELNET;
  109. FContextClass := TIdTelnetServerContext;
  110. end;
  111. function TIdTelnetServer.DoAuthenticate;
  112. begin
  113. if not Assigned(OnAuthentication) then begin
  114. raise EIdException.Create(RSTELNETSRVNoAuthHandler); // TODO: create a new Exception class for this
  115. end;
  116. Result := False;
  117. OnAuthentication(AContext, AUsername, APassword, result);
  118. end;
  119. procedure TIdTelnetServer.DoConnect(AContext: TIdContext);
  120. Var
  121. Data: TTelnetData;
  122. i: integer;
  123. begin
  124. try
  125. inherited;
  126. Data := (AContext as TIdTelnetServerContext).TelnetData;
  127. // do protocol negotiation first
  128. DoNegotiate(AContext);
  129. // Welcome the user
  130. if LoginMessage <> '' then
  131. begin
  132. AContext.Connection.IOHandler.WriteLn(LoginMessage);
  133. AContext.Connection.IOHandler.WriteLn; {Do not Localize}
  134. end;
  135. // Only prompt for creditentials if there is an authentication handler
  136. if Assigned(OnAuthentication) then
  137. begin
  138. // ask for username/password.
  139. for i := 1 to LoginAttempts do
  140. begin
  141. // UserName
  142. AContext.Connection.IOHandler.Write(RSTELNETSRVUsernamePrompt);
  143. Data.Username := AContext.Connection.IOHandler.InputLn;
  144. // Password
  145. AContext.Connection.IOHandler.Write(RSTELNETSRVPasswordPrompt);
  146. Data.Password := AContext.Connection.IOHandler.InputLn('*'); {Do not Localize}
  147. AContext.Connection.IOHandler.WriteLn;
  148. // Check authentication
  149. if DoAuthenticate(AContext, Data.Username, Data.Password) then begin
  150. Break; // exit the loop
  151. end;
  152. AContext.Connection.IOHandler.WriteLn(RSTELNETSRVInvalidLogin);
  153. if i = FLoginAttempts then begin
  154. raise EIdException.Create(RSTELNETSRVMaxloginAttempt); // TODO: create a new Exception class for this
  155. end;
  156. end;
  157. end;
  158. except
  159. on E: Exception do begin
  160. AContext.Connection.IOHandler.WriteLn(E.Message);
  161. AContext.Connection.Disconnect;
  162. end;
  163. end;
  164. end;
  165. procedure TIdTelnetServer.DoNegotiate(AContext: TIdContext);
  166. begin
  167. if Assigned(FOnNegotiate) then begin
  168. FOnNegotiate(AContext);
  169. end;
  170. end;
  171. { TIdTelnetServerContext }
  172. constructor TIdTelnetServerContext.Create(AConnection: TIdTCPConnection;
  173. AYarn: TIdYarn; AList: TIdContextThreadList = nil);
  174. begin
  175. inherited Create(AConnection, AYarn, AList);
  176. FTelnetData := TTelnetData.Create;
  177. end;
  178. destructor TIdTelnetServerContext.Destroy;
  179. begin
  180. FTelnetData.Free;
  181. inherited Destroy;
  182. end;
  183. end.