KeyboardInteractiveConnectionInfo.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Renci.SshNet.Common;
  6. namespace Renci.SshNet
  7. {
  8. /// <summary>
  9. /// Provides connection information when keyboard interactive authentication method is used
  10. /// </summary>
  11. public class KeyboardInteractiveConnectionInfo : ConnectionInfo, IDisposable
  12. {
  13. /// <summary>
  14. /// Occurs when server prompts for more authentication information.
  15. /// </summary>
  16. public event EventHandler<AuthenticationPromptEventArgs> AuthenticationPrompt;
  17. // TODO: DOCS Add exception documentation for this class.
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  20. /// </summary>
  21. /// <param name="host">The host.</param>
  22. /// <param name="username">The username.</param>
  23. public KeyboardInteractiveConnectionInfo(string host, string username)
  24. : this(host, 22, username, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty)
  25. {
  26. }
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  29. /// </summary>
  30. /// <param name="host">The host.</param>
  31. /// <param name="port">The port.</param>
  32. /// <param name="username">The username.</param>
  33. public KeyboardInteractiveConnectionInfo(string host, int port, string username)
  34. : this(host, port, username, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty)
  35. {
  36. }
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  39. /// </summary>
  40. /// <param name="host">Connection host.</param>
  41. /// <param name="port">Connection port.</param>
  42. /// <param name="username">Connection username.</param>
  43. /// <param name="proxyType">Type of the proxy.</param>
  44. /// <param name="proxyHost">The proxy host.</param>
  45. /// <param name="proxyPort">The proxy port.</param>
  46. public KeyboardInteractiveConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort)
  47. : this(host, port, username, proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  48. {
  49. }
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  52. /// </summary>
  53. /// <param name="host">Connection host.</param>
  54. /// <param name="port">Connection port.</param>
  55. /// <param name="username">Connection username.</param>
  56. /// <param name="proxyType">Type of the proxy.</param>
  57. /// <param name="proxyHost">The proxy host.</param>
  58. /// <param name="proxyPort">The proxy port.</param>
  59. /// <param name="proxyUsername">The proxy username.</param>
  60. public KeyboardInteractiveConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  61. : this(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  62. {
  63. }
  64. /// <summary>
  65. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  66. /// </summary>
  67. /// <param name="host">Connection host.</param>
  68. /// <param name="username">Connection username.</param>
  69. /// <param name="proxyType">Type of the proxy.</param>
  70. /// <param name="proxyHost">The proxy host.</param>
  71. /// <param name="proxyPort">The proxy port.</param>
  72. public KeyboardInteractiveConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort)
  73. : this(host, 22, username, proxyType, proxyHost, proxyPort, string.Empty, 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. /// <param name="proxyUsername">The proxy username.</param>
  85. public KeyboardInteractiveConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  86. : this(host, 22, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  87. {
  88. }
  89. /// <summary>
  90. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  91. /// </summary>
  92. /// <param name="host">Connection host.</param>
  93. /// <param name="username">Connection username.</param>
  94. /// <param name="proxyType">Type of the proxy.</param>
  95. /// <param name="proxyHost">The proxy host.</param>
  96. /// <param name="proxyPort">The proxy port.</param>
  97. /// <param name="proxyUsername">The proxy username.</param>
  98. /// <param name="proxyPassword">The proxy password.</param>
  99. public KeyboardInteractiveConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  100. : this(host, 22, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword)
  101. {
  102. }
  103. /// <summary>
  104. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  105. /// </summary>
  106. /// <param name="host">Connection host.</param>
  107. /// <param name="port">Connection port.</param>
  108. /// <param name="username">Connection username.</param>
  109. /// <param name="proxyType">Type of the proxy.</param>
  110. /// <param name="proxyHost">The proxy host.</param>
  111. /// <param name="proxyPort">The proxy port.</param>
  112. /// <param name="proxyUsername">The proxy username.</param>
  113. /// <param name="proxyPassword">The proxy password.</param>
  114. public KeyboardInteractiveConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  115. : base(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword, new KeyboardInteractiveAuthenticationMethod(username))
  116. {
  117. foreach (var authenticationMethod in this.AuthenticationMethods.OfType<KeyboardInteractiveAuthenticationMethod>())
  118. {
  119. authenticationMethod.AuthenticationPrompt += AuthenticationMethod_AuthenticationPrompt;
  120. }
  121. }
  122. private void AuthenticationMethod_AuthenticationPrompt(object sender, AuthenticationPromptEventArgs e)
  123. {
  124. if (this.AuthenticationPrompt != null)
  125. {
  126. this.AuthenticationPrompt(sender, e);
  127. }
  128. }
  129. #region IDisposable Members
  130. private bool isDisposed = false;
  131. /// <summary>
  132. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  133. /// </summary>
  134. public void Dispose()
  135. {
  136. Dispose(true);
  137. GC.SuppressFinalize(this);
  138. }
  139. /// <summary>
  140. /// Releases unmanaged and - optionally - managed resources
  141. /// </summary>
  142. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  143. protected virtual void Dispose(bool disposing)
  144. {
  145. // Check to see if Dispose has already been called.
  146. if (!this.isDisposed)
  147. {
  148. // If disposing equals true, dispose all managed
  149. // and unmanaged resources.
  150. if (disposing)
  151. {
  152. // Dispose managed resources.
  153. if (this.AuthenticationMethods != null)
  154. {
  155. foreach (var authenticationMethods in this.AuthenticationMethods.OfType<IDisposable>())
  156. {
  157. authenticationMethods.Dispose();
  158. }
  159. }
  160. }
  161. // Note disposing has been done.
  162. isDisposed = true;
  163. }
  164. }
  165. /// <summary>
  166. /// Releases unmanaged resources and performs other cleanup operations before the
  167. /// <see cref="PasswordConnectionInfo"/> is reclaimed by garbage collection.
  168. /// </summary>
  169. ~KeyboardInteractiveConnectionInfo()
  170. {
  171. // Do not re-create Dispose clean-up code here.
  172. // Calling Dispose(false) is optimal in terms of
  173. // readability and maintainability.
  174. Dispose(false);
  175. }
  176. #endregion
  177. }
  178. }