PasswordAuthenticationMethod.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using Renci.SshNet.Common;
  7. using Renci.SshNet.Messages.Authentication;
  8. using Renci.SshNet.Messages;
  9. namespace Renci.SshNet
  10. {
  11. /// <summary>
  12. /// Provides functionality to perform password authentication.
  13. /// </summary>
  14. public partial class PasswordAuthenticationMethod : AuthenticationMethod, IDisposable
  15. {
  16. private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
  17. private Session _session;
  18. private EventWaitHandle _authenticationCompleted = new AutoResetEvent(false);
  19. private Exception _exception;
  20. private RequestMessage _requestMessage;
  21. private string _password;
  22. /// <summary>
  23. /// Gets authentication method name
  24. /// </summary>
  25. public override string Name
  26. {
  27. get { return this._requestMessage.MethodName; }
  28. }
  29. /// <summary>
  30. /// Occurs when user's password has expired and needs to be changed.
  31. /// </summary>
  32. public event EventHandler<AuthenticationPasswordChangeEventArgs> PasswordExpired;
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="PasswordAuthenticationMethod"/> class.
  35. /// </summary>
  36. /// <param name="host">The host.</param>
  37. /// <param name="port">The port.</param>
  38. /// <param name="username">The username.</param>
  39. /// <param name="password">The password.</param>
  40. /// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or null.</exception>
  41. /// <exception cref="ArgumentException"><paramref name="password"/> is null.</exception>
  42. public PasswordAuthenticationMethod(string username, string password)
  43. : base(username)
  44. {
  45. if (password == null)
  46. throw new ArgumentNullException("password");
  47. this._password = password;
  48. this._requestMessage = new RequestMessagePassword(ServiceName.Connection, this.Username, password);
  49. }
  50. /// <summary>
  51. /// Authenticates the specified session.
  52. /// </summary>
  53. /// <param name="session">The session to authenticate.</param>
  54. /// <returns></returns>
  55. public override AuthenticationResult Authenticate(Session session)
  56. {
  57. this._session = session;
  58. session.UserAuthenticationSuccessReceived += Session_UserAuthenticationSuccessReceived;
  59. session.UserAuthenticationFailureReceived += Session_UserAuthenticationFailureReceived;
  60. session.MessageReceived += Session_MessageReceived;
  61. session.RegisterMessage("SSH_MSG_USERAUTH_PASSWD_CHANGEREQ");
  62. session.SendMessage(this._requestMessage);
  63. session.WaitHandle(this._authenticationCompleted);
  64. session.UserAuthenticationSuccessReceived -= Session_UserAuthenticationSuccessReceived;
  65. session.UserAuthenticationFailureReceived -= Session_UserAuthenticationFailureReceived;
  66. session.MessageReceived -= Session_MessageReceived;
  67. if (this._exception != null)
  68. {
  69. throw this._exception;
  70. }
  71. return this._authenticationResult;
  72. }
  73. private void Session_UserAuthenticationSuccessReceived(object sender, MessageEventArgs<SuccessMessage> e)
  74. {
  75. this._authenticationResult = AuthenticationResult.Success;
  76. this._authenticationCompleted.Set();
  77. }
  78. private void Session_UserAuthenticationFailureReceived(object sender, MessageEventArgs<FailureMessage> e)
  79. {
  80. if (e.Message.PartialSuccess)
  81. this._authenticationResult = AuthenticationResult.PartialSuccess;
  82. else
  83. this._authenticationResult = AuthenticationResult.Failure;
  84. // Copy allowed authentication methods
  85. this.AllowedAuthentications = e.Message.AllowedAuthentications.ToList();
  86. this._authenticationCompleted.Set();
  87. }
  88. private void Session_MessageReceived(object sender, MessageEventArgs<Message> e)
  89. {
  90. if (e.Message is PasswordChangeRequiredMessage)
  91. {
  92. this._session.UnRegisterMessage("SSH_MSG_USERAUTH_PASSWD_CHANGEREQ");
  93. this.ExecuteThread(() =>
  94. {
  95. try
  96. {
  97. var eventArgs = new AuthenticationPasswordChangeEventArgs(this.Username);
  98. // Raise an event to allow user to supply a new password
  99. if (this.PasswordExpired != null)
  100. {
  101. this.PasswordExpired(this, eventArgs);
  102. }
  103. // Send new authentication request with new password
  104. this._session.SendMessage(new RequestMessagePassword(ServiceName.Connection, this.Username, this._password, eventArgs.NewPassword));
  105. }
  106. catch (Exception exp)
  107. {
  108. this._exception = exp;
  109. this._authenticationCompleted.Set();
  110. }
  111. });
  112. }
  113. }
  114. partial void ExecuteThread(Action action);
  115. #region IDisposable Members
  116. private bool isDisposed = false;
  117. /// <summary>
  118. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  119. /// </summary>
  120. public void Dispose()
  121. {
  122. Dispose(true);
  123. GC.SuppressFinalize(this);
  124. }
  125. /// <summary>
  126. /// Releases unmanaged and - optionally - managed resources
  127. /// </summary>
  128. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  129. protected virtual void Dispose(bool disposing)
  130. {
  131. // Check to see if Dispose has already been called.
  132. if (!this.isDisposed)
  133. {
  134. // If disposing equals true, dispose all managed
  135. // and unmanaged resources.
  136. if (disposing)
  137. {
  138. // Dispose managed resources.
  139. if (this._authenticationCompleted != null)
  140. {
  141. this._authenticationCompleted.Dispose();
  142. this._authenticationCompleted = null;
  143. }
  144. }
  145. // Note disposing has been done.
  146. isDisposed = true;
  147. }
  148. }
  149. /// <summary>
  150. /// Releases unmanaged resources and performs other cleanup operations before the
  151. /// <see cref="PasswordConnectionInfo"/> is reclaimed by garbage collection.
  152. /// </summary>
  153. ~PasswordAuthenticationMethod()
  154. {
  155. // Do not re-create Dispose clean-up code here.
  156. // Calling Dispose(false) is optimal in terms of
  157. // readability and maintainability.
  158. Dispose(false);
  159. }
  160. #endregion
  161. }
  162. }