HGFriendsServerPostHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. IOSHttpRequest httpRequest, IOSHttpResponse 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}", 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. FriendInfo[] friendsInfo = m_FriendsService.GetFriends(principalID);
  112. foreach (FriendInfo finfo in friendsInfo)
  113. {
  114. if (finfo.Friend.StartsWith(friendID.ToString()))
  115. return SuccessResult(finfo.TheirFlags.ToString());
  116. }
  117. return FailureResult("Friend not found");
  118. }
  119. byte[] NewFriendship(Dictionary<string, object> request)
  120. {
  121. if (!VerifyServiceKey(request))
  122. return FailureResult();
  123. // OK, can proceed
  124. FriendInfo friend = new FriendInfo(request);
  125. UUID friendID;
  126. string tmp = string.Empty;
  127. if (!Util.ParseUniversalUserIdentifier(friend.Friend, out friendID, out tmp, out tmp, out tmp, out tmp))
  128. return FailureResult();
  129. m_log.DebugFormat("[HGFRIENDS HANDLER]: New friendship {0} {1}", friend.PrincipalID, friend.Friend);
  130. // If the friendship already exists, return fail
  131. FriendInfo[] finfos = m_FriendsService.GetFriends(friend.PrincipalID);
  132. foreach (FriendInfo finfo in finfos)
  133. if (finfo.Friend.StartsWith(friendID.ToString()))
  134. return FailureResult();
  135. // the user needs to confirm when he gets home
  136. bool success = m_FriendsService.StoreFriend(friend.PrincipalID.ToString(), friend.Friend, 0);
  137. if (success)
  138. return SuccessResult();
  139. else
  140. return FailureResult();
  141. }
  142. byte[] DeleteFriendship(Dictionary<string, object> request)
  143. {
  144. FriendInfo friend = new FriendInfo(request);
  145. string secret = string.Empty;
  146. if (request.ContainsKey("SECRET"))
  147. secret = request["SECRET"].ToString();
  148. if (secret == string.Empty)
  149. return FailureResult();
  150. FriendInfo[] finfos = m_FriendsService.GetFriends(friend.PrincipalID);
  151. foreach (FriendInfo finfo in finfos)
  152. {
  153. // We check the secret here
  154. if (finfo.Friend.StartsWith(friend.Friend) && finfo.Friend.EndsWith(secret))
  155. {
  156. m_log.DebugFormat("[HGFRIENDS HANDLER]: Delete friendship {0} {1}", friend.PrincipalID, friend.Friend);
  157. m_FriendsService.Delete(friend.PrincipalID, finfo.Friend);
  158. m_FriendsService.Delete(finfo.Friend, friend.PrincipalID.ToString());
  159. return SuccessResult();
  160. }
  161. }
  162. return FailureResult();
  163. }
  164. #endregion
  165. #region Misc
  166. private bool VerifyServiceKey(Dictionary<string, object> request)
  167. {
  168. if (!request.ContainsKey("KEY") || !request.ContainsKey("SESSIONID"))
  169. {
  170. m_log.WarnFormat("[HGFRIENDS HANDLER]: ignoring request without Key or SessionID");
  171. return false;
  172. }
  173. string serviceKey = request["KEY"].ToString();
  174. string sessionStr = request["SESSIONID"].ToString();
  175. UUID sessionID;
  176. UUID.TryParse(sessionStr, out sessionID);
  177. if (!m_UserAgentService.VerifyAgent(sessionID, serviceKey))
  178. {
  179. m_log.WarnFormat("[HGFRIENDS HANDLER]: Key {0} for session {1} did not match existing key. Ignoring request", serviceKey, sessionID);
  180. return false;
  181. }
  182. m_log.DebugFormat("[HGFRIENDS HANDLER]: Verification ok");
  183. return true;
  184. }
  185. private byte[] SuccessResult()
  186. {
  187. XmlDocument doc = new XmlDocument();
  188. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  189. "", "");
  190. doc.AppendChild(xmlnode);
  191. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  192. "");
  193. doc.AppendChild(rootElement);
  194. XmlElement result = doc.CreateElement("", "Result", "");
  195. result.AppendChild(doc.CreateTextNode("Success"));
  196. rootElement.AppendChild(result);
  197. return DocToBytes(doc);
  198. }
  199. private byte[] SuccessResult(string value)
  200. {
  201. XmlDocument doc = new XmlDocument();
  202. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  203. "", "");
  204. doc.AppendChild(xmlnode);
  205. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  206. "");
  207. doc.AppendChild(rootElement);
  208. XmlElement result = doc.CreateElement("", "Result", "");
  209. result.AppendChild(doc.CreateTextNode("Success"));
  210. rootElement.AppendChild(result);
  211. XmlElement message = doc.CreateElement("", "Value", "");
  212. message.AppendChild(doc.CreateTextNode(value));
  213. rootElement.AppendChild(message);
  214. return DocToBytes(doc);
  215. }
  216. private byte[] FailureResult()
  217. {
  218. return FailureResult(String.Empty);
  219. }
  220. private byte[] FailureResult(string msg)
  221. {
  222. XmlDocument doc = new XmlDocument();
  223. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  224. "", "");
  225. doc.AppendChild(xmlnode);
  226. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  227. "");
  228. doc.AppendChild(rootElement);
  229. XmlElement result = doc.CreateElement("", "Result", "");
  230. result.AppendChild(doc.CreateTextNode("Failure"));
  231. rootElement.AppendChild(result);
  232. XmlElement message = doc.CreateElement("", "Message", "");
  233. message.AppendChild(doc.CreateTextNode(msg));
  234. rootElement.AppendChild(message);
  235. return DocToBytes(doc);
  236. }
  237. private byte[] DocToBytes(XmlDocument doc)
  238. {
  239. MemoryStream ms = new MemoryStream();
  240. XmlTextWriter xw = new XmlTextWriter(ms, null);
  241. xw.Formatting = Formatting.Indented;
  242. doc.WriteTo(xw);
  243. xw.Flush();
  244. return ms.ToArray();
  245. }
  246. #endregion
  247. }
  248. }