SQLiteGridData.cs 10.0 KB

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