RemoteAdminPlugin.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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;
  29. using System.Net;
  30. using System.Timers;
  31. using libsecondlife;
  32. using Mono.Addins;
  33. using Nwc.XmlRpc;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Region.Environment.Scenes;
  38. [assembly : Addin]
  39. [assembly : AddinDependency("OpenSim", "0.5")]
  40. namespace OpenSim.ApplicationPlugins.LoadRegions
  41. {
  42. [Extension("/OpenSim/Startup")]
  43. public class RemoteAdminPlugin : IApplicationPlugin
  44. {
  45. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  46. private OpenSimMain m_app;
  47. private BaseHttpServer m_httpd;
  48. private string requiredPassword = String.Empty;
  49. public void Initialise(OpenSimMain openSim)
  50. {
  51. try
  52. {
  53. if (openSim.ConfigSource.Configs["RemoteAdmin"] != null && openSim.ConfigSource.Configs["RemoteAdmin"].GetBoolean("enabled", false))
  54. {
  55. m_log.Info("[RADMIN]: Remote Admin Plugin Enabled");
  56. requiredPassword = openSim.ConfigSource.Configs["RemoteAdmin"].GetString("access_password", String.Empty);
  57. m_app = openSim;
  58. m_httpd = openSim.HttpServer;
  59. m_httpd.AddXmlRPCHandler("admin_create_region", XmlRpcCreateRegionMethod);
  60. m_httpd.AddXmlRPCHandler("admin_shutdown", XmlRpcShutdownMethod);
  61. m_httpd.AddXmlRPCHandler("admin_broadcast", XmlRpcAlertMethod);
  62. m_httpd.AddXmlRPCHandler("admin_restart", XmlRpcRestartMethod);
  63. m_httpd.AddXmlRPCHandler("admin_load_heightmap", XmlRpcLoadHeightmapMethod);
  64. m_httpd.AddXmlRPCHandler("admin_create_user", XmlRpcCreateUserMethod);
  65. m_httpd.AddXmlRPCHandler("admin_load_xml", XmlRpcLoadXMLMethod);
  66. }
  67. }
  68. catch (NullReferenceException)
  69. {
  70. // Ignore.
  71. }
  72. }
  73. public XmlRpcResponse XmlRpcRestartMethod(XmlRpcRequest request)
  74. {
  75. XmlRpcResponse response = new XmlRpcResponse();
  76. Hashtable requestData = (Hashtable) request.Params[0];
  77. LLUUID regionID = new LLUUID((string) requestData["regionID"]);
  78. Hashtable responseData = new Hashtable();
  79. if (requiredPassword != String.Empty &&
  80. (!requestData.Contains("password") || (string) requestData["password"] != requiredPassword))
  81. {
  82. responseData["accepted"] = "false";
  83. response.Value = responseData;
  84. }
  85. else
  86. {
  87. responseData["accepted"] = "true";
  88. response.Value = responseData;
  89. Scene RebootedScene;
  90. if (m_app.SceneManager.TryGetScene(regionID, out RebootedScene))
  91. {
  92. responseData["rebooting"] = "true";
  93. RebootedScene.Restart(30);
  94. }
  95. else
  96. {
  97. responseData["rebooting"] = "false";
  98. }
  99. }
  100. return response;
  101. }
  102. public XmlRpcResponse XmlRpcAlertMethod(XmlRpcRequest request)
  103. {
  104. XmlRpcResponse response = new XmlRpcResponse();
  105. Hashtable requestData = (Hashtable) request.Params[0];
  106. Hashtable responseData = new Hashtable();
  107. if (requiredPassword != String.Empty &&
  108. (!requestData.Contains("password") || (string) requestData["password"] != requiredPassword))
  109. {
  110. responseData["accepted"] = "false";
  111. response.Value = responseData;
  112. }
  113. else
  114. {
  115. string message = (string) requestData["message"];
  116. m_log.Info("[RADMIN]: Broadcasting: " + message);
  117. responseData["accepted"] = "true";
  118. response.Value = responseData;
  119. m_app.SceneManager.SendGeneralMessage(message);
  120. }
  121. return response;
  122. }
  123. public XmlRpcResponse XmlRpcLoadHeightmapMethod(XmlRpcRequest request)
  124. {
  125. XmlRpcResponse response = new XmlRpcResponse();
  126. Hashtable requestData = (Hashtable)request.Params[0];
  127. Hashtable responseData = new Hashtable();
  128. if (requiredPassword != String.Empty &&
  129. (!requestData.Contains("password") || (string)requestData["password"] != requiredPassword))
  130. {
  131. responseData["accepted"] = "false";
  132. response.Value = responseData;
  133. }
  134. else
  135. {
  136. string file = (string)requestData["filename"];
  137. LLUUID regionID = LLUUID.Parse((string)requestData["regionid"]);
  138. m_log.Info("[RADMIN]: Terrain Loading: " + file);
  139. responseData["accepted"] = "true";
  140. Scene region = null;
  141. if (m_app.SceneManager.TryGetScene(regionID, out region))
  142. {
  143. //region.LoadWorldMap(file);
  144. responseData["success"] = "true";
  145. }
  146. else
  147. {
  148. responseData["success"] = "false";
  149. responseData["error"] = "1: Unable to get a scene with that name.";
  150. }
  151. response.Value = responseData;
  152. }
  153. return response;
  154. }
  155. public XmlRpcResponse XmlRpcShutdownMethod(XmlRpcRequest request)
  156. {
  157. m_log.Info("[RADMIN]: Received Shutdown Administrator Request");
  158. XmlRpcResponse response = new XmlRpcResponse();
  159. Hashtable requestData = (Hashtable) request.Params[0];
  160. Hashtable responseData = new Hashtable();
  161. if (requiredPassword != String.Empty &&
  162. (!requestData.Contains("password") || (string) requestData["password"] != requiredPassword))
  163. {
  164. responseData["accepted"] = "false";
  165. response.Value = responseData;
  166. }
  167. else
  168. {
  169. if ((string) requestData["shutdown"] == "delayed")
  170. {
  171. int timeout = (Int32) requestData["milliseconds"];
  172. responseData["accepted"] = "true";
  173. response.Value = responseData;
  174. m_app.SceneManager.SendGeneralMessage("Region is going down in " + ((int) (timeout/1000)).ToString() +
  175. " second(s). Please save what you are doing and log out.");
  176. // Perform shutdown
  177. Timer shutdownTimer = new Timer(timeout); // Wait before firing
  178. shutdownTimer.AutoReset = false;
  179. shutdownTimer.Elapsed += new ElapsedEventHandler(shutdownTimer_Elapsed);
  180. shutdownTimer.Start();
  181. return response;
  182. }
  183. else
  184. {
  185. responseData["accepted"] = "true";
  186. response.Value = responseData;
  187. m_app.SceneManager.SendGeneralMessage("Region is going down now.");
  188. // Perform shutdown
  189. Timer shutdownTimer = new Timer(2000); // Wait 2 seconds before firing
  190. shutdownTimer.AutoReset = false;
  191. shutdownTimer.Elapsed += new ElapsedEventHandler(shutdownTimer_Elapsed);
  192. shutdownTimer.Start();
  193. return response;
  194. }
  195. }
  196. return response;
  197. }
  198. private void shutdownTimer_Elapsed(object sender, ElapsedEventArgs e)
  199. {
  200. m_app.Shutdown();
  201. }
  202. public XmlRpcResponse XmlRpcCreateRegionMethod(XmlRpcRequest request)
  203. {
  204. m_log.Info("[RADMIN]: Received Create Region Administrator Request");
  205. XmlRpcResponse response = new XmlRpcResponse();
  206. Hashtable requestData = (Hashtable) request.Params[0];
  207. Hashtable responseData = new Hashtable();
  208. if (requiredPassword != System.String.Empty &&
  209. (!requestData.Contains("password") || (string) requestData["password"] != requiredPassword))
  210. {
  211. responseData["created"] = "false";
  212. response.Value = responseData;
  213. }
  214. else
  215. {
  216. RegionInfo newRegionData = new RegionInfo();
  217. try
  218. {
  219. newRegionData.RegionID = (string) requestData["region_id"];
  220. newRegionData.RegionName = (string) requestData["region_name"];
  221. newRegionData.RegionLocX = Convert.ToUInt32((Int32) requestData["region_x"]);
  222. newRegionData.RegionLocY = Convert.ToUInt32((Int32) requestData["region_y"]);
  223. // Security risk
  224. newRegionData.DataStore = (string) requestData["datastore"];
  225. newRegionData.InternalEndPoint = new IPEndPoint(
  226. IPAddress.Parse((string) requestData["listen_ip"]), 0);
  227. newRegionData.InternalEndPoint.Port = (Int32) requestData["listen_port"];
  228. newRegionData.ExternalHostName = (string) requestData["external_address"];
  229. newRegionData.MasterAvatarFirstName = (string) requestData["region_master_first"];
  230. newRegionData.MasterAvatarLastName = (string) requestData["region_master_last"];
  231. m_app.CreateRegion(newRegionData, true);
  232. responseData["created"] = "true";
  233. response.Value = responseData;
  234. }
  235. catch (Exception e)
  236. {
  237. responseData["created"] = "false";
  238. responseData["error"] = e.ToString();
  239. response.Value = responseData;
  240. }
  241. }
  242. return response;
  243. }
  244. public XmlRpcResponse XmlRpcCreateUserMethod(XmlRpcRequest request)
  245. {
  246. m_log.Info("[RADMIN]: Received Create User Administrator Request");
  247. XmlRpcResponse response = new XmlRpcResponse();
  248. Hashtable requestData = (Hashtable) request.Params[0];
  249. Hashtable responseData = new Hashtable();
  250. if (requiredPassword != System.String.Empty &&
  251. (!requestData.Contains("password") || (string) requestData["password"] != requiredPassword))
  252. {
  253. responseData["created"] = "false";
  254. response.Value = responseData;
  255. }
  256. else
  257. {
  258. try
  259. {
  260. string tempfirstname = (string) requestData["user_firstname"];
  261. string templastname = (string) requestData["user_lastname"];
  262. string tempPasswd = (string) requestData["user_password"];
  263. uint regX = Convert.ToUInt32((Int32) requestData["start_region_x"]);
  264. uint regY = Convert.ToUInt32((Int32) requestData["start_region_y"]);
  265. LLUUID tempuserID = m_app.CreateUser(tempfirstname, templastname, tempPasswd, regX, regY);
  266. if (tempuserID == LLUUID.Zero)
  267. {
  268. responseData["created"] = "false";
  269. responseData["error"] = "Error creating user";
  270. responseData["avatar_uuid"] = LLUUID.Zero;
  271. response.Value = responseData;
  272. m_log.Error("[RADMIN]: Error creating user (" + tempfirstname + " " + templastname + ") :");
  273. }
  274. else
  275. {
  276. responseData["created"] = "true";
  277. responseData["avatar_uuid"] = tempuserID;
  278. response.Value = responseData;
  279. m_log.Info("[RADMIN]: User " + tempfirstname + " " + templastname + " created. Userid " + tempuserID + " assigned.");
  280. }
  281. }
  282. catch (Exception e)
  283. {
  284. responseData["created"] = "false";
  285. responseData["error"] = e.ToString();
  286. responseData["avatar_uuid"] = LLUUID.Zero;
  287. response.Value = responseData;
  288. }
  289. }
  290. return response;
  291. }
  292. public XmlRpcResponse XmlRpcLoadXMLMethod(XmlRpcRequest request)
  293. {
  294. m_log.Info("[RADMIN]: Received Load XML Administrator Request");
  295. XmlRpcResponse response = new XmlRpcResponse();
  296. Hashtable requestData = (Hashtable) request.Params[0];
  297. Hashtable responseData = new Hashtable();
  298. if (requiredPassword != System.String.Empty &&
  299. (!requestData.Contains("password") || (string) requestData["password"] != requiredPassword))
  300. {
  301. responseData["loaded"] = "false";
  302. responseData["switched"] = "false";
  303. response.Value = responseData;
  304. }
  305. else
  306. {
  307. try
  308. {
  309. string region_name = (string) requestData["region_name"];
  310. string filename = (string) requestData["filename"];
  311. if (m_app.SceneManager.TrySetCurrentScene(region_name))
  312. {
  313. m_log.Info("[RADMIN] Switched to region "+region_name);
  314. responseData["switched"] = "true";
  315. m_app.SceneManager.LoadCurrentSceneFromXml(filename, true, new LLVector3(0, 0, 0));
  316. responseData["loaded"] = "true";
  317. response.Value = responseData;
  318. }
  319. else
  320. {
  321. m_log.Info("[RADMIN] Failed to switch to region "+region_name);
  322. responseData["loaded"] = "false";
  323. responseData["switched"] = "false";
  324. response.Value = responseData;
  325. }
  326. }
  327. catch (Exception e)
  328. {
  329. responseData["loaded"] = "false";
  330. responseData["error"] = e.ToString();
  331. response.Value = responseData;
  332. }
  333. }
  334. return response;
  335. }
  336. public void Close()
  337. {
  338. }
  339. }
  340. }