MySQLGridData.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 System;
  28. using System.Collections.Generic;
  29. using System.Data;
  30. using System.Reflection;
  31. using System.Threading;
  32. using log4net;
  33. using MySql.Data.MySqlClient;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. namespace OpenSim.Data.MySQL
  37. {
  38. /// <summary>
  39. /// A MySQL Interface for the Grid Server
  40. /// </summary>
  41. public class MySQLGridData : GridDataBase
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private MySQLManager m_database;
  45. private object m_dbLock = new object();
  46. private string m_connectionString;
  47. override public void Initialise()
  48. {
  49. m_log.Info("[MySQLGridData]: " + Name + " cannot be default-initialized!");
  50. throw new PluginNotInitialisedException (Name);
  51. }
  52. /// <summary>
  53. /// <para>Initialises Grid interface</para>
  54. /// <para>
  55. /// <list type="bullet">
  56. /// <item>Loads and initialises the MySQL storage plugin</item>
  57. /// <item>Warns and uses the obsolete mysql_connection.ini if connect string is empty.</item>
  58. /// <item>Check for migration</item>
  59. /// </list>
  60. /// </para>
  61. /// </summary>
  62. /// <param name="connect">connect string.</param>
  63. override public void Initialise(string connect)
  64. {
  65. m_connectionString = connect;
  66. m_database = new MySQLManager(connect);
  67. // This actually does the roll forward assembly stuff
  68. Assembly assem = GetType().Assembly;
  69. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  70. {
  71. Migration m = new Migration(dbcon, assem, "GridStore");
  72. m.Update();
  73. }
  74. }
  75. /// <summary>
  76. /// Shuts down the grid interface
  77. /// </summary>
  78. override public void Dispose()
  79. {
  80. }
  81. /// <summary>
  82. /// Returns the plugin name
  83. /// </summary>
  84. /// <returns>Plugin name</returns>
  85. override public string Name
  86. {
  87. get { return "MySql OpenGridData"; }
  88. }
  89. /// <summary>
  90. /// Returns the plugin version
  91. /// </summary>
  92. /// <returns>Plugin version</returns>
  93. override public string Version
  94. {
  95. get { return "0.1"; }
  96. }
  97. /// <summary>
  98. /// Returns all the specified region profiles within coordates -- coordinates are inclusive
  99. /// </summary>
  100. /// <param name="xmin">Minimum X coordinate</param>
  101. /// <param name="ymin">Minimum Y coordinate</param>
  102. /// <param name="xmax">Maximum X coordinate</param>
  103. /// <param name="ymax">Maximum Y coordinate</param>
  104. /// <returns>Array of sim profiles</returns>
  105. override public RegionProfileData[] GetProfilesInRange(uint xmin, uint ymin, uint xmax, uint ymax)
  106. {
  107. try
  108. {
  109. Dictionary<string, object> param = new Dictionary<string, object>();
  110. param["?xmin"] = xmin.ToString();
  111. param["?ymin"] = ymin.ToString();
  112. param["?xmax"] = xmax.ToString();
  113. param["?ymax"] = ymax.ToString();
  114. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  115. {
  116. dbcon.Open();
  117. using (IDbCommand result = m_database.Query(dbcon,
  118. "SELECT * FROM regions WHERE locX >= ?xmin AND locX <= ?xmax AND locY >= ?ymin AND locY <= ?ymax",
  119. param))
  120. {
  121. using (IDataReader reader = result.ExecuteReader())
  122. {
  123. RegionProfileData row;
  124. List<RegionProfileData> rows = new List<RegionProfileData>();
  125. while ((row = m_database.readSimRow(reader)) != null)
  126. rows.Add(row);
  127. return rows.ToArray();
  128. }
  129. }
  130. }
  131. }
  132. catch (Exception e)
  133. {
  134. m_log.Error(e.Message, e);
  135. return null;
  136. }
  137. }
  138. /// <summary>
  139. /// Returns up to maxNum profiles of regions that have a name starting with namePrefix
  140. /// </summary>
  141. /// <param name="name">The name to match against</param>
  142. /// <param name="maxNum">Maximum number of profiles to return</param>
  143. /// <returns>A list of sim profiles</returns>
  144. override public List<RegionProfileData> GetRegionsByName(string namePrefix, uint maxNum)
  145. {
  146. try
  147. {
  148. Dictionary<string, object> param = new Dictionary<string, object>();
  149. param["?name"] = namePrefix + "%";
  150. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  151. {
  152. dbcon.Open();
  153. using (IDbCommand result = m_database.Query(dbcon,
  154. "SELECT * FROM regions WHERE regionName LIKE ?name",
  155. param))
  156. {
  157. using (IDataReader reader = result.ExecuteReader())
  158. {
  159. RegionProfileData row;
  160. List<RegionProfileData> rows = new List<RegionProfileData>();
  161. while (rows.Count < maxNum && (row = m_database.readSimRow(reader)) != null)
  162. rows.Add(row);
  163. return rows;
  164. }
  165. }
  166. }
  167. }
  168. catch (Exception e)
  169. {
  170. m_log.Error(e.Message, e);
  171. return null;
  172. }
  173. }
  174. /// <summary>
  175. /// Returns a sim profile from it's location
  176. /// </summary>
  177. /// <param name="handle">Region location handle</param>
  178. /// <returns>Sim profile</returns>
  179. override public RegionProfileData GetProfileByHandle(ulong handle)
  180. {
  181. try
  182. {
  183. Dictionary<string, object> param = new Dictionary<string, object>();
  184. param["?handle"] = handle.ToString();
  185. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  186. {
  187. dbcon.Open();
  188. using (IDbCommand result = m_database.Query(dbcon, "SELECT * FROM regions WHERE regionHandle = ?handle", param))
  189. {
  190. using (IDataReader reader = result.ExecuteReader())
  191. {
  192. RegionProfileData row = m_database.readSimRow(reader);
  193. return row;
  194. }
  195. }
  196. }
  197. }
  198. catch (Exception e)
  199. {
  200. m_log.Error(e.Message, e);
  201. return null;
  202. }
  203. }
  204. /// <summary>
  205. /// Returns a sim profile from it's UUID
  206. /// </summary>
  207. /// <param name="uuid">The region UUID</param>
  208. /// <returns>The sim profile</returns>
  209. override public RegionProfileData GetProfileByUUID(UUID uuid)
  210. {
  211. try
  212. {
  213. Dictionary<string, object> param = new Dictionary<string, object>();
  214. param["?uuid"] = uuid.ToString();
  215. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  216. {
  217. dbcon.Open();
  218. using (IDbCommand result = m_database.Query(dbcon, "SELECT * FROM regions WHERE uuid = ?uuid", param))
  219. {
  220. using (IDataReader reader = result.ExecuteReader())
  221. {
  222. RegionProfileData row = m_database.readSimRow(reader);
  223. return row;
  224. }
  225. }
  226. }
  227. }
  228. catch (Exception e)
  229. {
  230. m_log.Error(e.Message, e);
  231. return null;
  232. }
  233. }
  234. /// <summary>
  235. /// Returns a sim profile from it's Region name string
  236. /// </summary>
  237. /// <returns>The sim profile</returns>
  238. override public RegionProfileData GetProfileByString(string regionName)
  239. {
  240. if (regionName.Length > 2)
  241. {
  242. try
  243. {
  244. Dictionary<string, object> param = new Dictionary<string, object>();
  245. // Add % because this is a like query.
  246. param["?regionName"] = regionName + "%";
  247. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  248. {
  249. dbcon.Open();
  250. // Order by statement will return shorter matches first. Only returns one record or no record.
  251. using (IDbCommand result = m_database.Query(dbcon,
  252. "SELECT * FROM regions WHERE regionName like ?regionName order by LENGTH(regionName) asc LIMIT 1",
  253. param))
  254. {
  255. using (IDataReader reader = result.ExecuteReader())
  256. {
  257. RegionProfileData row = m_database.readSimRow(reader);
  258. return row;
  259. }
  260. }
  261. }
  262. }
  263. catch (Exception e)
  264. {
  265. m_log.Error(e.Message, e);
  266. return null;
  267. }
  268. }
  269. m_log.Error("[GRID DB]: Searched for a Region Name shorter then 3 characters");
  270. return null;
  271. }
  272. /// <summary>
  273. /// Adds a new profile to the database
  274. /// </summary>
  275. /// <param name="profile">The profile to add</param>
  276. /// <returns>Successful?</returns>
  277. override public DataResponse StoreProfile(RegionProfileData profile)
  278. {
  279. try
  280. {
  281. if (m_database.insertRegion(profile))
  282. return DataResponse.RESPONSE_OK;
  283. else
  284. return DataResponse.RESPONSE_ERROR;
  285. }
  286. catch
  287. {
  288. return DataResponse.RESPONSE_ERROR;
  289. }
  290. }
  291. /// <summary>
  292. /// Deletes a sim profile from the database
  293. /// </summary>
  294. /// <param name="uuid">the sim UUID</param>
  295. /// <returns>Successful?</returns>
  296. //public DataResponse DeleteProfile(RegionProfileData profile)
  297. override public DataResponse DeleteProfile(string uuid)
  298. {
  299. try
  300. {
  301. if (m_database.deleteRegion(uuid))
  302. return DataResponse.RESPONSE_OK;
  303. else
  304. return DataResponse.RESPONSE_ERROR;
  305. }
  306. catch
  307. {
  308. return DataResponse.RESPONSE_ERROR;
  309. }
  310. }
  311. /// <summary>
  312. /// DEPRECATED. Attempts to authenticate a region by comparing a shared secret.
  313. /// </summary>
  314. /// <param name="uuid">The UUID of the challenger</param>
  315. /// <param name="handle">The attempted regionHandle of the challenger</param>
  316. /// <param name="authkey">The secret</param>
  317. /// <returns>Whether the secret and regionhandle match the database entry for UUID</returns>
  318. override public bool AuthenticateSim(UUID uuid, ulong handle, string authkey)
  319. {
  320. bool throwHissyFit = false; // Should be true by 1.0
  321. if (throwHissyFit)
  322. throw new Exception("CRYPTOWEAK AUTHENTICATE: Refusing to authenticate due to replay potential.");
  323. RegionProfileData data = GetProfileByUUID(uuid);
  324. return (handle == data.regionHandle && authkey == data.regionSecret);
  325. }
  326. /// <summary>
  327. /// NOT YET FUNCTIONAL. Provides a cryptographic authentication of a region
  328. /// </summary>
  329. /// <remarks>This requires a security audit.</remarks>
  330. /// <param name="uuid"></param>
  331. /// <param name="handle"></param>
  332. /// <param name="authhash"></param>
  333. /// <param name="challenge"></param>
  334. /// <returns></returns>
  335. public bool AuthenticateSim(UUID uuid, ulong handle, string authhash, string challenge)
  336. {
  337. // SHA512Managed HashProvider = new SHA512Managed();
  338. // Encoding TextProvider = new UTF8Encoding();
  339. // byte[] stream = TextProvider.GetBytes(uuid.ToString() + ":" + handle.ToString() + ":" + challenge);
  340. // byte[] hash = HashProvider.ComputeHash(stream);
  341. return false;
  342. }
  343. /// <summary>
  344. /// Adds a location reservation
  345. /// </summary>
  346. /// <param name="x">x coordinate</param>
  347. /// <param name="y">y coordinate</param>
  348. /// <returns></returns>
  349. override public ReservationData GetReservationAtPoint(uint x, uint y)
  350. {
  351. try
  352. {
  353. Dictionary<string, object> param = new Dictionary<string, object>();
  354. param["?x"] = x.ToString();
  355. param["?y"] = y.ToString();
  356. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  357. {
  358. dbcon.Open();
  359. using (IDbCommand result = m_database.Query(dbcon,
  360. "SELECT * FROM reservations WHERE resXMin <= ?x AND resXMax >= ?x AND resYMin <= ?y AND resYMax >= ?y",
  361. param))
  362. {
  363. using (IDataReader reader = result.ExecuteReader())
  364. {
  365. ReservationData row = m_database.readReservationRow(reader);
  366. return row;
  367. }
  368. }
  369. }
  370. }
  371. catch (Exception e)
  372. {
  373. m_log.Error(e.Message, e);
  374. return null;
  375. }
  376. }
  377. }
  378. }