PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/Renci.SshNet/PasswordConnectionInfo.cs

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