| | | 1 | | using System; |
| | | 2 | | using System.Runtime.ExceptionServices; |
| | | 3 | | using System.Text; |
| | | 4 | | using System.Threading; |
| | | 5 | | |
| | | 6 | | using Renci.SshNet.Abstractions; |
| | | 7 | | using Renci.SshNet.Common; |
| | | 8 | | using Renci.SshNet.Messages; |
| | | 9 | | using Renci.SshNet.Messages.Authentication; |
| | | 10 | | |
| | | 11 | | namespace Renci.SshNet |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides functionality to perform password authentication. |
| | | 15 | | /// </summary> |
| | | 16 | | public class PasswordAuthenticationMethod : AuthenticationMethod, IDisposable |
| | | 17 | | { |
| | | 18 | | private readonly RequestMessage _requestMessage; |
| | | 19 | | private readonly byte[] _password; |
| | 1490 | 20 | | private AuthenticationResult _authenticationResult = AuthenticationResult.Failure; |
| | | 21 | | private Session _session; |
| | 1490 | 22 | | private EventWaitHandle _authenticationCompleted = new AutoResetEvent(initialState: false); |
| | | 23 | | private Exception _exception; |
| | | 24 | | private bool _isDisposed; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the name of the authentication method. |
| | | 28 | | /// </summary> |
| | | 29 | | public override string Name |
| | | 30 | | { |
| | 1608 | 31 | | get { return _requestMessage.MethodName; } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the password as a sequence of bytes. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <value> |
| | | 38 | | /// The password as a sequence of bytes. |
| | | 39 | | /// </value> |
| | | 40 | | internal byte[] Password |
| | | 41 | | { |
| | 18 | 42 | | get { return _password; } |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Occurs when user's password has expired and needs to be changed. |
| | | 47 | | /// </summary> |
| | | 48 | | public event EventHandler<AuthenticationPasswordChangeEventArgs> PasswordExpired; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Initializes a new instance of the <see cref="PasswordAuthenticationMethod"/> class. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="username">The username.</param> |
| | | 54 | | /// <param name="password">The password.</param> |
| | | 55 | | /// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or <see langword="null"/>.</ex |
| | | 56 | | /// <exception cref="ArgumentException"><paramref name="password"/> is <see langword="null"/>.</exception> |
| | | 57 | | public PasswordAuthenticationMethod(string username, string password) |
| | 1253 | 58 | | : this(username, Encoding.UTF8.GetBytes(password)) |
| | 1244 | 59 | | { |
| | 1244 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Initializes a new instance of the <see cref="PasswordAuthenticationMethod"/> class. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="username">The username.</param> |
| | | 66 | | /// <param name="password">The password.</param> |
| | | 67 | | /// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or <see langword="null"/>.</ex |
| | | 68 | | /// <exception cref="ArgumentException"><paramref name="password"/> is <see langword="null"/>.</exception> |
| | | 69 | | public PasswordAuthenticationMethod(string username, byte[] password) |
| | 1490 | 70 | | : base(username) |
| | 1478 | 71 | | { |
| | 1478 | 72 | | if (password is null) |
| | 0 | 73 | | { |
| | 0 | 74 | | throw new ArgumentNullException(nameof(password)); |
| | | 75 | | } |
| | | 76 | | |
| | 1478 | 77 | | _password = password; |
| | 1478 | 78 | | _requestMessage = new RequestMessagePassword(ServiceName.Connection, Username, _password); |
| | 1478 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Authenticates the specified session. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <param name="session">The session to authenticate.</param> |
| | | 85 | | /// <returns> |
| | | 86 | | /// Result of authentication process. |
| | | 87 | | /// </returns> |
| | | 88 | | /// <exception cref="ArgumentNullException"><paramref name="session" /> is <see langword="null"/>.</exception> |
| | | 89 | | public override AuthenticationResult Authenticate(Session session) |
| | 528 | 90 | | { |
| | 528 | 91 | | if (session is null) |
| | 0 | 92 | | { |
| | 0 | 93 | | throw new ArgumentNullException(nameof(session)); |
| | | 94 | | } |
| | | 95 | | |
| | 528 | 96 | | _session = session; |
| | | 97 | | |
| | 528 | 98 | | session.UserAuthenticationSuccessReceived += Session_UserAuthenticationSuccessReceived; |
| | 528 | 99 | | session.UserAuthenticationFailureReceived += Session_UserAuthenticationFailureReceived; |
| | 528 | 100 | | session.UserAuthenticationPasswordChangeRequiredReceived += Session_UserAuthenticationPasswordChangeRequired |
| | | 101 | | |
| | | 102 | | try |
| | 528 | 103 | | { |
| | 528 | 104 | | session.RegisterMessage("SSH_MSG_USERAUTH_PASSWD_CHANGEREQ"); |
| | 528 | 105 | | session.SendMessage(_requestMessage); |
| | 528 | 106 | | session.WaitOnHandle(_authenticationCompleted); |
| | 528 | 107 | | } |
| | | 108 | | finally |
| | 528 | 109 | | { |
| | 528 | 110 | | session.UnRegisterMessage("SSH_MSG_USERAUTH_PASSWD_CHANGEREQ"); |
| | 528 | 111 | | session.UserAuthenticationSuccessReceived -= Session_UserAuthenticationSuccessReceived; |
| | 528 | 112 | | session.UserAuthenticationFailureReceived -= Session_UserAuthenticationFailureReceived; |
| | 528 | 113 | | session.UserAuthenticationPasswordChangeRequiredReceived -= Session_UserAuthenticationPasswordChangeRequ |
| | 528 | 114 | | } |
| | | 115 | | |
| | 528 | 116 | | if (_exception != null) |
| | 0 | 117 | | { |
| | 0 | 118 | | ExceptionDispatchInfo.Capture(_exception).Throw(); |
| | 0 | 119 | | } |
| | | 120 | | |
| | 528 | 121 | | return _authenticationResult; |
| | 528 | 122 | | } |
| | | 123 | | |
| | | 124 | | private void Session_UserAuthenticationSuccessReceived(object sender, MessageEventArgs<SuccessMessage> e) |
| | 514 | 125 | | { |
| | 514 | 126 | | _authenticationResult = AuthenticationResult.Success; |
| | | 127 | | |
| | 514 | 128 | | _ = _authenticationCompleted.Set(); |
| | 514 | 129 | | } |
| | | 130 | | |
| | | 131 | | private void Session_UserAuthenticationFailureReceived(object sender, MessageEventArgs<FailureMessage> e) |
| | 14 | 132 | | { |
| | 14 | 133 | | if (e.Message.PartialSuccess) |
| | 12 | 134 | | { |
| | 12 | 135 | | _authenticationResult = AuthenticationResult.PartialSuccess; |
| | 12 | 136 | | } |
| | | 137 | | else |
| | 2 | 138 | | { |
| | 2 | 139 | | _authenticationResult = AuthenticationResult.Failure; |
| | 2 | 140 | | } |
| | | 141 | | |
| | | 142 | | // Copy allowed authentication methods |
| | 14 | 143 | | AllowedAuthentications = e.Message.AllowedAuthentications; |
| | | 144 | | |
| | 14 | 145 | | _ = _authenticationCompleted.Set(); |
| | 14 | 146 | | } |
| | | 147 | | |
| | | 148 | | private void Session_UserAuthenticationPasswordChangeRequiredReceived(object sender, MessageEventArgs<PasswordCh |
| | 0 | 149 | | { |
| | 0 | 150 | | _session.UnRegisterMessage("SSH_MSG_USERAUTH_PASSWD_CHANGEREQ"); |
| | | 151 | | |
| | 0 | 152 | | ThreadAbstraction.ExecuteThread(() => |
| | 0 | 153 | | { |
| | 0 | 154 | | try |
| | 0 | 155 | | { |
| | 0 | 156 | | var eventArgs = new AuthenticationPasswordChangeEventArgs(Username); |
| | 0 | 157 | | |
| | 0 | 158 | | // Raise an event to allow user to supply a new password |
| | 0 | 159 | | PasswordExpired?.Invoke(this, eventArgs); |
| | 0 | 160 | | |
| | 0 | 161 | | // Send new authentication request with new password |
| | 0 | 162 | | _session.SendMessage(new RequestMessagePassword(ServiceName.Connection, Username, _password, eventAr |
| | 0 | 163 | | } |
| | 0 | 164 | | catch (Exception exp) |
| | 0 | 165 | | { |
| | 0 | 166 | | _exception = exp; |
| | 0 | 167 | | _ = _authenticationCompleted.Set(); |
| | 0 | 168 | | } |
| | 0 | 169 | | }); |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 174 | | /// </summary> |
| | | 175 | | public void Dispose() |
| | 174 | 176 | | { |
| | 174 | 177 | | Dispose(disposing: true); |
| | 174 | 178 | | GC.SuppressFinalize(this); |
| | 174 | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 183 | | /// </summary> |
| | | 184 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 185 | | protected virtual void Dispose(bool disposing) |
| | 1489 | 186 | | { |
| | 1489 | 187 | | if (_isDisposed) |
| | 0 | 188 | | { |
| | 0 | 189 | | return; |
| | | 190 | | } |
| | | 191 | | |
| | 1489 | 192 | | if (disposing) |
| | 174 | 193 | | { |
| | 174 | 194 | | var authenticationCompleted = _authenticationCompleted; |
| | 174 | 195 | | if (authenticationCompleted != null) |
| | 174 | 196 | | { |
| | 174 | 197 | | authenticationCompleted.Dispose(); |
| | 174 | 198 | | _authenticationCompleted = null; |
| | 174 | 199 | | } |
| | | 200 | | |
| | 174 | 201 | | _isDisposed = true; |
| | 174 | 202 | | } |
| | 1489 | 203 | | } |
| | | 204 | | |
| | | 205 | | /// <summary> |
| | | 206 | | /// Finalizes an instance of the <see cref="PasswordAuthenticationMethod"/> class. |
| | | 207 | | /// </summary> |
| | | 208 | | ~PasswordAuthenticationMethod() |
| | 2630 | 209 | | { |
| | 1315 | 210 | | Dispose(disposing: false); |
| | 2630 | 211 | | } |
| | | 212 | | } |
| | | 213 | | } |