PasswordConnectionInfo.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Renci.SshNet.Common;
  6. namespace Renci.SshNet
  7. {
  8. /// <summary>
  9. /// Provides connection information when password authentication method is used
  10. /// </summary>
  11. public class PasswordConnectionInfo : ConnectionInfo, IDisposable
  12. {
  13. /// <summary>
  14. /// Occurs when user's password has expired and needs to be changed.
  15. /// </summary>
  16. public event EventHandler<AuthenticationPasswordChangeEventArgs> PasswordExpired;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  19. /// </summary>
  20. /// <param name="host">Connection host.</param>
  21. /// <param name="username">Connection username.</param>
  22. /// <param name="password">Connection password.</param>
  23. public PasswordConnectionInfo(string host, string username, string password)
  24. : this(host, 22, username, Encoding.UTF8.GetBytes(password))
  25. {
  26. }
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  29. /// </summary>
  30. /// <param name="host">Connection host.</param>
  31. /// <param name="port">Connection port.</param>
  32. /// <param name="username">Connection username.</param>
  33. /// <param name="password">Connection password.</param>
  34. /// <exception cref="ArgumentNullException"><paramref name="password"/> is null.</exception>
  35. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is null or contains whitespace characters.</exception>
  36. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="f:IPEndPoint.MinPort"/> and <see cref="f:IPEndPoint.MaxPort"/>.</exception>
  37. public PasswordConnectionInfo(string host, int port, string username, string password)
  38. : this(host, port, username, Encoding.UTF8.GetBytes(password), ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty)
  39. {
  40. }
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  43. /// </summary>
  44. /// <param name="host">Connection host.</param>
  45. /// <param name="port">The port.</param>
  46. /// <param name="username">Connection username.</param>
  47. /// <param name="password">Connection password.</param>
  48. /// <param name="proxyType">Type of the proxy.</param>
  49. /// <param name="proxyHost">The proxy host.</param>
  50. /// <param name="proxyPort">The proxy port.</param>
  51. public PasswordConnectionInfo(string host, int port, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort)
  52. : this(host, port, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  53. {
  54. }
  55. /// <summary>
  56. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  57. /// </summary>
  58. /// <param name="host">Connection host.</param>
  59. /// <param name="port">The port.</param>
  60. /// <param name="username">Connection username.</param>
  61. /// <param name="password">Connection password.</param>
  62. /// <param name="proxyType">Type of the proxy.</param>
  63. /// <param name="proxyHost">The proxy host.</param>
  64. /// <param name="proxyPort">The proxy port.</param>
  65. /// <param name="proxyUsername">The proxy username.</param>
  66. public PasswordConnectionInfo(string host, int port, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  67. : this(host, port, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  68. {
  69. }
  70. /// <summary>
  71. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  72. /// </summary>
  73. /// <param name="host">Connection host.</param>
  74. /// <param name="username">Connection username.</param>
  75. /// <param name="password">Connection password.</param>
  76. /// <param name="proxyType">Type of the proxy.</param>
  77. /// <param name="proxyHost">The proxy host.</param>
  78. /// <param name="proxyPort">The proxy port.</param>
  79. public PasswordConnectionInfo(string host, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort)
  80. : this(host, 22, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  81. {
  82. }
  83. /// <summary>
  84. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  85. /// </summary>
  86. /// <param name="host">Connection host.</param>
  87. /// <param name="username">Connection username.</param>
  88. /// <param name="password">Connection password.</param>
  89. /// <param name="proxyType">Type of the proxy.</param>
  90. /// <param name="proxyHost">The proxy host.</param>
  91. /// <param name="proxyPort">The proxy port.</param>
  92. /// <param name="proxyUsername">The proxy username.</param>
  93. public PasswordConnectionInfo(string host, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  94. : this(host, 22, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  95. {
  96. }
  97. /// <summary>
  98. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  99. /// </summary>
  100. /// <param name="host">Connection host.</param>
  101. /// <param name="username">Connection username.</param>
  102. /// <param name="password">Connection password.</param>
  103. /// <param name="proxyType">Type of the proxy.</param>
  104. /// <param name="proxyHost">The proxy host.</param>
  105. /// <param name="proxyPort">The proxy port.</param>
  106. /// <param name="proxyUsername">The proxy username.</param>
  107. /// <param name="proxyPassword">The proxy password.</param>
  108. public PasswordConnectionInfo(string host, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  109. : this(host, 22, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword)
  110. {
  111. }
  112. /// <summary>
  113. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  114. /// </summary>
  115. /// <param name="host">Connection host.</param>
  116. /// <param name="username">Connection username.</param>
  117. /// <param name="password">Connection password.</param>
  118. public PasswordConnectionInfo(string host, string username, byte[] password)
  119. : this(host, 22, username, password)
  120. {
  121. }
  122. /// <summary>
  123. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  124. /// </summary>
  125. /// <param name="host">Connection host.</param>
  126. /// <param name="port">Connection port.</param>
  127. /// <param name="username">Connection username.</param>
  128. /// <param name="password">Connection password.</param>
  129. /// <exception cref="ArgumentNullException"><paramref name="password"/> is null.</exception>
  130. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is null or contains whitespace characters.</exception>
  131. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="f:IPEndPoint.MinPort"/> and <see cref="f:IPEndPoint.MaxPort"/>.</exception>
  132. public PasswordConnectionInfo(string host, int port, string username, byte[] password)
  133. : this(host, port, username, password, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty)
  134. {
  135. }
  136. /// <summary>
  137. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  138. /// </summary>
  139. /// <param name="host">Connection host.</param>
  140. /// <param name="port">The port.</param>
  141. /// <param name="username">Connection username.</param>
  142. /// <param name="password">Connection password.</param>
  143. /// <param name="proxyType">Type of the proxy.</param>
  144. /// <param name="proxyHost">The proxy host.</param>
  145. /// <param name="proxyPort">The proxy port.</param>
  146. public PasswordConnectionInfo(string host, int port, string username, byte[] password, ProxyTypes proxyType, string proxyHost, int proxyPort)
  147. : this(host, port, username, password, proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  148. {
  149. }
  150. /// <summary>
  151. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  152. /// </summary>
  153. /// <param name="host">Connection host.</param>
  154. /// <param name="port">The port.</param>
  155. /// <param name="username">Connection username.</param>
  156. /// <param name="password">Connection password.</param>
  157. /// <param name="proxyType">Type of the proxy.</param>
  158. /// <param name="proxyHost">The proxy host.</param>
  159. /// <param name="proxyPort">The proxy port.</param>
  160. /// <param name="proxyUsername">The proxy username.</param>
  161. public PasswordConnectionInfo(string host, int port, string username, byte[] password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  162. : this(host, port, username, password, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  163. {
  164. }
  165. /// <summary>
  166. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  167. /// </summary>
  168. /// <param name="host">Connection host.</param>
  169. /// <param name="username">Connection username.</param>
  170. /// <param name="password">Connection password.</param>
  171. /// <param name="proxyType">Type of the proxy.</param>
  172. /// <param name="proxyHost">The proxy host.</param>
  173. /// <param name="proxyPort">The proxy port.</param>
  174. public PasswordConnectionInfo(string host, string username, byte[] password, ProxyTypes proxyType, string proxyHost, int proxyPort)
  175. : this(host, 22, username, password, proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  176. {
  177. }
  178. /// <summary>
  179. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  180. /// </summary>
  181. /// <param name="host">Connection host.</param>
  182. /// <param name="username">Connection username.</param>
  183. /// <param name="password">Connection password.</param>
  184. /// <param name="proxyType">Type of the proxy.</param>
  185. /// <param name="proxyHost">The proxy host.</param>
  186. /// <param name="proxyPort">The proxy port.</param>
  187. /// <param name="proxyUsername">The proxy username.</param>
  188. public PasswordConnectionInfo(string host, string username, byte[] password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  189. : this(host, 22, username, password, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  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. /// <param name="proxyUsername">The proxy username.</param>
  202. /// <param name="proxyPassword">The proxy password.</param>
  203. public PasswordConnectionInfo(string host, string username, byte[] password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  204. : this(host, 22, username, password, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword)
  205. {
  206. }
  207. /// <summary>
  208. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  209. /// </summary>
  210. /// <param name="host">Connection host.</param>
  211. /// <param name="port">The port.</param>
  212. /// <param name="username">Connection username.</param>
  213. /// <param name="password">Connection password.</param>
  214. /// <param name="proxyType">Type of the proxy.</param>
  215. /// <param name="proxyHost">The proxy host.</param>
  216. /// <param name="proxyPort">The proxy port.</param>
  217. /// <param name="proxyUsername">The proxy username.</param>
  218. /// <param name="proxyPassword">The proxy password.</param>
  219. public PasswordConnectionInfo(string host, int port, string username, byte[] password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  220. : base(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword, new PasswordAuthenticationMethod(username, password))
  221. {
  222. foreach (var authenticationMethod in this.AuthenticationMethods.OfType<PasswordAuthenticationMethod>())
  223. {
  224. authenticationMethod.PasswordExpired += AuthenticationMethod_PasswordExpired;
  225. }
  226. }
  227. private void AuthenticationMethod_PasswordExpired(object sender, AuthenticationPasswordChangeEventArgs e)
  228. {
  229. if (this.PasswordExpired != null)
  230. {
  231. this.PasswordExpired(sender, e);
  232. }
  233. }
  234. #region IDisposable Members
  235. private bool isDisposed = false;
  236. /// <summary>
  237. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  238. /// </summary>
  239. public void Dispose()
  240. {
  241. Dispose(true);
  242. GC.SuppressFinalize(this);
  243. }
  244. /// <summary>
  245. /// Releases unmanaged and - optionally - managed resources
  246. /// </summary>
  247. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  248. protected virtual void Dispose(bool disposing)
  249. {
  250. // Check to see if Dispose has already been called.
  251. if (!this.isDisposed)
  252. {
  253. // If disposing equals true, dispose all managed
  254. // and unmanaged resources.
  255. if (disposing)
  256. {
  257. // Dispose managed resources.
  258. if (this.AuthenticationMethods != null)
  259. {
  260. foreach (var authenticationMethods in this.AuthenticationMethods.OfType<IDisposable>())
  261. {
  262. authenticationMethods.Dispose();
  263. }
  264. }
  265. }
  266. // Note disposing has been done.
  267. isDisposed = true;
  268. }
  269. }
  270. /// <summary>
  271. /// Releases unmanaged resources and performs other cleanup operations before the
  272. /// <see cref="PasswordConnectionInfo"/> is reclaimed by garbage collection.
  273. /// </summary>
  274. ~PasswordConnectionInfo()
  275. {
  276. // Do not re-create Dispose clean-up code here.
  277. // Calling Dispose(false) is optimal in terms of
  278. // readability and maintainability.
  279. Dispose(false);
  280. }
  281. #endregion
  282. }
  283. }