1
0

UserAccountServicesConnector.cs 17 KB

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