Main.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 OpenSim 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.IO;
  30. using System.Net;
  31. using System.Reflection;
  32. using OpenMetaverse;
  33. using log4net;
  34. using log4net.Config;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Communications;
  37. using OpenSim.Framework.Communications.Cache;
  38. using OpenSim.Framework.Console;
  39. using OpenSim.Framework.Servers;
  40. using OpenSim.Framework.Statistics;
  41. using OpenSim.Grid.Communications.OGS1;
  42. namespace OpenSim.Grid.UserServer
  43. {
  44. /// <summary>
  45. /// Grid user server main class
  46. /// </summary>
  47. public class OpenUser_Main : BaseOpenSimServer, conscmd_callback
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private UserConfig Cfg;
  51. public UserManager m_userManager;
  52. public UserLoginService m_loginService;
  53. public GridInfoService m_gridInfoService;
  54. public MessageServersConnector m_messagesService;
  55. protected IInterServiceInventoryServices m_interServiceInventoryService;
  56. private UUID m_lastCreatedUser = UUID.Random();
  57. public static void Main(string[] args)
  58. {
  59. XmlConfigurator.Configure();
  60. m_log.Info("Launching UserServer...");
  61. OpenUser_Main userserver = new OpenUser_Main();
  62. userserver.Startup();
  63. userserver.Work();
  64. }
  65. private OpenUser_Main()
  66. {
  67. m_console = new ConsoleBase("User", this);
  68. MainConsole.Instance = m_console;
  69. }
  70. private void Work()
  71. {
  72. m_console.Notice("Enter help for a list of commands\n");
  73. while (true)
  74. {
  75. m_console.Prompt();
  76. }
  77. }
  78. protected override void StartupSpecific()
  79. {
  80. Cfg = new UserConfig("USER SERVER", (Path.Combine(Util.configDir(), "UserServer_Config.xml")));
  81. m_stats = StatsManager.StartCollectingUserStats();
  82. m_log.Info("[REGION]: Establishing data connection");
  83. m_userManager = new UserManager();
  84. m_userManager.AddPlugin(Cfg.DatabaseProvider, Cfg.DatabaseConnect);
  85. m_gridInfoService = new GridInfoService();
  86. m_interServiceInventoryService = new OGS1InterServiceInventoryService(Cfg.InventoryUrl);
  87. m_loginService = new UserLoginService(
  88. m_userManager, m_interServiceInventoryService, new LibraryRootFolder(), Cfg, Cfg.DefaultStartupMsg);
  89. m_messagesService = new MessageServersConnector();
  90. m_loginService.OnUserLoggedInAtLocation += NotifyMessageServersUserLoggedInToLocation;
  91. m_userManager.OnLogOffUser += NotifyMessageServersUserLoggOff;
  92. m_log.Info("[REGION]: Starting HTTP process");
  93. m_httpServer = new BaseHttpServer(Cfg.HttpPort);
  94. AddHttpHandlers();
  95. m_httpServer.Start();
  96. m_log.Info("[SERVER]: Userserver 0.5 - Startup complete");
  97. }
  98. protected void AddHttpHandlers()
  99. {
  100. m_httpServer.AddXmlRPCHandler("login_to_simulator", m_loginService.XmlRpcLoginMethod);
  101. m_httpServer.AddHTTPHandler("login", m_loginService.ProcessHTMLLogin);
  102. //
  103. // Get the minimum defaultLevel to access to the grid
  104. //
  105. m_loginService.setloginlevel((int)Cfg.DefaultUserLevel);
  106. if (Cfg.EnableLLSDLogin)
  107. {
  108. m_httpServer.SetDefaultLLSDHandler(m_loginService.LLSDLoginMethod);
  109. }
  110. m_httpServer.AddXmlRPCHandler("get_user_by_name", m_userManager.XmlRPCGetUserMethodName);
  111. m_httpServer.AddXmlRPCHandler("get_user_by_uuid", m_userManager.XmlRPCGetUserMethodUUID);
  112. m_httpServer.AddXmlRPCHandler("get_avatar_picker_avatar", m_userManager.XmlRPCGetAvatarPickerAvatar);
  113. m_httpServer.AddXmlRPCHandler("add_new_user_friend", m_userManager.XmlRpcResponseXmlRPCAddUserFriend);
  114. m_httpServer.AddXmlRPCHandler("remove_user_friend", m_userManager.XmlRpcResponseXmlRPCRemoveUserFriend);
  115. m_httpServer.AddXmlRPCHandler("update_user_friend_perms",
  116. m_userManager.XmlRpcResponseXmlRPCUpdateUserFriendPerms);
  117. m_httpServer.AddXmlRPCHandler("get_user_friend_list", m_userManager.XmlRpcResponseXmlRPCGetUserFriendList);
  118. m_httpServer.AddXmlRPCHandler("get_avatar_appearance", m_userManager.XmlRPCGetAvatarAppearance);
  119. m_httpServer.AddXmlRPCHandler("update_avatar_appearance", m_userManager.XmlRPCUpdateAvatarAppearance);
  120. m_httpServer.AddXmlRPCHandler("update_user_current_region", m_userManager.XmlRPCAtRegion);
  121. m_httpServer.AddXmlRPCHandler("logout_of_simulator", m_userManager.XmlRPCLogOffUserMethodUUID);
  122. m_httpServer.AddXmlRPCHandler("get_agent_by_uuid", m_userManager.XmlRPCGetAgentMethodUUID);
  123. m_httpServer.AddXmlRPCHandler("check_auth_session", m_userManager.XmlRPCCheckAuthSession);
  124. m_httpServer.AddXmlRPCHandler("set_login_params", m_loginService.XmlRPCSetLoginParams);
  125. // Message Server ---> User Server
  126. m_httpServer.AddXmlRPCHandler("register_messageserver", m_messagesService.XmlRPCRegisterMessageServer);
  127. m_httpServer.AddXmlRPCHandler("agent_change_region", m_messagesService.XmlRPCUserMovedtoRegion);
  128. m_httpServer.AddXmlRPCHandler("deregister_messageserver", m_messagesService.XmlRPCDeRegisterMessageServer);
  129. m_httpServer.AddStreamHandler(new RestStreamHandler("GET", "/get_grid_info",
  130. m_gridInfoService.RestGetGridInfoMethod));
  131. m_httpServer.AddXmlRPCHandler("get_grid_info", m_gridInfoService.XmlRpcGridInfoMethod);
  132. m_httpServer.AddStreamHandler(
  133. new RestStreamHandler("DELETE", "/usersessions/", m_userManager.RestDeleteUserSessionMethod));
  134. m_httpServer.AddXmlRPCHandler("update_user_profile", m_userManager.XmlRpcResponseXmlRPCUpdateUserProfile);
  135. }
  136. public void do_create(string what)
  137. {
  138. switch (what)
  139. {
  140. case "user":
  141. string tempfirstname = m_console.CmdPrompt("First name");
  142. string templastname = m_console.CmdPrompt("Last name");
  143. //tempMD5Passwd = m_console.PasswdPrompt("Password");
  144. string tempMD5Passwd = m_console.CmdPrompt("Password");
  145. uint regX = Convert.ToUInt32(m_console.CmdPrompt("Start Region X"));
  146. uint regY = Convert.ToUInt32(m_console.CmdPrompt("Start Region Y"));
  147. if (null != m_userManager.GetUserProfile(tempfirstname, templastname))
  148. {
  149. m_log.ErrorFormat(
  150. "[USERS]: A user with the name {0} {1} already exists!", tempfirstname, templastname);
  151. break;
  152. }
  153. tempMD5Passwd = Util.Md5Hash(Util.Md5Hash(tempMD5Passwd) + ":" + String.Empty);
  154. UUID userID = new UUID();
  155. try
  156. {
  157. userID = m_userManager.AddUserProfile(tempfirstname, templastname, tempMD5Passwd, regX, regY);
  158. }
  159. catch (Exception ex)
  160. {
  161. m_log.ErrorFormat("[USERS]: Error creating user: {0}", ex.ToString());
  162. }
  163. try
  164. {
  165. if (!m_interServiceInventoryService.CreateNewUserInventory(userID))
  166. {
  167. throw new Exception(
  168. String.Format(
  169. "The inventory creation request for user {0} did not succeed."
  170. + " Please contact your inventory service provider for more information.",
  171. userID));
  172. }
  173. }
  174. catch (WebException)
  175. {
  176. m_log.ErrorFormat(
  177. "[USERS]: Could not contact the inventory service at {0} to create an inventory for {1}",
  178. Cfg.InventoryUrl + "CreateInventory/", userID);
  179. }
  180. catch (Exception e)
  181. {
  182. m_log.ErrorFormat("[USERS]: Error creating inventory for user: {0}", e);
  183. }
  184. m_lastCreatedUser = userID;
  185. break;
  186. }
  187. }
  188. /// <summary>
  189. /// Execute switch for some of the reset commands
  190. /// </summary>
  191. /// <param name="args"></param>
  192. protected void Reset(string[] args)
  193. {
  194. if (args.Length == 0)
  195. return;
  196. switch (args[0])
  197. {
  198. case "user":
  199. switch (args[1])
  200. {
  201. case "password":
  202. ResetUserPassword(args);
  203. break;
  204. }
  205. break;
  206. }
  207. }
  208. /// <summary>
  209. /// Reset a user password.
  210. /// </summary>
  211. /// <param name="cmdparams"></param>
  212. private void ResetUserPassword(string[] cmdparams)
  213. {
  214. string firstName;
  215. string lastName;
  216. string newPassword;
  217. if (cmdparams.Length < 3)
  218. firstName = MainConsole.Instance.CmdPrompt("First name");
  219. else firstName = cmdparams[2];
  220. if ( cmdparams.Length < 4 )
  221. lastName = MainConsole.Instance.CmdPrompt("Last name");
  222. else lastName = cmdparams[3];
  223. if ( cmdparams.Length < 5 )
  224. newPassword = MainConsole.Instance.PasswdPrompt("New password");
  225. else newPassword = cmdparams[4];
  226. m_userManager.ResetUserPassword(firstName, lastName, newPassword);
  227. }
  228. public override void RunCmd(string cmd, string[] cmdparams)
  229. {
  230. base.RunCmd(cmd, cmdparams);
  231. switch (cmd)
  232. {
  233. case "create":
  234. do_create(cmdparams[0]);
  235. break;
  236. case "reset":
  237. Reset(cmdparams);
  238. break;
  239. case "login-level":
  240. // Set the minimal level to allow login
  241. // Usefull to allow grid update without worrying about users.
  242. // or fixing critical issue
  243. if (cmdparams.Length == 1)
  244. {
  245. int level = Convert.ToInt32(cmdparams[0]);
  246. m_loginService.setloginlevel(level);
  247. }
  248. break;
  249. case "login-reset":
  250. m_loginService.setloginlevel(0);
  251. break;
  252. case "login-text":
  253. if (cmdparams.Length == 1)
  254. {
  255. m_loginService.setwelcometext(cmdparams[0]);
  256. }
  257. break;
  258. case "test-inventory":
  259. // RestObjectPosterResponse<List<InventoryFolderBase>> requester = new RestObjectPosterResponse<List<InventoryFolderBase>>();
  260. // requester.ReturnResponseVal = TestResponse;
  261. // requester.BeginPostObject<UUID>(m_userManager._config.InventoryUrl + "RootFolders/", m_lastCreatedUser);
  262. SynchronousRestObjectPoster.BeginPostObject<UUID, List<InventoryFolderBase>>(
  263. "POST", Cfg.InventoryUrl + "RootFolders/", m_lastCreatedUser);
  264. break;
  265. case "logoff-user":
  266. if (cmdparams.Length >= 3)
  267. {
  268. string firstname = cmdparams[0];
  269. string lastname = cmdparams[1];
  270. string message = "";
  271. for (int i = 2; i < cmdparams.Length; i++)
  272. message += " " + cmdparams[i];
  273. UserProfileData theUser = null;
  274. try
  275. {
  276. theUser = m_loginService.GetTheUser(firstname, lastname);
  277. }
  278. catch (Exception)
  279. {
  280. m_log.Error("[LOGOFF]: Error getting user data from the database.");
  281. }
  282. if (theUser != null)
  283. {
  284. if (theUser.CurrentAgent != null)
  285. {
  286. if (theUser.CurrentAgent.AgentOnline)
  287. {
  288. m_log.Info("[LOGOFF]: Logging off requested user!");
  289. m_loginService.LogOffUser(theUser, message);
  290. theUser.CurrentAgent.AgentOnline = false;
  291. m_loginService.CommitAgent(ref theUser);
  292. }
  293. else
  294. {
  295. m_log.Info(
  296. "[LOGOFF]: User Doesn't appear to be online, sending the logoff message anyway.");
  297. m_loginService.LogOffUser(theUser, message);
  298. theUser.CurrentAgent.AgentOnline = false;
  299. m_loginService.CommitAgent(ref theUser);
  300. }
  301. }
  302. else
  303. {
  304. m_log.Error(
  305. "[LOGOFF]: Unable to logoff-user. User doesn't have an agent record so I can't find the simulator to notify");
  306. }
  307. }
  308. else
  309. {
  310. m_log.Info("[LOGOFF]: User doesn't exist in the database");
  311. }
  312. }
  313. else
  314. {
  315. m_log.Error(
  316. "[LOGOFF]: Invalid amount of parameters. logoff-user takes at least three. Firstname, Lastname, and message");
  317. }
  318. break;
  319. }
  320. }
  321. protected override void ShowHelp(string[] helpArgs)
  322. {
  323. base.ShowHelp(helpArgs);
  324. m_console.Notice("create user - create a new user");
  325. m_console.Notice("logoff-user <firstname> <lastname> <message> - logs off the specified user from the grid");
  326. m_console.Notice("reset user password - reset a user's password.");
  327. m_console.Notice("login-level <value> - Set the miminim userlevel allowed To login.");
  328. m_console.Notice("login-reset - reset the login level to its default value.");
  329. m_console.Notice("login-text <text to print during the login>");
  330. }
  331. public override void ShutdownSpecific()
  332. {
  333. m_loginService.OnUserLoggedInAtLocation -= NotifyMessageServersUserLoggedInToLocation;
  334. }
  335. public void TestResponse(List<InventoryFolderBase> resp)
  336. {
  337. m_console.Notice("response got");
  338. }
  339. public void NotifyMessageServersUserLoggOff(UUID agentID)
  340. {
  341. m_messagesService.TellMessageServersAboutUserLogoff(agentID);
  342. }
  343. public void NotifyMessageServersUserLoggedInToLocation(UUID agentID, UUID sessionID, UUID RegionID,
  344. ulong regionhandle, float positionX, float positionY,
  345. float positionZ, string firstname, string lastname)
  346. {
  347. m_messagesService.TellMessageServersAboutUser(agentID, sessionID, RegionID, regionhandle, positionX,
  348. positionY, positionZ, firstname, lastname);
  349. }
  350. }
  351. }