UserManagerBase.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. InventoryFolderBase rootfolder = m_InventoryService.GetRootFolder(userProf.ID);
  606. if (rootfolder != null)
  607. userProf.RootInventoryFolderID = rootfolder.ID;
  608. }
  609. else if (m_commsManager.InterServiceInventoryService != null)
  610. {
  611. // used by the user server
  612. m_log.Debug("[USERSTORAGE]: using m_commsManager.InterServiceInventoryService to create user's inventory");
  613. m_commsManager.InterServiceInventoryService.CreateNewUserInventory(userProf.ID);
  614. }
  615. return userProf.ID;
  616. }
  617. }
  618. /// <summary>
  619. /// Reset a user password.
  620. /// </summary>
  621. /// <param name="firstName"></param>
  622. /// <param name="lastName"></param>
  623. /// <param name="newPassword"></param>
  624. /// <returns>true if the update was successful, false otherwise</returns>
  625. public virtual bool ResetUserPassword(string firstName, string lastName, string newPassword)
  626. {
  627. string md5PasswdHash = Util.Md5Hash(Util.Md5Hash(newPassword) + ":" + String.Empty);
  628. UserProfileData profile = GetUserProfile(firstName, lastName);
  629. if (null == profile)
  630. {
  631. m_log.ErrorFormat("[USERSTORAGE]: Could not find user {0} {1}", firstName, lastName);
  632. return false;
  633. }
  634. profile.PasswordHash = md5PasswdHash;
  635. profile.PasswordSalt = String.Empty;
  636. UpdateUserProfile(profile);
  637. return true;
  638. }
  639. public abstract UserProfileData SetupMasterUser(string firstName, string lastName);
  640. public abstract UserProfileData SetupMasterUser(string firstName, string lastName, string password);
  641. public abstract UserProfileData SetupMasterUser(UUID uuid);
  642. /// <summary>
  643. /// Add an agent using data plugins.
  644. /// </summary>
  645. /// <param name="agentdata">The agent data to be added</param>
  646. /// <returns>
  647. /// true if at least one plugin added the user agent. false if no plugin successfully added the agent
  648. /// </returns>
  649. public virtual bool AddUserAgent(UserAgentData agentdata)
  650. {
  651. bool result = false;
  652. foreach (IUserDataPlugin plugin in m_plugins)
  653. {
  654. try
  655. {
  656. plugin.AddNewUserAgent(agentdata);
  657. result = true;
  658. }
  659. catch (Exception e)
  660. {
  661. m_log.Error("[USERSTORAGE]: Unable to add agent via " + plugin.Name + "(" + e.ToString() + ")");
  662. }
  663. }
  664. return result;
  665. }
  666. /// <summary>
  667. /// Get avatar appearance information
  668. /// </summary>
  669. /// <param name="user"></param>
  670. /// <returns></returns>
  671. public virtual AvatarAppearance GetUserAppearance(UUID user)
  672. {
  673. foreach (IUserDataPlugin plugin in m_plugins)
  674. {
  675. try
  676. {
  677. AvatarAppearance appearance = plugin.GetUserAppearance(user);
  678. if (appearance != null)
  679. return appearance;
  680. }
  681. catch (Exception e)
  682. {
  683. m_log.ErrorFormat("[USERSTORAGE]: Unable to find user appearance {0} via {1} ({2})", user.ToString(), plugin.Name, e.ToString());
  684. }
  685. }
  686. return null;
  687. }
  688. public virtual void UpdateUserAppearance(UUID user, AvatarAppearance appearance)
  689. {
  690. foreach (IUserDataPlugin plugin in m_plugins)
  691. {
  692. try
  693. {
  694. plugin.UpdateUserAppearance(user, appearance);
  695. }
  696. catch (Exception e)
  697. {
  698. m_log.ErrorFormat("[USERSTORAGE]: Unable to update user appearance {0} via {1} ({2})", user.ToString(), plugin.Name, e.ToString());
  699. }
  700. }
  701. }
  702. #region IAuthentication
  703. protected Dictionary<UUID, List<string>> m_userKeys = new Dictionary<UUID, List<string>>();
  704. /// <summary>
  705. /// This generates authorization keys in the form
  706. /// http://userserver/uuid
  707. /// after verifying that the caller is, indeed, authorized to request a key
  708. /// </summary>
  709. /// <param name="url">URL of the user server</param>
  710. /// <param name="userID">The user ID requesting the new key</param>
  711. /// <param name="authToken">The original authorization token for that user, obtained during login</param>
  712. /// <returns></returns>
  713. public string GetNewKey(string url, UUID userID, UUID authToken)
  714. {
  715. UserProfileData profile = GetUserProfile(userID);
  716. string newKey = string.Empty;
  717. if (!url.EndsWith("/"))
  718. url = url + "/";
  719. if (profile != null)
  720. {
  721. // I'm overloading webloginkey for this, so that no changes are needed in the DB
  722. // The uses of webloginkey are fairly mutually exclusive
  723. if (profile.WebLoginKey.Equals(authToken))
  724. {
  725. newKey = UUID.Random().ToString();
  726. List<string> keys;
  727. lock (m_userKeys)
  728. {
  729. if (m_userKeys.ContainsKey(userID))
  730. {
  731. keys = m_userKeys[userID];
  732. }
  733. else
  734. {
  735. keys = new List<string>();
  736. m_userKeys.Add(userID, keys);
  737. }
  738. keys.Add(newKey);
  739. }
  740. m_log.InfoFormat("[USERAUTH]: Successfully generated new auth key for user {0}", userID);
  741. }
  742. else
  743. m_log.Warn("[USERAUTH]: Unauthorized key generation request. Denying new key.");
  744. }
  745. else
  746. m_log.Warn("[USERAUTH]: User not found.");
  747. return url + newKey;
  748. }
  749. /// <summary>
  750. /// This verifies the uuid portion of the key given out by GenerateKey
  751. /// </summary>
  752. /// <param name="userID"></param>
  753. /// <param name="key"></param>
  754. /// <returns></returns>
  755. public bool VerifyKey(UUID userID, string key)
  756. {
  757. lock (m_userKeys)
  758. {
  759. if (m_userKeys.ContainsKey(userID))
  760. {
  761. List<string> keys = m_userKeys[userID];
  762. if (keys.Contains(key))
  763. {
  764. // Keys are one-time only, so remove it
  765. keys.Remove(key);
  766. return true;
  767. }
  768. return false;
  769. }
  770. else
  771. return false;
  772. }
  773. }
  774. public virtual bool VerifySession(UUID userID, UUID sessionID)
  775. {
  776. UserProfileData userProfile = GetUserProfile(userID);
  777. if (userProfile != null && userProfile.CurrentAgent != null)
  778. {
  779. m_log.DebugFormat(
  780. "[USER AUTH]: Verifying session {0} for {1}; current session {2}",
  781. sessionID, userID, userProfile.CurrentAgent.SessionID);
  782. if (userProfile.CurrentAgent.SessionID == sessionID)
  783. {
  784. return true;
  785. }
  786. }
  787. return false;
  788. }
  789. public virtual bool AuthenticateUserByPassword(UUID userID, string password)
  790. {
  791. // m_log.DebugFormat("[USER AUTH]: Authenticating user {0} given password {1}", userID, password);
  792. UserProfileData userProfile = GetUserProfile(userID);
  793. if (null == userProfile)
  794. return false;
  795. string md5PasswordHash = Util.Md5Hash(Util.Md5Hash(password) + ":" + userProfile.PasswordSalt);
  796. // m_log.DebugFormat(
  797. // "[USER AUTH]: Submitted hash {0}, stored hash {1}", md5PasswordHash, userProfile.PasswordHash);
  798. if (md5PasswordHash == userProfile.PasswordHash)
  799. return true;
  800. else
  801. return false;
  802. }
  803. #endregion
  804. }
  805. }