| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Runtime.ExceptionServices; |
| | | 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 keyboard interactive authentication. |
| | | 15 | | /// </summary> |
| | | 16 | | public class KeyboardInteractiveAuthenticationMethod : AuthenticationMethod, IDisposable |
| | | 17 | | { |
| | | 18 | | private readonly RequestMessage _requestMessage; |
| | 535 | 19 | | private AuthenticationResult _authenticationResult = AuthenticationResult.Failure; |
| | | 20 | | private Session _session; |
| | 535 | 21 | | private EventWaitHandle _authenticationCompleted = new AutoResetEvent(initialState: false); |
| | | 22 | | private Exception _exception; |
| | | 23 | | private bool _isDisposed; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets the name of the authentication method. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <value> |
| | | 29 | | /// The name of the authentication method. |
| | | 30 | | /// </value> |
| | | 31 | | public override string Name |
| | | 32 | | { |
| | 12 | 33 | | get { return _requestMessage.MethodName; } |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Occurs when server prompts for more authentication information. |
| | | 38 | | /// </summary> |
| | | 39 | | public event EventHandler<AuthenticationPromptEventArgs> AuthenticationPrompt; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Initializes a new instance of the <see cref="KeyboardInteractiveAuthenticationMethod"/> class. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="username">The username.</param> |
| | | 45 | | /// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or <see langword="null"/>.</ex |
| | | 46 | | public KeyboardInteractiveAuthenticationMethod(string username) |
| | 535 | 47 | | : base(username) |
| | 529 | 48 | | { |
| | 529 | 49 | | _requestMessage = new RequestMessageKeyboardInteractive(ServiceName.Connection, username); |
| | 529 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Authenticates the specified session. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="session">The session to authenticate.</param> |
| | | 56 | | /// <returns>Result of authentication process.</returns> |
| | | 57 | | public override AuthenticationResult Authenticate(Session session) |
| | 3 | 58 | | { |
| | 3 | 59 | | _session = session; |
| | | 60 | | |
| | 3 | 61 | | session.UserAuthenticationSuccessReceived += Session_UserAuthenticationSuccessReceived; |
| | 3 | 62 | | session.UserAuthenticationFailureReceived += Session_UserAuthenticationFailureReceived; |
| | 3 | 63 | | session.UserAuthenticationInformationRequestReceived += Session_UserAuthenticationInformationRequestReceived |
| | | 64 | | |
| | 3 | 65 | | session.RegisterMessage("SSH_MSG_USERAUTH_INFO_REQUEST"); |
| | | 66 | | |
| | | 67 | | try |
| | 3 | 68 | | { |
| | 3 | 69 | | session.SendMessage(_requestMessage); |
| | 3 | 70 | | session.WaitOnHandle(_authenticationCompleted); |
| | 3 | 71 | | } |
| | | 72 | | finally |
| | 3 | 73 | | { |
| | 3 | 74 | | session.UnRegisterMessage("SSH_MSG_USERAUTH_INFO_REQUEST"); |
| | 3 | 75 | | session.UserAuthenticationSuccessReceived -= Session_UserAuthenticationSuccessReceived; |
| | 3 | 76 | | session.UserAuthenticationFailureReceived -= Session_UserAuthenticationFailureReceived; |
| | 3 | 77 | | session.UserAuthenticationInformationRequestReceived -= Session_UserAuthenticationInformationRequestRece |
| | 3 | 78 | | } |
| | | 79 | | |
| | 3 | 80 | | if (_exception != null) |
| | 0 | 81 | | { |
| | 0 | 82 | | ExceptionDispatchInfo.Capture(_exception).Throw(); |
| | 0 | 83 | | } |
| | | 84 | | |
| | 3 | 85 | | return _authenticationResult; |
| | 3 | 86 | | } |
| | | 87 | | |
| | | 88 | | private void Session_UserAuthenticationSuccessReceived(object sender, MessageEventArgs<SuccessMessage> e) |
| | 2 | 89 | | { |
| | 2 | 90 | | _authenticationResult = AuthenticationResult.Success; |
| | 2 | 91 | | _ = _authenticationCompleted.Set(); |
| | 2 | 92 | | } |
| | | 93 | | |
| | | 94 | | private void Session_UserAuthenticationFailureReceived(object sender, MessageEventArgs<FailureMessage> e) |
| | 1 | 95 | | { |
| | 1 | 96 | | if (e.Message.PartialSuccess) |
| | 1 | 97 | | { |
| | 1 | 98 | | _authenticationResult = AuthenticationResult.PartialSuccess; |
| | 1 | 99 | | } |
| | | 100 | | else |
| | 0 | 101 | | { |
| | 0 | 102 | | _authenticationResult = AuthenticationResult.Failure; |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | // Copy allowed authentication methods |
| | 1 | 106 | | AllowedAuthentications = e.Message.AllowedAuthentications; |
| | | 107 | | |
| | 1 | 108 | | _ = _authenticationCompleted.Set(); |
| | 1 | 109 | | } |
| | | 110 | | |
| | | 111 | | private void Session_UserAuthenticationInformationRequestReceived(object sender, MessageEventArgs<InformationReq |
| | 9 | 112 | | { |
| | 9 | 113 | | var informationRequestMessage = e.Message; |
| | | 114 | | |
| | 9 | 115 | | var eventArgs = new AuthenticationPromptEventArgs(Username, |
| | 9 | 116 | | informationRequestMessage.Instruction, |
| | 9 | 117 | | informationRequestMessage.Language, |
| | 9 | 118 | | informationRequestMessage.Prompts); |
| | | 119 | | |
| | 9 | 120 | | ThreadAbstraction.ExecuteThread(() => |
| | 9 | 121 | | { |
| | 9 | 122 | | try |
| | 9 | 123 | | { |
| | 9 | 124 | | AuthenticationPrompt?.Invoke(this, eventArgs); |
| | 9 | 125 | | |
| | 9 | 126 | | var informationResponse = new InformationResponseMessage(); |
| | 9 | 127 | | |
| | 51 | 128 | | foreach (var response in from r in eventArgs.Prompts orderby r.Id ascending select r.Response) |
| | 6 | 129 | | { |
| | 6 | 130 | | informationResponse.Responses.Add(response); |
| | 6 | 131 | | } |
| | 9 | 132 | | |
| | 9 | 133 | | // Send information response message |
| | 9 | 134 | | _session.SendMessage(informationResponse); |
| | 9 | 135 | | } |
| | 0 | 136 | | catch (Exception exp) |
| | 0 | 137 | | { |
| | 0 | 138 | | _exception = exp; |
| | 0 | 139 | | _ = _authenticationCompleted.Set(); |
| | 0 | 140 | | } |
| | 18 | 141 | | }); |
| | 9 | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 146 | | /// </summary> |
| | | 147 | | public void Dispose() |
| | 0 | 148 | | { |
| | 0 | 149 | | Dispose(disposing: true); |
| | 0 | 150 | | GC.SuppressFinalize(this); |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 155 | | /// </summary> |
| | | 156 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 157 | | protected virtual void Dispose(bool disposing) |
| | 532 | 158 | | { |
| | 532 | 159 | | if (_isDisposed) |
| | 0 | 160 | | { |
| | 0 | 161 | | return; |
| | | 162 | | } |
| | | 163 | | |
| | 532 | 164 | | if (disposing) |
| | 0 | 165 | | { |
| | 0 | 166 | | var authenticationCompleted = _authenticationCompleted; |
| | 0 | 167 | | if (authenticationCompleted != null) |
| | 0 | 168 | | { |
| | 0 | 169 | | _authenticationCompleted = null; |
| | 0 | 170 | | authenticationCompleted.Dispose(); |
| | 0 | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | _isDisposed = true; |
| | 0 | 174 | | } |
| | 532 | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Finalizes an instance of the <see cref="KeyboardInteractiveAuthenticationMethod"/> class. |
| | | 179 | | /// </summary> |
| | | 180 | | ~KeyboardInteractiveAuthenticationMethod() |
| | 1064 | 181 | | { |
| | 532 | 182 | | Dispose(disposing: false); |
| | 1064 | 183 | | } |
| | | 184 | | } |
| | | 185 | | } |