IdTelnetServer.pas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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: 10373: IdTelnetServer.pas
  11. {
  12. { Rev 1.0 2002.11.12 10:55:48 PM czhower
  13. }
  14. unit IdTelnetServer;
  15. interface
  16. uses
  17. Classes,
  18. IdAssignedNumbers, IdTCPServer,
  19. IdThreadMgr, IdThreadMgrDefault;
  20. const
  21. GLoginAttempts = 3;
  22. type
  23. // SG 16/02/2001: Moved the TTelnetData object from TIdPeerThread to custom TIdPeerThread descendant
  24. TTelnetData = class(TObject)
  25. public
  26. Username, Password: String;
  27. HUserToken: cardinal;
  28. end;
  29. // Custom Peer thread class
  30. TIdTelnetPeerThread = Class(TIdPeerThread)
  31. private
  32. FTelnetData: TTelnetData;
  33. public
  34. constructor Create(ACreateSuspended: Boolean = True); override;
  35. destructor Destroy; override;
  36. Property TelnetData: TTelnetData read FTelnetData;
  37. end; //class
  38. TIdTelnetNegotiateEvent = procedure(AThread: TIdPeerThread) of object;
  39. TAuthenticationEvent = procedure(AThread: TIdPeerThread;
  40. const AUsername, APassword: string; var AAuthenticated: Boolean) of object;
  41. TIdTelnetServer = class(TIdTCPServer)
  42. protected
  43. FLoginAttempts: Integer;
  44. FOnAuthentication: TAuthenticationEvent;
  45. FLoginMessage: String;
  46. FOnNegotiate: TIdTelnetNegotiateEvent;
  47. public
  48. constructor Create(AOwner: TComponent); override;
  49. function DoAuthenticate(AThread: TIdPeerThread; const AUsername, APassword: string)
  50. : boolean; virtual;
  51. procedure DoNegotiate(AThread: TIdPeerThread); virtual;
  52. procedure DoConnect(AThread: TIdPeerThread); override;
  53. published
  54. property DefaultPort default IdPORT_TELNET;
  55. property LoginAttempts: Integer read FLoginAttempts write FLoginAttempts Default GLoginAttempts;
  56. property LoginMessage: String read FLoginMessage write FLoginMessage;
  57. property OnAuthentication: TAuthenticationEvent read FOnAuthentication write FOnAuthentication;
  58. property OnNegotiate: TIdTelnetNegotiateEvent read FOnNegotiate write FOnNegotiate;
  59. end;
  60. implementation
  61. uses
  62. IdException, IdGlobal, IdResourceStrings, SysUtils;
  63. constructor TIdTelnetServer.Create(AOwner: TComponent);
  64. begin
  65. inherited;
  66. LoginAttempts := GLoginAttempts;
  67. LoginMessage := RSTELNETSRVWelcomeString;
  68. DefaultPort := IdPORT_TELNET;
  69. ThreadClass := TIdTelnetPeerThread;
  70. end;
  71. function TIdTelnetServer.DoAuthenticate;
  72. begin
  73. if not Assigned(OnAuthentication) then begin
  74. raise EIdException.Create(RSTELNETSRVNoAuthHandler);
  75. end;
  76. Result := False;
  77. OnAuthentication(AThread, AUsername, APassword, result);
  78. end;
  79. procedure TIdTelnetServer.DoConnect(AThread: TIdPeerThread);
  80. Var
  81. Data: TTelnetData;
  82. i: integer;
  83. begin
  84. try
  85. inherited;
  86. Data := (AThread as TIdTelnetPeerThread).TelnetData;
  87. // do protocol negotiation first
  88. DoNegotiate(AThread);
  89. // Welcome the user
  90. if length(LoginMessage) > 0 then
  91. begin
  92. AThread.Connection.WriteLn(LoginMessage);
  93. AThread.Connection.WriteLn(''); {Do not Localize}
  94. end;
  95. // Only prompt for creditentials if there is an authentication handler
  96. if assigned(OnAuthentication) then
  97. begin
  98. // ask for username/password.
  99. for i := 1 to LoginAttempts do
  100. begin
  101. // UserName
  102. AThread.Connection.Write(RSTELNETSRVUsernamePrompt);
  103. Data.Username := AThread.Connection.InputLn;
  104. // Password
  105. AThread.Connection.Write(RSTELNETSRVPasswordPrompt);
  106. Data.Password := AThread.Connection.InputLn('*'); {Do not Localize}
  107. AThread.Connection.WriteLn;
  108. // Check authentication
  109. if DoAuthenticate(AThread, Data.Username, Data.Password) then begin
  110. Break; // exit the loop
  111. end else begin
  112. AThread.Connection.WriteLn(RSTELNETSRVInvalidLogin); // translate
  113. if i = FLoginAttempts then begin
  114. raise EIdException.Create(RSTELNETSRVMaxloginAttempt); // translate
  115. end;
  116. end;
  117. end;
  118. end;
  119. except
  120. on E: Exception do begin
  121. AThread.Connection.WriteLn(E.Message);
  122. AThread.Connection.Disconnect;
  123. end;
  124. end;
  125. end;
  126. procedure TIdTelnetServer.DoNegotiate(AThread: TIdPeerThread);
  127. begin
  128. if assigned(FOnNegotiate) then begin
  129. FOnNegotiate(AThread);
  130. end;
  131. end;
  132. { TIdTelnetPeerThread }
  133. constructor TIdTelnetPeerThread.Create(ACreateSuspended: Boolean);
  134. begin
  135. Inherited;
  136. FTelnetData := TTelnetData.Create;
  137. end;
  138. destructor TIdTelnetPeerThread.Destroy;
  139. begin
  140. FreeAndNil(FTelnetData);
  141. inherited;
  142. end;
  143. end.