User.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2024 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. public const int DEFAULT_ITERATIONS = 100000;
  36. string _displayName;
  37. string _username;
  38. UserPasswordHashType _passwordHashType;
  39. int _iterations;
  40. byte[] _salt;
  41. string _passwordHash;
  42. bool _disabled;
  43. int _sessionTimeoutSeconds = 30 * 60; //default 30 mins
  44. DateTime _previousSessionLoggedOn;
  45. IPAddress _previousSessionRemoteAddress;
  46. DateTime _recentSessionLoggedOn;
  47. IPAddress _recentSessionRemoteAddress;
  48. readonly ConcurrentDictionary<string, Group> _memberOfGroups;
  49. #endregion
  50. #region constructor
  51. public User(string displayName, string username, string password, int iterations = DEFAULT_ITERATIONS)
  52. {
  53. Username = username;
  54. DisplayName = displayName;
  55. ChangePassword(password, iterations);
  56. _previousSessionRemoteAddress = IPAddress.Any;
  57. _recentSessionRemoteAddress = IPAddress.Any;
  58. _memberOfGroups = new ConcurrentDictionary<string, Group>(1, 2);
  59. }
  60. public User(BinaryReader bR, AuthManager authManager)
  61. {
  62. switch (bR.ReadByte())
  63. {
  64. case 1:
  65. _displayName = bR.ReadShortString();
  66. _username = bR.ReadShortString();
  67. _passwordHashType = (UserPasswordHashType)bR.ReadByte();
  68. _iterations = bR.ReadInt32();
  69. _salt = bR.ReadBuffer();
  70. _passwordHash = bR.ReadShortString();
  71. _disabled = bR.ReadBoolean();
  72. _sessionTimeoutSeconds = bR.ReadInt32();
  73. _previousSessionLoggedOn = bR.ReadDateTime();
  74. _previousSessionRemoteAddress = IPAddressExtensions.ReadFrom(bR);
  75. _recentSessionLoggedOn = bR.ReadDateTime();
  76. _recentSessionRemoteAddress = IPAddressExtensions.ReadFrom(bR);
  77. {
  78. int count = bR.ReadByte();
  79. _memberOfGroups = new ConcurrentDictionary<string, Group>(1, count);
  80. for (int i = 0; i < count; i++)
  81. {
  82. Group group = authManager.GetGroup(bR.ReadShortString());
  83. if (group is not null)
  84. _memberOfGroups.TryAdd(group.Name.ToLower(), group);
  85. }
  86. }
  87. break;
  88. default:
  89. throw new InvalidDataException("Invalid data or version not supported.");
  90. }
  91. }
  92. #endregion
  93. #region internal
  94. internal void RenameGroup(string oldName)
  95. {
  96. if (_memberOfGroups.TryRemove(oldName.ToLower(), out Group renamedGroup))
  97. _memberOfGroups.TryAdd(renamedGroup.Name.ToLower(), renamedGroup);
  98. }
  99. #endregion
  100. #region public
  101. public string GetPasswordHashFor(string password)
  102. {
  103. switch (_passwordHashType)
  104. {
  105. case UserPasswordHashType.OldScheme:
  106. using (HMAC hmac = new HMACSHA256(Encoding.UTF8.GetBytes(password)))
  107. {
  108. return Convert.ToHexString(hmac.ComputeHash(Encoding.UTF8.GetBytes(_username))).ToLower();
  109. }
  110. case UserPasswordHashType.PBKDF2_SHA256:
  111. return Convert.ToHexString(Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), _salt, _iterations, HashAlgorithmName.SHA256, 32)).ToLower();
  112. default:
  113. throw new NotSupportedException();
  114. }
  115. }
  116. public void ChangePassword(string newPassword, int iterations = DEFAULT_ITERATIONS)
  117. {
  118. _passwordHashType = UserPasswordHashType.PBKDF2_SHA256;
  119. _iterations = iterations;
  120. _salt = new byte[32];
  121. RandomNumberGenerator.Fill(_salt);
  122. _passwordHash = GetPasswordHashFor(newPassword);
  123. }
  124. public void LoadOldSchemeCredentials(string passwordHash)
  125. {
  126. _passwordHashType = UserPasswordHashType.OldScheme;
  127. _passwordHash = passwordHash;
  128. }
  129. public void LoggedInFrom(IPAddress remoteAddress)
  130. {
  131. if (remoteAddress.IsIPv4MappedToIPv6)
  132. remoteAddress = remoteAddress.MapToIPv4();
  133. _previousSessionLoggedOn = _recentSessionLoggedOn;
  134. _previousSessionRemoteAddress = _recentSessionRemoteAddress;
  135. _recentSessionLoggedOn = DateTime.UtcNow;
  136. _recentSessionRemoteAddress = remoteAddress;
  137. }
  138. public void AddToGroup(Group group)
  139. {
  140. if (_memberOfGroups.Count == 255)
  141. throw new InvalidOperationException("Cannot add user to group: user can be member of max 255 groups.");
  142. _memberOfGroups.TryAdd(group.Name.ToLower(), group);
  143. }
  144. public bool RemoveFromGroup(Group group)
  145. {
  146. if (group.Name.Equals("everyone", StringComparison.OrdinalIgnoreCase))
  147. throw new InvalidOperationException("Access was denied.");
  148. return _memberOfGroups.TryRemove(group.Name.ToLower(), out _);
  149. }
  150. public void SyncGroups(IReadOnlyDictionary<string, Group> groups)
  151. {
  152. //remove non-existent groups
  153. foreach (KeyValuePair<string, Group> group in _memberOfGroups)
  154. {
  155. if (!groups.ContainsKey(group.Key))
  156. _memberOfGroups.TryRemove(group.Key, out _);
  157. }
  158. //set new groups
  159. foreach (KeyValuePair<string, Group> group in groups)
  160. _memberOfGroups[group.Key] = group.Value;
  161. }
  162. public bool IsMemberOfGroup(Group group)
  163. {
  164. return _memberOfGroups.ContainsKey(group.Name.ToLower());
  165. }
  166. public void WriteTo(BinaryWriter bW)
  167. {
  168. bW.Write((byte)1);
  169. bW.WriteShortString(_displayName);
  170. bW.WriteShortString(_username);
  171. bW.Write((byte)_passwordHashType);
  172. bW.Write(_iterations);
  173. bW.WriteBuffer(_salt);
  174. bW.WriteShortString(_passwordHash);
  175. bW.Write(_disabled);
  176. bW.Write(_sessionTimeoutSeconds);
  177. bW.Write(_previousSessionLoggedOn);
  178. IPAddressExtensions.WriteTo(_previousSessionRemoteAddress, bW);
  179. bW.Write(_recentSessionLoggedOn);
  180. IPAddressExtensions.WriteTo(_recentSessionRemoteAddress, bW);
  181. bW.Write(Convert.ToByte(_memberOfGroups.Count));
  182. foreach (KeyValuePair<string, Group> group in _memberOfGroups)
  183. bW.WriteShortString(group.Value.Name.ToLower());
  184. }
  185. public override bool Equals(object obj)
  186. {
  187. if (obj is not User other)
  188. return false;
  189. return _username.Equals(other._username, StringComparison.OrdinalIgnoreCase);
  190. }
  191. public override int GetHashCode()
  192. {
  193. return HashCode.Combine(_username);
  194. }
  195. public override string ToString()
  196. {
  197. return _username;
  198. }
  199. public int CompareTo(User other)
  200. {
  201. return _username.CompareTo(other._username);
  202. }
  203. #endregion
  204. #region properties
  205. public string DisplayName
  206. {
  207. get { return _displayName; }
  208. set
  209. {
  210. if (value.Length > 255)
  211. throw new ArgumentException("Display name length cannot exceed 255 characters.", nameof(DisplayName));
  212. _displayName = value;
  213. if (string.IsNullOrWhiteSpace(_displayName))
  214. _displayName = _username;
  215. }
  216. }
  217. public string Username
  218. {
  219. get { return _username; }
  220. set
  221. {
  222. if (_passwordHashType == UserPasswordHashType.OldScheme)
  223. throw new InvalidOperationException("Cannot change username when using old password hash scheme. Change password once and try again.");
  224. if (string.IsNullOrWhiteSpace(value))
  225. throw new ArgumentException("Username cannot be null or empty.", nameof(Username));
  226. if (value.Length > 255)
  227. throw new ArgumentException("Username length cannot exceed 255 characters.", nameof(Username));
  228. foreach (char c in value)
  229. {
  230. if ((c >= 97) && (c <= 122)) //[a-z]
  231. continue;
  232. if ((c >= 65) && (c <= 90)) //[A-Z]
  233. continue;
  234. if ((c >= 48) && (c <= 57)) //[0-9]
  235. continue;
  236. if (c == '-')
  237. continue;
  238. if (c == '_')
  239. continue;
  240. if (c == '.')
  241. continue;
  242. throw new ArgumentException("Username can contain only alpha numeric, '-', '_', or '.' characters.", nameof(Username));
  243. }
  244. _username = value.ToLower();
  245. }
  246. }
  247. public UserPasswordHashType PasswordHashType
  248. { get { return _passwordHashType; } }
  249. public string PasswordHash
  250. { get { return _passwordHash; } }
  251. public bool Disabled
  252. {
  253. get { return _disabled; }
  254. set { _disabled = value; }
  255. }
  256. public int SessionTimeoutSeconds
  257. {
  258. get { return _sessionTimeoutSeconds; }
  259. set
  260. {
  261. if ((value < 0) || (value > 604800))
  262. throw new ArgumentOutOfRangeException(nameof(SessionTimeoutSeconds), "Session timeout value must be between 0-604800 seconds.");
  263. if ((value > 0) && (value < 60))
  264. value = 60; //to prevent issues with too low timeout set by mistake
  265. _sessionTimeoutSeconds = value;
  266. }
  267. }
  268. public DateTime PreviousSessionLoggedOn
  269. { get { return _previousSessionLoggedOn; } }
  270. public IPAddress PreviousSessionRemoteAddress
  271. { get { return _previousSessionRemoteAddress; } }
  272. public DateTime RecentSessionLoggedOn
  273. { get { return _recentSessionLoggedOn; } }
  274. public IPAddress RecentSessionRemoteAddress
  275. { get { return _recentSessionRemoteAddress; } }
  276. public ICollection<Group> MemberOfGroups
  277. { get { return _memberOfGroups.Values; } }
  278. #endregion
  279. }
  280. }