IdIdentServer.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.8 12/2/2004 4:23:54 PM JPMugaas
  18. Adjusted for changes in Core.
  19. Rev 1.7 2004.02.03 5:43:48 PM czhower
  20. Name changes
  21. Rev 1.6 1/21/2004 3:10:38 PM JPMugaas
  22. InitComponent
  23. Rev 1.5 3/27/2003 3:42:02 PM BGooijen
  24. Changed because some properties are moved to IOHandler
  25. Rev 1.4 2/24/2003 09:00:38 PM JPMugaas
  26. Rev 1.3 1/17/2003 07:10:32 PM JPMugaas
  27. Now compiles under new framework.
  28. Rev 1.2 1-1-2003 20:13:20 BGooijen
  29. Changed to support the new TIdContext class
  30. Rev 1.1 12/6/2002 04:35:16 PM JPMugaas
  31. Now compiles with new code.
  32. Rev 1.0 11/13/2002 07:54:44 AM JPMugaas
  33. 2001 - Feb 11 - J. Peter Mugaas
  34. Started this component.
  35. }
  36. unit IdIdentServer;
  37. {
  38. This is based on RFC 1413 - Identification Protocol
  39. Note that the default port is assigned to IdPORT_AUTH
  40. The reason for this is that the RFC specifies port 113 and the old protocol
  41. name was Authentication Server Protocol. This was renamed Ident to better
  42. reflect what it does.
  43. }
  44. interface
  45. {$i IdCompilerDefines.inc}
  46. uses
  47. IdAssignedNumbers, IdContext, IdCustomTCPServer, IdGlobal;
  48. const
  49. IdDefIdentQueryTimeOut = 60000; // 1 minute
  50. type
  51. TIdIdentQueryEvent = procedure (AContext:TIdContext; AServerPort, AClientPort : TIdPort) of object;
  52. TIdIdentErrorType = (ieInvalidPort, ieNoUser, ieHiddenUser, ieUnknownError);
  53. TIdIdentServer = class(TIdCustomTCPServer)
  54. protected
  55. FOnIdentQuery : TIdIdentQueryEvent;
  56. FQueryTimeOut : Integer;
  57. function DoExecute(AContext:TIdContext): boolean; override;
  58. public
  59. constructor Create(AOwner: TComponent); override;
  60. Procedure ReplyError(AContext:TIdContext; AServerPort, AClientPort : TIdPort; AErr : TIdIdentErrorType);
  61. Procedure ReplyIdent(AContext:TIdContext; AServerPort, AClientPort : TIdPort; AOS, AUserName : String; const ACharset : String = ''); {Do not Localize}
  62. Procedure ReplyOther(AContext:TIdContext; AServerPort, AClientPort : TIdPort; AOther : String);
  63. published
  64. property QueryTimeOut : Integer read FQueryTimeOut write FQueryTimeOut default IdDefIdentQueryTimeOut;
  65. Property OnIdentQuery : TIdIdentQueryEvent read FOnIdentQuery write FOnIdentQuery;
  66. Property DefaultPort default IdPORT_AUTH;
  67. end;
  68. implementation
  69. uses
  70. SysUtils;
  71. { TIdIdentServer }
  72. constructor TIdIdentServer.Create(AOwner: TComponent);
  73. begin
  74. inherited Create(AOwner);
  75. DefaultPort := IdPORT_AUTH;
  76. FQueryTimeOut := IdDefIdentQueryTimeOut;
  77. end;
  78. function TIdIdentServer.DoExecute(AContext:TIdContext): Boolean;
  79. var
  80. s : String;
  81. ServerPort, ClientPort : TIdPort;
  82. begin
  83. Result := True;
  84. s := AContext.Connection.IOHandler.ReadLn('', FQueryTimeOut); {Do not Localize}
  85. if not AContext.Connection.IOHandler.ReadLnTimedOut then begin
  86. ServerPort := IndyStrToInt(Fetch(s,',')); {Do not Localize}
  87. ClientPort := IndyStrToInt(s);
  88. if Assigned(FOnIdentQuery) then begin
  89. FOnIdentQuery(AContext, ServerPort, ClientPort);
  90. Exit;
  91. end;
  92. ReplyError(AContext, ServerPort, ClientPort, ieUnknownError);
  93. end;
  94. AContext.Connection.Disconnect;
  95. end;
  96. procedure TIdIdentServer.ReplyError(AContext:TIdContext; AServerPort,
  97. AClientPort: TIdPort; AErr : TIdIdentErrorType);
  98. var s : String;
  99. begin
  100. s := IntToStr(AServerPort)+', '+IntToStr(AClientPort) + ' : ERROR : '; {Do not Localize}
  101. case AErr of
  102. ieInvalidPort : s := s + 'INVALID-PORT'; {Do not Localize}
  103. ieNoUser : s := s + 'NO-USER'; {Do not Localize}
  104. ieHiddenUser : s := s + 'HIDDEN-USER'; {Do not Localize}
  105. ieUnknownError : s := s + 'UNKNOWN-ERROR'; {Do not Localize}
  106. end;
  107. AContext.Connection.IOHandler.WriteLn(s);
  108. end;
  109. procedure TIdIdentServer.ReplyIdent(AContext:TIdContext; AServerPort,
  110. AClientPort: TIdPort; AOS, AUserName: String; const ACharset: String);
  111. var s : String;
  112. begin
  113. s := IntToStr(AServerPort)+', '+IntToStr(AClientPort) + ' : USERID : '; {Do not Localize}
  114. s := s + AOS;
  115. if ACharset <> '' then begin
  116. s := s + ','+ACharset; {Do not Localize}
  117. end;
  118. s := s + ' : '+AUserName; {Do not Localize}
  119. AContext.Connection.IOHandler.WriteLn(s);
  120. end;
  121. procedure TIdIdentServer.ReplyOther(AContext:TIdContext; AServerPort,
  122. AClientPort: TIdPort; AOther: String);
  123. begin
  124. AContext.Connection.IOHandler.WriteLn(IntToStr(AServerPort)+', '+IntToStr(AClientPort) + ' : USERID : OTHER : '+AOther); {Do not Localize}
  125. end;
  126. end.