KeyboardInteractiveConnectionInfo.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using Renci.SshNet.Messages.Authentication;
  7. using Renci.SshNet.Messages;
  8. using Renci.SshNet.Common;
  9. namespace Renci.SshNet
  10. {
  11. /// <summary>
  12. /// Provides connection information when keyboard interactive authentication method is used
  13. /// </summary>
  14. public partial class KeyboardInteractiveConnectionInfo : ConnectionInfo, IDisposable
  15. {
  16. private EventWaitHandle _authenticationCompleted = new AutoResetEvent(false);
  17. private Exception _exception;
  18. private RequestMessage _requestMessage;
  19. /// <summary>
  20. /// Gets connection name
  21. /// </summary>
  22. public override string Name
  23. {
  24. get
  25. {
  26. return this._requestMessage.MethodName;
  27. }
  28. }
  29. /// <summary>
  30. /// Occurs when server prompts for more authentication information.
  31. /// </summary>
  32. public event EventHandler<AuthenticationPromptEventArgs> AuthenticationPrompt;
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  35. /// </summary>
  36. /// <param name="host">The host.</param>
  37. /// <param name="username">The username.</param>
  38. public KeyboardInteractiveConnectionInfo(string host, string username)
  39. : this(host, 22, username)
  40. {
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  44. /// </summary>
  45. /// <param name="host">Connection host.</param>
  46. /// <param name="port">Connection port.</param>
  47. /// <param name="username">Connection username.</param>
  48. public KeyboardInteractiveConnectionInfo(string host, int port, string username)
  49. : base(host, port, username)
  50. {
  51. this._requestMessage = new RequestMessageKeyboardInteractive(ServiceName.Connection, username);
  52. }
  53. /// <summary>
  54. /// Called when connection needs to be authenticated.
  55. /// </summary>
  56. protected override void OnAuthenticate()
  57. {
  58. this.Session.RegisterMessage("SSH_MSG_USERAUTH_INFO_REQUEST");
  59. this.Session.SendMessage(this._requestMessage);
  60. this.WaitHandle(this._authenticationCompleted);
  61. this.Session.UnRegisterMessage("SSH_MSG_USERAUTH_INFO_REQUEST");
  62. if (this._exception != null)
  63. {
  64. throw this._exception;
  65. }
  66. }
  67. /// <summary>
  68. /// Handles the UserAuthenticationSuccessMessageReceived event of the session.
  69. /// </summary>
  70. /// <param name="sender">The source of the event.</param>
  71. /// <param name="e">The event data.</param>
  72. protected override void Session_UserAuthenticationSuccessMessageReceived(object sender, MessageEventArgs<SuccessMessage> e)
  73. {
  74. base.Session_UserAuthenticationSuccessMessageReceived(sender, e);
  75. this._authenticationCompleted.Set();
  76. }
  77. /// <summary>
  78. /// Handles the UserAuthenticationFailureReceived event of the session.
  79. /// </summary>
  80. /// <param name="sender">The source of the event.</param>
  81. /// <param name="e">The event data.</param>
  82. protected override void Session_UserAuthenticationFailureReceived(object sender, MessageEventArgs<FailureMessage> e)
  83. {
  84. base.Session_UserAuthenticationFailureReceived(sender, e);
  85. this._authenticationCompleted.Set();
  86. }
  87. /// <summary>
  88. /// Handles the MessageReceived event of the session.
  89. /// </summary>
  90. /// <param name="sender">The source of the event.</param>
  91. /// <param name="e">The event data.</param>
  92. protected override void Session_MessageReceived(object sender, MessageEventArgs<Message> e)
  93. {
  94. var informationRequestMessage = e.Message as InformationRequestMessage;
  95. if (informationRequestMessage != null)
  96. {
  97. var eventArgs = new AuthenticationPromptEventArgs(this.Username, informationRequestMessage.Instruction, informationRequestMessage.Language, informationRequestMessage.Prompts);
  98. this.ExecuteThread(() =>
  99. {
  100. try
  101. {
  102. if (this.AuthenticationPrompt != null)
  103. {
  104. this.AuthenticationPrompt(this, eventArgs);
  105. }
  106. var informationResponse = new InformationResponseMessage();
  107. foreach (var response in from r in eventArgs.Prompts orderby r.Id ascending select r.Response)
  108. {
  109. informationResponse.Responses.Add(response);
  110. }
  111. // Send information response message
  112. this.SendMessage(informationResponse);
  113. }
  114. catch (Exception exp)
  115. {
  116. this._exception = exp;
  117. this._authenticationCompleted.Set();
  118. }
  119. });
  120. }
  121. }
  122. partial void ExecuteThread(Action action);
  123. #region IDisposable Members
  124. private bool isDisposed = false;
  125. /// <summary>
  126. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  127. /// </summary>
  128. public void Dispose()
  129. {
  130. Dispose(true);
  131. GC.SuppressFinalize(this);
  132. }
  133. /// <summary>
  134. /// Releases unmanaged and - optionally - managed resources
  135. /// </summary>
  136. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  137. protected virtual void Dispose(bool disposing)
  138. {
  139. // Check to see if Dispose has already been called.
  140. if (!this.isDisposed)
  141. {
  142. // If disposing equals true, dispose all managed
  143. // and unmanaged resources.
  144. if (disposing)
  145. {
  146. // Dispose managed resources.
  147. if (this._authenticationCompleted != null)
  148. {
  149. this._authenticationCompleted.Dispose();
  150. this._authenticationCompleted = null;
  151. }
  152. }
  153. // Note disposing has been done.
  154. isDisposed = true;
  155. }
  156. }
  157. /// <summary>
  158. /// Releases unmanaged resources and performs other cleanup operations before the
  159. /// <see cref="KeyboardInteractiveConnectionInfo"/> is reclaimed by garbage collection.
  160. /// </summary>
  161. ~KeyboardInteractiveConnectionInfo()
  162. {
  163. // Do not re-create Dispose clean-up code here.
  164. // Calling Dispose(false) is optimal in terms of
  165. // readability and maintainability.
  166. Dispose(false);
  167. }
  168. #endregion
  169. }
  170. }