SQLiteGridData.cs 9.5 KB

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