SQLiteGridData.cs 11 KB

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