UserAccountServerPostHandler.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 OpenSim.Services.UserAccountService;
  41. using OpenSim.Framework;
  42. using OpenSim.Framework.Servers.HttpServer;
  43. using OpenSim.Framework.ServiceAuth;
  44. using OpenMetaverse;
  45. namespace OpenSim.Server.Handlers.UserAccounts
  46. {
  47. public class UserAccountServerPostHandler : BaseStreamHandler
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private IUserAccountService m_UserAccountService;
  51. private bool m_AllowCreateUser = false;
  52. private bool m_AllowSetAccount = false;
  53. public UserAccountServerPostHandler(IUserAccountService service)
  54. : this(service, null, null) {}
  55. public UserAccountServerPostHandler(IUserAccountService service, IConfig config, IServiceAuth auth) :
  56. base("POST", "/accounts", auth)
  57. {
  58. m_UserAccountService = service;
  59. if (config != null)
  60. {
  61. m_AllowCreateUser = config.GetBoolean("AllowCreateUser", m_AllowCreateUser);
  62. m_AllowSetAccount = config.GetBoolean("AllowSetAccount", m_AllowSetAccount);
  63. }
  64. }
  65. protected override byte[] ProcessRequest(string path, Stream requestData,
  66. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  67. {
  68. string body;
  69. using(StreamReader sr = new StreamReader(requestData))
  70. body = sr.ReadToEnd();
  71. body = body.Trim();
  72. // We need to check the authorization header
  73. //httpRequest.Headers["authorization"] ...
  74. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  75. string method = string.Empty;
  76. try
  77. {
  78. Dictionary<string, object> request = ServerUtils.ParseQueryString(body);
  79. if (!request.ContainsKey("METHOD"))
  80. return FailureResult();
  81. method = request["METHOD"].ToString();
  82. switch (method)
  83. {
  84. case "createuser":
  85. if (m_AllowCreateUser)
  86. return CreateUser(request);
  87. else
  88. return FailureResult();
  89. case "getaccount":
  90. return GetAccount(request);
  91. case "getaccounts":
  92. return GetAccounts(request);
  93. case "getmultiaccounts":
  94. return GetMultiAccounts(request);
  95. case "setaccount":
  96. if (m_AllowSetAccount)
  97. return StoreAccount(request);
  98. else
  99. return FailureResult();
  100. }
  101. m_log.DebugFormat("[USER SERVICE HANDLER]: unknown method request: {0}", method);
  102. }
  103. catch (Exception e)
  104. {
  105. m_log.DebugFormat("[USER SERVICE HANDLER]: Exception in method {0}: {1}", method, e);
  106. }
  107. return FailureResult();
  108. }
  109. byte[] GetAccount(Dictionary<string, object> request)
  110. {
  111. UserAccount account = null;
  112. UUID scopeID = UUID.Zero;
  113. Dictionary<string, object> result = new Dictionary<string, object>();
  114. object otmp;
  115. if (request.TryGetValue("ScopeID", out otmp) && !UUID.TryParse(otmp.ToString(), out scopeID))
  116. {
  117. result["result"] = "null";
  118. return ResultToBytes(result);
  119. }
  120. if (request.TryGetValue("UserID", out otmp) && otmp != null)
  121. {
  122. if (UUID.TryParse(otmp.ToString(), out UUID userID))
  123. account = m_UserAccountService.GetUserAccount(scopeID, userID);
  124. }
  125. else if (request.TryGetValue("PrincipalID", out otmp) && otmp != null)
  126. {
  127. if (UUID.TryParse(otmp.ToString(), out UUID userID))
  128. account = m_UserAccountService.GetUserAccount(scopeID, userID);
  129. }
  130. else if (request.TryGetValue("Email", out otmp) && otmp != null)
  131. {
  132. account = m_UserAccountService.GetUserAccount(scopeID, otmp.ToString());
  133. }
  134. else if (request.TryGetValue("FirstName", out object ofn) && ofn != null &&
  135. request.TryGetValue("LastName", out object oln) && oln != null)
  136. {
  137. account = m_UserAccountService.GetUserAccount(scopeID, ofn.ToString(), oln.ToString());
  138. }
  139. if (account == null)
  140. {
  141. result["result"] = "null";
  142. }
  143. else
  144. {
  145. result["result"] = account.ToKeyValuePairs();
  146. }
  147. return ResultToBytes(result);
  148. }
  149. byte[] GetAccounts(Dictionary<string, object> request)
  150. {
  151. if (!request.TryGetValue("query", out object oquery) || oquery == null)
  152. return FailureResult();
  153. UUID scopeID = UUID.Zero;
  154. if (request.TryGetValue("ScopeID", out object oscope) && !UUID.TryParse(oscope.ToString(), out scopeID))
  155. return FailureResult();
  156. List<UserAccount> accounts = null;
  157. string query = oquery.ToString().Trim();
  158. if(!string.IsNullOrEmpty(query))
  159. accounts = m_UserAccountService.GetUserAccounts(scopeID, query);
  160. Dictionary<string, object> result = new Dictionary<string, object>();
  161. if ((accounts == null) || accounts.Count == 0)
  162. {
  163. result["result"] = "null";
  164. }
  165. else
  166. {
  167. int i = 0;
  168. foreach (UserAccount acc in accounts)
  169. {
  170. Dictionary<string, object> rinfoDict = acc.ToKeyValuePairs();
  171. result["account" + i] = rinfoDict;
  172. i++;
  173. }
  174. }
  175. string xmlString = ServerUtils.BuildXmlResponse(result);
  176. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  177. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  178. }
  179. byte[] GetMultiAccounts(Dictionary<string, object> request)
  180. {
  181. UUID scopeID = UUID.Zero;
  182. if (request.TryGetValue("ScopeID", out object oscope) && !UUID.TryParse(oscope.ToString(), out scopeID))
  183. return FailureResult();
  184. if (!request.TryGetValue("IDS", out object oids))
  185. {
  186. m_log.DebugFormat("[USER SERVICE HANDLER]: GetMultiAccounts called without required uuids argument");
  187. return FailureResult();
  188. }
  189. List<string> lids = oids as List<string>;
  190. if (lids == null)
  191. {
  192. m_log.DebugFormat("[USER SERVICE HANDLER]: GetMultiAccounts input argument was of unexpected type {0} or null", oids.GetType().ToString());
  193. return FailureResult();
  194. }
  195. List<string> userIDs = new List<string>(lids.Count);
  196. foreach (string s in lids)
  197. {
  198. if(UUID.TryParse(s, out UUID tmpid))
  199. userIDs.Add(s);
  200. }
  201. List<UserAccount> accounts = null;
  202. if (userIDs.Count > 0)
  203. accounts = m_UserAccountService.GetUserAccounts(scopeID, userIDs);
  204. Dictionary<string, object> result = new Dictionary<string, object>();
  205. if ((accounts == null) || accounts.Count == 0)
  206. {
  207. result["result"] = "null";
  208. }
  209. else
  210. {
  211. int i = 0;
  212. foreach (UserAccount acc in accounts)
  213. {
  214. if(acc == null)
  215. continue;
  216. Dictionary<string, object> rinfoDict = acc.ToKeyValuePairs();
  217. result["account" + i] = rinfoDict;
  218. i++;
  219. }
  220. }
  221. string xmlString = ServerUtils.BuildXmlResponse(result);
  222. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  223. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  224. }
  225. byte[] StoreAccount(Dictionary<string, object> request)
  226. {
  227. object otmp;
  228. UUID principalID = UUID.Zero;
  229. if (request.TryGetValue("PrincipalID", out otmp) && !UUID.TryParse(otmp.ToString(), out principalID) )
  230. return FailureResult();
  231. if(principalID.IsZero())
  232. return FailureResult();
  233. UUID scopeID = UUID.Zero;
  234. if (request.TryGetValue("ScopeID", out otmp) && !UUID.TryParse(otmp.ToString(), out scopeID))
  235. return FailureResult();
  236. UserAccount existingAccount = m_UserAccountService.GetUserAccount(scopeID, principalID);
  237. if (existingAccount == null)
  238. return FailureResult();
  239. if (request.TryGetValue("FirstName", out otmp))
  240. existingAccount.FirstName = otmp.ToString();
  241. if (request.TryGetValue("LastName", out otmp))
  242. existingAccount.LastName = otmp.ToString();
  243. if (request.TryGetValue("Email", out otmp))
  244. existingAccount.Email = otmp.ToString();
  245. int created = 0;
  246. if (request.TryGetValue("Created", out otmp) && int.TryParse(otmp.ToString(), out created))
  247. existingAccount.Created = created;
  248. int userLevel = 0;
  249. if (request.TryGetValue("UserLevel", out otmp) && int.TryParse(otmp.ToString(), out userLevel))
  250. existingAccount.UserLevel = userLevel;
  251. int userFlags = 0;
  252. if (request.TryGetValue("UserFlags", out otmp) && int.TryParse(otmp.ToString(), out userFlags))
  253. existingAccount.UserFlags = userFlags;
  254. if (request.TryGetValue("UserTitle", out otmp))
  255. existingAccount.UserTitle = otmp.ToString();
  256. if (!m_UserAccountService.StoreUserAccount(existingAccount))
  257. {
  258. m_log.ErrorFormat(
  259. "[USER ACCOUNT SERVER POST HANDLER]: Account store failed for account {0} {1} {2}",
  260. existingAccount.FirstName, existingAccount.LastName, existingAccount.PrincipalID);
  261. return FailureResult();
  262. }
  263. Dictionary<string, object> result = new Dictionary<string, object>();
  264. result["result"] = existingAccount.ToKeyValuePairs();
  265. return ResultToBytes(result);
  266. }
  267. byte[] CreateUser(Dictionary<string, object> request)
  268. {
  269. if (!(m_UserAccountService is UserAccountService))
  270. return FailureResult();
  271. object otmp;
  272. if (!request.TryGetValue("FirstName", out otmp) || otmp == null)
  273. return FailureResult();
  274. string firstName = otmp.ToString();
  275. if(!request.TryGetValue("LastName", out otmp) || otmp == null)
  276. return FailureResult();
  277. string lastName = otmp.ToString();
  278. if(!request.TryGetValue("Password", out otmp) || otmp == null)
  279. return FailureResult();
  280. string password = otmp.ToString();
  281. UUID scopeID = UUID.Zero;
  282. if (request.TryGetValue("ScopeID", out otmp) && !UUID.TryParse(otmp.ToString(), out scopeID))
  283. return FailureResult();
  284. UUID principalID = UUID.Random();
  285. if (request.TryGetValue("PrincipalID", out otmp) && !UUID.TryParse(otmp.ToString(), out principalID))
  286. return FailureResult();
  287. string email = "";
  288. if (request.TryGetValue("Email", out otmp))
  289. email = otmp.ToString();
  290. string model = "";
  291. if (request.TryGetValue("Model", out otmp))
  292. model = otmp.ToString();
  293. UserAccount createdUserAccount = ((UserAccountService)m_UserAccountService).CreateUser(
  294. scopeID, principalID, firstName, lastName, password, email, model);
  295. if (createdUserAccount == null)
  296. return FailureResult();
  297. Dictionary<string, object> result = new Dictionary<string, object>();
  298. result["result"] = createdUserAccount.ToKeyValuePairs();
  299. return ResultToBytes(result);
  300. }
  301. /*
  302. private byte[] SuccessResult()
  303. {
  304. XmlDocument doc = new XmlDocument();
  305. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
  306. doc.AppendChild(xmlnode);
  307. XmlElement rootElement = doc.CreateElement("", "ServerResponse", "");
  308. doc.AppendChild(rootElement);
  309. XmlElement result = doc.CreateElement("", "result", "");
  310. result.AppendChild(doc.CreateTextNode("Success"));
  311. rootElement.AppendChild(result);
  312. return Util.DocToBytes(doc);
  313. }
  314. */
  315. private static byte[] ResultFailureBytes = osUTF8.GetASCIIBytes("<?xml version =\"1.0\"?><ServerResponse><result>Failure</result></ServerResponse>");
  316. private byte[] FailureResult()
  317. {
  318. /*
  319. XmlDocument doc = new XmlDocument();
  320. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
  321. doc.AppendChild(xmlnode);
  322. XmlElement rootElement = doc.CreateElement("", "ServerResponse", "");
  323. doc.AppendChild(rootElement);
  324. XmlElement result = doc.CreateElement("", "result", "");
  325. result.AppendChild(doc.CreateTextNode("Failure"));
  326. rootElement.AppendChild(result);
  327. return Util.DocToBytes(doc);
  328. */
  329. return ResultFailureBytes;
  330. }
  331. private byte[] ResultToBytes(Dictionary<string, object> result)
  332. {
  333. string xmlString = ServerUtils.BuildXmlResponse(result);
  334. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  335. }
  336. }
  337. }