UserManagerBase.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Net;
  30. using System.Reflection;
  31. using System.Security.Cryptography;
  32. using log4net;
  33. using Nwc.XmlRpc;
  34. using OpenMetaverse;
  35. using OpenMetaverse.StructuredData;
  36. using OpenSim.Data;
  37. using OpenSim.Framework.Communications;
  38. using OpenSim.Framework.Statistics;
  39. using OpenSim.Services.Interfaces;
  40. namespace OpenSim.Framework.Communications
  41. {
  42. /// <summary>
  43. /// Base class for user management (create, read, etc)
  44. /// </summary>
  45. public abstract class UserManagerBase
  46. : IUserService, IUserAdminService, IAvatarService, IMessagingService, IAuthentication
  47. {
  48. private static readonly ILog m_log
  49. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. /// <value>
  51. /// List of plugins to search for user data
  52. /// </value>
  53. private List<IUserDataPlugin> m_plugins = new List<IUserDataPlugin>();
  54. protected CommunicationsManager m_commsManager;
  55. protected IInventoryService m_InventoryService;
  56. /// <summary>
  57. /// Constructor
  58. /// </summary>
  59. /// <param name="commsManager"></param>
  60. public UserManagerBase(CommunicationsManager commsManager)
  61. {
  62. m_commsManager = commsManager;
  63. }
  64. public virtual void SetInventoryService(IInventoryService invService)
  65. {
  66. m_InventoryService = invService;
  67. }
  68. /// <summary>
  69. /// Add a new user data plugin - plugins will be requested in the order they were added.
  70. /// </summary>
  71. /// <param name="plugin">The plugin that will provide user data</param>
  72. public void AddPlugin(IUserDataPlugin plugin)
  73. {
  74. m_plugins.Add(plugin);
  75. }
  76. /// <summary>
  77. /// Adds a list of user data plugins, as described by `provider' and
  78. /// `connect', to `_plugins'.
  79. /// </summary>
  80. /// <param name="provider">
  81. /// The filename of the inventory server plugin DLL.
  82. /// </param>
  83. /// <param name="connect">
  84. /// The connection string for the storage backend.
  85. /// </param>
  86. public void AddPlugin(string provider, string connect)
  87. {
  88. m_plugins.AddRange(DataPluginFactory.LoadDataPlugins<IUserDataPlugin>(provider, connect));
  89. }
  90. #region UserProfile
  91. public virtual void AddTemporaryUserProfile(UserProfileData userProfile)
  92. {
  93. foreach (IUserDataPlugin plugin in m_plugins)
  94. {
  95. plugin.AddTemporaryUserProfile(userProfile);
  96. }
  97. }
  98. public virtual UserProfileData GetUserProfile(string fname, string lname)
  99. {
  100. foreach (IUserDataPlugin plugin in m_plugins)
  101. {
  102. UserProfileData profile = plugin.GetUserByName(fname, lname);
  103. if (profile != null)
  104. {
  105. profile.CurrentAgent = GetUserAgent(profile.ID);
  106. return profile;
  107. }
  108. }
  109. return null;
  110. }
  111. public void LogoutUsers(UUID regionID)
  112. {
  113. foreach (IUserDataPlugin plugin in m_plugins)
  114. {
  115. plugin.LogoutUsers(regionID);
  116. }
  117. }
  118. public void ResetAttachments(UUID userID)
  119. {
  120. foreach (IUserDataPlugin plugin in m_plugins)
  121. {
  122. plugin.ResetAttachments(userID);
  123. }
  124. }
  125. public UserProfileData GetUserProfile(Uri uri)
  126. {
  127. foreach (IUserDataPlugin plugin in m_plugins)
  128. {
  129. UserProfileData profile = plugin.GetUserByUri(uri);
  130. if (null != profile)
  131. return profile;
  132. }
  133. return null;
  134. }
  135. public virtual UserAgentData GetAgentByUUID(UUID userId)
  136. {
  137. foreach (IUserDataPlugin plugin in m_plugins)
  138. {
  139. UserAgentData agent = plugin.GetAgentByUUID(userId);
  140. if (agent != null)
  141. {
  142. return agent;
  143. }
  144. }
  145. return null;
  146. }
  147. public Uri GetUserUri(UserProfileData userProfile)
  148. {
  149. throw new NotImplementedException();
  150. }
  151. // see IUserService
  152. public virtual UserProfileData GetUserProfile(UUID uuid)
  153. {
  154. foreach (IUserDataPlugin plugin in m_plugins)
  155. {
  156. UserProfileData profile = plugin.GetUserByUUID(uuid);
  157. if (null != profile)
  158. {
  159. profile.CurrentAgent = GetUserAgent(profile.ID);
  160. return profile;
  161. }
  162. }
  163. return null;
  164. }
  165. public virtual List<AvatarPickerAvatar> GenerateAgentPickerRequestResponse(UUID queryID, string query)
  166. {
  167. List<AvatarPickerAvatar> allPickerList = new List<AvatarPickerAvatar>();
  168. foreach (IUserDataPlugin plugin in m_plugins)
  169. {
  170. try
  171. {
  172. List<AvatarPickerAvatar> pickerList = plugin.GeneratePickerResults(queryID, query);
  173. if (pickerList != null)
  174. allPickerList.AddRange(pickerList);
  175. }
  176. catch (Exception)
  177. {
  178. m_log.Error(
  179. "[USERSTORAGE]: Unable to generate AgentPickerData via " + plugin.Name + "(" + query + ")");
  180. }
  181. }
  182. return allPickerList;
  183. }
  184. public virtual bool UpdateUserProfile(UserProfileData data)
  185. {
  186. bool result = false;
  187. foreach (IUserDataPlugin plugin in m_plugins)
  188. {
  189. try
  190. {
  191. plugin.UpdateUserProfile(data);
  192. result = true;
  193. }
  194. catch (Exception e)
  195. {
  196. m_log.ErrorFormat(
  197. "[USERSTORAGE]: Unable to set user {0} {1} via {2}: {3}",
  198. data.FirstName, data.SurName, plugin.Name, e.ToString());
  199. }
  200. }
  201. return result;
  202. }
  203. #endregion
  204. #region Get UserAgent
  205. /// <summary>
  206. /// Loads a user agent by uuid (not called directly)
  207. /// </summary>
  208. /// <param name="uuid">The agent's UUID</param>
  209. /// <returns>Agent profiles</returns>
  210. public UserAgentData GetUserAgent(UUID uuid)
  211. {
  212. foreach (IUserDataPlugin plugin in m_plugins)
  213. {
  214. try
  215. {
  216. UserAgentData result = plugin.GetAgentByUUID(uuid);
  217. if (result != null)
  218. return result;
  219. }
  220. catch (Exception e)
  221. {
  222. m_log.Error("[USERSTORAGE]: Unable to find user via " + plugin.Name + "(" + e.ToString() + ")");
  223. }
  224. }
  225. return null;
  226. }
  227. /// <summary>
  228. /// Loads a user agent by name (not called directly)
  229. /// </summary>
  230. /// <param name="name">The agent's name</param>
  231. /// <returns>A user agent</returns>
  232. public UserAgentData GetUserAgent(string name)
  233. {
  234. foreach (IUserDataPlugin plugin in m_plugins)
  235. {
  236. try
  237. {
  238. UserAgentData result = plugin.GetAgentByName(name);
  239. if (result != null)
  240. return result;
  241. }
  242. catch (Exception e)
  243. {
  244. m_log.Error("[USERSTORAGE]: Unable to find user via " + plugin.Name + "(" + e.ToString() + ")");
  245. }
  246. }
  247. return null;
  248. }
  249. /// <summary>
  250. /// Loads a user agent by name (not called directly)
  251. /// </summary>
  252. /// <param name="fname">The agent's firstname</param>
  253. /// <param name="lname">The agent's lastname</param>
  254. /// <returns>A user agent</returns>
  255. public UserAgentData GetUserAgent(string fname, string lname)
  256. {
  257. foreach (IUserDataPlugin plugin in m_plugins)
  258. {
  259. try
  260. {
  261. UserAgentData result = plugin.GetAgentByName(fname, lname);
  262. if (result != null)
  263. return result;
  264. }
  265. catch (Exception e)
  266. {
  267. m_log.Error("[USERSTORAGE]: Unable to find user via " + plugin.Name + "(" + e.ToString() + ")");
  268. }
  269. }
  270. return null;
  271. }
  272. public virtual List<FriendListItem> GetUserFriendList(UUID ownerID)
  273. {
  274. List<FriendListItem> allFriends = new List<FriendListItem>();
  275. foreach (IUserDataPlugin plugin in m_plugins)
  276. {
  277. try
  278. {
  279. List<FriendListItem> friends = plugin.GetUserFriendList(ownerID);
  280. if (friends != null)
  281. allFriends.AddRange(friends);
  282. }
  283. catch (Exception e)
  284. {
  285. m_log.Error("[USERSTORAGE]: Unable to GetUserFriendList via " + plugin.Name + "(" + e.ToString() + ")");
  286. }
  287. }
  288. return allFriends;
  289. }
  290. public virtual Dictionary<UUID, FriendRegionInfo> GetFriendRegionInfos (List<UUID> uuids)
  291. {
  292. //Dictionary<UUID, FriendRegionInfo> allFriendRegions = new Dictionary<UUID, FriendRegionInfo>();
  293. foreach (IUserDataPlugin plugin in m_plugins)
  294. {
  295. try
  296. {
  297. Dictionary<UUID, FriendRegionInfo> friendRegions = plugin.GetFriendRegionInfos(uuids);
  298. if (friendRegions != null)
  299. return friendRegions;
  300. }
  301. catch (Exception e)
  302. {
  303. m_log.Error("[USERSTORAGE]: Unable to GetFriendRegionInfos via " + plugin.Name + "(" + e.ToString() + ")");
  304. }
  305. }
  306. return new Dictionary<UUID, FriendRegionInfo>();
  307. }
  308. public void StoreWebLoginKey(UUID agentID, UUID webLoginKey)
  309. {
  310. foreach (IUserDataPlugin plugin in m_plugins)
  311. {
  312. try
  313. {
  314. plugin.StoreWebLoginKey(agentID, webLoginKey);
  315. }
  316. catch (Exception e)
  317. {
  318. m_log.Error("[USERSTORAGE]: Unable to Store WebLoginKey via " + plugin.Name + "(" + e.ToString() + ")");
  319. }
  320. }
  321. }
  322. public virtual void AddNewUserFriend(UUID friendlistowner, UUID friend, uint perms)
  323. {
  324. foreach (IUserDataPlugin plugin in m_plugins)
  325. {
  326. try
  327. {
  328. plugin.AddNewUserFriend(friendlistowner, friend, perms);
  329. }
  330. catch (Exception e)
  331. {
  332. m_log.Error("[USERSTORAGE]: Unable to AddNewUserFriend via " + plugin.Name + "(" + e.ToString() + ")");
  333. }
  334. }
  335. }
  336. public virtual void RemoveUserFriend(UUID friendlistowner, UUID friend)
  337. {
  338. foreach (IUserDataPlugin plugin in m_plugins)
  339. {
  340. try
  341. {
  342. plugin.RemoveUserFriend(friendlistowner, friend);
  343. }
  344. catch (Exception e)
  345. {
  346. m_log.Error("[USERSTORAGE]: Unable to RemoveUserFriend via " + plugin.Name + "(" + e.ToString() + ")");
  347. }
  348. }
  349. }
  350. public virtual void UpdateUserFriendPerms(UUID friendlistowner, UUID friend, uint perms)
  351. {
  352. foreach (IUserDataPlugin plugin in m_plugins)
  353. {
  354. try
  355. {
  356. plugin.UpdateUserFriendPerms(friendlistowner, friend, perms);
  357. }
  358. catch (Exception e)
  359. {
  360. m_log.Error("[USERSTORAGE]: Unable to UpdateUserFriendPerms via " + plugin.Name + "(" + e.ToString() + ")");
  361. }
  362. }
  363. }
  364. /// <summary>
  365. /// Resets the currentAgent in the user profile
  366. /// </summary>
  367. /// <param name="agentID">The agent's ID</param>
  368. public virtual void ClearUserAgent(UUID agentID)
  369. {
  370. UserProfileData profile = GetUserProfile(agentID);
  371. if (profile == null)
  372. {
  373. return;
  374. }
  375. profile.CurrentAgent = null;
  376. UpdateUserProfile(profile);
  377. }
  378. #endregion
  379. #region CreateAgent
  380. /// <summary>
  381. /// Creates and initialises a new user agent - make sure to use CommitAgent when done to submit to the DB
  382. /// </summary>
  383. /// <param name="profile">The users profile</param>
  384. /// <param name="request">The users loginrequest</param>
  385. public void CreateAgent(UserProfileData profile, XmlRpcRequest request)
  386. {
  387. //m_log.DebugFormat("[USER MANAGER]: Creating agent {0} {1}", profile.Name, profile.ID);
  388. UserAgentData agent = new UserAgentData();
  389. // User connection
  390. agent.AgentOnline = true;
  391. if (request.Params.Count > 1)
  392. {
  393. if (request.Params[1] != null)
  394. {
  395. IPEndPoint RemoteIPEndPoint = (IPEndPoint)request.Params[1];
  396. agent.AgentIP = RemoteIPEndPoint.Address.ToString();
  397. agent.AgentPort = (uint)RemoteIPEndPoint.Port;
  398. }
  399. }
  400. // Generate sessions
  401. RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
  402. byte[] randDataS = new byte[16];
  403. byte[] randDataSS = new byte[16];
  404. rand.GetBytes(randDataS);
  405. rand.GetBytes(randDataSS);
  406. agent.SecureSessionID = new UUID(randDataSS, 0);
  407. agent.SessionID = new UUID(randDataS, 0);
  408. // Profile UUID
  409. agent.ProfileID = profile.ID;
  410. // Current location/position/alignment
  411. if (profile.CurrentAgent != null)
  412. {
  413. agent.Region = profile.CurrentAgent.Region;
  414. agent.Handle = profile.CurrentAgent.Handle;
  415. agent.Position = profile.CurrentAgent.Position;
  416. agent.LookAt = profile.CurrentAgent.LookAt;
  417. }
  418. else
  419. {
  420. agent.Region = profile.HomeRegionID;
  421. agent.Handle = profile.HomeRegion;
  422. agent.Position = profile.HomeLocation;
  423. agent.LookAt = profile.HomeLookAt;
  424. }
  425. // What time did the user login?
  426. agent.LoginTime = Util.UnixTimeSinceEpoch();
  427. agent.LogoutTime = 0;
  428. profile.CurrentAgent = agent;
  429. }
  430. public void CreateAgent(UserProfileData profile, OSD request)
  431. {
  432. //m_log.DebugFormat("[USER MANAGER]: Creating agent {0} {1}", profile.Name, profile.ID);
  433. UserAgentData agent = new UserAgentData();
  434. // User connection
  435. agent.AgentOnline = true;
  436. //if (request.Params.Count > 1)
  437. //{
  438. // IPEndPoint RemoteIPEndPoint = (IPEndPoint)request.Params[1];
  439. // agent.AgentIP = RemoteIPEndPoint.Address.ToString();
  440. // agent.AgentPort = (uint)RemoteIPEndPoint.Port;
  441. //}
  442. // Generate sessions
  443. RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
  444. byte[] randDataS = new byte[16];
  445. byte[] randDataSS = new byte[16];
  446. rand.GetBytes(randDataS);
  447. rand.GetBytes(randDataSS);
  448. agent.SecureSessionID = new UUID(randDataSS, 0);
  449. agent.SessionID = new UUID(randDataS, 0);
  450. // Profile UUID
  451. agent.ProfileID = profile.ID;
  452. // Current location/position/alignment
  453. if (profile.CurrentAgent != null)
  454. {
  455. agent.Region = profile.CurrentAgent.Region;
  456. agent.Handle = profile.CurrentAgent.Handle;
  457. agent.Position = profile.CurrentAgent.Position;
  458. agent.LookAt = profile.CurrentAgent.LookAt;
  459. }
  460. else
  461. {
  462. agent.Region = profile.HomeRegionID;
  463. agent.Handle = profile.HomeRegion;
  464. agent.Position = profile.HomeLocation;
  465. agent.LookAt = profile.HomeLookAt;
  466. }
  467. // What time did the user login?
  468. agent.LoginTime = Util.UnixTimeSinceEpoch();
  469. agent.LogoutTime = 0;
  470. profile.CurrentAgent = agent;
  471. }
  472. /// <summary>
  473. /// Saves a target agent to the database
  474. /// </summary>
  475. /// <param name="profile">The users profile</param>
  476. /// <returns>Successful?</returns>
  477. public bool CommitAgent(ref UserProfileData profile)
  478. {
  479. //m_log.DebugFormat("[USER MANAGER]: Committing agent {0} {1}", profile.Name, profile.ID);
  480. // TODO: how is this function different from setUserProfile? -> Add AddUserAgent() here and commit both tables "users" and "agents"
  481. // TODO: what is the logic should be?
  482. bool ret = false;
  483. ret = AddUserAgent(profile.CurrentAgent);
  484. ret = ret & UpdateUserProfile(profile);
  485. return ret;
  486. }
  487. /// <summary>
  488. /// Process a user logoff from OpenSim.
  489. /// </summary>
  490. /// <param name="userid"></param>
  491. /// <param name="regionid"></param>
  492. /// <param name="regionhandle"></param>
  493. /// <param name="position"></param>
  494. /// <param name="lookat"></param>
  495. public virtual void LogOffUser(UUID userid, UUID regionid, ulong regionhandle, Vector3 position, Vector3 lookat)
  496. {
  497. if (StatsManager.UserStats != null)
  498. StatsManager.UserStats.AddLogout();
  499. UserProfileData userProfile = GetUserProfile(userid);
  500. if (userProfile != null)
  501. {
  502. UserAgentData userAgent = userProfile.CurrentAgent;
  503. if (userAgent != null)
  504. {
  505. userAgent.AgentOnline = false;
  506. userAgent.LogoutTime = Util.UnixTimeSinceEpoch();
  507. //userAgent.sessionID = UUID.Zero;
  508. if (regionid != UUID.Zero)
  509. {
  510. userAgent.Region = regionid;
  511. }
  512. userAgent.Handle = regionhandle;
  513. userAgent.Position = position;
  514. userAgent.LookAt = lookat;
  515. //userProfile.CurrentAgent = userAgent;
  516. userProfile.LastLogin = userAgent.LogoutTime;
  517. CommitAgent(ref userProfile);
  518. }
  519. else
  520. {
  521. // If currentagent is null, we can't reference it here or the UserServer crashes!
  522. m_log.Info("[LOGOUT]: didn't save logout position: " + userid.ToString());
  523. }
  524. }
  525. else
  526. {
  527. m_log.Warn("[LOGOUT]: Unknown User logged out");
  528. }
  529. }
  530. public void LogOffUser(UUID userid, UUID regionid, ulong regionhandle, float posx, float posy, float posz)
  531. {
  532. LogOffUser(userid, regionid, regionhandle, new Vector3(posx, posy, posz), new Vector3());
  533. }
  534. #endregion
  535. /// <summary>
  536. /// Add a new user
  537. /// </summary>
  538. /// <param name="firstName">first name</param>
  539. /// <param name="lastName">last name</param>
  540. /// <param name="password">password</param>
  541. /// <param name="email">email</param>
  542. /// <param name="regX">location X</param>
  543. /// <param name="regY">location Y</param>
  544. /// <returns>The UUID of the created user profile. On failure, returns UUID.Zero</returns>
  545. public virtual UUID AddUser(string firstName, string lastName, string password, string email, uint regX, uint regY)
  546. {
  547. return AddUser(firstName, lastName, password, email, regX, regY, UUID.Random());
  548. }
  549. /// <summary>
  550. /// Add a new user
  551. /// </summary>
  552. /// <param name="firstName">first name</param>
  553. /// <param name="lastName">last name</param>
  554. /// <param name="password">password</param>
  555. /// <param name="email">email</param>
  556. /// <param name="regX">location X</param>
  557. /// <param name="regY">location Y</param>
  558. /// <param name="SetUUID">UUID of avatar.</param>
  559. /// <returns>The UUID of the created user profile. On failure, returns UUID.Zero</returns>
  560. public virtual UUID AddUser(
  561. string firstName, string lastName, string password, string email, uint regX, uint regY, UUID SetUUID)
  562. {
  563. UserProfileData user = new UserProfileData();
  564. user.PasswordSalt = Util.Md5Hash(UUID.Random().ToString());
  565. string md5PasswdHash = Util.Md5Hash(Util.Md5Hash(password) + ":" + user.PasswordSalt);
  566. user.HomeLocation = new Vector3(128, 128, 100);
  567. user.ID = SetUUID;
  568. user.FirstName = firstName;
  569. user.SurName = lastName;
  570. user.PasswordHash = md5PasswdHash;
  571. user.Created = Util.UnixTimeSinceEpoch();
  572. user.HomeLookAt = new Vector3(100, 100, 100);
  573. user.HomeRegionX = regX;
  574. user.HomeRegionY = regY;
  575. user.Email = email;
  576. foreach (IUserDataPlugin plugin in m_plugins)
  577. {
  578. try
  579. {
  580. plugin.AddNewUserProfile(user);
  581. }
  582. catch (Exception e)
  583. {
  584. m_log.Error("[USERSTORAGE]: Unable to add user via " + plugin.Name + "(" + e.ToString() + ")");
  585. }
  586. }
  587. UserProfileData userProf = GetUserProfile(firstName, lastName);
  588. if (userProf == null)
  589. {
  590. return UUID.Zero;
  591. }
  592. else
  593. {
  594. //
  595. // WARNING: This is a horrible hack
  596. // The purpose here is to avoid touching the user server at this point.
  597. // There are dragons there that I can't deal with right now.
  598. // diva 06/09/09
  599. //
  600. if (m_InventoryService != null)
  601. {
  602. // local service (standalone)
  603. m_log.Debug("[USERSTORAGE]: using IInventoryService to create user's inventory");
  604. m_InventoryService.CreateUserInventory(userProf.ID);
  605. }
  606. else if (m_commsManager.InterServiceInventoryService != null)
  607. {
  608. // used by the user server
  609. m_log.Debug("[USERSTORAGE]: using m_commsManager.InterServiceInventoryService to create user's inventory");
  610. m_commsManager.InterServiceInventoryService.CreateNewUserInventory(userProf.ID);
  611. }
  612. return userProf.ID;
  613. }
  614. }
  615. /// <summary>
  616. /// Reset a user password.
  617. /// </summary>
  618. /// <param name="firstName"></param>
  619. /// <param name="lastName"></param>
  620. /// <param name="newPassword"></param>
  621. /// <returns>true if the update was successful, false otherwise</returns>
  622. public virtual bool ResetUserPassword(string firstName, string lastName, string newPassword)
  623. {
  624. string md5PasswdHash = Util.Md5Hash(Util.Md5Hash(newPassword) + ":" + String.Empty);
  625. UserProfileData profile = GetUserProfile(firstName, lastName);
  626. if (null == profile)
  627. {
  628. m_log.ErrorFormat("[USERSTORAGE]: Could not find user {0} {1}", firstName, lastName);
  629. return false;
  630. }
  631. profile.PasswordHash = md5PasswdHash;
  632. profile.PasswordSalt = String.Empty;
  633. UpdateUserProfile(profile);
  634. return true;
  635. }
  636. public abstract UserProfileData SetupMasterUser(string firstName, string lastName);
  637. public abstract UserProfileData SetupMasterUser(string firstName, string lastName, string password);
  638. public abstract UserProfileData SetupMasterUser(UUID uuid);
  639. /// <summary>
  640. /// Add an agent using data plugins.
  641. /// </summary>
  642. /// <param name="agentdata">The agent data to be added</param>
  643. /// <returns>
  644. /// true if at least one plugin added the user agent. false if no plugin successfully added the agent
  645. /// </returns>
  646. public virtual bool AddUserAgent(UserAgentData agentdata)
  647. {
  648. bool result = false;
  649. foreach (IUserDataPlugin plugin in m_plugins)
  650. {
  651. try
  652. {
  653. plugin.AddNewUserAgent(agentdata);
  654. result = true;
  655. }
  656. catch (Exception e)
  657. {
  658. m_log.Error("[USERSTORAGE]: Unable to add agent via " + plugin.Name + "(" + e.ToString() + ")");
  659. }
  660. }
  661. return result;
  662. }
  663. /// <summary>
  664. /// Get avatar appearance information
  665. /// </summary>
  666. /// <param name="user"></param>
  667. /// <returns></returns>
  668. public virtual AvatarAppearance GetUserAppearance(UUID user)
  669. {
  670. foreach (IUserDataPlugin plugin in m_plugins)
  671. {
  672. try
  673. {
  674. AvatarAppearance appearance = plugin.GetUserAppearance(user);
  675. if (appearance != null)
  676. return appearance;
  677. }
  678. catch (Exception e)
  679. {
  680. m_log.ErrorFormat("[USERSTORAGE]: Unable to find user appearance {0} via {1} ({2})", user.ToString(), plugin.Name, e.ToString());
  681. }
  682. }
  683. return null;
  684. }
  685. public virtual void UpdateUserAppearance(UUID user, AvatarAppearance appearance)
  686. {
  687. foreach (IUserDataPlugin plugin in m_plugins)
  688. {
  689. try
  690. {
  691. plugin.UpdateUserAppearance(user, appearance);
  692. }
  693. catch (Exception e)
  694. {
  695. m_log.ErrorFormat("[USERSTORAGE]: Unable to update user appearance {0} via {1} ({2})", user.ToString(), plugin.Name, e.ToString());
  696. }
  697. }
  698. }
  699. #region IAuthentication
  700. protected Dictionary<UUID, List<string>> m_userKeys = new Dictionary<UUID, List<string>>();
  701. /// <summary>
  702. /// This generates authorization keys in the form
  703. /// http://userserver/uuid
  704. /// after verifying that the caller is, indeed, authorized to request a key
  705. /// </summary>
  706. /// <param name="url">URL of the user server</param>
  707. /// <param name="userID">The user ID requesting the new key</param>
  708. /// <param name="authToken">The original authorization token for that user, obtained during login</param>
  709. /// <returns></returns>
  710. public string GetNewKey(string url, UUID userID, UUID authToken)
  711. {
  712. UserProfileData profile = GetUserProfile(userID);
  713. string newKey = string.Empty;
  714. if (!url.EndsWith("/"))
  715. url = url + "/";
  716. if (profile != null)
  717. {
  718. // I'm overloading webloginkey for this, so that no changes are needed in the DB
  719. // The uses of webloginkey are fairly mutually exclusive
  720. if (profile.WebLoginKey.Equals(authToken))
  721. {
  722. newKey = UUID.Random().ToString();
  723. List<string> keys;
  724. lock (m_userKeys)
  725. {
  726. if (m_userKeys.ContainsKey(userID))
  727. {
  728. keys = m_userKeys[userID];
  729. }
  730. else
  731. {
  732. keys = new List<string>();
  733. m_userKeys.Add(userID, keys);
  734. }
  735. keys.Add(newKey);
  736. }
  737. m_log.InfoFormat("[USERAUTH]: Successfully generated new auth key for user {0}", userID);
  738. }
  739. else
  740. m_log.Warn("[USERAUTH]: Unauthorized key generation request. Denying new key.");
  741. }
  742. else
  743. m_log.Warn("[USERAUTH]: User not found.");
  744. return url + newKey;
  745. }
  746. /// <summary>
  747. /// This verifies the uuid portion of the key given out by GenerateKey
  748. /// </summary>
  749. /// <param name="userID"></param>
  750. /// <param name="key"></param>
  751. /// <returns></returns>
  752. public bool VerifyKey(UUID userID, string key)
  753. {
  754. lock (m_userKeys)
  755. {
  756. if (m_userKeys.ContainsKey(userID))
  757. {
  758. List<string> keys = m_userKeys[userID];
  759. if (keys.Contains(key))
  760. {
  761. // Keys are one-time only, so remove it
  762. keys.Remove(key);
  763. return true;
  764. }
  765. return false;
  766. }
  767. else
  768. return false;
  769. }
  770. }
  771. public virtual bool VerifySession(UUID userID, UUID sessionID)
  772. {
  773. UserProfileData userProfile = GetUserProfile(userID);
  774. if (userProfile != null && userProfile.CurrentAgent != null)
  775. {
  776. m_log.DebugFormat(
  777. "[USER AUTH]: Verifying session {0} for {1}; current session {2}",
  778. sessionID, userID, userProfile.CurrentAgent.SessionID);
  779. if (userProfile.CurrentAgent.SessionID == sessionID)
  780. {
  781. return true;
  782. }
  783. }
  784. return false;
  785. }
  786. public virtual bool AuthenticateUserByPassword(UUID userID, string password)
  787. {
  788. // m_log.DebugFormat("[USER AUTH]: Authenticating user {0} given password {1}", userID, password);
  789. UserProfileData userProfile = GetUserProfile(userID);
  790. if (null == userProfile)
  791. return false;
  792. string md5PasswordHash = Util.Md5Hash(Util.Md5Hash(password) + ":" + userProfile.PasswordSalt);
  793. // m_log.DebugFormat(
  794. // "[USER AUTH]: Submitted hash {0}, stored hash {1}", md5PasswordHash, userProfile.PasswordHash);
  795. if (md5PasswordHash == userProfile.PasswordHash)
  796. return true;
  797. else
  798. return false;
  799. }
  800. #endregion
  801. }
  802. }