SQLiteGridData.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using System.Security.Cryptography;
  32. using System.Text;
  33. using libsecondlife;
  34. namespace OpenSim.Framework.Data.SQLite
  35. {
  36. /// <summary>
  37. /// A Grid Interface to the SQLite database
  38. /// </summary>
  39. public class SQLiteGridData : IGridData
  40. {
  41. /// <summary>
  42. /// A database manager
  43. /// </summary>
  44. private SQLiteManager database;
  45. /// <summary>
  46. /// Initialises the Grid Interface
  47. /// </summary>
  48. 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. 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. 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. 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. 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. 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 UUID
  105. /// </summary>
  106. /// <param name="uuid">The region UUID</param>
  107. /// <returns>The sim profile</returns>
  108. public RegionProfileData GetProfileByLLUUID(LLUUID uuid)
  109. {
  110. Dictionary<string, string> param = new Dictionary<string, string>();
  111. param["uuid"] = uuid.ToString();
  112. IDbCommand result = database.Query("SELECT * FROM regions WHERE uuid = @uuid", param);
  113. IDataReader reader = result.ExecuteReader();
  114. RegionProfileData row = database.getRow(reader);
  115. reader.Close();
  116. result.Dispose();
  117. return row;
  118. }
  119. /// <summary>
  120. /// // Returns a list of avatar and UUIDs that match the query
  121. /// </summary>
  122. public List<AvatarPickerAvatar> GeneratePickerResults(LLUUID queryID, string query)
  123. {
  124. //Do nothing yet
  125. List<AvatarPickerAvatar> returnlist = new List<AvatarPickerAvatar>();
  126. return returnlist;
  127. }
  128. /// <summary>
  129. /// Adds a new specified region to the database
  130. /// </summary>
  131. /// <param name="profile">The profile to add</param>
  132. /// <returns>A dataresponse enum indicating success</returns>
  133. public DataResponse AddProfile(RegionProfileData profile)
  134. {
  135. if (database.insertRow(profile))
  136. {
  137. return DataResponse.RESPONSE_OK;
  138. }
  139. else
  140. {
  141. return DataResponse.RESPONSE_ERROR;
  142. }
  143. }
  144. /// <summary>
  145. /// DEPRECIATED. Attempts to authenticate a region by comparing a shared secret.
  146. /// </summary>
  147. /// <param name="uuid">The UUID of the challenger</param>
  148. /// <param name="handle">The attempted regionHandle of the challenger</param>
  149. /// <param name="authkey">The secret</param>
  150. /// <returns>Whether the secret and regionhandle match the database entry for UUID</returns>
  151. public bool AuthenticateSim(LLUUID uuid, ulong handle, string authkey)
  152. {
  153. bool throwHissyFit = false; // Should be true by 1.0
  154. if (throwHissyFit)
  155. throw new Exception("CRYPTOWEAK AUTHENTICATE: Refusing to authenticate due to replay potential.");
  156. RegionProfileData data = GetProfileByLLUUID(uuid);
  157. return (handle == data.regionHandle && authkey == data.regionSecret);
  158. }
  159. /// <summary>
  160. /// NOT YET FUNCTIONAL. Provides a cryptographic authentication of a region
  161. /// </summary>
  162. /// <remarks>This requires a security audit.</remarks>
  163. /// <param name="uuid"></param>
  164. /// <param name="handle"></param>
  165. /// <param name="authhash"></param>
  166. /// <param name="challenge"></param>
  167. /// <returns></returns>
  168. public bool AuthenticateSim(LLUUID uuid, ulong handle, string authhash, string challenge)
  169. {
  170. SHA512Managed HashProvider = new SHA512Managed();
  171. ASCIIEncoding TextProvider = new ASCIIEncoding();
  172. byte[] stream = TextProvider.GetBytes(uuid.ToString() + ":" + handle.ToString() + ":" + challenge);
  173. byte[] hash = HashProvider.ComputeHash(stream);
  174. return false;
  175. }
  176. public ReservationData GetReservationAtPoint(uint x, uint y)
  177. {
  178. return null;
  179. }
  180. }
  181. }