UserServerCommandModule.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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.IO;
  30. using System.Reflection;
  31. using log4net;
  32. using log4net.Config;
  33. using OpenMetaverse;
  34. using OpenSim.Data;
  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.Servers.HttpServer;
  41. using OpenSim.Framework.Statistics;
  42. using OpenSim.Grid.Communications.OGS1;
  43. using OpenSim.Grid.Framework;
  44. using OpenSim.Grid.UserServer.Modules;
  45. namespace OpenSim.Grid.UserServer
  46. {
  47. public class UserServerCommandModule
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. protected CommandConsole m_console;
  51. protected UserConfig m_cfg;
  52. protected UserDataBaseService m_userDataBaseService;
  53. protected UserLoginService m_loginService;
  54. protected UUID m_lastCreatedUser = UUID.Random();
  55. protected IGridServiceCore m_core;
  56. public UserServerCommandModule()
  57. {
  58. }
  59. public void Initialise(IGridServiceCore core)
  60. {
  61. m_core = core;
  62. }
  63. public void PostInitialise()
  64. {
  65. UserConfig cfg;
  66. if (m_core.TryGet<UserConfig>(out cfg))
  67. {
  68. m_cfg = cfg;
  69. }
  70. UserDataBaseService userDBservice;
  71. if (m_core.TryGet<UserDataBaseService>(out userDBservice))
  72. {
  73. m_userDataBaseService = userDBservice;
  74. }
  75. UserLoginService loginService;
  76. if (m_core.TryGet<UserLoginService>(out loginService))
  77. {
  78. m_loginService = loginService;
  79. }
  80. CommandConsole console;
  81. if ((m_core.TryGet<CommandConsole>(out console)) && (m_cfg != null)
  82. && (m_userDataBaseService != null) && (m_loginService != null))
  83. {
  84. RegisterConsoleCommands(console);
  85. }
  86. }
  87. public void RegisterHandlers(BaseHttpServer httpServer)
  88. {
  89. }
  90. private void RegisterConsoleCommands(CommandConsole console)
  91. {
  92. m_console = console;
  93. m_console.Commands.AddCommand("userserver", false, "create user",
  94. "create user [<first> [<last> [<x> <y> [email]]]]",
  95. "Create a new user account", RunCommand);
  96. m_console.Commands.AddCommand("userserver", false, "reset user password",
  97. "reset user password [<first> [<last> [<new password>]]]",
  98. "Reset a user's password", RunCommand);
  99. m_console.Commands.AddCommand("userserver", false, "login level",
  100. "login level <level>",
  101. "Set the minimum user level to log in", HandleLoginCommand);
  102. m_console.Commands.AddCommand("userserver", false, "login reset",
  103. "login reset",
  104. "Reset the login level to allow all users",
  105. HandleLoginCommand);
  106. m_console.Commands.AddCommand("userserver", false, "login text",
  107. "login text <text>",
  108. "Set the text users will see on login", HandleLoginCommand);
  109. m_console.Commands.AddCommand("userserver", false, "test-inventory",
  110. "test-inventory",
  111. "Perform a test inventory transaction", RunCommand);
  112. m_console.Commands.AddCommand("userserver", false, "logoff-user",
  113. "logoff-user <first> <last> <message>",
  114. "Log off a named user", RunCommand);
  115. }
  116. #region Console Command Handlers
  117. public void do_create(string[] args)
  118. {
  119. switch (args[0])
  120. {
  121. case "user":
  122. CreateUser(args);
  123. break;
  124. }
  125. }
  126. /// <summary>
  127. /// Execute switch for some of the reset commands
  128. /// </summary>
  129. /// <param name="args"></param>
  130. protected void Reset(string[] args)
  131. {
  132. if (args.Length == 0)
  133. return;
  134. switch (args[0])
  135. {
  136. case "user":
  137. switch (args[1])
  138. {
  139. case "password":
  140. ResetUserPassword(args);
  141. break;
  142. }
  143. break;
  144. }
  145. }
  146. /// <summary>
  147. /// Create a new user
  148. /// </summary>
  149. /// <param name="cmdparams">string array with parameters: firstname, lastname, password, locationX, locationY, email</param>
  150. protected void CreateUser(string[] cmdparams)
  151. {
  152. string firstName;
  153. string lastName;
  154. string password;
  155. string email;
  156. uint regX = 1000;
  157. uint regY = 1000;
  158. if (cmdparams.Length < 2)
  159. firstName = MainConsole.Instance.CmdPrompt("First name", "Default");
  160. else firstName = cmdparams[1];
  161. if (cmdparams.Length < 3)
  162. lastName = MainConsole.Instance.CmdPrompt("Last name", "User");
  163. else lastName = cmdparams[2];
  164. if (cmdparams.Length < 4)
  165. password = MainConsole.Instance.PasswdPrompt("Password");
  166. else password = cmdparams[3];
  167. if (cmdparams.Length < 5)
  168. regX = Convert.ToUInt32(MainConsole.Instance.CmdPrompt("Start Region X", regX.ToString()));
  169. else regX = Convert.ToUInt32(cmdparams[4]);
  170. if (cmdparams.Length < 6)
  171. regY = Convert.ToUInt32(MainConsole.Instance.CmdPrompt("Start Region Y", regY.ToString()));
  172. else regY = Convert.ToUInt32(cmdparams[5]);
  173. if (cmdparams.Length < 7)
  174. email = MainConsole.Instance.CmdPrompt("Email", "");
  175. else email = cmdparams[6];
  176. if (null == m_userDataBaseService.GetUserProfile(firstName, lastName))
  177. {
  178. m_lastCreatedUser = m_userDataBaseService.AddUser(firstName, lastName, password, email, regX, regY);
  179. }
  180. else
  181. {
  182. m_log.ErrorFormat("[USERS]: A user with the name {0} {1} already exists!", firstName, lastName);
  183. }
  184. }
  185. /// <summary>
  186. /// Reset a user password.
  187. /// </summary>
  188. /// <param name="cmdparams"></param>
  189. private void ResetUserPassword(string[] cmdparams)
  190. {
  191. string firstName;
  192. string lastName;
  193. string newPassword;
  194. if (cmdparams.Length < 3)
  195. firstName = MainConsole.Instance.CmdPrompt("First name");
  196. else firstName = cmdparams[2];
  197. if (cmdparams.Length < 4)
  198. lastName = MainConsole.Instance.CmdPrompt("Last name");
  199. else lastName = cmdparams[3];
  200. if (cmdparams.Length < 5)
  201. newPassword = MainConsole.Instance.PasswdPrompt("New password");
  202. else newPassword = cmdparams[4];
  203. m_userDataBaseService.ResetUserPassword(firstName, lastName, newPassword);
  204. }
  205. /*
  206. private void HandleTestCommand(string module, string[] cmd)
  207. {
  208. m_log.Info("test command received");
  209. }
  210. */
  211. private void HandleLoginCommand(string module, string[] cmd)
  212. {
  213. string subcommand = cmd[1];
  214. switch (subcommand)
  215. {
  216. case "level":
  217. // Set the minimal level to allow login
  218. // Useful to allow grid update without worrying about users.
  219. // or fixing critical issues
  220. //
  221. if (cmd.Length > 2)
  222. {
  223. int level = Convert.ToInt32(cmd[2]);
  224. m_loginService.setloginlevel(level);
  225. }
  226. break;
  227. case "reset":
  228. m_loginService.setloginlevel(0);
  229. break;
  230. case "text":
  231. if (cmd.Length > 2)
  232. {
  233. m_loginService.setwelcometext(cmd[2]);
  234. }
  235. break;
  236. }
  237. }
  238. public void RunCommand(string module, string[] cmd)
  239. {
  240. List<string> args = new List<string>(cmd);
  241. string command = cmd[0];
  242. args.RemoveAt(0);
  243. string[] cmdparams = args.ToArray();
  244. switch (command)
  245. {
  246. case "create":
  247. do_create(cmdparams);
  248. break;
  249. case "reset":
  250. Reset(cmdparams);
  251. break;
  252. case "test-inventory":
  253. // RestObjectPosterResponse<List<InventoryFolderBase>> requester = new RestObjectPosterResponse<List<InventoryFolderBase>>();
  254. // requester.ReturnResponseVal = TestResponse;
  255. // requester.BeginPostObject<UUID>(m_userManager._config.InventoryUrl + "RootFolders/", m_lastCreatedUser);
  256. SynchronousRestObjectPoster.BeginPostObject<UUID, List<InventoryFolderBase>>(
  257. "POST", m_cfg.InventoryUrl + "RootFolders/", m_lastCreatedUser);
  258. break;
  259. case "logoff-user":
  260. if (cmdparams.Length >= 3)
  261. {
  262. string firstname = cmdparams[0];
  263. string lastname = cmdparams[1];
  264. string message = "";
  265. for (int i = 2; i < cmdparams.Length; i++)
  266. message += " " + cmdparams[i];
  267. UserProfileData theUser = null;
  268. try
  269. {
  270. theUser = m_loginService.GetTheUser(firstname, lastname);
  271. }
  272. catch (Exception)
  273. {
  274. m_log.Error("[LOGOFF]: Error getting user data from the database.");
  275. }
  276. if (theUser != null)
  277. {
  278. if (theUser.CurrentAgent != null)
  279. {
  280. if (theUser.CurrentAgent.AgentOnline)
  281. {
  282. m_log.Info("[LOGOFF]: Logging off requested user!");
  283. m_loginService.LogOffUser(theUser, message);
  284. theUser.CurrentAgent.AgentOnline = false;
  285. m_loginService.CommitAgent(ref theUser);
  286. }
  287. else
  288. {
  289. m_log.Info(
  290. "[LOGOFF]: User Doesn't appear to be online, sending the logoff message anyway.");
  291. m_loginService.LogOffUser(theUser, message);
  292. theUser.CurrentAgent.AgentOnline = false;
  293. m_loginService.CommitAgent(ref theUser);
  294. }
  295. }
  296. else
  297. {
  298. m_log.Error(
  299. "[LOGOFF]: Unable to logoff-user. User doesn't have an agent record so I can't find the simulator to notify");
  300. }
  301. }
  302. else
  303. {
  304. m_log.Info("[LOGOFF]: User doesn't exist in the database");
  305. }
  306. }
  307. else
  308. {
  309. m_log.Error(
  310. "[LOGOFF]: Invalid amount of parameters. logoff-user takes at least three. Firstname, Lastname, and message");
  311. }
  312. break;
  313. }
  314. }
  315. }
  316. #endregion
  317. }