NoneAuthenticationMethod.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 functionality for "none" authentication method
  12. /// </summary>
  13. public class NoneAuthenticationMethod : AuthenticationMethod, IDisposable
  14. {
  15. private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
  16. private EventWaitHandle _authenticationCompleted = new AutoResetEvent(false);
  17. /// <summary>
  18. /// Gets connection name
  19. /// </summary>
  20. public override string Name
  21. {
  22. get { return "none"; }
  23. }
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="KeyboardInteractiveConnectionInfo"/> class.
  26. /// </summary>
  27. /// <param name="username">The username.</param>
  28. /// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or null.</exception>
  29. public NoneAuthenticationMethod(string username)
  30. : base(username)
  31. {
  32. }
  33. /// <summary>
  34. /// Authenticates the specified session.
  35. /// </summary>
  36. /// <param name="session">The session.</param>
  37. /// <returns></returns>
  38. /// <exception cref="ArgumentNullException"><paramref name="session"/> is null.</exception>
  39. public override AuthenticationResult Authenticate(Session session)
  40. {
  41. if (session == null)
  42. throw new ArgumentNullException("session");
  43. session.UserAuthenticationSuccessReceived += Session_UserAuthenticationSuccessReceived;
  44. session.UserAuthenticationFailureReceived += Session_UserAuthenticationFailureReceived;
  45. session.SendMessage(new RequestMessageNone(ServiceName.Connection, this.Username));
  46. session.WaitHandle(this._authenticationCompleted);
  47. session.UserAuthenticationSuccessReceived -= Session_UserAuthenticationSuccessReceived;
  48. session.UserAuthenticationFailureReceived -= Session_UserAuthenticationFailureReceived;
  49. return this._authenticationResult;
  50. }
  51. private void Session_UserAuthenticationSuccessReceived(object sender, MessageEventArgs<SuccessMessage> e)
  52. {
  53. this._authenticationResult = AuthenticationResult.Success;
  54. this._authenticationCompleted.Set();
  55. }
  56. private void Session_UserAuthenticationFailureReceived(object sender, MessageEventArgs<FailureMessage> e)
  57. {
  58. if (e.Message.PartialSuccess)
  59. this._authenticationResult = AuthenticationResult.PartialSuccess;
  60. else
  61. this._authenticationResult = AuthenticationResult.Failure;
  62. // Copy allowed authentication methods
  63. this.AllowedAuthentications = e.Message.AllowedAuthentications.ToList();
  64. this._authenticationCompleted.Set();
  65. }
  66. #region IDisposable Members
  67. private bool isDisposed = false;
  68. /// <summary>
  69. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  70. /// </summary>
  71. public void Dispose()
  72. {
  73. Dispose(true);
  74. GC.SuppressFinalize(this);
  75. }
  76. /// <summary>
  77. /// Releases unmanaged and - optionally - managed resources
  78. /// </summary>
  79. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  80. protected virtual void Dispose(bool disposing)
  81. {
  82. // Check to see if Dispose has already been called.
  83. if (!this.isDisposed)
  84. {
  85. // If disposing equals true, dispose all managed
  86. // and unmanaged resources.
  87. if (disposing)
  88. {
  89. // Dispose managed resources.
  90. if (this._authenticationCompleted != null)
  91. {
  92. this._authenticationCompleted.Dispose();
  93. this._authenticationCompleted = null;
  94. }
  95. }
  96. // Note disposing has been done.
  97. isDisposed = true;
  98. }
  99. }
  100. /// <summary>
  101. /// Releases unmanaged resources and performs other cleanup operations before the
  102. /// <see cref="PasswordConnectionInfo"/> is reclaimed by garbage collection.
  103. /// </summary>
  104. ~NoneAuthenticationMethod()
  105. {
  106. // Do not re-create Dispose clean-up code here.
  107. // Calling Dispose(false) is optimal in terms of
  108. // readability and maintainability.
  109. Dispose(false);
  110. }
  111. #endregion
  112. }
  113. }