SQLiteGridData.cs 8.9 KB

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