KeyboardInteractiveConnectionInfo.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. using Renci.SshNet.Common;
  9. namespace Renci.SshNet
  10. {
  11. /// <summary>
  12. /// Provides connection information when keyboard interactive authentication method is used
  13. /// </summary>
  14. public partial class KeyboardInteractiveConnectionInfo : ConnectionInfo, IDisposable
  15. {
  16. private EventWaitHandle _authenticationCompleted = new AutoResetEvent(false);
  17. private Exception _exception;
  18. private RequestMessage _requestMessage;
  19. /// <summary>
  20. /// Gets connection name
  21. /// </summary>
  22. public override string Name
  23. {
  24. get
  25. {
  26. return this._requestMessage.MethodName;
  27. }
  28. }
  29. /// <summary>
  30. /// Occurs when server prompts for more authentication information.
  31. /// </summary>
  32. public event EventHandler<AuthenticationPromptEventArgs> AuthenticationPrompt;
  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="username">The username.</param>
  38. public KeyboardInteractiveConnectionInfo(string host, string username)
  39. : this(host, 22, username, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty)
  40. {
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  44. /// </summary>
  45. /// <param name="host">The host.</param>
  46. /// <param name="port">The port.</param>
  47. /// <param name="username">The username.</param>
  48. public KeyboardInteractiveConnectionInfo(string host, int port, string username)
  49. : this(host, port, username, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty)
  50. {
  51. }
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  54. /// </summary>
  55. /// <param name="host">Connection host.</param>
  56. /// <param name="port">Connection port.</param>
  57. /// <param name="username">Connection username.</param>
  58. /// <param name="proxyType">Type of the proxy.</param>
  59. /// <param name="proxyHost">The proxy host.</param>
  60. /// <param name="proxyPort">The proxy port.</param>
  61. public KeyboardInteractiveConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort)
  62. : this(host, port, username, proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  63. {
  64. }
  65. /// <summary>
  66. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  67. /// </summary>
  68. /// <param name="host">Connection host.</param>
  69. /// <param name="port">Connection port.</param>
  70. /// <param name="username">Connection username.</param>
  71. /// <param name="proxyType">Type of the proxy.</param>
  72. /// <param name="proxyHost">The proxy host.</param>
  73. /// <param name="proxyPort">The proxy port.</param>
  74. /// <param name="proxyUsername">The proxy username.</param>
  75. public KeyboardInteractiveConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  76. : this(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  77. {
  78. }
  79. /// <summary>
  80. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  81. /// </summary>
  82. /// <param name="host">Connection host.</param>
  83. /// <param name="username">Connection username.</param>
  84. /// <param name="proxyType">Type of the proxy.</param>
  85. /// <param name="proxyHost">The proxy host.</param>
  86. /// <param name="proxyPort">The proxy port.</param>
  87. public KeyboardInteractiveConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort)
  88. : this(host, 22, username, proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  89. {
  90. }
  91. /// <summary>
  92. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  93. /// </summary>
  94. /// <param name="host">Connection host.</param>
  95. /// <param name="username">Connection username.</param>
  96. /// <param name="proxyType">Type of the proxy.</param>
  97. /// <param name="proxyHost">The proxy host.</param>
  98. /// <param name="proxyPort">The proxy port.</param>
  99. /// <param name="proxyUsername">The proxy username.</param>
  100. public KeyboardInteractiveConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  101. : this(host, 22, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  102. {
  103. }
  104. /// <summary>
  105. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  106. /// </summary>
  107. /// <param name="host">Connection host.</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, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  115. : this(host, 22, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword)
  116. {
  117. }
  118. /// <summary>
  119. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  120. /// </summary>
  121. /// <param name="host">Connection host.</param>
  122. /// <param name="port">Connection port.</param>
  123. /// <param name="username">Connection username.</param>
  124. /// <param name="proxyType">Type of the proxy.</param>
  125. /// <param name="proxyHost">The proxy host.</param>
  126. /// <param name="proxyPort">The proxy port.</param>
  127. /// <param name="proxyUsername">The proxy username.</param>
  128. /// <param name="proxyPassword">The proxy password.</param>
  129. public KeyboardInteractiveConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  130. : base(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword)
  131. {
  132. this._requestMessage = new RequestMessageKeyboardInteractive(ServiceName.Connection, username);
  133. }
  134. /// <summary>
  135. /// Called when connection needs to be authenticated.
  136. /// </summary>
  137. protected override void OnAuthenticate()
  138. {
  139. this.Session.RegisterMessage("SSH_MSG_USERAUTH_INFO_REQUEST");
  140. this.SendMessage(this._requestMessage);
  141. this.WaitHandle(this._authenticationCompleted);
  142. this.Session.UnRegisterMessage("SSH_MSG_USERAUTH_INFO_REQUEST");
  143. if (this._exception != null)
  144. {
  145. throw this._exception;
  146. }
  147. }
  148. /// <summary>
  149. /// Handles the UserAuthenticationSuccessMessageReceived 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_UserAuthenticationSuccessMessageReceived(object sender, MessageEventArgs<SuccessMessage> e)
  154. {
  155. base.Session_UserAuthenticationSuccessMessageReceived(sender, e);
  156. this._authenticationCompleted.Set();
  157. }
  158. /// <summary>
  159. /// Handles the UserAuthenticationFailureReceived event of the session.
  160. /// </summary>
  161. /// <param name="sender">The source of the event.</param>
  162. /// <param name="e">The event data.</param>
  163. protected override void Session_UserAuthenticationFailureReceived(object sender, MessageEventArgs<FailureMessage> e)
  164. {
  165. base.Session_UserAuthenticationFailureReceived(sender, e);
  166. this._authenticationCompleted.Set();
  167. }
  168. /// <summary>
  169. /// Handles the MessageReceived event of the session.
  170. /// </summary>
  171. /// <param name="sender">The source of the event.</param>
  172. /// <param name="e">The event data.</param>
  173. protected override void Session_MessageReceived(object sender, MessageEventArgs<Message> e)
  174. {
  175. var informationRequestMessage = e.Message as InformationRequestMessage;
  176. if (informationRequestMessage != null)
  177. {
  178. var eventArgs = new AuthenticationPromptEventArgs(this.Username, informationRequestMessage.Instruction, informationRequestMessage.Language, informationRequestMessage.Prompts);
  179. this.ExecuteThread(() =>
  180. {
  181. try
  182. {
  183. if (this.AuthenticationPrompt != null)
  184. {
  185. this.AuthenticationPrompt(this, eventArgs);
  186. }
  187. var informationResponse = new InformationResponseMessage();
  188. foreach (var response in from r in eventArgs.Prompts orderby r.Id ascending select r.Response)
  189. {
  190. informationResponse.Responses.Add(response);
  191. }
  192. // Send information response message
  193. this.SendMessage(informationResponse);
  194. }
  195. catch (Exception exp)
  196. {
  197. this._exception = exp;
  198. this._authenticationCompleted.Set();
  199. }
  200. });
  201. }
  202. }
  203. partial void ExecuteThread(Action action);
  204. #region IDisposable Members
  205. private bool isDisposed = false;
  206. /// <summary>
  207. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  208. /// </summary>
  209. public void Dispose()
  210. {
  211. Dispose(true);
  212. GC.SuppressFinalize(this);
  213. }
  214. /// <summary>
  215. /// Releases unmanaged and - optionally - managed resources
  216. /// </summary>
  217. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  218. protected virtual void Dispose(bool disposing)
  219. {
  220. // Check to see if Dispose has already been called.
  221. if (!this.isDisposed)
  222. {
  223. // If disposing equals true, dispose all managed
  224. // and unmanaged resources.
  225. if (disposing)
  226. {
  227. // Dispose managed resources.
  228. if (this._authenticationCompleted != null)
  229. {
  230. this._authenticationCompleted.Dispose();
  231. this._authenticationCompleted = null;
  232. }
  233. }
  234. // Note disposing has been done.
  235. isDisposed = true;
  236. }
  237. }
  238. /// <summary>
  239. /// Releases unmanaged resources and performs other cleanup operations before the
  240. /// <see cref="KeyboardInteractiveConnectionInfo"/> is reclaimed by garbage collection.
  241. /// </summary>
  242. ~KeyboardInteractiveConnectionInfo()
  243. {
  244. // Do not re-create Dispose clean-up code here.
  245. // Calling Dispose(false) is optimal in terms of
  246. // readability and maintainability.
  247. Dispose(false);
  248. }
  249. #endregion
  250. }
  251. }