1
0

SQLiteGridData.cs 11 KB

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