HGFriendsServerPostHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 Nini.Config;
  28. using log4net;
  29. using System;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Net;
  33. using System.Text;
  34. using System.Text.RegularExpressions;
  35. using System.Xml;
  36. using System.Xml.Serialization;
  37. using System.Collections.Generic;
  38. using OpenSim.Server.Base;
  39. using OpenSim.Services.Interfaces;
  40. using FriendInfo = OpenSim.Services.Interfaces.FriendInfo;
  41. using OpenSim.Framework;
  42. using OpenSim.Framework.Servers.HttpServer;
  43. using OpenMetaverse;
  44. namespace OpenSim.Server.Handlers.Hypergrid
  45. {
  46. public class HGFriendsServerPostHandler : BaseStreamHandler
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private IFriendsService m_FriendsService;
  50. private IUserAgentService m_UserAgentService;
  51. public HGFriendsServerPostHandler(IFriendsService service, IUserAgentService uservice) :
  52. base("POST", "/hgfriends")
  53. {
  54. m_FriendsService = service;
  55. m_UserAgentService = uservice;
  56. m_log.DebugFormat("[HGFRIENDS HANDLER]: HGFriendsServerPostHandler is On");
  57. }
  58. public override byte[] Handle(string path, Stream requestData,
  59. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  60. {
  61. StreamReader sr = new StreamReader(requestData);
  62. string body = sr.ReadToEnd();
  63. sr.Close();
  64. body = body.Trim();
  65. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  66. try
  67. {
  68. Dictionary<string, object> request =
  69. ServerUtils.ParseQueryString(body);
  70. if (!request.ContainsKey("METHOD"))
  71. return FailureResult();
  72. string method = request["METHOD"].ToString();
  73. switch (method)
  74. {
  75. case "getfriendperms":
  76. return GetFriendPerms(request);
  77. case "newfriendship":
  78. return NewFriendship(request);
  79. case "deletefriendship":
  80. return DeleteFriendship(request);
  81. }
  82. m_log.DebugFormat("[HGFRIENDS HANDLER]: unknown method {0} request {1}", method.Length, method);
  83. }
  84. catch (Exception e)
  85. {
  86. m_log.DebugFormat("[HGFRIENDS HANDLER]: Exception {0}", e);
  87. }
  88. return FailureResult();
  89. }
  90. #region Method-specific handlers
  91. byte[] GetFriendPerms(Dictionary<string, object> request)
  92. {
  93. if (!VerifyServiceKey(request))
  94. return FailureResult();
  95. UUID principalID = UUID.Zero;
  96. if (request.ContainsKey("PRINCIPALID"))
  97. UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID);
  98. else
  99. {
  100. m_log.WarnFormat("[HGFRIENDS HANDLER]: no principalID in request to get friend perms");
  101. return FailureResult();
  102. }
  103. UUID friendID = UUID.Zero;
  104. if (request.ContainsKey("FRIENDID"))
  105. UUID.TryParse(request["FRIENDID"].ToString(), out friendID);
  106. else
  107. {
  108. m_log.WarnFormat("[HGFRIENDS HANDLER]: no friendID in request to get friend perms");
  109. return FailureResult();
  110. }
  111. string perms = "0";
  112. FriendInfo[] friendsInfo = m_FriendsService.GetFriends(principalID);
  113. foreach (FriendInfo finfo in friendsInfo)
  114. {
  115. if (finfo.Friend.StartsWith(friendID.ToString()))
  116. return SuccessResult(finfo.TheirFlags.ToString());
  117. }
  118. return FailureResult("Friend not found");
  119. }
  120. byte[] NewFriendship(Dictionary<string, object> request)
  121. {
  122. if (!VerifyServiceKey(request))
  123. return FailureResult();
  124. // OK, can proceed
  125. FriendInfo friend = new FriendInfo(request);
  126. UUID friendID;
  127. string tmp = string.Empty;
  128. if (!Util.ParseUniversalUserIdentifier(friend.Friend, out friendID, out tmp, out tmp, out tmp, out tmp))
  129. return FailureResult();
  130. m_log.DebugFormat("[HGFRIENDS HANDLER]: New friendship {0} {1}", friend.PrincipalID, friend.Friend);
  131. // If the friendship already exists, return fail
  132. FriendInfo[] finfos = m_FriendsService.GetFriends(friend.PrincipalID);
  133. foreach (FriendInfo finfo in finfos)
  134. if (finfo.Friend.StartsWith(friendID.ToString()))
  135. return FailureResult();
  136. // the user needs to confirm when he gets home
  137. bool success = m_FriendsService.StoreFriend(friend.PrincipalID.ToString(), friend.Friend, 0);
  138. if (success)
  139. return SuccessResult();
  140. else
  141. return FailureResult();
  142. }
  143. byte[] DeleteFriendship(Dictionary<string, object> request)
  144. {
  145. FriendInfo friend = new FriendInfo(request);
  146. string secret = string.Empty;
  147. if (request.ContainsKey("SECRET"))
  148. secret = request["SECRET"].ToString();
  149. if (secret == string.Empty)
  150. return FailureResult();
  151. FriendInfo[] finfos = m_FriendsService.GetFriends(friend.PrincipalID);
  152. foreach (FriendInfo finfo in finfos)
  153. {
  154. // We check the secret here
  155. if (finfo.Friend.StartsWith(friend.Friend) && finfo.Friend.EndsWith(secret))
  156. {
  157. m_log.DebugFormat("[HGFRIENDS HANDLER]: Delete friendship {0} {1}", friend.PrincipalID, friend.Friend);
  158. m_FriendsService.Delete(friend.PrincipalID, finfo.Friend);
  159. m_FriendsService.Delete(finfo.Friend, friend.PrincipalID.ToString());
  160. return SuccessResult();
  161. }
  162. }
  163. return FailureResult();
  164. }
  165. #endregion
  166. #region Misc
  167. private bool VerifyServiceKey(Dictionary<string, object> request)
  168. {
  169. if (!request.ContainsKey("KEY") || !request.ContainsKey("SESSIONID"))
  170. {
  171. m_log.WarnFormat("[HGFRIENDS HANDLER]: ignoring request without Key or SessionID");
  172. return false;
  173. }
  174. string serviceKey = request["KEY"].ToString();
  175. string sessionStr = request["SESSIONID"].ToString();
  176. UUID sessionID;
  177. UUID.TryParse(sessionStr, out sessionID);
  178. if (!m_UserAgentService.VerifyAgent(sessionID, serviceKey))
  179. {
  180. m_log.WarnFormat("[HGFRIENDS HANDLER]: Key {0} for session {1} did not match existing key. Ignoring request", serviceKey, sessionID);
  181. return false;
  182. }
  183. m_log.DebugFormat("[HGFRIENDS HANDLER]: Verification ok");
  184. return true;
  185. }
  186. private byte[] SuccessResult()
  187. {
  188. XmlDocument doc = new XmlDocument();
  189. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  190. "", "");
  191. doc.AppendChild(xmlnode);
  192. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  193. "");
  194. doc.AppendChild(rootElement);
  195. XmlElement result = doc.CreateElement("", "Result", "");
  196. result.AppendChild(doc.CreateTextNode("Success"));
  197. rootElement.AppendChild(result);
  198. return DocToBytes(doc);
  199. }
  200. private byte[] SuccessResult(string value)
  201. {
  202. XmlDocument doc = new XmlDocument();
  203. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  204. "", "");
  205. doc.AppendChild(xmlnode);
  206. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  207. "");
  208. doc.AppendChild(rootElement);
  209. XmlElement result = doc.CreateElement("", "Result", "");
  210. result.AppendChild(doc.CreateTextNode("Success"));
  211. rootElement.AppendChild(result);
  212. XmlElement message = doc.CreateElement("", "Value", "");
  213. message.AppendChild(doc.CreateTextNode(value));
  214. rootElement.AppendChild(message);
  215. return DocToBytes(doc);
  216. }
  217. private byte[] FailureResult()
  218. {
  219. return FailureResult(String.Empty);
  220. }
  221. private byte[] FailureResult(string msg)
  222. {
  223. XmlDocument doc = new XmlDocument();
  224. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  225. "", "");
  226. doc.AppendChild(xmlnode);
  227. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  228. "");
  229. doc.AppendChild(rootElement);
  230. XmlElement result = doc.CreateElement("", "Result", "");
  231. result.AppendChild(doc.CreateTextNode("Failure"));
  232. rootElement.AppendChild(result);
  233. XmlElement message = doc.CreateElement("", "Message", "");
  234. message.AppendChild(doc.CreateTextNode(msg));
  235. rootElement.AppendChild(message);
  236. return DocToBytes(doc);
  237. }
  238. private byte[] DocToBytes(XmlDocument doc)
  239. {
  240. MemoryStream ms = new MemoryStream();
  241. XmlTextWriter xw = new XmlTextWriter(ms, null);
  242. xw.Formatting = Formatting.Indented;
  243. doc.WriteTo(xw);
  244. xw.Flush();
  245. return ms.ToArray();
  246. }
  247. #endregion
  248. }
  249. }