MySQLGridData.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 OpenSim 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.Security.Cryptography;
  32. using System.Text;
  33. using libsecondlife;
  34. using log4net;
  35. namespace OpenSim.Data.MySQL
  36. {
  37. /// <summary>
  38. /// A MySQL Interface for the Grid Server
  39. /// </summary>
  40. public class MySQLGridData : GridDataBase
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. /// <summary>
  44. /// MySQL Database Manager
  45. /// </summary>
  46. private MySQLManager database;
  47. /// <summary>
  48. /// Initialises the Grid Interface
  49. /// </summary>
  50. override public void Initialise()
  51. {
  52. IniFile GridDataMySqlFile = new IniFile("mysql_connection.ini");
  53. string settingHostname = GridDataMySqlFile.ParseFileReadValue("hostname");
  54. string settingDatabase = GridDataMySqlFile.ParseFileReadValue("database");
  55. string settingUsername = GridDataMySqlFile.ParseFileReadValue("username");
  56. string settingPassword = GridDataMySqlFile.ParseFileReadValue("password");
  57. string settingPooling = GridDataMySqlFile.ParseFileReadValue("pooling");
  58. string settingPort = GridDataMySqlFile.ParseFileReadValue("port");
  59. database =
  60. new MySQLManager(settingHostname, settingDatabase, settingUsername, settingPassword, settingPooling,
  61. settingPort);
  62. TestTables();
  63. }
  64. #region Test and initialization code
  65. /// <summary>
  66. /// Ensure that the user related tables exists and are at the latest version
  67. /// </summary>
  68. private void TestTables()
  69. {
  70. Dictionary<string, string> tableList = new Dictionary<string, string>();
  71. tableList["regions"] = null;
  72. database.GetTableVersion(tableList);
  73. UpgradeRegionsTable(tableList["regions"]);
  74. }
  75. /// <summary>
  76. /// Create or upgrade the table if necessary
  77. /// </summary>
  78. /// <param name="oldVersion">A null indicates that the table does not
  79. /// currently exist</param>
  80. private void UpgradeRegionsTable(string oldVersion)
  81. {
  82. // null as the version, indicates that the table didn't exist
  83. if (oldVersion == null)
  84. {
  85. database.ExecuteResourceSql("CreateRegionsTable.sql");
  86. return;
  87. }
  88. if (oldVersion.Contains("Rev. 1"))
  89. {
  90. database.ExecuteResourceSql("UpgradeRegionsTableToVersion2.sql");
  91. return;
  92. }
  93. if (oldVersion.Contains("Rev. 2"))
  94. {
  95. database.ExecuteResourceSql("UpgradeRegionsTableToVersion3.sql");
  96. return;
  97. }
  98. }
  99. #endregion
  100. /// <summary>
  101. /// Shuts down the grid interface
  102. /// </summary>
  103. override public void Close()
  104. {
  105. database.Close();
  106. }
  107. /// <summary>
  108. /// Returns the plugin name
  109. /// </summary>
  110. /// <returns>Plugin name</returns>
  111. override public string getName()
  112. {
  113. return "MySql OpenGridData";
  114. }
  115. /// <summary>
  116. /// Returns the plugin version
  117. /// </summary>
  118. /// <returns>Plugin version</returns>
  119. override public string getVersion()
  120. {
  121. return "0.1";
  122. }
  123. /// <summary>
  124. /// Returns all the specified region profiles within coordates -- coordinates are inclusive
  125. /// </summary>
  126. /// <param name="xmin">Minimum X coordinate</param>
  127. /// <param name="ymin">Minimum Y coordinate</param>
  128. /// <param name="xmax">Maximum X coordinate</param>
  129. /// <param name="ymax">Maximum Y coordinate</param>
  130. /// <returns></returns>
  131. override public RegionProfileData[] GetProfilesInRange(uint xmin, uint ymin, uint xmax, uint ymax)
  132. {
  133. try
  134. {
  135. lock (database)
  136. {
  137. Dictionary<string, string> param = new Dictionary<string, string>();
  138. param["?xmin"] = xmin.ToString();
  139. param["?ymin"] = ymin.ToString();
  140. param["?xmax"] = xmax.ToString();
  141. param["?ymax"] = ymax.ToString();
  142. IDbCommand result =
  143. database.Query(
  144. "SELECT * FROM regions WHERE locX >= ?xmin AND locX <= ?xmax AND locY >= ?ymin AND locY <= ?ymax",
  145. param);
  146. IDataReader reader = result.ExecuteReader();
  147. RegionProfileData row;
  148. List<RegionProfileData> rows = new List<RegionProfileData>();
  149. while ((row = database.readSimRow(reader)) != null)
  150. {
  151. rows.Add(row);
  152. }
  153. reader.Close();
  154. result.Dispose();
  155. return rows.ToArray();
  156. }
  157. }
  158. catch (Exception e)
  159. {
  160. database.Reconnect();
  161. m_log.Error(e.ToString());
  162. return null;
  163. }
  164. }
  165. /// <summary>
  166. /// Returns a sim profile from it's location
  167. /// </summary>
  168. /// <param name="handle">Region location handle</param>
  169. /// <returns>Sim profile</returns>
  170. override public RegionProfileData GetProfileByHandle(ulong handle)
  171. {
  172. try
  173. {
  174. lock (database)
  175. {
  176. Dictionary<string, string> param = new Dictionary<string, string>();
  177. param["?handle"] = handle.ToString();
  178. IDbCommand result = database.Query("SELECT * FROM regions WHERE regionHandle = ?handle", param);
  179. IDataReader reader = result.ExecuteReader();
  180. RegionProfileData row = database.readSimRow(reader);
  181. reader.Close();
  182. result.Dispose();
  183. return row;
  184. }
  185. }
  186. catch (Exception e)
  187. {
  188. database.Reconnect();
  189. m_log.Error(e.ToString());
  190. return null;
  191. }
  192. }
  193. /// <summary>
  194. /// Returns a sim profile from it's UUID
  195. /// </summary>
  196. /// <param name="uuid">The region UUID</param>
  197. /// <returns>The sim profile</returns>
  198. override public RegionProfileData GetProfileByLLUUID(LLUUID uuid)
  199. {
  200. try
  201. {
  202. lock (database)
  203. {
  204. Dictionary<string, string> param = new Dictionary<string, string>();
  205. param["?uuid"] = uuid.ToString();
  206. IDbCommand result = database.Query("SELECT * FROM regions WHERE uuid = ?uuid", param);
  207. IDataReader reader = result.ExecuteReader();
  208. RegionProfileData row = database.readSimRow(reader);
  209. reader.Close();
  210. result.Dispose();
  211. return row;
  212. }
  213. }
  214. catch (Exception e)
  215. {
  216. database.Reconnect();
  217. m_log.Error(e.ToString());
  218. return null;
  219. }
  220. }
  221. /// <summary>
  222. /// Returns a sim profile from it's Region name string
  223. /// </summary>
  224. /// <param name="uuid">The region name search query</param>
  225. /// <returns>The sim profile</returns>
  226. override public RegionProfileData GetProfileByString(string regionName)
  227. {
  228. if (regionName.Length > 2)
  229. {
  230. try
  231. {
  232. lock (database)
  233. {
  234. Dictionary<string, string> param = new Dictionary<string, string>();
  235. // Add % because this is a like query.
  236. param["?regionName"] = regionName + "%";
  237. // Order by statement will return shorter matches first. Only returns one record or no record.
  238. IDbCommand result = database.Query("SELECT * FROM regions WHERE regionName like ?regionName order by LENGTH(regionName) asc LIMIT 1", param);
  239. IDataReader reader = result.ExecuteReader();
  240. RegionProfileData row = database.readSimRow(reader);
  241. reader.Close();
  242. result.Dispose();
  243. return row;
  244. }
  245. }
  246. catch (Exception e)
  247. {
  248. database.Reconnect();
  249. m_log.Error(e.ToString());
  250. return null;
  251. }
  252. }
  253. else
  254. {
  255. m_log.Error("[GRID DB]: Searched for a Region Name shorter then 3 characters");
  256. return null;
  257. }
  258. }
  259. /// <summary>
  260. /// Adds a new profile to the database
  261. /// </summary>
  262. /// <param name="profile">The profile to add</param>
  263. /// <returns>Successful?</returns>
  264. override public DataResponse AddProfile(RegionProfileData profile)
  265. {
  266. lock (database)
  267. {
  268. if (database.insertRegion(profile))
  269. {
  270. return DataResponse.RESPONSE_OK;
  271. }
  272. else
  273. {
  274. return DataResponse.RESPONSE_ERROR;
  275. }
  276. }
  277. }
  278. override public DataResponse UpdateProfile(RegionProfileData profile)
  279. {
  280. return AddProfile(profile);
  281. }
  282. /// <summary>
  283. /// Deletes a profile from the database
  284. /// </summary>
  285. /// <param name="profile">The profile to delete</param>
  286. /// <returns>Successful?</returns>
  287. //public DataResponse DeleteProfile(RegionProfileData profile)
  288. public DataResponse DeleteProfile(string uuid)
  289. {
  290. lock (database)
  291. {
  292. if (database.deleteRegion(uuid))
  293. {
  294. return DataResponse.RESPONSE_OK;
  295. }
  296. else
  297. {
  298. return DataResponse.RESPONSE_ERROR;
  299. }
  300. }
  301. }
  302. /// <summary>
  303. /// DEPRECATED. Attempts to authenticate a region by comparing a shared secret.
  304. /// </summary>
  305. /// <param name="uuid">The UUID of the challenger</param>
  306. /// <param name="handle">The attempted regionHandle of the challenger</param>
  307. /// <param name="authkey">The secret</param>
  308. /// <returns>Whether the secret and regionhandle match the database entry for UUID</returns>
  309. override public bool AuthenticateSim(LLUUID uuid, ulong handle, string authkey)
  310. {
  311. bool throwHissyFit = false; // Should be true by 1.0
  312. if (throwHissyFit)
  313. throw new Exception("CRYPTOWEAK AUTHENTICATE: Refusing to authenticate due to replay potential.");
  314. RegionProfileData data = GetProfileByLLUUID(uuid);
  315. return (handle == data.regionHandle && authkey == data.regionSecret);
  316. }
  317. /// <summary>
  318. /// NOT YET FUNCTIONAL. Provides a cryptographic authentication of a region
  319. /// </summary>
  320. /// <remarks>This requires a security audit.</remarks>
  321. /// <param name="uuid"></param>
  322. /// <param name="handle"></param>
  323. /// <param name="authhash"></param>
  324. /// <param name="challenge"></param>
  325. /// <returns></returns>
  326. public bool AuthenticateSim(LLUUID uuid, ulong handle, string authhash, string challenge)
  327. {
  328. SHA512Managed HashProvider = new SHA512Managed();
  329. ASCIIEncoding TextProvider = new ASCIIEncoding();
  330. byte[] stream = TextProvider.GetBytes(uuid.ToString() + ":" + handle.ToString() + ":" + challenge);
  331. byte[] hash = HashProvider.ComputeHash(stream);
  332. return false;
  333. }
  334. override public ReservationData GetReservationAtPoint(uint x, uint y)
  335. {
  336. try
  337. {
  338. lock (database)
  339. {
  340. Dictionary<string, string> param = new Dictionary<string, string>();
  341. param["?x"] = x.ToString();
  342. param["?y"] = y.ToString();
  343. IDbCommand result =
  344. database.Query(
  345. "SELECT * FROM reservations WHERE resXMin <= ?x AND resXMax >= ?x AND resYMin <= ?y AND resYMax >= ?y",
  346. param);
  347. IDataReader reader = result.ExecuteReader();
  348. ReservationData row = database.readReservationRow(reader);
  349. reader.Close();
  350. result.Dispose();
  351. return row;
  352. }
  353. }
  354. catch (Exception e)
  355. {
  356. database.Reconnect();
  357. m_log.Error(e.ToString());
  358. return null;
  359. }
  360. }
  361. }
  362. }