NoneConnectionInfo.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. namespace Renci.SshNet
  9. {
  10. /// <summary>
  11. /// Provides connection information when password authentication method is used
  12. /// </summary>
  13. public class NoneConnectionInfo : ConnectionInfo, IDisposable
  14. {
  15. private EventWaitHandle _authenticationCompleted = new AutoResetEvent(false);
  16. /// <summary>
  17. /// Gets list of allowed authentications.
  18. /// </summary>
  19. public IEnumerable<string> AllowedAuthentications { get; private set; }
  20. /// <summary>
  21. /// Gets connection name
  22. /// </summary>
  23. public override string Name
  24. {
  25. get
  26. {
  27. return "none";
  28. }
  29. }
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  32. /// </summary>
  33. /// <param name="host">The host.</param>
  34. /// <param name="username">The username.</param>
  35. public NoneConnectionInfo(string host, string username)
  36. : this(host, 22, username, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty)
  37. {
  38. }
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  41. /// </summary>
  42. /// <param name="host">The host.</param>
  43. /// <param name="port">The port.</param>
  44. /// <param name="username">The username.</param>
  45. public NoneConnectionInfo(string host, int port, string username)
  46. : this(host, port, username, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty)
  47. {
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  51. /// </summary>
  52. /// <param name="host">Connection host.</param>
  53. /// <param name="port">Connection port.</param>
  54. /// <param name="username">Connection username.</param>
  55. /// <param name="proxyType">Type of the proxy.</param>
  56. /// <param name="proxyHost">The proxy host.</param>
  57. /// <param name="proxyPort">The proxy port.</param>
  58. public NoneConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort)
  59. : this(host, port, username, proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  60. {
  61. }
  62. /// <summary>
  63. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  64. /// </summary>
  65. /// <param name="host">Connection host.</param>
  66. /// <param name="port">Connection port.</param>
  67. /// <param name="username">Connection username.</param>
  68. /// <param name="proxyType">Type of the proxy.</param>
  69. /// <param name="proxyHost">The proxy host.</param>
  70. /// <param name="proxyPort">The proxy port.</param>
  71. /// <param name="proxyUsername">The proxy username.</param>
  72. public NoneConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  73. : this(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  74. {
  75. }
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  78. /// </summary>
  79. /// <param name="host">Connection host.</param>
  80. /// <param name="username">Connection username.</param>
  81. /// <param name="proxyType">Type of the proxy.</param>
  82. /// <param name="proxyHost">The proxy host.</param>
  83. /// <param name="proxyPort">The proxy port.</param>
  84. public NoneConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort)
  85. : this(host, 22, username, proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  86. {
  87. }
  88. /// <summary>
  89. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  90. /// </summary>
  91. /// <param name="host">Connection host.</param>
  92. /// <param name="username">Connection username.</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. /// <param name="proxyUsername">The proxy username.</param>
  97. public NoneConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  98. : this(host, 22, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  99. {
  100. }
  101. /// <summary>
  102. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  103. /// </summary>
  104. /// <param name="host">Connection host.</param>
  105. /// <param name="username">Connection username.</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. /// <param name="proxyPassword">The proxy password.</param>
  111. public NoneConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  112. : this(host, 22, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword)
  113. {
  114. }
  115. /// <summary>
  116. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  117. /// </summary>
  118. /// <param name="host">Connection host.</param>
  119. /// <param name="port">Connection port.</param>
  120. /// <param name="username">Connection username.</param>
  121. /// <param name="proxyType">Type of the proxy.</param>
  122. /// <param name="proxyHost">The proxy host.</param>
  123. /// <param name="proxyPort">The proxy port.</param>
  124. /// <param name="proxyUsername">The proxy username.</param>
  125. /// <param name="proxyPassword">The proxy password.</param>
  126. public NoneConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  127. : base(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword)
  128. {
  129. }
  130. /// <summary>
  131. /// Called when connection needs to be authenticated.
  132. /// </summary>
  133. protected override void OnAuthenticate()
  134. {
  135. this.SendMessage(new RequestMessageNone(ServiceName.Connection, this.Username));
  136. this.WaitHandle(this._authenticationCompleted);
  137. }
  138. /// <summary>
  139. /// Handles the UserAuthenticationSuccessMessageReceived event of the session.
  140. /// </summary>
  141. /// <param name="sender">The source of the event.</param>
  142. /// <param name="e">The event data.</param>
  143. protected override void Session_UserAuthenticationSuccessMessageReceived(object sender, MessageEventArgs<SuccessMessage> e)
  144. {
  145. base.Session_UserAuthenticationSuccessMessageReceived(sender, e);
  146. this._authenticationCompleted.Set();
  147. }
  148. /// <summary>
  149. /// Handles the UserAuthenticationFailureReceived event of the session.
  150. /// </summary>
  151. /// <param name="sender">The source of the event.</param>
  152. /// <param name="e">The event data.</param>
  153. protected override void Session_UserAuthenticationFailureReceived(object sender, MessageEventArgs<FailureMessage> e)
  154. {
  155. base.Session_UserAuthenticationFailureReceived(sender, e);
  156. // Copy allowed authentication methods
  157. this.AllowedAuthentications = e.Message.AllowedAuthentications.ToList();
  158. this._authenticationCompleted.Set();
  159. }
  160. #region IDisposable Members
  161. private bool isDisposed = false;
  162. /// <summary>
  163. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  164. /// </summary>
  165. public void Dispose()
  166. {
  167. Dispose(true);
  168. GC.SuppressFinalize(this);
  169. }
  170. /// <summary>
  171. /// Releases unmanaged and - optionally - managed resources
  172. /// </summary>
  173. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  174. protected virtual void Dispose(bool disposing)
  175. {
  176. // Check to see if Dispose has already been called.
  177. if (!this.isDisposed)
  178. {
  179. // If disposing equals true, dispose all managed
  180. // and unmanaged resources.
  181. if (disposing)
  182. {
  183. // Dispose managed resources.
  184. if (this._authenticationCompleted != null)
  185. {
  186. this._authenticationCompleted.Dispose();
  187. this._authenticationCompleted = null;
  188. }
  189. }
  190. // Note disposing has been done.
  191. isDisposed = true;
  192. }
  193. }
  194. /// <summary>
  195. /// Releases unmanaged resources and performs other cleanup operations before the
  196. /// <see cref="PasswordConnectionInfo"/> is reclaimed by garbage collection.
  197. /// </summary>
  198. ~NoneConnectionInfo()
  199. {
  200. // Do not re-create Dispose clean-up code here.
  201. // Calling Dispose(false) is optimal in terms of
  202. // readability and maintainability.
  203. Dispose(false);
  204. }
  205. #endregion
  206. }
  207. }