GodsModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. protected Scene m_scene;
  60. protected IDialogModule m_dialogModule;
  61. protected IDialogModule DialogModule
  62. {
  63. get
  64. {
  65. if (m_dialogModule == null)
  66. m_dialogModule = m_scene.RequestModuleInterface<IDialogModule>();
  67. return m_dialogModule;
  68. }
  69. }
  70. public void Initialise(IConfigSource source)
  71. {
  72. }
  73. public void AddRegion(Scene scene)
  74. {
  75. m_scene = scene;
  76. m_scene.RegisterModuleInterface<IGodsModule>(this);
  77. m_scene.EventManager.OnNewClient += SubscribeToClientEvents;
  78. m_scene.EventManager.OnRegisterCaps += OnRegisterCaps;
  79. scene.EventManager.OnIncomingInstantMessage +=
  80. OnIncomingInstantMessage;
  81. }
  82. public void RemoveRegion(Scene scene)
  83. {
  84. m_scene.UnregisterModuleInterface<IGodsModule>(this);
  85. m_scene.EventManager.OnNewClient -= SubscribeToClientEvents;
  86. m_scene = null;
  87. }
  88. public void RegionLoaded(Scene scene)
  89. {
  90. }
  91. public void Close() {}
  92. public string Name { get { return "Gods Module"; } }
  93. public Type ReplaceableInterface
  94. {
  95. get { return null; }
  96. }
  97. public void SubscribeToClientEvents(IClientAPI client)
  98. {
  99. client.OnGodKickUser += KickUser;
  100. client.OnRequestGodlikePowers += RequestGodlikePowers;
  101. }
  102. public void UnsubscribeFromClientEvents(IClientAPI client)
  103. {
  104. client.OnGodKickUser -= KickUser;
  105. client.OnRequestGodlikePowers -= RequestGodlikePowers;
  106. }
  107. private void OnRegisterCaps(UUID agentID, Caps caps)
  108. {
  109. string uri = "/CAPS/" + UUID.Random();
  110. caps.RegisterHandler(
  111. "UntrustedSimulatorMessage",
  112. new RestStreamHandler("POST", uri, HandleUntrustedSimulatorMessage, "UntrustedSimulatorMessage", null));
  113. }
  114. private string HandleUntrustedSimulatorMessage(string request,
  115. string path, string param, IOSHttpRequest httpRequest,
  116. IOSHttpResponse httpResponse)
  117. {
  118. OSDMap osd = (OSDMap)OSDParser.DeserializeLLSDXml(request);
  119. string message = osd["message"].AsString();
  120. if (message == "GodKickUser")
  121. {
  122. OSDMap body = (OSDMap)osd["body"];
  123. OSDArray userInfo = (OSDArray)body["UserInfo"];
  124. OSDMap userData = (OSDMap)userInfo[0];
  125. UUID agentID = userData["AgentID"].AsUUID();
  126. UUID godID = userData["GodID"].AsUUID();
  127. UUID godSessionID = userData["GodSessionID"].AsUUID();
  128. uint kickFlags = userData["KickFlags"].AsUInteger();
  129. string reason = userData["Reason"].AsString();
  130. ScenePresence god = m_scene.GetScenePresence(godID);
  131. if (god == null || god.ControllingClient.SessionId != godSessionID)
  132. return String.Empty;
  133. KickUser(godID, godSessionID, agentID, kickFlags, Util.StringToBytes1024(reason));
  134. }
  135. else
  136. {
  137. m_log.ErrorFormat("[GOD]: Unhandled UntrustedSimulatorMessage: {0}", message);
  138. }
  139. return String.Empty;
  140. }
  141. public void RequestGodlikePowers(
  142. UUID agentID, UUID sessionID, UUID token, bool godLike, IClientAPI controllingClient)
  143. {
  144. ScenePresence sp = m_scene.GetScenePresence(agentID);
  145. if (sp != null)
  146. {
  147. if (godLike == false)
  148. {
  149. sp.GrantGodlikePowers(agentID, sessionID, token, godLike);
  150. return;
  151. }
  152. // First check that this is the sim owner
  153. if (m_scene.Permissions.IsGod(agentID))
  154. {
  155. // Next we check for spoofing.....
  156. UUID testSessionID = sp.ControllingClient.SessionId;
  157. if (sessionID == testSessionID)
  158. {
  159. if (sessionID == controllingClient.SessionId)
  160. {
  161. //m_log.Info("godlike: " + godLike.ToString());
  162. sp.GrantGodlikePowers(agentID, testSessionID, token, godLike);
  163. }
  164. }
  165. }
  166. else
  167. {
  168. if (DialogModule != null)
  169. DialogModule.SendAlertToUser(agentID, "Request for god powers denied");
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// Kicks User specified from the simulator. This logs them off of the grid
  175. /// If the client gets the UUID: 44e87126e7944ded05b37c42da3d5cdb it assumes
  176. /// that you're kicking it even if the avatar's UUID isn't the UUID that the
  177. /// agent is assigned
  178. /// </summary>
  179. /// <param name="godID">The person doing the kicking</param>
  180. /// <param name="sessionID">The session of the person doing the kicking</param>
  181. /// <param name="agentID">the person that is being kicked</param>
  182. /// <param name="kickflags">Tells what to do to the user</param>
  183. /// <param name="reason">The message to send to the user after it's been turned into a field</param>
  184. public void KickUser(UUID godID, UUID sessionID, UUID agentID, uint kickflags, byte[] reason)
  185. {
  186. if (!m_scene.Permissions.IsGod(godID))
  187. return;
  188. ScenePresence sp = m_scene.GetScenePresence(agentID);
  189. if (sp == null && agentID != ALL_AGENTS)
  190. {
  191. IMessageTransferModule transferModule =
  192. m_scene.RequestModuleInterface<IMessageTransferModule>();
  193. if (transferModule != null)
  194. {
  195. m_log.DebugFormat("[GODS]: Sending nonlocal kill for agent {0}", agentID);
  196. transferModule.SendInstantMessage(new GridInstantMessage(
  197. m_scene, godID, "God", agentID, (byte)250, false,
  198. Utils.BytesToString(reason), UUID.Zero, true,
  199. new Vector3(), new byte[] {(byte)kickflags}, true),
  200. delegate(bool success) {} );
  201. }
  202. return;
  203. }
  204. switch (kickflags)
  205. {
  206. case 0:
  207. if (sp != null)
  208. {
  209. KickPresence(sp, Utils.BytesToString(reason));
  210. }
  211. else if (agentID == ALL_AGENTS)
  212. {
  213. m_scene.ForEachRootScenePresence(
  214. delegate(ScenePresence p)
  215. {
  216. if (p.UUID != godID && (!m_scene.Permissions.IsGod(p.UUID)))
  217. KickPresence(p, Utils.BytesToString(reason));
  218. }
  219. );
  220. }
  221. break;
  222. case 1:
  223. if (sp != null)
  224. {
  225. sp.AllowMovement = false;
  226. m_dialogModule.SendAlertToUser(agentID, Utils.BytesToString(reason));
  227. m_dialogModule.SendAlertToUser(godID, "User Frozen");
  228. }
  229. break;
  230. case 2:
  231. if (sp != null)
  232. {
  233. sp.AllowMovement = true;
  234. m_dialogModule.SendAlertToUser(agentID, Utils.BytesToString(reason));
  235. m_dialogModule.SendAlertToUser(godID, "User Unfrozen");
  236. }
  237. break;
  238. default:
  239. break;
  240. }
  241. }
  242. private void KickPresence(ScenePresence sp, string reason)
  243. {
  244. if (sp.IsChildAgent)
  245. return;
  246. sp.ControllingClient.Kick(reason);
  247. sp.Scene.IncomingCloseAgent(sp.UUID, true);
  248. }
  249. private void OnIncomingInstantMessage(GridInstantMessage msg)
  250. {
  251. if (msg.dialog == (uint)250) // Nonlocal kick
  252. {
  253. UUID agentID = new UUID(msg.toAgentID);
  254. string reason = msg.message;
  255. UUID godID = new UUID(msg.fromAgentID);
  256. uint kickMode = (uint)msg.binaryBucket[0];
  257. KickUser(godID, UUID.Zero, agentID, kickMode, Util.StringToBytes1024(reason));
  258. }
  259. }
  260. }
  261. }