PasswordConnectionInfo.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /// <example>
  12. /// <code source="..\..\Renci.SshNet.Tests\Classes\PasswordConnectionInfoTest.cs" region="Example PasswordConnectionInfo" language="C#" title="Connect using username and password" />
  13. /// <code source="..\..\Renci.SshNet.Tests\Classes\PasswordConnectionInfoTest.cs" region="Example PasswordConnectionInfo PasswordExpired" language="C#" title="Change password when connecting" />
  14. /// <code source="..\..\Renci.SshNet.Tests\Classes\PasswordConnectionInfoTest.cs" region="Example PasswordConnectionInfo AuthenticationBanner" language="C#" title="Display authentication banner" />
  15. /// </example>
  16. public class PasswordConnectionInfo : ConnectionInfo, IDisposable
  17. {
  18. /// <summary>
  19. /// Occurs when user's password has expired and needs to be changed.
  20. /// </summary>
  21. /// <example>
  22. /// <code source="..\..\Renci.SshNet.Tests\Classes\PasswordConnectionInfoTest.cs" region="Example PasswordConnectionInfo PasswordExpired" language="C#" title="Change password when connecting" />
  23. /// </example>
  24. public event EventHandler<AuthenticationPasswordChangeEventArgs> PasswordExpired;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="PasswordConnectionInfo" /> class.
  27. /// </summary>
  28. /// <param name="host">Connection host.</param>
  29. /// <param name="username">Connection username.</param>
  30. /// <param name="password">Connection password.</param>
  31. /// <example>
  32. /// <code source="..\..\Renci.SshNet.Tests\Classes\PasswordConnectionInfoTest.cs" region="Example PasswordConnectionInfo" language="C#" title="Connect using username and password" />
  33. /// </example>
  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:System.Net.IPEndPoint.MinPort"/> and <see cref="F:System.Net.IPEndPoint.MaxPort"/>.</exception>
  37. public PasswordConnectionInfo(string host, string username, string password)
  38. : this(host, ConnectionInfo.DEFAULT_PORT, username, Encoding.UTF8.GetBytes(password))
  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">Connection port.</param>
  46. /// <param name="username">Connection username.</param>
  47. /// <param name="password">Connection password.</param>
  48. /// <exception cref="ArgumentNullException"><paramref name="password"/> is null.</exception>
  49. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is null or contains whitespace characters.</exception>
  50. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="F:System.Net.IPEndPoint.MinPort"/> and <see cref="F:System.Net.IPEndPoint.MaxPort"/>.</exception>
  51. public PasswordConnectionInfo(string host, int port, string username, string password)
  52. : this(host, port, username, Encoding.UTF8.GetBytes(password), ProxyTypes.None, string.Empty, 0, 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. public PasswordConnectionInfo(string host, int port, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort)
  66. : this(host, port, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  67. {
  68. }
  69. /// <summary>
  70. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  71. /// </summary>
  72. /// <param name="host">Connection host.</param>
  73. /// <param name="port">The port.</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. /// <param name="proxyUsername">The proxy username.</param>
  80. public PasswordConnectionInfo(string host, int port, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  81. : this(host, port, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  82. {
  83. }
  84. /// <summary>
  85. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  86. /// </summary>
  87. /// <param name="host">Connection host.</param>
  88. /// <param name="username">Connection username.</param>
  89. /// <param name="password">Connection password.</param>
  90. /// <param name="proxyType">Type of the proxy.</param>
  91. /// <param name="proxyHost">The proxy host.</param>
  92. /// <param name="proxyPort">The proxy port.</param>
  93. public PasswordConnectionInfo(string host, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort)
  94. : this(host, ConnectionInfo.DEFAULT_PORT, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, string.Empty, 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. public PasswordConnectionInfo(string host, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  108. : this(host, ConnectionInfo.DEFAULT_PORT, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  109. {
  110. }
  111. /// <summary>
  112. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  113. /// </summary>
  114. /// <param name="host">Connection host.</param>
  115. /// <param name="username">Connection username.</param>
  116. /// <param name="password">Connection password.</param>
  117. /// <param name="proxyType">Type of the proxy.</param>
  118. /// <param name="proxyHost">The proxy host.</param>
  119. /// <param name="proxyPort">The proxy port.</param>
  120. /// <param name="proxyUsername">The proxy username.</param>
  121. /// <param name="proxyPassword">The proxy password.</param>
  122. public PasswordConnectionInfo(string host, string username, string password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  123. : this(host, ConnectionInfo.DEFAULT_PORT, username, Encoding.UTF8.GetBytes(password), proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword)
  124. {
  125. }
  126. /// <summary>
  127. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  128. /// </summary>
  129. /// <param name="host">Connection host.</param>
  130. /// <param name="username">Connection username.</param>
  131. /// <param name="password">Connection password.</param>
  132. public PasswordConnectionInfo(string host, string username, byte[] password)
  133. : this(host, ConnectionInfo.DEFAULT_PORT, username, password)
  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">Connection port.</param>
  141. /// <param name="username">Connection username.</param>
  142. /// <param name="password">Connection password.</param>
  143. /// <exception cref="ArgumentNullException"><paramref name="password" /> is null.</exception>
  144. /// <exception cref="ArgumentException"><paramref name="host" /> is invalid, or <paramref name="username" /> is null or contains whitespace characters.</exception>
  145. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  146. public PasswordConnectionInfo(string host, int port, string username, byte[] password)
  147. : this(host, port, username, password, ProxyTypes.None, string.Empty, 0, 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. public PasswordConnectionInfo(string host, int port, string username, byte[] password, ProxyTypes proxyType, string proxyHost, int proxyPort)
  161. : this(host, port, username, password, proxyType, proxyHost, proxyPort, string.Empty, string.Empty)
  162. {
  163. }
  164. /// <summary>
  165. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  166. /// </summary>
  167. /// <param name="host">Connection host.</param>
  168. /// <param name="port">The port.</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. /// <param name="proxyUsername">The proxy username.</param>
  175. public PasswordConnectionInfo(string host, int port, string username, byte[] password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  176. : this(host, port, username, password, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  177. {
  178. }
  179. /// <summary>
  180. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  181. /// </summary>
  182. /// <param name="host">Connection host.</param>
  183. /// <param name="username">Connection username.</param>
  184. /// <param name="password">Connection password.</param>
  185. /// <param name="proxyType">Type of the proxy.</param>
  186. /// <param name="proxyHost">The proxy host.</param>
  187. /// <param name="proxyPort">The proxy port.</param>
  188. public PasswordConnectionInfo(string host, string username, byte[] password, ProxyTypes proxyType, string proxyHost, int proxyPort)
  189. : this(host, ConnectionInfo.DEFAULT_PORT, username, password, proxyType, proxyHost, proxyPort, string.Empty, 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. public PasswordConnectionInfo(string host, string username, byte[] password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername)
  203. : this(host, ConnectionInfo.DEFAULT_PORT, username, password, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty)
  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. /// <param name="proxyPassword">The proxy password.</param>
  217. public PasswordConnectionInfo(string host, string username, byte[] password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  218. : this(host, ConnectionInfo.DEFAULT_PORT, username, password, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword)
  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="port">The port.</param>
  226. /// <param name="username">Connection username.</param>
  227. /// <param name="password">Connection password.</param>
  228. /// <param name="proxyType">Type of the proxy.</param>
  229. /// <param name="proxyHost">The proxy host.</param>
  230. /// <param name="proxyPort">The proxy port.</param>
  231. /// <param name="proxyUsername">The proxy username.</param>
  232. /// <param name="proxyPassword">The proxy password.</param>
  233. public PasswordConnectionInfo(string host, int port, string username, byte[] password, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  234. : base(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword, new PasswordAuthenticationMethod(username, password))
  235. {
  236. foreach (var authenticationMethod in this.AuthenticationMethods.OfType<PasswordAuthenticationMethod>())
  237. {
  238. authenticationMethod.PasswordExpired += AuthenticationMethod_PasswordExpired;
  239. }
  240. }
  241. private void AuthenticationMethod_PasswordExpired(object sender, AuthenticationPasswordChangeEventArgs e)
  242. {
  243. if (this.PasswordExpired != null)
  244. {
  245. this.PasswordExpired(sender, e);
  246. }
  247. }
  248. #region IDisposable Members
  249. private bool isDisposed = false;
  250. /// <summary>
  251. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  252. /// </summary>
  253. public void Dispose()
  254. {
  255. Dispose(true);
  256. GC.SuppressFinalize(this);
  257. }
  258. /// <summary>
  259. /// Releases unmanaged and - optionally - managed resources
  260. /// </summary>
  261. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  262. protected virtual void Dispose(bool disposing)
  263. {
  264. // Check to see if Dispose has already been called.
  265. if (!this.isDisposed)
  266. {
  267. // If disposing equals true, dispose all managed
  268. // and unmanaged resources.
  269. if (disposing)
  270. {
  271. // Dispose managed resources.
  272. if (this.AuthenticationMethods != null)
  273. {
  274. foreach (var authenticationMethods in this.AuthenticationMethods.OfType<IDisposable>())
  275. {
  276. authenticationMethods.Dispose();
  277. }
  278. }
  279. }
  280. // Note disposing has been done.
  281. isDisposed = true;
  282. }
  283. }
  284. /// <summary>
  285. /// Releases unmanaged resources and performs other cleanup operations before the
  286. /// <see cref="PasswordConnectionInfo"/> is reclaimed by garbage collection.
  287. /// </summary>
  288. ~PasswordConnectionInfo()
  289. {
  290. // Do not re-create Dispose clean-up code here.
  291. // Calling Dispose(false) is optimal in terms of
  292. // readability and maintainability.
  293. Dispose(false);
  294. }
  295. #endregion
  296. }
  297. }