User.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2023 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.Collections.Concurrent;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Net;
  20. using System.Security.Cryptography;
  21. using System.Text;
  22. using TechnitiumLibrary.IO;
  23. using TechnitiumLibrary.Net;
  24. namespace DnsServerCore.Auth
  25. {
  26. enum UserPasswordHashType : byte
  27. {
  28. Unknown = 0,
  29. OldScheme = 1,
  30. PBKDF2_SHA256 = 2
  31. }
  32. class User : IComparable<User>
  33. {
  34. #region variables
  35. static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();
  36. public const int DEFAULT_ITERATIONS = 100000;
  37. string _displayName;
  38. string _username;
  39. UserPasswordHashType _passwordHashType;
  40. int _iterations;
  41. byte[] _salt;
  42. string _passwordHash;
  43. bool _disabled;
  44. int _sessionTimeoutSeconds = 30 * 60; //default 30 mins
  45. DateTime _previousSessionLoggedOn;
  46. IPAddress _previousSessionRemoteAddress;
  47. DateTime _recentSessionLoggedOn;
  48. IPAddress _recentSessionRemoteAddress;
  49. readonly ConcurrentDictionary<string, Group> _memberOfGroups;
  50. #endregion
  51. #region constructor
  52. public User(string displayName, string username, string password, int iterations = DEFAULT_ITERATIONS)
  53. {
  54. Username = username;
  55. DisplayName = displayName;
  56. ChangePassword(password, iterations);
  57. _previousSessionRemoteAddress = IPAddress.Any;
  58. _recentSessionRemoteAddress = IPAddress.Any;
  59. _memberOfGroups = new ConcurrentDictionary<string, Group>(1, 2);
  60. }
  61. public User(BinaryReader bR, AuthManager authManager)
  62. {
  63. switch (bR.ReadByte())
  64. {
  65. case 1:
  66. _displayName = bR.ReadShortString();
  67. _username = bR.ReadShortString();
  68. _passwordHashType = (UserPasswordHashType)bR.ReadByte();
  69. _iterations = bR.ReadInt32();
  70. _salt = bR.ReadBuffer();
  71. _passwordHash = bR.ReadShortString();
  72. _disabled = bR.ReadBoolean();
  73. _sessionTimeoutSeconds = bR.ReadInt32();
  74. _previousSessionLoggedOn = bR.ReadDateTime();
  75. _previousSessionRemoteAddress = IPAddressExtensions.ReadFrom(bR);
  76. _recentSessionLoggedOn = bR.ReadDateTime();
  77. _recentSessionRemoteAddress = IPAddressExtensions.ReadFrom(bR);
  78. {
  79. int count = bR.ReadByte();
  80. _memberOfGroups = new ConcurrentDictionary<string, Group>(1, count);
  81. for (int i = 0; i < count; i++)
  82. {
  83. Group group = authManager.GetGroup(bR.ReadShortString());
  84. if (group is not null)
  85. _memberOfGroups.TryAdd(group.Name.ToLower(), group);
  86. }
  87. }
  88. break;
  89. default:
  90. throw new InvalidDataException("Invalid data or version not supported.");
  91. }
  92. }
  93. #endregion
  94. #region internal
  95. internal void RenameGroup(string oldName)
  96. {
  97. if (_memberOfGroups.TryRemove(oldName.ToLower(), out Group renamedGroup))
  98. _memberOfGroups.TryAdd(renamedGroup.Name.ToLower(), renamedGroup);
  99. }
  100. #endregion
  101. #region public
  102. public string GetPasswordHashFor(string password)
  103. {
  104. switch (_passwordHashType)
  105. {
  106. case UserPasswordHashType.OldScheme:
  107. using (HMAC hmac = new HMACSHA256(Encoding.UTF8.GetBytes(password)))
  108. {
  109. return Convert.ToHexString(hmac.ComputeHash(Encoding.UTF8.GetBytes(_username))).ToLower();
  110. }
  111. case UserPasswordHashType.PBKDF2_SHA256:
  112. return Convert.ToHexString(Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), _salt, _iterations, HashAlgorithmName.SHA256, 32)).ToLower();
  113. default:
  114. throw new NotSupportedException();
  115. }
  116. }
  117. public void ChangePassword(string newPassword, int iterations = DEFAULT_ITERATIONS)
  118. {
  119. _passwordHashType = UserPasswordHashType.PBKDF2_SHA256;
  120. _iterations = iterations;
  121. _salt = new byte[32];
  122. _rng.GetBytes(_salt);
  123. _passwordHash = GetPasswordHashFor(newPassword);
  124. }
  125. public void LoadOldSchemeCredentials(string passwordHash)
  126. {
  127. _passwordHashType = UserPasswordHashType.OldScheme;
  128. _passwordHash = passwordHash;
  129. }
  130. public void LoggedInFrom(IPAddress remoteAddress)
  131. {
  132. if (remoteAddress.IsIPv4MappedToIPv6)
  133. remoteAddress = remoteAddress.MapToIPv4();
  134. _previousSessionLoggedOn = _recentSessionLoggedOn;
  135. _previousSessionRemoteAddress = _recentSessionRemoteAddress;
  136. _recentSessionLoggedOn = DateTime.UtcNow;
  137. _recentSessionRemoteAddress = remoteAddress;
  138. }
  139. public void AddToGroup(Group group)
  140. {
  141. if (_memberOfGroups.Count == 255)
  142. throw new InvalidOperationException("Cannot add user to group: user can be member of max 255 groups.");
  143. _memberOfGroups.TryAdd(group.Name.ToLower(), group);
  144. }
  145. public bool RemoveFromGroup(Group group)
  146. {
  147. if (group.Name.Equals("everyone", StringComparison.OrdinalIgnoreCase))
  148. throw new InvalidOperationException("Access was denied.");
  149. return _memberOfGroups.TryRemove(group.Name.ToLower(), out _);
  150. }
  151. public void SyncGroups(IReadOnlyDictionary<string, Group> groups)
  152. {
  153. //remove non-existent groups
  154. foreach (KeyValuePair<string, Group> group in _memberOfGroups)
  155. {
  156. if (!groups.ContainsKey(group.Key))
  157. _memberOfGroups.TryRemove(group.Key, out _);
  158. }
  159. //set new groups
  160. foreach (KeyValuePair<string, Group> group in groups)
  161. _memberOfGroups[group.Key] = group.Value;
  162. }
  163. public bool IsMemberOfGroup(Group group)
  164. {
  165. return _memberOfGroups.ContainsKey(group.Name.ToLower());
  166. }
  167. public void WriteTo(BinaryWriter bW)
  168. {
  169. bW.Write((byte)1);
  170. bW.WriteShortString(_displayName);
  171. bW.WriteShortString(_username);
  172. bW.Write((byte)_passwordHashType);
  173. bW.Write(_iterations);
  174. bW.WriteBuffer(_salt);
  175. bW.WriteShortString(_passwordHash);
  176. bW.Write(_disabled);
  177. bW.Write(_sessionTimeoutSeconds);
  178. bW.Write(_previousSessionLoggedOn);
  179. IPAddressExtensions.WriteTo(_previousSessionRemoteAddress, bW);
  180. bW.Write(_recentSessionLoggedOn);
  181. IPAddressExtensions.WriteTo(_recentSessionRemoteAddress, bW);
  182. bW.Write(Convert.ToByte(_memberOfGroups.Count));
  183. foreach (KeyValuePair<string, Group> group in _memberOfGroups)
  184. bW.WriteShortString(group.Value.Name.ToLower());
  185. }
  186. public override bool Equals(object obj)
  187. {
  188. if (obj is not User other)
  189. return false;
  190. return _username.Equals(other._username, StringComparison.OrdinalIgnoreCase);
  191. }
  192. public override int GetHashCode()
  193. {
  194. return base.GetHashCode();
  195. }
  196. public override string ToString()
  197. {
  198. return _username;
  199. }
  200. public int CompareTo(User other)
  201. {
  202. return _username.CompareTo(other._username);
  203. }
  204. #endregion
  205. #region properties
  206. public string DisplayName
  207. {
  208. get { return _displayName; }
  209. set
  210. {
  211. if (value.Length > 255)
  212. throw new ArgumentException("Display name length cannot exceed 255 characters.", nameof(DisplayName));
  213. _displayName = value;
  214. if (string.IsNullOrWhiteSpace(_displayName))
  215. _displayName = _username;
  216. }
  217. }
  218. public string Username
  219. {
  220. get { return _username; }
  221. set
  222. {
  223. if (_passwordHashType == UserPasswordHashType.OldScheme)
  224. throw new InvalidOperationException("Cannot change username when using old password hash scheme. Change password once and try again.");
  225. if (string.IsNullOrWhiteSpace(value))
  226. throw new ArgumentException("Username cannot be null or empty.", nameof(Username));
  227. if (value.Length > 255)
  228. throw new ArgumentException("Username length cannot exceed 255 characters.", nameof(Username));
  229. foreach (char c in value)
  230. {
  231. if ((c >= 97) && (c <= 122)) //[a-z]
  232. continue;
  233. if ((c >= 65) && (c <= 90)) //[A-Z]
  234. continue;
  235. if ((c >= 48) && (c <= 57)) //[0-9]
  236. continue;
  237. if (c == '-')
  238. continue;
  239. if (c == '_')
  240. continue;
  241. if (c == '.')
  242. continue;
  243. throw new ArgumentException("Username can contain only alpha numeric, '-', '_', or '.' characters.", nameof(Username));
  244. }
  245. _username = value.ToLower();
  246. }
  247. }
  248. public UserPasswordHashType PasswordHashType
  249. { get { return _passwordHashType; } }
  250. public string PasswordHash
  251. { get { return _passwordHash; } }
  252. public bool Disabled
  253. {
  254. get { return _disabled; }
  255. set { _disabled = value; }
  256. }
  257. public int SessionTimeoutSeconds
  258. {
  259. get { return _sessionTimeoutSeconds; }
  260. set
  261. {
  262. if ((value < 0) || (value > 604800))
  263. throw new ArgumentOutOfRangeException(nameof(SessionTimeoutSeconds), "Session timeout value must be between 0-604800 seconds.");
  264. if ((value > 0) && (value < 60))
  265. value = 60; //to prevent issues with too low timeout set by mistake
  266. _sessionTimeoutSeconds = value;
  267. }
  268. }
  269. public DateTime PreviousSessionLoggedOn
  270. { get { return _previousSessionLoggedOn; } }
  271. public IPAddress PreviousSessionRemoteAddress
  272. { get { return _previousSessionRemoteAddress; } }
  273. public DateTime RecentSessionLoggedOn
  274. { get { return _recentSessionLoggedOn; } }
  275. public IPAddress RecentSessionRemoteAddress
  276. { get { return _recentSessionRemoteAddress; } }
  277. public ICollection<Group> MemberOfGroups
  278. { get { return _memberOfGroups.Values; } }
  279. #endregion
  280. }
  281. }