MySQLGridData.cs 9.9 KB

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