| | | 1 | | using System; |
| | | 2 | | using System.Net; |
| | | 3 | | using System.Text; |
| | | 4 | | using Renci.SshNet.Common; |
| | | 5 | | |
| | | 6 | | namespace Renci.SshNet |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides connection information when password authentication method is used. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <example> |
| | | 12 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\PasswordConnectionInfoTest.cs" region="Example PasswordCo |
| | | 13 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\PasswordConnectionInfoTest.cs" region="Example PasswordCo |
| | | 14 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\PasswordConnectionInfoTest.cs" region="Example PasswordCo |
| | | 15 | | /// </example> |
| | | 16 | | public class PasswordConnectionInfo : ConnectionInfo, IDisposable |
| | | 17 | | { |
| | | 18 | | private bool _isDisposed; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Occurs when user's password has expired and needs to be changed. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <example> |
| | | 24 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\PasswordConnectionInfoTest.cs" region="Example Passwo |
| | | 25 | | /// </example> |
| | | 26 | | public event EventHandler<AuthenticationPasswordChangeEventArgs> PasswordExpired; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo" /> class. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="host">Connection host.</param> |
| | | 32 | | /// <param name="username">Connection username.</param> |
| | | 33 | | /// <param name="password">Connection password.</param> |
| | | 34 | | /// <example> |
| | | 35 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\PasswordConnectionInfoTest.cs" region="Example Passwo |
| | | 36 | | /// </example> |
| | | 37 | | /// <exception cref="ArgumentNullException"><paramref name="password"/> is <see langword="null"/>.</exception> |
| | | 38 | | /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is <s |
| | | 39 | | public PasswordConnectionInfo(string host, string username, string password) |
| | 12 | 40 | | : this(host, DefaultPort, username, Encoding.UTF8.GetBytes(password)) |
| | 0 | 41 | | { |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="host">Connection host.</param> |
| | | 48 | | /// <param name="port">Connection port.</param> |
| | | 49 | | /// <param name="username">Connection username.</param> |
| | | 50 | | /// <param name="password">Connection password.</param> |
| | | 51 | | /// <exception cref="ArgumentNullException"><paramref name="password"/> is <see langword="null"/>.</exception> |
| | | 52 | | /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is <s |
| | | 53 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.Mi |
| | | 54 | | public PasswordConnectionInfo(string host, int port, string username, string password) |
| | 231 | 55 | | : this(host, port, username, Encoding.UTF8.GetBytes(password), ProxyTypes.None, string.Empty, 0, string.Empt |
| | 225 | 56 | | { |
| | 225 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="host">Connection host.</param> |
| | | 63 | | /// <param name="port">The port.</param> |
| | | 64 | | /// <param name="username">Connection username.</param> |
| | | 65 | | /// <param name="password">Connection password.</param> |
| | | 66 | | /// <param name="proxyType">Type of the proxy.</param> |
| | | 67 | | /// <param name="proxyHost">The proxy host.</param> |
| | | 68 | | /// <param name="proxyPort">The proxy port.</param> |
| | | 69 | | public PasswordConnectionInfo(string host, int port, string username, string password, ProxyTypes proxyType, str |
| | 0 | 70 | | : this(host, port, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, string.Empty |
| | 0 | 71 | | { |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="host">Connection host.</param> |
| | | 78 | | /// <param name="port">The port.</param> |
| | | 79 | | /// <param name="username">Connection username.</param> |
| | | 80 | | /// <param name="password">Connection password.</param> |
| | | 81 | | /// <param name="proxyType">Type of the proxy.</param> |
| | | 82 | | /// <param name="proxyHost">The proxy host.</param> |
| | | 83 | | /// <param name="proxyPort">The proxy port.</param> |
| | | 84 | | /// <param name="proxyUsername">The proxy username.</param> |
| | | 85 | | public PasswordConnectionInfo(string host, int port, string username, string password, ProxyTypes proxyType, str |
| | 0 | 86 | | : this(host, port, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, proxyUsernam |
| | 0 | 87 | | { |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 92 | | /// </summary> |
| | | 93 | | /// <param name="host">Connection host.</param> |
| | | 94 | | /// <param name="username">Connection username.</param> |
| | | 95 | | /// <param name="password">Connection password.</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 | | public PasswordConnectionInfo(string host, string username, string password, ProxyTypes proxyType, string proxyH |
| | 0 | 100 | | : this(host, DefaultPort, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, strin |
| | 0 | 101 | | { |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="host">Connection host.</param> |
| | | 108 | | /// <param name="username">Connection username.</param> |
| | | 109 | | /// <param name="password">Connection password.</param> |
| | | 110 | | /// <param name="proxyType">Type of the proxy.</param> |
| | | 111 | | /// <param name="proxyHost">The proxy host.</param> |
| | | 112 | | /// <param name="proxyPort">The proxy port.</param> |
| | | 113 | | /// <param name="proxyUsername">The proxy username.</param> |
| | | 114 | | public PasswordConnectionInfo(string host, string username, string password, ProxyTypes proxyType, string proxyH |
| | 0 | 115 | | : this(host, DefaultPort, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, proxy |
| | 0 | 116 | | { |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 121 | | /// </summary> |
| | | 122 | | /// <param name="host">Connection host.</param> |
| | | 123 | | /// <param name="username">Connection username.</param> |
| | | 124 | | /// <param name="password">Connection password.</param> |
| | | 125 | | /// <param name="proxyType">Type of the proxy.</param> |
| | | 126 | | /// <param name="proxyHost">The proxy host.</param> |
| | | 127 | | /// <param name="proxyPort">The proxy port.</param> |
| | | 128 | | /// <param name="proxyUsername">The proxy username.</param> |
| | | 129 | | /// <param name="proxyPassword">The proxy password.</param> |
| | | 130 | | public PasswordConnectionInfo(string host, string username, string password, ProxyTypes proxyType, string proxyH |
| | 0 | 131 | | : this(host, DefaultPort, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, proxy |
| | 0 | 132 | | { |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="host">Connection host.</param> |
| | | 139 | | /// <param name="username">Connection username.</param> |
| | | 140 | | /// <param name="password">Connection password.</param> |
| | | 141 | | public PasswordConnectionInfo(string host, string username, byte[] password) |
| | 0 | 142 | | : this(host, DefaultPort, username, password) |
| | 0 | 143 | | { |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo" /> class. |
| | | 148 | | /// </summary> |
| | | 149 | | /// <param name="host">Connection host.</param> |
| | | 150 | | /// <param name="port">Connection port.</param> |
| | | 151 | | /// <param name="username">Connection username.</param> |
| | | 152 | | /// <param name="password">Connection password.</param> |
| | | 153 | | /// <exception cref="ArgumentNullException"><paramref name="password" /> is <see langword="null"/>.</exception> |
| | | 154 | | /// <exception cref="ArgumentException"><paramref name="host" /> is invalid, or <paramref name="username" /> is |
| | | 155 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="IPEndPoint.M |
| | | 156 | | public PasswordConnectionInfo(string host, int port, string username, byte[] password) |
| | 9 | 157 | | : this(host, port, username, password, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty) |
| | 0 | 158 | | { |
| | 0 | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 163 | | /// </summary> |
| | | 164 | | /// <param name="host">Connection host.</param> |
| | | 165 | | /// <param name="port">The port.</param> |
| | | 166 | | /// <param name="username">Connection username.</param> |
| | | 167 | | /// <param name="password">Connection password.</param> |
| | | 168 | | /// <param name="proxyType">Type of the proxy.</param> |
| | | 169 | | /// <param name="proxyHost">The proxy host.</param> |
| | | 170 | | /// <param name="proxyPort">The proxy port.</param> |
| | | 171 | | public PasswordConnectionInfo(string host, int port, string username, byte[] password, ProxyTypes proxyType, str |
| | 0 | 172 | | : this(host, port, username, password, proxyType, proxyHost, proxyPort, string.Empty, string.Empty) |
| | 0 | 173 | | { |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 178 | | /// </summary> |
| | | 179 | | /// <param name="host">Connection host.</param> |
| | | 180 | | /// <param name="port">The port.</param> |
| | | 181 | | /// <param name="username">Connection username.</param> |
| | | 182 | | /// <param name="password">Connection password.</param> |
| | | 183 | | /// <param name="proxyType">Type of the proxy.</param> |
| | | 184 | | /// <param name="proxyHost">The proxy host.</param> |
| | | 185 | | /// <param name="proxyPort">The proxy port.</param> |
| | | 186 | | /// <param name="proxyUsername">The proxy username.</param> |
| | | 187 | | public PasswordConnectionInfo(string host, int port, string username, byte[] password, ProxyTypes proxyType, str |
| | 0 | 188 | | : this(host, port, username, password, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty) |
| | 0 | 189 | | { |
| | 0 | 190 | | } |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 194 | | /// </summary> |
| | | 195 | | /// <param name="host">Connection host.</param> |
| | | 196 | | /// <param name="username">Connection username.</param> |
| | | 197 | | /// <param name="password">Connection password.</param> |
| | | 198 | | /// <param name="proxyType">Type of the proxy.</param> |
| | | 199 | | /// <param name="proxyHost">The proxy host.</param> |
| | | 200 | | /// <param name="proxyPort">The proxy port.</param> |
| | | 201 | | public PasswordConnectionInfo(string host, string username, byte[] password, ProxyTypes proxyType, string proxyH |
| | 0 | 202 | | : this(host, DefaultPort, username, password, proxyType, proxyHost, proxyPort, string.Empty, string.Empty) |
| | 0 | 203 | | { |
| | 0 | 204 | | } |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 208 | | /// </summary> |
| | | 209 | | /// <param name="host">Connection host.</param> |
| | | 210 | | /// <param name="username">Connection username.</param> |
| | | 211 | | /// <param name="password">Connection password.</param> |
| | | 212 | | /// <param name="proxyType">Type of the proxy.</param> |
| | | 213 | | /// <param name="proxyHost">The proxy host.</param> |
| | | 214 | | /// <param name="proxyPort">The proxy port.</param> |
| | | 215 | | /// <param name="proxyUsername">The proxy username.</param> |
| | | 216 | | public PasswordConnectionInfo(string host, string username, byte[] password, ProxyTypes proxyType, string proxyH |
| | 0 | 217 | | : this(host, DefaultPort, username, password, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty) |
| | 0 | 218 | | { |
| | 0 | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 223 | | /// </summary> |
| | | 224 | | /// <param name="host">Connection host.</param> |
| | | 225 | | /// <param name="username">Connection username.</param> |
| | | 226 | | /// <param name="password">Connection password.</param> |
| | | 227 | | /// <param name="proxyType">Type of the proxy.</param> |
| | | 228 | | /// <param name="proxyHost">The proxy host.</param> |
| | | 229 | | /// <param name="proxyPort">The proxy port.</param> |
| | | 230 | | /// <param name="proxyUsername">The proxy username.</param> |
| | | 231 | | /// <param name="proxyPassword">The proxy password.</param> |
| | | 232 | | public PasswordConnectionInfo(string host, string username, byte[] password, ProxyTypes proxyType, string proxyH |
| | 0 | 233 | | : this(host, DefaultPort, username, password, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword) |
| | 0 | 234 | | { |
| | 0 | 235 | | } |
| | | 236 | | |
| | | 237 | | /// <summary> |
| | | 238 | | /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 239 | | /// </summary> |
| | | 240 | | /// <param name="host">Connection host.</param> |
| | | 241 | | /// <param name="port">The port.</param> |
| | | 242 | | /// <param name="username">Connection username.</param> |
| | | 243 | | /// <param name="password">Connection password.</param> |
| | | 244 | | /// <param name="proxyType">Type of the proxy.</param> |
| | | 245 | | /// <param name="proxyHost">The proxy host.</param> |
| | | 246 | | /// <param name="proxyPort">The proxy port.</param> |
| | | 247 | | /// <param name="proxyUsername">The proxy username.</param> |
| | | 248 | | /// <param name="proxyPassword">The proxy password.</param> |
| | | 249 | | public PasswordConnectionInfo(string host, int port, string username, byte[] password, ProxyTypes proxyType, str |
| | 240 | 250 | | : base(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword, new PasswordAuth |
| | 225 | 251 | | { |
| | 1125 | 252 | | foreach (var authenticationMethod in AuthenticationMethods) |
| | 225 | 253 | | { |
| | 225 | 254 | | if (authenticationMethod is PasswordAuthenticationMethod pwdAuthentication) |
| | 225 | 255 | | { |
| | 225 | 256 | | pwdAuthentication.PasswordExpired += AuthenticationMethod_PasswordExpired; |
| | 225 | 257 | | } |
| | 225 | 258 | | } |
| | 225 | 259 | | } |
| | | 260 | | |
| | | 261 | | private void AuthenticationMethod_PasswordExpired(object sender, AuthenticationPasswordChangeEventArgs e) |
| | 0 | 262 | | { |
| | | 263 | | #pragma warning disable MA0091 // Sender should be 'this' for instance events |
| | 0 | 264 | | PasswordExpired?.Invoke(sender, e); |
| | | 265 | | #pragma warning restore MA0091 // Sender should be 'this' for instance events |
| | 0 | 266 | | } |
| | | 267 | | |
| | | 268 | | /// <summary> |
| | | 269 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 270 | | /// </summary> |
| | | 271 | | public void Dispose() |
| | 174 | 272 | | { |
| | 174 | 273 | | Dispose(disposing: true); |
| | 174 | 274 | | GC.SuppressFinalize(this); |
| | 174 | 275 | | } |
| | | 276 | | |
| | | 277 | | /// <summary> |
| | | 278 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 279 | | /// </summary> |
| | | 280 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 281 | | protected virtual void Dispose(bool disposing) |
| | 243 | 282 | | { |
| | 243 | 283 | | if (_isDisposed) |
| | 0 | 284 | | { |
| | 0 | 285 | | return; |
| | | 286 | | } |
| | | 287 | | |
| | 243 | 288 | | if (disposing) |
| | 174 | 289 | | { |
| | 174 | 290 | | if (AuthenticationMethods != null) |
| | 174 | 291 | | { |
| | 870 | 292 | | foreach (var authenticationMethod in AuthenticationMethods) |
| | 174 | 293 | | { |
| | 174 | 294 | | if (authenticationMethod is IDisposable disposable) |
| | 174 | 295 | | { |
| | 174 | 296 | | disposable.Dispose(); |
| | 174 | 297 | | } |
| | 174 | 298 | | } |
| | 174 | 299 | | } |
| | | 300 | | |
| | 174 | 301 | | _isDisposed = true; |
| | 174 | 302 | | } |
| | 243 | 303 | | } |
| | | 304 | | |
| | | 305 | | /// <summary> |
| | | 306 | | /// Finalizes an instance of the <see cref="PasswordConnectionInfo"/> class. |
| | | 307 | | /// </summary> |
| | | 308 | | ~PasswordConnectionInfo() |
| | 138 | 309 | | { |
| | 69 | 310 | | Dispose(disposing: false); |
| | 138 | 311 | | } |
| | | 312 | | } |
| | | 313 | | } |