PasswordConnectionInfo.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. /// <exception cref="ArgumentNullException"><paramref name="password"/> is null.</exception>
  52. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is null or contains whitespace characters.</exception>
  53. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="f:IPEndPoint.MinPort"/> and <see cref="f:IPEndPoint.MaxPort"/>.</exception>
  54. public PasswordConnectionInfo(string host, int port, string username, string password)
  55. : this(host, port, username, password, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty)
  56. {
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  60. /// </summary>
  61. /// <param name="host">Connection host.</param>
  62. /// <param name="port">The port.</param>
  63. /// <param name="username">Connection username.</param>
  64. /// <param name="password">Connection password.</param>
  65. /// <param name="proxyType">Type of the proxy.</param>
  66. /// <param name="proxyHost">The proxy host.</param>
  67. /// <param name="proxyPort">The proxy port.</param>
  68. public PasswordConnectionInfo(string host, int port, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort)
  69. : this(host, port, username, password, proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  70. {
  71. }
  72. /// <summary>
  73. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  74. /// </summary>
  75. /// <param name="host">Connection host.</param>
  76. /// <param name="port">The port.</param>
  77. /// <param name="username">Connection username.</param>
  78. /// <param name="password">Connection password.</param>
  79. /// <param name="proxyType">Type of the proxy.</param>
  80. /// <param name="proxyHost">The proxy host.</param>
  81. /// <param name="proxyPort">The proxy port.</param>
  82. /// <param name="proxyUsername">The proxy username.</param>
  83. public PasswordConnectionInfo(string host, int port, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  84. : this(host, port, username, password, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  85. {
  86. }
  87. /// <summary>
  88. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  89. /// </summary>
  90. /// <param name="host">Connection host.</param>
  91. /// <param name="username">Connection username.</param>
  92. /// <param name="password">Connection password.</param>
  93. /// <param name="proxyType">Type of the proxy.</param>
  94. /// <param name="proxyHost">The proxy host.</param>
  95. /// <param name="proxyPort">The proxy port.</param>
  96. public PasswordConnectionInfo(string host, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort)
  97. : this(host, 22, username, password, proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  98. {
  99. }
  100. /// <summary>
  101. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  102. /// </summary>
  103. /// <param name="host">Connection host.</param>
  104. /// <param name="username">Connection username.</param>
  105. /// <param name="password">Connection password.</param>
  106. /// <param name="proxyType">Type of the proxy.</param>
  107. /// <param name="proxyHost">The proxy host.</param>
  108. /// <param name="proxyPort">The proxy port.</param>
  109. /// <param name="proxyUsername">The proxy username.</param>
  110. public PasswordConnectionInfo(string host, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  111. : this(host, 22, username, password, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  112. {
  113. }
  114. /// <summary>
  115. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  116. /// </summary>
  117. /// <param name="host">Connection host.</param>
  118. /// <param name="username">Connection username.</param>
  119. /// <param name="password">Connection password.</param>
  120. /// <param name="proxyType">Type of the proxy.</param>
  121. /// <param name="proxyHost">The proxy host.</param>
  122. /// <param name="proxyPort">The proxy port.</param>
  123. /// <param name="proxyUsername">The proxy username.</param>
  124. /// <param name="proxyPassword">The proxy password.</param>
  125. public PasswordConnectionInfo(string host, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  126. : this(host, 22, username, password, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword)
  127. {
  128. }
  129. /// <summary>
  130. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  131. /// </summary>
  132. /// <param name="host">Connection host.</param>
  133. /// <param name="port">The port.</param>
  134. /// <param name="username">Connection username.</param>
  135. /// <param name="password">Connection password.</param>
  136. /// <param name="proxyType">Type of the proxy.</param>
  137. /// <param name="proxyHost">The proxy host.</param>
  138. /// <param name="proxyPort">The proxy port.</param>
  139. /// <param name="proxyUsername">The proxy username.</param>
  140. /// <param name="proxyPassword">The proxy password.</param>
  141. public PasswordConnectionInfo(string host, int port, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  142. : base(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword)
  143. {
  144. if (password == null)
  145. throw new ArgumentNullException("password");
  146. this._password = password;
  147. this._requestMessage = new RequestMessagePassword(ServiceName.Connection, this.Username, password);
  148. }
  149. /// <summary>
  150. /// Called when connection needs to be authenticated.
  151. /// </summary>
  152. protected override void OnAuthenticate()
  153. {
  154. this.Session.RegisterMessage("SSH_MSG_USERAUTH_PASSWD_CHANGEREQ");
  155. this.SendMessage(this._requestMessage);
  156. this.WaitHandle(this._authenticationCompleted);
  157. if (this._exception != null)
  158. {
  159. throw this._exception;
  160. }
  161. }
  162. /// <summary>
  163. /// Handles the UserAuthenticationSuccessMessageReceived event of the session.
  164. /// </summary>
  165. /// <param name="sender">The source of the event.</param>
  166. /// <param name="e">The event data.</param>
  167. protected override void Session_UserAuthenticationSuccessMessageReceived(object sender, MessageEventArgs<SuccessMessage> e)
  168. {
  169. base.Session_UserAuthenticationSuccessMessageReceived(sender, e);
  170. this._authenticationCompleted.Set();
  171. }
  172. /// <summary>
  173. /// Handles the UserAuthenticationFailureReceived event of the session.
  174. /// </summary>
  175. /// <param name="sender">The source of the event.</param>
  176. /// <param name="e">The event data.</param>
  177. protected override void Session_UserAuthenticationFailureReceived(object sender, MessageEventArgs<FailureMessage> e)
  178. {
  179. base.Session_UserAuthenticationFailureReceived(sender, e);
  180. this._authenticationCompleted.Set();
  181. }
  182. /// <summary>
  183. /// Handles the MessageReceived event of the session.
  184. /// </summary>
  185. /// <param name="sender">The source of the event.</param>
  186. /// <param name="e">The event data.</param>
  187. protected override void Session_MessageReceived(object sender, MessageEventArgs<Message> e)
  188. {
  189. base.Session_MessageReceived(sender, e);
  190. if (e.Message is PasswordChangeRequiredMessage)
  191. {
  192. this.Session.UnRegisterMessage("SSH_MSG_USERAUTH_PASSWD_CHANGEREQ");
  193. this.ExecuteThread(() =>
  194. {
  195. try
  196. {
  197. var eventArgs = new AuthenticationPasswordChangeEventArgs(this.Username);
  198. // Raise an event to allow user to supply a new password
  199. if (this.PasswordExpired != null)
  200. {
  201. this.PasswordExpired(this, eventArgs);
  202. }
  203. // Send new authentication request with new password
  204. this.SendMessage(new RequestMessagePassword(ServiceName.Connection, this.Username, this._password, eventArgs.NewPassword));
  205. }
  206. catch (Exception exp)
  207. {
  208. this._exception = exp;
  209. this._authenticationCompleted.Set();
  210. }
  211. });
  212. }
  213. }
  214. partial void ExecuteThread(Action action);
  215. #region IDisposable Members
  216. private bool isDisposed = false;
  217. /// <summary>
  218. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  219. /// </summary>
  220. public void Dispose()
  221. {
  222. Dispose(true);
  223. GC.SuppressFinalize(this);
  224. }
  225. /// <summary>
  226. /// Releases unmanaged and - optionally - managed resources
  227. /// </summary>
  228. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  229. protected virtual void Dispose(bool disposing)
  230. {
  231. // Check to see if Dispose has already been called.
  232. if (!this.isDisposed)
  233. {
  234. // If disposing equals true, dispose all managed
  235. // and unmanaged resources.
  236. if (disposing)
  237. {
  238. // Dispose managed resources.
  239. if (this._authenticationCompleted != null)
  240. {
  241. this._authenticationCompleted.Dispose();
  242. this._authenticationCompleted = null;
  243. }
  244. }
  245. // Note disposing has been done.
  246. isDisposed = true;
  247. }
  248. }
  249. /// <summary>
  250. /// Releases unmanaged resources and performs other cleanup operations before the
  251. /// <see cref="PasswordConnectionInfo"/> is reclaimed by garbage collection.
  252. /// </summary>
  253. ~PasswordConnectionInfo()
  254. {
  255. // Do not re-create Dispose clean-up code here.
  256. // Calling Dispose(false) is optimal in terms of
  257. // readability and maintainability.
  258. Dispose(false);
  259. }
  260. #endregion
  261. }
  262. }