KeyboardInteractiveConnectionInfo.cs 9.8 KB

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