MySQLGridData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using System.Security.Cryptography;
  32. using System.Text;
  33. using libsecondlife;
  34. namespace OpenSim.Framework.Data.MySQL
  35. {
  36. /// <summary>
  37. /// A MySQL Interface for the Grid Server
  38. /// </summary>
  39. public class MySQLGridData : IGridData
  40. {
  41. /// <summary>
  42. /// MySQL Database Manager
  43. /// </summary>
  44. private MySQLManager database;
  45. /// <summary>
  46. /// Initialises the Grid Interface
  47. /// </summary>
  48. public void Initialise()
  49. {
  50. IniFile GridDataMySqlFile = new IniFile("mysql_connection.ini");
  51. string settingHostname = GridDataMySqlFile.ParseFileReadValue("hostname");
  52. string settingDatabase = GridDataMySqlFile.ParseFileReadValue("database");
  53. string settingUsername = GridDataMySqlFile.ParseFileReadValue("username");
  54. string settingPassword = GridDataMySqlFile.ParseFileReadValue("password");
  55. string settingPooling = GridDataMySqlFile.ParseFileReadValue("pooling");
  56. string settingPort = GridDataMySqlFile.ParseFileReadValue("port");
  57. database = new MySQLManager(settingHostname, settingDatabase, settingUsername, settingPassword, settingPooling, settingPort);
  58. }
  59. /// <summary>
  60. /// Shuts down the grid interface
  61. /// </summary>
  62. public void Close()
  63. {
  64. database.Close();
  65. }
  66. /// <summary>
  67. /// Returns the plugin name
  68. /// </summary>
  69. /// <returns>Plugin name</returns>
  70. public string getName()
  71. {
  72. return "MySql OpenGridData";
  73. }
  74. /// <summary>
  75. /// Returns the plugin version
  76. /// </summary>
  77. /// <returns>Plugin version</returns>
  78. public string getVersion()
  79. {
  80. return "0.1";
  81. }
  82. /// <summary>
  83. /// Returns all the specified region profiles within coordates -- coordinates are inclusive
  84. /// </summary>
  85. /// <param name="xmin">Minimum X coordinate</param>
  86. /// <param name="ymin">Minimum Y coordinate</param>
  87. /// <param name="xmax">Maximum X coordinate</param>
  88. /// <param name="ymax">Maximum Y coordinate</param>
  89. /// <returns></returns>
  90. public SimProfileData[] GetProfilesInRange(uint xmin, uint ymin, uint xmax, uint ymax)
  91. {
  92. try
  93. {
  94. lock (database)
  95. {
  96. Dictionary<string, string> param = new Dictionary<string, string>();
  97. param["?xmin"] = xmin.ToString();
  98. param["?ymin"] = ymin.ToString();
  99. param["?xmax"] = xmax.ToString();
  100. param["?ymax"] = ymax.ToString();
  101. IDbCommand result = database.Query("SELECT * FROM regions WHERE locX >= ?xmin AND locX <= ?xmax AND locY >= ?ymin AND locY <= ?ymax", param);
  102. IDataReader reader = result.ExecuteReader();
  103. SimProfileData row;
  104. List<SimProfileData> rows = new List<SimProfileData>();
  105. while ((row = database.readSimRow(reader)) != null)
  106. {
  107. rows.Add(row);
  108. }
  109. reader.Close();
  110. result.Dispose();
  111. return rows.ToArray();
  112. }
  113. }
  114. catch (Exception e)
  115. {
  116. database.Reconnect();
  117. Console.WriteLine(e.ToString());
  118. return null;
  119. }
  120. }
  121. /// <summary>
  122. /// Returns a sim profile from it's location
  123. /// </summary>
  124. /// <param name="handle">Region location handle</param>
  125. /// <returns>Sim profile</returns>
  126. public SimProfileData GetProfileByHandle(ulong handle)
  127. {
  128. try
  129. {
  130. lock (database)
  131. {
  132. Dictionary<string, string> param = new Dictionary<string, string>();
  133. param["?handle"] = handle.ToString();
  134. IDbCommand result = database.Query("SELECT * FROM regions WHERE regionHandle = ?handle", param);
  135. IDataReader reader = result.ExecuteReader();
  136. SimProfileData row = database.readSimRow(reader);
  137. reader.Close();
  138. result.Dispose();
  139. return row;
  140. }
  141. }
  142. catch (Exception e)
  143. {
  144. database.Reconnect();
  145. Console.WriteLine(e.ToString());
  146. return null;
  147. }
  148. }
  149. /// <summary>
  150. /// Returns a sim profile from it's UUID
  151. /// </summary>
  152. /// <param name="uuid">The region UUID</param>
  153. /// <returns>The sim profile</returns>
  154. public SimProfileData GetProfileByLLUUID(LLUUID uuid)
  155. {
  156. try
  157. {
  158. lock (database)
  159. {
  160. Dictionary<string, string> param = new Dictionary<string, string>();
  161. param["?uuid"] = uuid.ToStringHyphenated();
  162. IDbCommand result = database.Query("SELECT * FROM regions WHERE uuid = ?uuid", param);
  163. IDataReader reader = result.ExecuteReader();
  164. SimProfileData row = database.readSimRow(reader);
  165. reader.Close();
  166. result.Dispose();
  167. return row;
  168. }
  169. }
  170. catch (Exception e)
  171. {
  172. database.Reconnect();
  173. Console.WriteLine(e.ToString());
  174. return null;
  175. }
  176. }
  177. /// <summary>
  178. /// Adds a new profile to the database
  179. /// </summary>
  180. /// <param name="profile">The profile to add</param>
  181. /// <returns>Successful?</returns>
  182. public DataResponse AddProfile(SimProfileData profile)
  183. {
  184. lock (database)
  185. {
  186. if (database.insertRegion(profile))
  187. {
  188. return DataResponse.RESPONSE_OK;
  189. }
  190. else
  191. {
  192. return DataResponse.RESPONSE_ERROR;
  193. }
  194. }
  195. }
  196. /// <summary>
  197. /// DEPRECIATED. Attempts to authenticate a region by comparing a shared secret.
  198. /// </summary>
  199. /// <param name="uuid">The UUID of the challenger</param>
  200. /// <param name="handle">The attempted regionHandle of the challenger</param>
  201. /// <param name="authkey">The secret</param>
  202. /// <returns>Whether the secret and regionhandle match the database entry for UUID</returns>
  203. public bool AuthenticateSim(LLUUID uuid, ulong handle, string authkey)
  204. {
  205. bool throwHissyFit = false; // Should be true by 1.0
  206. if (throwHissyFit)
  207. throw new Exception("CRYPTOWEAK AUTHENTICATE: Refusing to authenticate due to replay potential.");
  208. SimProfileData data = GetProfileByLLUUID(uuid);
  209. return (handle == data.regionHandle && authkey == data.regionSecret);
  210. }
  211. /// <summary>
  212. /// NOT YET FUNCTIONAL. Provides a cryptographic authentication of a region
  213. /// </summary>
  214. /// <remarks>This requires a security audit.</remarks>
  215. /// <param name="uuid"></param>
  216. /// <param name="handle"></param>
  217. /// <param name="authhash"></param>
  218. /// <param name="challenge"></param>
  219. /// <returns></returns>
  220. public bool AuthenticateSim(LLUUID uuid, ulong handle, string authhash, string challenge)
  221. {
  222. SHA512Managed HashProvider = new SHA512Managed();
  223. ASCIIEncoding TextProvider = new ASCIIEncoding();
  224. byte[] stream = TextProvider.GetBytes(uuid.ToStringHyphenated() + ":" + handle.ToString() + ":" + challenge);
  225. byte[] hash = HashProvider.ComputeHash(stream);
  226. return false;
  227. }
  228. public ReservationData GetReservationAtPoint(uint x, uint y)
  229. {
  230. try
  231. {
  232. lock (database)
  233. {
  234. Dictionary<string, string> param = new Dictionary<string, string>();
  235. param["?x"] = x.ToString();
  236. param["?y"] = y.ToString();
  237. IDbCommand result = database.Query("SELECT * FROM reservations WHERE resXMin <= ?x AND resXMax >= ?x AND resYMin <= ?y AND resYMax >= ?y", param);
  238. IDataReader reader = result.ExecuteReader();
  239. ReservationData row = database.readReservationRow(reader);
  240. reader.Close();
  241. result.Dispose();
  242. return row;
  243. }
  244. }
  245. catch (Exception e)
  246. {
  247. database.Reconnect();
  248. Console.WriteLine(e.ToString());
  249. return null;
  250. }
  251. }
  252. }
  253. }