UserAccountServicesConnector.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.ServiceAuth;
  35. using OpenSim.Server.Base;
  36. using OpenSim.Services.Interfaces;
  37. using OpenMetaverse;
  38. namespace OpenSim.Services.Connectors
  39. {
  40. public class UserAccountServicesConnector : BaseServiceConnector, IUserAccountService
  41. {
  42. private static readonly ILog m_log =
  43. LogManager.GetLogger(
  44. MethodBase.GetCurrentMethod().DeclaringType);
  45. private string m_ServerURI = String.Empty;
  46. public UserAccountServicesConnector()
  47. {
  48. }
  49. public UserAccountServicesConnector(string serverURI)
  50. {
  51. m_ServerURI = serverURI.TrimEnd('/');
  52. }
  53. public UserAccountServicesConnector(IConfigSource source)
  54. {
  55. Initialise(source);
  56. }
  57. public virtual void Initialise(IConfigSource source)
  58. {
  59. IConfig assetConfig = source.Configs["UserAccountService"];
  60. if (assetConfig == null)
  61. {
  62. m_log.Error("[ACCOUNT CONNECTOR]: UserAccountService missing from OpenSim.ini");
  63. throw new Exception("User account connector init error");
  64. }
  65. string serviceURI = assetConfig.GetString("UserAccountServerURI", string.Empty);
  66. if (string.IsNullOrWhiteSpace(serviceURI))
  67. {
  68. m_log.Error("[ACCOUNT CONNECTOR]: UserAccountServerURI not found in section UserAccountService");
  69. throw new Exception("User account connector init error");
  70. }
  71. OSHHTPHost tmp = new OSHHTPHost(serviceURI, true);
  72. if (!tmp.IsResolvedHost)
  73. {
  74. m_log.ErrorFormat("[ACCOUNT CONNECTOR]: {0}", tmp.IsValidHost ? "Could not resolve UserAccountServerURI" : "UserAccountServerURI is a invalid host");
  75. throw new Exception("User account connector init error");
  76. }
  77. m_ServerURI = tmp.URI;
  78. base.Initialise(source, "UserAccountService");
  79. }
  80. public virtual UserAccount GetUserAccount(UUID scopeID, string firstName, string lastName)
  81. {
  82. Dictionary<string, object> sendData = new Dictionary<string, object>();
  83. //sendData["SCOPEID"] = scopeID.ToString();
  84. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  85. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  86. sendData["METHOD"] = "getaccount";
  87. sendData["ScopeID"] = scopeID;
  88. sendData["FirstName"] = firstName.ToString();
  89. sendData["LastName"] = lastName.ToString();
  90. return SendAndGetReply(sendData);
  91. }
  92. public virtual UserAccount GetUserAccount(UUID scopeID, string email)
  93. {
  94. Dictionary<string, object> sendData = new Dictionary<string, object>();
  95. //sendData["SCOPEID"] = scopeID.ToString();
  96. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  97. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  98. sendData["METHOD"] = "getaccount";
  99. sendData["ScopeID"] = scopeID;
  100. sendData["Email"] = email;
  101. return SendAndGetReply(sendData);
  102. }
  103. public virtual UserAccount GetUserAccount(UUID scopeID, UUID userID)
  104. {
  105. //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetUserAccount {0}", userID);
  106. Dictionary<string, object> sendData = new Dictionary<string, object>();
  107. //sendData["SCOPEID"] = scopeID.ToString();
  108. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  109. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  110. sendData["METHOD"] = "getaccount";
  111. sendData["ScopeID"] = scopeID;
  112. sendData["UserID"] = userID.ToString();
  113. return SendAndGetReply(sendData);
  114. }
  115. public List<UserAccount> GetUserAccounts(UUID scopeID, string query)
  116. {
  117. Dictionary<string, object> sendData = new Dictionary<string, object>();
  118. //sendData["SCOPEID"] = scopeID.ToString();
  119. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  120. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  121. sendData["METHOD"] = "getaccounts";
  122. sendData["ScopeID"] = scopeID.ToString();
  123. sendData["query"] = query;
  124. string reply = string.Empty;
  125. string reqString = ServerUtils.BuildQueryString(sendData);
  126. string uri = m_ServerURI + "/accounts";
  127. // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString);
  128. try
  129. {
  130. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  131. uri,
  132. reqString,
  133. m_Auth);
  134. if (string.IsNullOrEmpty(reply))
  135. {
  136. m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccounts received null or empty reply");
  137. return null;
  138. }
  139. }
  140. catch (Exception e)
  141. {
  142. m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting user accounts server at {0}: {1}", uri, e.Message);
  143. }
  144. List<UserAccount> accounts = new List<UserAccount>();
  145. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  146. if (replyData != null)
  147. {
  148. if (replyData.ContainsKey("result") && replyData["result"].ToString() == "null")
  149. {
  150. return accounts;
  151. }
  152. Dictionary<string, object>.ValueCollection accountList = replyData.Values;
  153. //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetAgents returned {0} elements", pinfosList.Count);
  154. foreach (object acc in accountList)
  155. {
  156. if (acc is Dictionary<string, object>)
  157. {
  158. UserAccount pinfo = new UserAccount((Dictionary<string, object>)acc);
  159. accounts.Add(pinfo);
  160. }
  161. else
  162. m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccounts received invalid response type {0}",
  163. acc.GetType());
  164. }
  165. }
  166. else
  167. m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetUserAccounts received null response");
  168. return accounts;
  169. }
  170. public virtual List<UserAccount> GetUserAccounts(UUID scopeID, List<string> IDs)
  171. {
  172. List<UserAccount> accs = new List<UserAccount>();
  173. bool multisuported = true;
  174. accs = doGetMultiUserAccounts(scopeID, IDs, out multisuported);
  175. if(multisuported)
  176. return accs;
  177. // service does not do multi accounts so need to do it one by one
  178. UUID uuid = UUID.Zero;
  179. foreach(string id in IDs)
  180. {
  181. if(UUID.TryParse(id, out uuid) && !uuid.IsZero())
  182. accs.Add(GetUserAccount(scopeID,uuid));
  183. }
  184. return accs;
  185. }
  186. private List<UserAccount> doGetMultiUserAccounts(UUID scopeID, List<string> IDs, out bool suported)
  187. {
  188. suported = true;
  189. Dictionary<string, object> sendData = new Dictionary<string, object>();
  190. //sendData["SCOPEID"] = scopeID.ToString();
  191. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  192. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  193. sendData["METHOD"] = "getmultiaccounts";
  194. sendData["ScopeID"] = scopeID.ToString();
  195. sendData["IDS"] = new List<string>(IDs);
  196. string reply = string.Empty;
  197. string reqString = ServerUtils.BuildQueryString(sendData);
  198. string uri = m_ServerURI + "/accounts";
  199. // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString);
  200. try
  201. {
  202. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  203. uri,
  204. reqString,
  205. m_Auth);
  206. if (string.IsNullOrEmpty(reply))
  207. {
  208. m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetMultiUserAccounts received null or empty reply");
  209. return null;
  210. }
  211. }
  212. catch (Exception e)
  213. {
  214. m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting user accounts server at {0}: {1}", uri, e.Message);
  215. }
  216. List<UserAccount> accounts = new List<UserAccount>();
  217. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  218. if (replyData != null)
  219. {
  220. if (replyData.ContainsKey("result"))
  221. {
  222. if(replyData["result"].ToString() == "null")
  223. return accounts;
  224. if(replyData["result"].ToString() == "Failure")
  225. {
  226. suported = false;
  227. return accounts;
  228. }
  229. }
  230. Dictionary<string, object>.ValueCollection accountList = replyData.Values;
  231. //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetAgents returned {0} elements", pinfosList.Count);
  232. foreach (object acc in accountList)
  233. {
  234. if (acc is Dictionary<string, object>)
  235. {
  236. UserAccount pinfo = new UserAccount((Dictionary<string, object>)acc);
  237. accounts.Add(pinfo);
  238. }
  239. else
  240. m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetMultiUserAccounts received invalid response type {0}",
  241. acc.GetType());
  242. }
  243. }
  244. else
  245. m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetMultiUserAccounts received null response");
  246. return accounts;
  247. }
  248. public void InvalidateCache(UUID userID)
  249. {
  250. }
  251. public List<UserAccount> GetUserAccountsWhere(UUID scopeID, string where)
  252. {
  253. return null; // Not implemented for regions
  254. }
  255. public virtual bool StoreUserAccount(UserAccount data)
  256. {
  257. Dictionary<string, object> sendData = new Dictionary<string, object>();
  258. //sendData["SCOPEID"] = scopeID.ToString();
  259. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  260. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  261. sendData["METHOD"] = "setaccount";
  262. Dictionary<string, object> structData = data.ToKeyValuePairs();
  263. foreach (KeyValuePair<string, object> kvp in structData)
  264. {
  265. if (kvp.Value == null)
  266. {
  267. m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Null value for {0}", kvp.Key);
  268. continue;
  269. }
  270. sendData[kvp.Key] = kvp.Value.ToString();
  271. }
  272. if (SendAndGetReply(sendData) != null)
  273. return true;
  274. else
  275. return false;
  276. }
  277. /// <summary>
  278. /// Create user remotely. Note this this is not part of the IUserAccountsService
  279. /// </summary>
  280. /// <param name="first"></param>
  281. /// <param name="last"></param>
  282. /// <param name="password"></param>
  283. /// <param name="email"></param>
  284. /// <param name="scopeID"></param>
  285. /// <returns></returns>
  286. public virtual UserAccount CreateUser(string first, string last, string password, string email, UUID scopeID)
  287. {
  288. Dictionary<string, object> sendData = new Dictionary<string, object>();
  289. //sendData["SCOPEID"] = scopeID.ToString();
  290. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  291. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  292. sendData["METHOD"] = "createuser";
  293. sendData["FirstName"] = first;
  294. sendData["LastName"] = last;
  295. sendData["Password"] = password;
  296. if (!string.IsNullOrEmpty(email))
  297. sendData["Email"] = first;
  298. sendData["ScopeID"] = scopeID.ToString();
  299. return SendAndGetReply(sendData);
  300. }
  301. private UserAccount SendAndGetReply(Dictionary<string, object> sendData)
  302. {
  303. string reply = string.Empty;
  304. string reqString = ServerUtils.BuildQueryString(sendData);
  305. string uri = m_ServerURI + "/accounts";
  306. // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString);
  307. try
  308. {
  309. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  310. uri,
  311. reqString,
  312. m_Auth);
  313. if (string.IsNullOrEmpty(reply))
  314. {
  315. m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccount received null or empty reply");
  316. return null;
  317. }
  318. }
  319. catch (Exception e)
  320. {
  321. m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting user accounts server at {0}: {1}", uri, e.Message);
  322. }
  323. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  324. UserAccount account = null;
  325. if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null))
  326. {
  327. if (replyData["result"] is Dictionary<string, object>)
  328. {
  329. account = new UserAccount((Dictionary<string, object>)replyData["result"]);
  330. }
  331. }
  332. return account;
  333. }
  334. private bool SendAndGetBoolReply(Dictionary<string, object> sendData)
  335. {
  336. string reqString = ServerUtils.BuildQueryString(sendData);
  337. string uri = m_ServerURI + "/accounts";
  338. //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString);
  339. try
  340. {
  341. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  342. uri,
  343. reqString,
  344. m_Auth);
  345. if (reply != string.Empty)
  346. {
  347. //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: reply = {0}", reply);
  348. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  349. if (replyData.ContainsKey("result"))
  350. {
  351. if (replyData["result"].ToString().ToLower() == "success")
  352. return true;
  353. else
  354. return false;
  355. }
  356. else
  357. m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Set or Create UserAccount reply data does not contain result field");
  358. }
  359. else
  360. m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Set or Create UserAccount received empty reply");
  361. }
  362. catch (Exception e)
  363. {
  364. m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting user accounts server at {0}: {1}", uri, e.Message);
  365. }
  366. return false;
  367. }
  368. }
  369. }