| | | 1 | | using System; |
| | | 2 | | using System.Threading; |
| | | 3 | | |
| | | 4 | | using Renci.SshNet.Messages; |
| | | 5 | | using Renci.SshNet.Messages.Authentication; |
| | | 6 | | |
| | | 7 | | namespace Renci.SshNet |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides functionality for "none" authentication method. |
| | | 11 | | /// </summary> |
| | | 12 | | public class NoneAuthenticationMethod : AuthenticationMethod, IDisposable |
| | | 13 | | { |
| | 1633 | 14 | | private AuthenticationResult _authenticationResult = AuthenticationResult.Failure; |
| | 1633 | 15 | | private EventWaitHandle _authenticationCompleted = new AutoResetEvent(initialState: false); |
| | | 16 | | private bool _isDisposed; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets the name of the authentication method. |
| | | 20 | | /// </summary> |
| | | 21 | | public override string Name |
| | | 22 | | { |
| | 9 | 23 | | get { return "none"; } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="NoneAuthenticationMethod"/> class. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="username">The username.</param> |
| | | 30 | | /// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or <see langword="null"/>.</ex |
| | | 31 | | public NoneAuthenticationMethod(string username) |
| | 1633 | 32 | | : base(username) |
| | 1627 | 33 | | { |
| | 1627 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Authenticates the specified session. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="session">The session.</param> |
| | | 40 | | /// <returns> |
| | | 41 | | /// Result of authentication process. |
| | | 42 | | /// </returns> |
| | | 43 | | /// <exception cref="ArgumentNullException"><paramref name="session" /> is <see langword="null"/>.</exception> |
| | | 44 | | public override AuthenticationResult Authenticate(Session session) |
| | 1198 | 45 | | { |
| | 1198 | 46 | | if (session is null) |
| | 0 | 47 | | { |
| | 0 | 48 | | throw new ArgumentNullException(nameof(session)); |
| | | 49 | | } |
| | | 50 | | |
| | 1198 | 51 | | session.UserAuthenticationSuccessReceived += Session_UserAuthenticationSuccessReceived; |
| | 1198 | 52 | | session.UserAuthenticationFailureReceived += Session_UserAuthenticationFailureReceived; |
| | | 53 | | |
| | | 54 | | try |
| | 1198 | 55 | | { |
| | 1198 | 56 | | session.SendMessage(new RequestMessageNone(ServiceName.Connection, Username)); |
| | 1198 | 57 | | session.WaitOnHandle(_authenticationCompleted); |
| | 1197 | 58 | | } |
| | | 59 | | finally |
| | 1198 | 60 | | { |
| | 1198 | 61 | | session.UserAuthenticationSuccessReceived -= Session_UserAuthenticationSuccessReceived; |
| | 1198 | 62 | | session.UserAuthenticationFailureReceived -= Session_UserAuthenticationFailureReceived; |
| | 1198 | 63 | | } |
| | | 64 | | |
| | 1197 | 65 | | return _authenticationResult; |
| | 1197 | 66 | | } |
| | | 67 | | |
| | | 68 | | private void Session_UserAuthenticationSuccessReceived(object sender, MessageEventArgs<SuccessMessage> e) |
| | 0 | 69 | | { |
| | 0 | 70 | | _authenticationResult = AuthenticationResult.Success; |
| | | 71 | | |
| | 0 | 72 | | _ = _authenticationCompleted.Set(); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | private void Session_UserAuthenticationFailureReceived(object sender, MessageEventArgs<FailureMessage> e) |
| | 1197 | 76 | | { |
| | 1197 | 77 | | if (e.Message.PartialSuccess) |
| | 0 | 78 | | { |
| | 0 | 79 | | _authenticationResult = AuthenticationResult.PartialSuccess; |
| | 0 | 80 | | } |
| | | 81 | | else |
| | 1197 | 82 | | { |
| | 1197 | 83 | | _authenticationResult = AuthenticationResult.Failure; |
| | 1197 | 84 | | } |
| | | 85 | | |
| | | 86 | | // Copy allowed authentication methods |
| | 1197 | 87 | | AllowedAuthentications = e.Message.AllowedAuthentications; |
| | | 88 | | |
| | 1197 | 89 | | _ = _authenticationCompleted.Set(); |
| | 1197 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 94 | | /// </summary> |
| | | 95 | | public void Dispose() |
| | 0 | 96 | | { |
| | 0 | 97 | | Dispose(disposing: true); |
| | 0 | 98 | | GC.SuppressFinalize(this); |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 105 | | protected virtual void Dispose(bool disposing) |
| | 1631 | 106 | | { |
| | 1631 | 107 | | if (_isDisposed) |
| | 0 | 108 | | { |
| | 0 | 109 | | return; |
| | | 110 | | } |
| | | 111 | | |
| | 1631 | 112 | | if (disposing) |
| | 0 | 113 | | { |
| | 0 | 114 | | var authenticationCompleted = _authenticationCompleted; |
| | 0 | 115 | | if (authenticationCompleted != null) |
| | 0 | 116 | | { |
| | 0 | 117 | | authenticationCompleted.Dispose(); |
| | 0 | 118 | | _authenticationCompleted = null; |
| | 0 | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | _isDisposed = true; |
| | 0 | 122 | | } |
| | 1631 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Finalizes an instance of the <see cref="NoneAuthenticationMethod"/> class. |
| | | 127 | | /// </summary> |
| | | 128 | | ~NoneAuthenticationMethod() |
| | 3262 | 129 | | { |
| | 1631 | 130 | | Dispose(disposing: false); |
| | 3262 | 131 | | } |
| | | 132 | | } |
| | | 133 | | } |