GodsModule.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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;
  29. using System.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Web;
  34. using System.Xml;
  35. using log4net;
  36. using Mono.Addins;
  37. using Nini.Config;
  38. using OpenMetaverse;
  39. using OpenMetaverse.Messages.Linden;
  40. using OpenMetaverse.StructuredData;
  41. using OpenSim.Framework;
  42. using OpenSim.Framework.Capabilities;
  43. using OpenSim.Framework.Servers;
  44. using OpenSim.Framework.Servers.HttpServer;
  45. using OpenSim.Region.Framework.Scenes;
  46. using OpenSim.Region.Framework.Interfaces;
  47. using Caps = OpenSim.Framework.Capabilities.Caps;
  48. using OSDArray = OpenMetaverse.StructuredData.OSDArray;
  49. using OSDMap = OpenMetaverse.StructuredData.OSDMap;
  50. namespace OpenSim.Region.CoreModules.Avatar.Gods
  51. {
  52. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "GodsModule")]
  53. public class GodsModule : INonSharedRegionModule, IGodsModule
  54. {
  55. private static readonly ILog m_log =
  56. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  57. /// <summary>Special UUID for actions that apply to all agents</summary>
  58. private static readonly UUID ALL_AGENTS = new UUID("44e87126-e794-4ded-05b3-7c42da3d5cdb");
  59. private static readonly UUID UUID_GRID_GOD = new UUID("6571e388-6218-4574-87db-f9379718315e");
  60. protected Scene m_scene;
  61. protected IDialogModule m_dialogModule;
  62. public void Initialise(IConfigSource source)
  63. {
  64. }
  65. public void AddRegion(Scene scene)
  66. {
  67. m_scene = scene;
  68. m_scene.RegisterModuleInterface<IGodsModule>(this);
  69. m_scene.EventManager.OnNewClient += SubscribeToClientEvents;
  70. m_scene.EventManager.OnRegisterCaps += OnRegisterCaps;
  71. scene.EventManager.OnIncomingInstantMessage +=
  72. OnIncomingInstantMessage;
  73. }
  74. public void RemoveRegion(Scene scene)
  75. {
  76. m_scene.UnregisterModuleInterface<IGodsModule>(this);
  77. m_scene.EventManager.OnNewClient -= SubscribeToClientEvents;
  78. m_scene = null;
  79. }
  80. public void RegionLoaded(Scene scene)
  81. {
  82. m_dialogModule = m_scene.RequestModuleInterface<IDialogModule>();
  83. }
  84. public void Close() {}
  85. public string Name { get { return "Gods Module"; } }
  86. public Type ReplaceableInterface
  87. {
  88. get { return null; }
  89. }
  90. public void SubscribeToClientEvents(IClientAPI client)
  91. {
  92. client.OnGodKickUser += KickUser;
  93. client.OnRequestGodlikePowers += RequestGodlikePowers;
  94. }
  95. public void UnsubscribeFromClientEvents(IClientAPI client)
  96. {
  97. client.OnGodKickUser -= KickUser;
  98. client.OnRequestGodlikePowers -= RequestGodlikePowers;
  99. }
  100. private void OnRegisterCaps(UUID agentID, Caps caps)
  101. {
  102. string uri = "/CAPS/" + UUID.Random();
  103. caps.RegisterHandler(
  104. "UntrustedSimulatorMessage",
  105. new RestStreamHandler("POST", uri, HandleUntrustedSimulatorMessage, "UntrustedSimulatorMessage", null));
  106. }
  107. private string HandleUntrustedSimulatorMessage(string request,
  108. string path, string param, IOSHttpRequest httpRequest,
  109. IOSHttpResponse httpResponse)
  110. {
  111. OSDMap osd = (OSDMap)OSDParser.DeserializeLLSDXml(request);
  112. string message = osd["message"].AsString();
  113. if (message == "GodKickUser")
  114. {
  115. OSDMap body = (OSDMap)osd["body"];
  116. OSDArray userInfo = (OSDArray)body["UserInfo"];
  117. OSDMap userData = (OSDMap)userInfo[0];
  118. UUID agentID = userData["AgentID"].AsUUID();
  119. UUID godID = userData["GodID"].AsUUID();
  120. UUID godSessionID = userData["GodSessionID"].AsUUID();
  121. uint kickFlags = userData["KickFlags"].AsUInteger();
  122. string reason = userData["Reason"].AsString();
  123. ScenePresence god = m_scene.GetScenePresence(godID);
  124. if (god == null || god.ControllingClient.SessionId != godSessionID)
  125. return String.Empty;
  126. KickUser(godID, agentID, kickFlags, reason);
  127. }
  128. else
  129. {
  130. m_log.ErrorFormat("[GOD]: Unhandled UntrustedSimulatorMessage: {0}", message);
  131. }
  132. return String.Empty;
  133. }
  134. public void RequestGodlikePowers(
  135. UUID agentID, UUID sessionID, UUID token, bool godLike)
  136. {
  137. ScenePresence sp = m_scene.GetScenePresence(agentID);
  138. if(sp == null || sp.IsDeleted || sp.IsNPC)
  139. return;
  140. if (sessionID != sp.ControllingClient.SessionId)
  141. return;
  142. sp.GrantGodlikePowers(token, godLike);
  143. if (godLike && !sp.IsViewerUIGod && m_dialogModule != null)
  144. m_dialogModule.SendAlertToUser(agentID, "Request for god powers denied");
  145. }
  146. public void KickUser(UUID godID, UUID agentID, uint kickflags, byte[] reason)
  147. {
  148. KickUser(godID, agentID, kickflags, Utils.BytesToString(reason));
  149. }
  150. /// <summary>
  151. /// Kicks or freezes User specified from the simulator. This logs them off of the grid
  152. /// </summary>
  153. /// <param name="godID">The person doing the kicking</param>
  154. /// <param name="agentID">the person that is being kicked</param>
  155. /// <param name="kickflags">Tells what to do to the user</param>
  156. /// <param name="reason">The message to send to the user after it's been turned into a field</param>
  157. public void KickUser(UUID godID, UUID agentID, uint kickflags, string reason)
  158. {
  159. // assuming automatic god rights on this for fast griefing reaction
  160. // this is also needed for kick via message
  161. if(!m_scene.Permissions.IsGod(godID))
  162. return;
  163. int godlevel = 200;
  164. // update level so higher gods can kick lower ones
  165. ScenePresence god = m_scene.GetScenePresence(godID);
  166. if(god != null && god.GodController.GodLevel > godlevel)
  167. godlevel = god.GodController.GodLevel;
  168. if(agentID == ALL_AGENTS)
  169. {
  170. m_scene.ForEachRootScenePresence(delegate(ScenePresence p)
  171. {
  172. if (p.UUID != godID)
  173. {
  174. if(godlevel > p.GodController.GodLevel)
  175. doKickmodes(godID, p, kickflags, reason);
  176. else if(m_dialogModule != null)
  177. m_dialogModule.SendAlertToUser(p.UUID, "Kick from " + godID.ToString() + " ignored, kick reason: " + reason);
  178. }
  179. });
  180. return;
  181. }
  182. ScenePresence sp = m_scene.GetScenePresence(agentID);
  183. if (sp == null || sp.IsChildAgent)
  184. {
  185. IMessageTransferModule transferModule =
  186. m_scene.RequestModuleInterface<IMessageTransferModule>();
  187. if (transferModule != null)
  188. {
  189. m_log.DebugFormat("[GODS]: Sending nonlocal kill for agent {0}", agentID);
  190. transferModule.SendInstantMessage(new GridInstantMessage(
  191. m_scene, godID, "God", agentID, (byte)250, false,
  192. reason, UUID.Zero, true,
  193. new Vector3(), new byte[] {(byte)kickflags}, true),
  194. delegate(bool success) {} );
  195. }
  196. return;
  197. }
  198. if (godlevel <= sp.GodController.GodLevel) // no god wars
  199. {
  200. if(m_dialogModule != null)
  201. m_dialogModule.SendAlertToUser(sp.UUID, "Kick from " + godID.ToString() + " ignored, kick reason: " + reason);
  202. return;
  203. }
  204. if(sp.UUID == godID)
  205. return;
  206. doKickmodes(godID, sp, kickflags, reason);
  207. }
  208. private void doKickmodes(UUID godID, ScenePresence sp, uint kickflags, string reason)
  209. {
  210. switch (kickflags)
  211. {
  212. case 0:
  213. KickPresence(sp, reason);
  214. break;
  215. case 1:
  216. sp.AllowMovement = false;
  217. if(m_dialogModule != null)
  218. {
  219. m_dialogModule.SendAlertToUser(sp.UUID, reason);
  220. m_dialogModule.SendAlertToUser(godID, "User Frozen");
  221. }
  222. break;
  223. case 2:
  224. sp.AllowMovement = true;
  225. if(m_dialogModule != null)
  226. {
  227. m_dialogModule.SendAlertToUser(sp.UUID, reason);
  228. m_dialogModule.SendAlertToUser(godID, "User Unfrozen");
  229. }
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. private void KickPresence(ScenePresence sp, string reason)
  236. {
  237. if(sp.IsDeleted || sp.IsChildAgent)
  238. return;
  239. sp.ControllingClient.Kick(reason);
  240. sp.Scene.CloseAgent(sp.UUID, true);
  241. }
  242. public void GridKickUser(UUID agentID, string reason)
  243. {
  244. int godlevel = 240; // grid god default
  245. ScenePresence sp = m_scene.GetScenePresence(agentID);
  246. if (sp == null || sp.IsChildAgent)
  247. {
  248. IMessageTransferModule transferModule =
  249. m_scene.RequestModuleInterface<IMessageTransferModule>();
  250. if (transferModule != null)
  251. {
  252. m_log.DebugFormat("[GODS]: Sending nonlocal kill for agent {0}", agentID);
  253. transferModule.SendInstantMessage(new GridInstantMessage(
  254. m_scene, UUID_GRID_GOD, "GRID", agentID, (byte)250, false,
  255. reason, UUID.Zero, true,
  256. new Vector3(), new byte[] {0}, true),
  257. delegate(bool success) {} );
  258. }
  259. return;
  260. }
  261. if(sp.IsDeleted)
  262. return;
  263. if (godlevel <= sp.GodController.GodLevel) // no god wars
  264. {
  265. if(m_dialogModule != null)
  266. m_dialogModule.SendAlertToUser(sp.UUID, "GRID kick detected and ignored, kick reason: " + reason);
  267. return;
  268. }
  269. sp.ControllingClient.Kick(reason);
  270. sp.Scene.CloseAgent(sp.UUID, true);
  271. }
  272. private void OnIncomingInstantMessage(GridInstantMessage msg)
  273. {
  274. if (msg.dialog == (uint)250) // Nonlocal kick
  275. {
  276. UUID agentID = new UUID(msg.toAgentID);
  277. string reason = msg.message;
  278. UUID godID = new UUID(msg.fromAgentID);
  279. uint kickMode = (uint)msg.binaryBucket[0];
  280. if(godID == UUID_GRID_GOD)
  281. GridKickUser(agentID, reason);
  282. else
  283. KickUser(godID, agentID, kickMode, reason);
  284. }
  285. }
  286. }
  287. }