SQLiteGridData.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.Security.Cryptography;
  31. using System.Text;
  32. using libsecondlife;
  33. namespace OpenSim.Framework.Data.SQLite
  34. {
  35. /// <summary>
  36. /// A Grid Interface to the SQLite database
  37. /// </summary>
  38. public class SQLiteGridData : GridDataBase
  39. {
  40. /// <summary>
  41. /// A database manager
  42. /// </summary>
  43. private SQLiteManager database;
  44. /// <summary>
  45. /// Initialises the Grid Interface
  46. /// </summary>
  47. override public void Initialise()
  48. {
  49. database = new SQLiteManager("localhost", "db", "user", "password", "false");
  50. }
  51. /// <summary>
  52. /// Shuts down the grid interface
  53. /// </summary>
  54. override public void Close()
  55. {
  56. database.Close();
  57. }
  58. /// <summary>
  59. /// Returns the name of this grid interface
  60. /// </summary>
  61. /// <returns>A string containing the grid interface</returns>
  62. override public string getName()
  63. {
  64. return "SQLite OpenGridData";
  65. }
  66. /// <summary>
  67. /// Returns the version of this grid interface
  68. /// </summary>
  69. /// <returns>A string containing the version</returns>
  70. override public string getVersion()
  71. {
  72. return "0.1";
  73. }
  74. /// <summary>
  75. /// Returns a list of regions within the specified ranges
  76. /// </summary>
  77. /// <param name="a">minimum X coordinate</param>
  78. /// <param name="b">minimum Y coordinate</param>
  79. /// <param name="c">maximum X coordinate</param>
  80. /// <param name="d">maximum Y coordinate</param>
  81. /// <returns>An array of region profiles</returns>
  82. override public RegionProfileData[] GetProfilesInRange(uint a, uint b, uint c, uint d)
  83. {
  84. return null;
  85. }
  86. /// <summary>
  87. /// Returns a sim profile from it's location
  88. /// </summary>
  89. /// <param name="handle">Region location handle</param>
  90. /// <returns>Sim profile</returns>
  91. override public RegionProfileData GetProfileByHandle(ulong handle)
  92. {
  93. Dictionary<string, string> param = new Dictionary<string, string>();
  94. param["handle"] = handle.ToString();
  95. IDbCommand result = database.Query("SELECT * FROM regions WHERE handle = @handle", param);
  96. IDataReader reader = result.ExecuteReader();
  97. RegionProfileData row = database.getRow(reader);
  98. reader.Close();
  99. result.Dispose();
  100. return row;
  101. }
  102. /// <summary>
  103. /// Returns a sim profile from it's Region name string
  104. /// </summary>
  105. /// <param name="regionName">The region name search query</param>
  106. /// <returns>The sim profile</returns>
  107. override public RegionProfileData GetProfileByString(string regionName)
  108. {
  109. if (regionName.Length > 2)
  110. {
  111. Dictionary<string, string> param = new Dictionary<string, string>();
  112. // Add % because this is a like query.
  113. param["?regionName"] = regionName + "%";
  114. // Only returns one record or no record.
  115. IDbCommand result = database.Query("SELECT * FROM regions WHERE regionName like ?regionName LIMIT 1", param);
  116. IDataReader reader = result.ExecuteReader();
  117. RegionProfileData row = database.getRow(reader);
  118. reader.Close();
  119. result.Dispose();
  120. return row;
  121. }
  122. else
  123. {
  124. //m_log.Error("[DATABASE]: Searched for a Region Name shorter then 3 characters");
  125. return null;
  126. }
  127. }
  128. /// <summary>
  129. /// Returns a sim profile from it's UUID
  130. /// </summary>
  131. /// <param name="uuid">The region UUID</param>
  132. /// <returns>The sim profile</returns>
  133. override public RegionProfileData GetProfileByLLUUID(LLUUID uuid)
  134. {
  135. Dictionary<string, string> param = new Dictionary<string, string>();
  136. param["uuid"] = uuid.ToString();
  137. IDbCommand result = database.Query("SELECT * FROM regions WHERE uuid = @uuid", param);
  138. IDataReader reader = result.ExecuteReader();
  139. RegionProfileData row = database.getRow(reader);
  140. reader.Close();
  141. result.Dispose();
  142. return row;
  143. }
  144. /// <summary>
  145. /// // Returns a list of avatar and UUIDs that match the query
  146. /// </summary>
  147. public List<AvatarPickerAvatar> GeneratePickerResults(LLUUID queryID, string query)
  148. {
  149. //Do nothing yet
  150. List<AvatarPickerAvatar> returnlist = new List<AvatarPickerAvatar>();
  151. return returnlist;
  152. }
  153. /// <summary>
  154. /// Adds a new specified region to the database
  155. /// </summary>
  156. /// <param name="profile">The profile to add</param>
  157. /// <returns>A dataresponse enum indicating success</returns>
  158. override public DataResponse AddProfile(RegionProfileData profile)
  159. {
  160. if (database.insertRow(profile))
  161. {
  162. return DataResponse.RESPONSE_OK;
  163. }
  164. else
  165. {
  166. return DataResponse.RESPONSE_ERROR;
  167. }
  168. }
  169. /// <summary>
  170. /// DEPRECATED. Attempts to authenticate a region by comparing a shared secret.
  171. /// </summary>
  172. /// <param name="uuid">The UUID of the challenger</param>
  173. /// <param name="handle">The attempted regionHandle of the challenger</param>
  174. /// <param name="authkey">The secret</param>
  175. /// <returns>Whether the secret and regionhandle match the database entry for UUID</returns>
  176. override public bool AuthenticateSim(LLUUID uuid, ulong handle, string authkey)
  177. {
  178. bool throwHissyFit = false; // Should be true by 1.0
  179. if (throwHissyFit)
  180. throw new Exception("CRYPTOWEAK AUTHENTICATE: Refusing to authenticate due to replay potential.");
  181. RegionProfileData data = GetProfileByLLUUID(uuid);
  182. return (handle == data.regionHandle && authkey == data.regionSecret);
  183. }
  184. /// <summary>
  185. /// NOT YET FUNCTIONAL. Provides a cryptographic authentication of a region
  186. /// </summary>
  187. /// <remarks>This requires a security audit.</remarks>
  188. /// <param name="uuid"></param>
  189. /// <param name="handle"></param>
  190. /// <param name="authhash"></param>
  191. /// <param name="challenge"></param>
  192. /// <returns></returns>
  193. public bool AuthenticateSim(LLUUID uuid, ulong handle, string authhash, string challenge)
  194. {
  195. SHA512Managed HashProvider = new SHA512Managed();
  196. ASCIIEncoding TextProvider = new ASCIIEncoding();
  197. byte[] stream = TextProvider.GetBytes(uuid.ToString() + ":" + handle.ToString() + ":" + challenge);
  198. byte[] hash = HashProvider.ComputeHash(stream);
  199. return false;
  200. }
  201. override public ReservationData GetReservationAtPoint(uint x, uint y)
  202. {
  203. return null;
  204. }
  205. }
  206. }