RemoteAdminPlugin.cs 16 KB

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