PasswordConnectionInfo.cs 7.3 KB

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